4 This file is part of PulseAudio.
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.
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.
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
31 #include <pulse/utf8.h>
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/core-subscribe.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/namereg.h>
38 #include "source-output.h"
40 #define CHECK_VALIDITY_RETURN_NULL(condition) \
46 pa_source_output_new_data
* pa_source_output_new_data_init(pa_source_output_new_data
*data
) {
49 memset(data
, 0, sizeof(*data
));
50 data
->resample_method
= PA_RESAMPLER_INVALID
;
54 void pa_source_output_new_data_set_channel_map(pa_source_output_new_data
*data
, const pa_channel_map
*map
) {
57 if ((data
->channel_map_is_set
= !!map
))
58 data
->channel_map
= *map
;
61 void pa_source_output_new_data_set_sample_spec(pa_source_output_new_data
*data
, const pa_sample_spec
*spec
) {
64 if ((data
->sample_spec_is_set
= !!spec
))
65 data
->sample_spec
= *spec
;
68 pa_source_output
* pa_source_output_new(
70 pa_source_output_new_data
*data
,
71 pa_source_output_flags_t flags
) {
74 pa_resampler
*resampler
= NULL
;
76 char st
[PA_SAMPLE_SPEC_SNPRINT_MAX
];
81 if (!(flags
& PA_SOURCE_OUTPUT_NO_HOOKS
))
82 if (pa_hook_fire(&core
->hook_source_output_new
, data
) < 0)
85 CHECK_VALIDITY_RETURN_NULL(!data
->driver
|| pa_utf8_valid(data
->driver
));
86 CHECK_VALIDITY_RETURN_NULL(!data
->name
|| pa_utf8_valid(data
->name
));
89 data
->source
= pa_namereg_get(core
, NULL
, PA_NAMEREG_SOURCE
, 1);
91 CHECK_VALIDITY_RETURN_NULL(data
->source
);
92 CHECK_VALIDITY_RETURN_NULL(data
->source
->state
== PA_SOURCE_RUNNING
);
94 if (!data
->sample_spec_is_set
)
95 data
->sample_spec
= data
->source
->sample_spec
;
97 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(&data
->sample_spec
));
99 if (!data
->channel_map_is_set
)
100 pa_channel_map_init_auto(&data
->channel_map
, data
->sample_spec
.channels
, PA_CHANNEL_MAP_DEFAULT
);
102 CHECK_VALIDITY_RETURN_NULL(pa_channel_map_valid(&data
->channel_map
));
103 CHECK_VALIDITY_RETURN_NULL(data
->channel_map
.channels
== data
->sample_spec
.channels
);
105 if (data
->resample_method
== PA_RESAMPLER_INVALID
)
106 data
->resample_method
= core
->resample_method
;
108 CHECK_VALIDITY_RETURN_NULL(data
->resample_method
< PA_RESAMPLER_MAX
);
110 if (pa_idxset_size(data
->source
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
111 pa_log("Failed to create source output: too many outputs per source.");
115 if (!pa_sample_spec_equal(&data
->sample_spec
, &data
->source
->sample_spec
) ||
116 !pa_channel_map_equal(&data
->channel_map
, &data
->source
->channel_map
))
117 if (!(resampler
= pa_resampler_new(
119 &data
->source
->sample_spec
, &data
->source
->channel_map
,
120 &data
->sample_spec
, &data
->channel_map
,
121 data
->resample_method
))) {
122 pa_log_warn("Unsupported resampling operation.");
126 o
= pa_xnew(pa_source_output
, 1);
128 o
->state
= PA_SOURCE_OUTPUT_RUNNING
;
129 o
->name
= pa_xstrdup(data
->name
);
130 o
->driver
= pa_xstrdup(data
->driver
);
131 o
->module
= data
->module
;
132 o
->source
= data
->source
;
133 o
->client
= data
->client
;
135 o
->sample_spec
= data
->sample_spec
;
136 o
->channel_map
= data
->channel_map
;
140 o
->get_latency
= NULL
;
143 o
->resampler
= resampler
;
144 o
->resample_method
= data
->resample_method
;
146 r
= pa_idxset_put(core
->source_outputs
, o
, &o
->index
);
148 r
= pa_idxset_put(o
->source
->outputs
, o
, NULL
);
151 pa_log_info("created %u \"%s\" on %s with sample spec %s",
155 pa_sample_spec_snprint(st
, sizeof(st
), &o
->sample_spec
));
157 pa_subscription_post(core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
, o
->index
);
159 /* We do not call pa_source_notify() here, because the virtual
160 * functions have not yet been initialized */
165 void pa_source_output_disconnect(pa_source_output
*o
) {
167 assert(o
->state
!= PA_SOURCE_OUTPUT_DISCONNECTED
);
169 assert(o
->source
->core
);
171 pa_idxset_remove_by_data(o
->source
->core
->source_outputs
, o
, NULL
);
172 pa_idxset_remove_by_data(o
->source
->outputs
, o
, NULL
);
174 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_REMOVE
, o
->index
);
179 o
->get_latency
= NULL
;
181 o
->state
= PA_SOURCE_OUTPUT_DISCONNECTED
;
184 static void source_output_free(pa_source_output
* o
) {
187 if (o
->state
!= PA_SOURCE_OUTPUT_DISCONNECTED
)
188 pa_source_output_disconnect(o
);
190 pa_log_info("freed %u \"%s\"", o
->index
, o
->name
);
193 pa_resampler_free(o
->resampler
);
200 void pa_source_output_unref(pa_source_output
* o
) {
205 source_output_free(o
);
208 pa_source_output
* pa_source_output_ref(pa_source_output
*o
) {
216 void pa_source_output_kill(pa_source_output
*o
) {
224 void pa_source_output_push(pa_source_output
*o
, const pa_memchunk
*chunk
) {
229 assert(chunk
->length
);
232 if (o
->state
== PA_SOURCE_OUTPUT_CORKED
)
240 pa_resampler_run(o
->resampler
, chunk
, &rchunk
);
244 assert(rchunk
.memblock
);
246 pa_memblock_unref(rchunk
.memblock
);
249 void pa_source_output_set_name(pa_source_output
*o
, const char *name
) {
253 if (!o
->name
&& !name
)
256 if (o
->name
&& name
&& !strcmp(o
->name
, name
))
260 o
->name
= pa_xstrdup(name
);
262 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
265 pa_usec_t
pa_source_output_get_latency(pa_source_output
*o
) {
270 return o
->get_latency(o
);
275 void pa_source_output_cork(pa_source_output
*o
, int b
) {
281 if (o
->state
== PA_SOURCE_OUTPUT_DISCONNECTED
)
284 n
= o
->state
== PA_SOURCE_OUTPUT_CORKED
&& !b
;
286 o
->state
= b
? PA_SOURCE_OUTPUT_CORKED
: PA_SOURCE_OUTPUT_RUNNING
;
289 pa_source_notify(o
->source
);
292 pa_resample_method_t
pa_source_output_get_resample_method(pa_source_output
*o
) {
297 return o
->resample_method
;
299 return pa_resampler_get_method(o
->resampler
);
302 int pa_source_output_move_to(pa_source_output
*o
, pa_source
*dest
) {
304 pa_resampler
*new_resampler
= NULL
;
315 if (pa_idxset_size(dest
->outputs
) >= PA_MAX_OUTPUTS_PER_SOURCE
) {
316 pa_log_warn("Failed to move source output: too many outputs per source.");
321 pa_sample_spec_equal(&origin
->sample_spec
, &dest
->sample_spec
) &&
322 pa_channel_map_equal(&origin
->channel_map
, &dest
->channel_map
))
324 /* Try to reuse the old resampler if possible */
325 new_resampler
= o
->resampler
;
327 else if (!pa_sample_spec_equal(&o
->sample_spec
, &dest
->sample_spec
) ||
328 !pa_channel_map_equal(&o
->channel_map
, &dest
->channel_map
)) {
330 /* Okey, we need a new resampler for the new sink */
332 if (!(new_resampler
= pa_resampler_new(
334 &dest
->sample_spec
, &dest
->channel_map
,
335 &o
->sample_spec
, &o
->channel_map
,
336 o
->resample_method
))) {
337 pa_log_warn("Unsupported resampling operation.");
342 /* Okey, let's move it */
343 pa_idxset_remove_by_data(origin
->outputs
, o
, NULL
);
344 pa_idxset_put(dest
->outputs
, o
, NULL
);
347 /* Replace resampler */
348 if (new_resampler
!= o
->resampler
) {
350 pa_resampler_free(o
->resampler
);
351 o
->resampler
= new_resampler
;
354 /* Notify everyone */
355 pa_subscription_post(o
->source
->core
, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, o
->index
);
356 pa_source_notify(o
->source
);