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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6ba36a0 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 4.6 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 "connection.h"
44#include "log.h"
45
46/**
47 * Initialize audio sink structure.
48 * @param sink The structure to initialize.
49 * @param name string identifier
50 * @param private_data backend data
51 * @param connection_change connect/disconnect callback
52 * @param check_format format testing callback
53 * @param data_available trigger data prcoessing
54 * @param f sink format
55 * @return Error code.
56 */
57errno_t audio_sink_init(audio_sink_t *sink, const char *name, void *private_data,
58 errno_t (*connection_change)(audio_sink_t *, bool),
59 errno_t (*check_format)(audio_sink_t *), errno_t (*data_available)(audio_sink_t *),
60 const pcm_format_t *f)
61{
62 assert(sink);
63 if (!name)
64 return EINVAL;
65 link_initialize(&sink->link);
66 list_initialize(&sink->connections);
67 sink->name = str_dup(name);
68 if (!sink->name)
69 return ENOMEM;
70 sink->private_data = private_data;
71 sink->format = *f;
72 sink->connection_change = connection_change;
73 sink->check_format = check_format;
74 sink->data_available = data_available;
75 log_verbose("Initialized sink (%p) '%s'", sink, sink->name);
76 return EOK;
77}
78
79/**
80 * Release resources claimed by initialization.
81 * @param sink the structure to release.
82 */
83void audio_sink_fini(audio_sink_t *sink)
84{
85 assert(sink);
86 assert(list_empty(&sink->connections));
87 assert(!sink->private_data);
88 free(sink->name);
89 sink->name = NULL;
90}
91
92/**
93 * Set audio sink format and check with backend,
94 * @param sink The target sink isntance.
95 * @param format Th new format.
96 * @return Error code.
97 */
98errno_t audio_sink_set_format(audio_sink_t *sink, const pcm_format_t *format)
99{
100 assert(sink);
101 assert(format);
102 if (!pcm_format_is_any(&sink->format)) {
103 log_debug("Sink %s already has a format", sink->name);
104 return EEXIST;
105 }
106 const pcm_format_t old_format = sink->format;
107
108 if (pcm_format_is_any(format)) {
109 log_verbose("Setting DEFAULT format for sink %s", sink->name);
110 sink->format = AUDIO_FORMAT_DEFAULT;
111 } else {
112 sink->format = *format;
113 }
114 if (sink->check_format) {
115 const errno_t ret = sink->check_format(sink);
116 if (ret != EOK && ret != ELIMIT) {
117 log_debug("Format check failed on sink %s", sink->name);
118 sink->format = old_format;
119 return ret;
120 }
121 }
122 log_verbose("Set format for sink %s: %u channel(s), %uHz, %s",
123 sink->name, format->channels, format->sampling_rate,
124 pcm_sample_format_str(format->sample_format));
125 return EOK;
126}
127
128/**
129 * Pull data from all connections and add the mix to the destination.
130 * @param sink The sink to mix data.
131 * @param dest Destination buffer.
132 * @param size size of the @p dest buffer.
133 * @return Error code.
134 */
135void audio_sink_mix_inputs(audio_sink_t *sink, void *dest, size_t size)
136{
137 assert(sink);
138 assert(dest);
139
140 pcm_format_silence(dest, size, &sink->format);
141 list_foreach(sink->connections, sink_link, connection_t, conn) {
142 const errno_t ret = connection_add_source_data(
143 conn, dest, size, sink->format);
144 if (ret != EOK) {
145 log_warning("Failed to mix source %s: %s",
146 connection_source_name(conn), str_error(ret));
147 }
148 }
149}
150
151/**
152 * @}
153 */
Note: See TracBrowser for help on using the repository browser.