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 enum
31 {
32   SIGNAL_0,
33   LAST_SIGNAL,
34 };
35 
36 enum
37 {
38   PROP_0,
39   PROP_ICE,
40   PROP_STREAM_ID,
41 };
42 
43 //static guint gst_webrtc_ice_stream_signals[LAST_SIGNAL] = { 0 };
44 
45 struct _GstWebRTCICEStreamPrivate
46 {
47   gboolean gathered;
48   GList *transports;
49 };
50 
51 #define gst_webrtc_ice_stream_parent_class parent_class
52 G_DEFINE_TYPE_WITH_CODE (GstWebRTCICEStream, gst_webrtc_ice_stream,
53     GST_TYPE_OBJECT, G_ADD_PRIVATE (GstWebRTCICEStream)
54     GST_DEBUG_CATEGORY_INIT (gst_webrtc_ice_stream_debug,
55         "webrtcicestream", 0, "webrtcicestream"););
56 
57 static void
gst_webrtc_ice_stream_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)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
gst_webrtc_ice_stream_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)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
gst_webrtc_ice_stream_finalize(GObject * object)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
_on_candidate_gathering_done(NiceAgent * agent,guint stream_id,GstWebRTCICEStream * ice)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 *
gst_webrtc_ice_stream_find_transport(GstWebRTCICEStream * stream,GstWebRTCICEComponent component)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
gst_webrtc_ice_stream_constructed(GObject * object)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
gst_webrtc_ice_stream_gather_candidates(GstWebRTCICEStream * stream)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
gst_webrtc_ice_stream_class_init(GstWebRTCICEStreamClass * klass)200 gst_webrtc_ice_stream_class_init (GstWebRTCICEStreamClass * klass)
201 {
202   GObjectClass *gobject_class = (GObjectClass *) klass;
203 
204   gobject_class->constructed = gst_webrtc_ice_stream_constructed;
205   gobject_class->get_property = gst_webrtc_ice_stream_get_property;
206   gobject_class->set_property = gst_webrtc_ice_stream_set_property;
207   gobject_class->finalize = gst_webrtc_ice_stream_finalize;
208 
209   g_object_class_install_property (gobject_class,
210       PROP_ICE,
211       g_param_spec_object ("ice",
212           "ICE", "ICE agent associated with this stream",
213           GST_TYPE_WEBRTC_ICE,
214           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
215 
216   g_object_class_install_property (gobject_class,
217       PROP_STREAM_ID,
218       g_param_spec_uint ("stream-id",
219           "ICE stream id", "ICE stream id associated with this stream",
220           0, G_MAXUINT, 0,
221           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
222 }
223 
224 static void
gst_webrtc_ice_stream_init(GstWebRTCICEStream * ice)225 gst_webrtc_ice_stream_init (GstWebRTCICEStream * ice)
226 {
227   ice->priv = gst_webrtc_ice_stream_get_instance_private (ice);
228 }
229 
230 GstWebRTCICEStream *
gst_webrtc_ice_stream_new(GstWebRTCICE * ice,guint stream_id)231 gst_webrtc_ice_stream_new (GstWebRTCICE * ice, guint stream_id)
232 {
233   return g_object_new (GST_TYPE_WEBRTC_ICE_STREAM, "ice", ice,
234       "stream-id", stream_id, NULL);
235 }
236