]> code.delx.au - pulseaudio/blob - src/modules/module-null-source.c
alsa-mixer: Add surround 2.1 profile
[pulseaudio] / src / modules / module-null-source.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2008 Lennart Poettering
5 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <unistd.h>
31
32 #include <pulse/rtclock.h>
33 #include <pulse/timeval.h>
34 #include <pulse/xmalloc.h>
35
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/modargs.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/rtpoll.h>
42 #include <pulsecore/source.h>
43 #include <pulsecore/thread-mq.h>
44 #include <pulsecore/thread.h>
45
46 #include "module-null-source-symdef.h"
47
48 PA_MODULE_AUTHOR("Lennart Poettering & Marc-Andre Lureau");
49 PA_MODULE_DESCRIPTION("Clocked NULL source");
50 PA_MODULE_VERSION(PACKAGE_VERSION);
51 PA_MODULE_LOAD_ONCE(false);
52 PA_MODULE_USAGE(
53 "format=<sample format> "
54 "channels=<number of channels> "
55 "rate=<sample rate> "
56 "source_name=<name of source> "
57 "channel_map=<channel map> "
58 "description=<description for the source> "
59 "latency_time=<latency time in ms>");
60
61 #define DEFAULT_SOURCE_NAME "source.null"
62 #define DEFAULT_LATENCY_TIME 20
63 #define MAX_LATENCY_USEC (PA_USEC_PER_SEC * 2)
64
65 struct userdata {
66 pa_core *core;
67 pa_module *module;
68 pa_source *source;
69
70 pa_thread *thread;
71 pa_thread_mq thread_mq;
72 pa_rtpoll *rtpoll;
73
74 size_t block_size;
75
76 pa_usec_t block_usec;
77 pa_usec_t timestamp;
78 pa_usec_t latency_time;
79 };
80
81 static const char* const valid_modargs[] = {
82 "rate",
83 "format",
84 "channels",
85 "source_name",
86 "channel_map",
87 "description",
88 "latency_time",
89 NULL
90 };
91
92 static int source_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
93 struct userdata *u = PA_SOURCE(o)->userdata;
94
95 switch (code) {
96 case PA_SOURCE_MESSAGE_SET_STATE:
97
98 if (PA_PTR_TO_UINT(data) == PA_SOURCE_RUNNING)
99 u->timestamp = pa_rtclock_now();
100
101 break;
102
103 case PA_SOURCE_MESSAGE_GET_LATENCY: {
104 pa_usec_t now;
105
106 now = pa_rtclock_now();
107 *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0;
108
109 return 0;
110 }
111 }
112
113 return pa_source_process_msg(o, code, data, offset, chunk);
114 }
115
116 static void source_update_requested_latency_cb(pa_source *s) {
117 struct userdata *u;
118
119 pa_source_assert_ref(s);
120 u = s->userdata;
121 pa_assert(u);
122
123 u->block_usec = pa_source_get_requested_latency_within_thread(s);
124 }
125
126 static void thread_func(void *userdata) {
127 struct userdata *u = userdata;
128
129 pa_assert(u);
130
131 pa_log_debug("Thread starting up");
132
133 pa_thread_mq_install(&u->thread_mq);
134
135 u->timestamp = pa_rtclock_now();
136
137 for (;;) {
138 int ret;
139
140 /* Generate some null data */
141 if (PA_SOURCE_IS_OPENED(u->source->thread_info.state)) {
142 pa_usec_t now;
143 pa_memchunk chunk;
144
145 now = pa_rtclock_now();
146
147 if ((chunk.length = pa_usec_to_bytes(now - u->timestamp, &u->source->sample_spec)) > 0) {
148
149 chunk.memblock = pa_memblock_new(u->core->mempool, (size_t) -1); /* or chunk.length? */
150 chunk.index = 0;
151 pa_source_post(u->source, &chunk);
152 pa_memblock_unref(chunk.memblock);
153
154 u->timestamp = now;
155 }
156
157 pa_rtpoll_set_timer_absolute(u->rtpoll, u->timestamp + u->latency_time * PA_USEC_PER_MSEC);
158 } else
159 pa_rtpoll_set_timer_disabled(u->rtpoll);
160
161 /* Hmm, nothing to do. Let's sleep */
162 if ((ret = pa_rtpoll_run(u->rtpoll, true)) < 0)
163 goto fail;
164
165 if (ret == 0)
166 goto finish;
167 }
168
169 fail:
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);
174
175 finish:
176 pa_log_debug("Thread shutting down");
177 }
178
179 int pa__init(pa_module*m) {
180 struct userdata *u = NULL;
181 pa_sample_spec ss;
182 pa_channel_map map;
183 pa_modargs *ma = NULL;
184 pa_source_new_data data;
185 uint32_t latency_time = DEFAULT_LATENCY_TIME;
186
187 pa_assert(m);
188
189 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
190 pa_log("Failed to parse module arguments.");
191 goto fail;
192 }
193
194 ss = m->core->default_sample_spec;
195 map = m->core->default_channel_map;
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");
198 goto fail;
199 }
200
201 m->userdata = u = pa_xnew0(struct userdata, 1);
202 u->core = m->core;
203 u->module = m;
204 u->rtpoll = pa_rtpoll_new();
205 pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll);
206
207 pa_source_new_data_init(&data);
208 data.driver = __FILE__;
209 data.module = m;
210 pa_source_new_data_set_name(&data, pa_modargs_get_value(ma, "source_name", DEFAULT_SOURCE_NAME));
211 pa_source_new_data_set_sample_spec(&data, &ss);
212 pa_source_new_data_set_channel_map(&data, &map);
213 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_DESCRIPTION, pa_modargs_get_value(ma, "description", "Null Input"));
214 pa_proplist_sets(data.proplist, PA_PROP_DEVICE_CLASS, "abstract");
215
216 u->source = pa_source_new(m->core, &data, PA_SOURCE_LATENCY | PA_SOURCE_DYNAMIC_LATENCY);
217 pa_source_new_data_done(&data);
218
219 if (!u->source) {
220 pa_log("Failed to create source object.");
221 goto fail;
222 }
223
224 u->latency_time = DEFAULT_LATENCY_TIME;
225 if (pa_modargs_get_value_u32(ma, "latency_time", &latency_time) < 0) {
226 pa_log("Failed to parse latency_time value.");
227 goto fail;
228 }
229 u->latency_time = latency_time;
230
231 u->source->parent.process_msg = source_process_msg;
232 u->source->update_requested_latency = source_update_requested_latency_cb;
233 u->source->userdata = u;
234
235 pa_source_set_asyncmsgq(u->source, u->thread_mq.inq);
236 pa_source_set_rtpoll(u->source, u->rtpoll);
237
238 pa_source_set_latency_range(u->source, 0, MAX_LATENCY_USEC);
239 u->block_usec = u->source->thread_info.max_latency;
240
241 u->source->thread_info.max_rewind =
242 pa_usec_to_bytes(u->block_usec, &u->source->sample_spec);
243
244 if (!(u->thread = pa_thread_new("null-source", thread_func, u))) {
245 pa_log("Failed to create thread.");
246 goto fail;
247 }
248
249 pa_source_put(u->source);
250
251 pa_modargs_free(ma);
252
253 return 0;
254
255 fail:
256 if (ma)
257 pa_modargs_free(ma);
258
259 pa__done(m);
260
261 return -1;
262 }
263
264 void pa__done(pa_module*m) {
265 struct userdata *u;
266
267 pa_assert(m);
268
269 if (!(u = m->userdata))
270 return;
271
272 if (u->source)
273 pa_source_unlink(u->source);
274
275 if (u->thread) {
276 pa_asyncmsgq_send(u->thread_mq.inq, NULL, PA_MESSAGE_SHUTDOWN, NULL, 0, NULL);
277 pa_thread_free(u->thread);
278 }
279
280 pa_thread_mq_done(&u->thread_mq);
281
282 if (u->source)
283 pa_source_unref(u->source);
284
285 if (u->rtpoll)
286 pa_rtpoll_free(u->rtpoll);
287
288 pa_xfree(u);
289 }