diff options
Diffstat (limited to 'auto_tests/toxav_many_test.c')
-rw-r--r-- | auto_tests/toxav_many_test.c | 459 |
1 files changed, 137 insertions, 322 deletions
diff --git a/auto_tests/toxav_many_test.c b/auto_tests/toxav_many_test.c index 4287118f..ef59b2b2 100644 --- a/auto_tests/toxav_many_test.c +++ b/auto_tests/toxav_many_test.c | |||
@@ -14,6 +14,7 @@ | |||
14 | #include <vpx/vpx_image.h> | 14 | #include <vpx/vpx_image.h> |
15 | 15 | ||
16 | #include "../toxcore/tox.h" | 16 | #include "../toxcore/tox.h" |
17 | #include "../toxcore/util.h" | ||
17 | #include "../toxcore/logger.h" | 18 | #include "../toxcore/logger.h" |
18 | #include "../toxcore/crypto_core.h" | 19 | #include "../toxcore/crypto_core.h" |
19 | #include "../toxav/toxav.h" | 20 | #include "../toxav/toxav.h" |
@@ -26,359 +27,177 @@ | |||
26 | #define c_sleep(x) usleep(1000*x) | 27 | #define c_sleep(x) usleep(1000*x) |
27 | #endif | 28 | #endif |
28 | 29 | ||
29 | pthread_mutex_t muhmutex; | ||
30 | 30 | ||
31 | typedef enum _CallStatus { | 31 | typedef struct { |
32 | none, | 32 | bool incoming; |
33 | InCall, | 33 | uint32_t state; |
34 | Ringing, | 34 | |
35 | Ended, | 35 | } CallControl; |
36 | Rejected, | ||
37 | Canceled | ||
38 | 36 | ||
39 | } CallStatus; | ||
40 | 37 | ||
41 | typedef struct _Party { | 38 | /** |
42 | CallStatus status; | 39 | * Callbacks |
43 | ToxAv *av; | 40 | */ |
44 | int id; | 41 | void t_toxav_call_cb(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_enabled, void *user_data) |
45 | } Party; | ||
46 | |||
47 | typedef struct _ACall { | ||
48 | pthread_t tid; | ||
49 | int idx; | ||
50 | |||
51 | Party Caller; | ||
52 | Party Callee; | ||
53 | } ACall; | ||
54 | |||
55 | typedef struct _Status { | ||
56 | ACall calls[3]; /* Make 3 calls for this test */ | ||
57 | } Status; | ||
58 | |||
59 | Status status_control; | ||
60 | |||
61 | void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) | ||
62 | { | ||
63 | if (length == 7 && memcmp("gentoo", data, 7) == 0) { | ||
64 | tox_friend_add_norequest(m, public_key, 0); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | |||
69 | /******************************************************************************/ | ||
70 | void callback_recv_invite ( void *av, int32_t call_index, void *_arg ) | ||
71 | { | 42 | { |
72 | Status *cast = _arg; | 43 | printf("Handling CALL callback\n"); |
73 | cast->calls[call_index].Callee.status = Ringing; | 44 | ((CallControl*)user_data)->incoming = true; |
74 | } | 45 | } |
75 | void callback_recv_ringing ( void *av, int32_t call_index, void *_arg ) | 46 | void t_toxav_call_state_cb(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data) |
76 | { | 47 | { |
77 | Status *cast = _arg; | 48 | printf("Handling CALL STATE callback: %d\n", state); |
78 | cast->calls[call_index].Caller.status = Ringing; | 49 | ((CallControl*)user_data)->state = state; |
79 | } | 50 | } |
80 | void callback_call_ended ( void *av, int32_t call_index, void *_arg ) | 51 | void t_toxav_receive_video_frame_cb(ToxAV *av, uint32_t friend_number, |
52 | uint16_t width, uint16_t height, | ||
53 | uint8_t const *y, uint8_t const *u, uint8_t const *v, | ||
54 | int32_t ystride, int32_t ustride, int32_t vstride, | ||
55 | void *user_data) | ||
81 | { | 56 | { |
82 | Status *cast = _arg; | 57 | (void) av; |
83 | 58 | (void) friend_number; | |
84 | if (av == cast->calls[call_index].Caller.av) | 59 | (void) width; |
85 | cast->calls[call_index].Caller.status = Ended; | 60 | (void) height; |
86 | else | 61 | (void) y; |
87 | cast->calls[call_index].Callee.status = Ended; | 62 | (void) u; |
63 | (void) v; | ||
64 | (void) ystride; | ||
65 | (void) ustride; | ||
66 | (void) vstride; | ||
67 | (void) user_data; | ||
88 | } | 68 | } |
89 | void callback_call_started ( void *av, int32_t call_index, void *_arg ) | 69 | void t_toxav_receive_audio_frame_cb(ToxAV *av, uint32_t friend_number, |
70 | int16_t const *pcm, | ||
71 | size_t sample_count, | ||
72 | uint8_t channels, | ||
73 | uint32_t sampling_rate, | ||
74 | void *user_data) | ||
90 | { | 75 | { |
91 | Status *cast = _arg; | 76 | (void) av; |
92 | 77 | (void) friend_number; | |
93 | if (av == cast->calls[call_index].Caller.av) | 78 | (void) pcm; |
94 | cast->calls[call_index].Caller.status = InCall; | 79 | (void) sample_count; |
95 | else | 80 | (void) channels; |
96 | cast->calls[call_index].Callee.status = InCall; | 81 | (void) sampling_rate; |
82 | (void) user_data; | ||
97 | } | 83 | } |
98 | void callback_call_canceled ( void *av, int32_t call_index, void *_arg ) | 84 | void t_accept_friend_request_cb(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) |
99 | { | 85 | { |
86 | if (length == 7 && memcmp("gentoo", data, 7) == 0) { | ||
87 | assert(tox_friend_add_norequest(m, public_key, NULL) != (uint32_t) ~0); | ||
88 | } | ||
100 | } | 89 | } |
101 | void callback_call_rejected ( void *av, int32_t call_index, void *_arg ) | ||
102 | { | ||
103 | Status *cast = _arg; | ||
104 | cast->calls[call_index].Caller.status = Rejected; | ||
105 | } | ||
106 | |||
107 | void callback_requ_timeout ( void *av, int32_t call_index, void *_arg ) | ||
108 | { | ||
109 | ck_assert_msg(0, "No answer!"); | ||
110 | } | ||
111 | |||
112 | void callback_audio (void *agent, int32_t call_idx, const int16_t *PCM, uint16_t size, void *data) | ||
113 | {} | ||
114 | 90 | ||
115 | void callback_video (void *agent, int32_t call_idx, const vpx_image_t *img, void *data) | ||
116 | {} | ||
117 | 91 | ||
118 | void register_callbacks(ToxAv *av, void *data) | 92 | /** |
93 | * Iterate helper | ||
94 | */ | ||
95 | ToxAV* setup_av_instance(Tox* tox, CallControl *CC) | ||
119 | { | 96 | { |
120 | toxav_register_callstate_callback(av, callback_call_started, av_OnStart, data); | 97 | TOXAV_ERR_NEW error; |
121 | toxav_register_callstate_callback(av, callback_call_canceled, av_OnCancel, data); | 98 | |
122 | toxav_register_callstate_callback(av, callback_call_rejected, av_OnReject, data); | 99 | ToxAV* av = toxav_new(tox, &error); |
123 | toxav_register_callstate_callback(av, callback_call_ended, av_OnEnd, data); | 100 | assert(error == TOXAV_ERR_NEW_OK); |
124 | toxav_register_callstate_callback(av, callback_recv_invite, av_OnInvite, data); | 101 | |
125 | toxav_register_callstate_callback(av, callback_recv_ringing, av_OnRinging, data); | 102 | toxav_callback_call(av, t_toxav_call_cb, CC); |
126 | toxav_register_callstate_callback(av, callback_requ_timeout, av_OnRequestTimeout, data); | 103 | toxav_callback_call_state(av, t_toxav_call_state_cb, CC); |
127 | 104 | toxav_callback_receive_video_frame(av, t_toxav_receive_video_frame_cb, CC); | |
128 | 105 | toxav_callback_receive_audio_frame(av, t_toxav_receive_audio_frame_cb, CC); | |
129 | toxav_register_audio_callback(av, callback_audio, NULL); | 106 | |
130 | toxav_register_video_callback(av, callback_video, NULL); | 107 | return av; |
131 | } | 108 | } |
132 | /*************************************************************************************************/ | 109 | void* call_thread(ToxAV* Alice, ToxAV* Bob) |
133 | |||
134 | int call_running[3]; | ||
135 | |||
136 | void *in_thread_call (void *arg) | ||
137 | { | 110 | { |
138 | #define call_print(call, what, args...) printf("[%d] " what "\n", call, ##args) | ||
139 | |||
140 | ACall *this_call = arg; | ||
141 | uint64_t start = 0; | ||
142 | int step = 0; | ||
143 | int call_idx; | ||
144 | |||
145 | const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000); | ||
146 | int16_t sample_payload[frame_size]; | ||
147 | randombytes((uint8_t *)sample_payload, sizeof(int16_t) * frame_size); | ||
148 | |||
149 | uint8_t prepared_payload[RTP_PAYLOAD_SIZE]; | ||
150 | |||
151 | register_callbacks(this_call->Caller.av, &status_control); | ||
152 | register_callbacks(this_call->Callee.av, arg); | ||
153 | |||
154 | /* NOTE: CALLEE WILL ALWAHYS NEED CALL_IDX == 0 */ | ||
155 | pthread_mutex_lock(&muhmutex); | ||
156 | |||
157 | while (call_running[this_call->idx]) { | ||
158 | |||
159 | pthread_mutex_unlock(&muhmutex); | ||
160 | |||
161 | switch ( step ) { | ||
162 | case 0: /* CALLER */ | ||
163 | toxav_call(this_call->Caller.av, &call_idx, this_call->Callee.id, &av_DefaultSettings, 10); | ||
164 | call_print(call_idx, "Calling ..."); | ||
165 | step++; | ||
166 | break; | ||
167 | |||
168 | case 1: /* CALLEE */ | ||
169 | pthread_mutex_lock(&muhmutex); | ||
170 | |||
171 | if (this_call->Caller.status == Ringing) { | ||
172 | call_print(call_idx, "Callee answers ..."); | ||
173 | pthread_mutex_unlock(&muhmutex); | ||
174 | toxav_answer(this_call->Callee.av, 0, &av_DefaultSettings); | ||
175 | step++; | ||
176 | start = time(NULL); | ||
177 | pthread_mutex_lock(&muhmutex); | ||
178 | } | ||
179 | |||
180 | pthread_mutex_unlock(&muhmutex); | ||
181 | break; | ||
182 | |||
183 | case 2: /* Rtp transmission */ | ||
184 | pthread_mutex_lock(&muhmutex); | ||
185 | |||
186 | if (this_call->Caller.status == InCall) { /* I think this is okay */ | ||
187 | call_print(call_idx, "Sending rtp ..."); | ||
188 | pthread_mutex_unlock(&muhmutex); | ||
189 | |||
190 | c_sleep(1000); /* We have race condition here */ | ||
191 | toxav_prepare_transmission(this_call->Callee.av, 0, 1); | ||
192 | toxav_prepare_transmission(this_call->Caller.av, call_idx, 1); | ||
193 | |||
194 | int payload_size = toxav_prepare_audio_frame(this_call->Caller.av, call_idx, prepared_payload, RTP_PAYLOAD_SIZE, | ||
195 | sample_payload, frame_size); | ||
196 | |||
197 | if ( payload_size < 0 ) { | ||
198 | ck_assert_msg ( 0, "Failed to encode payload" ); | ||
199 | } | ||
200 | |||
201 | |||
202 | while (time(NULL) - start < 10) { /* 10 seconds */ | ||
203 | /* Both send */ | ||
204 | toxav_send_audio(this_call->Caller.av, call_idx, prepared_payload, payload_size); | ||
205 | |||
206 | toxav_send_audio(this_call->Callee.av, 0, prepared_payload, payload_size); | ||
207 | |||
208 | /* Both receive */ | ||
209 | int16_t storage[RTP_PAYLOAD_SIZE]; | ||
210 | int recved; | ||
211 | |||
212 | c_sleep(20); | ||
213 | } | ||
214 | |||
215 | step++; /* This terminates the loop */ | ||
216 | |||
217 | pthread_mutex_lock(&muhmutex); | ||
218 | toxav_kill_transmission(this_call->Callee.av, 0); | ||
219 | toxav_kill_transmission(this_call->Caller.av, call_idx); | ||
220 | pthread_mutex_unlock(&muhmutex); | ||
221 | |||
222 | /* Call over CALLER hangs up */ | ||
223 | toxav_hangup(this_call->Caller.av, call_idx); | ||
224 | call_print(call_idx, "Hanging up ..."); | ||
225 | |||
226 | pthread_mutex_lock(&muhmutex); | ||
227 | } | ||
228 | |||
229 | pthread_mutex_unlock(&muhmutex); | ||
230 | break; | ||
231 | |||
232 | case 3: /* Wait for Both to have status ended */ | ||
233 | pthread_mutex_lock(&muhmutex); | ||
234 | |||
235 | if (this_call->Caller.status == Ended) { | ||
236 | pthread_mutex_unlock(&muhmutex); | ||
237 | c_sleep(1000); /* race condition */ | ||
238 | pthread_mutex_lock(&muhmutex); | ||
239 | this_call->Callee.status = Ended; | ||
240 | call_running[this_call->idx] = 0; | ||
241 | } | ||
242 | |||
243 | pthread_mutex_unlock(&muhmutex); | ||
244 | |||
245 | break; | ||
246 | |||
247 | } | ||
248 | |||
249 | c_sleep(20); | ||
250 | |||
251 | pthread_mutex_lock(&muhmutex); | ||
252 | } | ||
253 | |||
254 | pthread_mutex_unlock(&muhmutex); | ||
255 | call_print(call_idx, "Call ended successfully!"); | ||
256 | pthread_exit(NULL); | 111 | pthread_exit(NULL); |
257 | } | 112 | } |
258 | 113 | ||
259 | |||
260 | |||
261 | |||
262 | |||
263 | START_TEST(test_AV_three_calls) | 114 | START_TEST(test_AV_three_calls) |
264 | // void test_AV_three_calls() | ||
265 | { | 115 | { |
266 | long long unsigned int cur_time = time(NULL); | 116 | Tox* Alice, *bootstrap, *Bobs[3]; |
267 | Tox *bootstrap_node = tox_new(0, 0, 0, 0); | 117 | ToxAV* AliceAV, *BobsAV[3]; |
268 | Tox *caller = tox_new(0, 0, 0, 0); | 118 | |
269 | Tox *callees[3] = { | 119 | CallControl AliceCC[3], BobsCC[3]; |
270 | tox_new(0, 0, 0, 0), | 120 | |
271 | tox_new(0, 0, 0, 0), | ||
272 | tox_new(0, 0, 0, 0), | ||
273 | }; | ||
274 | |||
275 | |||
276 | ck_assert_msg(bootstrap_node != NULL, "Failed to create bootstrap node"); | ||
277 | |||
278 | int i = 0; | 121 | int i = 0; |
279 | 122 | { | |
280 | for (; i < 3; i ++) { | 123 | TOX_ERR_NEW error; |
281 | ck_assert_msg(callees[i] != NULL, "Failed to create 3 tox instances"); | 124 | |
282 | } | 125 | bootstrap = tox_new(NULL, NULL, 0, &error); |
283 | 126 | assert(error == TOX_ERR_NEW_OK); | |
284 | for ( i = 0; i < 3; i ++ ) { | 127 | |
285 | uint32_t to_compare = 974536; | 128 | Alice = tox_new(NULL, NULL, 0, &error); |
286 | tox_callback_friend_request(callees[i], accept_friend_request, &to_compare); | 129 | assert(error == TOX_ERR_NEW_OK); |
287 | uint8_t address[TOX_ADDRESS_SIZE]; | 130 | |
288 | tox_self_get_address(callees[i], address); | 131 | for (; i < 3; i ++) { |
289 | 132 | BobsAV[i] = tox_new(NULL, NULL, 0, &error); | |
290 | uint32_t test = tox_friend_add(caller, address, (uint8_t *)"gentoo", 7, 0); | 133 | assert(error == TOX_ERR_NEW_OK); |
291 | ck_assert_msg( test == i, "Failed to add friend error code: %i", test); | 134 | } |
292 | } | 135 | } |
293 | 136 | ||
137 | printf("Created 5 instances of Tox\n"); | ||
138 | printf("Preparing network...\n"); | ||
139 | long long unsigned int cur_time = time(NULL); | ||
140 | |||
141 | uint32_t to_compare = 974536; | ||
142 | uint8_t address[TOX_ADDRESS_SIZE]; | ||
143 | |||
144 | tox_callback_friend_request(Alice, t_accept_friend_request_cb, &to_compare); | ||
145 | tox_self_get_address(Alice, address); | ||
146 | |||
147 | |||
148 | assert(tox_friend_add(Bobs[0], address, (uint8_t *)"gentoo", 7, NULL) != (uint32_t) ~0); | ||
149 | assert(tox_friend_add(Bobs[1], address, (uint8_t *)"gentoo", 7, NULL) != (uint32_t) ~0); | ||
150 | assert(tox_friend_add(Bobs[2], address, (uint8_t *)"gentoo", 7, NULL) != (uint32_t) ~0); | ||
151 | |||
294 | uint8_t off = 1; | 152 | uint8_t off = 1; |
295 | 153 | ||
296 | while (1) { | 154 | while (1) { |
297 | tox_iterate(bootstrap_node); | 155 | tox_iterate(bootstrap); |
298 | tox_iterate(caller); | 156 | tox_iterate(Alice); |
299 | 157 | tox_iterate(Bobs[0]); | |
300 | for (i = 0; i < 3; i ++) { | 158 | tox_iterate(Bobs[1]); |
301 | tox_iterate(callees[i]); | 159 | tox_iterate(Bobs[2]); |
302 | } | 160 | |
303 | 161 | if (tox_self_get_connection_status(bootstrap) && | |
304 | 162 | tox_self_get_connection_status(Alice) && | |
305 | if (tox_self_get_connection_status(bootstrap_node) && | 163 | tox_self_get_connection_status(Bobs[0]) && |
306 | tox_self_get_connection_status(caller) && | 164 | tox_self_get_connection_status(Bobs[1]) && |
307 | tox_self_get_connection_status(callees[0]) && | 165 | tox_self_get_connection_status(Bobs[2]) && off) { |
308 | tox_self_get_connection_status(callees[1]) && | ||
309 | tox_self_get_connection_status(callees[2]) && off) { | ||
310 | printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time); | 166 | printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time); |
311 | off = 0; | 167 | off = 0; |
312 | } | 168 | } |
313 | 169 | ||
314 | 170 | if (tox_friend_get_connection_status(Alice, 0, NULL) == TOX_CONNECTION_UDP && | |
315 | if (tox_friend_get_connection_status(caller, 0, 0) && | 171 | tox_friend_get_connection_status(Alice, 1, NULL) == TOX_CONNECTION_UDP && |
316 | tox_friend_get_connection_status(caller, 1, 0) && | 172 | tox_friend_get_connection_status(Alice, 2, NULL) == TOX_CONNECTION_UDP && |
317 | tox_friend_get_connection_status(caller, 2, 0) ) | 173 | tox_friend_get_connection_status(Bobs[0], 0, NULL) == TOX_CONNECTION_UDP && |
174 | tox_friend_get_connection_status(Bobs[1], 0, NULL) == TOX_CONNECTION_UDP && | ||
175 | tox_friend_get_connection_status(Bobs[2], 0, NULL) == TOX_CONNECTION_UDP) | ||
318 | break; | 176 | break; |
319 | 177 | ||
320 | c_sleep(20); | 178 | c_sleep(20); |
321 | } | 179 | } |
322 | 180 | ||
323 | printf("All set after %llu seconds! Starting call...\n", time(NULL) - cur_time); | 181 | AliceAV = setup_av_instance(Alice, &AliceCC); |
324 | 182 | BobsAV[0] = setup_av_instance(Bobs[0], &BobsCC[0]); | |
325 | ToxAv *uniqcallerav = toxav_new(caller, 3); | 183 | BobsAV[1] = setup_av_instance(Bobs[1], &BobsCC[1]); |
326 | 184 | BobsAV[2] = setup_av_instance(Bobs[2], &BobsCC[2]); | |
185 | |||
186 | printf("Created 4 instances of ToxAV\n"); | ||
187 | printf("All set after %llu seconds!\n", time(NULL) - cur_time); | ||
188 | |||
189 | |||
190 | |||
191 | tox_kill(bootstrap); | ||
192 | tox_kill(Alice); | ||
193 | toxav_kill(AliceAV); | ||
194 | |||
327 | for (i = 0; i < 3; i ++) { | 195 | for (i = 0; i < 3; i ++) { |
328 | status_control.calls[i].idx = i; | 196 | tox_kill(Bobs[i]); |
329 | 197 | toxav_kill(BobsAV[i]); | |
330 | status_control.calls[i].Caller.av = uniqcallerav; | ||
331 | status_control.calls[i].Caller.id = 0; | ||
332 | status_control.calls[i].Caller.status = none; | ||
333 | |||
334 | status_control.calls[i].Callee.av = toxav_new(callees[i], 1); | ||
335 | status_control.calls[i].Callee.id = i; | ||
336 | status_control.calls[i].Callee.status = none; | ||
337 | } | 198 | } |
338 | 199 | ||
339 | pthread_mutex_init(&muhmutex, NULL); | 200 | printf("\nTest successful!\n"); |
340 | |||
341 | for ( i = 0; i < 3; i++ ) { | ||
342 | call_running[i] = 1; | ||
343 | pthread_create(&status_control.calls[i].tid, NULL, in_thread_call, &status_control.calls[i]); | ||
344 | } | ||
345 | |||
346 | /* Now start 3 calls and they'll run for 10 s */ | ||
347 | |||
348 | for ( i = 0; i < 3; i++ ) | ||
349 | pthread_detach(status_control.calls[i].tid); | ||
350 | |||
351 | while (call_running[0] || call_running[1] || call_running[2]) { | ||
352 | pthread_mutex_lock(&muhmutex); | ||
353 | |||
354 | tox_iterate(bootstrap_node); | ||
355 | tox_iterate(caller); | ||
356 | tox_iterate(callees[0]); | ||
357 | tox_iterate(callees[1]); | ||
358 | tox_iterate(callees[2]); | ||
359 | |||
360 | for ( i = 0; i < 3; i++ ) | ||
361 | toxav_do(status_control.calls[0].Caller.av); | ||
362 | |||
363 | toxav_do(status_control.calls[0].Callee.av); | ||
364 | toxav_do(status_control.calls[1].Callee.av); | ||
365 | toxav_do(status_control.calls[2].Callee.av); | ||
366 | |||
367 | pthread_mutex_unlock(&muhmutex); | ||
368 | c_sleep(20); | ||
369 | } | ||
370 | |||
371 | toxav_kill(status_control.calls[0].Caller.av); | ||
372 | toxav_kill(status_control.calls[0].Callee.av); | ||
373 | toxav_kill(status_control.calls[1].Callee.av); | ||
374 | toxav_kill(status_control.calls[2].Callee.av); | ||
375 | |||
376 | tox_kill(bootstrap_node); | ||
377 | tox_kill(caller); | ||
378 | |||
379 | for ( i = 0; i < 3; i ++) | ||
380 | tox_kill(callees[i]); | ||
381 | |||
382 | } | 201 | } |
383 | END_TEST | 202 | END_TEST |
384 | 203 | ||
@@ -410,8 +229,4 @@ int main(int argc, char *argv[]) | |||
410 | srunner_free(test_runner); | 229 | srunner_free(test_runner); |
411 | 230 | ||
412 | return number_failed; | 231 | return number_failed; |
413 | |||
414 | // test_AV_three_calls(); | ||
415 | |||
416 | // return 0; | ||
417 | } | 232 | } |