Merge HUGE set of changes temporarily into a branch, to allow me to move them from...
[profile/ivi/pulseaudio-panda.git] / src / modules / module-null-sink.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <sys/stat.h>
30 #include <stdio.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <limits.h>
36
37 #include <pulse/timeval.h>
38 #include <pulse/xmalloc.h>
39
40 #include <pulsecore/macro.h>
41 #include <pulsecore/iochannel.h>
42 #include <pulsecore/sink.h>
43 #include <pulsecore/module.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47
48 #include "module-null-sink-symdef.h"
49
50 PA_MODULE_AUTHOR("Lennart Poettering")
51 PA_MODULE_DESCRIPTION("Clocked NULL sink")
52 PA_MODULE_VERSION(PACKAGE_VERSION)
53 PA_MODULE_USAGE(
54         "format=<sample format> "
55         "channels=<number of channels> "
56         "rate=<sample rate> "
57         "sink_name=<name of sink>"
58         "channel_map=<channel map>"
59         "description=<description for the sink>")
60
61 #define DEFAULT_SINK_NAME "null"
62
63 struct userdata {
64     pa_core *core;
65     pa_module *module;
66     pa_sink *sink;
67     pa_thread *thread;
68     size_t block_size;
69     struct timeval timestamp;
70 };
71
72 static const char* const valid_modargs[] = {
73     "rate",
74     "format",
75     "channels",
76     "sink_name",
77     "channel_map",
78     "description",
79     NULL
80 };
81
82 static void thread_func(void *userdata) {
83     struct userdata *u = userdata;
84     int quit = 0;
85     struct pollfd pollfd;
86     int running = 1;
87
88     pa_assert(u);
89
90     pa_log_debug("Thread starting up");
91
92     memset(&pollfd, 0, sizeof(pollfd));
93     pollfd.fd = pa_asyncmsgq_get_fd(u->sink->asyncmsgq, PA_ASYNCQ_POP);
94     pollfd.events = POLLIN;
95
96     pa_gettimeofday(u->timestamp);
97     
98     for (;;) {
99         int code;
100         void *data, *object;
101         int r, timeout;
102         struct timeval now;
103
104         /* Check whether there is a message for us to process */
105         if (pa_asyncmsgq_get(u->sink->asyncmsgq, &object, &code, &data) == 0) {
106
107
108             /* Now process these messages our own way */
109             if (!object) {
110
111                 switch (code) {
112                     case PA_MESSAGE_SHUTDOWN:
113                         goto finish;
114
115                     default:
116                         pa_sink_process_msg(u->sink->asyncmsgq, object, code, data);
117
118                 }
119                 
120             } else if (object == u->sink) {
121
122                 switch (code) {
123                     case PA_SINK_MESSAGE_STOP:
124                         pa_assert(running);
125                         running = 0;
126                         break;
127                         
128                     case PA_SINK_MESSAGE_START:
129                         pa_assert(!running);
130                         running = 1;
131                         
132                         pa_gettimeofday(u->timestamp);
133                         break;
134                         
135                     case PA_SINK_MESSAGE_GET_LATENCY:
136                         
137                         if (pa_timeval_cmp(&u->timestamp, &now) > 0)
138                             *((pa_usec_t*) data) = 0;
139                         else
140                             *((pa_usec_t*) data) = pa_timeval_diff(&u->timestamp, &now);
141                         break;
142                         
143                         /* ... */
144
145                     default:
146                         pa_sink_process_msg(u->sink->asyncmsgq, object, code, data);
147                 }
148             }
149             
150             pa_asyncmsgq_done(u->sink->asyncmsgq);
151             continue;
152         }
153
154         /* Render some data and drop it immediately */
155
156         if (running) {
157             pa_gettimeofday(&now);
158             
159             if (pa_timeval_cmp(u->timestamp, &now) <= 0) {
160                 pa_memchunk chunk;
161                 size_t l;
162                 
163                 if (pa_sink_render(u->sink, u->block_size, &chunk) >= 0) {
164                     l = chunk.length;
165                     pa_memblock_unref(chunk.memblock);
166                 } else
167                     l = u->block_size;
168                 
169                 pa_timeval_add(&u->timestamp, pa_bytes_to_usec(l, &u->sink->sample_spec));
170                 continue;
171             }
172
173             timeout = pa_timeval_diff(&u->timestamp, &now)/1000;
174             
175             if (timeout < 1)
176                 timeout = 1;
177         } else
178             timeout = -1;
179
180         /* Hmm, nothing to do. Let's sleep */
181         
182         if (pa_asyncmsgq_before_poll(u->sink->asyncmsgq) < 0)
183             continue;
184
185         r = poll(&pollfd, 1, timeout);
186         pa_asyncmsgq_after_poll(u->sink->asyncmsgq);
187
188         if (r < 0) {
189             if (errno == EINTR)
190                 continue;
191
192             pa_log("poll() failed: %s", pa_cstrerror(errno));
193             goto fail;
194         }
195         
196         pa_assert(r == 0 || pollfd.revents == POLLIN);
197     }
198
199 fail:
200     /* We have to continue processing messages until we receive the
201      * SHUTDOWN message */
202     pa_asyncmsgq_post(u->core->asyncmsgq, u->core, PA_CORE_MESSAGE_UNLOAD_MODULE, pa_module_ref(u->module), NULL, pa_module_unref);
203     pa_asyncmsgq_wait_for(PA_MESSAGE_SHUTDOWN);
204
205 finish:
206     pa_log_debug("Thread shutting down");
207 }
208
209 int pa__init(pa_core *c, pa_module*m) {
210     struct userdata *u = NULL;
211     pa_sample_spec ss;
212     pa_channel_map map;
213     pa_modargs *ma = NULL;
214
215     pa_assert(c);
216     pa_assert(m);
217
218     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
219         pa_log("Failed to parse module arguments.");
220         goto fail;
221     }
222
223     ss = c->default_sample_spec;
224     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
225         pa_log("Invalid sample format specification or channel map");
226         goto fail;
227     }
228
229     u = pa_xnew0(struct userdata, 1);
230     u->core = c;
231     u->module = m;
232     m->userdata = u;
233
234     if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
235         pa_log("Failed to create sink.");
236         goto fail;
237     }
238
239     u->sink->userdata = u;
240     pa_sink_set_owner(u->sink, m);
241     pa_sink_set_description(u->sink, pa_modargs_get_value(ma, "description", "NULL sink"));
242
243     u->block_size = pa_bytes_per_second(&ss) / 20; /* 50 ms */
244     
245     if (u->block_size <= 0)
246         u->block_size = pa_frame_size(&ss);
247
248     if (!(u->thread = pa_thread_new(thread_func, u))) {
249         pa_log("Failed to create thread.");
250         goto fail;
251     }
252     
253     pa_modargs_free(ma);
254
255     return 0;
256
257 fail:
258     if (ma)
259         pa_modargs_free(ma);
260
261     pa__done(c, m);
262
263     return -1;
264 }
265
266 void pa__done(pa_core *c, pa_module*m) {
267     struct userdata *u;
268     
269     pa_assert(c);
270     pa_assert(m);
271
272     if (!(u = m->userdata))
273         return;
274
275     pa_sink_disconnect(u->sink);
276
277     if (u->thread) {
278         pa_asyncmsgq_send(u->sink->asyncmsgq, PA_SINK_MESSAGE_SHUTDOWN, NULL);
279         pa_thread_free(u->thread);
280     }
281     
282     pa_sink_unref(u->sink);
283
284     pa_xfree(u);
285 }