2020-01-07 16:07:51 +01:00
|
|
|
/* PipeWire
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2019 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 "config.h"
|
|
|
|
|
|
|
|
|
|
#include <spa/node/node.h>
|
|
|
|
|
#include <spa/utils/hook.h>
|
|
|
|
|
#include <spa/param/audio/format-utils.h>
|
|
|
|
|
#include <spa/param/props.h>
|
|
|
|
|
#include <spa/debug/pod.h>
|
2021-03-05 17:37:12 +01:00
|
|
|
#include <spa/utils/json.h>
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
#include "pipewire/pipewire.h"
|
2020-07-15 14:26:16 +02:00
|
|
|
#include "extensions/metadata.h"
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
#include "media-session.h"
|
|
|
|
|
|
|
|
|
|
#define NAME "policy-node"
|
|
|
|
|
#define SESSION_KEY "policy-node"
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_IDLE_SECONDS 3
|
|
|
|
|
|
2021-02-06 15:58:07 +02:00
|
|
|
#define DEFAULT_AUDIO_SINK_KEY "default.audio.sink"
|
|
|
|
|
#define DEFAULT_AUDIO_SOURCE_KEY "default.audio.source"
|
|
|
|
|
#define DEFAULT_VIDEO_SOURCE_KEY "default.video.source"
|
|
|
|
|
#define DEFAULT_CONFIG_AUDIO_SINK_KEY "default.configured.audio.sink"
|
|
|
|
|
#define DEFAULT_CONFIG_AUDIO_SOURCE_KEY "default.configured.audio.source"
|
|
|
|
|
#define DEFAULT_CONFIG_VIDEO_SOURCE_KEY "default.configured.video.source"
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_AUDIO_SINK 0
|
|
|
|
|
#define DEFAULT_AUDIO_SOURCE 1
|
|
|
|
|
#define DEFAULT_VIDEO_SOURCE 2
|
|
|
|
|
|
|
|
|
|
struct default_node {
|
|
|
|
|
char *key;
|
|
|
|
|
char *key_config;
|
2021-03-05 17:37:12 +01:00
|
|
|
char *value;
|
|
|
|
|
char *config;
|
2021-02-06 15:58:07 +02:00
|
|
|
};
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
struct impl {
|
|
|
|
|
struct timespec now;
|
|
|
|
|
|
|
|
|
|
struct sm_media_session *session;
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
|
2020-07-15 14:26:16 +02:00
|
|
|
struct spa_hook meta_listener;
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
struct pw_context *context;
|
|
|
|
|
|
2020-01-09 15:52:53 +01:00
|
|
|
uint32_t sample_rate;
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
struct spa_list node_list;
|
|
|
|
|
int seq;
|
2020-07-15 14:26:16 +02:00
|
|
|
|
2021-02-06 15:58:07 +02:00
|
|
|
struct default_node defaults[4];
|
2021-01-17 00:31:47 +02:00
|
|
|
|
|
|
|
|
bool streams_follow_default;
|
2020-01-07 16:07:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct node {
|
|
|
|
|
struct sm_node *obj;
|
|
|
|
|
|
|
|
|
|
uint32_t id;
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
|
|
|
|
|
struct spa_list link; /**< link in impl node_list */
|
|
|
|
|
enum pw_direction direction;
|
|
|
|
|
|
|
|
|
|
struct spa_hook listener;
|
|
|
|
|
|
2020-01-08 10:48:48 +01:00
|
|
|
struct node *peer;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
uint32_t client_id;
|
|
|
|
|
int32_t priority;
|
|
|
|
|
|
|
|
|
|
#define NODE_TYPE_UNKNOWN 0
|
|
|
|
|
#define NODE_TYPE_STREAM 1
|
|
|
|
|
#define NODE_TYPE_DEVICE 2
|
|
|
|
|
uint32_t type;
|
|
|
|
|
char *media;
|
|
|
|
|
|
|
|
|
|
struct spa_audio_info format;
|
|
|
|
|
|
2020-11-20 14:54:37 +01:00
|
|
|
int connect_count;
|
2020-01-07 16:07:51 +01:00
|
|
|
uint64_t plugged;
|
|
|
|
|
unsigned int active:1;
|
|
|
|
|
unsigned int exclusive:1;
|
|
|
|
|
unsigned int enabled:1;
|
2020-07-28 10:18:49 +02:00
|
|
|
unsigned int configured:1;
|
|
|
|
|
unsigned int dont_remix:1;
|
2020-07-28 10:50:00 +02:00
|
|
|
unsigned int monitor:1;
|
2020-10-21 15:27:50 +02:00
|
|
|
unsigned int moving:1;
|
2020-10-21 17:00:49 +02:00
|
|
|
unsigned int capture_sink:1;
|
2021-01-27 16:27:06 +01:00
|
|
|
unsigned int virtual:1;
|
2020-01-07 16:07:51 +01:00
|
|
|
};
|
|
|
|
|
|
2020-07-28 10:18:49 +02:00
|
|
|
static bool find_format(struct node *node)
|
2020-01-07 16:07:51 +01:00
|
|
|
{
|
2020-08-06 12:51:20 +02:00
|
|
|
struct impl *impl = node->impl;
|
2020-07-22 16:53:02 +02:00
|
|
|
struct sm_param *p;
|
2020-01-28 15:38:35 +01:00
|
|
|
bool have_format = false;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
spa_list_for_each(p, &node->obj->param_list, link) {
|
|
|
|
|
struct spa_audio_info info = { 0, };
|
|
|
|
|
|
|
|
|
|
if (p->id != SPA_PARAM_EnumFormat)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (spa_format_parse(p->param, &info.media_type, &info.media_subtype) < 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (info.media_type != SPA_MEDIA_TYPE_audio ||
|
|
|
|
|
info.media_subtype != SPA_MEDIA_SUBTYPE_raw)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
spa_pod_object_fixate((struct spa_pod_object*)p->param);
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_pod(2, NULL, p->param);
|
|
|
|
|
|
2020-08-06 12:51:20 +02:00
|
|
|
/* defaults */
|
|
|
|
|
info.info.raw.format = SPA_AUDIO_FORMAT_F32;
|
|
|
|
|
info.info.raw.rate = impl->sample_rate;
|
|
|
|
|
info.info.raw.channels = 2;
|
|
|
|
|
info.info.raw.position[0] = SPA_AUDIO_CHANNEL_FL;
|
|
|
|
|
info.info.raw.position[1] = SPA_AUDIO_CHANNEL_FR;
|
|
|
|
|
|
|
|
|
|
spa_format_audio_raw_parse(p->param, &info.info.raw);
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
if (node->format.info.raw.channels < info.info.raw.channels)
|
|
|
|
|
node->format = info;
|
2020-01-28 15:38:35 +01:00
|
|
|
|
|
|
|
|
have_format = true;
|
|
|
|
|
}
|
2020-07-28 10:18:49 +02:00
|
|
|
return have_format;
|
|
|
|
|
}
|
2020-01-28 15:38:35 +01:00
|
|
|
|
2020-08-19 11:50:59 +02:00
|
|
|
static int configure_node(struct node *node, struct spa_audio_info *info, bool force)
|
2020-07-28 10:18:49 +02:00
|
|
|
{
|
|
|
|
|
struct impl *impl = node->impl;
|
|
|
|
|
char buf[1024];
|
|
|
|
|
struct spa_pod_builder b = { 0, };
|
|
|
|
|
struct spa_pod *param;
|
2020-08-19 11:26:51 +02:00
|
|
|
struct spa_audio_info format;
|
2021-01-27 16:27:06 +01:00
|
|
|
enum pw_direction direction;
|
2020-07-28 10:18:49 +02:00
|
|
|
|
2020-08-19 11:50:59 +02:00
|
|
|
if (node->configured && !force)
|
2020-07-28 10:18:49 +02:00
|
|
|
return 0;
|
|
|
|
|
|
2020-09-02 14:19:45 +02:00
|
|
|
if (strcmp(node->media, "Audio") != 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2020-08-19 11:26:51 +02:00
|
|
|
format = node->format;
|
|
|
|
|
|
2020-08-19 11:50:59 +02:00
|
|
|
if (info != NULL && info->info.raw.channels > 0) {
|
2020-11-04 09:47:20 +01:00
|
|
|
pw_log_info("node %d monitor:%d channelmix %d->%d",
|
|
|
|
|
node->id, node->monitor, format.info.raw.channels,
|
|
|
|
|
info->info.raw.channels);
|
|
|
|
|
format = *info;
|
2020-07-28 10:18:49 +02:00
|
|
|
}
|
2020-08-19 11:26:51 +02:00
|
|
|
format.info.raw.rate = impl->sample_rate;
|
2020-07-28 10:18:49 +02:00
|
|
|
|
2021-01-27 16:27:06 +01:00
|
|
|
if (node->virtual)
|
|
|
|
|
direction = pw_direction_reverse(node->direction);
|
|
|
|
|
else
|
|
|
|
|
direction = node->direction;
|
|
|
|
|
|
2020-07-28 10:18:49 +02:00
|
|
|
spa_pod_builder_init(&b, buf, sizeof(buf));
|
2020-08-19 11:26:51 +02:00
|
|
|
param = spa_format_audio_raw_build(&b, SPA_PARAM_Format, &format.info.raw);
|
2020-07-28 10:18:49 +02:00
|
|
|
param = spa_pod_builder_add_object(&b,
|
|
|
|
|
SPA_TYPE_OBJECT_ParamPortConfig, SPA_PARAM_PortConfig,
|
2021-01-27 16:27:06 +01:00
|
|
|
SPA_PARAM_PORT_CONFIG_direction, SPA_POD_Id(direction),
|
2020-07-28 10:18:49 +02:00
|
|
|
SPA_PARAM_PORT_CONFIG_mode, SPA_POD_Id(SPA_PARAM_PORT_CONFIG_MODE_dsp),
|
|
|
|
|
SPA_PARAM_PORT_CONFIG_monitor, SPA_POD_Bool(true),
|
|
|
|
|
SPA_PARAM_PORT_CONFIG_format, SPA_POD_Pod(param));
|
|
|
|
|
|
|
|
|
|
if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
|
|
|
|
|
spa_debug_pod(2, NULL, param);
|
|
|
|
|
|
|
|
|
|
pw_node_set_param((struct pw_node*)node->obj->obj.proxy,
|
|
|
|
|
SPA_PARAM_PortConfig, 0, param);
|
|
|
|
|
|
|
|
|
|
node->configured = true;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void object_update(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct node *node = data;
|
|
|
|
|
struct impl *impl = node->impl;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: node %p %08x", impl, node, node->obj->obj.changed);
|
|
|
|
|
|
|
|
|
|
if (node->obj->obj.avail & SM_NODE_CHANGE_MASK_PARAMS &&
|
2020-07-28 10:18:49 +02:00
|
|
|
!node->active) {
|
2020-07-28 10:50:00 +02:00
|
|
|
if (!find_format(node)) {
|
|
|
|
|
pw_log_debug(NAME" %p: can't find format %p", impl, node);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-28 10:18:49 +02:00
|
|
|
node->active = true;
|
|
|
|
|
sm_media_session_schedule_rescan(impl->session);
|
|
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct sm_object_events object_events = {
|
|
|
|
|
SM_VERSION_OBJECT_EVENTS,
|
|
|
|
|
.update = object_update
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
handle_node(struct impl *impl, struct sm_object *object)
|
|
|
|
|
{
|
2020-06-03 12:03:20 +02:00
|
|
|
const char *str, *media_class = NULL, *role;
|
2020-01-07 16:07:51 +01:00
|
|
|
enum pw_direction direction;
|
|
|
|
|
struct node *node;
|
|
|
|
|
uint32_t client_id = SPA_ID_INVALID;
|
|
|
|
|
|
|
|
|
|
if (object->props) {
|
|
|
|
|
if ((str = pw_properties_get(object->props, PW_KEY_CLIENT_ID)) != NULL)
|
|
|
|
|
client_id = atoi(str);
|
|
|
|
|
|
2020-06-03 12:03:20 +02:00
|
|
|
media_class = pw_properties_get(object->props, PW_KEY_MEDIA_CLASS);
|
|
|
|
|
role = pw_properties_get(object->props, PW_KEY_MEDIA_ROLE);
|
|
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: node "PW_KEY_MEDIA_CLASS" %s", impl, media_class);
|
|
|
|
|
|
|
|
|
|
if (media_class == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
node = sm_object_add_data(object, SESSION_KEY, sizeof(struct node));
|
|
|
|
|
node->obj = (struct sm_node*)object;
|
|
|
|
|
node->id = object->id;
|
|
|
|
|
node->impl = impl;
|
|
|
|
|
node->client_id = client_id;
|
|
|
|
|
node->type = NODE_TYPE_UNKNOWN;
|
|
|
|
|
spa_list_append(&impl->node_list, &node->link);
|
|
|
|
|
|
2020-06-03 12:03:20 +02:00
|
|
|
if (role && !strcmp(role, "DSP"))
|
2020-07-28 10:50:00 +02:00
|
|
|
node->active = node->configured = true;
|
2020-06-03 12:03:20 +02:00
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
if (strstr(media_class, "Stream/") == media_class) {
|
|
|
|
|
media_class += strlen("Stream/");
|
|
|
|
|
|
|
|
|
|
if (strstr(media_class, "Output/") == media_class) {
|
|
|
|
|
direction = PW_DIRECTION_OUTPUT;
|
|
|
|
|
media_class += strlen("Output/");
|
|
|
|
|
}
|
|
|
|
|
else if (strstr(media_class, "Input/") == media_class) {
|
|
|
|
|
direction = PW_DIRECTION_INPUT;
|
|
|
|
|
media_class += strlen("Input/");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
|
2020-02-06 17:42:52 +01:00
|
|
|
if (strstr(media_class, "Video") == media_class) {
|
|
|
|
|
if (direction == PW_DIRECTION_OUTPUT) {
|
|
|
|
|
if ((str = pw_properties_get(object->props, PW_KEY_NODE_PLUGGED)) != NULL)
|
|
|
|
|
node->plugged = pw_properties_parse_uint64(str);
|
|
|
|
|
else
|
|
|
|
|
node->plugged = SPA_TIMESPEC_TO_NSEC(&impl->now);
|
|
|
|
|
}
|
2020-07-28 10:50:00 +02:00
|
|
|
node->active = node->configured = true;
|
2020-02-06 17:42:52 +01:00
|
|
|
}
|
2020-07-02 14:36:28 +02:00
|
|
|
else if (strstr(media_class, "Unknown") == media_class) {
|
2020-07-28 10:50:00 +02:00
|
|
|
node->active = node->configured = true;
|
2020-07-02 14:36:28 +02:00
|
|
|
}
|
2020-01-08 10:48:48 +01:00
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
node->direction = direction;
|
|
|
|
|
node->type = NODE_TYPE_STREAM;
|
|
|
|
|
node->media = strdup(media_class);
|
2020-03-03 10:24:18 +01:00
|
|
|
pw_log_debug(NAME" %p: node %d is stream %s", impl, object->id, node->media);
|
2020-01-07 16:07:51 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const char *media;
|
2021-01-27 16:27:06 +01:00
|
|
|
bool virtual = false;
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
if (strstr(media_class, "Audio/") == media_class) {
|
|
|
|
|
media_class += strlen("Audio/");
|
|
|
|
|
media = "Audio";
|
|
|
|
|
}
|
|
|
|
|
else if (strstr(media_class, "Video/") == media_class) {
|
|
|
|
|
media_class += strlen("Video/");
|
|
|
|
|
media = "Video";
|
2020-07-28 10:50:00 +02:00
|
|
|
node->active = node->configured = true;
|
2020-01-07 16:07:51 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
|
Add support for virtual source
A virtual source is usually implemented with a null-sink, it looks
like a source to pulseaudio clients but just forwards what it gets
as input.
Make sure the port names make sense.
You can use the null sink now as:
pactl load-module module-null-sink sink_name=source object.linger=1
media.class=Audio/Source/Virtual channel_map=FL,FR,RL,RR
This creates a node with "input" and "capture" ports and looks
like a virtual source for pulseaudio clients.
pactl load-module module-null-sink sink_name=source object.linger=1
media.class=Audio/Sink channel_map=FL,FR,RL,RR
This creates a node with "playback" and "monitor" ports and looks
like a virtual sink for pulseaudio clients.
pactl load-module module-null-sink sink_name=source object.linger=1
media.class=Audio/Duplex channel_map=FL,FR,RL,RR
This creates a node with "playback" and "capture" ports and looks
like a virtual source and sink for pulseaudio clients.
2020-12-02 15:40:23 +01:00
|
|
|
if (strcmp(media_class, "Sink") == 0 ||
|
2021-01-27 16:14:39 +01:00
|
|
|
strcmp(media_class, "Duplex") == 0)
|
2020-01-07 16:07:51 +01:00
|
|
|
direction = PW_DIRECTION_INPUT;
|
2021-01-27 16:27:06 +01:00
|
|
|
else if (strcmp(media_class, "Source") == 0)
|
2020-01-07 16:07:51 +01:00
|
|
|
direction = PW_DIRECTION_OUTPUT;
|
2021-01-27 16:27:06 +01:00
|
|
|
else if (strcmp(media_class, "Source/Virtual") == 0) {
|
|
|
|
|
virtual = true;
|
|
|
|
|
direction = PW_DIRECTION_OUTPUT;
|
|
|
|
|
} else
|
2020-01-07 16:07:51 +01:00
|
|
|
return 0;
|
|
|
|
|
|
2020-01-08 17:29:11 +01:00
|
|
|
if ((str = pw_properties_get(object->props, PW_KEY_NODE_PLUGGED)) != NULL)
|
|
|
|
|
node->plugged = pw_properties_parse_uint64(str);
|
|
|
|
|
else
|
|
|
|
|
node->plugged = SPA_TIMESPEC_TO_NSEC(&impl->now);
|
|
|
|
|
|
|
|
|
|
if ((str = pw_properties_get(object->props, PW_KEY_PRIORITY_SESSION)) != NULL)
|
|
|
|
|
node->priority = pw_properties_parse_int(str);
|
|
|
|
|
else
|
|
|
|
|
node->priority = 0;
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
node->direction = direction;
|
2021-01-27 16:27:06 +01:00
|
|
|
node->virtual = virtual;
|
2020-01-07 16:07:51 +01:00
|
|
|
node->type = NODE_TYPE_DEVICE;
|
|
|
|
|
node->media = strdup(media);
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: node %d '%s' prio:%d", impl,
|
|
|
|
|
object->id, node->media, node->priority);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-16 16:47:01 +01:00
|
|
|
node->enabled = true;
|
2020-01-07 16:07:51 +01:00
|
|
|
node->obj->obj.mask |= SM_NODE_CHANGE_MASK_PARAMS;
|
|
|
|
|
sm_object_add_listener(&node->obj->obj, &node->listener, &object_events, node);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void destroy_node(struct impl *impl, struct node *node)
|
|
|
|
|
{
|
|
|
|
|
spa_list_remove(&node->link);
|
2020-01-16 16:47:01 +01:00
|
|
|
if (node->enabled)
|
|
|
|
|
spa_hook_remove(&node->listener);
|
2020-01-07 16:07:51 +01:00
|
|
|
free(node->media);
|
2020-07-15 14:22:56 +02:00
|
|
|
if (node->peer && node->peer->peer == node)
|
2020-01-08 10:48:48 +01:00
|
|
|
node->peer->peer = NULL;
|
2020-01-07 16:07:51 +01:00
|
|
|
sm_object_remove_data((struct sm_object*)node->obj, SESSION_KEY);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 17:37:12 +01:00
|
|
|
static inline int strzcmp(const char *s1, const char *s2)
|
|
|
|
|
{
|
|
|
|
|
if (s1 == s2)
|
|
|
|
|
return 0;
|
|
|
|
|
if (s1 == NULL || s2 == NULL)
|
|
|
|
|
return 1;
|
|
|
|
|
return strcmp(s1, s2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int json_object_find(const char *obj, const char *key, char *value, size_t len)
|
|
|
|
|
{
|
|
|
|
|
struct spa_json it[2];
|
|
|
|
|
const char *v;
|
|
|
|
|
char k[128];
|
|
|
|
|
|
|
|
|
|
spa_json_init(&it[0], obj, strlen(obj));
|
|
|
|
|
if (spa_json_enter_object(&it[0], &it[1]) <= 0)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
|
|
while (spa_json_get_string(&it[1], k, sizeof(k)-1) > 0) {
|
|
|
|
|
if (strcmp(k, key) == 0) {
|
|
|
|
|
if (spa_json_get_string(&it[1], value, len) <= 0)
|
|
|
|
|
continue;
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
if (spa_json_next(&it[1], &v) <= 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool check_node_name(struct node *node, const char *name)
|
|
|
|
|
{
|
|
|
|
|
const char *str;
|
|
|
|
|
if ((str = pw_properties_get(node->obj->obj.props, PW_KEY_NODE_NAME)) != NULL &&
|
|
|
|
|
name != NULL && strcmp(str, name) == 0)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct node *find_node_by_id_name(struct impl *impl, uint32_t id, const char *name)
|
2020-10-14 12:35:27 +02:00
|
|
|
{
|
|
|
|
|
struct node *node;
|
2021-03-05 20:16:05 +01:00
|
|
|
uint32_t name_id = name ? (uint32_t)atoi(name) : SPA_ID_INVALID;
|
2021-03-05 17:37:12 +01:00
|
|
|
|
2020-10-14 12:35:27 +02:00
|
|
|
spa_list_for_each(node, &impl->node_list, link) {
|
2021-03-05 17:37:12 +01:00
|
|
|
if (node->id == id || node->id == name_id)
|
|
|
|
|
return node;
|
|
|
|
|
if (check_node_name(node, name))
|
2020-10-14 12:35:27 +02:00
|
|
|
return node;
|
|
|
|
|
}
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char *get_device_name(struct node *node)
|
|
|
|
|
{
|
|
|
|
|
if (node->type != NODE_TYPE_DEVICE ||
|
|
|
|
|
node->obj->obj.props == NULL)
|
|
|
|
|
return NULL;
|
|
|
|
|
return pw_properties_get(node->obj->obj.props, PW_KEY_NODE_NAME);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint32_t find_device_for_name(struct impl *impl, const char *name)
|
|
|
|
|
{
|
|
|
|
|
struct node *node;
|
|
|
|
|
const char *str;
|
2020-10-24 15:50:09 +02:00
|
|
|
uint32_t id = atoi(name);
|
2020-10-14 12:35:27 +02:00
|
|
|
|
|
|
|
|
spa_list_for_each(node, &impl->node_list, link) {
|
2020-10-24 15:50:09 +02:00
|
|
|
if (id == node->obj->obj.id)
|
|
|
|
|
return id;
|
2020-10-14 12:35:27 +02:00
|
|
|
if ((str = get_device_name(node)) == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
if (strcmp(str, name) == 0)
|
|
|
|
|
return node->obj->obj.id;
|
|
|
|
|
}
|
|
|
|
|
return SPA_ID_INVALID;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
static void session_create(void *data, struct sm_object *object)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
int res;
|
|
|
|
|
|
2020-02-06 17:42:52 +01:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &impl->now);
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Node) == 0)
|
|
|
|
|
res = handle_node(impl, object);
|
|
|
|
|
else
|
|
|
|
|
res = 0;
|
|
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
pw_log_warn(NAME" %p: can't handle global %d", impl, object->id);
|
2020-07-09 13:03:02 +02:00
|
|
|
} else
|
2020-01-07 16:07:51 +01:00
|
|
|
sm_media_session_schedule_rescan(impl->session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void session_remove(void *data, struct sm_object *object)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
pw_log_debug(NAME " %p: remove global '%d'", impl, object->id);
|
|
|
|
|
|
|
|
|
|
if (strcmp(object->type, PW_TYPE_INTERFACE_Node) == 0) {
|
2020-01-08 10:48:48 +01:00
|
|
|
struct node *n, *node;
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
if ((node = sm_object_get_data(object, SESSION_KEY)) != NULL)
|
|
|
|
|
destroy_node(impl, node);
|
2020-01-08 10:48:48 +01:00
|
|
|
|
|
|
|
|
spa_list_for_each(n, &impl->node_list, link) {
|
|
|
|
|
if (n->peer == node)
|
|
|
|
|
n->peer = NULL;
|
|
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
}
|
|
|
|
|
sm_media_session_schedule_rescan(impl->session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct find_data {
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
struct node *node;
|
2021-02-06 15:58:07 +02:00
|
|
|
|
|
|
|
|
const char *media;
|
|
|
|
|
bool capture_sink;
|
|
|
|
|
enum pw_direction direction;
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
bool exclusive;
|
|
|
|
|
int priority;
|
|
|
|
|
uint64_t plugged;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int find_node(void *data, struct node *node)
|
|
|
|
|
{
|
|
|
|
|
struct find_data *find = data;
|
|
|
|
|
struct impl *impl = find->impl;
|
|
|
|
|
int priority = 0;
|
|
|
|
|
uint64_t plugged = 0;
|
2020-02-19 18:15:57 +01:00
|
|
|
struct sm_device *device = node->obj->device;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2021-02-06 15:58:07 +02:00
|
|
|
if (node->obj->info == NULL) {
|
|
|
|
|
pw_log_debug(NAME " %p: skipping node '%d' with no node info", impl, node->id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 10:48:48 +01:00
|
|
|
pw_log_debug(NAME " %p: looking at node '%d' enabled:%d state:%d peer:%p exclusive:%d",
|
|
|
|
|
impl, node->id, node->enabled, node->obj->info->state, node->peer, node->exclusive);
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
if (!node->enabled || node->type == NODE_TYPE_UNKNOWN)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2020-02-19 18:15:57 +01:00
|
|
|
if (device && device->locked) {
|
|
|
|
|
pw_log_debug(".. device locked");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 15:58:07 +02:00
|
|
|
if ((find->capture_sink && node->direction != PW_DIRECTION_INPUT) ||
|
|
|
|
|
(!find->capture_sink && node->direction == find->direction)) {
|
2020-01-07 16:07:51 +01:00
|
|
|
pw_log_debug(".. same direction");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-02-06 15:58:07 +02:00
|
|
|
if (strcmp(node->media, find->media) != 0) {
|
|
|
|
|
pw_log_debug(".. incompatible media %s <-> %s", node->media, find->media);
|
2020-01-07 16:07:51 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-27 10:15:10 +02:00
|
|
|
plugged = node->plugged;
|
|
|
|
|
priority = node->priority;
|
|
|
|
|
|
2020-07-28 11:17:36 +02:00
|
|
|
if (node->media) {
|
2020-07-27 10:15:10 +02:00
|
|
|
bool is_default = false;
|
2021-03-05 17:37:12 +01:00
|
|
|
|
2020-07-28 11:17:36 +02:00
|
|
|
if (strcmp(node->media, "Audio") == 0) {
|
|
|
|
|
if (node->direction == PW_DIRECTION_INPUT)
|
2021-03-05 17:37:12 +01:00
|
|
|
is_default = check_node_name(node,
|
|
|
|
|
impl->defaults[DEFAULT_AUDIO_SINK].config);
|
2020-07-28 11:17:36 +02:00
|
|
|
else if (node->direction == PW_DIRECTION_OUTPUT)
|
2021-03-05 17:37:12 +01:00
|
|
|
is_default = check_node_name(node,
|
|
|
|
|
impl->defaults[DEFAULT_AUDIO_SOURCE].config);
|
2020-07-28 11:17:36 +02:00
|
|
|
} else if (strcmp(node->media, "Video") == 0) {
|
|
|
|
|
if (node->direction == PW_DIRECTION_OUTPUT)
|
2021-03-05 17:37:12 +01:00
|
|
|
is_default = check_node_name(node,
|
|
|
|
|
impl->defaults[DEFAULT_VIDEO_SOURCE].config);
|
2020-07-28 11:17:36 +02:00
|
|
|
}
|
2020-07-27 10:15:10 +02:00
|
|
|
if (is_default)
|
2020-09-17 12:35:33 +02:00
|
|
|
priority += 10000;
|
2020-07-15 15:53:30 +02:00
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-01-08 10:48:48 +01:00
|
|
|
if ((find->exclusive && node->obj->info->state == PW_NODE_STATE_RUNNING) ||
|
|
|
|
|
(node->peer && node->peer->exclusive)) {
|
2020-01-07 16:07:51 +01:00
|
|
|
pw_log_debug(NAME " %p: node '%d' in use", impl, node->id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: found node '%d' %"PRIu64" prio:%d", impl,
|
|
|
|
|
node->id, plugged, priority);
|
|
|
|
|
|
2020-07-27 10:15:10 +02:00
|
|
|
if (find->node == NULL ||
|
2020-01-07 16:07:51 +01:00
|
|
|
priority > find->priority ||
|
|
|
|
|
(priority == find->priority && plugged > find->plugged)) {
|
|
|
|
|
pw_log_debug(NAME " %p: new best %d %" PRIu64, impl, priority, plugged);
|
|
|
|
|
find->node = node;
|
|
|
|
|
find->priority = priority;
|
|
|
|
|
find->plugged = plugged;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 15:58:07 +02:00
|
|
|
static struct node *find_auto_default_node(struct impl *impl, const struct default_node *def)
|
|
|
|
|
{
|
|
|
|
|
struct node *node;
|
|
|
|
|
struct find_data find;
|
|
|
|
|
|
|
|
|
|
spa_zero(find);
|
|
|
|
|
find.impl = impl;
|
|
|
|
|
find.capture_sink = false;
|
|
|
|
|
find.exclusive = false;
|
|
|
|
|
|
|
|
|
|
if (strcmp(def->key, DEFAULT_AUDIO_SINK_KEY) == 0) {
|
|
|
|
|
find.media = "Audio";
|
|
|
|
|
find.direction = PW_DIRECTION_OUTPUT;
|
|
|
|
|
} else if (strcmp(def->key, DEFAULT_AUDIO_SOURCE_KEY) == 0) {
|
|
|
|
|
find.media = "Audio";
|
|
|
|
|
find.direction = PW_DIRECTION_INPUT;
|
|
|
|
|
} else if (strcmp(def->key, DEFAULT_VIDEO_SOURCE_KEY) == 0) {
|
|
|
|
|
find.media = "Video";
|
|
|
|
|
find.direction = PW_DIRECTION_INPUT;
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_list_for_each(node, &impl->node_list, link)
|
|
|
|
|
find_node(&find, node);
|
|
|
|
|
|
|
|
|
|
return find.node;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
static int link_nodes(struct node *node, struct node *peer)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = node->impl;
|
|
|
|
|
struct pw_properties *props;
|
2020-08-06 17:58:42 +02:00
|
|
|
struct node *output, *input;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-08-19 11:50:59 +02:00
|
|
|
pw_log_debug(NAME " %p: link nodes %d %d remix:%d", impl,
|
|
|
|
|
node->id, peer->id, !node->dont_remix);
|
2020-07-28 10:18:49 +02:00
|
|
|
|
|
|
|
|
if (node->dont_remix)
|
2020-08-19 11:50:59 +02:00
|
|
|
configure_node(node, NULL, false);
|
2020-07-28 10:18:49 +02:00
|
|
|
else {
|
2020-08-19 11:50:59 +02:00
|
|
|
configure_node(node, &peer->format, true);
|
2020-07-28 10:18:49 +02:00
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
if (node->direction == PW_DIRECTION_INPUT) {
|
2020-08-06 17:58:42 +02:00
|
|
|
output = peer;
|
|
|
|
|
input = node;
|
|
|
|
|
} else {
|
|
|
|
|
output = node;
|
|
|
|
|
input = peer;
|
2020-01-07 16:07:51 +01:00
|
|
|
}
|
|
|
|
|
props = pw_properties_new(NULL, NULL);
|
2020-08-06 17:58:42 +02:00
|
|
|
pw_properties_setf(props, PW_KEY_LINK_OUTPUT_NODE, "%d", output->id);
|
|
|
|
|
pw_properties_setf(props, PW_KEY_LINK_INPUT_NODE, "%d", input->id);
|
|
|
|
|
pw_log_info("linking node %d to node %d", output->id, input->id);
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-11-20 14:54:37 +01:00
|
|
|
if (sm_media_session_create_links(impl->session, &props->dict) > 0) {
|
2020-08-07 10:50:30 +02:00
|
|
|
node->peer = peer;
|
2020-11-20 14:54:37 +01:00
|
|
|
node->connect_count++;
|
|
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
pw_properties_free(props);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 14:26:16 +02:00
|
|
|
static int unlink_nodes(struct node *node, struct node *peer)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = node->impl;
|
|
|
|
|
struct pw_properties *props;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: unlink nodes %d %d", impl, node->id, peer->id);
|
|
|
|
|
|
|
|
|
|
if (peer->peer == node)
|
|
|
|
|
peer->peer = NULL;
|
|
|
|
|
node->peer = NULL;
|
|
|
|
|
|
|
|
|
|
if (node->direction == PW_DIRECTION_INPUT) {
|
|
|
|
|
struct node *t = node;
|
|
|
|
|
node = peer;
|
|
|
|
|
peer = t;
|
|
|
|
|
}
|
|
|
|
|
props = pw_properties_new(NULL, NULL);
|
|
|
|
|
pw_properties_setf(props, PW_KEY_LINK_OUTPUT_NODE, "%d", node->id);
|
|
|
|
|
pw_properties_setf(props, PW_KEY_LINK_INPUT_NODE, "%d", peer->id);
|
2020-07-20 14:40:50 +02:00
|
|
|
pw_log_info("unlinking node %d from peer node %d", node->id, peer->id);
|
2020-07-15 14:26:16 +02:00
|
|
|
|
|
|
|
|
sm_media_session_remove_links(impl->session, &props->dict);
|
|
|
|
|
|
|
|
|
|
pw_properties_free(props);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
static int rescan_node(struct impl *impl, struct node *n)
|
|
|
|
|
{
|
|
|
|
|
struct spa_dict *props;
|
2020-07-09 13:03:02 +02:00
|
|
|
const char *str;
|
2020-10-19 18:53:18 +02:00
|
|
|
bool exclusive, reconnect, autoconnect;
|
2020-07-09 13:03:02 +02:00
|
|
|
struct find_data find;
|
2020-01-07 16:07:51 +01:00
|
|
|
struct pw_node_info *info;
|
|
|
|
|
struct node *peer;
|
|
|
|
|
struct sm_object *obj;
|
2020-10-24 15:50:09 +02:00
|
|
|
uint32_t path_id;
|
2021-01-17 00:31:47 +02:00
|
|
|
bool follows_default;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-07-29 12:20:24 +02:00
|
|
|
if (!n->active) {
|
|
|
|
|
pw_log_debug(NAME " %p: node %d is not active", impl, n->id);
|
2020-01-07 16:07:51 +01:00
|
|
|
return 0;
|
2020-07-28 10:18:49 +02:00
|
|
|
}
|
2020-10-21 15:27:50 +02:00
|
|
|
if (n->moving) {
|
|
|
|
|
pw_log_debug(NAME " %p: node %d is moving", impl, n->id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-07-29 12:20:24 +02:00
|
|
|
if (n->type == NODE_TYPE_DEVICE) {
|
2020-08-19 11:50:59 +02:00
|
|
|
configure_node(n, NULL, false);
|
2020-01-07 16:07:51 +01:00
|
|
|
return 0;
|
2020-01-08 10:48:48 +01:00
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
if (n->obj->info == NULL || n->obj->info->props == NULL) {
|
|
|
|
|
pw_log_debug(NAME " %p: node %d has no properties", impl, n->id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 00:31:47 +02:00
|
|
|
info = n->obj->info;
|
|
|
|
|
props = info->props;
|
|
|
|
|
|
|
|
|
|
str = spa_dict_lookup(props, PW_KEY_NODE_DONT_RECONNECT);
|
|
|
|
|
reconnect = str ? !pw_properties_parse_bool(str) : true;
|
|
|
|
|
|
|
|
|
|
follows_default = (impl->streams_follow_default &&
|
|
|
|
|
n->type == NODE_TYPE_STREAM &&
|
|
|
|
|
reconnect &&
|
|
|
|
|
n->obj->target_node == NULL &&
|
2021-03-10 15:27:06 +01:00
|
|
|
((str = spa_dict_lookup(props, PW_KEY_NODE_TARGET)) == NULL ||
|
|
|
|
|
(uint32_t)atoi(str) == SPA_ID_INVALID));
|
2021-01-17 00:31:47 +02:00
|
|
|
|
|
|
|
|
if (n->peer != NULL && !follows_default) {
|
2020-01-07 16:07:51 +01:00
|
|
|
pw_log_debug(NAME " %p: node %d is already linked", impl, n->id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 17:00:49 +02:00
|
|
|
if ((str = spa_dict_lookup(props, PW_KEY_STREAM_DONT_REMIX)) != NULL)
|
2020-07-28 10:50:00 +02:00
|
|
|
n->dont_remix = pw_properties_parse_bool(str);
|
|
|
|
|
|
2020-10-21 17:00:49 +02:00
|
|
|
if ((str = spa_dict_lookup(props, PW_KEY_STREAM_MONITOR)) != NULL)
|
2020-07-28 10:50:00 +02:00
|
|
|
n->monitor = pw_properties_parse_bool(str);
|
|
|
|
|
|
2020-10-21 17:00:49 +02:00
|
|
|
if (n->direction == PW_DIRECTION_INPUT &&
|
|
|
|
|
(str = spa_dict_lookup(props, PW_KEY_STREAM_CAPTURE_SINK)) != NULL)
|
|
|
|
|
n->capture_sink = pw_properties_parse_bool(str);
|
|
|
|
|
|
2020-10-19 18:53:18 +02:00
|
|
|
autoconnect = false;
|
|
|
|
|
if ((str = spa_dict_lookup(props, PW_KEY_NODE_AUTOCONNECT)) != NULL)
|
|
|
|
|
autoconnect = pw_properties_parse_bool(str);
|
|
|
|
|
|
|
|
|
|
if ((str = spa_dict_lookup(props, PW_KEY_DEVICE_API)) != NULL &&
|
|
|
|
|
strcmp(str, "bluez5") == 0)
|
|
|
|
|
autoconnect = true;
|
|
|
|
|
|
|
|
|
|
if (!autoconnect) {
|
2020-01-07 16:07:51 +01:00
|
|
|
pw_log_debug(NAME" %p: node %d does not need autoconnect", impl, n->id);
|
2020-08-19 11:50:59 +02:00
|
|
|
configure_node(n, NULL, false);
|
2020-07-28 10:18:49 +02:00
|
|
|
return 0;
|
2020-01-07 16:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (n->media == NULL) {
|
|
|
|
|
pw_log_debug(NAME" %p: node %d has unknown media", impl, n->id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-14 12:35:27 +02:00
|
|
|
str = spa_dict_lookup(props, PW_KEY_NODE_EXCLUSIVE);
|
|
|
|
|
exclusive = str ? pw_properties_parse_bool(str) : false;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-10-14 12:35:27 +02:00
|
|
|
pw_log_debug(NAME " %p: exclusive:%d", impl, exclusive);
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-10-14 12:35:27 +02:00
|
|
|
spa_zero(find);
|
2020-01-07 16:07:51 +01:00
|
|
|
find.impl = impl;
|
2021-02-06 15:58:07 +02:00
|
|
|
find.media = n->media;
|
|
|
|
|
find.capture_sink = n->capture_sink;
|
|
|
|
|
find.direction = n->direction;
|
2020-01-07 16:07:51 +01:00
|
|
|
find.exclusive = exclusive;
|
|
|
|
|
|
2020-10-14 12:35:27 +02:00
|
|
|
/* we always honour the target node asked for by the client */
|
2020-10-24 15:50:09 +02:00
|
|
|
path_id = SPA_ID_INVALID;
|
2020-10-14 12:35:27 +02:00
|
|
|
if ((str = spa_dict_lookup(props, PW_KEY_NODE_TARGET)) != NULL)
|
2020-10-24 15:50:09 +02:00
|
|
|
path_id = find_device_for_name(impl, str);
|
2020-10-14 12:35:27 +02:00
|
|
|
if (path_id == SPA_ID_INVALID && n->obj->target_node != NULL)
|
|
|
|
|
path_id = find_device_for_name(impl, n->obj->target_node);
|
2020-07-09 13:03:02 +02:00
|
|
|
|
2021-01-17 00:31:47 +02:00
|
|
|
pw_log_info("trying to link node %d exclusive:%d reconnect:%d target:%d follows-default:%d", n->id,
|
|
|
|
|
exclusive, reconnect, path_id, follows_default);
|
|
|
|
|
|
|
|
|
|
if (n->peer != NULL) {
|
|
|
|
|
spa_list_for_each(peer, &impl->node_list, link)
|
|
|
|
|
find_node(&find, peer);
|
|
|
|
|
|
|
|
|
|
if (follows_default && find.node != NULL && find.node != n->peer) {
|
|
|
|
|
pw_log_debug(NAME " %p: node %d follows default, changed (%d -> %d)", impl, n->id,
|
|
|
|
|
n->peer->id, find.node->id);
|
|
|
|
|
unlink_nodes(n, n->peer);
|
|
|
|
|
} else {
|
|
|
|
|
pw_log_debug(NAME " %p: node %d already linked (not changing)", impl, n->id);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-14 12:35:27 +02:00
|
|
|
|
|
|
|
|
if (path_id != SPA_ID_INVALID) {
|
2020-04-22 12:47:18 +02:00
|
|
|
pw_log_debug(NAME " %p: target:%d", impl, path_id);
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-12-20 11:41:59 +01:00
|
|
|
if (!reconnect)
|
|
|
|
|
n->obj->target_node = NULL;
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
if ((obj = sm_media_session_find_object(impl->session, path_id)) != NULL) {
|
2020-07-03 16:25:28 +02:00
|
|
|
pw_log_debug(NAME " %p: found target:%d type:%s", impl,
|
|
|
|
|
path_id, obj->type);
|
2020-01-07 16:07:51 +01:00
|
|
|
if (strcmp(obj->type, PW_TYPE_INTERFACE_Node) == 0) {
|
|
|
|
|
peer = sm_object_get_data(obj, SESSION_KEY);
|
2020-01-16 16:19:09 +01:00
|
|
|
if (peer == NULL)
|
|
|
|
|
return -ENOENT;
|
2020-01-07 16:07:51 +01:00
|
|
|
goto do_link;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-09 13:03:02 +02:00
|
|
|
pw_log_warn("node %d target:%d not found, find fallback:%d", n->id,
|
|
|
|
|
path_id, reconnect);
|
2020-01-07 16:07:51 +01:00
|
|
|
}
|
2020-11-20 14:54:37 +01:00
|
|
|
if (path_id == SPA_ID_INVALID && (reconnect || n->connect_count == 0)) {
|
2021-01-17 00:31:47 +02:00
|
|
|
if (find.node == NULL)
|
|
|
|
|
spa_list_for_each(peer, &impl->node_list, link)
|
|
|
|
|
find_node(&find, peer);
|
|
|
|
|
} else {
|
|
|
|
|
find.node = NULL;
|
2020-07-09 13:03:02 +02:00
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
if (find.node == NULL) {
|
|
|
|
|
struct sm_object *obj;
|
|
|
|
|
|
2021-01-18 20:56:40 +01:00
|
|
|
if (!reconnect) {
|
|
|
|
|
pw_log_info("don-reconnect target node destroyed: destroy %d", n->id);
|
2020-04-02 15:26:39 +02:00
|
|
|
sm_media_session_destroy_object(impl->session, n->id);
|
2021-01-18 20:56:40 +01:00
|
|
|
} else {
|
|
|
|
|
pw_log_warn("no node found for %d", n->id);
|
|
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
obj = sm_media_session_find_object(impl->session, n->client_id);
|
2020-07-02 14:36:28 +02:00
|
|
|
pw_log_debug(NAME " %p: client_id:%d object:%p type:%s", impl,
|
|
|
|
|
n->client_id, obj, obj ? obj->type : "None");
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
if (obj && strcmp(obj->type, PW_TYPE_INTERFACE_Client) == 0) {
|
|
|
|
|
pw_client_error((struct pw_client*)obj->proxy,
|
|
|
|
|
n->id, -ENOENT, "no node available");
|
|
|
|
|
}
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
|
|
|
|
peer = find.node;
|
|
|
|
|
|
2020-01-08 10:48:48 +01:00
|
|
|
if (exclusive && peer->obj->info->state == PW_NODE_STATE_RUNNING) {
|
2020-07-09 13:03:02 +02:00
|
|
|
pw_log_warn("node %d busy, can't get exclusive access", peer->id);
|
2020-01-07 16:07:51 +01:00
|
|
|
return -EBUSY;
|
|
|
|
|
}
|
2020-01-08 10:48:48 +01:00
|
|
|
n->exclusive = exclusive;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: linking to node '%d'", impl, peer->id);
|
|
|
|
|
|
|
|
|
|
do_link:
|
|
|
|
|
link_nodes(n, peer);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-09 15:52:53 +01:00
|
|
|
static void session_info(void *data, const struct pw_core_info *info)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
|
|
|
|
|
if (info && (info->change_mask & PW_CORE_CHANGE_MASK_PROPS)) {
|
|
|
|
|
const char *str;
|
|
|
|
|
|
|
|
|
|
if ((str = spa_dict_lookup(info->props, "default.clock.rate")) != NULL)
|
|
|
|
|
impl->sample_rate = atoi(str);
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: props changed sample_rate:%d", impl, impl->sample_rate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 15:58:07 +02:00
|
|
|
static void refresh_auto_default_nodes(struct impl *impl)
|
|
|
|
|
{
|
|
|
|
|
struct default_node *def;
|
|
|
|
|
|
2021-02-19 17:54:31 +01:00
|
|
|
if (impl->session->metadata == NULL)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-03-05 17:37:12 +01:00
|
|
|
pw_log_debug(NAME" %p: refresh", impl);
|
|
|
|
|
|
2021-02-06 15:58:07 +02:00
|
|
|
/* Auto set default nodes */
|
|
|
|
|
for (def = impl->defaults; def->key != NULL; ++def) {
|
|
|
|
|
struct node *node;
|
|
|
|
|
node = find_auto_default_node(impl, def);
|
2021-03-05 17:37:12 +01:00
|
|
|
if (node == NULL && def->value != NULL) {
|
|
|
|
|
def->value = NULL;
|
|
|
|
|
pw_metadata_set_property(impl->session->metadata,
|
|
|
|
|
PW_ID_CORE, def->key, NULL, NULL);
|
|
|
|
|
} else if (node != NULL) {
|
|
|
|
|
const char *name = pw_properties_get(node->obj->obj.props, PW_KEY_NODE_NAME);
|
|
|
|
|
char buf[1024];
|
|
|
|
|
|
2021-03-05 20:16:29 +01:00
|
|
|
if (name == NULL || strzcmp(name, def->value) == 0)
|
2021-03-05 17:37:12 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
free(def->value);
|
|
|
|
|
def->value = strdup(name);
|
|
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "{ \"name\": \"%s\" }", name);
|
|
|
|
|
pw_metadata_set_property(impl->session->metadata,
|
|
|
|
|
PW_ID_CORE, def->key,
|
|
|
|
|
"Spa:String:JSON", buf);
|
2021-02-06 15:58:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
static void session_rescan(void *data, int seq)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
struct node *node;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: rescan", impl);
|
|
|
|
|
|
|
|
|
|
spa_list_for_each(node, &impl->node_list, link)
|
|
|
|
|
rescan_node(impl, node);
|
2021-02-06 15:58:07 +02:00
|
|
|
|
|
|
|
|
refresh_auto_default_nodes(impl);
|
2020-01-07 16:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void session_destroy(void *data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
spa_hook_remove(&impl->listener);
|
2020-07-15 14:26:16 +02:00
|
|
|
if (impl->session->metadata)
|
|
|
|
|
spa_hook_remove(&impl->meta_listener);
|
2020-01-07 16:07:51 +01:00
|
|
|
free(impl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct sm_media_session_events session_events = {
|
|
|
|
|
SM_VERSION_MEDIA_SESSION_EVENTS,
|
2020-01-09 15:52:53 +01:00
|
|
|
.info = session_info,
|
2020-01-07 16:07:51 +01:00
|
|
|
.create = session_create,
|
|
|
|
|
.remove = session_remove,
|
|
|
|
|
.rescan = session_rescan,
|
|
|
|
|
.destroy = session_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-21 15:39:37 +02:00
|
|
|
static int do_move_node(struct node *n, struct node *src, struct node *dst)
|
|
|
|
|
{
|
|
|
|
|
n->moving = true;
|
|
|
|
|
if (src)
|
|
|
|
|
unlink_nodes(n, src);
|
|
|
|
|
if (dst)
|
|
|
|
|
link_nodes(n, dst);
|
|
|
|
|
n->moving = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-20 14:40:50 +02:00
|
|
|
static int handle_move(struct impl *impl, struct node *src_node, struct node *dst_node)
|
|
|
|
|
{
|
|
|
|
|
const char *str;
|
|
|
|
|
struct pw_node_info *info;
|
|
|
|
|
|
2020-07-20 14:46:44 +02:00
|
|
|
if (src_node->peer == dst_node)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2020-07-20 14:40:50 +02:00
|
|
|
if ((info = src_node->obj->info) == NULL)
|
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
|
|
if ((str = spa_dict_lookup(info->props, PW_KEY_NODE_DONT_RECONNECT)) != NULL &&
|
|
|
|
|
pw_properties_parse_bool(str)) {
|
|
|
|
|
pw_log_warn("can't reconnect node %d to %d", src_node->id,
|
|
|
|
|
dst_node->id);
|
|
|
|
|
return -EPERM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pw_log_info("move node %d: from peer %d to %d", src_node->id,
|
|
|
|
|
src_node->peer ? src_node->peer->id : SPA_ID_INVALID,
|
|
|
|
|
dst_node->id);
|
|
|
|
|
|
2020-10-14 12:35:27 +02:00
|
|
|
free(src_node->obj->target_node);
|
|
|
|
|
str = get_device_name(dst_node);
|
|
|
|
|
src_node->obj->target_node = str ? strdup(str) : NULL;
|
|
|
|
|
|
2020-10-21 15:39:37 +02:00
|
|
|
return do_move_node(src_node, src_node->peer, dst_node);
|
2020-07-20 14:40:50 +02:00
|
|
|
}
|
|
|
|
|
|
2020-07-15 14:26:16 +02:00
|
|
|
static int metadata_property(void *object, uint32_t subject,
|
|
|
|
|
const char *key, const char *type, const char *value)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = object;
|
2020-07-20 14:40:50 +02:00
|
|
|
|
2020-07-15 14:26:16 +02:00
|
|
|
if (subject == PW_ID_CORE) {
|
2021-02-06 15:58:07 +02:00
|
|
|
struct default_node *def;
|
2021-01-17 00:31:47 +02:00
|
|
|
bool changed = false;
|
2021-03-05 17:37:12 +01:00
|
|
|
char *val = NULL;
|
2021-03-09 12:48:48 +01:00
|
|
|
char name[1024];
|
2021-03-05 17:37:12 +01:00
|
|
|
|
|
|
|
|
if (key != NULL && value != NULL) {
|
|
|
|
|
pw_log_info("meta %s: %s", key, value);
|
|
|
|
|
if (json_object_find(value, "name", name, sizeof(name)) < 0)
|
|
|
|
|
return 0;
|
|
|
|
|
pw_log_info("meta name: %s", name);
|
|
|
|
|
val = name;
|
|
|
|
|
}
|
2021-02-06 15:58:07 +02:00
|
|
|
for (def = impl->defaults; def->key != NULL; ++def) {
|
|
|
|
|
if (key == NULL || strcmp(key, def->key_config) == 0) {
|
2021-03-05 17:37:12 +01:00
|
|
|
if (strzcmp(def->config, val) != 0)
|
2021-02-06 15:58:07 +02:00
|
|
|
changed = true;
|
2021-03-05 17:37:12 +01:00
|
|
|
free(def->config);
|
|
|
|
|
def->config = val ? strdup(val) : NULL;
|
2021-02-06 15:58:07 +02:00
|
|
|
}
|
|
|
|
|
if (key == NULL || strcmp(key, def->key) == 0) {
|
2021-03-05 17:37:12 +01:00
|
|
|
bool eff_changed = strzcmp(def->value, val) != 0;
|
|
|
|
|
free(def->value);
|
|
|
|
|
def->value = val ? strdup(val) : NULL;
|
2021-02-06 15:58:07 +02:00
|
|
|
|
|
|
|
|
/* The effective value was changed. In case it was changed by
|
|
|
|
|
* someone else than us, reset the value to avoid confusion. */
|
|
|
|
|
if (eff_changed)
|
|
|
|
|
refresh_auto_default_nodes(impl);
|
|
|
|
|
}
|
2020-07-15 14:26:16 +02:00
|
|
|
}
|
2021-02-06 15:58:07 +02:00
|
|
|
if (changed)
|
2021-01-17 00:31:47 +02:00
|
|
|
sm_media_session_schedule_rescan(impl->session);
|
2021-03-05 17:37:12 +01:00
|
|
|
} else if (key != NULL && strcmp(key, "target.node") == 0) {
|
|
|
|
|
if (value != NULL) {
|
2020-08-17 17:50:42 +02:00
|
|
|
struct node *src_node, *dst_node;
|
|
|
|
|
|
2021-03-05 17:37:12 +01:00
|
|
|
dst_node = find_node_by_id_name(impl, SPA_ID_INVALID, value);
|
|
|
|
|
src_node = dst_node ? find_node_by_id_name(impl, subject, NULL) : NULL;
|
2020-08-17 17:50:42 +02:00
|
|
|
|
|
|
|
|
if (dst_node && src_node)
|
|
|
|
|
handle_move(impl, src_node, dst_node);
|
2021-03-05 17:37:12 +01:00
|
|
|
} else {
|
2021-01-18 13:22:58 +02:00
|
|
|
/* Unset target node. Schedule rescan to re-link, if needed. */
|
|
|
|
|
struct node *src_node;
|
2021-03-05 17:37:12 +01:00
|
|
|
src_node = find_node_by_id_name(impl, subject, NULL);
|
2021-01-18 13:22:58 +02:00
|
|
|
if (src_node) {
|
|
|
|
|
free(src_node->obj->target_node);
|
|
|
|
|
src_node->obj->target_node = NULL;
|
|
|
|
|
sm_media_session_schedule_rescan(impl->session);
|
|
|
|
|
}
|
2020-07-20 14:40:50 +02:00
|
|
|
}
|
2020-07-15 14:26:16 +02:00
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct pw_metadata_events metadata_events = {
|
|
|
|
|
PW_VERSION_METADATA_EVENTS,
|
|
|
|
|
.property = metadata_property,
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
int sm_policy_node_start(struct sm_media_session *session)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl;
|
2021-01-17 00:31:47 +02:00
|
|
|
const char *flag;
|
2020-01-07 16:07:51 +01:00
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
impl->session = session;
|
|
|
|
|
impl->context = session->context;
|
|
|
|
|
|
2020-01-09 15:52:53 +01:00
|
|
|
impl->sample_rate = 48000;
|
2021-02-06 15:58:07 +02:00
|
|
|
|
|
|
|
|
impl->defaults[DEFAULT_AUDIO_SINK] = (struct default_node){
|
2021-03-05 17:37:12 +01:00
|
|
|
DEFAULT_AUDIO_SINK_KEY, DEFAULT_CONFIG_AUDIO_SINK_KEY, NULL, NULL
|
2021-02-06 15:58:07 +02:00
|
|
|
};
|
|
|
|
|
impl->defaults[DEFAULT_AUDIO_SOURCE] = (struct default_node){
|
2021-03-05 17:37:12 +01:00
|
|
|
DEFAULT_AUDIO_SOURCE_KEY, DEFAULT_CONFIG_AUDIO_SOURCE_KEY, NULL, NULL
|
2021-02-06 15:58:07 +02:00
|
|
|
};
|
|
|
|
|
impl->defaults[DEFAULT_VIDEO_SOURCE] = (struct default_node){
|
2021-03-05 17:37:12 +01:00
|
|
|
DEFAULT_VIDEO_SOURCE_KEY, DEFAULT_CONFIG_VIDEO_SOURCE_KEY, NULL, NULL
|
2021-02-06 15:58:07 +02:00
|
|
|
};
|
2021-03-05 17:37:12 +01:00
|
|
|
impl->defaults[3] = (struct default_node){ NULL, NULL, NULL, NULL };
|
2020-01-09 15:52:53 +01:00
|
|
|
|
2021-01-17 00:31:47 +02:00
|
|
|
flag = pw_properties_get(session->props, NAME ".streams-follow-default");
|
|
|
|
|
impl->streams_follow_default = (flag != NULL && pw_properties_parse_bool(flag));
|
|
|
|
|
|
2020-01-07 16:07:51 +01:00
|
|
|
spa_list_init(&impl->node_list);
|
|
|
|
|
|
2020-07-15 14:26:16 +02:00
|
|
|
sm_media_session_add_listener(impl->session,
|
|
|
|
|
&impl->listener,
|
|
|
|
|
&session_events, impl);
|
2020-01-07 16:07:51 +01:00
|
|
|
|
2020-07-15 14:26:16 +02:00
|
|
|
if (session->metadata) {
|
|
|
|
|
pw_metadata_add_listener(session->metadata,
|
|
|
|
|
&impl->meta_listener,
|
|
|
|
|
&metadata_events, impl);
|
|
|
|
|
}
|
2020-01-07 16:07:51 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|