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.
24 #include "gstwebrtcice.h"
27 #include "icestream.h"
28 #include "nicetransport.h"
32 * - are locally generated remote candidates meant to be readded to libnice?
35 static GstUri *_validate_turn_server (GstWebRTCICE * ice, const gchar * s);
37 #define GST_CAT_DEFAULT gst_webrtc_ice_debug
38 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
41 gst_webrtc_ice_error_quark (void)
43 return g_quark_from_static_string ("gst-webrtc-ice-error-quark");
49 ADD_LOCAL_IP_ADDRESS_SIGNAL,
61 static guint gst_webrtc_ice_signals[LAST_SIGNAL] = { 0 };
63 struct _GstWebRTCICEPrivate
65 NiceAgent *nice_agent;
67 GArray *nice_stream_map;
70 GMainContext *main_context;
75 GstWebRTCIceOnCandidateFunc on_candidate;
76 gpointer on_candidate_data;
77 GDestroyNotify on_candidate_notify;
80 #define gst_webrtc_ice_parent_class parent_class
81 G_DEFINE_TYPE_WITH_CODE (GstWebRTCICE, gst_webrtc_ice,
82 GST_TYPE_OBJECT, G_ADD_PRIVATE (GstWebRTCICE)
83 GST_DEBUG_CATEGORY_INIT (gst_webrtc_ice_debug, "webrtcice", 0,
87 _unlock_pc_thread (GMutex * lock)
89 g_mutex_unlock (lock);
90 return G_SOURCE_REMOVE;
94 _gst_nice_thread (GstWebRTCICE * ice)
96 g_mutex_lock (&ice->priv->lock);
97 ice->priv->main_context = g_main_context_new ();
98 ice->priv->loop = g_main_loop_new (ice->priv->main_context, FALSE);
100 g_cond_broadcast (&ice->priv->cond);
101 g_main_context_invoke (ice->priv->main_context,
102 (GSourceFunc) _unlock_pc_thread, &ice->priv->lock);
104 g_main_loop_run (ice->priv->loop);
106 g_mutex_lock (&ice->priv->lock);
107 g_main_context_unref (ice->priv->main_context);
108 ice->priv->main_context = NULL;
109 g_main_loop_unref (ice->priv->loop);
110 ice->priv->loop = NULL;
111 g_cond_broadcast (&ice->priv->cond);
112 g_mutex_unlock (&ice->priv->lock);
118 _start_thread (GstWebRTCICE * ice)
120 g_mutex_lock (&ice->priv->lock);
121 ice->priv->thread = g_thread_new (GST_OBJECT_NAME (ice),
122 (GThreadFunc) _gst_nice_thread, ice);
124 while (!ice->priv->loop)
125 g_cond_wait (&ice->priv->cond, &ice->priv->lock);
126 g_mutex_unlock (&ice->priv->lock);
130 _stop_thread (GstWebRTCICE * ice)
132 g_mutex_lock (&ice->priv->lock);
133 g_main_loop_quit (ice->priv->loop);
134 while (ice->priv->loop)
135 g_cond_wait (&ice->priv->cond, &ice->priv->lock);
136 g_mutex_unlock (&ice->priv->lock);
138 g_thread_unref (ice->priv->thread);
141 struct NiceStreamItem
144 guint nice_stream_id;
145 GstWebRTCICEStream *stream;
148 /* TRUE to continue, FALSE to stop */
149 typedef gboolean (*NiceStreamItemForeachFunc) (struct NiceStreamItem * item,
153 _nice_stream_item_foreach (GstWebRTCICE * ice, NiceStreamItemForeachFunc func,
158 len = ice->priv->nice_stream_map->len;
159 for (i = 0; i < len; i++) {
160 struct NiceStreamItem *item =
161 &g_array_index (ice->priv->nice_stream_map, struct NiceStreamItem,
164 if (!func (item, data))
169 /* TRUE for match, FALSE otherwise */
170 typedef gboolean (*NiceStreamItemFindFunc) (struct NiceStreamItem * item,
175 NiceStreamItemFindFunc func;
177 struct NiceStreamItem *ret;
181 _find_nice_item (struct NiceStreamItem *item, gpointer user_data)
183 struct nice_find *f = user_data;
184 if (f->func (item, f->data)) {
191 static struct NiceStreamItem *
192 _nice_stream_item_find (GstWebRTCICE * ice, NiceStreamItemFindFunc func,
201 _nice_stream_item_foreach (ice, _find_nice_item, &f);
206 #define NICE_MATCH_INIT { -1, -1, NULL }
209 _match (struct NiceStreamItem *item, struct NiceStreamItem *m)
211 if (m->session_id != -1 && m->session_id != item->session_id)
213 if (m->nice_stream_id != -1 && m->nice_stream_id != item->nice_stream_id)
215 if (m->stream != NULL && m->stream != item->stream)
221 static struct NiceStreamItem *
222 _find_item (GstWebRTCICE * ice, guint session_id, guint nice_stream_id,
223 GstWebRTCICEStream * stream)
225 struct NiceStreamItem m = NICE_MATCH_INIT;
227 m.session_id = session_id;
228 m.nice_stream_id = nice_stream_id;
231 return _nice_stream_item_find (ice, (NiceStreamItemFindFunc) _match, &m);
234 static struct NiceStreamItem *
235 _create_nice_stream_item (GstWebRTCICE * ice, guint session_id)
237 struct NiceStreamItem item;
239 item.session_id = session_id;
240 item.nice_stream_id = nice_agent_add_stream (ice->priv->nice_agent, 2);
241 item.stream = gst_webrtc_ice_stream_new (ice, item.nice_stream_id);
242 g_array_append_val (ice->priv->nice_stream_map, item);
244 return _find_item (ice, item.session_id, item.nice_stream_id, item.stream);
248 _parse_userinfo (const gchar * userinfo, gchar ** user, gchar ** pass)
258 colon = g_strstr_len (userinfo, -1, ":");
260 *user = g_uri_unescape_string (userinfo, NULL);
265 /* Check that the first occurence is also the last occurence */
266 if (colon != g_strrstr (userinfo, ":"))
267 GST_WARNING ("userinfo %s contains more than one ':', will assume that the "
268 "first ':' delineates user:pass. You should escape the user and pass "
269 "before adding to the URI.", userinfo);
271 *user = g_uri_unescape_segment (userinfo, colon, NULL);
272 *pass = g_uri_unescape_string (&colon[1], NULL);
276 _resolve_host (GstWebRTCICE * ice, const gchar * host)
278 GResolver *resolver = g_resolver_get_default ();
279 GError *error = NULL;
284 GST_DEBUG_OBJECT (ice, "Resolving host %s", host);
286 if (!(addresses = g_resolver_lookup_by_name (resolver, host, NULL, &error))) {
287 GST_ERROR ("%s", error->message);
288 g_clear_error (&error);
292 GST_DEBUG_OBJECT (ice, "Resolved %d addresses for host %s",
293 g_list_length (addresses), host);
295 /* XXX: only the first address is used */
296 addr = addresses->data;
297 address = g_inet_address_to_string (addr);
298 g_resolver_free_addresses (addresses);
304 _add_turn_server (GstWebRTCICE * ice, struct NiceStreamItem *item,
305 GstUri * turn_server)
309 const gchar *host, *userinfo, *transport, *scheme;
310 NiceRelayType relays[4] = { 0, };
314 host = gst_uri_get_host (turn_server);
316 GST_ERROR_OBJECT (ice, "Turn server has no host");
319 ip = _resolve_host (ice, host);
321 GST_ERROR_OBJECT (ice, "Failed to resolve turn server '%s'", host);
325 /* Set the resolved IP as the host since that's what libnice wants */
326 gst_uri_set_host (turn_server, ip);
328 scheme = gst_uri_get_scheme (turn_server);
329 transport = gst_uri_get_query_value (turn_server, "transport");
330 userinfo = gst_uri_get_userinfo (turn_server);
331 _parse_userinfo (userinfo, &user, &pass);
333 if (g_strcmp0 (scheme, "turns") == 0) {
334 relays[relay_n++] = NICE_RELAY_TYPE_TURN_TLS;
335 } else if (g_strcmp0 (scheme, "turn") == 0) {
336 if (!transport || g_strcmp0 (transport, "udp") == 0)
337 relays[relay_n++] = NICE_RELAY_TYPE_TURN_UDP;
338 if (!transport || g_strcmp0 (transport, "tcp") == 0)
339 relays[relay_n++] = NICE_RELAY_TYPE_TURN_TCP;
341 g_assert (relay_n < G_N_ELEMENTS (relays));
343 for (i = 0; i < relay_n; i++) {
344 ret = nice_agent_set_relay_info (ice->priv->nice_agent,
345 item->nice_stream_id, NICE_COMPONENT_TYPE_RTP,
346 gst_uri_get_host (turn_server), gst_uri_get_port (turn_server),
347 user, pass, relays[i]);
349 gchar *uri = gst_uri_to_string (turn_server);
350 GST_ERROR_OBJECT (ice, "Failed to set TURN server '%s'", uri);
365 struct NiceStreamItem *item;
369 _add_turn_server_func (const gchar * uri, GstUri * turn_server,
370 AddTurnServerData * data)
372 _add_turn_server (data->ice, data->item, turn_server);
376 _add_stun_server (GstWebRTCICE * ice, GstUri * stun_server)
378 const gchar *msg = "must be of the form stun://<host>:<port>";
384 s = gst_uri_to_string (stun_server);
385 GST_DEBUG_OBJECT (ice, "adding stun server, %s", s);
387 host = gst_uri_get_host (stun_server);
389 GST_ERROR_OBJECT (ice, "Stun server '%s' has no host, %s", s, msg);
393 port = gst_uri_get_port (stun_server);
394 if (port == GST_URI_NO_PORT) {
395 GST_INFO_OBJECT (ice, "Stun server '%s' has no port, assuming 3478", s);
397 gst_uri_set_port (stun_server, port);
400 ip = _resolve_host (ice, host);
402 GST_ERROR_OBJECT (ice, "Failed to resolve stun server '%s'", host);
406 g_object_set (ice->priv->nice_agent, "stun-server", ip,
407 "stun-server-port", port, NULL);
415 gst_webrtc_ice_add_stream (GstWebRTCICE * ice, guint session_id)
417 struct NiceStreamItem m = NICE_MATCH_INIT;
418 struct NiceStreamItem *item;
419 AddTurnServerData add_data;
421 m.session_id = session_id;
422 item = _nice_stream_item_find (ice, (NiceStreamItemFindFunc) _match, &m);
424 GST_ERROR_OBJECT (ice, "stream already added with session_id=%u",
429 if (ice->stun_server) {
430 _add_stun_server (ice, ice->stun_server);
433 item = _create_nice_stream_item (ice, session_id);
435 if (ice->turn_server) {
436 _add_turn_server (ice, item, ice->turn_server);
440 add_data.item = item;
442 g_hash_table_foreach (ice->turn_servers, (GHFunc) _add_turn_server_func,
449 _on_new_candidate (NiceAgent * agent, NiceCandidate * candidate,
452 struct NiceStreamItem *item;
455 item = _find_item (ice, -1, candidate->stream_id, NULL);
457 GST_WARNING_OBJECT (ice, "received signal for non-existent stream %u",
458 candidate->stream_id);
462 if (!candidate->username || !candidate->password) {
463 gboolean got_credentials;
464 gchar *ufrag, *password;
466 got_credentials = nice_agent_get_local_credentials (ice->priv->nice_agent,
467 candidate->stream_id, &ufrag, &password);
468 g_warn_if_fail (got_credentials);
470 if (!candidate->username)
471 candidate->username = ufrag;
475 if (!candidate->password)
476 candidate->password = password;
481 attr = nice_agent_generate_local_candidate_sdp (agent, candidate);
483 if (ice->priv->on_candidate)
484 ice->priv->on_candidate (ice, item->session_id, attr,
485 ice->priv->on_candidate_data);
490 GstWebRTCICETransport *
491 gst_webrtc_ice_find_transport (GstWebRTCICE * ice, GstWebRTCICEStream * stream,
492 GstWebRTCICEComponent component)
494 struct NiceStreamItem *item;
496 item = _find_item (ice, -1, -1, stream);
497 g_return_val_if_fail (item != NULL, NULL);
499 return gst_webrtc_ice_stream_find_transport (item->stream, component);
503 /* TODO don't rely on libnice to (de)serialize candidates */
504 static NiceCandidateType
505 _candidate_type_from_string (const gchar * s)
507 if (g_strcmp0 (s, "host") == 0) {
508 return NICE_CANDIDATE_TYPE_HOST;
509 } else if (g_strcmp0 (s, "srflx") == 0) {
510 return NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE;
511 } else if (g_strcmp0 (s, "prflx") == 0) { /* FIXME: is the right string? */
512 return NICE_CANDIDATE_TYPE_PEER_REFLEXIVE;
513 } else if (g_strcmp0 (s, "relay") == 0) {
514 return NICE_CANDIDATE_TYPE_RELAY;
516 g_assert_not_reached ();
522 _candidate_type_to_string (NiceCandidateType type)
525 case NICE_CANDIDATE_TYPE_HOST:
527 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
529 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
531 case NICE_CANDIDATE_TYPE_RELAY:
534 g_assert_not_reached ();
539 static NiceCandidateTransport
540 _candidate_transport_from_string (const gchar * s)
542 if (g_strcmp0 (s, "UDP") == 0) {
543 return NICE_CANDIDATE_TRANSPORT_UDP;
544 } else if (g_strcmp0 (s, "TCP tcptype") == 0) {
545 return NICE_CANDIDATE_TRANSPORT_TCP_ACTIVE;
546 } else if (g_strcmp0 (s, "tcp-passive") == 0) { /* FIXME: is the right string? */
547 return NICE_CANDIDATE_TRANSPORT_TCP_PASSIVE;
548 } else if (g_strcmp0 (s, "tcp-so") == 0) {
549 return NICE_CANDIDATE_TRANSPORT_TCP_SO;
551 g_assert_not_reached ();
557 _candidate_type_to_string (NiceCandidateType type)
560 case NICE_CANDIDATE_TYPE_HOST:
562 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
564 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
566 case NICE_CANDIDATE_TYPE_RELAY:
569 g_assert_not_reached ();
575 /* parse the address for possible resolution */
577 get_candidate_address (const gchar * candidate, gchar ** prefix,
578 gchar ** address, gchar ** postfix)
580 char **tokens = NULL;
582 if (!g_str_has_prefix (candidate, "a=candidate:")) {
583 GST_ERROR ("candidate \"%s\" does not start with \"a=candidate:\"",
588 if (!(tokens = g_strsplit (candidate, " ", 6))) {
589 GST_ERROR ("candidate \"%s\" could not be tokenized", candidate);
593 if (g_strv_length (tokens) < 6) {
594 GST_ERROR ("candidate \"%s\" tokenization resulted in not enough tokens",
600 *address = g_strdup (tokens[4]);
603 *prefix = g_strjoinv (" ", tokens);
605 *postfix = g_strdup (tokens[5]);
616 /* candidate must start with "a=candidate:" or be NULL*/
618 gst_webrtc_ice_add_candidate (GstWebRTCICE * ice, GstWebRTCICEStream * stream,
619 const gchar * candidate)
621 struct NiceStreamItem *item;
623 GSList *candidates = NULL;
625 item = _find_item (ice, -1, -1, stream);
626 g_return_if_fail (item != NULL);
628 if (candidate == NULL) {
629 nice_agent_peer_candidate_gathering_done (ice->priv->nice_agent,
630 item->nice_stream_id);
635 nice_agent_parse_remote_candidate_sdp (ice->priv->nice_agent,
636 item->nice_stream_id, candidate);
638 /* might be a .local candidate */
639 char *prefix = NULL, *address = NULL, *postfix = NULL;
641 char *new_addr, *new_candidate;
643 char *new_addr = NULL, *new_candidate = NULL;
645 char *new_candv[4] = { NULL, };
647 if (!get_candidate_address (candidate, &prefix, &address, &postfix)) {
648 GST_WARNING_OBJECT (ice, "Failed to retrieve address from candidate %s",
653 if (!g_str_has_suffix (address, ".local")) {
654 GST_WARNING_OBJECT (ice, "candidate address \'%s\' does not end "
655 "with \'.local\'", address);
660 if (!(new_addr = _resolve_host (ice, address))) {
661 GST_WARNING_OBJECT (ice, "Failed to resolve %s", address);
665 new_candv[0] = prefix;
666 new_candv[1] = new_addr;
667 new_candv[2] = postfix;
669 new_candidate = g_strjoinv (" ", new_candv);
671 GST_DEBUG_OBJECT (ice, "resolved to candidate %s", new_candidate);
674 nice_agent_parse_remote_candidate_sdp (ice->priv->nice_agent,
675 item->nice_stream_id, new_candidate);
677 g_free (new_candidate);
680 GST_WARNING_OBJECT (ice, "Could not parse candidate \'%s\'",
687 g_free (new_candidate);
701 g_free (new_candidate);
710 candidates = g_slist_append (candidates, cand);
712 nice_agent_set_remote_candidates (ice->priv->nice_agent, item->nice_stream_id,
713 cand->component_id, candidates);
715 g_slist_free (candidates);
716 nice_candidate_free (cand);
720 gst_webrtc_ice_set_remote_credentials (GstWebRTCICE * ice,
721 GstWebRTCICEStream * stream, gchar * ufrag, gchar * pwd)
723 struct NiceStreamItem *item;
725 g_return_val_if_fail (ufrag != NULL, FALSE);
726 g_return_val_if_fail (pwd != NULL, FALSE);
727 item = _find_item (ice, -1, -1, stream);
728 g_return_val_if_fail (item != NULL, FALSE);
730 GST_DEBUG_OBJECT (ice, "Setting remote ICE credentials on "
731 "ICE stream %u ufrag:%s pwd:%s", item->nice_stream_id, ufrag, pwd);
733 nice_agent_set_remote_credentials (ice->priv->nice_agent,
734 item->nice_stream_id, ufrag, pwd);
740 gst_webrtc_ice_add_turn_server (GstWebRTCICE * ice, const gchar * uri)
742 gboolean ret = FALSE;
745 if (!(valid_uri = _validate_turn_server (ice, uri)))
748 g_hash_table_insert (ice->turn_servers, g_strdup (uri), valid_uri);
757 gst_webrtc_ice_add_local_ip_address (GstWebRTCICE * ice, const gchar * address)
759 gboolean ret = FALSE;
760 NiceAddress nice_addr;
762 nice_address_init (&nice_addr);
764 ret = nice_address_set_from_string (&nice_addr, address);
767 ret = nice_agent_add_local_address (ice->priv->nice_agent, &nice_addr);
769 GST_ERROR_OBJECT (ice, "Failed to add local address to NiceAgent");
772 GST_ERROR_OBJECT (ice, "Failed to initialize NiceAddress [%s]", address);
779 gst_webrtc_ice_set_local_credentials (GstWebRTCICE * ice,
780 GstWebRTCICEStream * stream, gchar * ufrag, gchar * pwd)
782 struct NiceStreamItem *item;
784 g_return_val_if_fail (ufrag != NULL, FALSE);
785 g_return_val_if_fail (pwd != NULL, FALSE);
786 item = _find_item (ice, -1, -1, stream);
787 g_return_val_if_fail (item != NULL, FALSE);
789 GST_DEBUG_OBJECT (ice, "Setting local ICE credentials on "
790 "ICE stream %u ufrag:%s pwd:%s", item->nice_stream_id, ufrag, pwd);
792 nice_agent_set_local_credentials (ice->priv->nice_agent, item->nice_stream_id,
799 gst_webrtc_ice_gather_candidates (GstWebRTCICE * ice,
800 GstWebRTCICEStream * stream)
802 struct NiceStreamItem *item;
804 item = _find_item (ice, -1, -1, stream);
805 g_return_val_if_fail (item != NULL, FALSE);
807 GST_DEBUG_OBJECT (ice, "gather candidates for stream %u",
808 item->nice_stream_id);
810 return gst_webrtc_ice_stream_gather_candidates (stream);
814 gst_webrtc_ice_set_is_controller (GstWebRTCICE * ice, gboolean controller)
816 g_object_set (G_OBJECT (ice->priv->nice_agent), "controlling-mode",
821 gst_webrtc_ice_get_is_controller (GstWebRTCICE * ice)
824 g_object_get (G_OBJECT (ice->priv->nice_agent), "controlling-mode",
830 gst_webrtc_ice_set_force_relay (GstWebRTCICE * ice, gboolean force_relay)
832 g_object_set (G_OBJECT (ice->priv->nice_agent), "force-relay", force_relay,
837 gst_webrtc_ice_set_on_ice_candidate (GstWebRTCICE * ice,
838 GstWebRTCIceOnCandidateFunc func, gpointer user_data, GDestroyNotify notify)
840 if (ice->priv->on_candidate_notify)
841 ice->priv->on_candidate_notify (ice->priv->on_candidate_data);
842 ice->priv->on_candidate = NULL;
844 ice->priv->on_candidate = func;
845 ice->priv->on_candidate_data = user_data;
846 ice->priv->on_candidate_notify = notify;
850 gst_webrtc_ice_set_tos (GstWebRTCICE * ice, GstWebRTCICEStream * stream,
853 struct NiceStreamItem *item;
855 item = _find_item (ice, -1, -1, stream);
856 g_return_if_fail (item != NULL);
858 nice_agent_set_stream_tos (ice->priv->nice_agent, item->nice_stream_id, tos);
862 _clear_ice_stream (struct NiceStreamItem *item)
868 g_signal_handlers_disconnect_by_data (item->stream->ice->priv->nice_agent,
870 gst_object_unref (item->stream);
875 _validate_turn_server (GstWebRTCICE * ice, const gchar * s)
877 GstUri *uri = gst_uri_from_string_escaped (s);
878 const gchar *userinfo, *scheme;
879 GList *keys = NULL, *l;
880 gchar *user = NULL, *pass = NULL;
881 gboolean turn_tls = FALSE;
884 GST_DEBUG_OBJECT (ice, "validating turn server, %s", s);
887 GST_ERROR_OBJECT (ice, "Could not parse turn server '%s'", s);
891 scheme = gst_uri_get_scheme (uri);
892 if (g_strcmp0 (scheme, "turn") == 0) {
893 } else if (g_strcmp0 (scheme, "turns") == 0) {
896 GST_ERROR_OBJECT (ice, "unknown scheme '%s'", scheme);
900 keys = gst_uri_get_query_keys (uri);
901 for (l = keys; l; l = l->next) {
902 gchar *key = l->data;
904 if (g_strcmp0 (key, "transport") == 0) {
905 const gchar *transport = gst_uri_get_query_value (uri, "transport");
907 } else if (g_strcmp0 (transport, "udp") == 0) {
908 } else if (g_strcmp0 (transport, "tcp") == 0) {
910 GST_ERROR_OBJECT (ice, "unknown transport value, '%s'", transport);
914 GST_ERROR_OBJECT (ice, "unknown query key, '%s'", key);
919 /* TODO: Implement error checking similar to the stun server below */
920 userinfo = gst_uri_get_userinfo (uri);
921 _parse_userinfo (userinfo, &user, &pass);
923 GST_ERROR_OBJECT (ice, "No username specified in '%s'", s);
927 GST_ERROR_OBJECT (ice, "No password specified in '%s'", s);
931 port = gst_uri_get_port (uri);
933 if (port == GST_URI_NO_PORT) {
935 gst_uri_set_port (uri, 5349);
937 gst_uri_set_port (uri, 3478);
950 gst_webrtc_ice_set_stun_server (GstWebRTCICE * ice, const gchar * uri_s)
952 GstUri *uri = gst_uri_from_string_escaped (uri_s);
953 const gchar *msg = "must be of the form stun://<host>:<port>";
955 GST_DEBUG_OBJECT (ice, "setting stun server, %s", uri_s);
958 GST_ERROR_OBJECT (ice, "Couldn't parse stun server '%s', %s", uri_s, msg);
962 if (ice->stun_server)
963 gst_uri_unref (ice->stun_server);
964 ice->stun_server = uri;
968 gst_webrtc_ice_get_stun_server (GstWebRTCICE * ice)
970 if (ice->stun_server)
971 return gst_uri_to_string (ice->stun_server);
977 gst_webrtc_ice_set_turn_server (GstWebRTCICE * ice, const gchar * uri_s)
979 GstUri *uri = _validate_turn_server (ice, uri_s);
982 if (ice->turn_server)
983 gst_uri_unref (ice->turn_server);
984 ice->turn_server = uri;
989 gst_webrtc_ice_get_turn_server (GstWebRTCICE * ice)
991 if (ice->turn_server)
992 return gst_uri_to_string (ice->turn_server);
998 gst_webrtc_ice_set_property (GObject * object, guint prop_id,
999 const GValue * value, GParamSpec * pspec)
1001 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1005 g_object_set_property (G_OBJECT (ice->priv->nice_agent),
1009 g_object_set_property (G_OBJECT (ice->priv->nice_agent),
1013 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1019 gst_webrtc_ice_get_property (GObject * object, guint prop_id,
1020 GValue * value, GParamSpec * pspec)
1022 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1026 g_value_set_object (value, ice->priv->nice_agent);
1029 g_object_get_property (G_OBJECT (ice->priv->nice_agent),
1033 g_object_get_property (G_OBJECT (ice->priv->nice_agent),
1037 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1043 gst_webrtc_ice_finalize (GObject * object)
1045 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1047 g_signal_handlers_disconnect_by_data (ice->priv->nice_agent, ice);
1051 if (ice->priv->on_candidate_notify)
1052 ice->priv->on_candidate_notify (ice->priv->on_candidate_data);
1053 ice->priv->on_candidate = NULL;
1054 ice->priv->on_candidate_notify = NULL;
1056 if (ice->turn_server)
1057 gst_uri_unref (ice->turn_server);
1058 if (ice->stun_server)
1059 gst_uri_unref (ice->stun_server);
1061 g_mutex_clear (&ice->priv->lock);
1062 g_cond_clear (&ice->priv->cond);
1064 g_array_free (ice->priv->nice_stream_map, TRUE);
1066 g_object_unref (ice->priv->nice_agent);
1068 g_hash_table_unref (ice->turn_servers);
1070 G_OBJECT_CLASS (parent_class)->finalize (object);
1074 gst_webrtc_ice_constructed (GObject * object)
1076 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1077 NiceAgentOption options = 0;
1079 _start_thread (ice);
1081 options |= NICE_AGENT_OPTION_ICE_TRICKLE;
1082 options |= NICE_AGENT_OPTION_REGULAR_NOMINATION;
1084 ice->priv->nice_agent = nice_agent_new_full (ice->priv->main_context,
1085 NICE_COMPATIBILITY_RFC5245, options);
1086 g_signal_connect (ice->priv->nice_agent, "new-candidate-full",
1087 G_CALLBACK (_on_new_candidate), ice);
1089 G_OBJECT_CLASS (parent_class)->constructed (object);
1093 gst_webrtc_ice_class_init (GstWebRTCICEClass * klass)
1095 GObjectClass *gobject_class = (GObjectClass *) klass;
1097 gobject_class->constructed = gst_webrtc_ice_constructed;
1098 gobject_class->get_property = gst_webrtc_ice_get_property;
1099 gobject_class->set_property = gst_webrtc_ice_set_property;
1100 gobject_class->finalize = gst_webrtc_ice_finalize;
1102 g_object_class_install_property (gobject_class,
1104 g_param_spec_object ("agent", "ICE agent",
1105 "ICE agent in use by this object. WARNING! Accessing this property "
1106 "may have disastrous consequences for the operation of webrtcbin. "
1107 "Other ICE implementations may not have the same interface.",
1108 NICE_TYPE_AGENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1110 g_object_class_install_property (gobject_class,
1112 g_param_spec_boolean ("ice-tcp", "ICE TCP",
1113 "Whether the agent should use ICE-TCP when gathering candidates",
1114 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1116 g_object_class_install_property (gobject_class,
1118 g_param_spec_boolean ("ice-udp", "ICE UDP",
1119 "Whether the agent should use ICE-UDP when gathering candidates",
1120 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1123 * GstWebRTCICE::add-local-ip-address:
1124 * @object: the #GstWebRTCICE
1125 * @address: The local IP address
1127 * Add a local IP address to use for ICE candidate gathering. If none
1128 * are supplied, they will be discovered automatically. Calling this signal
1129 * stops automatic ICE gathering.
1131 * Returns: whether the address could be added.
1133 gst_webrtc_ice_signals[ADD_LOCAL_IP_ADDRESS_SIGNAL] =
1134 g_signal_new_class_handler ("add-local-ip-address",
1135 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1136 G_CALLBACK (gst_webrtc_ice_add_local_ip_address), NULL, NULL,
1137 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
1141 gst_webrtc_ice_init (GstWebRTCICE * ice)
1143 ice->priv = gst_webrtc_ice_get_instance_private (ice);
1145 g_mutex_init (&ice->priv->lock);
1146 g_cond_init (&ice->priv->cond);
1149 g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
1150 (GDestroyNotify) gst_uri_unref);
1152 ice->priv->nice_stream_map =
1153 g_array_new (FALSE, TRUE, sizeof (struct NiceStreamItem));
1154 g_array_set_clear_func (ice->priv->nice_stream_map,
1155 (GDestroyNotify) _clear_ice_stream);
1157 nice_debug_enable (FALSE);
1162 gst_webrtc_ice_new (const gchar * name)
1164 return g_object_new (GST_TYPE_WEBRTC_ICE, "name", name, NULL);