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
RevLine 
[737b4c0]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
[1df3018a]46int audio_sink_init(audio_sink_t *sink, const char *name,
[13df13c8]47 void *private_data, int (*connection_change)(audio_sink_t *, bool),
[ea6c838]48 int (*check_format)(audio_sink_t *sink), const pcm_format_t *f)
[737b4c0]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);
[1df3018a]58 sink->private_data = private_data;
59 sink->format = *f;
[f3fced0]60 sink->connection_change = connection_change;
[389ef25]61 sink->check_format = check_format;
[1df3018a]62 log_verbose("Initialized sink (%p) '%s'", sink, sink->name);
[737b4c0]63 return EOK;
64}
65
[1df3018a]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
[737b4c0]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
[ea6c838]81 const pcm_format_t old_format = sink->format;
[737b4c0]82
83 /* The first source for me */
84 if (list_count(&sink->sources) == 1) {
85 /* Set audio format according to the first source */
[ea6c838]86 if (pcm_format_is_any(&sink->format)) {
[389ef25]87 int ret = audio_sink_set_format(sink, &source->format);
88 if (ret != EOK)
89 return ret;
[737b4c0]90 }
91 }
92
[f3fced0]93 audio_source_connected(source, sink);
94
[1df3018a]95 if (sink->connection_change) {
[f3fced0]96 log_verbose("Calling connection change");
[13df13c8]97 const int ret = sink->connection_change(sink, true);
[737b4c0]98 if (ret != EOK) {
[1df3018a]99 log_debug("Connection hook failed.");
[f3fced0]100 audio_source_connected(source, NULL);
[737b4c0]101 list_remove(&source->link);
102 sink->format = old_format;
103 return ret;
104 }
105 }
[f3fced0]106 log_verbose("Connected source '%s' to sink '%s'",
107 source->name, sink->name);
[737b4c0]108
109 return EOK;
110}
111
[ea6c838]112int audio_sink_set_format(audio_sink_t *sink, const pcm_format_t *format)
[389ef25]113{
114 assert(sink);
115 assert(format);
[ea6c838]116 if (!pcm_format_is_any(&sink->format)) {
[389ef25]117 log_debug("Sink %s already has a format", sink->name);
118 return EEXISTS;
119 }
[ea6c838]120 const pcm_format_t old_format;
[389ef25]121
[ea6c838]122 if (pcm_format_is_any(format)) {
[389ef25]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
[737b4c0]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);
[1df3018a]148 if (sink->connection_change) {
[13df13c8]149 const int ret = sink->connection_change(sink, false);
[737b4c0]150 if (ret != EOK) {
151 log_debug("Connected hook failed.");
[1df3018a]152 list_append(&source->link, &sink->sources);
153 return ret;
[737b4c0]154 }
155 }
[1c33539]156 audio_source_connected(source, NULL);
[737b4c0]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
[cd8f19d]166 pcm_format_silence(dest, size, &sink->format);
[737b4c0]167 list_foreach(sink->sources, it) {
168 audio_source_t *source = audio_source_list_instance(it);
[ec49085]169 const int ret =
170 audio_source_add_self(source, dest, size, &sink->format);
[737b4c0]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.