]> code.delx.au - pulseaudio/blob - src/pulsecore/source-output.c
big s/polyp/pulse/g
[pulseaudio] / src / pulsecore / source-output.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
33
34 #include <pulsecore/core-subscribe.h>
35 #include <pulsecore/log.h>
36
37 #include "source-output.h"
38
39 #define CHECK_VALIDITY_RETURN_NULL(condition) \
40 do {\
41 if (!(condition)) \
42 return NULL; \
43 } while (0)
44
45 pa_source_output* pa_source_output_new(
46 pa_source *s,
47 const char *driver,
48 const char *name,
49 const pa_sample_spec *spec,
50 const pa_channel_map *map,
51 int resample_method) {
52
53 pa_source_output *o;
54 pa_resampler *resampler = NULL;
55 int r;
56 char st[256];
57 pa_channel_map tmap;
58
59 assert(s);
60 assert(spec);
61 assert(s->state == PA_SOURCE_RUNNING);
62
63 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(spec));
64
65 if (!map)
66 map = pa_channel_map_init_auto(&tmap, spec->channels, PA_CHANNEL_MAP_DEFAULT);
67
68 CHECK_VALIDITY_RETURN_NULL(map && pa_channel_map_valid(map));
69 CHECK_VALIDITY_RETURN_NULL(map->channels == spec->channels);
70 CHECK_VALIDITY_RETURN_NULL(!driver || pa_utf8_valid(driver));
71 CHECK_VALIDITY_RETURN_NULL(pa_utf8_valid(name));
72
73 if (pa_idxset_size(s->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
74 pa_log(__FILE__": Failed to create source output: too many outputs per source.");
75 return NULL;
76 }
77
78 if (resample_method == PA_RESAMPLER_INVALID)
79 resample_method = s->core->resample_method;
80
81 if (!pa_sample_spec_equal(&s->sample_spec, spec) || !pa_channel_map_equal(&s->channel_map, map))
82 if (!(resampler = pa_resampler_new(&s->sample_spec, &s->channel_map, spec, map, s->core->memblock_stat, resample_method)))
83 return NULL;
84
85 o = pa_xnew(pa_source_output, 1);
86 o->ref = 1;
87 o->state = PA_SOURCE_OUTPUT_RUNNING;
88 o->name = pa_xstrdup(name);
89 o->driver = pa_xstrdup(driver);
90 o->owner = NULL;
91 o->source = s;
92 o->client = NULL;
93
94 o->sample_spec = *spec;
95 o->channel_map = *map;
96
97 o->push = NULL;
98 o->kill = NULL;
99 o->get_latency = NULL;
100 o->userdata = NULL;
101
102 o->resampler = resampler;
103
104 assert(s->core);
105 r = pa_idxset_put(s->core->source_outputs, o, &o->index);
106 assert(r == 0 && o->index != PA_IDXSET_INVALID);
107 r = pa_idxset_put(s->outputs, o, NULL);
108 assert(r == 0);
109
110 pa_sample_spec_snprint(st, sizeof(st), spec);
111 pa_log_info(__FILE__": created %u \"%s\" on %u with sample spec \"%s\"", o->index, o->name, s->index, st);
112
113 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
114
115 return o;
116 }
117
118 void pa_source_output_disconnect(pa_source_output*o) {
119 assert(o);
120 assert(o->state != PA_SOURCE_OUTPUT_DISCONNECTED);
121 assert(o->source);
122 assert(o->source->core);
123
124 pa_idxset_remove_by_data(o->source->core->source_outputs, o, NULL);
125 pa_idxset_remove_by_data(o->source->outputs, o, NULL);
126
127 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
128 o->source = NULL;
129
130 o->push = NULL;
131 o->kill = NULL;
132 o->get_latency = NULL;
133
134 o->state = PA_SOURCE_OUTPUT_DISCONNECTED;
135 }
136
137 static void source_output_free(pa_source_output* o) {
138 assert(o);
139
140 if (o->state != PA_SOURCE_OUTPUT_DISCONNECTED)
141 pa_source_output_disconnect(o);
142
143 pa_log_info(__FILE__": freed %u \"%s\"", o->index, o->name);
144
145 if (o->resampler)
146 pa_resampler_free(o->resampler);
147
148 pa_xfree(o->name);
149 pa_xfree(o->driver);
150 pa_xfree(o);
151 }
152
153 void pa_source_output_unref(pa_source_output* o) {
154 assert(o);
155 assert(o->ref >= 1);
156
157 if (!(--o->ref))
158 source_output_free(o);
159 }
160
161 pa_source_output* pa_source_output_ref(pa_source_output *o) {
162 assert(o);
163 assert(o->ref >= 1);
164
165 o->ref++;
166 return o;
167 }
168
169
170 void pa_source_output_kill(pa_source_output*o) {
171 assert(o);
172 assert(o->ref >= 1);
173
174 if (o->kill)
175 o->kill(o);
176 }
177
178 void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
179 pa_memchunk rchunk;
180
181 assert(o);
182 assert(chunk);
183 assert(chunk->length);
184 assert(o->push);
185
186 if (o->state == PA_SOURCE_OUTPUT_CORKED)
187 return;
188
189 if (!o->resampler) {
190 o->push(o, chunk);
191 return;
192 }
193
194 pa_resampler_run(o->resampler, chunk, &rchunk);
195 if (!rchunk.length)
196 return;
197
198 assert(rchunk.memblock);
199 o->push(o, &rchunk);
200 pa_memblock_unref(rchunk.memblock);
201 }
202
203 void pa_source_output_set_name(pa_source_output *o, const char *name) {
204 assert(o);
205 assert(o->ref >= 1);
206
207 pa_xfree(o->name);
208 o->name = pa_xstrdup(name);
209
210 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
211 }
212
213 pa_usec_t pa_source_output_get_latency(pa_source_output *o) {
214 assert(o);
215 assert(o->ref >= 1);
216
217 if (o->get_latency)
218 return o->get_latency(o);
219
220 return 0;
221 }
222
223 void pa_source_output_cork(pa_source_output *o, int b) {
224 assert(o);
225 assert(o->ref >= 1);
226
227 if (o->state == PA_SOURCE_OUTPUT_DISCONNECTED)
228 return;
229
230 o->state = b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
231 }
232
233 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
234 assert(o);
235 assert(o->ref >= 1);
236
237 if (!o->resampler)
238 return PA_RESAMPLER_INVALID;
239
240 return pa_resampler_get_method(o->resampler);
241 }