2017-01-31 09:56:59 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2017 Matthew Waters <matthew@centricular.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
#include "nicestream.h"
|
2017-01-31 09:56:59 +00:00
|
|
|
#include "nicetransport.h"
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_webrtc_nice_stream_debug
|
2017-01-31 09:56:59 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_ICE,
|
|
|
|
};
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
struct _GstWebRTCNiceStreamPrivate
|
2017-01-31 09:56:59 +00:00
|
|
|
{
|
|
|
|
gboolean gathered;
|
|
|
|
GList *transports;
|
2019-08-15 15:25:26 +00:00
|
|
|
gboolean gathering_started;
|
2022-03-29 12:42:04 +00:00
|
|
|
gulong candidate_gathering_done_id;
|
2022-04-18 17:55:13 +00:00
|
|
|
GWeakRef ice_weak;
|
2017-01-31 09:56:59 +00:00
|
|
|
};
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
#define gst_webrtc_nice_stream_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstWebRTCNiceStream, gst_webrtc_nice_stream,
|
|
|
|
GST_TYPE_WEBRTC_ICE_STREAM, G_ADD_PRIVATE (GstWebRTCNiceStream)
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_webrtc_nice_stream_debug,
|
|
|
|
"webrtcnicestream", 0, "webrtcnicestream"););
|
2018-06-23 22:17:26 +00:00
|
|
|
|
2017-01-31 09:56:59 +00:00
|
|
|
static void
|
2022-04-18 17:55:13 +00:00
|
|
|
gst_webrtc_nice_stream_set_property (GObject * object, guint prop_id,
|
2017-01-31 09:56:59 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2022-04-18 17:55:13 +00:00
|
|
|
GstWebRTCNiceStream *stream = GST_WEBRTC_NICE_STREAM (object);
|
2017-01-31 09:56:59 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_ICE:
|
2022-04-18 17:55:13 +00:00
|
|
|
g_weak_ref_set (&stream->priv->ice_weak, g_value_get_object (value));
|
2017-01-31 09:56:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-04-18 17:55:13 +00:00
|
|
|
gst_webrtc_nice_stream_get_property (GObject * object, guint prop_id,
|
2017-01-31 09:56:59 +00:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2022-04-18 17:55:13 +00:00
|
|
|
GstWebRTCNiceStream *stream = GST_WEBRTC_NICE_STREAM (object);
|
2017-01-31 09:56:59 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_ICE:
|
2022-04-18 17:55:13 +00:00
|
|
|
g_value_take_object (value, g_weak_ref_get (&stream->priv->ice_weak));
|
2017-01-31 09:56:59 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 12:28:16 +00:00
|
|
|
static GWeakRef *
|
|
|
|
weak_new (gpointer object)
|
|
|
|
{
|
|
|
|
GWeakRef *weak = g_new0 (GWeakRef, 1);
|
|
|
|
g_weak_ref_init (weak, object);
|
|
|
|
return weak;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
weak_free (GWeakRef * weak)
|
|
|
|
{
|
|
|
|
g_weak_ref_clear (weak);
|
|
|
|
g_free (weak);
|
|
|
|
}
|
|
|
|
|
2017-01-31 09:56:59 +00:00
|
|
|
static void
|
2022-04-18 17:55:13 +00:00
|
|
|
gst_webrtc_nice_stream_finalize (GObject * object)
|
2017-01-31 09:56:59 +00:00
|
|
|
{
|
2022-04-18 17:55:13 +00:00
|
|
|
GstWebRTCNiceStream *stream = GST_WEBRTC_NICE_STREAM (object);
|
|
|
|
GstWebRTCNice *ice = g_weak_ref_get (&stream->priv->ice_weak);
|
2022-03-29 12:42:04 +00:00
|
|
|
|
|
|
|
if (ice) {
|
|
|
|
NiceAgent *agent;
|
|
|
|
g_object_get (ice, "agent", &agent, NULL);
|
|
|
|
|
|
|
|
if (stream->priv->candidate_gathering_done_id != 0) {
|
|
|
|
g_signal_handler_disconnect (agent,
|
|
|
|
stream->priv->candidate_gathering_done_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (agent);
|
|
|
|
gst_object_unref (ice);
|
|
|
|
}
|
2017-01-31 09:56:59 +00:00
|
|
|
|
2022-12-01 12:28:16 +00:00
|
|
|
g_list_foreach (stream->priv->transports, (GFunc) weak_free, NULL);
|
2017-01-31 09:56:59 +00:00
|
|
|
g_list_free (stream->priv->transports);
|
|
|
|
stream->priv->transports = NULL;
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
g_weak_ref_clear (&stream->priv->ice_weak);
|
2022-03-29 12:42:04 +00:00
|
|
|
|
2017-01-31 09:56:59 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2022-12-01 12:28:16 +00:00
|
|
|
static GList *
|
|
|
|
_delete_transport (GList ** transports, GList * link)
|
|
|
|
{
|
|
|
|
GList *next = link->next;
|
|
|
|
weak_free (link->data);
|
|
|
|
*transports = g_list_delete_link (*transports, link);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
2017-01-31 09:56:59 +00:00
|
|
|
static void
|
|
|
|
_on_candidate_gathering_done (NiceAgent * agent, guint stream_id,
|
2022-03-29 12:42:04 +00:00
|
|
|
GWeakRef * ice_weak)
|
2017-01-31 09:56:59 +00:00
|
|
|
{
|
2022-04-18 17:55:13 +00:00
|
|
|
GstWebRTCNiceStream *ice = g_weak_ref_get (ice_weak);
|
2017-01-31 09:56:59 +00:00
|
|
|
GList *l;
|
|
|
|
|
2022-03-29 12:42:04 +00:00
|
|
|
if (!ice)
|
2017-01-31 09:56:59 +00:00
|
|
|
return;
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
if (stream_id != GST_WEBRTC_ICE_STREAM (ice)->stream_id)
|
2022-03-29 12:42:04 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2017-01-31 09:56:59 +00:00
|
|
|
GST_DEBUG_OBJECT (ice, "%u gathering done", stream_id);
|
|
|
|
|
|
|
|
ice->priv->gathered = TRUE;
|
|
|
|
|
2023-03-16 12:33:46 +00:00
|
|
|
for (l = ice->priv->transports; l;) {
|
2022-12-01 12:28:16 +00:00
|
|
|
GstWebRTCICETransport *trans = g_weak_ref_get (l->data);
|
|
|
|
|
|
|
|
if (trans) {
|
|
|
|
gst_webrtc_ice_transport_gathering_state_change (trans,
|
|
|
|
GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE);
|
|
|
|
g_object_unref (trans);
|
2023-03-16 12:33:46 +00:00
|
|
|
l = l->next;
|
2022-12-01 12:28:16 +00:00
|
|
|
} else {
|
|
|
|
l = _delete_transport (&ice->priv->transports, l);
|
|
|
|
}
|
2017-01-31 09:56:59 +00:00
|
|
|
}
|
2022-03-29 12:42:04 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
gst_object_unref (ice);
|
2017-01-31 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
static GstWebRTCICETransport *
|
|
|
|
gst_webrtc_nice_stream_find_transport (GstWebRTCICEStream * stream,
|
2017-01-31 09:56:59 +00:00
|
|
|
GstWebRTCICEComponent component)
|
|
|
|
{
|
|
|
|
GstWebRTCICEComponent trans_comp;
|
|
|
|
GstWebRTCICETransport *ret;
|
|
|
|
GList *l;
|
2022-04-18 17:55:13 +00:00
|
|
|
GstWebRTCNiceStream *nice_stream = GST_WEBRTC_NICE_STREAM (stream);
|
2017-01-31 09:56:59 +00:00
|
|
|
|
2023-03-16 12:33:46 +00:00
|
|
|
for (l = nice_stream->priv->transports; l;) {
|
2022-12-01 12:28:16 +00:00
|
|
|
GstWebRTCICETransport *trans = g_weak_ref_get (l->data);
|
|
|
|
if (trans) {
|
|
|
|
g_object_get (trans, "component", &trans_comp, NULL);
|
|
|
|
|
|
|
|
if (component == trans_comp)
|
|
|
|
return trans;
|
|
|
|
else
|
|
|
|
gst_object_unref (trans);
|
2023-03-16 12:33:46 +00:00
|
|
|
l = l->next;
|
2022-12-01 12:28:16 +00:00
|
|
|
} else {
|
|
|
|
l = _delete_transport (&nice_stream->priv->transports, l);
|
|
|
|
}
|
2017-01-31 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret =
|
2022-04-18 17:55:13 +00:00
|
|
|
GST_WEBRTC_ICE_TRANSPORT (gst_webrtc_nice_transport_new (nice_stream,
|
2017-01-31 09:56:59 +00:00
|
|
|
component));
|
2022-04-18 17:55:13 +00:00
|
|
|
nice_stream->priv->transports =
|
2022-12-01 12:28:16 +00:00
|
|
|
g_list_prepend (nice_stream->priv->transports, weak_new (ret));
|
2017-01-31 09:56:59 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-04-18 17:55:13 +00:00
|
|
|
gst_webrtc_nice_stream_constructed (GObject * object)
|
2017-01-31 09:56:59 +00:00
|
|
|
{
|
2022-07-30 16:28:41 +00:00
|
|
|
GstWebRTCNiceStream *stream;
|
2017-01-31 09:56:59 +00:00
|
|
|
NiceAgent *agent;
|
2022-07-30 16:28:41 +00:00
|
|
|
GstWebRTCNice *ice;
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->constructed (object);
|
|
|
|
|
|
|
|
stream = GST_WEBRTC_NICE_STREAM (object);
|
|
|
|
ice = g_weak_ref_get (&stream->priv->ice_weak);
|
|
|
|
|
2017-01-31 09:56:59 +00:00
|
|
|
|
2022-03-29 12:42:04 +00:00
|
|
|
g_assert (ice != NULL);
|
|
|
|
g_object_get (ice, "agent", &agent, NULL);
|
|
|
|
stream->priv->candidate_gathering_done_id = g_signal_connect_data (agent,
|
|
|
|
"candidate-gathering-done", G_CALLBACK (_on_candidate_gathering_done),
|
|
|
|
weak_new (stream), (GClosureNotify) weak_free, (GConnectFlags) 0);
|
2017-01-31 09:56:59 +00:00
|
|
|
|
|
|
|
g_object_unref (agent);
|
2022-03-29 12:42:04 +00:00
|
|
|
gst_object_unref (ice);
|
2017-01-31 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
static gboolean
|
|
|
|
gst_webrtc_nice_stream_gather_candidates (GstWebRTCICEStream * stream)
|
2017-01-31 09:56:59 +00:00
|
|
|
{
|
|
|
|
NiceAgent *agent;
|
|
|
|
GList *l;
|
2022-03-29 12:42:04 +00:00
|
|
|
GstWebRTCICE *ice;
|
|
|
|
gboolean ret = TRUE;
|
2022-04-18 17:55:13 +00:00
|
|
|
GstWebRTCNiceStream *nice_stream = GST_WEBRTC_NICE_STREAM (stream);
|
2017-01-31 09:56:59 +00:00
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
GST_DEBUG_OBJECT (nice_stream, "start gathering candidates");
|
2017-01-31 09:56:59 +00:00
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
if (nice_stream->priv->gathered)
|
2017-01-31 09:56:59 +00:00
|
|
|
return TRUE;
|
|
|
|
|
2023-03-16 12:33:46 +00:00
|
|
|
for (l = nice_stream->priv->transports; l;) {
|
2022-12-01 12:28:16 +00:00
|
|
|
GstWebRTCICETransport *trans = g_weak_ref_get (l->data);
|
|
|
|
|
|
|
|
if (trans) {
|
|
|
|
gst_webrtc_ice_transport_gathering_state_change (trans,
|
|
|
|
GST_WEBRTC_ICE_GATHERING_STATE_GATHERING);
|
|
|
|
g_object_unref (trans);
|
2023-03-16 12:33:46 +00:00
|
|
|
l = l->next;
|
2022-12-01 12:28:16 +00:00
|
|
|
} else {
|
|
|
|
l = _delete_transport (&nice_stream->priv->transports, l);
|
|
|
|
}
|
2017-01-31 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
ice = GST_WEBRTC_ICE (g_weak_ref_get (&nice_stream->priv->ice_weak));
|
2022-03-29 12:42:04 +00:00
|
|
|
g_assert (ice != NULL);
|
|
|
|
|
|
|
|
g_object_get (ice, "agent", &agent, NULL);
|
2019-08-15 15:25:26 +00:00
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
if (!nice_stream->priv->gathering_started) {
|
2022-03-29 12:42:04 +00:00
|
|
|
if (ice->min_rtp_port != 0 || ice->max_rtp_port != 65535) {
|
|
|
|
if (ice->min_rtp_port > ice->max_rtp_port) {
|
|
|
|
GST_ERROR_OBJECT (ice,
|
2019-08-15 15:25:26 +00:00
|
|
|
"invalid port range: min-rtp-port %d must be <= max-rtp-port %d",
|
2022-03-29 12:42:04 +00:00
|
|
|
ice->min_rtp_port, ice->max_rtp_port);
|
|
|
|
ret = FALSE;
|
|
|
|
goto cleanup;
|
2019-08-15 15:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nice_agent_set_port_range (agent, stream->stream_id,
|
2022-03-29 12:42:04 +00:00
|
|
|
NICE_COMPONENT_TYPE_RTP, ice->min_rtp_port, ice->max_rtp_port);
|
2019-08-15 15:25:26 +00:00
|
|
|
}
|
|
|
|
/* mark as gathering started to prevent changing ports again */
|
2022-04-18 17:55:13 +00:00
|
|
|
nice_stream->priv->gathering_started = TRUE;
|
2019-08-15 15:25:26 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 09:56:59 +00:00
|
|
|
if (!nice_agent_gather_candidates (agent, stream->stream_id)) {
|
2022-03-29 12:42:04 +00:00
|
|
|
ret = FALSE;
|
|
|
|
goto cleanup;
|
2017-01-31 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-16 12:33:46 +00:00
|
|
|
for (l = nice_stream->priv->transports; l;) {
|
2022-12-01 12:28:16 +00:00
|
|
|
GstWebRTCNiceTransport *trans = g_weak_ref_get (l->data);
|
2020-06-23 14:29:42 +00:00
|
|
|
|
2022-12-01 12:28:16 +00:00
|
|
|
if (trans) {
|
|
|
|
gst_webrtc_nice_transport_update_buffer_size (trans);
|
|
|
|
g_object_unref (trans);
|
2023-03-16 12:33:46 +00:00
|
|
|
l = l->next;
|
2022-12-01 12:28:16 +00:00
|
|
|
} else {
|
|
|
|
l = _delete_transport (&nice_stream->priv->transports, l);
|
|
|
|
}
|
2020-06-23 14:29:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 12:42:04 +00:00
|
|
|
cleanup:
|
|
|
|
if (agent)
|
|
|
|
g_object_unref (agent);
|
|
|
|
if (ice)
|
|
|
|
gst_object_unref (ice);
|
|
|
|
|
|
|
|
return ret;
|
2017-01-31 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-04-18 17:55:13 +00:00
|
|
|
gst_webrtc_nice_stream_class_init (GstWebRTCNiceStreamClass * klass)
|
2017-01-31 09:56:59 +00:00
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = (GObjectClass *) klass;
|
2022-04-18 17:55:13 +00:00
|
|
|
GstWebRTCICEStreamClass *gst_webrtc_ice_stream_class =
|
|
|
|
GST_WEBRTC_ICE_STREAM_CLASS (klass);
|
|
|
|
|
|
|
|
gst_webrtc_ice_stream_class->find_transport =
|
|
|
|
gst_webrtc_nice_stream_find_transport;
|
|
|
|
gst_webrtc_ice_stream_class->gather_candidates =
|
|
|
|
gst_webrtc_nice_stream_gather_candidates;
|
2017-01-31 09:56:59 +00:00
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
gobject_class->constructed = gst_webrtc_nice_stream_constructed;
|
|
|
|
gobject_class->get_property = gst_webrtc_nice_stream_get_property;
|
|
|
|
gobject_class->set_property = gst_webrtc_nice_stream_set_property;
|
|
|
|
gobject_class->finalize = gst_webrtc_nice_stream_finalize;
|
2017-01-31 09:56:59 +00:00
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
PROP_ICE,
|
|
|
|
g_param_spec_object ("ice",
|
|
|
|
"ICE", "ICE agent associated with this stream",
|
|
|
|
GST_TYPE_WEBRTC_ICE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-04-18 17:55:13 +00:00
|
|
|
gst_webrtc_nice_stream_init (GstWebRTCNiceStream * stream)
|
2017-01-31 09:56:59 +00:00
|
|
|
{
|
2022-04-18 17:55:13 +00:00
|
|
|
stream->priv = gst_webrtc_nice_stream_get_instance_private (stream);
|
2022-03-29 12:42:04 +00:00
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
g_weak_ref_init (&stream->priv->ice_weak, NULL);
|
2017-01-31 09:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 17:55:13 +00:00
|
|
|
GstWebRTCNiceStream *
|
|
|
|
gst_webrtc_nice_stream_new (GstWebRTCICE * ice, guint stream_id)
|
2017-01-31 09:56:59 +00:00
|
|
|
{
|
2022-04-18 17:55:13 +00:00
|
|
|
return g_object_new (GST_TYPE_WEBRTC_NICE_STREAM, "ice", ice,
|
2017-01-31 09:56:59 +00:00
|
|
|
"stream-id", stream_id, NULL);
|
|
|
|
}
|