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/introspect.h>
32 #include <pulse/utf8.h>
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/sink-input.h>
36 #include <pulsecore/namereg.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/sample-util.h>
39 #include <pulsecore/core-subscribe.h>
40 #include <pulsecore/log.h>
44 #define MAX_MIX_CHANNELS 32
46 #define CHECK_VALIDITY_RETURN_NULL(condition) \
57 const pa_sample_spec
*spec
,
58 const pa_channel_map
*map
) {
70 CHECK_VALIDITY_RETURN_NULL(pa_sample_spec_valid(spec
));
73 map
= pa_channel_map_init_auto(&tmap
, spec
->channels
, PA_CHANNEL_MAP_DEFAULT
);
75 CHECK_VALIDITY_RETURN_NULL(map
&& pa_channel_map_valid(map
));
76 CHECK_VALIDITY_RETURN_NULL(map
->channels
== spec
->channels
);
77 CHECK_VALIDITY_RETURN_NULL(!driver
|| pa_utf8_valid(driver
));
78 CHECK_VALIDITY_RETURN_NULL(pa_utf8_valid(name
) && *name
);
80 s
= pa_xnew(pa_sink
, 1);
82 if (!(name
= pa_namereg_register(core
, name
, PA_NAMEREG_SINK
, s
, fail
))) {
89 s
->state
= PA_SINK_RUNNING
;
90 s
->name
= pa_xstrdup(name
);
91 s
->description
= NULL
;
92 s
->driver
= pa_xstrdup(driver
);
95 s
->sample_spec
= *spec
;
96 s
->channel_map
= *map
;
98 s
->inputs
= pa_idxset_new(NULL
, NULL
);
100 pa_cvolume_reset(&s
->sw_volume
, spec
->channels
);
101 pa_cvolume_reset(&s
->hw_volume
, spec
->channels
);
107 s
->get_latency
= NULL
;
109 s
->set_hw_volume
= NULL
;
110 s
->get_hw_volume
= NULL
;
111 s
->set_hw_mute
= NULL
;
112 s
->get_hw_mute
= NULL
;
115 r
= pa_idxset_put(core
->sinks
, s
, &s
->index
);
116 assert(s
->index
!= PA_IDXSET_INVALID
&& r
>= 0);
118 pa_sample_spec_snprint(st
, sizeof(st
), spec
);
119 pa_log_info("created %u \"%s\" with sample spec \"%s\"", s
->index
, s
->name
, st
);
121 n
= pa_sprintf_malloc("%s.monitor", name
);
123 if (!(s
->monitor_source
= pa_source_new(core
, driver
, n
, 0, spec
, map
)))
124 pa_log_warn("failed to create monitor source.");
127 s
->monitor_source
->monitor_of
= s
;
128 d
= pa_sprintf_malloc("Monitor Source of %s", s
->name
);
129 pa_source_set_description(s
->monitor_source
, d
);
135 pa_subscription_post(core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
140 void pa_sink_disconnect(pa_sink
* s
) {
141 pa_sink_input
*i
, *j
= NULL
;
144 assert(s
->state
== PA_SINK_RUNNING
);
146 s
->state
= PA_SINK_DISCONNECTED
;
147 pa_namereg_unregister(s
->core
, s
->name
);
149 pa_hook_fire(&s
->core
->hook_sink_disconnect
, s
);
151 while ((i
= pa_idxset_first(s
->inputs
, NULL
))) {
153 pa_sink_input_kill(i
);
157 if (s
->monitor_source
)
158 pa_source_disconnect(s
->monitor_source
);
160 pa_idxset_remove_by_data(s
->core
->sinks
, s
, NULL
);
162 s
->get_latency
= NULL
;
164 s
->get_hw_volume
= NULL
;
165 s
->set_hw_volume
= NULL
;
166 s
->set_hw_mute
= NULL
;
167 s
->get_hw_mute
= NULL
;
169 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
172 static void sink_free(pa_sink
*s
) {
176 if (s
->state
!= PA_SINK_DISCONNECTED
)
177 pa_sink_disconnect(s
);
179 pa_log_info("freed %u \"%s\"", s
->index
, s
->name
);
181 if (s
->monitor_source
) {
182 pa_source_unref(s
->monitor_source
);
183 s
->monitor_source
= NULL
;
186 pa_idxset_free(s
->inputs
, NULL
, NULL
);
189 pa_xfree(s
->description
);
194 void pa_sink_unref(pa_sink
*s
) {
202 pa_sink
* pa_sink_ref(pa_sink
*s
) {
210 void pa_sink_notify(pa_sink
*s
) {
218 static unsigned fill_mix_info(pa_sink
*s
, pa_mix_info
*info
, unsigned maxinfo
) {
219 uint32_t idx
= PA_IDXSET_INVALID
;
227 for (i
= pa_idxset_first(s
->inputs
, &idx
); maxinfo
> 0 && i
; i
= pa_idxset_next(s
->inputs
, &idx
)) {
228 /* Increase ref counter, to make sure that this input doesn't
229 * vanish while we still need it */
230 pa_sink_input_ref(i
);
232 if (pa_sink_input_peek(i
, &info
->chunk
, &info
->volume
) < 0) {
233 pa_sink_input_unref(i
);
239 assert(info
->chunk
.memblock
);
240 assert(info
->chunk
.memblock
->data
);
241 assert(info
->chunk
.length
);
251 static void inputs_drop(pa_sink
*s
, pa_mix_info
*info
, unsigned maxinfo
, size_t length
) {
256 for (; maxinfo
> 0; maxinfo
--, info
++) {
257 pa_sink_input
*i
= info
->userdata
;
260 assert(info
->chunk
.memblock
);
263 pa_sink_input_drop(i
, &info
->chunk
, length
);
264 pa_memblock_unref(info
->chunk
.memblock
);
266 /* Decrease ref counter */
267 pa_sink_input_unref(i
);
268 info
->userdata
= NULL
;
272 int pa_sink_render(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
273 pa_mix_info info
[MAX_MIX_CHANNELS
];
284 n
= fill_mix_info(s
, info
, MAX_MIX_CHANNELS
);
292 *result
= info
[0].chunk
;
293 pa_memblock_ref(result
->memblock
);
295 if (result
->length
> length
)
296 result
->length
= length
;
298 pa_sw_cvolume_multiply(&volume
, &s
->sw_volume
, &info
[0].volume
);
300 if (s
->sw_muted
|| !pa_cvolume_is_norm(&volume
)) {
301 pa_memchunk_make_writable(result
, 0);
303 pa_silence_memchunk(result
, &s
->sample_spec
);
305 pa_volume_memchunk(result
, &s
->sample_spec
, &volume
);
308 result
->memblock
= pa_memblock_new(s
->core
->mempool
, length
);
309 assert(result
->memblock
);
311 /* pa_log("mixing %i", n); */
313 result
->length
= pa_mix(info
, n
, result
->memblock
->data
, length
,
314 &s
->sample_spec
, &s
->sw_volume
, s
->sw_muted
);
318 inputs_drop(s
, info
, n
, result
->length
);
320 if (s
->monitor_source
)
321 pa_source_post(s
->monitor_source
, result
);
331 int pa_sink_render_into(pa_sink
*s
, pa_memchunk
*target
) {
332 pa_mix_info info
[MAX_MIX_CHANNELS
];
339 assert(target
->memblock
);
340 assert(target
->length
);
341 assert(target
->memblock
->data
);
345 n
= fill_mix_info(s
, info
, MAX_MIX_CHANNELS
);
353 if (target
->length
> info
[0].chunk
.length
)
354 target
->length
= info
[0].chunk
.length
;
356 memcpy((uint8_t*) target
->memblock
->data
+ target
->index
,
357 (uint8_t*) info
[0].chunk
.memblock
->data
+ info
[0].chunk
.index
,
360 pa_sw_cvolume_multiply(&volume
, &s
->sw_volume
, &info
[0].volume
);
363 pa_silence_memchunk(target
, &s
->sample_spec
);
364 else if (!pa_cvolume_is_norm(&volume
))
365 pa_volume_memchunk(target
, &s
->sample_spec
, &volume
);
367 target
->length
= pa_mix(info
, n
,
368 (uint8_t*) target
->memblock
->data
+ target
->index
,
374 inputs_drop(s
, info
, n
, target
->length
);
376 if (s
->monitor_source
)
377 pa_source_post(s
->monitor_source
, target
);
387 void pa_sink_render_into_full(pa_sink
*s
, pa_memchunk
*target
) {
394 assert(target
->memblock
);
395 assert(target
->length
);
396 assert(target
->memblock
->data
);
407 if (pa_sink_render_into(s
, &chunk
) < 0)
418 pa_silence_memchunk(&chunk
, &s
->sample_spec
);
424 void pa_sink_render_full(pa_sink
*s
, size_t length
, pa_memchunk
*result
) {
430 /*** This needs optimization ***/
432 result
->memblock
= pa_memblock_new(s
->core
->mempool
, result
->length
= length
);
435 pa_sink_render_into_full(s
, result
);
438 pa_usec_t
pa_sink_get_latency(pa_sink
*s
) {
445 return s
->get_latency(s
);
448 void pa_sink_set_owner(pa_sink
*s
, pa_module
*m
) {
457 if (s
->monitor_source
)
458 pa_source_set_owner(s
->monitor_source
, m
);
460 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
463 void pa_sink_set_volume(pa_sink
*s
, pa_mixer_t m
, const pa_cvolume
*volume
) {
470 if (m
== PA_MIXER_HARDWARE
&& s
->set_hw_volume
)
475 if (pa_cvolume_equal(v
, volume
))
480 if (v
== &s
->hw_volume
)
481 if (s
->set_hw_volume(s
) < 0)
482 s
->sw_volume
= *volume
;
484 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
487 const pa_cvolume
*pa_sink_get_volume(pa_sink
*s
, pa_mixer_t m
) {
491 if (m
== PA_MIXER_HARDWARE
&& s
->set_hw_volume
) {
493 if (s
->get_hw_volume
)
496 return &s
->hw_volume
;
498 return &s
->sw_volume
;
501 void pa_sink_set_mute(pa_sink
*s
, pa_mixer_t m
, int mute
) {
507 if (m
== PA_MIXER_HARDWARE
&& s
->set_hw_mute
)
517 if (t
== &s
->hw_muted
)
518 if (s
->set_hw_mute(s
) < 0)
519 s
->sw_muted
= !!mute
;
521 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
524 int pa_sink_get_mute(pa_sink
*s
, pa_mixer_t m
) {
528 if (m
== PA_MIXER_HARDWARE
&& s
->set_hw_mute
) {
538 void pa_sink_set_description(pa_sink
*s
, const char *description
) {
542 if (!description
&& !s
->description
)
545 if (description
&& s
->description
&& !strcmp(description
, s
->description
))
548 pa_xfree(s
->description
);
549 s
->description
= pa_xstrdup(description
);
551 if (s
->monitor_source
) {
554 n
= pa_sprintf_malloc("Monitor Source of %s", s
->description
? s
->description
: s
->name
);
555 pa_source_set_description(s
->monitor_source
, n
);
559 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
562 unsigned pa_sink_used_by(pa_sink
*s
) {
568 ret
= pa_idxset_size(s
->inputs
);
570 if (s
->monitor_source
)
571 ret
+= pa_source_used_by(s
->monitor_source
);