2 * This file is part of the Nice GLib ICE library.
4 * Contains a unit test for functionality to fallback to non-ICE
5 * operation if remote party does not support ICE.
7 * (C) 2007 Nokia Corporation. All rights reserved.
8 * Contact: Kai Vehmanen
10 * The contents of this file are subject to the Mozilla Public License Version
11 * 1.1 (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 * for the specific language governing rights and limitations under the
20 * The Original Code is the Nice GLib ICE library.
22 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
23 * Corporation. All Rights Reserved.
28 * Alternatively, the contents of this file may be used under the terms of the
29 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30 * case the provisions of LGPL are applicable instead of those above. If you
31 * wish to allow use of your version of this file only under the terms of the
32 * LGPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replace
34 * them with the notice and other provisions required by the LGPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the LGPL.
46 #include "agent-priv.h" /* for testing purposes */
49 static NiceComponentState global_lagent_state
= NICE_COMPONENT_STATE_LAST
;
50 static NiceComponentState global_ragent_state
= NICE_COMPONENT_STATE_LAST
;
51 static guint global_components_ready
= 0;
52 static guint global_components_ready_exit
= 0;
53 static guint global_components_failed
= 0;
54 static guint global_components_failed_exit
= 0;
55 static GMainLoop
*global_mainloop
= NULL
;
56 static gboolean global_lagent_gathering_done
= FALSE
;
57 static gboolean global_ragent_gathering_done
= FALSE
;
58 static gboolean global_lagent_ibr_received
= FALSE
;
59 static gboolean global_ragent_ibr_received
= FALSE
;
60 static int global_lagent_cands
= 0;
61 static int global_ragent_cands
= 0;
62 static gint global_ragent_read
= 0;
63 static gint global_ragent_read_exit
= 0;
65 static void priv_print_global_status (void)
67 g_debug ("\tgathering_done=%d", global_lagent_gathering_done
&& global_ragent_gathering_done
);
68 g_debug ("\tlstate=%d", global_lagent_state
);
69 g_debug ("\trstate=%d", global_ragent_state
);
72 static gboolean
timer_cb (gpointer pointer
)
74 g_debug ("test-fallback:%s: %p", G_STRFUNC
, pointer
);
76 /* signal status via a global variable */
78 /* note: should not be reached, abort */
79 g_error ("ERROR: test has got stuck, aborting...");
82 static void cb_nice_recv (NiceAgent
*agent
, guint stream_id
, guint component_id
, guint len
, gchar
*buf
, gpointer user_data
)
84 g_debug ("test-fallback:%s: %p", G_STRFUNC
, user_data
);
86 /* XXX: dear compiler, these are for you: */
87 (void)agent
; (void)stream_id
; (void)component_id
; (void)buf
;
89 if ((intptr_t)user_data
== 2) {
90 global_ragent_read
+= len
;
92 if (global_ragent_read
== global_ragent_read_exit
)
93 g_main_loop_quit (global_mainloop
);
97 static void cb_candidate_gathering_done(NiceAgent
*agent
, gpointer data
)
99 g_debug ("test-fallback:%s: %p", G_STRFUNC
, data
);
101 if ((intptr_t)data
== 1)
102 global_lagent_gathering_done
= TRUE
;
103 else if ((intptr_t)data
== 2)
104 global_ragent_gathering_done
= TRUE
;
106 if (global_lagent_gathering_done
&&
107 global_ragent_gathering_done
)
108 g_main_loop_quit (global_mainloop
);
110 /* XXX: dear compiler, these are for you: */
114 static void cb_component_state_changed (NiceAgent
*agent
, guint stream_id
, guint component_id
, guint state
, gpointer data
)
116 g_debug ("test-fallback:%s: %p", __func__
, data
);
118 if ((intptr_t)data
== 1)
119 global_lagent_state
= state
;
120 else if ((intptr_t)data
== 2)
121 global_ragent_state
= state
;
123 if (state
== NICE_COMPONENT_STATE_READY
)
124 global_components_ready
++;
125 if (state
== NICE_COMPONENT_STATE_FAILED
)
126 global_components_failed
++;
128 g_debug ("test-fallback: READY %u exit at %u.", global_components_ready
, global_components_ready_exit
);
130 /* signal status via a global variable */
131 if (global_components_ready
== global_components_ready_exit
) {
132 g_main_loop_quit (global_mainloop
);
136 /* signal status via a global variable */
137 if (global_components_failed
== global_components_failed_exit
) {
138 g_main_loop_quit (global_mainloop
);
142 /* XXX: dear compiler, these are for you: */
143 (void)agent
; (void)stream_id
; (void)data
; (void)component_id
;
146 static void cb_new_selected_pair(NiceAgent
*agent
, guint stream_id
, guint component_id
,
147 gchar
*lfoundation
, gchar
* rfoundation
, gpointer data
)
149 g_debug ("test-fallback:%s: %p", __func__
, data
);
151 if ((intptr_t)data
== 1)
152 ++global_lagent_cands
;
153 else if ((intptr_t)data
== 2)
154 ++global_ragent_cands
;
156 /* XXX: dear compiler, these are for you: */
157 (void)agent
; (void)stream_id
; (void)component_id
; (void)lfoundation
; (void)rfoundation
;
160 static void cb_new_candidate(NiceAgent
*agent
, guint stream_id
, guint component_id
,
161 gchar
*foundation
, gpointer data
)
163 g_debug ("test-fallback:%s: %p", __func__
, data
);
165 /* XXX: dear compiler, these are for you: */
166 (void)agent
; (void)stream_id
; (void)data
; (void)component_id
; (void)foundation
;
169 static void cb_initial_binding_request_received(NiceAgent
*agent
, guint stream_id
, gpointer data
)
171 g_debug ("test-fallback:%s: %p", __func__
, data
);
173 if ((intptr_t)data
== 1)
174 global_lagent_ibr_received
= TRUE
;
175 else if ((intptr_t)data
== 2)
176 global_ragent_ibr_received
= TRUE
;
178 /* XXX: dear compiler, these are for you: */
179 (void)agent
; (void)stream_id
; (void)data
;
182 static void priv_get_local_addr (NiceAgent
*agent
, guint stream_id
, guint component_id
, NiceAddress
*dstaddr
)
185 cands
= nice_agent_get_local_candidates(agent
, stream_id
, component_id
);
186 for (i
= cands
; i
; i
= i
->next
) {
187 NiceCandidate
*cand
= i
->data
;
190 *dstaddr
= cand
->addr
;
194 g_slist_free (cands
);
197 static int run_fallback_test (NiceAgent
*lagent
, NiceAgent
*ragent
, NiceAddress
*baseaddr
)
199 NiceAddress laddr
, raddr
, laddr_rtcp
, raddr_rtcp
;
204 memset (&cdes
, 0, sizeof(NiceCandidate
));
205 cdes
.priority
= 100000;
206 strcpy (cdes
.foundation
, "1");
207 cdes
.type
= NICE_CANDIDATE_TYPE_HOST
;
208 cdes
.transport
= NICE_CANDIDATE_TRANSPORT_UDP
;
209 cdes
.base_addr
= *baseaddr
;
211 /* step: initialize variables modified by the callbacks */
212 global_components_ready
= 0;
213 global_components_ready_exit
= 4;
214 global_components_failed
= 0;
215 global_components_failed_exit
= 4;
216 global_lagent_gathering_done
= FALSE
;
217 global_ragent_gathering_done
= FALSE
;
218 global_lagent_ibr_received
=
219 global_ragent_ibr_received
= FALSE
;
220 global_lagent_cands
=
221 global_ragent_cands
= 0;
222 global_ragent_read_exit
= -1;
224 g_object_set (G_OBJECT (lagent
), "controlling-mode", TRUE
, NULL
);
225 g_object_set (G_OBJECT (ragent
), "controlling-mode", FALSE
, NULL
);
227 /* step: add one stream, with RTP+RTCP components, to each agent */
228 ls_id
= nice_agent_add_stream (lagent
, 2);
229 rs_id
= nice_agent_add_stream (ragent
, 2);
230 g_assert (ls_id
> 0);
231 g_assert (rs_id
> 0);
233 nice_agent_gather_candidates (lagent
, ls_id
);
234 nice_agent_gather_candidates (ragent
, rs_id
);
236 /* step: attach to mainloop (needed to register the fds) */
237 nice_agent_attach_recv (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTP
,
238 g_main_loop_get_context (global_mainloop
), cb_nice_recv
, (gpointer
)1);
239 nice_agent_attach_recv (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTCP
,
240 g_main_loop_get_context (global_mainloop
), cb_nice_recv
, (gpointer
)1);
241 nice_agent_attach_recv (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTP
,
242 g_main_loop_get_context (global_mainloop
), cb_nice_recv
, (gpointer
)2);
243 nice_agent_attach_recv (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTCP
,
244 g_main_loop_get_context (global_mainloop
), cb_nice_recv
, (gpointer
)2);
246 /* step: run mainloop until local candidates are ready
247 * (see timer_cb() above) */
248 if (global_lagent_gathering_done
!= TRUE
||
249 global_ragent_gathering_done
!= TRUE
) {
250 g_debug ("test-fallback: Added streams, running mainloop until 'candidate-gathering-done'...");
251 g_main_loop_run (global_mainloop
);
252 g_assert (global_lagent_gathering_done
== TRUE
);
253 g_assert (global_ragent_gathering_done
== TRUE
);
256 /* step: find out the local candidates of each agent */
258 priv_get_local_addr (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTP
, &raddr
);
259 g_debug ("test-fallback: local RTP port R %u",
260 nice_address_get_port (&raddr
));
262 priv_get_local_addr (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTP
, &laddr
);
263 g_debug ("test-fallback: local RTP port L %u",
264 nice_address_get_port (&laddr
));
266 priv_get_local_addr (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTCP
, &raddr_rtcp
);
267 g_debug ("test-fallback: local RTCP port R %u",
268 nice_address_get_port (&raddr_rtcp
));
270 priv_get_local_addr (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTCP
, &laddr_rtcp
);
271 g_debug ("test-fallback: local RTCP port L %u",
272 nice_address_get_port (&laddr_rtcp
));
274 /* step: exchange candidate information but not the credentials */
276 cands
= g_slist_append (NULL
, &cdes
);
277 cdes
.component_id
= NICE_COMPONENT_TYPE_RTP
;
279 nice_agent_set_remote_candidates (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTP
, cands
);
281 nice_agent_set_remote_candidates (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTP
, cands
);
282 cdes
.component_id
= NICE_COMPONENT_TYPE_RTCP
;
283 cdes
.addr
= raddr_rtcp
;
284 nice_agent_set_remote_candidates (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTCP
, cands
);
285 cdes
.addr
= laddr_rtcp
;
286 nice_agent_set_remote_candidates (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTCP
, cands
);
288 /* step: fall back to non-ICE mode on both sides */
289 g_assert (nice_agent_set_selected_pair (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTP
, "1", "1") == TRUE
);
290 g_assert (nice_agent_set_selected_pair (lagent
, ls_id
, NICE_COMPONENT_TYPE_RTCP
, "1", "1") == TRUE
);
291 g_assert (nice_agent_set_selected_pair (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTP
, "1", "1") == TRUE
);
292 g_assert (nice_agent_set_selected_pair (ragent
, rs_id
, NICE_COMPONENT_TYPE_RTCP
, "1", "1") == TRUE
);
294 g_debug ("test-fallback: Requested for fallback, running mainloop until component state change is completed...");
296 /* step: run the mainloop until connectivity checks succeed
297 * (see timer_cb() above) */
298 if (global_components_ready
< global_components_ready_exit
)
299 g_main_loop_run (global_mainloop
);
301 /* note: verify that agents are in correct state */
302 g_assert (global_lagent_state
== NICE_COMPONENT_STATE_READY
);
303 g_assert (global_ragent_state
== NICE_COMPONENT_STATE_READY
);
305 /* step: next send a packet -> should work even if no ICE processing
308 g_debug ("test-fallback: Sent a payload packet, run mainloop until packet received.");
310 /* step: send a new test packet from L ot R */
311 global_ragent_read
= 0;
312 g_assert (nice_agent_send (lagent
, ls_id
, 1, 16, "1234567812345678") == 16);
313 global_ragent_read_exit
= 16;
314 g_main_loop_run (global_mainloop
);
316 /* note: verify that payload was succesfully received */
317 g_assert (global_ragent_read
== 16);
319 g_debug ("test-fallback: Ran mainloop, removing streams...");
321 /* step: clean up resources and exit */
323 g_slist_free (cands
);
324 nice_agent_remove_stream (lagent
, ls_id
);
325 nice_agent_remove_stream (ragent
, rs_id
);
327 g_debug ("test-fallback: test COMPLETED");
334 NiceAgent
*lagent
, *ragent
; /* agent's L and R */
335 NiceUDPSocketFactory udpfactory
;
336 NiceAddress baseaddr
;
339 const char *stun_server
= NULL
, *stun_server_port
= NULL
;
342 g_thread_init (NULL
);
343 global_mainloop
= g_main_loop_new (NULL
, FALSE
);
345 /* Note: impl limits ...
346 * - no multi-stream support
350 nice_udp_bsd_socket_factory_init (&udpfactory
);
352 /* step: create the agents L and R */
353 lagent
= nice_agent_new (&udpfactory
,
354 g_main_loop_get_context (global_mainloop
), NICE_COMPATIBILITY_ID19
);
355 ragent
= nice_agent_new (&udpfactory
,
356 g_main_loop_get_context (global_mainloop
), NICE_COMPATIBILITY_ID19
);
360 /* step: add a timer to catch state changes triggered by signals */
361 timer_id
= g_timeout_add (30000, timer_cb
, NULL
);
363 /* step: specify which local interface to use */
364 if (!nice_address_set_from_string (&baseaddr
, "127.0.0.1"))
365 g_assert_not_reached ();
366 nice_agent_add_local_address (lagent
, &baseaddr
);
367 nice_agent_add_local_address (ragent
, &baseaddr
);
369 g_signal_connect (G_OBJECT (lagent
), "candidate-gathering-done",
370 G_CALLBACK (cb_candidate_gathering_done
), (gpointer
)1);
371 g_signal_connect (G_OBJECT (ragent
), "candidate-gathering-done",
372 G_CALLBACK (cb_candidate_gathering_done
), (gpointer
)2);
373 g_signal_connect (G_OBJECT (lagent
), "component-state-changed",
374 G_CALLBACK (cb_component_state_changed
), (gpointer
)1);
375 g_signal_connect (G_OBJECT (ragent
), "component-state-changed",
376 G_CALLBACK (cb_component_state_changed
), (gpointer
)2);
377 g_signal_connect (G_OBJECT (lagent
), "new-selected-pair",
378 G_CALLBACK (cb_new_selected_pair
), (gpointer
)1);
379 g_signal_connect (G_OBJECT (ragent
), "new-selected-pair",
380 G_CALLBACK (cb_new_selected_pair
), (gpointer
)2);
381 g_signal_connect (G_OBJECT (lagent
), "new-candidate",
382 G_CALLBACK (cb_new_candidate
), (gpointer
)1);
383 g_signal_connect (G_OBJECT (ragent
), "new-candidate",
384 G_CALLBACK (cb_new_candidate
), (gpointer
)2);
385 g_signal_connect (G_OBJECT (lagent
), "initial-binding-request-received",
386 G_CALLBACK (cb_initial_binding_request_received
), (gpointer
)1);
387 g_signal_connect (G_OBJECT (ragent
), "initial-binding-request-received",
388 G_CALLBACK (cb_initial_binding_request_received
), (gpointer
)2);
390 stun_server
= getenv ("NICE_STUN_SERVER");
391 stun_server_port
= getenv ("NICE_STUN_SERVER_PORT");
393 g_object_set (G_OBJECT (lagent
), "stun-server", stun_server
, NULL
);
394 g_object_set (G_OBJECT (lagent
), "stun-server-port", atoi (stun_server_port
), NULL
);
395 g_object_set (G_OBJECT (ragent
), "stun-server", stun_server
, NULL
);
396 g_object_set (G_OBJECT (ragent
), "stun-server-port", atoi (stun_server_port
), NULL
);
399 /* step: run test the first time */
400 g_debug ("test-fallback: TEST STARTS / fallback test");
401 result
= run_fallback_test (lagent
, ragent
, &baseaddr
);
402 priv_print_global_status ();
403 g_assert (result
== 0);
404 g_assert (global_lagent_state
== NICE_COMPONENT_STATE_READY
);
405 g_assert (global_ragent_state
== NICE_COMPONENT_STATE_READY
);
407 g_object_unref (lagent
);
408 g_object_unref (ragent
);
410 nice_udp_socket_factory_close (&udpfactory
);
412 g_main_loop_unref (global_mainloop
),
413 global_mainloop
= NULL
;
415 g_source_remove (timer_id
);