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

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

hound: implement data update for hound_ctx

pushes data to every connection

  • Property mode set to 100644
File size: 6.3 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 ctx->sink = NULL;
59 ctx->source = malloc(sizeof(audio_source_t));
60 if (!ctx->source) {
61 free(ctx);
62 return NULL;
63 }
64 const int ret = audio_source_init(ctx->source, name, ctx, NULL,
65 update_data, &AUDIO_FORMAT_DEFAULT);
66 if (ret != EOK) {
67 free(ctx->source);
68 free(ctx);
69 return NULL;
70 }
71 }
72 return ctx;
73}
74
75void hound_ctx_destroy(hound_ctx_t *ctx)
76{
77 assert(ctx);
78 assert(!link_in_use(&ctx->link));
79 if (ctx->source)
80 audio_source_fini(ctx->source);
81 if (ctx->sink)
82 audio_sink_fini(ctx->sink);
83 //TODO remove streams
84 free(ctx->source);
85 free(ctx->sink);
86 free(ctx);
87}
88
89hound_context_id_t hound_ctx_get_id(hound_ctx_t *ctx)
90{
91 assert(ctx);
92 return (hound_context_id_t)ctx;
93}
94
95bool hound_ctx_is_record(hound_ctx_t *ctx)
96{
97 assert(ctx);
98 return ctx->source == NULL;
99}
100
101
102/*
103 * STREAMS
104 */
105typedef struct hound_ctx_stream {
106 link_t link;
107 audio_pipe_t fifo;
108 hound_ctx_t *ctx;
109 pcm_format_t format;
110 int flags;
111 size_t allowed_size;
112} hound_ctx_stream_t;
113
114static inline hound_ctx_stream_t *hound_ctx_stream_from_link(link_t *l)
115{
116 return l ? list_get_instance(l, hound_ctx_stream_t, link) : NULL;
117}
118
119hound_ctx_stream_t *hound_ctx_create_stream(hound_ctx_t *ctx, int flags,
120 pcm_format_t format, size_t buffer_size)
121{
122 assert(ctx);
123 hound_ctx_stream_t *stream = malloc(sizeof(hound_ctx_stream_t));
124 if (stream) {
125 audio_pipe_init(&stream->fifo);
126 link_initialize(&stream->link);
127 stream->ctx = ctx;
128 stream->flags = flags;
129 stream->format = format;
130 stream->allowed_size = buffer_size;
131 list_append(&stream->link, &ctx->streams);
132 log_verbose("CTX: %p added stream; flags:%#x ch: %u r:%u f:%s",
133 ctx, flags, format.channels, format.sampling_rate,
134 pcm_sample_format_str(format.sample_format));
135 }
136 return stream;
137}
138
139void hound_ctx_destroy_stream(hound_ctx_stream_t *stream)
140{
141 if (stream) {
142 //TODO consider DRAIN FLAG
143 list_remove(&stream->link);
144 if (audio_pipe_bytes(&stream->fifo))
145 log_warning("Destroying stream with non empty buffer");
146 log_verbose("CTX: %p remove stream (%zu/%zu); "
147 "flags:%#x ch: %u r:%u f:%s",
148 stream->ctx, audio_pipe_bytes(&stream->fifo),
149 stream->allowed_size, stream->flags,
150 stream->format.channels, stream->format.sampling_rate,
151 pcm_sample_format_str(stream->format.sample_format));
152 audio_pipe_fini(&stream->fifo);
153 free(stream);
154 }
155}
156
157
158int hound_ctx_stream_write(hound_ctx_stream_t *stream, const void *data,
159 size_t size)
160{
161 assert(stream);
162 log_verbose("%p: %zu", stream, size);
163
164 if (stream->allowed_size && size > stream->allowed_size)
165 return EINVAL;
166
167 if (stream->allowed_size &&
168 (audio_pipe_bytes(&stream->fifo) + size > stream->allowed_size))
169 return EBUSY;
170
171 return audio_pipe_push_data(&stream->fifo, data, size, stream->format);
172}
173
174int hound_ctx_stream_read(hound_ctx_stream_t *stream, void *data, size_t size)
175{
176 log_verbose("%p:, %zu", stream, size);
177 return ENOTSUP;
178}
179
180int hound_ctx_stream_add_self(hound_ctx_stream_t *stream, void *data,
181 size_t size, const pcm_format_t *f)
182{
183 assert(stream);
184 const ssize_t copied_size =
185 audio_pipe_mix_data(&stream->fifo, data, size, f);
186 if (copied_size != (ssize_t)size)
187 log_warning("Not enough data in stream buffer");
188 return copied_size > 0 ? EOK : copied_size;
189}
190
191void hound_ctx_stream_drain(hound_ctx_stream_t *stream)
192{
193 assert(stream);
194 log_debug("Draining stream");
195 while (audio_pipe_bytes(&stream->fifo))
196 async_usleep(10000);
197}
198
199int hound_ctx_stream_add(hound_ctx_stream_t *stream, void *buffer, size_t size,
200 pcm_format_t format)
201{
202 return ENOTSUP;
203}
204
205
206int update_data(audio_source_t *source, size_t size)
207{
208 assert(source);
209 assert(source->private_data);
210 hound_ctx_t *ctx = source->private_data;
211 void *buffer = malloc(size);
212 if (!buffer)
213 return ENOMEM;
214 audio_data_t *adata = audio_data_create(buffer, size, source->format);
215 if (!adata) {
216 free(buffer);
217 return ENOMEM;
218 }
219 log_verbose("CTX: %p. Mixing %zu streams", ctx,
220 list_count(&ctx->streams));
221 pcm_format_silence(buffer, size, &source->format);
222 list_foreach(ctx->streams, it) {
223 hound_ctx_stream_t *stream = hound_ctx_stream_from_link(it);
224 hound_ctx_stream_add_self(stream, buffer, size, &source->format);
225 }
226 log_verbose("CTX: %p. Pushing audio to %zu connections", ctx,
227 list_count(&source->connections));
228 list_foreach(source->connections, it) {
229 connection_t *conn = connection_from_source_list(it);
230 connection_push_data(conn, adata);
231 }
232 return EOK;
233}
234
235/**
236 * @}
237 */
Note: See TracBrowser for help on using the repository browser.