2 * This file is part of the Nice GLib ICE library.
4 * Unit test for ICE full-mode related features.
6 * (C) 2007 Nokia Corporation. All rights reserved.
7 * Contact: Kai Vehmanen
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
27 * Alternatively, the contents of this file may be used under the terms of the
28 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
29 * case the provisions of LGPL are applicable instead of those above. If you
30 * wish to allow use of your version of this file only under the terms of the
31 * LGPL and not to allow others to use your version of this file under the
32 * MPL, indicate your decision by deleting the provisions above and replace
33 * them with the notice and other provisions required by the LGPL. If you do
34 * not delete the provisions above, a recipient may use your version of this
35 * file under either the MPL or the LGPL.
49 volatile gint global_lagent_cands = 0;
50 volatile gint global_ragent_cands = 0;
54 gint global_lagent_buffers = 0;
55 gint global_ragent_buffers = 0;
57 /* Waits about 10 seconds for @var to be NULL/FALSE */
58 #define WAIT_UNTIL_UNSET(var, context) \
63 for (_i = 0; _i < 13 && (var); _i++) \
65 g_usleep (1000 * (1 << _i)); \
66 g_main_context_iteration (context, FALSE); \
73 mainloop_thread (gpointer data)
75 GMainLoop *loop = data;
77 g_main_loop_run (loop);
84 cb_new_selected_pair(NiceAgent *agent,
91 g_debug ("test-thread:%s: %p", G_STRFUNC, data);
93 if (GPOINTER_TO_UINT (data) == 1)
94 g_atomic_int_inc (&global_lagent_cands);
95 else if (GPOINTER_TO_UINT (data) == 2)
96 g_atomic_int_inc (&global_ragent_cands);
100 static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
102 NiceAgent *other = g_object_get_data (G_OBJECT (agent), "other-agent");
103 gchar *ufrag = NULL, *password = NULL;
108 g_debug ("test-thread:%s", G_STRFUNC);
110 tmp = g_object_get_data (G_OBJECT (agent), "id");
111 id = GPOINTER_TO_UINT (tmp);
112 tmp = g_object_get_data (G_OBJECT (other), "id");
113 other_id = GPOINTER_TO_UINT (tmp);
115 nice_agent_get_local_credentials(agent, id, &ufrag, &password);
116 nice_agent_set_remote_credentials (other,
117 other_id, ufrag, password);
121 cands = nice_agent_get_local_candidates(agent, id, 1);
122 g_assert (cands != NULL);
124 nice_agent_set_remote_candidates (other, other_id, 1, cands);
126 for (i = cands; i; i = i->next)
127 nice_candidate_free ((NiceCandidate *) i->data);
128 g_slist_free (cands);
132 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
137 g_debug ("Agent %p Stream %d Component: %d Received %u bytes", agent,
138 stream_id, component_id, len);
140 g_mutex_lock (&buffers_mutex);
141 if (GPOINTER_TO_UINT (user_data) == 1)
142 count = &global_lagent_buffers;
143 else if (GPOINTER_TO_UINT (user_data) == 2)
144 count = &global_ragent_buffers;
146 g_error ("Invalid agent ?");
152 memset (data, *count + '1', 10);
154 g_assert_cmpmem (buf, len, data, 10);
155 g_assert_cmpuint (len, ==, 10);
159 g_cond_signal (&buffers_cond);
160 g_mutex_unlock (&buffers_mutex);
164 static void cb_component_state_changed (NiceAgent *agent,
173 g_debug("Agent %p Stream %d Component %d state %s", agent, stream_id,
174 component_id, nice_component_state_to_string (state));
176 if (state != NICE_COMPONENT_STATE_READY)
181 memset (data, i+'1', 10);
183 g_debug ("Agent %p Stream: %d Component: %d Sending 10 bytes", agent, stream_id,
186 nice_agent_send (agent, stream_id, component_id, 10, data);
193 NiceAgent *lagent, *ragent; /* agent's L and R */
194 NiceAddress baseaddr;
195 const char *stun_server = NULL, *stun_server_port = NULL;
196 GMainContext *lmainctx, *rmainctx;
197 GMainLoop *lmainloop, *rmainloop;
198 GThread *lthread, *rthread;
200 GMainContext *ldmainctx, *rdmainctx;
201 GMainLoop *ldmainloop, *rdmainloop;
202 GThread *ldthread, *rdthread;
206 WSAStartup(0x0202, &w);
209 lmainctx = g_main_context_new ();
210 rmainctx = g_main_context_new ();
211 lmainloop = g_main_loop_new (lmainctx, FALSE);
212 rmainloop = g_main_loop_new (rmainctx, FALSE);
214 ldmainctx = g_main_context_new ();
215 rdmainctx = g_main_context_new ();
216 ldmainloop = g_main_loop_new (ldmainctx, FALSE);
217 rdmainloop = g_main_loop_new (rdmainctx, FALSE);
219 /* step: create the agents L and R */
220 lagent = nice_agent_new (lmainctx, NICE_COMPATIBILITY_MSN);
221 ragent = nice_agent_new (rmainctx, NICE_COMPATIBILITY_MSN);
223 g_object_set_data (G_OBJECT (lagent), "other-agent", ragent);
224 g_object_set_data (G_OBJECT (ragent), "other-agent", lagent);
226 g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
227 g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
228 g_object_set (G_OBJECT (lagent), "upnp", FALSE, NULL);
229 g_object_set (G_OBJECT (ragent), "upnp", FALSE, NULL);
231 /* step: specify which local interface to use */
232 if (!nice_address_set_from_string (&baseaddr, "127.0.0.1"))
233 g_assert_not_reached ();
234 nice_agent_add_local_address (lagent, &baseaddr);
235 nice_agent_add_local_address (ragent, &baseaddr);
237 g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
238 G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(1));
239 g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
240 G_CALLBACK (cb_candidate_gathering_done), GUINT_TO_POINTER(2));
241 g_signal_connect (G_OBJECT (lagent), "component-state-changed",
242 G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER(1));
243 g_signal_connect (G_OBJECT (ragent), "component-state-changed",
244 G_CALLBACK (cb_component_state_changed), GUINT_TO_POINTER(2));
245 g_signal_connect (G_OBJECT (lagent), "new-selected-pair",
246 G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(1));
247 g_signal_connect (G_OBJECT (ragent), "new-selected-pair",
248 G_CALLBACK (cb_new_selected_pair), GUINT_TO_POINTER(2));
250 stun_server = getenv ("NICE_STUN_SERVER");
251 stun_server_port = getenv ("NICE_STUN_SERVER_PORT");
253 g_object_set (G_OBJECT (lagent), "stun-server", stun_server, NULL);
254 g_object_set (G_OBJECT (lagent), "stun-server-port", atoi (stun_server_port), NULL);
255 g_object_set (G_OBJECT (ragent), "stun-server", stun_server, NULL);
256 g_object_set (G_OBJECT (ragent), "stun-server-port", atoi (stun_server_port), NULL);
259 /* step: test setter/getter functions for properties */
261 guint max_checks = 0;
262 gchar *string = NULL;
264 gboolean mode = FALSE;
265 g_object_get (G_OBJECT (lagent), "stun-server", &string, NULL);
266 g_assert (stun_server == NULL || strcmp (string, stun_server) == 0);
268 g_object_get (G_OBJECT (lagent), "stun-server-port", &port, NULL);
269 g_assert (stun_server_port == NULL || port == (guint)atoi (stun_server_port));
270 g_object_get (G_OBJECT (lagent), "controlling-mode", &mode, NULL);
271 g_assert (mode == TRUE);
272 g_object_set (G_OBJECT (lagent), "max-connectivity-checks", 300, NULL);
273 g_object_get (G_OBJECT (lagent), "max-connectivity-checks", &max_checks, NULL);
274 g_assert_cmpuint (max_checks, ==, 300);
277 /* step: run test the first time */
278 g_debug ("test-thread: TEST STARTS / running test for the 1st time");
280 lthread = g_thread_new ("lthread libnice", mainloop_thread, lmainloop);
281 rthread = g_thread_new ("rthread libnice", mainloop_thread, rmainloop);
286 ls_id = nice_agent_add_stream (lagent, 2);
287 rs_id = nice_agent_add_stream (ragent, 2);
288 g_assert_cmpuint (ls_id, >, 0);
289 g_assert_cmpuint (rs_id, >, 0);
291 g_object_set_data (G_OBJECT (lagent), "id", GUINT_TO_POINTER (ls_id));
292 g_object_set_data (G_OBJECT (ragent), "id", GUINT_TO_POINTER (rs_id));
294 nice_agent_gather_candidates (lagent, ls_id);
295 nice_agent_gather_candidates (ragent, rs_id);
297 nice_agent_attach_recv (lagent, ls_id, 1, ldmainctx, cb_nice_recv,
298 GUINT_TO_POINTER (1));
299 nice_agent_attach_recv (ragent, rs_id, 1, rdmainctx, cb_nice_recv,
300 GUINT_TO_POINTER (2));
302 ldthread = g_thread_new ("ldthread libnice", mainloop_thread, ldmainloop);
303 rdthread = g_thread_new ("rdthread libnice", mainloop_thread, rdmainloop);
308 g_debug ("ragent_buffers: %d lagent_buffers: %d", global_lagent_buffers,
309 global_ragent_buffers);
310 g_mutex_lock (&buffers_mutex);
311 while (global_ragent_buffers < 10 ||
312 global_lagent_buffers < 10) {
313 g_cond_wait (&buffers_cond, &buffers_mutex);
314 g_debug ("ragent_buffers: %d lagent_buffers: %d", global_lagent_buffers,
315 global_ragent_buffers);
317 g_mutex_unlock (&buffers_mutex);
320 while (!g_main_loop_is_running (ldmainloop));
321 while (g_main_loop_is_running (ldmainloop))
322 g_main_loop_quit (ldmainloop);
323 while (!g_main_loop_is_running (rdmainloop));
324 while (g_main_loop_is_running (rdmainloop))
325 g_main_loop_quit (rdmainloop);
326 while (!g_main_loop_is_running (lmainloop));
327 while (g_main_loop_is_running (lmainloop))
328 g_main_loop_quit (lmainloop);
329 while (!g_main_loop_is_running (rmainloop));
330 while (g_main_loop_is_running (rmainloop))
331 g_main_loop_quit (rmainloop);
333 g_thread_join (ldthread);
334 g_thread_join (rdthread);
335 g_thread_join (lthread);
336 g_thread_join (rthread);
338 /* note: verify that correct number of local candidates were reported */
339 g_assert_cmpint (g_atomic_int_get (&global_lagent_cands), ==, 1);
340 g_assert_cmpint (g_atomic_int_get (&global_ragent_cands), ==, 1);
342 g_object_add_weak_pointer (G_OBJECT (lagent), (gpointer *) &lagent);
343 g_object_add_weak_pointer (G_OBJECT (ragent), (gpointer *) &ragent);
345 g_object_unref (lagent);
346 g_object_unref (ragent);
348 WAIT_UNTIL_UNSET (lagent, lmainctx);
349 WAIT_UNTIL_UNSET (ragent, rmainctx);
351 g_main_loop_unref (lmainloop);
352 g_main_loop_unref (rmainloop);
353 g_main_loop_unref (ldmainloop);
354 g_main_loop_unref (rdmainloop);