summaryrefslogtreecommitdiff
path: root/auto_tests/toxav_basic_test.c
diff options
context:
space:
mode:
authorGregory Mullen (grayhatter) <greg@grayhatter.com>2015-11-07 20:36:57 -0800
committerGregory Mullen (grayhatter) <greg@grayhatter.com>2015-11-07 20:36:57 -0800
commite1ad6cc8f9a5613439937096b55b476f65a00730 (patch)
tree9dcf444c993681cc654f0e2874ab66264ec289c6 /auto_tests/toxav_basic_test.c
parent3631b460a6b763acda718bb71b7f6a1ee31a3299 (diff)
parent6a494e2cbdd146bb13185d8220061322661a5f5a (diff)
Merge remote-tracking branch 'upstream/master' into rm-files
Diffstat (limited to 'auto_tests/toxav_basic_test.c')
-rw-r--r--auto_tests/toxav_basic_test.c883
1 files changed, 425 insertions, 458 deletions
diff --git a/auto_tests/toxav_basic_test.c b/auto_tests/toxav_basic_test.c
index af8d91e9..20432dca 100644
--- a/auto_tests/toxav_basic_test.c
+++ b/auto_tests/toxav_basic_test.c
@@ -2,23 +2,31 @@
2#include "config.h" 2#include "config.h"
3#endif 3#endif
4 4
5#ifndef HAVE_LIBCHECK
6# include <assert.h>
7
8# define ck_assert(X) assert(X);
9# define START_TEST(NAME) void NAME ()
10# define END_TEST
11#else
12# include "helpers.h"
13#endif
14
5#include <sys/types.h> 15#include <sys/types.h>
6#include <stdint.h> 16#include <stdint.h>
7#include <string.h> 17#include <string.h>
8#include <stdio.h> 18#include <stdio.h>
9#include <check.h>
10#include <stdlib.h> 19#include <stdlib.h>
11#include <time.h> 20#include <time.h>
12#include <assert.h>
13 21
14#include <vpx/vpx_image.h> 22#include <vpx/vpx_image.h>
15 23
16#include "../toxcore/tox.h" 24#include "../toxcore/tox.h"
25#include "../toxcore/util.h"
17#include "../toxcore/logger.h" 26#include "../toxcore/logger.h"
18#include "../toxcore/crypto_core.h" 27#include "../toxcore/crypto_core.h"
19#include "../toxav/toxav.h" 28#include "../toxav/toxav.h"
20 29
21#include "helpers.h"
22 30
23#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 31#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
24#define c_sleep(x) Sleep(1*x) 32#define c_sleep(x) Sleep(1*x)
@@ -28,604 +36,564 @@
28#endif 36#endif
29 37
30 38
39#define TEST_REGULAR_AV 1
40#define TEST_REGULAR_A 1
41#define TEST_REGULAR_V 1
42#define TEST_REJECT 1
43#define TEST_CANCEL 1
44#define TEST_MUTE_UNMUTE 1
45#define TEST_STOP_RESUME_PAYLOAD 1
46#define TEST_PAUSE_RESUME_SEND 1
31 47
32typedef enum _CallStatus {
33 none,
34 InCall,
35 Ringing,
36 Ended,
37 Rejected,
38 Canceled,
39 TimedOut
40 48
41} CallStatus; 49typedef struct {
50 bool incoming;
51 uint32_t state;
42 52
43typedef struct _Party { 53} CallControl;
44 CallStatus status;
45 ToxAv *av;
46 time_t *CallStarted;
47 int call_index;
48} Party;
49 54
50typedef struct _Status {
51 Party Alice;
52 Party Bob;
53} Status;
54 55
55/* My default settings */ 56/**
56static ToxAvCSettings muhcaps; 57 * Callbacks
57 58 */
58void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) 59void t_toxav_call_cb(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_enabled, void *user_data)
59{ 60{
60 if (length == 7 && memcmp("gentoo", data, 7) == 0) { 61 (void) av;
61 tox_friend_add_norequest(m, public_key, 0); 62 (void) friend_number;
62 } 63 (void) audio_enabled;
63} 64 (void) video_enabled;
64 65
65 66 printf("Handling CALL callback\n");
66/******************************************************************************/ 67 ((CallControl *)user_data)->incoming = true;
67void callback_recv_invite ( void *av, int32_t call_index, void *_arg )
68{
69 Status *cast = _arg;
70
71 if (cast->Alice.av == av) {
72 // ...
73 } else if (cast->Bob.av == av) {
74 /* Bob always receives invite */
75 cast->Bob.status = Ringing;
76 cast->Bob.call_index = call_index;
77 }
78} 68}
79void callback_recv_ringing ( void *av, int32_t call_index, void *_arg ) 69void t_toxav_call_state_cb(ToxAV *av, uint32_t friend_number, uint32_t state, void *user_data)
80{ 70{
81 Status *cast = _arg; 71 (void) av;
72 (void) friend_number;
82 73
83 if (cast->Alice.av == av) { 74 printf("Handling CALL STATE callback: %d\n", state);
84 /* Alice always sends invite */ 75 ((CallControl *)user_data)->state = state;
85 cast->Alice.status = Ringing;
86 } else if (cast->Bob.av == av) {
87 // ...
88 }
89} 76}
90 77void t_toxav_receive_video_frame_cb(ToxAV *av, uint32_t friend_number,
91 78 uint16_t width, uint16_t height,
92void callback_call_started ( void *av, int32_t call_index, void *_arg ) 79 uint8_t const *y, uint8_t const *u, uint8_t const *v,
80 int32_t ystride, int32_t ustride, int32_t vstride,
81 void *user_data)
93{ 82{
94 Status *cast = _arg; 83 (void) av;
95 84 (void) friend_number;
96 if (cast->Alice.av == av) { 85 (void) width;
97 printf("Call started on Alices side...\n"); 86 (void) height;
98 cast->Alice.status = InCall; 87 (void) y;
99 toxav_prepare_transmission(av, call_index, 1); 88 (void) u;
100 } else if (cast->Bob.av == av) { 89 (void) v;
101 printf("Call started on Bob side...\n"); 90 (void) ystride;
102 cast->Bob.status = InCall; 91 (void) ustride;
103 toxav_prepare_transmission(av, call_index, 1); 92 (void) vstride;
104 } 93 (void) user_data;
94 printf("Received video payload\n");
105} 95}
106void callback_call_canceled ( void *av, int32_t call_index, void *_arg ) 96void t_toxav_receive_audio_frame_cb(ToxAV *av, uint32_t friend_number,
97 int16_t const *pcm,
98 size_t sample_count,
99 uint8_t channels,
100 uint32_t sampling_rate,
101 void *user_data)
107{ 102{
108 Status *cast = _arg; 103 (void) av;
109 104 (void) friend_number;
110 if (cast->Alice.av == av) { 105 (void) pcm;
111 // ... 106 (void) sample_count;
112 } else if (cast->Bob.av == av) { 107 (void) channels;
113 printf ( "Call Canceled for Bob!\n" ); 108 (void) sampling_rate;
114 cast->Bob.status = Canceled; 109 (void) user_data;
115 } 110 printf("Received audio payload\n");
116} 111}
117void callback_call_rejected ( void *av, int32_t call_index, void *_arg ) 112void t_accept_friend_request_cb(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
118{ 113{
119 Status *cast = _arg; 114 (void) userdata;
120 115
121 printf ( "Call rejected by Bob!\n" 116 if (length == 7 && memcmp("gentoo", data, 7) == 0) {
122 "Call ended for Alice!\n" ); 117 ck_assert(tox_friend_add_norequest(m, public_key, NULL) != (uint32_t) ~0);
123
124 /* If Bob rejects, call is ended for alice and she sends ending */
125 if (cast->Alice.av == av) {
126 cast->Alice.status = Rejected;
127 } else if (cast->Bob.av == av) {
128 //... ignor
129 }
130}
131void callback_call_ended ( void *av, int32_t call_index, void *_arg )
132{
133 Status *cast = _arg;
134
135 if (cast->Alice.av == av) {
136 printf ( "Call ended for Alice!\n" );
137 cast->Alice.status = Ended;
138 } else if (cast->Bob.av == av) {
139 printf ( "Call ended for Bob!\n" );
140 cast->Bob.status = Ended;
141 } 118 }
142} 119}
143 120
144void callback_peer_cs_change ( void *av, int32_t call_index, void *_arg )
145{
146 ToxAvCSettings csettings;
147 toxav_get_peer_csettings(av, call_index, 0, &csettings);
148
149 printf("Peer changing settings to: \n"
150 "Type: %u \n"
151 "Video bitrate: %u \n"
152 "Video height: %u \n"
153 "Video width: %u \n"
154 "Audio bitrate: %u \n"
155 "Audio framedur: %u \n"
156 "Audio sample rate: %u \n"
157 "Audio channels: %u \n",
158 csettings.call_type,
159 csettings.video_bitrate,
160 csettings.max_video_height,
161 csettings.max_video_width,
162 csettings.audio_bitrate,
163 csettings.audio_frame_duration,
164 csettings.audio_sample_rate,
165 csettings.audio_channels
166 );
167}
168
169void callback_self_cs_change ( void *av, int32_t call_index, void *_arg )
170{
171 ToxAvCSettings csettings;
172 toxav_get_peer_csettings(av, call_index, 0, &csettings);
173
174 printf("Changed settings to: \n"
175 "Type: %u \n"
176 "Video bitrate: %u \n"
177 "Video height: %u \n"
178 "Video width: %u \n"
179 "Audio bitrate: %u \n"
180 "Audio framedur: %u \n"
181 "Audio sample rate: %u \n"
182 "Audio channels: %u \n",
183 csettings.call_type,
184 csettings.video_bitrate,
185 csettings.max_video_height,
186 csettings.max_video_width,
187 csettings.audio_bitrate,
188 csettings.audio_frame_duration,
189 csettings.audio_sample_rate,
190 csettings.audio_channels
191 );
192}
193 121
194void callback_requ_timeout ( void *av, int32_t call_index, void *_arg ) 122/**
123 * Iterate helper
124 */
125int iterate_tox(Tox *bootstrap, Tox *Alice, Tox *Bob)
195{ 126{
196 Status *cast = _arg; 127 tox_iterate(bootstrap);
197 printf("Call timed-out!\n"); 128 tox_iterate(Alice);
129 tox_iterate(Bob);
198 130
199 if (cast->Alice.av == av) { 131 return MIN(tox_iteration_interval(Alice), tox_iteration_interval(Bob));
200 cast->Alice.status = TimedOut;
201 } else if (cast->Bob.av == av) {
202 cast->Bob.status = TimedOut;
203 }
204} 132}
205 133
206void callback_audio (void *agent, int32_t call_idx, const int16_t *PCM, uint16_t size, void *data)
207{}
208 134
209void callback_video (void *agent, int32_t call_idx, const vpx_image_t *img, void *data)
210{}
211 135
212void register_callbacks(ToxAv *av, void *data) 136START_TEST(test_AV_flows)
213{ 137{
214 toxav_register_callstate_callback(av, callback_call_started, av_OnStart, data); 138 Tox *Alice, *Bob, *bootstrap;
215 toxav_register_callstate_callback(av, callback_call_canceled, av_OnCancel, data); 139 ToxAV *AliceAV, *BobAV;
216 toxav_register_callstate_callback(av, callback_call_rejected, av_OnReject, data);
217 toxav_register_callstate_callback(av, callback_call_ended, av_OnEnd, data);
218 toxav_register_callstate_callback(av, callback_recv_invite, av_OnInvite, data);
219 toxav_register_callstate_callback(av, callback_recv_ringing, av_OnRinging, data);
220 toxav_register_callstate_callback(av, callback_requ_timeout, av_OnRequestTimeout, data);
221 toxav_register_callstate_callback(av, callback_peer_cs_change, av_OnPeerCSChange, data);
222 toxav_register_callstate_callback(av, callback_self_cs_change, av_OnSelfCSChange, data);
223 toxav_register_audio_callback(av, callback_audio, NULL);
224 toxav_register_video_callback(av, callback_video, NULL);
225}
226 140
141 CallControl AliceCC, BobCC;
227 142
228/*************************************************************************************************/ 143 {
144 TOX_ERR_NEW error;
229 145
230/* Alice calls bob and the call starts. 146 bootstrap = tox_new(NULL, &error);
231 * What happens during the call is defined after. To quit the loop use: step++; 147 ck_assert(error == TOX_ERR_NEW_OK);
232 */
233#define CALL_AND_START_LOOP(AliceCallType, BobCallType) \
234{ int step = 0, running = 1; while (running) {\
235 tox_iterate(bootstrap_node); tox_iterate(Alice); tox_iterate(Bob); \
236 toxav_do(status_control.Bob.av); toxav_do(status_control.Alice.av); \
237 switch ( step ) {\
238 case 0: /* Alice */ printf("Alice is calling...\n");\
239 toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, &muhcaps, 10); step++; break;\
240 case 1: /* Bob */ if (status_control.Bob.status == Ringing) { printf("Bob answers...\n");\
241 cur_time = time(NULL); toxav_answer(status_control.Bob.av, status_control.Bob.call_index, &muhcaps); step++; } break; \
242 case 2: /* Rtp transmission */ \
243 if (status_control.Bob.status == InCall && status_control.Alice.status == InCall)
244
245
246#define TERMINATE_SCOPE() break;\
247case 3: /* Wait for Both to have status ended */\
248if (status_control.Alice.status == Ended && status_control.Bob.status == Ended) running = 0; break; } c_sleep(20); } } printf("\n");
249 148
250START_TEST(test_AV_flows) 149 Alice = tox_new(NULL, &error);
251{ 150 ck_assert(error == TOX_ERR_NEW_OK);
252 long long unsigned int cur_time = time(NULL);
253 Tox *bootstrap_node = tox_new(0, 0);
254 Tox *Alice = tox_new(0, 0);
255 Tox *Bob = tox_new(0, 0);
256 151
257 ck_assert_msg(bootstrap_node || Alice || Bob, "Failed to create 3 tox instances"); 152 Bob = tox_new(NULL, &error);
153 ck_assert(error == TOX_ERR_NEW_OK);
154 }
155
156 printf("Created 3 instances of Tox\n");
157 printf("Preparing network...\n");
158 long long unsigned int cur_time = time(NULL);
258 159
259 uint32_t to_compare = 974536; 160 uint32_t to_compare = 974536;
260 tox_callback_friend_request(Alice, accept_friend_request, &to_compare);
261 uint8_t address[TOX_ADDRESS_SIZE]; 161 uint8_t address[TOX_ADDRESS_SIZE];
162
163 tox_callback_friend_request(Alice, t_accept_friend_request_cb, &to_compare);
262 tox_self_get_address(Alice, address); 164 tox_self_get_address(Alice, address);
263 uint32_t test = tox_friend_add(Bob, address, (uint8_t *)"gentoo", 7, 0);
264 165
265 ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); 166
167 ck_assert(tox_friend_add(Bob, address, (uint8_t *)"gentoo", 7, NULL) != (uint32_t) ~0);
266 168
267 uint8_t off = 1; 169 uint8_t off = 1;
268 170
269 while (1) { 171 while (1) {
270 tox_iterate(bootstrap_node); 172 iterate_tox(bootstrap, Alice, Bob);
271 tox_iterate(Alice);
272 tox_iterate(Bob);
273 173
274 if (tox_self_get_connection_status(bootstrap_node) && tox_self_get_connection_status(Alice) 174 if (tox_self_get_connection_status(bootstrap) &&
275 && tox_self_get_connection_status(Bob) 175 tox_self_get_connection_status(Alice) &&
276 && off) { 176 tox_self_get_connection_status(Bob) && off) {
277 printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time); 177 printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time);
278 off = 0; 178 off = 0;
279 } 179 }
280 180
281 if (tox_friend_get_connection_status(Alice, 0, 0) && tox_friend_get_connection_status(Bob, 0, 0)) 181 if (tox_friend_get_connection_status(Alice, 0, NULL) == TOX_CONNECTION_UDP &&
182 tox_friend_get_connection_status(Bob, 0, NULL) == TOX_CONNECTION_UDP)
282 break; 183 break;
283 184
284 c_sleep(20); 185 c_sleep(20);
285 } 186 }
286 187
287 printf("All set after %llu seconds! Starting call...\n", time(NULL) - cur_time);
288 188
289 muhcaps = av_DefaultSettings; 189 {
290 muhcaps.max_video_height = muhcaps.max_video_width = 128; 190 TOXAV_ERR_NEW error;
291 191 AliceAV = toxav_new(Alice, &error);
292 Status status_control = { 192 ck_assert(error == TOXAV_ERR_NEW_OK);
293 {none, toxav_new(Alice, 1), NULL, -1},
294 {none, toxav_new(Bob, 1), NULL, -1},
295 };
296
297 ck_assert_msg(status_control.Alice.av || status_control.Bob.av, "Failed to create 2 toxav instances");
298
299
300 register_callbacks(status_control.Alice.av, &status_control);
301 register_callbacks(status_control.Bob.av, &status_control);
302
303 const int frame_size = (av_DefaultSettings.audio_sample_rate * av_DefaultSettings.audio_frame_duration / 1000);
304 int16_t sample_payload[frame_size];
305 randombytes((uint8_t *)sample_payload, sizeof(int16_t) * frame_size);
306 193
307 uint8_t prepared_payload[RTP_PAYLOAD_SIZE]; 194 BobAV = toxav_new(Bob, &error);
308 int payload_size; 195 ck_assert(error == TOXAV_ERR_NEW_OK);
196 }
309 197
310 vpx_image_t *sample_image = vpx_img_alloc(NULL, VPX_IMG_FMT_I420, 128, 128, 1); 198 toxav_callback_call(AliceAV, t_toxav_call_cb, &AliceCC);
199 toxav_callback_call_state(AliceAV, t_toxav_call_state_cb, &AliceCC);
200 toxav_callback_video_receive_frame(AliceAV, t_toxav_receive_video_frame_cb, &AliceCC);
201 toxav_callback_audio_receive_frame(AliceAV, t_toxav_receive_audio_frame_cb, &AliceCC);
202
203 toxav_callback_call(BobAV, t_toxav_call_cb, &BobCC);
204 toxav_callback_call_state(BobAV, t_toxav_call_state_cb, &BobCC);
205 toxav_callback_video_receive_frame(BobAV, t_toxav_receive_video_frame_cb, &BobCC);
206 toxav_callback_audio_receive_frame(BobAV, t_toxav_receive_audio_frame_cb, &BobCC);
207
208 printf("Created 2 instances of ToxAV\n");
209 printf("All set after %llu seconds!\n", time(NULL) - cur_time);
210
211
212#define REGULAR_CALL_FLOW(A_BR, V_BR) \
213 do { \
214 memset(&AliceCC, 0, sizeof(CallControl)); \
215 memset(&BobCC, 0, sizeof(CallControl)); \
216 \
217 TOXAV_ERR_CALL rc; \
218 toxav_call(AliceAV, 0, A_BR, V_BR, &rc); \
219 \
220 if (rc != TOXAV_ERR_CALL_OK) { \
221 printf("toxav_call failed: %d\n", rc); \
222 ck_assert(0); \
223 } \
224 \
225 \
226 long long unsigned int start_time = time(NULL); \
227 \
228 \
229 while (BobCC.state != TOXAV_FRIEND_CALL_STATE_FINISHED) { \
230 \
231 if (BobCC.incoming) { \
232 TOXAV_ERR_ANSWER rc; \
233 toxav_answer(BobAV, 0, A_BR, V_BR, &rc); \
234 \
235 if (rc != TOXAV_ERR_ANSWER_OK) { \
236 printf("toxav_answer failed: %d\n", rc); \
237 ck_assert(0); \
238 } \
239 BobCC.incoming = false; \
240 } else { \
241 /* TODO rtp */ \
242 \
243 if (time(NULL) - start_time >= 1) { \
244 \
245 TOXAV_ERR_CALL_CONTROL rc; \
246 toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc); \
247 \
248 if (rc != TOXAV_ERR_CALL_CONTROL_OK) { \
249 printf("toxav_call_control failed: %d\n", rc); \
250 ck_assert(0); \
251 } \
252 } \
253 } \
254 \
255 iterate_tox(bootstrap, Alice, Bob); \
256 } \
257 printf("Success!\n");\
258 } while(0)
259
260 if (TEST_REGULAR_AV) {
261 printf("\nTrying regular call (Audio and Video)...\n");
262 REGULAR_CALL_FLOW(48, 4000);
263 }
311 264
312 memcpy(sample_image->planes[VPX_PLANE_Y], sample_payload, 10); 265 if (TEST_REGULAR_A) {
313 memcpy(sample_image->planes[VPX_PLANE_U], sample_payload, 10); 266 printf("\nTrying regular call (Audio only)...\n");
314 memcpy(sample_image->planes[VPX_PLANE_V], sample_payload, 10); 267 REGULAR_CALL_FLOW(48, 0);
268 }
315 269
270 if (TEST_REGULAR_V) {
271 printf("\nTrying regular call (Video only)...\n");
272 REGULAR_CALL_FLOW(0, 4000);
273 }
316 274
317 /************************************************************************************************* 275#undef REGULAR_CALL_FLOW
318 * Successful flows (when call starts)
319 */
320 276
321 /* 277 if (TEST_REJECT) { /* Alice calls; Bob rejects */
322 * Call with audio only on both sides. Alice calls Bob. 278 printf("\nTrying reject flow...\n");
323 */
324 279
280 memset(&AliceCC, 0, sizeof(CallControl));
281 memset(&BobCC, 0, sizeof(CallControl));
325 282
326 CALL_AND_START_LOOP(TypeAudio, TypeAudio) { 283 {
327 /* Both send */ 284 TOXAV_ERR_CALL rc;
328 payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 285 toxav_call(AliceAV, 0, 48, 0, &rc);
329 1000, sample_payload, frame_size);
330 286
331 if ( payload_size < 0 ) { 287 if (rc != TOXAV_ERR_CALL_OK) {
332 ck_assert_msg ( 0, "Failed to encode payload" ); 288 printf("toxav_call failed: %d\n", rc);
289 ck_assert(0);
290 }
333 } 291 }
334 292
335 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); 293 while (!BobCC.incoming)
294 iterate_tox(bootstrap, Alice, Bob);
336 295
337 payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, 296 /* Reject */
338 sample_payload, frame_size); 297 {
298 TOXAV_ERR_CALL_CONTROL rc;
299 toxav_call_control(BobAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc);
339 300
340 if ( payload_size < 0 ) { 301 if (rc != TOXAV_ERR_CALL_CONTROL_OK) {
341 ck_assert_msg ( 0, "Failed to encode payload" ); 302 printf("toxav_call_control failed: %d\n", rc);
303 ck_assert(0);
304 }
342 } 305 }
343 306
344 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); 307 while (AliceCC.state != TOXAV_FRIEND_CALL_STATE_FINISHED)
345 308 iterate_tox(bootstrap, Alice, Bob);
346 if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */
347 step++; /* This terminates the loop */
348 toxav_kill_transmission(status_control.Alice.av, status_control.Alice.call_index);
349 toxav_kill_transmission(status_control.Bob.av, status_control.Bob.call_index);
350 309
351 /* Call over Alice hangs up */ 310 printf("Success!\n");
352 toxav_hangup(status_control.Alice.av, status_control.Alice.call_index);
353 }
354 } 311 }
355 TERMINATE_SCOPE()
356
357 312
358 /* 313 if (TEST_CANCEL) { /* Alice calls; Alice cancels while ringing */
359 * Call with audio on both sides and video on one side. Alice calls Bob. 314 printf("\nTrying cancel (while ringing) flow...\n");
360 */
361 CALL_AND_START_LOOP(TypeAudio, TypeVideo) {
362 /* Both send */
363 payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload,
364 1000, sample_payload, frame_size);
365
366 if ( payload_size < 0 ) {
367 ck_assert_msg ( 0, "Failed to encode payload" );
368 }
369 315
370 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); 316 memset(&AliceCC, 0, sizeof(CallControl));
317 memset(&BobCC, 0, sizeof(CallControl));
371 318
372 payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, 319 {
373 sample_payload, frame_size); 320 TOXAV_ERR_CALL rc;
321 toxav_call(AliceAV, 0, 48, 0, &rc);
374 322
375 if ( payload_size < 0 ) { 323 if (rc != TOXAV_ERR_CALL_OK) {
376 ck_assert_msg ( 0, "Failed to encode payload" ); 324 printf("toxav_call failed: %d\n", rc);
325 ck_assert(0);
326 }
377 } 327 }
378 328
379 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); 329 while (!BobCC.incoming)
380 330 iterate_tox(bootstrap, Alice, Bob);
381// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image);
382 331
383 if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */ 332 /* Cancel */
384 step++; /* This terminates the loop */ 333 {
385 toxav_kill_transmission(status_control.Alice.av, status_control.Alice.call_index); 334 TOXAV_ERR_CALL_CONTROL rc;
386 toxav_kill_transmission(status_control.Bob.av, status_control.Bob.call_index); 335 toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc);
387 336
388 /* Call over Alice hangs up */ 337 if (rc != TOXAV_ERR_CALL_CONTROL_OK) {
389 toxav_hangup(status_control.Alice.av, status_control.Alice.call_index); 338 printf("toxav_call_control failed: %d\n", rc);
339 ck_assert(0);
340 }
390 } 341 }
391 }
392 TERMINATE_SCOPE()
393
394 342
395 /* 343 /* Alice will not receive end state */
396 * Call with audio and video on both sides. Alice calls Bob. 344 while (BobCC.state != TOXAV_FRIEND_CALL_STATE_FINISHED)
397 */ 345 iterate_tox(bootstrap, Alice, Bob);
398 CALL_AND_START_LOOP(TypeVideo, TypeVideo) {
399 /* Both send */
400 346
401 payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 347 printf("Success!\n");
402 1000, sample_payload, frame_size); 348 }
403 349
404 if ( payload_size < 0 ) { 350 if (TEST_MUTE_UNMUTE) { /* Check Mute-Unmute etc */
405 ck_assert_msg ( 0, "Failed to encode payload" ); 351 printf("\nTrying mute functionality...\n");
406 }
407 352
408 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); 353 memset(&AliceCC, 0, sizeof(CallControl));
354 memset(&BobCC, 0, sizeof(CallControl));
409 355
410 payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, 356 /* Assume sending audio and video */
411 sample_payload, frame_size); 357 {
358 TOXAV_ERR_CALL rc;
359 toxav_call(AliceAV, 0, 48, 1000, &rc);
412 360
413 if ( payload_size < 0 ) { 361 if (rc != TOXAV_ERR_CALL_OK) {
414 ck_assert_msg ( 0, "Failed to encode payload" ); 362 printf("toxav_call failed: %d\n", rc);
363 ck_assert(0);
364 }
415 } 365 }
416 366
417 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); 367 while (!BobCC.incoming)
418 368 iterate_tox(bootstrap, Alice, Bob);
419// toxav_send_video(status_control.Alice.av, status_control.Alice.call_index, sample_image);
420// toxav_send_video(status_control.Bob.av, status_control.Bob.call_index, sample_image);
421 369
370 /* At first try all stuff while in invalid state */
371 ck_assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_PAUSE, NULL));
372 ck_assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_RESUME, NULL));
373 ck_assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_MUTE_AUDIO, NULL));
374 ck_assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_UNMUTE_AUDIO, NULL));
375 ck_assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_HIDE_VIDEO, NULL));
376 ck_assert(!toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_SHOW_VIDEO, NULL));
422 377
423 if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */ 378 {
424 step++; /* This terminates the loop */ 379 TOXAV_ERR_ANSWER rc;
425 toxav_kill_transmission(status_control.Alice.av, status_control.Alice.call_index); 380 toxav_answer(BobAV, 0, 48, 4000, &rc);
426 toxav_kill_transmission(status_control.Bob.av, status_control.Bob.call_index);
427 381
428 /* Call over Alice hangs up */ 382 if (rc != TOXAV_ERR_ANSWER_OK) {
429 toxav_hangup(status_control.Alice.av, status_control.Alice.call_index); 383 printf("toxav_answer failed: %d\n", rc);
384 ck_assert(0);
385 }
430 } 386 }
431 }
432 TERMINATE_SCOPE()
433
434 387
435 uint64_t times_they_are_a_changin = time(NULL); 388 iterate_tox(bootstrap, Alice, Bob);
436 /* Media change */ 389
437 CALL_AND_START_LOOP(TypeAudio, TypeAudio) { 390 /* Pause and Resume */
438 /* Both send */ 391 printf("Pause and Resume\n");
439 payload_size = toxav_prepare_audio_frame(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, 392 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_PAUSE, NULL));
440 1000, sample_payload, frame_size); 393 iterate_tox(bootstrap, Alice, Bob);
441 394 ck_assert(BobCC.state == 0);
442 if ( payload_size < 0 ) { 395 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_RESUME, NULL));
443 ck_assert_msg ( 0, "Failed to encode payload" ); 396 iterate_tox(bootstrap, Alice, Bob);
397 ck_assert(BobCC.state & (TOXAV_FRIEND_CALL_STATE_SENDING_A | TOXAV_FRIEND_CALL_STATE_SENDING_V));
398
399 /* Mute/Unmute single */
400 printf("Mute/Unmute single\n");
401 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_MUTE_AUDIO, NULL));
402 iterate_tox(bootstrap, Alice, Bob);
403 ck_assert(BobCC.state ^ TOXAV_FRIEND_CALL_STATE_ACCEPTING_A);
404 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_UNMUTE_AUDIO, NULL));
405 iterate_tox(bootstrap, Alice, Bob);
406 ck_assert(BobCC.state & TOXAV_FRIEND_CALL_STATE_ACCEPTING_A);
407
408 /* Mute/Unmute both */
409 printf("Mute/Unmute both\n");
410 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_MUTE_AUDIO, NULL));
411 iterate_tox(bootstrap, Alice, Bob);
412 ck_assert(BobCC.state ^ TOXAV_FRIEND_CALL_STATE_ACCEPTING_A);
413 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_HIDE_VIDEO, NULL));
414 iterate_tox(bootstrap, Alice, Bob);
415 ck_assert(BobCC.state ^ TOXAV_FRIEND_CALL_STATE_ACCEPTING_V);
416 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_UNMUTE_AUDIO, NULL));
417 iterate_tox(bootstrap, Alice, Bob);
418 ck_assert(BobCC.state & TOXAV_FRIEND_CALL_STATE_ACCEPTING_A);
419 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_SHOW_VIDEO, NULL));
420 iterate_tox(bootstrap, Alice, Bob);
421 ck_assert(BobCC.state & TOXAV_FRIEND_CALL_STATE_ACCEPTING_V);
422
423 {
424 TOXAV_ERR_CALL_CONTROL rc;
425 toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc);
426
427 if (rc != TOXAV_ERR_CALL_CONTROL_OK) {
428 printf("toxav_call_control failed: %d\n", rc);
429 ck_assert(0);
430 }
444 } 431 }
445 432
446 toxav_send_audio(status_control.Alice.av, status_control.Alice.call_index, prepared_payload, payload_size); 433 iterate_tox(bootstrap, Alice, Bob);
434 ck_assert(BobCC.state == TOXAV_FRIEND_CALL_STATE_FINISHED);
447 435
448 payload_size = toxav_prepare_audio_frame(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, 1000, 436 printf("Success!\n");
449 sample_payload, frame_size); 437 }
450 438
451 if ( payload_size < 0 ) { 439 if (TEST_STOP_RESUME_PAYLOAD) { /* Stop and resume audio/video payload */
452 ck_assert_msg ( 0, "Failed to encode payload" ); 440 printf("\nTrying stop/resume functionality...\n");
453 }
454 441
455 toxav_send_audio(status_control.Bob.av, status_control.Bob.call_index, prepared_payload, payload_size); 442 memset(&AliceCC, 0, sizeof(CallControl));
443 memset(&BobCC, 0, sizeof(CallControl));
456 444
457 /* Wait 2 seconds and change transmission type */ 445 /* Assume sending audio and video */
458 if (time(NULL) - times_they_are_a_changin > 2) { 446 {
459 times_they_are_a_changin = time(NULL); 447 TOXAV_ERR_CALL rc;
460 muhcaps.audio_bitrate ++; 448 toxav_call(AliceAV, 0, 48, 0, &rc);
461 toxav_change_settings(status_control.Alice.av, status_control.Alice.call_index, &muhcaps);
462 }
463 449
464 if (time(NULL) - cur_time > 10) { /* Transmit for 10 seconds */ 450 if (rc != TOXAV_ERR_CALL_OK) {
465 step++; /* This terminates the loop */ 451 printf("toxav_call failed: %d\n", rc);
466 toxav_kill_transmission(status_control.Alice.av, status_control.Alice.call_index); 452 ck_assert(0);
467 toxav_kill_transmission(status_control.Bob.av, status_control.Bob.call_index); 453 }
468
469 /* Call over Alice hangs up */
470 toxav_hangup(status_control.Alice.av, status_control.Alice.call_index);
471 } 454 }
472 }
473 TERMINATE_SCOPE()
474 455
456 while (!BobCC.incoming)
457 iterate_tox(bootstrap, Alice, Bob);
475 458
476 /************************************************************************************************* 459 {
477 * Other flows 460 TOXAV_ERR_ANSWER rc;
478 */ 461 toxav_answer(BobAV, 0, 48, 0, &rc);
479 462
480 /* 463 if (rc != TOXAV_ERR_ANSWER_OK) {
481 * Call and reject 464 printf("toxav_answer failed: %d\n", rc);
482 */ 465 ck_assert(0);
483 {
484 int step = 0;
485 int running = 1;
486
487 while (running) {
488 tox_iterate(bootstrap_node);
489 tox_iterate(Alice);
490 tox_iterate(Bob);
491
492 switch ( step ) {
493 case 0: /* Alice */
494 printf("Alice is calling...\n");
495 toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, &muhcaps, 10);
496 step++;
497 break;
498
499 case 1: /* Bob */
500 if (status_control.Bob.status == Ringing) {
501 printf("Bob rejects...\n");
502 toxav_reject(status_control.Bob.av, status_control.Bob.call_index, "Who likes D's anyway?");
503 step++;
504 }
505
506 break;
507
508 case 2: /* Wait for Both to have status ended */
509 if (status_control.Alice.status == Rejected && status_control.Bob.status == Ended) running = 0;
510
511 break;
512 } 466 }
513
514 c_sleep(20);
515 } 467 }
516 468
517 printf("\n"); 469 iterate_tox(bootstrap, Alice, Bob);
518 }
519
520
521 /*
522 * Call and cancel
523 */
524 {
525 int step = 0;
526 int running = 1;
527
528 while (running) {
529 tox_iterate(bootstrap_node);
530 tox_iterate(Alice);
531 tox_iterate(Bob);
532 470
533 toxav_do(status_control.Alice.av); 471 printf("Call started as audio only\n");
534 toxav_do(status_control.Bob.av); 472 printf("Turning on video for Alice...\n");
473 ck_assert(toxav_bit_rate_set(AliceAV, 0, -1, 1000, NULL));
535 474
475 iterate_tox(bootstrap, Alice, Bob);
476 ck_assert(BobCC.state & TOXAV_FRIEND_CALL_STATE_SENDING_V);
536 477
537 switch ( step ) { 478 printf("Turning off video for Alice...\n");
538 case 0: /* Alice */ 479 ck_assert(toxav_bit_rate_set(AliceAV, 0, -1, 0, NULL));
539 printf("Alice is calling...\n");
540 toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, &muhcaps, 10);
541 step++;
542 break;
543 480
481 iterate_tox(bootstrap, Alice, Bob);
482 ck_assert(!(BobCC.state & TOXAV_FRIEND_CALL_STATE_SENDING_V));
544 483
545 case 1: /* Alice again */ 484 printf("Turning off audio for Alice...\n");
546 if (status_control.Bob.status == Ringing) { 485 ck_assert(toxav_bit_rate_set(AliceAV, 0, 0, -1, NULL));
547 printf("Alice cancels...\n");
548 toxav_cancel(status_control.Alice.av, status_control.Alice.call_index, 0, "Who likes D's anyway?");
549 step++;
550 }
551 486
552 break; 487 iterate_tox(bootstrap, Alice, Bob);
488 ck_assert(!(BobCC.state & TOXAV_FRIEND_CALL_STATE_SENDING_A));
553 489
554 case 2: /* Wait for Both to have status ended */ 490 {
555 if (status_control.Bob.status == Canceled) running = 0; 491 TOXAV_ERR_CALL_CONTROL rc;
492 toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc);
556 493
557 break; 494 if (rc != TOXAV_ERR_CALL_CONTROL_OK) {
495 printf("toxav_call_control failed: %d\n", rc);
496 ck_assert(0);
558 } 497 }
559
560 c_sleep(20);
561 } 498 }
562 499
563 printf("\n"); 500 iterate_tox(bootstrap, Alice, Bob);
501 ck_assert(BobCC.state == TOXAV_FRIEND_CALL_STATE_FINISHED);
502
503 printf("Success!\n");
564 } 504 }
565 505
566 /* 506 if (TEST_PAUSE_RESUME_SEND) { /* Stop and resume audio/video payload and test send options */
567 * Timeout 507 printf("\nTrying stop/resume functionality...\n");
568 */
569 {
570 int step = 0;
571 int running = 1;
572 508
573 while (running) { 509 memset(&AliceCC, 0, sizeof(CallControl));
574 tox_iterate(bootstrap_node); 510 memset(&BobCC, 0, sizeof(CallControl));
575 tox_iterate(Alice);
576 tox_iterate(Bob);
577 511
578 toxav_do(status_control.Alice.av); 512 /* Assume sending audio and video */
579 toxav_do(status_control.Bob.av); 513 {
514 TOXAV_ERR_CALL rc;
515 toxav_call(AliceAV, 0, 48, 0, &rc);
580 516
581 switch ( step ) { 517 if (rc != TOXAV_ERR_CALL_OK) {
582 case 0: 518 printf("toxav_call failed: %d\n", rc);
583 printf("Alice is calling...\n"); 519 ck_assert(0);
584 toxav_call(status_control.Alice.av, &status_control.Alice.call_index, 0, &muhcaps, 10); 520 }
585 step++; 521 }
586 break;
587 522
588 case 1: 523 while (!BobCC.incoming)
589 if (status_control.Alice.status == TimedOut) running = 0; 524 iterate_tox(bootstrap, Alice, Bob);
590 525
591 break; 526 {
527 TOXAV_ERR_ANSWER rc;
528 toxav_answer(BobAV, 0, 48, 0, &rc);
529
530 if (rc != TOXAV_ERR_ANSWER_OK) {
531 printf("toxav_answer failed: %d\n", rc);
532 ck_assert(0);
592 } 533 }
534 }
593 535
594 c_sleep(20); 536 int16_t PCM[5670];
537
538 iterate_tox(bootstrap, Alice, Bob);
539 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_PAUSE, NULL));
540 iterate_tox(bootstrap, Alice, Bob);
541 ck_assert(!toxav_audio_send_frame(AliceAV, 0, PCM, 960, 1, 48000, NULL));
542 ck_assert(!toxav_audio_send_frame(BobAV, 0, PCM, 960, 1, 48000, NULL));
543 ck_assert(toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_RESUME, NULL));
544 iterate_tox(bootstrap, Alice, Bob);
545 ck_assert(toxav_audio_send_frame(AliceAV, 0, PCM, 960, 1, 48000, NULL));
546 ck_assert(toxav_audio_send_frame(BobAV, 0, PCM, 960, 1, 48000, NULL));
547 iterate_tox(bootstrap, Alice, Bob);
548
549 {
550 TOXAV_ERR_CALL_CONTROL rc;
551 toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc);
552
553 if (rc != TOXAV_ERR_CALL_CONTROL_OK) {
554 printf("toxav_call_control failed: %d\n", rc);
555 ck_assert(0);
556 }
595 } 557 }
596 558
597 printf("\n"); 559 iterate_tox(bootstrap, Alice, Bob);
560 ck_assert(BobCC.state == TOXAV_FRIEND_CALL_STATE_FINISHED);
561
562 printf("Success!\n");
598 } 563 }
599 564
600 vpx_img_free(sample_image); 565 toxav_kill(BobAV);
601 toxav_kill(status_control.Alice.av); 566 toxav_kill(AliceAV);
602 toxav_kill(status_control.Bob.av);
603 tox_kill(bootstrap_node);
604 tox_kill(Alice);
605 tox_kill(Bob); 567 tox_kill(Bob);
568 tox_kill(Alice);
569 tox_kill(bootstrap);
606 570
607 printf("Calls ended!\n"); 571 printf("\nTest successful!\n");
608} 572}
609END_TEST 573END_TEST
610 574
611/*************************************************************************************************/ 575#ifndef HAVE_LIBCHECK
612 576int main(int argc, char *argv[])
613 577{
614/*************************************************************************************************/ 578 (void) argc;
615 579 (void) argv;
616/*************************************************************************************************/
617
618 580
581 test_AV_flows();
582 return 0;
583}
584#else
619Suite *tox_suite(void) 585Suite *tox_suite(void)
620{ 586{
621 Suite *s = suite_create("ToxAV"); 587 Suite *s = suite_create("ToxAV");
622 588
623 DEFTESTCASE_SLOW(AV_flows, 200); 589 DEFTESTCASE_SLOW(AV_flows, 200);
624
625 return s; 590 return s;
626} 591}
627int main(int argc, char *argv[]) 592int main(int argc, char *argv[])
628{ 593{
594 (void) argc;
595 (void) argv;
596
629 Suite *tox = tox_suite(); 597 Suite *tox = tox_suite();
630 SRunner *test_runner = srunner_create(tox); 598 SRunner *test_runner = srunner_create(tox);
631 599
@@ -637,6 +605,5 @@ int main(int argc, char *argv[])
637 srunner_free(test_runner); 605 srunner_free(test_runner);
638 606
639 return number_failed; 607 return number_failed;
640
641// return test_AV_flows();
642} 608}
609#endif