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

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

hound: Measure time needed to mix audio data

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