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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 95838f1 was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Fix block comment formatting (ccheck).

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