]> code.delx.au - pulseaudio/blob - src/modules/module-flat-volume.c
flat-volume: use pa_sink_get_volume(s, TRUE) to work with slaved sink
[pulseaudio] / src / modules / module-flat-volume.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
5 Copyright 2004-2006, 2008 Lennart Poettering
6
7 Contact: Marc-Andre Lureau <marc-andre.lureau@nokia.com>
8
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.
13
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.
18
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
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <regex.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <pulse/xmalloc.h>
38
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/sink-input.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/macro.h>
46
47 #include "module-flat-volume-symdef.h"
48
49 PA_MODULE_AUTHOR("Marc-Andre Lureau");
50 PA_MODULE_DESCRIPTION("Flat volume");
51 PA_MODULE_VERSION(PACKAGE_VERSION);
52 PA_MODULE_LOAD_ONCE(TRUE);
53 PA_MODULE_USAGE("");
54
55 struct userdata {
56 pa_subscription *subscription;
57 pa_hook_slot *sink_input_set_volume_hook_slot;
58 pa_hook_slot *sink_input_fixate_hook_slot;
59 };
60
61 static void process_input_volume_change(
62 pa_cvolume *dest_volume,
63 const pa_cvolume *dest_virtual_volume,
64 pa_channel_map *dest_channel_map,
65 pa_sink_input *this,
66 pa_sink *sink) {
67
68 pa_sink_input *i;
69 uint32_t idx;
70 pa_cvolume max_volume, sink_volume;
71
72 pa_assert(dest_volume);
73 pa_assert(dest_virtual_volume);
74 pa_assert(dest_channel_map);
75 pa_assert(sink);
76
77 if (!(sink->flags & PA_SINK_DECIBEL_VOLUME))
78 return;
79
80 pa_log_debug("Sink input volume changed");
81
82 max_volume = *dest_virtual_volume;
83 pa_cvolume_remap(&max_volume, dest_channel_map, &sink->channel_map);
84
85 for (i = PA_SINK_INPUT(pa_idxset_first(sink->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(sink->inputs, &idx))) {
86 /* skip this sink-input if we are processing a volume change request */
87 if (this && this == i)
88 continue;
89
90 if (pa_cvolume_max(&i->virtual_volume) > pa_cvolume_max(&max_volume)) {
91 max_volume = i->virtual_volume;
92 pa_cvolume_remap(&max_volume, &i->channel_map, &sink->channel_map);
93 }
94 }
95
96 /* Set the master volume, and normalize inputs */
97 if (!pa_cvolume_equal(&max_volume, pa_sink_get_volume(sink, TRUE))) {
98
99 pa_sink_set_volume(sink, &max_volume);
100
101 pa_log_debug("sink = %.2f (changed)", (double)pa_cvolume_avg(pa_sink_get_volume(sink, TRUE))/PA_VOLUME_NORM);
102
103 /* Now, normalize each of the internal volume (client sink-input volume / sink master volume) */
104 for (i = PA_SINK_INPUT(pa_idxset_first(sink->inputs, &idx)); i; i = PA_SINK_INPUT(pa_idxset_next(sink->inputs, &idx))) {
105 /* skip this sink-input if we are processing a volume change request */
106 if (this && this == i)
107 continue;
108
109 sink_volume = max_volume;
110 pa_cvolume_remap(&sink_volume, &sink->channel_map, &i->channel_map);
111 pa_sw_cvolume_divide(&i->volume, &i->virtual_volume, &sink_volume);
112 pa_log_debug("sink input { id = %d, flat = %.2f, true = %.2f }",
113 i->index,
114 (double)pa_cvolume_avg(&i->virtual_volume)/PA_VOLUME_NORM,
115 (double)pa_cvolume_avg(&i->volume)/PA_VOLUME_NORM);
116 pa_asyncmsgq_post(i->sink->asyncmsgq, PA_MSGOBJECT(i), PA_SINK_INPUT_MESSAGE_SET_VOLUME, pa_xnewdup(struct pa_cvolume, &i->volume, 1), 0, NULL, pa_xfree);
117 }
118 } else
119 pa_log_debug("sink = %.2f", (double)pa_cvolume_avg(pa_sink_get_volume(sink, TRUE))/PA_VOLUME_NORM);
120
121 /* and this one */
122
123 sink_volume = max_volume;
124 pa_cvolume_remap(&sink_volume, &sink->channel_map, dest_channel_map);
125 pa_sw_cvolume_divide(dest_volume, dest_virtual_volume, &sink_volume);
126 pa_log_debug("caller sink input: { id = %d, flat = %.2f, true = %.2f }",
127 this ? (int)this->index : -1,
128 (double)pa_cvolume_avg(dest_virtual_volume)/PA_VOLUME_NORM,
129 (double)pa_cvolume_avg(dest_volume)/PA_VOLUME_NORM);
130 }
131
132 static pa_hook_result_t sink_input_set_volume_hook_callback(pa_core *c, pa_sink_input_set_volume_data *this, struct userdata *u) {
133 pa_assert(this);
134 pa_assert(this->sink_input);
135
136 process_input_volume_change(&this->volume, &this->virtual_volume, &this->sink_input->channel_map,
137 this->sink_input, this->sink_input->sink);
138
139 return PA_HOOK_OK;
140 }
141
142 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *core, pa_sink_input_new_data *this, struct userdata *u) {
143 pa_assert(this);
144 pa_assert(this->sink);
145
146 process_input_volume_change(&this->volume, &this->virtual_volume, &this->channel_map,
147 NULL, this->sink);
148
149 return PA_HOOK_OK;
150 }
151
152 static void subscribe_callback(pa_core *core, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
153 struct userdata *u = userdata;
154 pa_sink *sink;
155 pa_sink_input *i;
156 uint32_t iidx;
157 pa_cvolume sink_volume;
158
159 pa_assert(core);
160 pa_assert(u);
161
162 if (t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_NEW) &&
163 t != (PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE))
164 return;
165
166 if (!(sink = pa_idxset_get_by_index(core->sinks, idx)))
167 return;
168
169 if (!(sink->flags & PA_SINK_DECIBEL_VOLUME))
170 return;
171
172 pa_log_debug("Sink volume changed");
173 pa_log_debug("sink = %.2f", (double)pa_cvolume_avg(pa_sink_get_volume(sink, TRUE)) / PA_VOLUME_NORM);
174
175 sink_volume = *pa_sink_get_volume(sink, TRUE);
176
177 for (i = PA_SINK_INPUT(pa_idxset_first(sink->inputs, &iidx)); i; i = PA_SINK_INPUT(pa_idxset_next(sink->inputs, &iidx))) {
178 pa_cvolume si_volume;
179
180 si_volume = sink_volume;
181 pa_cvolume_remap(&si_volume, &sink->channel_map, &i->channel_map);
182 pa_sw_cvolume_multiply(&i->virtual_volume, &i->volume, &si_volume);
183 pa_log_debug("sink input = { id = %d, flat = %.2f, true = %.2f }",
184 i->index,
185 (double)pa_cvolume_avg(&i->virtual_volume)/PA_VOLUME_NORM,
186 (double)pa_cvolume_avg(&i->volume)/PA_VOLUME_NORM);
187 pa_subscription_post(i->sink->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE, i->index);
188 }
189 }
190
191 int pa__init(pa_module*m) {
192 struct userdata *u;
193
194 pa_assert(m);
195
196 u = pa_xnew(struct userdata, 1);
197 m->userdata = u;
198
199 u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_fixate_hook_callback, u);
200 u->sink_input_set_volume_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME], PA_HOOK_LATE, (pa_hook_cb_t) sink_input_set_volume_hook_callback, u);
201
202 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK, subscribe_callback, u);
203
204 return 0;
205 }
206
207 void pa__done(pa_module*m) {
208 struct userdata* u;
209
210 pa_assert(m);
211
212 if (!(u = m->userdata))
213 return;
214
215 if (u->subscription)
216 pa_subscription_free(u->subscription);
217
218 if (u->sink_input_set_volume_hook_slot)
219 pa_hook_slot_free(u->sink_input_set_volume_hook_slot);
220 if (u->sink_input_fixate_hook_slot)
221 pa_hook_slot_free(u->sink_input_fixate_hook_slot);
222
223 pa_xfree(u);
224 }