4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/utf8.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/source-output.h>
38 #include <pulsecore/namereg.h>
39 #include <pulsecore/core-subscribe.h>
40 #include <pulsecore/log.h>
41 #include <pulsecore/sample-util.h>
45 static PA_DEFINE_CHECK_TYPE(pa_source
, source_check_type
, pa_msgobject_check_type
);
47 static void source_free(pa_object
*o
);
49 pa_source
* pa_source_new(
54 const pa_sample_spec
*spec
,
55 const pa_channel_map
*map
) {
66 pa_return_null_if_fail(pa_sample_spec_valid(spec
));
69 map
= pa_channel_map_init_auto(&tmap
, spec
->channels
, PA_CHANNEL_MAP_DEFAULT
);
71 pa_return_null_if_fail(map
&& pa_channel_map_valid(map
));
72 pa_return_null_if_fail(map
->channels
== spec
->channels
);
73 pa_return_null_if_fail(!driver
|| pa_utf8_valid(driver
));
74 pa_return_null_if_fail(pa_utf8_valid(name
) && *name
);
76 s
= pa_msgobject_new(pa_source
, source_check_type
);
78 if (!(name
= pa_namereg_register(core
, name
, PA_NAMEREG_SOURCE
, s
, fail
))) {
83 s
->parent
.parent
.free
= source_free
;
84 s
->parent
.process_msg
= pa_source_process_msg
;
87 s
->state
= PA_SOURCE_IDLE
;
88 s
->name
= pa_xstrdup(name
);
89 s
->description
= NULL
;
90 s
->driver
= pa_xstrdup(driver
);
93 s
->sample_spec
= *spec
;
94 s
->channel_map
= *map
;
96 s
->outputs
= pa_idxset_new(NULL
, NULL
);
99 pa_cvolume_reset(&s
->volume
, spec
->channels
);
101 s
->refresh_volume
= s
->refresh_muted
= 0;
105 s
->get_latency
= NULL
;
106 s
->set_volume
= NULL
;
107 s
->get_volume
= NULL
;
115 r
= pa_idxset_put(core
->sources
, 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 source %u \"%s\" with sample spec \"%s\"", s
->index
, s
->name
, st
);
121 s
->thread_info
.outputs
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
122 s
->thread_info
.soft_volume
= s
->volume
;
123 s
->thread_info
.soft_muted
= s
->muted
;
124 s
->thread_info
.state
= s
->state
;
126 pa_subscription_post(core
, PA_SUBSCRIPTION_EVENT_SOURCE
| PA_SUBSCRIPTION_EVENT_NEW
, s
->index
);
131 static int source_set_state(pa_source
*s
, pa_source_state_t state
) {
136 if (s
->state
== state
)
140 if ((ret
= s
->set_state(s
, state
)) < 0)
143 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_STATE
, PA_UINT_TO_PTR(state
), NULL
) < 0)
150 void pa_source_disconnect(pa_source
*s
) {
151 pa_source_output
*o
, *j
= NULL
;
154 pa_return_if_fail(s
->state
!= PA_SOURCE_DISCONNECTED
);
156 pa_namereg_unregister(s
->core
, s
->name
);
157 pa_idxset_remove_by_data(s
->core
->sources
, s
, NULL
);
159 pa_hook_fire(&s
->core
->hook_source_disconnect
, s
);
161 while ((o
= pa_idxset_first(s
->outputs
, NULL
))) {
163 pa_source_output_kill(o
);
167 source_set_state(s
, PA_SOURCE_DISCONNECTED
);
169 s
->get_latency
= NULL
;
170 s
->get_volume
= NULL
;
171 s
->set_volume
= NULL
;
176 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
| PA_SUBSCRIPTION_EVENT_REMOVE
, s
->index
);
179 static void source_free(pa_object
*o
) {
180 pa_source_output
*so
;
181 pa_source
*s
= PA_SOURCE(o
);
184 pa_assert(pa_source_refcnt(s
) == 0);
186 if (s
->state
!= PA_SOURCE_DISCONNECTED
)
187 pa_source_disconnect(s
);
189 pa_log_info("Freeing source %u \"%s\"", s
->index
, s
->name
);
191 pa_idxset_free(s
->outputs
, NULL
, NULL
);
193 while ((so
= pa_hashmap_steal_first(s
->thread_info
.outputs
)))
194 pa_source_output_unref(so
);
196 pa_hashmap_free(s
->thread_info
.outputs
, NULL
, NULL
);
199 pa_xfree(s
->description
);
204 int pa_source_update_status(pa_source
*s
) {
205 pa_source_assert_ref(s
);
207 if (s
->state
== PA_SOURCE_SUSPENDED
)
210 return source_set_state(s
, pa_source_used_by(s
) ? PA_SOURCE_RUNNING
: PA_SOURCE_IDLE
);
213 int pa_source_suspend(pa_source
*s
, int suspend
) {
214 pa_source_assert_ref(s
);
217 return source_set_state(s
, PA_SOURCE_SUSPENDED
);
219 return source_set_state(s
, pa_source_used_by(s
) ? PA_SOURCE_RUNNING
: PA_SOURCE_IDLE
);
222 void pa_source_ping(pa_source
*s
) {
223 pa_source_assert_ref(s
);
225 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_PING
, NULL
, NULL
, NULL
);
228 void pa_source_post(pa_source
*s
, const pa_memchunk
*chunk
) {
232 pa_source_assert_ref(s
);
235 if (s
->thread_info
.state
!= PA_SOURCE_RUNNING
)
238 if (s
->thread_info
.soft_muted
|| !pa_cvolume_is_norm(&s
->thread_info
.soft_volume
)) {
239 pa_memchunk vchunk
= *chunk
;
241 pa_memblock_ref(vchunk
.memblock
);
242 pa_memchunk_make_writable(&vchunk
, 0);
244 if (s
->thread_info
.soft_muted
|| pa_cvolume_is_muted(&s
->thread_info
.soft_volume
))
245 pa_silence_memchunk(&vchunk
, &s
->sample_spec
);
247 pa_volume_memchunk(&vchunk
, &s
->sample_spec
, &s
->thread_info
.soft_volume
);
249 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
250 pa_source_output_push(o
, &vchunk
);
252 pa_memblock_unref(vchunk
.memblock
);
255 while ((o
= pa_hashmap_iterate(s
->thread_info
.outputs
, &state
, NULL
)))
256 pa_source_output_push(o
, chunk
);
261 pa_usec_t
pa_source_get_latency(pa_source
*s
) {
264 pa_source_assert_ref(s
);
267 return s
->get_latency(s
);
269 if (pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_LATENCY
, &usec
, NULL
) < 0)
275 void pa_source_set_volume(pa_source
*s
, const pa_cvolume
*volume
) {
278 pa_source_assert_ref(s
);
281 changed
= !pa_cvolume_equal(volume
, &s
->volume
);
284 if (s
->set_volume
&& s
->set_volume(s
) < 0)
285 s
->set_volume
= NULL
;
288 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, volume
, 1), NULL
, pa_xfree
);
291 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
294 const pa_cvolume
*pa_source_get_volume(pa_source
*s
) {
295 pa_cvolume old_volume
;
296 pa_source_assert_ref(s
);
298 old_volume
= s
->volume
;
300 if (s
->get_volume
&& s
->get_volume(s
) < 0)
301 s
->get_volume
= NULL
;
303 if (!s
->get_volume
&& s
->refresh_volume
)
304 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_VOLUME
, &s
->volume
, NULL
);
306 if (!pa_cvolume_equal(&old_volume
, &s
->volume
))
307 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
312 void pa_source_set_mute(pa_source
*s
, int mute
) {
315 pa_source_assert_ref(s
);
317 changed
= s
->muted
!= mute
;
319 if (s
->set_mute
&& s
->set_mute(s
) < 0)
323 pa_asyncmsgq_post(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_SET_MUTE
, PA_UINT_TO_PTR(mute
), NULL
, NULL
);
326 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
329 int pa_source_get_mute(pa_source
*s
) {
332 pa_source_assert_ref(s
);
334 old_muted
= s
->muted
;
336 if (s
->get_mute
&& s
->get_mute(s
) < 0)
339 if (!s
->get_mute
&& s
->refresh_muted
)
340 pa_asyncmsgq_send(s
->asyncmsgq
, PA_MSGOBJECT(s
), PA_SOURCE_MESSAGE_GET_MUTE
, &s
->muted
, NULL
);
342 if (old_muted
!= s
->muted
)
343 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
348 void pa_source_set_module(pa_source
*s
, pa_module
*m
) {
349 pa_source_assert_ref(s
);
356 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
359 void pa_source_set_description(pa_source
*s
, const char *description
) {
360 pa_source_assert_ref(s
);
362 if (!description
&& !s
->description
)
365 if (description
&& s
->description
&& !strcmp(description
, s
->description
))
368 pa_xfree(s
->description
);
369 s
->description
= pa_xstrdup(description
);
371 pa_subscription_post(s
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
, s
->index
);
374 void pa_source_set_asyncmsgq(pa_source
*s
, pa_asyncmsgq
*q
) {
375 pa_source_assert_ref(s
);
381 unsigned pa_source_used_by(pa_source
*s
) {
382 pa_source_assert_ref(s
);
384 return pa_idxset_size(s
->outputs
);
387 int pa_source_process_msg(pa_msgobject
*o
, int code
, void *userdata
, pa_memchunk
*chunk
) {
388 pa_source
*s
= PA_SOURCE(o
);
389 pa_source_assert_ref(s
);
391 switch ((pa_source_message_t
) code
) {
392 case PA_SOURCE_MESSAGE_ADD_OUTPUT
: {
393 pa_source_output
*i
= userdata
;
394 pa_hashmap_put(s
->thread_info
.outputs
, PA_UINT32_TO_PTR(i
->index
), pa_source_output_ref(i
));
398 case PA_SOURCE_MESSAGE_REMOVE_OUTPUT
: {
399 pa_source_output
*i
= userdata
;
400 pa_hashmap_remove(s
->thread_info
.outputs
, PA_UINT32_TO_PTR(i
->index
));
404 case PA_SOURCE_MESSAGE_SET_VOLUME
:
405 s
->thread_info
.soft_volume
= *((pa_cvolume
*) userdata
);
408 case PA_SOURCE_MESSAGE_SET_MUTE
:
409 s
->thread_info
.soft_muted
= PA_PTR_TO_UINT(userdata
);
412 case PA_SOURCE_MESSAGE_GET_VOLUME
:
413 *((pa_cvolume
*) userdata
) = s
->thread_info
.soft_volume
;
416 case PA_SOURCE_MESSAGE_GET_MUTE
:
417 *((int*) userdata
) = s
->thread_info
.soft_muted
;
420 case PA_SOURCE_MESSAGE_PING
:
423 case PA_SOURCE_MESSAGE_SET_STATE
:
424 s
->thread_info
.state
= PA_PTR_TO_UINT(userdata
);
427 case PA_SOURCE_MESSAGE_GET_LATENCY
:
428 case PA_SOURCE_MESSAGE_MAX
: