| 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 <stdlib.h>
|
|---|
| 39 |
|
|---|
| 40 | #include "hound.h"
|
|---|
| 41 | #include "audio_client.h"
|
|---|
| 42 | #include "audio_device.h"
|
|---|
| 43 | #include "audio_sink.h"
|
|---|
| 44 | #include "audio_source.h"
|
|---|
| 45 | #include "connection.h"
|
|---|
| 46 | #include "log.h"
|
|---|
| 47 | #include "errno.h"
|
|---|
| 48 | #include "str_error.h"
|
|---|
| 49 |
|
|---|
| 50 | #define FIND_BY_NAME(type) \
|
|---|
| 51 | do { \
|
|---|
| 52 | assert(list); \
|
|---|
| 53 | assert(name); \
|
|---|
| 54 | list_foreach(*list, it) { \
|
|---|
| 55 | audio_ ## type ## _t *dev = \
|
|---|
| 56 | audio_ ## type ## _list_instance(it); \
|
|---|
| 57 | if (str_cmp(name, dev->name) == 0) { \
|
|---|
| 58 | log_debug("%s with name '%s' is in the list", \
|
|---|
| 59 | #type, name); \
|
|---|
| 60 | return dev; \
|
|---|
| 61 | } \
|
|---|
| 62 | } \
|
|---|
| 63 | return NULL; \
|
|---|
| 64 | } while (0)
|
|---|
| 65 |
|
|---|
| 66 | static audio_device_t * find_device_by_name(list_t *list, const char *name)
|
|---|
| 67 | {
|
|---|
| 68 | FIND_BY_NAME(device);
|
|---|
| 69 | }
|
|---|
| 70 | static audio_source_t * find_source_by_name(list_t *list, const char *name)
|
|---|
| 71 | {
|
|---|
| 72 | FIND_BY_NAME(source);
|
|---|
| 73 | }
|
|---|
| 74 | static audio_sink_t * find_sink_by_name(list_t *list, const char *name)
|
|---|
| 75 | {
|
|---|
| 76 | FIND_BY_NAME(sink);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | static int hound_disconnect_internal(hound_t *hound, const char* source_name, const char* sink_name);
|
|---|
| 80 |
|
|---|
| 81 | int hound_init(hound_t *hound)
|
|---|
| 82 | {
|
|---|
| 83 | assert(hound);
|
|---|
| 84 | fibril_mutex_initialize(&hound->list_guard);
|
|---|
| 85 | list_initialize(&hound->devices);
|
|---|
| 86 | list_initialize(&hound->contexts);
|
|---|
| 87 | list_initialize(&hound->sources);
|
|---|
| 88 | list_initialize(&hound->sinks);
|
|---|
| 89 | list_initialize(&hound->connections);
|
|---|
| 90 | return EOK;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | int hound_add_ctx(hound_t *hound, hound_ctx_t *ctx)
|
|---|
| 94 | {
|
|---|
| 95 | log_info("Trying to add context %p", ctx);
|
|---|
| 96 | assert(hound);
|
|---|
| 97 | if (!ctx)
|
|---|
| 98 | return EINVAL;
|
|---|
| 99 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 100 | list_append(&ctx->link, &hound->contexts);
|
|---|
| 101 | //TODO register sinks/sources
|
|---|
| 102 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 103 | return EOK;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | int hound_remove_ctx(hound_t *hound, hound_ctx_t *ctx)
|
|---|
| 107 | {
|
|---|
| 108 | assert(hound);
|
|---|
| 109 | if (!ctx)
|
|---|
| 110 | return EINVAL;
|
|---|
| 111 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 112 | list_remove(&ctx->link);
|
|---|
| 113 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 114 | return EOK;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | hound_ctx_t *hound_get_ctx_by_id(hound_t *hound, hound_context_id_t id)
|
|---|
| 118 | {
|
|---|
| 119 | assert(hound);
|
|---|
| 120 |
|
|---|
| 121 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 122 | hound_ctx_t *res = NULL;
|
|---|
| 123 | list_foreach(hound->contexts, it) {
|
|---|
| 124 | hound_ctx_t *ctx = hound_ctx_from_link(it);
|
|---|
| 125 | if (hound_ctx_get_id(ctx) == id) {
|
|---|
| 126 | res = ctx;
|
|---|
| 127 | break;
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 131 | return res;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | int hound_add_device(hound_t *hound, service_id_t id, const char *name)
|
|---|
| 135 | {
|
|---|
| 136 | log_verbose("Adding device \"%s\", service: %zu", name, id);
|
|---|
| 137 |
|
|---|
| 138 | assert(hound);
|
|---|
| 139 | if (!name || !id) {
|
|---|
| 140 | log_debug("Incorrect parameters.");
|
|---|
| 141 | return EINVAL;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | list_foreach(hound->devices, it) {
|
|---|
| 145 | audio_device_t *dev = audio_device_list_instance(it);
|
|---|
| 146 | if (dev->id == id) {
|
|---|
| 147 | log_debug("Device with id %zu is already present", id);
|
|---|
| 148 | return EEXISTS;
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | audio_device_t *dev = find_device_by_name(&hound->devices, name);
|
|---|
| 153 | if (dev) {
|
|---|
| 154 | log_debug("Device with name %s is already present", name);
|
|---|
| 155 | return EEXISTS;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | dev = malloc(sizeof(audio_device_t));
|
|---|
| 159 | if (!dev) {
|
|---|
| 160 | log_debug("Failed to malloc device structure.");
|
|---|
| 161 | return ENOMEM;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | const int ret = audio_device_init(dev, id, name);
|
|---|
| 165 | if (ret != EOK) {
|
|---|
| 166 | log_debug("Failed to initialize new audio device: %s",
|
|---|
| 167 | str_error(ret));
|
|---|
| 168 | free(dev);
|
|---|
| 169 | return ret;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | list_append(&dev->link, &hound->devices);
|
|---|
| 173 | log_info("Added new device: '%s'", dev->name);
|
|---|
| 174 |
|
|---|
| 175 | audio_source_t *source = audio_device_get_source(dev);
|
|---|
| 176 | if (source) {
|
|---|
| 177 | const int ret = hound_add_source(hound, source);
|
|---|
| 178 | if (ret != EOK) {
|
|---|
| 179 | log_debug("Failed to add device source: %s",
|
|---|
| 180 | str_error(ret));
|
|---|
| 181 | audio_device_fini(dev);
|
|---|
| 182 | return ret;
|
|---|
| 183 | }
|
|---|
| 184 | log_verbose("Added source: '%s'.", source->name);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | audio_sink_t *sink = audio_device_get_sink(dev);
|
|---|
| 188 | if (sink) {
|
|---|
| 189 | const int ret = hound_add_sink(hound, sink);
|
|---|
| 190 | if (ret != EOK) {
|
|---|
| 191 | log_debug("Failed to add device sink: %s",
|
|---|
| 192 | str_error(ret));
|
|---|
| 193 | audio_device_fini(dev);
|
|---|
| 194 | return ret;
|
|---|
| 195 | }
|
|---|
| 196 | log_verbose("Added sink: '%s'.", sink->name);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | if (!source && !sink)
|
|---|
| 200 | log_warning("Neither sink nor source on device '%s'.", name);
|
|---|
| 201 |
|
|---|
| 202 | return ret;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | int hound_add_source(hound_t *hound, audio_source_t *source)
|
|---|
| 206 | {
|
|---|
| 207 | assert(hound);
|
|---|
| 208 | if (!source || !source->name || str_cmp(source->name, "default") == 0) {
|
|---|
| 209 | log_debug("Invalid source specified.");
|
|---|
| 210 | return EINVAL;
|
|---|
| 211 | }
|
|---|
| 212 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 213 | if (find_source_by_name(&hound->sources, source->name)) {
|
|---|
| 214 | log_debug("Source by that name already exists");
|
|---|
| 215 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 216 | return EEXISTS;
|
|---|
| 217 | }
|
|---|
| 218 | list_append(&source->link, &hound->sources);
|
|---|
| 219 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 220 | return EOK;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | int hound_add_sink(hound_t *hound, audio_sink_t *sink)
|
|---|
| 224 | {
|
|---|
| 225 | assert(hound);
|
|---|
| 226 | if (!sink || !sink->name || str_cmp(sink->name, "default") == 0) {
|
|---|
| 227 | log_debug("Invalid source specified.");
|
|---|
| 228 | return EINVAL;
|
|---|
| 229 | }
|
|---|
| 230 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 231 | if (find_sink_by_name(&hound->sinks, sink->name)) {
|
|---|
| 232 | log_debug("Sink by that name already exists");
|
|---|
| 233 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 234 | return EEXISTS;
|
|---|
| 235 | }
|
|---|
| 236 | list_append(&sink->link, &hound->sinks);
|
|---|
| 237 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 238 | return EOK;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | int hound_remove_source(hound_t *hound, audio_source_t *source)
|
|---|
| 242 | {
|
|---|
| 243 | assert(hound);
|
|---|
| 244 | if (!source)
|
|---|
| 245 | return EINVAL;
|
|---|
| 246 | log_verbose("Removing source '%s'.", source->name);
|
|---|
| 247 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 248 |
|
|---|
| 249 | list_remove(&source->link);
|
|---|
| 250 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 251 | return EOK;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | int hound_remove_sink(hound_t *hound, audio_sink_t *sink)
|
|---|
| 255 | {
|
|---|
| 256 | assert(hound);
|
|---|
| 257 | if (!sink)
|
|---|
| 258 | return EINVAL;
|
|---|
| 259 | log_verbose("Removing sink '%s'.", sink->name);
|
|---|
| 260 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 261 |
|
|---|
| 262 | if (!list_empty(&sink->connections)) {
|
|---|
| 263 | // TODO disconnect instead
|
|---|
| 264 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 265 | return EBUSY;
|
|---|
| 266 | }
|
|---|
| 267 | list_remove(&sink->link);
|
|---|
| 268 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 269 | return EOK;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | int hound_list_sources(hound_t *hound, const char ***list, size_t *size)
|
|---|
| 273 | {
|
|---|
| 274 | assert(hound);
|
|---|
| 275 | if (!list || !size)
|
|---|
| 276 | return EINVAL;
|
|---|
| 277 |
|
|---|
| 278 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 279 | const size_t count = list_count(&hound->sources);
|
|---|
| 280 | if (count == 0) {
|
|---|
| 281 | *list = NULL;
|
|---|
| 282 | *size = 0;
|
|---|
| 283 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 284 | return EOK;
|
|---|
| 285 | }
|
|---|
| 286 | const char **names = calloc(count, sizeof(char *));
|
|---|
| 287 | int ret = names ? EOK : ENOMEM;
|
|---|
| 288 | for (size_t i = 0; i < count && ret == EOK; ++i) {
|
|---|
| 289 | link_t *slink = list_nth(&hound->sources, i);
|
|---|
| 290 | audio_source_t *source = audio_source_list_instance(slink);
|
|---|
| 291 | names[i] = str_dup(source->name);
|
|---|
| 292 | if (names[i])
|
|---|
| 293 | ret = ENOMEM;
|
|---|
| 294 | }
|
|---|
| 295 | if (ret == EOK) {
|
|---|
| 296 | *size = count;
|
|---|
| 297 | *list = names;
|
|---|
| 298 | } else {
|
|---|
| 299 | for (size_t i = 0; i < count; ++i)
|
|---|
| 300 | free(names[i]);
|
|---|
| 301 | free(names);
|
|---|
| 302 | }
|
|---|
| 303 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 304 | return ret;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | int hound_list_sinks(hound_t *hound, const char ***list, size_t *size)
|
|---|
| 308 | {
|
|---|
| 309 | assert(hound);
|
|---|
| 310 | if (!list || !size)
|
|---|
| 311 | return EINVAL;
|
|---|
| 312 |
|
|---|
| 313 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 314 | const size_t count = list_count(&hound->sinks);
|
|---|
| 315 | if (count == 0) {
|
|---|
| 316 | *list = NULL;
|
|---|
| 317 | *size = 0;
|
|---|
| 318 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 319 | return EOK;
|
|---|
| 320 | }
|
|---|
| 321 | const char **names = calloc(count, sizeof(char *));
|
|---|
| 322 | int ret = names ? EOK : ENOMEM;
|
|---|
| 323 | for (size_t i = 0; i < count && ret == EOK; ++i) {
|
|---|
| 324 | link_t *slink = list_nth(&hound->sinks, i);
|
|---|
| 325 | audio_sink_t *sink = audio_sink_list_instance(slink);
|
|---|
| 326 | names[i] = str_dup(sink->name);
|
|---|
| 327 | if (!names[i])
|
|---|
| 328 | ret = ENOMEM;
|
|---|
| 329 | }
|
|---|
| 330 | if (ret == EOK) {
|
|---|
| 331 | *size = count;
|
|---|
| 332 | *list = names;
|
|---|
| 333 | } else {
|
|---|
| 334 | for (size_t i = 0; i < count; ++i)
|
|---|
| 335 | free(names[i]);
|
|---|
| 336 | free(names);
|
|---|
| 337 | }
|
|---|
| 338 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 339 | return ret;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | int hound_list_connections(hound_t *hound, const char ***sources,
|
|---|
| 343 | const char ***sinks, size_t *size)
|
|---|
| 344 | {
|
|---|
| 345 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 346 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 347 | return ENOTSUP;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | int hound_connect(hound_t *hound, const char* source_name, const char* sink_name)
|
|---|
| 351 | {
|
|---|
| 352 | assert(hound);
|
|---|
| 353 | log_verbose("Connecting '%s' to '%s'.", source_name, sink_name);
|
|---|
| 354 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 355 |
|
|---|
| 356 | if (list_empty(&hound->sinks)) {
|
|---|
| 357 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 358 | log_debug("No sinks available");
|
|---|
| 359 | return EINVAL;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | if (list_empty(&hound->sources)) {
|
|---|
| 363 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 364 | log_debug("No sinks available");
|
|---|
| 365 | return EINVAL;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | audio_source_t *source =
|
|---|
| 369 | audio_source_list_instance(list_first(&hound->sources));
|
|---|
| 370 | if (str_cmp(source_name, "default") != 0)
|
|---|
| 371 | source = find_source_by_name(&hound->sources, source_name);
|
|---|
| 372 |
|
|---|
| 373 | audio_sink_t *sink =
|
|---|
| 374 | audio_sink_list_instance(list_first(&hound->sinks));
|
|---|
| 375 | if (str_cmp(sink_name, "default") != 0)
|
|---|
| 376 | sink = find_sink_by_name(&hound->sinks, sink_name);
|
|---|
| 377 |
|
|---|
| 378 | if (!source || !sink) {
|
|---|
| 379 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 380 | log_debug("Source (%p), or sink (%p) not found", source, sink);
|
|---|
| 381 | return ENOENT;
|
|---|
| 382 | }
|
|---|
| 383 | connection_t *conn = connection_create(source, sink);
|
|---|
| 384 | if (!conn) {
|
|---|
| 385 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 386 | log_debug("Failed to create connection");
|
|---|
| 387 | return ENOMEM;
|
|---|
| 388 | }
|
|---|
| 389 | list_append(&conn->hound_link, &hound->connections);
|
|---|
| 390 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 391 | log_debug("CONNECTED: %s -> %s", source_name, sink_name);
|
|---|
| 392 | return EOK;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | int hound_disconnect(hound_t *hound, const char* source_name, const char* sink_name)
|
|---|
| 396 | {
|
|---|
| 397 | assert(hound);
|
|---|
| 398 | fibril_mutex_lock(&hound->list_guard);
|
|---|
| 399 | const int ret = hound_disconnect_internal(hound, source_name, sink_name);
|
|---|
| 400 | fibril_mutex_unlock(&hound->list_guard);
|
|---|
| 401 | return ret;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | static int hound_disconnect_internal(hound_t *hound, const char* source_name, const char* sink_name)
|
|---|
| 405 | {
|
|---|
| 406 | assert(hound);
|
|---|
| 407 | assert(fibril_mutex_is_locked(&hound->list_guard));
|
|---|
| 408 | log_debug("Disconnecting '%s' to '%s'.", source_name, sink_name);
|
|---|
| 409 |
|
|---|
| 410 | list_foreach_safe(hound->connections, it, next) {
|
|---|
| 411 | connection_t *conn = connection_from_hound_list(it);
|
|---|
| 412 | if (str_cmp(connection_source_name(conn), source_name) == 0 ||
|
|---|
| 413 | str_cmp(connection_sink_name(conn), sink_name) == 0) {
|
|---|
| 414 | log_debug("Removing %s -> %s", connection_source_name(conn),
|
|---|
| 415 | connection_sink_name(conn));
|
|---|
| 416 | list_remove(it);
|
|---|
| 417 | connection_destroy(conn);
|
|---|
| 418 | }
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | return EOK;
|
|---|
| 422 | }
|
|---|
| 423 | /**
|
|---|
| 424 | * @}
|
|---|
| 425 | */
|
|---|