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

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

hound: add connection class

This will enable N to M routing in the future

  • Property mode set to 100644
File size: 7.9 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
[737b4c0]45#include "audio_device.h"
46#include "log.h"
47
[57e8b3b]48#define BUFFER_PARTS 2
[992ef56]49
[13df13c8]50static int device_sink_connection_callback(audio_sink_t *sink, bool new);
[fa60cd69]51static int device_source_connection_callback(audio_source_t *source, bool new);
[992ef56]52static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void *arg);
[389ef25]53static int device_check_format(audio_sink_t* sink);
[992ef56]54static int get_buffer(audio_device_t *dev);
55static int release_buffer(audio_device_t *dev);
[737b4c0]56
57
58int audio_device_init(audio_device_t *dev, service_id_t id, const char *name)
59{
60 assert(dev);
61 link_initialize(&dev->link);
62 dev->id = id;
63 dev->name = str_dup(name);
[2cc5c835]64 dev->sess = audio_pcm_open_service(id);
[737b4c0]65 if (!dev->sess) {
66 log_debug("Failed to connect to device \"%s\"", name);
67 return ENOMEM;
68 }
69
[1df3018a]70 audio_sink_init(&dev->sink, name, dev, device_sink_connection_callback,
[389ef25]71 device_check_format, &AUDIO_FORMAT_ANY);
[1df3018a]72 audio_source_init(&dev->source, name, dev,
73 device_source_connection_callback, NULL, &AUDIO_FORMAT_ANY);
[737b4c0]74
[992ef56]75 /* Init buffer members */
76 fibril_mutex_initialize(&dev->buffer.guard);
77 fibril_condvar_initialize(&dev->buffer.wc);
78 dev->buffer.base = NULL;
79 dev->buffer.position = NULL;
80 dev->buffer.size = 0;
81
[737b4c0]82 log_verbose("Initialized device (%p) '%s' with id %u.",
83 dev, dev->name, dev->id);
84
85 return EOK;
86}
[1df3018a]87void audio_device_fini(audio_device_t *dev)
88{
89 //TODO implement;
90}
[737b4c0]91
[13df13c8]92static int device_sink_connection_callback(audio_sink_t* sink, bool new)
[737b4c0]93{
[1df3018a]94 assert(sink);
95 audio_device_t *dev = sink->private_data;
[fa60cd69]96 if (new && list_count(&sink->connections) == 1) {
[f3fced0]97 log_verbose("First connection on device sink '%s'", sink->name);
98
[992ef56]99 int ret = get_buffer(dev);
[737b4c0]100 if (ret != EOK) {
101 log_error("Failed to get device buffer: %s",
102 str_error(ret));
103 return ret;
104 }
[018ab50]105 audio_pcm_register_event_callback(dev->sess,
[fa60cd69]106 device_event_callback, dev);\
107 // TODO set formats
[f3fced0]108
[2cc5c835]109 /* Fill the buffer first */
110 audio_sink_mix_inputs(&dev->sink,
111 dev->buffer.base, dev->buffer.size);
112
[fa60cd69]113 log_verbose("Mixed inputs: %zu/(%u * %u)",
114 dev->buffer.size, BUFFER_PARTS, pcm_format_frame_size(&dev->sink.format));
[57e8b3b]115 const unsigned frames = dev->buffer.size /
[ea6c838]116 (BUFFER_PARTS * pcm_format_frame_size(&dev->sink.format));
[fa60cd69]117 log_verbose("FRAME COUNT %u", frames);
[92b638c]118 ret = audio_pcm_start_playback_fragment(dev->sess, frames,
[2cc5c835]119 dev->sink.format.channels, dev->sink.format.sampling_rate,
120 dev->sink.format.sample_format);
[737b4c0]121 if (ret != EOK) {
122 log_error("Failed to start playback: %s",
123 str_error(ret));
[992ef56]124 release_buffer(dev);
[737b4c0]125 return ret;
126 }
127 }
[fa60cd69]128 if (list_count(&sink->connections) == 0) {
[13df13c8]129 assert(!new);
[f3fced0]130 log_verbose("No connections on device sink '%s'", sink->name);
[2cc5c835]131 int ret = audio_pcm_stop_playback(dev->sess);
[737b4c0]132 if (ret != EOK) {
[842eecdd]133 log_error("Failed to stop playback: %s",
[737b4c0]134 str_error(ret));
135 return ret;
136 }
137 dev->sink.format = AUDIO_FORMAT_ANY;
[992ef56]138 ret = release_buffer(dev);
[737b4c0]139 if (ret != EOK) {
140 log_error("Failed to release buffer: %s",
141 str_error(ret));
142 return ret;
143 }
144 }
145 return EOK;
146}
147
[fa60cd69]148static int device_source_connection_callback(audio_source_t *source, bool new)
[737b4c0]149{
[1df3018a]150 assert(source);
151 audio_device_t *dev = source->private_data;
[fa60cd69]152 if (new && list_count(&source->connections)) {
[992ef56]153 int ret = get_buffer(dev);
[737b4c0]154 if (ret != EOK) {
155 log_error("Failed to get device buffer: %s",
156 str_error(ret));
157 return ret;
158 }
[57e8b3b]159 const unsigned frames = dev->buffer.size /
[ea6c838]160 (BUFFER_PARTS * pcm_format_frame_size(&dev->sink.format));
[92b638c]161 ret = audio_pcm_start_capture_fragment(dev->sess, frames,
[2cc5c835]162 dev->sink.format.channels, dev->sink.format.sampling_rate,
163 dev->sink.format.sample_format);
[737b4c0]164 if (ret != EOK) {
165 log_error("Failed to start recording: %s",
166 str_error(ret));
[992ef56]167 release_buffer(dev);
[737b4c0]168 return ret;
169 }
[fa60cd69]170 }
171 if (list_count(&source->connections) == 0) { /* Disconnected */
172 assert(!new);
[d86c9736]173 int ret = audio_pcm_stop_capture(dev->sess);
[737b4c0]174 if (ret != EOK) {
175 log_error("Failed to start recording: %s",
176 str_error(ret));
177 return ret;
178 }
[1df3018a]179 source->format = AUDIO_FORMAT_ANY;
[992ef56]180 ret = release_buffer(dev);
[737b4c0]181 if (ret != EOK) {
182 log_error("Failed to release buffer: %s",
183 str_error(ret));
184 return ret;
185 }
[018ab50]186 audio_pcm_unregister_event_callback(dev->sess);
[737b4c0]187 }
188
189 return EOK;
190}
191
[992ef56]192static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void *arg)
193{
194 /* Answer initial request */
195 async_answer_0(iid, EOK);
196 audio_device_t *dev = arg;
197 assert(dev);
198 while (1) {
199 ipc_call_t call;
200 ipc_callid_t callid = async_get_call(&call);
201 async_answer_0(callid, EOK);
[fd5c0b7]202 switch(IPC_GET_IMETHOD(call)) {
[57e8b3b]203 case PCM_EVENT_FRAMES_PLAYED: {
[7f6d84b]204 struct timeval time1;
205 getuptime(&time1);
[57e8b3b]206 //TODO add underrun protection.
[fd5c0b7]207 if (dev->buffer.position) {
208 dev->buffer.position +=
[57e8b3b]209 (dev->buffer.size / BUFFER_PARTS);
[fd5c0b7]210 }
[ab07cf0]211 if ((!dev->buffer.position) ||
212 (dev->buffer.position >=
213 (dev->buffer.base + dev->buffer.size)))
[fd5c0b7]214 {
215 dev->buffer.position = dev->buffer.base;
216 }
217 audio_sink_mix_inputs(&dev->sink, dev->buffer.position,
[57e8b3b]218 dev->buffer.size / BUFFER_PARTS);
[7f6d84b]219 struct timeval time2;
220 getuptime(&time2);
221 log_verbose("Time to mix sources: %li\n",
222 tv_sub(&time2, &time1));
[fd5c0b7]223 break;
[992ef56]224 }
[d86c9736]225 case PCM_EVENT_PLAYBACK_TERMINATED:
[fd5c0b7]226 log_verbose("Playback terminated!");
227 return;
[d86c9736]228 case PCM_EVENT_FRAMES_CAPTURED:
[ab07cf0]229 //TODO implement
[fd5c0b7]230 break;
[d86c9736]231 case PCM_EVENT_CAPTURE_TERMINATED:
[fd5c0b7]232 log_verbose("Recording terminated!");
[ab07cf0]233 return;
[992ef56]234 }
235
236 }
237}
[389ef25]238static int device_check_format(audio_sink_t* sink)
239{
240 assert(sink);
241 audio_device_t *dev = sink->private_data;
242 assert(dev);
243 log_verbose("Checking format on sink %s", sink->name);
244 return audio_pcm_test_format(dev->sess, &sink->format.channels,
245 &sink->format.sampling_rate, &sink->format.sample_format);
246}
[992ef56]247
248static int get_buffer(audio_device_t *dev)
249{
250 assert(dev);
251 if (!dev->sess) {
252 log_debug("No connection to device");
253 return EIO;
254 }
255 if (dev->buffer.base) {
256 log_debug("We already have a buffer");
257 return EBUSY;
258 }
259
260 dev->buffer.size = 0;
261
[2cc5c835]262 return audio_pcm_get_buffer(dev->sess, &dev->buffer.base,
[018ab50]263 &dev->buffer.size);
[992ef56]264}
265
266static int release_buffer(audio_device_t *dev)
267{
[2cc5c835]268 assert(dev);
269 assert(dev->buffer.base);
[992ef56]270
[2cc5c835]271 const int ret = audio_pcm_release_buffer(dev->sess);
[992ef56]272 if (ret == EOK) {
273 dev->buffer.base = NULL;
274 dev->buffer.size = 0;
275 dev->buffer.position = NULL;
[2cc5c835]276 } else {
277 log_debug("Failed to release buffer: %s", str_error(ret));
[992ef56]278 }
279 return ret;
280}
[737b4c0]281/**
282 * @}
283 */
Note: See TracBrowser for help on using the repository browser.