]> code.delx.au - pulseaudio/blob - src/modules/module-stream-restore.c
store channel map in database and remap volumes if necessary
[pulseaudio] / src / modules / module-stream-restore.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2008 Lennart Poettering
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <gdbm.h>
34
35 #include <pulse/xmalloc.h>
36 #include <pulse/volume.h>
37 #include <pulse/timeval.h>
38 #include <pulse/util.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/core-subscribe.h>
46 #include <pulsecore/sink-input.h>
47 #include <pulsecore/source-output.h>
48 #include <pulsecore/namereg.h>
49
50 #include "module-stream-restore-symdef.h"
51
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("Automatically restore the volume/mute/device state of streams");
54 PA_MODULE_VERSION(PACKAGE_VERSION);
55 PA_MODULE_LOAD_ONCE(TRUE);
56
57 #define SAVE_INTERVAL 10
58
59 static const char* const valid_modargs[] = {
60 "restore_device",
61 "restore_volume",
62 "restore_muted",
63 NULL
64 };
65
66 struct userdata {
67 pa_core *core;
68 pa_module *module;
69 pa_subscription *subscription;
70 pa_hook_slot
71 *sink_input_new_hook_slot,
72 *sink_input_fixate_hook_slot,
73 *source_output_new_hook_slot;
74 pa_time_event *save_time_event;
75 GDBM_FILE gdbm_file;
76
77 pa_bool_t restore_device:1;
78 pa_bool_t restore_volume:1;
79 pa_bool_t restore_muted:1;
80 };
81
82 struct entry {
83 char device[PA_NAME_MAX];
84 pa_channel_map channel_map;
85 pa_cvolume volume;
86 pa_bool_t muted:1;
87 };
88
89 static void save_time_callback(pa_mainloop_api*a, pa_time_event* e, const struct timeval *tv, void *userdata) {
90 struct userdata *u = userdata;
91
92 pa_assert(a);
93 pa_assert(e);
94 pa_assert(tv);
95 pa_assert(u);
96
97 pa_assert(e == u->save_time_event);
98 u->core->mainloop->time_free(u->save_time_event);
99 u->save_time_event = NULL;
100
101 gdbm_sync(u->gdbm_file);
102 pa_log_info("Synced.");
103 }
104
105 static char *get_name(pa_proplist *p, const char *prefix) {
106 const char *r;
107
108 if (!p)
109 return NULL;
110
111 if ((r = pa_proplist_gets(p, PA_PROP_MEDIA_ROLE)))
112 return pa_sprintf_malloc("%s-by-media-role:%s", prefix, r);
113 else if ((r = pa_proplist_gets(p, PA_PROP_APPLICATION_ID)))
114 return pa_sprintf_malloc("%s-by-application-id:%s", prefix, r);
115 else if ((r = pa_proplist_gets(p, PA_PROP_APPLICATION_NAME)))
116 return pa_sprintf_malloc("%s-by-application-name:%s", prefix, r);
117 else if ((r = pa_proplist_gets(p, PA_PROP_MEDIA_NAME)))
118 return pa_sprintf_malloc("%s-by-media-name:%s", prefix, r);
119
120 return NULL;
121 }
122
123 static struct entry* read_entry(struct userdata *u, char *name) {
124 datum key, data;
125 struct entry *e;
126
127 pa_assert(u);
128 pa_assert(name);
129
130 key.dptr = name;
131 key.dsize = strlen(name);
132
133 data = gdbm_fetch(u->gdbm_file, key);
134
135 if (!data.dptr)
136 goto fail;
137
138 if (data.dsize != sizeof(struct entry)) {
139 pa_log_warn("Database contains entry for stream %s of wrong size %lu != %lu", name, (unsigned long) data.dsize, (unsigned long) sizeof(struct entry));
140 goto fail;
141 }
142
143 e = (struct entry*) data.dptr;
144
145 if (!memchr(e->device, 0, sizeof(e->device))) {
146 pa_log_warn("Database contains entry for stream %s with missing NUL byte in device name", name);
147 goto fail;
148 }
149
150 if (!(pa_cvolume_valid(&e->volume))) {
151 pa_log_warn("Invalid volume stored in database for stream %s", name);
152 goto fail;
153 }
154
155 if (!(pa_channel_map_valid(&e->channel_map))) {
156 pa_log_warn("Invalid channel map stored in database for stream %s", name);
157 goto fail;
158 }
159
160 if (e->volume.channels != e->channel_map.channels) {
161 pa_log_warn("Volume and channel map don't match in database entry for stream %s", name);
162 goto fail;
163 }
164
165 return e;
166
167 fail:
168
169 pa_xfree(data.dptr);
170 return NULL;
171 }
172
173 static void subscribe_callback(pa_core *c, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
174 struct userdata *u = userdata;
175 struct entry entry, *old;
176 char *name;
177 datum key, data;
178
179 pa_assert(c);
180 pa_assert(u);
181
182 if (t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
183 t != (PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_CHANGE) &&
184 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW) &&
185 t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE))
186 return;
187
188 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
189 pa_sink_input *sink_input;
190
191 if (!(sink_input = pa_idxset_get_by_index(c->sink_inputs, idx)))
192 return;
193
194 if (!(name = get_name(sink_input->proplist, "sink-input")))
195 return;
196
197 entry.channel_map = sink_input->channel_map;
198 entry.volume = *pa_sink_input_get_volume(sink_input);
199 entry.muted = pa_sink_input_get_mute(sink_input);
200 pa_strlcpy(entry.device, sink_input->sink->name, sizeof(entry.device));
201
202 } else {
203 pa_source_output *source_output;
204
205 pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT);
206
207 if (!(source_output = pa_idxset_get_by_index(c->source_outputs, idx)))
208 return;
209
210 if (!(name = get_name(source_output->proplist, "source-output")))
211 return;
212
213 /* The following fields are filled in to make the entry valid
214 * according to read_entry(). They are otherwise useless */
215 entry.channel_map = source_output->channel_map;
216 pa_cvolume_reset(&entry.volume, entry.channel_map.channels);
217 pa_strlcpy(entry.device, source_output->source->name, sizeof(entry.device));
218 }
219
220 if ((old = read_entry(u, name))) {
221
222 if (pa_cvolume_equal(pa_cvolume_remap(&old->volume, &old->channel_map, &entry.channel_map), &entry.volume) &&
223 !old->muted == !entry.muted &&
224 strcmp(old->device, entry.device) == 0) {
225
226 pa_xfree(old);
227 pa_xfree(name);
228 return;
229 }
230
231 pa_xfree(old);
232 }
233
234 key.dptr = name;
235 key.dsize = strlen(name);
236
237 data.dptr = (void*) &entry;
238 data.dsize = sizeof(entry);
239
240 pa_log_info("Storing volume/mute/device for stream %s.", name);
241
242 gdbm_store(u->gdbm_file, key, data, GDBM_REPLACE);
243
244 if (!u->save_time_event) {
245 struct timeval tv;
246 pa_gettimeofday(&tv);
247 tv.tv_sec += SAVE_INTERVAL;
248 u->save_time_event = u->core->mainloop->time_new(u->core->mainloop, &tv, save_time_callback, u);
249 }
250
251 pa_xfree(name);
252 }
253
254 static pa_hook_result_t sink_input_new_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
255 char *name;
256 struct entry *e;
257
258 pa_assert(new_data);
259
260 if (!(name = get_name(new_data->proplist, "sink-input")))
261 return PA_HOOK_OK;
262
263 if ((e = read_entry(u, name))) {
264 pa_sink *s;
265
266 if (u->restore_device &&
267 e->device[0] &&
268 (s = pa_namereg_get(c, e->device, PA_NAMEREG_SINK, TRUE))) {
269
270 pa_log_info("Restoring device for stream %s.", name);
271 new_data->sink = s;
272 }
273
274 pa_xfree(e);
275 }
276
277 pa_xfree(name);
278
279 return PA_HOOK_OK;
280 }
281
282 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *c, pa_sink_input_new_data *new_data, struct userdata *u) {
283 char *name;
284 struct entry *e;
285
286 pa_assert(new_data);
287
288 if (!(name = get_name(new_data->proplist, "sink-input")))
289 return PA_HOOK_OK;
290
291 if ((e = read_entry(u, name))) {
292
293 if (u->restore_volume) {
294 pa_log_info("Restoring volume for sink input %s.", name);
295 pa_sink_input_new_data_set_volume(new_data, pa_cvolume_remap(&e->volume, &e->channel_map, &new_data->channel_map));
296 }
297
298 if (u->restore_muted) {
299 pa_log_info("Restoring mute state for sink input %s.", name);
300 pa_sink_input_new_data_set_muted(new_data, e->muted);
301 }
302
303 pa_xfree(e);
304 }
305
306 pa_xfree(name);
307
308 return PA_HOOK_OK;
309 }
310
311 static pa_hook_result_t source_output_new_hook_callback(pa_core *c, pa_source_output_new_data *new_data, struct userdata *u) {
312 char *name;
313 struct entry *e;
314
315 pa_assert(new_data);
316
317 if (!(name = get_name(new_data->proplist, "source-output")))
318 return PA_HOOK_OK;
319
320 if ((e = read_entry(u, name))) {
321 pa_source *s;
322
323 if (u->restore_device &&
324 e->device[0] &&
325 (s = pa_namereg_get(c, e->device, PA_NAMEREG_SOURCE, TRUE))) {
326
327 pa_log_info("Restoring device for stream %s.", name);
328 new_data->source = s;
329 }
330
331 pa_xfree(e);
332 }
333
334 pa_xfree(name);
335
336 return PA_HOOK_OK;
337 }
338
339 int pa__init(pa_module*m) {
340 pa_modargs *ma = NULL;
341 struct userdata *u;
342 char *fname, *fn;
343 char hn[256];
344 pa_sink_input *si;
345 pa_source_output *so;
346 uint32_t idx;
347 pa_bool_t restore_device = TRUE, restore_volume = TRUE, restore_muted = TRUE;
348
349 pa_assert(m);
350
351 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
352 pa_log("Failed to parse module arguments");
353 goto fail;
354 }
355
356 if (pa_modargs_get_value_boolean(ma, "restore_device", &restore_device) < 0 ||
357 pa_modargs_get_value_boolean(ma, "restore_volume", &restore_volume) < 0 ||
358 pa_modargs_get_value_boolean(ma, "restore_muted", &restore_muted) < 0) {
359 pa_log("restore_device=, restore_volume= and restore_muted= expect boolean arguments");
360 goto fail;
361 }
362
363 if (!restore_muted && !restore_volume && !restore_device)
364 pa_log_warn("Neither restoring volume, nor restoring muted, nor restoring device enabled!");
365
366 m->userdata = u = pa_xnew(struct userdata, 1);
367 u->core = m->core;
368 u->module = m;
369 u->save_time_event = NULL;
370 u->restore_device = restore_device;
371 u->restore_volume = restore_volume;
372 u->restore_muted = restore_muted;
373
374 u->subscription = pa_subscription_new(m->core, PA_SUBSCRIPTION_MASK_SINK_INPUT|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, subscribe_callback, u);
375
376 if (restore_device) {
377 u->sink_input_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_new_hook_callback, u);
378 u->source_output_new_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_NEW], PA_HOOK_EARLY, (pa_hook_cb_t) source_output_new_hook_callback, u);
379 }
380
381 if (restore_volume || restore_muted)
382 u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_fixate_hook_callback, u);
383
384 if (!pa_get_host_name(hn, sizeof(hn)))
385 goto fail;
386
387 fn = pa_sprintf_malloc("stream-volumes.%s."CANONICAL_HOST".gdbm", hn);
388 fname = pa_state_path(fn);
389 pa_xfree(fn);
390
391 if (!fname)
392 goto fail;
393
394 if (!(u->gdbm_file = gdbm_open(fname, 0, GDBM_WRCREAT, 0600, NULL))) {
395 pa_log("Failed to open volume database '%s': %s", fname, gdbm_strerror(gdbm_errno));
396 pa_xfree(fname);
397 goto fail;
398 }
399
400 pa_log_info("Sucessfully opened database file '%s'.", fname);
401 pa_xfree(fname);
402
403 for (si = pa_idxset_first(m->core->sink_inputs, &idx); si; si = pa_idxset_next(m->core->sink_inputs, &idx))
404 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SINK_INPUT|PA_SUBSCRIPTION_EVENT_NEW, si->index, u);
405
406 for (so = pa_idxset_first(m->core->source_outputs, &idx); so; so = pa_idxset_next(m->core->source_outputs, &idx))
407 subscribe_callback(m->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, so->index, u);
408
409 pa_modargs_free(ma);
410 return 0;
411
412 fail:
413 pa__done(m);
414
415 if (ma)
416 pa_modargs_free(ma);
417
418 return -1;
419 }
420
421 void pa__done(pa_module*m) {
422 struct userdata* u;
423
424 pa_assert(m);
425
426 if (!(u = m->userdata))
427 return;
428
429 if (u->subscription)
430 pa_subscription_free(u->subscription);
431
432 if (u->sink_input_new_hook_slot)
433 pa_hook_slot_free(u->sink_input_new_hook_slot);
434 if (u->sink_input_fixate_hook_slot)
435 pa_hook_slot_free(u->sink_input_fixate_hook_slot);
436 if (u->source_output_new_hook_slot)
437 pa_hook_slot_free(u->source_output_new_hook_slot);
438
439 if (u->save_time_event)
440 u->core->mainloop->time_free(u->save_time_event);
441
442 if (u->gdbm_file)
443 gdbm_close(u->gdbm_file);
444
445 pa_xfree(u);
446 }