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"
|
---|
43 | #include "log.h"
|
---|
44 |
|
---|
45 |
|
---|
46 | int audio_sink_init(audio_sink_t *sink, const char *name,
|
---|
47 | void *private_data, int (*connection_change)(audio_sink_t *, bool),
|
---|
48 | const audio_format_t *f)
|
---|
49 | {
|
---|
50 | assert(sink);
|
---|
51 | if (!name) {
|
---|
52 | log_debug("Incorrect parameters.");
|
---|
53 | return EINVAL;
|
---|
54 | }
|
---|
55 | link_initialize(&sink->link);
|
---|
56 | list_initialize(&sink->sources);
|
---|
57 | sink->name = str_dup(name);
|
---|
58 | sink->private_data = private_data;
|
---|
59 | sink->format = *f;
|
---|
60 | sink->connection_change = connection_change;
|
---|
61 | log_verbose("Initialized sink (%p) '%s'", sink, sink->name);
|
---|
62 | return EOK;
|
---|
63 | }
|
---|
64 |
|
---|
65 | void audio_sink_fini(audio_sink_t *sink)
|
---|
66 | {
|
---|
67 | assert(sink);
|
---|
68 | assert(!sink->private_data);
|
---|
69 | free(sink->name);
|
---|
70 | sink->name = NULL;
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | int audio_sink_add_source(audio_sink_t *sink, audio_source_t *source)
|
---|
75 | {
|
---|
76 | assert(sink);
|
---|
77 | assert(source);
|
---|
78 | assert_link_not_used(&source->link);
|
---|
79 | list_append(&source->link, &sink->sources);
|
---|
80 |
|
---|
81 | const audio_format_t old_format = sink->format;
|
---|
82 |
|
---|
83 | /* The first source for me */
|
---|
84 | if (list_count(&sink->sources) == 1) {
|
---|
85 | /* Set audio format according to the first source */
|
---|
86 | if (audio_format_is_any(&sink->format)) {
|
---|
87 | /* Source does not care */
|
---|
88 | if (audio_format_is_any(&source->format)) {
|
---|
89 | log_verbose("Set default format for sink %s.",
|
---|
90 | sink->name);
|
---|
91 | sink->format = AUDIO_FORMAT_DEFAULT;
|
---|
92 | } else {
|
---|
93 | log_verbose("Set format based on the first "
|
---|
94 | "source(%s): %u channel(s), %uHz, %s for "
|
---|
95 | "sink %s.", source->name,
|
---|
96 | source->format.channels,
|
---|
97 | source->format.sampling_rate,
|
---|
98 | pcm_sample_format_str(
|
---|
99 | source->format.sample_format),
|
---|
100 | sink->name);
|
---|
101 | sink->format = source->format;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | audio_source_connected(source, sink);
|
---|
107 |
|
---|
108 | if (sink->connection_change) {
|
---|
109 | log_verbose("Calling connection change");
|
---|
110 | const int ret = sink->connection_change(sink, true);
|
---|
111 | if (ret != EOK) {
|
---|
112 | log_debug("Connection hook failed.");
|
---|
113 | audio_source_connected(source, NULL);
|
---|
114 | list_remove(&source->link);
|
---|
115 | sink->format = old_format;
|
---|
116 | return ret;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | log_verbose("Connected source '%s' to sink '%s'",
|
---|
120 | source->name, sink->name);
|
---|
121 |
|
---|
122 | return EOK;
|
---|
123 | }
|
---|
124 |
|
---|
125 | int audio_sink_remove_source(audio_sink_t *sink, audio_source_t *source)
|
---|
126 | {
|
---|
127 | assert(sink);
|
---|
128 | assert(source);
|
---|
129 | assert(list_member(&source->link, &sink->sources));
|
---|
130 | list_remove(&source->link);
|
---|
131 | if (sink->connection_change) {
|
---|
132 | const int ret = sink->connection_change(sink, false);
|
---|
133 | if (ret != EOK) {
|
---|
134 | log_debug("Connected hook failed.");
|
---|
135 | list_append(&source->link, &sink->sources);
|
---|
136 | return ret;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | audio_source_connected(source, NULL);
|
---|
140 | return EOK;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | void audio_sink_mix_inputs(audio_sink_t *sink, void* dest, size_t size)
|
---|
145 | {
|
---|
146 | assert(sink);
|
---|
147 | assert(dest);
|
---|
148 |
|
---|
149 | bzero(dest, size);
|
---|
150 | list_foreach(sink->sources, it) {
|
---|
151 | audio_source_t *source = audio_source_list_instance(it);
|
---|
152 | const int ret =
|
---|
153 | audio_source_add_self(source, dest, size, &sink->format);
|
---|
154 | if (ret != EOK) {
|
---|
155 | log_warning("Failed to mix source %s: %s",
|
---|
156 | source->name, str_error(ret));
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * @}
|
---|
164 | */
|
---|