2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
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.1 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
26 #include <pulsecore/macro.h>
27 #include <pulsecore/hashmap.h>
28 #include <pulsecore/hook-list.h>
29 #include <pulsecore/core.h>
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/sink-input.h>
32 #include <pulsecore/modargs.h>
34 #include "module-cork-music-on-phone-symdef.h"
36 PA_MODULE_AUTHOR("Lennart Poettering");
37 PA_MODULE_DESCRIPTION("Mute or cork music while a phone stream exists");
38 PA_MODULE_VERSION(PACKAGE_VERSION
);
39 PA_MODULE_LOAD_ONCE(TRUE
);
41 static const char* const valid_modargs
[] = {
47 pa_hashmap
*cork_state
;
50 *sink_input_unlink_slot
,
51 *sink_input_move_start_slot
,
52 *sink_input_move_finish_slot
;
55 static pa_bool_t
shall_cork(pa_sink
*s
, pa_sink_input
*ignore
) {
58 pa_sink_assert_ref(s
);
60 for (j
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); j
; j
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
))) {
66 if (!(role
= pa_proplist_gets(j
->proplist
, PA_PROP_MEDIA_ROLE
)))
69 if (pa_streq(role
, "phone")) {
70 pa_log_debug("Found a phone stream that will trigger the auto-cork.");
78 static void apply_cork(struct userdata
*u
, pa_sink
*s
, pa_sink_input
*ignore
, pa_bool_t cork
) {
83 pa_sink_assert_ref(s
);
85 for (j
= PA_SINK_INPUT(pa_idxset_first(s
->inputs
, &idx
)); j
; j
= PA_SINK_INPUT(pa_idxset_next(s
->inputs
, &idx
))) {
86 pa_bool_t corked
, muted
, corked_here
;
92 if (!(role
= pa_proplist_gets(j
->proplist
, PA_PROP_MEDIA_ROLE
)))
95 if (!pa_streq(role
, "video") &&
96 !pa_streq(role
, "music"))
99 corked
= (pa_sink_input_get_state(j
) == PA_SINK_INPUT_CORKED
);
100 muted
= pa_sink_input_get_mute(j
);
101 corked_here
= !!pa_hashmap_get(u
->cork_state
, j
);
103 if (cork
&& !corked
&& !muted
) {
104 pa_log_debug("Found a music/video stream that should be corked/muted.");
106 pa_hashmap_put(u
->cork_state
, j
, PA_INT_TO_PTR(1));
107 pa_sink_input_set_mute(j
, TRUE
, FALSE
);
108 pa_sink_input_send_event(j
, PA_STREAM_EVENT_REQUEST_CORK
, NULL
);
110 pa_hashmap_remove(u
->cork_state
, j
);
112 if (corked_here
&& (corked
|| muted
)) {
113 pa_log_debug("Found a music/video stream that should be uncorked/unmuted.");
115 pa_sink_input_set_mute(j
, FALSE
, FALSE
);
117 pa_sink_input_send_event(j
, PA_STREAM_EVENT_REQUEST_UNCORK
, NULL
);
123 static pa_hook_result_t
process(struct userdata
*u
, pa_sink_input
*i
, pa_bool_t create
) {
124 pa_bool_t cork
= FALSE
;
128 pa_sink_input_assert_ref(i
);
131 pa_hashmap_remove(u
->cork_state
, i
);
133 if (!(role
= pa_proplist_gets(i
->proplist
, PA_PROP_MEDIA_ROLE
)))
136 if (!pa_streq(role
, "phone") &&
137 !pa_streq(role
, "music") &&
138 !pa_streq(role
, "video"))
141 cork
= shall_cork(i
->sink
, create
? NULL
: i
);
142 apply_cork(u
, i
->sink
, create
? NULL
: i
, cork
);
147 static pa_hook_result_t
sink_input_put_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
148 pa_core_assert_ref(core
);
149 pa_sink_input_assert_ref(i
);
151 return process(u
, i
, TRUE
);
154 static pa_hook_result_t
sink_input_unlink_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
155 pa_sink_input_assert_ref(i
);
157 return process(u
, i
, FALSE
);
160 static pa_hook_result_t
sink_input_move_start_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
161 pa_core_assert_ref(core
);
162 pa_sink_input_assert_ref(i
);
164 return process(u
, i
, FALSE
);
167 static pa_hook_result_t
sink_input_move_finish_cb(pa_core
*core
, pa_sink_input
*i
, struct userdata
*u
) {
168 pa_core_assert_ref(core
);
169 pa_sink_input_assert_ref(i
);
171 return process(u
, i
, TRUE
);
174 int pa__init(pa_module
*m
) {
175 pa_modargs
*ma
= NULL
;
180 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
181 pa_log("Failed to parse module arguments");
185 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
188 u
->cork_state
= pa_hashmap_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
190 u
->sink_input_put_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_PUT
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_put_cb
, u
);
191 u
->sink_input_unlink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_UNLINK
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_unlink_cb
, u
);
192 u
->sink_input_move_start_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_START
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_move_start_cb
, u
);
193 u
->sink_input_move_finish_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_move_finish_cb
, u
);
210 void pa__done(pa_module
*m
) {
215 if (!(u
= m
->userdata
))
218 if (u
->sink_input_put_slot
)
219 pa_hook_slot_free(u
->sink_input_put_slot
);
220 if (u
->sink_input_unlink_slot
)
221 pa_hook_slot_free(u
->sink_input_unlink_slot
);
222 if (u
->sink_input_move_start_slot
)
223 pa_hook_slot_free(u
->sink_input_move_start_slot
);
224 if (u
->sink_input_move_finish_slot
)
225 pa_hook_slot_free(u
->sink_input_move_finish_slot
);
228 pa_hashmap_free(u
->cork_state
, NULL
, NULL
);