4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2008 Colin Guthrie
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.
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.
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
37 #include <sys/ioctl.h>
40 #include <pulse/xmalloc.h>
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>
56 #include "raop_client.h"
59 #include "module-raop-sink-symdef.h"
61 #define JACK_STATUS_DISCONNECTED 0
62 #define JACK_STATUS_CONNECTED 1
64 #define JACK_TYPE_ANALOG 0
65 #define JACK_TYPE_DIGITAL 1
67 #define VOLUME_DEF -30
68 #define VOLUME_MIN -144
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
);
78 "sink_name=<name for the sink> "
79 "format=<sample format> "
80 "channels=<number of channels> "
82 "channel_map=<channel map>");
84 #define DEFAULT_SINK_NAME "airtunes"
92 pa_thread_mq thread_mq
;
98 //pa_socket_client *client;
101 pa_rtpoll_item
*rtpoll_item
;
104 static const char* const valid_modargs
[] = {
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
;
119 case PA_SINK_MESSAGE_GET_LATENCY
: {
125 if (ioctl(u->fd, TIOCINQ, &l) >= 0 && l > 0)
130 n
+= u
->memchunk
.length
;
132 *((pa_usec_t
*) data
) = pa_bytes_to_usec(n
, &u
->sink
->sample_spec
);
137 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
140 static void thread_func(void *userdata
) {
141 struct userdata
*u
= userdata
;
142 //int write_type = 0;
146 pa_log_debug("Thread starting up");
148 pa_thread_mq_install(&u
->thread_mq
);
149 pa_rtpoll_install(u
->rtpoll
);
152 struct pollfd
*pollfd
;
155 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
157 /* Render some data and write it to the fifo */
158 if (u
->sink
->thread_info
.state
== PA_SINK_RUNNING
&& pollfd
->revents
) {
162 if (u
->memchunk
.length
<= 0)
163 pa_sink_render(u
->sink
, PIPE_BUF
, &u
->memchunk
);
165 pa_assert(u
->memchunk
.length
> 0);
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
);
179 else if (errno
!= EAGAIN
) {
180 pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno
));
186 u
->memchunk
.index
+= l
;
187 u
->memchunk
.length
-= l
;
189 if (u
->memchunk
.length
<= 0) {
190 pa_memblock_unref(u
->memchunk
.memblock
);
191 pa_memchunk_reset(&u
->memchunk
);
198 /* Hmm, nothing to do. Let's sleep */
199 pollfd
->events
= u
->sink
->thread_info
.state
== PA_SINK_RUNNING
? POLLOUT
: 0;
201 if ((ret
= pa_rtpoll_run(u
->rtpoll
, TRUE
)) < 0)
207 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
209 if (pollfd
->revents
& ~POLLOUT
) {
210 pa_log("FIFO shutdown.");
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
);
222 pa_log_debug("Thread shutting down");
225 int pa__init(pa_module
*m
) {
232 struct pollfd
*pollfd
;
236 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
237 pa_log("Failed to parse module arguments.");
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");
247 u
= pa_xnew0(struct userdata
, 1);
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
);
257 u
->server_name
= pa_xstrdup(pa_modargs_get_value(ma
, "server", NULL
));
259 // Open a connection to the server... this is just to connect and test....
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));
267 pa_make_fd_cloexec(u->fd);
268 pa_make_fd_nonblock(u->fd);
270 if (fstat(u->fd, &st) < 0) {
271 pa_log("fstat('%s'): %s", u->filename, pa_cstrerror(errno));
275 if (!S_ISFIFO(st.st_mode)) {
276 pa_log("'%s' is not a FIFO.", u->filename);
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.");
285 u
->sink
->parent
.process_msg
= sink_process_msg
;
286 u
->sink
->userdata
= u
;
287 u
->sink
->flags
= PA_SINK_LATENCY
;
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
));
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;
300 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
301 pa_log("Failed to create thread.");
305 pa_sink_put(u
->sink
);
320 void pa__done(pa_module
*m
) {
325 if (!(u
= m
->userdata
))
329 pa_sink_unlink(u
->sink
);
332 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
333 pa_thread_free(u
->thread
);
336 pa_thread_mq_done(&u
->thread_mq
);
339 pa_sink_unref(u
->sink
);
341 if (u
->memchunk
.memblock
)
342 pa_memblock_unref(u
->memchunk
.memblock
);
345 pa_rtpoll_item_free(u
->rtpoll_item
);
348 pa_rtpoll_free(u
->rtpoll
);
350 pa_xfree(u
->server_name
);