2020-08-17 17:55:20 +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>
|
|
|
|
|
#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"
|
2020-11-22 10:00:58 +01:00
|
|
|
#include "json.h"
|
2020-08-17 17:55:20 +02:00
|
|
|
|
|
|
|
|
#define NAME "restore-stream"
|
|
|
|
|
#define SESSION_KEY "restore-stream"
|
2020-11-22 10:00:58 +01:00
|
|
|
#define PREFIX "restore.stream."
|
2020-08-17 17:55:20 +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;
|
|
|
|
|
|
2020-11-20 18:01:20 +01:00
|
|
|
struct pw_metadata *metadata;
|
2020-11-22 10:00:58 +01:00
|
|
|
struct spa_hook metadata_listener;
|
2020-08-17 17:55:20 +02:00
|
|
|
|
|
|
|
|
struct pw_properties *props;
|
2020-11-22 10:00:58 +01:00
|
|
|
|
|
|
|
|
unsigned int sync:1;
|
2020-08-17 17:55:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct stream {
|
|
|
|
|
struct sm_node *obj;
|
|
|
|
|
|
|
|
|
|
uint32_t id;
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
char *media_class;
|
|
|
|
|
char *key;
|
|
|
|
|
unsigned int restored:1;
|
|
|
|
|
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
SESSION_KEY, PREFIX, impl->props)) < 0)
|
2020-08-17 17:55:20 +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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void session_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
remove_idle_timeout(impl);
|
|
|
|
|
spa_hook_remove(&impl->listener);
|
|
|
|
|
pw_properties_free(impl->props);
|
|
|
|
|
free(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static char *serialize_props(struct stream *str, 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 17:55:20 +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 17:55:20 +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 17:55:20 +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 17:55:20 +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-22 10:00:58 +01:00
|
|
|
fprintf(f, "%s\"volumes\": [", (comma ? ", " : ""));
|
2020-08-17 17:55:20 +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 17:55:20 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
2020-11-22 10:00:58 +01:00
|
|
|
continue;
|
2020-08-17 17:55:20 +02:00
|
|
|
}
|
2020-11-22 10:00:58 +01:00
|
|
|
comma = true;
|
2020-08-17 17:55:20 +02:00
|
|
|
}
|
2020-10-14 12:35:27 +02:00
|
|
|
if (str->obj->target_node != NULL)
|
2020-11-22 10:00:58 +01:00
|
|
|
fprintf(f, "\"target-node\": \"%s\"", str->obj->target_node);
|
2020-10-14 12:35:27 +02:00
|
|
|
|
2020-11-22 10:00:58 +01:00
|
|
|
fprintf(f, " }");
|
2020-08-17 17:55:20 +02:00
|
|
|
fclose(f);
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-20 18:01:20 +01:00
|
|
|
static void sync_metadata(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
const struct spa_dict_item *it;
|
|
|
|
|
|
2020-11-22 10:00:58 +01:00
|
|
|
impl->sync = true;
|
2020-11-20 18:01:20 +01:00
|
|
|
spa_dict_for_each(it, &impl->props->dict)
|
2020-11-22 10:00:58 +01:00
|
|
|
pw_metadata_set_property(impl->metadata, 0, it->key, "Spa:String:JSON", it->value);
|
|
|
|
|
impl->sync = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int metadata_property(void *object, uint32_t subject,
|
|
|
|
|
const char *key, const char *type, const char *value)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = object;
|
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
|
|
if (impl->sync)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (subject == PW_ID_CORE) {
|
|
|
|
|
if (key == NULL)
|
|
|
|
|
pw_properties_clear(impl->props);
|
|
|
|
|
else if (strstr(key, PREFIX) == key) {
|
|
|
|
|
changed = pw_properties_set(impl->props, key, value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (changed)
|
|
|
|
|
add_idle_timeout(impl);
|
|
|
|
|
|
|
|
|
|
return 0;
|
2020-11-20 18:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-22 10:00:58 +01:00
|
|
|
static const struct pw_metadata_events metadata_events = {
|
|
|
|
|
PW_VERSION_METADATA_EVENTS,
|
|
|
|
|
.property = metadata_property,
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-17 17:55:20 +02:00
|
|
|
static int handle_props(struct stream *str, struct sm_param *p)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = str->impl;
|
|
|
|
|
const char *key;
|
2020-11-20 18:01:20 +01:00
|
|
|
int changed = 0;
|
2020-08-17 17:55:20 +02:00
|
|
|
|
|
|
|
|
key = str->key;
|
|
|
|
|
if (key == NULL)
|
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
|
|
if (p->param) {
|
|
|
|
|
char *val = serialize_props(str, p->param);
|
|
|
|
|
pw_log_debug("stream %d: current props %s %s", str->id, key, val);
|
2020-11-20 18:01:20 +01:00
|
|
|
changed += pw_properties_set(impl->props, key, val);
|
2020-08-17 17:55:20 +02:00
|
|
|
free(val);
|
|
|
|
|
add_idle_timeout(impl);
|
|
|
|
|
}
|
2020-11-20 18:01:20 +01:00
|
|
|
if (changed)
|
|
|
|
|
sync_metadata(impl);
|
2020-08-17 17:55:20 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int restore_stream(struct stream *str, const char *val)
|
|
|
|
|
{
|
2020-11-22 10:00:58 +01:00
|
|
|
struct spa_json it[3];
|
|
|
|
|
const char *value;
|
|
|
|
|
int len;
|
2020-10-14 12:35:27 +02:00
|
|
|
char buf[1024];
|
2020-08-17 17:55:20 +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 17:55:20 +02:00
|
|
|
|
|
|
|
|
spa_pod_builder_push_object(&b, &f[0],
|
|
|
|
|
SPA_TYPE_OBJECT_Props, SPA_PARAM_Props);
|
2020-11-22 10:00:58 +01:00
|
|
|
|
|
|
|
|
while ((len = spa_json_next(&it[1], &value)) > 0) {
|
|
|
|
|
if (strncmp(value, "\"volume\"", len) == 0) {
|
|
|
|
|
float vol;
|
|
|
|
|
if (spa_json_get_float(&it[1], &vol) <= 0)
|
|
|
|
|
continue;
|
2020-08-17 17:55:20 +02:00
|
|
|
spa_pod_builder_prop(&b, SPA_PROP_volume, 0);
|
|
|
|
|
spa_pod_builder_float(&b, vol);
|
|
|
|
|
}
|
2020-11-22 10:00:58 +01:00
|
|
|
else if (strncmp(value, "\"mute\"", len) == 0) {
|
|
|
|
|
bool mute;
|
|
|
|
|
if (spa_json_get_bool(&it[1], &mute) <= 0)
|
|
|
|
|
continue;
|
2020-08-17 17:55:20 +02:00
|
|
|
spa_pod_builder_prop(&b, SPA_PROP_mute, 0);
|
|
|
|
|
spa_pod_builder_bool(&b, mute);
|
|
|
|
|
}
|
2020-11-22 10:00:58 +01:00
|
|
|
else if (strncmp(value, "\"volumes\"", len) == 0) {
|
|
|
|
|
uint32_t n_vols;
|
|
|
|
|
float vols[SPA_AUDIO_MAX_CHANNELS];
|
|
|
|
|
|
|
|
|
|
if (spa_json_enter_array(&it[1], &it[2]) <= 0)
|
2020-10-01 11:32:40 +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-08-17 17:55:20 +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-22 10:00:58 +01:00
|
|
|
else if (strncmp(value, "\"target-node\"", len) == 0) {
|
|
|
|
|
char name[1024];
|
|
|
|
|
|
|
|
|
|
if (spa_json_get_string(&it[1], name, sizeof(name)) <= 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
pw_log_info("stream %d: target '%s'", str->obj->obj.id, name);
|
2020-10-14 12:35:27 +02:00
|
|
|
free(str->obj->target_node);
|
2020-11-22 10:00:58 +01:00
|
|
|
str->obj->target_node = strdup(name);
|
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 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
param = spa_pod_builder_pop(&b, &f[0]);
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_pod(2, NULL, param);
|
|
|
|
|
|
|
|
|
|
pw_node_set_param((struct pw_node*)str->obj->obj.proxy,
|
|
|
|
|
SPA_PARAM_Props, 0, param);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void update_key(struct stream *str)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = str->impl;
|
|
|
|
|
uint32_t i;
|
|
|
|
|
const char *p, *val;
|
|
|
|
|
char *key;
|
|
|
|
|
struct sm_object *obj = &str->obj->obj;
|
|
|
|
|
bool changed;
|
|
|
|
|
const char *keys[] = {
|
|
|
|
|
PW_KEY_MEDIA_ROLE,
|
|
|
|
|
PW_KEY_APP_ID,
|
|
|
|
|
PW_KEY_APP_NAME,
|
|
|
|
|
PW_KEY_MEDIA_NAME,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
key = NULL;
|
|
|
|
|
for (i = 0; i < SPA_N_ELEMENTS(keys); i++) {
|
|
|
|
|
if ((p = pw_properties_get(obj->props, keys[i]))) {
|
2020-11-22 10:00:58 +01:00
|
|
|
key = spa_aprintf(PREFIX"%s.%s:%s", str->media_class, keys[i], p);
|
2020-08-17 17:55:20 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (key == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: stream %p key '%s'", impl, str, key);
|
|
|
|
|
changed = str->key == NULL || strcmp(str->key, key) != 0;
|
|
|
|
|
free(str->key);
|
|
|
|
|
str->key = key;
|
|
|
|
|
if (!changed || str->restored)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
val = pw_properties_get(impl->props, key);
|
|
|
|
|
if (val != NULL) {
|
|
|
|
|
pw_log_info("stream %d: restore '%s' to %s", str->id, key, val);
|
|
|
|
|
restore_stream(str, val);
|
|
|
|
|
str->restored = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void object_update(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct stream *str = data;
|
|
|
|
|
struct impl *impl = str->impl;
|
|
|
|
|
bool rescan = false;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: stream %p %08x/%08x", impl, str,
|
|
|
|
|
str->obj->obj.changed, str->obj->obj.avail);
|
|
|
|
|
|
|
|
|
|
if (str->obj->obj.changed & SM_OBJECT_CHANGE_MASK_PROPERTIES) {
|
|
|
|
|
if (str->key == NULL)
|
|
|
|
|
update_key(str);
|
|
|
|
|
}
|
|
|
|
|
if (str->obj->obj.changed & SM_NODE_CHANGE_MASK_PARAMS)
|
|
|
|
|
rescan = true;
|
|
|
|
|
|
|
|
|
|
if (rescan) {
|
|
|
|
|
struct sm_param *p;
|
|
|
|
|
spa_list_for_each(p, &str->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_Props:
|
|
|
|
|
handle_props(str, 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 stream *str;
|
|
|
|
|
const char *media_class;
|
|
|
|
|
|
|
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Node) != 0 ||
|
|
|
|
|
object->props == NULL ||
|
|
|
|
|
(media_class = pw_properties_get(object->props, PW_KEY_MEDIA_CLASS)) == NULL ||
|
|
|
|
|
strstr(media_class, "Stream/") != media_class)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
media_class += strlen("Stream/");
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: add stream '%d' %s", impl, object->id, media_class);
|
|
|
|
|
|
|
|
|
|
str = sm_object_add_data(object, SESSION_KEY, sizeof(struct stream));
|
|
|
|
|
str->obj = (struct sm_node*)object;
|
|
|
|
|
str->id = object->id;
|
|
|
|
|
str->impl = impl;
|
|
|
|
|
str->media_class = strdup(media_class);
|
|
|
|
|
update_key(str);
|
|
|
|
|
|
|
|
|
|
str->obj->obj.mask |= SM_OBJECT_CHANGE_MASK_PROPERTIES | SM_NODE_CHANGE_MASK_PARAMS;
|
|
|
|
|
sm_object_add_listener(&str->obj->obj, &str->listener, &object_events, str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void destroy_stream(struct impl *impl, struct stream *str)
|
|
|
|
|
{
|
|
|
|
|
remove_idle_timeout(impl);
|
|
|
|
|
spa_hook_remove(&str->listener);
|
|
|
|
|
free(str->media_class);
|
|
|
|
|
free(str->key);
|
|
|
|
|
sm_object_remove_data((struct sm_object*)str->obj, SESSION_KEY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void session_remove(void *data, struct sm_object *object)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
struct stream *str;
|
|
|
|
|
|
|
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Node) != 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: remove node '%d'", impl, object->id);
|
|
|
|
|
|
|
|
|
|
if ((str = sm_object_get_data(object, SESSION_KEY)) != NULL)
|
|
|
|
|
destroy_stream(impl, str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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_restore_stream_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->props = pw_properties_new(NULL, NULL);
|
2020-11-20 18:01:20 +01:00
|
|
|
if (impl->props == NULL)
|
|
|
|
|
goto exit_errno;
|
|
|
|
|
|
|
|
|
|
impl->metadata = sm_media_session_export_metadata(session, "route-settings");
|
|
|
|
|
if (impl->metadata == NULL)
|
|
|
|
|
goto exit_errno;
|
2020-08-17 17:55:20 +02:00
|
|
|
|
2020-11-22 10:00:58 +01:00
|
|
|
pw_metadata_add_listener(impl->metadata, &impl->metadata_listener,
|
|
|
|
|
&metadata_events, impl);
|
|
|
|
|
|
|
|
|
|
if ((res = sm_media_session_load_state(impl->session,
|
|
|
|
|
SESSION_KEY, PREFIX, impl->props)) < 0)
|
2020-08-17 17:55:20 +02:00
|
|
|
pw_log_info("can't load "SESSION_KEY" state: %s", spa_strerror(res));
|
|
|
|
|
|
2020-11-20 18:01:20 +01:00
|
|
|
sync_metadata(impl);
|
|
|
|
|
|
2020-08-17 17:55:20 +02:00
|
|
|
sm_media_session_add_listener(impl->session, &impl->listener, &session_events, impl);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
2020-11-20 18:01:20 +01:00
|
|
|
exit_errno:
|
|
|
|
|
res = -errno;
|
|
|
|
|
if (impl->props)
|
|
|
|
|
pw_properties_free(impl->props);
|
2020-08-17 17:55:20 +02:00
|
|
|
free(impl);
|
|
|
|
|
return res;
|
|
|
|
|
}
|