]> code.delx.au - pulseaudio/blob - src/modules/module-null-sink.c
54a8e89023cb7ff80cc1cb1e9046f7645160363d
[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 <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37
38 #include <pulse/timeval.h>
39 #include <pulse/xmalloc.h>
40
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_time_event *time_event;
68 size_t block_size;
69
70 uint64_t n_bytes;
71 struct timeval start_time;
72 };
73
74 static const char* const valid_modargs[] = {
75 "rate",
76 "format",
77 "channels",
78 "sink_name",
79 "channel_map",
80 "description",
81 NULL
82 };
83
84 static void time_callback(pa_mainloop_api *m, pa_time_event*e, const struct timeval *tv, void *userdata) {
85 struct userdata *u = userdata;
86 pa_memchunk chunk;
87 struct timeval ntv = *tv;
88 size_t l;
89
90 assert(u);
91
92 if (pa_sink_render(u->sink, u->block_size, &chunk) >= 0) {
93 l = chunk.length;
94 pa_memblock_unref(chunk.memblock);
95 } else
96 l = u->block_size;
97
98 pa_timeval_add(&ntv, pa_bytes_to_usec(l, &u->sink->sample_spec));
99 m->time_restart(e, &ntv);
100
101 u->n_bytes += l;
102 }
103
104 static pa_usec_t get_latency(pa_sink *s) {
105 struct userdata *u = s->userdata;
106 pa_usec_t a, b;
107 struct timeval now;
108
109 a = pa_timeval_diff(pa_gettimeofday(&now), &u->start_time);
110 b = pa_bytes_to_usec(u->n_bytes, &s->sample_spec);
111
112 return b > a ? b - a : 0;
113 }
114
115 int pa__init(pa_core *c, pa_module*m) {
116 struct userdata *u = NULL;
117 pa_sample_spec ss;
118 pa_channel_map map;
119 pa_modargs *ma = NULL;
120
121 assert(c);
122 assert(m);
123
124 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
125 pa_log("failed to parse module arguments.");
126 goto fail;
127 }
128
129 ss = c->default_sample_spec;
130 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
131 pa_log("invalid sample format specification or channel map.");
132 goto fail;
133 }
134
135 u = pa_xnew0(struct userdata, 1);
136 u->core = c;
137 u->module = m;
138 m->userdata = u;
139
140 if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
141 pa_log("failed to create sink.");
142 goto fail;
143 }
144
145 u->sink->get_latency = get_latency;
146 u->sink->userdata = u;
147 pa_sink_set_owner(u->sink, m);
148 pa_sink_set_description(u->sink, pa_modargs_get_value(ma, "description", "NULL sink"));
149
150 u->n_bytes = 0;
151 pa_gettimeofday(&u->start_time);
152
153 u->time_event = c->mainloop->time_new(c->mainloop, &u->start_time, time_callback, u);
154
155 u->block_size = pa_bytes_per_second(&ss) / 10;
156
157 pa_modargs_free(ma);
158
159 return 0;
160
161 fail:
162 if (ma)
163 pa_modargs_free(ma);
164
165 pa__done(c, m);
166
167 return -1;
168 }
169
170 void pa__done(pa_core *c, pa_module*m) {
171 struct userdata *u;
172 assert(c && m);
173
174 if (!(u = m->userdata))
175 return;
176
177 pa_sink_disconnect(u->sink);
178 pa_sink_unref(u->sink);
179
180 u->core->mainloop->time_free(u->time_event);
181
182 pa_xfree(u);
183 }