| 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 |
|
|---|
| 45 | static int update_data(audio_source_t *source, size_t size);
|
|---|
| 46 |
|
|---|
| 47 | hound_ctx_t *hound_record_ctx_get(const char *name)
|
|---|
| 48 | {
|
|---|
| 49 | return NULL;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | hound_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 |
|
|---|
| 75 | void 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 |
|
|---|
| 89 | hound_context_id_t hound_ctx_get_id(hound_ctx_t *ctx)
|
|---|
| 90 | {
|
|---|
| 91 | assert(ctx);
|
|---|
| 92 | return (hound_context_id_t)ctx;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | bool hound_ctx_is_record(hound_ctx_t *ctx)
|
|---|
| 96 | {
|
|---|
| 97 | assert(ctx);
|
|---|
| 98 | return ctx->source == NULL;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | /*
|
|---|
| 103 | * STREAMS
|
|---|
| 104 | */
|
|---|
| 105 | typedef 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 |
|
|---|
| 114 | static 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 |
|
|---|
| 119 | hound_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 |
|
|---|
| 139 | void 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 |
|
|---|
| 158 | int hound_ctx_stream_write(hound_ctx_stream_t *stream, const void *data,
|
|---|
| 159 | size_t size)
|
|---|
| 160 | {
|
|---|
| 161 | assert(stream);
|
|---|
| 162 |
|
|---|
| 163 | if (stream->allowed_size && size > stream->allowed_size)
|
|---|
| 164 | return EINVAL;
|
|---|
| 165 |
|
|---|
| 166 | if (stream->allowed_size &&
|
|---|
| 167 | (audio_pipe_bytes(&stream->fifo) + size > stream->allowed_size))
|
|---|
| 168 | return EBUSY;
|
|---|
| 169 |
|
|---|
| 170 | return audio_pipe_push_data(&stream->fifo, data, size, stream->format);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | int hound_ctx_stream_read(hound_ctx_stream_t *stream, void *data, size_t size)
|
|---|
| 174 | {
|
|---|
| 175 | log_verbose("%p:, %zu", stream, size);
|
|---|
| 176 | return ENOTSUP;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | int hound_ctx_stream_add_self(hound_ctx_stream_t *stream, void *data,
|
|---|
| 180 | size_t size, const pcm_format_t *f)
|
|---|
| 181 | {
|
|---|
| 182 | assert(stream);
|
|---|
| 183 | const ssize_t copied_size =
|
|---|
| 184 | audio_pipe_mix_data(&stream->fifo, data, size, f);
|
|---|
| 185 | if (copied_size != (ssize_t)size)
|
|---|
| 186 | log_warning("Not enough data in stream buffer");
|
|---|
| 187 | return copied_size > 0 ? EOK : copied_size;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | void hound_ctx_stream_drain(hound_ctx_stream_t *stream)
|
|---|
| 191 | {
|
|---|
| 192 | assert(stream);
|
|---|
| 193 | log_debug("Draining stream");
|
|---|
| 194 | while (audio_pipe_bytes(&stream->fifo))
|
|---|
| 195 | async_usleep(10000);
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | int hound_ctx_stream_add(hound_ctx_stream_t *stream, void *buffer, size_t size,
|
|---|
| 199 | pcm_format_t format)
|
|---|
| 200 | {
|
|---|
| 201 | return ENOTSUP;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 | int update_data(audio_source_t *source, size_t size)
|
|---|
| 206 | {
|
|---|
| 207 | assert(source);
|
|---|
| 208 | assert(source->private_data);
|
|---|
| 209 | hound_ctx_t *ctx = source->private_data;
|
|---|
| 210 | void *buffer = malloc(size);
|
|---|
| 211 | if (!buffer)
|
|---|
| 212 | return ENOMEM;
|
|---|
| 213 | audio_data_t *adata = audio_data_create(buffer, size, source->format);
|
|---|
| 214 | if (!adata) {
|
|---|
| 215 | free(buffer);
|
|---|
| 216 | return ENOMEM;
|
|---|
| 217 | }
|
|---|
| 218 | log_verbose("CTX: %p. Mixing %zu streams", ctx,
|
|---|
| 219 | list_count(&ctx->streams));
|
|---|
| 220 | pcm_format_silence(buffer, size, &source->format);
|
|---|
| 221 | list_foreach(ctx->streams, it) {
|
|---|
| 222 | hound_ctx_stream_t *stream = hound_ctx_stream_from_link(it);
|
|---|
| 223 | hound_ctx_stream_add_self(stream, buffer, size, &source->format);
|
|---|
| 224 | }
|
|---|
| 225 | log_verbose("CTX: %p. Pushing audio to %zu connections", ctx,
|
|---|
| 226 | list_count(&source->connections));
|
|---|
| 227 | list_foreach(source->connections, it) {
|
|---|
| 228 | connection_t *conn = connection_from_source_list(it);
|
|---|
| 229 | connection_push_data(conn, adata);
|
|---|
| 230 | }
|
|---|
| 231 | return EOK;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | /**
|
|---|
| 235 | * @}
|
|---|
| 236 | */
|
|---|