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

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

hound: let the first stream change format if we are not yet connected

format is set in connection creation routine, remove TODO

  • Property mode set to 100644
File size: 10.4 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
47/**
48 * Allocate and initialize hound context structure.
49 * @param name String identifier.
50 * @return Pointer to a new context structure, NULL on failure
51 *
52 * Creates record context structure.
53 */
54hound_ctx_t *hound_record_ctx_get(const char *name)
55{
56 hound_ctx_t *ctx = malloc(sizeof(hound_ctx_t));
57 if (ctx) {
58 link_initialize(&ctx->link);
59 list_initialize(&ctx->streams);
60 fibril_mutex_initialize(&ctx->guard);
61 ctx->source = NULL;
62 ctx->sink = malloc(sizeof(audio_sink_t));
63 if (!ctx->sink) {
64 free(ctx);
65 return NULL;
66 }
67 // TODO provide sink functions
68 const int ret = audio_sink_init(ctx->sink, name, ctx, NULL,
69 NULL, &AUDIO_FORMAT_DEFAULT);
70 if (ret != EOK) {
71 free(ctx->sink);
72 free(ctx);
73 return NULL;
74 }
75 }
76 return ctx;
77}
78
79/**
80 * Allocate and initialize hound context structure.
81 * @param name String identifier.
82 * @return Pointer to a new context structure, NULL on failure
83 *
84 * Creates record context structure.
85 */
86hound_ctx_t *hound_playback_ctx_get(const char *name)
87{
88 hound_ctx_t *ctx = malloc(sizeof(hound_ctx_t));
89 if (ctx) {
90 link_initialize(&ctx->link);
91 list_initialize(&ctx->streams);
92 fibril_mutex_initialize(&ctx->guard);
93 ctx->sink = NULL;
94 ctx->source = malloc(sizeof(audio_source_t));
95 if (!ctx->source) {
96 free(ctx);
97 return NULL;
98 }
99 const int ret = audio_source_init(ctx->source, name, ctx, NULL,
100 update_data, &AUDIO_FORMAT_DEFAULT);
101 if (ret != EOK) {
102 free(ctx->source);
103 free(ctx);
104 return NULL;
105 }
106 }
107 return ctx;
108}
109
110/**
111 * Destroy existing context structure.
112 * @param ctx hound cotnext to destroy.
113 */
114void hound_ctx_destroy(hound_ctx_t *ctx)
115{
116 assert(ctx);
117 assert(!link_in_use(&ctx->link));
118 assert(list_empty(&ctx->streams));
119 if (ctx->source)
120 audio_source_fini(ctx->source);
121 if (ctx->sink)
122 audio_sink_fini(ctx->sink);
123 free(ctx->source);
124 free(ctx->sink);
125 free(ctx);
126}
127
128/**
129 * Retrieve associated context id.
130 * @param ctx hound context.
131 * @return context id of the context.
132 */
133hound_context_id_t hound_ctx_get_id(hound_ctx_t *ctx)
134{
135 assert(ctx);
136 return (hound_context_id_t)ctx;
137}
138
139/**
140 * Query playback/record status of a hound context.
141 * @param ctx Hound context.
142 * @return True if ctx is a recording context.
143 */
144bool hound_ctx_is_record(hound_ctx_t *ctx)
145{
146 assert(ctx);
147 return ctx->source == NULL;
148}
149
150
151/*
152 * STREAMS
153 */
154
155/** Hound stream structure. */
156typedef struct hound_ctx_stream {
157 /** Hound context streams link */
158 link_t link;
159 /** Audio data pipe */
160 audio_pipe_t fifo;
161 /** Parent context */
162 hound_ctx_t *ctx;
163 /** Stream data format */
164 pcm_format_t format;
165 /** Stream modifiers */
166 int flags;
167 /** Maximum allowed buffer size */
168 size_t allowed_size;
169 /** Fifo access synchronization */
170 fibril_mutex_t guard;
171 /** buffer status change condition */
172 fibril_condvar_t change;
173} hound_ctx_stream_t;
174
175/**
176 * List instance helper.
177 * @param l link
178 * @return pointer to a hound context structure, NULL on failure.
179 */
180static inline hound_ctx_stream_t *hound_ctx_stream_from_link(link_t *l)
181{
182 return l ? list_get_instance(l, hound_ctx_stream_t, link) : NULL;
183}
184
185/**
186 * New stream append helper.
187 * @param ctx hound context.
188 * @param stream A new stream.
189 */
190static inline void stream_append(hound_ctx_t *ctx, hound_ctx_stream_t *stream)
191{
192 assert(ctx);
193 assert(stream);
194 fibril_mutex_lock(&ctx->guard);
195 list_append(&stream->link, &ctx->streams);
196 if (list_count(&ctx->streams) == 1) {
197 if (ctx->source && list_count(&ctx->source->connections) == 0)
198 ctx->source->format = stream->format;
199 }
200 fibril_mutex_unlock(&ctx->guard);
201}
202
203/**
204 * Old stream remove helper.
205 * @param ctx hound context.
206 * @param stream An old stream.
207 */
208static inline void stream_remove(hound_ctx_t *ctx, hound_ctx_stream_t *stream)
209{
210 assert(ctx);
211 assert(stream);
212 fibril_mutex_lock(&ctx->guard);
213 list_remove(&stream->link);
214 fibril_mutex_unlock(&ctx->guard);
215}
216
217/**
218 * Create new stream.
219 * @param ctx Assocaited hound context.
220 * @param flags Stream modidfiers.
221 * @param format PCM data format.
222 * @param buffer_size Maximum allowed buffer size.
223 * @return Pointer to a new stream structure, NULL on failure.
224 */
225hound_ctx_stream_t *hound_ctx_create_stream(hound_ctx_t *ctx, int flags,
226 pcm_format_t format, size_t buffer_size)
227{
228 assert(ctx);
229 hound_ctx_stream_t *stream = malloc(sizeof(hound_ctx_stream_t));
230 if (stream) {
231 audio_pipe_init(&stream->fifo);
232 link_initialize(&stream->link);
233 fibril_mutex_initialize(&stream->guard);
234 fibril_condvar_initialize(&stream->change);
235 stream->ctx = ctx;
236 stream->flags = flags;
237 stream->format = format;
238 stream->allowed_size = buffer_size;
239 stream_append(ctx, stream);
240 log_verbose("CTX: %p added stream; flags:%#x ch: %u r:%u f:%s",
241 ctx, flags, format.channels, format.sampling_rate,
242 pcm_sample_format_str(format.sample_format));
243 }
244 return stream;
245}
246
247/**
248 * Destroy existing stream structure.
249 * @param stream The stream to destroy.
250 *
251 * The function will print warning if there are data in the buffer.
252 */
253void hound_ctx_destroy_stream(hound_ctx_stream_t *stream)
254{
255 if (stream) {
256 stream_remove(stream->ctx, stream);
257 if (audio_pipe_bytes(&stream->fifo))
258 log_warning("Destroying stream with non empty buffer");
259 log_verbose("CTX: %p remove stream (%zu/%zu); "
260 "flags:%#x ch: %u r:%u f:%s",
261 stream->ctx, audio_pipe_bytes(&stream->fifo),
262 stream->allowed_size, stream->flags,
263 stream->format.channels, stream->format.sampling_rate,
264 pcm_sample_format_str(stream->format.sample_format));
265 audio_pipe_fini(&stream->fifo);
266 free(stream);
267 }
268}
269
270/**
271 * Write new data to a stream.
272 * @param stream The destination stream.
273 * @param data audio data buffer.
274 * @param size size of the @p data buffer.
275 * @return Error code.
276 */
277int hound_ctx_stream_write(hound_ctx_stream_t *stream, const void *data,
278 size_t size)
279{
280 assert(stream);
281
282 if (stream->allowed_size && size > stream->allowed_size)
283 return EINVAL;
284
285 fibril_mutex_lock(&stream->guard);
286 while (stream->allowed_size &&
287 (audio_pipe_bytes(&stream->fifo) + size > stream->allowed_size)) {
288 fibril_condvar_wait(&stream->change, &stream->guard);
289
290 }
291
292 const int ret =
293 audio_pipe_push_data(&stream->fifo, data, size, stream->format);
294 fibril_mutex_unlock(&stream->guard);
295 return ret;
296}
297
298/**
299 * Read data from a buffer.
300 * @param stream The source buffer.
301 * @param data Destination data buffer.
302 * @param size Size of the @p data buffer.
303 * @return Error code.
304 */
305int hound_ctx_stream_read(hound_ctx_stream_t *stream, void *data, size_t size)
306{
307 log_verbose("%p:, %zu", stream, size);
308 return ENOTSUP;
309}
310
311/**
312 * Add (mix) stream data to the destination buffer.
313 * @param stream The source stream.
314 * @param data Destination audio buffer.
315 * @param size Size of the @p data buffer.
316 * @param format Destination data format.
317 * @return Size of the destination buffer touch with stream's data,
318 * error code on failure.
319 */
320ssize_t hound_ctx_stream_add_self(hound_ctx_stream_t *stream, void *data,
321 size_t size, const pcm_format_t *f)
322{
323 assert(stream);
324 fibril_mutex_lock(&stream->guard);
325 const int ret = audio_pipe_mix_data(&stream->fifo, data, size, f);
326 fibril_condvar_signal(&stream->change);
327 fibril_mutex_unlock(&stream->guard);
328 return ret;
329}
330
331/**
332 * Block until the stream's buffer is empty.
333 * @param stream Target stream.
334 */
335void hound_ctx_stream_drain(hound_ctx_stream_t *stream)
336{
337 assert(stream);
338 log_debug("Draining stream");
339 fibril_mutex_lock(&stream->guard);
340 while (audio_pipe_bytes(&stream->fifo))
341 fibril_condvar_wait(&stream->change, &stream->guard);
342 fibril_mutex_unlock(&stream->guard);
343}
344
345/**
346 * Update context data.
347 * @param source Source abstraction.
348 * @param size Required size in source's format.
349 * @return error code.
350 *
351 * Mixes data from all streams and pushes it to all connections.
352 */
353int update_data(audio_source_t *source, size_t size)
354{
355 assert(source);
356 assert(source->private_data);
357 hound_ctx_t *ctx = source->private_data;
358 void *buffer = malloc(size);
359 if (!buffer)
360 return ENOMEM;
361 audio_data_t *adata = audio_data_create(buffer, size, source->format);
362 if (!adata) {
363 free(buffer);
364 return ENOMEM;
365 }
366 log_verbose("CTX: %p. Mixing %zu streams", ctx,
367 list_count(&ctx->streams));
368 pcm_format_silence(buffer, size, &source->format);
369 fibril_mutex_lock(&ctx->guard);
370 list_foreach(ctx->streams, it) {
371 hound_ctx_stream_t *stream = hound_ctx_stream_from_link(it);
372 ssize_t copied = hound_ctx_stream_add_self(
373 stream, buffer, size, &source->format);
374 if (copied != (ssize_t)size)
375 log_warning("Not enough data in stream buffer");
376 }
377 log_verbose("CTX: %p. Pushing audio to %zu connections", ctx,
378 list_count(&source->connections));
379 list_foreach(source->connections, it) {
380 connection_t *conn = connection_from_source_list(it);
381 connection_push_data(conn, adata);
382 }
383 fibril_mutex_unlock(&ctx->guard);
384 return EOK;
385}
386
387/**
388 * @}
389 */
Note: See TracBrowser for help on using the repository browser.