diff --git a/modules/module-config-policy/config-policy.c b/modules/module-config-policy/config-policy.c index 869e0727..22479b68 100644 --- a/modules/module-config-policy/config-policy.c +++ b/modules/module-config-policy/config-policy.c @@ -279,8 +279,9 @@ static WpBaseEndpoint * wp_config_policy_find_endpoint (WpPolicy *policy, GVariant *props, guint32 *stream_id) { - g_autoptr (WpCore) core = NULL; - g_autoptr (WpPolicyManager) pmgr = NULL; + g_autoptr (WpCore) core = wp_policy_get_core (policy); + g_autoptr (WpPolicyManager) pmgr = wp_policy_manager_get_instance (core); + g_autoptr (WpSession) session = wp_policy_manager_get_session (pmgr); const struct WpParserEndpointLinkData *data = NULL; g_autoptr (GPtrArray) endpoints = NULL; guint i; @@ -294,22 +295,58 @@ wp_config_policy_find_endpoint (WpPolicy *policy, GVariant *props, if (!data) return NULL; - /* Get all the endpoints matching the media class */ - core = wp_policy_get_core (policy); - pmgr = wp_policy_manager_get_instance (core); - endpoints = wp_policy_manager_list_endpoints (pmgr, - data->te.endpoint_data.media_class); - if (!endpoints) - return NULL; + /* If target-endpoint data was defined in the configuration file, find the + * matching endpoint based on target-endpoint data */ + if (data->has_te) { + /* Get all the endpoints matching the media class */ + endpoints = wp_policy_manager_list_endpoints (pmgr, + data->te.endpoint_data.media_class); + if (!endpoints) + return NULL; - /* Get the first endpoint that matches target data */ - for (i = 0; i < endpoints->len; i++) { - target = g_ptr_array_index (endpoints, i); - if (wp_parser_endpoint_link_matches_endpoint_data (target, - &data->te.endpoint_data)) - break; + /* Get the first endpoint that matches target data */ + for (i = 0; i < endpoints->len; i++) { + target = g_ptr_array_index (endpoints, i); + if (wp_parser_endpoint_link_matches_endpoint_data (target, + &data->te.endpoint_data)) + break; + } } + /* Otherwise, use the default session endpoint if the session is valid */ + else if (session) { + /* Get the default type */ + WpDefaultEndpointType type; + switch (data->me.endpoint_data.direction) { + case PW_DIRECTION_INPUT: + type = WP_DEFAULT_ENDPOINT_TYPE_AUDIO_SOURCE; + break; + case PW_DIRECTION_OUTPUT: + type = WP_DEFAULT_ENDPOINT_TYPE_AUDIO_SINK; + break; + default: + g_warn_if_reached (); + return NULL; + } + + /* Get all the endpoints */ + endpoints = wp_policy_manager_list_endpoints (pmgr, NULL); + if (!endpoints) + return NULL; + + /* Find the default session endpoint */ + for (i = 0; i < endpoints->len; i++) { + target = g_ptr_array_index (endpoints, i); + guint def_id = wp_session_get_default_endpoint (session, type); + if (def_id == wp_base_endpoint_get_global_id (target)) + break; + } + } + + /* If no target data has been defined and session is not valid, return null */ + else + return NULL; + /* If target did not match any data, return NULL */ if (i >= endpoints->len) return NULL; diff --git a/modules/module-config-policy/parser-endpoint-link.c b/modules/module-config-policy/parser-endpoint-link.c index 02785e16..1d726a73 100644 --- a/modules/module-config-policy/parser-endpoint-link.c +++ b/modules/module-config-policy/parser-endpoint-link.c @@ -199,27 +199,29 @@ wp_parser_endpoint_link_data_new (const gchar *location) /* Get the match endpoint properties (Optional) */ res->me.endpoint_data.props = parse_properties (me, "properties"); - /* Get the target-endpoint table */ + /* Get the target-endpoint table (Optional) */ + res->has_te = FALSE; te = wp_toml_table_get_table (table, "target-endpoint"); - if (!te) - goto error; + if (te) { + res->has_te = TRUE; - /* Get the name from the match endpoint table (Optional) */ - res->te.endpoint_data.name = wp_toml_table_get_string (te, "name"); + /* Get the name from the match endpoint table (Optional) */ + res->te.endpoint_data.name = wp_toml_table_get_string (te, "name"); - /* Get the media class from the match endpoint table (Optional) */ - res->te.endpoint_data.media_class = - wp_toml_table_get_string (te, "media_class"); + /* Get the media class from the match endpoint table (Optional) */ + res->te.endpoint_data.media_class = + wp_toml_table_get_string (te, "media_class"); - /* Set the direction to the match endpoint's reverse one */ - res->te.endpoint_data.direction = - pw_direction_reverse (res->me.endpoint_data.direction); + /* Set the direction to the match endpoint's reverse one */ + res->te.endpoint_data.direction = + pw_direction_reverse (res->me.endpoint_data.direction); - /* Get the target endpoint properties (Optional) */ - res->te.endpoint_data.props = parse_properties (te, "properties"); + /* Get the target endpoint properties (Optional) */ + res->te.endpoint_data.props = parse_properties (te, "properties"); - /* Get the target endpoint stream */ - res->te.stream = wp_toml_table_get_string (te, "stream"); + /* Get the target endpoint stream */ + res->te.stream = wp_toml_table_get_string (te, "stream"); + } /* Get the target-endpoint table */ el = wp_toml_table_get_table (table, "endpoint-link"); diff --git a/modules/module-config-policy/parser-endpoint-link.h b/modules/module-config-policy/parser-endpoint-link.h index 1cd2dd6c..6ffed405 100644 --- a/modules/module-config-policy/parser-endpoint-link.h +++ b/modules/module-config-policy/parser-endpoint-link.h @@ -27,6 +27,7 @@ struct WpParserEndpointLinkData { guint priority; struct WpParserEndpointLinkEndpointData endpoint_data; } me; + gboolean has_te; struct TargetEndpoint { struct WpParserEndpointLinkEndpointData endpoint_data; char *stream;