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

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

hound: add data available callback

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