81be82280c79d95c06e98535639b2304519a4b19
[platform/upstream/libnice.git] / tests / test-different-number-streams.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include "agent.h"
6
7 #include <stdlib.h>
8 #include <string.h>
9
10 #define ADD_2_STREAMS TRUE
11 #define USE_SECOND_STREAM TRUE
12
13 static GMainLoop *global_mainloop = NULL;
14
15 static guint global_components_ready = 0;
16 static guint global_components_ready_exit = 0;
17
18 static gboolean timer_cb (gpointer pointer)
19 {
20   g_debug ("test-different-number-streams:%s: %p", G_STRFUNC, pointer);
21
22   /* signal status via a global variable */
23
24   /* note: should not be reached, abort */
25   g_error ("ERROR: test has got stuck, aborting...");
26
27   return FALSE;
28 }
29
30 static void cb_candidate_gathering_done(NiceAgent *agent, guint stream_id, gpointer data)
31 {
32   g_debug ("%p: gathering done (stream_id: %u)", agent, stream_id);
33 }
34
35 static void cb_component_state_changed (NiceAgent *agent, guint stream_id, guint component_id, guint state, gpointer data)
36 {
37   g_debug ("%p: component state changed (stream_id: %u, component_id: %u, state: %s)",
38            agent, stream_id, component_id, nice_component_state_to_string (state));
39
40   if (state == NICE_COMPONENT_STATE_READY) {
41     global_components_ready++;
42   }
43
44   /* signal status via a global variable */
45   if (global_components_ready == global_components_ready_exit) {
46     g_debug ("Components ready/failed achieved. Stopping mailoop");
47     g_main_loop_quit (global_mainloop);
48   }
49 }
50
51 static void set_candidates (NiceAgent *from, guint from_stream,
52     NiceAgent *to, guint to_stream, guint component)
53 {
54   GSList *cands = NULL, *i;
55
56   cands = nice_agent_get_local_candidates (from, from_stream, component);
57   nice_agent_set_remote_candidates (to, to_stream, component, cands);
58
59   for (i = cands; i; i = i->next)
60     nice_candidate_free ((NiceCandidate *) i->data);
61   g_slist_free (cands);
62 }
63
64 static void cb_nice_recv (NiceAgent *agent, guint stream_id, guint component_id, guint len, gchar *buf, gpointer user_data)
65 {
66   g_debug ("%p: recv (stream_id: %u, component_id: %u)", agent, stream_id, component_id);
67 }
68
69 int main (void)
70 {
71   NiceAgent *lagent, *ragent;
72   guint timer_id;
73   guint ls_id, rs_id_1, rs_id_2;
74   gchar *lufrag = NULL, *lpassword = NULL;
75   gchar *rufrag1 = NULL, *rpassword1 = NULL, *rufrag2 = NULL, *rpassword2 = NULL;
76   NiceAddress addr;
77
78
79 #ifdef G_OS_WIN32
80   WSADATA w;
81
82   WSAStartup(0x0202, &w);
83 #endif
84
85   /* Initialize nice agents */
86   nice_address_init (&addr);
87   nice_address_set_from_string (&addr, "127.0.0.1");
88
89   global_mainloop = g_main_loop_new (NULL, FALSE);
90
91   /* step: create the agents L and R */
92   lagent = nice_agent_new (g_main_loop_get_context (global_mainloop),
93       NICE_COMPATIBILITY_GOOGLE);
94   g_debug ("lagent: %p", lagent);
95
96   nice_agent_add_local_address (lagent, &addr);
97   nice_agent_set_software (lagent, "test-different-number-streams, Left Agent");
98   g_object_set (G_OBJECT (lagent), "ice-tcp", FALSE,  NULL);
99   g_object_set (G_OBJECT (lagent), "controlling-mode", TRUE, NULL);
100   g_object_set (G_OBJECT (lagent), "upnp", FALSE,  NULL);
101   g_signal_connect (G_OBJECT (lagent), "candidate-gathering-done",
102       G_CALLBACK (cb_candidate_gathering_done), NULL);
103   g_signal_connect (G_OBJECT (lagent), "component-state-changed",
104       G_CALLBACK (cb_component_state_changed), NULL);
105
106   ragent = nice_agent_new (g_main_loop_get_context (global_mainloop),
107       NICE_COMPATIBILITY_GOOGLE);
108   g_debug ("ragent: %p", ragent);
109
110   nice_agent_add_local_address (ragent, &addr);
111   nice_agent_set_software (ragent, "test-different-number-streams, Right Agent");
112   g_object_set (G_OBJECT (ragent), "ice-tcp", FALSE,  NULL);
113   g_object_set (G_OBJECT (ragent), "controlling-mode", FALSE, NULL);
114   g_object_set (G_OBJECT (ragent), "upnp", FALSE,  NULL);
115   g_signal_connect (G_OBJECT (ragent), "candidate-gathering-done",
116       G_CALLBACK (cb_candidate_gathering_done), NULL);
117   g_signal_connect (G_OBJECT (ragent), "component-state-changed",
118       G_CALLBACK (cb_component_state_changed), NULL);
119
120   /* step: add a timer to catch state changes triggered by signals */
121   timer_id = g_timeout_add (30000, timer_cb, NULL);
122
123   ls_id = nice_agent_add_stream (lagent, 2);
124   g_assert_cmpuint (ls_id, >, 0);
125   nice_agent_get_local_credentials(lagent, ls_id, &lufrag, &lpassword);
126
127   /* step: attach to mainloop (needed to register the fds) */
128   nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTP,
129       g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
130   nice_agent_attach_recv (lagent, ls_id, NICE_COMPONENT_TYPE_RTCP,
131       g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
132
133   global_components_ready_exit = 4;
134
135   if (ADD_2_STREAMS) {
136     rs_id_1 = nice_agent_add_stream (ragent, 2);
137     g_assert_cmpuint (rs_id_1, >, 0);
138     nice_agent_get_local_credentials(ragent, rs_id_1, &rufrag1, &rpassword1);
139
140     rs_id_2 = nice_agent_add_stream (ragent, 2);
141     g_assert_cmpuint (rs_id_2, >, 0);
142     nice_agent_get_local_credentials(ragent, rs_id_2, &rufrag2, &rpassword2);
143
144     nice_agent_set_remote_credentials (ragent, rs_id_2, lufrag, lpassword);
145     nice_agent_set_remote_credentials (lagent, ls_id, rufrag2, rpassword2);
146
147     g_assert (nice_agent_gather_candidates (lagent, ls_id) == TRUE);
148     g_assert (nice_agent_gather_candidates (ragent, rs_id_2) == TRUE);
149     g_assert (nice_agent_gather_candidates (ragent, rs_id_1) == TRUE);
150
151     if (USE_SECOND_STREAM) {
152       set_candidates (ragent, rs_id_2, lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
153       set_candidates (ragent, rs_id_2, lagent, ls_id, NICE_COMPONENT_TYPE_RTCP);
154       set_candidates (lagent, ls_id, ragent, rs_id_2, NICE_COMPONENT_TYPE_RTP);
155       set_candidates (lagent, ls_id, ragent, rs_id_2, NICE_COMPONENT_TYPE_RTCP);
156     } else {
157       set_candidates (ragent, rs_id_1, lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
158       set_candidates (ragent, rs_id_1, lagent, ls_id, NICE_COMPONENT_TYPE_RTCP);
159       set_candidates (lagent, ls_id, ragent, rs_id_1, NICE_COMPONENT_TYPE_RTP);
160       set_candidates (lagent, ls_id, ragent, rs_id_1, NICE_COMPONENT_TYPE_RTCP);
161     }
162
163     /* step: attach to mainloop (needed to register the fds) */
164     nice_agent_attach_recv (ragent, rs_id_1, NICE_COMPONENT_TYPE_RTP,
165         g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
166     nice_agent_attach_recv (ragent, rs_id_1, NICE_COMPONENT_TYPE_RTCP,
167         g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
168     nice_agent_attach_recv (ragent, rs_id_2, NICE_COMPONENT_TYPE_RTP,
169         g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
170     nice_agent_attach_recv (ragent, rs_id_2, NICE_COMPONENT_TYPE_RTCP,
171         g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
172   } else {
173     rs_id_1 = nice_agent_add_stream (ragent, 2);
174     g_assert_cmpuint (rs_id_1, >, 0);
175     nice_agent_get_local_credentials(ragent, rs_id_1, &rufrag1, &rpassword1);
176
177     nice_agent_set_remote_credentials (ragent, rs_id_1, lufrag, lpassword);
178     nice_agent_set_remote_credentials (lagent, ls_id, rufrag1, rpassword1);
179
180     g_assert (nice_agent_gather_candidates (lagent, ls_id) == TRUE);
181     g_assert (nice_agent_gather_candidates (ragent, rs_id_1) == TRUE);
182
183     /* step: attach to mainloop (needed to register the fds) */
184     nice_agent_attach_recv (ragent, rs_id_1, NICE_COMPONENT_TYPE_RTP,
185         g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
186     nice_agent_attach_recv (ragent, rs_id_1, NICE_COMPONENT_TYPE_RTCP,
187         g_main_loop_get_context (global_mainloop), cb_nice_recv, NULL);
188
189     set_candidates (ragent, rs_id_1, lagent, ls_id, NICE_COMPONENT_TYPE_RTP);
190     set_candidates (ragent, rs_id_1, lagent, ls_id, NICE_COMPONENT_TYPE_RTCP);
191     set_candidates (lagent, ls_id, ragent, rs_id_1, NICE_COMPONENT_TYPE_RTP);
192     set_candidates (lagent, ls_id, ragent, rs_id_1, NICE_COMPONENT_TYPE_RTCP);
193   }
194
195   /* step: run the mainloop until connectivity checks succeed
196   *       (see timer_cb() above) */
197   g_main_loop_run (global_mainloop);
198
199   g_free (lufrag);
200   g_free (lpassword);
201   g_free (rufrag1);
202   g_free (rpassword1);
203   g_free (rufrag2);
204   g_free (rpassword2);
205   g_object_unref (lagent);
206   g_object_unref (ragent);
207
208   g_main_loop_unref (global_mainloop);
209   global_mainloop = NULL;
210
211   g_source_remove (timer_id);
212
213 #ifdef G_OS_WIN32
214   WSACleanup();
215 #endif
216
217   return 0;
218 }