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

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

hound: Only few more TODOs left

  • Property mode set to 100644
File size: 4.2 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 *sink),
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 log_verbose("Initialized sink (%p) '%s'", sink, sink->name);
61 return EOK;
62}
63
64void audio_sink_fini(audio_sink_t *sink)
65{
66 assert(sink);
67 assert(!sink->private_data);
68 free(sink->name);
69 sink->name = NULL;
70}
71
72
73int audio_sink_add_source(audio_sink_t *sink, audio_source_t *source)
74{
75 assert(sink);
76 assert(source);
77 assert_link_not_used(&source->link);
78 list_append(&source->link, &sink->sources);
79
80 const audio_format_t old_format = sink->format;
81
82 /* The first source for me */
83 if (list_count(&sink->sources) == 1) {
84 /* Set audio format according to the first source */
85 if (audio_format_is_any(&sink->format)) {
86 /* Source does not care */
87 if (audio_format_is_any(&source->format)) {
88 log_verbose("Set default format for sink %s.",
89 sink->name);
90 sink->format = AUDIO_FORMAT_DEFAULT;
91 } else {
92 log_verbose("Set format base on the first "
93 "source(%s): %u channels, %uHz, %s for "
94 " sink %s.", source->name,
95 source->format.channels,
96 source->format.sampling_rate,
97 pcm_sample_format_str(
98 source->format.sample_format),
99 sink->name);
100 sink->format = source->format;
101 }
102 }
103 }
104
105 if (sink->connection_change) {
106 const int ret = sink->connection_change(sink);
107 if (ret != EOK) {
108 log_debug("Connection hook failed.");
109 list_remove(&source->link);
110 sink->format = old_format;
111 return ret;
112 }
113 }
114
115 return EOK;
116}
117
118int audio_sink_remove_source(audio_sink_t *sink, audio_source_t *source)
119{
120 assert(sink);
121 assert(source);
122 assert(list_member(&source->link, &sink->sources));
123 list_remove(&source->link);
124 if (sink->connection_change) {
125 const int ret = sink->connection_change(sink);
126 if (ret != EOK) {
127 log_debug("Connected hook failed.");
128 list_append(&source->link, &sink->sources);
129 return ret;
130 }
131 }
132 return EOK;
133}
134
135
136void audio_sink_mix_inputs(audio_sink_t *sink, void* dest, size_t size)
137{
138 assert(sink);
139 assert(dest);
140
141 bzero(dest, size);
142 list_foreach(sink->sources, it) {
143 audio_source_t *source = audio_source_list_instance(it);
144 const int ret =
145 audio_source_add_self(source, dest, size, &sink->format);
146 if (ret != EOK) {
147 log_warning("Failed to mix source %s: %s",
148 source->name, str_error(ret));
149 }
150 }
151}
152
153
154/**
155 * @}
156 */
Note: See TracBrowser for help on using the repository browser.