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