pipewire/src/modules/module-suspend-on-idle.c

216 lines
5.3 KiB
C
Raw Normal View History

2017-05-23 19:15:33 +02:00
/* PipeWire
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "config.h"
2017-07-11 15:57:20 +02:00
#include "pipewire/core.h"
2017-08-08 19:55:14 +02:00
#include "pipewire/log.h"
#include "pipewire/type.h"
2017-07-11 15:57:20 +02:00
#include "pipewire/module.h"
#include "pipewire/private.h"
2017-05-23 19:15:33 +02:00
struct impl {
2017-05-26 08:05:01 +02:00
struct pw_core *core;
struct pw_type *t;
2017-05-26 08:05:01 +02:00
struct pw_properties *properties;
2017-05-23 19:15:33 +02:00
struct spa_hook module_listener;
struct spa_hook core_listener;
2017-05-23 19:15:33 +02:00
2017-05-26 08:05:01 +02:00
struct spa_list node_list;
2017-05-23 19:15:33 +02:00
};
struct node_info {
struct spa_list link;
2017-05-26 08:05:01 +02:00
struct impl *impl;
struct pw_node *node;
struct spa_hook node_listener;
2017-05-26 08:05:01 +02:00
struct spa_source *idle_timeout;
};
2017-05-23 19:15:33 +02:00
2017-05-26 08:05:01 +02:00
static struct node_info *find_node_info(struct impl *impl, struct pw_node *node)
2017-05-23 19:15:33 +02:00
{
2017-05-26 08:05:01 +02:00
struct node_info *info;
2017-05-23 19:15:33 +02:00
2017-05-26 08:05:01 +02:00
spa_list_for_each(info, &impl->node_list, link) {
if (info->node == node)
return info;
}
return NULL;
2017-05-23 19:15:33 +02:00
}
2017-05-26 08:05:01 +02:00
static void remove_idle_timeout(struct node_info *info)
2017-05-23 19:15:33 +02:00
{
2017-05-26 08:05:01 +02:00
if (info->idle_timeout) {
pw_loop_destroy_source(pw_core_get_main_loop(info->impl->core), info->idle_timeout);
2017-05-26 08:05:01 +02:00
info->idle_timeout = NULL;
}
2017-05-23 19:15:33 +02:00
}
2017-05-26 08:05:01 +02:00
static void node_info_free(struct node_info *info)
2017-05-23 19:15:33 +02:00
{
2017-05-26 08:05:01 +02:00
spa_list_remove(&info->link);
remove_idle_timeout(info);
spa_hook_remove(&info->node_listener);
2017-05-26 08:05:01 +02:00
free(info);
2017-05-23 19:15:33 +02:00
}
static void idle_timeout(void *data, uint64_t expirations)
2017-05-23 19:15:33 +02:00
{
2017-05-26 08:05:01 +02:00
struct node_info *info = data;
2017-05-23 19:15:33 +02:00
2017-05-26 08:05:01 +02:00
pw_log_debug("module %p: node %p idle timeout", info->impl, info->node);
remove_idle_timeout(info);
pw_node_set_state(info->node, PW_NODE_STATE_SUSPENDED);
2017-05-23 19:15:33 +02:00
}
static void
node_state_request(void *data, enum pw_node_state state)
2017-05-23 19:15:33 +02:00
{
struct node_info *info = data;
2017-05-26 08:05:01 +02:00
remove_idle_timeout(info);
2017-05-23 19:15:33 +02:00
}
static void
node_state_changed(void *data, enum pw_node_state old, enum pw_node_state state, const char *error)
2017-05-23 19:15:33 +02:00
{
struct node_info *info = data;
2017-05-26 08:05:01 +02:00
struct impl *impl = info->impl;
if (state != PW_NODE_STATE_IDLE) {
remove_idle_timeout(info);
} else {
struct timespec value;
struct pw_loop *main_loop = pw_core_get_main_loop(impl->core);
2017-05-26 08:05:01 +02:00
pw_log_debug("module %p: node %p became idle", impl, info->node);
info->idle_timeout = pw_loop_add_timer(main_loop, idle_timeout, info);
2017-05-26 08:05:01 +02:00
value.tv_sec = 3;
value.tv_nsec = 0;
pw_loop_update_timer(main_loop, info->idle_timeout, &value, NULL, false);
2017-05-26 08:05:01 +02:00
}
2017-05-23 19:15:33 +02:00
}
static const struct pw_node_events node_events = {
PW_VERSION_NODE_EVENTS,
.state_request = node_state_request,
.state_changed = node_state_changed,
};
2017-05-23 19:15:33 +02:00
static void
core_global_added(void *data, struct pw_global *global)
2017-05-23 19:15:33 +02:00
{
struct impl *impl = data;
2017-05-26 08:05:01 +02:00
if (pw_global_get_type(global) == impl->t->node) {
struct pw_node *node = pw_global_get_object(global);
2017-05-26 08:05:01 +02:00
struct node_info *info;
info = calloc(1, sizeof(struct node_info));
info->impl = impl;
info->node = node;
spa_list_append(&impl->node_list, &info->link);
pw_node_add_listener(node, &info->node_listener, &node_events, info);
2017-05-26 08:05:01 +02:00
pw_log_debug("module %p: node %p added", impl, node);
}
2017-05-23 19:15:33 +02:00
}
static void
core_global_removed(void *data, struct pw_global *global)
2017-05-23 19:15:33 +02:00
{
struct impl *impl = data;
2017-05-23 19:15:33 +02:00
if (pw_global_get_type(global) == impl->t->node) {
struct pw_node *node = pw_global_get_object(global);
2017-05-26 08:05:01 +02:00
struct node_info *info;
2017-05-23 19:15:33 +02:00
2017-05-26 08:05:01 +02:00
if ((info = find_node_info(impl, node)))
node_info_free(info);
2017-05-23 19:15:33 +02:00
2017-05-26 08:05:01 +02:00
pw_log_debug("module %p: node %p removed", impl, node);
}
2017-05-23 19:15:33 +02:00
}
static void module_destroy(void *data)
{
struct impl *impl = data;
struct node_info *info, *t;
spa_list_for_each_safe(info, t, &impl->node_list, link)
node_info_free(info);
spa_hook_remove(&impl->core_listener);
spa_hook_remove(&impl->module_listener);
if (impl->properties)
pw_properties_free(impl->properties);
free(impl);
}
const struct pw_module_events module_events = {
PW_VERSION_MODULE_EVENTS,
.destroy = module_destroy,
};
const struct pw_core_events core_events = {
PW_VERSION_CORE_EVENTS,
.global_added = core_global_added,
.global_removed = core_global_removed,
};
2017-05-23 19:15:33 +02:00
/**
* module_new:
* @core: #struct pw_core
* @properties: #struct pw_properties
*
* Make a new #struct impl object with given @properties
*
* Returns: a new #struct impl
*/
static bool module_init(struct pw_module *module, struct pw_properties *properties)
2017-05-23 19:15:33 +02:00
{
2017-05-26 08:05:01 +02:00
struct impl *impl;
2017-05-23 19:15:33 +02:00
2017-05-26 08:05:01 +02:00
impl = calloc(1, sizeof(struct impl));
pw_log_debug("module %p: new", impl);
2017-05-23 19:15:33 +02:00
impl->core = pw_module_get_core(module);
impl->t = pw_core_get_type(impl->core);
2017-05-26 08:05:01 +02:00
impl->properties = properties;
2017-05-23 19:15:33 +02:00
2017-05-26 08:05:01 +02:00
spa_list_init(&impl->node_list);
2017-05-23 19:15:33 +02:00
pw_module_add_listener(module, &impl->module_listener, &module_events, impl);
pw_core_add_listener(impl->core, &impl->core_listener, &core_events, impl);
2017-05-23 19:15:33 +02:00
2017-05-26 08:05:01 +02:00
return impl;
2017-05-23 19:15:33 +02:00
}
2017-05-26 08:05:01 +02:00
bool pipewire__module_init(struct pw_module *module, const char *args)
2017-05-23 19:15:33 +02:00
{
return module_init(module, NULL);
2017-05-23 19:15:33 +02:00
}