webrtcbin: an element that handles the transport aspects of webrtc connections
[platform/upstream/gst-plugins-bad.git] / ext / webrtc / icestream.c
1 /* GStreamer
2  * Copyright (C) 2017 Matthew Waters <matthew@centricular.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include "icestream.h"
25 #include "nicetransport.h"
26
27 #define GST_CAT_DEFAULT gst_webrtc_ice_stream_debug
28 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
29
30 #define gst_webrtc_ice_stream_parent_class parent_class
31 G_DEFINE_TYPE_WITH_CODE (GstWebRTCICEStream, gst_webrtc_ice_stream,
32     GST_TYPE_OBJECT,
33     GST_DEBUG_CATEGORY_INIT (gst_webrtc_ice_stream_debug,
34         "webrtcicestream", 0, "webrtcicestream"););
35
36 enum
37 {
38   SIGNAL_0,
39   LAST_SIGNAL,
40 };
41
42 enum
43 {
44   PROP_0,
45   PROP_ICE,
46   PROP_STREAM_ID,
47 };
48
49 //static guint gst_webrtc_ice_stream_signals[LAST_SIGNAL] = { 0 };
50
51 struct _GstWebRTCICEStreamPrivate
52 {
53   gboolean gathered;
54   GList *transports;
55 };
56
57 static void
58 gst_webrtc_ice_stream_set_property (GObject * object, guint prop_id,
59     const GValue * value, GParamSpec * pspec)
60 {
61   GstWebRTCICEStream *stream = GST_WEBRTC_ICE_STREAM (object);
62
63   switch (prop_id) {
64     case PROP_ICE:
65       /* XXX: weak-ref this? */
66       stream->ice = g_value_get_object (value);
67       break;
68     case PROP_STREAM_ID:
69       stream->stream_id = g_value_get_uint (value);
70       break;
71     default:
72       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
73       break;
74   }
75 }
76
77 static void
78 gst_webrtc_ice_stream_get_property (GObject * object, guint prop_id,
79     GValue * value, GParamSpec * pspec)
80 {
81   GstWebRTCICEStream *stream = GST_WEBRTC_ICE_STREAM (object);
82
83   switch (prop_id) {
84     case PROP_ICE:
85       g_value_set_object (value, stream->ice);
86       break;
87     case PROP_STREAM_ID:
88       g_value_set_uint (value, stream->stream_id);
89       break;
90     default:
91       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92       break;
93   }
94 }
95
96 static void
97 gst_webrtc_ice_stream_finalize (GObject * object)
98 {
99   GstWebRTCICEStream *stream = GST_WEBRTC_ICE_STREAM (object);
100
101   g_list_free (stream->priv->transports);
102   stream->priv->transports = NULL;
103
104   G_OBJECT_CLASS (parent_class)->finalize (object);
105 }
106
107 static void
108 _on_candidate_gathering_done (NiceAgent * agent, guint stream_id,
109     GstWebRTCICEStream * ice)
110 {
111   GList *l;
112
113   if (stream_id != ice->stream_id)
114     return;
115
116   GST_DEBUG_OBJECT (ice, "%u gathering done", stream_id);
117
118   ice->priv->gathered = TRUE;
119
120   for (l = ice->priv->transports; l; l = l->next) {
121     GstWebRTCICETransport *ice = l->data;
122
123     gst_webrtc_ice_transport_gathering_state_change (ice,
124         GST_WEBRTC_ICE_GATHERING_STATE_COMPLETE);
125   }
126 }
127
128 GstWebRTCICETransport *
129 gst_webrtc_ice_stream_find_transport (GstWebRTCICEStream * stream,
130     GstWebRTCICEComponent component)
131 {
132   GstWebRTCICEComponent trans_comp;
133   GstWebRTCICETransport *ret;
134   GList *l;
135
136   g_return_val_if_fail (GST_IS_WEBRTC_ICE_STREAM (stream), NULL);
137
138   for (l = stream->priv->transports; l; l = l->next) {
139     GstWebRTCICETransport *trans = l->data;
140     g_object_get (trans, "component", &trans_comp, NULL);
141
142     if (component == trans_comp)
143       return gst_object_ref (trans);
144   }
145
146   ret =
147       GST_WEBRTC_ICE_TRANSPORT (gst_webrtc_nice_transport_new (stream,
148           component));
149   stream->priv->transports = g_list_prepend (stream->priv->transports, ret);
150
151   return ret;
152 }
153
154 static void
155 gst_webrtc_ice_stream_constructed (GObject * object)
156 {
157   GstWebRTCICEStream *stream = GST_WEBRTC_ICE_STREAM (object);
158   NiceAgent *agent;
159
160   g_object_get (stream->ice, "agent", &agent, NULL);
161   g_signal_connect (agent, "candidate-gathering-done",
162       G_CALLBACK (_on_candidate_gathering_done), stream);
163
164   g_object_unref (agent);
165
166   G_OBJECT_CLASS (parent_class)->constructed (object);
167 }
168
169 gboolean
170 gst_webrtc_ice_stream_gather_candidates (GstWebRTCICEStream * stream)
171 {
172   NiceAgent *agent;
173   GList *l;
174
175   g_return_val_if_fail (GST_IS_WEBRTC_ICE_STREAM (stream), FALSE);
176
177   GST_DEBUG_OBJECT (stream, "start gathering candidates");
178
179   if (stream->priv->gathered)
180     return TRUE;
181
182   for (l = stream->priv->transports; l; l = l->next) {
183     GstWebRTCICETransport *trans = l->data;
184
185     gst_webrtc_ice_transport_gathering_state_change (trans,
186         GST_WEBRTC_ICE_GATHERING_STATE_GATHERING);
187   }
188
189   g_object_get (stream->ice, "agent", &agent, NULL);
190   if (!nice_agent_gather_candidates (agent, stream->stream_id)) {
191     g_object_unref (agent);
192     return FALSE;
193   }
194
195   g_object_unref (agent);
196   return TRUE;
197 }
198
199 static void
200 gst_webrtc_ice_stream_class_init (GstWebRTCICEStreamClass * klass)
201 {
202   GObjectClass *gobject_class = (GObjectClass *) klass;
203
204   g_type_class_add_private (klass, sizeof (GstWebRTCICEStreamPrivate));
205
206   gobject_class->constructed = gst_webrtc_ice_stream_constructed;
207   gobject_class->get_property = gst_webrtc_ice_stream_get_property;
208   gobject_class->set_property = gst_webrtc_ice_stream_set_property;
209   gobject_class->finalize = gst_webrtc_ice_stream_finalize;
210
211   g_object_class_install_property (gobject_class,
212       PROP_ICE,
213       g_param_spec_object ("ice",
214           "ICE", "ICE agent associated with this stream",
215           GST_TYPE_WEBRTC_ICE,
216           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
217
218   g_object_class_install_property (gobject_class,
219       PROP_STREAM_ID,
220       g_param_spec_uint ("stream-id",
221           "ICE stream id", "ICE stream id associated with this stream",
222           0, G_MAXUINT, 0,
223           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
224 }
225
226 static void
227 gst_webrtc_ice_stream_init (GstWebRTCICEStream * ice)
228 {
229   ice->priv =
230       G_TYPE_INSTANCE_GET_PRIVATE ((ice), GST_TYPE_WEBRTC_ICE_STREAM,
231       GstWebRTCICEStreamPrivate);
232 }
233
234 GstWebRTCICEStream *
235 gst_webrtc_ice_stream_new (GstWebRTCICE * ice, guint stream_id)
236 {
237   return g_object_new (GST_TYPE_WEBRTC_ICE_STREAM, "ice", ice,
238       "stream-id", stream_id, NULL);
239 }