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