diff options
author | mannol <eniz_vukovic@hotmail.com> | 2015-02-14 23:37:52 +0100 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2015-02-14 23:37:52 +0100 |
commit | aad857527cd63b5f79786df0c1aab50f4de87774 (patch) | |
tree | c94463c21fe2d73b1ec514b223a5e0166e1e8693 /toxav/av_test.c | |
parent | 39680f31d0121cef2358507fcea84cacad69893a (diff) |
Control part of new api already kind of works
Diffstat (limited to 'toxav/av_test.c')
-rw-r--r-- | toxav/av_test.c | 339 |
1 files changed, 339 insertions, 0 deletions
diff --git a/toxav/av_test.c b/toxav/av_test.c new file mode 100644 index 00000000..1e5e4ad7 --- /dev/null +++ b/toxav/av_test.c | |||
@@ -0,0 +1,339 @@ | |||
1 | #include "toxav.h" | ||
2 | #include "../toxcore/tox.h" | ||
3 | |||
4 | #include <assert.h> | ||
5 | #include <stdio.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <time.h> | ||
8 | #include <string.h> | ||
9 | |||
10 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
11 | #define c_sleep(x) Sleep(1*x) | ||
12 | #else | ||
13 | #include <unistd.h> | ||
14 | #define c_sleep(x) usleep(1000*x) | ||
15 | #endif | ||
16 | |||
17 | typedef struct { | ||
18 | bool incoming; | ||
19 | bool ringing; | ||
20 | bool ended; | ||
21 | bool errored; | ||
22 | bool sending; | ||
23 | bool paused; | ||
24 | } CallControl; | ||
25 | |||
26 | |||
27 | /** | ||
28 | * Callbacks | ||
29 | */ | ||
30 | void t_toxav_call_cb(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_enabled, void *user_data) | ||
31 | { | ||
32 | printf("Handling CALL callback\n"); | ||
33 | ((CallControl*)user_data)->incoming = true; | ||
34 | } | ||
35 | void t_toxav_call_state_cb(ToxAV *av, uint32_t friend_number, TOXAV_CALL_STATE state, void *user_data) | ||
36 | { | ||
37 | printf("Handling CALL STATE callback: "); | ||
38 | |||
39 | if (((CallControl*)user_data)->ringing) | ||
40 | ((CallControl*)user_data)->ringing = false; | ||
41 | |||
42 | if (((CallControl*)user_data)->paused) | ||
43 | ((CallControl*)user_data)->paused = false; | ||
44 | |||
45 | switch (state) | ||
46 | { | ||
47 | case TOXAV_CALL_STATE_RINGING: { | ||
48 | printf("Ringing"); | ||
49 | ((CallControl*)user_data)->ringing = true; | ||
50 | } break; | ||
51 | |||
52 | case TOXAV_CALL_STATE_NOT_SENDING: { | ||
53 | printf("Not sending"); | ||
54 | ((CallControl*)user_data)->sending = false; | ||
55 | } break; | ||
56 | |||
57 | case TOXAV_CALL_STATE_SENDING_A: { | ||
58 | printf("Sending Audio"); | ||
59 | ((CallControl*)user_data)->sending = true; | ||
60 | } break; | ||
61 | |||
62 | case TOXAV_CALL_STATE_SENDING_V: { | ||
63 | printf("Sending Video"); | ||
64 | ((CallControl*)user_data)->sending = true; | ||
65 | } break; | ||
66 | |||
67 | case TOXAV_CALL_STATE_SENDING_AV: { | ||
68 | printf("Sending Both"); | ||
69 | ((CallControl*)user_data)->sending = true; | ||
70 | } break; | ||
71 | |||
72 | case TOXAV_CALL_STATE_PAUSED: { | ||
73 | printf("Paused"); | ||
74 | ((CallControl*)user_data)->paused = true; | ||
75 | ((CallControl*)user_data)->sending = false; | ||
76 | } break; | ||
77 | |||
78 | case TOXAV_CALL_STATE_END: { | ||
79 | printf("Ended"); | ||
80 | ((CallControl*)user_data)->ended = true; | ||
81 | ((CallControl*)user_data)->sending = false; | ||
82 | } break; | ||
83 | |||
84 | case TOXAV_CALL_STATE_ERROR: { | ||
85 | printf("Error"); | ||
86 | ((CallControl*)user_data)->errored = true; | ||
87 | ((CallControl*)user_data)->sending = false; | ||
88 | } break; | ||
89 | } | ||
90 | |||
91 | printf("\n"); | ||
92 | } | ||
93 | void t_toxav_receive_video_frame_cb(ToxAV *av, uint32_t friend_number, | ||
94 | uint16_t width, uint16_t height, | ||
95 | uint8_t const *planes[], int32_t const stride[], | ||
96 | void *user_data) | ||
97 | { | ||
98 | printf("Handling VIDEO FRAME callback\n"); | ||
99 | } | ||
100 | void t_toxav_receive_audio_frame_cb(ToxAV *av, uint32_t friend_number, | ||
101 | int16_t const *pcm, | ||
102 | size_t sample_count, | ||
103 | uint8_t channels, | ||
104 | uint32_t sampling_rate, | ||
105 | void *user_data) | ||
106 | { | ||
107 | printf("Handling AUDIO FRAME callback\n"); | ||
108 | } | ||
109 | void t_accept_friend_request_cb(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) | ||
110 | { | ||
111 | if (length == 7 && memcmp("gentoo", data, 7) == 0) { | ||
112 | tox_add_friend_norequest(m, public_key); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | |||
117 | /** | ||
118 | */ | ||
119 | void prepare(Tox* Bsn, Tox* Alice, Tox* Bob) | ||
120 | { | ||
121 | long long unsigned int cur_time = time(NULL); | ||
122 | |||
123 | uint32_t to_compare = 974536; | ||
124 | uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; | ||
125 | |||
126 | tox_callback_friend_request(Alice, t_accept_friend_request_cb, &to_compare); | ||
127 | tox_get_address(Alice, address); | ||
128 | |||
129 | assert(tox_add_friend(Bob, address, (uint8_t *)"gentoo", 7) >= 0); | ||
130 | |||
131 | uint8_t off = 1; | ||
132 | |||
133 | while (1) { | ||
134 | tox_do(Bsn); | ||
135 | tox_do(Alice); | ||
136 | tox_do(Bob); | ||
137 | |||
138 | if (tox_isconnected(Bsn) && tox_isconnected(Alice) && tox_isconnected(Bob) && off) { | ||
139 | printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time); | ||
140 | off = 0; | ||
141 | } | ||
142 | |||
143 | if (tox_get_friend_connection_status(Alice, 0) == 1 && tox_get_friend_connection_status(Bob, 0) == 1) | ||
144 | break; | ||
145 | |||
146 | c_sleep(20); | ||
147 | } | ||
148 | |||
149 | printf("All set after %llu seconds!\n", time(NULL) - cur_time); | ||
150 | } | ||
151 | void prepareAV(ToxAV* AliceAV, void* AliceUD, ToxAV* BobAV, void* BobUD) | ||
152 | { | ||
153 | /* Alice */ | ||
154 | toxav_callback_call(AliceAV, t_toxav_call_cb, AliceUD); | ||
155 | toxav_callback_call_state(AliceAV, t_toxav_call_state_cb, AliceUD); | ||
156 | toxav_callback_receive_video_frame(AliceAV, t_toxav_receive_video_frame_cb, AliceUD); | ||
157 | toxav_callback_receive_audio_frame(AliceAV, t_toxav_receive_audio_frame_cb, AliceUD); | ||
158 | |||
159 | /* Bob */ | ||
160 | toxav_callback_call(BobAV, t_toxav_call_cb, BobUD); | ||
161 | toxav_callback_call_state(BobAV, t_toxav_call_state_cb, BobUD); | ||
162 | toxav_callback_receive_video_frame(BobAV, t_toxav_receive_video_frame_cb, BobUD); | ||
163 | toxav_callback_receive_audio_frame(BobAV, t_toxav_receive_audio_frame_cb, BobUD); | ||
164 | } | ||
165 | void iterate(Tox* Bsn, ToxAV* AliceAV, ToxAV* BobAV) | ||
166 | { | ||
167 | tox_do(Bsn); | ||
168 | tox_do(toxav_get_tox(AliceAV)); | ||
169 | tox_do(toxav_get_tox(BobAV)); | ||
170 | |||
171 | toxav_iteration(AliceAV); | ||
172 | toxav_iteration(BobAV); | ||
173 | |||
174 | c_sleep(20); | ||
175 | } | ||
176 | |||
177 | |||
178 | int main (int argc, char** argv) | ||
179 | { | ||
180 | Tox *Bsn = tox_new(0); | ||
181 | Tox *Alice = tox_new(0); | ||
182 | Tox *Bob = tox_new(0); | ||
183 | |||
184 | assert(Bsn && Alice && Bob); | ||
185 | |||
186 | prepare(Bsn, Alice, Bob); | ||
187 | |||
188 | |||
189 | ToxAV *AliceAV, *BobAV; | ||
190 | CallControl AliceCC, BobCC; | ||
191 | |||
192 | { | ||
193 | TOXAV_ERR_NEW rc; | ||
194 | AliceAV = toxav_new(Alice, &rc); | ||
195 | assert(rc == TOXAV_ERR_NEW_OK); | ||
196 | |||
197 | BobAV = toxav_new(Bob, &rc); | ||
198 | assert(rc == TOXAV_ERR_NEW_OK); | ||
199 | |||
200 | prepareAV(AliceAV, &AliceCC, BobAV, &BobCC); | ||
201 | printf("Created 2 instances of ToxAV\n"); | ||
202 | } | ||
203 | |||
204 | |||
205 | #define REGULAR_CALL_FLOW(A_BR, V_BR) \ | ||
206 | { \ | ||
207 | memset(&AliceCC, 0, sizeof(CallControl)); \ | ||
208 | memset(&BobCC, 0, sizeof(CallControl)); \ | ||
209 | \ | ||
210 | TOXAV_ERR_CALL rc; \ | ||
211 | toxav_call(AliceAV, 0, A_BR, V_BR, &rc); \ | ||
212 | \ | ||
213 | if (rc != TOXAV_ERR_CALL_OK) { \ | ||
214 | printf("toxav_call failed: %d\n", rc); \ | ||
215 | exit(1); \ | ||
216 | } \ | ||
217 | \ | ||
218 | \ | ||
219 | long long unsigned int start_time = time(NULL); \ | ||
220 | \ | ||
221 | \ | ||
222 | while (!AliceCC.ended || !BobCC.ended) { \ | ||
223 | \ | ||
224 | if (BobCC.incoming) { \ | ||
225 | TOXAV_ERR_ANSWER rc; \ | ||
226 | toxav_answer(BobAV, 0, 48, 4000, &rc); \ | ||
227 | \ | ||
228 | if (rc != TOXAV_ERR_ANSWER_OK) { \ | ||
229 | printf("toxav_answer failed: %d\n", rc); \ | ||
230 | exit(1); \ | ||
231 | } \ | ||
232 | BobCC.incoming = false; \ | ||
233 | } \ | ||
234 | else if (AliceCC.sending && BobCC.sending) { \ | ||
235 | /* TODO rtp */ \ | ||
236 | \ | ||
237 | if (time(NULL) - start_time == 5) { \ | ||
238 | \ | ||
239 | TOXAV_ERR_CALL_CONTROL rc; \ | ||
240 | toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc); \ | ||
241 | \ | ||
242 | if (rc != TOXAV_ERR_CALL_CONTROL_OK) { \ | ||
243 | printf("toxav_call_control failed: %d\n", rc); \ | ||
244 | exit(1); \ | ||
245 | } \ | ||
246 | } \ | ||
247 | } \ | ||
248 | \ | ||
249 | iterate(Bsn, AliceAV, BobAV); \ | ||
250 | } \ | ||
251 | printf("Success!\n");\ | ||
252 | } | ||
253 | |||
254 | printf("\nTrying regular call (Audio and Video)...\n"); | ||
255 | // REGULAR_CALL_FLOW(48, 4000); | ||
256 | |||
257 | printf("\nTrying regular call (Audio only)...\n"); | ||
258 | // REGULAR_CALL_FLOW(48, 0); | ||
259 | |||
260 | printf("\nTrying regular call (Video only)...\n"); | ||
261 | // REGULAR_CALL_FLOW(0, 4000); | ||
262 | |||
263 | #undef REGULAR_CALL_FLOW | ||
264 | |||
265 | { /* Alice calls; Bob rejects */ | ||
266 | printf("\nTrying reject flow...\n"); | ||
267 | |||
268 | memset(&AliceCC, 0, sizeof(CallControl)); | ||
269 | memset(&BobCC, 0, sizeof(CallControl)); | ||
270 | |||
271 | { | ||
272 | TOXAV_ERR_CALL rc; | ||
273 | toxav_call(AliceAV, 0, 48, 0, &rc); | ||
274 | |||
275 | if (rc != TOXAV_ERR_CALL_OK) { | ||
276 | printf("toxav_call failed: %d\n", rc); | ||
277 | exit(1); | ||
278 | } | ||
279 | } | ||
280 | |||
281 | while (!BobCC.incoming) | ||
282 | iterate(Bsn, AliceAV, BobAV); | ||
283 | |||
284 | /* Reject */ | ||
285 | { | ||
286 | TOXAV_ERR_CALL_CONTROL rc; | ||
287 | toxav_call_control(BobAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc); | ||
288 | |||
289 | if (rc != TOXAV_ERR_CALL_CONTROL_OK) { | ||
290 | printf("toxav_call_control failed: %d\n", rc); | ||
291 | exit(1); | ||
292 | } | ||
293 | } | ||
294 | |||
295 | while (!AliceCC.ended || !BobCC.ended) | ||
296 | iterate(Bsn, AliceAV, BobAV); | ||
297 | |||
298 | printf("Success!\n"); | ||
299 | } | ||
300 | |||
301 | { /* Alice calls; Alice cancels while ringing */ | ||
302 | printf("\nTrying cancel (while ringing) flow...\n"); | ||
303 | |||
304 | memset(&AliceCC, 0, sizeof(CallControl)); | ||
305 | memset(&BobCC, 0, sizeof(CallControl)); | ||
306 | |||
307 | { | ||
308 | TOXAV_ERR_CALL rc; | ||
309 | toxav_call(AliceAV, 0, 48, 0, &rc); | ||
310 | |||
311 | if (rc != TOXAV_ERR_CALL_OK) { | ||
312 | printf("toxav_call failed: %d\n", rc); | ||
313 | exit(1); | ||
314 | } | ||
315 | } | ||
316 | |||
317 | while (!BobCC.incoming) | ||
318 | iterate(Bsn, AliceAV, BobAV); | ||
319 | |||
320 | /* Cancel */ | ||
321 | { | ||
322 | TOXAV_ERR_CALL_CONTROL rc; | ||
323 | toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc); | ||
324 | |||
325 | if (rc != TOXAV_ERR_CALL_CONTROL_OK) { | ||
326 | printf("toxav_call_control failed: %d\n", rc); | ||
327 | exit(1); | ||
328 | } | ||
329 | } | ||
330 | |||
331 | while (!AliceCC.ended || !BobCC.ended) | ||
332 | iterate(Bsn, AliceAV, BobAV); | ||
333 | |||
334 | printf("Success!\n"); | ||
335 | } | ||
336 | |||
337 | printf("\nTest successful!\n"); | ||
338 | return 0; | ||
339 | } \ No newline at end of file | ||