Debian Codegraph @

third_party/rust/cubeb-sys/libcubeb/src/cubeb.c

raw
1 /*
2 * Copyright © 2013 Mozilla Foundation
3 *
4 * This program is made available under an ISC-style license. See the
5 * accompanying file LICENSE for details.
6 */
7 #undef NDEBUG
8 #include "cubeb/cubeb.h"
9 #include "cubeb-internal.h"
10 #include <assert.h>
11 #include <stddef.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #define NELEMS(x) ((int)(sizeof(x) / sizeof(x[0])))
16
17 struct cubeb {
18 struct cubeb_ops * ops;
19 };
20
21 struct cubeb_stream {
22 /*
23 * Note: All implementations of cubeb_stream must keep the following
24 * layout.
25 */
26 struct cubeb * context;
27 void * user_ptr;
28 };
29
30 #if defined(USE_PULSE)
31 int
32 pulse_init(cubeb ** context, char const * context_name);
33 #endif
34 #if defined(USE_PULSE_RUST)
35 int
36 pulse_rust_init(cubeb ** contet, char const * context_name);
37 #endif
38 #if defined(USE_JACK)
39 int
40 jack_init(cubeb ** context, char const * context_name);
41 #endif
42 #if defined(USE_ALSA)
43 int
44 alsa_init(cubeb ** context, char const * context_name);
45 #endif
46 #if defined(USE_AUDIOUNIT)
47 int
48 audiounit_init(cubeb ** context, char const * context_name);
49 #endif
50 #if defined(USE_AUDIOUNIT_RUST)
51 int
52 audiounit_rust_init(cubeb ** contet, char const * context_name);
53 #endif
54 #if defined(USE_WINMM)
55 int
56 winmm_init(cubeb ** context, char const * context_name);
57 #endif
58 #if defined(USE_WASAPI)
59 int
60 wasapi_init(cubeb ** context, char const * context_name);
61 #endif
62 #if defined(USE_SNDIO)
63 int
64 sndio_init(cubeb ** context, char const * context_name);
65 #endif
66 #if defined(USE_SUN)
67 int
68 sun_init(cubeb ** context, char const * context_name);
69 #endif
70 #if defined(USE_OPENSL)
71 int
72 opensl_init(cubeb ** context, char const * context_name);
73 #endif
74 #if defined(USE_OSS)
75 int
76 oss_init(cubeb ** context, char const * context_name);
77 #endif
78 #if defined(USE_AAUDIO)
79 int
80 aaudio_init(cubeb ** context, char const * context_name);
81 #endif
82 #if defined(USE_AUDIOTRACK)
83 int
84 audiotrack_init(cubeb ** context, char const * context_name);
85 #endif
86 #if defined(USE_KAI)
87 int
88 kai_init(cubeb ** context, char const * context_name);
89 #endif
90
91 static int
92 validate_stream_params(cubeb_stream_params * input_stream_params,
93 cubeb_stream_params * output_stream_params)
94 {
95 XASSERT(input_stream_params || output_stream_params);
96 if (output_stream_params) {
97 if (output_stream_params->rate < 1000 ||
98 output_stream_params->rate > 768000 ||
99 output_stream_params->channels < 1 ||
100 output_stream_params->channels > UINT8_MAX) {
101 return CUBEB_ERROR_INVALID_FORMAT;
102 }
103 }
104 if (input_stream_params) {
105 if (input_stream_params->rate < 1000 ||
106 input_stream_params->rate > 768000 ||
107 input_stream_params->channels < 1 ||
108 input_stream_params->channels > UINT8_MAX) {
109 return CUBEB_ERROR_INVALID_FORMAT;
110 }
111 }
112 // Rate and sample format must be the same for input and output, if using a
113 // duplex stream
114 if (input_stream_params && output_stream_params) {
115 if (input_stream_params->rate != output_stream_params->rate ||
116 input_stream_params->format != output_stream_params->format) {
117 return CUBEB_ERROR_INVALID_FORMAT;
118 }
119 }
120
121 cubeb_stream_params * params =
122 input_stream_params ? input_stream_params : output_stream_params;
123
124 switch (params->format) {
125 case CUBEB_SAMPLE_S16LE:
126 case CUBEB_SAMPLE_S16BE:
127 case CUBEB_SAMPLE_FLOAT32LE:
128 case CUBEB_SAMPLE_FLOAT32BE:
129 return CUBEB_OK;
130 }
131
132 return CUBEB_ERROR_INVALID_FORMAT;
133 }
134
135 static int
136 validate_latency(int latency)
137 {
138 if (latency < 1 || latency > 96000) {
139 return CUBEB_ERROR_INVALID_PARAMETER;
140 }
141 return CUBEB_OK;
142 }
143
144 int
145 cubeb_init(cubeb ** context, char const * context_name,
146 char const * backend_name)
147 {
148 int (*init_oneshot)(cubeb **, char const *) = NULL;
149
150 if (backend_name != NULL) {
151 if (!strcmp(backend_name, "pulse")) {
152 #if defined(USE_PULSE)
153 init_oneshot = pulse_init;
154 #endif
155 } else if (!strcmp(backend_name, "pulse-rust")) {
156 #if defined(USE_PULSE_RUST)
157 init_oneshot = pulse_rust_init;
158 #endif
159 } else if (!strcmp(backend_name, "jack")) {
160 #if defined(USE_JACK)
161 init_oneshot = jack_init;
162 #endif
163 } else if (!strcmp(backend_name, "alsa")) {
164 #if defined(USE_ALSA)
165 init_oneshot = alsa_init;
166 #endif
167 } else if (!strcmp(backend_name, "audiounit")) {
168 #if defined(USE_AUDIOUNIT)
169 init_oneshot = audiounit_init;
170 #endif
171 } else if (!strcmp(backend_name, "audiounit-rust")) {
172 #if defined(USE_AUDIOUNIT_RUST)
173 init_oneshot = audiounit_rust_init;
174 #endif
175 } else if (!strcmp(backend_name, "wasapi")) {
176 #if defined(USE_WASAPI)
177 init_oneshot = wasapi_init;
178 #endif
179 } else if (!strcmp(backend_name, "winmm")) {
180 #if defined(USE_WINMM)
181 init_oneshot = winmm_init;
182 #endif
183 } else if (!strcmp(backend_name, "sndio")) {
184 #if defined(USE_SNDIO)
185 init_oneshot = sndio_init;
186 #endif
187 } else if (!strcmp(backend_name, "sun")) {
188 #if defined(USE_SUN)
189 init_oneshot = sun_init;
190 #endif
191 } else if (!strcmp(backend_name, "opensl")) {
192 #if defined(USE_OPENSL)
193 init_oneshot = opensl_init;
194 #endif
195 } else if (!strcmp(backend_name, "oss")) {
196 #if defined(USE_OSS)
197 init_oneshot = oss_init;
198 #endif
199 } else if (!strcmp(backend_name, "aaudio")) {
200 #if defined(USE_AAUDIO)
201 init_oneshot = aaudio_init;
202 #endif
203 } else if (!strcmp(backend_name, "audiotrack")) {
204 #if defined(USE_AUDIOTRACK)
205 init_oneshot = audiotrack_init;
206 #endif
207 } else if (!strcmp(backend_name, "kai")) {
208 #if defined(USE_KAI)
209 init_oneshot = kai_init;
210 #endif
211 } else {
212 /* Already set */
213 }
214 }
215
216 int (*default_init[])(cubeb **, char const *) = {
217 /*
218 * init_oneshot must be at the top to allow user
219 * to override all other choices
220 */
221 init_oneshot,
222 #if defined(USE_PULSE_RUST)
223 pulse_rust_init,
224 #endif
225 #if defined(USE_PULSE)
226 pulse_init,
227 #endif
228 #if defined(USE_JACK)
229 jack_init,
230 #endif
231 #if defined(USE_SNDIO)
232 sndio_init,
233 #endif
234 #if defined(USE_ALSA)
235 alsa_init,
236 #endif
237 #if defined(USE_OSS)
238 oss_init,
239 #endif
240 #if defined(USE_AUDIOUNIT_RUST)
241 audiounit_rust_init,
242 #endif
243 #if defined(USE_AUDIOUNIT)
244 audiounit_init,
245 #endif
246 #if defined(USE_WASAPI)
247 wasapi_init,
248 #endif
249 #if defined(USE_WINMM)
250 winmm_init,
251 #endif
252 #if defined(USE_SUN)
253 sun_init,
254 #endif
255 #if defined(USE_AAUDIO)
256 aaudio_init,
257 #endif
258 #if defined(USE_OPENSL)
259 opensl_init,
260 #endif
261 #if defined(USE_AUDIOTRACK)
262 audiotrack_init,
263 #endif
264 #if defined(USE_KAI)
265 kai_init,
266 #endif
267 };
268 int i;
269
270 if (!context) {
271 return CUBEB_ERROR_INVALID_PARAMETER;
272 }
273
274 #define OK(fn) assert((*context)->ops->fn)
275 for (i = 0; i < NELEMS(default_init); ++i) {
276 if (default_init[i] && default_init[i](context, context_name) == CUBEB_OK) {
277 /* Assert that the minimal API is implemented. */
278 OK(get_backend_id);
279 OK(destroy);
280 OK(stream_init);
281 OK(stream_destroy);
282 OK(stream_start);
283 OK(stream_stop);
284 OK(stream_get_position);
285 return CUBEB_OK;
286 }
287 }
288 return CUBEB_ERROR;
289 }
290
291 char const *
292 cubeb_get_backend_id(cubeb * context)
293 {
294 if (!context) {
295 return NULL;
296 }
297
298 return context->ops->get_backend_id(context);
299 }
300
301 int
302 cubeb_get_max_channel_count(cubeb * context, uint32_t * max_channels)
303 {
304 if (!context || !max_channels) {
305 return CUBEB_ERROR_INVALID_PARAMETER;
306 }
307
308 if (!context->ops->get_max_channel_count) {
309 return CUBEB_ERROR_NOT_SUPPORTED;
310 }
311
312 return context->ops->get_max_channel_count(context, max_channels);
313 }
314
315 int
316 cubeb_get_min_latency(cubeb * context, cubeb_stream_params * params,
317 uint32_t * latency_ms)
318 {
319 if (!context || !params || !latency_ms) {
320 return CUBEB_ERROR_INVALID_PARAMETER;
321 }
322
323 if (!context->ops->get_min_latency) {
324 return CUBEB_ERROR_NOT_SUPPORTED;
325 }
326
327 return context->ops->get_min_latency(context, *params, latency_ms);
328 }
329
330 int
331 cubeb_get_preferred_sample_rate(cubeb * context, uint32_t * rate)
332 {
333 if (!context || !rate) {
334 return CUBEB_ERROR_INVALID_PARAMETER;
335 }
336
337 if (!context->ops->get_preferred_sample_rate) {
338 return CUBEB_ERROR_NOT_SUPPORTED;
339 }
340
341 return context->ops->get_preferred_sample_rate(context, rate);
342 }
343
344 int
345 cubeb_get_supported_input_processing_params(
346 cubeb * context, cubeb_input_processing_params * params)
347 {
348 if (!context || !params) {
349 return CUBEB_ERROR_INVALID_PARAMETER;
350 }
351
352 if (!context->ops->get_supported_input_processing_params) {
353 return CUBEB_ERROR_NOT_SUPPORTED;
354 }
355
356 return context->ops->get_supported_input_processing_params(context, params);
357 }
358
359 void
360 cubeb_destroy(cubeb * context)
361 {
362 if (!context) {
363 return;
364 }
365
366 context->ops->destroy(context);
367
368 cubeb_set_log_callback(CUBEB_LOG_DISABLED, NULL);
369 }
370
371 int
372 cubeb_stream_init(cubeb * context, cubeb_stream ** stream,
373 char const * stream_name, cubeb_devid input_device,
374 cubeb_stream_params * input_stream_params,
375 cubeb_devid output_device,
376 cubeb_stream_params * output_stream_params,
377 unsigned int latency, cubeb_data_callback data_callback,
378 cubeb_state_callback state_callback, void * user_ptr)
379 {
380 int r;
381
382 if (!context || !stream || !data_callback || !state_callback) {
383 return CUBEB_ERROR_INVALID_PARAMETER;
384 }
385
386 if ((r = validate_stream_params(input_stream_params, output_stream_params)) !=
387 CUBEB_OK ||
388 (r = validate_latency(latency)) != CUBEB_OK) {
389 return r;
390 }
391
392 r = context->ops->stream_init(context, stream, stream_name, input_device,
393 input_stream_params, output_device,
394 output_stream_params, latency, data_callback,
395 state_callback, user_ptr);
396
397 if (r == CUBEB_ERROR_INVALID_FORMAT) {
398 LOG("Invalid format, %p %p %d %d", output_stream_params,
399 input_stream_params,
400 output_stream_params && output_stream_params->format,
401 input_stream_params && input_stream_params->format);
402 }
403
404 return r;
405 }
406
407 void
408 cubeb_stream_destroy(cubeb_stream * stream)
409 {
410 if (!stream) {
411 return;
412 }
413
414 stream->context->ops->stream_destroy(stream);
415 }
416
417 int
418 cubeb_stream_start(cubeb_stream * stream)
419 {
420 if (!stream) {
421 return CUBEB_ERROR_INVALID_PARAMETER;
422 }
423
424 return stream->context->ops->stream_start(stream);
425 }
426
427 int
428 cubeb_stream_stop(cubeb_stream * stream)
429 {
430 if (!stream) {
431 return CUBEB_ERROR_INVALID_PARAMETER;
432 }
433
434 return stream->context->ops->stream_stop(stream);
435 }
436
437 int
438 cubeb_stream_get_position(cubeb_stream * stream, uint64_t * position)
439 {
440 if (!stream || !position) {
441 return CUBEB_ERROR_INVALID_PARAMETER;
442 }
443
444 return stream->context->ops->stream_get_position(stream, position);
445 }
446
447 int
448 cubeb_stream_get_latency(cubeb_stream * stream, uint32_t * latency)
449 {
450 if (!stream || !latency) {
451 return CUBEB_ERROR_INVALID_PARAMETER;
452 }
453
454 if (!stream->context->ops->stream_get_latency) {
455 return CUBEB_ERROR_NOT_SUPPORTED;
456 }
457
458 return stream->context->ops->stream_get_latency(stream, latency);
459 }
460
461 int
462 cubeb_stream_get_input_latency(cubeb_stream * stream, uint32_t * latency)
463 {
464 if (!stream || !latency) {
465 return CUBEB_ERROR_INVALID_PARAMETER;
466 }
467
468 if (!stream->context->ops->stream_get_input_latency) {
469 return CUBEB_ERROR_NOT_SUPPORTED;
470 }
471
472 return stream->context->ops->stream_get_input_latency(stream, latency);
473 }
474
475 int
476 cubeb_stream_set_volume(cubeb_stream * stream, float volume)
477 {
478 if (!stream || volume > 1.0 || volume < 0.0) {
479 return CUBEB_ERROR_INVALID_PARAMETER;
480 }
481
482 if (!stream->context->ops->stream_set_volume) {
483 return CUBEB_ERROR_NOT_SUPPORTED;
484 }
485
486 return stream->context->ops->stream_set_volume(stream, volume);
487 }
488
489 int
490 cubeb_stream_set_name(cubeb_stream * stream, char const * stream_name)
491 {
492 if (!stream || !stream_name) {
493 return CUBEB_ERROR_INVALID_PARAMETER;
494 }
495
496 if (!stream->context->ops->stream_set_name) {
497 return CUBEB_ERROR_NOT_SUPPORTED;
498 }
499
500 return stream->context->ops->stream_set_name(stream, stream_name);
501 }
502
503 int
504 cubeb_stream_get_current_device(cubeb_stream * stream,
505 cubeb_device ** const device)
506 {
507 if (!stream || !device) {
508 return CUBEB_ERROR_INVALID_PARAMETER;
509 }
510
511 if (!stream->context->ops->stream_get_current_device) {
512 return CUBEB_ERROR_NOT_SUPPORTED;
513 }
514
515 return stream->context->ops->stream_get_current_device(stream, device);
516 }
517
518 int
519 cubeb_stream_set_input_mute(cubeb_stream * stream, int mute)
520 {
521 if (!stream) {
522 return CUBEB_ERROR_INVALID_PARAMETER;
523 }
524
525 if (!stream->context->ops->stream_set_input_mute) {
526 return CUBEB_ERROR_NOT_SUPPORTED;
527 }
528
529 return stream->context->ops->stream_set_input_mute(stream, mute);
530 }
531
532 int
533 cubeb_stream_set_input_processing_params(cubeb_stream * stream,
534 cubeb_input_processing_params params)
535 {
536 if (!stream) {
537 return CUBEB_ERROR_INVALID_PARAMETER;
538 }
539
540 if (!stream->context->ops->stream_set_input_processing_params) {
541 return CUBEB_ERROR_NOT_SUPPORTED;
542 }
543
544 return stream->context->ops->stream_set_input_processing_params(stream,
545 params);
546 }
547
548 int
549 cubeb_stream_device_destroy(cubeb_stream * stream, cubeb_device * device)
550 {
551 if (!stream || !device) {
552 return CUBEB_ERROR_INVALID_PARAMETER;
553 }
554
555 if (!stream->context->ops->stream_device_destroy) {
556 return CUBEB_ERROR_NOT_SUPPORTED;
557 }
558
559 return stream->context->ops->stream_device_destroy(stream, device);
560 }
561
562 int
563 cubeb_stream_register_device_changed_callback(
564 cubeb_stream * stream,
565 cubeb_device_changed_callback device_changed_callback)
566 {
567 if (!stream) {
568 return CUBEB_ERROR_INVALID_PARAMETER;
569 }
570
571 if (!stream->context->ops->stream_register_device_changed_callback) {
572 return CUBEB_ERROR_NOT_SUPPORTED;
573 }
574
575 return stream->context->ops->stream_register_device_changed_callback(
576 stream, device_changed_callback);
577 }
578
579 void *
580 cubeb_stream_user_ptr(cubeb_stream * stream)
581 {
582 if (!stream) {
583 return NULL;
584 }
585
586 return stream->user_ptr;
587 }
588
589 static void
590 log_device(cubeb_device_info * device_info)
591 {
592 char devfmts[128] = "";
593 const char *devtype, *devstate, *devdeffmt;
594
595 switch (device_info->type) {
596 case CUBEB_DEVICE_TYPE_INPUT:
597 devtype = "input";
598 break;
599 case CUBEB_DEVICE_TYPE_OUTPUT:
600 devtype = "output";
601 break;
602 case CUBEB_DEVICE_TYPE_UNKNOWN:
603 default:
604 devtype = "unknown?";
605 break;
606 };
607
608 switch (device_info->state) {
609 case CUBEB_DEVICE_STATE_DISABLED:
610 devstate = "disabled";
611 break;
612 case CUBEB_DEVICE_STATE_UNPLUGGED:
613 devstate = "unplugged";
614 break;
615 case CUBEB_DEVICE_STATE_ENABLED:
616 devstate = "enabled";
617 break;
618 default:
619 devstate = "unknown?";
620 break;
621 };
622
623 switch (device_info->default_format) {
624 case CUBEB_DEVICE_FMT_S16LE:
625 devdeffmt = "S16LE";
626 break;
627 case CUBEB_DEVICE_FMT_S16BE:
628 devdeffmt = "S16BE";
629 break;
630 case CUBEB_DEVICE_FMT_F32LE:
631 devdeffmt = "F32LE";
632 break;
633 case CUBEB_DEVICE_FMT_F32BE:
634 devdeffmt = "F32BE";
635 break;
636 default:
637 devdeffmt = "unknown?";
638 break;
639 };
640
641 if (device_info->format & CUBEB_DEVICE_FMT_S16LE) {
642 strcat(devfmts, " S16LE");
643 }
644 if (device_info->format & CUBEB_DEVICE_FMT_S16BE) {
645 strcat(devfmts, " S16BE");
646 }
647 if (device_info->format & CUBEB_DEVICE_FMT_F32LE) {
648 strcat(devfmts, " F32LE");
649 }
650 if (device_info->format & CUBEB_DEVICE_FMT_F32BE) {
651 strcat(devfmts, " F32BE");
652 }
653
654 LOG("DeviceID: \"%s\"%s\n"
655 "\tName:\t\"%s\"\n"
656 "\tGroup:\t\"%s\"\n"
657 "\tVendor:\t\"%s\"\n"
658 "\tType:\t%s\n"
659 "\tState:\t%s\n"
660 "\tMaximum channels:\t%u\n"
661 "\tFormat:\t%s (0x%x) (default: %s)\n"
662 "\tRate:\t[%u, %u] (default: %u)\n"
663 "\tLatency: lo %u frames, hi %u frames",
664 device_info->device_id, device_info->preferred ? " (PREFERRED)" : "",
665 device_info->friendly_name, device_info->group_id,
666 device_info->vendor_name, devtype, devstate, device_info->max_channels,
667 (devfmts[0] == '\0') ? devfmts : devfmts + 1,
668 (unsigned int)device_info->format, devdeffmt, device_info->min_rate,
669 device_info->max_rate, device_info->default_rate, device_info->latency_lo,
670 device_info->latency_hi);
671 }
672
673 int
674 cubeb_enumerate_devices(cubeb * context, cubeb_device_type devtype,
675 cubeb_device_collection * collection)
676 {
677 int rv;
678 if ((devtype & (CUBEB_DEVICE_TYPE_INPUT | CUBEB_DEVICE_TYPE_OUTPUT)) == 0)
679 return CUBEB_ERROR_INVALID_PARAMETER;
680 if (context == NULL || collection == NULL)
681 return CUBEB_ERROR_INVALID_PARAMETER;
682 if (!context->ops->enumerate_devices)
683 return CUBEB_ERROR_NOT_SUPPORTED;
684
685 rv = context->ops->enumerate_devices(context, devtype, collection);
686
687 if (cubeb_log_get_callback()) {
688 for (size_t i = 0; i < collection->count; i++) {
689 log_device(&collection->device[i]);
690 }
691 }
692
693 return rv;
694 }
695
696 int
697 cubeb_device_collection_destroy(cubeb * context,
698 cubeb_device_collection * collection)
699 {
700 int r;
701
702 if (context == NULL || collection == NULL)
703 return CUBEB_ERROR_INVALID_PARAMETER;
704
705 if (!context->ops->device_collection_destroy)
706 return CUBEB_ERROR_NOT_SUPPORTED;
707
708 if (!collection->device)
709 return CUBEB_OK;
710
711 r = context->ops->device_collection_destroy(context, collection);
712 if (r == CUBEB_OK) {
713 collection->device = NULL;
714 collection->count = 0;
715 }
716
717 return r;
718 }
719
720 int
721 cubeb_register_device_collection_changed(
722 cubeb * context, cubeb_device_type devtype,
723 cubeb_device_collection_changed_callback callback, void * user_ptr)
724 {
725 if (context == NULL ||
726 (devtype & (CUBEB_DEVICE_TYPE_INPUT | CUBEB_DEVICE_TYPE_OUTPUT)) == 0)
727 return CUBEB_ERROR_INVALID_PARAMETER;
728
729 if (!context->ops->register_device_collection_changed) {
730 return CUBEB_ERROR_NOT_SUPPORTED;
731 }
732
733 return context->ops->register_device_collection_changed(context, devtype,
734 callback, user_ptr);
735 }
736
737 int
738 cubeb_set_log_callback(cubeb_log_level log_level,
739 cubeb_log_callback log_callback)
740 {
741 if (log_level < CUBEB_LOG_DISABLED || log_level > CUBEB_LOG_VERBOSE) {
742 return CUBEB_ERROR_INVALID_FORMAT;
743 }
744
745 if (!log_callback && log_level != CUBEB_LOG_DISABLED) {
746 return CUBEB_ERROR_INVALID_PARAMETER;
747 }
748
749 if (cubeb_log_get_callback() && log_callback) {
750 return CUBEB_ERROR_NOT_SUPPORTED;
751 }
752
753 cubeb_log_set(log_level, log_callback);
754
755 return CUBEB_OK;
756 }