source: mainline/uspace/srv/audio/hound/audio_device.c@ beb83c1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since beb83c1 was beb83c1, checked in by Jakub Jermar <jakub@…>, 7 years ago

Add async_accept_0() for accepting connections

  • Property mode set to 100644
File size: 11.4 KB
Line 
1/*
2 * Copyright (c) 2012 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
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
53static errno_t device_sink_connection_callback(audio_sink_t *sink, bool new);
54static errno_t device_source_connection_callback(audio_source_t *source, bool new);
55static void device_event_callback(ipc_call_t *icall, void *arg);
56static errno_t device_check_format(audio_sink_t *sink);
57static errno_t get_buffer(audio_device_t *dev);
58static errno_t release_buffer(audio_device_t *dev);
59static void advance_buffer(audio_device_t *dev, size_t size);
60static 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 */
74errno_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 */
109void 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 */
120audio_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 */
136audio_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 */
155static 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 */
219static 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 */
265static void device_event_callback(ipc_call_t *icall, void *arg)
266{
267 struct timespec time1;
268 errno_t ret;
269
270 /* Answer initial request */
271 async_accept_0(icall);
272 audio_device_t *dev = arg;
273 assert(dev);
274 while (true) {
275 ipc_call_t call;
276 async_get_call(&call);
277 async_answer_0(&call, EOK);
278
279 switch (IPC_GET_IMETHOD(call)) {
280 case PCM_EVENT_FRAMES_PLAYED:
281 getuptime(&time1);
282 //TODO add underrun detection.
283 /* We never cross the end of the buffer here */
284 audio_sink_mix_inputs(&dev->sink, dev->buffer.position,
285 dev->buffer.fragment_size);
286 advance_buffer(dev, dev->buffer.fragment_size);
287 struct timespec time2;
288 getuptime(&time2);
289 log_verbose("Time to mix sources: %lld\n",
290 NSEC2USEC(ts_sub_diff(&time2, &time1)));
291 break;
292 case PCM_EVENT_CAPTURE_TERMINATED:
293 log_verbose("Capture terminated");
294 dev->source.format = AUDIO_FORMAT_ANY;
295 ret = release_buffer(dev);
296 if (ret != EOK) {
297 log_error("Failed to release buffer: %s",
298 str_error(ret));
299 }
300 audio_pcm_unregister_event_callback(dev->sess);
301 break;
302 case PCM_EVENT_PLAYBACK_TERMINATED:
303 log_verbose("Playback Terminated");
304 dev->sink.format = AUDIO_FORMAT_ANY;
305 ret = release_buffer(dev);
306 if (ret != EOK) {
307 log_error("Failed to release buffer: %s",
308 str_error(ret));
309 }
310 audio_pcm_unregister_event_callback(dev->sess);
311 break;
312 case PCM_EVENT_FRAMES_CAPTURED:
313 ret = audio_source_push_data(&dev->source,
314 dev->buffer.position, dev->buffer.fragment_size);
315 advance_buffer(dev, dev->buffer.fragment_size);
316 if (ret != EOK)
317 log_warning("Failed to push recorded data");
318 break;
319 case 0:
320 log_info("Device event callback hangup");
321 return;
322 }
323 }
324}
325
326/**
327 * Test format against hardware limits.
328 * @param sink audio playback device.
329 * @return Error code.
330 */
331static errno_t device_check_format(audio_sink_t *sink)
332{
333 assert(sink);
334 audio_device_t *dev = sink->private_data;
335 assert(dev);
336 /* Check whether we are running */
337 if (is_running(dev))
338 return EBUSY;
339 log_verbose("Checking format on sink %s", sink->name);
340 return audio_pcm_test_format(dev->sess, &sink->format.channels,
341 &sink->format.sampling_rate, &sink->format.sample_format);
342}
343
344/**
345 * Get access to device buffer.
346 * @param dev Audio device.
347 * @return Error code.
348 */
349static errno_t get_buffer(audio_device_t *dev)
350{
351 assert(dev);
352 if (!dev->sess) {
353 log_debug("No connection to device");
354 return EIO;
355 }
356 if (dev->buffer.base) {
357 log_debug("We already have a buffer");
358 return EBUSY;
359 }
360
361 /* Ask for largest buffer possible */
362 size_t preferred_size = 0;
363
364 const errno_t ret = audio_pcm_get_buffer(dev->sess, &dev->buffer.base,
365 &preferred_size);
366 if (ret == EOK) {
367 dev->buffer.size = preferred_size;
368 dev->buffer.fragment_size = dev->buffer.size / BUFFER_PARTS;
369 dev->buffer.position = dev->buffer.base;
370 }
371 return ret;
372
373}
374
375/**
376 * Surrender access to device buffer.
377 * @param dev Audio device.
378 * @return Error code.
379 */
380static errno_t release_buffer(audio_device_t *dev)
381{
382 assert(dev);
383 assert(dev->buffer.base);
384
385 const errno_t ret = audio_pcm_release_buffer(dev->sess);
386 if (ret == EOK) {
387 as_area_destroy(dev->buffer.base);
388 dev->buffer.base = NULL;
389 dev->buffer.size = 0;
390 dev->buffer.position = NULL;
391 } else {
392 log_warning("Failed to release buffer: %s", str_error(ret));
393 }
394 return ret;
395}
396
397/**
398 * Move buffer position pointer.
399 * @param dev Audio device.
400 * @param size number of bytes to move forward
401 */
402static void advance_buffer(audio_device_t *dev, size_t size)
403{
404 assert(dev);
405 assert(dev->buffer.position >= dev->buffer.base);
406 assert(dev->buffer.position < (dev->buffer.base + dev->buffer.size));
407 dev->buffer.position += size;
408 if (dev->buffer.position == (dev->buffer.base + dev->buffer.size))
409 dev->buffer.position = dev->buffer.base;
410}
411/**
412 * @}
413 */
Note: See TracBrowser for help on using the repository browser.