[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 | /** @addtogroup audio
|
---|
| 30 | * @brief HelenOS sound server
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
| 37 | #include <errno.h>
|
---|
[2fbd49c] | 38 | #include <fibril_synch.h>
|
---|
[737b4c0] | 39 | #include <stdlib.h>
|
---|
| 40 | #include <str.h>
|
---|
| 41 | #include <str_error.h>
|
---|
| 42 |
|
---|
| 43 | #include "audio_sink.h"
|
---|
[fa60cd69] | 44 | #include "connection.h"
|
---|
[737b4c0] | 45 | #include "log.h"
|
---|
| 46 |
|
---|
[c799138] | 47 | /**
|
---|
| 48 | * Initialize audio sink structure.
|
---|
| 49 | * @param sink The structure to initialize.
|
---|
| 50 | * @param name string identifier
|
---|
[bee5349] | 51 | * @param private_data backend data
|
---|
| 52 | * @param connection_change connect/disconnect callback
|
---|
| 53 | * @param check_format format testing callback
|
---|
| 54 | * @param data_available trigger data prcoessing
|
---|
| 55 | * @param f sink format
|
---|
| 56 | * @return Error code.
|
---|
[c799138] | 57 | */
|
---|
[b7fd2a0] | 58 | errno_t audio_sink_init(audio_sink_t *sink, const char *name, void *private_data,
|
---|
| 59 | errno_t (*connection_change)(audio_sink_t *, bool),
|
---|
| 60 | errno_t (*check_format)(audio_sink_t *), errno_t (*data_available)(audio_sink_t *),
|
---|
[bee5349] | 61 | const pcm_format_t *f)
|
---|
[737b4c0] | 62 | {
|
---|
| 63 | assert(sink);
|
---|
[c799138] | 64 | if (!name)
|
---|
[737b4c0] | 65 | return EINVAL;
|
---|
| 66 | link_initialize(&sink->link);
|
---|
[2fbd49c] | 67 | fibril_mutex_initialize(&sink->lock);
|
---|
[fa60cd69] | 68 | list_initialize(&sink->connections);
|
---|
[737b4c0] | 69 | sink->name = str_dup(name);
|
---|
[c799138] | 70 | if (!sink->name)
|
---|
| 71 | return ENOMEM;
|
---|
[1df3018a] | 72 | sink->private_data = private_data;
|
---|
| 73 | sink->format = *f;
|
---|
[f3fced0] | 74 | sink->connection_change = connection_change;
|
---|
[389ef25] | 75 | sink->check_format = check_format;
|
---|
[bee5349] | 76 | sink->data_available = data_available;
|
---|
[1df3018a] | 77 | log_verbose("Initialized sink (%p) '%s'", sink, sink->name);
|
---|
[737b4c0] | 78 | return EOK;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[c799138] | 81 | /**
|
---|
| 82 | * Release resources claimed by initialization.
|
---|
| 83 | * @param sink the structure to release.
|
---|
| 84 | */
|
---|
[1df3018a] | 85 | void audio_sink_fini(audio_sink_t *sink)
|
---|
| 86 | {
|
---|
| 87 | assert(sink);
|
---|
[fa60cd69] | 88 | assert(list_empty(&sink->connections));
|
---|
[1df3018a] | 89 | assert(!sink->private_data);
|
---|
| 90 | free(sink->name);
|
---|
| 91 | sink->name = NULL;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[c799138] | 94 | /**
|
---|
| 95 | * Set audio sink format and check with backend,
|
---|
| 96 | * @param sink The target sink isntance.
|
---|
| 97 | * @param format Th new format.
|
---|
| 98 | * @return Error code.
|
---|
| 99 | */
|
---|
[b7fd2a0] | 100 | errno_t audio_sink_set_format(audio_sink_t *sink, const pcm_format_t *format)
|
---|
[389ef25] | 101 | {
|
---|
| 102 | assert(sink);
|
---|
| 103 | assert(format);
|
---|
[ea6c838] | 104 | if (!pcm_format_is_any(&sink->format)) {
|
---|
[389ef25] | 105 | log_debug("Sink %s already has a format", sink->name);
|
---|
[8a637a4] | 106 | return EEXIST;
|
---|
[389ef25] | 107 | }
|
---|
[fa60cd69] | 108 | const pcm_format_t old_format = sink->format;
|
---|
[389ef25] | 109 |
|
---|
[ea6c838] | 110 | if (pcm_format_is_any(format)) {
|
---|
[389ef25] | 111 | log_verbose("Setting DEFAULT format for sink %s", sink->name);
|
---|
| 112 | sink->format = AUDIO_FORMAT_DEFAULT;
|
---|
| 113 | } else {
|
---|
| 114 | sink->format = *format;
|
---|
| 115 | }
|
---|
| 116 | if (sink->check_format) {
|
---|
[b7fd2a0] | 117 | const errno_t ret = sink->check_format(sink);
|
---|
[389ef25] | 118 | if (ret != EOK && ret != ELIMIT) {
|
---|
| 119 | log_debug("Format check failed on sink %s", sink->name);
|
---|
| 120 | sink->format = old_format;
|
---|
| 121 | return ret;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | log_verbose("Set format for sink %s: %u channel(s), %uHz, %s",
|
---|
| 125 | sink->name, format->channels, format->sampling_rate,
|
---|
| 126 | pcm_sample_format_str(format->sample_format));
|
---|
| 127 | return EOK;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[c799138] | 130 | /**
|
---|
| 131 | * Pull data from all connections and add the mix to the destination.
|
---|
| 132 | * @param sink The sink to mix data.
|
---|
| 133 | * @param dest Destination buffer.
|
---|
| 134 | * @param size size of the @p dest buffer.
|
---|
| 135 | * @return Error code.
|
---|
| 136 | */
|
---|
[1433ecda] | 137 | void audio_sink_mix_inputs(audio_sink_t *sink, void *dest, size_t size)
|
---|
[737b4c0] | 138 | {
|
---|
| 139 | assert(sink);
|
---|
| 140 | assert(dest);
|
---|
| 141 |
|
---|
[cd8f19d] | 142 | pcm_format_silence(dest, size, &sink->format);
|
---|
[2fbd49c] | 143 | fibril_mutex_lock(&sink->lock);
|
---|
[feeac0d] | 144 | list_foreach(sink->connections, sink_link, connection_t, conn) {
|
---|
[b7fd2a0] | 145 | const errno_t ret = connection_add_source_data(
|
---|
[fa60cd69] | 146 | conn, dest, size, sink->format);
|
---|
[737b4c0] | 147 | if (ret != EOK) {
|
---|
[be7eccf] | 148 | log_warning("Failed to mix source %s: %s",
|
---|
[fa60cd69] | 149 | connection_source_name(conn), str_error(ret));
|
---|
[737b4c0] | 150 | }
|
---|
| 151 | }
|
---|
[2fbd49c] | 152 | fibril_mutex_unlock(&sink->lock);
|
---|
[737b4c0] | 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * @}
|
---|
| 157 | */
|
---|