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