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
use std;
use capi;
use std::os::raw::{c_char, c_void};
use std::ffi::{CStr, CString};
use std::ptr::{null, null_mut};
use std::borrow::Cow;
use error::PAErr;
pub use capi::pa_prop_type_t as PropType;
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub enum Encoding {
Any,
PCM,
AC3_IEC61937,
EAC3_IEC61937,
MPEG_IEC61937,
DTS_IEC61937,
MPEG2_AAC_IEC61937,
Invalid = -1,
}
impl From<Encoding> for capi::pa_encoding_t {
fn from(e: Encoding) -> Self {
unsafe { std::mem::transmute(e) }
}
}
impl From<capi::pa_encoding_t> for Encoding {
fn from(e: capi::pa_encoding_t) -> Self {
unsafe { std::mem::transmute(e) }
}
}
impl Default for Encoding {
fn default() -> Self {
Encoding::Invalid
}
}
pub struct Info {
pub(crate) ptr: *mut InfoInternal,
properties: ::proplist::Proplist,
weak: bool,
}
#[repr(C)]
pub(crate) struct InfoInternal {
pub encoding: Encoding,
pub list: *mut ::proplist::ProplistInternal,
}
impl From<capi::pa_format_info> for InfoInternal {
fn from(i: capi::pa_format_info) -> Self {
unsafe { std::mem::transmute(i) }
}
}
impl From<InfoInternal> for capi::pa_format_info {
fn from(i: InfoInternal) -> Self {
unsafe { std::mem::transmute(i) }
}
}
impl std::fmt::Debug for Info {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Info {{ encoding: {:?}, properties: {:?} }}", self.get_encoding(),
*self.get_properties())
}
}
impl Encoding {
pub fn to_string(e: Self) -> Option<Cow<'static, str>> {
let ptr = unsafe { capi::pa_encoding_to_string(e.into()) };
if ptr.is_null() {
return None;
}
Some(unsafe { CStr::from_ptr(ptr).to_string_lossy() })
}
#[cfg(feature = "pa_encoding_from_string")]
pub fn from_string(encoding: &str) -> Self {
let c_enc = CString::new(encoding.clone()).unwrap();
unsafe { capi::pa_encoding_from_string(c_enc.as_ptr()).into() }
}
}
impl Info {
pub fn new() -> Option<Self> {
let ptr = unsafe { capi::pa_format_info_new() };
if ptr.is_null() {
return None;
}
Some(Self::from_raw(unsafe { std::mem::transmute(ptr) }))
}
pub fn new_from_string(s: &str) -> Option<Self> {
let c_str = CString::new(s.clone()).unwrap();
let ptr = unsafe { capi::pa_format_info_from_string(c_str.as_ptr()) };
if ptr.is_null() {
return None;
}
Some(Self::from_raw(unsafe { std::mem::transmute(ptr) }))
}
pub fn new_from_sample_spec(ss: &::sample::Spec, map: Option<&::channelmap::Map>
) -> Option<Self>
{
let p_map: *const capi::pa_channel_map = match map {
Some(map) => unsafe { std::mem::transmute(map) },
None => null::<capi::pa_channel_map>(),
};
let ptr = unsafe { capi::pa_format_info_from_sample_spec(std::mem::transmute(ss), p_map) };
if ptr.is_null() {
return None;
}
Some(Self::from_raw(unsafe { std::mem::transmute(ptr) }))
}
pub(crate) fn from_raw(ptr: *mut InfoInternal) -> Self {
assert_eq!(false, ptr.is_null());
unsafe {
Self {
ptr: ptr,
properties: ::proplist::Proplist::from_raw_weak((*ptr).list),
weak: false,
}
}
}
pub(crate) fn from_raw_weak(ptr: *mut InfoInternal) -> Self {
assert_eq!(false, ptr.is_null());
unsafe {
Self {
ptr: ptr,
properties: ::proplist::Proplist::from_raw_weak((*ptr).list),
weak: true,
}
}
}
pub fn is_valid(&self) -> bool {
unsafe { capi::pa_format_info_valid(std::mem::transmute(&self.ptr)) != 0 }
}
pub fn is_pcm(&self) -> bool {
unsafe { capi::pa_format_info_is_pcm(std::mem::transmute(&self.ptr)) != 0 }
}
pub fn is_compatible_with(&self, with: &Self) -> bool {
unsafe { capi::pa_format_info_is_compatible(std::mem::transmute(&self.ptr),
std::mem::transmute(&with.ptr)) != 0 }
}
pub fn print(&self) -> String {
const PRINT_MAX: usize = capi::PA_FORMAT_INFO_SNPRINT_MAX;
let mut tmp = Vec::with_capacity(PRINT_MAX);
unsafe {
capi::pa_format_info_snprint(tmp.as_mut_ptr(), PRINT_MAX,
std::mem::transmute(&self.ptr));
CStr::from_ptr(tmp.as_mut_ptr()).to_string_lossy().into_owned()
}
}
pub fn to_sample_spec(&self, ss: &mut ::sample::Spec, map: &mut ::channelmap::Map
) -> Result<(), PAErr>
{
match unsafe { capi::pa_format_info_to_sample_spec(std::mem::transmute(&self.ptr),
std::mem::transmute(ss), std::mem::transmute(map)) }
{
0 => Ok(()),
e => Err(PAErr(e)),
}
}
pub fn get_encoding(&self) -> Encoding {
unsafe { (*self.ptr).encoding }
}
pub fn set_encoding(&mut self, encoding: Encoding) {
unsafe { (*self.ptr).encoding = encoding };
}
pub fn get_properties(&self) -> &::proplist::Proplist {
&self.properties
}
pub fn get_properties_mut(&mut self) -> &mut ::proplist::Proplist {
&mut self.properties
}
pub fn get_prop_type(&self, key: &str) -> PropType {
let c_key = CString::new(key.clone()).unwrap();
unsafe { capi::pa_format_info_get_prop_type(std::mem::transmute(&self.ptr), c_key.as_ptr()) }
}
pub fn get_prop_int(&self, key: &str) -> Result<i32, PAErr> {
let mut i: i32 = 0;
let c_key = CString::new(key.clone()).unwrap();
match unsafe { capi::pa_format_info_get_prop_int(std::mem::transmute(&self.ptr),
c_key.as_ptr(), &mut i) }
{
0 => Ok(i),
e => Err(PAErr(e)),
}
}
pub fn get_prop_int_range(&self, key: &str) -> Result<(i32, i32), PAErr> {
let mut min: i32 = 0;
let mut max: i32 = 0;
let c_key = CString::new(key.clone()).unwrap();
match unsafe { capi::pa_format_info_get_prop_int_range(std::mem::transmute(&self.ptr),
c_key.as_ptr(), &mut min, &mut max) }
{
0 => Ok((min, max)),
e => Err(PAErr(e)),
}
}
pub fn get_prop_int_array(&self, key: &str) -> Option<Vec<i32>> {
let c_key = CString::new(key.clone()).unwrap();
let mut count: i32 = 0;
let mut p_ints = null_mut::<i32>();
let result = unsafe { capi::pa_format_info_get_prop_int_array(
std::mem::transmute(&self.ptr), c_key.as_ptr(), &mut p_ints, &mut count) };
if result != 0 {
return None;
}
let mut values: Vec<i32> = Vec::with_capacity(count as usize);
for i in 0..count {
values.push(unsafe { *p_ints.offset(i as isize) });
}
unsafe { capi::pa_xfree(p_ints as *mut c_void) };
Some(values)
}
pub fn get_prop_string(&self, key: &str) -> Option<String> {
let c_key = CString::new(key.clone()).unwrap();
let mut p_str = null_mut::<c_char>();
let result = unsafe { capi::pa_format_info_get_prop_string(std::mem::transmute(&self.ptr),
c_key.as_ptr(), &mut p_str) };
if result != 0 || p_str.is_null() {
return None;
}
unsafe {
let ret = Some(CStr::from_ptr(p_str).to_string_lossy().into_owned());
capi::pa_xfree(p_str as *mut c_void);
ret
}
}
pub fn get_prop_string_array(&self, key: &str) -> Option<Vec<String>> {
let c_key = CString::new(key.clone()).unwrap();
let mut count: i32 = 0;
let mut pp_str = null_mut::<*mut c_char>();
let result = unsafe { capi::pa_format_info_get_prop_string_array(
std::mem::transmute(&self.ptr), c_key.as_ptr(), &mut pp_str, &mut count) };
if result != 0 || pp_str.is_null() {
return None;
}
let mut values: Vec<String> = Vec::with_capacity(count as usize);
for i in 0..count {
let p_str = unsafe { *pp_str.offset(i as isize) };
if !p_str.is_null() {
values.push(unsafe { CStr::from_ptr(p_str).to_string_lossy().into_owned() });
}
}
unsafe { capi::pa_format_info_free_string_array(pp_str, count) };
Some(values)
}
pub fn set_prop_int(&mut self, key: &str, value: i32) {
let c_key = CString::new(key.clone()).unwrap();
unsafe { capi::pa_format_info_set_prop_int(std::mem::transmute(&self.ptr), c_key.as_ptr(),
value); }
}
pub fn set_prop_int_array(&mut self, key: &str, values: &[i32]) {
let c_key = CString::new(key.clone()).unwrap();
unsafe { capi::pa_format_info_set_prop_int_array(std::mem::transmute(&self.ptr),
c_key.as_ptr(), values.as_ptr(), values.len() as i32); }
}
pub fn set_prop_int_range(&mut self, key: &str, min: i32, max: i32) {
let c_key = CString::new(key.clone()).unwrap();
unsafe { capi::pa_format_info_set_prop_int_range(std::mem::transmute(&self.ptr),
c_key.as_ptr(), min, max); }
}
pub fn set_prop_string(&mut self, key: &str, value: &str) {
let c_key = CString::new(key.clone()).unwrap();
let c_value = CString::new(value.clone()).unwrap();
unsafe { capi::pa_format_info_set_prop_string(std::mem::transmute(&self.ptr),
c_key.as_ptr(), c_value.as_ptr()); }
}
pub fn set_prop_string_array(&mut self, key: &str, values: &[&str]) {
let c_key = CString::new(key.clone()).unwrap();
let mut c_values: Vec<CString> = Vec::with_capacity(values.len());
for v in values {
c_values.push(CString::new(v.clone()).unwrap());
}
let mut c_value_ptrs: Vec<*const c_char> = Vec::with_capacity(c_values.len());
for v in c_values {
c_value_ptrs.push(v.as_ptr());
}
unsafe {
capi::pa_format_info_set_prop_string_array(std::mem::transmute(&self.ptr),
c_key.as_ptr(), c_value_ptrs.as_ptr(), c_value_ptrs.len() as i32);
}
}
pub fn set_sample_format(&mut self, sf: ::sample::Format) {
unsafe { capi::pa_format_info_set_sample_format(std::mem::transmute(&self.ptr),
sf.into()); }
}
pub fn set_rate(&mut self, rate: i32) {
unsafe { capi::pa_format_info_set_rate(std::mem::transmute(&self.ptr), rate) }
}
pub fn set_channels(&mut self, channels: u32) {
debug_assert!(channels <= std::i32::MAX as u32);
unsafe { capi::pa_format_info_set_channels(std::mem::transmute(&self.ptr), channels as i32) }
}
pub fn set_channel_map(&mut self, map: &::channelmap::Map) {
unsafe { capi::pa_format_info_set_channel_map(std::mem::transmute(&self.ptr),
std::mem::transmute(map)) }
}
}
impl Drop for Info {
fn drop(&mut self) {
if !self.weak {
unsafe { capi::pa_format_info_free(std::mem::transmute(&self.ptr)) };
}
}
}
impl Clone for Info {
fn clone(&self) -> Self {
let ptr = unsafe { capi::pa_format_info_copy(std::mem::transmute(&self.ptr)) };
assert_eq!(false, ptr.is_null());
Self::from_raw(unsafe { std::mem::transmute(ptr) })
}
}