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

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

hound: make audio data reference counted

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