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
|
// Copyright © 2017 Mozilla Foundation
//
// This program is made available under an ISC-style license. See the
// accompanying file LICENSE for details
#![warn(unused_extern_crates)]
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate log;
use audio_thread_priority::promote_current_thread_to_real_time;
use audioipc::ipccore;
use audioipc::sys;
use audioipc::PlatformHandleType;
use once_cell::sync::Lazy;
use std::ffi::{CStr, CString};
use std::os::raw::c_void;
use std::ptr;
use std::sync::Mutex;
use std::thread;
mod server;
struct CubebContextParams {
context_name: CString,
backend_name: Option<CString>,
}
static G_CUBEB_CONTEXT_PARAMS: Lazy<Mutex<CubebContextParams>> = Lazy::new(|| {
Mutex::new(CubebContextParams {
context_name: CString::new("AudioIPC Server").unwrap(),
backend_name: None,
})
});
#[allow(deprecated)]
pub mod errors {
#![allow(clippy::upper_case_acronyms)]
error_chain! {
links {
AudioIPC(::audioipc::errors::Error, ::audioipc::errors::ErrorKind);
}
foreign_links {
Cubeb(cubeb_core::Error);
Io(::std::io::Error);
}
}
}
use crate::errors::*;
struct ServerWrapper {
rpc_thread: ipccore::EventLoopThread,
callback_thread: ipccore::EventLoopThread,
device_collection_thread: ipccore::EventLoopThread,
}
fn register_thread(callback: Option<extern "C" fn(*const ::std::os::raw::c_char)>) {
if let Some(func) = callback {
let name = CString::new(thread::current().name().unwrap()).unwrap();
func(name.as_ptr());
}
}
fn unregister_thread(callback: Option<extern "C" fn()>) {
if let Some(func) = callback {
func();
}
}
fn init_threads(
thread_create_callback: Option<extern "C" fn(*const ::std::os::raw::c_char)>,
thread_destroy_callback: Option<extern "C" fn()>,
) -> Result<ServerWrapper> {
let rpc_name = "AudioIPC Server RPC";
let rpc_thread = ipccore::EventLoopThread::new(
rpc_name.to_string(),
None,
move || {
trace!("Starting {} thread", rpc_name);
register_thread(thread_create_callback);
audioipc::server_platform_init();
},
move || {
unregister_thread(thread_destroy_callback);
trace!("Stopping {} thread", rpc_name);
},
)
.map_err(|e| {
debug!("Failed to start {} thread: {:?}", rpc_name, e);
e
})?;
let callback_name = "AudioIPC Server Callback";
let callback_thread = ipccore::EventLoopThread::new(
callback_name.to_string(),
None,
move || {
trace!("Starting {} thread", callback_name);
if let Err(e) = promote_current_thread_to_real_time(0, 48000) {
debug!(
"Failed to promote {} thread to real-time: {:?}",
callback_name, e
);
}
register_thread(thread_create_callback);
},
move || {
unregister_thread(thread_destroy_callback);
trace!("Stopping {} thread", callback_name);
},
)
.map_err(|e| {
debug!("Failed to start {} thread: {:?}", callback_name, e);
e
})?;
let device_collection_name = "AudioIPC DeviceCollection RPC";
let device_collection_thread = ipccore::EventLoopThread::new(
device_collection_name.to_string(),
None,
move || {
trace!("Starting {} thread", device_collection_name);
register_thread(thread_create_callback);
},
move || {
unregister_thread(thread_destroy_callback);
trace!("Stopping {} thread", device_collection_name);
},
)
.map_err(|e| {
debug!("Failed to start {} thread: {:?}", device_collection_name, e);
e
})?;
Ok(ServerWrapper {
rpc_thread,
callback_thread,
device_collection_thread,
})
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct AudioIpcServerInitParams {
// Fields only need to be public for ipctest.
pub thread_create_callback: Option<extern "C" fn(*const ::std::os::raw::c_char)>,
pub thread_destroy_callback: Option<extern "C" fn()>,
}
#[allow(clippy::missing_safety_doc)]
#[no_mangle]
pub unsafe extern "C" fn audioipc2_server_start(
context_name: *const std::os::raw::c_char,
backend_name: *const std::os::raw::c_char,
init_params: *const AudioIpcServerInitParams,
) -> *mut c_void {
assert!(!init_params.is_null());
let mut params = G_CUBEB_CONTEXT_PARAMS.lock().unwrap();
if !context_name.is_null() {
params.context_name = CStr::from_ptr(context_name).to_owned();
}
if !backend_name.is_null() {
let backend_string = CStr::from_ptr(backend_name).to_owned();
params.backend_name = Some(backend_string);
}
match init_threads(
(*init_params).thread_create_callback,
(*init_params).thread_destroy_callback,
) {
Ok(server) => Box::into_raw(Box::new(server)) as *mut _,
Err(_) => ptr::null_mut() as *mut _,
}
}
// A `shm_area_size` of 0 allows the server to calculate an appropriate shm size for each stream.
// A non-zero `shm_area_size` forces all allocations to the specified size.
#[no_mangle]
pub extern "C" fn audioipc2_server_new_client(
p: *mut c_void,
shm_area_size: usize,
) -> PlatformHandleType {
let wrapper: &ServerWrapper = unsafe { &*(p as *mut _) };
// We create a connected pair of anonymous IPC endpoints. One side
// is registered with the event loop core, the other side is returned
// to the caller to be remoted to the client to complete setup.
let (server_pipe, client_pipe) = match sys::make_pipe_pair() {
Ok((server_pipe, client_pipe)) => (server_pipe, client_pipe),
Err(e) => {
error!(
"audioipc_server_new_client - make_pipe_pair failed: {:?}",
e
);
return audioipc::INVALID_HANDLE_VALUE;
}
};
let rpc_thread = wrapper.rpc_thread.handle();
let callback_thread = wrapper.callback_thread.handle();
let device_collection_thread = wrapper.device_collection_thread.handle();
let server = server::CubebServer::new(
callback_thread.clone(),
device_collection_thread.clone(),
shm_area_size,
);
if let Err(e) = rpc_thread.bind_server(server, server_pipe) {
error!("audioipc_server_new_client - bind_server failed: {:?}", e);
return audioipc::INVALID_HANDLE_VALUE;
}
unsafe { client_pipe.into_raw() }
}
#[no_mangle]
pub extern "C" fn audioipc2_server_stop(p: *mut c_void) {
let wrapper = unsafe { Box::<ServerWrapper>::from_raw(p as *mut _) };
drop(wrapper);
}
|