[pulseaudio-commits] r1603 - /branches/lennart/src/modules/module-suspend-on-idle.c

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Thu Aug 9 14:08:11 PDT 2007


Author: lennart
Date: Thu Aug  9 23:08:10 2007
New Revision: 1603

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=3D1603&root=3Dpulseaudio&vi=
ew=3Drev
Log:
forgot to actually add the new suspend-on-idle module source code

Added:
    branches/lennart/src/modules/module-suspend-on-idle.c   (with props)

Added: branches/lennart/src/modules/module-suspend-on-idle.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/modules/mo=
dule-suspend-on-idle.c?rev=3D1603&root=3Dpulseaudio&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/modules/module-suspend-on-idle.c (added)
+++ branches/lennart/src/modules/module-suspend-on-idle.c Thu Aug  9 23:08:=
10 2007
@@ -1,0 +1,303 @@
+/* $Id$ */
+
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2006 Lennart Poettering
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2 of the License,
+  or (at your option) any later version.
+
+  PulseAudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulse/xmalloc.h>
+#include <pulse/timeval.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/sink-input.h>
+#include <pulsecore/source-output.h>
+#include <pulsecore/modargs.h>
+#include <pulsecore/log.h>
+#include <pulsecore/namereg.h>
+
+#include "module-suspend-on-idle-symdef.h"
+
+PA_MODULE_AUTHOR("Lennart Poettering")
+PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it=
")
+PA_MODULE_VERSION(PACKAGE_VERSION)
+
+static const char* const valid_modargs[] =3D {
+    "timeout",
+    NULL,
+};
+
+struct userdata {
+    pa_core *core;
+    pa_usec_t timeout;
+    pa_hashmap *device_infos;
+    pa_hook_slot *sink_new_slot, *source_new_slot, *sink_disconnect_slot, =
*source_disconnect_slot;
+    pa_hook_slot *sink_input_new_slot, *source_output_new_slot, *sink_inpu=
t_disconnect_slot, *source_output_disconnect_slot;
+};
+
+struct device_info {
+    struct userdata *userdata;
+    pa_sink *sink;
+    pa_source *source;
+    struct timeval last_use;
+    pa_time_event *time_event;
+};
+
+static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct t=
imeval *tv, void *userdata) {
+    struct device_info *d =3D userdata;
+
+    pa_assert(d);
+
+    d->userdata->core->mainloop->time_restart(d->time_event, NULL);
+    =

+    if (d->sink && pa_sink_used_by(d->sink) <=3D 0) {
+        pa_log_info("Sink %s idle for too long, suspending ...", d->sink->=
name);
+        pa_sink_suspend(d->sink, 1);
+        pa_source_suspend(d->sink->monitor_source, 1);
+    }
+
+    if (d->source && pa_source_used_by(d->source) <=3D 0) {
+        pa_log_info("Source %s idle for too long, suspending ...", d->sour=
ce->name);
+        pa_source_suspend(d->source, 1);
+    }
+}
+
+static void restart(struct device_info *d) {
+    struct timeval tv;
+    pa_assert(d);
+
+    pa_gettimeofday(&tv);
+    d->last_use =3D tv;
+    pa_timeval_add(&tv, d->userdata->timeout*1000000);
+    d->userdata->core->mainloop->time_restart(d->time_event, &tv);
+
+    if (d->source)
+        pa_log_debug("Source %s becomes idle.", d->source->name);
+    if (d->sink)
+        pa_log_debug("Sink %s becomes idle.", d->sink->name);
+}
+
+static pa_hook_result_t sink_input_new_hook_cb(pa_core *c, pa_sink_input *=
s, struct userdata *u) {
+    struct device_info *d;
+    =

+    pa_assert(c);
+    pa_sink_input_assert_ref(s);
+    pa_assert(u);
+    =

+    pa_assert_se((d =3D pa_hashmap_get(u->device_infos, s->sink)));
+    d->userdata->core->mainloop->time_restart(d->time_event, NULL);
+
+    pa_sink_suspend(s->sink, 0);
+    pa_source_suspend(s->sink->monitor_source, 0);
+
+    pa_log_debug("Sink %s becomes busy.", s->sink->name);
+    =

+    return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_new_hook_cb(pa_core *c, pa_source_ou=
tput *s, struct userdata *u) {
+    struct device_info *d;
+
+    pa_assert(c);
+    pa_source_output_assert_ref(s);
+    pa_assert(u);
+    =

+    pa_assert_se((d =3D pa_hashmap_get(u->device_infos, s->source)));
+    d->userdata->core->mainloop->time_restart(d->time_event, NULL);
+
+    pa_source_suspend(s->source, 0);
+    if (s->source->monitor_of)
+        pa_sink_suspend(s->source->monitor_of, 0);
+
+    pa_log_debug("Source %s becomes busy.", s->source->name);
+    =

+    return PA_HOOK_OK;
+}
+
+static pa_hook_result_t sink_input_disconnect_hook_cb(pa_core *c, pa_sink_=
input *s, struct userdata *u) {
+    pa_assert(c);
+    pa_sink_input_assert_ref(s);
+    pa_assert(u);
+
+    if (pa_sink_used_by(s->sink) <=3D 0) {
+        struct device_info *d;
+        pa_assert_se((d =3D pa_hashmap_get(u->device_infos, s->sink)));
+        restart(d);
+    }
+    =

+    return PA_HOOK_OK;
+}
+
+static pa_hook_result_t source_output_disconnect_hook_cb(pa_core *c, pa_so=
urce_output *s, struct userdata *u) {
+    pa_assert(c);
+    pa_source_output_assert_ref(s);
+    pa_assert(u);
+
+    if (pa_source_used_by(s->source) <=3D 0) {
+        struct device_info *d;
+        pa_assert_se((d =3D pa_hashmap_get(u->device_infos, s->source)));
+        restart(d);
+    }
+    =

+    return PA_HOOK_OK;
+}
+
+static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struc=
t userdata *u) {
+    struct device_info *d;
+    =

+    pa_assert(c);
+    pa_object_assert_ref(o);
+    pa_assert(u);
+
+    d =3D pa_xnew(struct device_info, 1);
+    d->userdata =3D u;
+    d->source =3D pa_source_isinstance(o) ? pa_source_ref(PA_SOURCE(o)) : =
NULL;
+    d->sink =3D pa_sink_isinstance(o) ? pa_sink_ref(PA_SINK(o)) : NULL;
+    pa_assert(d->source || d->sink);
+    d->time_event =3D c->mainloop->time_new(c->mainloop, NULL, timeout_cb,=
 d);
+    pa_hashmap_put(u->device_infos, o, d);
+
+    if ((d->sink && pa_sink_used_by(d->sink) <=3D 0) ||
+        (d->source && pa_source_used_by(d->source) <=3D 0))
+        restart(d);
+
+    return PA_HOOK_OK;
+}
+
+static void device_info_free(struct device_info *d) {
+    pa_assert(d);
+    =

+    if (d->source)
+        pa_source_unref(d->source);
+    if (d->sink)
+        pa_sink_unref(d->sink);
+    =

+    d->userdata->core->mainloop->time_free(d->time_event);
+    =

+    pa_xfree(d);
+}
+
+static pa_hook_result_t device_disconnect_hook_cb(pa_core *c, pa_object *o=
, struct userdata *u) {
+    struct device_info *d;
+    =

+    pa_assert(c);
+    pa_object_assert_ref(o);
+    pa_assert(u);
+
+    pa_assert_se((d =3D pa_hashmap_remove(u->device_infos, o)));
+    device_info_free(d);
+
+    return PA_HOOK_OK;
+}
+
+int pa__init(pa_core *c, pa_module*m) {
+    pa_modargs *ma =3D NULL;
+    struct userdata *u;
+    uint32_t timeout =3D 5;
+    uint32_t idx;
+    pa_sink *sink;
+    pa_source *source;
+
+    assert(c);
+    assert(m);
+
+    if (!(ma =3D pa_modargs_new(m->argument, valid_modargs))) {
+        pa_log("Failed to parse module arguments.");
+        goto fail;
+    }
+
+    if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
+        pa_log("Failed to parse timeout value.");
+        goto fail;
+    }
+    =

+    m->userdata =3D u =3D pa_xnew(struct userdata, 1);
+    u->core =3D c;
+    u->timeout =3D timeout;
+    u->device_infos =3D pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idx=
set_trivial_compare_func);
+
+    for (sink =3D pa_idxset_first(c->sinks, &idx); sink; sink =3D pa_idxse=
t_next(c->sinks, &idx))
+        device_new_hook_cb(c, PA_OBJECT(sink), u);
+
+    for (source =3D pa_idxset_first(c->sources, &idx); source; source =3D =
pa_idxset_next(c->sources, &idx))
+        device_new_hook_cb(c, PA_OBJECT(source), u);
+
+    u->sink_new_slot =3D pa_hook_connect(&c->hook_sink_new_post, (pa_hook_=
cb_t) device_new_hook_cb, u);
+    u->source_new_slot =3D pa_hook_connect(&c->hook_source_new_post, (pa_h=
ook_cb_t) device_new_hook_cb, u);
+    u->sink_disconnect_slot =3D pa_hook_connect(&c->hook_sink_disconnect_p=
ost, (pa_hook_cb_t) device_disconnect_hook_cb, u);
+    u->source_disconnect_slot =3D pa_hook_connect(&c->hook_source_disconne=
ct_post, (pa_hook_cb_t) device_disconnect_hook_cb, u);
+
+    u->sink_input_new_slot =3D pa_hook_connect(&c->hook_sink_input_new_pos=
t, (pa_hook_cb_t) sink_input_new_hook_cb, u);
+    u->source_output_new_slot =3D pa_hook_connect(&c->hook_source_output_n=
ew_post, (pa_hook_cb_t) source_output_new_hook_cb, u);
+    u->sink_input_disconnect_slot =3D pa_hook_connect(&c->hook_sink_input_=
disconnect_post, (pa_hook_cb_t) sink_input_disconnect_hook_cb, u);
+    u->source_output_disconnect_slot =3D pa_hook_connect(&c->hook_source_o=
utput_disconnect_post, (pa_hook_cb_t) source_output_disconnect_hook_cb, u);
+    =

+    pa_modargs_free(ma);
+    return 0;
+
+fail:
+    =

+    if (ma)
+        pa_modargs_free(ma);
+    =

+    return -1;
+}
+
+void pa__done(pa_core *c, pa_module*m) {
+    struct userdata *u;
+    struct device_info *d;
+    =

+    assert(c);
+    assert(m);
+
+    if (!m->userdata)
+        return;
+
+    u =3D m->userdata;
+    =

+    if (u->sink_new_slot)
+        pa_hook_slot_free(u->sink_new_slot);
+    if (u->sink_disconnect_slot)
+        pa_hook_slot_free(u->sink_disconnect_slot);
+
+    if (u->source_new_slot)
+        pa_hook_slot_free(u->source_new_slot);
+    if (u->source_disconnect_slot)
+        pa_hook_slot_free(u->source_disconnect_slot);
+
+    if (u->sink_input_new_slot)
+        pa_hook_slot_free(u->sink_input_new_slot);
+    if (u->sink_input_disconnect_slot)
+        pa_hook_slot_free(u->sink_input_disconnect_slot);
+
+    if (u->source_output_new_slot)
+        pa_hook_slot_free(u->source_output_new_slot);
+    if (u->source_output_disconnect_slot)
+        pa_hook_slot_free(u->source_output_disconnect_slot);
+
+    while ((d =3D pa_hashmap_steal_first(u->device_infos)))
+        device_info_free(d);
+
+    pa_hashmap_free(u->device_infos, NULL, NULL);
+    =

+    pa_xfree(u);
+}

Propchange: branches/lennart/src/modules/module-suspend-on-idle.c
---------------------------------------------------------------------------=
---
    svn:keywords =3D Id




More information about the pulseaudio-commits mailing list