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

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

hound: Turn buffer release failure into a warning

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