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
|
// 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::core;
use audioipc::platformhandle_passing::framed_with_platformhandles;
use audioipc::rpc;
use audioipc::{MessageStream, PlatformHandle, PlatformHandleType};
use futures::sync::oneshot;
use futures::Future;
use once_cell::sync::Lazy;
use std::ffi::{CStr, CString};
use std::os::raw::c_void;
use std::ptr;
use std::sync::Mutex;
use tokio::reactor;
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 {
error_chain! {
links {
AudioIPC(::audioipc::errors::Error, ::audioipc::errors::ErrorKind);
}
foreign_links {
Cubeb(cubeb_core::Error);
Io(::std::io::Error);
Canceled(::futures::sync::oneshot::Canceled);
}
}
}
use crate::errors::*;
struct ServerWrapper {
core_thread: core::CoreThread,
callback_thread: core::CoreThread,
}
fn run() -> Result<ServerWrapper> {
trace!("Starting up cubeb audio server event loop thread...");
let callback_thread = core::spawn_thread(
"AudioIPC Callback RPC",
|| {
match promote_current_thread_to_real_time(0, 48000) {
Ok(_) => {}
Err(_) => {
debug!("Failed to promote audio callback thread to real-time.");
}
}
trace!("Starting up cubeb audio callback event loop thread...");
Ok(())
},
|| {},
)
.or_else(|e| {
debug!(
"Failed to start cubeb audio callback event loop thread: {:?}",
e
);
Err(e)
})?;
let core_thread = core::spawn_thread(
"AudioIPC Server RPC",
move || {
audioipc::server_platform_init();
Ok(())
},
|| {},
)
.or_else(|e| {
debug!("Failed to cubeb audio core event loop thread: {:?}", e);
Err(e)
})?;
Ok(ServerWrapper {
core_thread,
callback_thread,
})
}
#[no_mangle]
pub unsafe extern "C" fn audioipc_server_start(
context_name: *const std::os::raw::c_char,
backend_name: *const std::os::raw::c_char,
) -> *mut c_void {
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 run() {
Ok(server) => Box::into_raw(Box::new(server)) as *mut _,
Err(_) => ptr::null_mut() as *mut _,
}
}
#[no_mangle]
pub extern "C" fn audioipc_server_new_client(p: *mut c_void) -> PlatformHandleType {
let (wait_tx, wait_rx) = oneshot::channel();
let wrapper: &ServerWrapper = unsafe { &*(p as *mut _) };
let core_handle = wrapper.callback_thread.handle();
// We create a connected pair of anonymous IPC endpoints. One side
// is registered with the reactor core, the other side is returned
// to the caller.
MessageStream::anonymous_ipc_pair()
.and_then(|(ipc_server, ipc_client)| {
// Spawn closure to run on same thread as reactor::Core
// via remote handle.
wrapper
.core_thread
.handle()
.spawn(futures::future::lazy(|| {
trace!("Incoming connection");
let handle = reactor::Handle::default();
ipc_server.into_tokio_ipc(&handle)
.and_then(|sock| {
let transport = framed_with_platformhandles(sock, Default::default());
rpc::bind_server(transport, server::CubebServer::new(core_handle));
Ok(())
}).map_err(|_| ())
// Notify waiting thread that server has been registered.
.and_then(|_| wait_tx.send(()))
}))
.expect("Failed to spawn CubebServer");
// Wait for notification that server has been registered
// with reactor::Core.
let _ = wait_rx.wait();
Ok(unsafe { PlatformHandle::from(ipc_client).into_raw() })
})
.unwrap_or(audioipc::INVALID_HANDLE_VALUE)
}
#[no_mangle]
pub extern "C" fn audioipc_server_stop(p: *mut c_void) {
let wrapper = unsafe { Box::<ServerWrapper>::from_raw(p as *mut _) };
drop(wrapper);
}
|