Debian Codegraph @

third_party/rust/cubeb-backend/src/capi.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
6 use cubeb_core::{
7 ffi, DeviceCollectionRef, DeviceRef, DeviceType, InputProcessingParams, StreamParams,
8 StreamParamsRef,
9 };
10 use std::ffi::CStr;
11 use std::mem;
12 use std::os::raw::{c_char, c_int, c_void};
13 use {ContextOps, StreamOps};
14
15 // Helper macro for unwrapping `Result` values from rust-api calls
16 // while returning early with a c-api error code if the value of the
17 // expression is `Err`.
18 macro_rules! _try(
19 ($e:expr) => (match $e {
20 Ok(e) => e,
21 Err(e) => return e.raw_code()
22 })
23 );
24
25 macro_rules! as_opt_ref {
26 ($e:expr) => {
27 if $e.is_null() {
28 None
29 } else {
30 Some(StreamParamsRef::from_ptr($e))
31 }
32 };
33 }
34
35 #[macro_export]
36 macro_rules! capi_new(
37 ($ctx:ident, $stm:ident) => (
38 Ops {
39 init: Some($crate::capi::capi_init::<$ctx>),
40 get_backend_id: Some($crate::capi::capi_get_backend_id::<$ctx>),
41 get_max_channel_count: Some($crate::capi::capi_get_max_channel_count::<$ctx>),
42 get_min_latency: Some($crate::capi::capi_get_min_latency::<$ctx>),
43 get_preferred_sample_rate: Some($crate::capi::capi_get_preferred_sample_rate::<$ctx>),
44 get_supported_input_processing_params:
45 Some($crate::capi::capi_get_supported_input_processing_params::<$ctx>),
46 enumerate_devices: Some($crate::capi::capi_enumerate_devices::<$ctx>),
47 device_collection_destroy: Some($crate::capi::capi_device_collection_destroy::<$ctx>),
48 destroy: Some($crate::capi::capi_destroy::<$ctx>),
49 stream_init: Some($crate::capi::capi_stream_init::<$ctx>),
50 stream_destroy: Some($crate::capi::capi_stream_destroy::<$stm>),
51 stream_start: Some($crate::capi::capi_stream_start::<$stm>),
52 stream_stop: Some($crate::capi::capi_stream_stop::<$stm>),
53 stream_get_position: Some($crate::capi::capi_stream_get_position::<$stm>),
54 stream_get_latency: Some($crate::capi::capi_stream_get_latency::<$stm>),
55 stream_get_input_latency: Some($crate::capi::capi_stream_get_input_latency::<$stm>),
56 stream_set_volume: Some($crate::capi::capi_stream_set_volume::<$stm>),
57 stream_set_name: Some($crate::capi::capi_stream_set_name::<$stm>),
58 stream_get_current_device: Some($crate::capi::capi_stream_get_current_device::<$stm>),
59 stream_set_input_mute: Some($crate::capi::capi_stream_set_input_mute::<$stm>),
60 stream_set_input_processing_params:
61 Some($crate::capi::capi_stream_set_input_processing_params::<$stm>),
62 stream_device_destroy: Some($crate::capi::capi_stream_device_destroy::<$stm>),
63 stream_register_device_changed_callback:
64 Some($crate::capi::capi_stream_register_device_changed_callback::<$stm>),
65 register_device_collection_changed:
66 Some($crate::capi::capi_register_device_collection_changed::<$ctx>)
67 }));
68
69 /// # Safety
70 ///
71 /// Entry point from C code.
72 ///
73 /// This function is unsafe because it dereferences the given `c` and `context` pointers.
74 /// The caller should ensure those pointers are valid.
75 pub unsafe extern "C" fn capi_init<CTX: ContextOps>(
76 c: *mut *mut ffi::cubeb,
77 context_name: *const c_char,
78 ) -> c_int {
79 let anchor = &();
80 let context_name = opt_cstr(anchor, context_name);
81 let context = _try!(CTX::init(context_name));
82 *c = context.as_ptr();
83 // Leaking pointer across C FFI
84 mem::forget(context);
85 ffi::CUBEB_OK
86 }
87
88 /// # Safety
89 ///
90 /// Entry point from C code.
91 ///
92 /// This function is unsafe because it dereferences the given `c` pointer.
93 /// The caller should ensure that pointer is valid.
94 pub unsafe extern "C" fn capi_get_backend_id<CTX: ContextOps>(c: *mut ffi::cubeb) -> *const c_char {
95 let ctx = &mut *(c as *mut CTX);
96 ctx.backend_id().as_ptr()
97 }
98
99 /// # Safety
100 ///
101 /// Entry point from C code.
102 ///
103 /// This function is unsafe because it dereferences the given `c` and `max_channels` pointers.
104 /// The caller should ensure those pointers are valid.
105 pub unsafe extern "C" fn capi_get_max_channel_count<CTX: ContextOps>(
106 c: *mut ffi::cubeb,
107 max_channels: *mut u32,
108 ) -> c_int {
109 let ctx = &mut *(c as *mut CTX);
110
111 *max_channels = _try!(ctx.max_channel_count());
112 ffi::CUBEB_OK
113 }
114
115 /// # Safety
116 ///
117 /// Entry point from C code.
118 ///
119 /// This function is unsafe because it dereferences the given `c` and `latency_frames` pointers.
120 /// The caller should ensure those pointers are valid.
121 pub unsafe extern "C" fn capi_get_min_latency<CTX: ContextOps>(
122 c: *mut ffi::cubeb,
123 param: ffi::cubeb_stream_params,
124 latency_frames: *mut u32,
125 ) -> c_int {
126 let ctx = &mut *(c as *mut CTX);
127 let param = StreamParams::from(param);
128 *latency_frames = _try!(ctx.min_latency(param));
129 ffi::CUBEB_OK
130 }
131
132 /// # Safety
133 ///
134 /// Entry point from C code.
135 ///
136 /// This function is unsafe because it dereferences the given `c` and `rate` pointers.
137 /// The caller should ensure those pointers are valid.
138 pub unsafe extern "C" fn capi_get_preferred_sample_rate<CTX: ContextOps>(
139 c: *mut ffi::cubeb,
140 rate: *mut u32,
141 ) -> c_int {
142 let ctx = &mut *(c as *mut CTX);
143
144 *rate = _try!(ctx.preferred_sample_rate());
145 ffi::CUBEB_OK
146 }
147
148 /// # Safety
149 ///
150 /// Entry point from C code.
151 ///
152 /// This function is unsafe because it dereferences the given `c` and `params` pointers.
153 /// The caller should ensure those pointers are valid.
154 pub unsafe extern "C" fn capi_get_supported_input_processing_params<CTX: ContextOps>(
155 c: *mut ffi::cubeb,
156 params: *mut ffi::cubeb_input_processing_params,
157 ) -> c_int {
158 let ctx = &mut *(c as *mut CTX);
159 *params = _try!(ctx.supported_input_processing_params()).bits();
160 ffi::CUBEB_OK
161 }
162
163 /// # Safety
164 ///
165 /// Entry point from C code.
166 ///
167 /// This function is unsafe because it dereferences the given `c` and `collection` pointers.
168 /// The caller should ensure those pointers are valid.
169 pub unsafe extern "C" fn capi_enumerate_devices<CTX: ContextOps>(
170 c: *mut ffi::cubeb,
171 devtype: ffi::cubeb_device_type,
172 collection: *mut ffi::cubeb_device_collection,
173 ) -> c_int {
174 let ctx = &mut *(c as *mut CTX);
175 let devtype = DeviceType::from_bits_truncate(devtype);
176 let collection = DeviceCollectionRef::from_ptr(collection);
177 _try!(ctx.enumerate_devices(devtype, collection));
178 ffi::CUBEB_OK
179 }
180
181 /// # Safety
182 ///
183 /// Entry point from C code.
184 ///
185 /// This function is unsafe because it dereferences the given `c` and `collection` pointers.
186 /// The caller should ensure those pointers are valid.
187 pub unsafe extern "C" fn capi_device_collection_destroy<CTX: ContextOps>(
188 c: *mut ffi::cubeb,
189 collection: *mut ffi::cubeb_device_collection,
190 ) -> c_int {
191 let ctx = &mut *(c as *mut CTX);
192 let collection = DeviceCollectionRef::from_ptr_mut(collection);
193 _try!(ctx.device_collection_destroy(collection));
194 ffi::CUBEB_OK
195 }
196
197 /// # Safety
198 ///
199 /// Entry point from C code.
200 ///
201 /// This function is unsafe because it dereferences the given `c` pointer.
202 /// The caller should ensure that pointer is valid.
203 pub unsafe extern "C" fn capi_destroy<CTX>(c: *mut ffi::cubeb) {
204 let _: Box<CTX> = Box::from_raw(c as *mut _);
205 }
206
207 /// # Safety
208 ///
209 /// Entry point from C code.
210 ///
211 /// This function is unsafe because it dereferences the given `c`, `s`, `stream_name`, `input_stream_params`,
212 /// `output_stream_params`, `data_callback`, `state_callback`, and `user_ptr` pointers.
213 /// The caller should ensure those pointers are valid.
214 pub unsafe extern "C" fn capi_stream_init<CTX: ContextOps>(
215 c: *mut ffi::cubeb,
216 s: *mut *mut ffi::cubeb_stream,
217 stream_name: *const c_char,
218 input_device: ffi::cubeb_devid,
219 input_stream_params: *mut ffi::cubeb_stream_params,
220 output_device: ffi::cubeb_devid,
221 output_stream_params: *mut ffi::cubeb_stream_params,
222 latency_frames: u32,
223 data_callback: ffi::cubeb_data_callback,
224 state_callback: ffi::cubeb_state_callback,
225 user_ptr: *mut c_void,
226 ) -> c_int {
227 let ctx = &mut *(c as *mut CTX);
228 let anchor = &(); // for lifetime of stream_name as CStr
229
230 let input_stream_params = as_opt_ref!(input_stream_params);
231 let output_stream_params = as_opt_ref!(output_stream_params);
232
233 let stream = _try!(ctx.stream_init(
234 opt_cstr(anchor, stream_name),
235 input_device,
236 input_stream_params,
237 output_device,
238 output_stream_params,
239 latency_frames,
240 data_callback,
241 state_callback,
242 user_ptr
243 ));
244 *s = stream.as_ptr();
245 // Leaking pointer across C FFI
246 mem::forget(stream);
247 ffi::CUBEB_OK
248 }
249
250 /// # Safety
251 ///
252 /// Entry point from C code.
253 ///
254 /// This function is unsafe because it dereferences the given `s` pointer.
255 /// The caller should ensure that pointer is valid.
256 pub unsafe extern "C" fn capi_stream_destroy<STM>(s: *mut ffi::cubeb_stream) {
257 let _ = Box::from_raw(s as *mut STM);
258 }
259
260 /// # Safety
261 ///
262 /// Entry point from C code.
263 ///
264 /// This function is unsafe because it dereferences the given `s` pointer.
265 /// The caller should ensure that pointer is valid.
266 pub unsafe extern "C" fn capi_stream_start<STM: StreamOps>(s: *mut ffi::cubeb_stream) -> c_int {
267 let stm = &mut *(s as *mut STM);
268
269 _try!(stm.start());
270 ffi::CUBEB_OK
271 }
272
273 /// # Safety
274 ///
275 /// Entry point from C code.
276 ///
277 /// This function is unsafe because it dereferences the given `s` pointer.
278 /// The caller should ensure that pointer is valid.
279 pub unsafe extern "C" fn capi_stream_stop<STM: StreamOps>(s: *mut ffi::cubeb_stream) -> c_int {
280 let stm = &mut *(s as *mut STM);
281
282 _try!(stm.stop());
283 ffi::CUBEB_OK
284 }
285
286 /// # Safety
287 ///
288 /// Entry point from C code.
289 ///
290 /// This function is unsafe because it dereferences the given `s` and `position` pointers.
291 /// The caller should ensure those pointers are valid.
292 pub unsafe extern "C" fn capi_stream_get_position<STM: StreamOps>(
293 s: *mut ffi::cubeb_stream,
294 position: *mut u64,
295 ) -> c_int {
296 let stm = &mut *(s as *mut STM);
297
298 *position = _try!(stm.position());
299 ffi::CUBEB_OK
300 }
301
302 /// # Safety
303 ///
304 /// Entry point from C code.
305 ///
306 /// This function is unsafe because it dereferences the given `s` and `latency` pointers.
307 /// The caller should ensure those pointers are valid.
308 pub unsafe extern "C" fn capi_stream_get_latency<STM: StreamOps>(
309 s: *mut ffi::cubeb_stream,
310 latency: *mut u32,
311 ) -> c_int {
312 let stm = &mut *(s as *mut STM);
313
314 *latency = _try!(stm.latency());
315 ffi::CUBEB_OK
316 }
317
318 /// # Safety
319 ///
320 /// Entry point from C code.
321 ///
322 /// This function is unsafe because it dereferences the given `s` and `latency` pointers.
323 /// The caller should ensure those pointers are valid.
324 pub unsafe extern "C" fn capi_stream_get_input_latency<STM: StreamOps>(
325 s: *mut ffi::cubeb_stream,
326 latency: *mut u32,
327 ) -> c_int {
328 let stm = &mut *(s as *mut STM);
329
330 *latency = _try!(stm.input_latency());
331 ffi::CUBEB_OK
332 }
333
334 /// # Safety
335 ///
336 /// Entry point from C code.
337 ///
338 /// This function is unsafe because it dereferences the given `s` pointer.
339 /// The caller should ensure that pointer is valid.
340 pub unsafe extern "C" fn capi_stream_set_volume<STM: StreamOps>(
341 s: *mut ffi::cubeb_stream,
342 volume: f32,
343 ) -> c_int {
344 let stm = &mut *(s as *mut STM);
345
346 _try!(stm.set_volume(volume));
347 ffi::CUBEB_OK
348 }
349
350 /// # Safety
351 ///
352 /// Entry point from C code.
353 ///
354 /// This function is unsafe because it dereferences the given `s` and `name` pointers.
355 /// The caller should ensure those pointers are valid.
356 pub unsafe extern "C" fn capi_stream_set_name<STM: StreamOps>(
357 s: *mut ffi::cubeb_stream,
358 name: *const c_char,
359 ) -> c_int {
360 let stm = &mut *(s as *mut STM);
361 let anchor = &();
362 if let Some(name) = opt_cstr(anchor, name) {
363 _try!(stm.set_name(name));
364 ffi::CUBEB_OK
365 } else {
366 ffi::CUBEB_ERROR_INVALID_PARAMETER
367 }
368 }
369
370 /// # Safety
371 ///
372 /// Entry point from C code.
373 ///
374 /// This function is unsafe because it dereferences the given `s` and `device` pointers.
375 /// The caller should ensure those pointers are valid.
376 pub unsafe extern "C" fn capi_stream_get_current_device<STM: StreamOps>(
377 s: *mut ffi::cubeb_stream,
378 device: *mut *mut ffi::cubeb_device,
379 ) -> i32 {
380 let stm = &mut *(s as *mut STM);
381
382 *device = _try!(stm.current_device()).as_ptr();
383 ffi::CUBEB_OK
384 }
385
386 /// # Safety
387 ///
388 /// Entry point from C code.
389 ///
390 /// This function is unsafe because it dereferences the given `s` pointer.
391 /// The caller should ensure those pointers are valid.
392 pub unsafe extern "C" fn capi_stream_set_input_mute<STM: StreamOps>(
393 s: *mut ffi::cubeb_stream,
394 mute: c_int,
395 ) -> c_int {
396 let stm = &mut *(s as *mut STM);
397 _try!(stm.set_input_mute(mute != 0));
398 ffi::CUBEB_OK
399 }
400
401 /// # Safety
402 ///
403 /// Entry point from C code.
404 ///
405 /// This function is unsafe because it dereferences the given `s` pointer.
406 /// The caller should ensure those pointers are valid.
407 pub unsafe extern "C" fn capi_stream_set_input_processing_params<STM: StreamOps>(
408 s: *mut ffi::cubeb_stream,
409 params: ffi::cubeb_input_processing_params,
410 ) -> c_int {
411 let stm = &mut *(s as *mut STM);
412 _try!(stm.set_input_processing_params(InputProcessingParams::from_bits_truncate(params)));
413 ffi::CUBEB_OK
414 }
415
416 /// # Safety
417 ///
418 /// Entry point from C code.
419 ///
420 /// This function is unsafe because it dereferences the given `s` and `device` pointers.
421 /// The caller should ensure those pointers are valid.
422 pub unsafe extern "C" fn capi_stream_device_destroy<STM: StreamOps>(
423 s: *mut ffi::cubeb_stream,
424 device: *mut ffi::cubeb_device,
425 ) -> c_int {
426 let stm = &mut *(s as *mut STM);
427 let device = DeviceRef::from_ptr(device);
428 let _ = stm.device_destroy(device);
429 ffi::CUBEB_OK
430 }
431
432 /// # Safety
433 ///
434 /// Entry point from C code.
435 ///
436 /// This function is unsafe because it dereferences the given `s` and `device_changed_callback` pointers.
437 /// The caller should ensure those pointers are valid.
438 pub unsafe extern "C" fn capi_stream_register_device_changed_callback<STM: StreamOps>(
439 s: *mut ffi::cubeb_stream,
440 device_changed_callback: ffi::cubeb_device_changed_callback,
441 ) -> c_int {
442 let stm = &mut *(s as *mut STM);
443
444 _try!(stm.register_device_changed_callback(device_changed_callback));
445 ffi::CUBEB_OK
446 }
447
448 /// # Safety
449 ///
450 /// Entry point from C code.
451 ///
452 /// This function is unsafe because it dereferences the given `s`, `collection_changed_callback`, and
453 /// `user_ptr` pointers.
454 /// The caller should ensure those pointers are valid.
455 pub unsafe extern "C" fn capi_register_device_collection_changed<CTX: ContextOps>(
456 c: *mut ffi::cubeb,
457 devtype: ffi::cubeb_device_type,
458 collection_changed_callback: ffi::cubeb_device_collection_changed_callback,
459 user_ptr: *mut c_void,
460 ) -> i32 {
461 let ctx = &mut *(c as *mut CTX);
462 let devtype = DeviceType::from_bits_truncate(devtype);
463 _try!(ctx.register_device_collection_changed(devtype, collection_changed_callback, user_ptr));
464 ffi::CUBEB_OK
465 }
466
467 fn opt_cstr<T>(_anchor: &T, ptr: *const c_char) -> Option<&CStr> {
468 if ptr.is_null() {
469 None
470 } else {
471 Some(unsafe { CStr::from_ptr(ptr) })
472 }
473 }