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