source: mainline/uspace/lib/drv/generic/remote_audio_pcm_buffer.c@ a3ab774

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

libdrv/audio,drv/audio/sb16: Update and implement recording interface.

Untested.

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