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 | #include <audio_pcm_iface.h>
|
---|
45 |
|
---|
46 | #include "audio_device.h"
|
---|
47 | #include "log.h"
|
---|
48 |
|
---|
49 | #define BUFFER_BLOCKS 2
|
---|
50 |
|
---|
51 | static int device_sink_connection_callback(audio_sink_t *sink);
|
---|
52 | static int device_source_connection_callback(audio_source_t *source);
|
---|
53 | static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
54 | static int get_buffer(audio_device_t *dev);
|
---|
55 | static int release_buffer(audio_device_t *dev);
|
---|
56 | static int start_playback(audio_device_t *dev);
|
---|
57 | static int stop_playback(audio_device_t *dev);
|
---|
58 | static int start_recording(audio_device_t *dev);
|
---|
59 | static int stop_recording(audio_device_t *dev);
|
---|
60 |
|
---|
61 |
|
---|
62 | int 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 |
|
---|
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);
|
---|
78 |
|
---|
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 |
|
---|
87 | log_verbose("Initialized device (%p) '%s' with id %u.",
|
---|
88 | dev, dev->name, dev->id);
|
---|
89 |
|
---|
90 | return EOK;
|
---|
91 | }
|
---|
92 | void audio_device_fini(audio_device_t *dev)
|
---|
93 | {
|
---|
94 | //TODO implement;
|
---|
95 | }
|
---|
96 |
|
---|
97 | static int device_sink_connection_callback(audio_sink_t* sink)
|
---|
98 | {
|
---|
99 | assert(sink);
|
---|
100 | audio_device_t *dev = sink->private_data;
|
---|
101 | if (list_count(&sink->sources) == 1) {
|
---|
102 | log_verbose("First connection on device sink '%s'", sink->name);
|
---|
103 |
|
---|
104 | int ret = get_buffer(dev);
|
---|
105 | if (ret != EOK) {
|
---|
106 | log_error("Failed to get device buffer: %s",
|
---|
107 | str_error(ret));
|
---|
108 | return ret;
|
---|
109 | }
|
---|
110 |
|
---|
111 | ret = start_playback(dev);
|
---|
112 | if (ret != EOK) {
|
---|
113 | log_error("Failed to start playback: %s",
|
---|
114 | str_error(ret));
|
---|
115 | release_buffer(dev);
|
---|
116 | return ret;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | if (list_count(&sink->sources) == 0) {
|
---|
120 | log_verbose("No connections on device sink '%s'", sink->name);
|
---|
121 | int ret = stop_playback(dev);
|
---|
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;
|
---|
128 | ret = release_buffer(dev);
|
---|
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 |
|
---|
138 | static int device_source_connection_callback(audio_source_t *source)
|
---|
139 | {
|
---|
140 | assert(source);
|
---|
141 | audio_device_t *dev = source->private_data;
|
---|
142 | if (source->connected_sink) {
|
---|
143 | int ret = get_buffer(dev);
|
---|
144 | if (ret != EOK) {
|
---|
145 | log_error("Failed to get device buffer: %s",
|
---|
146 | str_error(ret));
|
---|
147 | return ret;
|
---|
148 | }
|
---|
149 | ret = start_recording(dev);
|
---|
150 | if (ret != EOK) {
|
---|
151 | log_error("Failed to start recording: %s",
|
---|
152 | str_error(ret));
|
---|
153 | release_buffer(dev);
|
---|
154 | return ret;
|
---|
155 | }
|
---|
156 | } else { /* Disconnected */
|
---|
157 | int ret = stop_recording(dev);
|
---|
158 | if (ret != EOK) {
|
---|
159 | log_error("Failed to start recording: %s",
|
---|
160 | str_error(ret));
|
---|
161 | return ret;
|
---|
162 | }
|
---|
163 | source->format = AUDIO_FORMAT_ANY;
|
---|
164 | ret = release_buffer(dev);
|
---|
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 |
|
---|
175 | static 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);
|
---|
185 | switch(IPC_GET_IMETHOD(call)) {
|
---|
186 | case PCM_EVENT_PLAYBACK_DONE: {
|
---|
187 | if (dev->buffer.position) {
|
---|
188 | dev->buffer.position +=
|
---|
189 | (dev->buffer.size / BUFFER_BLOCKS);
|
---|
190 | }
|
---|
191 | if ((!dev->buffer.position) ||
|
---|
192 | (dev->buffer.position >=
|
---|
193 | (dev->buffer.base + dev->buffer.size)))
|
---|
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;
|
---|
200 | }
|
---|
201 | case PCM_EVENT_PLAYBACK_TERMINATED: {
|
---|
202 | log_verbose("Playback terminated!");
|
---|
203 | return;
|
---|
204 | }
|
---|
205 | case PCM_EVENT_RECORDING_DONE: {
|
---|
206 | //TODO implement
|
---|
207 | break;
|
---|
208 | }
|
---|
209 | case PCM_EVENT_RECORDING_TERMINATED:
|
---|
210 | log_verbose("Recording terminated!");
|
---|
211 | return;
|
---|
212 | }
|
---|
213 |
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | static 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() \
|
---|
239 | do { \
|
---|
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 |
|
---|
252 | static 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 |
|
---|
267 | static 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 |
|
---|
282 | static 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 |
|
---|
292 | static 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 |
|
---|
304 | static 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 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * @}
|
---|
316 | */
|
---|