]> code.delx.au - pulseaudio/blob - src/modules/module-raop-sink.c
Some minor tidyup to remove code now in raop client. Still nowhere near functional.
[pulseaudio] / src / modules / module-raop-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2008 Colin Guthrie
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdlib.h>
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <sys/ioctl.h>
38 #include <poll.h>
39
40 #include <pulse/xmalloc.h>
41
42 #include <pulsecore/core-error.h>
43 #include <pulsecore/sink.h>
44 #include <pulsecore/module.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/modargs.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/thread.h>
49 #include <pulsecore/thread-mq.h>
50 #include <pulsecore/random.h>
51 #include <pulsecore/rtpoll.h>
52
53 #include "rtp.h"
54 #include "sdp.h"
55 #include "sap.h"
56 #include "raop_client.h"
57
58
59 #include "module-raop-sink-symdef.h"
60
61 #define JACK_STATUS_DISCONNECTED 0
62 #define JACK_STATUS_CONNECTED 1
63
64 #define JACK_TYPE_ANALOG 0
65 #define JACK_TYPE_DIGITAL 1
66
67 #define VOLUME_DEF -30
68 #define VOLUME_MIN -144
69 #define VOLUME_MAX 0
70
71
72 PA_MODULE_AUTHOR("Colin Guthrie");
73 PA_MODULE_DESCRIPTION("RAOP Sink (Apple Airport)");
74 PA_MODULE_VERSION(PACKAGE_VERSION);
75 PA_MODULE_LOAD_ONCE(FALSE);
76 PA_MODULE_USAGE(
77 "server=<address> "
78 "sink_name=<name for the sink> "
79 "format=<sample format> "
80 "channels=<number of channels> "
81 "rate=<sample rate>"
82 "channel_map=<channel map>");
83
84 #define DEFAULT_SINK_NAME "airtunes"
85
86 struct userdata {
87 pa_core *core;
88 pa_module *module;
89 pa_sink *sink;
90
91 pa_thread *thread;
92 pa_thread_mq thread_mq;
93 pa_rtpoll *rtpoll;
94
95 char *server_name;
96
97 pa_raop_client *raop;
98 //pa_socket_client *client;
99 pa_memchunk memchunk;
100
101 pa_rtpoll_item *rtpoll_item;
102 };
103
104 static const char* const valid_modargs[] = {
105 "server",
106 "rate",
107 "format",
108 "channels",
109 "sink_name",
110 "channel_map",
111 NULL
112 };
113
114 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
115 struct userdata *u = PA_SINK(o)->userdata;
116
117 switch (code) {
118
119 case PA_SINK_MESSAGE_GET_LATENCY: {
120 size_t n = 0;
121 //int l;
122
123 #ifdef TIOCINQ
124 /*
125 if (ioctl(u->fd, TIOCINQ, &l) >= 0 && l > 0)
126 n = (size_t) l;
127 */
128 #endif
129
130 n += u->memchunk.length;
131
132 *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
133 break;
134 }
135 }
136
137 return pa_sink_process_msg(o, code, data, offset, chunk);
138 }
139
140 static void thread_func(void *userdata) {
141 struct userdata *u = userdata;
142 //int write_type = 0;
143
144 pa_assert(u);
145
146 pa_log_debug("Thread starting up");
147
148 pa_thread_mq_install(&u->thread_mq);
149 pa_rtpoll_install(u->rtpoll);
150
151 for (;;) {
152 struct pollfd *pollfd;
153 int ret;
154
155 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
156
157 /* Render some data and write it to the fifo */
158 if (u->sink->thread_info.state == PA_SINK_RUNNING && pollfd->revents) {
159 ssize_t l;
160 void *p;
161
162 if (u->memchunk.length <= 0)
163 pa_sink_render(u->sink, PIPE_BUF, &u->memchunk);
164
165 pa_assert(u->memchunk.length > 0);
166
167 p = pa_memblock_acquire(u->memchunk.memblock);
168 //l = pa_write(u->fd, (uint8_t*) p + u->memchunk.index, u->memchunk.length, &write_type);
169 // Fake the length of the "write".
170 l = u->memchunk.length;
171 pa_memblock_release(u->memchunk.memblock);
172
173 pa_assert(l != 0);
174
175 if (l < 0) {
176
177 if (errno == EINTR)
178 continue;
179 else if (errno != EAGAIN) {
180 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
181 goto fail;
182 }
183
184 } else {
185
186 u->memchunk.index += l;
187 u->memchunk.length -= l;
188
189 if (u->memchunk.length <= 0) {
190 pa_memblock_unref(u->memchunk.memblock);
191 pa_memchunk_reset(&u->memchunk);
192 }
193
194 pollfd->revents = 0;
195 }
196 }
197
198 /* Hmm, nothing to do. Let's sleep */
199 pollfd->events = u->sink->thread_info.state == PA_SINK_RUNNING ? POLLOUT : 0;
200
201 if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0)
202 goto fail;
203
204 if (ret == 0)
205 goto finish;
206
207 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
208
209 if (pollfd->revents & ~POLLOUT) {
210 pa_log("FIFO shutdown.");
211 goto fail;
212 }
213 }
214
215 fail:
216 /* If this was no regular exit from the loop we have to continue
217 * processing messages until we received PA_MESSAGE_SHUTDOWN */
218 pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
219 pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
220
221 finish:
222 pa_log_debug("Thread shutting down");
223 }
224
225 int pa__init(pa_module*m) {
226 struct userdata *u;
227 //struct stat st;
228 pa_sample_spec ss;
229 pa_channel_map map;
230 pa_modargs *ma;
231 char *t;
232 struct pollfd *pollfd;
233
234 pa_assert(m);
235
236 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
237 pa_log("Failed to parse module arguments.");
238 goto fail;
239 }
240
241 ss = m->core->default_sample_spec;
242 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
243 pa_log("Invalid sample format specification or channel map");
244 goto fail;
245 }
246
247 u = pa_xnew0(struct userdata, 1);
248 u->core = m->core;
249 u->module = m;
250 m->userdata = u;
251
252 pa_memchunk_reset(&u->memchunk);
253 pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
254 u->rtpoll = pa_rtpoll_new();
255 pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
256
257 u->server_name = pa_xstrdup(pa_modargs_get_value(ma, "server", NULL));
258
259 // Open a connection to the server... this is just to connect and test....
260 /*
261 mkfifo(u->filename, 0666);
262 if ((u->fd = open(u->filename, O_RDWR|O_NOCTTY)) < 0) {
263 pa_log("open('%s'): %s", u->filename, pa_cstrerror(errno));
264 goto fail;
265 }
266
267 pa_make_fd_cloexec(u->fd);
268 pa_make_fd_nonblock(u->fd);
269
270 if (fstat(u->fd, &st) < 0) {
271 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
272 goto fail;
273 }
274
275 if (!S_ISFIFO(st.st_mode)) {
276 pa_log("'%s' is not a FIFO.", u->filename);
277 goto fail;
278 }
279 */
280 if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
281 pa_log("Failed to create sink.");
282 goto fail;
283 }
284
285 u->sink->parent.process_msg = sink_process_msg;
286 u->sink->userdata = u;
287 u->sink->flags = PA_SINK_LATENCY;
288
289 pa_sink_set_module(u->sink, m);
290 pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
291 pa_sink_set_rtpoll(u->sink, u->rtpoll);
292 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("Airtunes sink '%s'", u->server_name));
293 pa_xfree(t);
294
295 u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1);
296 pollfd = pa_rtpoll_item_get_pollfd(u->rtpoll_item, NULL);
297 //pollfd->fd = u->fd;
298 pollfd->events = pollfd->revents = 0;
299
300 if (!(u->thread = pa_thread_new(thread_func, u))) {
301 pa_log("Failed to create thread.");
302 goto fail;
303 }
304
305 pa_sink_put(u->sink);
306
307 pa_modargs_free(ma);
308
309 return 0;
310
311 fail:
312 if (ma)
313 pa_modargs_free(ma);
314
315 pa__done(m);
316
317 return -1;
318 }
319
320 void pa__done(pa_module*m) {
321 struct userdata *u;
322
323 pa_assert(m);
324
325 if (!(u = m->userdata))
326 return;
327
328 if (u->sink)
329 pa_sink_unlink(u->sink);
330
331 if (u->thread) {
332 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
333 pa_thread_free(u->thread);
334 }
335
336 pa_thread_mq_done(&u->thread_mq);
337
338 if (u->sink)
339 pa_sink_unref(u->sink);
340
341 if (u->memchunk.memblock)
342 pa_memblock_unref(u->memchunk.memblock);
343
344 if (u->rtpoll_item)
345 pa_rtpoll_item_free(u->rtpoll_item);
346
347 if (u->rtpoll)
348 pa_rtpoll_free(u->rtpoll);
349
350 pa_xfree(u->server_name);
351 pa_xfree(u);
352 }