Fix up some double spaces
[pulseaudio-raopUDP/pulseaudio-raop-alac.git] / src / modules / module-cork-music-on-phone.c
blobb629f06f343de4f073296515a7746976d807c302
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
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[] = {
42 NULL
45 struct userdata {
46 pa_core *core;
47 pa_hashmap *cork_state;
48 pa_hook_slot
49 *sink_input_put_slot,
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) {
56 pa_sink_input *j;
57 uint32_t idx;
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))) {
61 const char *role;
63 if (j == ignore)
64 continue;
66 if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
67 continue;
69 if (pa_streq(role, "phone")) {
70 pa_log_debug("Found a phone stream that will trigger the auto-cork.");
71 return TRUE;
75 return FALSE;
78 static void apply_cork(struct userdata *u, pa_sink *s, pa_sink_input *ignore, pa_bool_t cork) {
79 pa_sink_input *j;
80 uint32_t idx;
82 pa_assert(u);
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;
87 const char *role;
89 if (j == ignore)
90 continue;
92 if (!(role = pa_proplist_gets(j->proplist, PA_PROP_MEDIA_ROLE)))
93 continue;
95 if (!pa_streq(role, "video") &&
96 !pa_streq(role, "music"))
97 continue;
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.");
105 if (!corked_here)
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);
109 } else if (!cork) {
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.");
114 if (muted)
115 pa_sink_input_set_mute(j, FALSE, FALSE);
116 if (corked)
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;
125 const char *role;
127 pa_assert(u);
128 pa_sink_input_assert_ref(i);
130 if (!create)
131 pa_hashmap_remove(u->cork_state, i);
133 if (!(role = pa_proplist_gets(i->proplist, PA_PROP_MEDIA_ROLE)))
134 return PA_HOOK_OK;
136 if (!pa_streq(role, "phone") &&
137 !pa_streq(role, "music") &&
138 !pa_streq(role, "video"))
139 return PA_HOOK_OK;
141 cork = shall_cork(i->sink, create ? NULL : i);
142 apply_cork(u, i->sink, create ? NULL : i, cork);
144 return PA_HOOK_OK;
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;
176 struct userdata *u;
178 pa_assert(m);
180 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
181 pa_log("Failed to parse module arguments");
182 goto fail;
185 m->userdata = u = pa_xnew(struct userdata, 1);
187 u->core = m->core;
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);
195 pa_modargs_free(ma);
197 return 0;
199 fail:
200 pa__done(m);
202 if (ma)
203 pa_modargs_free(ma);
205 return -1;
210 void pa__done(pa_module *m) {
211 struct userdata* u;
213 pa_assert(m);
215 if (!(u = m->userdata))
216 return;
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);
227 if (u->cork_state)
228 pa_hashmap_free(u->cork_state, NULL, NULL);
230 pa_xfree(u);