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,
63 static guint gst_webrtc_ice_signals[LAST_SIGNAL] = { 0 };
65 struct _GstWebRTCICEPrivate
67 NiceAgent *nice_agent;
69 GArray *nice_stream_map;
72 GMainContext *main_context;
77 GstWebRTCIceOnCandidateFunc on_candidate;
78 gpointer on_candidate_data;
79 GDestroyNotify on_candidate_notify;
82 #define gst_webrtc_ice_parent_class parent_class
83 G_DEFINE_TYPE_WITH_CODE (GstWebRTCICE, gst_webrtc_ice,
84 GST_TYPE_OBJECT, G_ADD_PRIVATE (GstWebRTCICE)
85 GST_DEBUG_CATEGORY_INIT (gst_webrtc_ice_debug, "webrtcice", 0,
89 _unlock_pc_thread (GMutex * lock)
91 g_mutex_unlock (lock);
92 return G_SOURCE_REMOVE;
96 _gst_nice_thread (GstWebRTCICE * ice)
98 g_mutex_lock (&ice->priv->lock);
99 ice->priv->main_context = g_main_context_new ();
100 ice->priv->loop = g_main_loop_new (ice->priv->main_context, FALSE);
102 g_cond_broadcast (&ice->priv->cond);
103 g_main_context_invoke (ice->priv->main_context,
104 (GSourceFunc) _unlock_pc_thread, &ice->priv->lock);
106 g_main_loop_run (ice->priv->loop);
108 g_mutex_lock (&ice->priv->lock);
109 g_main_context_unref (ice->priv->main_context);
110 ice->priv->main_context = NULL;
111 g_main_loop_unref (ice->priv->loop);
112 ice->priv->loop = NULL;
113 g_cond_broadcast (&ice->priv->cond);
114 g_mutex_unlock (&ice->priv->lock);
120 _start_thread (GstWebRTCICE * ice)
122 g_mutex_lock (&ice->priv->lock);
123 ice->priv->thread = g_thread_new (GST_OBJECT_NAME (ice),
124 (GThreadFunc) _gst_nice_thread, ice);
126 while (!ice->priv->loop)
127 g_cond_wait (&ice->priv->cond, &ice->priv->lock);
128 g_mutex_unlock (&ice->priv->lock);
132 _stop_thread (GstWebRTCICE * ice)
134 g_mutex_lock (&ice->priv->lock);
135 g_main_loop_quit (ice->priv->loop);
136 while (ice->priv->loop)
137 g_cond_wait (&ice->priv->cond, &ice->priv->lock);
138 g_mutex_unlock (&ice->priv->lock);
140 g_thread_unref (ice->priv->thread);
143 struct NiceStreamItem
146 guint nice_stream_id;
147 GstWebRTCICEStream *stream;
150 /* TRUE to continue, FALSE to stop */
151 typedef gboolean (*NiceStreamItemForeachFunc) (struct NiceStreamItem * item,
155 _nice_stream_item_foreach (GstWebRTCICE * ice, NiceStreamItemForeachFunc func,
160 len = ice->priv->nice_stream_map->len;
161 for (i = 0; i < len; i++) {
162 struct NiceStreamItem *item =
163 &g_array_index (ice->priv->nice_stream_map, struct NiceStreamItem,
166 if (!func (item, data))
171 /* TRUE for match, FALSE otherwise */
172 typedef gboolean (*NiceStreamItemFindFunc) (struct NiceStreamItem * item,
177 NiceStreamItemFindFunc func;
179 struct NiceStreamItem *ret;
183 _find_nice_item (struct NiceStreamItem *item, gpointer user_data)
185 struct nice_find *f = user_data;
186 if (f->func (item, f->data)) {
193 static struct NiceStreamItem *
194 _nice_stream_item_find (GstWebRTCICE * ice, NiceStreamItemFindFunc func,
203 _nice_stream_item_foreach (ice, _find_nice_item, &f);
208 #define NICE_MATCH_INIT { -1, -1, NULL }
211 _match (struct NiceStreamItem *item, struct NiceStreamItem *m)
213 if (m->session_id != -1 && m->session_id != item->session_id)
215 if (m->nice_stream_id != -1 && m->nice_stream_id != item->nice_stream_id)
217 if (m->stream != NULL && m->stream != item->stream)
223 static struct NiceStreamItem *
224 _find_item (GstWebRTCICE * ice, guint session_id, guint nice_stream_id,
225 GstWebRTCICEStream * stream)
227 struct NiceStreamItem m = NICE_MATCH_INIT;
229 m.session_id = session_id;
230 m.nice_stream_id = nice_stream_id;
233 return _nice_stream_item_find (ice, (NiceStreamItemFindFunc) _match, &m);
236 static struct NiceStreamItem *
237 _create_nice_stream_item (GstWebRTCICE * ice, guint session_id)
239 struct NiceStreamItem item;
241 item.session_id = session_id;
242 item.nice_stream_id = nice_agent_add_stream (ice->priv->nice_agent, 1);
243 item.stream = gst_webrtc_ice_stream_new (ice, item.nice_stream_id);
244 g_array_append_val (ice->priv->nice_stream_map, item);
246 return _find_item (ice, item.session_id, item.nice_stream_id, item.stream);
250 _parse_userinfo (const gchar * userinfo, gchar ** user, gchar ** pass)
260 colon = g_strstr_len (userinfo, -1, ":");
262 *user = g_uri_unescape_string (userinfo, NULL);
267 /* Check that the first occurence is also the last occurence */
268 if (colon != g_strrstr (userinfo, ":"))
269 GST_WARNING ("userinfo %s contains more than one ':', will assume that the "
270 "first ':' delineates user:pass. You should escape the user and pass "
271 "before adding to the URI.", userinfo);
273 *user = g_uri_unescape_segment (userinfo, colon, NULL);
274 *pass = g_uri_unescape_string (&colon[1], NULL);
278 _resolve_host (GstWebRTCICE * ice, const gchar * host)
280 GResolver *resolver = g_resolver_get_default ();
281 GError *error = NULL;
286 GST_DEBUG_OBJECT (ice, "Resolving host %s", host);
288 if (!(addresses = g_resolver_lookup_by_name (resolver, host, NULL, &error))) {
289 GST_ERROR ("%s", error->message);
290 g_clear_error (&error);
294 GST_DEBUG_OBJECT (ice, "Resolved %d addresses for host %s",
295 g_list_length (addresses), host);
297 /* XXX: only the first address is used */
298 addr = addresses->data;
299 address = g_inet_address_to_string (addr);
300 g_resolver_free_addresses (addresses);
306 _add_turn_server (GstWebRTCICE * ice, struct NiceStreamItem *item,
307 GstUri * turn_server)
311 const gchar *host, *userinfo, *transport, *scheme;
312 NiceRelayType relays[4] = { 0, };
316 host = gst_uri_get_host (turn_server);
318 GST_ERROR_OBJECT (ice, "Turn server has no host");
321 ip = _resolve_host (ice, host);
323 GST_ERROR_OBJECT (ice, "Failed to resolve turn server '%s'", host);
327 /* Set the resolved IP as the host since that's what libnice wants */
328 gst_uri_set_host (turn_server, ip);
330 scheme = gst_uri_get_scheme (turn_server);
331 transport = gst_uri_get_query_value (turn_server, "transport");
332 userinfo = gst_uri_get_userinfo (turn_server);
333 _parse_userinfo (userinfo, &user, &pass);
335 if (g_strcmp0 (scheme, "turns") == 0) {
336 relays[relay_n++] = NICE_RELAY_TYPE_TURN_TLS;
337 } else if (g_strcmp0 (scheme, "turn") == 0) {
338 if (!transport || g_strcmp0 (transport, "udp") == 0)
339 relays[relay_n++] = NICE_RELAY_TYPE_TURN_UDP;
340 if (!transport || g_strcmp0 (transport, "tcp") == 0)
341 relays[relay_n++] = NICE_RELAY_TYPE_TURN_TCP;
343 g_assert (relay_n < G_N_ELEMENTS (relays));
345 for (i = 0; i < relay_n; i++) {
346 ret = nice_agent_set_relay_info (ice->priv->nice_agent,
347 item->nice_stream_id, NICE_COMPONENT_TYPE_RTP,
348 gst_uri_get_host (turn_server), gst_uri_get_port (turn_server),
349 user, pass, relays[i]);
351 gchar *uri = gst_uri_to_string (turn_server);
352 GST_ERROR_OBJECT (ice, "Failed to set TURN server '%s'", uri);
367 struct NiceStreamItem *item;
371 _add_turn_server_func (const gchar * uri, GstUri * turn_server,
372 AddTurnServerData * data)
374 _add_turn_server (data->ice, data->item, turn_server);
378 _add_stun_server (GstWebRTCICE * ice, GstUri * stun_server)
380 const gchar *msg = "must be of the form stun://<host>:<port>";
386 s = gst_uri_to_string (stun_server);
387 GST_DEBUG_OBJECT (ice, "adding stun server, %s", s);
389 host = gst_uri_get_host (stun_server);
391 GST_ERROR_OBJECT (ice, "Stun server '%s' has no host, %s", s, msg);
395 port = gst_uri_get_port (stun_server);
396 if (port == GST_URI_NO_PORT) {
397 GST_INFO_OBJECT (ice, "Stun server '%s' has no port, assuming 3478", s);
399 gst_uri_set_port (stun_server, port);
402 ip = _resolve_host (ice, host);
404 GST_ERROR_OBJECT (ice, "Failed to resolve stun server '%s'", host);
408 g_object_set (ice->priv->nice_agent, "stun-server", ip,
409 "stun-server-port", port, NULL);
417 gst_webrtc_ice_add_stream (GstWebRTCICE * ice, guint session_id)
419 struct NiceStreamItem m = NICE_MATCH_INIT;
420 struct NiceStreamItem *item;
421 AddTurnServerData add_data;
423 m.session_id = session_id;
424 item = _nice_stream_item_find (ice, (NiceStreamItemFindFunc) _match, &m);
426 GST_ERROR_OBJECT (ice, "stream already added with session_id=%u",
431 if (ice->stun_server) {
432 _add_stun_server (ice, ice->stun_server);
435 item = _create_nice_stream_item (ice, session_id);
437 if (ice->turn_server) {
438 _add_turn_server (ice, item, ice->turn_server);
442 add_data.item = item;
444 g_hash_table_foreach (ice->turn_servers, (GHFunc) _add_turn_server_func,
451 _on_new_candidate (NiceAgent * agent, NiceCandidate * candidate,
454 struct NiceStreamItem *item;
457 item = _find_item (ice, -1, candidate->stream_id, NULL);
459 GST_WARNING_OBJECT (ice, "received signal for non-existent stream %u",
460 candidate->stream_id);
464 if (!candidate->username || !candidate->password) {
465 gboolean got_credentials;
466 gchar *ufrag, *password;
468 got_credentials = nice_agent_get_local_credentials (ice->priv->nice_agent,
469 candidate->stream_id, &ufrag, &password);
470 g_warn_if_fail (got_credentials);
472 if (!candidate->username)
473 candidate->username = ufrag;
477 if (!candidate->password)
478 candidate->password = password;
483 attr = nice_agent_generate_local_candidate_sdp (agent, candidate);
485 if (ice->priv->on_candidate)
486 ice->priv->on_candidate (ice, item->session_id, attr,
487 ice->priv->on_candidate_data);
492 GstWebRTCICETransport *
493 gst_webrtc_ice_find_transport (GstWebRTCICE * ice, GstWebRTCICEStream * stream,
494 GstWebRTCICEComponent component)
496 struct NiceStreamItem *item;
498 item = _find_item (ice, -1, -1, stream);
499 g_return_val_if_fail (item != NULL, NULL);
501 return gst_webrtc_ice_stream_find_transport (item->stream, component);
505 /* TODO don't rely on libnice to (de)serialize candidates */
506 static NiceCandidateType
507 _candidate_type_from_string (const gchar * s)
509 if (g_strcmp0 (s, "host") == 0) {
510 return NICE_CANDIDATE_TYPE_HOST;
511 } else if (g_strcmp0 (s, "srflx") == 0) {
512 return NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE;
513 } else if (g_strcmp0 (s, "prflx") == 0) { /* FIXME: is the right string? */
514 return NICE_CANDIDATE_TYPE_PEER_REFLEXIVE;
515 } else if (g_strcmp0 (s, "relay") == 0) {
516 return NICE_CANDIDATE_TYPE_RELAY;
518 g_assert_not_reached ();
524 _candidate_type_to_string (NiceCandidateType type)
527 case NICE_CANDIDATE_TYPE_HOST:
529 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
531 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
533 case NICE_CANDIDATE_TYPE_RELAY:
536 g_assert_not_reached ();
541 static NiceCandidateTransport
542 _candidate_transport_from_string (const gchar * s)
544 if (g_strcmp0 (s, "UDP") == 0) {
545 return NICE_CANDIDATE_TRANSPORT_UDP;
546 } else if (g_strcmp0 (s, "TCP tcptype") == 0) {
547 return NICE_CANDIDATE_TRANSPORT_TCP_ACTIVE;
548 } else if (g_strcmp0 (s, "tcp-passive") == 0) { /* FIXME: is the right string? */
549 return NICE_CANDIDATE_TRANSPORT_TCP_PASSIVE;
550 } else if (g_strcmp0 (s, "tcp-so") == 0) {
551 return NICE_CANDIDATE_TRANSPORT_TCP_SO;
553 g_assert_not_reached ();
559 _candidate_type_to_string (NiceCandidateType type)
562 case NICE_CANDIDATE_TYPE_HOST:
564 case NICE_CANDIDATE_TYPE_SERVER_REFLEXIVE:
566 case NICE_CANDIDATE_TYPE_PEER_REFLEXIVE:
568 case NICE_CANDIDATE_TYPE_RELAY:
571 g_assert_not_reached ();
577 /* parse the address for possible resolution */
579 get_candidate_address (const gchar * candidate, gchar ** prefix,
580 gchar ** address, gchar ** postfix)
582 char **tokens = NULL;
584 if (!g_str_has_prefix (candidate, "a=candidate:")) {
585 GST_ERROR ("candidate \"%s\" does not start with \"a=candidate:\"",
590 if (!(tokens = g_strsplit (candidate, " ", 6))) {
591 GST_ERROR ("candidate \"%s\" could not be tokenized", candidate);
595 if (g_strv_length (tokens) < 6) {
596 GST_ERROR ("candidate \"%s\" tokenization resulted in not enough tokens",
602 *address = g_strdup (tokens[4]);
605 *prefix = g_strjoinv (" ", tokens);
607 *postfix = g_strdup (tokens[5]);
618 /* candidate must start with "a=candidate:" or be NULL*/
620 gst_webrtc_ice_add_candidate (GstWebRTCICE * ice, GstWebRTCICEStream * stream,
621 const gchar * candidate)
623 struct NiceStreamItem *item;
625 GSList *candidates = NULL;
627 item = _find_item (ice, -1, -1, stream);
628 g_return_if_fail (item != NULL);
630 if (candidate == NULL) {
631 nice_agent_peer_candidate_gathering_done (ice->priv->nice_agent,
632 item->nice_stream_id);
637 nice_agent_parse_remote_candidate_sdp (ice->priv->nice_agent,
638 item->nice_stream_id, candidate);
640 /* might be a .local candidate */
641 char *prefix = NULL, *address = NULL, *postfix = NULL;
642 char *new_addr, *new_candidate;
643 char *new_candv[4] = { NULL, };
645 if (!get_candidate_address (candidate, &prefix, &address, &postfix)) {
646 GST_WARNING_OBJECT (ice, "Failed to retrieve address from candidate %s",
651 if (!g_str_has_suffix (address, ".local")) {
652 GST_WARNING_OBJECT (ice, "candidate address \'%s\' does not end "
653 "with \'.local\'", address);
658 if (!(new_addr = _resolve_host (ice, address))) {
659 GST_WARNING_OBJECT (ice, "Failed to resolve %s", address);
663 new_candv[0] = prefix;
664 new_candv[1] = new_addr;
665 new_candv[2] = postfix;
667 new_candidate = g_strjoinv (" ", new_candv);
669 GST_DEBUG_OBJECT (ice, "resolved to candidate %s", new_candidate);
672 nice_agent_parse_remote_candidate_sdp (ice->priv->nice_agent,
673 item->nice_stream_id, new_candidate);
674 g_free (new_candidate);
676 GST_WARNING_OBJECT (ice, "Could not parse candidate \'%s\'",
694 if (cand->component_id == 2) {
695 /* we only support rtcp-mux so rtcp candidates are useless for us */
696 GST_INFO_OBJECT (ice, "Dropping RTCP candidate %s", candidate);
697 nice_candidate_free (cand);
701 candidates = g_slist_append (candidates, cand);
703 nice_agent_set_remote_candidates (ice->priv->nice_agent, item->nice_stream_id,
704 cand->component_id, candidates);
706 g_slist_free (candidates);
707 nice_candidate_free (cand);
711 gst_webrtc_ice_set_remote_credentials (GstWebRTCICE * ice,
712 GstWebRTCICEStream * stream, gchar * ufrag, gchar * pwd)
714 struct NiceStreamItem *item;
716 g_return_val_if_fail (ufrag != NULL, FALSE);
717 g_return_val_if_fail (pwd != NULL, FALSE);
718 item = _find_item (ice, -1, -1, stream);
719 g_return_val_if_fail (item != NULL, FALSE);
721 GST_DEBUG_OBJECT (ice, "Setting remote ICE credentials on "
722 "ICE stream %u ufrag:%s pwd:%s", item->nice_stream_id, ufrag, pwd);
724 nice_agent_set_remote_credentials (ice->priv->nice_agent,
725 item->nice_stream_id, ufrag, pwd);
731 gst_webrtc_ice_add_turn_server (GstWebRTCICE * ice, const gchar * uri)
733 gboolean ret = FALSE;
736 if (!(valid_uri = _validate_turn_server (ice, uri)))
739 g_hash_table_insert (ice->turn_servers, g_strdup (uri), valid_uri);
748 gst_webrtc_ice_add_local_ip_address (GstWebRTCICE * ice, const gchar * address)
750 gboolean ret = FALSE;
751 NiceAddress nice_addr;
753 nice_address_init (&nice_addr);
755 ret = nice_address_set_from_string (&nice_addr, address);
758 ret = nice_agent_add_local_address (ice->priv->nice_agent, &nice_addr);
760 GST_ERROR_OBJECT (ice, "Failed to add local address to NiceAgent");
763 GST_ERROR_OBJECT (ice, "Failed to initialize NiceAddress [%s]", address);
770 gst_webrtc_ice_set_local_credentials (GstWebRTCICE * ice,
771 GstWebRTCICEStream * stream, gchar * ufrag, gchar * pwd)
773 struct NiceStreamItem *item;
775 g_return_val_if_fail (ufrag != NULL, FALSE);
776 g_return_val_if_fail (pwd != NULL, FALSE);
777 item = _find_item (ice, -1, -1, stream);
778 g_return_val_if_fail (item != NULL, FALSE);
780 GST_DEBUG_OBJECT (ice, "Setting local ICE credentials on "
781 "ICE stream %u ufrag:%s pwd:%s", item->nice_stream_id, ufrag, pwd);
783 nice_agent_set_local_credentials (ice->priv->nice_agent, item->nice_stream_id,
790 gst_webrtc_ice_gather_candidates (GstWebRTCICE * ice,
791 GstWebRTCICEStream * stream)
793 struct NiceStreamItem *item;
795 item = _find_item (ice, -1, -1, stream);
796 g_return_val_if_fail (item != NULL, FALSE);
798 GST_DEBUG_OBJECT (ice, "gather candidates for stream %u",
799 item->nice_stream_id);
801 return gst_webrtc_ice_stream_gather_candidates (stream);
805 gst_webrtc_ice_set_is_controller (GstWebRTCICE * ice, gboolean controller)
807 g_object_set (G_OBJECT (ice->priv->nice_agent), "controlling-mode",
812 gst_webrtc_ice_get_is_controller (GstWebRTCICE * ice)
815 g_object_get (G_OBJECT (ice->priv->nice_agent), "controlling-mode",
821 gst_webrtc_ice_set_force_relay (GstWebRTCICE * ice, gboolean force_relay)
823 g_object_set (G_OBJECT (ice->priv->nice_agent), "force-relay", force_relay,
828 gst_webrtc_ice_set_on_ice_candidate (GstWebRTCICE * ice,
829 GstWebRTCIceOnCandidateFunc func, gpointer user_data, GDestroyNotify notify)
831 if (ice->priv->on_candidate_notify)
832 ice->priv->on_candidate_notify (ice->priv->on_candidate_data);
833 ice->priv->on_candidate = NULL;
835 ice->priv->on_candidate = func;
836 ice->priv->on_candidate_data = user_data;
837 ice->priv->on_candidate_notify = notify;
841 gst_webrtc_ice_set_tos (GstWebRTCICE * ice, GstWebRTCICEStream * stream,
844 struct NiceStreamItem *item;
846 item = _find_item (ice, -1, -1, stream);
847 g_return_if_fail (item != NULL);
849 nice_agent_set_stream_tos (ice->priv->nice_agent, item->nice_stream_id, tos);
853 _clear_ice_stream (struct NiceStreamItem *item)
859 g_signal_handlers_disconnect_by_data (item->stream->ice->priv->nice_agent,
861 gst_object_unref (item->stream);
866 _validate_turn_server (GstWebRTCICE * ice, const gchar * s)
868 GstUri *uri = gst_uri_from_string_escaped (s);
869 const gchar *userinfo, *scheme;
870 GList *keys = NULL, *l;
871 gchar *user = NULL, *pass = NULL;
872 gboolean turn_tls = FALSE;
875 GST_DEBUG_OBJECT (ice, "validating turn server, %s", s);
878 GST_ERROR_OBJECT (ice, "Could not parse turn server '%s'", s);
882 scheme = gst_uri_get_scheme (uri);
883 if (g_strcmp0 (scheme, "turn") == 0) {
884 } else if (g_strcmp0 (scheme, "turns") == 0) {
887 GST_ERROR_OBJECT (ice, "unknown scheme '%s'", scheme);
891 keys = gst_uri_get_query_keys (uri);
892 for (l = keys; l; l = l->next) {
893 gchar *key = l->data;
895 if (g_strcmp0 (key, "transport") == 0) {
896 const gchar *transport = gst_uri_get_query_value (uri, "transport");
898 } else if (g_strcmp0 (transport, "udp") == 0) {
899 } else if (g_strcmp0 (transport, "tcp") == 0) {
901 GST_ERROR_OBJECT (ice, "unknown transport value, '%s'", transport);
905 GST_ERROR_OBJECT (ice, "unknown query key, '%s'", key);
910 /* TODO: Implement error checking similar to the stun server below */
911 userinfo = gst_uri_get_userinfo (uri);
912 _parse_userinfo (userinfo, &user, &pass);
914 GST_ERROR_OBJECT (ice, "No username specified in '%s'", s);
918 GST_ERROR_OBJECT (ice, "No password specified in '%s'", s);
922 port = gst_uri_get_port (uri);
924 if (port == GST_URI_NO_PORT) {
926 gst_uri_set_port (uri, 5349);
928 gst_uri_set_port (uri, 3478);
941 gst_webrtc_ice_set_stun_server (GstWebRTCICE * ice, const gchar * uri_s)
943 GstUri *uri = gst_uri_from_string_escaped (uri_s);
944 const gchar *msg = "must be of the form stun://<host>:<port>";
946 GST_DEBUG_OBJECT (ice, "setting stun server, %s", uri_s);
949 GST_ERROR_OBJECT (ice, "Couldn't parse stun server '%s', %s", uri_s, msg);
953 if (ice->stun_server)
954 gst_uri_unref (ice->stun_server);
955 ice->stun_server = uri;
959 gst_webrtc_ice_get_stun_server (GstWebRTCICE * ice)
961 if (ice->stun_server)
962 return gst_uri_to_string (ice->stun_server);
968 gst_webrtc_ice_set_turn_server (GstWebRTCICE * ice, const gchar * uri_s)
970 GstUri *uri = _validate_turn_server (ice, uri_s);
973 if (ice->turn_server)
974 gst_uri_unref (ice->turn_server);
975 ice->turn_server = uri;
980 gst_webrtc_ice_get_turn_server (GstWebRTCICE * ice)
982 if (ice->turn_server)
983 return gst_uri_to_string (ice->turn_server);
989 gst_webrtc_ice_set_property (GObject * object, guint prop_id,
990 const GValue * value, GParamSpec * pspec)
992 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
996 g_object_set_property (G_OBJECT (ice->priv->nice_agent),
1000 g_object_set_property (G_OBJECT (ice->priv->nice_agent),
1004 case PROP_MIN_RTP_PORT:
1005 ice->min_rtp_port = g_value_get_uint (value);
1006 if (ice->min_rtp_port > ice->max_rtp_port)
1007 g_warning ("Set min-rtp-port to %u which is larger than"
1008 " max-rtp-port %u", ice->min_rtp_port, ice->max_rtp_port);
1011 case PROP_MAX_RTP_PORT:
1012 ice->max_rtp_port = g_value_get_uint (value);
1013 if (ice->min_rtp_port > ice->max_rtp_port)
1014 g_warning ("Set max-rtp-port to %u which is smaller than"
1015 " min-rtp-port %u", ice->max_rtp_port, ice->min_rtp_port);
1019 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1025 gst_webrtc_ice_get_property (GObject * object, guint prop_id,
1026 GValue * value, GParamSpec * pspec)
1028 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1032 g_value_set_object (value, ice->priv->nice_agent);
1035 g_object_get_property (G_OBJECT (ice->priv->nice_agent),
1039 g_object_get_property (G_OBJECT (ice->priv->nice_agent),
1043 case PROP_MIN_RTP_PORT:
1044 g_value_set_uint (value, ice->min_rtp_port);
1047 case PROP_MAX_RTP_PORT:
1048 g_value_set_uint (value, ice->max_rtp_port);
1052 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1058 gst_webrtc_ice_finalize (GObject * object)
1060 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1062 g_signal_handlers_disconnect_by_data (ice->priv->nice_agent, ice);
1066 if (ice->priv->on_candidate_notify)
1067 ice->priv->on_candidate_notify (ice->priv->on_candidate_data);
1068 ice->priv->on_candidate = NULL;
1069 ice->priv->on_candidate_notify = NULL;
1071 if (ice->turn_server)
1072 gst_uri_unref (ice->turn_server);
1073 if (ice->stun_server)
1074 gst_uri_unref (ice->stun_server);
1076 g_mutex_clear (&ice->priv->lock);
1077 g_cond_clear (&ice->priv->cond);
1079 g_array_free (ice->priv->nice_stream_map, TRUE);
1081 g_object_unref (ice->priv->nice_agent);
1083 g_hash_table_unref (ice->turn_servers);
1085 G_OBJECT_CLASS (parent_class)->finalize (object);
1089 gst_webrtc_ice_constructed (GObject * object)
1091 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1092 NiceAgentOption options = 0;
1094 _start_thread (ice);
1096 options |= NICE_AGENT_OPTION_ICE_TRICKLE;
1097 options |= NICE_AGENT_OPTION_REGULAR_NOMINATION;
1099 ice->priv->nice_agent = nice_agent_new_full (ice->priv->main_context,
1100 NICE_COMPATIBILITY_RFC5245, options);
1101 g_signal_connect (ice->priv->nice_agent, "new-candidate-full",
1102 G_CALLBACK (_on_new_candidate), ice);
1104 G_OBJECT_CLASS (parent_class)->constructed (object);
1108 gst_webrtc_ice_class_init (GstWebRTCICEClass * klass)
1110 GObjectClass *gobject_class = (GObjectClass *) klass;
1112 gobject_class->constructed = gst_webrtc_ice_constructed;
1113 gobject_class->get_property = gst_webrtc_ice_get_property;
1114 gobject_class->set_property = gst_webrtc_ice_set_property;
1115 gobject_class->finalize = gst_webrtc_ice_finalize;
1117 g_object_class_install_property (gobject_class,
1119 g_param_spec_object ("agent", "ICE agent",
1120 "ICE agent in use by this object. WARNING! Accessing this property "
1121 "may have disastrous consequences for the operation of webrtcbin. "
1122 "Other ICE implementations may not have the same interface.",
1123 NICE_TYPE_AGENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1125 g_object_class_install_property (gobject_class,
1127 g_param_spec_boolean ("ice-tcp", "ICE TCP",
1128 "Whether the agent should use ICE-TCP when gathering candidates",
1129 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1131 g_object_class_install_property (gobject_class,
1133 g_param_spec_boolean ("ice-udp", "ICE UDP",
1134 "Whether the agent should use ICE-UDP when gathering candidates",
1135 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1138 * GstWebRTCICE:min-rtp-port:
1140 * Minimum port for local rtp port range.
1141 * min-rtp-port must be <= max-rtp-port
1145 g_object_class_install_property (gobject_class,
1147 g_param_spec_uint ("min-rtp-port", "ICE RTP candidate min port",
1148 "Minimum port for local rtp port range. "
1149 "min-rtp-port must be <= max-rtp-port",
1150 0, 65535, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1153 * GstWebRTCICE:max-rtp-port:
1155 * Maximum port for local rtp port range.
1156 * min-rtp-port must be <= max-rtp-port
1160 g_object_class_install_property (gobject_class,
1162 g_param_spec_uint ("max-rtp-port", "ICE RTP candidate max port",
1163 "Maximum port for local rtp port range. "
1164 "max-rtp-port must be >= min-rtp-port",
1166 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
1169 * GstWebRTCICE::add-local-ip-address:
1170 * @object: the #GstWebRTCICE
1171 * @address: The local IP address
1173 * Add a local IP address to use for ICE candidate gathering. If none
1174 * are supplied, they will be discovered automatically. Calling this signal
1175 * stops automatic ICE gathering.
1177 * Returns: whether the address could be added.
1179 gst_webrtc_ice_signals[ADD_LOCAL_IP_ADDRESS_SIGNAL] =
1180 g_signal_new_class_handler ("add-local-ip-address",
1181 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1182 G_CALLBACK (gst_webrtc_ice_add_local_ip_address), NULL, NULL,
1183 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
1187 gst_webrtc_ice_init (GstWebRTCICE * ice)
1189 ice->priv = gst_webrtc_ice_get_instance_private (ice);
1191 g_mutex_init (&ice->priv->lock);
1192 g_cond_init (&ice->priv->cond);
1195 g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
1196 (GDestroyNotify) gst_uri_unref);
1198 ice->priv->nice_stream_map =
1199 g_array_new (FALSE, TRUE, sizeof (struct NiceStreamItem));
1200 g_array_set_clear_func (ice->priv->nice_stream_map,
1201 (GDestroyNotify) _clear_ice_stream);
1205 gst_webrtc_ice_new (const gchar * name)
1207 return g_object_new (GST_TYPE_WEBRTC_ICE, "name", name, NULL);