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 "log.h"
|
---|
46 | #include "errno.h"
|
---|
47 | #include "str_error.h"
|
---|
48 |
|
---|
49 | #define FIND_BY_NAME(type) \
|
---|
50 | do { \
|
---|
51 | assert(list); \
|
---|
52 | assert(name); \
|
---|
53 | list_foreach(*list, it) { \
|
---|
54 | audio_ ## type ## _t *dev = \
|
---|
55 | audio_ ## type ## _list_instance(it); \
|
---|
56 | if (str_cmp(name, dev->name) == 0) { \
|
---|
57 | log_debug("%s with name '%s' is in the list", \
|
---|
58 | #type, name); \
|
---|
59 | return dev; \
|
---|
60 | } \
|
---|
61 | } \
|
---|
62 | return NULL; \
|
---|
63 | } while (0)
|
---|
64 |
|
---|
65 | static audio_device_t * find_device_by_name(list_t *list, const char *name)
|
---|
66 | {
|
---|
67 | FIND_BY_NAME(device);
|
---|
68 | }
|
---|
69 | static audio_source_t * find_source_by_name(list_t *list, const char *name)
|
---|
70 | {
|
---|
71 | FIND_BY_NAME(source);
|
---|
72 | }
|
---|
73 | static audio_sink_t * find_sink_by_name(list_t *list, const char *name)
|
---|
74 | {
|
---|
75 | FIND_BY_NAME(sink);
|
---|
76 | }
|
---|
77 | static int hound_disconnect_internal(hound_t *hound, const char* source_name, const char* sink_name);
|
---|
78 |
|
---|
79 | int hound_init(hound_t *hound)
|
---|
80 | {
|
---|
81 | assert(hound);
|
---|
82 | fibril_mutex_initialize(&hound->list_guard);
|
---|
83 | list_initialize(&hound->devices);
|
---|
84 | list_initialize(&hound->sources);
|
---|
85 | list_initialize(&hound->sinks);
|
---|
86 | return EOK;
|
---|
87 | }
|
---|
88 |
|
---|
89 | int hound_add_device(hound_t *hound, service_id_t id, const char *name)
|
---|
90 | {
|
---|
91 | log_verbose("Adding device \"%s\", service: %zu", name, id);
|
---|
92 |
|
---|
93 | assert(hound);
|
---|
94 | if (!name || !id) {
|
---|
95 | log_debug("Incorrect parameters.");
|
---|
96 | return EINVAL;
|
---|
97 | }
|
---|
98 |
|
---|
99 | list_foreach(hound->devices, it) {
|
---|
100 | audio_device_t *dev = audio_device_list_instance(it);
|
---|
101 | if (dev->id == id) {
|
---|
102 | log_debug("Device with id %zu is already present", id);
|
---|
103 | return EEXISTS;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | audio_device_t *dev = find_device_by_name(&hound->devices, name);
|
---|
108 | if (dev) {
|
---|
109 | log_debug("Device with name %s is already present", name);
|
---|
110 | return EEXISTS;
|
---|
111 | }
|
---|
112 |
|
---|
113 | dev = malloc(sizeof(audio_device_t));
|
---|
114 | if (!dev) {
|
---|
115 | log_debug("Failed to malloc device structure.");
|
---|
116 | return ENOMEM;
|
---|
117 | }
|
---|
118 |
|
---|
119 | const int ret = audio_device_init(dev, id, name);
|
---|
120 | if (ret != EOK) {
|
---|
121 | log_debug("Failed to initialize new audio device: %s",
|
---|
122 | str_error(ret));
|
---|
123 | free(dev);
|
---|
124 | return ret;
|
---|
125 | }
|
---|
126 |
|
---|
127 | list_append(&dev->link, &hound->devices);
|
---|
128 | log_info("Added new device: '%s'", dev->name);
|
---|
129 |
|
---|
130 | audio_source_t *source = audio_device_get_source(dev);
|
---|
131 | if (source) {
|
---|
132 | const int ret = hound_add_source(hound, source);
|
---|
133 | if (ret != EOK) {
|
---|
134 | log_debug("Failed to add device source: %s",
|
---|
135 | str_error(ret));
|
---|
136 | audio_device_fini(dev);
|
---|
137 | return ret;
|
---|
138 | }
|
---|
139 | log_verbose("Added source: '%s'.", source->name);
|
---|
140 | }
|
---|
141 |
|
---|
142 | audio_sink_t *sink = audio_device_get_sink(dev);
|
---|
143 | if (sink) {
|
---|
144 | const int ret = hound_add_sink(hound, sink);
|
---|
145 | if (ret != EOK) {
|
---|
146 | log_debug("Failed to add device sink: %s",
|
---|
147 | str_error(ret));
|
---|
148 | audio_device_fini(dev);
|
---|
149 | return ret;
|
---|
150 | }
|
---|
151 | log_verbose("Added sink: '%s'.", sink->name);
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (!source && !sink)
|
---|
155 | log_warning("Neither sink nor source on device '%s'.", name);
|
---|
156 |
|
---|
157 | return ret;
|
---|
158 | }
|
---|
159 |
|
---|
160 | int hound_add_source(hound_t *hound, audio_source_t *source)
|
---|
161 | {
|
---|
162 | assert(hound);
|
---|
163 | if (!source || !source->name || str_cmp(source->name, "default") == 0) {
|
---|
164 | log_debug("Invalid source specified.");
|
---|
165 | return EINVAL;
|
---|
166 | }
|
---|
167 | fibril_mutex_lock(&hound->list_guard);
|
---|
168 | if (find_source_by_name(&hound->sources, source->name)) {
|
---|
169 | log_debug("Source by that name already exists");
|
---|
170 | fibril_mutex_unlock(&hound->list_guard);
|
---|
171 | return EEXISTS;
|
---|
172 | }
|
---|
173 | list_foreach(hound->sinks, it) {
|
---|
174 | audio_sink_t *sink = audio_sink_list_instance(it);
|
---|
175 | if (find_source_by_name(&sink->sources, source->name)) {
|
---|
176 | log_debug("Source by that name already exists");
|
---|
177 | fibril_mutex_unlock(&hound->list_guard);
|
---|
178 | return EEXISTS;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | list_append(&source->link, &hound->sources);
|
---|
182 | fibril_mutex_unlock(&hound->list_guard);
|
---|
183 | return EOK;
|
---|
184 | }
|
---|
185 |
|
---|
186 | int hound_add_sink(hound_t *hound, audio_sink_t *sink)
|
---|
187 | {
|
---|
188 | assert(hound);
|
---|
189 | if (!sink || !sink->name || str_cmp(sink->name, "default") == 0) {
|
---|
190 | log_debug("Invalid source specified.");
|
---|
191 | return EINVAL;
|
---|
192 | }
|
---|
193 | fibril_mutex_lock(&hound->list_guard);
|
---|
194 | if (find_sink_by_name(&hound->sinks, sink->name)) {
|
---|
195 | log_debug("Sink by that name already exists");
|
---|
196 | fibril_mutex_unlock(&hound->list_guard);
|
---|
197 | return EEXISTS;
|
---|
198 | }
|
---|
199 | list_append(&sink->link, &hound->sinks);
|
---|
200 | fibril_mutex_unlock(&hound->list_guard);
|
---|
201 | return EOK;
|
---|
202 | }
|
---|
203 |
|
---|
204 | int hound_remove_source(hound_t *hound, audio_source_t *source)
|
---|
205 | {
|
---|
206 | assert(hound);
|
---|
207 | if (!source)
|
---|
208 | return EINVAL;
|
---|
209 | log_verbose("Removing source '%s'.", source->name);
|
---|
210 | fibril_mutex_lock(&hound->list_guard);
|
---|
211 | if (!list_member(&source->link, &hound->sources)) {
|
---|
212 | assert(source->connected_sink);
|
---|
213 | hound_disconnect_internal(hound, source->name,
|
---|
214 | source->connected_sink->name);
|
---|
215 | }
|
---|
216 | list_remove(&source->link);
|
---|
217 | fibril_mutex_unlock(&hound->list_guard);
|
---|
218 | return EOK;
|
---|
219 | }
|
---|
220 |
|
---|
221 | int hound_remove_sink(hound_t *hound, audio_sink_t *sink)
|
---|
222 | {
|
---|
223 | assert(hound);
|
---|
224 | if (!sink)
|
---|
225 | return EINVAL;
|
---|
226 | log_verbose("Removing sink '%s'.", sink->name);
|
---|
227 | fibril_mutex_lock(&hound->list_guard);
|
---|
228 |
|
---|
229 | if (!list_empty(&sink->sources)) {
|
---|
230 | // TODO disconnect instead
|
---|
231 | fibril_mutex_unlock(&hound->list_guard);
|
---|
232 | return EBUSY;
|
---|
233 | }
|
---|
234 | list_remove(&sink->link);
|
---|
235 | fibril_mutex_unlock(&hound->list_guard);
|
---|
236 | return EOK;
|
---|
237 | }
|
---|
238 |
|
---|
239 | int hound_connect(hound_t *hound, const char* source_name, const char* sink_name)
|
---|
240 | {
|
---|
241 | assert(hound);
|
---|
242 | log_verbose("Connecting '%s' to '%s'.", source_name, sink_name);
|
---|
243 | fibril_mutex_lock(&hound->list_guard);
|
---|
244 |
|
---|
245 | audio_source_t *source =
|
---|
246 | audio_source_list_instance(list_first(&hound->sources));
|
---|
247 | if (str_cmp(source_name, "default") != 0)
|
---|
248 | source = find_source_by_name(&hound->sources, source_name);
|
---|
249 |
|
---|
250 | audio_sink_t *sink =
|
---|
251 | audio_sink_list_instance(list_first(&hound->sinks));
|
---|
252 | if (str_cmp(sink_name, "default") != 0)
|
---|
253 | sink = find_sink_by_name(&hound->sinks, sink_name);
|
---|
254 |
|
---|
255 | if (!source || !sink) {
|
---|
256 | fibril_mutex_unlock(&hound->list_guard);
|
---|
257 | log_debug("Source (%p), or sink (%p) not found", source, sink);
|
---|
258 | return ENOENT;
|
---|
259 | }
|
---|
260 | list_remove(&source->link);
|
---|
261 | const int ret = audio_sink_add_source(sink, source);
|
---|
262 | if (ret != EOK) {
|
---|
263 | log_debug("Failed add source to sink list: %s", str_error(ret));
|
---|
264 | list_append(&source->link, &hound->sources);
|
---|
265 | }
|
---|
266 | fibril_mutex_unlock(&hound->list_guard);
|
---|
267 | return EOK;
|
---|
268 | }
|
---|
269 |
|
---|
270 | int hound_disconnect(hound_t *hound, const char* source_name, const char* sink_name)
|
---|
271 | {
|
---|
272 | assert(hound);
|
---|
273 | fibril_mutex_lock(&hound->list_guard);
|
---|
274 | const int ret = hound_disconnect_internal(hound, source_name, sink_name);
|
---|
275 | fibril_mutex_unlock(&hound->list_guard);
|
---|
276 | return ret;
|
---|
277 | }
|
---|
278 |
|
---|
279 | static int hound_disconnect_internal(hound_t *hound, const char* source_name, const char* sink_name)
|
---|
280 | {
|
---|
281 | assert(hound);
|
---|
282 | assert(fibril_mutex_is_locked(&hound->list_guard));
|
---|
283 | log_verbose("Disconnecting '%s' to '%s'.", source_name, sink_name);
|
---|
284 |
|
---|
285 | audio_sink_t *sink =
|
---|
286 | audio_sink_list_instance(list_first(&hound->sinks));
|
---|
287 | if (str_cmp(sink_name, "default") != 0)
|
---|
288 | sink = find_sink_by_name(&hound->sinks, sink_name);
|
---|
289 |
|
---|
290 | audio_source_t *source =
|
---|
291 | audio_source_list_instance(list_first(&hound->sources));
|
---|
292 | if (str_cmp(source_name, "default") != 0)
|
---|
293 | source = sink ? find_source_by_name(&sink->sources, source_name) : NULL;
|
---|
294 | if (!source || !sink) {
|
---|
295 | log_debug("Source (%p), or sink (%p) not found", source, sink);
|
---|
296 | return ENOENT;
|
---|
297 | }
|
---|
298 | const int ret = audio_sink_remove_source(sink, source);
|
---|
299 | if (ret != EOK) {
|
---|
300 | log_debug("Failed remove source to sink list: %s", str_error(ret));
|
---|
301 | } else {
|
---|
302 | list_append(&source->link, &hound->sources);
|
---|
303 | }
|
---|
304 | return EOK;
|
---|
305 | }
|
---|
306 | /**
|
---|
307 | * @}
|
---|
308 | */
|
---|