1 /* $Id: module-esound-sink.c 2043 2007-11-09 18:25:40Z lennart $ */
4 This file is part of PulseAudio.
6 Copyright 2008 Colin Guthrie
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <sys/ioctl.h>
42 #ifdef HAVE_LINUX_SOCKIOS_H
43 #include <linux/sockios.h>
46 #include <pulse/xmalloc.h>
47 #include <pulse/timeval.h>
49 #include <pulsecore/core-error.h>
50 #include <pulsecore/iochannel.h>
51 #include <pulsecore/sink.h>
52 #include <pulsecore/module.h>
53 #include <pulsecore/core-util.h>
54 #include <pulsecore/modargs.h>
55 #include <pulsecore/log.h>
56 #include <pulsecore/socket-client.h>
57 #include <pulsecore/authkey.h>
58 #include <pulsecore/thread-mq.h>
59 #include <pulsecore/thread.h>
60 #include <pulsecore/time-smoother.h>
61 #include <pulsecore/rtclock.h>
62 #include <pulsecore/socket-util.h>
64 #include "module-raop-sink-symdef.h"
68 #include "raop_client.h"
70 PA_MODULE_AUTHOR("Colin Guthrie");
71 PA_MODULE_DESCRIPTION("RAOP Sink (Apple Airtunes)");
72 PA_MODULE_VERSION(PACKAGE_VERSION
);
73 PA_MODULE_LOAD_ONCE(FALSE
);
75 "sink_name=<name for the sink> "
76 "server=<address> cookie=<filename> "
77 "format=<sample format> "
78 "channels=<number of channels> "
79 "rate=<sample rate>");
81 #define DEFAULT_SINK_NAME "airtunes"
88 pa_thread_mq thread_mq
;
90 pa_rtpoll_item
*rtpoll_item
;
93 pa_memchunk raw_memchunk
;
94 pa_memchunk encoded_memchunk
;
97 size_t write_length
, write_index
;
100 size_t read_length
, read_index
;
104 /*esd_format_t format;*/
107 pa_smoother
*smoother
;
111 int64_t encoding_overhead
;
112 int32_t next_encoding_overhead
;
113 double encoding_ratio
;
115 pa_raop_client
*raop
;
120 static const char* const valid_modargs
[] = {
130 SINK_MESSAGE_PASS_SOCKET
= PA_SINK_MESSAGE_MAX
133 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
134 struct userdata
*u
= PA_SINK(o
)->userdata
;
138 case PA_SINK_MESSAGE_SET_STATE
:
140 switch ((pa_sink_state_t
) PA_PTR_TO_UINT(data
)) {
142 case PA_SINK_SUSPENDED
:
143 pa_assert(PA_SINK_OPENED(u
->sink
->thread_info
.state
));
145 pa_smoother_pause(u
->smoother
, pa_rtclock_usec());
149 case PA_SINK_RUNNING
:
151 if (u
->sink
->thread_info
.state
== PA_SINK_SUSPENDED
)
152 pa_smoother_resume(u
->smoother
, pa_rtclock_usec());
156 case PA_SINK_UNLINKED
:
163 case PA_SINK_MESSAGE_GET_LATENCY
: {
166 r
= pa_smoother_get(u
->smoother
, pa_rtclock_usec());
167 w
= pa_bytes_to_usec((u
->offset
- u
->encoding_overhead
+ (u
->encoded_memchunk
.length
/ u
->encoding_ratio
)), &u
->sink
->sample_spec
);
169 *((pa_usec_t
*) data
) = w
> r
? w
- r
: 0;
173 case SINK_MESSAGE_PASS_SOCKET
: {
174 struct pollfd
*pollfd
;
176 pa_assert(!u
->rtpoll_item
);
178 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
179 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
181 pollfd
->events
= pollfd
->revents
= 0;
187 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
190 static void thread_func(void *userdata
) {
191 struct userdata
*u
= userdata
;
196 pa_log_debug("Thread starting up");
198 pa_thread_mq_install(&u
->thread_mq
);
199 pa_rtpoll_install(u
->rtpoll
);
201 pa_smoother_set_time_offset(u
->smoother
, pa_rtclock_usec());
206 if (u
->rtpoll_item
) {
207 struct pollfd
*pollfd
;
208 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
210 /* Render some data and write it to the fifo */
211 if (PA_SINK_OPENED(u
->sink
->thread_info
.state
) && pollfd
->revents
) {
219 if (u
->raw_memchunk
.length
<= 0) {
220 /* Grab unencoded data */
221 pa_sink_render(u
->sink
, u
->block_size
, &u
->raw_memchunk
);
223 pa_assert(u
->raw_memchunk
.length
> 0);
225 if (u
->encoded_memchunk
.length
<= 0) {
227 size_t rl
= u
->raw_memchunk
.length
;
228 u
->encoding_overhead
+= u
->next_encoding_overhead
;
229 u
->encoded_memchunk
= pa_raop_client_encode_sample(u
->raop
, &u
->raw_memchunk
);
230 u
->next_encoding_overhead
= (u
->encoded_memchunk
.length
- (rl
- u
->raw_memchunk
.length
));
231 u
->encoding_ratio
= u
->encoded_memchunk
.length
/ (rl
- u
->raw_memchunk
.length
);
233 pa_assert(u
->encoded_memchunk
.length
> 0);
235 p
= pa_memblock_acquire(u
->encoded_memchunk
.memblock
);
236 l
= pa_write(u
->fd
, (uint8_t*) p
+ u
->encoded_memchunk
.index
, u
->encoded_memchunk
.length
, &write_type
);
237 pa_memblock_release(u
->encoded_memchunk
.memblock
);
245 else if (errno
== EAGAIN
) {
247 /* OK, we filled all socket buffers up
252 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
259 u
->encoded_memchunk
.index
+= l
;
260 u
->encoded_memchunk
.length
-= l
;
264 if (u
->encoded_memchunk
.length
> 0)
266 /* OK, we wrote less that we asked for,
267 * hence we can assume that the socket
268 * buffers are full now */
275 /* At this spot we know that the socket buffers are
276 * fully filled up. This is the best time to estimate
277 * the playback position of the server */
284 if (ioctl(u
->fd
, SIOCOUTQ
, &l
) >= 0 && l
> 0)
289 usec
= pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
291 if (usec
> u
->latency
)
296 pa_smoother_put(u
->smoother
, pa_rtclock_usec(), usec
);
299 /* Hmm, nothing to do. Let's sleep */
300 pollfd
->events
= PA_SINK_OPENED(u
->sink
->thread_info
.state
) ? POLLOUT
: 0;
303 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
309 if (u
->rtpoll_item
) {
310 struct pollfd
* pollfd
;
312 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
314 if (pollfd
->revents
& ~POLLOUT
) {
315 pa_log("FIFO shutdown.");
322 /* If this was no regular exit from the loop we have to continue
323 * processing messages until we received PA_MESSAGE_SHUTDOWN */
324 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
325 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
328 pa_log_debug("Thread shutting down");
331 static void on_connection(PA_GCC_UNUSED
int fd
, void*userdata
) {
332 struct userdata
*u
= userdata
;
335 pa_assert(u
->fd
< 0);
338 pa_log_debug("Connection authenticated, handing fd to IO thread...");
340 pa_asyncmsgq_post(u
->thread_mq
.inq
, PA_MSGOBJECT(u
->sink
), SINK_MESSAGE_PASS_SOCKET
, NULL
, 0, NULL
, NULL
);
343 static void on_close(void*userdata
) {
344 struct userdata
*u
= userdata
;
347 pa_log_debug("Control connection closed.");
348 pa_module_unload_request(u
->module
);
351 int pa__init(pa_module
*m
) {
352 struct userdata
*u
= NULL
;
355 pa_modargs
*ma
= NULL
;
360 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
361 pa_log("failed to parse module arguments");
365 ss
= m
->core
->default_sample_spec
;
366 if (pa_modargs_get_sample_spec(ma
, &ss
) < 0) {
367 pa_log("invalid sample format specification");
371 if ((/*ss.format != PA_SAMPLE_U8 &&*/ ss
.format
!= PA_SAMPLE_S16NE
) ||
373 pa_log("sample type support is limited to mono/stereo and U8 or S16NE sample data");
377 u
= pa_xnew0(struct userdata
, 1);
382 u
->smoother
= pa_smoother_new(PA_USEC_PER_SEC
, PA_USEC_PER_SEC
*2, TRUE
);
383 pa_memchunk_reset(&u
->raw_memchunk
);
384 pa_memchunk_reset(&u
->encoded_memchunk
);
386 u
->encoding_overhead
= 0;
387 u
->next_encoding_overhead
= 0;
388 u
->encoding_ratio
= 1.0;
390 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
391 u
->rtpoll
= pa_rtpoll_new();
392 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
393 u
->rtpoll_item
= NULL
;
396 (ss.format == PA_SAMPLE_U8 ? ESD_BITS8 : ESD_BITS16) |
397 (ss.channels == 2 ? ESD_STEREO : ESD_MONO);*/
399 u
->block_size
= pa_usec_to_bytes(PA_USEC_PER_SEC
/20, &ss
);
401 u
->read_data
= u
->write_data
= NULL
;
402 u
->read_index
= u
->write_index
= u
->read_length
= u
->write_length
= 0;
404 /*u->state = STATE_AUTH;*/
407 if (!(u
->sink
= pa_sink_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", DEFAULT_SINK_NAME
), 0, &ss
, NULL
))) {
408 pa_log("Failed to create sink.");
412 u
->sink
->parent
.process_msg
= sink_process_msg
;
413 u
->sink
->userdata
= u
;
414 u
->sink
->flags
= PA_SINK_LATENCY
|PA_SINK_NETWORK
;
416 pa_sink_set_module(u
->sink
, m
);
417 pa_sink_set_asyncmsgq(u
->sink
, u
->thread_mq
.inq
);
418 pa_sink_set_rtpoll(u
->sink
, u
->rtpoll
);
420 if (!(p
= pa_modargs_get_value(ma
, "server", NULL
))) {
421 pa_log("No server argument given.");
425 if (!(u
->raop
= pa_raop_client_new(u
->core
, p
))) {
426 pa_log("Failed to connect to server.");
430 pa_raop_client_set_callback(u
->raop
, on_connection
, u
);
431 pa_raop_client_set_closed_callback(u
->raop
, on_close
, u
);
432 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("Airtunes sink '%s'", p
));
436 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
437 pa_log("Failed to create thread.");
441 pa_sink_put(u
->sink
);
456 void pa__done(pa_module
*m
) {
460 if (!(u
= m
->userdata
))
464 pa_sink_unlink(u
->sink
);
467 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
468 pa_thread_free(u
->thread
);
471 pa_thread_mq_done(&u
->thread_mq
);
474 pa_sink_unref(u
->sink
);
477 pa_rtpoll_item_free(u
->rtpoll_item
);
480 pa_rtpoll_free(u
->rtpoll
);
482 if (u
->raw_memchunk
.memblock
)
483 pa_memblock_unref(u
->raw_memchunk
.memblock
);
486 pa_raop_client_free(u
->raop
);
488 pa_xfree(u
->read_data
);
489 pa_xfree(u
->write_data
);
492 pa_smoother_free(u
->smoother
);