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>
47 #include <pulsecore/thread-mq.h>
48 #include <pulsecore/rtpoll.h>
50 #include "module-pipe-source-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("UNIX pipe source");
54 PA_MODULE_VERSION(PACKAGE_VERSION
);
55 PA_MODULE_LOAD_ONCE(FALSE
);
57 "source_name=<name for the source> "
58 "file=<path of the FIFO> "
59 "format=<sample format> "
60 "channels=<number of channels> "
62 "channel_map=<channel map>");
64 #define DEFAULT_FILE_NAME "/tmp/music.input"
65 #define DEFAULT_SOURCE_NAME "fifo_input"
73 pa_thread_mq thread_mq
;
81 pa_rtpoll_item
*rtpoll_item
;
84 static const char* const valid_modargs
[] = {
94 static void thread_func(void *userdata
) {
95 struct userdata
*u
= userdata
;
100 pa_log_debug("Thread starting up");
102 pa_thread_mq_install(&u
->thread_mq
);
103 pa_rtpoll_install(u
->rtpoll
);
107 struct pollfd
*pollfd
;
109 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
111 /* Try to read some data and pass it on to the source driver */
112 if (u
->source
->thread_info
.state
== PA_SOURCE_RUNNING
&& pollfd
->revents
) {
116 if (!u
->memchunk
.memblock
) {
117 u
->memchunk
.memblock
= pa_memblock_new(u
->core
->mempool
, PIPE_BUF
);
118 u
->memchunk
.index
= u
->memchunk
.length
= 0;
121 pa_assert(pa_memblock_get_length(u
->memchunk
.memblock
) > u
->memchunk
.index
);
123 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
124 l
= pa_read(u
->fd
, (uint8_t*) p
+ u
->memchunk
.index
, pa_memblock_get_length(u
->memchunk
.memblock
) - u
->memchunk
.index
, &read_type
);
125 pa_memblock_release(u
->memchunk
.memblock
);
127 pa_assert(l
!= 0); /* EOF cannot happen, since we opened the fifo for both reading and writing */
133 else if (errno
!= EAGAIN
) {
134 pa_log("Faile to read data from FIFO: %s", pa_cstrerror(errno
));
140 u
->memchunk
.length
= l
;
141 pa_source_post(u
->source
, &u
->memchunk
);
142 u
->memchunk
.index
+= l
;
144 if (u
->memchunk
.index
>= pa_memblock_get_length(u
->memchunk
.memblock
)) {
145 pa_memblock_unref(u
->memchunk
.memblock
);
146 pa_memchunk_reset(&u
->memchunk
);
153 /* Hmm, nothing to do. Let's sleep */
154 pollfd
->events
= u
->source
->thread_info
.state
== PA_SOURCE_RUNNING
? POLLIN
: 0;
156 if ((ret
= pa_rtpoll_run(u
->rtpoll
, 1)) < 0)
162 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
163 if (pollfd
->revents
& ~POLLIN
) {
164 pa_log("FIFO shutdown.");
170 /* If this was no regular exit from the loop we have to continue
171 * processing messages until we received PA_MESSAGE_SHUTDOWN */
172 pa_asyncmsgq_post(u
->thread_mq
.outq
, PA_MSGOBJECT(u
->core
), PA_CORE_MESSAGE_UNLOAD_MODULE
, u
->module
, 0, NULL
, NULL
);
173 pa_asyncmsgq_wait_for(u
->thread_mq
.inq
, PA_MESSAGE_SHUTDOWN
);
176 pa_log_debug("Thread shutting down");
179 int pa__init(pa_module
*m
) {
186 struct pollfd
*pollfd
;
190 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
191 pa_log("failed to parse module arguments.");
195 ss
= m
->core
->default_sample_spec
;
196 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
197 pa_log("invalid sample format specification or channel map");
201 u
= pa_xnew0(struct userdata
, 1);
205 pa_memchunk_reset(&u
->memchunk
);
206 pa_thread_mq_init(&u
->thread_mq
, m
->core
->mainloop
);
207 u
->rtpoll
= pa_rtpoll_new();
208 pa_rtpoll_item_new_asyncmsgq(u
->rtpoll
, PA_RTPOLL_EARLY
, u
->thread_mq
.inq
);
210 u
->filename
= pa_xstrdup(pa_modargs_get_value(ma
, "file", DEFAULT_FILE_NAME
));
212 mkfifo(u
->filename
, 0666);
213 if ((u
->fd
= open(u
->filename
, O_RDWR
|O_NOCTTY
)) < 0) {
214 pa_log("open('%s'): %s", u
->filename
, pa_cstrerror(errno
));
218 pa_make_fd_cloexec(u
->fd
);
219 pa_make_fd_nonblock(u
->fd
);
221 if (fstat(u
->fd
, &st
) < 0) {
222 pa_log("fstat('%s'): %s",u
->filename
, pa_cstrerror(errno
));
226 if (!S_ISFIFO(st
.st_mode
)) {
227 pa_log("'%s' is not a FIFO.", u
->filename
);
231 if (!(u
->source
= pa_source_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "source_name", DEFAULT_SOURCE_NAME
), 0, &ss
, &map
))) {
232 pa_log("Failed to create source.");
236 u
->source
->userdata
= u
;
237 u
->source
->flags
= 0;
239 pa_source_set_module(u
->source
, m
);
240 pa_source_set_asyncmsgq(u
->source
, u
->thread_mq
.inq
);
241 pa_source_set_rtpoll(u
->source
, u
->rtpoll
);
242 pa_source_set_description(u
->source
, t
= pa_sprintf_malloc("Unix FIFO source '%s'", u
->filename
));
245 u
->rtpoll_item
= pa_rtpoll_item_new(u
->rtpoll
, PA_RTPOLL_NEVER
, 1);
246 pollfd
= pa_rtpoll_item_get_pollfd(u
->rtpoll_item
, NULL
);
248 pollfd
->events
= pollfd
->revents
= 0;
250 if (!(u
->thread
= pa_thread_new(thread_func
, u
))) {
251 pa_log("Failed to create thread.");
255 pa_source_put(u
->source
);
270 void pa__done(pa_module
*m
) {
275 if (!(u
= m
->userdata
))
279 pa_source_unlink(u
->source
);
282 pa_asyncmsgq_send(u
->thread_mq
.inq
, NULL
, PA_MESSAGE_SHUTDOWN
, NULL
, 0, NULL
);
283 pa_thread_free(u
->thread
);
286 pa_thread_mq_done(&u
->thread_mq
);
289 pa_source_unref(u
->source
);
291 if (u
->memchunk
.memblock
)
292 pa_memblock_unref(u
->memchunk
.memblock
);
295 pa_rtpoll_item_free(u
->rtpoll_item
);
298 pa_rtpoll_free(u
->rtpoll
);
302 pa_xfree(u
->filename
);
306 pa_assert_se(pa_close(u
->fd
) == 0);