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

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

Cleanup audio_pcm interface.

  • Property mode set to 100644
File size: 6.8 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_BLOCKS 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 get_buffer(audio_device_t *dev);
54static int release_buffer(audio_device_t *dev);
55
56
57int audio_device_init(audio_device_t *dev, service_id_t id, const char *name)
58{
59 assert(dev);
60 link_initialize(&dev->link);
61 dev->id = id;
62 dev->name = str_dup(name);
63 dev->sess = audio_pcm_open_service(id);
64 if (!dev->sess) {
65 log_debug("Failed to connect to device \"%s\"", name);
66 return ENOMEM;
67 }
68
69 audio_sink_init(&dev->sink, name, dev, device_sink_connection_callback,
70 &AUDIO_FORMAT_ANY);
71 audio_source_init(&dev->source, name, dev,
72 device_source_connection_callback, NULL, &AUDIO_FORMAT_ANY);
73
74 /* Init buffer members */
75 fibril_mutex_initialize(&dev->buffer.guard);
76 fibril_condvar_initialize(&dev->buffer.wc);
77 dev->buffer.base = NULL;
78 dev->buffer.position = NULL;
79 dev->buffer.size = 0;
80
81 log_verbose("Initialized device (%p) '%s' with id %u.",
82 dev, dev->name, dev->id);
83
84 return EOK;
85}
86void audio_device_fini(audio_device_t *dev)
87{
88 //TODO implement;
89}
90
91static int device_sink_connection_callback(audio_sink_t* sink, bool new)
92{
93 assert(sink);
94 audio_device_t *dev = sink->private_data;
95 if (new && list_count(&sink->sources) == 1) {
96 log_verbose("First connection on device sink '%s'", sink->name);
97
98 int ret = get_buffer(dev);
99 if (ret != EOK) {
100 log_error("Failed to get device buffer: %s",
101 str_error(ret));
102 return ret;
103 }
104
105 /* Fill the buffer first */
106 audio_sink_mix_inputs(&dev->sink,
107 dev->buffer.base, dev->buffer.size);
108
109 ret = audio_pcm_start_playback(dev->sess, BUFFER_BLOCKS,
110 dev->sink.format.channels, dev->sink.format.sampling_rate,
111 dev->sink.format.sample_format);
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 assert(!new);
121 log_verbose("No connections on device sink '%s'", sink->name);
122 int ret = audio_pcm_stop_playback(dev->sess);
123 if (ret != EOK) {
124 log_error("Failed to start playback: %s",
125 str_error(ret));
126 return ret;
127 }
128 dev->sink.format = AUDIO_FORMAT_ANY;
129 ret = release_buffer(dev);
130 if (ret != EOK) {
131 log_error("Failed to release buffer: %s",
132 str_error(ret));
133 return ret;
134 }
135 }
136 return EOK;
137}
138
139static int device_source_connection_callback(audio_source_t *source)
140{
141 assert(source);
142 audio_device_t *dev = source->private_data;
143 if (source->connected_sink) {
144 int ret = get_buffer(dev);
145 if (ret != EOK) {
146 log_error("Failed to get device buffer: %s",
147 str_error(ret));
148 return ret;
149 }
150 ret = audio_pcm_start_record(dev->sess, BUFFER_BLOCKS,
151 dev->sink.format.channels, dev->sink.format.sampling_rate,
152 dev->sink.format.sample_format);
153 if (ret != EOK) {
154 log_error("Failed to start recording: %s",
155 str_error(ret));
156 release_buffer(dev);
157 return ret;
158 }
159 } else { /* Disconnected */
160 int ret = audio_pcm_stop_record(dev->sess);
161 if (ret != EOK) {
162 log_error("Failed to start recording: %s",
163 str_error(ret));
164 return ret;
165 }
166 source->format = AUDIO_FORMAT_ANY;
167 ret = release_buffer(dev);
168 if (ret != EOK) {
169 log_error("Failed to release buffer: %s",
170 str_error(ret));
171 return ret;
172 }
173 }
174
175 return EOK;
176}
177
178static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void *arg)
179{
180 /* Answer initial request */
181 async_answer_0(iid, EOK);
182 audio_device_t *dev = arg;
183 assert(dev);
184 while (1) {
185 ipc_call_t call;
186 ipc_callid_t callid = async_get_call(&call);
187 async_answer_0(callid, EOK);
188 switch(IPC_GET_IMETHOD(call)) {
189 case PCM_EVENT_PLAYBACK_DONE: {
190 if (dev->buffer.position) {
191 dev->buffer.position +=
192 (dev->buffer.size / BUFFER_BLOCKS);
193 }
194 if ((!dev->buffer.position) ||
195 (dev->buffer.position >=
196 (dev->buffer.base + dev->buffer.size)))
197 {
198 dev->buffer.position = dev->buffer.base;
199 }
200 audio_sink_mix_inputs(&dev->sink, dev->buffer.position,
201 dev->buffer.size / BUFFER_BLOCKS);
202 break;
203 }
204 case PCM_EVENT_PLAYBACK_TERMINATED: {
205 log_verbose("Playback terminated!");
206 return;
207 }
208 case PCM_EVENT_RECORDING_DONE: {
209 //TODO implement
210 break;
211 }
212 case PCM_EVENT_RECORDING_TERMINATED:
213 log_verbose("Recording terminated!");
214 return;
215 }
216
217 }
218}
219
220static int get_buffer(audio_device_t *dev)
221{
222 assert(dev);
223 if (!dev->sess) {
224 log_debug("No connection to device");
225 return EIO;
226 }
227 if (dev->buffer.base) {
228 log_debug("We already have a buffer");
229 return EBUSY;
230 }
231
232 dev->buffer.size = 0;
233
234 return audio_pcm_get_buffer(dev->sess, &dev->buffer.base,
235 &dev->buffer.size, device_event_callback, dev);
236}
237
238static int release_buffer(audio_device_t *dev)
239{
240 assert(dev);
241 assert(dev->buffer.base);
242
243 const int ret = audio_pcm_release_buffer(dev->sess);
244 if (ret == EOK) {
245 dev->buffer.base = NULL;
246 dev->buffer.size = 0;
247 dev->buffer.position = NULL;
248 } else {
249 log_debug("Failed to release buffer: %s", str_error(ret));
250 }
251 return ret;
252}
253/**
254 * @}
255 */
Note: See TracBrowser for help on using the repository browser.