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 already present", \
|
---|
58 | #type, name); \
|
---|
59 | return NULL; \
|
---|
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 |
|
---|
78 | int hound_init(hound_t *hound)
|
---|
79 | {
|
---|
80 | assert(hound);
|
---|
81 | fibril_mutex_initialize(&hound->list_guard);
|
---|
82 | list_initialize(&hound->devices);
|
---|
83 | list_initialize(&hound->sources);
|
---|
84 | list_initialize(&hound->available_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) {
|
---|
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) {
|
---|
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 | /**
|
---|
205 | * @}
|
---|
206 | */
|
---|