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 "log.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | int audio_source_init(audio_source_t *source, const char* name)
|
---|
48 | {
|
---|
49 | assert(source);
|
---|
50 | if (!name) {
|
---|
51 | log_debug("Incorrect parameters.");
|
---|
52 | return EINVAL;
|
---|
53 | }
|
---|
54 | link_initialize(&source->link);
|
---|
55 | source->name = str_dup(name);
|
---|
56 | source->connected_change.hook = NULL;
|
---|
57 | source->connected_change.arg = NULL;
|
---|
58 | source->get_data.hook = NULL;
|
---|
59 | source->get_data.arg = NULL;
|
---|
60 | source->available.base = NULL;
|
---|
61 | source->available.size = 0;
|
---|
62 | log_verbose("Initialized source (%p) '%s' with ANY audio format",
|
---|
63 | source, source->name);
|
---|
64 | return EOK;
|
---|
65 | }
|
---|
66 |
|
---|
67 | int audio_source_connected(audio_source_t *source, const audio_format_t *f)
|
---|
68 | {
|
---|
69 | assert(source);
|
---|
70 | if (!f) {
|
---|
71 | log_debug("Incorrect parameters.");
|
---|
72 | return EINVAL;
|
---|
73 | }
|
---|
74 | if (source->connected_change.hook)
|
---|
75 | source->connected_change.hook(source->connected_change.arg, f);
|
---|
76 | return EOK;
|
---|
77 | }
|
---|
78 |
|
---|
79 | int audio_source_add_self(audio_source_t *source, void *buffer, size_t size,
|
---|
80 | const audio_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 (!audio_format_same(&source->format, f)) {
|
---|
95 | log_debug("Format conversion is not supported yet");
|
---|
96 | return ENOTSUP;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (source->available.size == 0) {
|
---|
100 | log_debug("No data to add");
|
---|
101 | return EOVERFLOW; /* In fact this is underflow... */
|
---|
102 | }
|
---|
103 |
|
---|
104 | const size_t real_size = min(size, source->available.size);
|
---|
105 | const int ret =
|
---|
106 | audio_format_mix(buffer, source->available.base, real_size, f);
|
---|
107 | if (ret != EOK) {
|
---|
108 | log_debug("Mixing failed");
|
---|
109 | return ret;
|
---|
110 | }
|
---|
111 | source->available.base += real_size;
|
---|
112 | source->available.size -= real_size;
|
---|
113 | return EOK;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * @}
|
---|
118 | */
|
---|