1 | /*
|
---|
2 | * Copyright (c) 2013 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 | /**
|
---|
30 | * @addtogroup audio
|
---|
31 | * @brief HelenOS sound server.
|
---|
32 | * @{
|
---|
33 | */
|
---|
34 | /** @file
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <errno.h>
|
---|
38 | #include <hound/protocol.h>
|
---|
39 | #include <malloc.h>
|
---|
40 |
|
---|
41 | #include "hound.h"
|
---|
42 | #include "hound_ctx.h"
|
---|
43 | #include "log.h"
|
---|
44 |
|
---|
45 | static int iface_add_context(void *server, hound_context_id_t *id,
|
---|
46 | const char *name, bool record)
|
---|
47 | {
|
---|
48 | assert(server);
|
---|
49 | assert(id);
|
---|
50 | assert(name);
|
---|
51 |
|
---|
52 | hound_ctx_t *ctx = record ? hound_record_ctx_get(name) :
|
---|
53 | hound_playback_ctx_get(name);
|
---|
54 | if (!ctx)
|
---|
55 | return ENOMEM;
|
---|
56 |
|
---|
57 | const int ret = hound_add_ctx(server, ctx);
|
---|
58 | if (ret != EOK)
|
---|
59 | hound_ctx_destroy(ctx);
|
---|
60 | else
|
---|
61 | *id = hound_ctx_get_id(ctx);
|
---|
62 | return ret;
|
---|
63 | }
|
---|
64 |
|
---|
65 | static int iface_rem_context(void *server, hound_context_id_t id)
|
---|
66 | {
|
---|
67 | assert(server);
|
---|
68 | hound_ctx_t *ctx = hound_get_ctx_by_id(server, id);
|
---|
69 | if (!ctx)
|
---|
70 | return EINVAL;
|
---|
71 | hound_remove_ctx(server, ctx);
|
---|
72 | hound_ctx_destroy(ctx);
|
---|
73 | log_info("%s: %p, %#x", __FUNCTION__, server, id);
|
---|
74 | return EOK;
|
---|
75 | }
|
---|
76 |
|
---|
77 | static bool iface_is_record_context(void *server, hound_context_id_t id)
|
---|
78 | {
|
---|
79 | assert(server);
|
---|
80 | hound_ctx_t *ctx = hound_get_ctx_by_id(server, id);
|
---|
81 | if (!ctx)
|
---|
82 | return false;
|
---|
83 | log_info("%s: %p, %d", __FUNCTION__, server, id);
|
---|
84 | return hound_ctx_is_record(ctx);
|
---|
85 | }
|
---|
86 |
|
---|
87 | static int iface_get_list(void *server, const char ***list, size_t *size,
|
---|
88 | const char *connection, int flags)
|
---|
89 | {
|
---|
90 | log_info("%s: %p, %zu, %s, %#x\n", __FUNCTION__, server, *size,
|
---|
91 | connection, flags);
|
---|
92 | if ((flags & (HOUND_SINK_DEVS | HOUND_SINK_APPS)) != 0)
|
---|
93 | return hound_list_sinks(server, list, size);
|
---|
94 | if ((flags & (HOUND_SOURCE_DEVS | HOUND_SOURCE_APPS)) != 0)
|
---|
95 | return hound_list_sources(server, list, size);
|
---|
96 | return ENOTSUP;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static int iface_connect(void *server, const char *source, const char *sink)
|
---|
100 | {
|
---|
101 | log_info("%s: %p, %s -> %s", __FUNCTION__, server, source, sink);
|
---|
102 | return hound_connect(server, source, sink);
|
---|
103 | }
|
---|
104 |
|
---|
105 | static int iface_disconnect(void *server, const char *source, const char *sink)
|
---|
106 | {
|
---|
107 | log_info("%s: %p, %s -> %s", __FUNCTION__, server, source, sink);
|
---|
108 | return hound_disconnect(server, source, sink);
|
---|
109 | }
|
---|
110 |
|
---|
111 | static int iface_add_stream(void *server, hound_context_id_t id, int flags,
|
---|
112 | pcm_format_t format, size_t size, void **data)
|
---|
113 | {
|
---|
114 | assert(data);
|
---|
115 | assert(server);
|
---|
116 |
|
---|
117 | log_verbose("%s: %p, %d %x ch:%u r:%u f:%s", __FUNCTION__, server, id,
|
---|
118 | flags, format.channels, format.sampling_rate,
|
---|
119 | pcm_sample_format_str(format.sample_format));
|
---|
120 | hound_ctx_t *ctx = hound_get_ctx_by_id(server, id);
|
---|
121 | if (!ctx)
|
---|
122 | return ENOENT;
|
---|
123 | hound_ctx_stream_t *stream =
|
---|
124 | hound_ctx_create_stream(ctx, flags, format, size);
|
---|
125 | if (!stream)
|
---|
126 | return ENOMEM;
|
---|
127 | *data = stream;
|
---|
128 | return EOK;
|
---|
129 | }
|
---|
130 |
|
---|
131 | static int iface_rem_stream(void *server, void *stream)
|
---|
132 | {
|
---|
133 | hound_ctx_destroy_stream(stream);
|
---|
134 | return EOK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static int iface_drain_stream(void *stream)
|
---|
138 | {
|
---|
139 | hound_ctx_stream_drain(stream);
|
---|
140 | return EOK;
|
---|
141 | }
|
---|
142 |
|
---|
143 | static int iface_stream_data_read(void *stream, void *buffer, size_t size)
|
---|
144 | {
|
---|
145 | return hound_ctx_stream_read(stream, buffer, size);
|
---|
146 | }
|
---|
147 |
|
---|
148 | static int iface_stream_data_write(void *stream, const void *buffer, size_t size)
|
---|
149 | {
|
---|
150 | int ret = EOK;
|
---|
151 | do {
|
---|
152 | ret = hound_ctx_stream_write(stream, buffer, size);
|
---|
153 | } while (ret == EBUSY && (async_usleep(100), 1));
|
---|
154 | return ret;
|
---|
155 | }
|
---|
156 |
|
---|
157 | hound_server_iface_t hound_iface = {
|
---|
158 | .add_context = iface_add_context,
|
---|
159 | .rem_context = iface_rem_context,
|
---|
160 | .is_record_context = iface_is_record_context,
|
---|
161 | .get_list = iface_get_list,
|
---|
162 | .connect = iface_connect,
|
---|
163 | .disconnect = iface_disconnect,
|
---|
164 | .add_stream = iface_add_stream,
|
---|
165 | .rem_stream = iface_rem_stream,
|
---|
166 | .drain_stream = iface_drain_stream,
|
---|
167 | .stream_data_write = iface_stream_data_write,
|
---|
168 | .stream_data_read = iface_stream_data_read,
|
---|
169 | .server = NULL,
|
---|
170 | };
|
---|