webrtc: Add/fix various annotations
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / webrtc / ice.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  * SECTION:gstwebrtcice
21  * @title: GstWebRTCICE
22  * @short_description: Base class WebRTC ICE handling
23  * @symbols:
24  * - GstWebRTCICE
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "ice.h"
32 #include "icestream.h"
33
34 #include "webrtc-priv.h"
35
36 #define GST_CAT_DEFAULT gst_webrtc_ice_debug
37 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
38
39 enum
40 {
41   SIGNAL_0,
42   ADD_LOCAL_IP_ADDRESS_SIGNAL,
43   LAST_SIGNAL,
44 };
45
46 enum
47 {
48   PROP_0,
49   PROP_MIN_RTP_PORT,
50   PROP_MAX_RTP_PORT,
51 };
52
53 static guint gst_webrtc_ice_signals[LAST_SIGNAL] = { 0 };
54
55 #define gst_webrtc_ice_parent_class parent_class
56 G_DEFINE_ABSTRACT_TYPE_WITH_CODE (GstWebRTCICE, gst_webrtc_ice,
57     GST_TYPE_OBJECT, GST_DEBUG_CATEGORY_INIT (gst_webrtc_ice_debug,
58         "webrtcice", 0, "webrtcice"););
59
60 /**
61  * gst_webrtc_ice_add_stream:
62  * @ice: The #GstWebRTCICE
63  * @session_id: The session id
64  *
65  * Returns: (transfer full) (nullable): The #GstWebRTCICEStream, or %NULL
66  *
67  * Since: 1.22
68  */
69 GstWebRTCICEStream *
70 gst_webrtc_ice_add_stream (GstWebRTCICE * ice, guint session_id)
71 {
72   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
73   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->add_stream);
74
75   return GST_WEBRTC_ICE_GET_CLASS (ice)->add_stream (ice, session_id);
76 }
77
78 /**
79  * gst_webrtc_ice_find_transport:
80  * @ice: The #GstWebRTCICE
81  * @stream: The #GstWebRTCICEStream
82  * @component: The #GstWebRTCICEComponent
83  *
84  * Returns: (transfer full) (nullable): The #GstWebRTCICETransport, or %NULL
85  *
86  * Since: 1.22
87  */
88 GstWebRTCICETransport *
89 gst_webrtc_ice_find_transport (GstWebRTCICE * ice,
90     GstWebRTCICEStream * stream, GstWebRTCICEComponent component)
91 {
92   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
93   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->find_transport);
94
95   return GST_WEBRTC_ICE_GET_CLASS (ice)->find_transport (ice, stream,
96       component);
97 }
98
99 /**
100  * gst_webrtc_ice_add_candidate:
101  * @ice: The #GstWebRTCICE
102  * @stream: The #GstWebRTCICEStream
103  * @candidate: The ICE candidate
104  *
105  * Since: 1.22
106  */
107 void
108 gst_webrtc_ice_add_candidate (GstWebRTCICE * ice,
109     GstWebRTCICEStream * stream, const gchar * candidate)
110 {
111   g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
112   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->add_candidate);
113
114   GST_WEBRTC_ICE_GET_CLASS (ice)->add_candidate (ice, stream, candidate);
115 }
116
117 /**
118  * gst_webrtc_ice_set_remote_credentials:
119  * @ice: The #GstWebRTCICE
120  * @stream: The #GstWebRTCICEStream
121  * @ufrag: ICE username
122  * @pwd: ICE password
123  *
124  * Returns: FALSE on error, TRUE otherwise
125  *
126  * Since: 1.22
127  */
128 gboolean
129 gst_webrtc_ice_set_remote_credentials (GstWebRTCICE * ice,
130     GstWebRTCICEStream * stream, const gchar * ufrag, const gchar * pwd)
131 {
132   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
133   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_remote_credentials);
134
135   return GST_WEBRTC_ICE_GET_CLASS (ice)->set_remote_credentials (ice, stream,
136       ufrag, pwd);
137 }
138
139 /**
140  * gst_webrtc_ice_add_turn_server:
141  * @ice: The #GstWebRTCICE
142  * @uri: URI of the TURN server
143  *
144  * Returns: FALSE on error, TRUE otherwise
145  *
146  * Since: 1.22
147  */
148 gboolean
149 gst_webrtc_ice_add_turn_server (GstWebRTCICE * ice, const gchar * uri)
150 {
151   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
152   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->add_turn_server);
153
154   return GST_WEBRTC_ICE_GET_CLASS (ice)->add_turn_server (ice, uri);
155 }
156
157 /**
158  * gst_webrtc_ice_set_local_credentials:
159  * @ice: The #GstWebRTCICE
160  * @stream: The #GstWebRTCICEStream
161  * @ufrag: ICE username
162  * @pwd: ICE password
163  *
164  * Returns: FALSE on error, TRUE otherwise
165  *
166  * Since: 1.22
167  */
168 gboolean
169 gst_webrtc_ice_set_local_credentials (GstWebRTCICE * ice,
170     GstWebRTCICEStream * stream, const gchar * ufrag, const gchar * pwd)
171 {
172   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
173   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_local_credentials);
174
175   return GST_WEBRTC_ICE_GET_CLASS (ice)->set_local_credentials (ice, stream,
176       ufrag, pwd);
177 }
178
179 /**
180  * gst_webrtc_ice_gather_candidates:
181  * @ice: The #GstWebRTCICE
182  * @stream: The #GstWebRTCICEStream
183  * Returns: FALSE on error, TRUE otherwise
184  *
185  * Since: 1.22
186  */
187 gboolean
188 gst_webrtc_ice_gather_candidates (GstWebRTCICE * ice,
189     GstWebRTCICEStream * stream)
190 {
191   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
192   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->gather_candidates);
193
194   return GST_WEBRTC_ICE_GET_CLASS (ice)->gather_candidates (ice, stream);
195 }
196
197 /**
198  * gst_webrtc_ice_set_is_controller:
199  * @ice: The #GstWebRTCICE
200  * @controller: TRUE to set as controller
201  *
202  * Since: 1.22
203  */
204 void
205 gst_webrtc_ice_set_is_controller (GstWebRTCICE * ice, gboolean controller)
206 {
207   g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
208   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_is_controller);
209
210   GST_WEBRTC_ICE_GET_CLASS (ice)->set_is_controller (ice, controller);
211 }
212
213 /**
214  * gst_webrtc_ice_get_is_controller:
215  * @ice: The #GstWebRTCICE
216  * Returns: TRUE if set as controller, FALSE otherwise
217  *
218  * Since: 1.22
219  */
220 gboolean
221 gst_webrtc_ice_get_is_controller (GstWebRTCICE * ice)
222 {
223   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
224   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_is_controller);
225
226   return GST_WEBRTC_ICE_GET_CLASS (ice)->get_is_controller (ice);
227 }
228
229 /**
230  * gst_webrtc_ice_set_force_relay:
231  * @ice: The #GstWebRTCICE
232  * @force_relay: TRUE to enable force relay
233  *
234  * Since: 1.22
235  */
236 void
237 gst_webrtc_ice_set_force_relay (GstWebRTCICE * ice, gboolean force_relay)
238 {
239   g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
240   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_force_relay);
241
242   GST_WEBRTC_ICE_GET_CLASS (ice)->set_force_relay (ice, force_relay);
243 }
244
245 /**
246  * gst_webrtc_ice_set_tos:
247  * @ice: The #GstWebRTCICE
248  * @stream: The #GstWebRTCICEStream
249  * @tos: ToS to be set
250  *
251  * Since: 1.22
252  */
253 void
254 gst_webrtc_ice_set_tos (GstWebRTCICE * ice, GstWebRTCICEStream * stream,
255     guint tos)
256 {
257   g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
258   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_tos);
259
260   GST_WEBRTC_ICE_GET_CLASS (ice)->set_tos (ice, stream, tos);
261 }
262
263
264 /**
265  * gst_webrtc_ice_get_local_candidates:
266  * @ice: The #GstWebRTCICE
267  * @stream: The #GstWebRTCICEStream
268  * Returns: (transfer full)(array zero-terminated=1): List of local candidates
269  *
270  * Since: 1.22
271  */
272 GstWebRTCICECandidateStats **
273 gst_webrtc_ice_get_local_candidates (GstWebRTCICE * ice,
274     GstWebRTCICEStream * stream)
275 {
276   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
277   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_local_candidates);
278
279   return GST_WEBRTC_ICE_GET_CLASS (ice)->get_local_candidates (ice, stream);
280 }
281
282
283 /**
284  * gst_webrtc_ice_get_remote_candidates:
285  * @ice: The #GstWebRTCICE
286  * @stream: The #GstWebRTCICEStream
287  * Returns: (transfer full) (array zero-terminated=1): List of remote candidates
288  *
289  * Since: 1.22
290  */
291 GstWebRTCICECandidateStats **
292 gst_webrtc_ice_get_remote_candidates (GstWebRTCICE * ice,
293     GstWebRTCICEStream * stream)
294 {
295   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
296   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_remote_candidates);
297
298   return GST_WEBRTC_ICE_GET_CLASS (ice)->get_remote_candidates (ice, stream);
299 }
300
301 /**
302  * gst_webrtc_ice_get_selected_pair:
303  * @ice: The #GstWebRTCICE
304  * @stream: The #GstWebRTCICEStream
305  * @local_stats: (out) (transfer full): A pointer to #GstWebRTCICECandidateStats for local candidate
306  * @remote_stats: (out) (transfer full): pointer to #GstWebRTCICECandidateStats for remote candidate
307  *
308  * Returns: FALSE on failure, otherwise @local_stats @remote_stats will be set
309  *
310  * Since: 1.22
311  */
312 gboolean
313 gst_webrtc_ice_get_selected_pair (GstWebRTCICE * ice,
314     GstWebRTCICEStream * stream, GstWebRTCICECandidateStats ** local_stats,
315     GstWebRTCICECandidateStats ** remote_stats)
316 {
317   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
318   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_selected_pair);
319
320   return GST_WEBRTC_ICE_GET_CLASS (ice)->get_selected_pair (ice, stream,
321       local_stats, remote_stats);
322 }
323
324 /**
325  * gst_webrtc_ice_candidate_stats_free:
326  * @stats: The #GstWebRTCICECandidateStats to be free'd
327  *
328  * Helper function to free #GstWebRTCICECandidateStats
329  *
330  * Since: 1.22
331  */
332 void
333 gst_webrtc_ice_candidate_stats_free (GstWebRTCICECandidateStats * stats)
334 {
335   if (stats) {
336     g_free (stats->ipaddr);
337     g_free (stats->url);
338   }
339
340   g_free (stats);
341 }
342
343 /**
344  * gst_webrtc_ice_candidate_stats_copy:
345  * @stats: The #GstWebRTCICE
346  *
347  * Returns: (transfer full): A copy of @stats
348  *
349  * Since: 1.22
350  */
351 GstWebRTCICECandidateStats *
352 gst_webrtc_ice_candidate_stats_copy (GstWebRTCICECandidateStats * stats)
353 {
354   GstWebRTCICECandidateStats *copy =
355       g_malloc (sizeof (GstWebRTCICECandidateStats));
356
357   *copy = *stats;
358
359   copy->ipaddr = g_strdup (stats->ipaddr);
360   copy->url = g_strdup (stats->url);
361
362   return copy;
363 }
364
365 G_DEFINE_BOXED_TYPE (GstWebRTCICECandidateStats, gst_webrtc_ice_candidate_stats,
366     (GBoxedCopyFunc) gst_webrtc_ice_candidate_stats_copy,
367     (GBoxedFreeFunc) gst_webrtc_ice_candidate_stats_free);
368
369 /**
370  * gst_webrtc_ice_set_on_ice_candidate:
371  * @ice: The #GstWebRTCICE
372  * @func: The #GstWebRTCICEOnCandidateFunc callback function
373  * @user_data: User data passed to the callback function
374  * @notify: a #GDestroyNotify when the candidate is no longer needed
375  *
376  * Since: 1.22
377  */
378 void
379 gst_webrtc_ice_set_on_ice_candidate (GstWebRTCICE * ice,
380     GstWebRTCICEOnCandidateFunc func, gpointer user_data, GDestroyNotify notify)
381 {
382   g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
383   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_on_ice_candidate);
384
385   GST_WEBRTC_ICE_GET_CLASS (ice)->set_on_ice_candidate (ice, func, user_data,
386       notify);
387 }
388
389 /**
390  * gst_webrtc_ice_set_stun_server:
391  * @ice: The #GstWebRTCICE
392  * @uri: (nullable): URI of the STUN server
393  *
394  * Since: 1.22
395  */
396 void
397 gst_webrtc_ice_set_stun_server (GstWebRTCICE * ice, const gchar * uri_s)
398 {
399   g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
400   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_stun_server);
401
402   GST_WEBRTC_ICE_GET_CLASS (ice)->set_stun_server (ice, uri_s);
403 }
404
405 /**
406  * gst_webrtc_ice_get_stun_server:
407  * @ice: The #GstWebRTCICE
408  *
409  * Returns: (nullable): URI of the STUN sever
410  *
411  * Since: 1.22
412  */
413 gchar *
414 gst_webrtc_ice_get_stun_server (GstWebRTCICE * ice)
415 {
416   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
417   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_stun_server);
418
419   return GST_WEBRTC_ICE_GET_CLASS (ice)->get_stun_server (ice);
420 }
421
422 /**
423  * gst_webrtc_ice_set_turn_server:
424  * @ice: The #GstWebRTCICE
425  * @uri: (nullable): URI of the TURN sever
426  *
427  * Since: 1.22
428  */
429 void
430 gst_webrtc_ice_set_turn_server (GstWebRTCICE * ice, const gchar * uri_s)
431 {
432   g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
433   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_turn_server);
434
435   GST_WEBRTC_ICE_GET_CLASS (ice)->set_turn_server (ice, uri_s);
436 }
437
438 /**
439  * gst_webrtc_ice_get_turn_server:
440  * @ice: The #GstWebRTCICE
441  *
442  * Returns: (nullable): URI of the TURN sever
443  *
444  * Since: 1.22
445  */
446 gchar *
447 gst_webrtc_ice_get_turn_server (GstWebRTCICE * ice)
448 {
449   g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
450   g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_turn_server);
451
452   return GST_WEBRTC_ICE_GET_CLASS (ice)->get_turn_server (ice);
453 }
454
455
456 static void
457 gst_webrtc_ice_set_property (GObject * object, guint prop_id,
458     const GValue * value, GParamSpec * pspec)
459 {
460   GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
461
462   switch (prop_id) {
463     case PROP_MIN_RTP_PORT:
464       ice->min_rtp_port = g_value_get_uint (value);
465       if (ice->min_rtp_port > ice->max_rtp_port)
466         g_warning ("Set min-rtp-port to %u which is larger than"
467             " max-rtp-port %u", ice->min_rtp_port, ice->max_rtp_port);
468       break;
469     case PROP_MAX_RTP_PORT:
470       ice->max_rtp_port = g_value_get_uint (value);
471       if (ice->min_rtp_port > ice->max_rtp_port)
472         g_warning ("Set max-rtp-port to %u which is smaller than"
473             " min-rtp-port %u", ice->max_rtp_port, ice->min_rtp_port);
474       break;
475     default:
476       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
477       break;
478   }
479 }
480
481 static void
482 gst_webrtc_ice_get_property (GObject * object, guint prop_id,
483     GValue * value, GParamSpec * pspec)
484 {
485   GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
486
487   switch (prop_id) {
488     case PROP_MIN_RTP_PORT:
489       g_value_set_uint (value, ice->min_rtp_port);
490       break;
491     case PROP_MAX_RTP_PORT:
492       g_value_set_uint (value, ice->max_rtp_port);
493       break;
494     default:
495       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
496       break;
497   }
498 }
499
500 static void
501 gst_webrtc_ice_class_init (GstWebRTCICEClass * klass)
502 {
503   GObjectClass *gobject_class = (GObjectClass *) klass;
504
505   klass->add_stream = NULL;
506   klass->find_transport = NULL;
507   klass->gather_candidates = NULL;
508   klass->add_candidate = NULL;
509   klass->set_local_credentials = NULL;
510   klass->set_remote_credentials = NULL;
511   klass->add_turn_server = NULL;
512   klass->set_is_controller = NULL;
513   klass->get_is_controller = NULL;
514   klass->set_force_relay = NULL;
515   klass->set_stun_server = NULL;
516   klass->get_stun_server = NULL;
517   klass->set_turn_server = NULL;
518   klass->get_turn_server = NULL;
519   klass->set_tos = NULL;
520   klass->set_on_ice_candidate = NULL;
521   klass->get_local_candidates = NULL;
522   klass->get_remote_candidates = NULL;
523   klass->get_selected_pair = NULL;
524
525   gobject_class->get_property = gst_webrtc_ice_get_property;
526   gobject_class->set_property = gst_webrtc_ice_set_property;
527
528   /**
529    * GstWebRTCICE:min-rtp-port:
530    *
531    * Minimum port for local rtp port range.
532    * min-rtp-port must be <= max-rtp-port
533    *
534    * Since: 1.20
535    */
536   g_object_class_install_property (gobject_class,
537       PROP_MIN_RTP_PORT,
538       g_param_spec_uint ("min-rtp-port", "ICE RTP candidate min port",
539           "Minimum port for local rtp port range. "
540           "min-rtp-port must be <= max-rtp-port",
541           0, 65535, 0,
542           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
543
544   /**
545    * GstWebRTCICE:max-rtp-port:
546    *
547    * Maximum port for local rtp port range.
548    * min-rtp-port must be <= max-rtp-port
549    *
550    * Since: 1.20
551    */
552   g_object_class_install_property (gobject_class,
553       PROP_MAX_RTP_PORT,
554       g_param_spec_uint ("max-rtp-port", "ICE RTP candidate max port",
555           "Maximum port for local rtp port range. "
556           "max-rtp-port must be >= min-rtp-port",
557           0, 65535, 65535,
558           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
559
560   /**
561    * GstWebRTCICE::add-local-ip-address:
562    * @object: the #GstWebRTCICE
563    * @address: The local IP address
564    *
565    * Add a local IP address to use for ICE candidate gathering.  If none
566    * are supplied, they will be discovered automatically. Calling this signal
567    * stops automatic ICE gathering.
568    *
569    * Returns: whether the address could be added.
570    */
571   gst_webrtc_ice_signals[ADD_LOCAL_IP_ADDRESS_SIGNAL] =
572       g_signal_new_class_handler ("add-local-ip-address",
573       G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
574       NULL, NULL, NULL,
575       g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
576 }
577
578 static void
579 gst_webrtc_ice_init (GstWebRTCICE * ice)
580 {
581 }