2 * Copyright (C) 2017 Matthew Waters <matthew@centricular.com>
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.
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.
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.
20 * SECTION:gstwebrtcice
21 * @title: GstWebRTCICE
22 * @short_description: Base class WebRTC ICE handling
32 #include "icestream.h"
34 #include "webrtc-priv.h"
36 #define GST_CAT_DEFAULT gst_webrtc_ice_debug
37 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
42 ADD_LOCAL_IP_ADDRESS_SIGNAL,
53 static guint gst_webrtc_ice_signals[LAST_SIGNAL] = { 0 };
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"););
61 * gst_webrtc_ice_add_stream:
62 * @ice: The #GstWebRTCICE
63 * @session_id: The session id
65 * Returns: (transfer full) (nullable): The #GstWebRTCICEStream, or %NULL
70 gst_webrtc_ice_add_stream (GstWebRTCICE * ice, guint session_id)
72 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
73 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->add_stream);
75 return GST_WEBRTC_ICE_GET_CLASS (ice)->add_stream (ice, session_id);
79 * gst_webrtc_ice_find_transport:
80 * @ice: The #GstWebRTCICE
81 * @stream: The #GstWebRTCICEStream
82 * @component: The #GstWebRTCICEComponent
84 * Returns: (transfer full) (nullable): The #GstWebRTCICETransport, or %NULL
88 GstWebRTCICETransport *
89 gst_webrtc_ice_find_transport (GstWebRTCICE * ice,
90 GstWebRTCICEStream * stream, GstWebRTCICEComponent component)
92 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
93 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->find_transport);
95 return GST_WEBRTC_ICE_GET_CLASS (ice)->find_transport (ice, stream,
100 * gst_webrtc_ice_add_candidate:
101 * @ice: The #GstWebRTCICE
102 * @stream: The #GstWebRTCICEStream
103 * @candidate: The ICE candidate
108 gst_webrtc_ice_add_candidate (GstWebRTCICE * ice,
109 GstWebRTCICEStream * stream, const gchar * candidate)
111 g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
112 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->add_candidate);
114 GST_WEBRTC_ICE_GET_CLASS (ice)->add_candidate (ice, stream, candidate);
118 * gst_webrtc_ice_set_remote_credentials:
119 * @ice: The #GstWebRTCICE
120 * @stream: The #GstWebRTCICEStream
121 * @ufrag: ICE username
124 * Returns: FALSE on error, TRUE otherwise
129 gst_webrtc_ice_set_remote_credentials (GstWebRTCICE * ice,
130 GstWebRTCICEStream * stream, const gchar * ufrag, const gchar * pwd)
132 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
133 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_remote_credentials);
135 return GST_WEBRTC_ICE_GET_CLASS (ice)->set_remote_credentials (ice, stream,
140 * gst_webrtc_ice_add_turn_server:
141 * @ice: The #GstWebRTCICE
142 * @uri: URI of the TURN server
144 * Returns: FALSE on error, TRUE otherwise
149 gst_webrtc_ice_add_turn_server (GstWebRTCICE * ice, const gchar * uri)
151 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
152 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->add_turn_server);
154 return GST_WEBRTC_ICE_GET_CLASS (ice)->add_turn_server (ice, uri);
158 * gst_webrtc_ice_set_local_credentials:
159 * @ice: The #GstWebRTCICE
160 * @stream: The #GstWebRTCICEStream
161 * @ufrag: ICE username
164 * Returns: FALSE on error, TRUE otherwise
169 gst_webrtc_ice_set_local_credentials (GstWebRTCICE * ice,
170 GstWebRTCICEStream * stream, const gchar * ufrag, const gchar * pwd)
172 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
173 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_local_credentials);
175 return GST_WEBRTC_ICE_GET_CLASS (ice)->set_local_credentials (ice, stream,
180 * gst_webrtc_ice_gather_candidates:
181 * @ice: The #GstWebRTCICE
182 * @stream: The #GstWebRTCICEStream
183 * Returns: FALSE on error, TRUE otherwise
188 gst_webrtc_ice_gather_candidates (GstWebRTCICE * ice,
189 GstWebRTCICEStream * stream)
191 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
192 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->gather_candidates);
194 return GST_WEBRTC_ICE_GET_CLASS (ice)->gather_candidates (ice, stream);
198 * gst_webrtc_ice_set_is_controller:
199 * @ice: The #GstWebRTCICE
200 * @controller: TRUE to set as controller
205 gst_webrtc_ice_set_is_controller (GstWebRTCICE * ice, gboolean controller)
207 g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
208 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_is_controller);
210 GST_WEBRTC_ICE_GET_CLASS (ice)->set_is_controller (ice, controller);
214 * gst_webrtc_ice_get_is_controller:
215 * @ice: The #GstWebRTCICE
216 * Returns: TRUE if set as controller, FALSE otherwise
221 gst_webrtc_ice_get_is_controller (GstWebRTCICE * ice)
223 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
224 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_is_controller);
226 return GST_WEBRTC_ICE_GET_CLASS (ice)->get_is_controller (ice);
230 * gst_webrtc_ice_set_force_relay:
231 * @ice: The #GstWebRTCICE
232 * @force_relay: TRUE to enable force relay
237 gst_webrtc_ice_set_force_relay (GstWebRTCICE * ice, gboolean force_relay)
239 g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
240 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_force_relay);
242 GST_WEBRTC_ICE_GET_CLASS (ice)->set_force_relay (ice, force_relay);
246 * gst_webrtc_ice_set_tos:
247 * @ice: The #GstWebRTCICE
248 * @stream: The #GstWebRTCICEStream
249 * @tos: ToS to be set
254 gst_webrtc_ice_set_tos (GstWebRTCICE * ice, GstWebRTCICEStream * stream,
257 g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
258 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_tos);
260 GST_WEBRTC_ICE_GET_CLASS (ice)->set_tos (ice, stream, tos);
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
272 GstWebRTCICECandidateStats **
273 gst_webrtc_ice_get_local_candidates (GstWebRTCICE * ice,
274 GstWebRTCICEStream * stream)
276 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
277 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_local_candidates);
279 return GST_WEBRTC_ICE_GET_CLASS (ice)->get_local_candidates (ice, stream);
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
291 GstWebRTCICECandidateStats **
292 gst_webrtc_ice_get_remote_candidates (GstWebRTCICE * ice,
293 GstWebRTCICEStream * stream)
295 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
296 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_remote_candidates);
298 return GST_WEBRTC_ICE_GET_CLASS (ice)->get_remote_candidates (ice, stream);
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
308 * Returns: FALSE on failure, otherwise @local_stats @remote_stats will be set
313 gst_webrtc_ice_get_selected_pair (GstWebRTCICE * ice,
314 GstWebRTCICEStream * stream, GstWebRTCICECandidateStats ** local_stats,
315 GstWebRTCICECandidateStats ** remote_stats)
317 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), FALSE);
318 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_selected_pair);
320 return GST_WEBRTC_ICE_GET_CLASS (ice)->get_selected_pair (ice, stream,
321 local_stats, remote_stats);
325 * gst_webrtc_ice_candidate_stats_free:
326 * @stats: The #GstWebRTCICECandidateStats to be free'd
328 * Helper function to free #GstWebRTCICECandidateStats
333 gst_webrtc_ice_candidate_stats_free (GstWebRTCICECandidateStats * stats)
336 g_free (stats->ipaddr);
344 * gst_webrtc_ice_candidate_stats_copy:
345 * @stats: The #GstWebRTCICE
347 * Returns: (transfer full): A copy of @stats
351 GstWebRTCICECandidateStats *
352 gst_webrtc_ice_candidate_stats_copy (GstWebRTCICECandidateStats * stats)
354 GstWebRTCICECandidateStats *copy =
355 g_malloc (sizeof (GstWebRTCICECandidateStats));
359 copy->ipaddr = g_strdup (stats->ipaddr);
360 copy->url = g_strdup (stats->url);
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);
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
379 gst_webrtc_ice_set_on_ice_candidate (GstWebRTCICE * ice,
380 GstWebRTCICEOnCandidateFunc func, gpointer user_data, GDestroyNotify notify)
382 g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
383 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_on_ice_candidate);
385 GST_WEBRTC_ICE_GET_CLASS (ice)->set_on_ice_candidate (ice, func, user_data,
390 * gst_webrtc_ice_set_stun_server:
391 * @ice: The #GstWebRTCICE
392 * @uri: (nullable): URI of the STUN server
397 gst_webrtc_ice_set_stun_server (GstWebRTCICE * ice, const gchar * uri_s)
399 g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
400 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_stun_server);
402 GST_WEBRTC_ICE_GET_CLASS (ice)->set_stun_server (ice, uri_s);
406 * gst_webrtc_ice_get_stun_server:
407 * @ice: The #GstWebRTCICE
409 * Returns: (nullable): URI of the STUN sever
414 gst_webrtc_ice_get_stun_server (GstWebRTCICE * ice)
416 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
417 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_stun_server);
419 return GST_WEBRTC_ICE_GET_CLASS (ice)->get_stun_server (ice);
423 * gst_webrtc_ice_set_turn_server:
424 * @ice: The #GstWebRTCICE
425 * @uri: (nullable): URI of the TURN sever
430 gst_webrtc_ice_set_turn_server (GstWebRTCICE * ice, const gchar * uri_s)
432 g_return_if_fail (GST_IS_WEBRTC_ICE (ice));
433 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->set_turn_server);
435 GST_WEBRTC_ICE_GET_CLASS (ice)->set_turn_server (ice, uri_s);
439 * gst_webrtc_ice_get_turn_server:
440 * @ice: The #GstWebRTCICE
442 * Returns: (nullable): URI of the TURN sever
447 gst_webrtc_ice_get_turn_server (GstWebRTCICE * ice)
449 g_return_val_if_fail (GST_IS_WEBRTC_ICE (ice), NULL);
450 g_assert (GST_WEBRTC_ICE_GET_CLASS (ice)->get_turn_server);
452 return GST_WEBRTC_ICE_GET_CLASS (ice)->get_turn_server (ice);
457 gst_webrtc_ice_set_property (GObject * object, guint prop_id,
458 const GValue * value, GParamSpec * pspec)
460 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
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);
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);
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 gst_webrtc_ice_get_property (GObject * object, guint prop_id,
483 GValue * value, GParamSpec * pspec)
485 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
488 case PROP_MIN_RTP_PORT:
489 g_value_set_uint (value, ice->min_rtp_port);
491 case PROP_MAX_RTP_PORT:
492 g_value_set_uint (value, ice->max_rtp_port);
495 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
501 gst_webrtc_ice_class_init (GstWebRTCICEClass * klass)
503 GObjectClass *gobject_class = (GObjectClass *) klass;
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;
525 gobject_class->get_property = gst_webrtc_ice_get_property;
526 gobject_class->set_property = gst_webrtc_ice_set_property;
529 * GstWebRTCICE:min-rtp-port:
531 * Minimum port for local rtp port range.
532 * min-rtp-port must be <= max-rtp-port
536 g_object_class_install_property (gobject_class,
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",
542 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
545 * GstWebRTCICE:max-rtp-port:
547 * Maximum port for local rtp port range.
548 * min-rtp-port must be <= max-rtp-port
552 g_object_class_install_property (gobject_class,
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",
558 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
561 * GstWebRTCICE::add-local-ip-address:
562 * @object: the #GstWebRTCICE
563 * @address: The local IP address
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.
569 * Returns: whether the address could be added.
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,
575 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
579 gst_webrtc_ice_init (GstWebRTCICE * ice)