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