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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 301032a was eb0ef51, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libdrv: few more comments

use ARRAY_SIZE macro

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