commit glitch-free work
[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/sink.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/core-error.h>
45 #include <pulsecore/modargs.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/thread.h>
48 #include <pulsecore/thread-mq.h>
49 #include <pulsecore/rtpoll.h>
50 #include <pulsecore/rtclock.h>
51
52 #include "module-null-sink-symdef.h"
53
54 PA_MODULE_AUTHOR("Lennart Poettering");
55 PA_MODULE_DESCRIPTION("Clocked NULL sink");
56 PA_MODULE_VERSION(PACKAGE_VERSION);
57 PA_MODULE_LOAD_ONCE(FALSE);
58 PA_MODULE_USAGE(
59         "format=<sample format> "
60         "channels=<number of channels> "
61         "rate=<sample rate> "
62         "sink_name=<name of sink>"
63         "channel_map=<channel map>"
64         "description=<description for the sink>");
65
66 #define DEFAULT_SINK_NAME "null"
67
68 struct userdata {
69     pa_core *core;
70     pa_module *module;
71     pa_sink *sink;
72
73     pa_thread *thread;
74     pa_thread_mq thread_mq;
75     pa_rtpoll *rtpoll;
76
77     size_t block_size;
78
79     struct timeval timestamp;
80 };
81
82 static const char* const valid_modargs[] = {
83     "rate",
84     "format",
85     "channels",
86     "sink_name",
87     "channel_map",
88     "description",
89     NULL
90 };
91
92 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
93     struct userdata *u = PA_SINK(o)->userdata;
94
95     switch (code) {
96         case PA_SINK_MESSAGE_SET_STATE:
97
98             if (PA_PTR_TO_UINT(data) == PA_SINK_RUNNING)
99                 pa_rtclock_get(&u->timestamp);
100
101             break;
102
103         case PA_SINK_MESSAGE_GET_LATENCY: {
104             struct timeval now;
105
106             pa_rtclock_get(&now);
107
108             if (pa_timeval_cmp(&u->timestamp, &now) > 0)
109                 *((pa_usec_t*) data) = 0;
110             else
111                 *((pa_usec_t*) data) = pa_timeval_diff(&u->timestamp, &now);
112             break;
113         }
114     }
115
116     return pa_sink_process_msg(o, code, data, offset, chunk);
117 }
118
119 static void thread_func(void *userdata) {
120     struct userdata *u = userdata;
121
122     pa_assert(u);
123
124     pa_log_debug("Thread starting up");
125
126     pa_thread_mq_install(&u->thread_mq);
127     pa_rtpoll_install(u->rtpoll);
128
129     pa_rtclock_get(&u->timestamp);
130
131     for (;;) {
132         int ret;
133
134         /* Render some data and drop it immediately */
135         if (u->sink->thread_info.state == PA_SINK_RUNNING) {
136             struct timeval now;
137
138             pa_rtclock_get(&now);
139
140             if (pa_timeval_cmp(&u->timestamp, &now) <= 0) {
141                 pa_sink_skip(u->sink, u->block_size);
142                 pa_timeval_add(&u->timestamp, pa_bytes_to_usec(u->block_size, &u->sink->sample_spec));
143             }
144
145             pa_rtpoll_set_timer_absolute(u->rtpoll, &u->timestamp);
146         } else
147             pa_rtpoll_set_timer_disabled(u->rtpoll);
148
149         /* Hmm, nothing to do. Let's sleep */
150         if ((ret = pa_rtpoll_run(u->rtpoll, 1)) < 0)
151             goto fail;
152
153         if (ret == 0)
154             goto finish;
155     }
156
157 fail:
158     /* If this was no regular exit from the loop we have to continue
159      * processing messages until we received PA_MESSAGE_SHUTDOWN */
160     pa_asyncmsgq_post(u->thread_mq.outq, PA_MSGOBJECT(u->core), PA_CORE_MESSAGE_UNLOAD_MODULE, u->module, 0, NULL, NULL);
161     pa_asyncmsgq_wait_for(u->thread_mq.inq, PA_MESSAGE_SHUTDOWN);
162
163 finish:
164     pa_log_debug("Thread shutting down");
165 }
166
167 int pa__init(pa_module*m) {
168     struct userdata *u = NULL;
169     pa_sample_spec ss;
170     pa_channel_map map;
171     pa_modargs *ma = NULL;
172     pa_sink_new_data data;
173
174     pa_assert(m);
175
176     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
177         pa_log("Failed to parse module arguments.");
178         goto fail;
179     }
180
181     ss = m->core->default_sample_spec;
182     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
183         pa_log("Invalid sample format specification or channel map");
184         goto fail;
185     }
186
187     u = pa_xnew0(struct userdata, 1);
188     u->core = m->core;
189     u->module = m;
190     m->userdata = u;
191     pa_thread_mq_init(&u->thread_mq, m->core->mainloop);
192     u->rtpoll = pa_rtpoll_new();
193     pa_rtpoll_item_new_asyncmsgq(u->rtpoll, PA_RTPOLL_EARLY, u->thread_mq.inq);
194
195     pa_sink_new_data_init(&data);
196     data.driver = __FILE__;
197     data.module = m;
198     pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME));
199     pa_sink_new_data_set_sample_spec(&data, &ss);
200     pa_sink_new_data_set_channel_map(&data, &map);
201     pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "NULL sink"));
202
203     u->sink = pa_sink_new(m->core, &data, 0);
204     pa_sink_new_data_done(&data);
205
206     if (!u->sink) {
207         pa_log("Failed to create sink.");
208         goto fail;
209     }
210
211     u->sink->parent.process_msg = sink_process_msg;
212     u->sink->userdata = u;
213     u->sink->flags = PA_SINK_LATENCY;
214
215     pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq);
216     pa_sink_set_rtpoll(u->sink, u->rtpoll);
217
218     u->block_size = pa_bytes_per_second(&ss) / 20; /* 50 ms */
219     if (u->block_size <= 0)
220         u->block_size = pa_frame_size(&ss);
221
222     if (!(u->thread = pa_thread_new(thread_func, u))) {
223         pa_log("Failed to create thread.");
224         goto fail;
225     }
226
227     pa_sink_put(u->sink);
228
229     pa_modargs_free(ma);
230
231     return 0;
232
233 fail:
234     if (ma)
235         pa_modargs_free(ma);
236
237     pa__done(m);
238
239     return -1;
240 }
241
242 void pa__done(pa_module*m) {
243     struct userdata *u;
244
245     pa_assert(m);
246
247     if (!(u = m->userdata))
248         return;
249
250     if (u->sink)
251         pa_sink_unlink(u->sink);
252
253     if (u->thread) {
254         pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
255         pa_thread_free(u->thread);
256     }
257
258     pa_thread_mq_done(&u->thread_mq);
259
260     if (u->sink)
261         pa_sink_unref(u->sink);
262
263     if (u->rtpoll)
264         pa_rtpoll_free(u->rtpoll);
265
266     pa_xfree(u);
267 }