source: mainline/uspace/srv/audio/hound/audio_sink.c@ cd8f19d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cd8f19d was cd8f19d, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

hound: Silence buffer instead of zeroing it.

  • Property mode set to 100644
File size: 5.0 KB
Line 
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
46int audio_sink_init(audio_sink_t *sink, const char *name,
47 void *private_data, int (*connection_change)(audio_sink_t *, bool),
48 int (*check_format)(audio_sink_t *sink), const pcm_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 sink->check_format = check_format;
62 log_verbose("Initialized sink (%p) '%s'", sink, sink->name);
63 return EOK;
64}
65
66void audio_sink_fini(audio_sink_t *sink)
67{
68 assert(sink);
69 assert(!sink->private_data);
70 free(sink->name);
71 sink->name = NULL;
72}
73
74int 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 pcm_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 (pcm_format_is_any(&sink->format)) {
87 int ret = audio_sink_set_format(sink, &source->format);
88 if (ret != EOK)
89 return ret;
90 }
91 }
92
93 audio_source_connected(source, sink);
94
95 if (sink->connection_change) {
96 log_verbose("Calling connection change");
97 const int ret = sink->connection_change(sink, true);
98 if (ret != EOK) {
99 log_debug("Connection hook failed.");
100 audio_source_connected(source, NULL);
101 list_remove(&source->link);
102 sink->format = old_format;
103 return ret;
104 }
105 }
106 log_verbose("Connected source '%s' to sink '%s'",
107 source->name, sink->name);
108
109 return EOK;
110}
111
112int audio_sink_set_format(audio_sink_t *sink, const pcm_format_t *format)
113{
114 assert(sink);
115 assert(format);
116 if (!pcm_format_is_any(&sink->format)) {
117 log_debug("Sink %s already has a format", sink->name);
118 return EEXISTS;
119 }
120 const pcm_format_t old_format;
121
122 if (pcm_format_is_any(format)) {
123 log_verbose("Setting DEFAULT format for sink %s", sink->name);
124 sink->format = AUDIO_FORMAT_DEFAULT;
125 } else {
126 sink->format = *format;
127 }
128 if (sink->check_format) {
129 const int ret = sink->check_format(sink);
130 if (ret != EOK && ret != ELIMIT) {
131 log_debug("Format check failed on sink %s", sink->name);
132 sink->format = old_format;
133 return ret;
134 }
135 }
136 log_verbose("Set format for sink %s: %u channel(s), %uHz, %s",
137 sink->name, format->channels, format->sampling_rate,
138 pcm_sample_format_str(format->sample_format));
139 return EOK;
140}
141
142int audio_sink_remove_source(audio_sink_t *sink, audio_source_t *source)
143{
144 assert(sink);
145 assert(source);
146 assert(list_member(&source->link, &sink->sources));
147 list_remove(&source->link);
148 if (sink->connection_change) {
149 const int ret = sink->connection_change(sink, false);
150 if (ret != EOK) {
151 log_debug("Connected hook failed.");
152 list_append(&source->link, &sink->sources);
153 return ret;
154 }
155 }
156 audio_source_connected(source, NULL);
157 return EOK;
158}
159
160
161void audio_sink_mix_inputs(audio_sink_t *sink, void* dest, size_t size)
162{
163 assert(sink);
164 assert(dest);
165
166 pcm_format_silence(dest, size, &sink->format);
167 list_foreach(sink->sources, it) {
168 audio_source_t *source = audio_source_list_instance(it);
169 const int ret =
170 audio_source_add_self(source, dest, size, &sink->format);
171 if (ret != EOK) {
172 log_warning("Failed to mix source %s: %s",
173 source->name, str_error(ret));
174 }
175 }
176}
177
178
179/**
180 * @}
181 */
Note: See TracBrowser for help on using the repository browser.