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

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

hound: cleanup

remove unused sync primitives
move buffer filling to a helper function

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