[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),
|
---|
[ea6c838] | 51 | const pcm_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;
|
---|
[ab07cf0] | 66 | source->available_data.position = NULL;
|
---|
[1df3018a] | 67 | source->available_data.size = 0;
|
---|
| 68 | log_verbose("Initialized source (%p) '%s'", source, source->name);
|
---|
[737b4c0] | 69 | return EOK;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[1df3018a] | 72 | void audio_source_fini(audio_source_t *source)
|
---|
| 73 | {
|
---|
[63d6ff9] | 74 | assert(source);
|
---|
[1df3018a] | 75 | assert(source->connected_sink == NULL);
|
---|
| 76 | free(source->name);
|
---|
[63d6ff9] | 77 | source->name = NULL;
|
---|
[1df3018a] | 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;
|
---|
[ea6c838] | 84 | const pcm_format_t old_format = source->format;
|
---|
[1df3018a] | 85 |
|
---|
| 86 | source->connected_sink = sink;
|
---|
[ea6c838] | 87 | if (pcm_format_is_any(&source->format)) {
|
---|
[1df3018a] | 88 | assert(sink);
|
---|
[ea6c838] | 89 | assert(!pcm_format_is_any(&sink->format));
|
---|
[1df3018a] | 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,
|
---|
[ea6c838] | 104 | const pcm_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 | }
|
---|
[bb67def] | 118 | if (source->format.sampling_rate != f->sampling_rate) {
|
---|
| 119 | log_debug("Resampling is not supported, yet");
|
---|
[ec49085] | 120 | return ENOTSUP;
|
---|
| 121 | }
|
---|
[ea6c838] | 122 | const size_t src_frame_size = pcm_format_frame_size(&source->format);
|
---|
| 123 | const size_t dst_frames = size / pcm_format_frame_size(f);
|
---|
[950110ee] | 124 |
|
---|
[ab07cf0] | 125 | if (source->available_data.position == NULL ||
|
---|
[1df3018a] | 126 | source->available_data.size == 0) {
|
---|
| 127 | int ret = EOVERFLOW; /* In fact this is underflow... */
|
---|
| 128 | if (source->update_available_data)
|
---|
[950110ee] | 129 | ret = source->update_available_data(source,
|
---|
| 130 | dst_frames * src_frame_size);
|
---|
[1df3018a] | 131 | if (ret != EOK) {
|
---|
[ab07cf0] | 132 | log_debug("No data to add to %p(%zu)", buffer, size);
|
---|
[1df3018a] | 133 | return ret;
|
---|
| 134 | }
|
---|
[ec49085] | 135 | }
|
---|
| 136 |
|
---|
[ea6c838] | 137 | const int ret = pcm_format_convert_and_mix(buffer, size,
|
---|
[950110ee] | 138 | source->available_data.position, source->available_data.size,
|
---|
| 139 | &source->format, f);
|
---|
[ec49085] | 140 | if (ret != EOK) {
|
---|
[950110ee] | 141 | log_debug("Mixing failed %p <= %p, frames: %zu",
|
---|
| 142 | buffer, source->available_data.position, dst_frames);
|
---|
[ec49085] | 143 | return ret;
|
---|
| 144 | }
|
---|
[ab07cf0] | 145 |
|
---|
[950110ee] | 146 | source->available_data.position += (dst_frames * src_frame_size);
|
---|
| 147 | source->available_data.size -= (dst_frames * src_frame_size);
|
---|
[737b4c0] | 148 | return EOK;
|
---|
| 149 | }
|
---|
[ec49085] | 150 |
|
---|
[737b4c0] | 151 | /**
|
---|
| 152 | * @}
|
---|
| 153 | */
|
---|