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