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