aboutsummaryrefslogtreecommitdiff
path: root/gd/rust/topshim/src/profiles/a2dp.rs
blob: ec285c4d5e130488ac4db7694dcbd5c1aa583a79 (plain)
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
use crate::btif::{BluetoothInterface, RawAddress};
use crate::topstack::get_dispatchers;

use num_traits::cast::FromPrimitive;
use std::sync::{Arc, Mutex};
use topshim_macros::cb_variant;

#[derive(Debug, FromPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum BtavConnectionState {
    Disconnected = 0,
    Connecting,
    Connected,
    Disconnecting,
}

impl From<u32> for BtavConnectionState {
    fn from(item: u32) -> Self {
        BtavConnectionState::from_u32(item).unwrap()
    }
}

#[derive(Debug, FromPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum BtavAudioState {
    RemoteSuspend = 0,
    Stopped,
    Started,
}

impl From<u32> for BtavAudioState {
    fn from(item: u32) -> Self {
        BtavAudioState::from_u32(item).unwrap()
    }
}

#[derive(Debug, FromPrimitive, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum A2dpCodecIndex {
    SrcSbc = 0,
    SrcAac,
    SrcAptx,
    SrcAptxHD,
    SrcLdac,
    SinkSbc,
    SinkAac,
    SinkLdac,
    Max,
}

impl A2dpCodecIndex {
    pub const SRC_MIN: A2dpCodecIndex = A2dpCodecIndex::SrcSbc;
    pub const SRC_MAX: A2dpCodecIndex = A2dpCodecIndex::SinkSbc;
    pub const SINK_MIN: A2dpCodecIndex = A2dpCodecIndex::SinkSbc;
    pub const SINK_MAX: A2dpCodecIndex = A2dpCodecIndex::Max;
    pub const MAX: A2dpCodecIndex = A2dpCodecIndex::Max;
    pub const MIN: A2dpCodecIndex = A2dpCodecIndex::SrcSbc;
}

impl From<i32> for A2dpCodecIndex {
    fn from(item: i32) -> Self {
        A2dpCodecIndex::from_i32(item).unwrap_or_else(|| A2dpCodecIndex::MIN)
    }
}

#[derive(Debug, FromPrimitive, PartialEq, PartialOrd)]
#[repr(i32)]
pub enum A2dpCodecPriority {
    Disabled = -1,
    Default = 0,
    Highest = 1_000_000,
}

impl From<i32> for A2dpCodecPriority {
    fn from(item: i32) -> Self {
        A2dpCodecPriority::from_i32(item).unwrap_or_else(|| A2dpCodecPriority::Default)
    }
}

bitflags! {
    pub struct A2dpCodecSampleRate: i32 {
        const RATE_NONE = 0x0;
        const RATE_44100 = 0x01;
        const RATE_48000 = 0x02;
        const RATE_88200 = 0x04;
        const RATE_96000 = 0x08;
        const RATE_176400 = 0x10;
        const RATE_192000 = 0x20;
        const RATE_16000 = 0x40;
        const RATE_24000 = 0x80;
    }
}

impl A2dpCodecSampleRate {
    pub fn validate_bits(val: i32) -> bool {
        val <= A2dpCodecSampleRate::all().bits()
    }
}

bitflags! {
    pub struct A2dpCodecBitsPerSample: i32 {
        const SAMPLE_NONE = 0x0;
        const SAMPLE_16 = 0x01;
        const SAMPLE_24 = 0x02;
        const SAMPLE_32 = 0x04;
    }
}

impl A2dpCodecBitsPerSample {
    pub fn validate_bits(val: i32) -> bool {
        val <= A2dpCodecBitsPerSample::all().bits()
    }
}

bitflags! {
    pub struct A2dpCodecChannelMode: i32 {
        const MODE_NONE = 0x0;
        const MODE_MONO = 0x01;
        const MODE_STEREO = 0x02;
    }
}

impl A2dpCodecChannelMode {
    pub fn validate_bits(val: i32) -> bool {
        val <= A2dpCodecChannelMode::all().bits()
    }
}

#[cxx::bridge(namespace = bluetooth::topshim::rust)]
pub mod ffi {
    #[derive(Debug, Copy, Clone)]
    pub struct RustRawAddress {
        address: [u8; 6],
    }

    #[derive(Debug)]
    pub struct A2dpCodecConfig {
        codec_type: i32,
        codec_priority: i32,
        sample_rate: i32,
        bits_per_sample: i32,
        channel_mode: i32,
        codec_specific_1: i64,
        codec_specific_2: i64,
        codec_specific_3: i64,
        codec_specific_4: i64,
    }

    #[derive(Debug, Default)]
    pub struct RustPresentationPosition {
        remote_delay_report_ns: u64,
        total_bytes_read: u64,
        data_position_sec: i64,
        data_position_nsec: i32,
    }

    unsafe extern "C++" {
        include!("btav/btav_shim.h");
        include!("btav_sink/btav_sink_shim.h");

        type A2dpIntf;
        type A2dpSinkIntf;

        unsafe fn GetA2dpProfile(btif: *const u8) -> UniquePtr<A2dpIntf>;

        fn init(self: &A2dpIntf) -> i32;
        fn connect(self: &A2dpIntf, bt_addr: RustRawAddress) -> i32;
        fn disconnect(self: &A2dpIntf, bt_addr: RustRawAddress) -> i32;
        fn set_silence_device(self: &A2dpIntf, bt_addr: RustRawAddress, silent: bool) -> i32;
        fn set_active_device(self: &A2dpIntf, bt_addr: RustRawAddress) -> i32;
        fn config_codec(
            self: &A2dpIntf,
            bt_addr: RustRawAddress,
            codec_preferences: Vec<A2dpCodecConfig>,
        ) -> i32;
        fn set_audio_config(self: &A2dpIntf, config: A2dpCodecConfig) -> bool;
        fn start_audio_request(self: &A2dpIntf) -> bool;
        fn stop_audio_request(self: &A2dpIntf) -> bool;
        fn cleanup(self: &A2dpIntf);
        fn get_presentation_position(self: &A2dpIntf) -> RustPresentationPosition;
        // A2dp sink functions

        unsafe fn GetA2dpSinkProfile(btif: *const u8) -> UniquePtr<A2dpSinkIntf>;

        fn init(self: Pin<&mut A2dpSinkIntf>) -> i32;
        fn cleanup(self: Pin<&mut A2dpSinkIntf>);
    }
    extern "Rust" {
        fn connection_state_callback(addr: RustRawAddress, state: u32);
        fn audio_state_callback(addr: RustRawAddress, state: u32);
        fn audio_config_callback(
            addr: RustRawAddress,
            codec_config: A2dpCodecConfig,
            codecs_local_capabilities: Vec<A2dpCodecConfig>,
            codecs_selectable_capabilities: Vec<A2dpCodecConfig>,
        );
        fn mandatory_codec_preferred_callback(addr: RustRawAddress);
    }
}

pub type FfiAddress = ffi::RustRawAddress;
pub type A2dpCodecConfig = ffi::A2dpCodecConfig;
pub type PresentationPosition = ffi::RustPresentationPosition;

impl From<RawAddress> for FfiAddress {
    fn from(addr: RawAddress) -> Self {
        FfiAddress { address: addr.val }
    }
}

impl Into<RawAddress> for FfiAddress {
    fn into(self) -> RawAddress {
        RawAddress { val: self.address }
    }
}

impl Default for A2dpCodecConfig {
    fn default() -> A2dpCodecConfig {
        A2dpCodecConfig {
            codec_type: 0,
            codec_priority: 0,
            sample_rate: 0,
            bits_per_sample: 0,
            channel_mode: 0,
            codec_specific_1: 0,
            codec_specific_2: 0,
            codec_specific_3: 0,
            codec_specific_4: 0,
        }
    }
}

#[derive(Debug)]
pub enum A2dpCallbacks {
    ConnectionState(RawAddress, BtavConnectionState),
    AudioState(RawAddress, BtavAudioState),
    AudioConfig(RawAddress, A2dpCodecConfig, Vec<A2dpCodecConfig>, Vec<A2dpCodecConfig>),
    MandatoryCodecPreferred(RawAddress),
}

pub struct A2dpCallbacksDispatcher {
    pub dispatch: Box<dyn Fn(A2dpCallbacks) + Send>,
}

type A2dpCb = Arc<Mutex<A2dpCallbacksDispatcher>>;

cb_variant!(A2dpCb, connection_state_callback -> A2dpCallbacks::ConnectionState,
FfiAddress -> RawAddress, u32 -> BtavConnectionState, {
    let _0 = _0.into();
});

cb_variant!(A2dpCb, audio_state_callback -> A2dpCallbacks::AudioState,
FfiAddress -> RawAddress, u32 -> BtavAudioState, {
    let _0 = _0.into();
});

cb_variant!(A2dpCb, mandatory_codec_preferred_callback -> A2dpCallbacks::MandatoryCodecPreferred,
FfiAddress -> RawAddress, {
    let _0 = _0.into();
});

cb_variant!(A2dpCb, audio_config_callback -> A2dpCallbacks::AudioConfig,
FfiAddress -> RawAddress, A2dpCodecConfig, Vec<A2dpCodecConfig>, Vec<A2dpCodecConfig>, {
    let _0 = _0.into();
});

pub struct A2dp {
    internal: cxx::UniquePtr<ffi::A2dpIntf>,
    _is_init: bool,
}

// For *const u8 opaque btif
unsafe impl Send for A2dp {}

impl A2dp {
    pub fn new(intf: &BluetoothInterface) -> A2dp {
        let a2dpif: cxx::UniquePtr<ffi::A2dpIntf>;
        unsafe {
            a2dpif = ffi::GetA2dpProfile(intf.as_raw_ptr());
        }

        A2dp { internal: a2dpif, _is_init: false }
    }

    pub fn initialize(&mut self, callbacks: A2dpCallbacksDispatcher) -> bool {
        if get_dispatchers().lock().unwrap().set::<A2dpCb>(Arc::new(Mutex::new(callbacks))) {
            panic!("Tried to set dispatcher for A2dp callbacks while it already exists");
        }
        self.internal.init();
        true
    }

    pub fn connect(&mut self, device: String) {
        let addr = RawAddress::from_string(device.clone());
        if addr.is_none() {
            eprintln!("Invalid device string {}", device);
            return;
        }
        self.internal.connect(addr.unwrap().into());
    }

    pub fn set_active_device(&mut self, device: String) {
        let addr = RawAddress::from_string(device.clone());
        if addr.is_none() {
            eprintln!("Invalid device string {}", device);
            return;
        }
        self.internal.set_active_device(addr.unwrap().into());
    }

    pub fn disconnect(&mut self, device: String) {
        let addr = RawAddress::from_string(device.clone());
        if addr.is_none() {
            eprintln!("Invalid device string {}", device);
            return;
        }
        self.internal.disconnect(addr.unwrap().into());
    }

    pub fn set_audio_config(&self, sample_rate: i32, bits_per_sample: i32, channel_mode: i32) {
        let config =
            A2dpCodecConfig { sample_rate, bits_per_sample, channel_mode, ..Default::default() };
        self.internal.set_audio_config(config);
    }
    pub fn start_audio_request(&self) {
        self.internal.start_audio_request();
    }

    pub fn stop_audio_request(&self) {
        self.internal.stop_audio_request();
    }

    pub fn get_presentation_position(&self) -> PresentationPosition {
        self.internal.get_presentation_position()
    }
}

pub struct A2dpSink {
    internal: cxx::UniquePtr<ffi::A2dpSinkIntf>,
    _is_init: bool,
}

// For *const u8 opaque btif
unsafe impl Send for A2dpSink {}

impl A2dpSink {
    pub fn new(intf: &BluetoothInterface) -> A2dpSink {
        let a2dp_sink: cxx::UniquePtr<ffi::A2dpSinkIntf>;
        unsafe {
            a2dp_sink = ffi::GetA2dpSinkProfile(intf.as_raw_ptr());
        }

        A2dpSink { internal: a2dp_sink, _is_init: false }
    }

    pub fn initialize(&mut self) -> bool {
        self.internal.pin_mut().init();
        true
    }

    pub fn cleanup(&mut self) {}
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn validate_sample_rate() {
        assert!(!A2dpCodecSampleRate::validate_bits(256));
        assert!(A2dpCodecSampleRate::validate_bits(2 + 32 + 128));
    }

    #[test]
    fn validate_bits_per_sample() {
        assert!(!A2dpCodecBitsPerSample::validate_bits(8));
        assert!(A2dpCodecBitsPerSample::validate_bits(1 + 4));
    }

    #[test]
    fn validate_channel_mode() {
        assert!(!A2dpCodecChannelMode::validate_bits(4));
        assert!(A2dpCodecChannelMode::validate_bits(1 + 2));
    }
}