| 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 |
|
|---|
| 44 | hound_ctx_t *hound_record_ctx_get(const char *name)
|
|---|
| 45 | {
|
|---|
| 46 | return NULL;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | hound_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 |
|
|---|
| 72 | void 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 |
|
|---|
| 86 | hound_context_id_t hound_ctx_get_id(hound_ctx_t *ctx)
|
|---|
| 87 | {
|
|---|
| 88 | assert(ctx);
|
|---|
| 89 | return (hound_context_id_t)ctx;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | bool hound_ctx_is_record(hound_ctx_t *ctx)
|
|---|
| 93 | {
|
|---|
| 94 | assert(ctx);
|
|---|
| 95 | return ctx->source == NULL;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /*
|
|---|
| 99 | * STREAMS
|
|---|
| 100 | */
|
|---|
| 101 | typedef struct hound_ctx_stream {
|
|---|
| 102 | link_t link;
|
|---|
| 103 | audio_pipe_t fifo;
|
|---|
| 104 | hound_ctx_t *ctx;
|
|---|
| 105 | pcm_format_t format;
|
|---|
| 106 | int flags;
|
|---|
| 107 | size_t allowed_size;
|
|---|
| 108 | } hound_ctx_stream_t;
|
|---|
| 109 |
|
|---|
| 110 | static 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 |
|
|---|
| 115 | hound_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 | audio_pipe_init(&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 | list_append(&stream->link, &ctx->streams);
|
|---|
| 128 | log_verbose("CTX: %p added stream; flags:%#x ch: %u r:%u f:%s",
|
|---|
| 129 | ctx, flags, format.channels, format.sampling_rate,
|
|---|
| 130 | pcm_sample_format_str(format.sample_format));
|
|---|
| 131 | }
|
|---|
| 132 | return stream;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | void hound_ctx_destroy_stream(hound_ctx_stream_t *stream)
|
|---|
| 136 | {
|
|---|
| 137 | if (stream) {
|
|---|
| 138 | //TODO consider DRAIN FLAG
|
|---|
| 139 | list_remove(&stream->link);
|
|---|
| 140 | if (audio_pipe_bytes(&stream->fifo))
|
|---|
| 141 | log_warning("Destroying stream with non empty buffer");
|
|---|
| 142 | audio_pipe_fini(&stream->fifo);
|
|---|
| 143 | log_verbose("CTX: %p remove stream (%zu/%zu); "
|
|---|
| 144 | "flags:%#x ch: %u r:%u f:%s",
|
|---|
| 145 | stream->ctx, audio_pipe_bytes(&stream->fifo),
|
|---|
| 146 | stream->allowed_size, stream->flags,
|
|---|
| 147 | stream->format.channels, stream->format.sampling_rate,
|
|---|
| 148 | pcm_sample_format_str(stream->format.sample_format));
|
|---|
| 149 | free(stream);
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 | int hound_ctx_stream_write(hound_ctx_stream_t *stream, const void *data,
|
|---|
| 155 | size_t size)
|
|---|
| 156 | {
|
|---|
| 157 | assert(stream);
|
|---|
| 158 | log_verbose("%p:, %zu", stream, size);
|
|---|
| 159 |
|
|---|
| 160 | if (stream->allowed_size && size > stream->allowed_size)
|
|---|
| 161 | return EINVAL;
|
|---|
| 162 |
|
|---|
| 163 | if (stream->allowed_size &&
|
|---|
| 164 | (audio_pipe_bytes(&stream->fifo) + size > stream->allowed_size))
|
|---|
| 165 | return EBUSY;
|
|---|
| 166 |
|
|---|
| 167 | return audio_pipe_push_data(&stream->fifo, data, size, stream->format);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | int hound_ctx_stream_read(hound_ctx_stream_t *stream, void *data, size_t size)
|
|---|
| 171 | {
|
|---|
| 172 | log_verbose("%p:, %zu", stream, size);
|
|---|
| 173 | return ENOTSUP;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | int hound_ctx_stream_add_self(hound_ctx_stream_t *stream, void *data,
|
|---|
| 177 | size_t size, const pcm_format_t *f)
|
|---|
| 178 | {
|
|---|
| 179 | assert(stream);
|
|---|
| 180 | const ssize_t copied_size =
|
|---|
| 181 | audio_pipe_mix_data(&stream->fifo, data, size, f);
|
|---|
| 182 | if (copied_size != (ssize_t)size)
|
|---|
| 183 | log_warning("Not enough data in stream buffer");
|
|---|
| 184 | return copied_size > 0 ? EOK : copied_size;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | void hound_ctx_stream_drain(hound_ctx_stream_t *stream)
|
|---|
| 188 | {
|
|---|
| 189 | assert(stream);
|
|---|
| 190 | while (audio_pipe_bytes(&stream->fifo))
|
|---|
| 191 | async_usleep(10000);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | int hound_ctx_stream_add(hound_ctx_stream_t *stream, void *buffer, size_t size,
|
|---|
| 195 | pcm_format_t format)
|
|---|
| 196 | {
|
|---|
| 197 | return ENOTSUP;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /**
|
|---|
| 201 | * @}
|
|---|
| 202 | */
|
|---|