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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c280d7e was f9b2cb4c, checked in by Martin Decky <martin@…>, 10 years ago

unify interface API

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