summaryrefslogtreecommitdiff
path: root/auto_tests/toxav_many_test.c
diff options
context:
space:
mode:
authormannol <eniz_vukovic@hotmail.com>2014-07-21 01:10:57 +0200
committermannol <eniz_vukovic@hotmail.com>2014-07-21 01:10:57 +0200
commit1aeeef58b2b9e817563c9655d6b736cf41d1c110 (patch)
tree512faccefcb5f70c0640e831cd2209b5bbbf7e83 /auto_tests/toxav_many_test.c
parent77c7a3e1030daf9cfba9dae4f850a7a92810ec83 (diff)
Improved protocol and cleaned code a bit
Diffstat (limited to 'auto_tests/toxav_many_test.c')
-rw-r--r--auto_tests/toxav_many_test.c146
1 files changed, 72 insertions, 74 deletions
diff --git a/auto_tests/toxav_many_test.c b/auto_tests/toxav_many_test.c
index 2a931cb0..b64caf10 100644
--- a/auto_tests/toxav_many_test.c
+++ b/auto_tests/toxav_many_test.c
@@ -24,6 +24,7 @@
24#define c_sleep(x) usleep(1000*x) 24#define c_sleep(x) usleep(1000*x)
25#endif 25#endif
26 26
27pthread_mutex_t muhmutex;
27 28
28typedef enum _CallStatus { 29typedef enum _CallStatus {
29 none, 30 none,
@@ -43,6 +44,7 @@ typedef struct _Party {
43 44
44typedef struct _ACall { 45typedef struct _ACall {
45 pthread_t tid; 46 pthread_t tid;
47 int idx;
46 48
47 Party Caller; 49 Party Caller;
48 Party Callee; 50 Party Callee;
@@ -52,6 +54,8 @@ typedef struct _Status {
52 ACall calls[3]; /* Make 3 calls for this test */ 54 ACall calls[3]; /* Make 3 calls for this test */
53} Status; 55} Status;
54 56
57Status status_control;
58
55void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) 59void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata)
56{ 60{
57 if (length == 7 && memcmp("gentoo", data, 7) == 0) { 61 if (length == 7 && memcmp("gentoo", data, 7) == 0) {
@@ -61,58 +65,49 @@ void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *dat
61 65
62 66
63/******************************************************************************/ 67/******************************************************************************/
64void callback_recv_invite ( int32_t call_index, void *_arg ) 68void callback_recv_invite ( void* av, int32_t call_index, void *_arg )
65{ 69{
66 /* 70 /*
67 Status *cast = _arg; 71 Status *cast = _arg;
68 72
69 cast->calls[call_index].Callee.status = Ringing;*/ 73 cast->calls[call_index].Callee.status = Ringing;*/
70} 74}
71void callback_recv_ringing ( int32_t call_index, void *_arg ) 75void callback_recv_ringing ( void* av, int32_t call_index, void *_arg )
72{ 76{
73 Status *cast = _arg; 77 Status *cast = _arg;
74
75 cast->calls[call_index].Caller.status = Ringing; 78 cast->calls[call_index].Caller.status = Ringing;
76} 79}
77void callback_recv_starting ( int32_t call_index, void *_arg ) 80void callback_recv_starting ( void* av, int32_t call_index, void *_arg )
78{ 81{
79 Status *cast = _arg; 82 Status *cast = _arg;
80
81 cast->calls[call_index].Caller.status = InCall; 83 cast->calls[call_index].Caller.status = InCall;
82} 84}
83void callback_recv_ending ( int32_t call_index, void *_arg ) 85void callback_recv_ending ( void* av, int32_t call_index, void *_arg )
84{ 86{
85 Status *cast = _arg; 87 Status *cast = _arg;
86
87 cast->calls[call_index].Caller.status = Ended; 88 cast->calls[call_index].Caller.status = Ended;
88} 89}
89 90
90void callback_recv_error ( int32_t call_index, void *_arg ) 91void callback_call_started ( void* av, int32_t call_index, void *_arg )
91{
92 ck_assert_msg(0, "AV internal error");
93}
94
95void callback_call_started ( int32_t call_index, void *_arg )
96{ 92{
97 /* 93 /*
98 Status *cast = _arg; 94 Status *cast = _arg;
99 95
100 cast->calls[call_index].Callee.status = InCall;*/ 96 cast->calls[call_index].Callee.status = InCall;*/
101} 97}
102void callback_call_canceled ( int32_t call_index, void *_arg ) 98void callback_call_canceled ( void* av, int32_t call_index, void *_arg )
103{ 99{
104 /* 100 /*
105 Status *cast = _arg; 101 Status *cast = _arg;
106 102
107 cast->calls[call_index].Callee.status = Cancel;*/ 103 cast->calls[call_index].Callee.status = Cancel;*/
108} 104}
109void callback_call_rejected ( int32_t call_index, void *_arg ) 105void callback_call_rejected ( void* av, int32_t call_index, void *_arg )
110{ 106{
111 Status *cast = _arg; 107 Status *cast = _arg;
112
113 cast->calls[call_index].Caller.status = Rejected; 108 cast->calls[call_index].Caller.status = Rejected;
114} 109}
115void callback_call_ended ( int32_t call_index, void *_arg ) 110void callback_call_ended ( void* av, int32_t call_index, void *_arg )
116{ 111{
117 /* 112 /*
118 Status *cast = _arg; 113 Status *cast = _arg;
@@ -120,12 +115,40 @@ void callback_call_ended ( int32_t call_index, void *_arg )
120 cast->calls[call_index].Callee.status = Ended;*/ 115 cast->calls[call_index].Callee.status = Ended;*/
121} 116}
122 117
123void callback_requ_timeout ( int32_t call_index, void *_arg ) 118void callback_requ_timeout ( void* av, int32_t call_index, void *_arg )
124{ 119{
125 ck_assert_msg(0, "No answer!"); 120 ck_assert_msg(0, "No answer!");
126} 121}
122
123static void callback_audio(ToxAv *av, int32_t call_index, int16_t *data, int length)
124{
125}
126
127static void callback_video(ToxAv *av, int32_t call_index, vpx_image_t *img)
128{
129}
130
131void register_callbacks(ToxAv* av, void* data)
132{
133 toxav_register_callstate_callback(av, callback_call_started, av_OnStart, data);
134 toxav_register_callstate_callback(av, callback_call_canceled, av_OnCancel, data);
135 toxav_register_callstate_callback(av, callback_call_rejected, av_OnReject, data);
136 toxav_register_callstate_callback(av, callback_call_ended, av_OnEnd, data);
137 toxav_register_callstate_callback(av, callback_recv_invite, av_OnInvite, data);
138
139 toxav_register_callstate_callback(av, callback_recv_ringing, av_OnRinging, data);
140 toxav_register_callstate_callback(av, callback_recv_starting, av_OnStarting, data);
141 toxav_register_callstate_callback(av, callback_recv_ending, av_OnEnding, data);
142
143 toxav_register_callstate_callback(av, callback_requ_timeout, av_OnRequestTimeout, data);
144
145
146 toxav_register_audio_recv_callback(av, callback_audio);
147 toxav_register_video_recv_callback(av, callback_video);
148}
127/*************************************************************************************************/ 149/*************************************************************************************************/
128 150
151int call_running[3];
129 152
130void *in_thread_call (void *arg) 153void *in_thread_call (void *arg)
131{ 154{
@@ -133,18 +156,22 @@ void *in_thread_call (void *arg)
133 156
134 ACall *this_call = arg; 157 ACall *this_call = arg;
135 uint64_t start = 0; 158 uint64_t start = 0;
136 int step = 0, running = 1; 159 int step = 0;
137 int call_idx; 160 int call_idx;
138 161
162 call_running[this_call->idx] = 1;
163
139 const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000); 164 const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000);
140 int16_t sample_payload[frame_size]; 165 int16_t sample_payload[frame_size];
141 randombytes((uint8_t *)sample_payload, sizeof(int16_t) * frame_size); 166 randombytes((uint8_t *)sample_payload, sizeof(int16_t) * frame_size);
142 167
143 uint8_t prepared_payload[RTP_PAYLOAD_SIZE]; 168 uint8_t prepared_payload[RTP_PAYLOAD_SIZE];
144 169
145 170 register_callbacks(this_call->Caller.av, &status_control);
171 register_callbacks(this_call->Callee.av, arg);
172
146 /* NOTE: CALLEE WILL ALWAHYS NEED CALL_IDX == 0 */ 173 /* NOTE: CALLEE WILL ALWAHYS NEED CALL_IDX == 0 */
147 while (running) { 174 while (call_running[this_call->idx]) {
148 175
149 switch ( step ) { 176 switch ( step ) {
150 case 0: /* CALLER */ 177 case 0: /* CALLER */
@@ -191,27 +218,15 @@ void *in_thread_call (void *arg)
191 int16_t storage[RTP_PAYLOAD_SIZE]; 218 int16_t storage[RTP_PAYLOAD_SIZE];
192 int recved; 219 int recved;
193 220
194 /* Payload from CALLER */
195 recved = toxav_recv_audio(this_call->Callee.av, 0, frame_size, storage);
196
197 if ( recved ) {
198 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLER is invalid");*/
199 }
200
201 /* Payload from CALLEE */
202 recved = toxav_recv_audio(this_call->Caller.av, call_idx, frame_size, storage);
203
204 if ( recved ) {
205 /*ck_assert_msg(recved == 10 && memcmp(storage, sample_payload, 10) == 0, "Payload from CALLEE is invalid");*/
206 }
207
208 c_sleep(20); 221 c_sleep(20);
209 } 222 }
210 223
211 step++; /* This terminates the loop */ 224 step++; /* This terminates the loop */
212 225
226 pthread_mutex_lock(&muhmutex);
213 toxav_kill_transmission(this_call->Callee.av, 0); 227 toxav_kill_transmission(this_call->Callee.av, 0);
214 toxav_kill_transmission(this_call->Caller.av, call_idx); 228 toxav_kill_transmission(this_call->Caller.av, call_idx);
229 pthread_mutex_unlock(&muhmutex);
215 230
216 /* Call over CALLER hangs up */ 231 /* Call over CALLER hangs up */
217 toxav_hangup(this_call->Caller.av, call_idx); 232 toxav_hangup(this_call->Caller.av, call_idx);
@@ -224,7 +239,7 @@ void *in_thread_call (void *arg)
224 if (this_call->Caller.status == Ended) { 239 if (this_call->Caller.status == Ended) {
225 c_sleep(1000); /* race condition */ 240 c_sleep(1000); /* race condition */
226 this_call->Callee.status = Ended; 241 this_call->Callee.status = Ended;
227 running = 0; 242 call_running[this_call->idx] = 0;
228 } 243 }
229 244
230 break; 245 break;
@@ -306,55 +321,38 @@ START_TEST(test_AV_three_calls)
306 321
307 ToxAv *uniqcallerav = toxav_new(caller, 3); 322 ToxAv *uniqcallerav = toxav_new(caller, 3);
308 323
309 Status status_control = { 324 for (i = 0; i < 3; i ++) {
310 0, 325 status_control.calls[i].idx = i;
311 {none, uniqcallerav, 0}, 326
312 {none, toxav_new(callees[0], 1), 0}, 327 status_control.calls[i].Caller.av = uniqcallerav;
313 328 status_control.calls[i].Caller.id = 0;
314 0, 329 status_control.calls[i].Caller.status= none;
315 {none, uniqcallerav}, 330
316 {none, toxav_new(callees[1], 1), 1}, 331 status_control.calls[i].Callee.av = toxav_new(callees[i], 1);
317 332 status_control.calls[i].Callee.id = i;
318 0, 333 status_control.calls[i].Callee.status= none;
319 {none, uniqcallerav}, 334 }
320 {none, toxav_new(callees[2], 1), 2} 335
321 }; 336 pthread_mutex_init(&muhmutex, NULL);
322
323
324 toxav_register_callstate_callback(callback_call_started, av_OnStart, &status_control);
325 toxav_register_callstate_callback(callback_call_canceled, av_OnCancel, &status_control);
326 toxav_register_callstate_callback(callback_call_rejected, av_OnReject, &status_control);
327 toxav_register_callstate_callback(callback_call_ended, av_OnEnd, &status_control);
328 toxav_register_callstate_callback(callback_recv_invite, av_OnInvite, &status_control);
329
330 toxav_register_callstate_callback(callback_recv_ringing, av_OnRinging, &status_control);
331 toxav_register_callstate_callback(callback_recv_starting, av_OnStarting, &status_control);
332 toxav_register_callstate_callback(callback_recv_ending, av_OnEnding, &status_control);
333
334 toxav_register_callstate_callback(callback_recv_error, av_OnError, &status_control);
335 toxav_register_callstate_callback(callback_requ_timeout, av_OnRequestTimeout, &status_control);
336
337
338 337
339 for ( i = 0; i < 3; i++ ) 338 for ( i = 0; i < 3; i++ )
340 pthread_create(&status_control.calls[i].tid, NULL, in_thread_call, &status_control.calls[i]); 339 pthread_create(&status_control.calls[i].tid, NULL, in_thread_call, &status_control.calls[i]);
341 340
342
343 /* Now start 3 calls and they'll run for 10 s */ 341 /* Now start 3 calls and they'll run for 10 s */
344 342
345 for ( i = 0; i < 3; i++ ) 343 for ( i = 0; i < 3; i++ )
346 pthread_detach(status_control.calls[i].tid); 344 pthread_detach(status_control.calls[i].tid);
347 345
348 while ( 346 while (call_running[0] || call_running[1] || call_running[2]) {
349 status_control.calls[0].Callee.status != Ended && status_control.calls[0].Caller.status != Ended && 347 pthread_mutex_lock(&muhmutex);
350 status_control.calls[1].Callee.status != Ended && status_control.calls[1].Caller.status != Ended && 348
351 status_control.calls[2].Callee.status != Ended && status_control.calls[2].Caller.status != Ended
352 ) {
353 tox_do(bootstrap_node); 349 tox_do(bootstrap_node);
354 tox_do(caller); 350 tox_do(caller);
355 tox_do(callees[0]); 351 tox_do(callees[0]);
356 tox_do(callees[1]); 352 tox_do(callees[1]);
357 tox_do(callees[2]); 353 tox_do(callees[2]);
354
355 pthread_mutex_unlock(&muhmutex);
358 c_sleep(20); 356 c_sleep(20);
359 } 357 }
360 358
@@ -403,4 +401,4 @@ int main(int argc, char *argv[])
403// test_AV_three_calls(); 401// test_AV_three_calls();
404// 402//
405// return 0; 403// return 0;
406} \ No newline at end of file 404}