source: mainline/uspace/lib/drv/generic/remote_audio_pcm.c@ d7f7a4a

Last change on this file since d7f7a4a was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 4 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

  • Property mode set to 100644
File size: 22.8 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2012 Jan Vesely
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6/** @addtogroup libdrv
7 * @{
8 */
9/** @file
10 */
11
12#include <async.h>
13#include <devman.h>
14#include <ddf/log.h>
15#include <errno.h>
16#include <macros.h>
17#include <str.h>
18#include <as.h>
19
20#include "audio_pcm_iface.h"
21#include "ddf/driver.h"
22
23typedef enum {
24 IPC_M_AUDIO_PCM_GET_INFO_STR,
25 IPC_M_AUDIO_PCM_QUERY_CAPS,
26 IPC_M_AUDIO_PCM_REGISTER_EVENTS,
27 IPC_M_AUDIO_PCM_UNREGISTER_EVENTS,
28 IPC_M_AUDIO_PCM_TEST_FORMAT,
29 IPC_M_AUDIO_PCM_GET_BUFFER,
30 IPC_M_AUDIO_PCM_RELEASE_BUFFER,
31 IPC_M_AUDIO_PCM_GET_BUFFER_POS,
32 IPC_M_AUDIO_PCM_START_PLAYBACK,
33 IPC_M_AUDIO_PCM_STOP_PLAYBACK,
34 IPC_M_AUDIO_PCM_START_CAPTURE,
35 IPC_M_AUDIO_PCM_STOP_CAPTURE,
36} audio_pcm_iface_funcs_t;
37
38/**
39 * Get human readable capability name.
40 * @param cap audio capability.
41 * @return Valid string
42 */
43const char *audio_pcm_cap_str(audio_cap_t cap)
44{
45 static const char *caps[] = {
46 [AUDIO_CAP_CAPTURE] = "CAPTURE",
47 [AUDIO_CAP_PLAYBACK] = "PLAYBACK",
48 [AUDIO_CAP_MAX_BUFFER] = "MAXIMUM BUFFER SIZE",
49 [AUDIO_CAP_BUFFER_POS] = "KNOWS BUFFER POSITION",
50 [AUDIO_CAP_INTERRUPT] = "FRAGMENT INTERRUPTS",
51 [AUDIO_CAP_INTERRUPT_MIN_FRAMES] = "MINIMUM FRAGMENT SIZE",
52 [AUDIO_CAP_INTERRUPT_MAX_FRAMES] = "MAXIMUM FRAGMENT SIZE",
53 };
54 if (cap >= ARRAY_SIZE(caps))
55 return "UNKNOWN CAP";
56 return caps[cap];
57
58}
59
60/**
61 * Get human readable event name.
62 * @param event Audio device event
63 * @return Valid string
64 */
65const char *audio_pcm_event_str(pcm_event_t event)
66{
67 static const char *events[] = {
68 [PCM_EVENT_PLAYBACK_STARTED] = "PLAYBACK STARTED",
69 [PCM_EVENT_CAPTURE_STARTED] = "CAPTURE STARTED",
70 [PCM_EVENT_FRAMES_PLAYED] = "FRAGMENT PLAYED",
71 [PCM_EVENT_FRAMES_CAPTURED] = "FRAGMENT CAPTURED",
72 [PCM_EVENT_PLAYBACK_TERMINATED] = "PLAYBACK TERMINATED",
73 [PCM_EVENT_CAPTURE_TERMINATED] = "CAPTURE TERMINATED",
74 };
75 if (event >= ARRAY_SIZE(events))
76 return "UNKNOWN EVENT";
77 return events[event];
78}
79
80/*
81 * CLIENT SIDE
82 */
83
84/**
85 * Open audio session with the first registered device.
86 *
87 * @return Pointer to a new audio device session, NULL on failure.
88 */
89audio_pcm_sess_t *audio_pcm_open_default(void)
90{
91 static bool resolved = false;
92 static category_id_t pcm_id = 0;
93 if (!resolved) {
94 const errno_t ret = loc_category_get_id("audio-pcm", &pcm_id,
95 IPC_FLAG_BLOCKING);
96 if (ret != EOK)
97 return NULL;
98 resolved = true;
99 }
100
101 service_id_t *svcs = NULL;
102 size_t count = 0;
103 const errno_t ret = loc_category_get_svcs(pcm_id, &svcs, &count);
104 if (ret != EOK)
105 return NULL;
106
107 audio_pcm_sess_t *session = NULL;
108 if (count)
109 session = audio_pcm_open_service(svcs[0]);
110 free(svcs);
111 return session;
112}
113
114/**
115 * Open audio session with device identified by location service string.
116 *
117 * @param name Location service string.
118 * @return Pointer to a new audio device session, NULL on failure.
119 */
120audio_pcm_sess_t *audio_pcm_open(const char *name)
121{
122 service_id_t sid;
123 const errno_t ret = loc_service_get_id(name, &sid, 0);
124 if (ret != EOK)
125 return NULL;
126 return loc_service_connect(sid, INTERFACE_DDF, 0);
127}
128
129/**
130 * Open audio session with device identified by location service id
131 *
132 * @param name Location service id.
133 * @return Pointer to a new audio device session, NULL on failure.
134 */
135audio_pcm_sess_t *audio_pcm_open_service(service_id_t id)
136{
137 return loc_service_connect(id, INTERFACE_DDF, IPC_FLAG_BLOCKING);
138}
139
140/**
141 * Close open audio device session.
142 *
143 * @param name Open audio device session.
144 *
145 * @note Calling this function on already closed or invalid session results
146 * in undefined behavior.
147 */
148void audio_pcm_close(audio_pcm_sess_t *sess)
149{
150 if (sess)
151 async_hangup(sess);
152}
153
154/**
155 * Get a short description string.
156 *
157 * @param sess Audio device session.
158 * @param name Place to store newly allocated string.
159 *
160 * @return Error code.
161 *
162 * @note Caller is responsible for freeing newly allocated memory.
163 */
164errno_t audio_pcm_get_info_str(audio_pcm_sess_t *sess, char **name)
165{
166 if (!name)
167 return EINVAL;
168 async_exch_t *exch = async_exchange_begin(sess);
169 sysarg_t name_size;
170 const errno_t ret = async_req_1_1(exch,
171 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
172 IPC_M_AUDIO_PCM_GET_INFO_STR, &name_size);
173 if (ret == EOK) {
174 char *name_place = calloc(1, name_size);
175 if (!name_place) {
176 /*
177 * Make the other side fail
178 * as it waits for read request
179 */
180 async_data_read_start(exch, (void *)-1, 0);
181 async_exchange_end(exch);
182 return ENOMEM;
183 }
184 const errno_t ret =
185 async_data_read_start(exch, name_place, name_size);
186 if (ret != EOK) {
187 free(name_place);
188 async_exchange_end(exch);
189 return ret;
190 }
191 *name = name_place;
192 }
193 async_exchange_end(exch);
194 return ret;
195}
196
197/**
198 * Query value of specified capability.
199 *
200 * @param sess Audio device session.
201 * @param cap Audio device capability.
202 * @param[out] val Place to store queried value.
203 *
204 * @return Error code.
205 */
206errno_t audio_pcm_query_cap(audio_pcm_sess_t *sess, audio_cap_t cap, sysarg_t *value)
207{
208 async_exch_t *exch = async_exchange_begin(sess);
209 const errno_t ret = async_req_2_1(exch,
210 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_QUERY_CAPS,
211 cap, value);
212 async_exchange_end(exch);
213 return ret;
214}
215
216/**
217 * Query current position in device buffer.
218 *
219 * @param sess Audio device session.
220 * @param pos Place to store the result.
221 *
222 * @return Error code.
223 *
224 * Works for both playback and capture.
225 */
226errno_t audio_pcm_get_buffer_pos(audio_pcm_sess_t *sess, size_t *pos)
227{
228 if (!pos)
229 return EINVAL;
230 async_exch_t *exch = async_exchange_begin(sess);
231 sysarg_t value = 0;
232 const errno_t ret = async_req_1_1(exch,
233 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
234 IPC_M_AUDIO_PCM_GET_BUFFER_POS, &value);
235 if (ret == EOK)
236 *pos = value;
237 async_exchange_end(exch);
238 return ret;
239}
240
241/**
242 * Test format parameters for device support.
243 *
244 * @param sess Audio device session.
245 * @param channels Number of channels
246 * @param rate Sampling rate.
247 * @format Sample format.
248 *
249 * @return Error code.
250 *
251 * Works for both playback and capture. This function modifies provided
252 * parameters to the nearest values supported by the device.
253 */
254errno_t audio_pcm_test_format(audio_pcm_sess_t *sess, unsigned *channels,
255 unsigned *rate, pcm_sample_format_t *format)
256{
257 async_exch_t *exch = async_exchange_begin(sess);
258 sysarg_t channels_arg = channels ? *channels : 0;
259 sysarg_t rate_arg = rate ? *rate : 0;
260 sysarg_t format_arg = format ? *format : 0;
261 const errno_t ret = async_req_4_3(exch,
262 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
263 IPC_M_AUDIO_PCM_TEST_FORMAT, channels_arg, rate_arg, format_arg,
264 &channels_arg, &rate_arg, &format_arg);
265 async_exchange_end(exch);
266
267 /*
268 * All OK or something has changed. Verify that it was not one of the
269 * params we care about
270 */
271 if ((ret == EOK || ret == ELIMIT) &&
272 (!channels || *channels == channels_arg) &&
273 (!rate || *rate == rate_arg) &&
274 (!format || *format == format_arg))
275 return EOK;
276 if (channels)
277 *channels = channels_arg;
278 if (rate)
279 *rate = rate_arg;
280 if (format)
281 *format = format_arg;
282 return ret;
283}
284
285/**
286 * Register callback for device generated events.
287 *
288 * @param sess Audio device session.
289 * @param event_rec Event callback function.
290 * @param arg Event callback custom parameter.
291 *
292 * @return Error code.
293 */
294errno_t audio_pcm_register_event_callback(audio_pcm_sess_t *sess,
295 async_port_handler_t event_callback, void *arg)
296{
297 if (!event_callback)
298 return EINVAL;
299
300 async_exch_t *exch = async_exchange_begin(sess);
301
302 errno_t ret = async_req_1_0(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
303 IPC_M_AUDIO_PCM_REGISTER_EVENTS);
304 if (ret == EOK) {
305 port_id_t port;
306 ret = async_create_callback_port(exch, INTERFACE_AUDIO_PCM_CB, 0, 0,
307 event_callback, arg, &port);
308 }
309
310 async_exchange_end(exch);
311 return ret;
312}
313
314/**
315 * Unregister callback for device generated events.
316 *
317 * @param sess Audio device session.
318 *
319 * @return Error code.
320 */
321errno_t audio_pcm_unregister_event_callback(audio_pcm_sess_t *sess)
322{
323 async_exch_t *exch = async_exchange_begin(sess);
324 const errno_t ret = async_req_1_0(exch,
325 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
326 IPC_M_AUDIO_PCM_UNREGISTER_EVENTS);
327 async_exchange_end(exch);
328 return ret;
329}
330
331/**
332 * Get device accessible playback/capture buffer.
333 *
334 * @param sess Audio device session.
335 * @param buffer Place to store pointer to the buffer.
336 * @param size Place to store buffer size (bytes).
337 *
338 * @return Error code.
339 */
340errno_t audio_pcm_get_buffer(audio_pcm_sess_t *sess, void **buffer, size_t *size)
341{
342 if (!buffer || !size)
343 return EINVAL;
344
345 async_exch_t *exch = async_exchange_begin(sess);
346
347 sysarg_t buffer_size = *size;
348 errno_t ret = async_req_2_1(exch, DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
349 IPC_M_AUDIO_PCM_GET_BUFFER, (sysarg_t)buffer_size, &buffer_size);
350 if (ret == EOK) {
351 void *dst = NULL;
352 ret = async_share_in_start_0_0(exch, buffer_size, &dst);
353 if (ret != EOK) {
354 async_exchange_end(exch);
355 return ret;
356 }
357 *buffer = dst;
358 *size = buffer_size;
359 }
360 async_exchange_end(exch);
361 return ret;
362}
363
364/**
365 * Release device accessible playback/capture buffer.
366 *
367 * @param sess Audio device session.
368 *
369 * @return Error code.
370 */
371errno_t audio_pcm_release_buffer(audio_pcm_sess_t *sess)
372{
373 async_exch_t *exch = async_exchange_begin(sess);
374 const errno_t ret = async_req_1_0(exch,
375 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
376 IPC_M_AUDIO_PCM_RELEASE_BUFFER);
377 async_exchange_end(exch);
378 return ret;
379}
380
381/**
382 * Start playback on buffer from position 0.
383 *
384 * @param sess Audio device session.
385 * @param frames Size of fragment (in frames).
386 * @param channels Number of channels.
387 * @param sample_rate Sampling rate (for one channel).
388 * @param format Sample format.
389 *
390 * @return Error code.
391 *
392 * Event will be generated after every fragment. Set fragment size to
393 * 0 to turn off event generation.
394 */
395errno_t audio_pcm_start_playback_fragment(audio_pcm_sess_t *sess, unsigned frames,
396 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
397{
398 if (channels > UINT16_MAX)
399 return EINVAL;
400 assert((format & UINT16_MAX) == format);
401 const sysarg_t packed = (channels << 16) | (format & UINT16_MAX);
402 async_exch_t *exch = async_exchange_begin(sess);
403 const errno_t ret = async_req_4_0(exch,
404 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
405 IPC_M_AUDIO_PCM_START_PLAYBACK,
406 frames, sample_rate, packed);
407 async_exchange_end(exch);
408 return ret;
409}
410/**
411 * Stops playback after current fragment.
412 *
413 * @param sess Audio device session.
414 *
415 * @return Error code.
416 */
417errno_t audio_pcm_last_playback_fragment(audio_pcm_sess_t *sess)
418{
419 async_exch_t *exch = async_exchange_begin(sess);
420 const errno_t ret = async_req_2_0(exch,
421 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
422 IPC_M_AUDIO_PCM_STOP_PLAYBACK, false);
423 async_exchange_end(exch);
424 return ret;
425}
426
427/**
428 * Start playback on buffer from the current position.
429 *
430 * @param sess Audio device session.
431 * @param channels Number of channels.
432 * @param sample_rate Sampling rate (for one channel).
433 * @param format Sample format.
434 *
435 * @return Error code.
436 */
437errno_t audio_pcm_start_playback(audio_pcm_sess_t *sess,
438 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
439{
440 return audio_pcm_start_playback_fragment(
441 sess, 0, channels, sample_rate, format);
442}
443
444/**
445 * Immediately stops current playback.
446 *
447 * @param sess Audio device session.
448 *
449 * @return Error code.
450 */
451errno_t audio_pcm_stop_playback_immediate(audio_pcm_sess_t *sess)
452{
453 async_exch_t *exch = async_exchange_begin(sess);
454 const errno_t ret = async_req_2_0(exch,
455 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
456 IPC_M_AUDIO_PCM_STOP_PLAYBACK, true);
457 async_exchange_end(exch);
458 return ret;
459}
460
461/**
462 * Stops playback at the end of the current fragment.
463 *
464 * @param sess Audio device session.
465 *
466 * @return Error code.
467 */
468errno_t audio_pcm_stop_playback(audio_pcm_sess_t *sess)
469{
470 async_exch_t *exch = async_exchange_begin(sess);
471 const errno_t ret = async_req_2_0(exch,
472 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
473 IPC_M_AUDIO_PCM_STOP_PLAYBACK, false);
474 async_exchange_end(exch);
475 return ret;
476}
477
478/**
479 * Start capture on buffer from the current position.
480 *
481 * @param sess Audio device session.
482 * @param frames Size of fragment (in frames).
483 * @param channels Number of channels.
484 * @param sample_rate Sampling rate (for one channel).
485 * @param format Sample format.
486 *
487 * @return Error code.
488 *
489 * Event will be generated after every fragment. Set fragment size to
490 * 0 to turn off event generation.
491 */
492errno_t audio_pcm_start_capture_fragment(audio_pcm_sess_t *sess, unsigned frames,
493 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
494{
495 if (channels > UINT16_MAX)
496 return EINVAL;
497 assert((format & UINT16_MAX) == format);
498 const sysarg_t packed = (channels << 16) | (format & UINT16_MAX);
499 async_exch_t *exch = async_exchange_begin(sess);
500 const errno_t ret = async_req_4_0(exch,
501 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_START_CAPTURE,
502 frames, sample_rate, packed);
503 async_exchange_end(exch);
504 return ret;
505}
506
507/**
508 * Start capture on buffer from the current position.
509 *
510 * @param sess Audio device session.
511 * @param channels Number of channels.
512 * @param sample_rate Sampling rate (for one channel).
513 * @param format Sample format.
514 *
515 * @return Error code.
516 */
517errno_t audio_pcm_start_capture(audio_pcm_sess_t *sess,
518 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
519{
520 return audio_pcm_start_capture_fragment(
521 sess, 0, channels, sample_rate, format);
522}
523
524/**
525 * Stops capture at the end of current fragment.
526 *
527 * Won't work if capture was started with fragment size 0.
528 * @param sess Audio device session.
529 *
530 * @return Error code.
531 */
532errno_t audio_pcm_last_capture_fragment(audio_pcm_sess_t *sess)
533{
534 async_exch_t *exch = async_exchange_begin(sess);
535 const errno_t ret = async_req_2_0(exch,
536 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
537 IPC_M_AUDIO_PCM_STOP_CAPTURE, false);
538 async_exchange_end(exch);
539 return ret;
540}
541
542/**
543 * Immediately stops current capture.
544 *
545 * @param sess Audio device session.
546 *
547 * @return Error code.
548 */
549errno_t audio_pcm_stop_capture_immediate(audio_pcm_sess_t *sess)
550{
551 async_exch_t *exch = async_exchange_begin(sess);
552 const errno_t ret = async_req_2_0(exch,
553 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
554 IPC_M_AUDIO_PCM_STOP_CAPTURE, true);
555 async_exchange_end(exch);
556 return ret;
557}
558
559/**
560 * Stops capture at the end of the current fragment.
561 *
562 * @param sess Audio device session.
563 *
564 * @return Error code.
565 */
566errno_t audio_pcm_stop_capture(audio_pcm_sess_t *sess)
567{
568 async_exch_t *exch = async_exchange_begin(sess);
569 const errno_t ret = async_req_2_0(exch,
570 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
571 IPC_M_AUDIO_PCM_STOP_CAPTURE, false);
572 async_exchange_end(exch);
573 return ret;
574}
575
576/*
577 * SERVER SIDE
578 */
579static void remote_audio_pcm_get_info_str(ddf_fun_t *, void *, ipc_call_t *);
580static void remote_audio_pcm_query_caps(ddf_fun_t *, void *, ipc_call_t *);
581static void remote_audio_pcm_events_register(ddf_fun_t *, void *, ipc_call_t *);
582static void remote_audio_pcm_events_unregister(ddf_fun_t *, void *, ipc_call_t *);
583static void remote_audio_pcm_get_buffer_pos(ddf_fun_t *, void *, ipc_call_t *);
584static void remote_audio_pcm_test_format(ddf_fun_t *, void *, ipc_call_t *);
585static void remote_audio_pcm_get_buffer(ddf_fun_t *, void *, ipc_call_t *);
586static void remote_audio_pcm_release_buffer(ddf_fun_t *, void *, ipc_call_t *);
587static void remote_audio_pcm_start_playback(ddf_fun_t *, void *, ipc_call_t *);
588static void remote_audio_pcm_stop_playback(ddf_fun_t *, void *, ipc_call_t *);
589static void remote_audio_pcm_start_capture(ddf_fun_t *, void *, ipc_call_t *);
590static void remote_audio_pcm_stop_capture(ddf_fun_t *, void *, ipc_call_t *);
591
592/** Remote audio pcm buffer interface operations. */
593static const remote_iface_func_ptr_t remote_audio_pcm_iface_ops[] = {
594 [IPC_M_AUDIO_PCM_GET_INFO_STR] = remote_audio_pcm_get_info_str,
595 [IPC_M_AUDIO_PCM_QUERY_CAPS] = remote_audio_pcm_query_caps,
596 [IPC_M_AUDIO_PCM_REGISTER_EVENTS] = remote_audio_pcm_events_register,
597 [IPC_M_AUDIO_PCM_UNREGISTER_EVENTS] = remote_audio_pcm_events_unregister,
598 [IPC_M_AUDIO_PCM_GET_BUFFER_POS] = remote_audio_pcm_get_buffer_pos,
599 [IPC_M_AUDIO_PCM_TEST_FORMAT] = remote_audio_pcm_test_format,
600 [IPC_M_AUDIO_PCM_GET_BUFFER] = remote_audio_pcm_get_buffer,
601 [IPC_M_AUDIO_PCM_RELEASE_BUFFER] = remote_audio_pcm_release_buffer,
602 [IPC_M_AUDIO_PCM_START_PLAYBACK] = remote_audio_pcm_start_playback,
603 [IPC_M_AUDIO_PCM_STOP_PLAYBACK] = remote_audio_pcm_stop_playback,
604 [IPC_M_AUDIO_PCM_START_CAPTURE] = remote_audio_pcm_start_capture,
605 [IPC_M_AUDIO_PCM_STOP_CAPTURE] = remote_audio_pcm_stop_capture,
606};
607
608/** Remote audio mixer interface structure. */
609const remote_iface_t remote_audio_pcm_iface = {
610 .method_count = ARRAY_SIZE(remote_audio_pcm_iface_ops),
611 .methods = remote_audio_pcm_iface_ops
612};
613
614void remote_audio_pcm_get_info_str(ddf_fun_t *fun, void *iface,
615 ipc_call_t *call)
616{
617 const audio_pcm_iface_t *pcm_iface = iface;
618
619 if (!pcm_iface->get_info_str) {
620 async_answer_0(call, ENOTSUP);
621 return;
622 }
623
624 const char *name = NULL;
625 const errno_t ret = pcm_iface->get_info_str(fun, &name);
626 const size_t name_size = name ? str_size(name) + 1 : 0;
627 async_answer_1(call, ret, name_size);
628
629 /* Send the string. */
630 if (ret == EOK && name_size > 0) {
631 ipc_call_t call;
632 size_t size;
633 if (!async_data_read_receive(&call, &size)) {
634 async_answer_0(&call, EPARTY);
635 return;
636 }
637
638 if (size != name_size) {
639 async_answer_0(&call, ELIMIT);
640 return;
641 }
642
643 async_data_read_finalize(&call, name, name_size);
644 }
645}
646
647void remote_audio_pcm_query_caps(ddf_fun_t *fun, void *iface,
648 ipc_call_t *call)
649{
650 const audio_pcm_iface_t *pcm_iface = iface;
651 const audio_cap_t cap = DEV_IPC_GET_ARG1(*call);
652 if (pcm_iface->query_cap) {
653 const unsigned value = pcm_iface->query_cap(fun, cap);
654 async_answer_1(call, EOK, value);
655 } else {
656 async_answer_0(call, ENOTSUP);
657 }
658}
659
660static void remote_audio_pcm_events_register(ddf_fun_t *fun, void *iface,
661 ipc_call_t *call)
662{
663 const audio_pcm_iface_t *pcm_iface = iface;
664 if (!pcm_iface->get_event_session ||
665 !pcm_iface->set_event_session) {
666 async_answer_0(call, ENOTSUP);
667 return;
668 }
669
670 async_answer_0(call, EOK);
671
672 ipc_call_t callback_call;
673 async_get_call(&callback_call);
674 async_sess_t *sess =
675 async_callback_receive_start(EXCHANGE_ATOMIC, &callback_call);
676 if (sess == NULL) {
677 ddf_msg(LVL_DEBUG, "Failed to create event callback");
678 async_answer_0(&callback_call, EAGAIN);
679 return;
680 }
681
682 const errno_t ret = pcm_iface->set_event_session(fun, sess);
683 if (ret != EOK) {
684 ddf_msg(LVL_DEBUG, "Failed to set event callback.");
685 async_hangup(sess);
686 async_answer_0(&callback_call, ret);
687 return;
688 }
689
690 async_answer_0(&callback_call, EOK);
691}
692
693static void remote_audio_pcm_events_unregister(ddf_fun_t *fun, void *iface,
694 ipc_call_t *call)
695{
696 const audio_pcm_iface_t *pcm_iface = iface;
697 if (!pcm_iface->get_event_session ||
698 !pcm_iface->set_event_session) {
699 async_answer_0(call, ENOTSUP);
700 return;
701 }
702
703 async_sess_t *sess = pcm_iface->get_event_session(fun);
704 if (sess) {
705 async_hangup(sess);
706 pcm_iface->set_event_session(fun, NULL);
707 }
708
709 async_answer_0(call, EOK);
710}
711
712void remote_audio_pcm_get_buffer_pos(ddf_fun_t *fun, void *iface,
713 ipc_call_t *call)
714{
715 const audio_pcm_iface_t *pcm_iface = iface;
716 size_t pos = 0;
717 const errno_t ret = pcm_iface->get_buffer_pos ?
718 pcm_iface->get_buffer_pos(fun, &pos) : ENOTSUP;
719 async_answer_1(call, ret, pos);
720}
721
722void remote_audio_pcm_test_format(ddf_fun_t *fun, void *iface,
723 ipc_call_t *call)
724{
725 const audio_pcm_iface_t *pcm_iface = iface;
726 unsigned channels = DEV_IPC_GET_ARG1(*call);
727 unsigned rate = DEV_IPC_GET_ARG2(*call);
728 pcm_sample_format_t format = DEV_IPC_GET_ARG3(*call);
729 const errno_t ret = pcm_iface->test_format ?
730 pcm_iface->test_format(fun, &channels, &rate, &format) : ENOTSUP;
731 async_answer_3(call, ret, channels, rate, format);
732}
733
734void remote_audio_pcm_get_buffer(ddf_fun_t *fun, void *iface,
735 ipc_call_t *call)
736{
737 const audio_pcm_iface_t *pcm_iface = iface;
738
739 if (!pcm_iface->get_buffer ||
740 !pcm_iface->release_buffer) {
741 async_answer_0(call, ENOTSUP);
742 return;
743 }
744 void *buffer = NULL;
745 size_t size = DEV_IPC_GET_ARG1(*call);
746 errno_t ret = pcm_iface->get_buffer(fun, &buffer, &size);
747 async_answer_1(call, ret, size);
748 if (ret != EOK || size == 0)
749 return;
750
751 /* Share the buffer. */
752 ipc_call_t share;
753 size_t share_size = 0;
754
755 ddf_msg(LVL_DEBUG2, "Receiving share request.");
756 if (!async_share_in_receive(&share, &share_size)) {
757 ddf_msg(LVL_DEBUG, "Failed to share pcm buffer.");
758 pcm_iface->release_buffer(fun);
759 async_answer_0(&share, EPARTY);
760 return;
761 }
762
763 ddf_msg(LVL_DEBUG2, "Checking requested share size.");
764 if (share_size != size) {
765 ddf_msg(LVL_DEBUG, "Incorrect pcm buffer size requested.");
766 pcm_iface->release_buffer(fun);
767 async_answer_0(&share, ELIMIT);
768 return;
769 }
770
771 ddf_msg(LVL_DEBUG2, "Calling share finalize.");
772 ret = async_share_in_finalize(&share, buffer, AS_AREA_WRITE |
773 AS_AREA_READ);
774 if (ret != EOK) {
775 ddf_msg(LVL_DEBUG, "Failed to share buffer.");
776 pcm_iface->release_buffer(fun);
777 return;
778 }
779
780 ddf_msg(LVL_DEBUG2, "Buffer shared with size %zu.", share_size);
781}
782
783void remote_audio_pcm_release_buffer(ddf_fun_t *fun, void *iface,
784 ipc_call_t *call)
785{
786 const audio_pcm_iface_t *pcm_iface = iface;
787
788 const errno_t ret = pcm_iface->release_buffer ?
789 pcm_iface->release_buffer(fun) : ENOTSUP;
790 async_answer_0(call, ret);
791}
792
793void remote_audio_pcm_start_playback(ddf_fun_t *fun, void *iface,
794 ipc_call_t *call)
795{
796 const audio_pcm_iface_t *pcm_iface = iface;
797
798 const unsigned frames = DEV_IPC_GET_ARG1(*call);
799 const unsigned rate = DEV_IPC_GET_ARG2(*call);
800 const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT8_MAX;
801 const pcm_sample_format_t format = DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
802
803 const errno_t ret = pcm_iface->start_playback ?
804 pcm_iface->start_playback(fun, frames, channels, rate, format) :
805 ENOTSUP;
806 async_answer_0(call, ret);
807}
808
809void remote_audio_pcm_stop_playback(ddf_fun_t *fun, void *iface,
810 ipc_call_t *call)
811{
812 const audio_pcm_iface_t *pcm_iface = iface;
813 const bool immediate = DEV_IPC_GET_ARG1(*call);
814
815 const errno_t ret = pcm_iface->stop_playback ?
816 pcm_iface->stop_playback(fun, immediate) : ENOTSUP;
817 async_answer_0(call, ret);
818}
819
820void remote_audio_pcm_start_capture(ddf_fun_t *fun, void *iface,
821 ipc_call_t *call)
822{
823 const audio_pcm_iface_t *pcm_iface = iface;
824
825 const unsigned frames = DEV_IPC_GET_ARG1(*call);
826 const unsigned rate = DEV_IPC_GET_ARG2(*call);
827 const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT16_MAX;
828 const pcm_sample_format_t format = DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
829
830 const errno_t ret = pcm_iface->start_capture ?
831 pcm_iface->start_capture(fun, frames, channels, rate, format) :
832 ENOTSUP;
833 async_answer_0(call, ret);
834}
835
836void remote_audio_pcm_stop_capture(ddf_fun_t *fun, void *iface,
837 ipc_call_t *call)
838{
839 const audio_pcm_iface_t *pcm_iface = iface;
840 const bool immediate = DEV_IPC_GET_ARG1(*call);
841
842 const errno_t ret = pcm_iface->stop_capture ?
843 pcm_iface->stop_capture(fun, immediate) : ENOTSUP;
844 async_answer_0(call, ret);
845}
846
847/**
848 * @}
849 */
Note: See TracBrowser for help on using the repository browser.