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 |
|
---|
29 | /**
|
---|
30 | * @addtogroup audio
|
---|
31 | * @brief HelenOS sound server.
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <async.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <inttypes.h>
|
---|
41 | #include <loc.h>
|
---|
42 | #include <stdbool.h>
|
---|
43 | #include <str.h>
|
---|
44 | #include <str_error.h>
|
---|
45 | #include <as.h>
|
---|
46 |
|
---|
47 | #include "audio_device.h"
|
---|
48 | #include "log.h"
|
---|
49 |
|
---|
50 | /* hardwired to provide ~21ms per fragment */
|
---|
51 | #define BUFFER_PARTS 16
|
---|
52 |
|
---|
53 | static errno_t device_sink_connection_callback(audio_sink_t *sink, bool new);
|
---|
54 | static errno_t device_source_connection_callback(audio_source_t *source, bool new);
|
---|
55 | static void device_event_callback(ipc_call_t *icall, void *arg);
|
---|
56 | static errno_t device_check_format(audio_sink_t *sink);
|
---|
57 | static errno_t get_buffer(audio_device_t *dev);
|
---|
58 | static errno_t release_buffer(audio_device_t *dev);
|
---|
59 | static void advance_buffer(audio_device_t *dev, size_t size);
|
---|
60 | static inline bool is_running(audio_device_t *dev)
|
---|
61 | {
|
---|
62 | assert(dev);
|
---|
63 | /* we release buffer on stop so this should be enough */
|
---|
64 | return dev->buffer.base != NULL;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Initialize audio device structure.
|
---|
69 | * @param dev The structure to initialize.
|
---|
70 | * @param id Location service id of the device driver.
|
---|
71 | * @param name Name of the device.
|
---|
72 | * @return Error code.
|
---|
73 | */
|
---|
74 | errno_t audio_device_init(audio_device_t *dev, service_id_t id, const char *name)
|
---|
75 | {
|
---|
76 | assert(dev);
|
---|
77 | link_initialize(&dev->link);
|
---|
78 | dev->id = id;
|
---|
79 | dev->name = str_dup(name);
|
---|
80 | dev->sess = audio_pcm_open_service(id);
|
---|
81 | if (!dev->sess) {
|
---|
82 | log_debug("Failed to connect to device \"%s\"", name);
|
---|
83 | return ENOMEM;
|
---|
84 | }
|
---|
85 |
|
---|
86 | audio_sink_init(&dev->sink, name, dev, device_sink_connection_callback,
|
---|
87 | device_check_format, NULL, &AUDIO_FORMAT_ANY);
|
---|
88 | audio_source_init(&dev->source, name, dev,
|
---|
89 | device_source_connection_callback, NULL, &AUDIO_FORMAT_ANY);
|
---|
90 |
|
---|
91 | /* Init buffer members */
|
---|
92 | dev->buffer.base = NULL;
|
---|
93 | dev->buffer.position = NULL;
|
---|
94 | dev->buffer.size = 0;
|
---|
95 | dev->buffer.fragment_size = 0;
|
---|
96 |
|
---|
97 | log_verbose("Initialized device (%p) '%s' with id %" PRIun ".",
|
---|
98 | dev, dev->name, dev->id);
|
---|
99 |
|
---|
100 | return EOK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Restore resource cplaimed during initialization.
|
---|
105 | * @param dev The device to release.
|
---|
106 | *
|
---|
107 | * NOT IMPLEMENTED
|
---|
108 | */
|
---|
109 | void audio_device_fini(audio_device_t *dev)
|
---|
110 | {
|
---|
111 | //TODO implement;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Get device provided audio source.
|
---|
116 | * @param dev Th device.
|
---|
117 | * @return pointer to aa audio source structure, NULL if the device is not
|
---|
118 | * capable of capturing audio.
|
---|
119 | */
|
---|
120 | audio_source_t *audio_device_get_source(audio_device_t *dev)
|
---|
121 | {
|
---|
122 | assert(dev);
|
---|
123 | sysarg_t val;
|
---|
124 | errno_t rc = audio_pcm_query_cap(dev->sess, AUDIO_CAP_CAPTURE, &val);
|
---|
125 | if (rc == EOK && val)
|
---|
126 | return &dev->source;
|
---|
127 | return NULL;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Get device provided audio sink.
|
---|
132 | * @param dev Th device.
|
---|
133 | * @return pointer to aa audio source structure, NULL if the device is not
|
---|
134 | * capable of audio playback.
|
---|
135 | */
|
---|
136 | audio_sink_t *audio_device_get_sink(audio_device_t *dev)
|
---|
137 | {
|
---|
138 | assert(dev);
|
---|
139 | sysarg_t val;
|
---|
140 | errno_t rc = audio_pcm_query_cap(dev->sess, AUDIO_CAP_PLAYBACK, &val);
|
---|
141 | if (rc == EOK && val)
|
---|
142 | return &dev->sink;
|
---|
143 | return NULL;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Handle connection addition and removal.
|
---|
148 | * @param sink audio sink that is connected or disconnected.
|
---|
149 | * @param new True of a connection was added, false otherwise.
|
---|
150 | * @return Error code.
|
---|
151 | *
|
---|
152 | * Starts playback on first connection. Stops playback when there are no
|
---|
153 | * connections.
|
---|
154 | */
|
---|
155 | static errno_t device_sink_connection_callback(audio_sink_t *sink, bool new)
|
---|
156 | {
|
---|
157 | assert(sink);
|
---|
158 | audio_device_t *dev = sink->private_data;
|
---|
159 | if (new && list_count(&sink->connections) == 1) {
|
---|
160 | log_verbose("First connection on device sink '%s'", sink->name);
|
---|
161 |
|
---|
162 | errno_t ret = get_buffer(dev);
|
---|
163 | if (ret != EOK) {
|
---|
164 | log_error("Failed to get device buffer: %s",
|
---|
165 | str_error(ret));
|
---|
166 | return ret;
|
---|
167 | }
|
---|
168 | audio_pcm_register_event_callback(dev->sess,
|
---|
169 | device_event_callback, dev);
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * Fill the buffer first. Fill the first two fragments,
|
---|
173 | * so that we stay one fragment ahead
|
---|
174 | */
|
---|
175 | pcm_format_silence(dev->buffer.base, dev->buffer.size,
|
---|
176 | &dev->sink.format);
|
---|
177 | //TODO add underrun detection.
|
---|
178 | const size_t size = dev->buffer.fragment_size * 2;
|
---|
179 | /* We never cross the end of the buffer here */
|
---|
180 | audio_sink_mix_inputs(&dev->sink, dev->buffer.position, size);
|
---|
181 | advance_buffer(dev, size);
|
---|
182 |
|
---|
183 | const unsigned frames = dev->buffer.fragment_size /
|
---|
184 | pcm_format_frame_size(&dev->sink.format);
|
---|
185 | log_verbose("Fragment frame count %u", frames);
|
---|
186 | ret = audio_pcm_start_playback_fragment(dev->sess, frames,
|
---|
187 | dev->sink.format.channels, dev->sink.format.sampling_rate,
|
---|
188 | dev->sink.format.sample_format);
|
---|
189 | if (ret != EOK) {
|
---|
190 | log_error("Failed to start playback: %s",
|
---|
191 | str_error(ret));
|
---|
192 | release_buffer(dev);
|
---|
193 | return ret;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | if (list_count(&sink->connections) == 0) {
|
---|
197 | assert(!new);
|
---|
198 | log_verbose("Removed last connection on device sink '%s'",
|
---|
199 | sink->name);
|
---|
200 | errno_t ret = audio_pcm_stop_playback(dev->sess);
|
---|
201 | if (ret != EOK) {
|
---|
202 | log_error("Failed to stop playback: %s",
|
---|
203 | str_error(ret));
|
---|
204 | return ret;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | return EOK;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Handle connection addition and removal.
|
---|
212 | * @param source audio source that is connected or disconnected.
|
---|
213 | * @param new True of a connection was added, false otherwise.
|
---|
214 | * @return Error code.
|
---|
215 | *
|
---|
216 | * Starts capture on first connection. Stops capture when there are no
|
---|
217 | * connections.
|
---|
218 | */
|
---|
219 | static errno_t device_source_connection_callback(audio_source_t *source, bool new)
|
---|
220 | {
|
---|
221 | assert(source);
|
---|
222 | audio_device_t *dev = source->private_data;
|
---|
223 | if (new && list_count(&source->connections) == 1) {
|
---|
224 | errno_t ret = get_buffer(dev);
|
---|
225 | if (ret != EOK) {
|
---|
226 | log_error("Failed to get device buffer: %s",
|
---|
227 | str_error(ret));
|
---|
228 | return ret;
|
---|
229 | }
|
---|
230 |
|
---|
231 | //TODO set and test format
|
---|
232 |
|
---|
233 | const unsigned frames = dev->buffer.fragment_size /
|
---|
234 | pcm_format_frame_size(&dev->sink.format);
|
---|
235 | ret = audio_pcm_start_capture_fragment(dev->sess, frames,
|
---|
236 | dev->source.format.channels,
|
---|
237 | dev->source.format.sampling_rate,
|
---|
238 | dev->source.format.sample_format);
|
---|
239 | if (ret != EOK) {
|
---|
240 | log_error("Failed to start recording: %s",
|
---|
241 | str_error(ret));
|
---|
242 | release_buffer(dev);
|
---|
243 | return ret;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | if (list_count(&source->connections) == 0) { /* Disconnected */
|
---|
247 | assert(!new);
|
---|
248 | errno_t ret = audio_pcm_stop_capture_immediate(dev->sess);
|
---|
249 | if (ret != EOK) {
|
---|
250 | log_error("Failed to start recording: %s",
|
---|
251 | str_error(ret));
|
---|
252 | return ret;
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | return EOK;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /** Audio device event handler.
|
---|
260 | *
|
---|
261 | * @param icall Initial call structure.
|
---|
262 | * @param arg (unused)
|
---|
263 | *
|
---|
264 | */
|
---|
265 | static void device_event_callback(ipc_call_t *icall, void *arg)
|
---|
266 | {
|
---|
267 | struct timespec time1;
|
---|
268 | errno_t ret;
|
---|
269 |
|
---|
270 | audio_device_t *dev = arg;
|
---|
271 | assert(dev);
|
---|
272 | while (true) {
|
---|
273 | ipc_call_t call;
|
---|
274 | async_get_call(&call);
|
---|
275 | async_answer_0(&call, EOK);
|
---|
276 |
|
---|
277 | switch (IPC_GET_IMETHOD(call)) {
|
---|
278 | case PCM_EVENT_FRAMES_PLAYED:
|
---|
279 | getuptime(&time1);
|
---|
280 | //TODO add underrun detection.
|
---|
281 | /* We never cross the end of the buffer here */
|
---|
282 | audio_sink_mix_inputs(&dev->sink, dev->buffer.position,
|
---|
283 | dev->buffer.fragment_size);
|
---|
284 | advance_buffer(dev, dev->buffer.fragment_size);
|
---|
285 | struct timespec time2;
|
---|
286 | getuptime(&time2);
|
---|
287 | log_verbose("Time to mix sources: %lld\n",
|
---|
288 | NSEC2USEC(ts_sub_diff(&time2, &time1)));
|
---|
289 | break;
|
---|
290 | case PCM_EVENT_CAPTURE_TERMINATED:
|
---|
291 | log_verbose("Capture terminated");
|
---|
292 | dev->source.format = AUDIO_FORMAT_ANY;
|
---|
293 | ret = release_buffer(dev);
|
---|
294 | if (ret != EOK) {
|
---|
295 | log_error("Failed to release buffer: %s",
|
---|
296 | str_error(ret));
|
---|
297 | }
|
---|
298 | audio_pcm_unregister_event_callback(dev->sess);
|
---|
299 | break;
|
---|
300 | case PCM_EVENT_PLAYBACK_TERMINATED:
|
---|
301 | log_verbose("Playback Terminated");
|
---|
302 | dev->sink.format = AUDIO_FORMAT_ANY;
|
---|
303 | ret = release_buffer(dev);
|
---|
304 | if (ret != EOK) {
|
---|
305 | log_error("Failed to release buffer: %s",
|
---|
306 | str_error(ret));
|
---|
307 | }
|
---|
308 | audio_pcm_unregister_event_callback(dev->sess);
|
---|
309 | break;
|
---|
310 | case PCM_EVENT_FRAMES_CAPTURED:
|
---|
311 | ret = audio_source_push_data(&dev->source,
|
---|
312 | dev->buffer.position, dev->buffer.fragment_size);
|
---|
313 | advance_buffer(dev, dev->buffer.fragment_size);
|
---|
314 | if (ret != EOK)
|
---|
315 | log_warning("Failed to push recorded data");
|
---|
316 | break;
|
---|
317 | case 0:
|
---|
318 | log_info("Device event callback hangup");
|
---|
319 | return;
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Test format against hardware limits.
|
---|
326 | * @param sink audio playback device.
|
---|
327 | * @return Error code.
|
---|
328 | */
|
---|
329 | static errno_t device_check_format(audio_sink_t *sink)
|
---|
330 | {
|
---|
331 | assert(sink);
|
---|
332 | audio_device_t *dev = sink->private_data;
|
---|
333 | assert(dev);
|
---|
334 | /* Check whether we are running */
|
---|
335 | if (is_running(dev))
|
---|
336 | return EBUSY;
|
---|
337 | log_verbose("Checking format on sink %s", sink->name);
|
---|
338 | return audio_pcm_test_format(dev->sess, &sink->format.channels,
|
---|
339 | &sink->format.sampling_rate, &sink->format.sample_format);
|
---|
340 | }
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Get access to device buffer.
|
---|
344 | * @param dev Audio device.
|
---|
345 | * @return Error code.
|
---|
346 | */
|
---|
347 | static errno_t get_buffer(audio_device_t *dev)
|
---|
348 | {
|
---|
349 | assert(dev);
|
---|
350 | if (!dev->sess) {
|
---|
351 | log_debug("No connection to device");
|
---|
352 | return EIO;
|
---|
353 | }
|
---|
354 | if (dev->buffer.base) {
|
---|
355 | log_debug("We already have a buffer");
|
---|
356 | return EBUSY;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /* Ask for largest buffer possible */
|
---|
360 | size_t preferred_size = 0;
|
---|
361 |
|
---|
362 | const errno_t ret = audio_pcm_get_buffer(dev->sess, &dev->buffer.base,
|
---|
363 | &preferred_size);
|
---|
364 | if (ret == EOK) {
|
---|
365 | dev->buffer.size = preferred_size;
|
---|
366 | dev->buffer.fragment_size = dev->buffer.size / BUFFER_PARTS;
|
---|
367 | dev->buffer.position = dev->buffer.base;
|
---|
368 | }
|
---|
369 | return ret;
|
---|
370 |
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Surrender access to device buffer.
|
---|
375 | * @param dev Audio device.
|
---|
376 | * @return Error code.
|
---|
377 | */
|
---|
378 | static errno_t release_buffer(audio_device_t *dev)
|
---|
379 | {
|
---|
380 | assert(dev);
|
---|
381 | assert(dev->buffer.base);
|
---|
382 |
|
---|
383 | const errno_t ret = audio_pcm_release_buffer(dev->sess);
|
---|
384 | if (ret == EOK) {
|
---|
385 | as_area_destroy(dev->buffer.base);
|
---|
386 | dev->buffer.base = NULL;
|
---|
387 | dev->buffer.size = 0;
|
---|
388 | dev->buffer.position = NULL;
|
---|
389 | } else {
|
---|
390 | log_warning("Failed to release buffer: %s", str_error(ret));
|
---|
391 | }
|
---|
392 | return ret;
|
---|
393 | }
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Move buffer position pointer.
|
---|
397 | * @param dev Audio device.
|
---|
398 | * @param size number of bytes to move forward
|
---|
399 | */
|
---|
400 | static void advance_buffer(audio_device_t *dev, size_t size)
|
---|
401 | {
|
---|
402 | assert(dev);
|
---|
403 | assert(dev->buffer.position >= dev->buffer.base);
|
---|
404 | assert(dev->buffer.position < (dev->buffer.base + dev->buffer.size));
|
---|
405 | dev->buffer.position += size;
|
---|
406 | if (dev->buffer.position == (dev->buffer.base + dev->buffer.size))
|
---|
407 | dev->buffer.position = dev->buffer.base;
|
---|
408 | }
|
---|
409 | /**
|
---|
410 | * @}
|
---|
411 | */
|
---|