1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use self::super::libc_sys::timeval;
use std::os::raw::{
    c_void,
    c_uint,
    c_char,
    c_int,
 };
 
pub type pa_io_event_flags = c_uint; 
pub use self::pa_io_event_flags as pa_io_event_flags_t;
pub type pa_context_flags = c_uint;
pub use self::pa_context_flags as pa_context_flags_t;
pub type pa_context_state = c_uint;
pub use self::pa_context_state as pa_context_state_t;
pub type pa_source_state = c_int;
pub use self::pa_source_state as pa_source_state_t;
pub type pa_sample_format = c_int;
pub use self::pa_sample_format as pa_sample_format_t;
pub type pa_channel_map_def = c_uint;
pub use self::pa_channel_map_def as pa_channel_map_def_t;
pub type pa_channel_position = c_int;
pub use self::pa_channel_position as pa_channel_position_t;
pub type pa_volume_t = u32;
pub type pa_usec_t = u64;
pub type pa_sink_flags = c_uint;
pub use self::pa_sink_flags as pa_sink_flags_t;
pub type pa_sink_state = c_int;
pub use self::pa_sink_state as pa_sink_state_t;
pub type pa_encoding = c_int;
pub use self::pa_encoding as pa_encoding_t;
pub type pa_source_flags = c_uint;
pub use self::pa_source_flags as pa_source_flags_t;
pub type pa_operation_state = c_uint;
pub use self::pa_operation_state as pa_operation_state_t;
pub type pa_subscription_event_type = c_uint;
pub use self::pa_subscription_event_type as pa_subscription_event_type_t;
pub type pa_stream_state = c_uint;
pub use self::pa_stream_state as pa_stream_state_t;
pub type pa_seek_mode = c_uint;
pub use self::pa_seek_mode as pa_seek_mode_t;
pub type pa_stream_flags = c_uint;
pub use self::pa_stream_flags as pa_stream_flags_t;

pub const PA_OPERATION_RUNNING: pa_operation_state = 0;
pub const PA_OPERATION_DONE: pa_operation_state = 1;
pub const PA_OPERATION_CANCELLED: pa_operation_state = 2;

pub const PA_CONTEXT_UNCONNECTED: pa_context_state = 0;
pub const PA_CONTEXT_CONNECTING: pa_context_state = 1;
pub const PA_CONTEXT_AUTHORIZING: pa_context_state = 2;
pub const PA_CONTEXT_SETTING_NAME: pa_context_state = 3;
pub const PA_CONTEXT_READY: pa_context_state = 4;
pub const PA_CONTEXT_FAILED: pa_context_state = 5;
pub const PA_CONTEXT_TERMINATED: pa_context_state = 6;

pub const PA_SAMPLE_FLOAT32LE: pa_sample_format = 5;

pub const PA_CHANNEL_POSITION_INVALID: pa_channel_position = -1;
pub const PA_CHANNEL_POSITION_LEFT: pa_channel_position = 1;
pub const PA_CHANNEL_POSITION_RIGHT: pa_channel_position = 2;

pub const PA_STREAM_UNCONNECTED: pa_stream_state = 0;
pub const PA_STREAM_CREATING: pa_stream_state = 1;
pub const PA_STREAM_READY: pa_stream_state = 2;
pub const PA_STREAM_FAILED: pa_stream_state = 3;
pub const PA_STREAM_TERMINATED: pa_stream_state = 4;

pub const PA_SEEK_RELATIVE: pa_seek_mode = 0;
pub const PA_SEEK_RELATIVE_ON_READ: pa_seek_mode = 2;

pub const PA_STREAM_START_CORKED: pa_stream_flags = 1;
pub const PA_STREAM_INTERPOLATE_TIMING: pa_stream_flags = 2;
pub const PA_STREAM_AUTO_TIMING_UPDATE: pa_stream_flags = 8;
pub const PA_STREAM_ADJUST_LATENCY: pa_stream_flags = 8192;
pub const PA_STREAM_START_UNMUTED: pa_stream_flags = 65536;

pub const PA_CHANNEL_MAP_DEFAULT: pa_channel_map_def = 0;


#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_time_event {
    _unused: [u8; 0],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_defer_event {
    _unused: [u8; 0],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_context {
    _unused: [u8; 0],
}

pub type pa_context_notify_cb_t = ::std::option::Option<
unsafe extern "C" fn(c: *mut pa_context, userdata: *mut c_void),
>;

pub type pa_defer_event_destroy_cb_t = ::std::option::Option<
unsafe extern "C" fn(
    a: *mut pa_mainloop_api,
    e: *mut pa_defer_event,
    userdata: *mut c_void,
),
>;

pub type pa_defer_event_cb_t = ::std::option::Option<
unsafe extern "C" fn(
    a: *mut pa_mainloop_api,
    e: *mut pa_defer_event,
    userdata: *mut c_void,
),
>;

pub type pa_time_event_destroy_cb_t = ::std::option::Option<
unsafe extern "C" fn(
    a: *mut pa_mainloop_api,
    e: *mut pa_time_event,
    userdata: *mut c_void,
),
>;

pub type pa_time_event_cb_t = ::std::option::Option<
unsafe extern "C" fn(
    a: *mut pa_mainloop_api,
    e: *mut pa_time_event,
    tv: *const timeval,
    userdata: *mut c_void,
),
>;

pub type pa_io_event_cb_t = ::std::option::Option<
unsafe extern "C" fn(
    ea: *mut pa_mainloop_api,
    e: *mut pa_io_event,
    fd: c_int,
    events: pa_io_event_flags_t,
    userdata: *mut c_void,
),
>;

pub type pa_io_event_destroy_cb_t = ::std::option::Option<
unsafe extern "C" fn(
    a: *mut pa_mainloop_api,
    e: *mut pa_io_event,
    userdata: *mut c_void,
),
>;

pub type pa_sink_info_cb_t = ::std::option::Option<
unsafe extern "C" fn(
    c: *mut pa_context,
    i: *const pa_sink_info,
    eol: c_int,
    userdata: *mut c_void,
),
>;

pub type pa_source_info_cb_t = ::std::option::Option<
    unsafe extern "C" fn(
        c: *mut pa_context,
        i: *const pa_source_info,
        eol: c_int,
        userdata: *mut c_void,
    ),
>;

pub type pa_context_subscribe_cb_t = ::std::option::Option<
    unsafe extern "C" fn(
        c: *mut pa_context,
        t: pa_subscription_event_type_t,
        idx: u32,
        userdata: *mut c_void,
    ),
>;

pub type pa_server_info_cb_t = ::std::option::Option<
    unsafe extern "C" fn(
        c: *mut pa_context,
        i: *const pa_server_info,
        userdata: *mut c_void,
    ),
>;

pub type pa_stream_notify_cb_t = ::std::option::Option<
    unsafe extern "C" fn(p: *mut pa_stream, userdata: *mut c_void),
>;

pub type pa_stream_success_cb_t = ::std::option::Option<
    unsafe extern "C" fn(
        s: *mut pa_stream,
        success: c_int,
        userdata: *mut c_void,
    ),
>;

pub type pa_stream_request_cb_t = ::std::option::Option<
    unsafe extern "C" fn(p: *mut pa_stream, nbytes: usize, userdata: *mut c_void),
>;

pub type pa_free_cb_t = ::std::option::Option<unsafe extern "C" fn(p: *mut ::std::os::raw::c_void)>;

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_cvolume {
    pub channels: u8,
    pub values: [pa_volume_t; 32usize],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_channel_map {
    pub channels: u8,
    pub map: [pa_channel_position_t; 32usize],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_sample_spec {
    pub format: pa_sample_format_t,
    pub rate: u32,
    pub channels: u8,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_proplist {
    _unused: [u8; 0],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_format_info {
    pub encoding: pa_encoding_t,
    pub plist: *mut pa_proplist,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_sink_port_info {
    pub name: *const c_char,
    pub description: *const c_char,
    pub priority: u32,
    pub available: c_int,
    pub availability_group: *const c_char,
    pub type_: u32,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_sink_info {
    pub name: *const c_char,
    pub index: u32,
    pub description: *const c_char,
    pub sample_spec: pa_sample_spec,
    pub channel_map: pa_channel_map,
    pub owner_module: u32,
    pub volume: pa_cvolume,
    pub mute: c_int,
    pub monitor_source: u32,
    pub monitor_source_name: *const c_char,
    pub latency: pa_usec_t,
    pub driver: *const c_char,
    pub flags: pa_sink_flags_t,
    pub proplist: *mut pa_proplist,
    pub configured_latency: pa_usec_t,
    pub base_volume: pa_volume_t,
    pub state: pa_sink_state_t,
    pub n_volume_steps: u32,
    pub card: u32,
    pub n_ports: u32,
    pub ports: *mut *mut pa_sink_port_info,
    pub active_port: *mut pa_sink_port_info,
    pub n_formats: u8,
    pub formats: *mut *mut pa_format_info,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_source_info {
    pub name: *const c_char,
    pub index: u32,
    pub description: *const c_char,
    pub sample_spec: pa_sample_spec,
    pub channel_map: pa_channel_map,
    pub owner_module: u32,
    pub volume: pa_cvolume,
    pub mute: c_int,
    pub monitor_of_sink: u32,
    pub monitor_of_sink_name: *const c_char,
    pub latency: pa_usec_t,
    pub driver: *const c_char,
    pub flags: pa_source_flags_t,
    pub proplist: *mut pa_proplist,
    pub configured_latency: pa_usec_t,
    pub base_volume: pa_volume_t,
    pub state: pa_source_state_t,
    pub n_volume_steps: u32,
    pub card: u32,
    pub n_ports: u32,
    pub ports: *mut *mut pa_source_port_info,
    pub active_port: *mut pa_source_port_info,
    pub n_formats: u8,
    pub formats: *mut *mut pa_format_info,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_source_port_info {
    pub name: *const c_char,
    pub description: *const c_char,
    pub priority: u32,
    pub available: c_int,
    pub availability_group: *const c_char,
    pub type_: u32,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_io_event {
    _unused: [u8; 0],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_threaded_mainloop {
    _unused: [u8; 0],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_spawn_api {
    pub prefork: ::std::option::Option<unsafe extern "C" fn()>,
    pub postfork: ::std::option::Option<unsafe extern "C" fn()>,
    pub atfork: ::std::option::Option<unsafe extern "C" fn()>,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_operation {
    _unused: [u8; 0],
}



#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_server_info {
    pub user_name: *const ::std::os::raw::c_char,
    pub host_name: *const ::std::os::raw::c_char,
    pub server_version: *const ::std::os::raw::c_char,
    pub server_name: *const ::std::os::raw::c_char,
    pub sample_spec: pa_sample_spec,
    pub default_sink_name: *const ::std::os::raw::c_char,
    pub default_source_name: *const ::std::os::raw::c_char,
    pub cookie: u32,
    pub channel_map: pa_channel_map,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_stream {
    _unused: [u8; 0],
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_buffer_attr {
    pub maxlength: u32,
    pub tlength: u32,
    pub prebuf: u32,
    pub minreq: u32,
    pub fragsize: u32,
}

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct pa_mainloop_api {
    pub userdata: *mut c_void,
    pub io_new: ::std::option::Option<
    unsafe extern "C" fn(
        a: *mut pa_mainloop_api,
        fd: c_int,
        events: pa_io_event_flags_t,
        cb: pa_io_event_cb_t,
        userdata: *mut c_void,
    ) -> *mut pa_io_event,
    >,
    pub io_enable: ::std::option::Option<
    unsafe extern "C" fn(e: *mut pa_io_event, events: pa_io_event_flags_t),
    >,
    pub io_free: ::std::option::Option<unsafe extern "C" fn(e: *mut pa_io_event)>,
    pub io_set_destroy: ::std::option::Option<
    unsafe extern "C" fn(e: *mut pa_io_event, cb: pa_io_event_destroy_cb_t),
    >,
    pub time_new: ::std::option::Option<
    unsafe extern "C" fn(
        a: *mut pa_mainloop_api,
        tv: *const timeval,
        cb: pa_time_event_cb_t,
        userdata: *mut c_void,
    ) -> *mut pa_time_event,
    >,
    pub time_restart:
    ::std::option::Option<unsafe extern "C" fn(e: *mut pa_time_event, tv: *const timeval)>,
    pub time_free: ::std::option::Option<unsafe extern "C" fn(e: *mut pa_time_event)>,
    pub time_set_destroy: ::std::option::Option<
    unsafe extern "C" fn(e: *mut pa_time_event, cb: pa_time_event_destroy_cb_t),
    >,
    pub defer_new: ::std::option::Option<
    unsafe extern "C" fn(
        a: *mut pa_mainloop_api,
        cb: pa_defer_event_cb_t,
        userdata: *mut c_void,
    ) -> *mut pa_defer_event,
    >,
    pub defer_enable: ::std::option::Option<
    unsafe extern "C" fn(e: *mut pa_defer_event, b: c_int),
    >,
    pub defer_free: ::std::option::Option<unsafe extern "C" fn(e: *mut pa_defer_event)>,
    pub defer_set_destroy: ::std::option::Option<
    unsafe extern "C" fn(e: *mut pa_defer_event, cb: pa_defer_event_destroy_cb_t),
    >,
    pub quit: ::std::option::Option<
    unsafe extern "C" fn(a: *mut pa_mainloop_api, retval: c_int),
    >,
}


#[link(name = "pulse")]
extern "C" {
    pub fn pa_context_connect(
        c: *mut pa_context,
        server: *const c_char,
        flags: pa_context_flags_t,
        api: *const pa_spawn_api,
    ) -> c_int;

    pub fn pa_context_new(
        mainloop: *mut pa_mainloop_api,
        name: *const c_char,
    ) -> *mut pa_context;
    
    pub fn pa_context_set_state_callback(
        c: *mut pa_context,
        cb: pa_context_notify_cb_t,
        userdata: *mut c_void,
    );
    pub fn pa_context_get_state(c: *const pa_context) -> pa_context_state_t;

    pub fn pa_context_disconnect(c: *mut pa_context);
    
    pub fn pa_context_unref(c: *mut pa_context);
    
    pub fn pa_context_get_sink_info_list(
        c: *mut pa_context,
        cb: pa_sink_info_cb_t,
        userdata: *mut c_void,
    ) -> *mut pa_operation;
    
    pub fn pa_operation_get_state(o: *const pa_operation) -> pa_operation_state_t;
    pub fn pa_operation_unref(o: *mut pa_operation);
    
    pub fn pa_context_get_source_info_list(
        c: *mut pa_context,
        cb: pa_source_info_cb_t,
        userdata: *mut c_void,
    ) -> *mut pa_operation;

    pub fn pa_threaded_mainloop_get_api(m: *mut pa_threaded_mainloop) -> *mut pa_mainloop_api;

    pub fn pa_threaded_mainloop_new() -> *mut pa_threaded_mainloop;

    pub fn pa_threaded_mainloop_signal(
        m: *mut pa_threaded_mainloop,
        wait_for_accept:c_int,
    );

    pub fn pa_threaded_mainloop_start(m: *mut pa_threaded_mainloop) -> c_int;
    pub fn pa_threaded_mainloop_lock(m: *mut pa_threaded_mainloop);
    pub fn pa_threaded_mainloop_unlock(m: *mut pa_threaded_mainloop);
    pub fn pa_threaded_mainloop_wait(m: *mut pa_threaded_mainloop);
    pub fn pa_threaded_mainloop_stop(m: *mut pa_threaded_mainloop);
    pub fn pa_threaded_mainloop_free(m: *mut pa_threaded_mainloop);
    pub fn pa_context_get_server_info(
        c: *mut pa_context,
        cb: pa_server_info_cb_t,
        userdata: *mut c_void,
    ) -> *mut pa_operation;
    pub fn pa_proplist_new() -> *mut pa_proplist;
    pub fn pa_context_new_with_proplist(
        mainloop: *mut pa_mainloop_api,
        name: *const u8,
        proplist: *const pa_proplist,
    ) -> *mut pa_context;

    pub fn pa_stream_new(
        c: *mut pa_context,
        name: *const u8,
        ss: *const pa_sample_spec,
        map: *const pa_channel_map,
    ) -> *mut pa_stream;

    pub fn pa_stream_set_state_callback(
        s: *mut pa_stream,
        cb: pa_stream_notify_cb_t,
        userdata: *mut c_void,
    );

    pub fn pa_stream_get_state(p: *const pa_stream) -> pa_stream_state_t;

   pub fn pa_stream_cork(
        s: *mut pa_stream,
        b: c_int,
        cb: pa_stream_success_cb_t,
        userdata: *mut c_void,
    ) -> *mut pa_operation;
    
    pub fn pa_stream_set_write_callback(
        p: *mut pa_stream,
        cb: pa_stream_request_cb_t,
        userdata: *mut ::std::os::raw::c_void,
    );
    pub fn pa_stream_disconnect(s: *mut pa_stream) -> ::std::os::raw::c_int;
    pub fn pa_stream_unref(s: *mut pa_stream);
    pub fn pa_stream_begin_write(
        p: *mut pa_stream,
        data: *mut *mut c_void,
        nbytes: *mut usize,
    ) -> c_int;
    pub fn pa_stream_write(
        p: *mut pa_stream,
        data: *const c_void,
        nbytes: usize,
        free_cb: pa_free_cb_t,
        offset: i64,
        seek: pa_seek_mode_t,
    ) -> c_int;

    pub fn pa_stream_connect_playback(
        s: *mut pa_stream,
        dev: *const u8,
        attr: *const pa_buffer_attr,
        flags: pa_stream_flags_t,
        volume: *const pa_cvolume,
        sync_stream: *mut pa_stream,
    ) -> c_int;
    pub fn pa_stream_writable_size(p: *const pa_stream) -> usize;
    
    pub fn pa_stream_set_read_callback(
        p: *mut pa_stream,
        cb: pa_stream_request_cb_t,
        userdata: *mut c_void,
    );

    pub fn pa_stream_connect_record(
        s: *mut pa_stream,
        dev: *const u8,
        attr: *const pa_buffer_attr,
        flags: pa_stream_flags_t,
    ) -> c_int;
    
    pub fn pa_stream_peek(
        p: *mut pa_stream,
        data: *mut *const c_void,
        nbytes: *mut usize,
    ) -> c_int;

    pub fn pa_stream_drop(p: *mut pa_stream) -> c_int;

}