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 = NULL, *new_candidate = NULL;
643 char *new_candv[4] = { NULL, };
644 gboolean failure = TRUE;
646 if (!get_candidate_address (candidate, &prefix, &address, &postfix)) {
647 GST_WARNING_OBJECT (ice, "Failed to retrieve address from candidate %s",
652 if (!g_str_has_suffix (address, ".local")) {
653 GST_WARNING_OBJECT (ice, "candidate address \'%s\' does not end "
654 "with \'.local\'", address);
659 if (!(new_addr = _resolve_host (ice, address))) {
660 GST_WARNING_OBJECT (ice, "Failed to resolve %s", address);
664 new_candv[0] = prefix;
665 new_candv[1] = new_addr;
666 new_candv[2] = postfix;
668 new_candidate = g_strjoinv (" ", new_candv);
670 GST_DEBUG_OBJECT (ice, "resolved to candidate %s", new_candidate);
673 nice_agent_parse_remote_candidate_sdp (ice->priv->nice_agent,
674 item->nice_stream_id, new_candidate);
676 GST_WARNING_OBJECT (ice, "Could not parse candidate \'%s\'",
688 g_free (new_candidate);
693 if (cand->component_id == 2) {
694 /* we only support rtcp-mux so rtcp candidates are useless for us */
695 GST_INFO_OBJECT (ice, "Dropping RTCP candidate %s", candidate);
696 nice_candidate_free (cand);
700 candidates = g_slist_append (candidates, cand);
702 nice_agent_set_remote_candidates (ice->priv->nice_agent, item->nice_stream_id,
703 cand->component_id, candidates);
705 g_slist_free (candidates);
706 nice_candidate_free (cand);
710 gst_webrtc_ice_set_remote_credentials (GstWebRTCICE * ice,
711 GstWebRTCICEStream * stream, gchar * ufrag, gchar * pwd)
713 struct NiceStreamItem *item;
715 g_return_val_if_fail (ufrag != NULL, FALSE);
716 g_return_val_if_fail (pwd != NULL, FALSE);
717 item = _find_item (ice, -1, -1, stream);
718 g_return_val_if_fail (item != NULL, FALSE);
720 GST_DEBUG_OBJECT (ice, "Setting remote ICE credentials on "
721 "ICE stream %u ufrag:%s pwd:%s", item->nice_stream_id, ufrag, pwd);
723 nice_agent_set_remote_credentials (ice->priv->nice_agent,
724 item->nice_stream_id, ufrag, pwd);
730 gst_webrtc_ice_add_turn_server (GstWebRTCICE * ice, const gchar * uri)
732 gboolean ret = FALSE;
735 if (!(valid_uri = _validate_turn_server (ice, uri)))
738 g_hash_table_insert (ice->turn_servers, g_strdup (uri), valid_uri);
747 gst_webrtc_ice_add_local_ip_address (GstWebRTCICE * ice, const gchar * address)
749 gboolean ret = FALSE;
750 NiceAddress nice_addr;
752 nice_address_init (&nice_addr);
754 ret = nice_address_set_from_string (&nice_addr, address);
757 ret = nice_agent_add_local_address (ice->priv->nice_agent, &nice_addr);
759 GST_ERROR_OBJECT (ice, "Failed to add local address to NiceAgent");
762 GST_ERROR_OBJECT (ice, "Failed to initialize NiceAddress [%s]", address);
769 gst_webrtc_ice_set_local_credentials (GstWebRTCICE * ice,
770 GstWebRTCICEStream * stream, gchar * ufrag, gchar * pwd)
772 struct NiceStreamItem *item;
774 g_return_val_if_fail (ufrag != NULL, FALSE);
775 g_return_val_if_fail (pwd != NULL, FALSE);
776 item = _find_item (ice, -1, -1, stream);
777 g_return_val_if_fail (item != NULL, FALSE);
779 GST_DEBUG_OBJECT (ice, "Setting local ICE credentials on "
780 "ICE stream %u ufrag:%s pwd:%s", item->nice_stream_id, ufrag, pwd);
782 nice_agent_set_local_credentials (ice->priv->nice_agent, item->nice_stream_id,
789 gst_webrtc_ice_gather_candidates (GstWebRTCICE * ice,
790 GstWebRTCICEStream * stream)
792 struct NiceStreamItem *item;
794 item = _find_item (ice, -1, -1, stream);
795 g_return_val_if_fail (item != NULL, FALSE);
797 GST_DEBUG_OBJECT (ice, "gather candidates for stream %u",
798 item->nice_stream_id);
800 return gst_webrtc_ice_stream_gather_candidates (stream);
804 gst_webrtc_ice_set_is_controller (GstWebRTCICE * ice, gboolean controller)
806 g_object_set (G_OBJECT (ice->priv->nice_agent), "controlling-mode",
811 gst_webrtc_ice_get_is_controller (GstWebRTCICE * ice)
814 g_object_get (G_OBJECT (ice->priv->nice_agent), "controlling-mode",
820 gst_webrtc_ice_set_force_relay (GstWebRTCICE * ice, gboolean force_relay)
822 g_object_set (G_OBJECT (ice->priv->nice_agent), "force-relay", force_relay,
827 gst_webrtc_ice_set_on_ice_candidate (GstWebRTCICE * ice,
828 GstWebRTCIceOnCandidateFunc func, gpointer user_data, GDestroyNotify notify)
830 if (ice->priv->on_candidate_notify)
831 ice->priv->on_candidate_notify (ice->priv->on_candidate_data);
832 ice->priv->on_candidate = NULL;
834 ice->priv->on_candidate = func;
835 ice->priv->on_candidate_data = user_data;
836 ice->priv->on_candidate_notify = notify;
840 gst_webrtc_ice_set_tos (GstWebRTCICE * ice, GstWebRTCICEStream * stream,
843 struct NiceStreamItem *item;
845 item = _find_item (ice, -1, -1, stream);
846 g_return_if_fail (item != NULL);
848 nice_agent_set_stream_tos (ice->priv->nice_agent, item->nice_stream_id, tos);
852 _clear_ice_stream (struct NiceStreamItem *item)
858 g_signal_handlers_disconnect_by_data (item->stream->ice->priv->nice_agent,
860 gst_object_unref (item->stream);
865 _validate_turn_server (GstWebRTCICE * ice, const gchar * s)
867 GstUri *uri = gst_uri_from_string_escaped (s);
868 const gchar *userinfo, *scheme;
869 GList *keys = NULL, *l;
870 gchar *user = NULL, *pass = NULL;
871 gboolean turn_tls = FALSE;
874 GST_DEBUG_OBJECT (ice, "validating turn server, %s", s);
877 GST_ERROR_OBJECT (ice, "Could not parse turn server '%s'", s);
881 scheme = gst_uri_get_scheme (uri);
882 if (g_strcmp0 (scheme, "turn") == 0) {
883 } else if (g_strcmp0 (scheme, "turns") == 0) {
886 GST_ERROR_OBJECT (ice, "unknown scheme '%s'", scheme);
890 keys = gst_uri_get_query_keys (uri);
891 for (l = keys; l; l = l->next) {
892 gchar *key = l->data;
894 if (g_strcmp0 (key, "transport") == 0) {
895 const gchar *transport = gst_uri_get_query_value (uri, "transport");
897 } else if (g_strcmp0 (transport, "udp") == 0) {
898 } else if (g_strcmp0 (transport, "tcp") == 0) {
900 GST_ERROR_OBJECT (ice, "unknown transport value, '%s'", transport);
904 GST_ERROR_OBJECT (ice, "unknown query key, '%s'", key);
909 /* TODO: Implement error checking similar to the stun server below */
910 userinfo = gst_uri_get_userinfo (uri);
911 _parse_userinfo (userinfo, &user, &pass);
913 GST_ERROR_OBJECT (ice, "No username specified in '%s'", s);
917 GST_ERROR_OBJECT (ice, "No password specified in '%s'", s);
921 port = gst_uri_get_port (uri);
923 if (port == GST_URI_NO_PORT) {
925 gst_uri_set_port (uri, 5349);
927 gst_uri_set_port (uri, 3478);
940 gst_webrtc_ice_set_stun_server (GstWebRTCICE * ice, const gchar * uri_s)
942 GstUri *uri = gst_uri_from_string_escaped (uri_s);
943 const gchar *msg = "must be of the form stun://<host>:<port>";
945 GST_DEBUG_OBJECT (ice, "setting stun server, %s", uri_s);
948 GST_ERROR_OBJECT (ice, "Couldn't parse stun server '%s', %s", uri_s, msg);
952 if (ice->stun_server)
953 gst_uri_unref (ice->stun_server);
954 ice->stun_server = uri;
958 gst_webrtc_ice_get_stun_server (GstWebRTCICE * ice)
960 if (ice->stun_server)
961 return gst_uri_to_string (ice->stun_server);
967 gst_webrtc_ice_set_turn_server (GstWebRTCICE * ice, const gchar * uri_s)
969 GstUri *uri = _validate_turn_server (ice, uri_s);
972 if (ice->turn_server)
973 gst_uri_unref (ice->turn_server);
974 ice->turn_server = uri;
979 gst_webrtc_ice_get_turn_server (GstWebRTCICE * ice)
981 if (ice->turn_server)
982 return gst_uri_to_string (ice->turn_server);
988 gst_webrtc_ice_set_property (GObject * object, guint prop_id,
989 const GValue * value, GParamSpec * pspec)
991 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
995 g_object_set_property (G_OBJECT (ice->priv->nice_agent),
999 g_object_set_property (G_OBJECT (ice->priv->nice_agent),
1003 case PROP_MIN_RTP_PORT:
1004 ice->min_rtp_port = g_value_get_uint (value);
1005 if (ice->min_rtp_port > ice->max_rtp_port)
1006 g_warning ("Set min-rtp-port to %u which is larger than"
1007 " max-rtp-port %u", ice->min_rtp_port, ice->max_rtp_port);
1010 case PROP_MAX_RTP_PORT:
1011 ice->max_rtp_port = g_value_get_uint (value);
1012 if (ice->min_rtp_port > ice->max_rtp_port)
1013 g_warning ("Set max-rtp-port to %u which is smaller than"
1014 " min-rtp-port %u", ice->max_rtp_port, ice->min_rtp_port);
1018 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1024 gst_webrtc_ice_get_property (GObject * object, guint prop_id,
1025 GValue * value, GParamSpec * pspec)
1027 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1031 g_value_set_object (value, ice->priv->nice_agent);
1034 g_object_get_property (G_OBJECT (ice->priv->nice_agent),
1038 g_object_get_property (G_OBJECT (ice->priv->nice_agent),
1042 case PROP_MIN_RTP_PORT:
1043 g_value_set_uint (value, ice->min_rtp_port);
1046 case PROP_MAX_RTP_PORT:
1047 g_value_set_uint (value, ice->max_rtp_port);
1051 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1057 gst_webrtc_ice_finalize (GObject * object)
1059 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1061 g_signal_handlers_disconnect_by_data (ice->priv->nice_agent, ice);
1065 if (ice->priv->on_candidate_notify)
1066 ice->priv->on_candidate_notify (ice->priv->on_candidate_data);
1067 ice->priv->on_candidate = NULL;
1068 ice->priv->on_candidate_notify = NULL;
1070 if (ice->turn_server)
1071 gst_uri_unref (ice->turn_server);
1072 if (ice->stun_server)
1073 gst_uri_unref (ice->stun_server);
1075 g_mutex_clear (&ice->priv->lock);
1076 g_cond_clear (&ice->priv->cond);
1078 g_array_free (ice->priv->nice_stream_map, TRUE);
1080 g_object_unref (ice->priv->nice_agent);
1082 g_hash_table_unref (ice->turn_servers);
1084 G_OBJECT_CLASS (parent_class)->finalize (object);
1088 gst_webrtc_ice_constructed (GObject * object)
1090 GstWebRTCICE *ice = GST_WEBRTC_ICE (object);
1091 NiceAgentOption options = 0;
1093 _start_thread (ice);
1095 options |= NICE_AGENT_OPTION_ICE_TRICKLE;
1096 options |= NICE_AGENT_OPTION_REGULAR_NOMINATION;
1098 ice->priv->nice_agent = nice_agent_new_full (ice->priv->main_context,
1099 NICE_COMPATIBILITY_RFC5245, options);
1100 g_signal_connect (ice->priv->nice_agent, "new-candidate-full",
1101 G_CALLBACK (_on_new_candidate), ice);
1103 G_OBJECT_CLASS (parent_class)->constructed (object);
1107 gst_webrtc_ice_class_init (GstWebRTCICEClass * klass)
1109 GObjectClass *gobject_class = (GObjectClass *) klass;
1111 gobject_class->constructed = gst_webrtc_ice_constructed;
1112 gobject_class->get_property = gst_webrtc_ice_get_property;
1113 gobject_class->set_property = gst_webrtc_ice_set_property;
1114 gobject_class->finalize = gst_webrtc_ice_finalize;
1116 g_object_class_install_property (gobject_class,
1118 g_param_spec_object ("agent", "ICE agent",
1119 "ICE agent in use by this object. WARNING! Accessing this property "
1120 "may have disastrous consequences for the operation of webrtcbin. "
1121 "Other ICE implementations may not have the same interface.",
1122 NICE_TYPE_AGENT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
1124 g_object_class_install_property (gobject_class,
1126 g_param_spec_boolean ("ice-tcp", "ICE TCP",
1127 "Whether the agent should use ICE-TCP when gathering candidates",
1128 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1130 g_object_class_install_property (gobject_class,
1132 g_param_spec_boolean ("ice-udp", "ICE UDP",
1133 "Whether the agent should use ICE-UDP when gathering candidates",
1134 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1137 * GstWebRTCICE:min-rtp-port:
1139 * Minimum port for local rtp port range.
1140 * min-rtp-port must be <= max-rtp-port
1144 g_object_class_install_property (gobject_class,
1146 g_param_spec_uint ("min-rtp-port", "ICE RTP candidate min port",
1147 "Minimum port for local rtp port range. "
1148 "min-rtp-port must be <= max-rtp-port",
1149 0, 65535, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1152 * GstWebRTCICE:max-rtp-port:
1154 * Maximum port for local rtp port range.
1155 * min-rtp-port must be <= max-rtp-port
1159 g_object_class_install_property (gobject_class,
1161 g_param_spec_uint ("max-rtp-port", "ICE RTP candidate max port",
1162 "Maximum port for local rtp port range. "
1163 "max-rtp-port must be >= min-rtp-port",
1165 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
1168 * GstWebRTCICE::add-local-ip-address:
1169 * @object: the #GstWebRTCICE
1170 * @address: The local IP address
1172 * Add a local IP address to use for ICE candidate gathering. If none
1173 * are supplied, they will be discovered automatically. Calling this signal
1174 * stops automatic ICE gathering.
1176 * Returns: whether the address could be added.
1178 gst_webrtc_ice_signals[ADD_LOCAL_IP_ADDRESS_SIGNAL] =
1179 g_signal_new_class_handler ("add-local-ip-address",
1180 G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1181 G_CALLBACK (gst_webrtc_ice_add_local_ip_address), NULL, NULL,
1182 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
1186 gst_webrtc_ice_init (GstWebRTCICE * ice)
1188 ice->priv = gst_webrtc_ice_get_instance_private (ice);
1190 g_mutex_init (&ice->priv->lock);
1191 g_cond_init (&ice->priv->cond);
1194 g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
1195 (GDestroyNotify) gst_uri_unref);
1197 ice->priv->nice_stream_map =
1198 g_array_new (FALSE, TRUE, sizeof (struct NiceStreamItem));
1199 g_array_set_clear_func (ice->priv->nice_stream_map,
1200 (GDestroyNotify) _clear_ice_stream);
1204 gst_webrtc_ice_new (const gchar * name)
1206 return g_object_new (GST_TYPE_WEBRTC_ICE, "name", name, NULL);