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

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

hound: fix ctx removal.

warn if still connected

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