[737b4c0] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 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 | /** @addtogroup audio
|
---|
| 30 | * @brief HelenOS sound server
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
| 37 | #include <errno.h>
|
---|
[ec49085] | 38 | #include <macros.h>
|
---|
[737b4c0] | 39 | #include <stdlib.h>
|
---|
| 40 | #include <str.h>
|
---|
| 41 | #include <str_error.h>
|
---|
| 42 |
|
---|
| 43 | #include "audio_source.h"
|
---|
[1df3018a] | 44 | #include "audio_sink.h"
|
---|
[737b4c0] | 45 | #include "log.h"
|
---|
| 46 |
|
---|
| 47 |
|
---|
[1df3018a] | 48 | int audio_source_init(audio_source_t *source, const char *name, void *data,
|
---|
| 49 | int (*connection_change)(audio_source_t *),
|
---|
| 50 | int (*update_available_data)(audio_source_t *, size_t),
|
---|
| 51 | const audio_format_t *f)
|
---|
[737b4c0] | 52 | {
|
---|
| 53 | assert(source);
|
---|
[1df3018a] | 54 | if (!name || !f) {
|
---|
[737b4c0] | 55 | log_debug("Incorrect parameters.");
|
---|
| 56 | return EINVAL;
|
---|
| 57 | }
|
---|
| 58 | link_initialize(&source->link);
|
---|
| 59 | source->name = str_dup(name);
|
---|
[1df3018a] | 60 | source->private_data = data;
|
---|
| 61 | source->connection_change = connection_change;
|
---|
| 62 | source->update_available_data = update_available_data;
|
---|
| 63 | source->connected_sink = NULL;
|
---|
| 64 | source->format = *f;
|
---|
| 65 | source->available_data.base = NULL;
|
---|
| 66 | source->available_data.size = 0;
|
---|
| 67 | log_verbose("Initialized source (%p) '%s'", source, source->name);
|
---|
[737b4c0] | 68 | return EOK;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[1df3018a] | 71 | void audio_source_fini(audio_source_t *source)
|
---|
| 72 | {
|
---|
| 73 | if (!source)
|
---|
| 74 | return;
|
---|
| 75 | assert(source->connected_sink == NULL);
|
---|
| 76 | free(source->name);
|
---|
| 77 | free(source);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | int audio_source_connected(audio_source_t *source, struct audio_sink *sink)
|
---|
[737b4c0] | 81 | {
|
---|
| 82 | assert(source);
|
---|
[1df3018a] | 83 | audio_sink_t *old_sink = source->connected_sink;
|
---|
| 84 | const audio_format_t old_format = source->format;
|
---|
| 85 |
|
---|
| 86 | source->connected_sink = sink;
|
---|
| 87 | if (audio_format_is_any(&source->format)) {
|
---|
| 88 | assert(sink);
|
---|
| 89 | assert(!audio_format_is_any(&sink->format));
|
---|
| 90 | source->format = sink->format;
|
---|
| 91 | }
|
---|
| 92 | if (source->connection_change) {
|
---|
| 93 | const int ret = source->connection_change(source);
|
---|
| 94 | if (ret != EOK) {
|
---|
| 95 | source->format = old_format;
|
---|
| 96 | source->connected_sink = old_sink;
|
---|
| 97 | return ret;
|
---|
| 98 | }
|
---|
[737b4c0] | 99 | }
|
---|
| 100 | return EOK;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[ec49085] | 103 | int audio_source_add_self(audio_source_t *source, void *buffer, size_t size,
|
---|
| 104 | const audio_format_t *f)
|
---|
[737b4c0] | 105 | {
|
---|
| 106 | assert(source);
|
---|
[ec49085] | 107 | if (!buffer) {
|
---|
| 108 | log_debug("Non-existent buffer");
|
---|
| 109 | return EBADMEM;
|
---|
| 110 | }
|
---|
| 111 | if (!f || size == 0) {
|
---|
| 112 | log_debug("No format or zero size");
|
---|
| 113 | }
|
---|
| 114 | if (size % (pcm_sample_format_size(f->sample_format) * f->channels)) {
|
---|
| 115 | log_debug("Buffer does not fit integer number of frames");
|
---|
[737b4c0] | 116 | return EINVAL;
|
---|
| 117 | }
|
---|
[ec49085] | 118 | if (!audio_format_same(&source->format, f)) {
|
---|
| 119 | log_debug("Format conversion is not supported yet");
|
---|
| 120 | return ENOTSUP;
|
---|
| 121 | }
|
---|
[1df3018a] | 122 | if (source->available_data.base == NULL ||
|
---|
| 123 | source->available_data.size == 0) {
|
---|
| 124 | int ret = EOVERFLOW; /* In fact this is underflow... */
|
---|
| 125 | if (source->update_available_data)
|
---|
| 126 | ret = source->update_available_data(source, size);
|
---|
| 127 | if (ret != EOK) {
|
---|
| 128 | log_debug("No data to add");
|
---|
| 129 | return ret;
|
---|
| 130 | }
|
---|
[ec49085] | 131 | }
|
---|
| 132 |
|
---|
[1df3018a] | 133 | const size_t real_size = min(size, source->available_data.size);
|
---|
[ec49085] | 134 | const int ret =
|
---|
[1df3018a] | 135 | audio_format_mix(buffer, source->available_data.base, real_size, f);
|
---|
[ec49085] | 136 | if (ret != EOK) {
|
---|
| 137 | log_debug("Mixing failed");
|
---|
| 138 | return ret;
|
---|
| 139 | }
|
---|
[1df3018a] | 140 | source->available_data.base += real_size;
|
---|
| 141 | source->available_data.size -= real_size;
|
---|
| 142 | buffer += real_size;
|
---|
| 143 | size -= real_size;
|
---|
| 144 |
|
---|
| 145 | //TODO update data again
|
---|
| 146 | if (size)
|
---|
| 147 | log_warning("not enough data");
|
---|
| 148 |
|
---|
[737b4c0] | 149 | return EOK;
|
---|
| 150 | }
|
---|
[ec49085] | 151 |
|
---|
[737b4c0] | 152 | /**
|
---|
| 153 | * @}
|
---|
| 154 | */
|
---|