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

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

hound: add stream fifo

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