| 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>
|
|---|
| 38 | #include <macros.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <str.h>
|
|---|
| 41 | #include <str_error.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "audio_source.h"
|
|---|
| 44 | #include "audio_sink.h"
|
|---|
| 45 | #include "log.h"
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | int audio_source_init(audio_source_t *source, const char *name, void *data,
|
|---|
| 49 | int (*connection_change)(audio_source_t *, bool new),
|
|---|
| 50 | int (*update_available_data)(audio_source_t *, size_t),
|
|---|
| 51 | const pcm_format_t *f)
|
|---|
| 52 | {
|
|---|
| 53 | assert(source);
|
|---|
| 54 | if (!name || !f) {
|
|---|
| 55 | log_debug("Incorrect parameters.");
|
|---|
| 56 | return EINVAL;
|
|---|
| 57 | }
|
|---|
| 58 | link_initialize(&source->link);
|
|---|
| 59 | list_initialize(&source->connections);
|
|---|
| 60 | source->name = str_dup(name);
|
|---|
| 61 | if (!source->name)
|
|---|
| 62 | return ENOMEM;
|
|---|
| 63 | source->private_data = data;
|
|---|
| 64 | source->connection_change = connection_change;
|
|---|
| 65 | source->update_available_data = update_available_data;
|
|---|
| 66 | source->format = *f;
|
|---|
| 67 | source->available_data.base = NULL;
|
|---|
| 68 | source->available_data.position = NULL;
|
|---|
| 69 | source->available_data.size = 0;
|
|---|
| 70 | log_verbose("Initialized source (%p) '%s'", source, source->name);
|
|---|
| 71 | return EOK;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | void audio_source_fini(audio_source_t *source)
|
|---|
| 75 | {
|
|---|
| 76 | assert(source);
|
|---|
| 77 | free(source->name);
|
|---|
| 78 | source->name = NULL;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | int audio_source_add_self(audio_source_t *source, void *buffer, size_t size,
|
|---|
| 82 | const pcm_format_t *f)
|
|---|
| 83 | {
|
|---|
| 84 | assert(source);
|
|---|
| 85 | if (!buffer) {
|
|---|
| 86 | log_debug("Non-existent buffer");
|
|---|
| 87 | return EBADMEM;
|
|---|
| 88 | }
|
|---|
| 89 | if (!f || size == 0) {
|
|---|
| 90 | log_debug("No format or zero size");
|
|---|
| 91 | }
|
|---|
| 92 | if (size % (pcm_sample_format_size(f->sample_format) * f->channels)) {
|
|---|
| 93 | log_debug("Buffer does not fit integer number of frames");
|
|---|
| 94 | return EINVAL;
|
|---|
| 95 | }
|
|---|
| 96 | if (source->format.sampling_rate != f->sampling_rate) {
|
|---|
| 97 | log_debug("Resampling is not supported, yet");
|
|---|
| 98 | return ENOTSUP;
|
|---|
| 99 | }
|
|---|
| 100 | const size_t src_frame_size = pcm_format_frame_size(&source->format);
|
|---|
| 101 | const size_t dst_frames = size / pcm_format_frame_size(f);
|
|---|
| 102 |
|
|---|
| 103 | if (source->available_data.position == NULL ||
|
|---|
| 104 | source->available_data.size == 0) {
|
|---|
| 105 | int ret = EOVERFLOW; /* In fact this is underflow... */
|
|---|
| 106 | if (source->update_available_data)
|
|---|
| 107 | ret = source->update_available_data(source,
|
|---|
| 108 | dst_frames * src_frame_size);
|
|---|
| 109 | if (ret != EOK) {
|
|---|
| 110 | log_debug("No data to add to %p(%zu)", buffer, size);
|
|---|
| 111 | return ret;
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | const int ret = pcm_format_convert_and_mix(buffer, size,
|
|---|
| 116 | source->available_data.position, source->available_data.size,
|
|---|
| 117 | &source->format, f);
|
|---|
| 118 | if (ret != EOK) {
|
|---|
| 119 | log_debug("Mixing failed %p <= %p, frames: %zu",
|
|---|
| 120 | buffer, source->available_data.position, dst_frames);
|
|---|
| 121 | return ret;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | source->available_data.position += (dst_frames * src_frame_size);
|
|---|
| 125 | source->available_data.size -= (dst_frames * src_frame_size);
|
|---|
| 126 | return EOK;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /**
|
|---|
| 130 | * @}
|
|---|
| 131 | */
|
|---|