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

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

hound: Use buffer position in source available data.

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[737b4c0]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
[992ef56]44#include <audio_pcm_iface.h>
45
[737b4c0]46#include "audio_device.h"
47#include "log.h"
48
[992ef56]49#define BUFFER_BLOCKS 2
50
[1df3018a]51static int device_sink_connection_callback(audio_sink_t *sink);
52static int device_source_connection_callback(audio_source_t *source);
[992ef56]53static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void *arg);
54static int get_buffer(audio_device_t *dev);
55static int release_buffer(audio_device_t *dev);
56static int start_playback(audio_device_t *dev);
57static int stop_playback(audio_device_t *dev);
58static int start_recording(audio_device_t *dev);
59static int stop_recording(audio_device_t *dev);
[737b4c0]60
61
62int audio_device_init(audio_device_t *dev, service_id_t id, const char *name)
63{
64 assert(dev);
65 link_initialize(&dev->link);
66 dev->id = id;
67 dev->name = str_dup(name);
68 dev->sess = loc_service_connect(EXCHANGE_SERIALIZE, id, 0);
69 if (!dev->sess) {
70 log_debug("Failed to connect to device \"%s\"", name);
71 return ENOMEM;
72 }
73
[1df3018a]74 audio_sink_init(&dev->sink, name, dev, device_sink_connection_callback,
75 &AUDIO_FORMAT_ANY);
76 audio_source_init(&dev->source, name, dev,
77 device_source_connection_callback, NULL, &AUDIO_FORMAT_ANY);
[737b4c0]78
[992ef56]79 /* Init buffer members */
80 fibril_mutex_initialize(&dev->buffer.guard);
81 fibril_condvar_initialize(&dev->buffer.wc);
82 dev->buffer.id = 0;
83 dev->buffer.base = NULL;
84 dev->buffer.position = NULL;
85 dev->buffer.size = 0;
86
[737b4c0]87 log_verbose("Initialized device (%p) '%s' with id %u.",
88 dev, dev->name, dev->id);
89
90 return EOK;
91}
[1df3018a]92void audio_device_fini(audio_device_t *dev)
93{
94 //TODO implement;
95}
[737b4c0]96
[1df3018a]97static int device_sink_connection_callback(audio_sink_t* sink)
[737b4c0]98{
[1df3018a]99 assert(sink);
100 audio_device_t *dev = sink->private_data;
101 if (list_count(&sink->sources) == 1) {
[f3fced0]102 log_verbose("First connection on device sink '%s'", sink->name);
103
[992ef56]104 int ret = get_buffer(dev);
[737b4c0]105 if (ret != EOK) {
106 log_error("Failed to get device buffer: %s",
107 str_error(ret));
108 return ret;
109 }
[f3fced0]110
[992ef56]111 ret = start_playback(dev);
[737b4c0]112 if (ret != EOK) {
113 log_error("Failed to start playback: %s",
114 str_error(ret));
[992ef56]115 release_buffer(dev);
[737b4c0]116 return ret;
117 }
118 }
[1df3018a]119 if (list_count(&sink->sources) == 0) {
[f3fced0]120 log_verbose("No connections on device sink '%s'", sink->name);
[992ef56]121 int ret = stop_playback(dev);
[737b4c0]122 if (ret != EOK) {
123 log_error("Failed to start playback: %s",
124 str_error(ret));
125 return ret;
126 }
127 dev->sink.format = AUDIO_FORMAT_ANY;
[992ef56]128 ret = release_buffer(dev);
[737b4c0]129 if (ret != EOK) {
130 log_error("Failed to release buffer: %s",
131 str_error(ret));
132 return ret;
133 }
134 }
135 return EOK;
136}
137
[1df3018a]138static int device_source_connection_callback(audio_source_t *source)
[737b4c0]139{
[1df3018a]140 assert(source);
141 audio_device_t *dev = source->private_data;
142 if (source->connected_sink) {
[992ef56]143 int ret = get_buffer(dev);
[737b4c0]144 if (ret != EOK) {
145 log_error("Failed to get device buffer: %s",
146 str_error(ret));
147 return ret;
148 }
[992ef56]149 ret = start_recording(dev);
[737b4c0]150 if (ret != EOK) {
151 log_error("Failed to start recording: %s",
152 str_error(ret));
[992ef56]153 release_buffer(dev);
[737b4c0]154 return ret;
155 }
[1df3018a]156 } else { /* Disconnected */
[992ef56]157 int ret = stop_recording(dev);
[737b4c0]158 if (ret != EOK) {
159 log_error("Failed to start recording: %s",
160 str_error(ret));
161 return ret;
162 }
[1df3018a]163 source->format = AUDIO_FORMAT_ANY;
[992ef56]164 ret = release_buffer(dev);
[737b4c0]165 if (ret != EOK) {
166 log_error("Failed to release buffer: %s",
167 str_error(ret));
168 return ret;
169 }
170 }
171
172 return EOK;
173}
174
[992ef56]175static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void *arg)
176{
177 /* Answer initial request */
178 async_answer_0(iid, EOK);
179 audio_device_t *dev = arg;
180 assert(dev);
181 while (1) {
182 ipc_call_t call;
183 ipc_callid_t callid = async_get_call(&call);
184 async_answer_0(callid, EOK);
[fd5c0b7]185 switch(IPC_GET_IMETHOD(call)) {
186 case PCM_EVENT_PLAYBACK_DONE: {
187 if (dev->buffer.position) {
188 dev->buffer.position +=
[ab07cf0]189 (dev->buffer.size / BUFFER_BLOCKS);
[fd5c0b7]190 }
[ab07cf0]191 if ((!dev->buffer.position) ||
192 (dev->buffer.position >=
193 (dev->buffer.base + dev->buffer.size)))
[fd5c0b7]194 {
195 dev->buffer.position = dev->buffer.base;
196 }
197 audio_sink_mix_inputs(&dev->sink, dev->buffer.position,
198 dev->buffer.size / BUFFER_BLOCKS);
199 break;
[992ef56]200 }
[fd5c0b7]201 case PCM_EVENT_PLAYBACK_TERMINATED: {
202 log_verbose("Playback terminated!");
203 return;
[992ef56]204 }
[fd5c0b7]205 case PCM_EVENT_RECORDING_DONE: {
[ab07cf0]206 //TODO implement
[fd5c0b7]207 break;
208 }
209 case PCM_EVENT_RECORDING_TERMINATED:
210 log_verbose("Recording terminated!");
[ab07cf0]211 return;
[992ef56]212 }
213
214 }
215}
216
217static int get_buffer(audio_device_t *dev)
218{
219 assert(dev);
220 if (!dev->sess) {
221 log_debug("No connection to device");
222 return EIO;
223 }
224 if (dev->buffer.base) {
225 log_debug("We already have a buffer");
226 return EBUSY;
227 }
228
229 dev->buffer.size = 0;
230
231 async_exch_t *exch = async_exchange_begin(dev->sess);
232 const int ret = audio_pcm_get_buffer(exch, &dev->buffer.base,
233 &dev->buffer.size, &dev->buffer.id, device_event_callback, dev);
234 async_exchange_end(exch);
235 return ret;
236}
237
238#define CHECK_BUFFER_AND_CONNECTION() \
239do { \
240 assert(dev); \
241 if (!dev->sess) { \
242 log_debug("No connection to device"); \
243 return EIO; \
244 } \
245 if (!dev->buffer.base) { \
246 log_debug("We don't have a buffer"); \
247 return ENOENT; \
248 } \
249} while (0)
250
251
252static int release_buffer(audio_device_t *dev)
253{
254 CHECK_BUFFER_AND_CONNECTION();
255
256 async_exch_t *exch = async_exchange_begin(dev->sess);
257 const int ret = audio_pcm_release_buffer(exch, dev->buffer.id);
258 async_exchange_end(exch);
259 if (ret == EOK) {
260 dev->buffer.base = NULL;
261 dev->buffer.size = 0;
262 dev->buffer.position = NULL;
263 }
264 return ret;
265}
266
267static int start_playback(audio_device_t *dev)
268{
269 CHECK_BUFFER_AND_CONNECTION();
270
271 /* Fill the buffer first */
272 audio_sink_mix_inputs(&dev->sink, dev->buffer.base, dev->buffer.size);
273
274 async_exch_t *exch = async_exchange_begin(dev->sess);
275 const int ret = audio_pcm_start_playback(exch, dev->buffer.id,
276 BUFFER_BLOCKS, dev->sink.format.channels,
277 dev->sink.format.sampling_rate, dev->sink.format.sample_format);
278 async_exchange_end(exch);
279 return ret;
280}
281
282static int stop_playback(audio_device_t *dev)
283{
284 CHECK_BUFFER_AND_CONNECTION();
285
286 async_exch_t *exch = async_exchange_begin(dev->sess);
287 const int ret = audio_pcm_stop_playback(exch, dev->buffer.id);
288 async_exchange_end(exch);
289 return ret;
290}
291
292static int start_recording(audio_device_t *dev)
293{
294 CHECK_BUFFER_AND_CONNECTION();
295
296 async_exch_t *exch = async_exchange_begin(dev->sess);
297 const int ret = audio_pcm_start_record(exch, dev->buffer.id,
298 BUFFER_BLOCKS, dev->sink.format.channels,
299 dev->sink.format.sampling_rate, dev->sink.format.sample_format);
300 async_exchange_end(exch);
301 return ret;
302}
303
304static int stop_recording(audio_device_t *dev)
305{
306 CHECK_BUFFER_AND_CONNECTION();
307
308 async_exch_t *exch = async_exchange_begin(dev->sess);
309 const int ret = audio_pcm_stop_record(exch, dev->buffer.id);
310 async_exchange_end(exch);
311 return ret;
312}
[737b4c0]313
314/**
315 * @}
316 */
Note: See TracBrowser for help on using the repository browser.