4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/source.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/thread.h>
48 #include "module-pipe-source-symdef.h"
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("UNIX pipe source")
52 PA_MODULE_VERSION(PACKAGE_VERSION
)
54 "source_name=<name for the source> "
55 "file=<path of the FIFO> "
56 "format=<sample format> "
57 "channels=<number of channels> "
59 "channel_map=<channel map>")
61 #define DEFAULT_FILE_NAME "/tmp/music.input"
62 #define DEFAULT_SOURCE_NAME "fifo_input"
69 pa_asyncmsgq
*asyncmsgq
;
77 static const char* const valid_modargs
[] = {
87 static void thread_func(void *userdata
) {
94 struct userdata
*u
= userdata
;
95 struct pollfd pollfd
[POLLFD_MAX
];
100 pa_log_debug("Thread starting up");
102 pa_memchunk_reset(&u
->memchunk
);
104 memset(&pollfd
, 0, sizeof(pollfd
));
106 pollfd
[POLLFD_ASYNCQ
].fd
= pa_asyncmsgq_get_fd(u
->asyncmsgq
);
107 pollfd
[POLLFD_ASYNCQ
].events
= POLLIN
;
108 pollfd
[POLLFD_FIFO
].fd
= u
->fd
;
111 pa_msgobject
*object
;
117 /* Check whether there is a message for us to process */
118 if (pa_asyncmsgq_get(u
->asyncmsgq
, &object
, &code
, &data
, &chunk
, 0) == 0) {
121 if (!object
&& code
== PA_MESSAGE_SHUTDOWN
) {
122 pa_asyncmsgq_done(u
->asyncmsgq
, 0);
126 ret
= pa_asyncmsgq_dispatch(object
, code
, data
, &chunk
);
127 pa_asyncmsgq_done(u
->asyncmsgq
, ret
);
131 /* Render some data and write it to the fifo */
133 if (u
->source
->thread_info
.state
== PA_SOURCE_RUNNING
&& pollfd
[POLLFD_FIFO
].revents
) {
137 if (!u
->memchunk
.memblock
) {
138 u
->memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, PIPE_BUF
);
139 u
->memchunk
.index
= u
->memchunk
.length
= 0;
142 pa_assert(pa_memblock_get_length(u
->memchunk
.memblock
) > u
->memchunk
.index
);
144 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
145 l
= pa_read(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, pa_memblock_get_length(u
->memchunk
.memblock
) - u
->memchunk
.index
, &read_type
);
146 pa_memblock_release(u
->memchunk
.memblock
);
148 pa_assert(l
!= 0); /* EOF cannot happen, since we opened the fifo for both reading and writing */
154 else if (errno
!= EAGAIN
) {
155 pa_log("Faile to read data from FIFO: %s", pa_cstrerror(errno
));
161 u
->memchunk
.length
= l
;
162 pa_source_post(u
->source
, &u
->memchunk
);
163 u
->memchunk
.index
+= l
;
165 if (u
->memchunk
.index
>= pa_memblock_get_length(u
->memchunk
.memblock
)) {
166 pa_memblock_unref(u
->memchunk
.memblock
);
167 pa_memchunk_reset(&u
->memchunk
);
170 pollfd
[POLLFD_FIFO
].revents
= 0;
175 pollfd
[POLLFD_FIFO
].events
= u
->source
->thread_info
.state
== PA_SINK_RUNNING
? POLLIN
: 0;
177 /* Hmm, nothing to do. Let's sleep */
179 if (pa_asyncmsgq_before_poll(u
->asyncmsgq
) < 0)
182 /* pa_log("polling for %i", pollfd[POLLFD_FIFO].events); */
183 r
= poll(pollfd
, POLLFD_MAX
, -1);
184 /* pa_log("polling got %i (r=%i) %i", r > 0 ? pollfd[POLLFD_FIFO].revents : 0, r, r > 0 ? pollfd[POLLFD_ASYNCQ].revents: 0); */
186 pa_asyncmsgq_after_poll(u
->asyncmsgq
);
192 pa_log("poll() failed: %s", pa_cstrerror(errno
));
196 if (pollfd
[POLLFD_FIFO
].revents
& ~POLLIN
) {
197 pa_log("FIFO shutdown.");
201 pa_assert((pollfd
[POLLFD_ASYNCQ
].revents
& ~POLLIN
) == 0);
205 /* We have to continue processing messages until we receive the
206 * SHUTDOWN message */
207 pa_asyncmsgq_post(u
->core
->asyncmsgq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, NULL
, NULL
);
208 pa_asyncmsgq_wait_for(u
->asyncmsgq
, PA_MESSAGE_SHUTDOWN
);
211 pa_log_debug("Thread shutting down");
214 int pa__init(pa_core
*c
, pa_module
*m
) {
225 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
226 pa_log("failed to parse module arguments.");
230 ss
= c
->default_sample_spec
;
231 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
232 pa_log("invalid sample format specification or channel map");
236 u
= pa_xnew0(struct userdata
, 1);
241 pa_assert_se(u
->asyncmsgq
= pa_asyncmsgq_new(0));
243 u
->filename
= pa_xstrdup(pa_modargs_get_value(ma
, "file", DEFAULT_FILE_NAME
));
245 mkfifo(u
->filename
, 0666);
246 if ((u
->fd
= open(u
->filename
, O_RDWR
|O_NOCTTY
)) < 0) {
247 pa_log("open('%s'): %s", u
->filename
, pa_cstrerror(errno
));
251 pa_fd_set_cloexec(u
->fd
, 1);
252 pa_make_nonblock_fd(u
->fd
);
254 if (fstat(u
->fd
, &st
) < 0) {
255 pa_log("fstat('%s'): %s",u
->filename
, pa_cstrerror(errno
));
259 if (!S_ISFIFO(st
.st_mode
)) {
260 pa_log("'%s' is not a FIFO.", u
->filename
);
264 if (!(u
->source
= pa_source_new(c
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &ss
, &map
))) {
265 pa_log("Failed to create source.");
269 u
->source
->userdata
= u
;
271 pa_source_set_module(u
->source
, m
);
272 pa_source_set_asyncmsgq(u
->source
, u
->asyncmsgq
);
273 pa_source_set_description(u
->source
, t
= pa_sprintf_malloc("Unix FIFO source '%s'", u
->filename
));
276 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
277 pa_log("Failed to create thread.");
294 void pa__done(pa_core
*c
, pa_module
*m
) {
300 if (!(u
= m
->userdata
))
304 pa_source_disconnect(u
->source
);
307 pa_asyncmsgq_send(u
->asyncmsgq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, NULL
);
308 pa_thread_free(u
->thread
);
312 pa_asyncmsgq_free(u
->asyncmsgq
);
315 pa_source_unref(u
->source
);
317 if (u
->memchunk
.memblock
)
318 pa_memblock_unref(u
->memchunk
.memblock
);
322 pa_xfree(u
->filename
);