]> code.delx.au - pulseaudio/blob - polyp/module-null-sink.c
865420107e7b72cae50f4fd30dcc6791d7988e9f
[pulseaudio] / polyp / module-null-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <limits.h>
35
36 #include "iochannel.h"
37 #include "sink.h"
38 #include "module.h"
39 #include "util.h"
40 #include "modargs.h"
41 #include "xmalloc.h"
42 #include "log.h"
43
44 PA_MODULE_AUTHOR("Lennart Poettering")
45 PA_MODULE_DESCRIPTION("Clocked NULL sink")
46 PA_MODULE_VERSION(PACKAGE_VERSION)
47 PA_MODULE_USAGE("format=<sample format> channels=<number of channels> rate=<sample rate> sink_name=<name of sink>")
48
49 #define DEFAULT_SINK_NAME "null"
50
51 struct userdata {
52 struct pa_core *core;
53 struct pa_module *module;
54 struct pa_sink *sink;
55 struct pa_time_event *time_event;
56 size_t block_size;
57 };
58
59 static const char* const valid_modargs[] = {
60 "rate",
61 "format",
62 "channels",
63 "sink_name",
64 NULL
65 };
66
67 static void time_callback(struct pa_mainloop_api *m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
68 struct userdata *u = userdata;
69 struct pa_memchunk chunk;
70 struct timeval ntv = *tv;
71 size_t l;
72
73 assert(u);
74
75 if (pa_sink_render(u->sink, u->block_size, &chunk) >= 0) {
76 l = chunk.length;
77 pa_memblock_unref(chunk.memblock);
78 } else
79 l = u->block_size;
80
81 pa_timeval_add(&ntv, pa_bytes_to_usec(l, &u->sink->sample_spec));
82 m->time_restart(e, &ntv);
83 }
84
85 int pa__init(struct pa_core *c, struct pa_module*m) {
86 struct userdata *u = NULL;
87 struct pa_sample_spec ss;
88 struct pa_modargs *ma = NULL;
89 struct timeval tv;
90 assert(c && m);
91
92 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
93 pa_log(__FILE__": failed to parse module arguments.\n");
94 goto fail;
95 }
96
97 ss = c->default_sample_spec;
98 if (pa_modargs_get_sample_spec(ma, &ss) < 0) {
99 pa_log(__FILE__": invalid sample format specification.\n");
100 goto fail;
101 }
102
103 u = pa_xmalloc0(sizeof(struct userdata));
104 u->core = c;
105 u->module = m;
106 m->userdata = u;
107
108 if (!(u->sink = pa_sink_new(c, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss))) {
109 pa_log(__FILE__": failed to create sink.\n");
110 goto fail;
111 }
112
113 u->sink->userdata = u;
114 pa_sink_set_owner(u->sink, m);
115 u->sink->description = pa_sprintf_malloc("NULL sink");
116
117 gettimeofday(&tv, NULL);
118 u->time_event = c->mainloop->time_new(c->mainloop, &tv, time_callback, u);
119
120 u->block_size = pa_bytes_per_second(&ss) / 10;
121
122 pa_modargs_free(ma);
123
124 return 0;
125
126 fail:
127 if (ma)
128 pa_modargs_free(ma);
129
130 pa__done(c, m);
131
132 return -1;
133 }
134
135 void pa__done(struct pa_core *c, struct pa_module*m) {
136 struct userdata *u;
137 assert(c && m);
138
139 if (!(u = m->userdata))
140 return;
141
142 pa_sink_disconnect(u->sink);
143 pa_sink_unref(u->sink);
144
145 u->core->mainloop->time_free(u->time_event);
146
147 pa_xfree(u);
148 }