webrtc nice: Let libnice handle STUN/TURN DNS resolution

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/3676>
This commit is contained in:
Olivier Crête 2023-01-01 19:19:03 -05:00 committed by Olivier Crête
parent 069eeeb5c3
commit af315967da

View file

@ -392,48 +392,23 @@ resolve_host_finish (GstWebRTCNice * ice, GAsyncResult * res, GError ** error)
return g_task_propagate_pointer (G_TASK (res), error); return g_task_propagate_pointer (G_TASK (res), error);
} }
struct turn_server_data
{
GstUri *uri;
guint nice_stream_id;
};
static void static void
turn_server_data_free (struct turn_server_data *data) _add_turn_server (GstWebRTCNice * ice, struct NiceStreamItem *item,
GstUri * turn_server)
{ {
gst_uri_unref (data->uri); GstWebRTCNice *nice = GST_WEBRTC_NICE (ice);
const gchar *host;
g_free (data); NiceRelayType relays[4] = { 0, };
}
static void
on_turn_server_resolved (GstWebRTCICE * ice, GAsyncResult * res,
struct turn_server_data *user_data)
{
GList *addresses;
GError *error = NULL;
GstUri *turn_server = user_data->uri;
gboolean ret;
gchar *user, *pass; gchar *user, *pass;
const gchar *userinfo, *transport, *scheme; const gchar *userinfo, *transport, *scheme;
NiceRelayType relays[4] = { 0, };
int i, relay_n = 0; int i, relay_n = 0;
gchar *ip = NULL;
GstWebRTCNice *nice = GST_WEBRTC_NICE (ice);
if (!(addresses = resolve_host_finish (nice, res, &error))) { host = gst_uri_get_host (turn_server);
GST_WARNING_OBJECT (ice, "failed to resolve turn address: %s", if (!host) {
error->message); GST_ERROR_OBJECT (ice, "Turn server has no host");
g_clear_error (&error);
return; return;
} }
/* XXX: only the first IP is used */
ip = g_inet_address_to_string (addresses->data);
/* Set the resolved IP as the host since that's what libnice wants */
gst_uri_set_host (turn_server, ip);
scheme = gst_uri_get_scheme (turn_server); scheme = gst_uri_get_scheme (turn_server);
transport = gst_uri_get_query_value (turn_server, "transport"); transport = gst_uri_get_query_value (turn_server, "transport");
userinfo = gst_uri_get_userinfo (turn_server); userinfo = gst_uri_get_userinfo (turn_server);
@ -450,42 +425,20 @@ on_turn_server_resolved (GstWebRTCICE * ice, GAsyncResult * res,
g_assert (relay_n < G_N_ELEMENTS (relays)); g_assert (relay_n < G_N_ELEMENTS (relays));
for (i = 0; i < relay_n; i++) { for (i = 0; i < relay_n; i++) {
ret = nice_agent_set_relay_info (nice->priv->nice_agent, if (!nice_agent_set_relay_info (nice->priv->nice_agent,
user_data->nice_stream_id, NICE_COMPONENT_TYPE_RTP, item->nice_stream_id, NICE_COMPONENT_TYPE_RTP,
gst_uri_get_host (turn_server), gst_uri_get_port (turn_server), gst_uri_get_host (turn_server), gst_uri_get_port (turn_server),
user, pass, relays[i]); user, pass, relays[i])) {
if (!ret) { gchar *uri_str = gst_uri_to_string (turn_server);
gchar *uri = gst_uri_to_string (turn_server); GST_ERROR_OBJECT (ice, "Could not set TURN server %s on libnice",
GST_ERROR_OBJECT (ice, "Failed to set TURN server '%s'", uri); uri_str);
g_free (uri); g_free (uri_str);
break;
} }
} }
g_free (user); g_free (user);
g_free (pass); g_free (pass);
g_free (ip);
}
static void
_add_turn_server (GstWebRTCNice * ice, struct NiceStreamItem *item,
GstUri * turn_server)
{
struct turn_server_data *data;
const gchar *host;
host = gst_uri_get_host (turn_server);
if (!host) {
GST_ERROR_OBJECT (ice, "Turn server has no host");
return;
}
data = g_new0 (struct turn_server_data, 1);
data->nice_stream_id = item->nice_stream_id;
data->uri = gst_uri_copy (turn_server);
resolve_host_async (ice, host, (GAsyncReadyCallback) on_turn_server_resolved,
data, (GDestroyNotify) turn_server_data_free);
} }
typedef struct typedef struct
@ -501,31 +454,6 @@ _add_turn_server_func (const gchar * uri, GstUri * turn_server,
_add_turn_server (data->ice, data->item, turn_server); _add_turn_server (data->ice, data->item, turn_server);
} }
static void
on_stun_server_resolved (GstWebRTCNice * ice, GAsyncResult * res,
gpointer user_data)
{
GList *addresses;
GError *error = NULL;
guint port = GPOINTER_TO_UINT (user_data);
char *ip;
if (!(addresses = resolve_host_finish (ice, res, &error))) {
GST_WARNING_OBJECT (ice, "Failed to resolve stun server: %s",
error->message);
g_clear_error (&error);
return;
}
/* XXX: only the first IP is used */
ip = g_inet_address_to_string (addresses->data);
g_object_set (ice->priv->nice_agent, "stun-server", ip,
"stun-server-port", port, NULL);
g_free (ip);
}
static void static void
_add_stun_server (GstWebRTCNice * ice, GstUri * stun_server) _add_stun_server (GstWebRTCNice * ice, GstUri * stun_server)
{ {
@ -550,8 +478,8 @@ _add_stun_server (GstWebRTCNice * ice, GstUri * stun_server)
gst_uri_set_port (stun_server, port); gst_uri_set_port (stun_server, port);
} }
resolve_host_async (ice, host, (GAsyncReadyCallback) on_stun_server_resolved, g_object_set (ice->priv->nice_agent, "stun-server", host,
GUINT_TO_POINTER (port), NULL); "stun-server-port", port, NULL);
out: out:
g_free (s); g_free (s);