source: mainline/uspace/srv/audio/hound/hound_ctx.c@ e1d2f0e

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

hound: Use synchronization primitives instead of sleep waiting

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/*
2 * Copyright (c) 2013 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/**
30 * @addtogroup audio
31 * @brief HelenOS sound server.
32 * @{
33 */
34/** @file
35 */
36
37#include <malloc.h>
38#include <macros.h>
39
40#include "hound_ctx.h"
41#include "audio_data.h"
42#include "connection.h"
43#include "log.h"
44
45static int update_data(audio_source_t *source, size_t size);
46
47hound_ctx_t *hound_record_ctx_get(const char *name)
48{
49 return NULL;
50}
51
52hound_ctx_t *hound_playback_ctx_get(const char *name)
53{
54 hound_ctx_t *ctx = malloc(sizeof(hound_ctx_t));
55 if (ctx) {
56 link_initialize(&ctx->link);
57 list_initialize(&ctx->streams);
58 fibril_mutex_initialize(&ctx->guard);
59 ctx->sink = NULL;
60 ctx->source = malloc(sizeof(audio_source_t));
61 if (!ctx->source) {
62 free(ctx);
63 return NULL;
64 }
65 const int ret = audio_source_init(ctx->source, name, ctx, NULL,
66 update_data, &AUDIO_FORMAT_DEFAULT);
67 if (ret != EOK) {
68 free(ctx->source);
69 free(ctx);
70 return NULL;
71 }
72 }
73 return ctx;
74}
75
76void hound_ctx_destroy(hound_ctx_t *ctx)
77{
78 assert(ctx);
79 assert(!link_in_use(&ctx->link));
80 assert(list_empty(&ctx->streams));
81 if (ctx->source)
82 audio_source_fini(ctx->source);
83 if (ctx->sink)
84 audio_sink_fini(ctx->sink);
85 free(ctx->source);
86 free(ctx->sink);
87 free(ctx);
88}
89
90hound_context_id_t hound_ctx_get_id(hound_ctx_t *ctx)
91{
92 assert(ctx);
93 return (hound_context_id_t)ctx;
94}
95
96bool hound_ctx_is_record(hound_ctx_t *ctx)
97{
98 assert(ctx);
99 return ctx->source == NULL;
100}
101
102
103/*
104 * STREAMS
105 */
106typedef struct hound_ctx_stream {
107 link_t link;
108 audio_pipe_t fifo;
109 hound_ctx_t *ctx;
110 pcm_format_t format;
111 int flags;
112 size_t allowed_size;
113 fibril_mutex_t guard;
114 fibril_condvar_t change;
115} hound_ctx_stream_t;
116
117static inline hound_ctx_stream_t *hound_ctx_stream_from_link(link_t *l)
118{
119 return l ? list_get_instance(l, hound_ctx_stream_t, link) : NULL;
120}
121
122static inline void stream_append(hound_ctx_t *ctx, hound_ctx_stream_t *stream)
123{
124 assert(ctx);
125 assert(stream);
126 fibril_mutex_lock(&ctx->guard);
127 list_append(&stream->link, &ctx->streams);
128 fibril_mutex_unlock(&ctx->guard);
129}
130
131static inline void stream_remove(hound_ctx_t *ctx, hound_ctx_stream_t *stream)
132{
133 assert(ctx);
134 assert(stream);
135 fibril_mutex_lock(&ctx->guard);
136 list_remove(&stream->link);
137 fibril_mutex_unlock(&ctx->guard);
138}
139
140hound_ctx_stream_t *hound_ctx_create_stream(hound_ctx_t *ctx, int flags,
141 pcm_format_t format, size_t buffer_size)
142{
143 assert(ctx);
144 hound_ctx_stream_t *stream = malloc(sizeof(hound_ctx_stream_t));
145 if (stream) {
146 audio_pipe_init(&stream->fifo);
147 link_initialize(&stream->link);
148 fibril_mutex_initialize(&stream->guard);
149 fibril_condvar_initialize(&stream->change);
150 stream->ctx = ctx;
151 stream->flags = flags;
152 stream->format = format;
153 stream->allowed_size = buffer_size;
154 stream_append(ctx, stream);
155 log_verbose("CTX: %p added stream; flags:%#x ch: %u r:%u f:%s",
156 ctx, flags, format.channels, format.sampling_rate,
157 pcm_sample_format_str(format.sample_format));
158 }
159 return stream;
160}
161
162void hound_ctx_destroy_stream(hound_ctx_stream_t *stream)
163{
164 if (stream) {
165 stream_remove(stream->ctx, stream);
166 if (audio_pipe_bytes(&stream->fifo))
167 log_warning("Destroying stream with non empty buffer");
168 log_verbose("CTX: %p remove stream (%zu/%zu); "
169 "flags:%#x ch: %u r:%u f:%s",
170 stream->ctx, audio_pipe_bytes(&stream->fifo),
171 stream->allowed_size, stream->flags,
172 stream->format.channels, stream->format.sampling_rate,
173 pcm_sample_format_str(stream->format.sample_format));
174 audio_pipe_fini(&stream->fifo);
175 free(stream);
176 }
177}
178
179
180int hound_ctx_stream_write(hound_ctx_stream_t *stream, const void *data,
181 size_t size)
182{
183 assert(stream);
184
185 if (stream->allowed_size && size > stream->allowed_size)
186 return EINVAL;
187
188 fibril_mutex_lock(&stream->guard);
189 while (stream->allowed_size &&
190 (audio_pipe_bytes(&stream->fifo) + size > stream->allowed_size)) {
191 fibril_condvar_wait(&stream->change, &stream->guard);
192
193 }
194
195 const int ret =
196 audio_pipe_push_data(&stream->fifo, data, size, stream->format);
197 fibril_mutex_unlock(&stream->guard);
198 return ret;
199}
200
201int hound_ctx_stream_read(hound_ctx_stream_t *stream, void *data, size_t size)
202{
203 log_verbose("%p:, %zu", stream, size);
204 return ENOTSUP;
205}
206
207ssize_t hound_ctx_stream_add_self(hound_ctx_stream_t *stream, void *data,
208 size_t size, const pcm_format_t *f)
209{
210 assert(stream);
211 fibril_mutex_lock(&stream->guard);
212 const int ret = audio_pipe_mix_data(&stream->fifo, data, size, f);
213 fibril_condvar_signal(&stream->change);
214 fibril_mutex_unlock(&stream->guard);
215 return ret;
216}
217
218void hound_ctx_stream_drain(hound_ctx_stream_t *stream)
219{
220 assert(stream);
221 log_debug("Draining stream");
222 fibril_mutex_lock(&stream->guard);
223 while (audio_pipe_bytes(&stream->fifo))
224 fibril_condvar_wait(&stream->change, &stream->guard);
225 fibril_mutex_unlock(&stream->guard);
226}
227
228int update_data(audio_source_t *source, size_t size)
229{
230 assert(source);
231 assert(source->private_data);
232 hound_ctx_t *ctx = source->private_data;
233 void *buffer = malloc(size);
234 if (!buffer)
235 return ENOMEM;
236 audio_data_t *adata = audio_data_create(buffer, size, source->format);
237 if (!adata) {
238 free(buffer);
239 return ENOMEM;
240 }
241 log_verbose("CTX: %p. Mixing %zu streams", ctx,
242 list_count(&ctx->streams));
243 pcm_format_silence(buffer, size, &source->format);
244 fibril_mutex_lock(&ctx->guard);
245 list_foreach(ctx->streams, it) {
246 hound_ctx_stream_t *stream = hound_ctx_stream_from_link(it);
247 ssize_t copied = hound_ctx_stream_add_self(
248 stream, buffer, size, &source->format);
249 if (copied != (ssize_t)size)
250 log_warning("Not enough data in stream buffer");
251 }
252 log_verbose("CTX: %p. Pushing audio to %zu connections", ctx,
253 list_count(&source->connections));
254 list_foreach(source->connections, it) {
255 connection_t *conn = connection_from_source_list(it);
256 connection_push_data(conn, adata);
257 }
258 fibril_mutex_unlock(&ctx->guard);
259 return EOK;
260}
261
262/**
263 * @}
264 */
Note: See TracBrowser for help on using the repository browser.