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 *),
|
---|
50 | int (*update_available_data)(audio_source_t *, size_t),
|
---|
51 | const audio_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 | source->name = str_dup(name);
|
---|
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.position = NULL;
|
---|
67 | source->available_data.size = 0;
|
---|
68 | log_verbose("Initialized source (%p) '%s'", source, source->name);
|
---|
69 | return EOK;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void audio_source_fini(audio_source_t *source)
|
---|
73 | {
|
---|
74 | assert(source);
|
---|
75 | assert(source->connected_sink == NULL);
|
---|
76 | free(source->name);
|
---|
77 | source->name = NULL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | int audio_source_connected(audio_source_t *source, struct audio_sink *sink)
|
---|
81 | {
|
---|
82 | assert(source);
|
---|
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 | }
|
---|
99 | }
|
---|
100 | return EOK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | int audio_source_add_self(audio_source_t *source, void *buffer, size_t size,
|
---|
104 | const audio_format_t *f)
|
---|
105 | {
|
---|
106 | assert(source);
|
---|
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");
|
---|
116 | return EINVAL;
|
---|
117 | }
|
---|
118 | if (!audio_format_same(&source->format, f)) {
|
---|
119 | log_debug("Format conversion is not supported yet");
|
---|
120 | return ENOTSUP;
|
---|
121 | }
|
---|
122 | if (source->available_data.position == 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 to %p(%zu)", buffer, size);
|
---|
129 | return ret;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | const size_t real_size = min(size, source->available_data.size);
|
---|
134 | const int ret =
|
---|
135 | audio_format_mix(buffer, source->available_data.position, real_size, f);
|
---|
136 | if (ret != EOK) {
|
---|
137 | log_debug("Mixing failed %p <= %p, %zu",
|
---|
138 | buffer, source->available_data.position, real_size);
|
---|
139 | return ret;
|
---|
140 | }
|
---|
141 |
|
---|
142 | source->available_data.position += real_size;
|
---|
143 | source->available_data.size -= real_size;
|
---|
144 |
|
---|
145 | log_verbose("Mixing successful %p <= %p, %zu",
|
---|
146 | buffer, source->available_data.position, real_size);
|
---|
147 |
|
---|
148 | buffer += real_size;
|
---|
149 | size -= real_size;
|
---|
150 |
|
---|
151 | //TODO update data again
|
---|
152 | if (size)
|
---|
153 | log_warning("not enough data");
|
---|
154 |
|
---|
155 | return EOK;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * @}
|
---|
160 | */
|
---|