cidre 0.11.5

Apple frameworks bindings for rust
Documentation
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
use crate::{
    at::{
        au::{self, Scope, Unit, UnitRef},
        audio::{
            self,
            component::{InitializedState, State, UninitializedState},
        },
    },
    os,
};

#[cfg(all(target_os = "macos", feature = "core_audio"))]
use crate::core_audio::Device;

#[derive(Debug)]
pub struct Output<S>(UnitRef<S>)
where
    S: State<Unit>;

impl<S> Output<S>
where
    S: State<Unit>,
{
    #[inline]
    pub fn unit(&self) -> &Unit {
        self.0.unit()
    }

    #[inline]
    pub fn unit_mut(&mut self) -> &mut Unit {
        self.0.unit_mut()
    }

    #[inline]
    pub fn last_render_err(&self) -> os::Result<os::Status> {
        self.unit().last_render_err()
    }

    #[inline]
    pub fn set_input_cb<const N: usize, T>(
        &mut self,
        cb: au::RenderCb<N, T>,
        ref_con: *const T,
    ) -> os::Result {
        self.0
            .unit_mut()
            .output_set_input_cb(au::Scope::GLOBAL, 1, cb, ref_con)
    }

    #[inline]
    pub fn set_output_cb<const N: usize, T>(
        &mut self,
        cb: au::RenderCb<N, T>,
        ref_con: *const T,
    ) -> os::Result {
        self.0
            .unit_mut()
            .set_input_cb(au::Scope::GLOBAL, 0, cb, ref_con)
    }

    pub fn set_render_cb<const N: usize, T>(
        &mut self,
        scope: au::Scope,
        bus: u32,
        cb: au::RenderCb<N, T>,
        ref_con: *const T,
    ) -> os::Result {
        self.0.unit_mut().set_render_cb(scope, bus, cb, ref_con)
    }

    #[inline]
    pub fn output_stream_format(&self, bus: u32) -> os::Result<audio::StreamBasicDesc> {
        self.unit().stream_format(Scope::INPUT, bus)
    }

    #[inline]
    pub fn input_stream_format(&self, bus: u32) -> os::Result<audio::StreamBasicDesc> {
        self.unit().stream_format(Scope::OUTPUT, bus)
    }

    pub fn is_running(&self) -> os::Result<bool> {
        let res: u32 = self.unit().prop(
            au::PropId::OUTPUT_IS_RUNNING,
            au::Scope::GLOBAL,
            au::Element(0),
        )?;
        Ok(res != 0)
    }

    pub fn is_io_enabled(&self, scope: au::Scope, bus: u32) -> os::Result<bool> {
        let res: u32 = self
            .unit()
            .prop(au::PropId::OUTPUT_ENABLE_IO, scope, au::Element(bus))?;
        Ok(res != 0)
    }

    pub fn set_io_enabled(&mut self, scope: au::Scope, bus: u32, val: bool) -> os::Result {
        let val = val as u32;
        self.unit_mut()
            .set_prop(au::PropId::OUTPUT_ENABLE_IO, scope, au::Element(bus), &val)
    }

    pub fn has_output_io(&self) -> os::Result<bool> {
        let res: u32 =
            self.unit()
                .prop(au::PropId::OUTPUT_HAS_IO, au::Scope::OUTPUT, au::Element(0))?;
        Ok(res != 0)
    }

    pub fn has_input_io(&self) -> os::Result<bool> {
        let res: u32 =
            self.unit()
                .prop(au::PropId::OUTPUT_HAS_IO, au::Scope::INPUT, au::Element(1))?;
        Ok(res != 0)
    }

    pub fn set_should_allocate_input_buf(&mut self, arg: bool) -> os::Result {
        let arg: u32 = arg as u32;
        self.unit_mut().set_prop(
            au::PropId::SHOULD_ALLOCATE_BUF,
            au::Scope::OUTPUT,
            au::Element(1),
            &arg,
        )
    }

    pub fn set_should_allocate_output_buf(&mut self, arg: bool) -> os::Result {
        let arg: u32 = arg as u32;
        self.unit_mut().set_prop(
            au::PropId::SHOULD_ALLOCATE_BUF,
            au::Scope::INPUT,
            au::Element(0),
            &arg,
        )
    }

    pub fn vp_mute_output(&mut self) -> os::Result<bool> {
        let val: u32 = self.unit().prop(
            au::PropId::VOICE_IO_MUTE_OUTPUT,
            au::Scope::GLOBAL,
            au::Element(1),
        )?;
        Ok(val == 1)
    }

    pub fn vp_set_mute_output(&mut self, arg: bool) -> os::Result {
        let arg: u32 = arg as u32;
        self.unit_mut().set_prop(
            au::PropId::VOICE_IO_MUTE_OUTPUT,
            au::Scope::GLOBAL,
            au::Element(1),
            &arg,
        )
    }

    pub fn vp_enable_agc(&mut self) -> os::Result<bool> {
        let val: u32 = self.unit().prop(
            au::PropId::VOICE_IO_ENABLE_AGC,
            au::Scope::GLOBAL,
            au::Element(1),
        )?;
        Ok(val == 1)
    }

    pub fn vp_set_enable_agc(&mut self, arg: bool) -> os::Result {
        let arg: u32 = arg as u32;
        self.unit_mut().set_prop(
            au::PropId::VOICE_IO_ENABLE_AGC,
            au::Scope::GLOBAL,
            au::Element(1),
            &arg,
        )
    }
    pub fn vp_bypass_voice_processing(&mut self) -> os::Result<bool> {
        let val: u32 = self.unit().prop(
            au::PropId::VOICE_IO_BYPASS_VOICE_PROCESSING,
            au::Scope::GLOBAL,
            au::Element(1),
        )?;
        Ok(val == 1)
    }

    pub fn vp_set_bypass_voice_processing(&mut self, arg: bool) -> os::Result {
        let arg: u32 = arg as u32;
        self.unit_mut().set_prop(
            au::PropId::VOICE_IO_BYPASS_VOICE_PROCESSING,
            au::Scope::GLOBAL,
            au::Element(1),
            &arg,
        )
    }
}

impl Output<UninitializedState> {
    pub fn new_apple() -> os::Result<Self> {
        let desc = audio::ComponentDesc {
            type_: au::Type::OUTPUT.0,
            #[cfg(target_os = "macos")]
            sub_type: au::SubType::HAL_OUTPUT.0,
            #[cfg(not(target_os = "macos"))]
            sub_type: au::SubType::REMOTE_IO.0,
            manufacturer: au::Manufacturer::APPLE.0,
            flags: 0,
            flags_mask: 0,
        };

        let comp = desc
            .into_iter()
            .next()
            .ok_or(au::component_err::UNSUPPORTED_TYPE)?;
        let unit = comp.open_unit()?;
        Ok(Self(unit))
    }

    /// Apple voice processing unit
    ///
    /// Note: It is reported that the echo source need to be specified as the output device.
    /// If no output device is specified, MacOS would take the default output device as echo source.
    /// See [mumble-voip](https://github.com/mumble-voip/mumble/blob/70dea6077854d79d4cb60b3ed1e8f1f8f78963a5/src/mumble/CoreAudio.mm#L614)
    pub fn new_apple_vp() -> os::Result<Self> {
        let desc = audio::ComponentDesc {
            type_: au::Type::OUTPUT.0,
            sub_type: au::SubType::VOICE_PROCESSING_IO.0,
            manufacturer: au::Manufacturer::APPLE.0,
            flags: 0,
            flags_mask: 0,
        };

        let comp = desc
            .into_iter()
            .next()
            .ok_or(au::component_err::UNSUPPORTED_TYPE)?;
        let unit = comp.open_unit()?;
        Ok(Self(unit))
    }

    #[doc(alias = "AudioUnitInitialize")]
    pub fn allocate_resources(self) -> os::Result<Output<InitializedState>> {
        Ok(Output(self.0.initialize()?))
    }

    pub fn start_ts_at_zero(&self) -> os::Result<bool> {
        let res: u32 = self.unit().prop(
            au::PropId::OUTPUT_START_TS_AT_ZERO,
            au::Scope::GLOBAL,
            au::Element(0),
        )?;
        Ok(res != 0)
    }

    pub fn set_start_ts_at_zero(&mut self, val: bool) -> os::Result {
        let val = val as u32;
        self.unit_mut().set_prop(
            au::PropId::OUTPUT_START_TS_AT_ZERO,
            au::Scope::GLOBAL,
            au::Element(0),
            &val,
        )
    }

    #[inline]
    pub fn set_output_stream_format(&mut self, val: &audio::StreamBasicDesc) -> os::Result {
        self.unit_mut().set_stream_format(Scope::INPUT, 0, val)
    }

    #[inline]
    pub fn set_input_stream_format(&mut self, val: &audio::StreamBasicDesc) -> os::Result {
        self.unit_mut().set_stream_format(Scope::OUTPUT, 1, val)
    }

    #[cfg(all(target_os = "macos", feature = "core_audio"))]
    #[inline]
    pub fn output_device(&self) -> os::Result<Device> {
        self.unit().prop(
            au::PropId::OUTPUT_CURRENT_DEVICE,
            Scope::GLOBAL,
            au::Element(0),
        )
    }

    /// Set device after enable IO
    #[cfg(all(target_os = "macos", feature = "core_audio"))]
    #[inline]
    pub fn set_output_device(&mut self, val: &Device) -> os::Result {
        self.unit_mut().set_prop(
            au::PropId::OUTPUT_CURRENT_DEVICE,
            Scope::GLOBAL,
            au::Element(0),
            val,
        )
    }

    #[cfg(all(target_os = "macos", feature = "core_audio"))]
    #[inline]
    pub fn input_device(&self) -> os::Result<Device> {
        self.unit().prop(
            au::PropId::OUTPUT_CURRENT_DEVICE,
            Scope::GLOBAL,
            au::Element(1),
        )
    }

    /// Set device after enable IO
    #[cfg(all(target_os = "macos", feature = "core_audio"))]
    #[inline]
    pub fn set_input_device(&mut self, val: &Device) -> os::Result {
        self.unit_mut().set_prop(
            au::PropId::OUTPUT_CURRENT_DEVICE,
            Scope::GLOBAL,
            au::Element(1),
            val,
        )
    }
}

impl Output<InitializedState> {
    #[cfg(all(target_os = "macos", feature = "core_audio"))]
    #[inline]
    pub fn input_device(&self) -> os::Result<Device> {
        self.unit().prop(
            au::PropId::OUTPUT_CURRENT_DEVICE,
            Scope::GLOBAL,
            au::Element(1),
        )
    }

    #[inline]
    pub fn deallocate_resources(self) -> os::Result<Output<UninitializedState>> {
        Ok(Output(self.0.unintialize()?))
    }

    #[inline]
    pub fn render<const N: usize>(
        &mut self,
        n_frames: u32,
        buf_list: &mut audio::BufList<N>,
        bus: u32,
    ) -> os::Result {
        let ts = audio::TimeStamp::invalid();
        self.0.render(&ts, bus, n_frames, buf_list)
    }

    #[doc(alias = "AudioOutputUnitStart")]
    #[inline]
    pub fn start(&mut self) -> os::Result {
        unsafe { AudioOutputUnitStart(self.unit_mut()).result() }
    }

    #[doc(alias = "AudioOutputUnitStop")]
    #[inline]
    pub fn stop(&mut self) -> os::Result {
        unsafe { AudioOutputUnitStop(self.unit_mut()).result() }
    }
}

#[link(name = "AudioToolbox", kind = "framework")]
unsafe extern "C-unwind" {
    fn AudioOutputUnitStart(unit: &mut audio::Unit) -> os::Status;
    fn AudioOutputUnitStop(unit: &mut audio::Unit) -> os::Status;
}

#[cfg(all(test, target_os = "macos"))]
mod tests {

    use std::ffi::c_void;

    use crate::{
        at::{self, au},
        core_audio::{Obj, PropAddr, PropElement, PropScope, PropSelector, System},
        os,
    };
    const BUS_IN: u32 = 1;
    const BUS_OUT: u32 = 0;

    #[test]
    fn basics() {
        let mut output = au::Output::new_apple().unwrap();
        let count = output.unit().element_count(au::Scope::INPUT);
        println!("input count {count:?}");
        let count = output.unit().element_count(au::Scope::OUTPUT);
        println!("output count {count:?}");
        let format = output.input_stream_format(BUS_IN).unwrap();
        eprintln!("{format:?}");
        let format = output.output_stream_format(BUS_OUT).unwrap();
        eprintln!("{format:?}");

        assert!(!output.is_running().unwrap());
        assert!(output.start_ts_at_zero().unwrap());

        assert_eq!(
            output.is_io_enabled(au::Scope::INPUT, BUS_IN).unwrap(),
            false
        );
        // An I/O unit’s bus 1 connects to input hardware, such as for recording from a microphone.
        // Input is disabled by default. To enable input, the bus 1 input scope must be enabled,
        // as follows:
        output
            .set_io_enabled(au::Scope::INPUT, BUS_IN, true)
            .unwrap();
        output
            .set_io_enabled(au::Scope::OUTPUT, BUS_OUT, false)
            .unwrap();

        extern "C-unwind" fn input_cb(
            _in_ref_con: *mut c_void,
            _io_action_flags: &mut au::RenderActionFlags,
            _in_timestamp: &at::AudioTimeStamp,
            _in_bus_num: u32,
            _in_number_frames: u32,
            _io_data: *mut at::AudioBufList<1>,
        ) -> os::Status {
            os::Status::NO_ERR
        }

        output.set_input_cb(input_cb, std::ptr::null_mut()).unwrap();

        let device = System::default_input_device().unwrap();
        device.show();

        output.set_input_device(&device).unwrap();
        let mut output = output.allocate_resources().unwrap();

        output.start().unwrap();
        assert!(output.is_running().unwrap());
        output.stop().unwrap();

        assert_eq!(output.last_render_err().unwrap(), os::Status::NO_ERR);
    }

    #[test]
    fn voice_processing() {
        let output = au::Output::new_apple_vp().unwrap();

        let count = output.unit().element_count(au::Scope::INPUT);
        println!("input count {count:?}");
        let count = output.unit().element_count(au::Scope::OUTPUT);
        println!("output count {count:?}");
        let format = output.input_stream_format(BUS_IN).unwrap();
        eprintln!("{format:?}");

        let output_device = output.input_device().unwrap();
        let mut mute_count = 0usize;

        extern "C-unwind" fn callback(
            _obj_id: Obj,
            _number_addresses: u32,
            _addresses: *const PropAddr,
            client_data: *mut usize,
        ) -> os::Status {
            unsafe { client_data.write(1) };
            os::Status::NO_ERR
        }

        let mute_addr = PropAddr {
            selector: PropSelector::DEVICE_MUTE,
            scope: PropScope::INPUT,
            element: PropElement::MAIN,
        };

        output_device
            .add_prop_listener(&mute_addr, callback, &mut mute_count)
            .unwrap();

        output_device.set_prop(&mute_addr, &1u32).unwrap();

        let mut output = output.allocate_resources().unwrap();

        assert_eq!(true, output.vp_mute_output().unwrap());
        output.vp_set_mute_output(false).unwrap();
        assert_eq!(false, output.vp_mute_output().unwrap());

        assert_eq!(true, output.vp_enable_agc().unwrap());
        output.vp_set_enable_agc(false).unwrap();
        assert_eq!(false, output.vp_enable_agc().unwrap());

        assert_eq!(false, output.vp_bypass_voice_processing().unwrap());
        output.vp_set_bypass_voice_processing(true).unwrap();
        assert_eq!(true, output.vp_bypass_voice_processing().unwrap());

        assert_eq!(1, mute_count);

        output.start().unwrap();
        assert!(output.is_running().unwrap());
        output.stop().unwrap();
    }
}