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 "transportstream.h"
25 #include "transportsendbin.h"
26 #include "transportreceivebin.h"
27 #include "gstwebrtcice.h"
28 #include "gstwebrtcbin.h"
29 #include "utils.h"
30
31 #define transport_stream_parent_class parent_class
32 G_DEFINE_TYPE (TransportStream, transport_stream, GST_TYPE_OBJECT);
33
34 enum
35 {
36 PROP_0,
37 PROP_WEBRTC,
38 PROP_SESSION_ID,
39 PROP_RTCP_MUX,
40 PROP_DTLS_CLIENT,
41 };
42
43 GstCaps *
transport_stream_get_caps_for_pt(TransportStream * stream,guint pt)44 transport_stream_get_caps_for_pt (TransportStream * stream, guint pt)
45 {
46 guint i, len;
47
48 len = stream->ptmap->len;
49 for (i = 0; i < len; i++) {
50 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
51 if (item->pt == pt)
52 return item->caps;
53 }
54 return NULL;
55 }
56
57 int
transport_stream_get_pt(TransportStream * stream,const gchar * encoding_name)58 transport_stream_get_pt (TransportStream * stream, const gchar * encoding_name)
59 {
60 guint i;
61 gint ret = 0;
62
63 for (i = 0; i < stream->ptmap->len; i++) {
64 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
65 if (!gst_caps_is_empty (item->caps)) {
66 GstStructure *s = gst_caps_get_structure (item->caps, 0);
67 if (!g_strcmp0 (gst_structure_get_string (s, "encoding-name"),
68 encoding_name)) {
69 ret = item->pt;
70 break;
71 }
72 }
73 }
74
75 return ret;
76 }
77
78 static void
transport_stream_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)79 transport_stream_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec)
81 {
82 TransportStream *stream = TRANSPORT_STREAM (object);
83
84 switch (prop_id) {
85 case PROP_WEBRTC:
86 gst_object_set_parent (GST_OBJECT (stream), g_value_get_object (value));
87 break;
88 }
89
90 GST_OBJECT_LOCK (stream);
91 switch (prop_id) {
92 case PROP_WEBRTC:
93 break;
94 case PROP_SESSION_ID:
95 stream->session_id = g_value_get_uint (value);
96 break;
97 case PROP_RTCP_MUX:
98 stream->rtcp_mux = g_value_get_boolean (value);
99 break;
100 case PROP_DTLS_CLIENT:
101 stream->dtls_client = g_value_get_boolean (value);
102 break;
103 default:
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
105 break;
106 }
107 GST_OBJECT_UNLOCK (stream);
108 }
109
110 static void
transport_stream_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)111 transport_stream_get_property (GObject * object, guint prop_id,
112 GValue * value, GParamSpec * pspec)
113 {
114 TransportStream *stream = TRANSPORT_STREAM (object);
115
116 GST_OBJECT_LOCK (stream);
117 switch (prop_id) {
118 case PROP_SESSION_ID:
119 g_value_set_uint (value, stream->session_id);
120 break;
121 case PROP_RTCP_MUX:
122 g_value_set_boolean (value, stream->rtcp_mux);
123 break;
124 case PROP_DTLS_CLIENT:
125 g_value_set_boolean (value, stream->dtls_client);
126 break;
127 default:
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
129 break;
130 }
131 GST_OBJECT_UNLOCK (stream);
132 }
133
134 static void
transport_stream_dispose(GObject * object)135 transport_stream_dispose (GObject * object)
136 {
137 TransportStream *stream = TRANSPORT_STREAM (object);
138
139 if (stream->send_bin)
140 gst_object_unref (stream->send_bin);
141 stream->send_bin = NULL;
142
143 if (stream->receive_bin)
144 gst_object_unref (stream->receive_bin);
145 stream->receive_bin = NULL;
146
147 if (stream->transport)
148 gst_object_unref (stream->transport);
149 stream->transport = NULL;
150
151 if (stream->rtcp_transport)
152 gst_object_unref (stream->rtcp_transport);
153 stream->rtcp_transport = NULL;
154
155 GST_OBJECT_PARENT (object) = NULL;
156
157 G_OBJECT_CLASS (parent_class)->dispose (object);
158 }
159
160 static void
transport_stream_finalize(GObject * object)161 transport_stream_finalize (GObject * object)
162 {
163 TransportStream *stream = TRANSPORT_STREAM (object);
164
165 g_array_free (stream->ptmap, TRUE);
166 g_array_free (stream->remote_ssrcmap, TRUE);
167
168 G_OBJECT_CLASS (parent_class)->finalize (object);
169 }
170
171 static void
transport_stream_constructed(GObject * object)172 transport_stream_constructed (GObject * object)
173 {
174 TransportStream *stream = TRANSPORT_STREAM (object);
175 GstWebRTCBin *webrtc;
176 GstWebRTCICETransport *ice_trans;
177
178 stream->transport = gst_webrtc_dtls_transport_new (stream->session_id, FALSE);
179 stream->rtcp_transport =
180 gst_webrtc_dtls_transport_new (stream->session_id, TRUE);
181
182 webrtc = GST_WEBRTC_BIN (gst_object_get_parent (GST_OBJECT (object)));
183
184 g_object_bind_property (stream->transport, "client", stream, "dtls-client",
185 G_BINDING_BIDIRECTIONAL);
186 g_object_bind_property (stream->rtcp_transport, "client", stream,
187 "dtls-client", G_BINDING_BIDIRECTIONAL);
188
189 g_object_bind_property (stream->transport, "certificate",
190 stream->rtcp_transport, "certificate", G_BINDING_BIDIRECTIONAL);
191
192 /* Need to go full Java and have a transport manager?
193 * Or make the caller set the ICE transport up? */
194
195 stream->stream = _find_ice_stream_for_session (webrtc, stream->session_id);
196 if (stream->stream == NULL) {
197 stream->stream = gst_webrtc_ice_add_stream (webrtc->priv->ice,
198 stream->session_id);
199 _add_ice_stream_item (webrtc, stream->session_id, stream->stream);
200 }
201 ice_trans =
202 gst_webrtc_ice_find_transport (webrtc->priv->ice, stream->stream,
203 GST_WEBRTC_ICE_COMPONENT_RTP);
204 gst_webrtc_dtls_transport_set_transport (stream->transport, ice_trans);
205 gst_object_unref (ice_trans);
206
207 ice_trans =
208 gst_webrtc_ice_find_transport (webrtc->priv->ice, stream->stream,
209 GST_WEBRTC_ICE_COMPONENT_RTCP);
210 gst_webrtc_dtls_transport_set_transport (stream->rtcp_transport, ice_trans);
211 gst_object_unref (ice_trans);
212
213 stream->send_bin = g_object_new (transport_send_bin_get_type (), "stream",
214 stream, NULL);
215 gst_object_ref_sink (stream->send_bin);
216 stream->receive_bin = g_object_new (transport_receive_bin_get_type (),
217 "stream", stream, NULL);
218 gst_object_ref_sink (stream->receive_bin);
219
220 gst_object_unref (webrtc);
221
222 G_OBJECT_CLASS (parent_class)->constructed (object);
223 }
224
225 static void
transport_stream_class_init(TransportStreamClass * klass)226 transport_stream_class_init (TransportStreamClass * klass)
227 {
228 GObjectClass *gobject_class = (GObjectClass *) klass;
229
230 gobject_class->constructed = transport_stream_constructed;
231 gobject_class->get_property = transport_stream_get_property;
232 gobject_class->set_property = transport_stream_set_property;
233 gobject_class->dispose = transport_stream_dispose;
234 gobject_class->finalize = transport_stream_finalize;
235
236 /* some acrobatics are required to set the parent before _constructed()
237 * has been called */
238 g_object_class_install_property (gobject_class,
239 PROP_WEBRTC,
240 g_param_spec_object ("webrtc", "Parent webrtcbin",
241 "Parent webrtcbin",
242 GST_TYPE_WEBRTC_BIN,
243 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
244
245 g_object_class_install_property (gobject_class,
246 PROP_SESSION_ID,
247 g_param_spec_uint ("session-id", "Session ID",
248 "Session ID used for this transport",
249 0, G_MAXUINT, 0,
250 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
251
252 g_object_class_install_property (gobject_class,
253 PROP_RTCP_MUX,
254 g_param_spec_boolean ("rtcp-mux", "RTCP Mux",
255 "Whether RTCP packets are muxed with RTP packets",
256 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
257
258 g_object_class_install_property (gobject_class,
259 PROP_DTLS_CLIENT,
260 g_param_spec_boolean ("dtls-client", "DTLS client",
261 "Whether we take the client role in DTLS negotiation",
262 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263 }
264
265 static void
clear_ptmap_item(PtMapItem * item)266 clear_ptmap_item (PtMapItem * item)
267 {
268 if (item->caps)
269 gst_caps_unref (item->caps);
270 }
271
272 static void
transport_stream_init(TransportStream * stream)273 transport_stream_init (TransportStream * stream)
274 {
275 stream->ptmap = g_array_new (FALSE, TRUE, sizeof (PtMapItem));
276 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
277 stream->remote_ssrcmap = g_array_new (FALSE, TRUE, sizeof (SsrcMapItem));
278 }
279
280 TransportStream *
transport_stream_new(GstWebRTCBin * webrtc,guint session_id)281 transport_stream_new (GstWebRTCBin * webrtc, guint session_id)
282 {
283 TransportStream *stream;
284
285 stream = g_object_new (transport_stream_get_type (), "webrtc", webrtc,
286 "session-id", session_id, NULL);
287
288 return stream;
289 }
290