1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <check.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include "../toxcore/tox.h"
#include "../toxcore/logger.h"
#include "../toxcore/crypto_core.h"
#include "../toxav/toxav.h"
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
#define c_sleep(x) Sleep(1*x)
#else
#include <unistd.h>
#define c_sleep(x) usleep(1000*x)
#endif
typedef enum _CallStatus {
none,
InCall,
Ringing,
Ended,
Rejected,
Cancel
} CallStatus;
typedef struct _Party {
CallStatus status;
ToxAv *av;
time_t *CallStarted;
int call_index;
} Party;
typedef struct _Status {
Party Alice;
Party Bob;
} Status;
/* My default settings */
static ToxAvCodecSettings muhcaps;
void accept_friend_request(Tox *m, uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
{
if (length == 7 && memcmp("gentoo", data, 7) == 0) {
tox_add_friend_norequest(m, public_key);
}
}
/******************************************************************************/
void callback_recv_invite ( int32_t call_index, void *_arg )
{
Status *cast = _arg;
/* Bob always receives invite */
cast->Bob.status = Ringing;
cast->Bob.call_index = call_index;
}
void callback_recv_ringing ( int32_t call_index, void *_arg )
{
Status *cast = _arg;
/* Alice always sends invite */
cast->Alice.status = Ringing;
}
void callback_recv_starting ( int32_t call_index, void *_arg )
{
Status *cast = _arg;
/* Alice always sends invite */
printf("Call started on Alice side...\n");
cast->Alice.status = InCall;
toxav_prepare_transmission(cast->Alice.av, call_index, &muhcaps, 1);
}
void callback_recv_ending ( int32_t call_index, void *_arg )
{
Status *cast = _arg;
if ( cast->Alice.status == Rejected) {
printf ( "Call ended for Bob!\n" );
cast->Bob.status = Ended;
} else {
printf ( "Call ended for Alice!\n" );
cast->Alice.status = Ended;
}
}
void callback_recv_error ( int32_t call_index, void *_arg )
{
ck_assert_msg(0, "AV internal error");
}
void callback_call_started ( int32_t call_index, void *_arg )
{
Status *cast = _arg;
/* Alice always sends invite */
printf("Call started on Bob side...\n");
cast->Bob.status = InCall;
toxav_prepare_transmission(cast->Bob.av, call_index, &muhcaps, 1);
}
void callback_call_canceled ( int32_t call_index, void *_arg )
{
Status *cast = _arg;
printf ( "Call Canceled for Bob!\n" );
cast->Bob.status = Cancel;
}
void callback_call_rejected ( int32_t call_index, void *_arg )
{
Status *cast = _arg;
printf ( "Call rejected by Bob!\n"
"Call ended for Alice!\n" );
/* If Bob rejects, call is ended for alice and she sends ending */
cast->Alice.status = Rejected;
}
void callback_call_ended ( int32_t call_index, void *_arg )
{
Status *cast = _arg;
printf ( "Call ended for Bob!\n" );
cast->Bob.status = Ended;
}
void callback_requ_timeout ( int32_t call_index, void *_arg )
{
ck_assert_msg(0, "No answer!");
}
/*************************************************************************************************/
/* Alice calls bob and the call starts.
* What happens in the call is defined after. To quit the loop use: step++;
*/
#define CALL_AND_START_LOOP(AliceCallType, BobCallType) \
{ int step = 0, running = 1; while (running) {\
tox_do(bootstrap_node); tox_do(Alice); tox_do(Bob); \
switch ( step ) {\
case 0: /* Alice */ printf("Alice is calling...\n");\
toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, AliceCallType, 10); step++; break;\
case 1: /* Bob */ if (status_control.Bob.status == Ringing) { printf("Bob answers...\n");\
cur_time = time(NULL); toxav_answer(status_control.Bob.av, status_control.Bob.call_index, BobCallType); step++; } break; \
case 2: /* Rtp transmission */ \
if (status_control.Bob.status == InCall && status_control.Alice.status == InCall)
#define TERMINATE_SCOPE() break;\
case 3: /* Wait for Both to have status ended */\
if (status_control.Alice.status == Ended && status_control.Bob.status == Ended) running = 0; break; } c_sleep(20); } } printf("\n");
START_TEST(test_AV_flows)
// int test_AV_flows()
{
long long unsigned int cur_time = time(NULL);
Tox *bootstrap_node = tox_new(0);
Tox *Alice = tox_new(0);
Tox *Bob = tox_new(0);
ck_assert_msg(bootstrap_node || Alice || Bob, "Failed to create 3 tox instances");
uint32_t to_compare = 974536;
tox_callback_friend_request(Alice, accept_friend_request, &to_compare);
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
tox_get_address(Alice, address);
int test = tox_add_friend(Bob, address, (uint8_t *)"gentoo", 7);
ck_assert_msg(test == 0, "Failed to add friend error code: %i", test);
uint8_t off = 1;
while (1) {
tox_do(bootstrap_node);
tox_do(Alice);
tox_do(Bob);
if (tox_isconnected(bootstrap_node) && tox_isconnected(Alice) && tox_isconnected(Bob) && off) {
printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time);
off = 0;
}
if (tox_get_friend_connection_status(Alice, 0) == 1 && tox_get_friend_connection_status(Bob, 0) == 1)
break;
c_sleep(20);
}
printf("All set after %llu seconds! Starting call...\n", time(NULL) - cur_time);
muhcaps = av_DefaultSettings;
muhcaps.video_height = muhcaps.video_width = 128;
Status status_control = {
{none, toxav_new(Alice, 1), NULL, -1},
{none, toxav_new(Bob, 1), NULL, -1},
};
ck_assert_msg(status_control.Alice.av || status_control.Bob.av, "Failed to create 2 toxav instances");
toxav_register_callstate_callback(callback_call_started, av_OnStart, &status_control);
toxav_register_callstate_callback(callback_call_canceled, av_OnCancel, &status_control);
toxav_register_callstate_callback(callback_call_rejected, av_OnReject, &status_control);
toxav_register_callstate_callback(callback_call_ended, av_OnEnd, &status_control);
toxav_register_callstate_callback(callback_recv_invite, av_OnInvite, &status_control);
toxav_register_callstate_callback(callback_recv_ringing, av_OnRinging, &status_control);
toxav_register_callstate_callback(callback_recv_starting, av_OnStarting, &status_control);
toxav_register_callstate_callback(callback_recv_ending, av_OnEnding, &status_control);
toxav_register_callstate_callback(callback_recv_error, av_OnError, &status_control);
toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, &status_control);
const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000);
int16_t sample_payload[frame_size];
randombytes((uint8_t *)sample_payload, sizeof(int16_t) * frame_size);
uint8_t prepared_payload[RTP_PAYLOAD_SIZE];
int payload_size;
vpx_image_t *sample_image = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, 128, 128, 1);
memcpy(sample_image->planes[VPX_PLANE_Y], sample_payload, 10);
memcpy(sample_image->planes[VPX_PLANE_U], sample_payload, 10);
memcpy(sample_image->planes[VPX_PLANE_V], sample_payload, 10);
/*************************************************************************************************
* Successful flows (when call starts)
*/
/*
* Call with audio only on both sides. Alice calls Bob.
*/
CALL_AND_START_LOOP(TypeAudio, TypeAudio) {
/* Both send */
payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload,
1000, sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" );
}
toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size);
payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000,
sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" );
}
toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size);
/* Both receive */
int16_t storage[frame_size];
int recved;
/* Payload from Bob */
recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage);
if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
}
recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage);
if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
}
if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */
step++; /* This terminates the loop */
toxav_kill_transmission(status_control.Alice.av, status_control.Alice.call_index);
toxav_kill_transmission(status_control.Bob.av, status_control.Bob.call_index);
/* Call over Alice hangs up */
toxav_hangup(status_control.Alice.av, status_control.Alice.call_index);
}
}
TERMINATE_SCOPE()
/*
* Call with audio on both sides and video on one side. Alice calls Bob.
*/
CALL_AND_START_LOOP(TypeAudio, TypeVideo) {
/* Both send */
payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload,
1000, sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" );
}
toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size);
payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000,
sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" );
}
toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size);
// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image);
/* Both receive */
int16_t storage[frame_size];
vpx_image_t *video_storage;
int recved;
/* Payload from Bob */
recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage);
if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
}
/* Video payload */
// toxav_recv_video(status_control.Alice.av, status_control.Alice.call_index, &video_storage);
//
// if ( video_storage ) {
// /*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
// memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
// memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Bob is invalid");*/
// vpx_img_free(video_storage);
// }
/* Payload from Alice */
recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage);
if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
}
if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */
step++; /* This terminates the loop */
toxav_kill_transmission(status_control.Alice.av, status_control.Alice.call_index);
toxav_kill_transmission(status_control.Bob.av, status_control.Bob.call_index);
/* Call over Alice hangs up */
toxav_hangup(status_control.Alice.av, status_control.Alice.call_index);
}
}
TERMINATE_SCOPE()
/*
* Call with audio and video on both sides. Alice calls Bob.
*/
CALL_AND_START_LOOP(TypeVideo, TypeVideo) {
/* Both send */
payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload,
1000, sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" );
}
toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size);
payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000,
sample_payload, frame_size);
if ( payload_size < 0 ) {
ck_assert_msg ( 0, "Failed to encode payload" );
}
toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size);
// toxav_send_video(status_control.Alice.av, status_control.Alice.call_index, sample_image);
// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image);
/* Both receive */
int16_t storage[frame_size];
vpx_image_t *video_storage;
int recved;
/* Payload from Bob */
recved = toxav_recv_audio(status_control.Alice.av, status_control.Alice.call_index, frame_size, storage);
if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Bob is invalid");*/
}
/* Video payload */
// toxav_recv_video(status_control.Alice.av, status_control.Alice.call_index, &video_storage);
//
// if ( video_storage ) {
// /*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
// memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
// memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Bob is invalid");*/
// vpx_img_free(video_storage);
// }
/* Payload from Alice */
recved = toxav_recv_audio(status_control.Bob.av, status_control.Bob.call_index, frame_size, storage);
if ( recved ) {
/*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from Alice is invalid");*/
}
/* Video payload */
// toxav_recv_video(status_control.Bob.av, status_control.Bob.call_index, &video_storage);
//
// if ( video_storage ) {
// /*ck_assert_msg( memcmp(video_storage->planes[VPX_PLANE_Y], sample_payload, 10) == 0 ||
// memcmp(video_storage->planes[VPX_PLANE_U], sample_payload, 10) == 0 ||
// memcmp(video_storage->planes[VPX_PLANE_V], sample_payload, 10) == 0 , "Payload from Alice is invalid");*/
// vpx_img_free(video_storage);
// }
if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */
step++; /* This terminates the loop */
toxav_kill_transmission(status_control.Alice.av, status_control.Alice.call_index);
toxav_kill_transmission(status_control.Bob.av, status_control.Bob.call_index);
/* Call over Alice hangs up */
toxav_hangup(status_control.Alice.av, status_control.Alice.call_index);
}
}
TERMINATE_SCOPE()
/*************************************************************************************************
* Other flows
*/
/*
* Call and reject
*/
{
int step = 0;
int running = 1;
while (running) {
tox_do(bootstrap_node);
tox_do(Alice);
tox_do(Bob);
switch ( step ) {
case 0: /* Alice */
printf("Alice is calling...\n");
toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, TypeAudio, 10);
step++;
break;
case 1: /* Bob */
if (status_control.Bob.status == Ringing) {
printf("Bob rejects...\n");
toxav_reject(status_control.Bob.av, status_control.Bob.call_index, "Who likes D's anyway?");
step++;
}
break;
case 2: /* Wait for Both to have status ended */
if (status_control.Alice.status == Rejected && status_control.Bob.status == Ended) running = 0;
break;
}
c_sleep(20);
}
printf("\n");
}
/*
* Call and cancel
*/
{
int step = 0;
int running = 1;
while (running) {
tox_do(bootstrap_node);
tox_do(Alice);
tox_do(Bob);
switch ( step ) {
case 0: /* Alice */
printf("Alice is calling...\n");
toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, TypeAudio, 10);
step++;
break;
\
case 1: /* Alice again */
if (status_control.Bob.status == Ringing) {
printf("Alice cancels...\n");
toxav_cancel(status_control.Alice.av, status_control.Alice.call_index, 0, "Who likes D's anyway?");
step++;
}
break;
case 2: /* Wait for Both to have status ended */
if (status_control.Bob.status == Cancel) running = 0;
break;
}
c_sleep(20);
}
printf("\n");
}
printf("Calls ended!\n");
}
END_TEST
/*************************************************************************************************/
/*************************************************************************************************/
/*************************************************************************************************/
Suite *tox_suite(void)
{
Suite *s = suite_create("ToxAV");
TCase *tc_av_flows = tcase_create("AV_flows");
tcase_add_test(tc_av_flows, test_AV_flows);
tcase_set_timeout(tc_av_flows, 200);
suite_add_tcase(s, tc_av_flows);
return s;
}
int main(int argc, char *argv[])
{
Suite *tox = tox_suite();
SRunner *test_runner = srunner_create(tox);
setbuf(stdout, NULL);
srunner_run_all(test_runner, CK_NORMAL);
int number_failed = srunner_ntests_failed(test_runner);
srunner_free(test_runner);
return number_failed;
// return test_AV_flows();
}
|