| 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 |
|
|---|
| 43 | hound_ctx_t *hound_record_ctx_get(const char *name)
|
|---|
| 44 | {
|
|---|
| 45 | return NULL;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | hound_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 |
|
|---|
| 71 | void 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 |
|
|---|
| 85 | hound_context_id_t hound_ctx_get_id(hound_ctx_t *ctx)
|
|---|
| 86 | {
|
|---|
| 87 | assert(ctx);
|
|---|
| 88 | return (hound_context_id_t)ctx;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | bool hound_ctx_is_record(hound_ctx_t *ctx)
|
|---|
| 92 | {
|
|---|
| 93 | assert(ctx);
|
|---|
| 94 | return ctx->source == NULL;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /*
|
|---|
| 98 | * STREAMS
|
|---|
| 99 | */
|
|---|
| 100 | typedef 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 |
|
|---|
| 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 | 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 |
|
|---|
| 136 | void 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_t *data = audio_data_list_instance(l);
|
|---|
| 146 | list_remove(l);
|
|---|
| 147 | free(data->data);
|
|---|
| 148 | free(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 |
|
|---|
| 161 | int 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_t *adata = malloc(sizeof(audio_data_t));
|
|---|
| 175 | if (adata) {
|
|---|
| 176 | adata->data = data;
|
|---|
| 177 | adata->size = size;
|
|---|
| 178 | link_initialize(&adata->link);
|
|---|
| 179 | list_append(&adata->link, &stream->fifo);
|
|---|
| 180 | stream->current_size += size;
|
|---|
| 181 | return EOK;
|
|---|
| 182 | }
|
|---|
| 183 | log_warning("Failed to enqueue %zu bytes of data.", size);
|
|---|
| 184 | return ENOMEM;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | int hound_ctx_stream_read(hound_ctx_stream_t *stream, void *data, size_t size)
|
|---|
| 188 | {
|
|---|
| 189 | log_verbose("%p:, %zu", stream, size);
|
|---|
| 190 | return ENOTSUP;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /**
|
|---|
| 194 | * @}
|
|---|
| 195 | */
|
|---|