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 <fibril_synch.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <str_error.h>
|
---|
42 |
|
---|
43 | #include "audio_sink.h"
|
---|
44 | #include "connection.h"
|
---|
45 | #include "log.h"
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Initialize audio sink structure.
|
---|
49 | * @param sink The structure to initialize.
|
---|
50 | * @param name string identifier
|
---|
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.
|
---|
57 | */
|
---|
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 *),
|
---|
61 | const pcm_format_t *f)
|
---|
62 | {
|
---|
63 | assert(sink);
|
---|
64 | if (!name)
|
---|
65 | return EINVAL;
|
---|
66 | link_initialize(&sink->link);
|
---|
67 | fibril_mutex_initialize(&sink->lock);
|
---|
68 | list_initialize(&sink->connections);
|
---|
69 | sink->name = str_dup(name);
|
---|
70 | if (!sink->name)
|
---|
71 | return ENOMEM;
|
---|
72 | sink->private_data = private_data;
|
---|
73 | sink->format = *f;
|
---|
74 | sink->connection_change = connection_change;
|
---|
75 | sink->check_format = check_format;
|
---|
76 | sink->data_available = data_available;
|
---|
77 | log_verbose("Initialized sink (%p) '%s'", sink, sink->name);
|
---|
78 | return EOK;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Release resources claimed by initialization.
|
---|
83 | * @param sink the structure to release.
|
---|
84 | */
|
---|
85 | void audio_sink_fini(audio_sink_t *sink)
|
---|
86 | {
|
---|
87 | assert(sink);
|
---|
88 | assert(list_empty(&sink->connections));
|
---|
89 | assert(!sink->private_data);
|
---|
90 | free(sink->name);
|
---|
91 | sink->name = NULL;
|
---|
92 | }
|
---|
93 |
|
---|
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 | */
|
---|
100 | errno_t audio_sink_set_format(audio_sink_t *sink, const pcm_format_t *format)
|
---|
101 | {
|
---|
102 | assert(sink);
|
---|
103 | assert(format);
|
---|
104 | if (!pcm_format_is_any(&sink->format)) {
|
---|
105 | log_debug("Sink %s already has a format", sink->name);
|
---|
106 | return EEXIST;
|
---|
107 | }
|
---|
108 | const pcm_format_t old_format = sink->format;
|
---|
109 |
|
---|
110 | if (pcm_format_is_any(format)) {
|
---|
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) {
|
---|
117 | const errno_t ret = sink->check_format(sink);
|
---|
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 |
|
---|
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 | */
|
---|
137 | void audio_sink_mix_inputs(audio_sink_t *sink, void *dest, size_t size)
|
---|
138 | {
|
---|
139 | assert(sink);
|
---|
140 | assert(dest);
|
---|
141 |
|
---|
142 | pcm_format_silence(dest, size, &sink->format);
|
---|
143 | fibril_mutex_lock(&sink->lock);
|
---|
144 | list_foreach(sink->connections, sink_link, connection_t, conn) {
|
---|
145 | const errno_t ret = connection_add_source_data(
|
---|
146 | conn, dest, size, sink->format);
|
---|
147 | if (ret != EOK) {
|
---|
148 | log_warning("Failed to mix source %s: %s",
|
---|
149 | connection_source_name(conn), str_error(ret));
|
---|
150 | }
|
---|
151 | }
|
---|
152 | fibril_mutex_unlock(&sink->lock);
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * @}
|
---|
157 | */
|
---|