| 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 | int ret = get_buffer(dev);
|
|---|
| 103 | if (ret != EOK) {
|
|---|
| 104 | log_error("Failed to get device buffer: %s",
|
|---|
| 105 | str_error(ret));
|
|---|
| 106 | return ret;
|
|---|
| 107 | }
|
|---|
| 108 | ret = start_playback(dev);
|
|---|
| 109 | if (ret != EOK) {
|
|---|
| 110 | log_error("Failed to start playback: %s",
|
|---|
| 111 | str_error(ret));
|
|---|
| 112 | release_buffer(dev);
|
|---|
| 113 | return ret;
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | if (list_count(&sink->sources) == 0) {
|
|---|
| 117 | int ret = stop_playback(dev);
|
|---|
| 118 | if (ret != EOK) {
|
|---|
| 119 | log_error("Failed to start playback: %s",
|
|---|
| 120 | str_error(ret));
|
|---|
| 121 | return ret;
|
|---|
| 122 | }
|
|---|
| 123 | dev->sink.format = AUDIO_FORMAT_ANY;
|
|---|
| 124 | ret = release_buffer(dev);
|
|---|
| 125 | if (ret != EOK) {
|
|---|
| 126 | log_error("Failed to release buffer: %s",
|
|---|
| 127 | str_error(ret));
|
|---|
| 128 | return ret;
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 | return EOK;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | static int device_source_connection_callback(audio_source_t *source)
|
|---|
| 135 | {
|
|---|
| 136 | assert(source);
|
|---|
| 137 | audio_device_t *dev = source->private_data;
|
|---|
| 138 | if (source->connected_sink) {
|
|---|
| 139 | int ret = get_buffer(dev);
|
|---|
| 140 | if (ret != EOK) {
|
|---|
| 141 | log_error("Failed to get device buffer: %s",
|
|---|
| 142 | str_error(ret));
|
|---|
| 143 | return ret;
|
|---|
| 144 | }
|
|---|
| 145 | ret = start_recording(dev);
|
|---|
| 146 | if (ret != EOK) {
|
|---|
| 147 | log_error("Failed to start recording: %s",
|
|---|
| 148 | str_error(ret));
|
|---|
| 149 | release_buffer(dev);
|
|---|
| 150 | return ret;
|
|---|
| 151 | }
|
|---|
| 152 | } else { /* Disconnected */
|
|---|
| 153 | int ret = stop_recording(dev);
|
|---|
| 154 | if (ret != EOK) {
|
|---|
| 155 | log_error("Failed to start recording: %s",
|
|---|
| 156 | str_error(ret));
|
|---|
| 157 | return ret;
|
|---|
| 158 | }
|
|---|
| 159 | source->format = AUDIO_FORMAT_ANY;
|
|---|
| 160 | ret = release_buffer(dev);
|
|---|
| 161 | if (ret != EOK) {
|
|---|
| 162 | log_error("Failed to release buffer: %s",
|
|---|
| 163 | str_error(ret));
|
|---|
| 164 | return ret;
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | return EOK;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 172 | {
|
|---|
| 173 | /* Answer initial request */
|
|---|
| 174 | async_answer_0(iid, EOK);
|
|---|
| 175 | audio_device_t *dev = arg;
|
|---|
| 176 | assert(dev);
|
|---|
| 177 | while (1) {
|
|---|
| 178 | ipc_call_t call;
|
|---|
| 179 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 180 | async_answer_0(callid, EOK);
|
|---|
| 181 | if (IPC_GET_IMETHOD(call) != IPC_FIRST_USER_METHOD) {
|
|---|
| 182 | log_debug("Unknown event.\n");
|
|---|
| 183 | continue;
|
|---|
| 184 | }
|
|---|
| 185 | // Assume playback for now
|
|---|
| 186 | if (dev->buffer.position) {
|
|---|
| 187 | dev->buffer.position += dev->buffer.size / BUFFER_BLOCKS;
|
|---|
| 188 | }
|
|---|
| 189 | if (!dev->buffer.position ||
|
|---|
| 190 | dev->buffer.position >= dev->buffer.base + dev->buffer.size)
|
|---|
| 191 | {
|
|---|
| 192 | dev->buffer.position = dev->buffer.base;
|
|---|
| 193 | }
|
|---|
| 194 | audio_sink_mix_inputs(
|
|---|
| 195 | &dev->sink, dev->buffer.base,
|
|---|
| 196 | dev->buffer.size / BUFFER_BLOCKS);
|
|---|
| 197 |
|
|---|
| 198 | }
|
|---|
| 199 | //TODO implement
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | static int get_buffer(audio_device_t *dev)
|
|---|
| 203 | {
|
|---|
| 204 | assert(dev);
|
|---|
| 205 | if (!dev->sess) {
|
|---|
| 206 | log_debug("No connection to device");
|
|---|
| 207 | return EIO;
|
|---|
| 208 | }
|
|---|
| 209 | if (dev->buffer.base) {
|
|---|
| 210 | log_debug("We already have a buffer");
|
|---|
| 211 | return EBUSY;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | dev->buffer.size = 0;
|
|---|
| 215 |
|
|---|
| 216 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 217 | const int ret = audio_pcm_get_buffer(exch, &dev->buffer.base,
|
|---|
| 218 | &dev->buffer.size, &dev->buffer.id, device_event_callback, dev);
|
|---|
| 219 | async_exchange_end(exch);
|
|---|
| 220 | return ret;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | #define CHECK_BUFFER_AND_CONNECTION() \
|
|---|
| 224 | do { \
|
|---|
| 225 | assert(dev); \
|
|---|
| 226 | if (!dev->sess) { \
|
|---|
| 227 | log_debug("No connection to device"); \
|
|---|
| 228 | return EIO; \
|
|---|
| 229 | } \
|
|---|
| 230 | if (!dev->buffer.base) { \
|
|---|
| 231 | log_debug("We don't have a buffer"); \
|
|---|
| 232 | return ENOENT; \
|
|---|
| 233 | } \
|
|---|
| 234 | } while (0)
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | static int release_buffer(audio_device_t *dev)
|
|---|
| 238 | {
|
|---|
| 239 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 240 |
|
|---|
| 241 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 242 | const int ret = audio_pcm_release_buffer(exch, dev->buffer.id);
|
|---|
| 243 | async_exchange_end(exch);
|
|---|
| 244 | if (ret == EOK) {
|
|---|
| 245 | dev->buffer.base = NULL;
|
|---|
| 246 | dev->buffer.size = 0;
|
|---|
| 247 | dev->buffer.position = NULL;
|
|---|
| 248 | }
|
|---|
| 249 | return ret;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | static int start_playback(audio_device_t *dev)
|
|---|
| 253 | {
|
|---|
| 254 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 255 |
|
|---|
| 256 | /* Fill the buffer first */
|
|---|
| 257 | audio_sink_mix_inputs(&dev->sink, dev->buffer.base, dev->buffer.size);
|
|---|
| 258 |
|
|---|
| 259 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 260 | const int ret = audio_pcm_start_playback(exch, dev->buffer.id,
|
|---|
| 261 | BUFFER_BLOCKS, dev->sink.format.channels,
|
|---|
| 262 | dev->sink.format.sampling_rate, dev->sink.format.sample_format);
|
|---|
| 263 | async_exchange_end(exch);
|
|---|
| 264 | return ret;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | static int stop_playback(audio_device_t *dev)
|
|---|
| 268 | {
|
|---|
| 269 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 270 |
|
|---|
| 271 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 272 | const int ret = audio_pcm_stop_playback(exch, dev->buffer.id);
|
|---|
| 273 | async_exchange_end(exch);
|
|---|
| 274 | return ret;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | static int start_recording(audio_device_t *dev)
|
|---|
| 278 | {
|
|---|
| 279 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 280 |
|
|---|
| 281 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 282 | const int ret = audio_pcm_start_record(exch, dev->buffer.id,
|
|---|
| 283 | BUFFER_BLOCKS, dev->sink.format.channels,
|
|---|
| 284 | dev->sink.format.sampling_rate, dev->sink.format.sample_format);
|
|---|
| 285 | async_exchange_end(exch);
|
|---|
| 286 | return ret;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | static int stop_recording(audio_device_t *dev)
|
|---|
| 290 | {
|
|---|
| 291 | CHECK_BUFFER_AND_CONNECTION();
|
|---|
| 292 |
|
|---|
| 293 | async_exch_t *exch = async_exchange_begin(dev->sess);
|
|---|
| 294 | const int ret = audio_pcm_stop_record(exch, dev->buffer.id);
|
|---|
| 295 | async_exchange_end(exch);
|
|---|
| 296 | return ret;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | /**
|
|---|
| 300 | * @}
|
|---|
| 301 | */
|
|---|