4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <pulse/utf8.h>
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/core-subscribe.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/namereg.h>
40 #include "source-output.h"
42 #define CHECK_VALIDITY_RETURN_NULL(condition) \
48 pa_source_output_new_data* pa_source_output_new_data_init(pa_source_output_new_data *data) {
51 memset(data, 0, sizeof(*data));
52 data->resample_method = PA_RESAMPLER_INVALID;
56 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data *data, const pa_channel_map *map) {
59 if ((data->channel_map_is_set = !!map))
60 data->channel_map = *map;
63 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data *data, const pa_sample_spec *spec) {
66 if ((data->sample_spec_is_set = !!spec))
67 data->sample_spec = *spec;
70 pa_source_output* pa_source_output_new(
72 pa_source_output_new_data *data,
73 pa_source_output_flags_t flags) {
76 pa_resampler *resampler = NULL;
78 char st[PA_SAMPLE_SPEC_SNPRINT_MAX];
83 if (!(flags & PA_SOURCE_OUTPUT_NO_HOOKS))
84 if (pa_hook_fire(&core->hook_source_output_new, data) < 0)
87 CHECK_VALIDITY_RETURN_NULL(!data->driver || pa_utf8_valid(data->driver));
88 CHECK_VALIDITY_RETURN_NULL(!data->name || pa_utf8_valid(data->name));
91 data->source = pa_namereg_get(core, NULL, PA_NAMEREG_SOURCE, 1);
93 CHECK_VALIDITY_RETURN_NULL(data->source);
94 CHECK_VALIDITY_RETURN_NULL(data->source->state == PA_SOURCE_RUNNING);
96 if (!data->sample_spec_is_set)
97 data->sample_spec = data->source->sample_spec;
99 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(&data->sample_spec));
101 if (!data->channel_map_is_set)
102 pa_channel_map_init_auto(&data->channel_map, data->sample_spec.channels, PA_CHANNEL_MAP_DEFAULT);
104 CHECK_VALIDITY_RETURN_NULL(pa_channel_map_valid(&data->channel_map));
105 CHECK_VALIDITY_RETURN_NULL(data->channel_map.channels == data->sample_spec.channels);
107 if (data->resample_method == PA_RESAMPLER_INVALID)
108 data->resample_method = core->resample_method;
110 CHECK_VALIDITY_RETURN_NULL(data->resample_method < PA_RESAMPLER_MAX);
112 if (pa_idxset_size(data->source->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
113 pa_log("Failed to create source output: too many outputs per source.");
117 if (!pa_sample_spec_equal(&data->sample_spec, &data->source->sample_spec) ||
118 !pa_channel_map_equal(&data->channel_map, &data->source->channel_map))
119 if (!(resampler = pa_resampler_new(
121 &data->source->sample_spec, &data->source->channel_map,
122 &data->sample_spec, &data->channel_map,
123 data->resample_method))) {
124 pa_log_warn("Unsupported resampling operation.");
128 o = pa_xnew(pa_source_output, 1);
130 o->state = PA_SOURCE_OUTPUT_RUNNING;
131 o->name = pa_xstrdup(data->name);
132 o->driver = pa_xstrdup(data->driver);
133 o->module = data->module;
134 o->source = data->source;
135 o->client = data->client;
137 o->sample_spec = data->sample_spec;
138 o->channel_map = data->channel_map;
142 o->get_latency = NULL;
145 o->resampler = resampler;
146 o->resample_method = data->resample_method;
148 r = pa_idxset_put(core->source_outputs, o, &o->index);
150 r = pa_idxset_put(o->source->outputs, o, NULL);
153 pa_log_info("created %u \"%s\" on %s with sample spec %s",
157 pa_sample_spec_snprint(st, sizeof(st), &o->sample_spec));
159 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
161 /* We do not call pa_source_notify() here, because the virtual
162 * functions have not yet been initialized */
167 void pa_source_output_disconnect(pa_source_output*o) {
169 assert(o->state != PA_SOURCE_OUTPUT_DISCONNECTED);
171 assert(o->source->core);
173 pa_idxset_remove_by_data(o->source->core->source_outputs, o, NULL);
174 pa_idxset_remove_by_data(o->source->outputs, o, NULL);
176 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
181 o->get_latency = NULL;
183 o->state = PA_SOURCE_OUTPUT_DISCONNECTED;
186 static void source_output_free(pa_source_output* o) {
189 if (o->state != PA_SOURCE_OUTPUT_DISCONNECTED)
190 pa_source_output_disconnect(o);
192 pa_log_info("freed %u \"%s\"", o->index, o->name);
195 pa_resampler_free(o->resampler);
202 void pa_source_output_unref(pa_source_output* o) {
207 source_output_free(o);
210 pa_source_output* pa_source_output_ref(pa_source_output *o) {
218 void pa_source_output_kill(pa_source_output*o) {
226 void pa_source_output_push(pa_source_output *o, const pa_memchunk *chunk) {
231 assert(chunk->length);
234 if (o->state == PA_SOURCE_OUTPUT_CORKED)
242 pa_resampler_run(o->resampler, chunk, &rchunk);
246 assert(rchunk.memblock);
248 pa_memblock_unref(rchunk.memblock);
251 void pa_source_output_set_name(pa_source_output *o, const char *name) {
255 if (!o->name && !name)
258 if (o->name && name && !strcmp(o->name, name))
262 o->name = pa_xstrdup(name);
264 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
267 pa_usec_t pa_source_output_get_latency(pa_source_output *o) {
272 return o->get_latency(o);
277 void pa_source_output_cork(pa_source_output *o, int b) {
283 if (o->state == PA_SOURCE_OUTPUT_DISCONNECTED)
286 n = o->state == PA_SOURCE_OUTPUT_CORKED && !b;
288 o->state = b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
291 pa_source_notify(o->source);
294 pa_resample_method_t pa_source_output_get_resample_method(pa_source_output *o) {
299 return o->resample_method;
301 return pa_resampler_get_method(o->resampler);
304 int pa_source_output_move_to(pa_source_output *o, pa_source *dest) {
306 pa_resampler *new_resampler = NULL;
317 if (pa_idxset_size(dest->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
318 pa_log_warn("Failed to move source output: too many outputs per source.");
323 pa_sample_spec_equal(&origin->sample_spec, &dest->sample_spec) &&
324 pa_channel_map_equal(&origin->channel_map, &dest->channel_map))
326 /* Try to reuse the old resampler if possible */
327 new_resampler = o->resampler;
329 else if (!pa_sample_spec_equal(&o->sample_spec, &dest->sample_spec) ||
330 !pa_channel_map_equal(&o->channel_map, &dest->channel_map)) {
332 /* Okey, we need a new resampler for the new sink */
334 if (!(new_resampler = pa_resampler_new(
336 &dest->sample_spec, &dest->channel_map,
337 &o->sample_spec, &o->channel_map,
338 o->resample_method))) {
339 pa_log_warn("Unsupported resampling operation.");
344 /* Okey, let's move it */
345 pa_idxset_remove_by_data(origin->outputs, o, NULL);
346 pa_idxset_put(dest->outputs, o, NULL);
349 /* Replace resampler */
350 if (new_resampler != o->resampler) {
352 pa_resampler_free(o->resampler);
353 o->resampler = new_resampler;
356 /* Notify everyone */
357 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
358 pa_source_notify(o->source);