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

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

hound: add connection fifo, implement stream mixing

  • Property mode set to 100644
File size: 6.6 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 "log.h"
43
44hound_ctx_t *hound_record_ctx_get(const char *name)
45{
46 return NULL;
47}
48
49hound_ctx_t *hound_playback_ctx_get(const char *name)
50{
51 hound_ctx_t *ctx = malloc(sizeof(hound_ctx_t));
52 if (ctx) {
53 link_initialize(&ctx->link);
54 list_initialize(&ctx->streams);
55 ctx->sink = NULL;
56 ctx->source = malloc(sizeof(audio_source_t));
57 if (!ctx->source) {
58 free(ctx);
59 return NULL;
60 }
61 const int ret = audio_source_init(ctx->source, name, ctx, NULL,
62 NULL, &AUDIO_FORMAT_ANY);
63 if (ret != EOK) {
64 free(ctx->source);
65 free(ctx);
66 return NULL;
67 }
68 }
69 return ctx;
70}
71
72void hound_ctx_destroy(hound_ctx_t *ctx)
73{
74 assert(ctx);
75 assert(!link_in_use(&ctx->link));
76 if (ctx->source)
77 audio_source_fini(ctx->source);
78 if (ctx->sink)
79 audio_sink_fini(ctx->sink);
80 //TODO remove streams
81 free(ctx->source);
82 free(ctx->sink);
83 free(ctx);
84}
85
86hound_context_id_t hound_ctx_get_id(hound_ctx_t *ctx)
87{
88 assert(ctx);
89 return (hound_context_id_t)ctx;
90}
91
92bool hound_ctx_is_record(hound_ctx_t *ctx)
93{
94 assert(ctx);
95 return ctx->source == NULL;
96}
97
98/*
99 * STREAMS
100 */
101typedef struct hound_ctx_stream {
102 link_t link;
103 list_t fifo;
104 hound_ctx_t *ctx;
105 pcm_format_t format;
106 int flags;
107 size_t allowed_size;
108 size_t current_size;
109} hound_ctx_stream_t;
110
111static inline hound_ctx_stream_t *hound_ctx_stream_from_link(link_t *l)
112{
113 return l ? list_get_instance(l, hound_ctx_stream_t, link) : NULL;
114}
115
116hound_ctx_stream_t *hound_ctx_create_stream(hound_ctx_t *ctx, int flags,
117 pcm_format_t format, size_t buffer_size)
118{
119 assert(ctx);
120 hound_ctx_stream_t *stream = malloc(sizeof(hound_ctx_stream_t));
121 if (stream) {
122 list_initialize(&stream->fifo);
123 link_initialize(&stream->link);
124 stream->ctx = ctx;
125 stream->flags = flags;
126 stream->format = format;
127 stream->allowed_size = buffer_size;
128 stream->current_size = 0;
129 list_append(&stream->link, &ctx->streams);
130 log_verbose("CTX: %p added stream; flags:%#x ch: %u r:%u f:%s",
131 ctx, flags, format.channels, format.sampling_rate,
132 pcm_sample_format_str(format.sample_format));
133 }
134 return stream;
135}
136
137void hound_ctx_destroy_stream(hound_ctx_stream_t *stream)
138{
139 if (stream) {
140 //TODO consider DRAIN FLAG
141 list_remove(&stream->link);
142 if (!list_empty(&stream->fifo))
143 log_warning("Destroying stream with non empty buffer");
144 while (!list_empty(&stream->fifo)) {
145 link_t *l = list_first(&stream->fifo);
146 audio_data_link_t *data =
147 audio_data_link_list_instance(l);
148 list_remove(l);
149 audio_data_link_destroy(data);
150 }
151 log_verbose("CTX: %p remove stream (%zu/%zu); "
152 "flags:%#x ch: %u r:%u f:%s",
153 stream->ctx, stream->current_size, stream->allowed_size,
154 stream->flags, stream->format.channels,
155 stream->format.sampling_rate,
156 pcm_sample_format_str(stream->format.sample_format));
157 free(stream);
158 }
159}
160
161
162int hound_ctx_stream_write(hound_ctx_stream_t *stream, const void *data,
163 size_t size)
164{
165 assert(stream);
166 log_verbose("%p:, %zu", stream, size);
167
168 if (stream->allowed_size && size > stream->allowed_size)
169 return EINVAL;
170
171 if (stream->allowed_size &&
172 (stream->current_size + size > stream->allowed_size))
173 return EBUSY;
174
175 audio_data_link_t *adatalink =
176 audio_data_link_create_data(data, size, stream->format);
177 if (adatalink) {
178 list_append(&adatalink->link, &stream->fifo);
179 stream->current_size += size;
180 return EOK;
181 }
182 log_warning("Failed to enqueue %zu bytes of data.", size);
183 return ENOMEM;
184}
185
186int hound_ctx_stream_read(hound_ctx_stream_t *stream, void *data, size_t size)
187{
188 log_verbose("%p:, %zu", stream, size);
189 return ENOTSUP;
190}
191
192int hound_ctx_stream_add_self(hound_ctx_stream_t *stream, void *data,
193 size_t size, const pcm_format_t *f)
194{
195 assert(stream);
196 const size_t src_frame_size = pcm_format_frame_size(&stream->format);
197 const size_t dst_frame_size = pcm_format_frame_size(f);
198 //TODO consider sample rate
199 size_t needed_frames = size / dst_frame_size;
200 while (needed_frames > 0 && !list_empty(&stream->fifo)) {
201 link_t *l = list_first(&stream->fifo);
202 audio_data_link_t *alink = audio_data_link_list_instance(l);
203 /* Get actual audio data chunk */
204 const size_t available_frames =
205 audio_data_link_available_frames(alink);
206 const size_t copy_frames = min(available_frames, needed_frames);
207 const size_t copy_size = copy_frames * dst_frame_size;
208
209 /* Copy audio data */
210 pcm_format_convert_and_mix(data, copy_size,
211 audio_data_link_start(alink),
212 audio_data_link_remain_size(alink),
213 &alink->adata->format, f);
214
215 /* Update values */
216 needed_frames -= copy_frames;
217 data += copy_size;
218 alink->position += (copy_frames * src_frame_size);
219 if (audio_data_link_remain_size(alink) == 0) {
220 list_remove(&alink->link);
221 audio_data_link_destroy(alink);
222 } else {
223 assert(needed_frames == 0);
224 }
225 }
226 return ENOTSUP;
227}
228
229void hound_ctx_stream_drain(hound_ctx_stream_t *stream)
230{
231 assert(stream);
232 while (!list_empty(&stream->fifo))
233 async_usleep(10000);
234}
235
236int hound_ctx_stream_add(hound_ctx_stream_t *stream, void *buffer, size_t size,
237 pcm_format_t format)
238{
239 return ENOTSUP;
240}
241
242/**
243 * @}
244 */
Note: See TracBrowser for help on using the repository browser.