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