Debian Codegraph @

third_party/rust/audioipc2-client/src/lib.rs

raw
1 // Copyright © 2017 Mozilla Foundation
2 //
3 // This program is made available under an ISC-style license. See the
4 // accompanying file LICENSE for details.
5 #![warn(unused_extern_crates)]
6
7 #[macro_use]
8 extern crate log;
9
10 #[macro_use]
11 mod send_recv;
12 mod context;
13 mod stream;
14
15 use crate::context::ClientContext;
16 use crate::stream::ClientStream;
17 use audioipc::PlatformHandleType;
18 use cubeb_backend::{capi, ffi};
19 use std::os::raw::{c_char, c_int};
20
21 thread_local!(static IN_CALLBACK: std::cell::RefCell<bool> = const { std::cell::RefCell::new(false) });
22 thread_local!(static AUDIOIPC_INIT_PARAMS: std::cell::RefCell<Option<AudioIpcInitParams>> = const { std::cell::RefCell::new(None) });
23
24 #[repr(C)]
25 #[derive(Clone, Copy, Debug)]
26 pub struct AudioIpcInitParams {
27 // Fields only need to be public for ipctest.
28 pub server_connection: PlatformHandleType,
29 pub pool_size: usize,
30 pub stack_size: usize,
31 pub thread_create_callback: Option<extern "C" fn(*const ::std::os::raw::c_char)>,
32 pub thread_destroy_callback: Option<extern "C" fn()>,
33 }
34
35 unsafe impl Send for AudioIpcInitParams {}
36
37 fn set_in_callback(in_callback: bool) {
38 IN_CALLBACK.with(|b| {
39 assert_eq!(*b.borrow(), !in_callback);
40 *b.borrow_mut() = in_callback;
41 });
42 }
43
44 fn run_in_callback<F, R>(f: F) -> R
45 where
46 F: FnOnce() -> R,
47 {
48 set_in_callback(true);
49
50 let r = f();
51
52 set_in_callback(false);
53
54 r
55 }
56
57 fn assert_not_in_callback() {
58 IN_CALLBACK.with(|b| {
59 assert!(!*b.borrow());
60 });
61 }
62
63 #[allow(clippy::missing_safety_doc)]
64 #[no_mangle]
65 /// Entry point from C code.
66 pub unsafe extern "C" fn audioipc2_client_init(
67 c: *mut *mut ffi::cubeb,
68 context_name: *const c_char,
69 init_params: *const AudioIpcInitParams,
70 ) -> c_int {
71 if init_params.is_null() {
72 return cubeb_backend::ffi::CUBEB_ERROR;
73 }
74
75 let init_params = &*init_params;
76
77 AUDIOIPC_INIT_PARAMS.with(|p| {
78 *p.borrow_mut() = Some(*init_params);
79 });
80 capi::capi_init::<ClientContext>(c, context_name)
81 }