source: mainline/uspace/srv/audio/hound/audio_source.c@ fa60cd69

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fa60cd69 was fa60cd69, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

hound: add connection class

This will enable N to M routing in the future

  • Property mode set to 100644
File size: 4.0 KB
Line 
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
48int 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 source->private_data = data;
62 source->connection_change = connection_change;
63 source->update_available_data = update_available_data;
64 source->format = *f;
65 source->available_data.base = NULL;
66 source->available_data.position = NULL;
67 source->available_data.size = 0;
68 log_verbose("Initialized source (%p) '%s'", source, source->name);
69 return EOK;
70}
71
72void audio_source_fini(audio_source_t *source)
73{
74 assert(source);
75 free(source->name);
76 source->name = NULL;
77}
78
79int audio_source_add_self(audio_source_t *source, void *buffer, size_t size,
80 const pcm_format_t *f)
81{
82 assert(source);
83 if (!buffer) {
84 log_debug("Non-existent buffer");
85 return EBADMEM;
86 }
87 if (!f || size == 0) {
88 log_debug("No format or zero size");
89 }
90 if (size % (pcm_sample_format_size(f->sample_format) * f->channels)) {
91 log_debug("Buffer does not fit integer number of frames");
92 return EINVAL;
93 }
94 if (source->format.sampling_rate != f->sampling_rate) {
95 log_debug("Resampling is not supported, yet");
96 return ENOTSUP;
97 }
98 const size_t src_frame_size = pcm_format_frame_size(&source->format);
99 const size_t dst_frames = size / pcm_format_frame_size(f);
100
101 if (source->available_data.position == NULL ||
102 source->available_data.size == 0) {
103 int ret = EOVERFLOW; /* In fact this is underflow... */
104 if (source->update_available_data)
105 ret = source->update_available_data(source,
106 dst_frames * src_frame_size);
107 if (ret != EOK) {
108 log_debug("No data to add to %p(%zu)", buffer, size);
109 return ret;
110 }
111 }
112
113 const int ret = pcm_format_convert_and_mix(buffer, size,
114 source->available_data.position, source->available_data.size,
115 &source->format, f);
116 if (ret != EOK) {
117 log_debug("Mixing failed %p <= %p, frames: %zu",
118 buffer, source->available_data.position, dst_frames);
119 return ret;
120 }
121
122 source->available_data.position += (dst_frames * src_frame_size);
123 source->available_data.size -= (dst_frames * src_frame_size);
124 return EOK;
125}
126
127/**
128 * @}
129 */
Note: See TracBrowser for help on using the repository browser.