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

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

Add frame count to event report.

This enables applications to detect underruns.

  • Property mode set to 100644
File size: 16.3 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 <str.h>
39#include <as.h>
40#include <sys/mman.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_GET_BUFFER,
48 IPC_M_AUDIO_PCM_RELEASE_BUFFER,
49 IPC_M_AUDIO_PCM_START_PLAYBACK,
50 IPC_M_AUDIO_PCM_STOP_PLAYBACK,
51 IPC_M_AUDIO_PCM_START_RECORD,
52 IPC_M_AUDIO_PCM_STOP_RECORD,
53} audio_pcm_iface_funcs_t;
54
55/*
56 * CLIENT SIDE
57 */
58audio_pcm_sess_t *audio_pcm_open(const char *name)
59{
60 devman_handle_t device_handle = 0;
61 const int ret = devman_fun_get_handle(name, &device_handle, 0);
62 if (ret != EOK)
63 return NULL;
64 return devman_device_connect(EXCHANGE_SERIALIZE, device_handle,
65 IPC_FLAG_BLOCKING);
66}
67
68audio_pcm_sess_t *audio_pcm_open_service(service_id_t id)
69{
70 return loc_service_connect(EXCHANGE_SERIALIZE, id, IPC_FLAG_BLOCKING);
71}
72
73void audio_pcm_close(audio_pcm_sess_t *sess)
74{
75 if (sess)
76 async_hangup(sess);
77}
78
79int audio_pcm_get_info_str(audio_pcm_sess_t *sess, const char **name)
80{
81 async_exch_t *exch = async_exchange_begin(sess);
82 sysarg_t name_size;
83 const int ret = async_req_1_1(exch,
84 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
85 IPC_M_AUDIO_PCM_GET_INFO_STR, &name_size);
86 if (ret == EOK && name) {
87 char *name_place = calloc(1, name_size);
88 if (!name_place) {
89 /* Make the other side fail
90 * as it waits for read request */
91 async_data_read_start(exch, (void*)-1, 0);
92 async_exchange_end(exch);
93 return ENOMEM;
94 }
95 const int ret =
96 async_data_read_start(exch, name_place, name_size);
97 if (ret != EOK) {
98 free(name_place);
99 async_exchange_end(exch);
100 return ret;
101 }
102 *name = name_place;
103 }
104 async_exchange_end(exch);
105 return ret;
106}
107
108int audio_pcm_get_buffer(audio_pcm_sess_t *sess, void **buffer, size_t *size,
109 async_client_conn_t event_rec, void* arg)
110{
111 if (!buffer || !size)
112 return EINVAL;
113
114 async_exch_t *exch = async_exchange_begin(sess);
115
116 sysarg_t buffer_size = *size;
117 const int ret = async_req_2_1(exch,
118 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_GET_BUFFER,
119 (sysarg_t)buffer_size, &buffer_size);
120 if (ret == EOK) {
121 void *dst = NULL;
122 int ret = async_share_in_start_0_0(exch, buffer_size, &dst);
123 if (ret != EOK) {
124 async_exchange_end(exch);
125 return ret;
126 }
127 ret = async_connect_to_me(exch, 0, 0, 0, event_rec, arg);
128 if (ret != EOK) {
129 async_exchange_end(exch);
130 return ret;
131 }
132
133 *buffer = dst;
134 *size = buffer_size;
135 }
136 async_exchange_end(exch);
137 return ret;
138}
139
140int audio_pcm_release_buffer(audio_pcm_sess_t *sess)
141{
142 async_exch_t *exch = async_exchange_begin(sess);
143 const int ret = async_req_1_0(exch,
144 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
145 IPC_M_AUDIO_PCM_RELEASE_BUFFER);
146 async_exchange_end(exch);
147 return ret;
148}
149
150int audio_pcm_start_playback(audio_pcm_sess_t *sess, unsigned frames,
151 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
152{
153 if (channels > UINT16_MAX)
154 return EINVAL;
155 assert((format & UINT16_MAX) == format);
156 const sysarg_t packed = (channels << 16) | (format & UINT16_MAX);
157 async_exch_t *exch = async_exchange_begin(sess);
158 const int ret = async_req_4_0(exch,
159 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
160 IPC_M_AUDIO_PCM_START_PLAYBACK,
161 frames, sample_rate, packed);
162 async_exchange_end(exch);
163 return ret;
164}
165
166int audio_pcm_stop_playback(audio_pcm_sess_t *sess)
167{
168 async_exch_t *exch = async_exchange_begin(sess);
169 const int ret = async_req_1_0(exch,
170 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE),
171 IPC_M_AUDIO_PCM_STOP_PLAYBACK);
172 async_exchange_end(exch);
173 return ret;
174}
175
176int audio_pcm_start_record(audio_pcm_sess_t *sess, unsigned frames,
177 unsigned channels, unsigned sample_rate, pcm_sample_format_t format)
178{
179 if (channels > UINT16_MAX)
180 return EINVAL;
181 assert((format & UINT16_MAX) == format);
182 const sysarg_t packed = (channels << 16) | (format & UINT16_MAX);
183 async_exch_t *exch = async_exchange_begin(sess);
184 const int ret = async_req_4_0(exch,
185 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_START_RECORD,
186 frames, sample_rate, packed);
187 async_exchange_end(exch);
188 return ret;
189}
190
191int audio_pcm_stop_record(audio_pcm_sess_t *sess)
192{
193 async_exch_t *exch = async_exchange_begin(sess);
194 const int ret = async_req_1_0(exch,
195 DEV_IFACE_ID(AUDIO_PCM_BUFFER_IFACE), IPC_M_AUDIO_PCM_STOP_RECORD);
196 async_exchange_end(exch);
197 return ret;
198}
199
200/*
201 * SERVER SIDE
202 */
203static void remote_audio_pcm_get_info_str(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
204static void remote_audio_pcm_get_buffer(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
205static void remote_audio_pcm_release_buffer(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
206static void remote_audio_pcm_start_playback(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
207static void remote_audio_pcm_stop_playback(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
208static void remote_audio_pcm_start_record(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
209static void remote_audio_pcm_stop_record(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
210
211/** Remote audio pcm buffer interface operations. */
212static remote_iface_func_ptr_t remote_audio_pcm_iface_ops[] = {
213 [IPC_M_AUDIO_PCM_GET_INFO_STR] = remote_audio_pcm_get_info_str,
214 [IPC_M_AUDIO_PCM_GET_BUFFER] = remote_audio_pcm_get_buffer,
215 [IPC_M_AUDIO_PCM_RELEASE_BUFFER] = remote_audio_pcm_release_buffer,
216 [IPC_M_AUDIO_PCM_START_PLAYBACK] = remote_audio_pcm_start_playback,
217 [IPC_M_AUDIO_PCM_STOP_PLAYBACK] = remote_audio_pcm_stop_playback,
218 [IPC_M_AUDIO_PCM_START_RECORD] = remote_audio_pcm_start_record,
219 [IPC_M_AUDIO_PCM_STOP_RECORD] = remote_audio_pcm_stop_record,
220};
221
222/** Remote audio mixer interface structure. */
223remote_iface_t remote_audio_pcm_iface = {
224 .method_count = sizeof(remote_audio_pcm_iface_ops) /
225 sizeof(remote_audio_pcm_iface_ops[0]),
226 .methods = remote_audio_pcm_iface_ops
227};
228
229void remote_audio_pcm_get_info_str(ddf_fun_t *fun, void *iface,
230 ipc_callid_t callid, ipc_call_t *call)
231{
232 const audio_pcm_iface_t *pcm_iface = iface;
233
234 if (!pcm_iface->get_info_str) {
235 async_answer_0(callid, ENOTSUP);
236 return;
237 }
238 const char *name = NULL;
239 const int ret = pcm_iface->get_info_str(fun, &name);
240 const size_t name_size = name ? str_size(name) + 1 : 0;
241 async_answer_1(callid, ret, name_size);
242 /* Send the string. */
243 if (ret == EOK && name_size > 0) {
244 size_t size;
245 ipc_callid_t name_id;
246 if (!async_data_read_receive(&name_id, &size)) {
247 async_answer_0(name_id, EPARTY);
248 return;
249 }
250 if (size != name_size) {
251 async_answer_0(name_id, ELIMIT);
252 return;
253 }
254 async_data_read_finalize(name_id, name, name_size);
255 }
256}
257
258void remote_audio_pcm_get_buffer(ddf_fun_t *fun, void *iface,
259 ipc_callid_t callid, ipc_call_t *call)
260{
261 const audio_pcm_iface_t *pcm_iface = iface;
262
263 if (!pcm_iface->get_buffer ||
264 !pcm_iface->release_buffer ||
265 !pcm_iface->set_event_session) {
266 async_answer_0(callid, ENOTSUP);
267 return;
268 }
269 void *buffer = NULL;
270 size_t size = DEV_IPC_GET_ARG1(*call);
271 int ret = pcm_iface->get_buffer(fun, &buffer, &size);
272 async_answer_1(callid, ret, size);
273 if (ret != EOK || size == 0)
274 return;
275
276 /* Share the buffer. */
277 size_t share_size = 0;
278 ipc_callid_t share_id = 0;
279
280 ddf_msg(LVL_DEBUG2, "Receiving share request.");
281 if (!async_share_in_receive(&share_id, &share_size)) {
282 ddf_msg(LVL_DEBUG, "Failed to share pcm buffer.");
283 pcm_iface->release_buffer(fun);
284 async_answer_0(share_id, EPARTY);
285 return;
286 }
287
288 ddf_msg(LVL_DEBUG2, "Checking requested share size.");
289 if (share_size != size) {
290 ddf_msg(LVL_DEBUG, "Incorrect pcm buffer size requested.");
291 pcm_iface->release_buffer(fun);
292 async_answer_0(share_id, ELIMIT);
293 return;
294 }
295
296 ddf_msg(LVL_DEBUG2, "Calling share finalize.");
297 ret = async_share_in_finalize(share_id, buffer, AS_AREA_WRITE
298 | AS_AREA_READ);
299 if (ret != EOK) {
300 ddf_msg(LVL_DEBUG, "Failed to share buffer.");
301 pcm_iface->release_buffer(fun);
302 return;
303 }
304
305 ddf_msg(LVL_DEBUG2, "Buffer shared with size %zu, creating callback.",
306 share_size);
307 {
308 ipc_call_t call;
309 ipc_callid_t callid = async_get_call(&call);
310 async_sess_t *sess =
311 async_callback_receive_start(EXCHANGE_ATOMIC, &call);
312 if (sess == NULL) {
313 ddf_msg(LVL_DEBUG, "Failed to create event callback");
314 pcm_iface->release_buffer(fun);
315 async_answer_0(callid, EAGAIN);
316 return;
317 }
318 ret = pcm_iface->set_event_session(fun, sess);
319 if (ret != EOK) {
320 ddf_msg(LVL_DEBUG, "Failed to set event callback.");
321 pcm_iface->release_buffer(fun);
322 async_answer_0(callid, ret);
323 return;
324 }
325 ddf_msg(LVL_DEBUG2, "Buffer and event session setup OK.");
326 async_answer_0(callid, EOK);
327 }
328}
329
330void remote_audio_pcm_release_buffer(ddf_fun_t *fun, void *iface,
331 ipc_callid_t callid, ipc_call_t *call)
332{
333 const audio_pcm_iface_t *pcm_iface = iface;
334
335 const int ret = pcm_iface->release_buffer ?
336 pcm_iface->release_buffer(fun) : ENOTSUP;
337 async_answer_0(callid, ret);
338}
339
340void remote_audio_pcm_start_playback(ddf_fun_t *fun, void *iface,
341 ipc_callid_t callid, ipc_call_t *call)
342{
343 const audio_pcm_iface_t *pcm_iface = iface;
344
345 const unsigned frames = DEV_IPC_GET_ARG1(*call);
346 const unsigned rate = DEV_IPC_GET_ARG2(*call);
347 const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT8_MAX;
348 const pcm_sample_format_t format = DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
349
350 const int ret = pcm_iface->start_playback
351 ? pcm_iface->start_playback(fun, frames, channels, rate, format)
352 : ENOTSUP;
353 async_answer_0(callid, ret);
354}
355
356void remote_audio_pcm_stop_playback(ddf_fun_t *fun, void *iface,
357 ipc_callid_t callid, ipc_call_t *call)
358{
359 const audio_pcm_iface_t *pcm_iface = iface;
360
361 const int ret = pcm_iface->stop_playback ?
362 pcm_iface->stop_playback(fun) : ENOTSUP;
363 async_answer_0(callid, ret);
364}
365
366void remote_audio_pcm_start_record(ddf_fun_t *fun, void *iface,
367 ipc_callid_t callid, ipc_call_t *call)
368{
369 const audio_pcm_iface_t *pcm_iface = iface;
370
371 const unsigned frames = DEV_IPC_GET_ARG1(*call);
372 const unsigned rate = DEV_IPC_GET_ARG2(*call);
373 const unsigned channels = (DEV_IPC_GET_ARG3(*call) >> 16) & UINT16_MAX;
374 const pcm_sample_format_t format = DEV_IPC_GET_ARG3(*call) & UINT16_MAX;
375
376 const int ret = pcm_iface->start_record
377 ? pcm_iface->start_record(fun, frames, channels, rate, format)
378 : ENOTSUP;
379 async_answer_0(callid, ret);
380}
381
382void remote_audio_pcm_stop_record(ddf_fun_t *fun, void *iface,
383 ipc_callid_t callid, ipc_call_t *call)
384{
385 const audio_pcm_iface_t *pcm_iface = iface;
386
387 const int ret = pcm_iface->stop_record ?
388 pcm_iface->stop_record(fun) : ENOTSUP;
389 async_answer_0(callid, ret);
390}
391
392#if 0
393
394void remote_audio_mixer_get_info(
395 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
396{
397 audio_mixer_iface_t *mixer_iface = iface;
398
399 if (!mixer_iface->get_info) {
400 async_answer_0(callid, ENOTSUP);
401 return;
402 }
403 const char *name = NULL;
404 unsigned items = 0;
405 const int ret = mixer_iface->get_info(fun, &name, &items);
406 const size_t name_size = name ? str_size(name) + 1 : 0;
407 async_answer_2(callid, ret, name_size, items);
408 /* Send the name. */
409 if (ret == EOK && name_size > 0) {
410 size_t size;
411 ipc_callid_t name_id;
412 if (!async_data_read_receive(&name_id, &size)) {
413 async_answer_0(name_id, EPARTY);
414 return;
415 }
416 if (size != name_size) {
417 async_answer_0(name_id, ELIMIT);
418 return;
419 }
420 async_data_read_finalize(name_id, name, name_size);
421 }
422}
423
424void remote_audio_mixer_get_item_info(
425 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
426{
427 audio_mixer_iface_t *mixer_iface = iface;
428
429 if (!mixer_iface->get_item_info) {
430 async_answer_0(callid, ENOTSUP);
431 return;
432 }
433
434 const unsigned item = DEV_IPC_GET_ARG1(*call);
435 const char *name = NULL;
436 unsigned channels = 0;
437 const int ret = mixer_iface->get_item_info(fun, item, &name, &channels);
438 const size_t name_size = name ? str_size(name) + 1 : 0;
439 async_answer_2(callid, ret, name_size, channels);
440 /* Send the name. */
441 if (ret == EOK && name_size > 0) {
442 size_t size;
443 ipc_callid_t name_id;
444 if (!async_data_read_receive(&name_id, &size)) {
445 async_answer_0(name_id, EPARTY);
446 return;
447 }
448 if (size != name_size) {
449 async_answer_0(name_id, ELIMIT);
450 return;
451 }
452 async_data_read_finalize(name_id, name, name_size);
453 }
454}
455
456void remote_audio_mixer_get_channel_info(
457 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
458{
459 audio_mixer_iface_t *mixer_iface = iface;
460
461 if (!mixer_iface->get_channel_info) {
462 async_answer_0(callid, ENOTSUP);
463 return;
464 }
465
466 const unsigned item = DEV_IPC_GET_ARG1(*call);
467 const unsigned channel = DEV_IPC_GET_ARG2(*call);
468 const char *name = NULL;
469 unsigned levels = 0;
470 const int ret =
471 mixer_iface->get_channel_info(fun, item, channel, &name, &levels);
472 const size_t name_size = name ? str_size(name) + 1 : 0;
473 async_answer_2(callid, ret, name_size, levels);
474 /* Send the name. */
475 if (ret == EOK && name_size > 0) {
476 size_t size;
477 ipc_callid_t name_id;
478 if (!async_data_read_receive(&name_id, &size)) {
479 async_answer_0(name_id, EPARTY);
480 return;
481 }
482 if (size != name_size) {
483 async_answer_0(name_id, ELIMIT);
484 return;
485 }
486 async_data_read_finalize(name_id, name, name_size);
487 }
488}
489
490void remote_audio_mixer_channel_mute_set(
491 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
492{
493 audio_mixer_iface_t *mixer_iface = iface;
494
495 if (!mixer_iface->channel_mute_set) {
496 async_answer_0(callid, ENOTSUP);
497 return;
498 }
499 const unsigned item = DEV_IPC_GET_ARG1(*call);
500 const unsigned channel = DEV_IPC_GET_ARG2(*call);
501 const bool mute = DEV_IPC_GET_ARG3(*call);
502 const int ret = mixer_iface->channel_mute_set(fun, item, channel, mute);
503 async_answer_0(callid, ret);
504}
505
506void remote_audio_mixer_channel_mute_get(
507 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
508{
509 audio_mixer_iface_t *mixer_iface = iface;
510
511 if (!mixer_iface->channel_mute_get) {
512 async_answer_0(callid, ENOTSUP);
513 return;
514 }
515 const unsigned item = DEV_IPC_GET_ARG1(*call);
516 const unsigned channel = DEV_IPC_GET_ARG2(*call);
517 bool mute = false;
518 const int ret =
519 mixer_iface->channel_mute_get(fun, item, channel, &mute);
520 async_answer_1(callid, ret, mute);
521}
522
523void remote_audio_mixer_channel_volume_set(
524 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
525{
526 audio_mixer_iface_t *mixer_iface = iface;
527
528 if (!mixer_iface->channel_volume_set) {
529 async_answer_0(callid, ENOTSUP);
530 return;
531 }
532 const unsigned item = DEV_IPC_GET_ARG1(*call);
533 const unsigned channel = DEV_IPC_GET_ARG2(*call);
534 const unsigned level = DEV_IPC_GET_ARG3(*call);
535 const int ret =
536 mixer_iface->channel_volume_set(fun, item, channel, level);
537 async_answer_0(callid, ret);
538}
539
540void remote_audio_mixer_channel_volume_get(
541 ddf_fun_t *fun, void *iface, ipc_callid_t callid, ipc_call_t *call)
542{
543 audio_mixer_iface_t *mixer_iface = iface;
544
545 if (!mixer_iface->channel_volume_get) {
546 async_answer_0(callid, ENOTSUP);
547 return;
548 }
549 const unsigned item = DEV_IPC_GET_ARG1(*call);
550 const unsigned channel = DEV_IPC_GET_ARG2(*call);
551 unsigned current = 0, max = 0;
552 const int ret =
553 mixer_iface->channel_volume_get(fun, item, channel, &current, &max);
554 async_answer_2(callid, ret, current, max);
555}
556#endif
557
558/**
559 * @}
560 */
561
Note: See TracBrowser for help on using the repository browser.