2020-08-17 11:17:21 +02:00
|
|
|
/* PipeWire
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2020 Wim Taymans
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
#include <spa/utils/hook.h>
|
|
|
|
|
#include <spa/utils/result.h>
|
2020-11-28 15:34:01 +01:00
|
|
|
#include <spa/utils/json.h>
|
2020-08-17 11:17:21 +02:00
|
|
|
#include <spa/pod/parser.h>
|
|
|
|
|
#include <spa/pod/builder.h>
|
|
|
|
|
#include <spa/debug/pod.h>
|
|
|
|
|
|
|
|
|
|
#include "pipewire/pipewire.h"
|
|
|
|
|
#include "extensions/metadata.h"
|
|
|
|
|
|
|
|
|
|
#include "media-session.h"
|
|
|
|
|
|
|
|
|
|
#define NAME "default-routes"
|
|
|
|
|
#define SESSION_KEY "default-routes"
|
2020-11-22 10:00:58 +01:00
|
|
|
#define PREFIX "default.route."
|
2020-08-17 11:17:21 +02:00
|
|
|
|
|
|
|
|
#define SAVE_INTERVAL 1
|
|
|
|
|
|
|
|
|
|
struct impl {
|
|
|
|
|
struct timespec now;
|
|
|
|
|
|
|
|
|
|
struct sm_media_session *session;
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
|
|
|
|
|
struct pw_context *context;
|
|
|
|
|
struct spa_source *idle_timeout;
|
|
|
|
|
|
|
|
|
|
struct spa_hook meta_listener;
|
|
|
|
|
|
|
|
|
|
struct pw_properties *to_restore;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct device {
|
|
|
|
|
struct sm_device *obj;
|
|
|
|
|
|
|
|
|
|
uint32_t id;
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
|
|
struct spa_hook listener;
|
2021-01-25 12:11:16 +01:00
|
|
|
|
|
|
|
|
uint32_t active_route[2];
|
2020-08-17 11:17:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct find_data {
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
const char *name;
|
|
|
|
|
uint32_t id;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void remove_idle_timeout(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
struct pw_loop *main_loop = pw_context_get_main_loop(impl->context);
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
if (impl->idle_timeout) {
|
2020-11-22 10:00:58 +01:00
|
|
|
if ((res = sm_media_session_save_state(impl->session,
|
2021-01-25 12:11:16 +01:00
|
|
|
SESSION_KEY, PREFIX, impl->to_restore)) < 0)
|
2020-08-17 11:17:21 +02:00
|
|
|
pw_log_error("can't save "SESSION_KEY" state: %s", spa_strerror(res));
|
|
|
|
|
pw_loop_destroy_source(main_loop, impl->idle_timeout);
|
|
|
|
|
impl->idle_timeout = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void idle_timeout(void *data, uint64_t expirations)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
pw_log_debug(NAME " %p: idle timeout", impl);
|
|
|
|
|
remove_idle_timeout(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void add_idle_timeout(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
struct timespec value;
|
|
|
|
|
struct pw_loop *main_loop = pw_context_get_main_loop(impl->context);
|
|
|
|
|
|
|
|
|
|
if (impl->idle_timeout == NULL)
|
|
|
|
|
impl->idle_timeout = pw_loop_add_timer(main_loop, idle_timeout, impl);
|
|
|
|
|
|
|
|
|
|
value.tv_sec = SAVE_INTERVAL;
|
|
|
|
|
value.tv_nsec = 0;
|
|
|
|
|
pw_loop_update_timer(main_loop, impl->idle_timeout, &value, NULL, false);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-23 15:34:24 +01:00
|
|
|
static uint32_t channel_from_name(const char *name)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; spa_type_audio_channel[i].name; i++) {
|
|
|
|
|
if (strcmp(name, spa_debug_type_short_name(spa_type_audio_channel[i].name)) == 0)
|
|
|
|
|
return spa_type_audio_channel[i].type;
|
|
|
|
|
}
|
|
|
|
|
return SPA_AUDIO_CHANNEL_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *channel_to_name(uint32_t channel)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 0; spa_type_audio_channel[i].name; i++) {
|
|
|
|
|
if (spa_type_audio_channel[i].type == channel)
|
|
|
|
|
return spa_debug_type_short_name(spa_type_audio_channel[i].name);
|
|
|
|
|
}
|
|
|
|
|
return "UNK";
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 11:17:21 +02:00
|
|
|
static char *serialize_props(struct device *dev, const struct spa_pod *param)
|
|
|
|
|
{
|
|
|
|
|
struct spa_pod_prop *prop;
|
|
|
|
|
struct spa_pod_object *obj = (struct spa_pod_object *) param;
|
|
|
|
|
float val = 0.0f;
|
2020-11-22 10:00:58 +01:00
|
|
|
bool b = false, comma = false;
|
2020-08-17 11:17:21 +02:00
|
|
|
char *ptr;
|
|
|
|
|
size_t size;
|
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
|
|
f = open_memstream(&ptr, &size);
|
2020-11-22 10:00:58 +01:00
|
|
|
fprintf(f, "{ ");
|
2020-08-17 11:17:21 +02:00
|
|
|
|
|
|
|
|
SPA_POD_OBJECT_FOREACH(obj, prop) {
|
|
|
|
|
switch (prop->key) {
|
|
|
|
|
case SPA_PROP_volume:
|
|
|
|
|
spa_pod_get_float(&prop->value, &val);
|
2020-11-22 10:00:58 +01:00
|
|
|
fprintf(f, "%s\"volume\": %f ", (comma ? ", " : ""), val);
|
2020-08-17 11:17:21 +02:00
|
|
|
break;
|
|
|
|
|
case SPA_PROP_mute:
|
|
|
|
|
spa_pod_get_bool(&prop->value, &b);
|
2020-11-22 10:00:58 +01:00
|
|
|
fprintf(f, "%s\"mute\": %s ", (comma ? ", " : ""), b ? "true" : "false");
|
2020-08-17 11:17:21 +02:00
|
|
|
break;
|
|
|
|
|
case SPA_PROP_channelVolumes:
|
|
|
|
|
{
|
|
|
|
|
uint32_t i, n_vals;
|
|
|
|
|
float vals[SPA_AUDIO_MAX_CHANNELS];
|
|
|
|
|
|
|
|
|
|
n_vals = spa_pod_copy_array(&prop->value, SPA_TYPE_Float,
|
|
|
|
|
vals, SPA_AUDIO_MAX_CHANNELS);
|
2020-11-23 20:18:32 +01:00
|
|
|
if (n_vals == 0)
|
|
|
|
|
continue;
|
2020-08-17 11:17:21 +02:00
|
|
|
|
2020-11-22 10:00:58 +01:00
|
|
|
fprintf(f, "%s\"volumes\": [", (comma ? ", " : ""));
|
2020-08-17 11:17:21 +02:00
|
|
|
for (i = 0; i < n_vals; i++)
|
2020-11-22 10:00:58 +01:00
|
|
|
fprintf(f, "%s%f", (i == 0 ? " " : ", "), vals[i]);
|
|
|
|
|
fprintf(f, " ]");
|
2020-08-17 11:17:21 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2020-11-23 15:34:24 +01:00
|
|
|
case SPA_PROP_channelMap:
|
|
|
|
|
{
|
|
|
|
|
uint32_t i, n_vals;
|
|
|
|
|
uint32_t map[SPA_AUDIO_MAX_CHANNELS];
|
|
|
|
|
|
|
|
|
|
n_vals = spa_pod_copy_array(&prop->value, SPA_TYPE_Id,
|
|
|
|
|
map, SPA_AUDIO_MAX_CHANNELS);
|
2020-11-23 20:18:32 +01:00
|
|
|
if (n_vals == 0)
|
|
|
|
|
continue;
|
2020-11-23 15:34:24 +01:00
|
|
|
|
|
|
|
|
fprintf(f, "%s\"channels\": [", (comma ? ", " : ""));
|
|
|
|
|
for (i = 0; i < n_vals; i++)
|
|
|
|
|
fprintf(f, "%s\"%s\"", (i == 0 ? " " : ", "), channel_to_name(map[i]));
|
|
|
|
|
fprintf(f, " ]");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-08-17 11:17:21 +02:00
|
|
|
default:
|
2020-11-22 10:00:58 +01:00
|
|
|
continue;
|
2020-08-17 11:17:21 +02:00
|
|
|
}
|
2020-11-22 10:00:58 +01:00
|
|
|
comma = true;
|
2020-08-17 11:17:21 +02:00
|
|
|
}
|
2020-11-22 10:00:58 +01:00
|
|
|
fprintf(f, " }");
|
2020-08-17 11:17:21 +02:00
|
|
|
fclose(f);
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int restore_route(struct device *dev, const char *val, uint32_t index, uint32_t device_id)
|
|
|
|
|
{
|
2020-11-22 10:00:58 +01:00
|
|
|
struct spa_json it[3];
|
2021-01-27 15:35:01 +01:00
|
|
|
char buf[1024], key[128];
|
2020-11-22 10:00:58 +01:00
|
|
|
const char *value;
|
2020-08-17 11:17:21 +02:00
|
|
|
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buf, sizeof(buf));
|
|
|
|
|
struct spa_pod_frame f[2];
|
|
|
|
|
struct spa_pod *param;
|
2020-11-22 10:00:58 +01:00
|
|
|
|
|
|
|
|
spa_json_init(&it[0], val, strlen(val));
|
|
|
|
|
|
|
|
|
|
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
|
|
|
|
|
return -EINVAL;
|
2020-08-17 11:17:21 +02:00
|
|
|
|
|
|
|
|
spa_pod_builder_push_object(&b, &f[0],
|
|
|
|
|
SPA_TYPE_OBJECT_ParamRoute, SPA_PARAM_Route);
|
|
|
|
|
spa_pod_builder_add(&b,
|
|
|
|
|
SPA_PARAM_ROUTE_index, SPA_POD_Int(index),
|
|
|
|
|
SPA_PARAM_ROUTE_device, SPA_POD_Int(device_id),
|
|
|
|
|
0);
|
|
|
|
|
spa_pod_builder_prop(&b, SPA_PARAM_ROUTE_props, 0);
|
|
|
|
|
spa_pod_builder_push_object(&b, &f[1],
|
|
|
|
|
SPA_TYPE_OBJECT_Props, SPA_PARAM_Route);
|
2020-10-01 11:32:40 +02:00
|
|
|
|
2021-01-27 15:35:01 +01:00
|
|
|
while (spa_json_get_string(&it[1], key, sizeof(key)-1) > 0) {
|
|
|
|
|
if (strcmp(key, "volume") == 0) {
|
2020-11-22 10:00:58 +01:00
|
|
|
float vol;
|
|
|
|
|
if (spa_json_get_float(&it[1], &vol) <= 0)
|
|
|
|
|
continue;
|
2020-08-17 11:17:21 +02:00
|
|
|
spa_pod_builder_prop(&b, SPA_PROP_volume, 0);
|
|
|
|
|
spa_pod_builder_float(&b, vol);
|
|
|
|
|
}
|
2021-01-27 15:35:01 +01:00
|
|
|
else if (strcmp(key, "mute") == 0) {
|
2020-11-22 10:00:58 +01:00
|
|
|
bool mute;
|
|
|
|
|
if (spa_json_get_bool(&it[1], &mute) <= 0)
|
|
|
|
|
continue;
|
2020-08-17 11:17:21 +02:00
|
|
|
spa_pod_builder_prop(&b, SPA_PROP_mute, 0);
|
|
|
|
|
spa_pod_builder_bool(&b, mute);
|
|
|
|
|
}
|
2021-01-27 15:35:01 +01:00
|
|
|
else if (strcmp(key, "volumes") == 0) {
|
2020-11-22 10:00:58 +01:00
|
|
|
uint32_t n_vols;
|
|
|
|
|
float vols[SPA_AUDIO_MAX_CHANNELS];
|
|
|
|
|
|
|
|
|
|
if (spa_json_enter_array(&it[1], &it[2]) <= 0)
|
2020-09-22 12:39:39 +02:00
|
|
|
continue;
|
|
|
|
|
|
2020-11-22 10:00:58 +01:00
|
|
|
for (n_vols = 0; n_vols < SPA_AUDIO_MAX_CHANNELS; n_vols++) {
|
|
|
|
|
if (spa_json_get_float(&it[2], &vols[n_vols]) <= 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-11-23 20:18:32 +01:00
|
|
|
if (n_vols == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2020-08-17 11:17:21 +02:00
|
|
|
spa_pod_builder_prop(&b, SPA_PROP_channelVolumes, 0);
|
|
|
|
|
spa_pod_builder_array(&b, sizeof(float), SPA_TYPE_Float,
|
|
|
|
|
n_vols, vols);
|
2020-11-23 15:34:24 +01:00
|
|
|
}
|
2021-01-27 15:35:01 +01:00
|
|
|
else if (strcmp(key, "channels") == 0) {
|
2020-11-23 15:34:24 +01:00
|
|
|
uint32_t n_ch;
|
|
|
|
|
uint32_t map[SPA_AUDIO_MAX_CHANNELS];
|
|
|
|
|
|
|
|
|
|
if (spa_json_enter_array(&it[1], &it[2]) <= 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for (n_ch = 0; n_ch < SPA_AUDIO_MAX_CHANNELS; n_ch++) {
|
|
|
|
|
char chname[16];
|
|
|
|
|
if (spa_json_get_string(&it[2], chname, sizeof(chname)) <= 0)
|
|
|
|
|
break;
|
|
|
|
|
map[n_ch] = channel_from_name(chname);
|
|
|
|
|
}
|
2020-11-23 20:18:32 +01:00
|
|
|
if (n_ch == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
2020-11-23 15:34:24 +01:00
|
|
|
spa_pod_builder_prop(&b, SPA_PROP_channelMap, 0);
|
|
|
|
|
spa_pod_builder_array(&b, sizeof(uint32_t), SPA_TYPE_Id,
|
|
|
|
|
n_ch, map);
|
2020-10-01 11:32:40 +02:00
|
|
|
} else {
|
2020-11-22 10:00:58 +01:00
|
|
|
if (spa_json_next(&it[1], &value) <= 0)
|
|
|
|
|
break;
|
2020-08-17 11:17:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spa_pod_builder_pop(&b, &f[1]);
|
|
|
|
|
param = spa_pod_builder_pop(&b, &f[0]);
|
2020-08-17 17:52:25 +02:00
|
|
|
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_pod(2, NULL, param);
|
2020-08-17 11:17:21 +02:00
|
|
|
|
|
|
|
|
pw_device_set_param((struct pw_node*)dev->obj->obj.proxy,
|
|
|
|
|
SPA_PARAM_Route, 0, param);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int handle_route(struct device *dev, struct sm_param *p)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = dev->impl;
|
|
|
|
|
int res;
|
2021-01-25 12:11:16 +01:00
|
|
|
const char *name;
|
2020-08-17 11:17:21 +02:00
|
|
|
uint32_t index, device_id;
|
|
|
|
|
enum spa_direction direction;
|
|
|
|
|
struct spa_pod *props = NULL;
|
|
|
|
|
char key[1024];
|
|
|
|
|
|
|
|
|
|
if ((res = spa_pod_parse_object(p->param,
|
|
|
|
|
SPA_TYPE_OBJECT_ParamRoute, NULL,
|
|
|
|
|
SPA_PARAM_ROUTE_index, SPA_POD_Int(&index),
|
|
|
|
|
SPA_PARAM_ROUTE_device, SPA_POD_Int(&device_id),
|
|
|
|
|
SPA_PARAM_ROUTE_direction, SPA_POD_Id(&direction),
|
|
|
|
|
SPA_PARAM_ROUTE_name, SPA_POD_String(&name),
|
|
|
|
|
SPA_PARAM_ROUTE_props, SPA_POD_OPT_Pod(&props))) < 0) {
|
|
|
|
|
pw_log_warn("device %d: can't parse route: %s", dev->id, spa_strerror(res));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 10:00:58 +01:00
|
|
|
snprintf(key, sizeof(key)-1, PREFIX"%s:%s:%s", dev->name,
|
2020-08-17 11:17:21 +02:00
|
|
|
direction == SPA_DIRECTION_INPUT ? "input" : "output", name);
|
|
|
|
|
|
2021-01-25 12:11:16 +01:00
|
|
|
if (dev->active_route[direction] != index) {
|
|
|
|
|
const char *val = pw_properties_get(impl->to_restore, key);
|
|
|
|
|
dev->active_route[direction] = index;
|
2021-01-27 15:44:52 +01:00
|
|
|
if (val == NULL)
|
|
|
|
|
val = "{ \"volumes\": [ 0.4 ], \"mute\": false }";
|
|
|
|
|
pw_log_info("device %d: restore route '%s' to %s", dev->id, key, val);
|
|
|
|
|
restore_route(dev, val, index, device_id);
|
2020-08-17 11:17:21 +02:00
|
|
|
} else if (props) {
|
|
|
|
|
char *val = serialize_props(dev, props);
|
2021-01-25 12:11:16 +01:00
|
|
|
pw_log_info("device %d: current route %s %s", dev->id, key, val);
|
|
|
|
|
pw_properties_set(impl->to_restore, key, val);
|
2020-08-17 11:17:21 +02:00
|
|
|
free(val);
|
|
|
|
|
add_idle_timeout(impl);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void object_update(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct device *dev = data;
|
|
|
|
|
struct impl *impl = dev->impl;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: device %p %08x/%08x", impl, dev,
|
|
|
|
|
dev->obj->obj.changed, dev->obj->obj.avail);
|
|
|
|
|
|
2021-01-27 11:39:57 +01:00
|
|
|
if (dev->obj->obj.changed & SM_DEVICE_CHANGE_MASK_PARAMS) {
|
2020-08-17 11:17:21 +02:00
|
|
|
struct sm_param *p;
|
|
|
|
|
spa_list_for_each(p, &dev->obj->param_list, link) {
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_pod(2, NULL, p->param);
|
|
|
|
|
|
|
|
|
|
switch (p->id) {
|
|
|
|
|
case SPA_PARAM_EnumRoute:
|
|
|
|
|
break;
|
|
|
|
|
case SPA_PARAM_Route:
|
|
|
|
|
handle_route(dev, p);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct sm_object_events object_events = {
|
|
|
|
|
SM_VERSION_OBJECT_EVENTS,
|
|
|
|
|
.update = object_update
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void session_create(void *data, struct sm_object *object)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
struct device *dev;
|
|
|
|
|
const char *name;
|
|
|
|
|
|
|
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Device) != 0 ||
|
|
|
|
|
object->props == NULL ||
|
|
|
|
|
(name = pw_properties_get(object->props, PW_KEY_DEVICE_NAME)) == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: add device '%d' %s", impl, object->id, name);
|
|
|
|
|
|
|
|
|
|
dev = sm_object_add_data(object, SESSION_KEY, sizeof(struct device));
|
|
|
|
|
dev->obj = (struct sm_device*)object;
|
|
|
|
|
dev->id = object->id;
|
|
|
|
|
dev->impl = impl;
|
|
|
|
|
dev->name = strdup(name);
|
2021-01-25 12:11:16 +01:00
|
|
|
dev->active_route[SPA_DIRECTION_INPUT] = SPA_ID_INVALID;
|
|
|
|
|
dev->active_route[SPA_DIRECTION_OUTPUT] = SPA_ID_INVALID;
|
2020-08-17 11:17:21 +02:00
|
|
|
|
|
|
|
|
dev->obj->obj.mask |= SM_DEVICE_CHANGE_MASK_PARAMS;
|
|
|
|
|
sm_object_add_listener(&dev->obj->obj, &dev->listener, &object_events, dev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void destroy_device(struct impl *impl, struct device *dev)
|
|
|
|
|
{
|
2020-08-17 17:50:15 +02:00
|
|
|
spa_hook_remove(&dev->listener);
|
2020-08-17 11:17:21 +02:00
|
|
|
free(dev->name);
|
|
|
|
|
sm_object_remove_data((struct sm_object*)dev->obj, SESSION_KEY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void session_remove(void *data, struct sm_object *object)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Device) != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: remove device '%d'", impl, object->id);
|
|
|
|
|
|
|
|
|
|
if ((dev = sm_object_get_data(object, SESSION_KEY)) != NULL)
|
|
|
|
|
destroy_device(impl, dev);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 17:50:15 +02:00
|
|
|
static void session_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
remove_idle_timeout(impl);
|
|
|
|
|
spa_hook_remove(&impl->listener);
|
|
|
|
|
pw_properties_free(impl->to_restore);
|
|
|
|
|
free(impl);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 11:17:21 +02:00
|
|
|
static const struct sm_media_session_events session_events = {
|
|
|
|
|
SM_VERSION_MEDIA_SESSION_EVENTS,
|
|
|
|
|
.create = session_create,
|
|
|
|
|
.remove = session_remove,
|
|
|
|
|
.destroy = session_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int sm_default_routes_start(struct sm_media_session *session)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
impl->session = session;
|
|
|
|
|
impl->context = session->context;
|
|
|
|
|
|
|
|
|
|
impl->to_restore = pw_properties_new(NULL, NULL);
|
|
|
|
|
if (impl->to_restore == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto exit_free;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 10:00:58 +01:00
|
|
|
if ((res = sm_media_session_load_state(impl->session,
|
|
|
|
|
SESSION_KEY, PREFIX, impl->to_restore)) < 0)
|
2020-08-17 11:17:21 +02:00
|
|
|
pw_log_info("can't load "SESSION_KEY" state: %s", spa_strerror(res));
|
|
|
|
|
|
|
|
|
|
sm_media_session_add_listener(impl->session, &impl->listener, &session_events, impl);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
exit_free:
|
|
|
|
|
free(impl);
|
|
|
|
|
return res;
|
|
|
|
|
}
|