2019-11-13 09:38:40 +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>
|
|
|
|
|
|
|
|
|
|
#include "pipewire/pipewire.h"
|
|
|
|
|
#include "pipewire/private.h"
|
|
|
|
|
#include "extensions/session-manager.h"
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
#include "media-session.h"
|
|
|
|
|
|
2019-11-13 09:38:40 +01:00
|
|
|
#define NAME "policy-ep"
|
2019-11-15 12:08:46 +01:00
|
|
|
#define SESSION_KEY "policy-endpoint"
|
2019-11-13 09:38:40 +01:00
|
|
|
|
|
|
|
|
#define DEFAULT_CHANNELS 2
|
|
|
|
|
#define DEFAULT_SAMPLERATE 48000
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_IDLE_SECONDS 3
|
|
|
|
|
|
|
|
|
|
struct impl {
|
|
|
|
|
struct timespec now;
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
struct sm_media_session *session;
|
|
|
|
|
struct spa_hook listener;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
struct pw_core *core;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
|
|
|
|
struct spa_list endpoint_list;
|
|
|
|
|
int seq;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct endpoint {
|
2019-11-14 18:35:29 +01:00
|
|
|
struct sm_endpoint *obj;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
uint32_t id;
|
|
|
|
|
struct impl *impl;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
struct spa_list link; /**< link in impl endpoint_list */
|
|
|
|
|
enum pw_direction direction;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
|
|
|
|
struct endpoint *peer;
|
|
|
|
|
|
|
|
|
|
uint32_t client_id;
|
|
|
|
|
int32_t priority;
|
|
|
|
|
|
|
|
|
|
#define ENDPOINT_TYPE_UNKNOWN 0
|
|
|
|
|
#define ENDPOINT_TYPE_STREAM 1
|
|
|
|
|
#define ENDPOINT_TYPE_DEVICE 2
|
|
|
|
|
uint32_t type;
|
|
|
|
|
char *media;
|
|
|
|
|
|
|
|
|
|
uint32_t media_type;
|
|
|
|
|
uint32_t media_subtype;
|
|
|
|
|
struct spa_audio_info_raw format;
|
|
|
|
|
|
|
|
|
|
uint64_t plugged;
|
|
|
|
|
unsigned int exclusive:1;
|
|
|
|
|
unsigned int enabled:1;
|
|
|
|
|
unsigned int busy:1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct stream {
|
2019-11-14 18:35:29 +01:00
|
|
|
struct sm_endpoint_stream *obj;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
uint32_t id;
|
|
|
|
|
struct impl *impl;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
struct endpoint *endpoint;
|
2019-11-13 09:38:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int
|
2019-11-14 18:35:29 +01:00
|
|
|
handle_endpoint(struct impl *impl, struct sm_object *object)
|
2019-11-13 09:38:40 +01:00
|
|
|
{
|
|
|
|
|
const char *str, *media_class;
|
|
|
|
|
enum pw_direction direction;
|
|
|
|
|
struct endpoint *ep;
|
|
|
|
|
uint32_t client_id = SPA_ID_INVALID;
|
|
|
|
|
|
2019-11-15 12:08:46 +01:00
|
|
|
if (sm_object_get_data(object, SESSION_KEY) != NULL)
|
2019-11-14 18:35:29 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (object->props) {
|
|
|
|
|
if ((str = pw_properties_get(object->props, PW_KEY_CLIENT_ID)) != NULL)
|
2019-11-13 09:38:40 +01:00
|
|
|
client_id = atoi(str);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
media_class = object->props ? pw_properties_get(object->props, PW_KEY_MEDIA_CLASS) : NULL;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
|
|
|
|
pw_log_debug(NAME" %p: endpoint "PW_KEY_MEDIA_CLASS" %s", impl, media_class);
|
|
|
|
|
|
|
|
|
|
if (media_class == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2019-11-15 12:08:46 +01:00
|
|
|
ep = sm_object_add_data(object, SESSION_KEY, sizeof(struct endpoint));
|
2019-11-14 18:35:29 +01:00
|
|
|
ep->obj = (struct sm_endpoint*)object;
|
|
|
|
|
ep->id = object->id;
|
|
|
|
|
ep->impl = impl;
|
|
|
|
|
ep->client_id = client_id;
|
|
|
|
|
ep->type = ENDPOINT_TYPE_UNKNOWN;
|
|
|
|
|
ep->enabled = true;
|
|
|
|
|
spa_list_append(&impl->endpoint_list, &ep->link);
|
|
|
|
|
|
2019-11-13 09:38:40 +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;
|
|
|
|
|
|
|
|
|
|
ep->direction = direction;
|
|
|
|
|
ep->type = ENDPOINT_TYPE_STREAM;
|
|
|
|
|
ep->media = strdup(media_class);
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_debug(NAME "%p: endpoint %d is stream %s", impl, object->id, ep->media);
|
2019-11-13 09:38:40 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (strstr(media_class, "Audio/") == media_class) {
|
|
|
|
|
media_class += strlen("Audio/");
|
|
|
|
|
}
|
|
|
|
|
else if (strstr(media_class, "Video/") == media_class) {
|
|
|
|
|
media_class += strlen("Video/");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (strcmp(media_class, "Sink") == 0)
|
|
|
|
|
direction = PW_DIRECTION_OUTPUT;
|
|
|
|
|
else if (strcmp(media_class, "Source") == 0)
|
|
|
|
|
direction = PW_DIRECTION_INPUT;
|
|
|
|
|
else
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
ep->direction = direction;
|
|
|
|
|
ep->type = ENDPOINT_TYPE_DEVICE;
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_debug(NAME" %p: endpoint %d prio:%d", impl, object->id, ep->priority);
|
2019-11-13 09:38:40 +01:00
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2019-11-14 18:35:29 +01:00
|
|
|
handle_stream(struct impl *impl, struct sm_object *object)
|
2019-11-13 09:38:40 +01:00
|
|
|
{
|
2019-11-14 18:35:29 +01:00
|
|
|
struct sm_endpoint_stream *stream = (struct sm_endpoint_stream*)object;
|
2019-11-13 09:38:40 +01:00
|
|
|
struct stream *s;
|
|
|
|
|
struct endpoint *ep;
|
|
|
|
|
|
2019-11-15 12:08:46 +01:00
|
|
|
if (sm_object_get_data(object, SESSION_KEY) != NULL)
|
2019-11-14 18:35:29 +01:00
|
|
|
return 0;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
if (stream->endpoint == NULL)
|
|
|
|
|
return 0;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-15 12:08:46 +01:00
|
|
|
ep = sm_object_get_data(&stream->endpoint->obj, SESSION_KEY);
|
2019-11-14 18:35:29 +01:00
|
|
|
if (ep == NULL)
|
|
|
|
|
return 0;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-15 12:08:46 +01:00
|
|
|
s = sm_object_add_data(object, SESSION_KEY, sizeof(struct stream));
|
2019-11-14 18:35:29 +01:00
|
|
|
s->obj = (struct sm_endpoint_stream*)object;
|
|
|
|
|
s->id = object->id;
|
|
|
|
|
s->impl = impl;
|
2019-11-13 09:38:40 +01:00
|
|
|
s->endpoint = ep;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
static void session_update(void *data, struct sm_object *object)
|
2019-11-13 09:38:40 +01:00
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
|
|
|
|
int res;
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
switch (object->type) {
|
2019-11-13 09:38:40 +01:00
|
|
|
case PW_TYPE_INTERFACE_Endpoint:
|
2019-11-14 18:35:29 +01:00
|
|
|
res = handle_endpoint(impl, object);
|
2019-11-13 09:38:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PW_TYPE_INTERFACE_EndpointStream:
|
2019-11-14 18:35:29 +01:00
|
|
|
res = handle_stream(impl, object);
|
2019-11-13 09:38:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
res = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (res < 0) {
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_warn(NAME" %p: can't handle global %d", impl, object->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
}
|
|
|
|
|
else
|
2019-11-14 18:35:29 +01:00
|
|
|
sm_media_session_schedule_rescan(impl->session);
|
2019-11-13 09:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
static void session_remove(void *data, struct sm_object *object)
|
2019-11-13 09:38:40 +01:00
|
|
|
{
|
|
|
|
|
struct impl *impl = data;
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_debug(NAME " %p: remove global '%d'", impl, object->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
switch (object->type) {
|
|
|
|
|
case PW_TYPE_INTERFACE_Endpoint:
|
|
|
|
|
{
|
|
|
|
|
struct endpoint *ep;
|
2019-11-15 12:08:46 +01:00
|
|
|
if ((ep = sm_object_get_data(object, SESSION_KEY)) != NULL) {
|
2019-11-14 18:35:29 +01:00
|
|
|
spa_list_remove(&ep->link);
|
2019-11-15 12:08:46 +01:00
|
|
|
free(ep->media);
|
|
|
|
|
}
|
2019-11-14 18:35:29 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
sm_media_session_schedule_rescan(impl->session);
|
2019-11-13 09:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct find_data {
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
uint32_t path_id;
|
|
|
|
|
const char *media_class;
|
|
|
|
|
struct endpoint *endpoint;
|
|
|
|
|
bool exclusive;
|
|
|
|
|
int priority;
|
|
|
|
|
uint64_t plugged;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int find_endpoint(void *data, struct endpoint *endpoint)
|
|
|
|
|
{
|
|
|
|
|
struct find_data *find = data;
|
|
|
|
|
struct impl *impl = find->impl;
|
|
|
|
|
const struct spa_dict *props;
|
|
|
|
|
const char *str;
|
|
|
|
|
int priority = 0;
|
|
|
|
|
uint64_t plugged = 0;
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: looking at endpoint '%d' enabled:%d busy:%d exclusive:%d",
|
2019-11-14 18:35:29 +01:00
|
|
|
impl, endpoint->id, endpoint->enabled, endpoint->busy, endpoint->exclusive);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
|
|
|
|
if (!endpoint->enabled)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
if (find->path_id != SPA_ID_INVALID && endpoint->id != find->path_id)
|
2019-11-13 09:38:40 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (find->path_id == SPA_ID_INVALID) {
|
2019-11-14 18:35:29 +01:00
|
|
|
if (endpoint->obj->info == NULL ||
|
|
|
|
|
(props = endpoint->obj->info->props) == NULL)
|
2019-11-13 09:38:40 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if ((str = spa_dict_lookup(props, PW_KEY_MEDIA_CLASS)) == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if (strcmp(str, find->media_class) != 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
plugged = endpoint->plugged;
|
|
|
|
|
priority = endpoint->priority;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((find->exclusive && endpoint->busy) || endpoint->exclusive) {
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_debug(NAME " %p: endpoint '%d' in use", impl, endpoint->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pw_log_debug(NAME " %p: found endpoint '%d' %"PRIu64" prio:%d", impl,
|
2019-11-14 18:35:29 +01:00
|
|
|
endpoint->id, plugged, priority);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
|
|
|
|
if (find->endpoint == NULL ||
|
|
|
|
|
priority > find->priority ||
|
|
|
|
|
(priority == find->priority && plugged > find->plugged)) {
|
|
|
|
|
pw_log_debug(NAME " %p: new best %d %" PRIu64, impl, priority, plugged);
|
|
|
|
|
find->endpoint = endpoint;
|
|
|
|
|
find->priority = priority;
|
|
|
|
|
find->plugged = plugged;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int link_endpoints(struct endpoint *endpoint, enum pw_direction direction, struct endpoint *peer, int max)
|
|
|
|
|
{
|
2019-11-14 18:35:29 +01:00
|
|
|
struct impl *impl = peer->impl;
|
|
|
|
|
struct pw_properties *props;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_debug(NAME " %p: link endpoints %d %d %d", impl, max, endpoint->id, peer->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-15 12:08:46 +01:00
|
|
|
if (endpoint->direction == PW_DIRECTION_INPUT) {
|
|
|
|
|
struct endpoint *t = endpoint;
|
|
|
|
|
endpoint = peer;
|
|
|
|
|
peer = t;
|
2019-11-14 18:35:29 +01:00
|
|
|
}
|
2019-11-15 12:08:46 +01:00
|
|
|
props = pw_properties_new(NULL, NULL);
|
|
|
|
|
pw_properties_setf(props, PW_KEY_ENDPOINT_LINK_OUTPUT_ENDPOINT, "%d", endpoint->id);
|
|
|
|
|
pw_properties_setf(props, PW_KEY_ENDPOINT_LINK_OUTPUT_STREAM, "%d", -1);
|
|
|
|
|
pw_properties_setf(props, PW_KEY_ENDPOINT_LINK_INPUT_ENDPOINT, "%d", peer->id);
|
|
|
|
|
pw_properties_setf(props, PW_KEY_ENDPOINT_LINK_INPUT_STREAM, "%d", -1);
|
|
|
|
|
pw_log_debug(NAME " %p: endpoint %d -> endpoint %d", impl,
|
|
|
|
|
endpoint->id, peer->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_endpoint_proxy_create_link((struct pw_endpoint_proxy*)endpoint->obj->obj.proxy,
|
|
|
|
|
&props->dict);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_properties_free(props);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
|
|
|
|
endpoint->peer = peer;
|
|
|
|
|
peer->peer = endpoint;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int rescan_endpoint(struct impl *impl, struct endpoint *ep)
|
|
|
|
|
{
|
|
|
|
|
struct spa_dict *props;
|
|
|
|
|
const char *str, *media, *category, *role;
|
|
|
|
|
bool exclusive;
|
|
|
|
|
struct find_data find;
|
|
|
|
|
struct pw_endpoint_info *info;
|
|
|
|
|
struct endpoint *peer;
|
|
|
|
|
enum pw_direction direction;
|
|
|
|
|
|
|
|
|
|
if (ep->type == ENDPOINT_TYPE_DEVICE)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
if (ep->obj->info == NULL || ep->obj->info->props == NULL) {
|
|
|
|
|
pw_log_debug(NAME " %p: endpoint %d has no properties", impl, ep->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ep->peer != NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
info = ep->obj->info;
|
2019-11-13 09:38:40 +01:00
|
|
|
props = info->props;
|
|
|
|
|
|
|
|
|
|
str = spa_dict_lookup(props, PW_KEY_ENDPOINT_AUTOCONNECT);
|
|
|
|
|
if (str == NULL || !pw_properties_parse_bool(str)) {
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_debug(NAME" %p: endpoint %d does not need autoconnect", impl, ep->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((media = spa_dict_lookup(props, PW_KEY_MEDIA_TYPE)) == NULL)
|
|
|
|
|
media = ep->media;
|
|
|
|
|
if (media == NULL) {
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_debug(NAME" %p: endpoint %d has unknown media", impl, ep->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_zero(find);
|
|
|
|
|
|
|
|
|
|
if ((category = spa_dict_lookup(props, PW_KEY_MEDIA_CATEGORY)) == NULL) {
|
|
|
|
|
pw_log_debug(NAME" %p: endpoint %d find category",
|
2019-11-14 18:35:29 +01:00
|
|
|
impl, ep->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
if (ep->direction == PW_DIRECTION_INPUT) {
|
|
|
|
|
category = "Capture";
|
|
|
|
|
} else if (ep->direction == PW_DIRECTION_OUTPUT) {
|
|
|
|
|
category = "Playback";
|
|
|
|
|
} else {
|
|
|
|
|
pw_log_warn(NAME" %p: endpoint %d can't determine category",
|
2019-11-14 18:35:29 +01:00
|
|
|
impl, ep->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((role = spa_dict_lookup(props, PW_KEY_MEDIA_ROLE)) == NULL) {
|
|
|
|
|
if (strcmp(media, "Audio") == 0) {
|
|
|
|
|
if (strcmp(category, "Duplex") == 0)
|
|
|
|
|
role = "Communication";
|
|
|
|
|
else if (strcmp(category, "Capture") == 0)
|
|
|
|
|
role = "Production";
|
|
|
|
|
else
|
|
|
|
|
role = "Music";
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(media, "Video") == 0) {
|
|
|
|
|
if (strcmp(category, "Duplex") == 0)
|
|
|
|
|
role = "Communication";
|
|
|
|
|
else if (strcmp(category, "Capture") == 0)
|
|
|
|
|
role = "Camera";
|
|
|
|
|
else
|
|
|
|
|
role = "Video";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((str = spa_dict_lookup(props, PW_KEY_NODE_EXCLUSIVE)) != NULL)
|
|
|
|
|
exclusive = pw_properties_parse_bool(str);
|
|
|
|
|
else
|
|
|
|
|
exclusive = false;
|
|
|
|
|
|
|
|
|
|
if (strcmp(media, "Audio") == 0) {
|
|
|
|
|
if (strcmp(category, "Playback") == 0)
|
|
|
|
|
find.media_class = "Audio/Sink";
|
|
|
|
|
else if (strcmp(category, "Capture") == 0)
|
|
|
|
|
find.media_class = "Audio/Source";
|
|
|
|
|
else {
|
|
|
|
|
pw_log_debug(NAME" %p: endpoint %d unhandled category %s",
|
2019-11-14 18:35:29 +01:00
|
|
|
impl, ep->id, category);
|
2019-11-13 09:38:40 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(media, "Video") == 0) {
|
|
|
|
|
if (strcmp(category, "Capture") == 0)
|
|
|
|
|
find.media_class = "Video/Source";
|
|
|
|
|
else {
|
|
|
|
|
pw_log_debug(NAME" %p: endpoint %d unhandled category %s",
|
2019-11-14 18:35:29 +01:00
|
|
|
impl, ep->id, category);
|
2019-11-13 09:38:40 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pw_log_debug(NAME" %p: endpoint %d unhandled media %s",
|
2019-11-14 18:35:29 +01:00
|
|
|
impl, ep->id, media);
|
2019-11-13 09:38:40 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(category, "Capture") == 0)
|
|
|
|
|
direction = PW_DIRECTION_OUTPUT;
|
|
|
|
|
else if (strcmp(category, "Playback") == 0)
|
|
|
|
|
direction = PW_DIRECTION_INPUT;
|
|
|
|
|
else {
|
|
|
|
|
pw_log_debug(NAME" %p: endpoint %d unhandled category %s",
|
2019-11-14 18:35:29 +01:00
|
|
|
impl, ep->id, category);
|
2019-11-13 09:38:40 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 13:34:16 +01:00
|
|
|
str = spa_dict_lookup(props, PW_KEY_ENDPOINT_TARGET);
|
2019-11-13 09:38:40 +01:00
|
|
|
if (str != NULL)
|
|
|
|
|
find.path_id = atoi(str);
|
|
|
|
|
else
|
|
|
|
|
find.path_id = SPA_ID_INVALID;
|
|
|
|
|
|
|
|
|
|
pw_log_info(NAME " %p: '%s' '%s' '%s' exclusive:%d target %d", impl,
|
|
|
|
|
media, category, role, exclusive, find.path_id);
|
|
|
|
|
|
|
|
|
|
find.impl = impl;
|
|
|
|
|
find.exclusive = exclusive;
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
spa_list_for_each(peer, &impl->endpoint_list, link)
|
2019-11-13 09:38:40 +01:00
|
|
|
find_endpoint(&find, peer);
|
|
|
|
|
|
|
|
|
|
if (find.endpoint == NULL && find.path_id != SPA_ID_INVALID) {
|
2019-11-14 18:35:29 +01:00
|
|
|
struct sm_object *obj;
|
|
|
|
|
pw_log_debug(NAME " %p: no endpoint found for %d, try endpoint", impl, ep->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
if ((obj = sm_media_session_find_object(impl->session, find.path_id)) != NULL) {
|
|
|
|
|
if (obj->type == PW_TYPE_INTERFACE_Endpoint) {
|
2019-11-15 12:08:46 +01:00
|
|
|
peer = sm_object_get_data(obj, SESSION_KEY);
|
2019-11-13 09:38:40 +01:00
|
|
|
goto do_link;
|
2019-11-14 18:35:29 +01:00
|
|
|
}
|
2019-11-13 09:38:40 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
str = spa_dict_lookup(props, PW_KEY_NODE_DONT_RECONNECT);
|
|
|
|
|
if (str != NULL && pw_properties_parse_bool(str)) {
|
2019-11-14 18:35:29 +01:00
|
|
|
// pw_registry_proxy_destroy(impl->registry_proxy, ep->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (find.endpoint == NULL) {
|
2019-11-14 18:35:29 +01:00
|
|
|
struct sm_object *obj;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_warn(NAME " %p: no endpoint found for %d", impl, ep->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
obj = sm_media_session_find_object(impl->session, ep->client_id);
|
|
|
|
|
if (obj && obj->type == PW_TYPE_INTERFACE_Client) {
|
|
|
|
|
pw_client_proxy_error((struct pw_client_proxy*)obj->proxy,
|
|
|
|
|
ep->id, -ENOENT, "no endpoint available");
|
2019-11-13 09:38:40 +01:00
|
|
|
}
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
|
|
|
|
peer = find.endpoint;
|
|
|
|
|
|
|
|
|
|
if (exclusive && peer->busy) {
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_warn(NAME" %p: endpoint %d busy, can't get exclusive access", impl, peer->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
return -EBUSY;
|
|
|
|
|
}
|
|
|
|
|
peer->exclusive = exclusive;
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_debug(NAME" %p: linking to endpoint '%d'", impl, peer->id);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
|
|
|
|
peer->busy = true;
|
|
|
|
|
|
|
|
|
|
do_link:
|
|
|
|
|
link_endpoints(ep, direction, peer, 1);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
static void session_rescan(void *data, int seq)
|
2019-11-13 09:38:40 +01:00
|
|
|
{
|
2019-11-14 18:35:29 +01:00
|
|
|
struct impl *impl = data;
|
2019-11-13 09:38:40 +01:00
|
|
|
struct endpoint *ep;
|
|
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &impl->now);
|
2019-11-14 18:35:29 +01:00
|
|
|
pw_log_debug(NAME" %p: rescan", impl);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
spa_list_for_each(ep, &impl->endpoint_list, link)
|
2019-11-13 09:38:40 +01:00
|
|
|
rescan_endpoint(impl, ep);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
static const struct sm_media_session_events session_events = {
|
|
|
|
|
SM_VERSION_MEDIA_SESSION_EVENTS,
|
|
|
|
|
.update = session_update,
|
|
|
|
|
.remove = session_remove,
|
|
|
|
|
.rescan = session_rescan,
|
2019-11-13 09:38:40 +01:00
|
|
|
};
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
void *sm_policy_ep_start(struct sm_media_session *session)
|
2019-11-13 09:38:40 +01:00
|
|
|
{
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
2019-11-20 16:18:46 +01:00
|
|
|
return NULL;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
impl->session = session;
|
|
|
|
|
impl->core = session->core;
|
2019-11-13 09:38:40 +01:00
|
|
|
|
|
|
|
|
spa_list_init(&impl->endpoint_list);
|
|
|
|
|
|
2019-11-14 18:35:29 +01:00
|
|
|
sm_media_session_add_listener(impl->session, &impl->listener, &session_events, impl);
|
2019-11-13 09:38:40 +01:00
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
return impl;
|
2019-11-13 09:38:40 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-20 16:18:46 +01:00
|
|
|
int sm_policy_ep_stop(void *data)
|
2019-11-13 09:38:40 +01:00
|
|
|
{
|
2019-11-20 16:18:46 +01:00
|
|
|
struct impl *impl = data;
|
|
|
|
|
free(impl);
|
2019-11-13 09:38:40 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|