webrtcbin: an element that handles the transport aspects of webrtc connections
[platform/upstream/gst-plugins-bad.git] / gst-libs / gst / webrtc / dtlstransport.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 /**
21  * SECTION:gstwebrtc-dtlstransport
22  * @short_description: RTCDtlsTransport object
23  * @title: GstWebRTCDTLSTransport
24  * @see_also: #GstWebRTCRTPSender, #GstWebRTCRTPReceiver, #GstWebRTCICETransport
25  *
26  * <ulink url="https://www.w3.org/TR/webrtc/#rtcdtlstransport">https://www.w3.org/TR/webrtc/#rtcdtlstransport</ulink>
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include "dtlstransport.h"
34
35 #define GST_CAT_DEFAULT gst_webrtc_dtls_transport_debug
36 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
37
38 #define gst_webrtc_dtls_transport_parent_class parent_class
39 G_DEFINE_TYPE_WITH_CODE (GstWebRTCDTLSTransport, gst_webrtc_dtls_transport,
40     GST_TYPE_OBJECT, GST_DEBUG_CATEGORY_INIT (gst_webrtc_dtls_transport_debug,
41         "dtlstransport", 0, "dtlstransport");
42     );
43
44 enum
45 {
46   SIGNAL_0,
47   LAST_SIGNAL,
48 };
49
50 enum
51 {
52   PROP_0,
53   PROP_SESSION_ID,
54   PROP_TRANSPORT,
55   PROP_STATE,
56   PROP_CLIENT,
57   PROP_CERTIFICATE,
58   PROP_REMOTE_CERTIFICATE,
59   PROP_RTCP,
60 };
61
62 void
63 gst_webrtc_dtls_transport_set_transport (GstWebRTCDTLSTransport * transport,
64     GstWebRTCICETransport * ice)
65 {
66   g_return_if_fail (GST_IS_WEBRTC_DTLS_TRANSPORT (transport));
67   g_return_if_fail (GST_IS_WEBRTC_ICE_TRANSPORT (ice));
68
69   gst_object_replace ((GstObject **) & transport->transport, GST_OBJECT (ice));
70 }
71
72 static void
73 gst_webrtc_dtls_transport_set_property (GObject * object, guint prop_id,
74     const GValue * value, GParamSpec * pspec)
75 {
76   GstWebRTCDTLSTransport *webrtc = GST_WEBRTC_DTLS_TRANSPORT (object);
77
78   switch (prop_id) {
79     case PROP_SESSION_ID:
80       webrtc->session_id = g_value_get_uint (value);
81       break;
82     case PROP_CLIENT:
83       g_object_set_property (G_OBJECT (webrtc->dtlssrtpenc), "is-client",
84           value);
85       gst_element_set_locked_state (webrtc->dtlssrtpenc, FALSE);
86       gst_element_sync_state_with_parent (webrtc->dtlssrtpenc);
87       break;
88     case PROP_CERTIFICATE:
89       g_object_set_property (G_OBJECT (webrtc->dtlssrtpdec), "pem", value);
90       break;
91     case PROP_RTCP:
92       webrtc->is_rtcp = g_value_get_boolean (value);
93       break;
94     default:
95       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96       break;
97   }
98 }
99
100 static void
101 gst_webrtc_dtls_transport_get_property (GObject * object, guint prop_id,
102     GValue * value, GParamSpec * pspec)
103 {
104   GstWebRTCDTLSTransport *webrtc = GST_WEBRTC_DTLS_TRANSPORT (object);
105
106   switch (prop_id) {
107     case PROP_SESSION_ID:
108       g_value_set_uint (value, webrtc->session_id);
109       break;
110     case PROP_TRANSPORT:
111       g_value_set_object (value, webrtc->transport);
112       break;
113     case PROP_STATE:
114       g_value_set_enum (value, webrtc->state);
115       break;
116     case PROP_CLIENT:
117       g_object_get_property (G_OBJECT (webrtc->dtlssrtpenc), "is-client",
118           value);
119       break;
120     case PROP_CERTIFICATE:
121       g_object_get_property (G_OBJECT (webrtc->dtlssrtpdec), "pem", value);
122       break;
123     case PROP_REMOTE_CERTIFICATE:
124       g_object_get_property (G_OBJECT (webrtc->dtlssrtpdec), "peer-pem", value);
125       break;
126     case PROP_RTCP:
127       g_value_set_boolean (value, webrtc->is_rtcp);
128       break;
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131       break;
132   }
133 }
134
135 static void
136 gst_webrtc_dtls_transport_finalize (GObject * object)
137 {
138   GstWebRTCDTLSTransport *webrtc = GST_WEBRTC_DTLS_TRANSPORT (object);
139
140   if (webrtc->transport) {
141     gst_object_unref (webrtc->transport);
142   }
143   webrtc->transport = NULL;
144
145   G_OBJECT_CLASS (parent_class)->finalize (object);
146 }
147
148 static void
149 gst_webrtc_dtls_transport_constructed (GObject * object)
150 {
151   GstWebRTCDTLSTransport *webrtc = GST_WEBRTC_DTLS_TRANSPORT (object);
152   gchar *connection_id;
153
154   /* XXX: this may collide with another connection_id however this is only a
155    * problem if multiple dtls element sets are being used within the same
156    * process */
157   connection_id = g_strdup_printf ("%s_%u_%u", webrtc->is_rtcp ? "rtcp" : "rtp",
158       webrtc->session_id, g_random_int ());
159
160   webrtc->dtlssrtpenc = gst_element_factory_make ("dtlssrtpenc", NULL);
161   g_object_set (webrtc->dtlssrtpenc, "connection-id", connection_id,
162       "is-client", webrtc->client, NULL);
163
164   webrtc->dtlssrtpdec = gst_element_factory_make ("dtlssrtpdec", NULL);
165   g_object_set (webrtc->dtlssrtpdec, "connection-id", connection_id, NULL);
166   g_free (connection_id);
167
168   G_OBJECT_CLASS (parent_class)->constructed (object);
169 }
170
171 static void
172 gst_webrtc_dtls_transport_class_init (GstWebRTCDTLSTransportClass * klass)
173 {
174   GObjectClass *gobject_class = (GObjectClass *) klass;
175
176   gobject_class->constructed = gst_webrtc_dtls_transport_constructed;
177   gobject_class->get_property = gst_webrtc_dtls_transport_get_property;
178   gobject_class->set_property = gst_webrtc_dtls_transport_set_property;
179   gobject_class->finalize = gst_webrtc_dtls_transport_finalize;
180
181   g_object_class_install_property (gobject_class,
182       PROP_SESSION_ID,
183       g_param_spec_uint ("session-id", "Session ID",
184           "Unique session ID", 0, G_MAXUINT, 0,
185           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
186
187   g_object_class_install_property (gobject_class,
188       PROP_TRANSPORT,
189       g_param_spec_object ("transport", "ICE transport",
190           "ICE transport used by this dtls transport",
191           GST_TYPE_WEBRTC_ICE_TRANSPORT,
192           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
193
194   /* FIXME: implement */
195   g_object_class_install_property (gobject_class,
196       PROP_STATE,
197       g_param_spec_enum ("state", "DTLS state",
198           "State of the DTLS transport",
199           GST_TYPE_WEBRTC_DTLS_TRANSPORT_STATE,
200           GST_WEBRTC_DTLS_TRANSPORT_STATE_NEW,
201           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
202
203   g_object_class_install_property (gobject_class,
204       PROP_CLIENT,
205       g_param_spec_boolean ("client", "DTLS client",
206           "Are we the client in the DTLS handshake?", FALSE,
207           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208
209   g_object_class_install_property (gobject_class,
210       PROP_CERTIFICATE,
211       g_param_spec_string ("certificate", "DTLS certificate",
212           "DTLS certificate", NULL,
213           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214
215   g_object_class_install_property (gobject_class,
216       PROP_REMOTE_CERTIFICATE,
217       g_param_spec_string ("remote-certificate", "Remote DTLS certificate",
218           "Remote DTLS certificate", NULL,
219           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
220
221   g_object_class_install_property (gobject_class,
222       PROP_RTCP,
223       g_param_spec_boolean ("rtcp", "RTCP",
224           "The transport is being used solely for RTCP", FALSE,
225           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
226 }
227
228 static void
229 gst_webrtc_dtls_transport_init (GstWebRTCDTLSTransport * webrtc)
230 {
231 }
232
233 GstWebRTCDTLSTransport *
234 gst_webrtc_dtls_transport_new (guint session_id, gboolean is_rtcp)
235 {
236   return g_object_new (GST_TYPE_WEBRTC_DTLS_TRANSPORT, "session-id", session_id,
237       "rtcp", is_rtcp, NULL);
238 }