| 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 | break;
|
|---|
| 205 | }
|
|---|
| 206 | case PCM_EVENT_RECORDING_DONE: {
|
|---|
| 207 | break;
|
|---|
| 208 | }
|
|---|
| 209 | case PCM_EVENT_RECORDING_TERMINATED:
|
|---|
| 210 | log_verbose("Recording terminated!");
|
|---|
| 211 | break;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | }
|
|---|
| 215 | //TODO implement
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | static int get_buffer(audio_device_t *dev)
|
|---|
| 219 | {
|
|---|
| 220 | assert(dev);
|
|---|
| 221 | if (!dev->sess) {
|
|---|
| 222 | log_debug("No connection to device");
|
|---|
| 223 | return EIO;
|
|---|
| 224 | }
|
|---|
| 225 | if (dev->buffer.base) {
|
|---|
| 226 | log_debug("We already have a buffer");
|
|---|
| 227 | return EBUSY;
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | dev->buffer.size = 0;
|
|---|
| 231 |
|
|---|
| 232 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 233 | const int ret = audio_pcm_get_buffer(exch, &dev->buffer.base,
|
|---|
| 234 | &dev->buffer.size, &dev->buffer.id, device_event_callback, dev);
|
|---|
| 235 | async_exchange_end(exch);
|
|---|
| 236 | return ret;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | #define CHECK_BUFFER_AND_CONNECTION() \
|
|---|
| 240 | do { \
|
|---|
| 241 | assert(dev); \
|
|---|
| 242 | if (!dev->sess) { \
|
|---|
| 243 | log_debug("No connection to device"); \
|
|---|
| 244 | return EIO; \
|
|---|
| 245 | } \
|
|---|
| 246 | if (!dev->buffer.base) { \
|
|---|
| 247 | log_debug("We don't have a buffer"); \
|
|---|
| 248 | return ENOENT; \
|
|---|
| 249 | } \
|
|---|
| 250 | } while (0)
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 | static int release_buffer(audio_device_t *dev)
|
|---|
| 254 | {
|
|---|
| 255 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 256 |
|
|---|
| 257 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 258 | const int ret = audio_pcm_release_buffer(exch, dev->buffer.id);
|
|---|
| 259 | async_exchange_end(exch);
|
|---|
| 260 | if (ret == EOK) {
|
|---|
| 261 | dev->buffer.base = NULL;
|
|---|
| 262 | dev->buffer.size = 0;
|
|---|
| 263 | dev->buffer.position = NULL;
|
|---|
| 264 | }
|
|---|
| 265 | return ret;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | static int start_playback(audio_device_t *dev)
|
|---|
| 269 | {
|
|---|
| 270 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 271 |
|
|---|
| 272 | /* Fill the buffer first */
|
|---|
| 273 | audio_sink_mix_inputs(&dev->sink, dev->buffer.base, dev->buffer.size);
|
|---|
| 274 |
|
|---|
| 275 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 276 | const int ret = audio_pcm_start_playback(exch, dev->buffer.id,
|
|---|
| 277 | BUFFER_BLOCKS, dev->sink.format.channels,
|
|---|
| 278 | dev->sink.format.sampling_rate, dev->sink.format.sample_format);
|
|---|
| 279 | async_exchange_end(exch);
|
|---|
| 280 | return ret;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | static int stop_playback(audio_device_t *dev)
|
|---|
| 284 | {
|
|---|
| 285 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 286 |
|
|---|
| 287 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 288 | const int ret = audio_pcm_stop_playback(exch, dev->buffer.id);
|
|---|
| 289 | async_exchange_end(exch);
|
|---|
| 290 | return ret;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | static int start_recording(audio_device_t *dev)
|
|---|
| 294 | {
|
|---|
| 295 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 296 |
|
|---|
| 297 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 298 | const int ret = audio_pcm_start_record(exch, dev->buffer.id,
|
|---|
| 299 | BUFFER_BLOCKS, dev->sink.format.channels,
|
|---|
| 300 | dev->sink.format.sampling_rate, dev->sink.format.sample_format);
|
|---|
| 301 | async_exchange_end(exch);
|
|---|
| 302 | return ret;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | static int stop_recording(audio_device_t *dev)
|
|---|
| 306 | {
|
|---|
| 307 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 308 |
|
|---|
| 309 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 310 | const int ret = audio_pcm_stop_record(exch, dev->buffer.id);
|
|---|
| 311 | async_exchange_end(exch);
|
|---|
| 312 | return ret;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /**
|
|---|
| 316 | * @}
|
|---|
| 317 | */
|
|---|