| 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 | /** @addtogroup audio
|
|---|
| 30 | * @brief HelenOS sound server
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 40 | #include <str_error.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "audio_sink.h"
|
|---|
| 43 | #include "connection.h"
|
|---|
| 44 | #include "log.h"
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | int audio_sink_init(audio_sink_t *sink, const char *name,
|
|---|
| 48 | void *private_data, int (*connection_change)(audio_sink_t *, bool),
|
|---|
| 49 | int (*check_format)(audio_sink_t *sink), const pcm_format_t *f)
|
|---|
| 50 | {
|
|---|
| 51 | assert(sink);
|
|---|
| 52 | if (!name) {
|
|---|
| 53 | log_debug("Incorrect parameters.");
|
|---|
| 54 | return EINVAL;
|
|---|
| 55 | }
|
|---|
| 56 | link_initialize(&sink->link);
|
|---|
| 57 | list_initialize(&sink->connections);
|
|---|
| 58 | sink->name = str_dup(name);
|
|---|
| 59 | sink->private_data = private_data;
|
|---|
| 60 | sink->format = *f;
|
|---|
| 61 | sink->connection_change = connection_change;
|
|---|
| 62 | sink->check_format = check_format;
|
|---|
| 63 | log_verbose("Initialized sink (%p) '%s'", sink, sink->name);
|
|---|
| 64 | return EOK;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void audio_sink_fini(audio_sink_t *sink)
|
|---|
| 68 | {
|
|---|
| 69 | assert(sink);
|
|---|
| 70 | assert(list_empty(&sink->connections));
|
|---|
| 71 | assert(!sink->private_data);
|
|---|
| 72 | free(sink->name);
|
|---|
| 73 | sink->name = NULL;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | #if 0
|
|---|
| 77 | int audio_sink_add_source(audio_sink_t *sink, audio_source_t *source)
|
|---|
| 78 | {
|
|---|
| 79 | assert(sink);
|
|---|
| 80 | assert(source);
|
|---|
| 81 | assert_link_not_used(&source->link);
|
|---|
| 82 | list_append(&source->link, &sink->sources);
|
|---|
| 83 |
|
|---|
| 84 | const pcm_format_t old_format = sink->format;
|
|---|
| 85 |
|
|---|
| 86 | /* The first source for me */
|
|---|
| 87 | if (list_count(&sink->sources) == 1) {
|
|---|
| 88 | /* Set audio format according to the first source */
|
|---|
| 89 | if (pcm_format_is_any(&sink->format)) {
|
|---|
| 90 | int ret = audio_sink_set_format(sink, &source->format);
|
|---|
| 91 | if (ret != EOK)
|
|---|
| 92 | return ret;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | audio_source_connected(source, sink);
|
|---|
| 97 | if (sink->connection_change) {
|
|---|
| 98 | log_verbose("Calling connection change");
|
|---|
| 99 | const int ret = sink->connection_change(sink, true);
|
|---|
| 100 | if (ret != EOK) {
|
|---|
| 101 | log_debug("Connection hook failed.");
|
|---|
| 102 | // audio_source_connected(source, NULL);
|
|---|
| 103 | // list_remove(&source->link);
|
|---|
| 104 | // sink->format = old_format;
|
|---|
| 105 | return ret;
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | log_verbose("Connected source '%s' to sink '%s'",
|
|---|
| 109 | source->name, sink->name);
|
|---|
| 110 |
|
|---|
| 111 | return EOK;
|
|---|
| 112 | }
|
|---|
| 113 | #endif
|
|---|
| 114 |
|
|---|
| 115 | int audio_sink_set_format(audio_sink_t *sink, const pcm_format_t *format)
|
|---|
| 116 | {
|
|---|
| 117 | assert(sink);
|
|---|
| 118 | assert(format);
|
|---|
| 119 | if (!pcm_format_is_any(&sink->format)) {
|
|---|
| 120 | log_debug("Sink %s already has a format", sink->name);
|
|---|
| 121 | return EEXISTS;
|
|---|
| 122 | }
|
|---|
| 123 | const pcm_format_t old_format = sink->format;
|
|---|
| 124 |
|
|---|
| 125 | if (pcm_format_is_any(format)) {
|
|---|
| 126 | log_verbose("Setting DEFAULT format for sink %s", sink->name);
|
|---|
| 127 | sink->format = AUDIO_FORMAT_DEFAULT;
|
|---|
| 128 | } else {
|
|---|
| 129 | sink->format = *format;
|
|---|
| 130 | }
|
|---|
| 131 | if (sink->check_format) {
|
|---|
| 132 | const int ret = sink->check_format(sink);
|
|---|
| 133 | if (ret != EOK && ret != ELIMIT) {
|
|---|
| 134 | log_debug("Format check failed on sink %s", sink->name);
|
|---|
| 135 | sink->format = old_format;
|
|---|
| 136 | return ret;
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | log_verbose("Set format for sink %s: %u channel(s), %uHz, %s",
|
|---|
| 140 | sink->name, format->channels, format->sampling_rate,
|
|---|
| 141 | pcm_sample_format_str(format->sample_format));
|
|---|
| 142 | return EOK;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | #if 0
|
|---|
| 146 | int audio_sink_remove_source(audio_sink_t *sink, audio_source_t *source)
|
|---|
| 147 | {
|
|---|
| 148 | assert(sink);
|
|---|
| 149 | assert(source);
|
|---|
| 150 | assert(list_member(&source->link, &sink->sources));
|
|---|
| 151 | list_remove(&source->link);
|
|---|
| 152 | if (sink->connection_change) {
|
|---|
| 153 | const int ret = sink->connection_change(sink, false);
|
|---|
| 154 | if (ret != EOK) {
|
|---|
| 155 | log_debug("Connected hook failed.");
|
|---|
| 156 | list_append(&source->link, &sink->sources);
|
|---|
| 157 | return ret;
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | audio_source_connected(source, NULL);
|
|---|
| 161 | return EOK;
|
|---|
| 162 | }
|
|---|
| 163 | #endif
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 | void audio_sink_mix_inputs(audio_sink_t *sink, void* dest, size_t size)
|
|---|
| 167 | {
|
|---|
| 168 | assert(sink);
|
|---|
| 169 | assert(dest);
|
|---|
| 170 |
|
|---|
| 171 | pcm_format_silence(dest, size, &sink->format);
|
|---|
| 172 | list_foreach(sink->connections, it) {
|
|---|
| 173 | connection_t * conn = connection_from_sink_list(it);
|
|---|
| 174 | const int ret = connection_add_source_data(
|
|---|
| 175 | conn, dest, size, sink->format);
|
|---|
| 176 | if (ret != EOK) {
|
|---|
| 177 | log_warning("Failed to add source %s: %s",
|
|---|
| 178 | connection_source_name(conn), str_error(ret));
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 | /**
|
|---|
| 185 | * @}
|
|---|
| 186 | */
|
|---|