[737b4c0] | 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>
|
---|
[e5bc912] | 40 | #include <inttypes.h>
|
---|
[737b4c0] | 41 | #include <loc.h>
|
---|
| 42 | #include <str.h>
|
---|
| 43 | #include <str_error.h>
|
---|
[b6e481b] | 44 | #include <as.h>
|
---|
[737b4c0] | 45 |
|
---|
[992ef56] | 46 |
|
---|
[737b4c0] | 47 | #include "audio_device.h"
|
---|
| 48 | #include "log.h"
|
---|
| 49 |
|
---|
[c799138] | 50 | /* hardwired to provide ~21ms per fragment */
|
---|
[e1d2f0e] | 51 | #define BUFFER_PARTS 16
|
---|
[992ef56] | 52 |
|
---|
[13df13c8] | 53 | static int device_sink_connection_callback(audio_sink_t *sink, bool new);
|
---|
[fa60cd69] | 54 | static int device_source_connection_callback(audio_source_t *source, bool new);
|
---|
[992ef56] | 55 | static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
[389ef25] | 56 | static int device_check_format(audio_sink_t* sink);
|
---|
[992ef56] | 57 | static int get_buffer(audio_device_t *dev);
|
---|
| 58 | static int release_buffer(audio_device_t *dev);
|
---|
[4eff63c] | 59 | static void advance_buffer(audio_device_t *dev, size_t size);
|
---|
[d1f144a] | 60 | static inline bool is_running(audio_device_t *dev)
|
---|
| 61 | {
|
---|
| 62 | assert(dev);
|
---|
| 63 | /* we release buffer on stop so this should be enough */
|
---|
| 64 | return (bool)dev->buffer.base;
|
---|
| 65 | }
|
---|
[737b4c0] | 66 |
|
---|
[c799138] | 67 | /**
|
---|
| 68 | * Initialize audio device structure.
|
---|
| 69 | * @param dev The structure to initialize.
|
---|
| 70 | * @param id Location service id of the device driver.
|
---|
| 71 | * @param name Name of the device.
|
---|
| 72 | * @return Error code.
|
---|
| 73 | */
|
---|
[737b4c0] | 74 | int audio_device_init(audio_device_t *dev, service_id_t id, const char *name)
|
---|
| 75 | {
|
---|
| 76 | assert(dev);
|
---|
| 77 | link_initialize(&dev->link);
|
---|
| 78 | dev->id = id;
|
---|
| 79 | dev->name = str_dup(name);
|
---|
[2cc5c835] | 80 | dev->sess = audio_pcm_open_service(id);
|
---|
[737b4c0] | 81 | if (!dev->sess) {
|
---|
| 82 | log_debug("Failed to connect to device \"%s\"", name);
|
---|
| 83 | return ENOMEM;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[1df3018a] | 86 | audio_sink_init(&dev->sink, name, dev, device_sink_connection_callback,
|
---|
[bee5349] | 87 | device_check_format, NULL, &AUDIO_FORMAT_ANY);
|
---|
[1df3018a] | 88 | audio_source_init(&dev->source, name, dev,
|
---|
| 89 | device_source_connection_callback, NULL, &AUDIO_FORMAT_ANY);
|
---|
[737b4c0] | 90 |
|
---|
[992ef56] | 91 | /* Init buffer members */
|
---|
| 92 | dev->buffer.base = NULL;
|
---|
| 93 | dev->buffer.position = NULL;
|
---|
| 94 | dev->buffer.size = 0;
|
---|
[5c98bb28] | 95 | dev->buffer.fragment_size = 0;
|
---|
[992ef56] | 96 |
|
---|
[e5bc912] | 97 | log_verbose("Initialized device (%p) '%s' with id %" PRIun ".",
|
---|
[737b4c0] | 98 | dev, dev->name, dev->id);
|
---|
| 99 |
|
---|
| 100 | return EOK;
|
---|
| 101 | }
|
---|
[c799138] | 102 |
|
---|
| 103 | /**
|
---|
| 104 | * Restore resource cplaimed during initialization.
|
---|
| 105 | * @param dev The device to release.
|
---|
| 106 | *
|
---|
| 107 | * NOT IMPLEMENTED
|
---|
| 108 | */
|
---|
[1df3018a] | 109 | void audio_device_fini(audio_device_t *dev)
|
---|
| 110 | {
|
---|
| 111 | //TODO implement;
|
---|
| 112 | }
|
---|
[737b4c0] | 113 |
|
---|
[c799138] | 114 | /**
|
---|
| 115 | * Get device provided audio source.
|
---|
| 116 | * @param dev Th device.
|
---|
| 117 | * @return pointer to aa audio source structure, NULL if the device is not
|
---|
| 118 | * capable of capturing audio.
|
---|
| 119 | */
|
---|
| 120 | audio_source_t * audio_device_get_source(audio_device_t *dev)
|
---|
| 121 | {
|
---|
| 122 | assert(dev);
|
---|
[e172429] | 123 | sysarg_t val;
|
---|
| 124 | int rc = audio_pcm_query_cap(dev->sess, AUDIO_CAP_CAPTURE, &val);
|
---|
| 125 | if (rc == EOK && val)
|
---|
[c799138] | 126 | return &dev->source;
|
---|
| 127 | return NULL;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /**
|
---|
| 131 | * Get device provided audio sink.
|
---|
| 132 | * @param dev Th device.
|
---|
| 133 | * @return pointer to aa audio source structure, NULL if the device is not
|
---|
| 134 | * capable of audio playback.
|
---|
| 135 | */
|
---|
| 136 | audio_sink_t * audio_device_get_sink(audio_device_t *dev)
|
---|
| 137 | {
|
---|
| 138 | assert(dev);
|
---|
[e172429] | 139 | sysarg_t val;
|
---|
| 140 | int rc = audio_pcm_query_cap(dev->sess, AUDIO_CAP_PLAYBACK, &val);
|
---|
| 141 | if (rc == EOK && val)
|
---|
[c799138] | 142 | return &dev->sink;
|
---|
| 143 | return NULL;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * Handle connection addition and removal.
|
---|
| 148 | * @param sink audio sink that is connected or disconnected.
|
---|
| 149 | * @param new True of a connection was added, false otherwise.
|
---|
| 150 | * @return Error code.
|
---|
| 151 | *
|
---|
| 152 | * Starts playback on first connection. Stops playback when there are no
|
---|
| 153 | * connections.
|
---|
| 154 | */
|
---|
[13df13c8] | 155 | static int device_sink_connection_callback(audio_sink_t* sink, bool new)
|
---|
[737b4c0] | 156 | {
|
---|
[1df3018a] | 157 | assert(sink);
|
---|
| 158 | audio_device_t *dev = sink->private_data;
|
---|
[fa60cd69] | 159 | if (new && list_count(&sink->connections) == 1) {
|
---|
[f3fced0] | 160 | log_verbose("First connection on device sink '%s'", sink->name);
|
---|
| 161 |
|
---|
[992ef56] | 162 | int ret = get_buffer(dev);
|
---|
[737b4c0] | 163 | if (ret != EOK) {
|
---|
| 164 | log_error("Failed to get device buffer: %s",
|
---|
| 165 | str_error(ret));
|
---|
| 166 | return ret;
|
---|
| 167 | }
|
---|
[018ab50] | 168 | audio_pcm_register_event_callback(dev->sess,
|
---|
[7f84430] | 169 | device_event_callback, dev);
|
---|
[f3fced0] | 170 |
|
---|
[eca79ff] | 171 | /* Fill the buffer first. Fill the first two fragments,
|
---|
| 172 | * so that we stay one fragment ahead */
|
---|
[7e706a3] | 173 | pcm_format_silence(dev->buffer.base, dev->buffer.size,
|
---|
| 174 | &dev->sink.format);
|
---|
[4eff63c] | 175 | //TODO add underrun detection.
|
---|
| 176 | const size_t size = dev->buffer.fragment_size * 2;
|
---|
| 177 | /* We never cross the end of the buffer here */
|
---|
| 178 | audio_sink_mix_inputs(&dev->sink, dev->buffer.position, size);
|
---|
| 179 | advance_buffer(dev, size);
|
---|
[2cc5c835] | 180 |
|
---|
[5c98bb28] | 181 | const unsigned frames = dev->buffer.fragment_size /
|
---|
| 182 | pcm_format_frame_size(&dev->sink.format);
|
---|
| 183 | log_verbose("Fragment frame count %u", frames);
|
---|
[92b638c] | 184 | ret = audio_pcm_start_playback_fragment(dev->sess, frames,
|
---|
[2cc5c835] | 185 | dev->sink.format.channels, dev->sink.format.sampling_rate,
|
---|
| 186 | dev->sink.format.sample_format);
|
---|
[737b4c0] | 187 | if (ret != EOK) {
|
---|
| 188 | log_error("Failed to start playback: %s",
|
---|
| 189 | str_error(ret));
|
---|
[992ef56] | 190 | release_buffer(dev);
|
---|
[737b4c0] | 191 | return ret;
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
[fa60cd69] | 194 | if (list_count(&sink->connections) == 0) {
|
---|
[13df13c8] | 195 | assert(!new);
|
---|
[6da3baec] | 196 | log_verbose("Removed last connection on device sink '%s'",
|
---|
| 197 | sink->name);
|
---|
[ff03f79] | 198 | int ret = audio_pcm_stop_playback(dev->sess);
|
---|
[737b4c0] | 199 | if (ret != EOK) {
|
---|
[842eecdd] | 200 | log_error("Failed to stop playback: %s",
|
---|
[737b4c0] | 201 | str_error(ret));
|
---|
| 202 | return ret;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | return EOK;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[c799138] | 208 | /**
|
---|
| 209 | * Handle connection addition and removal.
|
---|
| 210 | * @param source audio source that is connected or disconnected.
|
---|
| 211 | * @param new True of a connection was added, false otherwise.
|
---|
| 212 | * @return Error code.
|
---|
| 213 | *
|
---|
| 214 | * Starts capture on first connection. Stops capture when there are no
|
---|
| 215 | * connections.
|
---|
| 216 | */
|
---|
[fa60cd69] | 217 | static int device_source_connection_callback(audio_source_t *source, bool new)
|
---|
[737b4c0] | 218 | {
|
---|
[1df3018a] | 219 | assert(source);
|
---|
| 220 | audio_device_t *dev = source->private_data;
|
---|
[5c98bb28] | 221 | if (new && list_count(&source->connections) == 1) {
|
---|
[992ef56] | 222 | int ret = get_buffer(dev);
|
---|
[737b4c0] | 223 | if (ret != EOK) {
|
---|
| 224 | log_error("Failed to get device buffer: %s",
|
---|
| 225 | str_error(ret));
|
---|
| 226 | return ret;
|
---|
| 227 | }
|
---|
[c799138] | 228 |
|
---|
| 229 | //TODO set and test format
|
---|
| 230 |
|
---|
[5c98bb28] | 231 | const unsigned frames = dev->buffer.fragment_size /
|
---|
| 232 | pcm_format_frame_size(&dev->sink.format);
|
---|
[92b638c] | 233 | ret = audio_pcm_start_capture_fragment(dev->sess, frames,
|
---|
[c799138] | 234 | dev->source.format.channels,
|
---|
| 235 | dev->source.format.sampling_rate,
|
---|
| 236 | dev->source.format.sample_format);
|
---|
[737b4c0] | 237 | if (ret != EOK) {
|
---|
| 238 | log_error("Failed to start recording: %s",
|
---|
| 239 | str_error(ret));
|
---|
[992ef56] | 240 | release_buffer(dev);
|
---|
[737b4c0] | 241 | return ret;
|
---|
| 242 | }
|
---|
[fa60cd69] | 243 | }
|
---|
| 244 | if (list_count(&source->connections) == 0) { /* Disconnected */
|
---|
| 245 | assert(!new);
|
---|
[62310d7] | 246 | int ret = audio_pcm_stop_capture_immediate(dev->sess);
|
---|
[737b4c0] | 247 | if (ret != EOK) {
|
---|
| 248 | log_error("Failed to start recording: %s",
|
---|
| 249 | str_error(ret));
|
---|
| 250 | return ret;
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | return EOK;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[c799138] | 257 | /**
|
---|
| 258 | * Audio device event handler.
|
---|
| 259 | * @param iid initial call id.
|
---|
| 260 | * @param icall initial call structure.
|
---|
| 261 | * @param arg (unused)
|
---|
| 262 | */
|
---|
[992ef56] | 263 | static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 264 | {
|
---|
| 265 | /* Answer initial request */
|
---|
| 266 | async_answer_0(iid, EOK);
|
---|
| 267 | audio_device_t *dev = arg;
|
---|
| 268 | assert(dev);
|
---|
| 269 | while (1) {
|
---|
| 270 | ipc_call_t call;
|
---|
| 271 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 272 | async_answer_0(callid, EOK);
|
---|
[fd5c0b7] | 273 | switch(IPC_GET_IMETHOD(call)) {
|
---|
[57e8b3b] | 274 | case PCM_EVENT_FRAMES_PLAYED: {
|
---|
[7f6d84b] | 275 | struct timeval time1;
|
---|
| 276 | getuptime(&time1);
|
---|
[4eff63c] | 277 | //TODO add underrun detection.
|
---|
| 278 | /* We never cross the end of the buffer here */
|
---|
| 279 | audio_sink_mix_inputs(&dev->sink, dev->buffer.position,
|
---|
| 280 | dev->buffer.fragment_size);
|
---|
| 281 | advance_buffer(dev, dev->buffer.fragment_size);
|
---|
[7f6d84b] | 282 | struct timeval time2;
|
---|
| 283 | getuptime(&time2);
|
---|
| 284 | log_verbose("Time to mix sources: %li\n",
|
---|
[7f9d97f3] | 285 | tv_sub_diff(&time2, &time1));
|
---|
[fd5c0b7] | 286 | break;
|
---|
[992ef56] | 287 | }
|
---|
[8e77b60e] | 288 | case PCM_EVENT_CAPTURE_TERMINATED: {
|
---|
| 289 | log_verbose("Capture terminated");
|
---|
| 290 | dev->source.format = AUDIO_FORMAT_ANY;
|
---|
| 291 | const int ret = release_buffer(dev);
|
---|
| 292 | if (ret != EOK) {
|
---|
| 293 | log_error("Failed to release buffer: %s",
|
---|
| 294 | str_error(ret));
|
---|
| 295 | }
|
---|
| 296 | audio_pcm_unregister_event_callback(dev->sess);
|
---|
| 297 | break;
|
---|
| 298 | }
|
---|
| 299 | case PCM_EVENT_PLAYBACK_TERMINATED: {
|
---|
| 300 | log_verbose("Playback Terminated");
|
---|
| 301 | dev->sink.format = AUDIO_FORMAT_ANY;
|
---|
| 302 | const int ret = release_buffer(dev);
|
---|
| 303 | if (ret != EOK) {
|
---|
| 304 | log_error("Failed to release buffer: %s",
|
---|
| 305 | str_error(ret));
|
---|
| 306 | }
|
---|
| 307 | audio_pcm_unregister_event_callback(dev->sess);
|
---|
[3efaeb6] | 308 | break;
|
---|
[8e77b60e] | 309 | }
|
---|
[4eff63c] | 310 | case PCM_EVENT_FRAMES_CAPTURED: {
|
---|
| 311 | const int ret = audio_source_push_data(&dev->source,
|
---|
| 312 | dev->buffer.position, dev->buffer.fragment_size);
|
---|
| 313 | advance_buffer(dev, dev->buffer.fragment_size);
|
---|
| 314 | if (ret != EOK)
|
---|
| 315 | log_warning("Failed to push recorded data");
|
---|
[fd5c0b7] | 316 | break;
|
---|
[4eff63c] | 317 | }
|
---|
[3efaeb6] | 318 | case 0:
|
---|
[8e77b60e] | 319 | log_info("Device event callback hangup");
|
---|
[ab07cf0] | 320 | return;
|
---|
[992ef56] | 321 | }
|
---|
| 322 |
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
[eca79ff] | 325 |
|
---|
[c799138] | 326 | /**
|
---|
| 327 | * Test format against hardware limits.
|
---|
| 328 | * @param sink audio playback device.
|
---|
| 329 | * @return Error code.
|
---|
| 330 | */
|
---|
[389ef25] | 331 | static int device_check_format(audio_sink_t* sink)
|
---|
| 332 | {
|
---|
| 333 | assert(sink);
|
---|
| 334 | audio_device_t *dev = sink->private_data;
|
---|
| 335 | assert(dev);
|
---|
[d1f144a] | 336 | /* Check whether we are running */
|
---|
| 337 | if (is_running(dev))
|
---|
| 338 | return EBUSY;
|
---|
[389ef25] | 339 | log_verbose("Checking format on sink %s", sink->name);
|
---|
| 340 | return audio_pcm_test_format(dev->sess, &sink->format.channels,
|
---|
| 341 | &sink->format.sampling_rate, &sink->format.sample_format);
|
---|
| 342 | }
|
---|
[992ef56] | 343 |
|
---|
[c799138] | 344 | /**
|
---|
| 345 | * Get access to device buffer.
|
---|
| 346 | * @param dev Audio device.
|
---|
| 347 | * @return Error code.
|
---|
| 348 | */
|
---|
[992ef56] | 349 | static int get_buffer(audio_device_t *dev)
|
---|
| 350 | {
|
---|
| 351 | assert(dev);
|
---|
| 352 | if (!dev->sess) {
|
---|
| 353 | log_debug("No connection to device");
|
---|
| 354 | return EIO;
|
---|
| 355 | }
|
---|
| 356 | if (dev->buffer.base) {
|
---|
| 357 | log_debug("We already have a buffer");
|
---|
| 358 | return EBUSY;
|
---|
| 359 | }
|
---|
| 360 |
|
---|
[eca79ff] | 361 | /* Ask for largest buffer possible */
|
---|
| 362 | size_t preferred_size = 0;
|
---|
[992ef56] | 363 |
|
---|
[5c98bb28] | 364 | const int ret = audio_pcm_get_buffer(dev->sess, &dev->buffer.base,
|
---|
[eca79ff] | 365 | &preferred_size);
|
---|
| 366 | if (ret == EOK) {
|
---|
| 367 | dev->buffer.size = preferred_size;
|
---|
[5c98bb28] | 368 | dev->buffer.fragment_size = dev->buffer.size / BUFFER_PARTS;
|
---|
[eca79ff] | 369 | dev->buffer.position = dev->buffer.base;
|
---|
| 370 | }
|
---|
[5c98bb28] | 371 | return ret;
|
---|
| 372 |
|
---|
[992ef56] | 373 | }
|
---|
| 374 |
|
---|
[c799138] | 375 | /**
|
---|
| 376 | * Surrender access to device buffer.
|
---|
| 377 | * @param dev Audio device.
|
---|
| 378 | * @return Error code.
|
---|
| 379 | */
|
---|
[992ef56] | 380 | static int release_buffer(audio_device_t *dev)
|
---|
| 381 | {
|
---|
[2cc5c835] | 382 | assert(dev);
|
---|
| 383 | assert(dev->buffer.base);
|
---|
[992ef56] | 384 |
|
---|
[2cc5c835] | 385 | const int ret = audio_pcm_release_buffer(dev->sess);
|
---|
[992ef56] | 386 | if (ret == EOK) {
|
---|
[b6e481b] | 387 | as_area_destroy(dev->buffer.base);
|
---|
[992ef56] | 388 | dev->buffer.base = NULL;
|
---|
| 389 | dev->buffer.size = 0;
|
---|
| 390 | dev->buffer.position = NULL;
|
---|
[2cc5c835] | 391 | } else {
|
---|
[7373117] | 392 | log_warning("Failed to release buffer: %s", str_error(ret));
|
---|
[992ef56] | 393 | }
|
---|
| 394 | return ret;
|
---|
| 395 | }
|
---|
[eca79ff] | 396 |
|
---|
[c799138] | 397 | /**
|
---|
[4eff63c] | 398 | * Move buffer position pointer.
|
---|
[c799138] | 399 | * @param dev Audio device.
|
---|
[4eff63c] | 400 | * @param size number of bytes to move forward
|
---|
[c799138] | 401 | */
|
---|
[4eff63c] | 402 | static void advance_buffer(audio_device_t *dev, size_t size)
|
---|
[eca79ff] | 403 | {
|
---|
| 404 | assert(dev);
|
---|
| 405 | assert(dev->buffer.position >= dev->buffer.base);
|
---|
| 406 | assert(dev->buffer.position < (dev->buffer.base + dev->buffer.size));
|
---|
| 407 | dev->buffer.position += size;
|
---|
| 408 | if (dev->buffer.position == (dev->buffer.base + dev->buffer.size))
|
---|
| 409 | dev->buffer.position = dev->buffer.base;
|
---|
| 410 | }
|
---|
[737b4c0] | 411 | /**
|
---|
| 412 | * @}
|
---|
| 413 | */
|
---|