summaryrefslogtreecommitdiff
path: root/toxav
diff options
context:
space:
mode:
Diffstat (limited to 'toxav')
-rwxr-xr-xtoxav/phone.c737
-rw-r--r--toxav/toxmedia.c825
-rw-r--r--toxav/toxmedia.h168
-rwxr-xr-xtoxav/toxmsi.c1337
-rwxr-xr-xtoxav/toxmsi.h231
-rwxr-xr-xtoxav/toxrtp.c878
-rwxr-xr-xtoxav/toxrtp.h211
7 files changed, 4387 insertions, 0 deletions
diff --git a/toxav/phone.c b/toxav/phone.c
new file mode 100755
index 00000000..b55a072c
--- /dev/null
+++ b/toxav/phone.c
@@ -0,0 +1,737 @@
1/** phone.c
2 *
3 * NOTE NOTE NOTE NOTE NOTE NOTE
4 *
5 * This file is for testing/reference purposes only, hence
6 * it is _poorly_ designed and it does not fully reflect the
7 * quaility of msi nor rtp. Although toxmsi* and toxrtp* are tested
8 * there is always possiblity of crashes. If crash occures,
9 * contact me ( mannol ) on either irc channel #tox-dev @ freenode.net:6667
10 * or eniz_vukovic@hotmail.com
11 *
12 * NOTE NOTE NOTE NOTE NOTE NOTE
13 *
14 * Copyright (C) 2013 Tox project All Rights Reserved.
15 *
16 * This file is part of Tox.
17 *
18 * Tox is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * Tox is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
30 *
31 */
32
33#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif /* HAVE_CONFIG_H */
36
37#define _BSD_SOURCE
38#define _GNU_SOURCE
39
40#include <stdio.h>
41#include <string.h>
42#include <stdlib.h>
43
44#include "toxmsi.h"
45#include "toxrtp.h"
46#include <stdarg.h>
47#include <unistd.h>
48#include <assert.h>
49
50#include "../toxcore/network.h"
51#include "../toxcore/event.h"
52#include "../toxcore/tox.h"
53
54/* Define client version */
55#define _USERAGENT "v.0.3.0"
56
57
58typedef struct av_friend_s {
59 int _id;
60 int _active; /* 0=false; 1=true; */
61} av_friend_t;
62
63typedef struct av_session_s {
64 MSISession* _msi;
65
66 RTPSession* _rtp_audio;
67 RTPSession* _rtp_video;
68
69 pthread_mutex_t _mutex;
70
71 Tox* _messenger;
72 av_friend_t* _friends;
73 int _friend_cout;
74 uint8_t _my_public_id[200];
75} av_session_t;
76
77
78void av_allocate_friend(av_session_t* _phone, int _id, int _active)
79{
80 static int _new_id = 0;
81
82 if ( !_phone->_friends ) {
83 _phone->_friends = calloc(sizeof(av_friend_t), 1);
84 _phone->_friend_cout = 1;
85 } else{
86 _phone->_friend_cout ++;
87 _phone->_friends = realloc(_phone->_friends, sizeof(av_friend_t) * _phone->_friend_cout);
88 }
89
90 if ( _id = -1 ) {
91 _phone->_friends->_id = _new_id;
92 _new_id ++;
93 } else _phone->_friends->_id = _id;
94
95 _phone->_friends->_active = _active;
96}
97av_friend_t* av_get_friend(av_session_t* _phone, int _id)
98{
99 av_friend_t* _friends = _phone->_friends;
100
101 if ( !_friends ) return NULL;
102
103 int _it = 0;
104 for (; _it < _phone->_friend_cout; _it ++)
105 if ( _friends[_it]._id == _id )
106 return _friends + _it;
107
108 return NULL;
109}
110
111
112/***************** MISC *****************/
113
114void INFO (const char* _format, ...)
115{
116 printf("\r[!] ");
117 va_list _arg;
118 va_start (_arg, _format);
119 vfprintf (stdout, _format, _arg);
120 va_end (_arg);
121 printf("\n\r >> ");
122 fflush(stdout);
123}
124
125unsigned char *hex_string_to_bin(char hex_string[])
126{
127 size_t i, len = strlen(hex_string);
128 unsigned char *val = calloc(sizeof(char), len);
129 char *pos = hex_string;
130
131 for (i = 0; i < len; ++i, pos += 2)
132 sscanf(pos, "%2hhx", &val[i]);
133
134 return val;
135}
136
137int getinput( char* _buff, size_t _limit, int* _len )
138{
139 if ( fgets(_buff, _limit, stdin) == NULL )
140 return -1;
141
142 *_len = strlen(_buff) - 1;
143
144 /* Get rid of newline */
145 _buff[*_len] = '\0';
146
147 return 0;
148}
149
150char* trim_spaces ( char* buff )
151{
152
153 int _i = 0, _len = strlen(buff);
154
155 char* container = calloc(sizeof(char), _len);
156 int _ci = 0;
157
158 for ( ; _i < _len; _i++ ) {
159 while ( _i < _len && buff[_i] == ' ' )
160 _i++;
161
162 if ( _i < _len ){
163 container[_ci] = buff[_i];
164 _ci ++;
165 }
166 }
167
168 memcpy( buff, container, _ci );
169 buff[_ci] = '\0';
170 free(container);
171 return buff;
172}
173
174#define FRADDR_TOSTR_CHUNK_LEN 8
175
176static void fraddr_to_str(uint8_t *id_bin, char *id_str)
177{
178 uint i, delta = 0, pos_extra, sum_extra = 0;
179
180 for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
181 sprintf(&id_str[2 * i + delta], "%02hhX", id_bin[i]);
182
183 if ((i + 1) == TOX_CLIENT_ID_SIZE)
184 pos_extra = 2 * (i + 1) + delta;
185
186 if (i >= TOX_CLIENT_ID_SIZE)
187 sum_extra |= id_bin[i];
188
189 if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) {
190 id_str[2 * (i + 1) + delta] = ' ';
191 delta++;
192 }
193 }
194
195 id_str[2 * i + delta] = 0;
196
197 if (!sum_extra)
198 id_str[pos_extra] = 0;
199}
200
201void* phone_handle_media_transport_poll ( void* _hmtc_args_p )
202{
203 RTPMessage* _audio_msg, * _video_msg;
204 av_session_t* _phone = _hmtc_args_p;
205 MSISession* _session = _phone->_msi;
206
207 RTPSession* _rtp_audio = _phone->_rtp_audio;
208 RTPSession* _rtp_video = _phone->_rtp_video;
209
210
211 Tox* _messenger = _phone->_messenger;
212
213
214 while ( _session->call ) {
215
216 _audio_msg = rtp_recv_msg ( _rtp_audio );
217 _video_msg = rtp_recv_msg ( _rtp_video );
218
219 if ( _audio_msg ) {
220 /* Do whatever with msg
221 printf("%d - %s\n", _audio_msg->header->sequnum, _audio_msg->data);*/
222 rtp_free_msg ( _rtp_audio, _audio_msg );
223 }
224
225 if ( _video_msg ) {
226 /* Do whatever with msg
227 p rintf("%d - %s\n", _video_msg->header->sequnum, _video_msg->data);*/
228 rtp_free_msg ( _rtp_video, _video_msg );
229 }
230
231 /*
232 * Send test message to the 'remote'
233 */
234 rtp_send_msg ( _rtp_audio, _messenger, (const uint8_t*)"audio\0", 6 );
235
236 if ( _session->call->type_local == type_video ){ /* if local call send video */
237 rtp_send_msg ( _rtp_video, _messenger, (const uint8_t*)"video\0", 6 );
238 }
239
240 _audio_msg = _video_msg = NULL;
241
242
243 /* Send ~1k messages per second
244 * That _should_ be enough for both Audio and Video
245 */
246 usleep ( 1000 );
247 /* -------------------- */
248 }
249
250 if ( _audio_msg ) rtp_free_msg(_rtp_audio, _audio_msg);
251 rtp_release_session_recv(_rtp_audio);
252 rtp_terminate_session(_rtp_audio, _messenger);
253
254 if ( _video_msg ) rtp_free_msg(_rtp_video, _video_msg);
255 rtp_release_session_recv(_rtp_video);
256 rtp_terminate_session(_rtp_video, _messenger);
257
258 INFO("Media thread finished!");
259
260 pthread_exit ( NULL );
261}
262
263int phone_startmedia_loop ( av_session_t* _phone )
264{
265 if ( !_phone ){
266 return -1;
267 }
268
269 _phone->_rtp_audio = rtp_init_session (
270 type_audio,
271 _phone->_messenger,
272 _phone->_msi->call->peers[0],
273 _phone->_msi->call->key_peer,
274 _phone->_msi->call->key_local,
275 _phone->_msi->call->nonce_peer,
276 _phone->_msi->call->nonce_local
277 );
278
279 _phone->_rtp_audio = rtp_init_session (
280 type_video,
281 _phone->_messenger,
282 _phone->_msi->call->peers[0],
283 _phone->_msi->call->key_peer,
284 _phone->_msi->call->key_local,
285 _phone->_msi->call->nonce_peer,
286 _phone->_msi->call->nonce_local
287 );
288
289
290 if ( 0 > event.rise(phone_handle_media_transport_poll, _phone) )
291 {
292 printf("Error while starting phone_handle_media_transport_poll()\n");
293 return -1;
294 }
295 else return 0;
296}
297
298
299/* Some example callbacks */
300
301void* callback_recv_invite ( void* _arg )
302{
303 const char* _call_type;
304
305 MSISession* _msi = _arg;
306
307 switch ( _msi->call->type_peer[_msi->call->peer_count - 1] ){
308 case type_audio:
309 _call_type = "audio";
310 break;
311 case type_video:
312 _call_type = "video";
313 break;
314 }
315
316 INFO( "Incoming %s call!", _call_type );
317
318}
319void* callback_recv_ringing ( void* _arg )
320{
321 INFO ( "Ringing!" );
322}
323void* callback_recv_starting ( void* _arg )
324{
325 MSISession* _session = _arg;
326 if ( 0 != phone_startmedia_loop(_session->agent_handler) ){
327 INFO("Starting call failed!");
328 } else {
329 INFO ("Call started! ( press h to hangup )");
330 }
331}
332void* callback_recv_ending ( void* _arg )
333{
334 INFO ( "Call ended!" );
335}
336
337void* callback_recv_error ( void* _arg )
338{
339 MSISession* _session = _arg;
340
341 INFO( "Error: %s", _session->last_error_str );
342}
343
344void* callback_call_started ( void* _arg )
345{
346 MSISession* _session = _arg;
347 if ( 0 != phone_startmedia_loop(_session->agent_handler) ){
348 INFO("Starting call failed!");
349 } else {
350 INFO ("Call started! ( press h to hangup )");
351 }
352
353}
354void* callback_call_canceled ( void* _arg )
355{
356 INFO ( "Call canceled!" );
357}
358void* callback_call_rejected ( void* _arg )
359{
360 INFO ( "Call rejected!" );
361}
362void* callback_call_ended ( void* _arg )
363{
364 INFO ( "Call ended!" );
365}
366
367void* callback_requ_timeout ( void* _arg )
368{
369 INFO( "No answer! " );
370}
371
372int av_connect_to_dht(av_session_t* _phone, char* _dht_key, const char* _dht_addr, unsigned short _dht_port)
373{
374 unsigned char *_binary_string = hex_string_to_bin(_dht_key);
375
376 uint16_t _port = htons(_dht_port);
377
378 int _if = tox_bootstrap_from_address(_phone->_messenger, _dht_addr, 1, _port, _binary_string );
379
380 free(_binary_string);
381
382 return _if ? 0 : -1;
383}
384
385av_session_t* av_init_session()
386{
387 av_session_t* _retu = malloc(sizeof(av_session_t));
388
389 /* Initialize our mutex */
390 pthread_mutex_init ( &_retu->_mutex, NULL );
391
392 _retu->_messenger = tox_new(1);
393
394 if ( !_retu->_messenger ) {
395 fprintf ( stderr, "tox_new() failed!\n" );
396 return NULL;
397 }
398
399 _retu->_friends = NULL;
400
401 _retu->_rtp_audio = NULL;
402 _retu->_rtp_video = NULL;
403
404 uint8_t _byte_address[TOX_FRIEND_ADDRESS_SIZE];
405 tox_get_address(_retu->_messenger, _byte_address );
406 fraddr_to_str( _byte_address, _retu->_my_public_id );
407
408
409 /* Initialize msi */
410 _retu->_msi = msi_init_session ( _retu->_messenger, _USERAGENT );
411
412 if ( !_retu->_msi ) {
413 fprintf ( stderr, "msi_init_session() failed\n" );
414 return NULL;
415 }
416
417 _retu->_msi->agent_handler = _retu;
418
419 /* ------------------ */
420 msi_register_callback(callback_call_started, cb_onstart);
421 msi_register_callback(callback_call_canceled, cb_oncancel);
422 msi_register_callback(callback_call_rejected, cb_onreject);
423 msi_register_callback(callback_call_ended, cb_onend);
424 msi_register_callback(callback_recv_invite, cb_oninvite);
425
426 msi_register_callback(callback_recv_ringing, cb_ringing);
427 msi_register_callback(callback_recv_starting, cb_starting);
428 msi_register_callback(callback_recv_ending, cb_ending);
429
430 msi_register_callback(callback_recv_error, cb_error);
431 msi_register_callback(callback_requ_timeout, cb_timeout);
432 /* ------------------ */
433
434 return _retu;
435}
436
437int av_terminate_session(av_session_t* _phone)
438{
439 if ( _phone->_msi->call ){
440 msi_hangup(_phone->_msi); /* Hangup the phone first */
441 }
442
443 free(_phone->_friends);
444 msi_terminate_session(_phone->_msi);
445 pthread_mutex_destroy ( &_phone->_mutex );
446
447 Tox* _p = _phone->_messenger;
448 _phone->_messenger = NULL; usleep(100000); /* Wait for tox_pool to end */
449 tox_kill(_p);
450
451 printf("\r[i] Quit!\n");
452 return 0;
453}
454
455/****** AV HELPER FUNCTIONS ******/
456
457/* Auto accept friend request */
458void av_friend_requ(uint8_t *_public_key, uint8_t *_data, uint16_t _length, void *_userdata)
459{
460 av_session_t* _phone = _userdata;
461 av_allocate_friend (_phone, -1, 0);
462
463 INFO("Got friend request with message: %s", _data);
464
465 tox_add_friend_norequest(_phone->_messenger, _public_key);
466
467 INFO("Auto-accepted! Friend id: %d", _phone->_friends->_id );
468}
469
470void av_friend_active(Tox *_messenger, int _friendnumber, uint8_t *_string, uint16_t _length, void *_userdata)
471{
472 av_session_t* _phone = _userdata;
473 INFO("Friend no. %d is online", _friendnumber);
474
475 av_friend_t* _this_friend = av_get_friend(_phone, _friendnumber);
476
477 if ( !_this_friend ) {
478 INFO("But it's not registered!");
479 return;
480 }
481
482 (*_this_friend)._active = 1;
483}
484
485int av_add_friend(av_session_t* _phone, char* _friend_hash)
486{
487 trim_spaces(_friend_hash);
488
489 unsigned char *_bin_string = hex_string_to_bin(_friend_hash);
490 int _number = tox_add_friend(_phone->_messenger, _bin_string, (uint8_t *)"Tox phone "_USERAGENT, sizeof("Tox phone "_USERAGENT));
491 free(_bin_string);
492
493 if ( _number >= 0) {
494 INFO("Added friend as %d", _number );
495 av_allocate_friend(_phone, _number, 0);
496 }
497 else
498 INFO("Unknown error %i", _number );
499
500 return _number;
501}
502/*********************************/
503
504void do_phone ( av_session_t* _phone )
505{
506 INFO("Welcome to tox_phone version: " _USERAGENT "\n"
507 "Usage: \n"
508 "f [pubkey] (add friend)\n"
509 "c [a/v] (type) [friend] (friend id) (calls friend if online)\n"
510 "h (if call is active hang up)\n"
511 "a [a/v] (answer incoming call: a - audio / v - audio + video (audio is default))\n"
512 "r (reject incoming call)\n"
513 "q (quit)\n"
514 "================================================================================"
515 );
516
517 while ( 1 )
518 {
519 char _line [ 1500 ];
520 int _len;
521
522 if ( -1 == getinput(_line, 1500, &_len) ){
523 printf(" >> ");
524 fflush(stdout);
525 continue;
526 }
527
528 if ( _len > 1 && _line[1] != ' ' && _line[1] != '\n' ){
529 INFO("Invalid input!");
530 continue;
531 }
532
533 switch (_line[0]){
534
535 case 'f':
536 {
537 char _id [128];
538 strncpy(_id, _line + 2, 128);
539
540 av_add_friend(_phone, _id);
541
542 } break;
543 case 'c':
544 {
545 if ( _phone->_msi->call ){
546 INFO("Already in a call");
547 break;
548 }
549
550 MSICallType _ctype;
551
552 if ( _len < 5 ){
553 INFO("Invalid input; usage: c a/v [friend]");
554 break;
555 }
556 else if ( _line[2] == 'a' || _line[2] != 'v' ){ /* default and audio */
557 _ctype = type_audio;
558 }
559 else { /* video */
560 _ctype = type_video;
561 }
562
563 char* _end;
564 int _friend = strtol(_line + 4, &_end, 10);
565
566 if ( *_end ){
567 INFO("Friend num has to be numerical value");
568 break;
569 }
570
571 /* Set timeout */
572 msi_invite ( _phone->_msi, _ctype, 10 * 1000, _friend );
573 INFO("Calling friend: %d!", _friend);
574
575 } break;
576 case 'h':
577 {
578 if ( !_phone->_msi->call ){
579 INFO("No call!");
580 break;
581 }
582
583 msi_hangup(_phone->_msi);
584
585 INFO("Hung up...");
586
587 } break;
588 case 'a':
589 {
590
591 if ( _phone->_msi->call && _phone->_msi->call->state != call_starting ) {
592 break;
593 }
594
595 if ( _len > 1 && _line[2] == 'v' )
596 msi_answer(_phone->_msi, type_video);
597 else
598 msi_answer(_phone->_msi, type_audio);
599
600 } break;
601 case 'r':
602 {
603 if ( _phone->_msi->call && _phone->_msi->call->state != call_starting ){
604 break;
605 }
606
607 msi_reject(_phone->_msi);
608
609 INFO("Call Rejected...");
610
611 } break;
612 case 'q':
613 {
614 INFO("Quitting!");
615 return;
616 }
617 default:
618 {
619 INFO("Invalid command!");
620 } break;
621
622 }
623
624 }
625}
626
627void* tox_poll (void* _messenger_p)
628{
629 Tox** _messenger = _messenger_p;
630 while( *_messenger ) {
631 tox_do(*_messenger);
632 usleep(10000);
633 }
634
635 pthread_exit(NULL);
636}
637
638int av_wait_dht(av_session_t* _phone, int _wait_seconds, const char* _ip, char* _key, unsigned short _port)
639{
640 if ( !_wait_seconds )
641 return -1;
642
643 int _waited = 0;
644
645 while( !tox_isconnected(_phone->_messenger) ) {
646
647 if ( -1 == av_connect_to_dht(_phone, _key, _ip, _port) )
648 {
649 INFO("Could not connect to: %s", _ip);
650 av_terminate_session(_phone);
651 return -1;
652 }
653
654 if ( _waited >= _wait_seconds ) return 0;
655
656 printf(".");
657 fflush(stdout);
658
659 _waited ++;
660 usleep(1000000);
661 }
662
663 int _r = _wait_seconds - _waited;
664 return _r ? _r : 1;
665}
666/* ---------------------- */
667
668int print_help ( const char* _name )
669{
670 printf ( "Usage: %s [IP] [PORT] [KEY]\n"
671 "\t[IP] (DHT ip)\n"
672 "\t[PORT] (DHT port)\n"
673 "\t[KEY] (DHT public key)\n"
674 "P.S. Friends and key are stored in ./tox_phone.conf\n"
675 ,_name );
676 return 1;
677}
678
679int main ( int argc, char* argv [] )
680{
681 if ( argc < 1 || argc < 4 )
682 return print_help(argv[0]);
683
684 char* _convertable;
685
686 int _wait_seconds = 5;
687
688 const char* _ip = argv[1];
689 char* _key = argv[3];
690 unsigned short _port = strtol(argv[2], &_convertable, 10);
691
692 if ( *_convertable ){
693 printf("Invalid port: cannot convert string to long: %s", _convertable);
694 return 1;
695 }
696
697 av_session_t* _phone = av_init_session();
698
699 tox_callback_friend_request(_phone->_messenger, av_friend_requ, _phone);
700 tox_callback_status_message(_phone->_messenger, av_friend_active, _phone);
701
702 system("clear");
703
704 INFO("\r================================================================================\n"
705 "[!] Trying dht@%s:%d"
706 , _ip, _port);
707
708 /* Start tox protocol */
709 event.rise( tox_poll, &_phone->_messenger );
710
711 /* Just clean one line */
712 printf("\r \r");
713 fflush(stdout);
714
715 int _r;
716 for ( _r = 0; _r == 0; _r = av_wait_dht(_phone, _wait_seconds, _ip, _key, _port) ) _wait_seconds --;
717
718
719 if ( -1 == _r ) {
720 INFO("Error while connecting to dht: %s:%d", _ip, _port);
721 av_terminate_session(_phone);
722 return 1;
723 }
724
725 INFO("CONNECTED!\n"
726 "================================================================================\n"
727 "%s\n"
728 "================================================================================"
729 , _phone->_my_public_id );
730
731
732 do_phone (_phone);
733
734 av_terminate_session(_phone);
735
736 return 0;
737}
diff --git a/toxav/toxmedia.c b/toxav/toxmedia.c
new file mode 100644
index 00000000..4c9f5261
--- /dev/null
+++ b/toxav/toxmedia.c
@@ -0,0 +1,825 @@
1/* AV_codec.c
2// *
3 * Audio and video codec intitialisation, encoding/decoding and playback
4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24/*----------------------------------------------------------------------------------*/
25
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif /* HAVE_CONFIG_H */
29
30#include <stdio.h>
31#include <math.h>
32#include <libavcodec/avcodec.h>
33#include <libavformat/avformat.h>
34#include <libswscale/swscale.h>
35#include <libavdevice/avdevice.h>
36#include <libavutil/opt.h>
37#include <AL/al.h>
38#include <AL/alc.h>
39#include <SDL/SDL.h>
40#include <SDL/SDL_thread.h>
41#include <pthread.h>
42#include <opus/opus.h>
43
44#include "toxmsi.h"
45#include "toxmsi_message.h"
46#include "../toxrtp/toxrtp_message.h"
47#include "../toxrtp/tests/test_helper.h"
48#include "phone.h"
49#include "toxmedia.h"
50
51SDL_Surface *screen;
52
53int display_received_frame(codec_state *cs, AVFrame *r_video_frame)
54{
55 AVPicture pict;
56 SDL_LockYUVOverlay(cs->video_picture.bmp);
57
58 pict.data[0] = cs->video_picture.bmp->pixels[0];
59 pict.data[1] = cs->video_picture.bmp->pixels[2];
60 pict.data[2] = cs->video_picture.bmp->pixels[1];
61 pict.linesize[0] = cs->video_picture.bmp->pitches[0];
62 pict.linesize[1] = cs->video_picture.bmp->pitches[2];
63 pict.linesize[2] = cs->video_picture.bmp->pitches[1];
64
65 /* Convert the image into YUV format that SDL uses */
66 sws_scale(cs->sws_SDL_r_ctx, (uint8_t const * const *)r_video_frame->data, r_video_frame->linesize, 0,
67 cs->video_decoder_ctx->height, pict.data, pict.linesize );
68
69 SDL_UnlockYUVOverlay(cs->video_picture.bmp);
70 SDL_Rect rect;
71 rect.x = 0;
72 rect.y = 0;
73 rect.w = cs->video_decoder_ctx->width;
74 rect.h = cs->video_decoder_ctx->height;
75 SDL_DisplayYUVOverlay(cs->video_picture.bmp, &rect);
76 return 1;
77}
78
79struct jitter_buffer {
80 rtp_msg_t **queue;
81 uint16_t capacity;
82 uint16_t size;
83 uint16_t front;
84 uint16_t rear;
85 uint8_t queue_ready;
86 uint16_t current_id;
87 uint32_t current_ts;
88 uint8_t id_set;
89};
90
91struct jitter_buffer *create_queue(int capacity)
92{
93 struct jitter_buffer *q;
94 q = (struct jitter_buffer *)calloc(sizeof(struct jitter_buffer),1);
95 q->queue = (rtp_msg_t **)calloc((sizeof(rtp_msg_t) * capacity),1);
96 int i = 0;
97
98 for (i = 0; i < capacity; ++i) {
99 q->queue[i] = NULL;
100 }
101
102 q->size = 0;
103 q->capacity = capacity;
104 q->front = 0;
105 q->rear = -1;
106 q->queue_ready = 0;
107 q->current_id = 0;
108 q->current_ts = 0;
109 q->id_set = 0;
110 return q;
111}
112
113/* returns 1 if 'a' has a higher sequence number than 'b' */
114uint8_t sequence_number_older(uint16_t sn_a, uint16_t sn_b, uint32_t ts_a, uint32_t ts_b)
115{
116 /* should be stable enough */
117 return (sn_a > sn_b || ts_a > ts_b);
118}
119
120/* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */
121rtp_msg_t *dequeue(struct jitter_buffer *q, int *success)
122{
123 if (q->size == 0 || q->queue_ready == 0) {
124 q->queue_ready = 0;
125 *success = 0;
126 return NULL;
127 }
128
129 int front = q->front;
130
131 if (q->id_set == 0) {
132 q->current_id = q->queue[front]->_header->_sequence_number;
133 q->current_ts = q->queue[front]->_header->_timestamp;
134 q->id_set = 1;
135 } else {
136 int next_id = q->queue[front]->_header->_sequence_number;
137 int next_ts = q->queue[front]->_header->_timestamp;
138
139 /* if this packet is indeed the expected packet */
140 if (next_id == (q->current_id + 1) % _MAX_SEQU_NUM) {
141 q->current_id = next_id;
142 q->current_ts = next_ts;
143 } else {
144 if (sequence_number_older(next_id, q->current_id, next_ts, q->current_ts)) {
145 printf("nextid: %d current: %d\n", next_id, q->current_id);
146 q->current_id = (q->current_id + 1) % _MAX_SEQU_NUM;
147 *success = 2; /* tell the decoder the packet is lost */
148 return NULL;
149 } else {
150 /* packet too old */
151 printf("packet too old\n");
152 *success = 0;
153 return NULL;
154 }
155 }
156 }
157
158 q->size--;
159 q->front++;
160
161 if (q->front == q->capacity)
162 q->front = 0;
163
164 *success = 1;
165 q->current_id = q->queue[front]->_header->_sequence_number;
166 q->current_ts = q->queue[front]->_header->_timestamp;
167 return q->queue[front];
168}
169
170int empty_queue(struct jitter_buffer *q)
171{
172 while (q->size > 0) {
173 q->size--;
174 /* FIXME: */
175 /* rtp_free_msg(cs->_rtp_video, q->queue[q->front]); */
176 q->front++;
177
178 if (q->front == q->capacity)
179 q->front = 0;
180 }
181
182 q->id_set = 0;
183 q->queue_ready = 0;
184 return 0;
185}
186
187int queue(struct jitter_buffer *q, rtp_msg_t *pk)
188{
189 if (q->size == q->capacity) {
190 printf("buffer full, emptying buffer...\n");
191 empty_queue(q);
192 return 0;
193 }
194
195 if (q->size > 8)
196 q->queue_ready = 1;
197
198 ++q->size;
199 ++q->rear;
200
201 if (q->rear == q->capacity)
202 q->rear = 0;
203
204 q->queue[q->rear] = pk;
205
206 int a;
207 int b;
208 int j;
209 a = q->rear;
210
211 for (j = 0; j < q->size - 1; ++j) {
212 b = a - 1;
213
214 if (b < 0)
215 b += q->capacity;
216
217 if (sequence_number_older(q->queue[b]->_header->_sequence_number, q->queue[a]->_header->_sequence_number,
218 q->queue[b]->_header->_timestamp, q->queue[a]->_header->_timestamp)) {
219 rtp_msg_t *temp;
220 temp = q->queue[a];
221 q->queue[a] = q->queue[b];
222 q->queue[b] = temp;
223 printf("had to swap\n");
224 } else {
225 break;
226 }
227
228 a -= 1;
229
230 if (a < 0)
231 a += q->capacity;
232 }
233
234 if (pk)
235 return 1;
236
237 return 0;
238}
239
240int init_receive_audio(codec_state *cs)
241{
242 int err = OPUS_OK;
243 cs->audio_decoder = opus_decoder_create(48000, 1, &err);
244 opus_decoder_init(cs->audio_decoder, 48000, 1);
245 printf("init audio decoder successful\n");
246 return 1;
247}
248
249int init_receive_video(codec_state *cs)
250{
251 cs->video_decoder = avcodec_find_decoder(VIDEO_CODEC);
252
253 if (!cs->video_decoder) {
254 printf("init video_decoder failed\n");
255 return 0;
256 }
257
258 cs->video_decoder_ctx = avcodec_alloc_context3(cs->video_decoder);
259
260 if (!cs->video_decoder_ctx) {
261 printf("init video_decoder_ctx failed\n");
262 return 0;
263 }
264
265 if (avcodec_open2(cs->video_decoder_ctx, cs->video_decoder, NULL) < 0) {
266 printf("opening video decoder failed\n");
267 return 0;
268 }
269
270 printf("init video decoder successful\n");
271 return 1;
272}
273
274int init_send_video(codec_state *cs)
275{
276 cs->video_input_format = av_find_input_format(VIDEO_DRIVER);
277
278 if (avformat_open_input(&cs->video_format_ctx, DEFAULT_WEBCAM, cs->video_input_format, NULL) != 0) {
279 printf("opening video_input_format failed\n");
280 return 0;
281 }
282
283 avformat_find_stream_info(cs->video_format_ctx, NULL);
284 av_dump_format(cs->video_format_ctx, 0, DEFAULT_WEBCAM, 0);
285
286 int i;
287
288 for (i = 0; i < cs->video_format_ctx->nb_streams; ++i) {
289 if (cs->video_format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
290 cs->video_stream = i;
291 break;
292 }
293 }
294
295 cs->webcam_decoder_ctx = cs->video_format_ctx->streams[cs->video_stream]->codec;
296 cs->webcam_decoder = avcodec_find_decoder(cs->webcam_decoder_ctx->codec_id);
297
298 if (cs->webcam_decoder == NULL) {
299 printf("Unsupported codec\n");
300 return 0;
301 }
302
303 if (cs->webcam_decoder_ctx == NULL) {
304 printf("init webcam_decoder_ctx failed\n");
305 return 0;
306 }
307
308 if (avcodec_open2(cs->webcam_decoder_ctx, cs->webcam_decoder, NULL) < 0) {
309 printf("opening webcam decoder failed\n");
310 return 0;
311 }
312
313 cs->video_encoder = avcodec_find_encoder(VIDEO_CODEC);
314
315 if (!cs->video_encoder) {
316 printf("init video_encoder failed\n");
317 return 0;
318 }
319
320 cs->video_encoder_ctx = avcodec_alloc_context3(cs->video_encoder);
321
322 if (!cs->video_encoder_ctx) {
323 printf("init video_encoder_ctx failed\n");
324 return 0;
325 }
326
327 cs->video_encoder_ctx->bit_rate = VIDEO_BITRATE;
328 cs->video_encoder_ctx->rc_min_rate = cs->video_encoder_ctx->rc_max_rate = cs->video_encoder_ctx->bit_rate;
329 av_opt_set_double(cs->video_encoder_ctx->priv_data, "max-intra-rate", 90, 0);
330 av_opt_set(cs->video_encoder_ctx->priv_data, "quality", "realtime", 0);
331
332 cs->video_encoder_ctx->thread_count = 4;
333 cs->video_encoder_ctx->rc_buffer_aggressivity = 0.95;
334 cs->video_encoder_ctx->rc_buffer_size = VIDEO_BITRATE * 6;
335 cs->video_encoder_ctx->profile = 3;
336 cs->video_encoder_ctx->qmax = 54;
337 cs->video_encoder_ctx->qmin = 4;
338 AVRational myrational = {1, 25};
339 cs->video_encoder_ctx->time_base = myrational;
340 cs->video_encoder_ctx->gop_size = 99999;
341 cs->video_encoder_ctx->pix_fmt = PIX_FMT_YUV420P;
342 cs->video_encoder_ctx->width = cs->webcam_decoder_ctx->width;
343 cs->video_encoder_ctx->height = cs->webcam_decoder_ctx->height;
344
345 if (avcodec_open2(cs->video_encoder_ctx, cs->video_encoder, NULL) < 0) {
346 printf("opening video encoder failed\n");
347 return 0;
348 }
349
350 printf("init video encoder successful\n");
351 return 1;
352}
353
354int init_send_audio(codec_state *cs)
355{
356 cs->support_send_audio = 0;
357
358 const ALchar *pDeviceList = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
359 int i = 0;
360 const ALchar *device_names[20];
361
362 if (pDeviceList) {
363 printf("\nAvailable Capture Devices are:\n");
364
365 while (*pDeviceList) {
366 device_names[i] = pDeviceList;
367 printf("%d) %s\n", i, device_names[i]);
368 pDeviceList += strlen(pDeviceList) + 1;
369 ++i;
370 }
371 }
372
373 printf("enter capture device number: \n");
374 char dev[2];
375 fgets(dev, sizeof(dev), stdin);
376 cs->audio_capture_device = alcCaptureOpenDevice(device_names[dev[0] - 48], AUDIO_SAMPLE_RATE, AL_FORMAT_MONO16,
377 AUDIO_FRAME_SIZE * 4);
378
379 if (alcGetError(cs->audio_capture_device) != AL_NO_ERROR) {
380 printf("could not start capture device! %d\n", alcGetError(cs->audio_capture_device));
381 return 0;
382 }
383
384 int err = OPUS_OK;
385 cs->audio_bitrate = AUDIO_BITRATE;
386 cs->audio_encoder = opus_encoder_create(AUDIO_SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP, &err);
387 err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_bitrate));
388 err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10));
389 err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
390
391 opus_encoder_init(cs->audio_encoder, AUDIO_SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP);
392
393 int nfo;
394 err = opus_encoder_ctl(cs->audio_encoder, OPUS_GET_LOOKAHEAD(&nfo));
395 /* printf("Encoder lookahead delay : %d\n", nfo); */
396 printf("init audio encoder successful\n");
397
398 return 1;
399}
400
401int init_encoder(codec_state *cs)
402{
403 avdevice_register_all();
404 avcodec_register_all();
405 avdevice_register_all();
406 av_register_all();
407
408 pthread_mutex_init(&cs->rtp_msg_mutex_lock, NULL);
409 pthread_mutex_init(&cs->avcodec_mutex_lock, NULL);
410
411 cs->support_send_video = init_send_video(cs);
412 cs->support_send_audio = init_send_audio(cs);
413
414 cs->send_audio = 1;
415 cs->send_video = 1;
416
417 return 1;
418}
419
420int init_decoder(codec_state *cs)
421{
422 avdevice_register_all();
423 avcodec_register_all();
424 avdevice_register_all();
425 av_register_all();
426
427 cs->receive_video = 0;
428 cs->receive_audio = 0;
429
430 cs->support_receive_video = init_receive_video(cs);
431 cs->support_receive_audio = init_receive_audio(cs);
432
433 cs->receive_audio = 1;
434 cs->receive_video = 1;
435
436 return 1;
437}
438
439int video_encoder_refresh(codec_state *cs, int bps)
440{
441 if (cs->video_encoder_ctx)
442 avcodec_close(cs->video_encoder_ctx);
443
444 cs->video_encoder = avcodec_find_encoder(VIDEO_CODEC);
445
446 if (!cs->video_encoder) {
447 printf("init video_encoder failed\n");
448 return -1;
449 }
450
451 cs->video_encoder_ctx = avcodec_alloc_context3(cs->video_encoder);
452
453 if (!cs->video_encoder_ctx) {
454 printf("init video_encoder_ctx failed\n");
455 return -1;
456 }
457
458 cs->video_encoder_ctx->bit_rate = bps;
459 cs->video_encoder_ctx->rc_min_rate = cs->video_encoder_ctx->rc_max_rate = cs->video_encoder_ctx->bit_rate;
460 av_opt_set_double(cs->video_encoder_ctx->priv_data, "max-intra-rate", 90, 0);
461 av_opt_set(cs->video_encoder_ctx->priv_data, "quality", "realtime", 0);
462
463 cs->video_encoder_ctx->thread_count = 4;
464 cs->video_encoder_ctx->rc_buffer_aggressivity = 0.95;
465 cs->video_encoder_ctx->rc_buffer_size = bps * 6;
466 cs->video_encoder_ctx->profile = 0;
467 cs->video_encoder_ctx->qmax = 54;
468 cs->video_encoder_ctx->qmin = 4;
469 AVRational myrational = {1, 25};
470 cs->video_encoder_ctx->time_base = myrational;
471 cs->video_encoder_ctx->gop_size = 99999;
472 cs->video_encoder_ctx->pix_fmt = PIX_FMT_YUV420P;
473 cs->video_encoder_ctx->width = cs->webcam_decoder_ctx->width;
474 cs->video_encoder_ctx->height = cs->webcam_decoder_ctx->height;
475
476 if (avcodec_open2(cs->video_encoder_ctx, cs->video_encoder, NULL) < 0) {
477 printf("opening video encoder failed\n");
478 return -1;
479 }
480 return 0;
481}
482
483void *encode_video_thread(void *arg)
484{
485 codec_state *cs = (codec_state *)arg;
486 AVPacket pkt1, *packet = &pkt1;
487 int p = 0;
488 int err;
489 int got_packet;
490 rtp_msg_t *s_video_msg;
491 int video_frame_finished;
492 AVFrame *s_video_frame;
493 AVFrame *webcam_frame;
494 s_video_frame = avcodec_alloc_frame();
495 webcam_frame = avcodec_alloc_frame();
496 AVPacket enc_video_packet;
497
498 uint8_t *buffer;
499 int numBytes;
500 /* Determine required buffer size and allocate buffer */
501 numBytes = avpicture_get_size(PIX_FMT_YUV420P, cs->webcam_decoder_ctx->width, cs->webcam_decoder_ctx->height);
502 buffer = (uint8_t *)av_calloc(numBytes * sizeof(uint8_t),1);
503 avpicture_fill((AVPicture *)s_video_frame, buffer, PIX_FMT_YUV420P, cs->webcam_decoder_ctx->width,
504 cs->webcam_decoder_ctx->height);
505 cs->sws_ctx = sws_getContext(cs->webcam_decoder_ctx->width, cs->webcam_decoder_ctx->height,
506 cs->webcam_decoder_ctx->pix_fmt, cs->webcam_decoder_ctx->width, cs->webcam_decoder_ctx->height, PIX_FMT_YUV420P,
507 SWS_BILINEAR, NULL, NULL, NULL);
508
509 while (!cs->quit && cs->send_video) {
510
511 if (av_read_frame(cs->video_format_ctx, packet) < 0) {
512 printf("error reading frame\n");
513
514 if (cs->video_format_ctx->pb->error != 0)
515 break;
516
517 continue;
518 }
519
520 if (packet->stream_index == cs->video_stream) {
521 if (avcodec_decode_video2(cs->webcam_decoder_ctx, webcam_frame, &video_frame_finished, packet) < 0) {
522 printf("couldn't decode\n");
523 continue;
524 }
525
526 av_free_packet(packet);
527 sws_scale(cs->sws_ctx, (uint8_t const * const *)webcam_frame->data, webcam_frame->linesize, 0,
528 cs->webcam_decoder_ctx->height, s_video_frame->data, s_video_frame->linesize);
529 /* create a new I-frame every 60 frames */
530 ++p;
531
532 if (p == 60) {
533
534 s_video_frame->pict_type = AV_PICTURE_TYPE_BI ;
535 } else if (p == 61) {
536 s_video_frame->pict_type = AV_PICTURE_TYPE_I ;
537 p = 0;
538 } else {
539 s_video_frame->pict_type = AV_PICTURE_TYPE_P ;
540 }
541
542 if (video_frame_finished) {
543 err = avcodec_encode_video2(cs->video_encoder_ctx, &enc_video_packet, s_video_frame, &got_packet);
544
545 if (err < 0) {
546 printf("could not encode video frame\n");
547 continue;
548 }
549
550 if (!got_packet) {
551 continue;
552 }
553
554 pthread_mutex_lock(&cs->rtp_msg_mutex_lock);
555 THREADLOCK()
556
557 if (!enc_video_packet.data) fprintf(stderr, "video packet data is NULL\n");
558
559 s_video_msg = rtp_msg_new ( cs->_rtp_video, enc_video_packet.data, enc_video_packet.size ) ;
560
561 if (!s_video_msg) {
562 printf("invalid message\n");
563 }
564
565 rtp_send_msg ( cs->_rtp_video, s_video_msg, cs->_networking );
566 THREADUNLOCK()
567 pthread_mutex_unlock(&cs->rtp_msg_mutex_lock);
568 av_free_packet(&enc_video_packet);
569 }
570 } else {
571 av_free_packet(packet);
572 }
573 }
574
575 /* clean up codecs */
576 pthread_mutex_lock(&cs->avcodec_mutex_lock);
577 av_free(buffer);
578 av_free(webcam_frame);
579 av_free(s_video_frame);
580 sws_freeContext(cs->sws_ctx);
581 avcodec_close(cs->webcam_decoder_ctx);
582 avcodec_close(cs->video_encoder_ctx);
583 pthread_mutex_unlock(&cs->avcodec_mutex_lock);
584 pthread_exit ( NULL );
585}
586
587void *encode_audio_thread(void *arg)
588{
589 codec_state *cs = (codec_state *)arg;
590 rtp_msg_t *s_audio_msg;
591 unsigned char encoded_data[4096];
592 int encoded_size = 0;
593 int16_t frame[4096];
594 int frame_size = AUDIO_FRAME_SIZE;
595 ALint sample = 0;
596 alcCaptureStart(cs->audio_capture_device);
597
598 while (!cs->quit && cs->send_audio) {
599 alcGetIntegerv(cs->audio_capture_device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample);
600
601 if (sample >= frame_size) {
602 alcCaptureSamples(cs->audio_capture_device, frame, frame_size);
603 encoded_size = opus_encode(cs->audio_encoder, frame, frame_size, encoded_data, 480);
604
605 if (encoded_size <= 0) {
606 printf("Could not encode audio packet\n");
607 } else {
608 pthread_mutex_lock(&cs->rtp_msg_mutex_lock);
609 THREADLOCK()
610 rtp_set_payload_type(cs->_rtp_audio, 96);
611 s_audio_msg = rtp_msg_new (cs->_rtp_audio, encoded_data, encoded_size) ;
612 rtp_send_msg ( cs->_rtp_audio, s_audio_msg, cs->_networking );
613 pthread_mutex_unlock(&cs->rtp_msg_mutex_lock);
614 THREADUNLOCK()
615 }
616 } else {
617 usleep(1000);
618 }
619 }
620
621 /* clean up codecs */
622 pthread_mutex_lock(&cs->avcodec_mutex_lock);
623 alcCaptureStop(cs->audio_capture_device);
624 alcCaptureCloseDevice(cs->audio_capture_device);
625
626 pthread_mutex_unlock(&cs->avcodec_mutex_lock);
627 pthread_exit ( NULL );
628}
629
630
631int video_decoder_refresh(codec_state *cs, int width, int height)
632{
633 printf("need to refresh\n");
634 screen = SDL_SetVideoMode(width, height, 0, 0);
635
636 if (cs->video_picture.bmp)
637 SDL_FreeYUVOverlay(cs->video_picture.bmp);
638
639 cs->video_picture.bmp = SDL_CreateYUVOverlay(width, height, SDL_YV12_OVERLAY, screen);
640 cs->sws_SDL_r_ctx = sws_getContext(width, height, cs->video_decoder_ctx->pix_fmt, width, height, PIX_FMT_YUV420P,
641 SWS_BILINEAR, NULL, NULL, NULL);
642 return 1;
643}
644
645void *decode_video_thread(void *arg)
646{
647 codec_state *cs = (codec_state *)arg;
648 cs->video_stream = 0;
649 rtp_msg_t *r_msg;
650 int dec_frame_finished;
651 AVFrame *r_video_frame;
652 r_video_frame = avcodec_alloc_frame();
653 AVPacket dec_video_packet;
654 av_new_packet (&dec_video_packet, 65536);
655 int width = 0;
656 int height = 0;
657
658 while (!cs->quit && cs->receive_video) {
659 r_msg = rtp_recv_msg ( cs->_rtp_video );
660
661 if (r_msg) {
662 memcpy(dec_video_packet.data, r_msg->_data, r_msg->_length);
663 dec_video_packet.size = r_msg->_length;
664 avcodec_decode_video2(cs->video_decoder_ctx, r_video_frame, &dec_frame_finished, &dec_video_packet);
665
666 if (dec_frame_finished) {
667 if (cs->video_decoder_ctx->width != width || cs->video_decoder_ctx->height != height) {
668 width = cs->video_decoder_ctx->width;
669 height = cs->video_decoder_ctx->height;
670 printf("w: %d h%d \n", width, height);
671 video_decoder_refresh(cs, width, height);
672 }
673
674 display_received_frame(cs, r_video_frame);
675 } else {
676 /* TODO: request the sender to create a new i-frame immediatly */
677 printf("bad video packet\n");
678 }
679
680 rtp_free_msg(cs->_rtp_video, r_msg);
681 }
682
683 usleep(1000);
684 }
685
686 printf("vend\n");
687 /* clean up codecs */
688 pthread_mutex_lock(&cs->avcodec_mutex_lock);
689 av_free(r_video_frame);
690 avcodec_close(cs->video_decoder_ctx);
691 pthread_mutex_unlock(&cs->avcodec_mutex_lock);
692 pthread_exit ( NULL );
693}
694
695void *decode_audio_thread(void *arg)
696{
697 codec_state *cs = (codec_state *)arg;
698 rtp_msg_t *r_msg;
699
700 int frame_size = AUDIO_FRAME_SIZE;
701 int data_size;
702
703 ALCdevice *dev;
704 ALCcontext *ctx;
705 ALuint source, *buffers;
706 dev = alcOpenDevice(NULL);
707 ctx = alcCreateContext(dev, NULL);
708 alcMakeContextCurrent(ctx);
709 int openal_buffers = 5;
710
711 buffers = calloc(sizeof(ALuint) * openal_buffers,1);
712 alGenBuffers(openal_buffers, buffers);
713 alGenSources((ALuint)1, &source);
714 alSourcei(source, AL_LOOPING, AL_FALSE);
715
716 ALuint buffer;
717 ALint val;
718
719 ALenum error;
720 uint16_t zeros[frame_size];
721 int i;
722
723 for (i = 0; i < frame_size; i++) {
724 zeros[i] = 0;
725 }
726
727 for (i = 0; i < openal_buffers; ++i) {
728 alBufferData(buffers[i], AL_FORMAT_MONO16, zeros, frame_size, 48000);
729 }
730
731 alSourceQueueBuffers(source, openal_buffers, buffers);
732 alSourcePlay(source);
733
734 if (alGetError() != AL_NO_ERROR) {
735 fprintf(stderr, "Error starting audio\n");
736 cs->quit = 1;
737 }
738
739 struct jitter_buffer *j_buf = NULL;
740
741 j_buf = create_queue(20);
742
743 int success = 0;
744
745 int dec_frame_len;
746
747 opus_int16 PCM[frame_size];
748
749 while (!cs->quit && cs->receive_audio) {
750 THREADLOCK()
751 r_msg = rtp_recv_msg ( cs->_rtp_audio );
752
753 if (r_msg) {
754 /* push the packet into the queue */
755 queue(j_buf, r_msg);
756 }
757
758 /* grab a packet from the queue */
759 success = 0;
760 alGetSourcei(source, AL_BUFFERS_PROCESSED, &val);
761
762 if (val > 0)
763 r_msg = dequeue(j_buf, &success);
764
765 if (success > 0) {
766 /* good packet */
767 if (success == 1) {
768 dec_frame_len = opus_decode(cs->audio_decoder, r_msg->_data, r_msg->_length, PCM, frame_size, 0);
769 rtp_free_msg(cs->_rtp_audio, r_msg);
770 }
771
772 /* lost packet */
773 if (success == 2) {
774 printf("lost packet\n");
775 dec_frame_len = opus_decode(cs->audio_decoder, NULL, 0, PCM, frame_size, 1);
776 }
777
778 if (dec_frame_len > 0) {
779 alGetSourcei(source, AL_BUFFERS_PROCESSED, &val);
780
781 if (val <= 0)
782 continue;
783
784 alSourceUnqueueBuffers(source, 1, &buffer);
785 data_size = av_samples_get_buffer_size(NULL, 1, dec_frame_len, AV_SAMPLE_FMT_S16, 1);
786 alBufferData(buffer, AL_FORMAT_MONO16, PCM, data_size, 48000);
787 int error = alGetError();
788
789 if (error != AL_NO_ERROR) {
790 fprintf(stderr, "Error setting buffer %d\n", error);
791 break;
792 }
793
794 alSourceQueueBuffers(source, 1, &buffer);
795
796 if (alGetError() != AL_NO_ERROR) {
797 fprintf(stderr, "error: could not buffer audio\n");
798 break;
799 }
800
801 alGetSourcei(source, AL_SOURCE_STATE, &val);
802
803 if (val != AL_PLAYING)
804 alSourcePlay(source);
805
806
807 }
808 }
809
810 THREADUNLOCK()
811 usleep(1000);
812 }
813
814 /* clean up codecs */
815 pthread_mutex_lock(&cs->avcodec_mutex_lock);
816
817 /* clean up openal */
818 alDeleteSources(1, &source);
819 alDeleteBuffers(openal_buffers, buffers);
820 alcMakeContextCurrent(NULL);
821 alcDestroyContext(ctx);
822 alcCloseDevice(dev);
823 pthread_mutex_unlock(&cs->avcodec_mutex_lock);
824 pthread_exit ( NULL );
825}
diff --git a/toxav/toxmedia.h b/toxav/toxmedia.h
new file mode 100644
index 00000000..7eea39ae
--- /dev/null
+++ b/toxav/toxmedia.h
@@ -0,0 +1,168 @@
1/* AV_codec.h
2 *
3 * Audio and video codec intitialisation, encoding/decoding and playback
4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24/*----------------------------------------------------------------------------------*/
25#ifndef _AVCODEC_H_
26#define _AVCODEC_H_
27
28#include <stdio.h>
29#include <math.h>
30#include <libavcodec/avcodec.h>
31#include <libavformat/avformat.h>
32#include <libswscale/swscale.h>
33#include <libavdevice/avdevice.h>
34#include <libavutil/opt.h>
35#include <pthread.h>
36#include <AL/al.h>
37#include <AL/alc.h>
38#include "toxrtp.h"
39#include "tox.h"
40
41#include <SDL/SDL.h>
42#include <opus/opus.h>
43
44/* ffmpeg VP8 codec ID */
45#define VIDEO_CODEC AV_CODEC_ID_VP8
46
47/* ffmpeg Opus codec ID */
48#define AUDIO_CODEC AV_CODEC_ID_OPUS
49
50/* default video bitrate in bytes/s */
51#define VIDEO_BITRATE 10*1000
52
53/* default audio bitrate in bytes/s */
54#define AUDIO_BITRATE 64000
55
56/* audio frame duration in miliseconds */
57#define AUDIO_FRAME_DURATION 20
58
59/* audio sample rate recommended to be 48kHz for Opus */
60#define AUDIO_SAMPLE_RATE 48000
61
62/* the amount of samples in one audio frame */
63#define AUDIO_FRAME_SIZE AUDIO_SAMPLE_RATE*AUDIO_FRAME_DURATION/1000
64
65/* the quit event for SDL */
66#define FF_QUIT_EVENT (SDL_USEREVENT + 2)
67
68#ifdef __linux__
69#define VIDEO_DRIVER "video4linux2"
70#define DEFAULT_WEBCAM "/dev/video0"
71#endif
72
73#ifdef WIN32
74#define VIDEO_DRIVER "vfwcap"
75#define DEFAULT_WEBCAM "0"
76#endif
77
78extern SDL_Surface *screen;
79
80typedef struct {
81 SDL_Overlay *bmp;
82 int width, height;
83} VideoPicture;
84
85
86typedef struct {
87 uint8_t send_audio;
88 uint8_t receive_audio;
89 uint8_t send_video;
90 uint8_t receive_video;
91
92 uint8_t support_send_audio;
93 uint8_t support_send_video;
94 uint8_t support_receive_audio;
95 uint8_t support_receive_video;
96
97 /* video encoding */
98 AVInputFormat *video_input_format;
99 AVFormatContext *video_format_ctx;
100 uint8_t video_stream;
101 AVCodecContext *webcam_decoder_ctx;
102 AVCodec *webcam_decoder;
103 AVCodecContext *video_encoder_ctx;
104 AVCodec *video_encoder;
105
106 /* video decoding */
107 AVCodecContext *video_decoder_ctx;
108 AVCodec *video_decoder;
109
110 /* audio encoding */
111 ALCdevice *audio_capture_device;
112 OpusEncoder *audio_encoder;
113 int audio_bitrate;
114
115 /* audio decoding */
116 OpusDecoder *audio_decoder;
117
118 uint8_t req_video_refresh;
119
120 /* context for converting image format to something SDL can use*/
121 struct SwsContext *sws_SDL_r_ctx;
122
123 /* context for converting webcam image format to something the video encoder can use */
124 struct SwsContext *sws_ctx;
125
126 /* rendered video picture, ready for display */
127 VideoPicture video_picture;
128
129 rtp_session_t *_rtp_video;
130 rtp_session_t *_rtp_audio;
131 int socket;
132 Networking_Core *_networking;
133
134 pthread_t encode_audio_thread;
135 pthread_t encode_video_thread;
136
137 pthread_t decode_audio_thread;
138 pthread_t decode_video_thread;
139
140 pthread_mutex_t rtp_msg_mutex_lock;
141 pthread_mutex_t avcodec_mutex_lock;
142
143 uint8_t quit;
144 SDL_Event SDL_event;
145
146 msi_session_t *_msi;
147 uint32_t _frame_rate;
148 uint16_t _send_port, _recv_port;
149 int _tox_sock;
150 //pthread_id _medialoop_id;
151
152} codec_state;
153
154int display_received_frame(codec_state *cs, AVFrame *r_video_frame);
155int init_receive_audio(codec_state *cs);
156int init_decoder(codec_state *cs);
157int init_send_video(codec_state *cs);
158int init_send_audio(codec_state *cs);
159int init_encoder(codec_state *cs);
160int video_encoder_refresh(codec_state *cs, int bps);
161void *encode_video_thread(void *arg);
162void *encode_audio_thread(void *arg);
163int video_decoder_refresh(codec_state *cs, int width, int height);
164int handle_rtp_video_packet(codec_state *cs, rtp_msg_t *r_msg);
165void *decode_video_thread(void *arg);
166void *decode_audio_thread(void *arg);
167
168#endif
diff --git a/toxav/toxmsi.c b/toxav/toxmsi.c
new file mode 100755
index 00000000..cf0914ab
--- /dev/null
+++ b/toxav/toxmsi.c
@@ -0,0 +1,1337 @@
1/** toxmsi.c
2 *
3 * Copyright (C) 2013 Tox project All Rights Reserved.
4 *
5 * This file is part of Tox.
6 *
7 * Tox is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Tox is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 * Report bugs/suggestions to me ( mannol ) at either #tox-dev @ freenode.net:6667 or
22 * my email: eniz_vukovic@hotmail.com
23 */
24
25
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif /* HAVE_CONFIG_H */
29
30#define _BSD_SOURCE
31
32#include "toxmsi.h"
33#include "../toxcore/util.h"
34#include "../toxcore/network.h"
35#include "../toxcore/event.h"
36#include "../toxcore/Messenger.h"
37
38#include <assert.h>
39#include <unistd.h>
40#include <string.h>
41#include <stdlib.h>
42
43#define same(x, y) strcmp((const char*) x, (const char*) y) == 0
44
45#define MSI_MAXMSG_SIZE 1024
46
47#define TYPE_REQUEST 1
48#define TYPE_RESPONSE 2
49
50#define VERSION_STRING "0.3.1"
51#define VERSION_STRLEN 5
52
53#define CT_AUDIO_HEADER_VALUE "AUDIO"
54#define CT_VIDEO_HEADER_VALUE "VIDEO"
55
56
57/* Define default timeout for a request.
58 * There is no behavior specified by the msi on what will
59 * client do on timeout, but to call timeout callback.
60 */
61#define m_deftout 10000 /* in milliseconds */
62
63/**
64 * Protocol:
65 *
66 * | desc. ( 1 byte ) | length ( 2 bytes ) | value ( length bytes ) |
67 *
68 * ie.
69 *
70 * | 0x1 | 0x0 0x7 | "version"
71 *
72 * Means: it's field value with length of 7 bytes and value of "version"
73 * It's similar to amp protocol
74 */
75
76
77#define GENERIC_HEADER(header) \
78typedef struct _MSIHeader##header { \
79uint8_t* header_value; \
80uint16_t size; \
81} MSIHeader##header;
82
83
84GENERIC_HEADER ( Version )
85GENERIC_HEADER ( Request )
86GENERIC_HEADER ( Response )
87GENERIC_HEADER ( CallType )
88GENERIC_HEADER ( UserAgent )
89GENERIC_HEADER ( CallId )
90GENERIC_HEADER ( Info )
91GENERIC_HEADER ( Reason )
92GENERIC_HEADER ( CryptoKey )
93GENERIC_HEADER ( Nonce )
94
95
96/**
97 * @brief This is the message structure. It contains all of the headers and
98 * destination/source of the message stored in friend_id.
99 *
100 */
101typedef struct _MSIMessage {
102
103 MSIHeaderVersion version;
104 MSIHeaderRequest request;
105 MSIHeaderResponse response;
106 MSIHeaderCallType calltype;
107 MSIHeaderUserAgent useragent;
108 MSIHeaderInfo info;
109 MSIHeaderReason reason;
110 MSIHeaderCallId callid;
111 MSIHeaderCryptoKey cryptokey;
112 MSIHeaderNonce nonce;
113
114 struct _MSIMessage* next;
115
116 int friend_id;
117
118} MSIMessage;
119
120
121
122static MSICallback callbacks[9] = {0};
123
124
125/* define strings for the identifiers */
126#define VERSION_FIELD "Version"
127#define REQUEST_FIELD "Request"
128#define RESPONSE_FIELD "Response"
129#define INFO_FIELD "INFO"
130#define REASON_FIELD "Reason"
131#define CALLTYPE_FIELD "Call-type"
132#define USERAGENT_FIELD "User-agent"
133#define CALLID_FIELD "Call-id"
134#define CRYPTOKEY_FIELD "Crypto-key"
135#define NONCE_FIELD "Nonce"
136
137/* protocol descriptors */
138#define end_byte 0x0
139#define field_byte 0x1
140#define value_byte 0x2
141
142
143typedef enum {
144 invite,
145 start,
146 cancel,
147 reject,
148 end,
149
150} MSIRequest;
151
152
153/**
154 * @brief Get string value for request.
155 *
156 * @param request The request.
157 * @return const uint8_t* The string
158 */
159static inline const uint8_t *stringify_request ( MSIRequest request ) {
160 static const uint8_t* strings[] = {
161 ( uint8_t* ) "INVITE",
162 ( uint8_t* ) "START",
163 ( uint8_t* ) "CANCEL",
164 ( uint8_t* ) "REJECT",
165 ( uint8_t* ) "END"
166 };
167
168 return strings[request];
169}
170
171
172typedef enum {
173 ringing,
174 starting,
175 ending,
176 error
177
178} MSIResponse;
179
180
181/**
182 * @brief Get string value for response.
183 *
184 * @param response The response.
185 * @return const uint8_t* The string
186 */
187static inline const uint8_t *stringify_response ( MSIResponse response ) {
188 static const uint8_t* strings[] = {
189 ( uint8_t* ) "ringing",
190 ( uint8_t* ) "starting",
191 ( uint8_t* ) "ending",
192 ( uint8_t* ) "error"
193 };
194
195 return strings[response];
196}
197
198
199#define ON_HEADER(iterator, header, descriptor, size_const) \
200( memcmp(iterator, descriptor, size_const) == 0){ /* Okay */ \
201 iterator += size_const; /* Set iterator at begining of value part */ \
202 if ( *iterator != value_byte ) { assert(0); return -1; }\
203 iterator ++;\
204 uint16_t _value_size = (uint16_t) *(iterator ) << 8 | \
205 (uint16_t) *(iterator + 1); \
206 header.header_value = calloc(sizeof(uint8_t), _value_size); \
207 header.size = _value_size; \
208 memcpy(header.header_value, iterator + 2, _value_size);\
209 iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ \
210}
211
212/**
213 * @brief Parse raw 'data' received from socket into MSIMessage struct.
214 * Every message has to have end value of 'end_byte' or _undefined_ behavior
215 * occures. The best practice is to check the end of the message at the handle_packet.
216 *
217 * @param msg Container.
218 * @param data The data.
219 * @return int
220 * @retval -1 Error occured.
221 * @retval 0 Success.
222 */
223int parse_raw_data ( MSIMessage* msg, const uint8_t* data ) {
224 assert ( msg );
225
226 const uint8_t* _it = data;
227
228 while ( *_it ) {/* until end_byte is hit */
229
230 if ( *_it == field_byte ) {
231 uint16_t _size = ( uint16_t ) * ( _it + 1 ) << 8 |
232 ( uint16_t ) * ( _it + 2 );
233
234 _it += 3; /*place it at the field value beginning*/
235
236 switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */
237
238 case 4: { /* INFO header */
239 if ON_HEADER ( _it, msg->info, INFO_FIELD, 4 )
240 }
241 break;
242
243 case 5: { /* NONCE header */
244 if ON_HEADER ( _it, msg->nonce, NONCE_FIELD, 5 )
245 }
246 break;
247
248 case 6: { /* Reason header */
249 if ON_HEADER ( _it, msg->reason, REASON_FIELD, 6 )
250 }
251 break;
252
253 case 7: { /* Version, Request, Call-id headers */
254 if ON_HEADER ( _it, msg->version, VERSION_FIELD, 7 )
255 else if ON_HEADER ( _it, msg->request, REQUEST_FIELD, 7 )
256 else if ON_HEADER ( _it, msg->callid, CALLID_FIELD, 7 )
257 }
258 break;
259
260 case 8: { /* Response header */
261 if ON_HEADER ( _it, msg->response, RESPONSE_FIELD, 8 )
262 }
263 break;
264
265 case 9: { /* Call-type header */
266 if ON_HEADER ( _it, msg->calltype, CALLTYPE_FIELD, 9 )
267 }
268 break;
269
270 case 10: { /* User-agent, Crypto-key headers */
271 if ON_HEADER ( _it, msg->useragent, USERAGENT_FIELD, 10 )
272 else if ON_HEADER ( _it, msg->cryptokey, CRYPTOKEY_FIELD, 10 )
273 }
274 break;
275
276 default:
277 return -1;
278 }
279 } else return -1;
280 /* If it's anything else return failure as the message is invalid */
281
282 }
283
284 return 0;
285}
286
287
288#define ALLOCATE_HEADER( var, mheader_value, t_size) \
289var.header_value = calloc(sizeof *mheader_value, t_size); \
290memcpy(var.header_value, mheader_value, t_size); \
291var.size = t_size;
292
293
294/**
295 * @brief Speaks for it self.
296 *
297 * @param msg The message.
298 * @return void
299 */
300void free_message ( MSIMessage* msg ) {
301 assert ( msg );
302
303 free ( msg->calltype.header_value );
304 free ( msg->request.header_value );
305 free ( msg->response.header_value );
306 free ( msg->useragent.header_value );
307 free ( msg->version.header_value );
308 free ( msg->info.header_value );
309 free ( msg->cryptokey.header_value );
310 free ( msg->nonce.header_value );
311 free ( msg->reason.header_value );
312 free ( msg->callid.header_value );
313
314 free ( msg );
315}
316
317
318/**
319 * @brief Create the message.
320 *
321 * @param type Request or response.
322 * @param type_id Type of request/response.
323 * @return MSIMessage* Created message.
324 * @retval NULL Error occured.
325 */
326MSIMessage* msi_new_message ( uint8_t type, const uint8_t* type_id ) {
327 MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 );
328 assert ( _retu );
329
330 memset ( _retu, 0, sizeof ( MSIMessage ) );
331
332 if ( type == TYPE_REQUEST ) {
333 ALLOCATE_HEADER ( _retu->request, type_id, strlen ( type_id ) )
334
335 } else if ( type == TYPE_RESPONSE ) {
336 ALLOCATE_HEADER ( _retu->response, type_id, strlen ( type_id ) )
337
338 } else {
339 free_message ( _retu );
340 return NULL;
341 }
342
343 ALLOCATE_HEADER ( _retu->version, VERSION_STRING, strlen ( VERSION_STRING ) )
344
345 return _retu;
346}
347
348
349/**
350 * @brief Parse data from handle_packet.
351 *
352 * @param data The data.
353 * @return MSIMessage* Parsed message.
354 * @retval NULL Error occured.
355 */
356MSIMessage* parse_message ( const uint8_t* data ) {
357 assert ( data );
358
359 MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 );
360 assert ( _retu );
361
362 memset ( _retu, 0, sizeof ( MSIMessage ) );
363
364 if ( parse_raw_data ( _retu, data ) == -1 ) {
365
366 free_message ( _retu );
367 return NULL;
368 }
369
370 if ( !_retu->version.header_value || VERSION_STRLEN != _retu->version.size ||
371 memcmp ( _retu->version.header_value, VERSION_STRING, VERSION_STRLEN ) != 0 ) {
372
373 free_message ( _retu );
374 return NULL;
375 }
376
377 return _retu;
378}
379
380
381
382/**
383 * @brief Speaks for it self.
384 *
385 * @param dest Container.
386 * @param header_field Field.
387 * @param header_value Field value.
388 * @param value_len Length of field value.
389 * @param length Pointer to container length.
390 * @return uint8_t* Iterated container.
391 */
392uint8_t* append_header_to_string (
393 uint8_t* dest,
394 const uint8_t* header_field,
395 const uint8_t* header_value,
396 uint16_t value_len,
397 uint16_t* length )
398{
399 assert ( dest );
400 assert ( header_value );
401 assert ( header_field );
402
403 const uint8_t* _hvit = header_value;
404 uint16_t _total = 6 + value_len; /* 6 is known plus header value len + field len*/
405
406 *dest = field_byte; /* Set the first byte */
407
408 uint8_t* _getback_byte = dest + 1; /* remeber the byte we were on */
409 dest += 3; /* swith to 4th byte where field value starts */
410
411 /* Now set the field value and calculate it's length */
412 uint16_t _i = 0;
413 for ( ; header_field[_i]; ++_i ) {
414 *dest = header_field[_i];
415 ++dest;
416 };
417 _total += _i;
418
419 /* Now set the length of the field byte */
420 *_getback_byte = ( uint8_t ) _i >> 8;
421 _getback_byte++;
422 *_getback_byte = ( uint8_t ) _i;
423
424 /* for value part do it regulary */
425 *dest = value_byte;
426 dest++;
427
428 *dest = ( uint8_t ) value_len >> 8;
429 dest++;
430 *dest = ( uint8_t ) value_len;
431 dest++;
432
433 for ( _i = value_len; _i; --_i ) {
434 *dest = *_hvit;
435 ++_hvit;
436 ++dest;
437 }
438
439 *length += _total;
440 return dest;
441}
442
443
444#define CLEAN_ASSIGN(added, var, field, header)\
445if ( header.header_value ) { var = append_header_to_string(var, (const uint8_t*)field, header.header_value, header.size, &added); }
446
447
448/**
449 * @brief Convert MSIMessage struct to _sendable_ string.
450 *
451 * @param msg The message.
452 * @param dest Destination.
453 * @return uint16_t It's final size.
454 */
455uint16_t message_to_string ( MSIMessage* msg, uint8_t* dest ) {
456 assert ( msg );
457 assert ( dest );
458
459 uint8_t* _iterated = dest;
460 uint16_t _size = 0;
461
462 CLEAN_ASSIGN ( _size, _iterated, VERSION_FIELD, msg->version );
463 CLEAN_ASSIGN ( _size, _iterated, REQUEST_FIELD, msg->request );
464 CLEAN_ASSIGN ( _size, _iterated, RESPONSE_FIELD, msg->response );
465 CLEAN_ASSIGN ( _size, _iterated, CALLTYPE_FIELD, msg->calltype );
466 CLEAN_ASSIGN ( _size, _iterated, USERAGENT_FIELD, msg->useragent );
467 CLEAN_ASSIGN ( _size, _iterated, INFO_FIELD, msg->info );
468 CLEAN_ASSIGN ( _size, _iterated, CALLID_FIELD, msg->callid );
469 CLEAN_ASSIGN ( _size, _iterated, REASON_FIELD, msg->reason );
470 CLEAN_ASSIGN ( _size, _iterated, CRYPTOKEY_FIELD, msg->cryptokey );
471 CLEAN_ASSIGN ( _size, _iterated, NONCE_FIELD, msg->nonce );
472
473 *_iterated = end_byte;
474 _size ++;
475
476 return _size;
477}
478
479
480#define GENERIC_SETTER_DEFINITION(header) \
481void msi_msg_set_##header ( MSIMessage* _msg, const uint8_t* header_value, uint16_t _size ) \
482{ assert(_msg); assert(header_value); \
483 free(_msg->header.header_value); \
484 ALLOCATE_HEADER( _msg->header, header_value, _size )}
485
486GENERIC_SETTER_DEFINITION ( calltype )
487GENERIC_SETTER_DEFINITION ( useragent )
488GENERIC_SETTER_DEFINITION ( reason )
489GENERIC_SETTER_DEFINITION ( info )
490GENERIC_SETTER_DEFINITION ( callid )
491GENERIC_SETTER_DEFINITION ( cryptokey )
492GENERIC_SETTER_DEFINITION ( nonce )
493
494
495/**
496 * @brief Generate _random_ alphanumerical string.
497 *
498 * @param str Destination.
499 * @param size Size of string.
500 * @return void
501 */
502void t_randomstr ( uint8_t* str, size_t size ) {
503 assert ( str );
504
505 static const uint8_t _bytes[] =
506 "0123456789"
507 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
508 "abcdefghijklmnopqrstuvwxyz";
509
510 int _it = 0;
511
512 for ( ; _it < size; _it++ ) {
513 str[_it] = _bytes[ randombytes_random() % 61 ];
514 }
515}
516
517
518typedef enum {
519 error_deadcall = 1, /* has call id but it's from old call */
520 error_id_mismatch, /* non-existing call */
521
522 error_no_callid, /* not having call id */
523 error_no_call, /* no call in session */
524 error_no_crypto_key, /* no crypto key */
525
526 error_busy,
527
528} MSICallError; /* Error codes */
529
530
531/**
532 * @brief Stringify error code.
533 *
534 * @param error_code The code.
535 * @return const uint8_t* The string.
536 */
537static inline const uint8_t *stringify_error ( MSICallError error_code ) {
538 static const uint8_t* strings[] = {
539 ( uint8_t* ) "",
540 ( uint8_t* ) "Using dead call",
541 ( uint8_t* ) "Call id not set to any call",
542 ( uint8_t* ) "Call id not available",
543 ( uint8_t* ) "No active call in session",
544 ( uint8_t* ) "No Crypto-key set",
545 ( uint8_t* ) "Callee busy"
546 };
547
548 return strings[error_code];
549}
550
551
552/**
553 * @brief Convert error_code into string.
554 *
555 * @param error_code The code.
556 * @return const uint8_t* The string.
557 */
558static inline const uint8_t *stringify_error_code ( MSICallError error_code ) {
559 static const uint8_t* strings[] = {
560 ( uint8_t* ) "",
561 ( uint8_t* ) "1",
562 ( uint8_t* ) "2",
563 ( uint8_t* ) "3",
564 ( uint8_t* ) "4",
565 ( uint8_t* ) "5",
566 ( uint8_t* ) "6"
567 };
568
569 return strings[error_code];
570}
571
572
573/**
574 * @brief Speaks for it self.
575 *
576 * @param session Control session.
577 * @param msg The message.
578 * @param to Where to.
579 * @return int
580 * @retval -1 Error occured.
581 * @retval 0 Success.
582 */
583int send_message ( MSISession* session, MSIMessage* msg, uint32_t to )
584{
585 msi_msg_set_callid ( msg, session->call->id, CALL_ID_LEN );
586
587 uint8_t _msg_string_final [MSI_MAXMSG_SIZE];
588 uint16_t _length = message_to_string ( msg, _msg_string_final );
589
590 return m_msi_packet((struct Messenger*) session->messenger_handle, to, _msg_string_final, _length) ? 0 : -1;
591}
592
593
594/**
595 * @brief Speaks for it self.
596 *
597 * @param session Control session.
598 * @param msg The message.
599 * @param peer_id The peer.
600 * @return void
601 */
602void flush_peer_type ( MSISession* session, MSIMessage* msg, int peer_id ) {
603 if ( msg->calltype.header_value ) {
604 if ( strcmp ( ( const char* ) msg->calltype.header_value, CT_AUDIO_HEADER_VALUE ) == 0 ) {
605 session->call->type_peer[peer_id] = type_audio;
606
607 } else if ( strcmp ( ( const char* ) msg->calltype.header_value, CT_VIDEO_HEADER_VALUE ) == 0 ) {
608 session->call->type_peer[peer_id] = type_video;
609 } else {} /* Error */
610 } else {} /* Error */
611}
612
613
614
615/**
616 * @brief Sends error response to peer.
617 *
618 * @param session The session.
619 * @param errid The id.
620 * @param to Where to?
621 * @return int
622 * @retval 0 It's always success.
623 */
624int handle_error ( MSISession* session, MSICallError errid, uint32_t to ) {
625 MSIMessage* _msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) );
626
627 const uint8_t* _error_code_str = stringify_error_code ( errid );
628
629 msi_msg_set_reason ( _msg_error, _error_code_str, strlen ( ( const char* ) _error_code_str ) );
630 send_message ( session, _msg_error, to );
631 free_message ( _msg_error );
632
633 session->last_error_id = errid;
634 session->last_error_str = stringify_error ( errid );
635
636 event.rise ( callbacks[cb_error], session );
637
638 return 0;
639}
640
641
642/**
643 * @brief Determine the error if any.
644 *
645 * @param session Control session.
646 * @param msg The message.
647 * @return int
648 * @retval -1 No error.
649 * @retval 0 Error occured and response sent.
650 */
651int has_call_error ( MSISession* session, MSIMessage* msg ) {
652 if ( !msg->callid.header_value ) {
653 return handle_error ( session, error_no_callid, msg->friend_id );
654
655 } else if ( !session->call ) {
656 return handle_error ( session, error_no_call, msg->friend_id );
657
658 } else if ( memcmp ( session->call->id, msg->callid.header_value, CALL_ID_LEN ) != 0 ) {
659 return handle_error ( session, error_id_mismatch, msg->friend_id );
660
661 }
662
663 return -1;
664}
665
666
667/**
668 * @brief Function called at request timeout.
669 *
670 * @param arg Control session
671 * @return void*
672 */
673void* handle_timeout ( void* arg )
674{
675 /* Send hangup either way */
676 MSISession* _session = arg;
677
678 uint32_t* _peers = _session->call->peers;
679 uint16_t _peer_count = _session->call->peer_count;
680
681
682 /* Cancel all? */
683 uint16_t _it = 0;
684 for ( ; _it < _peer_count; _it++ )
685 msi_cancel ( arg, _peers[_it] );
686
687
688 ( *callbacks[cb_timeout] ) ( arg );
689 ( *callbacks[cb_ending ] ) ( arg );
690
691 return NULL;
692}
693
694
695/**
696 * @brief Add peer to peer list.
697 *
698 * @param call What call.
699 * @param peer_id Its id.
700 * @return void
701 */
702void add_peer( MSICall* call, int peer_id )
703{
704 if ( !call->peers ) {
705 call->peers = calloc(sizeof(int), 1);
706 call->peer_count = 1;
707 } else{
708 call->peer_count ++;
709 call->peers = realloc( call->peers, sizeof(int) * call->peer_count);
710 }
711
712 call->peers[call->peer_count - 1] = peer_id;
713}
714
715
716/**
717 * @brief BASIC call flow:
718 *
719 * ALICE BOB
720 * | invite --> |
721 * | |
722 * | <-- ringing |
723 * | |
724 * | <-- starting |
725 * | |
726 * | start --> |
727 * | |
728 * | <-- MEDIA TRANS --> |
729 * | |
730 * | end --> |
731 * | |
732 * | <-- ending |
733 *
734 * Alice calls Bob by sending invite packet.
735 * Bob recvs the packet and sends an ringing packet;
736 * which notifies Alice that her invite is acknowledged.
737 * Ringing screen shown on both sides.
738 * Bob accepts the invite for a call by sending starting packet.
739 * Alice recvs the starting packet and sends the started packet to
740 * inform Bob that she recved the starting packet.
741 * Now the media transmission is established ( i.e. RTP transmission ).
742 * Alice hangs up and sends end packet.
743 * Bob recves the end packet and sends ending packet
744 * as the acknowledgement that the call is ending.
745 *
746 *
747 */
748void msi_handle_packet ( Messenger* messenger, int source, uint8_t* data, uint16_t length, void* object )
749{
750 MSISession* _session = object;
751 MSIMessage* _msg;
752
753 _msg = parse_message ( data );
754
755 if ( !_msg ) return;
756
757 _msg->friend_id = source;
758
759
760 /* Now handle message */
761
762 if ( _msg->request.header_value ) { /* Handle request */
763
764 const uint8_t* _request_value = _msg->request.header_value;
765
766 if ( same ( _request_value, stringify_request ( invite ) ) ) {
767 handle_recv_invite ( _session, _msg );
768
769 } else if ( same ( _request_value, stringify_request ( start ) ) ) {
770 handle_recv_start ( _session, _msg );
771
772 } else if ( same ( _request_value, stringify_request ( cancel ) ) ) {
773 handle_recv_cancel ( _session, _msg );
774
775 } else if ( same ( _request_value, stringify_request ( reject ) ) ) {
776 handle_recv_reject ( _session, _msg );
777
778 } else if ( same ( _request_value, stringify_request ( end ) ) ) {
779 handle_recv_end ( _session, _msg );
780 }
781
782 else {
783 free_message ( _msg );
784 return;
785 }
786
787 } else if ( _msg->response.header_value ) { /* Handle response */
788
789 const uint8_t* _response_value = _msg->response.header_value;
790
791 if ( same ( _response_value, stringify_response ( ringing ) ) ) {
792 handle_recv_ringing ( _session, _msg );
793
794 } else if ( same ( _response_value, stringify_response ( starting ) ) ) {
795 handle_recv_starting ( _session, _msg );
796
797 } else if ( same ( _response_value, stringify_response ( ending ) ) ) {
798 handle_recv_ending ( _session, _msg );
799
800 } else if ( same ( _response_value, stringify_response ( error ) ) ) {
801 handle_recv_error ( _session, _msg );
802 } else {
803 free_message ( _msg );
804 return;
805 }
806
807 /* Got response so cancel timer */
808 if ( _session->call )
809 event.timer_release ( _session->call->request_timer_id );
810
811 }
812
813 free_message ( _msg );
814}
815
816
817/**
818 * @brief Speaks for it self.
819 *
820 * @param session Control session.
821 * @param peers Amount of peers. (Currently it only supports 1)
822 * @param ringing_timeout Ringing timeout.
823 * @return MSICall* The created call.
824 */
825MSICall* init_call ( MSISession* session, int peers, int ringing_timeout ) {
826 assert ( session );
827 assert ( peers );
828
829 MSICall* _call = calloc ( sizeof ( MSICall ), 1 );
830 _call->type_peer = calloc ( sizeof ( MSICallType ), peers );
831
832 assert ( _call );
833 assert ( _call->type_peer );
834
835 /*_call->_participant_count = _peers;*/
836
837 _call->request_timer_id = 0;
838 _call->ringing_timer_id = 0;
839
840 _call->key_local = NULL;
841 _call->key_peer = NULL;
842 _call->nonce_local = NULL;
843 _call->nonce_peer = NULL;
844
845 _call->ringing_tout_ms = ringing_timeout;
846
847 pthread_mutex_init ( &_call->mutex, NULL );
848
849 return _call;
850}
851
852
853/**
854 * @brief Terminate the call.
855 *
856 * @param session Control session.
857 * @return int
858 * @retval -1 Error occured.
859 * @retval 0 Success.
860 */
861int terminate_call ( MSISession* session ) {
862 assert ( session );
863
864 if ( !session->call )
865 return -1;
866
867
868 /* Check event loop and cancel timed events if there are any
869 * Notice: This has to be done before possibly
870 * locking the mutex the second time
871 */
872 event.timer_release ( session->call->request_timer_id );
873 event.timer_release ( session->call->ringing_timer_id );
874
875 /* Get a handle */
876 pthread_mutex_lock ( &session->call->mutex );
877
878 MSICall* _call = session->call;
879 session->call = NULL;
880
881 free ( _call->type_peer );
882 free ( _call->key_local );
883 free ( _call->key_peer );
884 free ( _call->peers);
885
886 /* Release handle */
887 pthread_mutex_unlock ( &_call->mutex );
888
889 pthread_mutex_destroy ( &_call->mutex );
890
891 free ( _call );
892
893 return 0;
894}
895
896
897/********** Request handlers **********/
898int handle_recv_invite ( MSISession* session, MSIMessage* msg ) {
899 assert ( session );
900
901 if ( session->call ) {
902 handle_error ( session, error_busy, msg->friend_id );
903 return 0;
904 }
905 if ( !msg->callid.header_value ) {
906 handle_error ( session, error_no_callid, msg->friend_id );
907 return 0;
908 }
909
910 session->call = init_call ( session, 1, 0 );
911 memcpy ( session->call->id, msg->callid.header_value, CALL_ID_LEN );
912 session->call->state = call_starting;
913
914 add_peer( session->call, msg->friend_id);
915
916 flush_peer_type ( session, msg, 0 );
917
918 MSIMessage* _msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) );
919 send_message ( session, _msg_ringing, msg->friend_id );
920 free_message ( _msg_ringing );
921
922 event.rise ( callbacks[cb_oninvite], session );
923
924 return 1;
925}
926int handle_recv_start ( MSISession* session, MSIMessage* msg ) {
927 assert ( session );
928
929 if ( has_call_error ( session, msg ) == 0 )
930 return 0;
931
932 if ( !msg->cryptokey.header_value )
933 return handle_error ( session, error_no_crypto_key, msg->friend_id );
934
935 session->call->state = call_active;
936
937 session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES );
938 memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES );
939
940 session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
941 memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES );
942
943 flush_peer_type ( session, msg, 0 );
944
945 event.rise ( callbacks[cb_onstart], session );
946
947 return 1;
948}
949int handle_recv_reject ( MSISession* session, MSIMessage* msg ) {
950 assert ( session );
951
952 if ( has_call_error ( session, msg ) == 0 )
953 return 0;
954
955
956 MSIMessage* _msg_end = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
957 send_message ( session, _msg_end, msg->friend_id );
958 free_message ( _msg_end );
959
960 event.timer_release ( session->call->request_timer_id );
961 event.rise ( callbacks[cb_onreject], session );
962 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
963
964 return 1;
965}
966int handle_recv_cancel ( MSISession* session, MSIMessage* msg ) {
967 assert ( session );
968
969 if ( has_call_error ( session, msg ) == 0 )
970 return 0;
971
972
973 terminate_call ( session );
974
975 event.rise ( callbacks[cb_oncancel], session );
976
977 return 1;
978}
979int handle_recv_end ( MSISession* session, MSIMessage* msg ) {
980 assert ( session );
981
982 if ( has_call_error ( session, msg ) == 0 )
983 return 0;
984
985
986 MSIMessage* _msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
987 send_message ( session, _msg_ending, msg->friend_id );
988 free_message ( _msg_ending );
989
990 terminate_call ( session );
991
992 event.rise ( callbacks[cb_onend], session );
993
994 return 1;
995}
996
997/********** Response handlers **********/
998int handle_recv_ringing ( MSISession* session, MSIMessage* msg ) {
999 assert ( session );
1000
1001 if ( has_call_error ( session, msg ) == 0 )
1002 return 0;
1003
1004 session->call->ringing_timer_id = event.timer_alloc ( handle_timeout, session, session->call->ringing_tout_ms );
1005 event.rise ( callbacks[cb_ringing], session );
1006
1007 return 1;
1008}
1009int handle_recv_starting ( MSISession* session, MSIMessage* msg ) {
1010 assert ( session );
1011
1012 if ( has_call_error ( session, msg ) == 0 )
1013 return 0;
1014
1015 if ( !msg->cryptokey.header_value ) {
1016 return handle_error ( session, error_no_crypto_key, msg->friend_id );
1017 }
1018
1019 /* Generate local key/nonce to send */
1020 session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES );
1021 new_symmetric_key ( session->call->key_local );
1022
1023 session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
1024 new_nonce ( session->call->nonce_local );
1025
1026 /* Save peer key/nonce */
1027 session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES );
1028 memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES );
1029
1030 session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
1031 memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES );
1032
1033 session->call->state = call_active;
1034
1035 MSIMessage* _msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) );
1036 msi_msg_set_cryptokey ( _msg_start, session->call->key_local, crypto_secretbox_KEYBYTES );
1037 msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_box_NONCEBYTES );
1038 send_message ( session, _msg_start, msg->friend_id );
1039 free_message ( _msg_start );
1040
1041 flush_peer_type ( session, msg, 0 );
1042
1043 event.rise ( callbacks[cb_starting], session );
1044 event.timer_release ( session->call->ringing_timer_id );
1045
1046 return 1;
1047}
1048int handle_recv_ending ( MSISession* session, MSIMessage* msg ) {
1049 assert ( session );
1050
1051 if ( has_call_error ( session, msg ) == 0 )
1052 return 0;
1053
1054
1055 terminate_call ( session );
1056
1057 event.rise ( callbacks[cb_ending], session );
1058
1059 return 1;
1060}
1061int handle_recv_error ( MSISession* session, MSIMessage* msg ) {
1062 assert ( session );
1063 assert ( session->call );
1064
1065 /* Handle error accordingly */
1066 if ( msg->reason.header_value ) {
1067 session->last_error_id = atoi ( ( const char* ) msg->reason.header_value );
1068 session->last_error_str = stringify_error ( session->last_error_id );
1069 }
1070
1071 terminate_call ( session );
1072
1073 event.rise ( callbacks[cb_ending], session );
1074
1075 return 1;
1076}
1077
1078
1079/********************************************************************************************************************
1080 * *******************************************************************************************************************
1081 ********************************************************************************************************************
1082 ********************************************************************************************************************
1083 ********************************************************************************************************************
1084 *
1085 *
1086 *
1087 * PUBLIC API FUNCTIONS IMPLEMENTATIONS
1088 *
1089 *
1090 *
1091 ********************************************************************************************************************
1092 ********************************************************************************************************************
1093 ********************************************************************************************************************
1094 ********************************************************************************************************************
1095 ********************************************************************************************************************/
1096
1097
1098
1099
1100
1101
1102
1103
1104/**
1105 * @brief Callback setter.
1106 *
1107 * @param callback The callback.
1108 * @param id The id.
1109 * @return void
1110 */
1111void msi_register_callback ( MSICallback callback, MSICallbackID id )
1112{
1113 callbacks[id] = callback;
1114}
1115
1116
1117/**
1118 * @brief Start the control session.
1119 *
1120 * @param messenger Tox* object.
1121 * @param user_agent User agent, i.e. 'Venom'; 'QT-gui'
1122 * @return MSISession* The created session.
1123 * @retval NULL Error occured.
1124 */
1125MSISession* msi_init_session ( Tox* messenger, const uint8_t* user_agent ) {
1126 assert ( messenger );
1127 assert ( user_agent );
1128
1129 MSISession* _retu = calloc ( sizeof ( MSISession ), 1 );
1130 assert ( _retu );
1131
1132 _retu->user_agent = user_agent;
1133 _retu->messenger_handle = messenger;
1134 _retu->agent_handler = NULL;
1135
1136 _retu->call = NULL;
1137
1138 _retu->frequ = 10000; /* default value? */
1139 _retu->call_timeout = 30000; /* default value? */
1140
1141
1142 m_callback_msi_packet((struct Messenger*) messenger, msi_handle_packet, _retu );
1143
1144
1145 return _retu;
1146}
1147
1148
1149/**
1150 * @brief Terminate control session.
1151 *
1152 * @param session The session
1153 * @return int
1154 */
1155int msi_terminate_session ( MSISession* session ) {
1156 assert ( session );
1157
1158 int _status = 0;
1159
1160 terminate_call ( session );
1161
1162 /* TODO: Clean it up more? */
1163
1164 free ( session );
1165 return _status;
1166}
1167
1168
1169/**
1170 * @brief Send invite request to friend_id.
1171 *
1172 * @param session Control session.
1173 * @param call_type Type of the call. Audio or Video(both audio and video)
1174 * @param rngsec Ringing timeout.
1175 * @param friend_id The friend.
1176 * @return int
1177 */
1178int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ) {
1179 assert ( session );
1180
1181 MSIMessage* _msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) );
1182
1183 session->call = init_call ( session, 1, rngsec ); /* Just one for now */
1184 t_randomstr ( session->call->id, CALL_ID_LEN );
1185
1186 add_peer(session->call, friend_id );
1187
1188 session->call->type_local = call_type;
1189 /* Do whatever with message */
1190
1191 if ( call_type == type_audio ) {
1192 msi_msg_set_calltype
1193 ( _msg_invite, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
1194 } else {
1195 msi_msg_set_calltype
1196 ( _msg_invite, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
1197 }
1198
1199 send_message ( session, _msg_invite, friend_id );
1200 free_message ( _msg_invite );
1201
1202 session->call->state = call_inviting;
1203
1204 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
1205
1206 return 0;
1207}
1208
1209
1210/**
1211 * @brief Hangup active call.
1212 *
1213 * @param session Control session.
1214 * @return int
1215 * @retval -1 Error occured.
1216 * @retval 0 Success.
1217 */
1218int msi_hangup ( MSISession* session ) {
1219 assert ( session );
1220
1221 if ( !session->call && session->call->state != call_active )
1222 return -1;
1223
1224 MSIMessage* _msg_ending = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
1225
1226 /* hangup for each peer */
1227 int _it = 0;
1228 for ( ; _it < session->call->peer_count; _it ++ )
1229 send_message ( session, _msg_ending, session->call->peers[_it] );
1230
1231
1232 free_message ( _msg_ending );
1233
1234 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
1235
1236 return 0;
1237}
1238
1239
1240/**
1241 * @brief Answer active call request.
1242 *
1243 * @param session Control session.
1244 * @param call_type Answer with Audio or Video(both).
1245 * @return int
1246 */
1247int msi_answer ( MSISession* session, MSICallType call_type ) {
1248 assert ( session );
1249
1250 MSIMessage* _msg_starting = msi_new_message ( TYPE_RESPONSE, stringify_response ( starting ) );
1251 session->call->type_local = call_type;
1252
1253 if ( call_type == type_audio ) {
1254 msi_msg_set_calltype
1255 ( _msg_starting, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
1256 } else {
1257 msi_msg_set_calltype
1258 ( _msg_starting, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
1259 }
1260
1261 /* Now set the local encryption key and pass it with STARTING message */
1262
1263 session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES );
1264 new_symmetric_key ( session->call->key_local );
1265
1266 session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
1267 new_nonce ( session->call->nonce_local );
1268
1269 msi_msg_set_cryptokey ( _msg_starting, session->call->key_local, crypto_secretbox_KEYBYTES );
1270 msi_msg_set_nonce ( _msg_starting, session->call->nonce_local, crypto_box_NONCEBYTES );
1271
1272 send_message ( session, _msg_starting, session->call->peers[session->call->peer_count - 1] );
1273 free_message ( _msg_starting );
1274
1275 session->call->state = call_active;
1276
1277 return 0;
1278}
1279
1280
1281/**
1282 * @brief Cancel request.
1283 *
1284 * @param session Control session.
1285 * @param friend_id The friend.
1286 * @return int
1287 */
1288int msi_cancel ( MSISession* session, int friend_id ) {
1289 assert ( session );
1290
1291 MSIMessage* _msg_cancel = msi_new_message ( TYPE_REQUEST, stringify_request ( cancel ) );
1292 send_message ( session, _msg_cancel, friend_id );
1293 free_message ( _msg_cancel );
1294
1295 terminate_call ( session );
1296
1297 return 0;
1298}
1299
1300
1301/**
1302 * @brief Reject request.
1303 *
1304 * @param session Control session.
1305 * @return int
1306 */
1307int msi_reject ( MSISession* session ) {
1308 assert ( session );
1309
1310 MSIMessage* _msg_reject = msi_new_message ( TYPE_REQUEST, stringify_request ( reject ) );
1311 send_message ( session, _msg_reject, session->call->peers[session->call->peer_count - 1] );
1312 free_message ( _msg_reject );
1313
1314 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
1315
1316 return 0;
1317}
1318
1319
1320/**
1321 * @brief Terminate the current call.
1322 *
1323 * @param session Control session.
1324 * @return int
1325 */
1326int msi_stopcall ( MSISession* session ) {
1327 assert ( session );
1328
1329 if ( !session->call )
1330 return -1;
1331
1332 /* just terminate it */
1333
1334 terminate_call ( session );
1335
1336 return 0;
1337} \ No newline at end of file
diff --git a/toxav/toxmsi.h b/toxav/toxmsi.h
new file mode 100755
index 00000000..c45662a6
--- /dev/null
+++ b/toxav/toxmsi.h
@@ -0,0 +1,231 @@
1/** toxmsi.h
2 *
3 * Copyright (C) 2013 Tox project All Rights Reserved.
4 *
5 * This file is part of Tox.
6 *
7 * Tox is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Tox is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 * Report bugs/suggestions to me ( mannol ) at either #tox-dev @ freenode.net:6667 or
22 * my email: eniz_vukovic@hotmail.com
23 */
24
25#ifndef __TOXMSI
26#define __TOXMSI
27
28#include <inttypes.h>
29#include "../toxcore/tox.h"
30#include <pthread.h>
31
32/* define size for call_id */
33#define CALL_ID_LEN 12
34
35
36typedef void* ( *MSICallback ) ( void* arg );
37
38
39/**
40 * @brief Call type identifier. Also used as rtp callback prefix.
41 */
42typedef enum {
43 type_audio = 70,
44 type_video,
45} MSICallType;
46
47
48/**
49 * @brief Call state identifiers.
50 */
51typedef enum {
52 call_inviting, /* when sending call invite */
53 call_starting, /* when getting call invite */
54 call_active,
55 call_hold
56
57} MSICallState;
58
59
60
61/**
62 * @brief The call struct.
63 *
64 */
65typedef struct _MSICall { /* Call info structure */
66 MSICallState state;
67
68 MSICallType type_local; /* Type of payload user is ending */
69 MSICallType* type_peer; /* Type of payload others are sending */
70
71 uint8_t id[CALL_ID_LEN]; /* Random value identifying the call */
72
73 uint8_t* key_local; /* The key for encryption */
74 uint8_t* key_peer; /* The key for decryption */
75
76 uint8_t* nonce_local; /* Local nonce */
77 uint8_t* nonce_peer; /* Peer nonce */
78
79 int ringing_tout_ms; /* Ringing timeout in ms */
80
81 int request_timer_id; /* Timer id for outgoing request/action */
82 int ringing_timer_id; /* Timer id for ringing timeout */
83
84 pthread_mutex_t mutex; /* It's to be assumed that call will have
85 * seperate thread so add mutex
86 */
87 uint32_t* peers;
88 uint16_t peer_count;
89
90
91} MSICall;
92
93
94/**
95 * @brief Control session struct
96 *
97 */
98typedef struct _MSISession {
99
100 /* Call handler */
101 struct _MSICall* call;
102
103 int last_error_id; /* Determine the last error */
104 const uint8_t* last_error_str;
105
106 const uint8_t* user_agent;
107
108 void* agent_handler; /* Pointer to an object that is handling msi */
109 Tox* messenger_handle;
110
111 uint32_t frequ;
112 uint32_t call_timeout; /* Time of the timeout for some action to end; 0 if infinite */
113
114
115} MSISession;
116
117
118/**
119 * @brief Callbacks ids that handle the states
120 */
121typedef enum {
122 /* Requests */
123 cb_oninvite,
124 cb_onstart,
125 cb_oncancel,
126 cb_onreject,
127 cb_onend,
128
129 /* Responses */
130 cb_ringing,
131 cb_starting,
132 cb_ending,
133
134 /* Protocol */
135 cb_error,
136 cb_timeout,
137
138} MSICallbackID;
139
140
141/**
142 * @brief Callback setter.
143 *
144 * @param callback The callback.
145 * @param id The id.
146 * @return void
147 */
148void msi_register_callback(MSICallback callback, MSICallbackID id);
149
150
151/**
152 * @brief Start the control session.
153 *
154 * @param messenger Tox* object.
155 * @param user_agent User agent, i.e. 'Venom'; 'QT-gui'
156 * @return MSISession* The created session.
157 * @retval NULL Error occured.
158 */
159MSISession* msi_init_session ( Tox* messenger, const uint8_t* user_agent );
160
161
162/**
163 * @brief Terminate control session.
164 *
165 * @param session The session
166 * @return int
167 */
168int msi_terminate_session ( MSISession* session );
169
170
171/**
172 * @brief Send invite request to friend_id.
173 *
174 * @param session Control session.
175 * @param call_type Type of the call. Audio or Video(both audio and video)
176 * @param rngsec Ringing timeout.
177 * @param friend_id The friend.
178 * @return int
179 */
180int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id );
181
182
183/**
184 * @brief Hangup active call.
185 *
186 * @param session Control session.
187 * @return int
188 * @retval -1 Error occured.
189 * @retval 0 Success.
190 */
191int msi_hangup ( MSISession* session );
192
193
194/**
195 * @brief Answer active call request.
196 *
197 * @param session Control session.
198 * @param call_type Answer with Audio or Video(both).
199 * @return int
200 */
201int msi_answer ( MSISession* session, MSICallType call_type );
202
203
204/**
205 * @brief Cancel request.
206 *
207 * @param session Control session.
208 * @param friend_id The friend.
209 * @return int
210 */
211int msi_cancel ( MSISession* session, int friend_id );
212
213
214/**
215 * @brief Reject request.
216 *
217 * @param session Control session.
218 * @return int
219 */
220int msi_reject ( MSISession* session );
221
222
223/**
224 * @brief Terminate the current call.
225 *
226 * @param session Control session.
227 * @return int
228 */
229int msi_stopcall ( MSISession* session );
230
231#endif /* __TOXMSI */
diff --git a/toxav/toxrtp.c b/toxav/toxrtp.c
new file mode 100755
index 00000000..2363deea
--- /dev/null
+++ b/toxav/toxrtp.c
@@ -0,0 +1,878 @@
1/** toxrtp.c
2 *
3 * Copyright (C) 2013 Tox project All Rights Reserved.
4 *
5 * This file is part of Tox.
6 *
7 * Tox is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Tox is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 * Report bugs/suggestions to me ( mannol ) at either #tox-dev @ freenode.net:6667 or
22 * my email: eniz_vukovic@hotmail.com
23 */
24
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif /* HAVE_CONFIG_H */
28
29#include "toxrtp.h"
30#include <assert.h>
31#include <limits.h>
32#include <pthread.h>
33
34#include "../toxcore/util.h"
35#include "../toxcore/network.h"
36#include "../toxcore/net_crypto.h"
37#include "../toxcore/Messenger.h"
38
39#define PAYLOAD_ID_VALUE_OPUS 1
40#define PAYLOAD_ID_VALUE_VP8 2
41
42#define MAX_SEQU_NUM 65535
43
44#define size_32 4
45
46#define inline__ inline __attribute__((always_inline))
47
48
49#define ADD_FLAG_VERSION(_h, _v) do { ( _h->flags ) &= 0x3F; ( _h->flags ) |= ( ( ( _v ) << 6 ) & 0xC0 ); } while(0)
50#define ADD_FLAG_PADDING(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xDF; ( _h->flags ) |= ( ( ( _v ) << 5 ) & 0x20 ); } while(0)
51#define ADD_FLAG_EXTENSION(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xEF;( _h->flags ) |= ( ( ( _v ) << 4 ) & 0x10 ); } while(0)
52#define ADD_FLAG_CSRCC(_h, _v) do { ( _h->flags ) &= 0xF0; ( _h->flags ) |= ( ( _v ) & 0x0F ); } while(0)
53#define ADD_SETTING_MARKER(_h, _v) do { if ( _v > 1 ) _v = 1; ( _h->marker_payloadt ) &= 0x7F; ( _h->marker_payloadt ) |= ( ( ( _v ) << 7 ) /*& 0x80 */ ); } while(0)
54#define ADD_SETTING_PAYLOAD(_h, _v) do { if ( _v > 127 ) _v = 127; ( _h->marker_payloadt ) &= 0x80; ( _h->marker_payloadt ) |= ( ( _v ) /* & 0x7F */ ); } while(0)
55
56#define GET_FLAG_VERSION(_h) (( _h->flags & 0xd0 ) >> 6)
57#define GET_FLAG_PADDING(_h) (( _h->flags & 0x20 ) >> 5)
58#define GET_FLAG_EXTENSION(_h) (( _h->flags & 0x10 ) >> 4)
59#define GET_FLAG_CSRCC(_h) ( _h->flags & 0x0f )
60#define GET_SETTING_MARKER(_h) (( _h->marker_payloadt ) >> 7)
61#define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f)
62
63
64
65/**
66 * @brief Checks if message came in late.
67 *
68 * @param session Control session.
69 * @param msg The message.
70 * @return int
71 * @retval -1 The message came in order.
72 * @retval 0 The message came late.
73 */
74inline__ int check_late_message (RTPSession* session, RTPMessage* msg)
75{
76 /*
77 * Check Sequence number. If this new msg has lesser number then the session->rsequnum
78 * it shows that the message came in late. Also check timestamp to be 100% certain.
79 *
80 */
81 return ( msg->header->sequnum < session->rsequnum && msg->header->timestamp < session->timestamp ) ? 0 : -1;
82}
83
84
85/**
86 * @brief Increases nonce value by 'target'
87 *
88 * @param nonce The nonce
89 * @param target The target
90 * @return void
91 */
92inline__ void increase_nonce(uint8_t* nonce, uint16_t target)
93{
94 uint16_t _nonce_counter = ((uint16_t)(
95 (((uint16_t) nonce [crypto_box_NONCEBYTES - 1]) << 8 ) |
96 (((uint16_t) nonce [crypto_box_NONCEBYTES - 2]) )));
97
98 /* Check overflow */
99 if (_nonce_counter > USHRT_MAX - target ) { /* 2 bytes are not long enough */
100 int _it = 3;
101 while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ? crypto_box_NONCEBYTES : 1;
102
103 _nonce_counter = _nonce_counter - (USHRT_MAX - target ); /* Assign the rest of it */
104 } else { /* Increase nonce */
105
106 _nonce_counter+= target;
107 }
108
109 /* Assign the 8 last bytes */
110
111 nonce [crypto_box_NONCEBYTES - 1] = (uint8_t) (_nonce_counter >> 8);
112 nonce [crypto_box_NONCEBYTES - 2] = (uint8_t) (_nonce_counter);
113}
114
115
116/**
117 * @brief Speaks for it self.
118 *
119 */
120static const uint32_t payload_table[] =
121{
122 8000, 8000, 8000, 8000, 8000, 8000, 16000, 8000, 8000, 8000, /* 0-9 */
123 44100, 44100, 0, 0, 90000, 8000, 11025, 22050, 0, 0, /* 10-19 */
124 0, 0, 0, 0, 0, 90000, 90000, 0, 90000, 0, /* 20-29 */
125 0, 90000, 90000, 90000, 90000, 0, 0, 0, 0, 0, /* 30-39 */
126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40-49 */
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50-59 */
128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60-69 */
129 PAYLOAD_ID_VALUE_OPUS, PAYLOAD_ID_VALUE_VP8, 0, 0, 0, 0, 0, 0, 0, 0,/* 70-79 */
130 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-89 */
131 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90-99 */
132 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100-109 */
133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110-119 */
134 0, 0, 0, 0, 0, 0, 0, 0 /* 120-127 */
135};
136
137
138/**
139 * @brief Extracts header from payload.
140 *
141 * @param payload The payload.
142 * @param length The size of payload.
143 * @return RTPHeader* Extracted header.
144 * @retval NULL Error occurred while extracting header.
145 */
146RTPHeader* extract_header ( const uint8_t* payload, size_t length )
147{
148 if ( !payload ) {
149 return NULL;
150 }
151
152 const uint8_t* _it = payload;
153
154 RTPHeader* _retu = calloc(sizeof(RTPHeader), 1);
155 assert(_retu);
156
157 _retu->flags = *_it; ++_it;
158
159 /* This indicates if the first 2 bytes are valid.
160 * Now it my happen that this is out of order but
161 * it cuts down chances of parsing some invalid value
162 */
163 if ( GET_FLAG_VERSION(_retu) != RTP_VERSION ){
164 /* Deallocate */
165 free(_retu);
166 return NULL;
167 }
168
169 /*
170 * Added a check for the size of the header little sooner so
171 * I don't need to parse the other stuff if it's bad
172 */
173 uint8_t _cc = GET_FLAG_CSRCC ( _retu );
174 uint32_t _length = 12 /* Minimum header len */ + ( _cc * 4 );
175
176 if ( length < _length ) {
177 /* Deallocate */
178 free(_retu);
179 return NULL;
180 }
181
182 if ( _cc > 0 ) {
183 _retu->csrc = calloc ( sizeof ( uint32_t ), _cc );
184 assert(_retu->csrc);
185
186 } else { /* But this should not happen ever */
187 /* Deallocate */
188 free(_retu);
189 return NULL;
190 }
191
192
193 _retu->marker_payloadt = *_it; ++_it;
194 _retu->length = _length;
195
196 _retu->timestamp = ( ( uint32_t ) * _it << 24 ) |
197 ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
198 ( ( uint32_t ) * ( _it + 2 ) << 8 ) |
199 ( * ( _it + 3 ) ) ;
200
201 _it += 4;
202
203 _retu->ssrc = ( ( uint32_t ) * _it << 24 ) |
204 ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
205 ( ( uint32_t ) * ( _it + 2 ) << 8 ) |
206 ( ( uint32_t ) * ( _it + 3 ) ) ;
207
208
209 size_t _x;
210 for ( _x = 0; _x < _cc; _x++ ) {
211 _it += 4;
212 _retu->csrc[_x] = ( ( uint32_t ) * _it << 24 ) |
213 ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
214 ( ( uint32_t ) * ( _it + 2 ) << 8 ) |
215 ( ( uint32_t ) * ( _it + 3 ) ) ;
216 }
217
218 return _retu;
219}
220
221/**
222 * @brief Extracts external header from payload. Must be called AFTER extract_header()!
223 *
224 * @param payload The ITERATED payload.
225 * @param length The size of payload.
226 * @return RTPExtHeader* Extracted extension header.
227 * @retval NULL Error occurred while extracting extension header.
228 */
229RTPExtHeader* extract_ext_header ( const uint8_t* payload, size_t length )
230{
231 const uint8_t* _it = payload;
232
233 RTPExtHeader* _retu = calloc(sizeof(RTPExtHeader), 1);
234 assert(_retu);
235
236 uint16_t _ext_length = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 ); _it += 2;
237
238 if ( length < ( _ext_length * sizeof(uint32_t) ) ) {
239 return NULL;
240 }
241
242 _retu->length = _ext_length;
243 _retu->type = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 ); _it -= 2;
244
245 _retu->table = calloc(sizeof(uint32_t), _ext_length );
246 assert(_retu->table);
247
248 uint32_t* _table = _retu->table;
249 size_t _i;
250 for ( _i = 0; _i < _ext_length; _i++ ) {
251 _it += 4;
252 _table[_i] = ( ( uint32_t ) * _it << 24 ) |
253 ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
254 ( ( uint32_t ) * ( _it + 2 ) << 8 ) |
255 ( ( uint32_t ) * ( _it + 3 ) ) ;
256 }
257
258 return _retu;
259}
260
261/**
262 * @brief Adds header to payload. Make sure _payload_ has enough space.
263 *
264 * @param header The header.
265 * @param payload The payload.
266 * @return uint8_t* Iterated position.
267 */
268uint8_t* add_header ( RTPHeader* header, uint8_t* payload )
269{
270 uint8_t _cc = GET_FLAG_CSRCC ( header );
271
272 uint8_t* _it = payload;
273
274
275 /* Add sequence number first */
276 *_it = ( header->sequnum >> 8 ); ++_it;
277 *_it = ( header->sequnum ); ++_it;
278
279 *_it = header->flags; ++_it;
280 *_it = header->marker_payloadt; ++_it;
281
282
283 uint32_t _timestamp = header->timestamp;
284 *_it = ( _timestamp >> 24 ); ++_it;
285 *_it = ( _timestamp >> 16 ); ++_it;
286 *_it = ( _timestamp >> 8 ); ++_it;
287 *_it = ( _timestamp ); ++_it;
288
289 uint32_t _ssrc = header->ssrc;
290 *_it = ( _ssrc >> 24 ); ++_it;
291 *_it = ( _ssrc >> 16 ); ++_it;
292 *_it = ( _ssrc >> 8 ); ++_it;
293 *_it = ( _ssrc );
294
295 uint32_t *_csrc = header->csrc;
296 size_t _x;
297 for ( _x = 0; _x < _cc; _x++ ) {
298 ++_it;
299 *_it = ( _csrc[_x] >> 24 ); ++_it;
300 *_it = ( _csrc[_x] >> 16 ); ++_it;
301 *_it = ( _csrc[_x] >> 8 ); ++_it;
302 *_it = ( _csrc[_x] );
303 }
304
305 return _it;
306}
307
308/**
309 * @brief Adds extension header to payload. Make sure _payload_ has enough space.
310 *
311 * @param header The header.
312 * @param payload The payload.
313 * @return uint8_t* Iterated position.
314 */
315uint8_t* add_ext_header ( RTPExtHeader* header, uint8_t* payload )
316{
317 uint8_t* _it = payload;
318
319 *_it = ( header->length >> 8 ); _it++;
320 *_it = ( header->length ); _it++;
321
322 *_it = ( header->type >> 8 ); ++_it;
323 *_it = ( header->type );
324
325 size_t x;
326
327 uint32_t* _hd_ext = header->table;
328 for ( x = 0; x < header->length; x++ ) {
329 ++_it;
330 *_it = ( _hd_ext[x] >> 24 ); ++_it;
331 *_it = ( _hd_ext[x] >> 16 ); ++_it;
332 *_it = ( _hd_ext[x] >> 8 ); ++_it;
333 *_it = ( _hd_ext[x] );
334 }
335
336 return _it;
337}
338
339/**
340 * @brief Builds header from control session values.
341 *
342 * @param session Control session.
343 * @return RTPHeader* Created header.
344 */
345RTPHeader* build_header ( RTPSession* session )
346{
347 RTPHeader* _retu;
348 _retu = calloc ( sizeof * _retu, 1 );
349 assert(_retu);
350
351 ADD_FLAG_VERSION ( _retu, session->version );
352 ADD_FLAG_PADDING ( _retu, session->padding );
353 ADD_FLAG_EXTENSION ( _retu, session->extension );
354 ADD_FLAG_CSRCC ( _retu, session->cc );
355 ADD_SETTING_MARKER ( _retu, session->marker );
356 ADD_SETTING_PAYLOAD ( _retu, session->payload_type );
357
358 _retu->sequnum = session->sequnum;
359 _retu->timestamp = ((uint32_t)(current_time() / 1000)); /* micro to milli */
360 _retu->ssrc = session->ssrc;
361
362 if ( session->cc > 0 ) {
363 _retu->csrc = calloc(sizeof(uint32_t), session->cc);
364 assert(_retu->csrc);
365
366 int i;
367
368 for ( i = 0; i < session->cc; i++ ) {
369 _retu->csrc[i] = session->csrc[i];
370 }
371 } else {
372 _retu->csrc = NULL;
373 }
374
375 _retu->length = 12 /* Minimum header len */ + ( session->cc * size_32 );
376
377 return _retu;
378}
379
380
381/**
382 * @brief Parses data into RTPMessage struct. Stores headers separately from the payload data
383 * and so the length variable is set accordingly. _sequnum_ argument is
384 * passed by the handle_packet() since it's parsed already.
385 *
386 * @param session Control session.
387 * @param sequnum Sequence number that's parsed from payload in handle_packet()
388 * @param data Payload data.
389 * @param length Payload size.
390 * @return RTPMessage*
391 * @retval NULL Error occurred.
392 */
393RTPMessage* msg_parse ( RTPSession* session, uint16_t sequnum, const uint8_t* data, uint32_t length )
394{
395 assert( length != -1);
396
397 RTPMessage* _retu = calloc(sizeof(RTPMessage), 1);
398 assert(_retu);
399
400 _retu->header = extract_header ( data, length ); /* It allocates memory and all */
401
402 if ( !_retu->header ){
403 free(_retu);
404 return NULL;
405 }
406 _retu->header->sequnum = sequnum;
407
408 _retu->length = length - _retu->header->length;
409
410 uint16_t _from_pos = _retu->header->length - 2 /* Since sequ num is excluded */ ;
411
412
413 if ( GET_FLAG_EXTENSION ( _retu->header ) ) {
414 _retu->ext_header = extract_ext_header ( data + _from_pos, length );
415 if ( _retu->ext_header ){
416 _retu->length -= ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
417 _from_pos += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
418 } else {
419 free (_retu->ext_header);
420 free (_retu->header);
421 free (_retu);
422 return NULL;
423 }
424 } else {
425 _retu->ext_header = NULL;
426 }
427
428 /* Get the payload */
429 _retu->data = calloc ( sizeof ( uint8_t ), _retu->length );
430 assert(_retu->data);
431
432 memcpy ( _retu->data, data + _from_pos, length - _from_pos );
433
434 _retu->next = NULL;
435
436
437 if ( session && check_late_message ( session, _retu) < 0 ){
438 session->rsequnum = _retu->header->sequnum;
439 session->timestamp = _retu->header->timestamp;
440 }
441
442 return _retu;
443}
444
445/**
446 * @brief Callback for networking core.
447 *
448 * @param object RTPSession object.
449 * @param ip_port Where the message comes from.
450 * @param data Message data.
451 * @param length Message length.
452 * @return int
453 * @retval -1 Error occurred.
454 * @retval 0 Success.
455 */
456int rtp_handle_packet ( void* object, IP_Port ip_port, uint8_t* data, uint32_t length )
457{
458 RTPSession* _session = object;
459 RTPMessage* _msg;
460
461 if ( !_session )
462 return -1;
463
464 uint8_t _plain[MAX_UDP_PACKET_SIZE];
465
466 uint16_t _sequnum = ( ( uint16_t ) data[1] << 8 ) | data[2];
467
468 /* Clculate the right nonce */
469 uint8_t _calculated[crypto_box_NONCEBYTES];
470 memcpy(_calculated, _session->decrypt_nonce, crypto_box_NONCEBYTES);
471 increase_nonce ( _calculated, _sequnum );
472
473 /* Decrypt message */
474 int _decrypted_length = decrypt_data_symmetric(
475 (uint8_t*)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
476
477 /* This packet is either not encrypted properly or late
478 */
479 if ( -1 == _decrypted_length ){
480
481 /* If this is the case, then the packet is most likely late.
482 * Try with old nonce cycle.
483 */
484 if ( _session->rsequnum < _sequnum ) {
485 _decrypted_length = decrypt_data_symmetric(
486 (uint8_t*)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain );
487
488 if ( !_decrypted_length ) return -1; /* This packet is not encrypted properly */
489
490 /* Otherwise, if decryption is ok with new cycle, set new cycle
491 */
492 } else {
493 increase_nonce ( _calculated, MAX_SEQU_NUM );
494 _decrypted_length = decrypt_data_symmetric(
495 (uint8_t*)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
496
497 if ( !_decrypted_length ) return -1; /* This is just an error */
498
499 /* A new cycle setting. */
500 memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES);
501 memcpy(_session->decrypt_nonce, _calculated, crypto_box_NONCEBYTES);
502 }
503 }
504
505 _msg = msg_parse ( NULL, _sequnum, _plain, _decrypted_length );
506
507 if ( !_msg )
508 return -1;
509
510 /* Hopefully this goes well
511 * NOTE: Is this even used?
512 */
513 memcpy(&_msg->from, &ip_port, sizeof(tox_IP_Port));
514
515 /* Check if message came in late */
516 if ( check_late_message(_session, _msg) < 0 ) { /* Not late */
517 _session->rsequnum = _msg->header->sequnum;
518 _session->timestamp = _msg->header->timestamp;
519 }
520
521 pthread_mutex_lock(&_session->mutex);
522
523 if ( _session->last_msg ) {
524 _session->last_msg->next = _msg;
525 _session->last_msg = _msg;
526 } else {
527 _session->last_msg = _session->oldest_msg = _msg;
528 }
529
530 pthread_mutex_unlock(&_session->mutex);
531
532 return 0;
533}
534
535
536
537/**
538 * @brief Stores headers and payload data in one container ( data )
539 * and the length is set accordingly. Returned message is used for sending _only_.
540 *
541 * @param session The control session.
542 * @param data Payload data to send ( This is what you pass ).
543 * @param length Size of the payload data.
544 * @return RTPMessage* Created message.
545 * @retval NULL Error occurred.
546 */
547RTPMessage* rtp_new_message ( RTPSession* session, const uint8_t* data, uint32_t length )
548{
549 if ( !session )
550 return NULL;
551
552 uint8_t* _from_pos;
553 RTPMessage* _retu = calloc(sizeof(RTPMessage), 1);
554 assert(_retu);
555
556 /* Sets header values and copies the extension header in _retu */
557 _retu->header = build_header ( session ); /* It allocates memory and all */
558 _retu->ext_header = session->ext_header;
559
560
561 uint32_t _total_length = length + _retu->header->length;
562
563 if ( _retu->ext_header ) {
564 _total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
565 /* Allocate Memory for _retu->_data */
566 _retu->data = calloc ( sizeof _retu->data, _total_length );
567 assert(_retu->data);
568
569 _from_pos = add_header ( _retu->header, _retu->data );
570 _from_pos = add_ext_header ( _retu->ext_header, _from_pos + 1 );
571 } else {
572 /* Allocate Memory for _retu->_data */
573 _retu->data = calloc ( sizeof _retu->data, _total_length );
574 assert(_retu->data);
575
576 _from_pos = add_header ( _retu->header, _retu->data );
577 }
578
579 /*
580 * Parses the extension header into the message
581 * Of course if any
582 */
583
584 /* Appends _data on to _retu->_data */
585 memcpy ( _from_pos + 1, data, length );
586
587 _retu->length = _total_length;
588
589 _retu->next = NULL;
590
591 return _retu;
592}
593
594
595/********************************************************************************************************************
596 ********************************************************************************************************************
597 ********************************************************************************************************************
598 ********************************************************************************************************************
599 ********************************************************************************************************************
600 *
601 *
602 *
603 * PUBLIC API FUNCTIONS IMPLEMENTATIONS
604 *
605 *
606 *
607 ********************************************************************************************************************
608 ********************************************************************************************************************
609 ********************************************************************************************************************
610 ********************************************************************************************************************
611 ********************************************************************************************************************/
612
613
614
615
616
617
618
619
620
621/**
622 * @brief Release all messages held by session.
623 *
624 * @param session The session.
625 * @return int
626 * @retval -1 Error occurred.
627 * @retval 0 Success.
628 */
629int rtp_release_session_recv ( RTPSession* session )
630{
631 if ( !session ){
632 return -1;
633 }
634
635 RTPMessage* _tmp,* _it;
636
637 pthread_mutex_lock(&session->mutex);
638
639 for ( _it = session->oldest_msg; _it; _it = _tmp ){
640 _tmp = _it->next;
641 rtp_free_msg( session, _it);
642 }
643
644 session->last_msg = session->oldest_msg = NULL;
645
646 pthread_mutex_unlock(&session->mutex);
647
648 return 0;
649}
650
651
652/**
653 * @brief Get's oldes message in the list.
654 *
655 * @param session Where the list is.
656 * @return RTPMessage* The message. You _must_ call rtp_msg_free() to free it.
657 * @retval NULL No messages in the list, or no list.
658 */
659RTPMessage* rtp_recv_msg ( RTPSession* session )
660{
661 if ( !session )
662 return NULL;
663
664 RTPMessage* _retu = session->oldest_msg;
665
666 pthread_mutex_lock(&session->mutex);
667
668 if ( _retu )
669 session->oldest_msg = _retu->next;
670
671 if ( !session->oldest_msg )
672 session->last_msg = NULL;
673
674 pthread_mutex_unlock(&session->mutex);
675
676 return _retu;
677}
678
679
680/**
681 * @brief Sends data to _RTPSession::dest
682 *
683 * @param session The session.
684 * @param messenger Tox* object.
685 * @param data The payload.
686 * @param length Size of the payload.
687 * @return int
688 * @retval -1 On error.
689 * @retval 0 On success.
690 */
691int rtp_send_msg ( RTPSession* session, Tox* messenger, const uint8_t* data, uint16_t length )
692{
693 RTPMessage* msg = rtp_new_message (session, data, length);
694
695 if ( !msg ) return -1;
696
697 uint8_t _send_data [ MAX_UDP_PACKET_SIZE ];
698
699 _send_data[0] = session->prefix;
700
701 /* Generate the right nonce */
702 uint8_t _calculated[crypto_box_NONCEBYTES];
703 memcpy(_calculated, session->encrypt_nonce, crypto_box_NONCEBYTES);
704 increase_nonce ( _calculated, msg->header->sequnum );
705
706 /* Need to skip 2 bytes that are for sequnum */
707 int encrypted_length = encrypt_data_symmetric(
708 (uint8_t*) session->encrypt_key, _calculated, msg->data + 2, msg->length - 2, _send_data + 3 );
709
710 int full_length = encrypted_length + 3;
711
712 _send_data[1] = msg->data[0];
713 _send_data[2] = msg->data[1];
714
715
716 if ( full_length != sendpacket ( ((Messenger*)messenger)->net, *((IP_Port*) &session->dest), _send_data, full_length) ) {
717 printf("Rtp error: %s\n", strerror(errno));
718 return -1;
719 }
720
721
722 /* Set sequ number */
723 if ( session->sequnum >= MAX_SEQU_NUM ) {
724 session->sequnum = 0;
725 memcpy(session->encrypt_nonce, _calculated, crypto_box_NONCEBYTES);
726 } else {
727 session->sequnum++;
728 }
729
730 rtp_free_msg ( session, msg );
731 return 0;
732}
733
734
735/**
736 * @brief Speaks for it self.
737 *
738 * @param session The control session msg belongs to. It can be NULL.
739 * @param msg The message.
740 * @return void
741 */
742void rtp_free_msg ( RTPSession* session, RTPMessage* msg )
743{
744 free ( msg->data );
745
746 if ( !session ){
747 free ( msg->header->csrc );
748 if ( msg->ext_header ){
749 free ( msg->ext_header->table );
750 free ( msg->ext_header );
751 }
752 } else {
753 if ( session->csrc != msg->header->csrc )
754 free ( msg->header->csrc );
755 if ( msg->ext_header && session->ext_header != msg->ext_header ) {
756 free ( msg->ext_header->table );
757 free ( msg->ext_header );
758 }
759 }
760
761 free ( msg->header );
762 free ( msg );
763}
764
765
766/**
767 * @brief Must be called before calling any other rtp function. It's used
768 * to initialize RTP control session.
769 *
770 * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType
771 * @param messenger Tox* object.
772 * @param friend_num Friend id.
773 * @param encrypt_key Speaks for it self.
774 * @param decrypt_key Speaks for it self.
775 * @param encrypt_nonce Speaks for it self.
776 * @param decrypt_nonce Speaks for it self.
777 * @return RTPSession* Created control session.
778 * @retval NULL Error occurred.
779 */
780RTPSession* rtp_init_session ( int payload_type,
781 Tox* messenger,
782 int friend_num,
783 const uint8_t* encrypt_key,
784 const uint8_t* decrypt_key,
785 const uint8_t* encrypt_nonce,
786 const uint8_t* decrypt_nonce
787)
788{
789 Messenger* _messenger_casted = (Messenger*) messenger;
790
791 IP_Port _dest = get_friend_ipport(_messenger_casted, friend_num );
792
793 /* This should be enough eh? */
794 if ( _dest.port == 0) {
795 return NULL;
796 }
797
798 RTPSession* _retu = calloc(sizeof(RTPSession), 1);
799 assert(_retu);
800
801 networking_registerhandler(_messenger_casted->net, payload_type, rtp_handle_packet, _retu);
802
803 _retu->version = RTP_VERSION; /* It's always 2 */
804 _retu->padding = 0; /* If some additional data is needed about the packet */
805 _retu->extension = 0; /* If extension to header is needed */
806 _retu->cc = 1; /* Amount of contributors */
807 _retu->csrc = NULL; /* Container */
808 _retu->ssrc = randombytes_random();
809 _retu->marker = 0;
810 _retu->payload_type = payload_table[payload_type];
811
812 _retu->dest = *((tox_IP_Port*)&_dest);
813
814 _retu->rsequnum = _retu->sequnum = 1;
815
816 _retu->ext_header = NULL; /* When needed allocate */
817 _retu->framerate = -1;
818 _retu->resolution = -1;
819
820 _retu->encrypt_key = encrypt_key;
821 _retu->decrypt_key = decrypt_key;
822
823 /* Need to allocate new memory */
824 _retu->encrypt_nonce = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); assert(_retu->encrypt_nonce);
825 _retu->decrypt_nonce = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); assert(_retu->decrypt_nonce);
826 _retu->nonce_cycle = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); assert(_retu->nonce_cycle);
827
828 memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES);
829 memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES);
830 memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES);
831
832 _retu->csrc = calloc(sizeof(uint32_t), 1);
833 assert(_retu->csrc);
834
835 _retu->csrc[0] = _retu->ssrc; /* Set my ssrc to the list receive */
836
837 /* Also set payload type as prefix */
838 _retu->prefix = payload_type;
839
840 _retu->oldest_msg = _retu->last_msg = NULL;
841
842 pthread_mutex_init(&_retu->mutex, NULL);
843 /*
844 *
845 */
846 return _retu;
847}
848
849
850/**
851 * @brief Terminate the session.
852 *
853 * @param session The session.
854 * @param messenger The messenger who owns the session
855 * @return int
856 * @retval -1 Error occurred.
857 * @retval 0 Success.
858 */
859int rtp_terminate_session ( RTPSession* session, Tox* messenger )
860{
861 if ( !session )
862 return -1;
863
864 networking_registerhandler(((Messenger*)messenger)->net, session->prefix, NULL, NULL);
865
866 free ( session->ext_header );
867 free ( session->csrc );
868 free ( session->decrypt_nonce );
869 free ( session->encrypt_nonce );
870 free ( session->nonce_cycle );
871
872 pthread_mutex_destroy(&session->mutex);
873
874 /* And finally free session */
875 free ( session );
876
877 return 0;
878} \ No newline at end of file
diff --git a/toxav/toxrtp.h b/toxav/toxrtp.h
new file mode 100755
index 00000000..b4275aba
--- /dev/null
+++ b/toxav/toxrtp.h
@@ -0,0 +1,211 @@
1/** toxrtp.h
2 *
3 * Copyright (C) 2013 Tox project All Rights Reserved.
4 *
5 * This file is part of Tox.
6 *
7 * Tox is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Tox is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
19 *
20 *
21 * Report bugs/suggestions to me ( mannol ) at either #tox-dev @ freenode.net:6667 or
22 * my email: eniz_vukovic@hotmail.com
23 */
24
25#ifndef __TOXRTP
26#define __TOXRTP
27
28#define RTP_VERSION 2
29#include <inttypes.h>
30#include "../toxcore/tox.h"
31
32/**
33 * Standard rtp header
34 */
35
36typedef struct _RTPHeader {
37 uint8_t flags; /* Version(2),Padding(1), Ext(1), Cc(4) */
38 uint8_t marker_payloadt; /* Marker(1), PlayLoad Type(7) */
39 uint16_t sequnum; /* Sequence Number */
40 uint32_t timestamp; /* Timestamp */
41 uint32_t ssrc; /* SSRC */
42 uint32_t* csrc; /* CSRC's table */
43 uint32_t length; /* Length of the header in payload string. */
44
45} RTPHeader;
46
47
48/**
49 * @brief Standard rtp extension header.
50 *
51 */
52typedef struct _RTPExtHeader {
53 uint16_t type; /* Extension profile */
54 uint16_t length; /* Number of extensions */
55 uint32_t* table; /* Extension's table */
56
57} RTPExtHeader;
58
59
60/**
61 * @brief Standard rtp message.
62 *
63 */
64typedef struct _RTPMessage {
65 RTPHeader* header;
66 RTPExtHeader* ext_header;
67
68 uint8_t* data;
69 uint32_t length;
70 tox_IP_Port from;
71
72 struct _RTPMessage* next;
73} RTPMessage;
74
75
76/**
77 * @brief Our main session descriptor.
78 * It measures the session variables and controls
79 * the entire session. There are functions for manipulating
80 * the session so tend to use those instead of directly modifying
81 * session parameters.
82 *
83 */
84typedef struct _RTPSession {
85 uint8_t version;
86 uint8_t padding;
87 uint8_t extension;
88 uint8_t cc;
89 uint8_t marker;
90 uint8_t payload_type;
91 uint16_t sequnum; /* Set when sending */
92 uint16_t rsequnum; /* Check when recving msg */
93 uint32_t timestamp;
94 uint32_t ssrc;
95 uint32_t* csrc;
96
97 /* If some additional data must be sent via message
98 * apply it here. Only by allocating this member you will be
99 * automatically placing it within a message.
100 */
101 RTPExtHeader* ext_header;
102
103 /* External header identifiers */
104 int resolution;
105 int framerate;
106
107
108 /* Since these are only references of the
109 * call structure don't allocate or free
110 */
111
112 const uint8_t* encrypt_key;
113 const uint8_t* decrypt_key;
114 uint8_t* encrypt_nonce;
115 uint8_t* decrypt_nonce;
116
117 uint8_t* nonce_cycle;
118
119 RTPMessage* oldest_msg;
120 RTPMessage* last_msg; /* tail */
121
122 /* Msg prefix for core to know when recving */
123 uint8_t prefix;
124
125 pthread_mutex_t mutex;
126 tox_IP_Port dest;
127
128} RTPSession;
129
130
131/**
132 * @brief Release all messages held by session.
133 *
134 * @param session The session.
135 * @return int
136 * @retval -1 Error occurred.
137 * @retval 0 Success.
138 */
139int rtp_release_session_recv ( RTPSession* session );
140
141
142/**
143 * @brief Get's oldest message in the list.
144 *
145 * @param session Where the list is.
146 * @return RTPMessage* The message. You need to call rtp_msg_free() to free it.
147 * @retval NULL No messages in the list, or no list.
148 */
149RTPMessage* rtp_recv_msg ( RTPSession* session );
150
151
152/**
153 * @brief Sends msg to _RTPSession::dest
154 *
155 * @param session The session.
156 * @param msg The message
157 * @param messenger Tox* object.
158 * @return int
159 * @retval -1 On error.
160 * @retval 0 On success.
161 */
162int rtp_send_msg ( RTPSession* session, Tox* messenger, const uint8_t* data, uint16_t length );
163
164
165/**
166 * @brief Speaks for it self.
167 *
168 * @param session The control session msg belongs to. It can be NULL.
169 * @param msg The message.
170 * @return void
171 */
172void rtp_free_msg ( RTPSession* session, RTPMessage* msg );
173
174
175/**
176 * @brief Must be called before calling any other rtp function. It's used
177 * to initialize RTP control session.
178 *
179 * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType
180 * @param messenger Tox* object.
181 * @param friend_num Friend id.
182 * @param encrypt_key Speaks for it self.
183 * @param decrypt_key Speaks for it self.
184 * @param encrypt_nonce Speaks for it self.
185 * @param decrypt_nonce Speaks for it self.
186 * @return RTPSession* Created control session.
187 * @retval NULL Error occurred.
188 */
189RTPSession* rtp_init_session ( int payload_type,
190 Tox* messenger,
191 int friend_num,
192 const uint8_t* encrypt_key,
193 const uint8_t* decrypt_key,
194 const uint8_t* encrypt_nonce,
195 const uint8_t* decrypt_nonce );
196
197
198/**
199 * @brief Terminate the session.
200 *
201 * @param session The session.
202 * @param messenger The messenger who owns the session
203 * @return int
204 * @retval -1 Error occurred.
205 * @retval 0 Success.
206 */
207int rtp_terminate_session ( RTPSession* session, Tox* messenger );
208
209
210
211#endif /* __TOXRTP */