support new channel_map argument in sink/source modules
[profile/ivi/pulseaudio.git] / src / modules / 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 Lesser 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 Lesser 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 <polypcore/iochannel.h>
37 #include <polypcore/sink.h>
38 #include <polypcore/module.h>
39 #include <polypcore/util.h>
40 #include <polypcore/modargs.h>
41 #include <polypcore/xmalloc.h>
42 #include <polypcore/log.h>
43
44 #include "module-null-sink-symdef.h"
45
46 PA_MODULE_AUTHOR("Lennart Poettering")
47 PA_MODULE_DESCRIPTION("Clocked NULL sink")
48 PA_MODULE_VERSION(PACKAGE_VERSION)
49 PA_MODULE_USAGE(
50         "format=<sample format> "
51         "channels=<number of channels> "
52         "rate=<sample rate> "
53         "sink_name=<name of sink>"
54         "channel_map=<channel map>")
55
56 #define DEFAULT_SINK_NAME "null"
57
58 struct userdata {
59     pa_core *core;
60     pa_module *module;
61     pa_sink *sink;
62     pa_time_event *time_event;
63     size_t block_size;
64 };
65
66 static const char* const valid_modargs[] = {
67     "rate",
68     "format",
69     "channels",
70     "sink_name",
71     "channel_map", 
72     NULL
73 };
74
75 static void time_callback(pa_mainloop_api *m, pa_time_event*e, const struct timeval *tv, void *userdata) {
76     struct userdata *u = userdata;
77     pa_memchunk chunk;
78     struct timeval ntv = *tv;
79     size_t l;
80
81     assert(u);
82
83     if (pa_sink_render(u->sink, u->block_size, &chunk) >= 0) {
84         l = chunk.length;
85         pa_memblock_unref(chunk.memblock);
86     } else
87         l = u->block_size;
88
89     pa_timeval_add(&ntv, pa_bytes_to_usec(l, &u->sink->sample_spec));
90     m->time_restart(e, &ntv);
91 }
92
93 int pa__init(pa_core *c, pa_module*m) {
94     struct userdata *u = NULL;
95     pa_sample_spec ss;
96     pa_channel_map map;
97     pa_modargs *ma = NULL;
98     struct timeval tv;
99     assert(c && m);
100     
101     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
102         pa_log(__FILE__": failed to parse module arguments.");
103         goto fail;
104     }
105
106     ss = c->default_sample_spec;
107     if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map) < 0) {
108         pa_log(__FILE__": invalid sample format specification or channel map.");
109         goto fail;
110     }
111     
112     u = pa_xmalloc0(sizeof(struct userdata));
113     u->core = c;
114     u->module = m;
115     m->userdata = u;
116     
117     if (!(u->sink = pa_sink_new(c, __FILE__, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME), 0, &ss, &map))) {
118         pa_log(__FILE__": failed to create sink.");
119         goto fail;
120     }
121     
122     u->sink->userdata = u;
123     pa_sink_set_owner(u->sink, m);
124     u->sink->description = pa_sprintf_malloc("NULL sink");
125
126     pa_gettimeofday(&tv);
127     u->time_event = c->mainloop->time_new(c->mainloop, &tv, time_callback, u);
128
129     u->block_size = pa_bytes_per_second(&ss) / 10;
130     
131     pa_modargs_free(ma);
132     
133     return 0;
134
135 fail:
136     if (ma)
137         pa_modargs_free(ma);
138         
139     pa__done(c, m);
140
141     return -1;
142 }
143
144 void pa__done(pa_core *c, pa_module*m) {
145     struct userdata *u;
146     assert(c && m);
147
148     if (!(u = m->userdata))
149         return;
150     
151     pa_sink_disconnect(u->sink);
152     pa_sink_unref(u->sink);
153
154     u->core->mainloop->time_free(u->time_event);
155
156     pa_xfree(u);
157 }