summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-02-16 19:13:34 -0500
committerirungentoo <irungentoo@gmail.com>2014-02-16 19:13:34 -0500
commit9d1eb27717472f5655242fac7b8123a44f1ff33c (patch)
treefd205f1ea53749717d9930d93ddb153fc8a16929
parentfd3dc22ef3bd77beb78faf24cd43e9d2b9706f5d (diff)
parentf79b327fd639b2dcca9d4d2c137f93c1dce622fc (diff)
Merge branch 'av-fix' into av-good
-rw-r--r--.travis.yml7
-rw-r--r--INSTALL.md4
-rw-r--r--build/Makefile.am3
-rw-r--r--configure.ac79
-rw-r--r--toxav/Makefile.inc75
-rwxr-xr-xtoxav/event.c373
-rwxr-xr-xtoxav/event.h50
-rw-r--r--toxav/media.c309
-rw-r--r--toxav/media.h80
-rw-r--r--toxav/msi.c1379
-rw-r--r--toxav/msi.h233
-rwxr-xr-xtoxav/phone.c1308
-rw-r--r--toxav/rtp.c908
-rw-r--r--toxav/rtp.h219
-rw-r--r--toxav/toxav.c539
-rw-r--r--toxav/toxav.h300
-rw-r--r--toxmsi/Makefile.inc69
-rw-r--r--toxmsi/phone.c668
-rw-r--r--toxmsi/phone.h62
-rw-r--r--toxmsi/toxmedia.c825
-rw-r--r--toxmsi/toxmedia.h168
-rw-r--r--toxmsi/toxmsi.c835
-rw-r--r--toxmsi/toxmsi.h145
-rw-r--r--toxmsi/toxmsi_event.c214
-rw-r--r--toxmsi/toxmsi_event.h46
-rw-r--r--toxmsi/toxmsi_header.c181
-rw-r--r--toxmsi/toxmsi_header.h99
-rw-r--r--toxmsi/toxmsi_message.c267
-rw-r--r--toxmsi/toxmsi_message.h120
-rw-r--r--toxrtp/Makefile.inc34
-rw-r--r--toxrtp/tests/Makefile.inc1
-rw-r--r--toxrtp/tests/test_bidirect.c109
-rw-r--r--toxrtp/tests/test_headers.c316
-rw-r--r--toxrtp/tests/test_helper.c83
-rw-r--r--toxrtp/tests/test_helper.h61
-rw-r--r--toxrtp/toxrtp.c693
-rw-r--r--toxrtp/toxrtp.h188
-rw-r--r--toxrtp/toxrtp_error.c68
-rw-r--r--toxrtp/toxrtp_error.h25
-rw-r--r--toxrtp/toxrtp_error_id.h32
-rw-r--r--toxrtp/toxrtp_helper.c208
-rw-r--r--toxrtp/toxrtp_helper.h77
-rw-r--r--toxrtp/toxrtp_message.c351
-rw-r--r--toxrtp/toxrtp_message.h111
44 files changed, 5835 insertions, 6087 deletions
diff --git a/.travis.yml b/.travis.yml
index 879f3a22..9d175564 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -49,6 +49,13 @@ before_script:
49 - make -j3 >/dev/null 49 - make -j3 >/dev/null
50 - sudo make install > /dev/null 50 - sudo make install > /dev/null
51 - cd .. 51 - cd ..
52 #installing vpx
53 - git clone http://git.chromium.org/webm/libvpx.git > /dev/null
54 - cd libvpx
55 - ./configure > /dev/null
56 - make -j3 >/dev/null
57 - sudo make install > /dev/null
58 - cd ..
52 #creating libraries links and updating cache 59 #creating libraries links and updating cache
53 - sudo ldconfig > /dev/null 60 - sudo ldconfig > /dev/null
54 #installing check, needed for unit tests 61 #installing check, needed for unit tests
diff --git a/INSTALL.md b/INSTALL.md
index 76a2623b..e82fe5bf 100644
--- a/INSTALL.md
+++ b/INSTALL.md
@@ -1,7 +1,7 @@
1#Install Instructions 1#Install Instructions
2 2
3- [Installation](#installation) 3- [Installation](#installation)
4 - [Unix like (Linux)](#unix) 4 - [Unix like](#unix)
5 - [OS X](#osx) 5 - [OS X](#osx)
6 - [Homebrew](#homebrew) 6 - [Homebrew](#homebrew)
7 - [Non-Homebrew](#non-homebrew) 7 - [Non-Homebrew](#non-homebrew)
@@ -11,7 +11,7 @@
11##Installation 11##Installation
12 12
13<a name="unix" /> 13<a name="unix" />
14###Most Unix like OSes (Linux): 14###Most Unix like OSes:
15 15
16Build dependencies: 16Build dependencies:
17 17
diff --git a/build/Makefile.am b/build/Makefile.am
index 9530e1a7..5514a4d5 100644
--- a/build/Makefile.am
+++ b/build/Makefile.am
@@ -5,8 +5,7 @@ noinst_bindir = $(top_builddir)/build
5EXTRA_DIST= 5EXTRA_DIST=
6 6
7include ../toxcore/Makefile.inc 7include ../toxcore/Makefile.inc
8include ../toxrtp/Makefile.inc 8include ../toxav/Makefile.inc
9include ../toxmsi/Makefile.inc
10include ../other/Makefile.inc 9include ../other/Makefile.inc
11include ../testing/Makefile.inc 10include ../testing/Makefile.inc
12include ../other/bootstrap_serverdaemon/Makefile.inc 11include ../other/bootstrap_serverdaemon/Makefile.inc
diff --git a/configure.ac b/configure.ac
index 62bb91bd..2b4fad78 100644
--- a/configure.ac
+++ b/configure.ac
@@ -13,8 +13,7 @@ AC_CONFIG_MACRO_DIR([m4])
13EXTRA_LT_LDFLAGS= 13EXTRA_LT_LDFLAGS=
14 14
15LIBTOXCORE_LT_VERSION=0:0:0 15LIBTOXCORE_LT_VERSION=0:0:0
16LIBTOXMSI_LT_VERSION=0:0:0 16LIBTOXAV_LT_VERSION=0:0:0
17LIBTOXRTP_LT_VERSION=0:0:0
18dnl 17dnl
19dnl current:revision:age 18dnl current:revision:age
20dnl 19dnl
@@ -24,12 +23,10 @@ dnl incremented
24dnl age: increment if interfaces have been added, set to zero if 23dnl age: increment if interfaces have been added, set to zero if
25dnl interfaces have been removed or changed 24dnl interfaces have been removed or changed
26TOXCORE_LT_LDFLAGS="-version-info $LIBTOXCORE_LT_VERSION" 25TOXCORE_LT_LDFLAGS="-version-info $LIBTOXCORE_LT_VERSION"
27TOXMSI_LT_LDFLAGS="-version-info $LIBTOXMSI_LT_VERSION" 26TOXAV_LT_LDFLAGS="-version-info $LIBTOXAV_LT_VERSION"
28TOXRTP_LT_LDFLAGS="-version-info $LIBTOXMSI_LT_VERSION"
29 27
30AC_SUBST(TOXCORE_LT_LDFLAGS) 28AC_SUBST(TOXCORE_LT_LDFLAGS)
31AC_SUBST(TOXMSI_LT_LDFLAGS) 29AC_SUBST(TOXAV_LT_LDFLAGS)
32AC_SUBST(TOXRTP_LT_LDFLAGS)
33 30
34if test "x${prefix}" = "xNONE"; then 31if test "x${prefix}" = "xNONE"; then
35 prefix="${ac_default_prefix}" 32 prefix="${ac_default_prefix}"
@@ -39,6 +36,7 @@ BUILD_DHT_BOOTSTRAP_DAEMON="yes"
39BUILD_NTOX="yes" 36BUILD_NTOX="yes"
40BUILD_TESTS="yes" 37BUILD_TESTS="yes"
41BUILD_AV="yes" 38BUILD_AV="yes"
39BUILD_PHONE="yes"
42BUILD_TESTING="yes" 40BUILD_TESTING="yes"
43 41
44NCURSES_FOUND="no" 42NCURSES_FOUND="no"
@@ -64,12 +62,24 @@ AC_ARG_ENABLE([av],
64 [ 62 [
65 if test "x$enableval" = "xno"; then 63 if test "x$enableval" = "xno"; then
66 BUILD_AV="no" 64 BUILD_AV="no"
65 BUILD_PHONE="no"
67 elif test "x$enableval" = "xyes"; then 66 elif test "x$enableval" = "xyes"; then
68 BUILD_AV="yes" 67 BUILD_AV="yes"
69 fi 68 fi
70 ] 69 ]
71) 70)
72 71
72AC_ARG_ENABLE([phone],
73 [AC_HELP_STRING([--disable-phone], [build test phone (default: auto)]) ],
74 [
75 if test "x$enableval" = "xno"; then
76 BUILD_PHONE="no"
77 elif test "x$enableval" = "xyes"; then
78 BUILD_PHONE="yes"
79 fi
80 ]
81)
82
73AC_ARG_ENABLE([tests], 83AC_ARG_ENABLE([tests],
74 [AC_HELP_STRING([--disable-tests], [build unit tests (default: auto)]) ], 84 [AC_HELP_STRING([--disable-tests], [build unit tests (default: auto)]) ],
75 [ 85 [
@@ -333,6 +343,7 @@ AC_TYPE_UINT16_T
333AC_TYPE_UINT32_T 343AC_TYPE_UINT32_T
334AC_TYPE_UINT64_T 344AC_TYPE_UINT64_T
335AC_TYPE_UINT8_T 345AC_TYPE_UINT8_T
346AC_C_BIGENDIAN
336 347
337# Checks for library functions. 348# Checks for library functions.
338AC_FUNC_FORK 349AC_FUNC_FORK
@@ -344,76 +355,77 @@ if test "x$BUILD_AV" = "xyes"; then
344 [ 355 [
345 AC_MSG_WARN([disabling AV support: required pthread library not found]) 356 AC_MSG_WARN([disabling AV support: required pthread library not found])
346 BUILD_AV="no" 357 BUILD_AV="no"
358 BUILD_PHONE="no"
347 ] 359 ]
348 ) 360 )
349fi 361fi
350 362
351if test "x$BUILD_AV" = "xyes"; then 363if test "x$BUILD_PHONE" = "xyes"; then
352 PKG_CHECK_MODULES([AVFORMAT], [libavformat], 364 PKG_CHECK_MODULES([AVFORMAT], [libavformat],
353 [], 365 [],
354 [ 366 [
355 AC_MSG_WARN([disabling AV support $AVFORMAT_PKG_ERRORS]) 367 AC_MSG_WARN([disabling phone $AVFORMAT_PKG_ERRORS])
356 BUILD_AV="no" 368 BUILD_PHONE="no"
357 ] 369 ]
358 ) 370 )
359fi 371fi
360 372
361if test "x$BUILD_AV" = "xyes"; then 373if test "x$BUILD_PHONE" = "xyes"; then
362 PKG_CHECK_MODULES([AVCODEC], [libavcodec], 374 PKG_CHECK_MODULES([AVCODEC], [libavcodec],
363 [], 375 [],
364 [ 376 [
365 AC_MSG_WARN([disabling AV support $AVCODEC_PKG_ERRORS]) 377 AC_MSG_WARN([disabling phone $AVCODEC_PKG_ERRORS])
366 BUILD_AV="no" 378 BUILD_PHONE="no"
367 ] 379 ]
368 ) 380 )
369fi 381fi
370 382
371if test "x$BUILD_AV" = "xyes"; then 383if test "x$BUILD_PHONE" = "xyes"; then
372 PKG_CHECK_MODULES([AVUTIL], [libavutil], 384 PKG_CHECK_MODULES([AVUTIL], [libavutil],
373 [], 385 [],
374 [ 386 [
375 AC_MSG_WARN([disabling AV support $AVUTIL_PKG_ERRORS]) 387 AC_MSG_WARN([disabling phone $AVUTIL_PKG_ERRORS])
376 BUILD_AV="no" 388 BUILD_PHONE="no"
377 ] 389 ]
378 ) 390 )
379fi 391fi
380 392
381if test "x$BUILD_AV" = "xyes"; then 393if test "x$BUILD_PHONE" = "xyes"; then
382 PKG_CHECK_MODULES([AVDEVICE], [libavdevice], 394 PKG_CHECK_MODULES([AVDEVICE], [libavdevice],
383 [], 395 [],
384 [ 396 [
385 AC_MSG_WARN([disabling AV support $AVDEVICE_PKG_ERRORS]) 397 AC_MSG_WARN([disabling phone $AVDEVICE_PKG_ERRORS])
386 BUILD_AV="no" 398 BUILD_PHONE="no"
387 ] 399 ]
388 ) 400 )
389fi 401fi
390 402
391if test "x$BUILD_AV" = "xyes"; then 403if test "x$BUILD_PHONE" = "xyes"; then
392 PKG_CHECK_MODULES([SWSCALE], [libswscale], 404 PKG_CHECK_MODULES([SWSCALE], [libswscale],
393 [], 405 [],
394 [ 406 [
395 AC_MSG_WARN([disabling AV support $SWSCALE_PKG_ERRORS]) 407 AC_MSG_WARN([disabling phone $SWSCALE_PKG_ERRORS])
396 BUILD_AV="no" 408 BUILD_PHONE="no"
397 ] 409 ]
398 ) 410 )
399fi 411fi
400 412
401if test "x$BUILD_AV" = "xyes"; then 413if test "x$BUILD_PHONE" = "xyes"; then
402 PKG_CHECK_MODULES([SDL], [sdl], 414 PKG_CHECK_MODULES([SDL], [sdl],
403 [], 415 [],
404 [ 416 [
405 AC_MSG_WARN([disabling AV support $SDL_PKG_ERRORS]) 417 AC_MSG_WARN([disabling phone $SDL_PKG_ERRORS])
406 BUILD_AV="no" 418 BUILD_PHONE="no"
407 ] 419 ]
408 ) 420 )
409fi 421fi
410 422
411if test "x$BUILD_AV" = "xyes"; then 423if test "x$BUILD_PHONE" = "xyes"; then
412 PKG_CHECK_MODULES([OPENAL], [openal], 424 PKG_CHECK_MODULES([OPENAL], [openal],
413 [], 425 [],
414 [ 426 [
415 AC_MSG_WARN([disabling AV support $OPENAL_PKG_ERRORS]) 427 AC_MSG_WARN([disabling phone $OPENAL_PKG_ERRORS])
416 BUILD_AV="no" 428 BUILD_PHONE="no"
417 ] 429 ]
418 ) 430 )
419fi 431fi
@@ -424,6 +436,18 @@ if test "x$BUILD_AV" = "xyes"; then
424 [ 436 [
425 AC_MSG_WARN([disabling AV support $OPUS_PKG_ERRORS]) 437 AC_MSG_WARN([disabling AV support $OPUS_PKG_ERRORS])
426 BUILD_AV="no" 438 BUILD_AV="no"
439 BUILD_PHONE="no"
440 ]
441 )
442fi
443
444if test "x$BUILD_AV" = "xyes"; then
445 PKG_CHECK_MODULES([VPX], [vpx],
446 [],
447 [
448 AC_MSG_WARN([disabling AV support $VPX_PKG_ERRORS])
449 BUILD_AV="no"
450 BUILD_PHONE="no"
427 ] 451 ]
428 ) 452 )
429fi 453fi
@@ -586,6 +610,7 @@ AM_CONDITIONAL(BUILD_DHT_BOOTSTRAP_DAEMON, test "x$BUILD_DHT_BOOTSTRAP_DAEMON" =
586AM_CONDITIONAL(BUILD_TESTS, test "x$BUILD_TESTS" = "xyes") 610AM_CONDITIONAL(BUILD_TESTS, test "x$BUILD_TESTS" = "xyes")
587AM_CONDITIONAL(BUILD_NTOX, test "x$BUILD_NTOX" = "xyes") 611AM_CONDITIONAL(BUILD_NTOX, test "x$BUILD_NTOX" = "xyes")
588AM_CONDITIONAL(BUILD_AV, test "x$BUILD_AV" = "xyes") 612AM_CONDITIONAL(BUILD_AV, test "x$BUILD_AV" = "xyes")
613AM_CONDITIONAL(BUILD_PHONE, test "x$BUILD_PHONE" = "xyes")
589AM_CONDITIONAL(BUILD_TESTING, test "x$BUILD_TESTING" = "xyes") 614AM_CONDITIONAL(BUILD_TESTING, test "x$BUILD_TESTING" = "xyes")
590AM_CONDITIONAL(WIN32, test "x$WIN32" = "xyes") 615AM_CONDITIONAL(WIN32, test "x$WIN32" = "xyes")
591 616
diff --git a/toxav/Makefile.inc b/toxav/Makefile.inc
new file mode 100644
index 00000000..30879f65
--- /dev/null
+++ b/toxav/Makefile.inc
@@ -0,0 +1,75 @@
1if BUILD_AV
2
3lib_LTLIBRARIES += libtoxav.la
4libtoxav_la_include_HEADERS = ../toxav/toxav.h
5libtoxav_la_includedir = $(includedir)/tox
6
7libtoxav_la_SOURCES = ../toxav/event.h \
8 ../toxav/event.c \
9 ../toxav/rtp.h \
10 ../toxav/rtp.c \
11 ../toxav/msi.h \
12 ../toxav/msi.c \
13 ../toxav/media.h \
14 ../toxav/media.c \
15 ../toxav/toxav.h \
16 ../toxav/toxav.c
17
18
19libtoxav_la_CFLAGS = -I../toxcore \
20 -I../toxav \
21 $(NACL_CFLAGS) \
22 $(OPUS_CFLAGS) \
23 $(VPX_CFLAGS) \
24 $(PTHREAD_CFLAGS)
25
26libtoxav_la_LDFLAGS = $(TOXAV_LT_LDFLAGS) \
27 $(NACL_LDFLAGS) \
28 $(EXTRA_LT_LDFLAGS)
29
30libtoxav_la_LIBS = $(NACL_LIBS) \
31 $(OPUS_LIBS) \
32 $(VPX_LIBS) \
33 $(PTHREAD_LIBS)
34
35
36endif
37
38
39
40
41
42
43if BUILD_PHONE
44
45
46noinst_PROGRAMS += phone
47
48phone_SOURCES = ../toxav/phone.c
49
50phone_CFLAGS = -I../toxcore \
51 -I../toxav \
52 $(AVFORMAT_CFLAGS) \
53 $(AVCODEC_CFLAGS) \
54 $(AVUTIL_CFLAGS) \
55 $(AVDEVICE_CFLAGS) \
56 $(SWSCALE_CFLAGS) \
57 $(SDL_CFLAGS) \
58 $(OPENAL_CFLAGS)
59
60phone_LDADD = libtoxav.la \
61 libtoxcore.la \
62 $(AVFORMAT_LIBS) \
63 $(AVCODEC_LIBS) \
64 $(AVUTIL_LIBS) \
65 $(AVDEVICE_LIBS) \
66 $(SWSCALE_LIBS) \
67 $(SDL_LIBS) \
68 $(OPENAL_LIBS) \
69 $(OPUS_LIBS) \
70 $(VPX_LIBS)\
71 $(PTHREAD_LIBS)\
72 $(NACL_LIBS)
73
74
75endif \ No newline at end of file
diff --git a/toxav/event.c b/toxav/event.c
new file mode 100755
index 00000000..9efe4be4
--- /dev/null
+++ b/toxav/event.c
@@ -0,0 +1,373 @@
1/** event.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 at #tox-dev @ freenode.net:6667
22 */
23
24
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif /* HAVE_CONFIG_H */
28
29#include <stdlib.h>
30#include "event.h"
31
32#include "../toxcore/util.h"
33#include "../toxcore/network.h"
34
35#define _GNU_SOURCE
36
37#include <assert.h>
38#include <unistd.h>
39#include <stddef.h>
40#include <inttypes.h>
41#include <pthread.h>
42#include <stdio.h>
43
44#define RUN_IN_THREAD(func, args) { pthread_t _tid; \
45pthread_create(&_tid, NULL, func, args); assert( pthread_detach(_tid) == 0 ); }
46
47#define LOCK(event_handler) pthread_mutex_lock (&event_handler->mutex)
48#define UNLOCK(event_handler) pthread_mutex_unlock(&event_handler->mutex)
49
50#define FREQUENCY 10000
51
52#define inline__ inline __attribute__((always_inline))
53
54
55typedef struct _EventContainer {
56 void* (*func)(void*);
57 void* func_args;
58 unsigned timeout;
59 long long id;
60
61} EventContainer;
62
63typedef struct _EventHandler {
64 EventContainer* timed_events;
65 size_t timed_events_count;
66
67 int running;
68
69 pthread_mutex_t mutex;
70
71} EventHandler;
72
73int throw_event( void* (func)(void*), void* arg );
74int reset_timer_event ( int id, uint32_t timeout );
75int throw_timer_event ( void* (func)(void*), void* arg, unsigned timeout);
76int cancel_timer_event ( int id );
77int execute_timer_event ( int id );
78
79struct _Event event =
80{
81 throw_event,
82 /* reset_timer_event */ NULL,
83 throw_timer_event,
84 cancel_timer_event,
85 /*execute_timer_event*/ NULL
86};
87
88/*
89 * Random functions used by this file
90 */
91void clear_events (EventContainer** event_container, size_t* counter)
92{
93 free(*event_container );
94
95 *event_container = NULL;
96 *counter = 0;
97}
98
99int pop_id ( EventContainer** event_container, size_t* counter, int id )
100{
101 if ( !*event_container || !*counter || !id )
102 return -1;
103
104 EventContainer* _it = *event_container;
105 int i;
106
107 for ( i = *counter; i; -- i ){
108 if ( _it->id == id ) { /* Hit! */
109 break;
110 }
111 ++_it;
112 }
113
114 if ( i ) {
115 for ( ; i; -- i ){ *_it = *(_it + 1); ++_it; }
116 -- (*counter );
117
118 if ( !(*counter)) { /* Free and set to NULL */
119 free(*event_container);
120 *event_container = NULL;
121 }
122 else {
123 void* _result = realloc(*event_container, sizeof(EventContainer) * (*counter )); /* resize */
124
125
126 if ( _result != NULL ) { *event_container = _result; return 0; }
127 else {
128 /* Not sure what would happen next so abort execution.
129 */
130 fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__);
131 abort();
132 return -1;
133 }
134 }
135 }
136
137 /* not found here */
138
139 return -1;
140}
141
142void push_event ( EventContainer** container, size_t* counter, void* (func)(void*), void* arg )
143{
144 EventContainer* _new = realloc((*container ), sizeof(EventContainer) * ((*counter ) + 1));
145
146 if ( _new == NULL ) {
147 /* Not sure what would happen next so abort execution.
148 * TODO: This could notice the calling function
149 * about realloc failing.
150 */
151 fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__);
152 abort();
153 }
154
155 _new[*counter].func = func;
156 _new[*counter].func_args = arg;
157 _new[*counter].timeout = 0;
158 _new[*counter].id = 0;
159
160 (*container) = _new;
161
162 (*counter )++;
163}
164
165void reorder_events ( size_t counter, EventContainer* container, unsigned timeout )
166{
167 if ( counter > 1 ) {
168
169 int i = counter - 1;
170
171 /* start from behind excluding last added member */
172 EventContainer* _it = &container[i - 1];
173
174 EventContainer _last_added = container[i];
175
176 for ( ; i; --i ) {
177 if ( _it->timeout > timeout ){
178 *(_it + 1) = *_it;
179 *_it = _last_added; -- _it;
180 }
181 }
182
183 }
184}
185
186/* ============================================= */
187
188/* main poll for event execution */
189void* event_poll( void* arg )
190{
191 EventHandler* _event_handler = arg;
192
193 while ( _event_handler->running )
194 {
195
196 LOCK( _event_handler );
197
198 if ( _event_handler->timed_events ){
199
200 uint32_t _time = ((uint32_t)(current_time() / 1000));
201
202 if ( _event_handler->timed_events[0].timeout < _time ) {
203
204 RUN_IN_THREAD ( _event_handler->timed_events[0].func,
205 _event_handler->timed_events[0].func_args );
206
207 pop_id(&_event_handler->timed_events,
208 &_event_handler->timed_events_count,
209 _event_handler->timed_events[0].id);
210
211 }
212
213 }
214
215 UNLOCK( _event_handler );
216
217 usleep(FREQUENCY);
218 }
219
220 LOCK( _event_handler );
221
222 clear_events(&_event_handler->timed_events, &_event_handler->timed_events_count);
223
224 UNLOCK( _event_handler );
225
226 _event_handler->running = -1;
227 pthread_exit(NULL);
228}
229
230int throw_event( void* (func)(void*), void* arg )
231{
232 pthread_t _tid;
233 int _rc =
234 pthread_create(&_tid, NULL, func, arg );
235
236 return (0 != _rc ) ? _rc : pthread_detach(_tid);
237}
238
239EventHandler event_handler;
240
241/* Place and order array of timers */
242int throw_timer_event ( void* (func)(void*), void* arg, unsigned timeout)
243{
244 static int _unique_id = 1;
245
246 push_event(&event_handler.timed_events, &(event_handler.timed_events_count), func, arg );
247
248 size_t _counter = event_handler.timed_events_count;
249
250 event_handler.timed_events[_counter - 1].timeout = timeout + ((uint32_t)(current_time() / 1000));
251 event_handler.timed_events[_counter - 1].id = _unique_id; ++_unique_id;
252
253
254 /* reorder */
255
256 reorder_events(_counter, event_handler.timed_events, timeout );
257
258 return _unique_id - 1;
259}
260
261int execute_timer_event ( int id )
262{
263 int _status;
264
265 LOCK((&event_handler));
266 EventContainer* _it = event_handler.timed_events;
267
268 int _i = event_handler.timed_events_count;
269
270 /* Find it and execute */
271 for ( ; _i; _i-- ) {
272 if ( _it->id == id ) {
273 RUN_IN_THREAD ( _it->func, _it->func_args );
274 break;
275 }
276 ++_it;
277 }
278
279 /* Now remove it from the queue */
280
281 if ( _i ) {
282 for ( ; _i; -- _i ){ *_it = *(_it + 1); ++_it; }
283
284 -- event_handler.timed_events_count;
285
286 if ( !event_handler.timed_events_count ) { /* Free and set to null */
287 free(event_handler.timed_events);
288 event_handler.timed_events = NULL;
289 }
290 else {
291 void* _result = realloc(event_handler.timed_events, sizeof(EventContainer) * event_handler.timed_events_count); /* resize */
292
293 if ( _result != NULL ) { event_handler.timed_events = _result; }
294 else {
295 /* Not sure what would happen next so abort execution.
296 */
297 fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__);
298 abort();
299 return -1;
300 }
301 }
302
303 _status = 0;
304
305 }
306 else _status = -1;
307
308 UNLOCK((&event_handler));
309
310 return _status;
311}
312
313int reset_timer_event ( int id, uint32_t timeout )
314{
315 int _status;
316
317 LOCK((&event_handler));
318
319 EventContainer* _it = event_handler.timed_events;
320
321 int _i = event_handler.timed_events_count;
322
323 /* Find it and change */
324 for ( ; _i; _i-- ) {
325 if ( _it->id == id ) {
326 _it->timeout = timeout + ((uint32_t)(current_time() / 1000));
327 break;
328 }
329 ++_it;
330 }
331
332 _status = _i ? -1 : 0;
333
334 UNLOCK((&event_handler));
335
336 return _status;
337}
338
339/* Remove timer from array */
340inline__ int cancel_timer_event ( int id )
341{
342 return pop_id (&event_handler.timed_events, &event_handler.timed_events_count, id );
343}
344
345
346/* Initialization and termination of event polls
347 * This will be run at the beginning and the end of the program execution.
348 * I think that's the best way to do it.
349 */
350
351void __attribute__((constructor)) init_event_poll ()
352{
353 event_handler.timed_events = NULL;
354 event_handler.timed_events_count = 0;
355
356 event_handler.running = 1;
357
358 pthread_mutex_init(&event_handler.mutex, NULL);
359
360 RUN_IN_THREAD(event_poll, &event_handler);
361}
362
363/* NOTE: Do we need this? */
364void __attribute__((destructor)) terminate_event_poll()
365{
366 /* Exit thread */
367 event_handler.running = 0;
368
369 /* Give it enought time to exit */
370 usleep(FREQUENCY*2);
371
372 pthread_mutex_destroy( &event_handler.mutex );
373} \ No newline at end of file
diff --git a/toxav/event.h b/toxav/event.h
new file mode 100755
index 00000000..17dadbd5
--- /dev/null
+++ b/toxav/event.h
@@ -0,0 +1,50 @@
1/** event.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 at #tox-dev @ freenode.net:6667
22 */
23
24
25#ifndef __TOXEVENT
26#define __TOXEVENT
27
28
29/**
30 * - Events are, in fact, ran in their own threads upon execution.
31 * - Event handler is initialized at the start, before the main() function
32 * and terminated after it's execution.
33 * - Timers are checked for timeout every ~10000 ns.
34 * - Timers can be canceled or ran immediately via
35 * timer_release() or timer_now() functions.
36 * - Timeout is measured in milliseconds.
37 *
38 * NOTE: timer_reset () and timer_now() are not tested nor usable atm
39 *
40 */
41extern struct _Event
42{
43 int (*rise) (void* ( func ) ( void* ), void* arg);
44 int (*timer_reset ) ( int id, unsigned timeout );
45 int (*timer_alloc) (void* ( func ) ( void* ), void* arg, unsigned timeout);
46 int (*timer_release) (int id);
47 int (*timer_now) ( int id );
48} event;
49
50#endif /* _MSI__EVENT_H_ */
diff --git a/toxav/media.c b/toxav/media.c
new file mode 100644
index 00000000..c4894076
--- /dev/null
+++ b/toxav/media.c
@@ -0,0 +1,309 @@
1/** media.c
2 *
3 * Audio and video codec intitialization, 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#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif /* HAVE_CONFIG_H */
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <math.h>
32#include <assert.h>
33
34#include "rtp.h"
35#include "media.h"
36
37struct jitter_buffer {
38 RTPMessage **queue;
39 uint16_t capacity;
40 uint16_t size;
41 uint16_t front;
42 uint16_t rear;
43 uint8_t queue_ready;
44 uint16_t current_id;
45 uint32_t current_ts;
46 uint8_t id_set;
47};
48
49
50struct jitter_buffer *create_queue(int capacity)
51{
52 struct jitter_buffer *q;
53 q = (struct jitter_buffer *)calloc(sizeof(struct jitter_buffer),1);
54 q->queue = (RTPMessage **)calloc(sizeof(RTPMessage*), capacity);
55 int i = 0;
56
57 for (i = 0; i < capacity; ++i) {
58 q->queue[i] = NULL;
59 }
60
61 q->size = 0;
62 q->capacity = capacity;
63 q->front = 0;
64 q->rear = -1;
65 q->queue_ready = 0;
66 q->current_id = 0;
67 q->current_ts = 0;
68 q->id_set = 0;
69 return q;
70}
71
72/* returns 1 if 'a' has a higher sequence number than 'b' */
73uint8_t sequence_number_older(uint16_t sn_a, uint16_t sn_b, uint32_t ts_a, uint32_t ts_b)
74{
75 /* TODO: There is already this kind of function in toxrtp.c.
76 * Maybe merge?
77 */
78 return (sn_a > sn_b || ts_a > ts_b);
79}
80
81/* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */
82RTPMessage *dequeue(struct jitter_buffer *q, int *success)
83{
84 if (q->size == 0 || q->queue_ready == 0) {
85 q->queue_ready = 0;
86 *success = 0;
87 return NULL;
88 }
89
90 int front = q->front;
91
92 if (q->id_set == 0) {
93 q->current_id = q->queue[front]->header->sequnum;
94 q->current_ts = q->queue[front]->header->timestamp;
95 q->id_set = 1;
96 } else {
97 int next_id = q->queue[front]->header->sequnum;
98 int next_ts = q->queue[front]->header->timestamp;
99
100 /* if this packet is indeed the expected packet */
101 if (next_id == (q->current_id + 1) % MAX_SEQU_NUM) {
102 q->current_id = next_id;
103 q->current_ts = next_ts;
104 } else {
105 if (sequence_number_older(next_id, q->current_id, next_ts, q->current_ts)) {
106 printf("nextid: %d current: %d\n", next_id, q->current_id);
107 q->current_id = (q->current_id + 1) % MAX_SEQU_NUM;
108 *success = 2; /* tell the decoder the packet is lost */
109 return NULL;
110 } else {
111 /* packet too old */
112 printf("packet too old\n");
113 *success = 0;
114 return NULL;
115 }
116 }
117 }
118
119 q->size--;
120 q->front++;
121
122 if (q->front == q->capacity)
123 q->front = 0;
124
125 *success = 1;
126 q->current_id = q->queue[front]->header->sequnum;
127 q->current_ts = q->queue[front]->header->timestamp;
128 return q->queue[front];
129}
130
131int empty_queue(struct jitter_buffer *q)
132{
133 while (q->size > 0) {
134 q->size--;
135 rtp_free_msg(NULL, q->queue[q->front]);
136 q->front++;
137
138 if (q->front == q->capacity)
139 q->front = 0;
140 }
141
142 q->id_set = 0;
143 q->queue_ready = 0;
144 return 0;
145}
146
147int queue(struct jitter_buffer *q, RTPMessage *pk)
148{
149 if (q->size == q->capacity) {
150 printf("buffer full, emptying buffer...\n");
151 empty_queue(q);
152 return 0;
153 }
154
155 if (q->size > 8)
156 q->queue_ready = 1;
157
158 ++q->size;
159 ++q->rear;
160
161 if (q->rear == q->capacity)
162 q->rear = 0;
163
164 q->queue[q->rear] = pk;
165
166 int a;
167 int b;
168 int j;
169 a = q->rear;
170
171 for (j = 0; j < q->size - 1; ++j) {
172 b = a - 1;
173
174 if (b < 0)
175 b += q->capacity;
176
177 if (sequence_number_older(q->queue[b]->header->sequnum, q->queue[a]->header->sequnum,
178 q->queue[b]->header->timestamp, q->queue[a]->header->timestamp)) {
179 RTPMessage *temp;
180 temp = q->queue[a];
181 q->queue[a] = q->queue[b];
182 q->queue[b] = temp;
183 printf("had to swap\n");
184 } else {
185 break;
186 }
187
188 a -= 1;
189
190 if (a < 0)
191 a += q->capacity;
192 }
193
194 if (pk)
195 return 1;
196
197 return 0;
198}
199
200
201int init_video_decoder(CodecState *cs)
202{
203 if (vpx_codec_dec_init_ver(&cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0, VPX_DECODER_ABI_VERSION) != VPX_CODEC_OK) {
204 fprintf(stderr, "Init video_decoder failed!\n");
205 return -1;
206 }
207
208 return 0;
209}
210
211int init_audio_decoder(CodecState *cs, uint32_t audio_channels)
212{
213 int rc;
214 cs->audio_decoder = opus_decoder_create(cs->audio_sample_rate, audio_channels, &rc );
215
216 if ( rc != OPUS_OK ){
217 fprintf(stderr, "Error while starting audio decoder!\n");
218 return -1;
219 }
220
221 return 0;
222}
223
224
225int init_video_encoder(CodecState *cs, uint16_t width, uint16_t height, uint32_t video_bitrate)
226{
227 vpx_codec_enc_cfg_t cfg;
228 int res = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0);
229 if(res) {
230 printf("Failed to get config: %s\n", vpx_codec_err_to_string(res));
231 return -1;
232 }
233
234 cfg.rc_target_bitrate = video_bitrate;
235 cfg.g_w = width;
236 cfg.g_h = height;
237 if(vpx_codec_enc_init_ver(&cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0, VPX_ENCODER_ABI_VERSION) != VPX_CODEC_OK) {
238 fprintf(stderr, "Failed to initialize encoder\n");
239 return -1;
240 }
241 return 0;
242}
243
244int init_audio_encoder(CodecState *cs, uint32_t audio_channels)
245{
246 int err = OPUS_OK;
247 cs->audio_encoder = opus_encoder_create(cs->audio_sample_rate, audio_channels, OPUS_APPLICATION_AUDIO, &err);
248 err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_bitrate));
249 err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10));
250
251
252 return err == OPUS_OK ? 0 : -1;
253}
254
255
256CodecState* codec_init_session ( uint32_t audio_bitrate,
257 uint16_t audio_frame_duration,
258 uint32_t audio_sample_rate,
259 uint32_t audio_channels,
260 uint16_t video_width,
261 uint16_t video_height,
262 uint32_t video_bitrate )
263{
264 CodecState* _retu = calloc(sizeof(CodecState), 1);
265 assert(_retu);
266
267 _retu->audio_bitrate = audio_bitrate;
268 _retu->audio_sample_rate = audio_sample_rate;
269
270 /* Encoders */
271 if (!video_width || !video_height) {
272 video_width = 320;
273 video_height = 240;
274 }
275
276 if ( 0 == init_video_encoder(_retu, video_width, video_height, video_bitrate) )
277 printf("Video encoder initialized!\n");
278
279 if ( 0 == init_audio_encoder(_retu, audio_channels) )
280 printf("Audio encoder initialized!\n");
281
282
283 /* Decoders */
284 if ( 0 == init_video_decoder(_retu) )
285 printf("Video decoder initialized!\n");
286
287 if ( 0 == init_audio_decoder(_retu, audio_channels) )
288 printf("Audio decoder initialized!\n");
289
290
291 return _retu;
292}
293
294void codec_terminate_session ( CodecState* cs )
295{
296 if ( cs->audio_encoder ) {
297 opus_encoder_destroy(cs->audio_encoder);
298 printf("Terminated encoder!\n");
299 }
300
301 if ( cs->audio_decoder ) {
302 opus_decoder_destroy(cs->audio_decoder);
303 printf("Terminated decoder!\n");
304 }
305
306 /* TODO: Terminate video */
307 vpx_codec_destroy(&cs->v_decoder);
308 vpx_codec_destroy(&cs->v_encoder);
309}
diff --git a/toxav/media.h b/toxav/media.h
new file mode 100644
index 00000000..030a36ac
--- /dev/null
+++ b/toxav/media.h
@@ -0,0 +1,80 @@
1/** media.h
2 *
3 * Audio and video codec intitialization, 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#ifndef _AVCODEC_H_
25#define _AVCODEC_H_
26
27#include <stdio.h>
28#include <math.h>
29#include <pthread.h>
30
31#include <vpx/vpx_decoder.h>
32#include <vpx/vpx_encoder.h>
33#include <vpx/vp8dx.h>
34#include <vpx/vp8cx.h>
35#define VIDEO_CODEC_DECODER_INTERFACE (vpx_codec_vp8_dx())
36#define VIDEO_CODEC_ENCODER_INTERFACE (vpx_codec_vp8_cx())
37
38/* Audio encoding/decoding */
39#include <opus/opus.h>
40
41
42typedef struct _CodecState{
43
44 /* video encoding */
45 vpx_codec_ctx_t v_encoder;
46 uint32_t frame_counter;
47
48 /* video decoding */
49 vpx_codec_ctx_t v_decoder;
50
51 /* audio encoding */
52 OpusEncoder *audio_encoder;
53 int audio_bitrate;
54 int audio_sample_rate;
55
56 /* audio decoding */
57 OpusDecoder *audio_decoder;
58
59} CodecState;
60
61typedef struct _RTPMessage RTPMessage;
62
63struct jitter_buffer *create_queue(int capacity);
64int empty_queue(struct jitter_buffer *q);
65
66int queue(struct jitter_buffer *q, RTPMessage *pk);
67RTPMessage *dequeue(struct jitter_buffer *q, int *success);
68
69
70CodecState* codec_init_session ( uint32_t audio_bitrate,
71 uint16_t audio_frame_duration,
72 uint32_t audio_sample_rate,
73 uint32_t audio_channels,
74 uint16_t video_width,
75 uint16_t video_height,
76 uint32_t video_bitrate );
77
78void codec_terminate_session(CodecState* cs);
79
80#endif
diff --git a/toxav/msi.c b/toxav/msi.c
new file mode 100644
index 00000000..84ab973f
--- /dev/null
+++ b/toxav/msi.c
@@ -0,0 +1,1379 @@
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 at #tox-dev @ freenode.net:6667
22 */
23
24
25#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif /* HAVE_CONFIG_H */
28
29#define _BSD_SOURCE
30
31#include "msi.h"
32#include "event.h"
33#include "../toxcore/util.h"
34#include "../toxcore/network.h"
35#include "../toxcore/Messenger.h"
36
37#include <assert.h>
38#include <unistd.h>
39#include <string.h>
40#include <stdlib.h>
41
42#define same(x, y) strcmp((const char*) x, (const char*) y) == 0
43
44#define MSI_MAXMSG_SIZE 1024
45
46#define TYPE_REQUEST 1
47#define TYPE_RESPONSE 2
48
49unsigned char* VERSION_STRING = (unsigned char*)"0.3.1";
50#define VERSION_STRLEN 5
51
52#define CT_AUDIO_HEADER_VALUE "AUDIO"
53#define CT_VIDEO_HEADER_VALUE "VIDEO"
54
55
56/* Define default timeout for a request.
57 * There is no behavior specified by the msi on what will
58 * client do on timeout, but to call timeout callback.
59 */
60#define m_deftout 10000 /* in milliseconds */
61
62/**
63 * Protocol:
64 *
65 * | desc. ( 1 byte ) | length ( 2 bytes ) | value ( length bytes ) |
66 *
67 * ie.
68 *
69 * | 0x1 | 0x0 0x7 | "version"
70 *
71 * Means: it's field value with length of 7 bytes and value of "version"
72 * It's similar to amp protocol
73 */
74
75
76#define GENERIC_HEADER(header) \
77typedef struct _MSIHeader##header { \
78uint8_t* header_value; \
79uint16_t size; \
80} MSIHeader##header;
81
82
83GENERIC_HEADER ( Version )
84GENERIC_HEADER ( Request )
85GENERIC_HEADER ( Response )
86GENERIC_HEADER ( CallType )
87GENERIC_HEADER ( UserAgent )
88GENERIC_HEADER ( CallId )
89GENERIC_HEADER ( Info )
90GENERIC_HEADER ( Reason )
91GENERIC_HEADER ( CryptoKey )
92GENERIC_HEADER ( Nonce )
93
94
95/**
96 * @brief This is the message structure. It contains all of the headers and
97 * destination/source of the message stored in friend_id.
98 *
99 */
100typedef struct _MSIMessage {
101
102 MSIHeaderVersion version;
103 MSIHeaderRequest request;
104 MSIHeaderResponse response;
105 MSIHeaderCallType calltype;
106 MSIHeaderUserAgent useragent;
107 MSIHeaderInfo info;
108 MSIHeaderReason reason;
109 MSIHeaderCallId callid;
110 MSIHeaderCryptoKey cryptokey;
111 MSIHeaderNonce nonce;
112
113 struct _MSIMessage* next;
114
115 int friend_id;
116
117} MSIMessage;
118
119
120
121static MSICallback callbacks[10] = {0};
122
123
124/* define strings for the identifiers */
125#define VERSION_FIELD "Version"
126#define REQUEST_FIELD "Request"
127#define RESPONSE_FIELD "Response"
128#define INFO_FIELD "INFO"
129#define REASON_FIELD "Reason"
130#define CALLTYPE_FIELD "Call-type"
131#define USERAGENT_FIELD "User-agent"
132#define CALLID_FIELD "Call-id"
133#define CRYPTOKEY_FIELD "Crypto-key"
134#define NONCE_FIELD "Nonce"
135
136/* protocol descriptors */
137#define end_byte 0x0
138#define field_byte 0x1
139#define value_byte 0x2
140
141
142typedef enum {
143 invite,
144 start,
145 cancel,
146 reject,
147 end,
148
149} MSIRequest;
150
151
152/**
153 * @brief Get string value for request.
154 *
155 * @param request The request.
156 * @return const uint8_t* The string
157 */
158static inline const uint8_t *stringify_request ( MSIRequest request ) {
159 static const uint8_t* strings[] = {
160 ( uint8_t* ) "INVITE",
161 ( uint8_t* ) "START",
162 ( uint8_t* ) "CANCEL",
163 ( uint8_t* ) "REJECT",
164 ( uint8_t* ) "END"
165 };
166
167 return strings[request];
168}
169
170
171typedef enum {
172 ringing,
173 starting,
174 ending,
175 error
176
177} MSIResponse;
178
179
180/**
181 * @brief Get string value for response.
182 *
183 * @param response The response.
184 * @return const uint8_t* The string
185 */
186static inline const uint8_t *stringify_response ( MSIResponse response ) {
187 static const uint8_t* strings[] = {
188 ( uint8_t* ) "ringing",
189 ( uint8_t* ) "starting",
190 ( uint8_t* ) "ending",
191 ( uint8_t* ) "error"
192 };
193
194 return strings[response];
195}
196
197
198#define ON_HEADER(iterator, header, descriptor, size_const) \
199( memcmp(iterator, descriptor, size_const) == 0){ /* Okay */ \
200 iterator += size_const; /* Set iterator at begining of value part */ \
201 if ( *iterator != value_byte ) { assert(0); return -1; }\
202 iterator ++;\
203 uint16_t _value_size = (uint16_t) *(iterator ) << 8 | \
204 (uint16_t) *(iterator + 1); \
205 header.header_value = calloc(sizeof(uint8_t), _value_size); \
206 header.size = _value_size; \
207 memcpy(header.header_value, iterator + 2, _value_size);\
208 iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ \
209}
210
211/**
212 * @brief Parse raw 'data' received from socket into MSIMessage struct.
213 * Every message has to have end value of 'end_byte' or _undefined_ behavior
214 * occures. The best practice is to check the end of the message at the handle_packet.
215 *
216 * @param msg Container.
217 * @param data The data.
218 * @return int
219 * @retval -1 Error occured.
220 * @retval 0 Success.
221 */
222int parse_raw_data ( MSIMessage* msg, const uint8_t* data, uint16_t length ) {
223 assert ( msg );
224
225 if ( data[length - 1] ) /* End byte must have value 0 */
226 return -1;
227
228 const uint8_t* _it = data;
229
230 while ( *_it ) {/* until end_byte is hit */
231
232 uint16_t itedlen = (_it - data) + 2;
233
234 if ( *_it == field_byte && itedlen < length ) {
235
236 uint16_t _size = ( uint16_t ) * ( _it + 1 ) << 8 |
237 ( uint16_t ) * ( _it + 2 );
238
239 if ( itedlen + _size > length ) return -1;
240
241 _it += 3; /* place it at the field value beginning */
242
243 switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */
244
245 case 4: { /* INFO header */
246 if ON_HEADER ( _it, msg->info, INFO_FIELD, 4 )
247 }
248 break;
249
250 case 5: { /* NONCE header */
251 if ON_HEADER ( _it, msg->nonce, NONCE_FIELD, 5 )
252 }
253 break;
254
255 case 6: { /* Reason header */
256 if ON_HEADER ( _it, msg->reason, REASON_FIELD, 6 )
257 }
258 break;
259
260 case 7: { /* Version, Request, Call-id headers */
261 if ON_HEADER ( _it, msg->version, VERSION_FIELD, 7 )
262 else if ON_HEADER ( _it, msg->request, REQUEST_FIELD, 7 )
263 else if ON_HEADER ( _it, msg->callid, CALLID_FIELD, 7 )
264 }
265 break;
266
267 case 8: { /* Response header */
268 if ON_HEADER ( _it, msg->response, RESPONSE_FIELD, 8 )
269 }
270 break;
271
272 case 9: { /* Call-type header */
273 if ON_HEADER ( _it, msg->calltype, CALLTYPE_FIELD, 9 )
274 }
275 break;
276
277 case 10: { /* User-agent, Crypto-key headers */
278 if ON_HEADER ( _it, msg->useragent, USERAGENT_FIELD, 10 )
279 else if ON_HEADER ( _it, msg->cryptokey, CRYPTOKEY_FIELD, 10 )
280 }
281 break;
282
283 default:
284 return -1;
285 }
286 } else return -1;
287 /* If it's anything else return failure as the message is invalid */
288
289 }
290
291 return 0;
292}
293
294
295#define ALLOCATE_HEADER( var, mheader_value, t_size) \
296var.header_value = calloc(sizeof *mheader_value, t_size); \
297memcpy(var.header_value, mheader_value, t_size); \
298var.size = t_size;
299
300
301/**
302 * @brief Speaks for it self.
303 *
304 * @param msg The message.
305 * @return void
306 */
307void free_message ( MSIMessage* msg ) {
308 assert ( msg );
309
310 free ( msg->calltype.header_value );
311 free ( msg->request.header_value );
312 free ( msg->response.header_value );
313 free ( msg->useragent.header_value );
314 free ( msg->version.header_value );
315 free ( msg->info.header_value );
316 free ( msg->cryptokey.header_value );
317 free ( msg->nonce.header_value );
318 free ( msg->reason.header_value );
319 free ( msg->callid.header_value );
320
321 free ( msg );
322}
323
324
325/**
326 * @brief Create the message.
327 *
328 * @param type Request or response.
329 * @param type_id Type of request/response.
330 * @return MSIMessage* Created message.
331 * @retval NULL Error occured.
332 */
333MSIMessage* msi_new_message ( uint8_t type, const uint8_t* type_id ) {
334 MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 );
335 assert ( _retu );
336
337 if ( type == TYPE_REQUEST ) {
338 ALLOCATE_HEADER ( _retu->request, type_id, strlen ( (const char*)type_id ) )
339
340 } else if ( type == TYPE_RESPONSE ) {
341 ALLOCATE_HEADER ( _retu->response, type_id, strlen ( (const char*)type_id ) )
342
343 } else {
344 free_message ( _retu );
345 return NULL;
346 }
347
348 ALLOCATE_HEADER ( _retu->version, VERSION_STRING, strlen ( (const char*)VERSION_STRING ) )
349
350 return _retu;
351}
352
353
354/**
355 * @brief Parse data from handle_packet.
356 *
357 * @param data The data.
358 * @return MSIMessage* Parsed message.
359 * @retval NULL Error occured.
360 */
361MSIMessage* parse_message ( const uint8_t* data, uint16_t length ) {
362 assert ( data );
363
364 MSIMessage* _retu = calloc ( sizeof ( MSIMessage ), 1 );
365 assert ( _retu );
366
367 memset ( _retu, 0, sizeof ( MSIMessage ) );
368
369 if ( parse_raw_data ( _retu, data, length ) == -1 ) {
370
371 free_message ( _retu );
372 return NULL;
373 }
374
375 if ( !_retu->version.header_value || VERSION_STRLEN != _retu->version.size ||
376 memcmp ( _retu->version.header_value, VERSION_STRING, VERSION_STRLEN ) != 0 ) {
377
378 free_message ( _retu );
379 return NULL;
380 }
381
382 return _retu;
383}
384
385
386
387/**
388 * @brief Speaks for it self.
389 *
390 * @param dest Container.
391 * @param header_field Field.
392 * @param header_value Field value.
393 * @param value_len Length of field value.
394 * @param length Pointer to container length.
395 * @return uint8_t* Iterated container.
396 */
397uint8_t* append_header_to_string (
398 uint8_t* dest,
399 const uint8_t* header_field,
400 const uint8_t* header_value,
401 uint16_t value_len,
402 uint16_t* length )
403{
404 assert ( dest );
405 assert ( header_value );
406 assert ( header_field );
407
408 const uint8_t* _hvit = header_value;
409 uint16_t _total = 6 + value_len; /* 6 is known plus header value len + field len*/
410
411 *dest = field_byte; /* Set the first byte */
412
413 uint8_t* _getback_byte = dest + 1; /* remeber the byte we were on */
414 dest += 3; /* swith to 4th byte where field value starts */
415
416 /* Now set the field value and calculate it's length */
417 uint16_t _i = 0;
418 for ( ; header_field[_i]; ++_i ) {
419 *dest = header_field[_i];
420 ++dest;
421 };
422 _total += _i;
423
424 /* Now set the length of the field byte */
425 *_getback_byte = ( uint8_t ) _i >> 8;
426 _getback_byte++;
427 *_getback_byte = ( uint8_t ) _i;
428
429 /* for value part do it regulary */
430 *dest = value_byte;
431 dest++;
432
433 *dest = ( uint8_t ) value_len >> 8;
434 dest++;
435 *dest = ( uint8_t ) value_len;
436 dest++;
437
438 for ( _i = value_len; _i; --_i ) {
439 *dest = *_hvit;
440 ++_hvit;
441 ++dest;
442 }
443
444 *length += _total;
445 return dest;
446}
447
448
449#define CLEAN_ASSIGN(added, var, field, header)\
450if ( header.header_value ) { var = append_header_to_string(var, (const uint8_t*)field, header.header_value, header.size, &added); }
451
452
453/**
454 * @brief Convert MSIMessage struct to _sendable_ string.
455 *
456 * @param msg The message.
457 * @param dest Destination.
458 * @return uint16_t It's final size.
459 */
460uint16_t message_to_string ( MSIMessage* msg, uint8_t* dest ) {
461 assert ( msg );
462 assert ( dest );
463
464 uint8_t* _iterated = dest;
465 uint16_t _size = 0;
466
467 CLEAN_ASSIGN ( _size, _iterated, VERSION_FIELD, msg->version );
468 CLEAN_ASSIGN ( _size, _iterated, REQUEST_FIELD, msg->request );
469 CLEAN_ASSIGN ( _size, _iterated, RESPONSE_FIELD, msg->response );
470 CLEAN_ASSIGN ( _size, _iterated, CALLTYPE_FIELD, msg->calltype );
471 CLEAN_ASSIGN ( _size, _iterated, USERAGENT_FIELD, msg->useragent );
472 CLEAN_ASSIGN ( _size, _iterated, INFO_FIELD, msg->info );
473 CLEAN_ASSIGN ( _size, _iterated, CALLID_FIELD, msg->callid );
474 CLEAN_ASSIGN ( _size, _iterated, REASON_FIELD, msg->reason );
475 CLEAN_ASSIGN ( _size, _iterated, CRYPTOKEY_FIELD, msg->cryptokey );
476 CLEAN_ASSIGN ( _size, _iterated, NONCE_FIELD, msg->nonce );
477
478 *_iterated = end_byte;
479 _size ++;
480
481 return _size;
482}
483
484
485#define GENERIC_SETTER_DEFINITION(header) \
486void msi_msg_set_##header ( MSIMessage* _msg, const uint8_t* header_value, uint16_t _size ) \
487{ assert(_msg); assert(header_value); \
488 free(_msg->header.header_value); \
489 ALLOCATE_HEADER( _msg->header, header_value, _size )}
490
491GENERIC_SETTER_DEFINITION ( calltype )
492GENERIC_SETTER_DEFINITION ( useragent )
493GENERIC_SETTER_DEFINITION ( reason )
494GENERIC_SETTER_DEFINITION ( info )
495GENERIC_SETTER_DEFINITION ( callid )
496GENERIC_SETTER_DEFINITION ( cryptokey )
497GENERIC_SETTER_DEFINITION ( nonce )
498
499
500/**
501 * @brief Generate _random_ alphanumerical string.
502 *
503 * @param str Destination.
504 * @param size Size of string.
505 * @return void
506 */
507void t_randomstr ( uint8_t* str, size_t size ) {
508 assert ( str );
509
510 static const uint8_t _bytes[] =
511 "0123456789"
512 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
513 "abcdefghijklmnopqrstuvwxyz";
514
515 size_t _it = 0;
516
517 for ( ; _it < size; _it++ ) {
518 str[_it] = _bytes[ random_int() % 61 ];
519 }
520}
521
522
523typedef enum {
524 error_deadcall = 1, /* has call id but it's from old call */
525 error_id_mismatch, /* non-existing call */
526
527 error_no_callid, /* not having call id */
528 error_no_call, /* no call in session */
529 error_no_crypto_key, /* no crypto key */
530
531 error_busy
532
533} MSICallError; /* Error codes */
534
535
536/**
537 * @brief Stringify error code.
538 *
539 * @param error_code The code.
540 * @return const uint8_t* The string.
541 */
542static inline const uint8_t *stringify_error ( MSICallError error_code ) {
543 static const uint8_t* strings[] = {
544 ( uint8_t* ) "",
545 ( uint8_t* ) "Using dead call",
546 ( uint8_t* ) "Call id not set to any call",
547 ( uint8_t* ) "Call id not available",
548 ( uint8_t* ) "No active call in session",
549 ( uint8_t* ) "No Crypto-key set",
550 ( uint8_t* ) "Callee busy"
551 };
552
553 return strings[error_code];
554}
555
556
557/**
558 * @brief Convert error_code into string.
559 *
560 * @param error_code The code.
561 * @return const uint8_t* The string.
562 */
563static inline const uint8_t *stringify_error_code ( MSICallError error_code ) {
564 static const uint8_t* strings[] = {
565 ( uint8_t* ) "",
566 ( uint8_t* ) "1",
567 ( uint8_t* ) "2",
568 ( uint8_t* ) "3",
569 ( uint8_t* ) "4",
570 ( uint8_t* ) "5",
571 ( uint8_t* ) "6"
572 };
573
574 return strings[error_code];
575}
576
577
578/**
579 * @brief Speaks for it self.
580 *
581 * @param session Control session.
582 * @param msg The message.
583 * @param to Where to.
584 * @return int
585 * @retval -1 Error occured.
586 * @retval 0 Success.
587 */
588int send_message ( MSISession* session, MSIMessage* msg, uint32_t to )
589{
590 msi_msg_set_callid ( msg, session->call->id, CALL_ID_LEN );
591
592 uint8_t _msg_string_final [MSI_MAXMSG_SIZE];
593 uint16_t _length = message_to_string ( msg, _msg_string_final );
594
595 return m_msi_packet(session->messenger_handle, to, _msg_string_final, _length) ? 0 : -1;
596}
597
598
599/**
600 * @brief Speaks for it self.
601 *
602 * @param session Control session.
603 * @param msg The message.
604 * @param peer_id The peer.
605 * @return void
606 */
607void flush_peer_type ( MSISession* session, MSIMessage* msg, int peer_id ) {
608 if ( msg->calltype.header_value ) {
609 if ( strcmp ( ( const char* ) msg->calltype.header_value, CT_AUDIO_HEADER_VALUE ) == 0 ) {
610 session->call->type_peer[peer_id] = type_audio;
611
612 } else if ( strcmp ( ( const char* ) msg->calltype.header_value, CT_VIDEO_HEADER_VALUE ) == 0 ) {
613 session->call->type_peer[peer_id] = type_video;
614 } else {} /* Error */
615 } else {} /* Error */
616}
617
618void handle_remote_connection_change(Messenger* messenger, int friend_num, uint8_t status, void* session_p)
619{
620 MSISession* session = session_p;
621
622 switch ( status )
623 {
624 case 0: /* Went offline */
625 {
626 if ( session->call ) {
627 int i = 0;
628 for ( ; i < session->call->peer_count; i ++ )
629 if ( session->call->peers[i] == friend_num ) {
630 msi_stopcall(session); /* Stop the call for now */
631 return;
632 }
633 }
634 } break;
635
636 default: break;
637 }
638}
639
640/**
641 * @brief Sends error response to peer.
642 *
643 * @param session The session.
644 * @param errid The id.
645 * @param to Where to?
646 * @return int
647 * @retval 0 It's always success.
648 */
649int handle_error ( MSISession* session, MSICallError errid, uint32_t to ) {
650 MSIMessage* _msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) );
651
652 const uint8_t* _error_code_str = stringify_error_code ( errid );
653
654 msi_msg_set_reason ( _msg_error, _error_code_str, strlen ( ( const char* ) _error_code_str ) );
655 send_message ( session, _msg_error, to );
656 free_message ( _msg_error );
657
658 session->last_error_id = errid;
659 session->last_error_str = stringify_error ( errid );
660
661 event.rise ( callbacks[MSI_OnError], session->agent_handler );
662
663 return 0;
664}
665
666
667/**
668 * @brief Determine the error if any.
669 *
670 * @param session Control session.
671 * @param msg The message.
672 * @return int
673 * @retval -1 No error.
674 * @retval 0 Error occured and response sent.
675 */
676int has_call_error ( MSISession* session, MSIMessage* msg ) {
677 if ( !msg->callid.header_value ) {
678 return handle_error ( session, error_no_callid, msg->friend_id );
679
680 } else if ( !session->call ) {
681 return handle_error ( session, error_no_call, msg->friend_id );
682
683 } else if ( memcmp ( session->call->id, msg->callid.header_value, CALL_ID_LEN ) != 0 ) {
684 return handle_error ( session, error_id_mismatch, msg->friend_id );
685
686 }
687
688 return -1;
689}
690
691
692/**
693 * @brief Function called at request timeout.
694 *
695 * @param arg Control session
696 * @return void*
697 */
698void* handle_timeout ( void* arg )
699{
700 /* Send hangup either way */
701 MSISession* _session = arg;
702
703 if ( _session && _session->call ) {
704
705 uint32_t* _peers = _session->call->peers;
706 uint16_t _peer_count = _session->call->peer_count;
707
708
709 /* Cancel all? */
710 uint16_t _it = 0;
711 for ( ; _it < _peer_count; _it++ )
712 msi_cancel ( arg, _peers[_it], (const uint8_t*)"Timeout" );
713
714 }
715
716 ( *callbacks[MSI_OnRequestTimeout] ) ( _session->agent_handler );
717 ( *callbacks[MSI_OnEnding ] ) ( _session->agent_handler );
718
719 return NULL;
720}
721
722
723/**
724 * @brief Add peer to peer list.
725 *
726 * @param call What call.
727 * @param peer_id Its id.
728 * @return void
729 */
730void add_peer( MSICall* call, int peer_id )
731{
732 if ( !call->peers ) {
733 call->peers = calloc(sizeof(int), 1);
734 call->peer_count = 1;
735 } else{
736 call->peer_count ++;
737 call->peers = realloc( call->peers, sizeof(int) * call->peer_count);
738 }
739
740 call->peers[call->peer_count - 1] = peer_id;
741}
742
743
744/**
745 * @brief Speaks for it self.
746 *
747 * @param session Control session.
748 * @param peers Amount of peers. (Currently it only supports 1)
749 * @param ringing_timeout Ringing timeout.
750 * @return MSICall* The created call.
751 */
752MSICall* init_call ( MSISession* session, int peers, int ringing_timeout ) {
753 assert ( session );
754 assert ( peers );
755
756 MSICall* _call = calloc ( sizeof ( MSICall ), 1 );
757 _call->type_peer = calloc ( sizeof ( MSICallType ), peers );
758
759 assert ( _call );
760 assert ( _call->type_peer );
761
762 /*_call->_participant_count = _peers;*/
763
764 _call->request_timer_id = 0;
765 _call->ringing_timer_id = 0;
766
767 _call->key_local = NULL;
768 _call->key_peer = NULL;
769 _call->nonce_local = NULL;
770 _call->nonce_peer = NULL;
771
772 _call->ringing_tout_ms = ringing_timeout;
773
774 pthread_mutex_init ( &_call->mutex, NULL );
775
776 return _call;
777}
778
779
780/**
781 * @brief Terminate the call.
782 *
783 * @param session Control session.
784 * @return int
785 * @retval -1 Error occured.
786 * @retval 0 Success.
787 */
788int terminate_call ( MSISession* session ) {
789 assert ( session );
790
791 if ( !session->call )
792 return -1;
793
794
795 /* Check event loop and cancel timed events if there are any
796 * NOTE: This has to be done before possibly
797 * locking the mutex the second time
798 */
799 event.timer_release ( session->call->request_timer_id );
800 event.timer_release ( session->call->ringing_timer_id );
801
802 /* Get a handle */
803 pthread_mutex_lock ( &session->call->mutex );
804
805 MSICall* _call = session->call;
806 session->call = NULL;
807
808 free ( _call->type_peer );
809 free ( _call->key_local );
810 free ( _call->key_peer );
811 free ( _call->peers);
812
813 /* Release handle */
814 pthread_mutex_unlock ( &_call->mutex );
815
816 pthread_mutex_destroy ( &_call->mutex );
817
818 free ( _call );
819
820 return 0;
821}
822
823
824/********** Request handlers **********/
825int handle_recv_invite ( MSISession* session, MSIMessage* msg ) {
826 assert ( session );
827
828 if ( session->call ) {
829 handle_error ( session, error_busy, msg->friend_id );
830 return 0;
831 }
832 if ( !msg->callid.header_value ) {
833 handle_error ( session, error_no_callid, msg->friend_id );
834 return 0;
835 }
836
837 session->call = init_call ( session, 1, 0 );
838 memcpy ( session->call->id, msg->callid.header_value, CALL_ID_LEN );
839 session->call->state = call_starting;
840
841 add_peer( session->call, msg->friend_id);
842
843 flush_peer_type ( session, msg, 0 );
844
845 MSIMessage* _msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) );
846 send_message ( session, _msg_ringing, msg->friend_id );
847 free_message ( _msg_ringing );
848
849 event.rise ( callbacks[MSI_OnInvite], session->agent_handler );
850
851 return 1;
852}
853int handle_recv_start ( MSISession* session, MSIMessage* msg ) {
854 assert ( session );
855
856 if ( has_call_error ( session, msg ) == 0 )
857 return 0;
858
859 if ( !msg->cryptokey.header_value )
860 return handle_error ( session, error_no_crypto_key, msg->friend_id );
861
862 session->call->state = call_active;
863
864 session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES );
865 memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES );
866
867 session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
868 memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES );
869
870 flush_peer_type ( session, msg, 0 );
871
872 event.rise ( callbacks[MSI_OnStart], session->agent_handler );
873
874 return 1;
875}
876int handle_recv_reject ( MSISession* session, MSIMessage* msg ) {
877 assert ( session );
878
879 if ( has_call_error ( session, msg ) == 0 )
880 return 0;
881
882
883 MSIMessage* _msg_end = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
884 send_message ( session, _msg_end, msg->friend_id );
885 free_message ( _msg_end );
886
887 event.timer_release ( session->call->request_timer_id );
888 event.rise ( callbacks[MSI_OnReject], session->agent_handler );
889 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
890
891 return 1;
892}
893int handle_recv_cancel ( MSISession* session, MSIMessage* msg ) {
894 assert ( session );
895
896 if ( has_call_error ( session, msg ) == 0 )
897 return 0;
898
899
900 terminate_call ( session );
901
902 event.rise ( callbacks[MSI_OnCancel], session->agent_handler );
903
904 return 1;
905}
906int handle_recv_end ( MSISession* session, MSIMessage* msg ) {
907 assert ( session );
908
909 if ( has_call_error ( session, msg ) == 0 )
910 return 0;
911
912
913 MSIMessage* _msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
914 send_message ( session, _msg_ending, msg->friend_id );
915 free_message ( _msg_ending );
916
917 terminate_call ( session );
918
919 event.rise ( callbacks[MSI_OnEnd], session->agent_handler );
920
921 return 1;
922}
923
924/********** Response handlers **********/
925int handle_recv_ringing ( MSISession* session, MSIMessage* msg ) {
926 assert ( session );
927
928 if ( has_call_error ( session, msg ) == 0 )
929 return 0;
930
931 session->call->ringing_timer_id = event.timer_alloc ( handle_timeout, session, session->call->ringing_tout_ms );
932 event.rise ( callbacks[MSI_OnRinging], session->agent_handler );
933
934 return 1;
935}
936int handle_recv_starting ( MSISession* session, MSIMessage* msg ) {
937 assert ( session );
938
939 if ( has_call_error ( session, msg ) == 0 )
940 return 0;
941
942 if ( !msg->cryptokey.header_value ) {
943 return handle_error ( session, error_no_crypto_key, msg->friend_id );
944 }
945
946 /* Generate local key/nonce to send */
947 session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES );
948 new_symmetric_key ( session->call->key_local );
949
950 session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
951 new_nonce ( session->call->nonce_local );
952
953 /* Save peer key/nonce */
954 session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES );
955 memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES );
956
957 session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
958 memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES );
959
960 session->call->state = call_active;
961
962 MSIMessage* _msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) );
963 msi_msg_set_cryptokey ( _msg_start, session->call->key_local, crypto_secretbox_KEYBYTES );
964 msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_box_NONCEBYTES );
965 send_message ( session, _msg_start, msg->friend_id );
966 free_message ( _msg_start );
967
968 flush_peer_type ( session, msg, 0 );
969
970 event.rise ( callbacks[MSI_OnStarting], session->agent_handler );
971 event.timer_release ( session->call->ringing_timer_id );
972
973 return 1;
974}
975int handle_recv_ending ( MSISession* session, MSIMessage* msg ) {
976 assert ( session );
977
978 if ( has_call_error ( session, msg ) == 0 )
979 return 0;
980
981
982 terminate_call ( session );
983
984 event.rise ( callbacks[MSI_OnEnding], session->agent_handler );
985
986 return 1;
987}
988int handle_recv_error ( MSISession* session, MSIMessage* msg ) {
989 assert ( session );
990 assert ( session->call );
991
992 /* Handle error accordingly */
993 if ( msg->reason.header_value ) {
994 session->last_error_id = atoi ( ( const char* ) msg->reason.header_value );
995 session->last_error_str = stringify_error ( session->last_error_id );
996 }
997
998 terminate_call ( session );
999
1000 event.rise ( callbacks[MSI_OnEnding], session->agent_handler );
1001
1002 return 1;
1003}
1004
1005
1006/**
1007 * @brief BASIC call flow:
1008 *
1009 * ALICE BOB
1010 * | invite --> |
1011 * | |
1012 * | <-- ringing |
1013 * | |
1014 * | <-- starting |
1015 * | |
1016 * | start --> |
1017 * | |
1018 * | <-- MEDIA TRANS --> |
1019 * | |
1020 * | end --> |
1021 * | |
1022 * | <-- ending |
1023 *
1024 * Alice calls Bob by sending invite packet.
1025 * Bob recvs the packet and sends an ringing packet;
1026 * which notifies Alice that her invite is acknowledged.
1027 * Ringing screen shown on both sides.
1028 * Bob accepts the invite for a call by sending starting packet.
1029 * Alice recvs the starting packet and sends the started packet to
1030 * inform Bob that she recved the starting packet.
1031 * Now the media transmission is established ( i.e. RTP transmission ).
1032 * Alice hangs up and sends end packet.
1033 * Bob recves the end packet and sends ending packet
1034 * as the acknowledgement that the call is ending.
1035 *
1036 *
1037 */
1038void msi_handle_packet ( Messenger* messenger, int source, uint8_t* data, uint16_t length, void* object )
1039{
1040 /* Unused */
1041 (void)messenger;
1042
1043 MSISession* _session = object;
1044 MSIMessage* _msg;
1045
1046 if ( !length ) return;
1047
1048 _msg = parse_message ( data, length );
1049
1050 if ( !_msg ) return;
1051
1052 _msg->friend_id = source;
1053
1054
1055 /* Now handle message */
1056
1057 if ( _msg->request.header_value ) { /* Handle request */
1058
1059 const uint8_t* _request_value = _msg->request.header_value;
1060
1061 if ( same ( _request_value, stringify_request ( invite ) ) ) {
1062 handle_recv_invite ( _session, _msg );
1063
1064 } else if ( same ( _request_value, stringify_request ( start ) ) ) {
1065 handle_recv_start ( _session, _msg );
1066
1067 } else if ( same ( _request_value, stringify_request ( cancel ) ) ) {
1068 handle_recv_cancel ( _session, _msg );
1069
1070 } else if ( same ( _request_value, stringify_request ( reject ) ) ) {
1071 handle_recv_reject ( _session, _msg );
1072
1073 } else if ( same ( _request_value, stringify_request ( end ) ) ) {
1074 handle_recv_end ( _session, _msg );
1075 }
1076
1077 else {
1078 free_message ( _msg );
1079 return;
1080 }
1081
1082 } else if ( _msg->response.header_value ) { /* Handle response */
1083
1084 const uint8_t* _response_value = _msg->response.header_value;
1085
1086 if ( same ( _response_value, stringify_response ( ringing ) ) ) {
1087 handle_recv_ringing ( _session, _msg );
1088
1089 } else if ( same ( _response_value, stringify_response ( starting ) ) ) {
1090 handle_recv_starting ( _session, _msg );
1091
1092 } else if ( same ( _response_value, stringify_response ( ending ) ) ) {
1093 handle_recv_ending ( _session, _msg );
1094
1095 } else if ( same ( _response_value, stringify_response ( error ) ) ) {
1096 handle_recv_error ( _session, _msg );
1097 } else {
1098 free_message ( _msg );
1099 return;
1100 }
1101
1102 /* Got response so cancel timer */
1103 if ( _session->call )
1104 event.timer_release ( _session->call->request_timer_id );
1105
1106 }
1107
1108 free_message ( _msg );
1109}
1110
1111
1112/********************************************************************************************************************
1113 * *******************************************************************************************************************
1114 ********************************************************************************************************************
1115 ********************************************************************************************************************
1116 ********************************************************************************************************************
1117 *
1118 *
1119 *
1120 * PUBLIC API FUNCTIONS IMPLEMENTATIONS
1121 *
1122 *
1123 *
1124 ********************************************************************************************************************
1125 ********************************************************************************************************************
1126 ********************************************************************************************************************
1127 ********************************************************************************************************************
1128 ********************************************************************************************************************/
1129
1130
1131
1132
1133
1134
1135
1136
1137/**
1138 * @brief Callback setter.
1139 *
1140 * @param callback The callback.
1141 * @param id The id.
1142 * @return void
1143 */
1144void msi_register_callback ( MSICallback callback, MSICallbackID id )
1145{
1146 callbacks[id] = callback;
1147}
1148
1149
1150/**
1151 * @brief Start the control session.
1152 *
1153 * @param messenger Tox* object.
1154 * @param user_agent User agent, i.e. 'Venom'; 'QT-gui'
1155 * @return MSISession* The created session.
1156 * @retval NULL Error occured.
1157 */
1158MSISession* msi_init_session ( Messenger* messenger, const uint8_t* ua_name ) {
1159 assert ( messenger );
1160
1161 MSISession* _retu = calloc ( sizeof ( MSISession ), 1 );
1162 assert ( _retu );
1163
1164 _retu->ua_name = ua_name;
1165 _retu->messenger_handle = messenger;
1166 _retu->agent_handler = NULL;
1167
1168 _retu->call = NULL;
1169
1170 _retu->frequ = 10000; /* default value? */
1171 _retu->call_timeout = 30000; /* default value? */
1172
1173
1174 m_callback_msi_packet(messenger, msi_handle_packet, _retu );
1175
1176 /* This is called when remote terminates session */
1177 m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, _retu);
1178
1179 return _retu;
1180}
1181
1182
1183/**
1184 * @brief Terminate control session.
1185 *
1186 * @param session The session
1187 * @return int
1188 */
1189int msi_terminate_session ( MSISession* session ) {
1190 assert ( session );
1191
1192 int _status = 0;
1193
1194 terminate_call ( session );
1195 m_callback_msi_packet((struct Messenger*) session->messenger_handle, NULL, NULL);
1196
1197
1198 /* TODO: Clean it up more? */
1199
1200 free ( session );
1201 return _status;
1202}
1203
1204
1205/**
1206 * @brief Send invite request to friend_id.
1207 *
1208 * @param session Control session.
1209 * @param call_type Type of the call. Audio or Video(both audio and video)
1210 * @param rngsec Ringing timeout.
1211 * @param friend_id The friend.
1212 * @return int
1213 */
1214int msi_invite ( MSISession* session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ) {
1215 assert ( session );
1216
1217 MSIMessage* _msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) );
1218
1219 session->call = init_call ( session, 1, rngsec ); /* Just one for now */
1220 t_randomstr ( session->call->id, CALL_ID_LEN );
1221
1222 add_peer(session->call, friend_id );
1223
1224 session->call->type_local = call_type;
1225 /* Do whatever with message */
1226
1227 if ( call_type == type_audio ) {
1228 msi_msg_set_calltype
1229 ( _msg_invite, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
1230 } else {
1231 msi_msg_set_calltype
1232 ( _msg_invite, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
1233 }
1234
1235 send_message ( session, _msg_invite, friend_id );
1236 free_message ( _msg_invite );
1237
1238 session->call->state = call_inviting;
1239
1240 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
1241
1242 return 0;
1243}
1244
1245
1246/**
1247 * @brief Hangup active call.
1248 *
1249 * @param session Control session.
1250 * @return int
1251 * @retval -1 Error occured.
1252 * @retval 0 Success.
1253 */
1254int msi_hangup ( MSISession* session ) {
1255 assert ( session );
1256
1257 if ( !session->call || session->call->state != call_active )
1258 return -1;
1259
1260 MSIMessage* _msg_ending = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) );
1261
1262 /* hangup for each peer */
1263 int _it = 0;
1264 for ( ; _it < session->call->peer_count; _it ++ )
1265 send_message ( session, _msg_ending, session->call->peers[_it] );
1266
1267
1268 free_message ( _msg_ending );
1269
1270 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
1271
1272 return 0;
1273}
1274
1275
1276/**
1277 * @brief Answer active call request.
1278 *
1279 * @param session Control session.
1280 * @param call_type Answer with Audio or Video(both).
1281 * @return int
1282 */
1283int msi_answer ( MSISession* session, MSICallType call_type ) {
1284 assert ( session );
1285
1286 MSIMessage* _msg_starting = msi_new_message ( TYPE_RESPONSE, stringify_response ( starting ) );
1287 session->call->type_local = call_type;
1288
1289 if ( call_type == type_audio ) {
1290 msi_msg_set_calltype
1291 ( _msg_starting, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) );
1292 } else {
1293 msi_msg_set_calltype
1294 ( _msg_starting, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) );
1295 }
1296
1297 /* Now set the local encryption key and pass it with STARTING message */
1298
1299 session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES );
1300 new_symmetric_key ( session->call->key_local );
1301
1302 session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES );
1303 new_nonce ( session->call->nonce_local );
1304
1305 msi_msg_set_cryptokey ( _msg_starting, session->call->key_local, crypto_secretbox_KEYBYTES );
1306 msi_msg_set_nonce ( _msg_starting, session->call->nonce_local, crypto_box_NONCEBYTES );
1307
1308 send_message ( session, _msg_starting, session->call->peers[session->call->peer_count - 1] );
1309 free_message ( _msg_starting );
1310
1311 session->call->state = call_active;
1312
1313 return 0;
1314}
1315
1316
1317/**
1318 * @brief Cancel request.
1319 *
1320 * @param session Control session.
1321 * @param reason Set optional reason header. Pass NULL if none.
1322 * @return int
1323 */
1324int msi_cancel ( MSISession* session, uint32_t peer, const uint8_t* reason ) {
1325 assert ( session );
1326
1327 MSIMessage* _msg_cancel = msi_new_message ( TYPE_REQUEST, stringify_request ( cancel ) );
1328
1329 if ( reason ) msi_msg_set_reason(_msg_cancel, reason, strlen((const char*)reason));
1330
1331 send_message ( session, _msg_cancel, peer );
1332 free_message ( _msg_cancel );
1333
1334 terminate_call ( session );
1335
1336 return 0;
1337}
1338
1339
1340/**
1341 * @brief Reject request.
1342 *
1343 * @param session Control session.
1344 * @return int
1345 */
1346int msi_reject ( MSISession* session, const uint8_t* reason ) {
1347 assert ( session );
1348
1349 MSIMessage* _msg_reject = msi_new_message ( TYPE_REQUEST, stringify_request ( reject ) );
1350
1351 if ( reason ) msi_msg_set_reason(_msg_reject, reason, strlen((const char*)reason) + 1);
1352
1353 send_message ( session, _msg_reject, session->call->peers[session->call->peer_count - 1] );
1354 free_message ( _msg_reject );
1355
1356 session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout );
1357
1358 return 0;
1359}
1360
1361
1362/**
1363 * @brief Terminate the current call.
1364 *
1365 * @param session Control session.
1366 * @return int
1367 */
1368int msi_stopcall ( MSISession* session ) {
1369 assert ( session );
1370
1371 if ( !session->call )
1372 return -1;
1373
1374 /* just terminate it */
1375
1376 terminate_call ( session );
1377
1378 return 0;
1379} \ No newline at end of file
diff --git a/toxav/msi.h b/toxav/msi.h
new file mode 100644
index 00000000..5b693da4
--- /dev/null
+++ b/toxav/msi.h
@@ -0,0 +1,233 @@
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 at #tox-dev @ freenode.net:6667
22 */
23
24#ifndef __TOXMSI
25#define __TOXMSI
26
27#include <inttypes.h>
28#include <pthread.h>
29
30#include "../toxcore/Messenger.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* ua_name;
107
108 void* agent_handler; /* Pointer to an object that is handling msi */
109 Messenger* 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 MSI_OnInvite,
124 MSI_OnStart,
125 MSI_OnCancel,
126 MSI_OnReject,
127 MSI_OnEnd,
128
129 /* Responses */
130 MSI_OnRinging,
131 MSI_OnStarting,
132 MSI_OnEnding,
133
134 /* Protocol */
135 MSI_OnError,
136 MSI_OnRequestTimeout
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 ( Messenger* messenger, const uint8_t* ua_name );
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 peer To which peer.
209 * @param reason Set optional reason header. Pass NULL if none.
210 * @return int
211 */
212int msi_cancel ( MSISession* session, uint32_t peer, const uint8_t* reason );
213
214
215/**
216 * @brief Reject request.
217 *
218 * @param session Control session.
219 * @param reason Set optional reason header. Pass NULL if none.
220 * @return int
221 */
222int msi_reject ( MSISession* session, const uint8_t* reason );
223
224
225/**
226 * @brief Terminate the current call.
227 *
228 * @param session Control session.
229 * @return int
230 */
231int msi_stopcall ( MSISession* session );
232
233#endif /* __TOXMSI */
diff --git a/toxav/phone.c b/toxav/phone.c
new file mode 100755
index 00000000..161275d9
--- /dev/null
+++ b/toxav/phone.c
@@ -0,0 +1,1308 @@
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
39#include <stdio.h>
40#include <string.h>
41#include <stdlib.h>
42#include <stdarg.h>
43#include <unistd.h>
44#include <assert.h>
45#include <math.h>
46#include <pthread.h>
47
48//#include "media.h"
49#include "toxav.h"
50#include "event.h"
51#include "../toxcore/tox.h"
52
53#ifdef TOX_FFMPEG
54/* Video encoding/decoding */
55#include <libavcodec/avcodec.h>
56#include <libavformat/avformat.h>
57#include <libswscale/swscale.h>
58#include <libavdevice/avdevice.h>
59#include <libavutil/opt.h>
60#endif
61
62#include <AL/al.h>
63#include <AL/alc.h>
64#include <SDL/SDL.h>
65#include <SDL/SDL_thread.h>
66
67/* the quit event for SDL */
68#define FF_QUIT_EVENT (SDL_USEREVENT + 2)
69
70#ifdef __linux__
71#define VIDEO_DRIVER "video4linux2"
72#define DEFAULT_WEBCAM "/dev/video0"
73#endif
74
75#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
76#define VIDEO_DRIVER "vfwcap"
77#define DEFAULT_WEBCAM "0"
78#endif
79
80
81/* Define client version */
82#define _USERAGENT "v.0.3.0"
83
84
85struct SDL_Surface *screen;
86
87typedef struct {
88 struct SDL_Overlay *bmp;
89 int width, height;
90} VideoPicture;
91
92
93typedef struct av_friend_s {
94 int _id;
95 int _active; /* 0=false; 1=true; */
96} av_friend_t;
97
98typedef struct av_session_s {
99 /* Encoding/decoding/capturing/playing */
100 ToxAv* av;
101
102 VideoPicture video_picture;
103 struct ALCdevice *audio_capture_device;
104
105 /* context for converting image format to something SDL can use*/
106 struct SwsContext *sws_SDL_r_ctx;
107
108 /* context for converting webcam image format to something the video encoder can use */
109 struct SwsContext *sws_ctx;
110
111 /* Thread running control */
112 int running_decaud, running_encaud,
113 running_decvid, running_encvid;
114
115 pthread_mutex_t _mutex;
116
117 Tox* _messenger;
118 av_friend_t* _friends;
119 int _friend_cout;
120 char _my_public_id[200];
121#ifdef TOX_FFMPEG
122 AVInputFormat *video_input_format;
123 AVFormatContext *video_format_ctx;
124 uint8_t video_stream;
125 AVCodecContext *webcam_decoder_ctx;
126 AVCodec *webcam_decoder;
127#endif
128} av_session_t;
129
130
131void av_allocate_friend(av_session_t* _phone, int _id, int _active)
132{
133 static int _new_id = 0;
134
135 if ( !_phone->_friends ) {
136 _phone->_friends = calloc(sizeof(av_friend_t), 1);
137 _phone->_friend_cout = 1;
138 } else{
139 _phone->_friend_cout ++;
140 _phone->_friends = realloc(_phone->_friends, sizeof(av_friend_t) * _phone->_friend_cout);
141 }
142
143 if ( _id == -1 ) {
144 _phone->_friends->_id = _new_id;
145 _new_id ++;
146 } else _phone->_friends->_id = _id;
147
148 _phone->_friends->_active = _active;
149}
150av_friend_t* av_get_friend(av_session_t* _phone, int _id)
151{
152 av_friend_t* _friends = _phone->_friends;
153
154 if ( !_friends ) return NULL;
155
156 int _it = 0;
157 for (; _it < _phone->_friend_cout; _it ++)
158 if ( _friends[_it]._id == _id )
159 return _friends + _it;
160
161 return NULL;
162}
163
164
165/***************** MISC *****************/
166
167void INFO (const char* _format, ...)
168{
169 printf("\r[!] ");
170 va_list _arg;
171 va_start (_arg, _format);
172 vfprintf (stdout, _format, _arg);
173 va_end (_arg);
174 printf("\n\r >> ");
175 fflush(stdout);
176}
177
178unsigned char *hex_string_to_bin(char hex_string[])
179{
180 size_t i, len = strlen(hex_string);
181 unsigned char *val = calloc(sizeof(unsigned char), len);
182 char *pos = hex_string;
183
184 for (i = 0; i < len; ++i, pos += 2)
185 sscanf(pos, "%2hhx", &val[i]);
186
187 return val;
188}
189
190int getinput( char* _buff, size_t _limit, int* _len )
191{
192 if ( fgets(_buff, _limit, stdin) == NULL )
193 return -1;
194
195 *_len = strlen(_buff) - 1;
196
197 /* Get rid of newline */
198 _buff[*_len] = '\0';
199
200 return 0;
201}
202
203char* trim_spaces ( char* buff )
204{
205
206 int _i = 0, _len = strlen(buff);
207
208 char* container = calloc(sizeof(char), _len);
209 int _ci = 0;
210
211 for ( ; _i < _len; _i++ ) {
212 while ( _i < _len && buff[_i] == ' ' )
213 _i++;
214
215 if ( _i < _len ){
216 container[_ci] = buff[_i];
217 _ci ++;
218 }
219 }
220
221 memcpy( buff, container, _ci );
222 buff[_ci] = '\0';
223 free(container);
224 return buff;
225}
226
227#define FRADDR_TOSTR_CHUNK_LEN 8
228
229static void fraddr_to_str(uint8_t *id_bin, char *id_str)
230{
231 uint i, delta = 0, pos_extra = 0, sum_extra = 0;
232
233 for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
234 sprintf(&id_str[2 * i + delta], "%02hhX", id_bin[i]);
235
236 if ((i + 1) == TOX_CLIENT_ID_SIZE)
237 pos_extra = 2 * (i + 1) + delta;
238
239 if (i >= TOX_CLIENT_ID_SIZE)
240 sum_extra |= id_bin[i];
241
242 if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) {
243 id_str[2 * (i + 1) + delta] = ' ';
244 delta++;
245 }
246 }
247
248 id_str[2 * i + delta] = 0;
249
250 if (!sum_extra)
251 id_str[pos_extra] = 0;
252}
253
254/*********************************************
255 *********************************************
256 *********************************************
257 *********************************************
258 *********************************************
259 *********************************************
260 *********************************************
261 *********************************************
262 */
263
264
265/*
266 * How av stuff _should_ look like
267 */
268/*
269int display_received_frame(av_session_t* _phone, vpx_image_t *image)
270{
271 CodecState* cs = get_cs_temp(_phone->av);
272 AVPicture pict;
273 SDL_LockYUVOverlay(_phone->video_picture.bmp);
274
275 pict.data[0] = _phone->video_picture.bmp->pixels[0];
276 pict.data[1] = _phone->video_picture.bmp->pixels[2];
277 pict.data[2] = _phone->video_picture.bmp->pixels[1];
278 pict.linesize[0] = _phone->video_picture.bmp->pitches[0];
279 pict.linesize[1] = _phone->video_picture.bmp->pitches[2];
280 pict.linesize[2] = _phone->video_picture.bmp->pitches[1];
281 */
282 /* Convert the image into YUV format that SDL uses *//*
283 sws_scale(_phone->sws_SDL_r_ctx, (uint8_t const * const *)r_video_frame->data, r_video_frame->linesize, 0,
284 cs->video_decoder_ctx->height, pict.data, pict.linesize );
285
286 SDL_UnlockYUVOverlay(_phone->video_picture.bmp);
287 SDL_Rect rect;
288 rect.x = 0;
289 rect.y = 0;
290 rect.w = cs->video_decoder_ctx->width;
291 rect.h = cs->video_decoder_ctx->height;
292 SDL_DisplayYUVOverlay(_phone->video_picture.bmp, &rect);
293 return 1;
294}
295*/
296#ifdef TOX_FFMPEG
297void *encode_video_thread(void *arg)
298{
299 INFO("Started encode video thread!");
300
301 av_session_t* _phone = arg;
302
303 _phone->running_encvid = 1;
304 //CodecState *cs = get_cs_temp(_phone->av);
305 AVPacket pkt1, *packet = &pkt1;
306 //int p = 0;
307 //int got_packet;
308 int video_frame_finished;
309 AVFrame *s_video_frame;
310 AVFrame *webcam_frame;
311 s_video_frame = avcodec_alloc_frame();
312 webcam_frame = avcodec_alloc_frame();
313 //AVPacket enc_video_packet;
314
315 uint8_t *buffer;
316 int numBytes;
317 /* Determine required buffer size and allocate buffer */
318 numBytes = avpicture_get_size(PIX_FMT_YUV420P, _phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height);
319 buffer = (uint8_t *)av_calloc(numBytes * sizeof(uint8_t),1);
320 avpicture_fill((AVPicture *)s_video_frame, buffer, PIX_FMT_YUV420P, _phone->webcam_decoder_ctx->width,
321 _phone->webcam_decoder_ctx->height);
322 _phone->sws_ctx = sws_getContext(_phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height,
323 _phone->webcam_decoder_ctx->pix_fmt, _phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height, PIX_FMT_YUV420P,
324 SWS_BILINEAR, NULL, NULL, NULL);
325
326
327 vpx_image_t *image =
328 vpx_img_alloc(NULL, VPX_IMG_FMT_I420, _phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height, 1);
329
330 //uint32_t frame_counter = 0;
331 while (_phone->running_encvid) {
332
333 if (av_read_frame(_phone->video_format_ctx, packet) < 0) {
334 printf("error reading frame\n");
335
336 if (_phone->video_format_ctx->pb->error != 0)
337 break;
338
339 continue;
340 }
341
342 if (packet->stream_index == _phone->video_stream) {
343 if (avcodec_decode_video2(_phone->webcam_decoder_ctx, webcam_frame, &video_frame_finished, packet) < 0) {
344 printf("couldn't decode\n");
345 continue;
346 }
347
348 av_free_packet(packet);
349 sws_scale(_phone->sws_ctx, (uint8_t const * const *)webcam_frame->data, webcam_frame->linesize, 0,
350 _phone->webcam_decoder_ctx->height, s_video_frame->data, s_video_frame->linesize);
351 /* create a new I-frame every 60 frames */
352 //++p;
353 /*
354 if (p == 60) {
355
356 s_video_frame->pict_type = AV_PICTURE_TYPE_BI ;
357 } else if (p == 61) {
358 s_video_frame->pict_type = AV_PICTURE_TYPE_I ;
359 p = 0;
360 } else {
361 s_video_frame->pict_type = AV_PICTURE_TYPE_P ;
362 }*/
363
364 if (video_frame_finished) {
365 memcpy(image->planes[VPX_PLANE_Y], s_video_frame->data[0], s_video_frame->linesize[0] * _phone->webcam_decoder_ctx->height);
366 memcpy(image->planes[VPX_PLANE_U], s_video_frame->data[1], s_video_frame->linesize[1] * _phone->webcam_decoder_ctx->height / 2);
367 memcpy(image->planes[VPX_PLANE_V], s_video_frame->data[2], s_video_frame->linesize[2] * _phone->webcam_decoder_ctx->height / 2);
368 toxav_send_video (_phone->av, image);
369 //if (avcodec_encode_video2(cs->video_encoder_ctx, &enc_video_packet, s_video_frame, &got_packet) < 0) {
370 /*if (vpx_codec_encode(&cs->v_encoder, image, frame_counter, 1, 0, 0) != VPX_CODEC_OK) {
371 printf("could not encode video frame\n");
372 continue;
373 }
374 ++frame_counter;
375
376 vpx_codec_iter_t iter = NULL;
377 vpx_codec_cx_pkt_t *pkt;
378 while( (pkt = vpx_codec_get_cx_data(&cs->v_encoder, &iter)) ) {
379 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT)
380 toxav_send_rtp_payload(_phone->av, TypeVideo, pkt->data.frame.buf, pkt->data.frame.sz);
381 }*/
382 //if (!got_packet) {
383 // continue;
384 //}
385
386 //if (!enc_video_packet.data) fprintf(stderr, "video packet data is NULL\n");
387
388 //toxav_send_rtp_payload(_phone->av, TypeVideo, enc_video_packet.data, enc_video_packet.size);
389
390 //av_free_packet(&enc_video_packet);
391 }
392 } else {
393 av_free_packet(packet);
394 }
395 }
396
397 vpx_img_free(image);
398
399 /* clean up codecs */
400 //pthread_mutex_lock(&cs->ctrl_mutex);
401 av_free(buffer);
402 av_free(webcam_frame);
403 av_free(s_video_frame);
404 sws_freeContext(_phone->sws_ctx);
405 //avcodec_close(webcam_decoder_ctx);
406 //avcodec_close(cs->video_encoder_ctx);
407 //pthread_mutex_unlock(&cs->ctrl_mutex);
408
409 _phone->running_encvid = -1;
410
411 pthread_exit ( NULL );
412}
413#endif
414
415void *encode_audio_thread(void *arg)
416{
417 INFO("Started encode audio thread!");
418 av_session_t* _phone = arg;
419 _phone->running_encaud = 1;
420
421 int ret = 0;
422 int16_t frame[4096];
423 int frame_size = AUDIO_FRAME_SIZE;
424 ALint sample = 0;
425 alcCaptureStart((ALCdevice*)_phone->audio_capture_device);
426
427 while (_phone->running_encaud) {
428 alcGetIntegerv((ALCdevice*)_phone->audio_capture_device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample);
429
430 if (sample >= frame_size) {
431 alcCaptureSamples((ALCdevice*)_phone->audio_capture_device, frame, frame_size);
432
433 ret = toxav_send_audio(_phone->av, frame, frame_size);
434
435 if (ret < 0)
436 printf("Could not encode or send audio packet\n");
437
438 } else {
439 usleep(1000);
440 }
441 }
442
443 /* clean up codecs *
444 pthread_mutex_lock(&cs->ctrl_mutex);* /
445 alcCaptureStop((ALCdevice*)_phone->audio_capture_device);
446 alcCaptureCloseDevice((ALCdevice*)_phone->audio_capture_device);
447 / *pthread_mutex_unlock(&cs->ctrl_mutex);*/
448 _phone->running_encaud = -1;
449 pthread_exit ( NULL );
450}
451
452void convert_to_rgb(vpx_image_t *img, unsigned char *out)
453{
454 const int w = img->d_w;
455 const int w2 = w/2;
456 const int pstride = w*3;
457 const int h = img->d_h;
458 const int h2 = h/2;
459
460 const int strideY = img->stride[0];
461 const int strideU = img->stride[1];
462 const int strideV = img->stride[2];
463 int posy, posx;
464 for (posy = 0; posy < h2; posy++) {
465 unsigned char *dst = out + pstride * (posy * 2);
466 unsigned char *dst2 = out + pstride * (posy * 2 + 1);
467 const unsigned char *srcY = img->planes[0] + strideY * posy * 2;
468 const unsigned char *srcY2 = img->planes[0] + strideY * (posy * 2 + 1);
469 const unsigned char *srcU = img->planes[1] + strideU * posy;
470 const unsigned char *srcV = img->planes[2] + strideV * posy;
471
472 for (posx = 0; posx < w2; posx++) {
473 unsigned char Y,U,V;
474 short R,G,B;
475 short iR,iG,iB;
476
477 U = *(srcU++); V = *(srcV++);
478 iR = (351 * (V-128)) / 256;
479 iG = - (179 * (V-128)) / 256 - (86 * (U-128)) / 256;
480 iB = (444 * (U-128)) / 256;
481
482 Y = *(srcY++);
483 R = Y + iR ; G = Y + iG ; B = Y + iB ;
484 R = (R<0?0:(R>255?255:R)); G = (G<0?0:(G>255?255:G)); B = (B<0?0:(B>255?255:B));
485 *(dst++) = R; *(dst++) = G; *(dst++) = B;
486
487 Y = *(srcY2++);
488 R = Y + iR ; G = Y + iG ; B = Y + iB ;
489 R = (R<0?0:(R>255?255:R)); G = (G<0?0:(G>255?255:G)); B = (B<0?0:(B>255?255:B));
490 *(dst2++) = R; *(dst2++) = G; *(dst2++) = B;
491
492 Y = *(srcY++) ;
493 R = Y + iR ; G = Y + iG ; B = Y + iB ;
494 R = (R<0?0:(R>255?255:R)); G = (G<0?0:(G>255?255:G)); B = (B<0?0:(B>255?255:B));
495 *(dst++) = R; *(dst++) = G; *(dst++) = B;
496
497 Y = *(srcY2++);
498 R = Y + iR ; G = Y + iG ; B = Y + iB ;
499 R = (R<0?0:(R>255?255:R)); G = (G<0?0:(G>255?255:G)); B = (B<0?0:(B>255?255:B));
500 *(dst2++) = R; *(dst2++) = G; *(dst2++) = B;
501 }
502 }
503}
504
505#define mask32(BYTE) (*(uint32_t *)(uint8_t [4]){ [BYTE] = 0xff })
506
507void *decode_video_thread(void *arg)
508{
509 INFO("Started decode video thread!");
510 av_session_t* _phone = arg;
511 _phone->running_decvid = 1;
512
513 //CodecState *cs = get_cs_temp(_phone->av);
514 //cs->video_stream = 0;
515
516 //int recved_size;
517 //uint8_t dest[RTP_PAYLOAD_SIZE];
518
519 //int dec_frame_finished;
520 //AVFrame *r_video_frame;
521 //r_video_frame = avcodec_alloc_frame();
522 //AVPacket dec_video_packet;
523 //av_new_packet (&dec_video_packet, 65536);
524 int width = 0;
525 int height = 0;
526
527 while (_phone->running_decvid) {
528 //recved_size = toxav_recv_rtp_payload(_phone->av, TypeVideo, dest);
529 //if (recved_size) {
530 vpx_image_t *image;
531 if (toxav_recv_video(_phone->av, &image) == 0) {
532 //memcpy(dec_video_packet.data, dest, recved_size);
533 //dec_video_packet.size = recved_size;
534
535 //avcodec_decode_video2(cs->video_decoder_ctx, r_video_frame, &dec_frame_finished, &dec_video_packet);
536
537 //if (dec_frame_finished) {
538
539 /* Check if size has changed */
540 if (image->d_w != width || image->d_h != height) {
541
542 width = image->d_w;
543 height = image->d_h;
544
545 printf("w: %d h: %d \n", width, height);
546
547 screen = SDL_SetVideoMode(width, height, 0, 0);
548
549 //if (_phone->video_picture.bmp)
550 // SDL_FreeYUVOverlay(_phone->video_picture.bmp);
551
552 //_phone->video_picture.bmp = SDL_CreateYUVOverlay(width, height, SDL_YV12_OVERLAY, screen);
553 // _phone->sws_SDL_r_ctx = sws_getContext(width, height, cs->video_decoder_ctx->pix_fmt, width, height, PIX_FMT_YUV420P,
554 // SWS_BILINEAR, NULL, NULL, NULL);
555 }
556 uint8_t *rgb_image = malloc(width*height*3);
557 convert_to_rgb(image, rgb_image);
558 SDL_Surface* img_surface = SDL_CreateRGBSurfaceFrom(rgb_image, width, height, 24, width * 3, mask32(0), mask32(1), mask32(2), 0);
559 if(SDL_BlitSurface(img_surface, NULL, screen, NULL) == 0)
560 SDL_UpdateRect(screen, 0, 0, 0, 0);
561 /*
562 SDL_LockYUVOverlay(_phone->video_picture.bmp);
563 memcpy(_phone->video_picture.bmp->pixels[0], image->planes[VPX_PLANE_Y], _phone->video_picture.bmp->pitches[0] * height);
564 memcpy(_phone->video_picture.bmp->pixels[1], image->planes[VPX_PLANE_V], _phone->video_picture.bmp->pitches[1] * height / 2);
565 memcpy(_phone->video_picture.bmp->pixels[2], image->planes[VPX_PLANE_U], _phone->video_picture.bmp->pitches[2] * height / 2);
566
567 SDL_Rect rect;
568 rect.x = 0;
569 rect.y = 0;
570 rect.w = width;
571 rect.h = height;
572 SDL_DisplayYUVOverlay(_phone->video_picture.bmp, &rect);*/
573 free(rgb_image);
574 //display_received_frame(_phone, image);
575
576 } //else {
577 /* TODO: request the sender to create a new i-frame immediatly */
578 //printf("Bad video packet\n");
579 //}
580 //}
581
582 usleep(1000);
583 }
584
585 /* clean up codecs */
586 //av_free(r_video_frame);
587
588 //pthread_mutex_lock(&cs->ctrl_mutex);
589 //avcodec_close(cs->video_decoder_ctx);
590 //pthread_mutex_unlock(&cs->ctrl_mutex);
591
592 _phone->running_decvid = -1;
593
594 pthread_exit ( NULL );
595}
596
597void *decode_audio_thread(void *arg)
598{
599 INFO("Started decode audio thread!");
600 av_session_t* _phone = arg;
601 _phone->running_decaud = 1;
602
603 //int recved_size;
604 //uint8_t dest [RTP_PAYLOAD_SIZE];
605
606 int frame_size = AUDIO_FRAME_SIZE;
607 //int data_size;
608
609 ALCdevice *dev;
610 ALCcontext *ctx;
611 ALuint source, *buffers;
612 dev = alcOpenDevice(NULL);
613 ctx = alcCreateContext(dev, NULL);
614 alcMakeContextCurrent(ctx);
615 int openal_buffers = 5;
616
617 buffers = calloc(sizeof(ALuint) * openal_buffers,1);
618 alGenBuffers(openal_buffers, buffers);
619 alGenSources((ALuint)1, &source);
620 alSourcei(source, AL_LOOPING, AL_FALSE);
621
622 ALuint buffer;
623 ALint ready;
624
625 uint16_t zeros[frame_size];
626 memset(zeros, 0, frame_size);
627 int16_t PCM[frame_size];
628
629 int i;
630 for (i = 0; i < openal_buffers; ++i) {
631 alBufferData(buffers[i], AL_FORMAT_MONO16, zeros, frame_size, 48000);
632 }
633
634 alSourceQueueBuffers(source, openal_buffers, buffers);
635 alSourcePlay(source);
636
637 if (alGetError() != AL_NO_ERROR) {
638 fprintf(stderr, "Error starting audio\n");
639 goto ending;
640 }
641
642 int dec_frame_len = 0;
643
644 while (_phone->running_decaud) {
645
646 alGetSourcei(source, AL_BUFFERS_PROCESSED, &ready);
647 if (ready <= 0)
648 continue;
649
650 dec_frame_len = toxav_recv_audio(_phone->av, frame_size, PCM);
651
652 /* Play the packet */
653 if (dec_frame_len > 0) {
654 alSourceUnqueueBuffers(source, 1, &buffer);
655 alBufferData(buffer, AL_FORMAT_MONO16, PCM, dec_frame_len * 2 * 1, 48000);
656 int error = alGetError();
657
658 if (error != AL_NO_ERROR) {
659 fprintf(stderr, "Error setting buffer %d\n", error);
660 break;
661 }
662
663 alSourceQueueBuffers(source, 1, &buffer);
664
665 if (alGetError() != AL_NO_ERROR) {
666 fprintf(stderr, "Error: could not buffer audio\n");
667 break;
668 }
669
670 alGetSourcei(source, AL_SOURCE_STATE, &ready);
671
672 if (ready != AL_PLAYING) alSourcePlay(source);
673 }
674
675 usleep(1000);
676 }
677
678
679ending:
680 /* clean up codecs */
681 //pthread_mutex_lock(&cs->ctrl_mutex);
682 /*
683 alDeleteSources(1, &source);
684 alDeleteBuffers(openal_buffers, buffers);
685 alcMakeContextCurrent(NULL);
686 alcDestroyContext(ctx);
687 alcCloseDevice(dev);
688 */
689 //pthread_mutex_unlock(&cs->ctrl_mutex);
690
691 _phone->running_decaud = -1;
692
693 pthread_exit ( NULL );
694}
695
696
697
698
699
700int phone_startmedia_loop ( ToxAv* arg )
701{
702 if ( !arg ){
703 return -1;
704 }
705
706 toxav_prepare_transmission(arg);
707
708 /*
709 * Rise all threads
710 */
711#ifdef TOX_FFMPEG
712 /* Only checks for last peer */
713 if ( toxav_get_peer_transmission_type(arg, 0) == TypeVideo &&
714 0 > event.rise(encode_video_thread, toxav_get_agent_handler(arg)) )
715 {
716 INFO("Error while starting encode_video_thread()");
717 return -1;
718 }
719#endif
720 /* Always send audio */
721 if ( 0 > event.rise(encode_audio_thread, toxav_get_agent_handler(arg)) )
722 {
723 INFO("Error while starting encode_audio_thread()");
724 return -1;
725 }
726
727 /* Only checks for last peer */
728 if ( toxav_get_peer_transmission_type(arg, 0) == TypeVideo &&
729 0 > event.rise(decode_video_thread, toxav_get_agent_handler(arg)) )
730 {
731 INFO("Error while starting decode_video_thread()");
732 return -1;
733 }
734
735 if ( 0 > event.rise(decode_audio_thread, toxav_get_agent_handler(arg)) )
736 {
737 INFO("Error while starting decode_audio_thread()");
738 return -1;
739 }
740
741
742 return 0;
743}
744
745
746
747
748
749
750/*********************************************
751 *********************************************
752 *********************************************
753 *********************************************
754 *********************************************
755 *********************************************
756 *********************************************
757 *********************************************
758 */
759
760
761/* Some example callbacks */
762
763void* callback_recv_invite ( void* _arg )
764{
765 assert(_arg);
766
767 switch ( toxav_get_peer_transmission_type(_arg, 0) ){
768 case TypeAudio:
769 INFO( "Incoming audio call!");
770 break;
771 case TypeVideo:
772 INFO( "Incoming video call!");
773 break;
774 }
775
776 pthread_exit(NULL);
777}
778void* callback_recv_ringing ( void* _arg )
779{
780 INFO ( "Ringing!" );
781 pthread_exit(NULL);
782}
783void* callback_recv_starting ( void* _arg )
784{
785 if ( 0 != phone_startmedia_loop(_arg) ){
786 INFO("Starting call failed!");
787 } else {
788 INFO ("Call started! ( press h to hangup )");
789 }
790 pthread_exit(NULL);
791}
792void* callback_recv_ending ( void* _arg )
793{
794 av_session_t* _phone = toxav_get_agent_handler(_arg);
795
796 _phone->running_encaud = 0;
797 _phone->running_decaud = 0;
798 _phone->running_encvid = 0;
799 _phone->running_decvid = 0;
800
801 /* Wait until all threads are done */
802
803 while ( _phone->running_encaud != -1 ||
804 _phone->running_decaud != -1 ||
805 _phone->running_encvid != -1 ||
806 _phone->running_decvid != -1 )
807
808 usleep(10000000);
809
810 INFO ( "Call ended!" );
811 pthread_exit(NULL);
812}
813
814void* callback_recv_error ( void* _arg )
815{
816 /*MSISession* _session = _arg;
817
818 INFO( "Error: %s", _session->last_error_str ); */
819 pthread_exit(NULL);
820}
821
822void* callback_call_started ( void* _arg )
823{
824 if ( 0 != phone_startmedia_loop(_arg) ){
825 INFO("Starting call failed!");
826 } else {
827 INFO ("Call started! ( press h to hangup )");
828 }
829
830 pthread_exit(NULL);
831}
832void* callback_call_canceled ( void* _arg )
833{
834 INFO ( "Call canceled!" );
835 pthread_exit(NULL);
836}
837void* callback_call_rejected ( void* _arg )
838{
839 INFO ( "Call rejected!" );
840 pthread_exit(NULL);
841}
842void* callback_call_ended ( void* _arg )
843{
844 av_session_t* _phone = toxav_get_agent_handler(_arg);
845
846 _phone->running_encaud = 0;
847 _phone->running_decaud = 0;
848 _phone->running_encvid = 0;
849 _phone->running_decvid = 0;
850
851 /* Wait until all threads are done */
852
853 while ( _phone->running_encaud != -1 ||
854 _phone->running_decaud != -1 ||
855 _phone->running_encvid != -1 ||
856 _phone->running_decvid != -1 )
857
858 usleep(10000000);
859
860 toxav_kill_transmission(_phone->av);
861 INFO ( "Call ended!" );
862 pthread_exit(NULL);
863}
864
865void* callback_requ_timeout ( void* _arg )
866{
867 INFO( "No answer! " );
868 pthread_exit(NULL);
869}
870
871av_session_t* av_init_session()
872{
873 av_session_t* _retu = malloc(sizeof(av_session_t));
874
875 /* Initialize our mutex */
876 pthread_mutex_init ( &_retu->_mutex, NULL );
877
878 _retu->_messenger = tox_new(1);
879
880 if ( !_retu->_messenger ) {
881 fprintf ( stderr, "tox_new() failed!\n" );
882 return NULL;
883 }
884
885 _retu->_friends = NULL;
886
887
888 const ALchar *_device_list = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
889 int i = 0;
890 const ALchar *device_names[20];
891
892 if ( _device_list ) {
893 INFO("\nAvailable Capture Devices are:");
894
895 while (*_device_list ) {
896 device_names[i] = _device_list;
897 INFO("%d) %s", i, device_names[i]);
898 _device_list += strlen( _device_list ) + 1;
899 ++i;
900 }
901 }
902
903 INFO("Enter capture device number");
904
905 char dev[2]; char* left;
906 char* warned_ = fgets(dev, 2, stdin);
907 (void)warned_;
908 long selection = strtol(dev, &left, 10);
909
910 if ( *left ) {
911 printf("'%s' is not a number!", dev);
912 fflush(stdout);
913 exit(EXIT_FAILURE);
914 }
915 else {
916 INFO("Selected: %d ( %s )", selection, device_names[selection]);
917 }
918
919 _retu->audio_capture_device =
920 (struct ALCdevice*)alcCaptureOpenDevice(
921 device_names[selection], AUDIO_SAMPLE_RATE, AL_FORMAT_MONO16, AUDIO_FRAME_SIZE * 4);
922
923
924 if (alcGetError((ALCdevice*)_retu->audio_capture_device) != AL_NO_ERROR) {
925 printf("Could not start capture device! %d\n", alcGetError((ALCdevice*)_retu->audio_capture_device));
926 return 0;
927 }
928 uint16_t height = 0, width = 0;
929#ifdef TOX_FFMPEG
930 avdevice_register_all();
931 avcodec_register_all();
932 av_register_all();
933
934 _retu->video_input_format = av_find_input_format(VIDEO_DRIVER);
935 if (avformat_open_input(&_retu->video_format_ctx, DEFAULT_WEBCAM, _retu->video_input_format, NULL) != 0) {
936 fprintf(stderr, "Opening video_input_format failed!\n");
937 //return -1;
938 return NULL;
939 }
940
941 avformat_find_stream_info(_retu->video_format_ctx, NULL);
942 av_dump_format(_retu->video_format_ctx, 0, DEFAULT_WEBCAM, 0);
943
944 for (i = 0; i < _retu->video_format_ctx->nb_streams; ++i) {
945 if (_retu->video_format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
946 _retu->video_stream = i;
947 break;
948 }
949 }
950
951 _retu->webcam_decoder_ctx = _retu->video_format_ctx->streams[_retu->video_stream]->codec;
952 _retu->webcam_decoder = avcodec_find_decoder(_retu->webcam_decoder_ctx->codec_id);
953
954 if (_retu->webcam_decoder == NULL) {
955 fprintf(stderr, "Unsupported codec!\n");
956 //return -1;
957 return NULL;
958 }
959
960 if (_retu->webcam_decoder_ctx == NULL) {
961 fprintf(stderr, "Init webcam_decoder_ctx failed!\n");
962 //return -1;
963 return NULL;
964 }
965
966 if (avcodec_open2(_retu->webcam_decoder_ctx, _retu->webcam_decoder, NULL) < 0) {
967 fprintf(stderr, "Opening webcam decoder failed!\n");
968 //return -1;
969 return NULL;
970 }
971 width = _retu->webcam_decoder_ctx->width;
972 height = _retu->webcam_decoder_ctx->height;
973#endif
974 uint8_t _byte_address[TOX_FRIEND_ADDRESS_SIZE];
975 tox_get_address(_retu->_messenger, _byte_address );
976 fraddr_to_str( _byte_address, _retu->_my_public_id );
977
978
979 _retu->av = toxav_new(_retu->_messenger, _retu, _USERAGENT, width, height);
980
981 /* ------------------ */
982
983 toxav_register_callstate_callback(callback_call_started, OnStart);
984 toxav_register_callstate_callback(callback_call_canceled, OnCancel);
985 toxav_register_callstate_callback(callback_call_rejected, OnReject);
986 toxav_register_callstate_callback(callback_call_ended, OnEnd);
987 toxav_register_callstate_callback(callback_recv_invite, OnInvite);
988
989 toxav_register_callstate_callback(callback_recv_ringing, OnRinging);
990 toxav_register_callstate_callback(callback_recv_starting, OnStarting);
991 toxav_register_callstate_callback(callback_recv_ending, OnEnding);
992
993 toxav_register_callstate_callback(callback_recv_error, OnError);
994 toxav_register_callstate_callback(callback_requ_timeout, OnRequestTimeout);
995
996 /* ------------------ */
997
998 return _retu;
999}
1000
1001int av_terminate_session(av_session_t* _phone)
1002{
1003 toxav_hangup(_phone->av);
1004
1005 free(_phone->_friends);
1006 pthread_mutex_destroy ( &_phone->_mutex );
1007
1008 Tox* _p = _phone->_messenger;
1009 _phone->_messenger = NULL; usleep(100000); /* Wait for tox_poll to end */
1010
1011 tox_kill(_p);
1012 toxav_kill(_phone->av);
1013
1014 free(_phone);
1015
1016 printf("\r[i] Quit!\n");
1017 return 0;
1018}
1019
1020/****** AV HELPER FUNCTIONS ******/
1021
1022/* Auto accept friend request */
1023void av_friend_requ(uint8_t *_public_key, uint8_t *_data, uint16_t _length, void *_userdata)
1024{
1025 av_session_t* _phone = _userdata;
1026 av_allocate_friend (_phone, -1, 0);
1027
1028 INFO("Got friend request with message: %s", _data);
1029
1030 tox_add_friend_norequest(_phone->_messenger, _public_key);
1031
1032 INFO("Auto-accepted! Friend id: %d", _phone->_friends->_id );
1033}
1034
1035void av_friend_active(Tox *_messenger, int _friendnumber, uint8_t *_string, uint16_t _length, void *_userdata)
1036{
1037 av_session_t* _phone = _userdata;
1038 INFO("Friend no. %d is online", _friendnumber);
1039
1040 av_friend_t* _this_friend = av_get_friend(_phone, _friendnumber);
1041
1042 if ( !_this_friend ) {
1043 INFO("But it's not registered!");
1044 return;
1045 }
1046
1047 (*_this_friend)._active = 1;
1048}
1049
1050int av_add_friend(av_session_t* _phone, char* _friend_hash)
1051{
1052 trim_spaces(_friend_hash);
1053
1054 unsigned char *_bin_string = hex_string_to_bin(_friend_hash);
1055 int _number = tox_add_friend(_phone->_messenger, _bin_string, (uint8_t *)"Tox phone "_USERAGENT, sizeof("Tox phone "_USERAGENT));
1056 free(_bin_string);
1057
1058 if ( _number >= 0) {
1059 INFO("Added friend as %d", _number );
1060 av_allocate_friend(_phone, _number, 0);
1061 }
1062 else
1063 INFO("Unknown error %i", _number );
1064
1065 return _number;
1066}
1067
1068int av_connect_to_dht(av_session_t* _phone, char* _dht_key, const char* _dht_addr, unsigned short _dht_port)
1069{
1070 unsigned char *_binary_string = hex_string_to_bin(_dht_key);
1071
1072 uint16_t _port = htons(_dht_port);
1073
1074 int _if = tox_bootstrap_from_address(_phone->_messenger, _dht_addr, 1, _port, _binary_string );
1075
1076 free(_binary_string);
1077
1078 return _if ? 0 : -1;
1079}
1080
1081/*********************************/
1082
1083void do_phone ( av_session_t* _phone )
1084{
1085 INFO("Welcome to tox_phone version: " _USERAGENT "\n"
1086 "Usage: \n"
1087 "f [pubkey] (add friend)\n"
1088 "c [a/v] (type) [friend] (friend id) (calls friend if online)\n"
1089 "h (if call is active hang up)\n"
1090 "a [a/v] (answer incoming call: a - audio / v - audio + video (audio is default))\n"
1091 "r (reject incoming call)\n"
1092 "q (quit)\n"
1093 "================================================================================"
1094 );
1095
1096 while ( 1 )
1097 {
1098 char _line [ 1500 ];
1099 int _len;
1100
1101 if ( -1 == getinput(_line, 1500, &_len) ){
1102 printf(" >> ");
1103 fflush(stdout);
1104 continue;
1105 }
1106
1107 if ( _len > 1 && _line[1] != ' ' && _line[1] != '\n' ){
1108 INFO("Invalid input!");
1109 continue;
1110 }
1111
1112 switch (_line[0]){
1113
1114 case 'f':
1115 {
1116 char _id [128];
1117 strncpy(_id, _line + 2, 128);
1118
1119 av_add_friend(_phone, _id);
1120
1121 } break;
1122 case 'c':
1123 {
1124 ToxAvCallType _ctype;
1125
1126 if ( _len < 5 ){
1127 INFO("Invalid input; usage: c a/v [friend]");
1128 break;
1129 }
1130 else if ( _line[2] == 'a' || _line[2] != 'v' ){ /* default and audio */
1131 _ctype = TypeAudio;
1132 }
1133 else { /* video */
1134 _ctype = TypeVideo;
1135 }
1136
1137 char* _end;
1138 int _friend = strtol(_line + 4, &_end, 10);
1139
1140 if ( *_end ){
1141 INFO("Friend num has to be numerical value");
1142 break;
1143 }
1144
1145 if ( toxav_call(_phone->av, _friend, _ctype, 30) == ErrorAlreadyInCall ){
1146 INFO("Already in a call");
1147 break;
1148 }
1149 else INFO("Calling friend: %d!", _friend);
1150
1151 } break;
1152 case 'h':
1153 {
1154 if ( toxav_hangup(_phone->av) == ErrorNoCall ) {
1155 INFO("No call!");
1156 break;
1157 }
1158 else INFO("Hung up...");
1159
1160 } break;
1161 case 'a':
1162 {
1163 ToxAvError rc;
1164
1165 if ( _len > 1 && _line[2] == 'v' ) {
1166 rc = toxav_answer(_phone->av, TypeVideo);
1167 } else
1168 rc = toxav_answer(_phone->av, TypeAudio);
1169
1170 if ( rc == ErrorInvalidState ) {
1171 INFO("No call to answer!");
1172 }
1173
1174 } break;
1175 case 'r':
1176 {
1177 if ( toxav_reject(_phone->av, "User action") == ErrorInvalidState )
1178 INFO("No state to cancel!");
1179 else INFO("Call Rejected...");
1180
1181 } break;
1182 case 'q':
1183 {
1184 INFO("Quitting!");
1185 return;
1186 }
1187 case '\n':
1188 {
1189 }
1190 default:
1191 {
1192 } break;
1193
1194 }
1195
1196 }
1197}
1198
1199void* tox_poll (void* _messenger_p)
1200{
1201 Tox** _messenger = _messenger_p;
1202 while( *_messenger ) {
1203 tox_do(*_messenger);
1204 usleep(10000);
1205 }
1206
1207 pthread_exit(NULL);
1208}
1209
1210int av_wait_dht(av_session_t* _phone, int _wait_seconds, const char* _ip, char* _key, unsigned short _port)
1211{
1212 if ( !_wait_seconds )
1213 return -1;
1214
1215 int _waited = 0;
1216
1217 while( !tox_isconnected(_phone->_messenger) ) {
1218
1219 if ( -1 == av_connect_to_dht(_phone, _key, _ip, _port) )
1220 {
1221 INFO("Could not connect to: %s", _ip);
1222 av_terminate_session(_phone);
1223 return -1;
1224 }
1225
1226 if ( _waited >= _wait_seconds ) return 0;
1227
1228 printf(".");
1229 fflush(stdout);
1230
1231 _waited ++;
1232 usleep(1000000);
1233 }
1234
1235 int _r = _wait_seconds - _waited;
1236 return _r ? _r : 1;
1237}
1238/* ---------------------- */
1239
1240int print_help ( const char* _name )
1241{
1242 printf ( "Usage: %s [IP] [PORT] [KEY]\n"
1243 "\t[IP] (DHT ip)\n"
1244 "\t[PORT] (DHT port)\n"
1245 "\t[KEY] (DHT public key)\n"
1246 "P.S. Friends and key are stored in ./tox_phone.conf\n"
1247 ,_name );
1248 return 1;
1249}
1250
1251int main ( int argc, char* argv [] )
1252{
1253 if ( argc < 1 || argc < 4 )
1254 return print_help(argv[0]);
1255
1256 char* _convertable;
1257
1258
1259 const char* _ip = argv[1];
1260 char* _key = argv[3];
1261 unsigned short _port = strtol(argv[2], &_convertable, 10);
1262
1263 if ( *_convertable ) {
1264 printf("Invalid port: cannot convert string to long: %s", _convertable);
1265 return 1;
1266 }
1267
1268 av_session_t* _phone = av_init_session();
1269
1270 tox_callback_friend_request(_phone->_messenger, av_friend_requ, _phone);
1271 tox_callback_status_message(_phone->_messenger, av_friend_active, _phone);
1272
1273
1274 INFO("\r================================================================================\n"
1275 "[!] Trying dht@%s:%d"
1276 , _ip, _port);
1277
1278 /* Start tox protocol */
1279 event.rise( tox_poll, &_phone->_messenger );
1280
1281 /* Just clean one line */
1282 printf("\r \r");
1283 fflush(stdout);
1284
1285 int _r;
1286 int _wait_seconds = 5;
1287 for ( _r = 0; _r == 0; _r = av_wait_dht(_phone, _wait_seconds, _ip, _key, _port) ) _wait_seconds --;
1288
1289
1290 if ( -1 == _r ) {
1291 INFO("Error while connecting to dht: %s:%d", _ip, _port);
1292 av_terminate_session(_phone);
1293 return 1;
1294 }
1295
1296 INFO("CONNECTED!\n"
1297 "================================================================================\n"
1298 "%s\n"
1299 "================================================================================"
1300 , _phone->_my_public_id );
1301
1302
1303 do_phone (_phone);
1304
1305 av_terminate_session(_phone);
1306
1307 return 0;
1308}
diff --git a/toxav/rtp.c b/toxav/rtp.c
new file mode 100644
index 00000000..2543c509
--- /dev/null
+++ b/toxav/rtp.c
@@ -0,0 +1,908 @@
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 at #tox-dev @ freenode.net:6667
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif /* HAVE_CONFIG_H */
27
28#include "rtp.h"
29#include <assert.h>
30#include <stdlib.h>
31
32
33#define PAYLOAD_ID_VALUE_OPUS 1
34#define PAYLOAD_ID_VALUE_VP8 2
35
36#define size_32 4
37
38#define inline__ inline __attribute__((always_inline))
39
40
41#define ADD_FLAG_VERSION(_h, _v) do { ( _h->flags ) &= 0x3F; ( _h->flags ) |= ( ( ( _v ) << 6 ) & 0xC0 ); } while(0)
42#define ADD_FLAG_PADDING(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xDF; ( _h->flags ) |= ( ( ( _v ) << 5 ) & 0x20 ); } while(0)
43#define ADD_FLAG_EXTENSION(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xEF;( _h->flags ) |= ( ( ( _v ) << 4 ) & 0x10 ); } while(0)
44#define ADD_FLAG_CSRCC(_h, _v) do { ( _h->flags ) &= 0xF0; ( _h->flags ) |= ( ( _v ) & 0x0F ); } while(0)
45#define ADD_SETTING_MARKER(_h, _v) do { if ( _v > 1 ) _v = 1; ( _h->marker_payloadt ) &= 0x7F; ( _h->marker_payloadt ) |= ( ( ( _v ) << 7 ) /*& 0x80 */ ); } while(0)
46#define ADD_SETTING_PAYLOAD(_h, _v) do { if ( _v > 127 ) _v = 127; ( _h->marker_payloadt ) &= 0x80; ( _h->marker_payloadt ) |= ( ( _v ) /* & 0x7F */ ); } while(0)
47
48#define GET_FLAG_VERSION(_h) (( _h->flags & 0xd0 ) >> 6)
49#define GET_FLAG_PADDING(_h) (( _h->flags & 0x20 ) >> 5)
50#define GET_FLAG_EXTENSION(_h) (( _h->flags & 0x10 ) >> 4)
51#define GET_FLAG_CSRCC(_h) ( _h->flags & 0x0f )
52#define GET_SETTING_MARKER(_h) (( _h->marker_payloadt ) >> 7)
53#define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f)
54
55
56/**
57 * @brief Converts 4 bytes to uint32_t
58 *
59 * @param dest Where to convert
60 * @param bytes What bytes
61 * @return void
62 */
63inline__ void bytes_to_U32(uint32_t* dest, const uint8_t* bytes)
64{
65 *dest =
66#ifdef WORDS_BIGENDIAN
67 ( ( uint32_t ) * bytes ) |
68 ( ( uint32_t ) * ( bytes + 1 ) << 8 ) |
69 ( ( uint32_t ) * ( bytes + 2 ) << 16 ) |
70 ( ( uint32_t ) * ( bytes + 3 ) << 24 ) ;
71#else
72 ( ( uint32_t ) * bytes << 24 ) |
73 ( ( uint32_t ) * ( bytes + 1 ) << 16 ) |
74 ( ( uint32_t ) * ( bytes + 2 ) << 8 ) |
75 ( ( uint32_t ) * ( bytes + 3 ) ) ;
76#endif
77}
78
79/**
80 * @brief Converts 2 bytes to uint16_t
81 *
82 * @param dest Where to convert
83 * @param bytes What bytes
84 * @return void
85 */
86inline__ void bytes_to_U16(uint16_t* dest, const uint8_t* bytes)
87{
88 *dest =
89#ifdef WORDS_BIGENDIAN
90 ( ( uint16_t ) * bytes ) |
91 ( ( uint16_t ) * ( bytes + 1 ) << 8 );
92#else
93 ( ( uint16_t ) * bytes << 8 ) |
94 ( ( uint16_t ) * ( bytes + 1 ) );
95#endif
96}
97
98/**
99 * @brief Convert uint32_t to byte string of size 4
100 *
101 * @param dest Where to convert
102 * @param value The value
103 * @return void
104 */
105inline__ void U32_to_bytes(uint8_t* dest, uint32_t value)
106{
107#ifdef WORDS_BIGENDIAN
108 *(dest) = ( value );
109 *(dest + 1) = ( value >> 8 );
110 *(dest + 2) = ( value >> 16 );
111 *(dest + 3) = ( value >> 24 );
112#else
113 *(dest) = ( value >> 24 );
114 *(dest + 1) = ( value >> 16 );
115 *(dest + 2) = ( value >> 8 );
116 *(dest + 3) = ( value );
117#endif
118}
119
120/**
121 * @brief Convert uint16_t to byte string of size 2
122 *
123 * @param dest Where to convert
124 * @param value The value
125 * @return void
126 */
127inline__ void U16_to_bytes(uint8_t* dest, uint16_t value)
128{
129#ifdef WORDS_BIGENDIAN
130 *(dest) = ( value );
131 *(dest + 1) = ( value >> 8 );
132#else
133 *(dest) = ( value >> 8 );
134 *(dest + 1) = ( value );
135#endif
136}
137
138
139/**
140 * @brief Checks if message came in late.
141 *
142 * @param session Control session.
143 * @param msg The message.
144 * @return int
145 * @retval -1 The message came in order.
146 * @retval 0 The message came late.
147 */
148inline__ int check_late_message (RTPSession* session, RTPMessage* msg)
149{
150 /*
151 * Check Sequence number. If this new msg has lesser number then the session->rsequnum
152 * it shows that the message came in late. Also check timestamp to be 100% certain.
153 *
154 */
155 return ( msg->header->sequnum < session->rsequnum && msg->header->timestamp < session->timestamp ) ? 0 : -1;
156}
157
158
159/**
160 * @brief Increases nonce value by 'target'
161 *
162 * @param nonce The nonce
163 * @param target The target
164 * @return void
165 */
166inline__ void increase_nonce(uint8_t* nonce, uint16_t target)
167{
168 uint16_t _nonce_counter;
169
170 uint8_t _reverse_bytes[2];
171 _reverse_bytes[0] = nonce[crypto_box_NONCEBYTES - 1];
172 _reverse_bytes[1] = nonce[crypto_box_NONCEBYTES - 2];
173
174 bytes_to_U16(&_nonce_counter, _reverse_bytes );
175
176 /* Check overflow */
177 if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */
178 uint8_t _it = 3;
179 while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ? crypto_box_NONCEBYTES : 1;
180
181 _nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */
182 } else { /* Increase nonce */
183
184 _nonce_counter+= target;
185 }
186
187 /* Assign the last bytes */
188
189 U16_to_bytes( _reverse_bytes, _nonce_counter);
190 nonce [crypto_box_NONCEBYTES - 1] = _reverse_bytes[0];
191 nonce [crypto_box_NONCEBYTES - 2] = _reverse_bytes[1];
192
193}
194
195
196/**
197 * @brief Speaks for it self.
198 *
199 */
200static const uint32_t payload_table[] =
201{
202 8000, 8000, 8000, 8000, 8000, 8000, 16000, 8000, 8000, 8000, /* 0-9 */
203 44100, 44100, 0, 0, 90000, 8000, 11025, 22050, 0, 0, /* 10-19 */
204 0, 0, 0, 0, 0, 90000, 90000, 0, 90000, 0, /* 20-29 */
205 0, 90000, 90000, 90000, 90000, 0, 0, 0, 0, 0, /* 30-39 */
206 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40-49 */
207 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50-59 */
208 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60-69 */
209 PAYLOAD_ID_VALUE_OPUS, PAYLOAD_ID_VALUE_VP8, 0, 0, 0, 0, 0, 0, 0, 0,/* 70-79 */
210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-89 */
211 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90-99 */
212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100-109 */
213 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110-119 */
214 0, 0, 0, 0, 0, 0, 0, 0 /* 120-127 */
215};
216
217
218/**
219 * @brief Extracts header from payload.
220 *
221 * @param payload The payload.
222 * @param length The size of payload.
223 * @return RTPHeader* Extracted header.
224 * @retval NULL Error occurred while extracting header.
225 */
226RTPHeader* extract_header ( const uint8_t* payload, int length )
227{
228 if ( !payload || !length ) {
229 return NULL;
230 }
231
232 const uint8_t* _it = payload;
233
234 RTPHeader* _retu = calloc(1, sizeof (RTPHeader));
235 assert(_retu);
236
237 _retu->flags = *_it; ++_it;
238
239 /* This indicates if the first 2 bits are valid.
240 * Now it may happen that this is out of order but
241 * it cuts down chances of parsing some invalid value
242 */
243
244 if ( GET_FLAG_VERSION(_retu) != RTP_VERSION ){
245 /* Deallocate */
246 free(_retu);
247 return NULL;
248 }
249
250 /*
251 * Added a check for the size of the header little sooner so
252 * I don't need to parse the other stuff if it's bad
253 */
254 uint8_t _cc = GET_FLAG_CSRCC ( _retu );
255 uint32_t _length = 12 /* Minimum header len */ + ( _cc * 4 );
256
257 if ( length < _length ) {
258 /* Deallocate */
259 free(_retu);
260 return NULL;
261 }
262
263 if ( _cc > 0 ) {
264 _retu->csrc = calloc (_cc, sizeof (uint32_t));
265 assert(_retu->csrc);
266
267 } else { /* But this should not happen ever */
268 /* Deallocate */
269 free(_retu);
270 return NULL;
271 }
272
273
274 _retu->marker_payloadt = *_it; ++_it;
275 _retu->length = _length;
276
277
278 bytes_to_U32(&_retu->timestamp, _it); _it += 4;
279 bytes_to_U32(&_retu->ssrc, _it);
280
281 uint8_t _x;
282 for ( _x = 0; _x < _cc; _x++ ) {
283 _it += 4; bytes_to_U32(&(_retu->csrc[_x]), _it);
284 }
285
286 return _retu;
287}
288
289/**
290 * @brief Extracts external header from payload. Must be called AFTER extract_header()!
291 *
292 * @param payload The ITERATED payload.
293 * @param length The size of payload.
294 * @return RTPExtHeader* Extracted extension header.
295 * @retval NULL Error occurred while extracting extension header.
296 */
297RTPExtHeader* extract_ext_header ( const uint8_t* payload, uint16_t length )
298{
299 const uint8_t* _it = payload;
300
301 RTPExtHeader* _retu = calloc(1, sizeof (RTPExtHeader));
302 assert(_retu);
303
304 uint16_t _ext_length;
305 bytes_to_U16(&_ext_length, _it); _it += 2;
306
307
308 if ( length < ( _ext_length * sizeof(uint32_t) ) ) {
309 free(_retu);
310 return NULL;
311 }
312
313 _retu->length = _ext_length;
314 bytes_to_U16(&_retu->type, _it); _it += 2;
315
316 _retu->table = calloc(_ext_length, sizeof (uint32_t));
317 assert(_retu->table);
318
319 uint16_t _x;
320 for ( _x = 0; _x < _ext_length; _x++ ) {
321 _it += 4; bytes_to_U32(&(_retu->table[_x]), _it);
322 }
323
324 return _retu;
325}
326
327/**
328 * @brief Adds header to payload. Make sure _payload_ has enough space.
329 *
330 * @param header The header.
331 * @param payload The payload.
332 * @return uint8_t* Iterated position.
333 */
334uint8_t* add_header ( RTPHeader* header, uint8_t* payload )
335{
336 uint8_t _cc = GET_FLAG_CSRCC ( header );
337
338 uint8_t* _it = payload;
339
340
341 /* Add sequence number first */
342 U16_to_bytes(_it, header->sequnum); _it += 2;
343
344 *_it = header->flags; ++_it;
345 *_it = header->marker_payloadt; ++_it;
346
347
348 U32_to_bytes( _it, header->timestamp); _it+=4;
349 U32_to_bytes( _it, header->ssrc);
350
351 if ( header->csrc ) {
352 uint8_t _x;
353 for ( _x = 0; _x < _cc; _x++ ) {
354 _it+=4; U32_to_bytes( _it, header->csrc[_x]);
355 }
356 }
357
358 return _it + 4;
359}
360
361/**
362 * @brief Adds extension header to payload. Make sure _payload_ has enough space.
363 *
364 * @param header The header.
365 * @param payload The payload.
366 * @return uint8_t* Iterated position.
367 */
368uint8_t* add_ext_header ( RTPExtHeader* header, uint8_t* payload )
369{
370 uint8_t* _it = payload;
371
372 U16_to_bytes(_it, header->length); _it+=2;
373 U16_to_bytes(_it, header->type); _it-=2; /* Return to 0 position */
374
375 if ( header->table ) {
376 uint16_t _x;
377 for ( _x = 0; _x < header->length; _x++ ) {
378 _it+=4; U32_to_bytes(_it, header->table[_x]);
379 }
380 }
381
382 return _it + 4;
383}
384
385/**
386 * @brief Builds header from control session values.
387 *
388 * @param session Control session.
389 * @return RTPHeader* Created header.
390 */
391RTPHeader* build_header ( RTPSession* session )
392{
393 RTPHeader* _retu = calloc ( 1, sizeof (RTPHeader) );
394 assert(_retu);
395
396 ADD_FLAG_VERSION ( _retu, session->version );
397 ADD_FLAG_PADDING ( _retu, session->padding );
398 ADD_FLAG_EXTENSION ( _retu, session->extension );
399 ADD_FLAG_CSRCC ( _retu, session->cc );
400 ADD_SETTING_MARKER ( _retu, session->marker );
401 ADD_SETTING_PAYLOAD ( _retu, session->payload_type );
402
403 _retu->sequnum = session->sequnum;
404 _retu->timestamp = ((uint32_t)(current_time() / 1000)); /* micro to milli */
405 _retu->ssrc = session->ssrc;
406
407 if ( session->cc > 0 ) {
408 _retu->csrc = calloc(session->cc, sizeof (uint32_t));
409 assert(_retu->csrc);
410
411 int i;
412
413 for ( i = 0; i < session->cc; i++ ) {
414 _retu->csrc[i] = session->csrc[i];
415 }
416 } else {
417 _retu->csrc = NULL;
418 }
419
420 _retu->length = 12 /* Minimum header len */ + ( session->cc * size_32 );
421
422 return _retu;
423}
424
425
426/**
427 * @brief Parses data into RTPMessage struct. Stores headers separately from the payload data
428 * and so the length variable is set accordingly. _sequnum_ argument is
429 * passed by the handle_packet() since it's parsed already.
430 *
431 * @param session Control session.
432 * @param sequnum Sequence number that's parsed from payload in handle_packet()
433 * @param data Payload data.
434 * @param length Payload size.
435 * @return RTPMessage*
436 * @retval NULL Error occurred.
437 */
438RTPMessage* msg_parse ( uint16_t sequnum, const uint8_t* data, int length )
439{
440 RTPMessage* _retu = calloc(1, sizeof (RTPMessage));
441
442 _retu->header = extract_header ( data, length ); /* It allocates memory and all */
443
444 if ( !_retu->header ){
445 free(_retu);
446 return NULL;
447 }
448 _retu->header->sequnum = sequnum;
449
450 _retu->length = length - _retu->header->length;
451
452 uint16_t _from_pos = _retu->header->length - 2 /* Since sequ num is excluded */ ;
453
454
455 if ( GET_FLAG_EXTENSION ( _retu->header ) ) {
456 _retu->ext_header = extract_ext_header ( data + _from_pos, length );
457 if ( _retu->ext_header ){
458 _retu->length -= ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
459 _from_pos += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
460 } else { /* Error */
461 free (_retu->ext_header);
462 free (_retu->header);
463 free (_retu);
464 return NULL;
465 }
466 } else {
467 _retu->ext_header = NULL;
468 }
469
470 if ( length - _from_pos <= MAX_RTP_SIZE )
471 memcpy ( _retu->data, data + _from_pos, length - _from_pos );
472 else {
473 rtp_free_msg(NULL, _retu);
474 return NULL;
475 }
476 _retu->next = NULL;
477
478 return _retu;
479}
480
481/**
482 * @brief Callback for networking core.
483 *
484 * @param object RTPSession object.
485 * @param ip_port Where the message comes from.
486 * @param data Message data.
487 * @param length Message length.
488 * @return int
489 * @retval -1 Error occurred.
490 * @retval 0 Success.
491 */
492int rtp_handle_packet ( void* object, IP_Port ip_port, uint8_t* data, uint32_t length )
493{
494 RTPSession* _session = object;
495 RTPMessage* _msg;
496
497 if ( !_session || length < 13 ) /* 12 is the minimum length for rtp + desc. byte */
498 return -1;
499
500 uint8_t _plain[MAX_UDP_PACKET_SIZE];
501
502 uint16_t _sequnum;
503 bytes_to_U16(&_sequnum, data + 1);
504
505 /* Clculate the right nonce */
506 uint8_t _calculated[crypto_box_NONCEBYTES];
507 memcpy(_calculated, _session->decrypt_nonce, crypto_box_NONCEBYTES);
508 increase_nonce ( _calculated, _sequnum );
509
510 /* Decrypt message */
511 int _decrypted_length = decrypt_data_symmetric(
512 (uint8_t*)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
513
514 /* This packet is either not encrypted properly or late
515 */
516 if ( -1 == _decrypted_length ){
517
518 /* If this is the case, then the packet is most likely late.
519 * Try with old nonce cycle.
520 */
521 if ( _session->rsequnum < _sequnum ) {
522 _decrypted_length = decrypt_data_symmetric(
523 (uint8_t*)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain );
524
525 if ( !_decrypted_length ) return -1; /* This packet is not encrypted properly */
526
527 /* Otherwise, if decryption is ok with new cycle, set new cycle
528 */
529 } else {
530 increase_nonce ( _calculated, MAX_SEQU_NUM );
531 _decrypted_length = decrypt_data_symmetric(
532 (uint8_t*)_session->decrypt_key, _calculated, data + 3, length - 3, _plain );
533
534 if ( !_decrypted_length ) return -1; /* This is just an error */
535
536 /* A new cycle setting. */
537 memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES);
538 memcpy(_session->decrypt_nonce, _calculated, crypto_box_NONCEBYTES);
539 }
540 }
541
542 _msg = msg_parse ( _sequnum, _plain, _decrypted_length );
543
544 if ( !_msg ) return -1;
545
546 /* Hopefully this goes well
547 * NOTE: Is this even used?
548 */
549 memcpy(&_msg->from, &ip_port, sizeof(IP_Port));
550
551 /* Check if message came in late */
552 if ( check_late_message(_session, _msg) < 0 ) { /* Not late */
553 _session->rsequnum = _msg->header->sequnum;
554 _session->timestamp = _msg->header->timestamp;
555 }
556
557 pthread_mutex_lock(&_session->mutex);
558
559 if ( _session->last_msg ) {
560 _session->last_msg->next = _msg;
561 _session->last_msg = _msg;
562 } else {
563 _session->last_msg = _session->oldest_msg = _msg;
564 }
565
566 pthread_mutex_unlock(&_session->mutex);
567
568 return 0;
569}
570
571
572
573/**
574 * @brief Stores headers and payload data in one container ( data )
575 * and the length is set accordingly. Returned message is used for sending _only_.
576 *
577 * @param session The control session.
578 * @param data Payload data to send ( This is what you pass ).
579 * @param length Size of the payload data.
580 * @return RTPMessage* Created message.
581 * @retval NULL Error occurred.
582 */
583RTPMessage* rtp_new_message ( RTPSession* session, const uint8_t* data, uint32_t length )
584{
585 if ( !session )
586 return NULL;
587
588 uint8_t* _from_pos;
589 RTPMessage* _retu = calloc(1, sizeof (RTPMessage));
590 assert(_retu);
591
592 /* Sets header values and copies the extension header in _retu */
593 _retu->header = build_header ( session ); /* It allocates memory and all */
594 _retu->ext_header = session->ext_header;
595
596
597 uint32_t _total_length = length + _retu->header->length;
598
599 if ( _retu->ext_header ) {
600 _total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 );
601
602 _from_pos = add_header ( _retu->header, _retu->data );
603 _from_pos = add_ext_header ( _retu->ext_header, _from_pos + 1 );
604 } else {
605 _from_pos = add_header ( _retu->header, _retu->data );
606 }
607
608 /*
609 * Parses the extension header into the message
610 * Of course if any
611 */
612
613 /* Appends _data on to _retu->_data */
614 memcpy ( _from_pos, data, length );
615
616 _retu->length = _total_length;
617
618 _retu->next = NULL;
619
620 return _retu;
621}
622
623
624
625
626
627
628
629/********************************************************************************************************************
630 ********************************************************************************************************************
631 ********************************************************************************************************************
632 ********************************************************************************************************************
633 ********************************************************************************************************************
634 *
635 *
636 *
637 * PUBLIC API FUNCTIONS IMPLEMENTATIONS
638 *
639 *
640 *
641 ********************************************************************************************************************
642 ********************************************************************************************************************
643 ********************************************************************************************************************
644 ********************************************************************************************************************
645 ********************************************************************************************************************/
646
647
648
649
650
651
652
653
654
655/**
656 * @brief Release all messages held by session.
657 *
658 * @param session The session.
659 * @return int
660 * @retval -1 Error occurred.
661 * @retval 0 Success.
662 */
663int rtp_release_session_recv ( RTPSession* session )
664{
665 if ( !session ){
666 return -1;
667 }
668
669 RTPMessage* _tmp,* _it;
670
671 pthread_mutex_lock(&session->mutex);
672
673 for ( _it = session->oldest_msg; _it; _it = _tmp ){
674 _tmp = _it->next;
675 rtp_free_msg( session, _it);
676 }
677
678 session->last_msg = session->oldest_msg = NULL;
679
680 pthread_mutex_unlock(&session->mutex);
681
682 return 0;
683}
684
685
686/**
687 * @brief Gets oldest message in the list.
688 *
689 * @param session Where the list is.
690 * @return RTPMessage* The message. You _must_ call rtp_msg_free() to free it.
691 * @retval NULL No messages in the list, or no list.
692 */
693RTPMessage* rtp_recv_msg ( RTPSession* session )
694{
695 if ( !session )
696 return NULL;
697
698 RTPMessage* _retu = session->oldest_msg;
699
700 pthread_mutex_lock(&session->mutex);
701
702 if ( _retu )
703 session->oldest_msg = _retu->next;
704
705 if ( !session->oldest_msg )
706 session->last_msg = NULL;
707
708 pthread_mutex_unlock(&session->mutex);
709
710 return _retu;
711}
712
713
714/**
715 * @brief Sends data to _RTPSession::dest
716 *
717 * @param session The session.
718 * @param messenger Tox* object.
719 * @param data The payload.
720 * @param length Size of the payload.
721 * @return int
722 * @retval -1 On error.
723 * @retval 0 On success.
724 */
725int rtp_send_msg ( RTPSession* session, Messenger* messenger, const uint8_t* data, uint16_t length )
726{
727 RTPMessage* msg = rtp_new_message (session, data, length);
728
729 if ( !msg ) return -1;
730
731 uint8_t _send_data [ MAX_UDP_PACKET_SIZE ];
732
733 _send_data[0] = session->prefix;
734
735 /* Generate the right nonce */
736 uint8_t _calculated[crypto_box_NONCEBYTES];
737 memcpy(_calculated, session->encrypt_nonce, crypto_box_NONCEBYTES);
738 increase_nonce ( _calculated, msg->header->sequnum );
739
740 /* Need to skip 2 bytes that are for sequnum */
741 int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/
742 (uint8_t*) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 );
743
744 int full_length = encrypted_length + 3;
745
746 _send_data[1] = msg->data[0];
747 _send_data[2] = msg->data[1];
748
749
750 /*if ( full_length != sendpacket ( messenger->net, *((IP_Port*) &session->dest), _send_data, full_length) ) {*/
751 if ( full_length != send_custom_user_packet(messenger, session->dest, _send_data, full_length) ) {
752 printf("Rtp error: %s\n", strerror(errno));
753 return -1;
754 }
755
756
757 /* Set sequ number */
758 if ( session->sequnum >= MAX_SEQU_NUM ) {
759 session->sequnum = 0;
760 memcpy(session->encrypt_nonce, _calculated, crypto_box_NONCEBYTES);
761 } else {
762 session->sequnum++;
763 }
764
765 rtp_free_msg ( session, msg );
766 return 0;
767}
768
769
770/**
771 * @brief Speaks for it self.
772 *
773 * @param session The control session msg belongs to. You set it as NULL when freeing recved messages.
774 * Otherwise set it to session the message was created from.
775 * @param msg The message.
776 * @return void
777 */
778void rtp_free_msg ( RTPSession* session, RTPMessage* msg )
779{
780 if ( !session ){
781 free ( msg->header->csrc );
782 if ( msg->ext_header ){
783 free ( msg->ext_header->table );
784 free ( msg->ext_header );
785 }
786 } else {
787 if ( session->csrc != msg->header->csrc )
788 free ( msg->header->csrc );
789 if ( msg->ext_header && session->ext_header != msg->ext_header ) {
790 free ( msg->ext_header->table );
791 free ( msg->ext_header );
792 }
793 }
794
795 free ( msg->header );
796 free ( msg );
797}
798
799
800/**
801 * @brief Must be called before calling any other rtp function. It's used
802 * to initialize RTP control session.
803 *
804 * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType
805 * @param messenger Tox* object.
806 * @param friend_num Friend id.
807 * @param encrypt_key Speaks for it self.
808 * @param decrypt_key Speaks for it self.
809 * @param encrypt_nonce Speaks for it self.
810 * @param decrypt_nonce Speaks for it self.
811 * @return RTPSession* Created control session.
812 * @retval NULL Error occurred.
813 */
814RTPSession* rtp_init_session ( int payload_type,
815 Messenger* messenger,
816 int friend_num,
817 const uint8_t* encrypt_key,
818 const uint8_t* decrypt_key,
819 const uint8_t* encrypt_nonce,
820 const uint8_t* decrypt_nonce )
821{
822 RTPSession* _retu = calloc(1, sizeof(RTPSession));
823 assert(_retu);
824
825 /*networking_registerhandler(messenger->net, payload_type, rtp_handle_packet, _retu);*/
826 if ( -1 == custom_user_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu) )
827 {
828 fprintf(stderr, "Error setting custom register handler for rtp session\n");
829 free(_retu);
830 return NULL;
831 }
832
833 _retu->version = RTP_VERSION; /* It's always 2 */
834 _retu->padding = 0; /* If some additional data is needed about the packet */
835 _retu->extension = 0; /* If extension to header is needed */
836 _retu->cc = 1; /* Amount of contributors */
837 _retu->csrc = NULL; /* Container */
838 _retu->ssrc = random_int();
839 _retu->marker = 0;
840 _retu->payload_type = payload_table[payload_type];
841
842 _retu->dest = friend_num;
843
844 _retu->rsequnum = _retu->sequnum = 1;
845
846 _retu->ext_header = NULL; /* When needed allocate */
847 _retu->framerate = -1;
848 _retu->resolution = -1;
849
850 _retu->encrypt_key = encrypt_key;
851 _retu->decrypt_key = decrypt_key;
852
853 /* Need to allocate new memory */
854 _retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->encrypt_nonce);
855 _retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->decrypt_nonce);
856 _retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); assert(_retu->nonce_cycle);
857
858 memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES);
859 memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES);
860 memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES);
861
862 _retu->csrc = calloc(1, sizeof (uint32_t));
863 assert(_retu->csrc);
864
865 _retu->csrc[0] = _retu->ssrc; /* Set my ssrc to the list receive */
866
867 /* Also set payload type as prefix */
868 _retu->prefix = payload_type;
869
870 _retu->oldest_msg = _retu->last_msg = NULL;
871
872 pthread_mutex_init(&_retu->mutex, NULL);
873 /*
874 *
875 */
876 return _retu;
877}
878
879
880/**
881 * @brief Terminate the session.
882 *
883 * @param session The session.
884 * @param messenger The messenger who owns the session
885 * @return int
886 * @retval -1 Error occurred.
887 * @retval 0 Success.
888 */
889int rtp_terminate_session ( RTPSession* session, Messenger* messenger )
890{
891 if ( !session )
892 return -1;
893
894 custom_user_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL);
895
896 free ( session->ext_header );
897 free ( session->csrc );
898 free ( session->decrypt_nonce );
899 free ( session->encrypt_nonce );
900 free ( session->nonce_cycle );
901
902 pthread_mutex_destroy(&session->mutex);
903
904 /* And finally free session */
905 free ( session );
906
907 return 0;
908} \ No newline at end of file
diff --git a/toxav/rtp.h b/toxav/rtp.h
new file mode 100644
index 00000000..acec8b0a
--- /dev/null
+++ b/toxav/rtp.h
@@ -0,0 +1,219 @@
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 at #tox-dev @ freenode.net:6667
22 */
23
24#ifndef __TOXRTP
25#define __TOXRTP
26
27#define RTP_VERSION 2
28#include <inttypes.h>
29#include <pthread.h>
30
31#include "../toxcore/util.h"
32#include "../toxcore/network.h"
33#include "../toxcore/net_crypto.h"
34#include "../toxcore/Messenger.h"
35
36#define MAX_SEQU_NUM 65535
37#define MAX_RTP_SIZE 65535
38
39/**
40 * @brief Standard rtp header
41 *
42 */
43
44typedef struct _RTPHeader {
45 uint8_t flags; /* Version(2),Padding(1), Ext(1), Cc(4) */
46 uint8_t marker_payloadt; /* Marker(1), PlayLoad Type(7) */
47 uint16_t sequnum; /* Sequence Number */
48 uint32_t timestamp; /* Timestamp */
49 uint32_t ssrc; /* SSRC */
50 uint32_t* csrc; /* CSRC's table */
51 uint32_t length; /* Length of the header in payload string. */
52
53} RTPHeader;
54
55
56/**
57 * @brief Standard rtp extension header.
58 *
59 */
60typedef struct _RTPExtHeader {
61 uint16_t type; /* Extension profile */
62 uint16_t length; /* Number of extensions */
63 uint32_t* table; /* Extension's table */
64
65} RTPExtHeader;
66
67
68/**
69 * @brief Standard rtp message.
70 *
71 */
72typedef struct _RTPMessage {
73 RTPHeader* header;
74 RTPExtHeader* ext_header;
75
76 uint8_t data[MAX_RTP_SIZE];
77 uint32_t length;
78 IP_Port from;
79
80 struct _RTPMessage* next;
81} RTPMessage;
82
83
84/**
85 * @brief Our main session descriptor.
86 * It measures the session variables and controls
87 * the entire session. There are functions for manipulating
88 * the session so tend to use those instead of directly modifying
89 * session parameters.
90 *
91 */
92typedef struct _RTPSession {
93 uint8_t version;
94 uint8_t padding;
95 uint8_t extension;
96 uint8_t cc;
97 uint8_t marker;
98 uint8_t payload_type;
99 uint16_t sequnum; /* Set when sending */
100 uint16_t rsequnum; /* Check when recving msg */
101 uint32_t timestamp;
102 uint32_t ssrc;
103 uint32_t* csrc;
104
105 /* If some additional data must be sent via message
106 * apply it here. Only by allocating this member you will be
107 * automatically placing it within a message.
108 */
109 RTPExtHeader* ext_header;
110
111 /* External header identifiers */
112 int resolution;
113 int framerate;
114
115
116 /* Since these are only references of the
117 * call structure don't allocate or free
118 */
119
120 const uint8_t* encrypt_key;
121 const uint8_t* decrypt_key;
122 uint8_t* encrypt_nonce;
123 uint8_t* decrypt_nonce;
124
125 uint8_t* nonce_cycle;
126
127 RTPMessage* oldest_msg;
128 RTPMessage* last_msg; /* tail */
129
130 /* Msg prefix for core to know when recving */
131 uint8_t prefix;
132
133 pthread_mutex_t mutex;
134 int dest;
135
136} RTPSession;
137
138
139/**
140 * @brief Release all messages held by session.
141 *
142 * @param session The session.
143 * @return int
144 * @retval -1 Error occurred.
145 * @retval 0 Success.
146 */
147int rtp_release_session_recv ( RTPSession* session );
148
149
150/**
151 * @brief Get's oldest message in the list.
152 *
153 * @param session Where the list is.
154 * @return RTPMessage* The message. You need to call rtp_msg_free() to free it.
155 * @retval NULL No messages in the list, or no list.
156 */
157RTPMessage* rtp_recv_msg ( RTPSession* session );
158
159
160/**
161 * @brief Sends msg to _RTPSession::dest
162 *
163 * @param session The session.
164 * @param msg The message
165 * @param messenger Tox* object.
166 * @return int
167 * @retval -1 On error.
168 * @retval 0 On success.
169 */
170int rtp_send_msg ( RTPSession* session, Messenger* messenger, const uint8_t* data, uint16_t length );
171
172
173/**
174 * @brief Speaks for it self.
175 *
176 * @param session The control session msg belongs to. It can be NULL.
177 * @param msg The message.
178 * @return void
179 */
180void rtp_free_msg ( RTPSession* session, RTPMessage* msg );
181
182
183/**
184 * @brief Must be called before calling any other rtp function. It's used
185 * to initialize RTP control session.
186 *
187 * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType
188 * @param messenger Tox* object.
189 * @param friend_num Friend id.
190 * @param encrypt_key Speaks for it self.
191 * @param decrypt_key Speaks for it self.
192 * @param encrypt_nonce Speaks for it self.
193 * @param decrypt_nonce Speaks for it self.
194 * @return RTPSession* Created control session.
195 * @retval NULL Error occurred.
196 */
197RTPSession* rtp_init_session ( int payload_type,
198 Messenger* messenger,
199 int friend_num,
200 const uint8_t* encrypt_key,
201 const uint8_t* decrypt_key,
202 const uint8_t* encrypt_nonce,
203 const uint8_t* decrypt_nonce );
204
205
206/**
207 * @brief Terminate the session.
208 *
209 * @param session The session.
210 * @param messenger The messenger who owns the session
211 * @return int
212 * @retval -1 Error occurred.
213 * @retval 0 Success.
214 */
215int rtp_terminate_session ( RTPSession* session, Messenger* messenger );
216
217
218
219#endif /* __TOXRTP */
diff --git a/toxav/toxav.c b/toxav/toxav.c
new file mode 100644
index 00000000..b40a1c02
--- /dev/null
+++ b/toxav/toxav.c
@@ -0,0 +1,539 @@
1/** toxav.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 at #tox-dev @ freenode.net:6667
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif /* HAVE_CONFIG_H */
27
28#include "media.h"
29#include "rtp.h"
30#include "msi.h"
31
32#include <stdlib.h>
33#include <string.h>
34#include <assert.h>
35
36#include "toxav.h"
37
38#define inline__ inline __attribute__((always_inline))
39
40static const uint8_t audio_index = 0, video_index = 1;
41
42
43typedef enum {
44 ts_closing,
45 ts_running,
46 ts_closed
47
48} ThreadState;
49
50typedef struct _ToxAv
51{
52 Messenger* messenger;
53
54 MSISession* msi_session; /** Main msi session */
55
56 RTPSession* rtp_sessions[2]; /* Audio is first and video is second */
57
58 struct jitter_buffer* j_buf;
59 CodecState* cs;
60
61 void* agent_handler;
62} ToxAv;
63
64/**
65 * @brief Start new A/V session. There can only be one session at the time. If you register more
66 * it will result in undefined behaviour.
67 *
68 * @param messenger The messenger handle.
69 * @param useragent The agent handling A/V session (i.e. phone).
70 * @param ua_name Useragent name.
71 * @param video_width Width of video frame.
72 * @param video_height Height of video frame.
73 * @return ToxAv*
74 * @retval NULL On error.
75 */
76ToxAv* toxav_new( Tox* messenger, void* useragent, const char* ua_name , uint16_t video_width, uint16_t video_height)
77{
78 ToxAv* av = calloc ( sizeof(ToxAv), 1);
79 if (av == NULL)
80 return NULL;
81
82 av->messenger = (Messenger *)messenger;
83
84 av->msi_session = msi_init_session(av->messenger, (const unsigned char*) ua_name );
85 av->msi_session->agent_handler = av;
86
87 av->rtp_sessions[0] = av->rtp_sessions [1] = NULL;
88
89 /* NOTE: This should be user defined or? */
90 av->j_buf = create_queue(20);
91
92 av->cs = codec_init_session(AUDIO_BITRATE, AUDIO_FRAME_DURATION, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, video_width, video_height, VIDEO_BITRATE);
93
94 av->agent_handler = useragent;
95
96 return av;
97}
98
99/**
100 * @brief Remove A/V session.
101 *
102 * @param av Handler.
103 * @return void
104 */
105void toxav_kill ( ToxAv* av )
106{
107 msi_terminate_session(av->msi_session);
108
109 if ( av->rtp_sessions[audio_index] ) {
110 rtp_terminate_session(av->rtp_sessions[audio_index], av->msi_session->messenger_handle);
111 }
112
113 if ( av->rtp_sessions[video_index] ) {
114 rtp_terminate_session(av->rtp_sessions[video_index], av->msi_session->messenger_handle);
115 }
116
117 codec_terminate_session(av->cs);
118
119 free(av);
120}
121
122/**
123 * @brief Register callback for call state.
124 *
125 * @param callback The callback
126 * @param id One of the ToxAvCallbackID values
127 * @return void
128 */
129void toxav_register_callstate_callback ( ToxAVCallback callback, ToxAvCallbackID id )
130{
131 msi_register_callback((MSICallback)callback, (MSICallbackID) id);
132}
133
134/**
135 * @brief Call user. Use its friend_id.
136 *
137 * @param av Handler.
138 * @param user The user.
139 * @param call_type Call type.
140 * @param ringing_seconds Ringing timeout.
141 * @return int
142 * @retval 0 Success.
143 * @retval ToxAvError On error.
144 */
145int toxav_call (ToxAv* av, int user, ToxAvCallType call_type, int ringing_seconds )
146{
147 if ( av->msi_session->call ) {
148 return ErrorAlreadyInCall;
149 }
150
151 return msi_invite(av->msi_session, call_type, ringing_seconds * 1000, user);
152}
153
154/**
155 * @brief Hangup active call.
156 *
157 * @param av Handler.
158 * @return int
159 * @retval 0 Success.
160 * @retval ToxAvError On error.
161 */
162int toxav_hangup ( ToxAv* av )
163{
164 if ( !av->msi_session->call ) {
165 return ErrorNoCall;
166 }
167
168 if ( av->msi_session->call->state != call_active ) {
169 return ErrorInvalidState;
170 }
171
172 return msi_hangup(av->msi_session);
173}
174
175/**
176 * @brief Answer incomming call.
177 *
178 * @param av Handler.
179 * @param call_type Answer with...
180 * @return int
181 * @retval 0 Success.
182 * @retval ToxAvError On error.
183 */
184int toxav_answer ( ToxAv* av, ToxAvCallType call_type )
185{
186 if ( !av->msi_session->call ) {
187 return ErrorNoCall;
188 }
189
190 if ( av->msi_session->call->state != call_starting ) {
191 return ErrorInvalidState;
192 }
193
194 return msi_answer(av->msi_session, call_type);
195}
196
197/**
198 * @brief Reject incomming call.
199 *
200 * @param av Handler.
201 * @param reason Optional reason. Set NULL if none.
202 * @return int
203 * @retval 0 Success.
204 * @retval ToxAvError On error.
205 */
206int toxav_reject ( ToxAv* av, const char* reason )
207{
208 if ( !av->msi_session->call ) {
209 return ErrorNoCall;
210 }
211
212 if ( av->msi_session->call->state != call_starting ) {
213 return ErrorInvalidState;
214 }
215
216 return msi_reject(av->msi_session, (const uint8_t*) reason);
217}
218
219/**
220 * @brief Cancel outgoing request.
221 *
222 * @param av Handler.
223 * @param reason Optional reason.
224 * @return int
225 * @retval 0 Success.
226 * @retval ToxAvError On error.
227 */
228int toxav_cancel ( ToxAv* av, const char* reason )
229{
230 if ( !av->msi_session->call ) {
231 return ErrorNoCall;
232 }
233
234 return msi_cancel(av->msi_session, 0, (const uint8_t*)reason);
235}
236
237/**
238 * @brief Terminate transmission. Note that transmission will be terminated without informing remote peer.
239 *
240 * @param av Handler.
241 * @return int
242 * @retval 0 Success.
243 * @retval ToxAvError On error.
244 */
245int toxav_stop_call ( ToxAv* av )
246{
247 if ( !av->msi_session->call ) {
248 return ErrorNoCall;
249 }
250
251 return msi_stopcall(av->msi_session);
252}
253
254/**
255 * @brief Must be call before any RTP transmission occurs.
256 *
257 * @param av Handler.
258 * @return int
259 * @retval 0 Success.
260 * @retval ToxAvError On error.
261 */
262int toxav_prepare_transmission ( ToxAv* av )
263{
264 assert(av->msi_session);
265 if ( !av->msi_session || !av->msi_session->call ) {
266 return ErrorNoCall;
267 }
268
269 av->rtp_sessions[audio_index] = rtp_init_session(
270 type_audio,
271 av->messenger,
272 av->msi_session->call->peers[0],
273 av->msi_session->call->key_peer,
274 av->msi_session->call->key_local,
275 av->msi_session->call->nonce_peer,
276 av->msi_session->call->nonce_local
277 );
278
279
280 if ( !av->rtp_sessions[audio_index] ) {
281 fprintf(stderr, "Error while starting audio RTP session!\n");
282 return ErrorStartingAudioRtp;
283 }
284
285 av->rtp_sessions[video_index] = rtp_init_session (
286 type_video,
287 av->messenger,
288 av->msi_session->call->peers[0],
289 av->msi_session->call->key_peer,
290 av->msi_session->call->key_local,
291 av->msi_session->call->nonce_peer,
292 av->msi_session->call->nonce_local
293 );
294
295
296 if ( !av->rtp_sessions[video_index] ) {
297 fprintf(stderr, "Error while starting video RTP session!\n");
298 return ErrorStartingVideoRtp;
299 }
300
301 return ErrorNone;
302}
303
304/**
305 * @brief Call this at the end of the transmission.
306 *
307 * @param av Handler.
308 * @return int
309 * @retval 0 Success.
310 * @retval ToxAvError On error.
311 */
312int toxav_kill_transmission ( ToxAv* av )
313{
314 /* Both sessions should be active at any time */
315 if ( !av->rtp_sessions[0] || !av->rtp_sessions[0] )
316 return ErrorNoTransmission;
317
318
319 if ( -1 == rtp_terminate_session(av->rtp_sessions[audio_index], av->messenger) ) {
320 fprintf(stderr, "Error while terminating audio RTP session!\n");
321 return ErrorTerminatingAudioRtp;
322 }
323
324 if ( -1 == rtp_terminate_session(av->rtp_sessions[video_index], av->messenger) ) {
325 fprintf(stderr, "Error while terminating video RTP session!\n");
326 return ErrorTerminatingVideoRtp;
327 }
328
329 return ErrorNone;
330}
331
332
333/**
334 * @brief Send RTP payload.
335 *
336 * @param av Handler.
337 * @param type Type of payload.
338 * @param payload The payload.
339 * @param length Size of it.
340 * @return int
341 * @retval 0 Success.
342 * @retval -1 Failure.
343 */
344inline__ int toxav_send_rtp_payload ( ToxAv* av, ToxAvCallType type, const uint8_t* payload, uint16_t length )
345{
346 if ( av->rtp_sessions[type - TypeAudio] )
347 return rtp_send_msg ( av->rtp_sessions[type - TypeAudio], av->msi_session->messenger_handle, payload, length );
348 else return -1;
349}
350
351/**
352 * @brief Receive RTP payload.
353 *
354 * @param av Handler.
355 * @param type Type of the payload.
356 * @param dest Storage.
357 * @return int
358 * @retval ToxAvError On Error.
359 * @retval >=0 Size of received payload.
360 */
361inline__ int toxav_recv_rtp_payload ( ToxAv* av, ToxAvCallType type, uint8_t* dest )
362{
363 if ( !dest ) return ErrorInternal;
364
365 if ( !av->rtp_sessions[type - TypeAudio] ) return ErrorNoRtpSession;
366
367 RTPMessage* message;
368
369 if ( type == TypeAudio ) {
370
371 do {
372 message = rtp_recv_msg(av->rtp_sessions[audio_index]);
373
374 if (message) {
375 /* push the packet into the queue */
376 queue(av->j_buf, message);
377 }
378 } while(message);
379
380 int success = 0;
381 message = dequeue(av->j_buf, &success);
382
383 if ( success == 2) return ErrorAudioPacketLost;
384 }
385 else {
386 message = rtp_recv_msg(av->rtp_sessions[video_index]);
387 }
388
389 if ( message ) {
390 memcpy(dest, message->data, message->length);
391
392 int length = message->length;
393
394 rtp_free_msg(NULL, message);
395
396 return length;
397 }
398
399 return 0;
400}
401
402/**
403 * @brief Receive decoded video packet.
404 *
405 * @param av Handler.
406 * @param output Storage.
407 * @return int
408 * @retval 0 Success.
409 * @retval ToxAvError On Error.
410 */
411inline__ int toxav_recv_video ( ToxAv* av, vpx_image_t **output)
412{
413 if ( !output ) return ErrorInternal;
414 uint8_t packet [RTP_PAYLOAD_SIZE];
415 int recved_size = 0;
416 do {
417 recved_size = toxav_recv_rtp_payload(av, TypeVideo, packet);
418 if (recved_size > 0) {
419 printf("decode: %s\n", vpx_codec_err_to_string(vpx_codec_decode(&av->cs->v_decoder, packet, recved_size, NULL, 0)));
420 }
421 }while (recved_size > 0);
422 vpx_codec_iter_t iter = NULL;
423 vpx_image_t *img;
424 img = vpx_codec_get_frame(&av->cs->v_decoder, &iter);
425 if (img == NULL)
426 return ErrorInternal;
427
428 *output = img;
429 return 0;
430}
431
432/**
433 * @brief Encode and send video packet.
434 *
435 * @param av Handler.
436 * @param input The packet.
437 * @return int
438 * @retval 0 Success.
439 * @retval ToxAvError On error.
440 */
441inline__ int toxav_send_video ( ToxAv* av, vpx_image_t *input)
442{
443 if (vpx_codec_encode(&av->cs->v_encoder, input, av->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US) != VPX_CODEC_OK) {
444 printf("could not encode video frame\n");
445 return ErrorInternal;
446 }
447 ++av->cs->frame_counter;
448
449 vpx_codec_iter_t iter = NULL;
450 const vpx_codec_cx_pkt_t *pkt;
451 int sent = 0;
452 while( (pkt = vpx_codec_get_cx_data(&av->cs->v_encoder, &iter)) ) {
453 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
454 if (toxav_send_rtp_payload(av, TypeVideo, pkt->data.frame.buf, pkt->data.frame.sz) != -1)
455 ++sent;
456 }
457 }
458 if (sent > 0)
459 return 0;
460
461 return ErrorInternal;
462}
463
464/**
465 * @brief Receive decoded audio frame.
466 *
467 * @param av Handler.
468 * @param frame_size ...
469 * @param dest Destination of the packet. Make sure it has enough space for
470 * RTP_PAYLOAD_SIZE bytes.
471 * @return int
472 * @retval >=0 Size of received packet.
473 * @retval ToxAvError On error.
474 */
475inline__ int toxav_recv_audio ( ToxAv* av, int frame_size, int16_t* dest )
476{
477 if ( !dest ) return ErrorInternal;
478 uint8_t packet [RTP_PAYLOAD_SIZE];
479
480 int recved_size = toxav_recv_rtp_payload(av, TypeAudio, packet);
481
482 if ( recved_size == ErrorAudioPacketLost ) {
483 printf("Lost packet\n");
484 return opus_decode(av->cs->audio_decoder, NULL, 0, dest, frame_size, 1);
485 } else if ( recved_size ){
486 return opus_decode(av->cs->audio_decoder, packet, recved_size, dest, frame_size, 0);
487 } else {
488 return 0; /* Nothing received */
489 }
490}
491
492/**
493 * @brief Encode and send audio frame.
494 *
495 * @param av Handler.
496 * @param frame The frame.
497 * @param frame_size It's size.
498 * @return int
499 * @retval 0 Success.
500 * @retval ToxAvError On error.
501 */
502inline__ int toxav_send_audio ( ToxAv* av, const int16_t* frame, int frame_size)
503{
504 uint8_t temp_data[RTP_PAYLOAD_SIZE];
505 int32_t ret = opus_encode(av->cs->audio_encoder, frame, frame_size, temp_data, sizeof(temp_data));
506 if (ret <= 0)
507 return ErrorInternal;
508
509 return toxav_send_rtp_payload(av, TypeAudio, temp_data, ret);
510}
511
512/**
513 * @brief Get peer transmission type. It can either be audio or video.
514 *
515 * @param av Handler.
516 * @param peer The peer
517 * @return int
518 * @retval ToxAvCallType On success.
519 * @retval ToxAvError On error.
520 */
521int toxav_get_peer_transmission_type ( ToxAv* av, int peer )
522{
523 assert(av->msi_session);
524 if ( peer < 0 || !av->msi_session->call || av->msi_session->call->peer_count <= peer )
525 return ErrorInternal;
526
527 return av->msi_session->call->type_peer[peer];
528}
529
530/**
531 * @brief Get reference to an object that is handling av session.
532 *
533 * @param av Handler.
534 * @return void*
535 */
536void* toxav_get_agent_handler ( ToxAv* av )
537{
538 return av->agent_handler;
539}
diff --git a/toxav/toxav.h b/toxav/toxav.h
new file mode 100644
index 00000000..82334273
--- /dev/null
+++ b/toxav/toxav.h
@@ -0,0 +1,300 @@
1/** toxav.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 at #tox-dev @ freenode.net:6667
22 */
23
24
25#ifndef __TOXAV
26#define __TOXAV
27#include <inttypes.h>
28
29/* vpx_image_t */
30#include <vpx/vpx_image.h>
31
32typedef void* ( *ToxAVCallback ) ( void* arg );
33typedef struct _ToxAv ToxAv;
34
35#ifndef __TOX_DEFINED__
36#define __TOX_DEFINED__
37typedef struct Tox Tox;
38#endif
39
40#define RTP_PAYLOAD_SIZE 65535
41
42/* Default video bitrate in bytes/s */
43#define VIDEO_BITRATE 10*1000*100
44
45/* Default audio bitrate in bits/s */
46#define AUDIO_BITRATE 64000
47
48/* Number of audio channels. */
49#define AUDIO_CHANNELS 1
50
51/* Audio frame duration in miliseconds */
52#define AUDIO_FRAME_DURATION 20
53
54/* Audio sample rate recommended to be 48kHz for Opus */
55#define AUDIO_SAMPLE_RATE 48000
56
57/* The amount of samples in one audio frame */
58#define AUDIO_FRAME_SIZE AUDIO_SAMPLE_RATE*AUDIO_FRAME_DURATION/1000
59
60/* Assume 60 fps*/
61#define MAX_ENCODE_TIME_US ((1000 / 60) * 1000)
62
63
64/**
65 * @brief Callbacks ids that handle the call states.
66 */
67typedef enum {
68 /* Requests */
69 OnInvite,
70 OnStart,
71 OnCancel,
72 OnReject,
73 OnEnd,
74
75 /* Responses */
76 OnRinging,
77 OnStarting,
78 OnEnding,
79
80 /* Protocol */
81 OnError,
82 OnRequestTimeout
83
84} ToxAvCallbackID;
85
86
87/**
88 * @brief Call type identifier.
89 */
90typedef enum {
91 TypeAudio = 70,
92 TypeVideo
93} ToxAvCallType;
94
95
96/**
97 * @brief Error indicators.
98 *
99 */
100typedef enum {
101 ErrorNone = 0,
102 ErrorInternal = -1, /* Internal error */
103 ErrorAlreadyInCall = -2, /* Already has an active call */
104 ErrorNoCall = -3, /* Trying to perform call action while not in a call */
105 ErrorInvalidState = -4, /* Trying to perform call action while in invalid state*/
106 ErrorNoRtpSession = -5, /* Trying to perform rtp action on invalid session */
107 ErrorAudioPacketLost = -6, /* Indicating packet loss */
108 ErrorStartingAudioRtp = -7, /* Error in toxav_prepare_transmission() */
109 ErrorStartingVideoRtp = -8 , /* Error in toxav_prepare_transmission() */
110 ErrorNoTransmission = -9, /* Returned in toxav_kill_transmission() */
111 ErrorTerminatingAudioRtp = -10, /* Returned in toxav_kill_transmission() */
112 ErrorTerminatingVideoRtp = -11, /* Returned in toxav_kill_transmission() */
113
114} ToxAvError;
115
116
117/**
118 * @brief Start new A/V session. There can only be one session at the time. If you register more
119 * it will result in undefined behaviour.
120 *
121 * @param messenger The messenger handle.
122 * @param useragent The agent handling A/V session (i.e. phone).
123 * @param ua_name Useragent name.
124 * @param video_width Width of video frame.
125 * @param video_height Height of video frame.
126 * @return ToxAv*
127 * @retval NULL On error.
128 */
129ToxAv* toxav_new(Tox* messenger, void* useragent, const char* ua_name, uint16_t video_width, uint16_t video_height);
130
131/**
132 * @brief Remove A/V session.
133 *
134 * @param av Handler.
135 * @return void
136 */
137void toxav_kill(ToxAv* av);
138
139/**
140 * @brief Register callback for call state.
141 *
142 * @param callback The callback
143 * @param id One of the ToxAvCallbackID values
144 * @return void
145 */
146void toxav_register_callstate_callback (ToxAVCallback callback, ToxAvCallbackID id);
147
148/**
149 * @brief Call user. Use its friend_id.
150 *
151 * @param av Handler.
152 * @param user The user.
153 * @param call_type Call type.
154 * @param ringing_seconds Ringing timeout.
155 * @return int
156 * @retval 0 Success.
157 * @retval ToxAvError On error.
158 */
159int toxav_call(ToxAv* av, int user, ToxAvCallType call_type, int ringing_seconds);
160
161/**
162 * @brief Hangup active call.
163 *
164 * @param av Handler.
165 * @return int
166 * @retval 0 Success.
167 * @retval ToxAvError On error.
168 */
169int toxav_hangup(ToxAv* av);
170
171/**
172 * @brief Answer incomming call.
173 *
174 * @param av Handler.
175 * @param call_type Answer with...
176 * @return int
177 * @retval 0 Success.
178 * @retval ToxAvError On error.
179 */
180int toxav_answer(ToxAv* av, ToxAvCallType call_type );
181
182/**
183 * @brief Reject incomming call.
184 *
185 * @param av Handler.
186 * @param reason Optional reason. Set NULL if none.
187 * @return int
188 * @retval 0 Success.
189 * @retval ToxAvError On error.
190 */
191int toxav_reject(ToxAv* av, const char* reason);
192
193/**
194 * @brief Cancel outgoing request.
195 *
196 * @param av Handler.
197 * @param reason Optional reason.
198 * @return int
199 * @retval 0 Success.
200 * @retval ToxAvError On error.
201 */
202int toxav_cancel(ToxAv* av, const char* reason);
203
204/**
205 * @brief Terminate transmission. Note that transmission will be terminated without informing remote peer.
206 *
207 * @param av Handler.
208 * @return int
209 * @retval 0 Success.
210 * @retval ToxAvError On error.
211 */
212int toxav_stop_call(ToxAv* av);
213
214/**
215 * @brief Must be call before any RTP transmission occurs.
216 *
217 * @param av Handler.
218 * @return int
219 * @retval 0 Success.
220 * @retval ToxAvError On error.
221 */
222int toxav_prepare_transmission(ToxAv* av);
223
224/**
225 * @brief Call this at the end of the transmission.
226 *
227 * @param av Handler.
228 * @return int
229 * @retval 0 Success.
230 * @retval ToxAvError On error.
231 */
232int toxav_kill_transmission(ToxAv* av);
233
234/**
235 * @brief Receive decoded video packet.
236 *
237 * @param av Handler.
238 * @param output Storage.
239 * @return int
240 * @retval 0 Success.
241 * @retval ToxAvError On Error.
242 */
243int toxav_recv_video ( ToxAv* av, vpx_image_t **output);
244
245/**
246 * @brief Receive decoded audio frame.
247 *
248 * @param av Handler.
249 * @param frame_size ...
250 * @param dest Destination of the packet. Make sure it has enough space for
251 * RTP_PAYLOAD_SIZE bytes.
252 * @return int
253 * @retval >=0 Size of received packet.
254 * @retval ToxAvError On error.
255 */
256int toxav_recv_audio( ToxAv* av, int frame_size, int16_t* dest );
257
258/**
259 * @brief Encode and send video packet.
260 *
261 * @param av Handler.
262 * @param input The packet.
263 * @return int
264 * @retval 0 Success.
265 * @retval ToxAvError On error.
266 */
267int toxav_send_video ( ToxAv* av, vpx_image_t *input);
268
269/**
270 * @brief Encode and send audio frame.
271 *
272 * @param av Handler.
273 * @param frame The frame.
274 * @param frame_size It's size.
275 * @return int
276 * @retval 0 Success.
277 * @retval ToxAvError On error.
278 */
279int toxav_send_audio ( ToxAv* av, const int16_t* frame, int frame_size);
280
281/**
282 * @brief Get peer transmission type. It can either be audio or video.
283 *
284 * @param av Handler.
285 * @param peer The peer
286 * @return int
287 * @retval ToxAvCallType On success.
288 * @retval ToxAvError On error.
289 */
290int toxav_get_peer_transmission_type ( ToxAv* av, int peer );
291
292/**
293 * @brief Get reference to an object that is handling av session.
294 *
295 * @param av Handler.
296 * @return void*
297 */
298void* toxav_get_agent_handler ( ToxAv* av );
299
300#endif /* __TOXAV */ \ No newline at end of file
diff --git a/toxmsi/Makefile.inc b/toxmsi/Makefile.inc
deleted file mode 100644
index 7d620e70..00000000
--- a/toxmsi/Makefile.inc
+++ /dev/null
@@ -1,69 +0,0 @@
1if BUILD_AV
2
3lib_LTLIBRARIES += libtoxmsi.la
4
5libtoxmsi_la_include_HEADERS = \
6 ../toxmsi/toxmsi.h \
7 ../toxmsi/toxmedia.h
8
9libtoxmsi_la_includedir = $(includedir)/tox
10
11
12libtoxmsi_la_SOURCES = ../toxmsi/toxmsi.h \
13 ../toxmsi/toxmsi.c \
14 ../toxmsi/toxmsi_message.h \
15 ../toxmsi/toxmsi_message.c \
16 ../toxmsi/toxmsi_header.h \
17 ../toxmsi/toxmsi_header.c \
18 ../toxmsi/toxmsi_event.h \
19 ../toxmsi/toxmsi_event.c \
20 ../toxrtp/tests/test_helper.h \
21 ../toxrtp/tests/test_helper.c
22
23libtoxmsi_la_CFLAGS = -I../toxcore \
24 -I../toxmsi \
25 -I../toxrtp \
26 $(NACL_CFLAGS) \
27 $(PTHREAD_CFLAGS)
28
29libtoxmsi_la_LDFLAGS = $(TOXMSI_LT_LDFLAGS) \
30 $(EXTRA_LT_LDFLAGS) \
31 $(NACL_LDFLAGS) \
32 $(PTHREAD_LIBS)
33
34libtoxmsi_la_LIBS = $(NACL_LIBS)
35
36noinst_PROGRAMS += phone
37
38phone_SOURCES = ../toxmsi/phone.c \
39 ../toxmsi/toxmedia.c
40
41phone_CFLAGS = -I../toxcore \
42 -I../toxrtp \
43 $(AVFORMAT_CFLAGS) \
44 $(AVCODEC_CFLAGS) \
45 $(AVUTIL_CFLAGS) \
46 $(AVDEVICE_CFLAGS) \
47 $(SWSCALE_CFLAGS) \
48 $(SDL_CFLAGS) \
49 $(OPENAL_CFLAGS) \
50 $(NACL_CFLAGS) \
51 $(OPUS_CFLAGS) \
52 $(PTHREAD_CFLAGS)
53
54
55phone_LDADD = $(PTHREAD_LIBS) \
56 libtoxrtp.la \
57 libtoxmsi.la \
58 $(NACL_LDFLAGS) \
59 $(AVFORMAT_LIBS) \
60 $(AVCODEC_LIBS) \
61 $(AVUTIL_LIBS) \
62 $(AVDEVICE_LIBS) \
63 $(SWSCALE_LIBS) \
64 $(SDL_LIBS) \
65 $(OPENAL_LIBS) \
66 $(NACL_LIBS) \
67 $(OPUS_LIBS)
68
69endif
diff --git a/toxmsi/phone.c b/toxmsi/phone.c
deleted file mode 100644
index 432be94c..00000000
--- a/toxmsi/phone.c
+++ /dev/null
@@ -1,668 +0,0 @@
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif /* HAVE_CONFIG_H */
4
5#define _BSD_SOURCE
6#define _GNU_SOURCE
7
8#define _CT_PHONE
9
10#ifdef _CT_PHONE
11#include "phone.h"
12#include <stdarg.h>
13#include <unistd.h>
14#include <stdio.h>
15#include <string.h>
16#include <stdlib.h>
17/* #include <termios.h> Can this be removed? */
18#include <pthread.h>
19#include "toxmedia.h"
20
21
22
23void INFO (const char *_format, ...)
24{
25 printf("\r[!] ");
26 va_list _arg;
27 va_start (_arg, _format);
28 vfprintf (stdout, _format, _arg);
29 va_end (_arg);
30 printf("\n\r >> ");
31 fflush(stdout);
32}
33
34int rtp_handlepacket ( void *_object, tox_IP_Port ip_port, uint8_t *data, uint32_t length )
35{
36 phone_t *_phone = _object;
37 rtp_msg_t *_msg;
38 uint8_t _payload_id;
39
40 if ( _phone->_msi->_call && _phone->_msi->_call->_state == call_active ) {
41
42 _msg = rtp_msg_parse ( NULL, data + 1, length - 1 ); /* ignore marker byte */
43
44 if ( !_msg )
45 return 0;
46
47 _payload_id = rtp_header_get_setting_payload_type(_msg->_header);
48
49 if ( _payload_id == _PAYLOAD_OPUS && _phone->_rtp_audio )
50 rtp_store_msg(_phone->_rtp_audio, _msg);
51 else if ( _payload_id == _PAYLOAD_VP8 && _phone->_rtp_video )
52 rtp_store_msg(_phone->_rtp_video, _msg);
53 else rtp_free_msg( NULL, _msg);
54 }
55
56 return SUCCESS;
57}
58int msi_handlepacket ( void *_object, tox_IP_Port ip_port, uint8_t *data, uint32_t length )
59{
60 msi_session_t *_session = _object;
61 msi_msg_t *_msg;
62
63 _msg = msi_parse_msg ( data + 1 ); /* ignore marker byte */
64
65 if ( _msg ) {
66 /* my current solution for "hole punching" */
67 _session->_friend_id = ip_port;
68 } else {
69 return FAILURE;
70 }
71
72 /* place message in a session */
73 msi_store_msg(_session, _msg);
74
75 return SUCCESS;
76}
77
78void *phone_receivepacket ( void *_phone_p )
79{
80 phone_t *_phone = _phone_p;
81
82
83 networking_registerhandler(_phone->_networking, MSI_PACKET, msi_handlepacket, _phone->_msi);
84 networking_registerhandler(_phone->_networking, RTP_PACKET, rtp_handlepacket, _phone);
85
86 /* Now start main networking loop */
87 while ( _phone->_networking ) { /* so not thread safe */
88 networking_poll(_phone->_networking);
89 usleep(10000);
90 }
91
92 pthread_exit ( NULL );
93}
94
95/* Media transport callback */
96typedef struct hmtc_args_s {
97 rtp_session_t **_rtp_audio;
98 rtp_session_t **_rtp_video;
99 call_type *_local_type_call;
100 call_state *_this_call;
101 void *_core_handler;
102} hmtc_args_t;
103
104void *phone_handle_media_transport_poll ( void *_hmtc_args_p )
105{
106 rtp_msg_t *_audio_msg, * _video_msg;
107
108 hmtc_args_t *_hmtc_args = _hmtc_args_p;
109
110 rtp_session_t *_rtp_audio = *_hmtc_args->_rtp_audio;
111 rtp_session_t *_rtp_video = *_hmtc_args->_rtp_video;
112
113 call_type *_type = _hmtc_args->_local_type_call;
114 void *_core_handler = _hmtc_args->_core_handler;
115
116
117 call_state *_this_call = _hmtc_args->_this_call;
118
119 while ( *_this_call == call_active ) {
120
121 // THREADLOCK()
122
123 _audio_msg = rtp_recv_msg ( _rtp_audio );
124 _video_msg = rtp_recv_msg ( _rtp_video );
125
126 if ( _audio_msg ) {
127 /* Do whatever with msg */
128 puts("audio");
129 /* Do whatever with msg
130 puts(_audio_msg->_data);*/
131 rtp_free_msg ( _rtp_audio, _audio_msg );
132 }
133
134 if ( _video_msg ) {
135 /* Do whatever with msg */
136 puts("video");
137 /* Do whatever with msg
138 puts(_video_msg->_data); */
139 rtp_free_msg ( _rtp_video, _video_msg );
140 _video_msg = NULL;
141 }
142
143 /* -------------------- */
144
145 _audio_msg = rtp_msg_new ( _rtp_audio, (const uint8_t *)"audio\0", 6 ) ;
146 rtp_send_msg ( _rtp_audio, _audio_msg, _core_handler );
147 _audio_msg = NULL;
148
149 if ( *_type == type_video ) { /* if local call send video */
150 _video_msg = rtp_msg_new ( _rtp_video, (const uint8_t *)"video\0", 6 ) ;
151 rtp_send_msg ( _rtp_video, _video_msg, _core_handler );
152 _video_msg = NULL;
153 }
154
155 //THREADUNLOCK()
156
157 usleep ( 10000 );
158 /* -------------------- */
159 }
160
161 //THREADLOCK()
162
163 if ( _audio_msg ) {
164 rtp_free_msg(_rtp_audio, _audio_msg);
165 }
166
167 if ( _video_msg ) {
168 rtp_free_msg(_rtp_video, _video_msg);
169 }
170
171 rtp_release_session_recv(_rtp_video);
172 rtp_release_session_recv(_rtp_audio);
173
174 rtp_terminate_session(_rtp_audio);
175 rtp_terminate_session(_rtp_video);
176
177 *_hmtc_args->_rtp_audio = NULL;
178 *_hmtc_args->_rtp_video = NULL;
179
180 free(_hmtc_args_p);
181
182 //THREADUNLOCK()
183
184 INFO("Media thread finished!");
185
186 pthread_exit ( NULL );
187}
188
189pthread_t phone_startmedia_loop ( phone_t *_phone )
190{
191 if ( !_phone ) {
192 return 0;
193 }
194
195 int _status;
196
197 uint8_t _prefix = RTP_PACKET;
198
199 pthread_t _rtp_tid;
200 int _rtp_thread_running = 1;
201
202 _phone->_rtp_audio = rtp_init_session ( -1, 1 );
203 rtp_set_prefix ( _phone->_rtp_audio, &_prefix, 1 );
204 rtp_add_receiver ( _phone->_rtp_audio, &_phone->_msi->_friend_id );
205 rtp_set_payload_type(_phone->_rtp_audio, _PAYLOAD_OPUS);
206
207 _phone->_rtp_video = rtp_init_session ( -1, 1 );
208 rtp_set_prefix ( _phone->_rtp_video, &_prefix, 1 );
209 rtp_add_receiver ( _phone->_rtp_video, &_phone->_msi->_friend_id );
210 rtp_set_payload_type(_phone->_rtp_video, _PAYLOAD_VP8);
211
212
213
214 hmtc_args_t *rtp_targs = calloc(sizeof(hmtc_args_t), 1);
215
216
217 rtp_targs->_rtp_audio = &_phone->_rtp_audio;
218 rtp_targs->_rtp_video = &_phone->_rtp_video;
219 rtp_targs->_local_type_call = &_phone->_msi->_call->_type_local;
220 rtp_targs->_this_call = &_phone->_msi->_call->_state;
221 rtp_targs->_core_handler = _phone->_networking;
222
223 codec_state *cs;
224 cs = _phone->cs;
225 //_status = pthread_create ( &_rtp_tid, NULL, phone_handle_media_transport_poll, rtp_targs );
226 cs->_rtp_audio = _phone->_rtp_audio;
227 cs->_rtp_video = _phone->_rtp_video;
228 cs->_networking = _phone->_networking;
229 cs->socket = _phone->_tox_sock;
230 cs->quit = 0;
231
232 printf("support: %d %d\n", cs->support_send_audio, cs->support_send_video);
233
234 if (cs->support_send_audio && cs->support_send_video) /* quick fix */
235 pthread_create(&_phone->cs->encode_audio_thread, NULL, encode_audio_thread, _phone->cs);
236
237 if (cs->support_receive_audio)
238 pthread_create(&_phone->cs->decode_audio_thread, NULL, decode_audio_thread, _phone->cs);
239
240 if (cs->support_send_video)
241 pthread_create(&_phone->cs->encode_video_thread, NULL, encode_video_thread, _phone->cs);
242
243 if (cs->support_receive_video)
244 pthread_create(&_phone->cs->decode_video_thread, NULL, decode_video_thread, _phone->cs);
245
246//
247 return 1;
248
249
250
251
252}
253
254
255/* Some example callbacks */
256
257MCBTYPE callback_recv_invite ( MCBARGS )
258{
259 const char *_call_type;
260
261 msi_session_t *_msi = _arg;
262
263 /* Get the last one */
264 call_type _type = _msi->_call->_type_peer[_msi->_call->_participants - 1];
265
266 switch ( _type ) {
267 case type_audio:
268 _call_type = "audio";
269 break;
270
271 case type_video:
272 _call_type = "video";
273 break;
274 }
275
276 INFO( "Incoming %s call!", _call_type );
277
278}
279MCBTYPE callback_recv_trying ( MCBARGS )
280{
281 INFO ( "Trying...");
282}
283MCBTYPE callback_recv_ringing ( MCBARGS )
284{
285 INFO ( "Ringing!" );
286}
287MCBTYPE callback_recv_starting ( MCBARGS )
288{
289 msi_session_t *_session = _arg;
290
291 if ( !phone_startmedia_loop(_session->_agent_handler) ) {
292 INFO("Starting call failed!");
293 } else {
294 INFO ("Call started! ( press h to hangup )");
295 }
296}
297MCBTYPE callback_recv_ending ( MCBARGS )
298{
299 msi_session_t *_session = _arg;
300 phone_t *_phone = _session->_agent_handler;
301 _phone->cs->quit = 1;
302
303 if (_phone->cs->encode_video_thread)
304 pthread_join(_phone->cs->encode_video_thread, NULL);
305
306 if (_phone->cs->encode_audio_thread)
307 pthread_join(_phone->cs->encode_audio_thread, NULL);
308
309 if (_phone->cs->decode_audio_thread)
310 pthread_join(_phone->cs->decode_audio_thread, NULL);
311
312 if (_phone->cs->decode_video_thread)
313 pthread_join(_phone->cs->decode_video_thread, NULL);
314
315 SDL_Quit();
316 printf("all A/V threads successfully shut down\n");
317
318 INFO ( "Call ended!" );
319}
320
321MCBTYPE callback_recv_error ( MCBARGS )
322{
323 msi_session_t *_session = _arg;
324
325 INFO( "Error: %s", _session->_last_error_str );
326}
327
328MCBTYPE callback_call_started ( MCBARGS )
329{
330 msi_session_t *_session = _arg;
331
332 if ( !phone_startmedia_loop(_session->_agent_handler) ) {
333 INFO("Starting call failed!");
334 } else {
335 INFO ("Call started! ( press h to hangup )");
336 }
337
338}
339MCBTYPE callback_call_canceled ( MCBARGS )
340{
341 INFO ( "Call canceled!" );
342}
343MCBTYPE callback_call_rejected ( MCBARGS )
344{
345 INFO ( "Call rejected!\n" );
346}
347MCBTYPE callback_call_ended ( MCBARGS )
348{
349
350 msi_session_t *_session = _arg;
351 phone_t *_phone = _session->_agent_handler;
352 _phone->cs->quit = 1;
353
354 if (_phone->cs->encode_video_thread)
355 pthread_join(_phone->cs->encode_video_thread, NULL);
356
357 if (_phone->cs->encode_audio_thread)
358 pthread_join(_phone->cs->encode_audio_thread, NULL);
359
360 if (_phone->cs->decode_audio_thread)
361 pthread_join(_phone->cs->decode_audio_thread, NULL);
362
363 if (_phone->cs->decode_video_thread)
364 pthread_join(_phone->cs->decode_video_thread, NULL);
365
366 SDL_Quit();
367 printf("all A/V threads successfully shut down\n");
368
369 INFO ( "Call ended!" );
370}
371
372MCBTYPE callback_requ_timeout ( MCBARGS )
373{
374 INFO( "No answer! " );
375}
376
377
378phone_t *initPhone(uint16_t _listen_port, uint16_t _send_port)
379{
380 phone_t *_retu = calloc(sizeof(phone_t), 1);
381 _retu->cs = av_calloc(sizeof(codec_state), 1);
382
383 /* Initialize our mutex */
384 pthread_mutex_init ( &_mutex, NULL );
385
386 IP_Port _local;
387 ip_init(&_local.ip, 0);
388 _local.ip.ip4.uint32 = htonl ( INADDR_ANY );
389
390 /* Bind local receive port to any address */
391 _retu->_networking = new_networking ( _local.ip, _listen_port );
392
393 if ( !_retu->_networking ) {
394 fprintf ( stderr, "new_networking() failed!\n" );
395 return NULL;
396 }
397
398 _retu->_send_port = _send_port;
399 _retu->_recv_port = _listen_port;
400
401 _retu->_tox_sock = _retu->_networking->sock;
402
403 _retu->_rtp_audio = NULL;
404 _retu->_rtp_video = NULL;
405
406
407 /* Initialize msi */
408 _retu->_msi = msi_init_session ( _retu->_networking, (const uint8_t *)_USERAGENT );
409
410 if ( !_retu->_msi ) {
411 fprintf ( stderr, "msi_init_session() failed\n" );
412 return NULL;
413 }
414
415 /* Initiate codecs */
416 init_encoder(_retu->cs);
417 init_decoder(_retu->cs);
418
419 _retu->_msi->_agent_handler = _retu;
420 /* Initiate callbacks */
421 msi_register_callback_send ( sendpacket ); /* Using core's send */
422
423 msi_register_callback_call_started ( callback_call_started );
424 msi_register_callback_call_canceled ( callback_call_canceled );
425 msi_register_callback_call_rejected ( callback_call_rejected );
426 msi_register_callback_call_ended ( callback_call_ended );
427
428 msi_register_callback_recv_invite ( callback_recv_invite );
429 msi_register_callback_recv_ringing ( callback_recv_ringing );
430 msi_register_callback_recv_starting ( callback_recv_starting );
431 msi_register_callback_recv_ending ( callback_recv_ending );
432 msi_register_callback_recv_error(callback_recv_error);
433
434 msi_register_callback_requ_timeout ( callback_requ_timeout );
435 /* ------------------ */
436
437 /* Now start msi main loop. It's a must!
438 * Define a frequency in ms; 10 ms is just fine
439 */
440 msi_start_main_loop ( _retu->_msi, 10 );
441
442 return _retu;
443}
444
445pthread_t phone_startmain_loop(phone_t *_phone)
446{
447 int _status;
448 /* Start receive thread */
449 pthread_t _recv_thread, _phone_loop_thread;
450 _status = pthread_create ( &_recv_thread, NULL, phone_receivepacket, _phone );
451
452 if ( _status < 0 ) {
453 printf ( "Error while starting handle call: %d, %s\n", errno, strerror ( errno ) );
454 return 0;
455 }
456
457 _status = pthread_detach ( _recv_thread );
458
459 if ( _status < 0 ) {
460 printf ( "Error while starting handle call: %d, %s\n", errno, strerror ( errno ) );
461 return 0;
462 }
463
464 _status = pthread_create ( &_phone_loop_thread, NULL, phone_poll, _phone );
465
466 if ( _status < 0 ) {
467 printf ( "Error while starting main phone loop: %d, %s\n", errno, strerror ( errno ) );
468 return 0;
469 }
470
471 _status = pthread_join ( _phone_loop_thread, NULL );
472
473 if ( _status < 0 ) {
474 printf ( "Error while starting main phone loop: %d, %s\n", errno, strerror ( errno ) );
475 return 0;
476 }
477
478 return _phone_loop_thread;
479}
480
481void *phone_poll ( void *_p_phone )
482{
483 phone_t *_phone = _p_phone;
484
485 int _status = SUCCESS;
486
487 char _line[100];
488 size_t _len;
489
490
491 char _dest[17]; /* For parsing destination ip */
492 memset(_dest, '\0', 17);
493
494 INFO("Welcome to tox_phone version: " _USERAGENT "\n"
495 "Usage: \n"
496 "c [a/v] (type) [0.0.0.0] (dest ip) (calls dest ip)\n"
497 "h (if call is active hang up)\n"
498 "a [a/v] (answer incoming call: a - audio / v - audio + video (audio is default))\n"
499 "r (reject incoming call)\n"
500 "q (quit)\n"
501 "================================================================================"
502 );
503
504 while ( 1 ) {
505 fgets(_line, sizeof(_line), stdin);
506 int i;
507
508 for (i = 0; i < 100; i++) {
509 if (_line[i] == '\n') {
510 _line[i] = '\0';
511 }
512 }
513
514 _len = strlen(_line);
515
516 if ( !_len ) {
517 printf(" >> ");
518 fflush(stdout);
519 continue;
520 }
521
522 if ( _len > 1 && _line[1] != ' ' && _line[1] != '\n' ) {
523 INFO("Invalid input!");
524 continue;
525 }
526
527 switch (_line[0]) {
528
529 case 'c': {
530 if ( _phone->_msi->_call ) {
531 INFO("Already in a call");
532 break;
533 }
534
535 call_type _ctype;
536
537 if ( _len < 11 ) {
538 INFO("Invalid input; usage: c a/v 0.0.0.0");
539 break;
540 } else if ( _line[2] == 'a' || _line[2] != 'v' ) { /* default and audio */
541 _ctype = type_audio;
542 } else { /* video */
543 _ctype = type_video;
544 }
545
546 strcpy(_dest, _line + 4 );
547 _status = t_setipport(_dest, _phone->_send_port, &(_phone->_msi->_friend_id));
548
549 if ( _status < 0 ) {
550 INFO("Could not resolve address!");
551 } else {
552 /* Set timeout */
553 msi_invite ( _phone->_msi, _ctype, 30 * 1000 );
554 INFO("Calling!");
555 }
556
557 t_memset((uint8_t *)_dest, '\0', 17);
558
559 }
560 break;
561
562 case 'h': {
563 if ( !_phone->_msi->_call ) {
564 break;
565 }
566
567 msi_hangup(_phone->_msi);
568
569 INFO("Hung up...");
570
571 }
572 break;
573
574 case 'a': {
575 if ( _phone->_msi->_call && _phone->_msi->_call->_state != call_starting ) {
576 break;
577 }
578
579 if ( _len > 1 && _line[2] == 'v' )
580 msi_answer(_phone->_msi, type_video);
581 else
582 msi_answer(_phone->_msi, type_audio);
583
584 }
585 break;
586
587 case 'r': {
588 if ( _phone->_msi->_call && _phone->_msi->_call->_state != call_starting ) {
589 break;
590 }
591
592 msi_reject(_phone->_msi);
593
594 INFO("Call Rejected...");
595
596 }
597 break;
598
599 case 'q': {
600 INFO("Quitting!");
601 pthread_exit(NULL);
602 }
603
604 default: {
605 INFO("Invalid command!");
606 }
607 break;
608
609 }
610
611 usleep(1000);
612 }
613
614 pthread_exit(NULL);
615}
616
617int quitPhone(phone_t *_phone)
618{
619 if ( _phone->_msi->_call ) {
620 msi_hangup(_phone->_msi); /* Hangup the phone first */
621 }
622
623 msi_terminate_session(_phone->_msi);
624 pthread_mutex_destroy ( &_mutex );
625
626 printf("\r[i] Quit!\n");
627 return SUCCESS;
628}
629
630/* ---------------------- */
631
632int print_help ( const char *_name )
633{
634 printf ( "Usage: %s -m (mode) -r/s ( for setting the ports on test version )\n", _name );
635 return FAILURE;
636}
637
638int main ( int argc, char *argv [] )
639{
640 arg_t *_args = parse_args ( argc, argv );
641
642 const char *_mode = find_arg_duble ( _args, "-m" );
643 uint16_t _listen_port;
644 uint16_t _send_port;
645
646 if ( !_mode )
647 return print_help ( argv[0] );
648
649 if ( _mode[0] == 'r' ) {
650 _send_port = 31000;
651 _listen_port = 31001;
652 } else if ( _mode[0] == 's' ) {
653 _send_port = 31001;
654 _listen_port = 31000;
655 } else return print_help ( argv[0] );
656
657 phone_t *_phone = initPhone(_listen_port, _send_port);
658
659 if ( _phone ) {
660 phone_startmain_loop(_phone);
661
662 quitPhone(_phone);
663 }
664
665 return SUCCESS;
666}
667
668#endif /* _CT_PHONE */
diff --git a/toxmsi/phone.h b/toxmsi/phone.h
deleted file mode 100644
index f96aac73..00000000
--- a/toxmsi/phone.h
+++ /dev/null
@@ -1,62 +0,0 @@
1#ifndef _PHONE_H_
2#define _PHONE_H_
3
4#include "toxmsi.h"
5#include "../toxrtp/toxrtp.h"
6#include "toxmsi_message.h"
7#include "../toxrtp/toxrtp_message.h"
8#include "../toxrtp/tests/test_helper.h"
9#include <assert.h>
10#include <pthread.h>
11#include "toxmedia.h"
12
13/* Define client version */
14#define _USERAGENT "tox_phone-v.0.2.1"
15
16static pthread_mutex_t _mutex;
17
18#define THREADLOCK() \
19pthread_mutex_lock ( &_mutex );
20
21#define THREADUNLOCK() \
22pthread_mutex_unlock ( &_mutex );
23
24typedef struct phone_s {
25 msi_session_t* _msi;
26
27 rtp_session_t* _rtp_audio;
28 rtp_session_t* _rtp_video;
29
30 uint32_t _frame_rate;
31
32 uint16_t _send_port, _recv_port;
33
34 int _tox_sock;
35
36 pthread_t _medialoop_id;
37 codec_state *cs;
38
39 Networking_Core* _networking;
40} phone_t;
41
42phone_t* initPhone(uint16_t _listen_port, uint16_t _send_port);
43int quitPhone(phone_t* _phone);
44
45/* My recv functions */
46int rtp_handlepacket ( void* _object, tox_IP_Port ip_port, uint8_t* data, uint32_t length );
47int msi_handlepacket ( void* _object, tox_IP_Port ip_port, uint8_t* data, uint32_t length );
48
49/* This is basically representation of networking_poll of toxcore */
50void* phone_receivepacket ( void* _phone );
51
52/* Phones main loop */
53void* phone_poll ( void* _phone );
54
55pthread_t phone_startmain_loop(phone_t* _phone);
56pthread_t phone_startmedia_loop ( phone_t* _phone );
57
58/* Thread handlers */
59void* phone_handle_receive_callback ( void* _p );
60void* phone_handle_media_transport_poll ( void* _hmtc_args_p );
61
62#endif /* _PHONE_H_ */
diff --git a/toxmsi/toxmedia.c b/toxmsi/toxmedia.c
deleted file mode 100644
index 4c9f5261..00000000
--- a/toxmsi/toxmedia.c
+++ /dev/null
@@ -1,825 +0,0 @@
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/toxmsi/toxmedia.h b/toxmsi/toxmedia.h
deleted file mode 100644
index 8734b173..00000000
--- a/toxmsi/toxmedia.h
+++ /dev/null
@@ -1,168 +0,0 @@
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#if defined(_WIN32) || defined(__WIN32__) || defined (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/toxmsi/toxmsi.c b/toxmsi/toxmsi.c
deleted file mode 100644
index 38af28fb..00000000
--- a/toxmsi/toxmsi.c
+++ /dev/null
@@ -1,835 +0,0 @@
1
2#ifdef HAVE_CONFIG_H
3#include "config.h"
4#endif /* HAVE_CONFIG_H */
5
6#define _BSD_SOURCE
7
8#include "toxmsi.h"
9#include "toxmsi_event.h"
10#include "toxmsi_message.h"
11#include "../toxrtp/toxrtp_helper.h"
12#include "../toxcore/network.h"
13
14#include <assert.h>
15#include <unistd.h>
16#include <string.h>
17
18#define same(x, y) strcmp((const char*) x, (const char*) y) == 0
19
20typedef enum {
21 error_deadcall = 1, /* has call id but it's from old call */
22 error_id_mismatch, /* non-existing call */
23
24 error_no_callid, /* not having call id */
25 error_no_call, /* no call in session */
26
27 error_busy
28} msi_error_t; /* Error codes */
29
30static inline const uint8_t *stringify_error(msi_error_t _error_code)
31{
32 static const uint8_t* strings[] =
33 {
34 (uint8_t*)"",
35 (uint8_t*)"Using dead call",
36 (uint8_t*)"Call id not set to any call",
37 (uint8_t*)"Call id not available",
38 (uint8_t*)"No active call in session",
39 (uint8_t*)"Callee busy"
40 };
41
42 return strings[_error_code];
43}
44
45static inline const uint8_t *stringify_error_code(msi_error_t _error_code)
46{
47 static const uint8_t* strings[] =
48 {
49 (uint8_t*)"",
50 (uint8_t*)"1",
51 (uint8_t*)"2",
52 (uint8_t*)"3",
53 (uint8_t*)"4",
54 (uint8_t*)"5"
55 };
56
57 return strings[_error_code];
58}
59
60/* ******************* */
61/* --------- GLOBAL FUNCTIONS USED BY THIS FILE --------- */
62
63/* CALLBACKS */
64/*int (*msi_send_message_callback) ( int, uint8_t*, uint32_t ) = NULL;*/
65int ( *msi_send_message_callback ) ( void* _core_handler, tox_IP_Port, uint8_t*, uint32_t ) = NULL;
66int ( *msi_recv_message_callback ) ( tox_IP_Port*, uint8_t*, uint32_t* ) = NULL;
67
68MCBTYPE ( *msi_recv_invite_callback ) ( MCBARGS ) = NULL;
69MCBTYPE ( *msi_start_call_callback ) ( MCBARGS ) = NULL;
70MCBTYPE ( *msi_reject_call_callback ) ( MCBARGS ) = NULL;
71MCBTYPE ( *msi_cancel_call_callback ) ( MCBARGS ) = NULL;
72MCBTYPE ( *msi_end_call_callback ) ( MCBARGS ) = NULL;
73
74MCBTYPE ( *msi_ringing_callback ) ( MCBARGS ) = NULL;
75MCBTYPE ( *msi_starting_callback ) ( MCBARGS ) = NULL;
76MCBTYPE ( *msi_ending_callback ) ( MCBARGS ) = NULL;
77MCBTYPE ( *msi_error_callback ) ( MCBARGS ) = NULL;
78
79MCBTYPE ( *msi_timeout_callback ) ( MCBARGS ) = NULL;
80/* End of CALLBACKS */
81
82/*------------------------*/
83/*------------------------*/
84/*------------------------*/
85/*------------------------*/
86/*------------------------*/
87/*------------------------*/
88/*------------------------*/
89/*------------------------*/
90/*------------------------*/
91/*------------------------*/
92/*------------------------*/
93/*------------------------*/
94
95/* REGISTER CALLBACKS */
96/*void msi_register_callback_send(int (*callback) ( int, uint8_t*, uint32_t ) )*/
97void msi_register_callback_send ( int ( *callback ) ( void* _core_handler, tox_IP_Port, uint8_t*, uint32_t ) )
98{
99 msi_send_message_callback = callback;
100}
101
102void msi_register_callback_recv ( int ( *callback ) ( tox_IP_Port*, uint8_t*, uint32_t* ) )
103{
104 msi_recv_message_callback = callback;
105}
106
107/* Function to be called when received invite.
108 * This callback is all about what you do with it.
109 * Everything else is done internally.
110 */
111void msi_register_callback_recv_invite ( MCALLBACK )
112{
113 msi_recv_invite_callback = callback;
114}
115
116/* Function to be called when the call is started
117 * This callback is all about what you do with it.
118 * Everything else is done internally.
119 */
120void msi_register_callback_call_started ( MCALLBACK )
121{
122 msi_start_call_callback = callback;
123}
124
125/* Function to be called when call is rejected
126 * This callback is all about what you do with it.
127 * Everything else is done internally.
128 */
129void msi_register_callback_call_rejected ( MCALLBACK )
130{
131 msi_reject_call_callback = callback;
132}
133
134/* Function to be called when call is canceled
135 * This callback is all about what you do with it.
136 * Everything else is done internally.
137 */
138void msi_register_callback_call_canceled ( MCALLBACK )
139{
140 msi_cancel_call_callback = callback;
141}
142
143void msi_register_callback_call_ended ( MCALLBACK )
144{
145 msi_end_call_callback = callback;
146}
147
148
149/* Functions to be called when gotten x response */
150
151void msi_register_callback_recv_ringing ( MCALLBACK )
152{
153 msi_ringing_callback = callback;
154}
155void msi_register_callback_recv_starting ( MCALLBACK )
156{
157 msi_starting_callback = callback;
158}
159void msi_register_callback_recv_ending ( MCALLBACK )
160{
161 msi_ending_callback = callback;
162}
163
164void msi_register_callback_recv_error ( MCALLBACK )
165{
166 msi_error_callback = callback;
167}
168
169/* Timeout */
170void msi_register_callback_requ_timeout ( MCALLBACK )
171{
172 msi_timeout_callback = callback;
173}
174/* END REGISTERING */
175
176/*------------------------*/
177/*------------------------*/
178/*------------------------*/
179/*------------------------*/
180/*------------------------*/
181/*------------------------*/
182/*------------------------*/
183/*------------------------*/
184/*------------------------*/
185/*------------------------*/
186/*------------------------*/
187/*------------------------*/
188
189/* Function for receiving and parsing a message that will be used internally */
190
191msi_msg_t* receive_message ( msi_session_t* _session )
192{
193 assert(_session);
194
195
196 msi_msg_t* _retu = _session->_oldest_msg;
197
198 pthread_mutex_lock ( &_session->_mutex );
199
200 if ( _retu )
201 _session->_oldest_msg = _retu->_next;
202
203 if ( !_session->_oldest_msg )
204 _session->_last_msg = NULL;
205
206 pthread_mutex_unlock ( &_session->_mutex );
207
208 return _retu;
209}
210
211void msi_store_msg ( msi_session_t* _session, msi_msg_t* _msg )
212{
213 assert(_session);
214 assert(_msg);
215
216 pthread_mutex_lock ( &_session->_mutex );
217
218 if ( _session->_last_msg ) {
219 _session->_last_msg->_next = _msg;
220 _session->_last_msg = _msg;
221 } else {
222 _session->_last_msg = _session->_oldest_msg = _msg;
223 }
224
225 pthread_mutex_unlock ( &_session->_mutex );
226}
227
228int msi_send_msg ( msi_session_t* _session, msi_msg_t* _msg )
229{
230 int _status;
231
232 if ( !_session->_call ) /* Which should never happen */
233 return FAILURE;
234
235 msi_msg_set_call_id ( _msg, _session->_call->_id );
236
237 uint8_t _msg_string_final [MSI_MAXMSG_SIZE];
238 t_memset ( _msg_string_final, '\0', MSI_MAXMSG_SIZE );
239
240 _msg_string_final[0] = 69;
241
242 uint8_t* _msg_string = msi_msg_to_string ( _msg );
243
244 size_t _lenght = t_memlen ( _msg_string );
245
246 memcpy ( _msg_string_final + 1, _msg_string, _lenght );
247
248 _lenght += 1;
249
250 _status = ( *msi_send_message_callback ) ( _session->_core_handler, _session->_friend_id, _msg_string_final, _lenght );
251
252 free ( _msg_string );
253
254 return _status;
255}
256
257/* Random stuff */
258void flush_peer_type ( msi_session_t* _session, msi_msg_t* _msg, int _peer_id )
259{
260 if ( _msg->_call_type ) {
261 if ( strcmp ( ( const char* ) _msg->_call_type->_header_value, CT_AUDIO_HEADER_VALUE ) == 0 ) {
262 _session->_call->_type_peer[_peer_id] = type_audio;
263
264 } else if ( strcmp ( ( const char* ) _msg->_call_type->_header_value, CT_VIDEO_HEADER_VALUE ) == 0 ) {
265 _session->_call->_type_peer[_peer_id] = type_video;
266 } else {} /* Error */
267 } else {} /* Error */
268}
269
270int has_call_error ( msi_session_t* _session, msi_msg_t* _msg )
271{
272 msi_msg_t* _msg_error = msi_msg_new ( TYPE_RESPONSE, stringify_response ( _error ) );
273
274 if ( !_msg->_call_id ) {
275 msi_msg_set_reason(_msg_error, stringify_error_code(error_no_callid) );
276
277 } else if ( !_session->_call ) {
278 msi_msg_set_reason(_msg_error, stringify_error_code(error_no_call) );
279
280 } else if ( strcmp((const char*)_session->_call->_id, (const char*)_msg->_call_id->_header_value ) != 0 ) {
281 msi_msg_set_reason(_msg_error, stringify_error_code(error_id_mismatch) );
282 }
283
284 if ( _msg_error->_reason ) {
285 msi_send_msg ( _session, _msg_error );
286 msi_free_msg ( _msg_error );
287 return SUCCESS;
288 }
289
290 msi_free_msg ( _msg_error );
291 return FAILURE;
292}
293
294/* --------- END OF GLOBAL FUNCTIONS USED BY THIS FILE --------- */
295
296/*------------------------*/
297/*------------------------*/
298/*------------------------*/
299/*------------------------*/
300/*------------------------*/
301/*------------------------*/
302/*------------------------*/
303/*------------------------*/
304/*------------------------*/
305/*------------------------*/
306/*------------------------*/
307/*------------------------*/
308
309msi_session_t* msi_init_session ( void* _core_handler, const uint8_t* _user_agent )
310{
311 assert(_core_handler);
312 assert(_user_agent);
313
314 msi_session_t* _session = calloc ( sizeof ( msi_session_t ), 1 );
315 assert(_session);
316
317 _session->_oldest_msg = _session->_last_msg = NULL;
318 _session->_core_handler = _core_handler;
319
320 _session->_user_agent = t_strallcpy ( _user_agent );
321 _session->_agent_handler = NULL;
322
323 _session->_key = 0;
324 _session->_call = NULL;
325
326 _session->_frequ = 10000; /* default value? */
327 _session->_call_timeout = 30000; /* default value? */
328
329 /* Use the same frequency */
330 _session->_event_handler = init_event_poll ( _session->_frequ );
331
332 pthread_mutex_init ( &_session->_mutex, NULL );
333
334 return _session;
335}
336
337int msi_terminate_session ( msi_session_t* _session )
338{
339 assert(_session);
340
341 int _status = 0;
342
343 terminate_event_poll ( _session->_event_handler );
344 free ( _session );
345 /* TODO: terminate the rest of the session */
346
347 pthread_mutex_destroy ( &_session->_mutex );
348
349 return _status;
350}
351
352msi_call_t* msi_init_call ( msi_session_t* _session, int _peers, uint32_t _timeoutms )
353{
354 assert(_session);
355 assert(_peers);
356
357 msi_call_t* _call = calloc ( sizeof ( msi_call_t ), 1 );
358 _call->_type_peer = calloc ( sizeof ( call_type ), _peers );
359
360 assert(_call);
361 assert(_call->_type_peer);
362
363 _call->_participants = _peers;
364 _call->_key = _session->_key;
365 _call->_timeoutst = _timeoutms;
366 _call->_outgoing_timer_id = 0;
367
368 return _call;
369}
370
371int msi_terminate_call ( msi_session_t* _session )
372{
373 assert(_session);
374
375 if ( _session->_call->_type_peer )
376 free ( _session->_call->_type_peer );
377
378 cancel_timer_event(_session->_event_handler, _session->_call->_outgoing_timer_id);
379
380 free ( _session->_call );
381
382 _session->_call = NULL;
383
384 return SUCCESS;
385}
386/*------------------------*/
387/*------------------------*/
388/*------------------------*/
389/*------------------------*/
390/*------------------------*/
391/*------------------------*/
392/*------------------------*/
393/*------------------------*/
394/*------------------------*/
395/*------------------------*/
396/*------------------------*/
397/*------------------------*/
398
399/* STATE HANDLERS */
400
401/* REQUESTS */
402int msi_handle_recv_invite ( msi_session_t* _session, msi_msg_t* _msg )
403{
404 assert(_session);
405
406 if ( _session->_call ) {
407 msi_msg_t* _msg_error = msi_msg_new ( TYPE_RESPONSE, stringify_response ( _error ) );
408 msi_msg_set_reason(_msg_error, stringify_error_code(error_busy));
409 msi_send_msg(_session, _msg_error);
410 msi_free_msg(_msg_error);
411
412 return 0;
413 }
414 if ( !_msg->_call_id ) {
415 msi_msg_t* _msg_error = msi_msg_new ( TYPE_RESPONSE, stringify_response ( _error ) );
416 msi_msg_set_reason(_msg_error, stringify_error_code(error_no_callid));
417 msi_send_msg(_session, _msg_error);
418 msi_free_msg(_msg_error);
419 return 0;
420 }
421
422 _session->_call = msi_init_call ( _session, 1, _session->_call_timeout );
423 t_memcpy(_session->_call->_id, _msg->_call_id->_header_value, _CALL_ID_LEN);
424 _session->_call->_state = call_starting;
425
426 flush_peer_type ( _session, _msg, 0 );
427
428 msi_msg_t* _msg_ringing = msi_msg_new ( TYPE_RESPONSE, stringify_response ( _ringing ) );
429 msi_send_msg ( _session, _msg_ringing );
430 msi_free_msg ( _msg_ringing );
431
432 throw_event ( _session->_event_handler, msi_recv_invite_callback, _session );
433 return 1;
434}
435int msi_handle_recv_start ( msi_session_t* _session, msi_msg_t* _msg )
436{
437 assert(_session);
438
439 if ( has_call_error(_session, _msg) == 0 )
440 return 0;
441
442 _session->_call->_state = call_active;
443
444 flush_peer_type ( _session, _msg, 0 );
445
446 throw_event ( _session->_event_handler, msi_start_call_callback, _session );
447 return 1;
448}
449int msi_handle_recv_reject ( msi_session_t* _session, msi_msg_t* _msg )
450{
451 assert(_session);
452
453 if ( has_call_error(_session, _msg) == 0 )
454 return 0;
455
456 msi_msg_t* _msg_end = msi_msg_new ( TYPE_REQUEST, stringify_request ( _end ) );
457 msi_send_msg ( _session, _msg_end );
458 msi_free_msg ( _msg_end );
459
460 throw_event ( _session->_event_handler, msi_reject_call_callback, _session );
461
462 return 1;
463}
464int msi_handle_recv_cancel ( msi_session_t* _session, msi_msg_t* _msg )
465{
466 assert(_session);
467
468 if ( has_call_error(_session, _msg) == 0 )
469 return 0;
470
471 msi_msg_t* _msg_ending = msi_msg_new ( TYPE_RESPONSE, stringify_response ( _ending ) );
472 msi_send_msg ( _session, _msg_ending );
473 msi_free_msg ( _msg_ending );
474
475 msi_terminate_call ( _session );
476
477 throw_event ( _session->_event_handler, msi_cancel_call_callback, _session );
478
479 return 1;
480}
481int msi_handle_recv_end ( msi_session_t* _session, msi_msg_t* _msg )
482{
483 assert(_session);
484
485 if ( has_call_error(_session, _msg) == 0 )
486 return 0;
487
488 msi_msg_t* _msg_ending = msi_msg_new ( TYPE_RESPONSE, stringify_response ( _ending ) );
489 msi_send_msg ( _session, _msg_ending );
490 msi_free_msg ( _msg_ending );
491
492 msi_terminate_call ( _session );
493
494 throw_event ( _session->_event_handler, msi_end_call_callback, _session );
495
496 return 1;
497}
498/*--------*/
499
500/* RESPONSES */
501int msi_handle_recv_ringing ( msi_session_t* _session, msi_msg_t* _msg )
502{
503 assert(_session);
504
505 if ( has_call_error(_session, _msg) == 0 )
506 return 0;
507
508 throw_event ( _session->_event_handler, msi_ringing_callback, _session );
509
510 return 1;
511}
512int msi_handle_recv_starting ( msi_session_t* _session, msi_msg_t* _msg )
513{
514 assert(_session);
515
516 if ( has_call_error(_session, _msg) == 0 )
517 return 0;
518
519 _session->_call->_state = call_active;
520
521 msi_msg_t* _msg_start = msi_msg_new ( TYPE_REQUEST, stringify_request ( _start ) );
522 msi_send_msg ( _session, _msg_start );
523 msi_free_msg ( _msg_start );
524
525 flush_peer_type ( _session, _msg, 0 );
526
527 throw_event ( _session->_event_handler, msi_starting_callback, _session );
528 cancel_timer_event(_session->_event_handler, _session->_call->_outgoing_timer_id);
529 _session->_call->_outgoing_timer_id = 0;
530
531 return 1;
532}
533int msi_handle_recv_ending ( msi_session_t* _session, msi_msg_t* _msg )
534{
535 assert(_session);
536
537 if ( has_call_error(_session, _msg) == 0 )
538 return 0;
539
540 msi_terminate_call ( _session );
541 throw_event ( _session->_event_handler, msi_ending_callback, _session );
542
543 return 1;
544}
545int msi_handle_recv_error ( msi_session_t* _session, msi_msg_t* _msg )
546{
547 assert(_session);
548 assert(_session->_call);
549
550 /* Handle error accordingly */
551 if ( _msg->_reason ) {
552 _session->_last_error_id = atoi((const char*)_msg->_reason->_header_value);
553 _session->_last_error_str = stringify_error(_session->_last_error_id);
554 }
555
556 msi_terminate_call(_session);
557
558 throw_event ( _session->_event_handler, msi_error_callback, _session );
559
560 return 1;
561}
562/* ------------------ */
563
564MCBTYPE msi_handle_timeout (void* _arg)
565{
566 msi_session_t* _session = _arg;
567 msi_terminate_call(_session);
568
569 (*msi_timeout_callback) (_arg);
570 (*msi_ending_callback) (_arg);
571
572}
573
574/*------------------------*/
575/*------------------------*/
576/*------------------------*/
577/*------------------------*/
578/*------------------------*/
579/*------------------------*/
580/*------------------------*/
581/*------------------------*/
582/*------------------------*/
583/*------------------------*/
584/*------------------------*/
585/*------------------------*/
586
587int msi_invite ( msi_session_t* _session, call_type _call_type, uint32_t _timeoutms )
588{
589 assert(_session);
590
591 if ( !msi_send_message_callback )
592 return 0;
593
594 msi_msg_t* _msg_invite = msi_msg_new ( TYPE_REQUEST, stringify_request ( _invite ) );
595
596 _session->_call = msi_init_call ( _session, 1, _timeoutms ); /* Just one for now */
597 msi_genterate_call_id(_session->_call->_id, _CALL_ID_LEN);
598 _session->_call->_type_local = _call_type;
599 /* Do whatever with message */
600
601 if ( _call_type == type_audio ) {
602 msi_msg_set_call_type ( _msg_invite, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE );
603 } else {
604 msi_msg_set_call_type ( _msg_invite, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE );
605 }
606
607 msi_send_msg ( _session, _msg_invite );
608 msi_free_msg ( _msg_invite );
609
610 _session->_call->_state = call_inviting;
611
612 _session->_call->_outgoing_timer_id = throw_timer_event(_session->_event_handler, msi_handle_timeout, _session, _timeoutms );
613
614 return 1;
615}
616int msi_hangup ( msi_session_t* _session )
617{
618 assert(_session);
619
620 if ( !_session->_call || ( !msi_send_message_callback && _session->_call->_state != call_active ) )
621 return 0;
622
623 msi_msg_t* _msg_ending = msi_msg_new ( TYPE_REQUEST, stringify_request ( _end ) );
624 msi_send_msg ( _session, _msg_ending );
625 msi_free_msg ( _msg_ending );
626
627 return 1;
628}
629
630
631int msi_answer ( msi_session_t* _session, call_type _call_type )
632{
633 assert(_session);
634
635 if ( !msi_send_message_callback || !_session->_call )
636 return 0;
637
638 msi_msg_t* _msg_starting = msi_msg_new ( TYPE_RESPONSE, stringify_response ( _starting ) );
639 _session->_call->_type_local = _call_type;
640
641 if ( _call_type == type_audio ) {
642 msi_msg_set_call_type ( _msg_starting, ( const uint8_t* ) CT_AUDIO_HEADER_VALUE );
643 } else {
644 msi_msg_set_call_type ( _msg_starting, ( const uint8_t* ) CT_VIDEO_HEADER_VALUE );
645 }
646
647 msi_send_msg ( _session, _msg_starting );
648 msi_free_msg ( _msg_starting );
649
650 _session->_call->_state = call_active;
651 return 1;
652}
653int msi_cancel ( msi_session_t* _session )
654{
655 assert(_session);
656
657 if ( !_session->_call || !msi_send_message_callback )
658 return 0;
659
660 msi_msg_t* _msg_cancel = msi_msg_new ( TYPE_REQUEST, stringify_request ( _cancel ) );
661 msi_send_msg ( _session, _msg_cancel );
662 msi_free_msg ( _msg_cancel );
663
664
665
666 return 1;
667}
668int msi_reject ( msi_session_t* _session )
669{
670 assert(_session);
671
672 if ( !_session->_call || !msi_send_message_callback )
673 return 0;
674
675 msi_msg_t* _msg_reject = msi_msg_new ( TYPE_REQUEST, stringify_request ( _reject ) );
676 msi_send_msg ( _session, _msg_reject );
677 msi_free_msg ( _msg_reject );
678
679 return 1;
680}
681
682/*------------------------*/
683/*------------------------*/
684/*------------------------*/
685/*------------------------*/
686/*------------------------*/
687/*------------------------*/
688/*------------------------*/
689/*------------------------*/
690/*------------------------*/
691/*------------------------*/
692/*------------------------*/
693/*------------------------*/
694
695/* OUR MAIN POOL FUNCTION */
696/*
697 * Forks it self to other thread and then handles the session initiation.
698 *
699 * BASIC call flow:
700 *
701 * ALICE BOB
702 * | invite --> |
703 * | |
704 * | <-- ringing |
705 * | |
706 * | <-- starting |
707 * | |
708 * | start --> |
709 * | |
710 * | <-- MEDIA TRANS --> |
711 * | |
712 * | end --> |
713 * | |
714 * | <-- ending |
715 *
716 * Alice calls Bob by sending invite packet.
717 * Bob recvs the packet and sends an ringing packet;
718 * which notifies Alice that her invite is acknowledged.
719 * Ringing screen shown on both sides.
720 * Bob accepts the invite for a call by sending starting packet.
721 * Alice recvs the starting packet and sends the started packet to
722 * inform Bob that she recved the starting packet.
723 * Now the media transmission is established ( i.e. RTP transmission ).
724 * Alice hangs up and sends end packet.
725 * Bob recves the end packet and sends ending packet
726 * as the acknowledgement that the call is ending.
727 *
728 *
729 */
730
731
732/*
733 * Needs a bit more work on the protocol
734 */
735void* msi_poll_stack ( void* _session_p )
736{
737 msi_session_t* _session = ( msi_session_t* ) _session_p;
738 msi_msg_t* _msg = NULL;
739
740 uint32_t* _frequ = &_session->_frequ;
741 while ( _session ) {
742
743 /* At this point it's already parsed */
744 _msg = receive_message ( _session );
745
746 if ( _msg ) {
747
748 if ( _msg->_request ) { /* Handle request */
749
750 const uint8_t* _request_value = _msg->_request->_header_value;
751
752 if ( same ( _request_value, stringify_request ( _invite ) ) ) {
753 msi_handle_recv_invite ( _session, _msg );
754
755 } else if ( same ( _request_value, stringify_request ( _start ) ) ) {
756 msi_handle_recv_start ( _session, _msg );
757
758 } else if ( same ( _request_value, stringify_request ( _cancel ) ) ) {
759 msi_handle_recv_cancel ( _session, _msg );
760
761 } else if ( same ( _request_value, stringify_request ( _reject ) ) ) {
762 msi_handle_recv_reject ( _session, _msg );
763
764 } else if ( same ( _request_value, stringify_request ( _end ) ) ) {
765 msi_handle_recv_end ( _session, _msg );
766 }
767
768 } else if ( _msg->_response ) { /* Handle response */
769
770 const uint8_t* _response_value = _msg->_response->_header_value;
771
772 if ( same ( _response_value, stringify_response ( _ringing ) ) ) {
773 msi_handle_recv_ringing ( _session, _msg );
774
775 } else if ( same ( _response_value, stringify_response ( _starting ) ) ) {
776 msi_handle_recv_starting ( _session, _msg );
777
778 } else if ( same ( _response_value, stringify_response ( _ending ) ) ) {
779 msi_handle_recv_ending ( _session, _msg );
780
781 } else if ( same ( _response_value, stringify_response ( _error ) ) ) {
782 msi_handle_recv_error ( _session, _msg );
783 }
784
785 }
786
787 msi_free_msg ( _msg );
788
789 }
790 usleep ( *_frequ );
791 }
792
793 return NULL;
794}
795
796/*------------------------*/
797/*------------------------*/
798/*------------------------*/
799/*------------------------*/
800/*------------------------*/
801/*------------------------*/
802/*------------------------*/
803/*------------------------*/
804/*------------------------*/
805/*------------------------*/
806/*------------------------*/
807/*------------------------*/
808
809/* Easy way to start the poll */
810
811pthread_t msi_start_main_loop ( msi_session_t* _session, uint32_t _frequms )
812{
813 assert(_session);
814
815 int _status;
816 pthread_t _thread_id;
817
818
819 _session->_frequ = _frequms * 1000;
820
821 _status = pthread_create ( &_thread_id, NULL, msi_poll_stack, _session );
822
823 if ( _status < 0 ) {
824 printf ( "Error while starting main loop: %d, %s\n", errno, strerror ( errno ) );
825 return _status;
826 }
827
828 _status = pthread_detach ( _thread_id );
829
830 if ( _status < 0 ) {
831 printf ( "Error while starting main loop: %d, %s\n", errno, strerror ( errno ) );
832 }
833
834 return _thread_id;
835}
diff --git a/toxmsi/toxmsi.h b/toxmsi/toxmsi.h
deleted file mode 100644
index d8985c64..00000000
--- a/toxmsi/toxmsi.h
+++ /dev/null
@@ -1,145 +0,0 @@
1/* msi_initiation.h
2*
3* Has function for session initiation along with session description.
4* It follows the Tox API ( http://wiki.tox.im/index.php/Messaging_Protocol ). !Red!
5*
6*
7* Copyright (C) 2013 Tox project All Rights Reserved.
8*
9* This file is part of Tox.
10*
11* Tox is free software: you can redistribute it and/or modify
12* it under the terms of the GNU General Public License as published by
13* the Free Software Foundation, either version 3 of the License, or
14* (at your option) any later version.
15*
16* Tox is distributed in the hope that it will be useful,
17* but WITHOUT ANY WARRANTY; without even the implied warranty of
18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19* GNU General Public License for more details.
20*
21* You should have received a copy of the GNU General Public License
22* along with Tox. If not, see <http://www.gnu.org/licenses/>.
23*
24*/
25
26
27#ifndef _MSI_IMPL_H_
28#define _MSI_IMPL_H_
29
30#include <inttypes.h>
31#include "tox.h"
32#include <pthread.h>
33
34#define MCBTYPE void
35#define MCBARGS void* _arg
36#define MCALLBACK MCBTYPE(*callback)(void* _arg)
37
38#define MSI_PACKET 69
39
40#define CT_AUDIO_HEADER_VALUE "AUDIO"
41#define CT_VIDEO_HEADER_VALUE "VIDEO"
42
43/* define size for call_id */
44#define _CALL_ID_LEN 12
45
46typedef enum {
47 type_audio = 1,
48 type_video,
49} call_type;
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} call_state;
58
59typedef int crypto_key;
60
61typedef struct msi_call_s { /* Call info structure */
62 call_state _state;
63 call_type _type_local;
64 call_type* _type_peer; /* Support for conference starts with this */
65 uint8_t _id[_CALL_ID_LEN]; /* Random value identifying the call */
66 crypto_key _key; /* What is the type again? */
67 uint16_t _participants; /* Number of participants */
68 uint32_t _timeoutst; /* Time of the timeout for some action to end; 0 if infinite */
69 int _outgoing_timer_id; /* Timer id */
70
71} msi_call_t;
72
73typedef struct msi_session_s {
74 pthread_mutex_t _mutex;
75
76 crypto_key _key; /* The key */
77
78 /* Call information/handler. ( Maybe only information? ) */
79 msi_call_t* _call;
80
81 /* Storage for message receiving */
82 struct msi_msg_s* _oldest_msg;
83 struct msi_msg_s* _last_msg; /* tail */
84
85 /*int _friend_id;*/
86 tox_IP_Port _friend_id;
87
88 int _last_error_id; /* Determine the last error */
89 const uint8_t* _last_error_str;
90
91 const uint8_t* _user_agent;
92
93 void* _agent_handler; /* Pointer to an object that is handling msi */
94 void* _core_handler; /* Pointer to networking core or to anything that
95 * should handle interaction with core/networking
96 */
97 void* _event_handler; /* Pointer to an object which handles the events */
98
99 uint32_t _frequ;
100 uint32_t _call_timeout; /* Time of the timeout for some action to end; 0 if infinite */
101} msi_session_t;
102
103
104
105msi_session_t* msi_init_session ( void* _core_handler, const uint8_t* _user_agent );
106int msi_terminate_session ( msi_session_t* _session );
107
108pthread_t msi_start_main_loop ( msi_session_t* _session, uint32_t _frequms );
109
110/* Registering callbacks */
111
112/*void msi_register_callback_send(int (*callback) ( int, uint8_t*, uint32_t ) );*/
113void msi_register_callback_send ( int ( *callback ) ( void* _core_handler, tox_IP_Port, uint8_t*, uint32_t ) );
114
115/* Callbacks that handle the states */
116void msi_register_callback_call_started ( MCALLBACK );
117void msi_register_callback_call_canceled ( MCALLBACK );
118void msi_register_callback_call_rejected ( MCALLBACK );
119void msi_register_callback_call_ended ( MCALLBACK );
120
121void msi_register_callback_recv_invite ( MCALLBACK );
122void msi_register_callback_recv_ringing ( MCALLBACK );
123void msi_register_callback_recv_starting ( MCALLBACK );
124void msi_register_callback_recv_ending ( MCALLBACK );
125void msi_register_callback_recv_error ( MCALLBACK );
126
127void msi_register_callback_requ_timeout ( MCALLBACK );
128/* -------- */
129
130
131/* Function handling receiving from core */
132/*static int msi_handlepacket ( tox_IP_Port ip_port, uint8_t* _data, uint16_t _lenght ); */
133
134/* functions describing the usage of msi */
135int msi_invite ( msi_session_t* _session, call_type _call_type, uint32_t _timeoutms );
136int msi_hangup ( msi_session_t* _session );
137
138int msi_answer ( msi_session_t* _session, call_type _call_type );
139int msi_cancel ( msi_session_t* _session );
140int msi_reject ( msi_session_t* _session );
141
142int msi_send_msg ( msi_session_t* _session, struct msi_msg_s* _msg );
143void msi_store_msg ( msi_session_t* _session, struct msi_msg_s* _msg );
144
145#endif /* _MSI_IMPL_H_ */
diff --git a/toxmsi/toxmsi_event.c b/toxmsi/toxmsi_event.c
deleted file mode 100644
index d8c0d269..00000000
--- a/toxmsi/toxmsi_event.c
+++ /dev/null
@@ -1,214 +0,0 @@
1
2#ifdef HAVE_CONFIG_H
3#include "config.h"
4#endif /* HAVE_CONFIG_H */
5
6#include <stdlib.h>
7
8#include "toxmsi_event.h"
9
10#include "../toxrtp/toxrtp_helper.h"
11#include <assert.h>
12#include <stdlib.h>
13#include <unistd.h>
14
15static int _unique_id = 1;
16
17/* clear events */
18void clear_events (event_container_t** _event_container, size_t* _counter)
19{
20 assert( *_event_container );
21
22 free(*_event_container);
23 *_event_container = NULL;
24
25 *_counter = 0;
26}
27
28int pop_id ( event_container_t** _event_container, size_t* _counter, int _id )
29{
30 if ( !*_event_container || !*_counter || !_id )
31 return FAILURE;
32
33 event_container_t* _it = *_event_container;
34 int i;
35
36 for ( i = *_counter; i > 0 ; -- i ){
37 if ( _it->_id == _id ) { /* Hit! */
38 break;
39 }
40 ++_it;
41 }
42
43 if ( i ) {
44 for ( ; i > 0; -- i ){ *_it = *(_it + 1); ++_it; }
45 -- (*_counter);
46 *_event_container = realloc(*_event_container, sizeof(event_container_t) * (*_counter)); /* resize */
47
48 if ( *_counter )
49 assert(*_event_container);
50
51 return SUCCESS;
52
53 } else {
54 assert(i);
55 return FAILURE;
56 }
57}
58
59/* main poll for event execution */
60void* event_poll( void* _event_handler_p )
61{
62 event_handler_t* _event_handler = _event_handler_p;
63 uint32_t* _frequms = &_event_handler->_frequms;
64
65
66 while ( _event_handler->_running )
67 {
68
69 if ( _event_handler->_events_count ){
70 pthread_mutex_lock(&_event_handler->_mutex);
71
72 int i;
73 for ( i = 0; i < _event_handler->_events_count; i ++ ){
74 _event_handler->_events[i]._event(_event_handler->_events[i]._event_args);
75
76 }
77 clear_events(&_event_handler->_events, &_event_handler->_events_count);
78
79 pthread_mutex_unlock(&_event_handler->_mutex);
80 }
81
82 if ( _event_handler->_timed_events_count ){
83 pthread_mutex_lock(&_event_handler->_mutex);
84
85 uint32_t _time = t_time();
86
87 if ( _event_handler->_timed_events[0]._timeout < _time ) {
88 _event_handler->_timed_events[0]._event(_event_handler->_timed_events[0]._event_args);
89
90 pop_id(&_event_handler->_timed_events,
91 &_event_handler->_timed_events_count,
92 _event_handler->_timed_events[0]._id);
93 }
94 pthread_mutex_unlock(&_event_handler->_mutex);
95 }
96
97
98 usleep(*_frequms);
99 }
100
101 _event_handler->_running = -1;
102 pthread_exit(NULL);
103}
104
105void push_event ( event_container_t** _container, size_t* _counter, event_t _func, event_arg_t _arg )
106{
107 (*_counter)++;
108 (*_container) = realloc((*_container), sizeof(event_container_t) * (*_counter));
109 assert((*_container) != NULL);
110
111 (*_container[*_counter - 1])._event = _func;
112 (*_container[*_counter - 1])._event_args = _arg;
113 (*_container[*_counter - 1])._timeout = 0;
114 (*_container[*_counter - 1])._id = 0;
115}
116
117void throw_event( void* _event_handler_p, event_t _func, event_arg_t _arg )
118{
119 if ( !_func )
120 return;
121
122 event_handler_t* _event_handler = _event_handler_p;
123
124 pthread_mutex_lock(&_event_handler->_mutex);
125
126 push_event(&_event_handler->_events, &_event_handler->_events_count, _func, _arg);
127
128 pthread_mutex_unlock(&_event_handler->_mutex);
129}
130
131int throw_timer_event ( void* _event_handler_p, event_t _func, event_arg_t _arg, uint32_t _timeout)
132{
133 if ( !_func )
134 return FAILURE;
135
136 event_handler_t* _event_handler = _event_handler_p;
137
138 pthread_mutex_lock(&_event_handler->_mutex);
139
140 push_event(&_event_handler->_timed_events, &_event_handler->_timed_events_count, _func, _arg);
141 size_t _counter = _event_handler->_timed_events_count;
142 _event_handler->_timed_events[_counter - 1]._timeout = _timeout + t_time();
143 _event_handler->_timed_events[_counter - 1]._id = _unique_id; ++_unique_id;
144
145
146 /* reorder */
147 if ( _counter > 1 ) {
148
149 int i = _counter - 1;
150 /* start from behind excluding last added member */
151 event_container_t* _it = &_event_handler->_timed_events[i - 1];
152
153 event_container_t _last_added = _event_handler->_timed_events[i];
154
155 for ( ; i > 0; --i ) {
156 if ( _it->_timeout > _timeout ){
157 *(_it + 1) = *_it;
158 *_it = _last_added; -- _it;
159 }
160 }
161
162 }
163
164 pthread_mutex_unlock(&_event_handler->_mutex);
165
166 return _event_handler->_timed_events[_counter - 1]._id;
167}
168int cancel_timer_event ( void* _event_handler_p, int _id )
169{
170 event_handler_t* _event_handler = _event_handler_p;
171 return pop_id(&_event_handler->_timed_events, &_event_handler->_timed_events_count, _id);
172}
173
174event_handler_t* init_event_poll (uint32_t _frequms)
175{
176 event_handler_t* _retu = calloc(sizeof(event_handler_t), 1);
177 assert(_retu);
178 /* Initialize basic events */
179 _retu->_events = NULL ;
180
181 /* Initialize timed events */
182 _retu->_timed_events = NULL;
183
184 _retu->_frequms = _frequms;
185 _retu->_running = 1;
186 pthread_mutex_init(&_retu->_mutex, NULL);
187
188 pthread_create(&_retu->_thread_id, NULL, event_poll, _retu);
189 int _rc = pthread_detach(_retu->_thread_id);
190 assert(_rc == 0);
191
192 return _retu;
193}
194
195int terminate_event_poll(event_handler_t* _handler)
196{
197 if ( !_handler )
198 return FAILURE;
199
200 _handler->_running = 0;
201 while (_handler->_running != -1); /* Wait for execution */
202
203 if (_handler->_events)
204 clear_events(&_handler->_events, &_handler->_events_count);
205 if (_handler->_events)
206 clear_events(&_handler->_timed_events, &_handler->_timed_events_count);
207
208 pthread_mutex_destroy( &_handler->_mutex );
209
210 free(_handler);
211
212 return SUCCESS;
213}
214
diff --git a/toxmsi/toxmsi_event.h b/toxmsi/toxmsi_event.h
deleted file mode 100644
index 032e4df5..00000000
--- a/toxmsi/toxmsi_event.h
+++ /dev/null
@@ -1,46 +0,0 @@
1#ifndef _MSI__EVENT_H_
2#define _MSI__EVENT_H_
3
4#include <stddef.h>
5#include <inttypes.h>
6#include <pthread.h>
7
8typedef void* event_arg_t;
9
10typedef void ( *event_t ) ( event_arg_t );
11typedef void ( *timed_event_t ) ( event_arg_t );
12
13typedef struct event_container_s {
14 event_t _event;
15 event_arg_t _event_args;
16 uint32_t _timeout;
17 long long _id;
18
19} event_container_t;
20
21typedef struct event_handler_s {
22 event_container_t* _events;
23 size_t _events_count;
24
25 event_container_t* _timed_events;
26 size_t _timed_events_count;
27
28 uint32_t _frequms;
29 int _running;
30
31 pthread_mutex_t _mutex;
32 pthread_t _thread_id;
33
34} event_handler_t;
35
36event_handler_t* init_event_poll ( uint32_t _frequms );
37int terminate_event_poll ( event_handler_t* _event_handler );
38
39void throw_event ( void* _event_handler_p, event_t _func, event_arg_t _arg );
40
41/* Not yet ready for use */
42int throw_timer_event ( void* _event_handler_p, event_t _func, event_arg_t _arg, uint32_t _timeout);
43int cancel_timer_event ( void* _event_handler_p, int _id );
44
45
46#endif /* _MSI__EVENT_H_ */
diff --git a/toxmsi/toxmsi_header.c b/toxmsi/toxmsi_header.c
deleted file mode 100644
index 448ebc7f..00000000
--- a/toxmsi/toxmsi_header.c
+++ /dev/null
@@ -1,181 +0,0 @@
1
2#ifdef HAVE_CONFIG_H
3#include "config.h"
4#endif /* HAVE_CONFIG_H */
5
6#include "toxmsi_message.h"
7#include <string.h>
8#include "../toxrtp/toxrtp_helper.h"
9#include <assert.h>
10#include "../toxcore/Lossless_UDP.h"
11
12#define ALLOC_ADD_DATA(_tempval, _hdrlist, _fielddef, _msgvar, _alloctype) \
13_tempval = msi_search_field(_hdrlist, (const uint8_t*)_fielddef); \
14if ( _tempval ){ \
15 _msgvar = calloc(sizeof(_alloctype), 1); \
16 assert(_msgvar); \
17 _msgvar->_header_value = _tempval; \
18}
19
20uint8_t* msi_search_field ( msi_header_t* _list, const uint8_t* _field )
21{
22 assert(_list);
23 assert(_field);
24
25 msi_header_t* _iterator;
26
27 for ( _iterator = _list;
28 _iterator && strcmp((const char*)_iterator->_header_field, (const char*)_field) != 0;
29 _iterator = _iterator->next );
30
31 if ( _iterator ){
32 return t_strallcpy(_iterator->_header_value);
33 } else return NULL;
34}
35
36int msi_parse_headers ( msi_msg_t* _msg )
37{
38 assert(_msg);
39
40 if ( !_msg->_headers )
41 return FAILURE;
42
43 msi_header_t* _list = _msg->_headers;
44 uint8_t* _field_current;
45
46 ALLOC_ADD_DATA(_field_current, _list, _CALL_ID_FIELD, _msg->_call_id, msi_header_call_id_t)
47 ALLOC_ADD_DATA(_field_current, _list, _VERSION_FIELD, _msg->_version, msi_header_version_t)
48 ALLOC_ADD_DATA(_field_current, _list, _REQUEST_FIELD, _msg->_request, msi_header_request_t)
49 ALLOC_ADD_DATA(_field_current, _list, _RESPONSE_FIELD, _msg->_response, msi_header_response_t)
50 ALLOC_ADD_DATA(_field_current, _list, _FRIENDID_FIELD, _msg->_friend_id, msi_header_friendid_t)
51 ALLOC_ADD_DATA(_field_current, _list, _CALLTYPE_FIELD, _msg->_call_type, msi_header_call_type_t)
52 ALLOC_ADD_DATA(_field_current, _list, _USERAGENT_FIELD, _msg->_user_agent, msi_header_user_agent_t)
53 ALLOC_ADD_DATA(_field_current, _list, _INFO_FIELD, _msg->_info, msi_header_info_t)
54 ALLOC_ADD_DATA(_field_current, _list, _REASON_FIELD, _msg->_reason, msi_header_reason_t)
55
56 /* Since we don't need the raw header anymore remove it */
57 msi_header_t* _temp;
58 while ( _list ){
59 _temp = _list->next;
60 free(_list->_header_field);
61 free(_list->_header_value);
62 free(_list);
63 _list = _temp;
64 }
65
66 _msg->_headers = NULL;
67
68 return SUCCESS;
69}
70
71/*
72 * If you find better way of parsing values let me know
73 */
74msi_header_t* msi_add_new_header ( uint8_t* _value )
75{
76 if ( !_value )
77 return NULL;
78
79 size_t _length = t_memlen(_value);
80
81 if ( !_length ) {
82 return NULL;
83 }
84
85 size_t _first_len = t_strfind(_value, (const uint8_t*)" ");
86 if ( !_first_len ){
87 return NULL;
88 }
89
90 size_t _second_len = (_length - _first_len);
91 if ( !_second_len ){
92 return NULL;
93 }
94
95
96 uint8_t* _identifier = calloc(sizeof (uint8_t), (_first_len + 1) );
97 uint8_t* _data = calloc(sizeof (uint8_t), (_second_len + 1) );
98
99 assert(_identifier);
100 assert(_data);
101
102
103 uint8_t* _p_it = _value;
104 size_t _num_it;
105
106 for ( _num_it = 0; *_p_it != ' '; _num_it++ ){
107 _identifier[_num_it] = *_p_it;
108 ++_p_it;
109 }
110 _identifier[_num_it] = '\0';
111 ++_p_it;
112
113
114 for ( _num_it = 0; *_p_it != '\r'; _num_it++ ){
115 _data[_num_it] = *_p_it;
116 ++_p_it;
117 }
118 _data[_num_it] = '\r';
119 _data[_num_it + 1] = '\0';
120
121 msi_header_t* _retu = calloc(sizeof(msi_header_t), 1);
122 assert(_retu);
123
124 _retu->_header_field = _identifier;
125 _retu->_header_value = _data;
126
127 _retu->next = NULL;
128
129 return _retu;
130}
131
132msi_header_t* msi_parse_raw_data ( const uint8_t* _data )
133{
134 assert(_data);
135
136 uint8_t* _header_string;
137
138 _header_string = (uint8_t*) strtok ((char*)_data, _RAW_TERMINATOR);
139
140 msi_header_t* _head = msi_add_new_header(_header_string);
141 msi_header_t* _it = _head;
142
143 while ( _header_string && _it ){
144
145 _header_string = (uint8_t*) strtok (NULL, _RAW_TERMINATOR);
146 _it->next = msi_add_new_header(_header_string);
147 if ( _it->next ){
148 _it = _it->next;
149 }
150 }
151
152 /* Iterate through list and remove all fault headers if any */
153
154 msi_header_t* _tmp = _it;
155
156 for ( _it = _head; _it; _it = _it->next ){
157
158 if ( !_it->_header_value || !_it->_header_field ) {
159 _tmp ->next = _it->next;
160
161 if ( _it->_header_field )
162 free(_it->_header_field);
163 if ( _it->_header_value )
164 free(_it->_header_value);
165
166 if ( _it == _head ){
167 _head = _head->next;
168 }
169
170 free(_it);
171 _it = _tmp;
172 } else
173 _tmp = _it;
174
175 }
176
177 return _head;
178}
179
180
181
diff --git a/toxmsi/toxmsi_header.h b/toxmsi/toxmsi_header.h
deleted file mode 100644
index 599fafa3..00000000
--- a/toxmsi/toxmsi_header.h
+++ /dev/null
@@ -1,99 +0,0 @@
1#ifndef _MSI_HEADER_
2#define _MSI_HEADER_
3
4/* Basic format of the unparsed header string
5 * ( No spaces in field allowed )
6 * Version 0.1.1\n\r
7 * Request INVITE\n\r
8 * Response\n\r
9 * Friend-id ( from ip )\n\r
10 * Call-type AUDIO\n\r
11 * User-agent phone-v.1.0.0\n\r
12 */
13
14
15/* define raw header terminator */
16static const char* _RAW_TERMINATOR = "\n\r";
17
18/* define string formats for the identifiers */
19#define _VERSION_FIELD "Version"
20#define _REQUEST_FIELD "Request"
21#define _RESPONSE_FIELD "Response"
22#define _FRIENDID_FIELD "Friend-id"
23#define _CALLTYPE_FIELD "Call-type"
24#define _USERAGENT_FIELD "User-agent"
25#define _INFO_FIELD "INFO"
26#define _REASON_FIELD "Reason"
27#define _CALL_ID_FIELD "Call-id"
28
29#define HEADER_VALUES \
30/*uint8_t* _header_field */ \
31uint8_t* _header_value;
32
33typedef struct msi_header_s { /* raw header list */
34 uint8_t* _header_field;
35 uint8_t* _header_value;
36
37 struct msi_header_s* next;
38
39} msi_header_t;
40
41
42
43typedef struct msi_header_version_s { /* Defines our version */
44 HEADER_VALUES
45
46} msi_header_version_t;
47
48typedef struct msi_header_request_s { /* Defines our request */
49 HEADER_VALUES
50
51} msi_header_request_t;
52
53typedef struct msi_header_response_s { /* Defines our response */
54 HEADER_VALUES
55
56} msi_header_response_t;
57
58typedef struct msi_header_friendid_s { /* Defines source that sent the message */
59 HEADER_VALUES
60
61} msi_header_friendid_t;
62
63typedef struct msi_header_call_type_s { /* Defines the type of the call */
64 HEADER_VALUES
65
66} msi_header_call_type_t;
67
68typedef struct msi_header_user_agent_s { /* Defines the device of the participant */
69 HEADER_VALUES
70
71} msi_header_user_agent_t;
72
73
74typedef struct msi_header_call_id_s { /* Call id that is used to identify the call */
75 HEADER_VALUES
76
77} msi_header_call_id_t;
78
79typedef struct msi_header_info_s { /* Defines informational message header */
80 HEADER_VALUES
81
82} msi_header_info_t;
83
84typedef struct msi_header_reason_s { /* Defines reason mostly for error messages */
85 HEADER_VALUES
86
87} msi_header_reason_t;
88
89struct msi_msg_s;
90
91/*
92 * Parses the header list to header types
93 */
94int msi_parse_headers ( struct msi_msg_s* _msg );
95
96/* Make sure it's null terminated */
97msi_header_t* msi_parse_raw_data ( const uint8_t* _data );
98
99#endif /* _MSI_HEADER_ */
diff --git a/toxmsi/toxmsi_message.c b/toxmsi/toxmsi_message.c
deleted file mode 100644
index 72d7ee01..00000000
--- a/toxmsi/toxmsi_message.c
+++ /dev/null
@@ -1,267 +0,0 @@
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif /* HAVE_CONFIG_H */
4
5#include "toxmsi_message.h"
6#include <stdlib.h>
7#include <string.h>
8#include "../toxrtp/toxrtp_helper.h"
9#include <assert.h>
10#include <stdlib.h>
11
12#define ALLOCATE_HEADER(_header_type, _var, _m_header_value) \
13_var = calloc( sizeof(_header_type), 1 ); \
14assert(_var); \
15_var->_header_value = t_strallcpy((const uint8_t*)_m_header_value);
16
17#define DEALLOCATE_HEADER(_header) \
18if ( _header && _header->_header_value ) { \
19free( _header->_header_value ); \
20free( _header ); }
21
22#define SET_HEADER(_header_type, _var, _m_header_value) \
23if ( _var ){ \
24free(_var->_header_value); \
25free(_var);} \
26ALLOCATE_HEADER( _header_type, _var, _m_header_value )
27
28
29/* Sets empty message
30 */
31void set_msg ( msi_msg_t* _msg )
32{
33 _msg->_call_type = NULL;
34 _msg->_version = NULL;
35 _msg->_request = NULL;
36 _msg->_response = NULL;
37 _msg->_friend_id = NULL;
38 _msg->_user_agent = NULL;
39 _msg->_call_id = NULL;
40 _msg->_reason = NULL;
41 _msg->_info = NULL;
42 _msg->_next = NULL;
43 _msg->_headers = NULL;
44}
45
46void msi_free_msg ( msi_msg_t* _msg )
47{
48 assert(_msg);
49
50 DEALLOCATE_HEADER(_msg->_call_type);
51 DEALLOCATE_HEADER(_msg->_friend_id);
52 DEALLOCATE_HEADER(_msg->_request);
53 DEALLOCATE_HEADER(_msg->_response);
54 DEALLOCATE_HEADER(_msg->_user_agent);
55 DEALLOCATE_HEADER(_msg->_version);
56 DEALLOCATE_HEADER(_msg->_info);
57 DEALLOCATE_HEADER(_msg->_reason);
58 DEALLOCATE_HEADER(_msg->_call_id);
59
60 free(_msg);
61}
62
63void append_header_to_string ( uint8_t* _dest, const uint8_t* _header_field, const uint8_t* _header_value )
64{
65 assert(_dest);
66 assert(_header_value);
67 assert(_header_field);
68
69 size_t _dest_len = t_memlen(_dest);
70
71 uint8_t* _storage_iterator = _dest + _dest_len;
72 const uint8_t* _header_fit = _header_field;
73 const uint8_t* _header_val = _header_value;
74 const uint8_t* _term_it = (const uint8_t*) _RAW_TERMINATOR;
75
76 while ( *_header_fit ){
77 *_storage_iterator = *_header_fit;
78 ++_header_fit;
79 ++_storage_iterator;
80 }
81
82 *_storage_iterator = ' '; /* place space */
83 ++_storage_iterator;
84
85 while ( *_header_val ){
86 *_storage_iterator = *_header_val;
87 ++_header_val;
88 ++_storage_iterator;
89 }
90
91 while ( *_term_it ){
92 *_storage_iterator = *_term_it;
93 ++_term_it;
94 ++_storage_iterator;
95 }
96}
97
98msi_msg_t* msi_parse_msg ( const uint8_t* _data )
99{
100 assert(_data);
101
102 msi_msg_t* _retu = calloc ( sizeof ( msi_msg_t ), 1 );
103 assert(_retu);
104
105 set_msg(_retu);
106
107 _retu->_headers = msi_parse_raw_data ( _data );
108
109 if ( msi_parse_headers (_retu) == FAILURE ) {
110 msi_free_msg(_retu);
111 return NULL;
112 }
113
114 if ( !_retu->_version || strcmp((const char*)_retu->_version->_header_value, VERSION_STRING) != 0 ){
115 msi_free_msg(_retu);
116 return NULL;
117 }
118
119 return _retu;
120}
121
122
123uint8_t* msi_msg_to_string ( msi_msg_t* _msg )
124{
125 assert(_msg);
126
127 uint8_t* _retu = calloc(sizeof(uint8_t), MSI_MAXMSG_SIZE );
128 assert(_retu);
129
130 t_memset(_retu, '\0', MSI_MAXMSG_SIZE);
131
132 if ( _msg->_version ){
133 append_header_to_string(_retu, (const uint8_t*)_VERSION_FIELD, _msg->_version->_header_value);
134 }
135
136 if ( _msg->_request ){
137 append_header_to_string(_retu, (const uint8_t*)_REQUEST_FIELD, _msg->_request->_header_value);
138 }
139
140 if ( _msg->_response ){
141 append_header_to_string(_retu, (const uint8_t*)_RESPONSE_FIELD, _msg->_response->_header_value);
142 }
143
144 if ( _msg->_friend_id ){
145 append_header_to_string(_retu, (const uint8_t*)_FRIENDID_FIELD, _msg->_friend_id->_header_value);
146 }
147
148 if ( _msg->_call_type ){
149 append_header_to_string(_retu, (const uint8_t*)_CALLTYPE_FIELD, _msg->_call_type->_header_value);
150 }
151
152 if ( _msg->_user_agent ){
153 append_header_to_string(_retu, (const uint8_t*)_USERAGENT_FIELD, _msg->_user_agent->_header_value);
154 }
155
156 if ( _msg->_info ){
157 append_header_to_string(_retu, (const uint8_t*)_INFO_FIELD, _msg->_info->_header_value);
158 }
159
160 if ( _msg->_call_id ){
161 append_header_to_string(_retu, (const uint8_t*)_CALL_ID_FIELD, _msg->_call_id->_header_value);
162 }
163
164 if ( _msg->_reason ){
165 append_header_to_string(_retu, (const uint8_t*)_REASON_FIELD, _msg->_reason->_header_value);
166 }
167
168 return _retu;
169}
170
171msi_msg_t* msi_msg_new ( uint8_t _type, const uint8_t* _typeid )
172{
173 msi_msg_t* _retu = calloc ( sizeof ( msi_msg_t ), 1 );
174 assert(_retu);
175
176 set_msg(_retu);
177
178 if ( _type == TYPE_REQUEST ){
179 ALLOCATE_HEADER( msi_header_request_t, _retu->_request, _typeid )
180 _retu->_response = NULL;
181
182 } else if ( _type == TYPE_RESPONSE ) {
183 ALLOCATE_HEADER( msi_header_response_t, _retu->_response, _typeid )
184 _retu->_request = NULL;
185
186 } else {
187 msi_free_msg(_retu);
188 return NULL;
189 }
190
191
192 ALLOCATE_HEADER( msi_header_version_t, _retu->_version, VERSION_STRING)
193
194 _retu->_friend_id = NULL;
195 _retu->_call_type = NULL;
196 _retu->_user_agent = NULL;
197 _retu->_info = NULL;
198
199 _retu->_next = NULL;
200
201 return _retu;
202}
203
204uint8_t* msi_genterate_call_id ( uint8_t* _storage, size_t _len )
205{
206 assert(_storage);
207
208 static const char _alphanum[] =
209 "0123456789"
210 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
211 "abcdefghijklmnopqrstuvwxyz"; /* avoids warning */
212
213 uint8_t _val = 0;
214 size_t _it;
215
216 /* Generate random values 1-255 */
217 for ( _it = 0; _it < _len; _it ++ ) {
218 while ( !_val ) _val = (uint8_t) _alphanum[ t_random(61) ];
219
220 _storage[_it] = _val;
221 _val = 0;
222 }
223
224 return _storage;
225}
226
227/* HEADER SETTING
228 */
229
230void msi_msg_set_call_type ( msi_msg_t* _msg, const uint8_t* _header_field )
231{
232 assert(_msg);
233 assert(_header_field);
234
235 SET_HEADER(msi_header_call_type_t, _msg->_call_type, _header_field)
236}
237void msi_msg_set_user_agent ( msi_msg_t* _msg, const uint8_t* _header_field )
238{
239 assert(_msg);
240 assert(_header_field);
241
242 SET_HEADER(msi_header_user_agent_t, _msg->_user_agent, _header_field)
243}
244void msi_msg_set_friend_id ( msi_msg_t* _msg, const uint8_t* _header_field )
245{
246 assert(_msg);
247 assert(_header_field);
248
249 SET_HEADER(msi_header_friendid_t, _msg->_friend_id, _header_field)
250}
251void msi_msg_set_info ( msi_msg_t* _msg, const uint8_t* _header_field )
252{
253}
254void msi_msg_set_reason ( msi_msg_t* _msg, const uint8_t* _header_field )
255{
256 assert(_msg);
257 assert(_header_field);
258
259 SET_HEADER(msi_header_reason_t, _msg->_reason, _header_field)
260}
261void msi_msg_set_call_id ( msi_msg_t* _msg, const uint8_t* _header_field )
262{
263 assert(_msg);
264 assert(_header_field);
265
266 SET_HEADER(msi_header_call_id_t, _msg->_call_id, _header_field)
267}
diff --git a/toxmsi/toxmsi_message.h b/toxmsi/toxmsi_message.h
deleted file mode 100644
index 87dfccc2..00000000
--- a/toxmsi/toxmsi_message.h
+++ /dev/null
@@ -1,120 +0,0 @@
1#ifndef _MSI_MESSAGE_H_
2#define _MSI_MESSAGE_H_
3
4#ifdef HAVE_CONFIG_H
5#include "config.h"
6#endif /* HAVE_CONFIG_H */
7
8#include <inttypes.h>
9#include "../toxcore/network.h"
10#include "../toxcore/tox.h"
11
12#include "toxmsi_header.h"
13
14#define TYPE_REQUEST 1
15#define TYPE_RESPONSE 2
16
17#define VERSION_STRING "0.2.2"
18
19#define MSI_MAXMSG_SIZE 65535
20
21typedef enum {
22 _invite,
23 _start,
24 _cancel,
25 _reject,
26 _end,
27
28} msi_request_t;
29
30static inline const uint8_t *stringify_request(msi_request_t _request)
31{
32 static const uint8_t* strings[] =
33 {
34 (uint8_t*)"INVITE",
35 (uint8_t*)"START",
36 (uint8_t*)"CANCEL",
37 (uint8_t*)"REJECT",
38 (uint8_t*)"END"
39 };
40
41 return strings[_request];
42}
43
44typedef enum {
45 _trying,
46 _ringing,
47 _starting,
48 _ending,
49 _error
50
51} msi_response_t;
52
53static inline const uint8_t *stringify_response(msi_response_t _response)
54{
55 static const uint8_t* strings[] =
56 {
57 (uint8_t*)"trying",
58 (uint8_t*)"ringing",
59 (uint8_t*)"starting",
60 (uint8_t*)"ending",
61 (uint8_t*)"error"
62 };
63
64 return strings[_response];
65}
66
67
68typedef struct msi_msg_s {
69 /* This is the header list which contains unparsed headers */
70 msi_header_t* _headers;
71
72 /* Headers parsed */
73 msi_header_version_t* _version;
74 msi_header_request_t* _request;
75 msi_header_response_t* _response;
76 msi_header_friendid_t* _friend_id;
77 msi_header_call_type_t* _call_type;
78 msi_header_user_agent_t* _user_agent;
79 msi_header_info_t* _info;
80 msi_header_reason_t* _reason;
81 msi_header_call_id_t* _call_id;
82
83 /* Pointer to next member since it's list duuh */
84 struct msi_msg_s* _next;
85
86} msi_msg_t;
87
88
89/*
90 * Parse data received from socket
91 */
92msi_msg_t* msi_parse_msg ( const uint8_t* _data );
93
94/*
95 * Make new message. Arguments: _type: (request, response); _type_field ( value )
96 */
97msi_msg_t* msi_msg_new ( uint8_t _type, const uint8_t* _type_field );
98
99/* Header setting */
100void msi_msg_set_call_type ( msi_msg_t* _msg, const uint8_t* _header_field );
101void msi_msg_set_user_agent ( msi_msg_t* _msg, const uint8_t* _header_field );
102void msi_msg_set_friend_id ( msi_msg_t* _msg, const uint8_t* _header_field );
103void msi_msg_set_info ( msi_msg_t* _msg, const uint8_t* _header_field );
104void msi_msg_set_reason ( msi_msg_t* _msg, const uint8_t* _header_field );
105void msi_msg_set_call_id ( msi_msg_t* _msg, const uint8_t* _header_field );
106
107
108uint8_t* msi_genterate_call_id ( uint8_t* _storage, size_t _len );
109/*
110 * Parses message struct to string.
111 * Allocates memory so don't forget to free it
112 */
113uint8_t* msi_msg_to_string ( msi_msg_t* _msg );
114
115/*
116 * msi_msg_s struct deallocator
117 */
118void msi_free_msg ( msi_msg_t* _msg );
119
120#endif /* _MSI_MESSAGE_H_ */
diff --git a/toxrtp/Makefile.inc b/toxrtp/Makefile.inc
deleted file mode 100644
index 801a7fd3..00000000
--- a/toxrtp/Makefile.inc
+++ /dev/null
@@ -1,34 +0,0 @@
1if BUILD_AV
2
3lib_LTLIBRARIES += libtoxrtp.la
4
5libtoxrtp_la_include_HEADERS = \
6 ../toxrtp/toxrtp.h
7
8libtoxrtp_la_includedir = $(includedir)/tox
9
10libtoxrtp_la_SOURCES = ../toxrtp/toxrtp_error.h \
11 ../toxrtp/toxrtp_error.c \
12 ../toxrtp/toxrtp_error_id.h \
13 ../toxrtp/toxrtp_helper.h \
14 ../toxrtp/toxrtp_helper.c \
15 ../toxrtp/toxrtp.h \
16 ../toxrtp/toxrtp.c \
17 ../toxrtp/toxrtp_message.h \
18 ../toxrtp/toxrtp_message.c \
19 ../toxcore/network.h \
20 ../toxcore/network.c \
21 ../toxcore/util.h \
22 ../toxcore/util.c
23
24libtoxrtp_la_CFLAGS = -I../toxcore \
25 -I../toxrtp \
26 $(NACL_CFLAGS)
27
28libtoxrtp_la_LDFLAGS = $(TOXRTP_LT_LDFLAGS) \
29 $(NACL_LDFLAGS) \
30 $(EXTRA_LT_LDFLAGS)
31
32libtoxrtp_la_LIBS = $(NACL_LIBS)
33
34endif
diff --git a/toxrtp/tests/Makefile.inc b/toxrtp/tests/Makefile.inc
deleted file mode 100644
index 8d1c8b69..00000000
--- a/toxrtp/tests/Makefile.inc
+++ /dev/null
@@ -1 +0,0 @@
1
diff --git a/toxrtp/tests/test_bidirect.c b/toxrtp/tests/test_bidirect.c
deleted file mode 100644
index b5a0899e..00000000
--- a/toxrtp/tests/test_bidirect.c
+++ /dev/null
@@ -1,109 +0,0 @@
1#define _BSD_SOURCE
2
3#include "../toxrtp.h"
4#include "../toxrtp_message.h"
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <utime.h>
9#include <assert.h>
10
11#include "test_helper.h"
12#include "../../toxcore/tox.h"
13
14#ifdef _CT_BIDIRECT
15
16int _print_help( const char* name )
17{
18 char* _help = malloc( 300 );
19 memset(_help, '\0', 300);
20
21 strcat(_help, " Usage: ");
22 strcat(_help, name);
23 strcat(_help, "\n -d IP ( destination )\n"
24 " -p PORT ( dest Port )\n"
25 " -l PORT ( listen Port ) \n");
26
27 puts ( _help );
28
29 free(_help);
30 return FAILURE;
31}
32
33int main( int argc, char* argv[] )
34{
35 int status;
36 tox_IP_Port Ip_port;
37 const char* ip, *psend, *plisten;
38 uint16_t port_send, port_listen;
39 const uint8_t* test_bytes = "0123456789012345678901234567890123456789012345678901234567890123456789"
40 "0123456789012345678901234567890123456789012345678901234567890123456789"
41 "0123456789012345678901234567890123456789012345678901234567890123456789"
42 "0123456789012345678901234567890123456789012345678901234567890123456789";
43
44
45 rtp_session_t* _m_session;
46 rtp_msg_t *_m_msg_R, *_m_msg_S;
47 arg_t* _list = parse_args ( argc, argv );
48
49 ip = find_arg_duble(_list, "-d");
50 psend = find_arg_duble(_list, "-p");
51 plisten = find_arg_duble(_list, "-l");
52
53 if ( !ip || !plisten || !psend )
54 return _print_help(argv[0]);
55
56 port_send = atoi(psend);
57 port_listen = atoi(plisten);
58
59 IP_Port local, remote;
60
61 /*
62 * This is the Local ip. We initiate networking on
63 * this value for it's the local one. To make stuff simpler we receive over this value
64 * and send on the other one ( see remote )
65 */
66 local.ip.i = htonl(INADDR_ANY);
67 local.port = port_listen;
68 Networking_Core* _networking = new_networking(local.ip, port_listen);
69
70 if ( !_networking )
71 return FAILURE;
72
73 int _socket = _networking->sock;
74 /*
75 * Now this is the remote. It's used by rtp_session_t to determine the receivers ip etc.
76 */
77 t_setipport ( ip, port_send, &remote );
78 _m_session = rtp_init_session(-1, -1);
79 rtp_add_receiver( _m_session, &remote );
80
81 /* Now let's start our main loop in both recv and send mode */
82
83 for ( ;; )
84 {
85 /*
86 * This part checks for received messages and if gotten one
87 * display 'Received msg!' indicator and free message
88 */
89 _m_msg_R = rtp_recv_msg ( _m_session );
90
91 if ( _m_msg_R ) {
92 puts ( "Received msg!" );
93 rtp_free_msg(_m_session, _m_msg_R);
94 }
95 /* -------------------- */
96
97 /*
98 * This one makes a test msg and sends that message to the 'remote'
99 */
100 _m_msg_S = rtp_msg_new ( _m_session, test_bytes, 280 ) ;
101 rtp_send_msg ( _m_session, _m_msg_S, _socket );
102 usleep ( 10000 );
103 /* -------------------- */
104 }
105
106 return SUCCESS;
107}
108
109#endif /* _CT_BIDIRECT */
diff --git a/toxrtp/tests/test_headers.c b/toxrtp/tests/test_headers.c
deleted file mode 100644
index be3f1375..00000000
--- a/toxrtp/tests/test_headers.c
+++ /dev/null
@@ -1,316 +0,0 @@
1/* test_headers.c
2 *
3 * Tests header parsing. You probably won't need this. !Red!
4 *
5 *
6 * Copyright (C) 2013 Tox project All Rights Reserved.
7 *
8 * This file is part of Tox.
9 *
10 * Tox is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Tox is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include "test_helper.h"
26#include "../toxrtp.h"
27#include "../toxrtp_message.h"
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <utime.h>
33#include <assert.h>
34#include "../toxrtp_error_id.h"
35
36#define _CT_HEADERS_
37
38#ifdef _CT_HEADERS
39
40int _socket;
41pthread_mutex_t _mutex;
42
43int print_help()
44{
45 puts (
46 " Usage: Tuxrtp [-s (send mode) -d IP ( destination ) -p PORT ( dest Port )] \n"
47 " [-r ( recv mode ) ]"
48 );
49 return FAILURE;
50}
51
52void print_session_stats ( rtp_session_t* _m_session )
53{
54 printf
55 (
56 "Session test:\n"
57 "\tPackets sent:%d\n"
58 "\tPackets recv:%d\n\n"
59 "\tBytes sent:%d\n"
60 "\tBytes recv:%d\n\n"
61 "\tHeader CCSRs:%d\n"
62 ,
63 _m_session->_packets_sent,
64 _m_session->_packets_recv,
65 _m_session->_bytes_sent,
66 _m_session->_bytes_recv,
67 _m_session->_cc
68 );
69
70 uint8_t i;
71 for ( i = 0; i < _m_session->_cc; i++ ) {
72 printf (
73 "\t%d > :%d\n", i, _m_session->_csrc[i]
74 );
75 }
76}
77
78void print_header_info ( rtp_header_t* _header )
79{
80 printf
81 (
82 "Header info:\n"
83 "\tVersion :%d\n"
84 "\tPadding :%d\n"
85 "\tExtension :%d\n"
86 "\tCSRC count :%d\n"
87 "\tPayload type :%d\n"
88 "\tMarker :%d\n\n"
89
90 "\tSSrc :%d\n"
91 "\tSequence num :%d\n"
92 "\tLenght: :%d\n"
93 "\tCSRC's:\n"
94 ,
95 rtp_header_get_flag_version ( _header ),
96 rtp_header_get_flag_padding ( _header ),
97 rtp_header_get_flag_extension ( _header ),
98 rtp_header_get_flag_CSRC_count ( _header ),
99 rtp_header_get_setting_payload_type ( _header ),
100 rtp_header_get_setting_marker ( _header ),
101
102 _header->_ssrc,
103 _header->_sequence_number,
104 _header->_length
105 );
106
107
108 uint8_t i;
109 for ( i = 0; i < rtp_header_get_flag_CSRC_count ( _header ); i++ ) {
110 printf (
111 "\t%d > :%d\n", i, _header->_csrc[i]
112 );
113 }
114
115 puts ( "\n" );
116}
117
118void print_ext_header_info(rtp_ext_header_t* _ext_header)
119{
120 printf
121 (
122 "External Header info: \n"
123 "\tLenght :%d\n"
124 "\tID :%d\n"
125 "\tValue H :%d\n"
126 "\tValue W :%d\n\n",
127 _ext_header->_ext_len,
128 _ext_header->_ext_type,
129 rtp_get_resolution_marking_height(_ext_header, 0),
130 rtp_get_resolution_marking_width(_ext_header, 0)
131 );
132}
133
134int rtp_handlepacket ( rtp_session_t* _session, rtp_msg_t* _msg )
135{
136 if ( !_msg )
137 return FAILURE;
138
139 if ( rtp_check_late_message(_session, _msg) < 0 ) {
140 rtp_register_msg(_session, _msg);
141 }
142
143 if ( _session->_last_msg ) {
144 _session->_last_msg->_next = _msg;
145 _session->_last_msg = _msg;
146 } else {
147 _session->_last_msg = _session->_oldest_msg = _msg;
148 }
149
150
151 return SUCCESS;
152}
153
154void* receivepacket_callback(void* _p_session)
155{
156 rtp_msg_t* _msg;
157 rtp_session_t* _session = _p_session;
158
159 uint32_t _bytes;
160 tox_IP_Port _from;
161 uint8_t _socket_data[MAX_UDP_PACKET_SIZE];
162
163 int _m_socket = _socket;
164
165 while ( 1 )
166 {
167 int _status = receivepacket ( _m_socket, &_from, _socket_data, &_bytes );
168
169 if ( _status == FAILURE ) { /* nothing recved */
170 usleep(1000);
171 continue;
172 }
173
174 pthread_mutex_lock ( &_mutex );
175
176 _msg = rtp_msg_parse ( NULL, _socket_data, _bytes );
177 rtp_handlepacket(_session, _msg);
178
179 pthread_mutex_unlock ( &_mutex );
180 }
181
182 pthread_exit(NULL);
183}
184
185int main ( int argc, char* argv[] )
186{
187 arg_t* _list = parse_args ( argc, argv );
188
189 if ( _list == NULL ) { /* failed */
190 return print_help();
191 }
192
193 pthread_mutex_init ( &_mutex, NULL );
194
195 int status;
196 IP_Port Ip_port;
197 const char* ip;
198 uint16_t port;
199
200
201 const uint8_t* test_bytes [300];
202 memset(test_bytes, 'a', 300);
203
204 rtp_session_t* _m_session;
205 rtp_msg_t* _m_msg;
206
207 if ( find_arg_simple ( _list, "-r" ) != FAILURE ) { /* Server mode */
208
209 IP_Port LOCAL_IP; /* since you need at least 1 recv-er */
210 LOCAL_IP.ip.i = htonl(INADDR_ANY);
211 LOCAL_IP.port = RTP_PORT;
212 LOCAL_IP.padding = -1;
213
214 _m_session = rtp_init_session ( -1, -1 );
215 Networking_Core* _networking = new_networking(LOCAL_IP.ip, RTP_PORT_LISTEN);
216 _socket = _networking->sock;
217
218
219 if ( !_networking ){
220 pthread_mutex_destroy ( &_mutex );
221 return FAILURE;
222 }
223
224 int _socket = _networking->sock;
225
226 if ( status < 0 ) {
227 pthread_mutex_destroy ( &_mutex );
228 return FAILURE;
229 }
230 /* -- start in recv mode, get 1 message and then analyze it -- */
231 pthread_t _tid;
232 RUN_IN_THREAD(receivepacket_callback, _tid, _m_session)
233
234 for ( ; ; ) { /* Recv for x seconds */
235 _m_msg = rtp_recv_msg ( _m_session );
236
237 /* _m_msg = rtp_session_get_message_queded ( _m_session ); DEPRECATED */
238 if ( _m_msg ) {
239 /*rtp_free_msg(_m_session, _m_msg);
240 _m_msg = NULL;*/
241 printf("Timestamp: %d\n", _m_msg->_header->_timestamp);
242 }
243
244 usleep ( 10000 );
245 }
246
247 if ( _m_msg->_header ) {
248 rtp_header_print ( _m_msg->_header );
249 }
250 if ( _m_msg->_ext_header ){
251 print_ext_header_info(_m_msg->_ext_header);
252 }
253
254 //print_session_stats ( _m_session );
255
256
257 //printf ( "Payload: ( %d ) \n%s\n", _m_msg->_length, _m_msg->_data );
258
259
260 } else if ( find_arg_simple ( _list, "-s" ) != FAILURE ) {
261 ip = find_arg_duble ( _list, "-d" );
262
263 if ( ip == NULL ) {
264 pthread_mutex_destroy ( &_mutex );
265 return FAILURE;
266 }
267
268 const char* _port = find_arg_duble ( _list, "-p" );
269
270 if ( _port != NULL ) {
271 port = atoi ( _port );
272 }
273
274 t_setipport ( ip, port, &Ip_port );
275 printf ( "Remote: %s:%d\n", ip, port );
276
277 Networking_Core* _networking = new_networking(Ip_port.ip, RTP_PORT);
278
279 if ( !_networking ){
280 pthread_mutex_destroy ( &_mutex );
281 return FAILURE;
282 }
283
284 int _socket = _networking->sock;
285
286 _m_session = rtp_init_session ( -1, -1 );
287 rtp_add_receiver( _m_session, &Ip_port );
288 //rtp_add_resolution_marking(_m_session, 1920, 1080);
289 //rtp_add_framerate_marking(_m_session, 1000);
290
291 puts ( "Now sending payload!\n" );
292 uint16_t _first_sequ = _m_session->_sequence_number;
293
294 /* use already defined buffer lenght */
295 while ( 1 ){
296 _m_msg = rtp_msg_new ( _m_session, test_bytes, 300 );
297 rtp_send_msg ( _m_session, _m_msg, _socket );
298 usleep(10000);
299 }
300
301 if ( _m_session->_last_error ) {
302 puts ( _m_session->_last_error );
303 }
304
305 return rtp_terminate_session(_m_session);
306
307 } else {
308 pthread_mutex_destroy ( &_mutex );
309 return FAILURE;
310 }
311 pthread_mutex_destroy ( &_mutex );
312
313 return SUCCESS;
314}
315
316#endif /* _CT_HEADERS */
diff --git a/toxrtp/tests/test_helper.c b/toxrtp/tests/test_helper.c
deleted file mode 100644
index 526b6b38..00000000
--- a/toxrtp/tests/test_helper.c
+++ /dev/null
@@ -1,83 +0,0 @@
1/* test_helper.c
2 *
3 * Tests support. !Red!
4 *
5 *
6 * Copyright (C) 2013 Tox project All Rights Reserved.
7 *
8 * This file is part of Tox.
9 *
10 * Tox is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Tox is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include "test_helper.h"
26
27#include <string.h>
28#include <stdlib.h>
29
30arg_t* parse_args ( int argc, char* argv[] )
31{
32 arg_t* _list = calloc(sizeof(arg_t), 1);
33 _list->next = _list->prev = NULL;
34
35 arg_t* it = _list;
36
37 size_t val;
38 for ( val = 0; val < argc; val ++ ) {
39 it->value = argv[val];
40
41 if ( val < argc - 1 ) { /* just about to end */
42 it->next = calloc(sizeof(arg_t), 1);
43 it->next->prev = it;
44 it = it->next;
45 it->next = NULL;
46 }
47 }
48
49 return _list;
50}
51
52int find_arg_simple ( arg_t* _head, const char* _id )
53{
54 arg_t* it = _head;
55
56 int i;
57 for ( i = 1; it != NULL; it = it->next ) {
58 if ( strcmp ( _id, it->value ) == 0 ) {
59 return i;
60 }
61
62 i++;
63 }
64
65 return FAILURE;
66}
67
68const char* find_arg_duble ( arg_t* _head, const char* _id )
69{
70 arg_t* _it;
71 for ( _it = _head; _it != NULL; _it = _it->next ) {
72 if ( strcmp ( _id, _it->value ) == 0 ) {
73 if ( _it->next && _it->next->value[0] != '-' ) { /* exclude option */
74 return _it->next->value;
75 } else {
76 return NULL;
77 }
78 }
79 }
80
81 return NULL;
82}
83
diff --git a/toxrtp/tests/test_helper.h b/toxrtp/tests/test_helper.h
deleted file mode 100644
index de654743..00000000
--- a/toxrtp/tests/test_helper.h
+++ /dev/null
@@ -1,61 +0,0 @@
1/* test_helper.h
2 *
3 * Tests support. !Red!
4 *
5 *
6 * Copyright (C) 2013 Tox project All Rights Reserved.
7 *
8 * This file is part of Tox.
9 *
10 * Tox is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * Tox is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#ifndef _TEST__HELPER_
26#define _TEST__HELPER_
27
28#include "../toxrtp_helper.h"
29
30#define RTP_PORT 31003
31#define RTP_PORT_LISTEN 31001
32
33#define _SLEEP_INTERVAL 1000
34
35typedef struct arg_s {
36 const char* value;
37 struct arg_s* next;
38 struct arg_s* prev;
39
40} arg_t;
41
42
43
44/* Parses arguments into d-list arg_t */
45arg_t* parse_args ( int argc, char* argv[] );
46
47/* Get a single argument ( i.e. ./test -s |find if has 's' >> | find_arg_simple(_t, "-s") )
48 * A little error checking, of course, returns FAILURE if not found and if found returns position
49 * where it's found.
50 */
51int find_arg_simple ( arg_t* _head, const char* _id );
52
53/* Get a single argument ( i.e. ./test -d 127.0.0.1 |get 'd' value >> | find_arg_duble(_t, "-d") )
54 * A little error checking, of course, returns NULL if not found and if found returns value
55 * of that argument ( i.e. '127.0.0.1').
56 */
57const char* find_arg_duble ( arg_t* _head, const char* _id );
58
59#endif /* _TEST__HELPER_ */
60
61
diff --git a/toxrtp/toxrtp.c b/toxrtp/toxrtp.c
deleted file mode 100644
index 6844b0b1..00000000
--- a/toxrtp/toxrtp.c
+++ /dev/null
@@ -1,693 +0,0 @@
1/* rtp_impl.c
2 *
3 * Rtp implementation includes rtp_session_s struct which is a session identifier.
4 * It contains session information and it's a must for every session.
5 * It's best if you don't touch any variable directly but use callbacks to do so. !Red!
6 *
7 *
8 * Copyright (C) 2013 Tox project All Rights Reserved.
9 *
10 * This file is part of Tox.
11 *
12 * Tox is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * Tox is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif /* HAVE_CONFIG_H */
30
31#include "toxrtp.h"
32#include "toxrtp_message.h"
33#include "toxrtp_helper.h"
34#include <assert.h>
35#include <pthread.h>
36#include "../toxcore/util.h"
37#include "../toxcore/network.h"
38
39/* Some defines */
40#define PAYLOAD_ID_VALUE_OPUS 1
41#define PAYLOAD_ID_VALUE_VP8 2
42
43#define size_32 4
44/* End of defines */
45
46#ifdef _USE_ERRORS
47#include "toxrtp_error_id.h"
48#endif /* _USE_ERRORS */
49
50static const uint32_t _payload_table[] = /* PAYLOAD TABLE */
51{
52 8000, 8000, 8000, 8000, 8000, 8000, 16000, 8000, 8000, 8000, /* 0-9 */
53 44100, 44100, 0, 0, 90000, 8000, 11025, 22050, 0, 0, /* 10-19 */
54 0, 0, 0, 0, 0, 90000, 90000, 0, 90000, 0, /* 20-29 */
55 0, 90000, 90000, 90000, 90000, 0, 0, 0, 0, 0, /* 30-39 */
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40-49 */
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50-59 */
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60-69 */
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 70-79 */
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-89 */
61 0, 0, 0, 0, 0, 0, PAYLOAD_ID_VALUE_OPUS, 0, 0, 0, /* 90-99 */
62 0, 0, 0, 0, 0, 0, PAYLOAD_ID_VALUE_VP8, 0, 0, 0, /* 100-109 */
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110-119 */
64 0, 0, 0, 0, 0, 0, 0, 0 /* 120-127 */
65};
66
67/* Current compatibility solution */
68int m_sendpacket(Networking_Core* _core_handler, void *ip_port, uint8_t *data, uint32_t length)
69{
70 return sendpacket(_core_handler, *((IP_Port*) ip_port), data, length);
71}
72
73rtp_session_t* rtp_init_session ( int max_users, int _multi_session )
74{
75#ifdef _USE_ERRORS
76 REGISTER_RTP_ERRORS
77#endif /* _USE_ERRORS */
78
79 rtp_session_t* _retu = calloc(sizeof(rtp_session_t), 1);
80 assert(_retu);
81
82 _retu->_dest_list = _retu->_last_user = NULL;
83
84 _retu->_max_users = max_users;
85 _retu->_packets_recv = 0;
86 _retu->_packets_sent = 0;
87 _retu->_bytes_sent = 0;
88 _retu->_bytes_recv = 0;
89 _retu->_last_error = NULL;
90 _retu->_packet_loss = 0;
91
92 /*
93 * SET HEADER FIELDS
94 */
95
96 _retu->_version = RTP_VERSION; /* It's always 2 */
97 _retu->_padding = 0; /* If some additional data is needed about
98 * the packet */
99 _retu->_extension = 0; /* If extension to header is needed */
100 _retu->_cc = 1; /* It basically represents amount of contributors */
101 _retu->_csrc = NULL; /* Container */
102 _retu->_ssrc = t_random ( -1 );
103 _retu->_marker = 0;
104 _retu->_payload_type = 0; /* You should specify payload type */
105
106 /* Sequence starts at random number and goes to _MAX_SEQU_NUM */
107 _retu->_sequence_number = t_random ( _MAX_SEQU_NUM );
108 _retu->_last_sequence_number = _retu->_sequence_number; /* Do not touch this variable */
109
110 _retu->_initial_time = t_time(); /* In seconds */
111 assert(_retu->_initial_time);
112 _retu->_time_elapsed = 0; /* In seconds */
113
114 _retu->_ext_header = NULL; /* When needed allocate */
115 _retu->_exthdr_framerate = -1;
116 _retu->_exthdr_resolution = -1;
117
118 _retu->_csrc = calloc(sizeof(uint32_t), 1);
119 assert(_retu->_csrc);
120
121 _retu->_csrc[0] = _retu->_ssrc; /* Set my ssrc to the list receive */
122
123 _retu->_prefix_length = 0;
124 _retu->_prefix = NULL;
125
126 _retu->_multi_session = _multi_session;
127
128 /* Initial */
129 _retu->_current_framerate = 0;
130
131
132 _retu->_oldest_msg = _retu->_last_msg = NULL;
133
134 pthread_mutex_init(&_retu->_mutex, NULL);
135 /*
136 *
137 */
138 return _retu;
139}
140
141int rtp_terminate_session ( rtp_session_t* _session )
142{
143 if ( !_session )
144 return FAILURE;
145
146 if ( _session->_dest_list ){
147 rtp_dest_list_t* _fordel = NULL;
148 rtp_dest_list_t* _tmp = _session->_dest_list;
149
150 while( _tmp ){
151 _fordel = _tmp;
152 _tmp = _tmp->next;
153 free(_fordel);
154 }
155 }
156
157 if ( _session->_ext_header )
158 free ( _session->_ext_header );
159
160 if ( _session->_csrc )
161 free ( _session->_csrc );
162
163 if ( _session->_prefix )
164 free ( _session->_prefix );
165
166 pthread_mutex_destroy(&_session->_mutex);
167
168 /* And finally free session */
169 free ( _session );
170
171 return SUCCESS;
172}
173
174uint16_t rtp_get_resolution_marking_height ( rtp_ext_header_t* _header, uint32_t _position )
175{
176 if ( _header->_ext_type & RTP_EXT_TYPE_RESOLUTION )
177 return _header->_hd_ext[_position];
178 else
179 return 0;
180}
181
182uint16_t rtp_get_resolution_marking_width ( rtp_ext_header_t* _header, uint32_t _position )
183{
184 if ( _header->_ext_type & RTP_EXT_TYPE_RESOLUTION )
185 return ( _header->_hd_ext[_position] >> 16 );
186 else
187 return 0;
188}
189
190void rtp_free_msg ( rtp_session_t* _session, rtp_msg_t* _message )
191{
192 free ( _message->_data );
193
194 if ( !_session ){
195 free ( _message->_header->_csrc );
196 if ( _message->_ext_header ){
197 free ( _message->_ext_header->_hd_ext );
198 free ( _message->_ext_header );
199 }
200 } else {
201 if ( _session->_csrc != _message->_header->_csrc )
202 free ( _message->_header->_csrc );
203 if ( _message->_ext_header && _session->_ext_header != _message->_ext_header ) {
204 free ( _message->_ext_header->_hd_ext );
205 free ( _message->_ext_header );
206 }
207 }
208
209 free ( _message->_header );
210 free ( _message );
211}
212
213rtp_header_t* rtp_build_header ( rtp_session_t* _session )
214{
215 rtp_header_t* _retu;
216 _retu = calloc ( sizeof * _retu, 1 );
217 assert(_retu);
218
219 rtp_header_add_flag_version ( _retu, _session->_version );
220 rtp_header_add_flag_padding ( _retu, _session->_padding );
221 rtp_header_add_flag_extension ( _retu, _session->_extension );
222 rtp_header_add_flag_CSRC_count ( _retu, _session->_cc );
223 rtp_header_add_setting_marker ( _retu, _session->_marker );
224 rtp_header_add_setting_payload ( _retu, _session->_payload_type );
225
226 _retu->_sequence_number = _session->_sequence_number;
227 _session->_time_elapsed = t_time() - _session->_initial_time;
228 _retu->_timestamp = t_time();
229 _retu->_ssrc = _session->_ssrc;
230
231 if ( _session->_cc > 0 ) {
232 _retu->_csrc = calloc(sizeof(uint32_t), _session->_cc);
233 assert(_retu->_csrc);
234
235 int i;
236
237 for ( i = 0; i < _session->_cc; i++ ) {
238 _retu->_csrc[i] = _session->_csrc[i];
239 }
240 } else {
241 _retu->_csrc = NULL;
242 }
243
244 _retu->_length = _MIN_HEADER_LENGTH + ( _session->_cc * size_32 );
245
246 return _retu;
247}
248
249void rtp_set_payload_type ( rtp_session_t* _session, uint8_t _payload_value )
250{
251 _session->_payload_type = _payload_value;
252}
253uint32_t rtp_get_payload_type ( rtp_session_t* _session )
254{
255 return _payload_table[_session->_payload_type];
256}
257
258int rtp_add_receiver ( rtp_session_t* _session, tox_IP_Port* _dest )
259{
260 if ( !_session )
261 return FAILURE;
262
263 rtp_dest_list_t* _new_user = calloc(sizeof(rtp_dest_list_t), 1);
264 assert(_new_user);
265
266 _new_user->next = NULL;
267 _new_user->_dest = *_dest;
268
269 if ( _session->_last_user == NULL ) { /* New member */
270 _session->_dest_list = _session->_last_user = _new_user;
271
272 } else { /* Append */
273 _session->_last_user->next = _new_user;
274 _session->_last_user = _new_user;
275 }
276
277 return SUCCESS;
278}
279
280int rtp_send_msg ( rtp_session_t* _session, rtp_msg_t* _msg, void* _core_handler )
281{
282 if ( !_msg || _msg->_data == NULL || _msg->_length <= 0 ) {
283 t_perror ( RTP_ERROR_EMPTY_MESSAGE );
284 return FAILURE;
285 }
286
287 int _last;
288 unsigned long long _total = 0;
289
290 size_t _length = _msg->_length;
291 uint8_t _send_data [ MAX_UDP_PACKET_SIZE ];
292
293 uint16_t _prefix_length = _session->_prefix_length;
294
295 _send_data[0] = 70;
296
297 if ( _session->_prefix && _length + _prefix_length < MAX_UDP_PACKET_SIZE ) {
298 /*t_memcpy(_send_data, _session->_prefix, _prefix_length);*/
299 t_memcpy ( _send_data + 1, _msg->_data, _length );
300 } else {
301 t_memcpy ( _send_data + 1, _msg->_data, _length );
302 }
303
304 /* Set sequ number */
305 if ( _session->_sequence_number >= _MAX_SEQU_NUM ) {
306 _session->_sequence_number = 0;
307 } else {
308 _session->_sequence_number++;
309 }
310
311 /* Start sending loop */
312 rtp_dest_list_t* _it;
313 for ( _it = _session->_dest_list; _it != NULL; _it = _it->next ) {
314
315 _last = m_sendpacket ( _core_handler, &_it->_dest, _send_data, _length + 1);
316
317 if ( _last < 0 ) {
318 t_perror ( RTP_ERROR_STD_SEND_FAILURE );
319 printf("Stderror: %s", strerror(errno));
320 } else {
321 _session->_packets_sent ++;
322 _total += _last;
323 }
324
325 }
326
327 rtp_free_msg ( _session, _msg );
328 _session->_bytes_sent += _total;
329 return SUCCESS;
330}
331
332rtp_msg_t* rtp_recv_msg ( rtp_session_t* _session )
333{
334 if ( !_session )
335 return NULL;
336
337 rtp_msg_t* _retu = _session->_oldest_msg;
338
339 pthread_mutex_lock(&_session->_mutex);
340
341 if ( _retu )
342 _session->_oldest_msg = _retu->_next;
343
344 if ( !_session->_oldest_msg )
345 _session->_last_msg = NULL;
346
347 pthread_mutex_unlock(&_session->_mutex);
348
349 return _retu;
350}
351
352void rtp_store_msg ( rtp_session_t* _session, rtp_msg_t* _msg )
353{
354 if ( rtp_check_late_message(_session, _msg) < 0 ) {
355 rtp_register_msg(_session, _msg);
356 }
357
358 pthread_mutex_lock(&_session->_mutex);
359
360 if ( _session->_last_msg ) {
361 _session->_last_msg->_next = _msg;
362 _session->_last_msg = _msg;
363 } else {
364 _session->_last_msg = _session->_oldest_msg = _msg;
365 }
366
367 pthread_mutex_unlock(&_session->_mutex);
368 return;
369}
370
371int rtp_release_session_recv ( rtp_session_t* _session )
372{
373 if ( !_session ){
374 return FAILURE;
375 }
376
377 rtp_msg_t* _tmp,* _it;
378
379 pthread_mutex_lock(&_session->_mutex);
380
381 for ( _it = _session->_oldest_msg; _it; _it = _tmp ){
382 _tmp = _it->_next;
383 rtp_free_msg(_session, _it);
384 }
385
386 _session->_last_msg = _session->_oldest_msg = NULL;
387
388 pthread_mutex_unlock(&_session->_mutex);
389
390 return SUCCESS;
391}
392
393rtp_msg_t* rtp_msg_new ( rtp_session_t* _session, const uint8_t* _data, uint32_t _length )
394{
395 if ( !_session )
396 return NULL;
397
398 uint8_t* _from_pos;
399 rtp_msg_t* _retu = calloc(sizeof(rtp_msg_t), 1);
400 assert(_retu);
401
402 /* Sets header values and copies the extension header in _retu */
403 _retu->_header = rtp_build_header ( _session ); /* It allocates memory and all */
404 _retu->_ext_header = _session->_ext_header;
405
406 uint32_t _total_lenght = _length + _retu->_header->_length;
407
408 if ( _retu->_ext_header ) {
409
410 _total_lenght += ( _MIN_EXT_HEADER_LENGTH + _retu->_ext_header->_ext_len * size_32 );
411 /* Allocate Memory for _retu->_data */
412 _retu->_data = calloc ( sizeof _retu->_data, _total_lenght );
413 assert(_retu->_data);
414
415 _from_pos = rtp_add_header ( _retu->_header, _retu->_data );
416 _from_pos = rtp_add_extention_header ( _retu->_ext_header, _from_pos + 1 );
417 } else {
418 /* Allocate Memory for _retu->_data */
419 _retu->_data = calloc ( sizeof _retu->_data, _total_lenght );
420 assert(_retu->_data);
421
422 _from_pos = rtp_add_header ( _retu->_header, _retu->_data );
423 }
424
425 /*
426 * Parses the extension header into the message
427 * Of course if any
428 */
429
430 /* Appends _data on to _retu->_data */
431 t_memcpy ( _from_pos + 1, _data, _length );
432
433 _retu->_length = _total_lenght;
434
435 _retu->_next = NULL;
436
437 return _retu;
438}
439
440rtp_msg_t* rtp_msg_parse ( rtp_session_t* _session, const uint8_t* _data, uint32_t _length )
441{
442 rtp_msg_t* _retu = calloc(sizeof(rtp_msg_t), 1);
443 assert(_retu);
444
445 _retu->_header = rtp_extract_header ( _data, _length ); /* It allocates memory and all */
446 if ( !_retu->_header ){
447 free(_retu);
448 return NULL;
449 }
450
451 _retu->_length = _length - _retu->_header->_length;
452
453 uint16_t _from_pos = _retu->_header->_length;
454
455
456 if ( rtp_header_get_flag_extension ( _retu->_header ) ) {
457 _retu->_ext_header = rtp_extract_ext_header ( _data + _from_pos, _length );
458 if ( _retu->_ext_header ){
459 _retu->_length -= ( _MIN_EXT_HEADER_LENGTH + _retu->_ext_header->_ext_len * size_32 );
460 _from_pos += ( _MIN_EXT_HEADER_LENGTH + _retu->_ext_header->_ext_len * size_32 );
461 } else {
462 free (_retu->_ext_header);
463 free (_retu->_header);
464 free (_retu);
465 return NULL;
466 }
467 } else {
468 _retu->_ext_header = NULL;
469 }
470
471 /* Get the payload */
472 _retu->_data = calloc ( sizeof ( uint8_t ), _retu->_length );
473 assert(_retu->_data);
474
475 t_memcpy ( _retu->_data, _data + _from_pos, _length - _from_pos );
476
477 _retu->_next = NULL;
478
479
480 if ( _session && !_session->_multi_session && rtp_check_late_message(_session, _retu) < 0 ){
481 rtp_register_msg(_session, _retu);
482 }
483
484 return _retu;
485}
486
487int rtp_check_late_message (rtp_session_t* _session, rtp_msg_t* _msg)
488{
489 /*
490 * Check Sequence number. If this new msg has lesser number then the _session->_last_sequence_number
491 * it shows that the message came in late
492 */
493 if ( _msg->_header->_sequence_number < _session->_last_sequence_number &&
494 _msg->_header->_timestamp < _session->_current_timestamp
495 ) {
496 return SUCCESS; /* Drop the packet. You can check if the packet dropped by checking _packet_loss increment. */
497 }
498 return FAILURE;
499}
500
501void rtp_register_msg ( rtp_session_t* _session, rtp_msg_t* _msg )
502{
503 _session->_last_sequence_number = _msg->_header->_sequence_number;
504 _session->_current_timestamp = _msg->_header->_timestamp;
505}
506
507
508int rtp_add_resolution_marking ( rtp_session_t* _session, uint16_t _width, uint16_t _height )
509{
510 if ( !_session )
511 return FAILURE;
512
513 rtp_ext_header_t* _ext_header = _session->_ext_header;
514 _session->_exthdr_resolution = 0;
515
516 if ( ! ( _ext_header ) ) {
517 _session->_ext_header = calloc (sizeof(rtp_ext_header_t), 1);
518 assert(_session->_ext_header);
519
520 _session->_extension = 1;
521 _session->_ext_header->_ext_len = 1;
522 _ext_header = _session->_ext_header;
523 _session->_ext_header->_hd_ext = calloc(sizeof(uint32_t), 1);
524 assert(_session->_ext_header->_hd_ext);
525
526 } else { /* If there is need for more headers this will be needed to change */
527 if ( !(_ext_header->_ext_type & RTP_EXT_TYPE_RESOLUTION) ){
528 uint32_t _exthdr_framerate = _ext_header->_hd_ext[_session->_exthdr_framerate];
529 /* it's position is at 2nd place by default */
530 _session->_exthdr_framerate ++;
531
532 /* Update length */
533 _ext_header->_ext_len++;
534
535 /* Allocate the value */
536 _ext_header->_hd_ext = realloc(_ext_header->_hd_ext, sizeof(rtp_ext_header_t) * _ext_header->_ext_len);
537 assert(_ext_header->_hd_ext);
538
539 /* Reset other values */
540 _ext_header->_hd_ext[_session->_exthdr_framerate] = _exthdr_framerate;
541 }
542 }
543
544 /* Add flag */
545 _ext_header->_ext_type |= RTP_EXT_TYPE_RESOLUTION;
546
547 _ext_header->_hd_ext[_session->_exthdr_resolution] = _width << 16 | ( uint32_t ) _height;
548
549 return SUCCESS;
550}
551
552int rtp_remove_resolution_marking ( rtp_session_t* _session )
553{
554 if ( _session->_extension == 0 || ! ( _session->_ext_header ) ) {
555 t_perror ( RTP_ERROR_INVALID_EXTERNAL_HEADER );
556 return FAILURE;
557 }
558
559 if ( !( _session->_ext_header->_ext_type & RTP_EXT_TYPE_RESOLUTION ) ) {
560 t_perror ( RTP_ERROR_INVALID_EXTERNAL_HEADER );
561 return FAILURE;
562 }
563
564 _session->_ext_header->_ext_type &= ~RTP_EXT_TYPE_RESOLUTION; /* Remove the flag */
565 _session->_exthdr_resolution = -1; /* Remove identifier */
566
567 /* Check if extension is empty */
568 if ( _session->_ext_header->_ext_type == 0 ){
569
570 free ( _session->_ext_header->_hd_ext );
571 free ( _session->_ext_header );
572
573 _session->_ext_header = NULL; /* It's very important */
574 _session->_extension = 0;
575
576 } else {
577 _session->_ext_header->_ext_len --;
578
579 /* this will also be needed to change if there are more than 2 headers */
580 if ( _session->_ext_header->_ext_type & RTP_EXT_TYPE_FRAMERATE ){
581 memcpy(_session->_ext_header->_hd_ext + 1, _session->_ext_header->_hd_ext, _session->_ext_header->_ext_len);
582 _session->_exthdr_framerate = 0;
583 _session->_ext_header->_hd_ext = realloc( _session->_ext_header->_hd_ext, sizeof( rtp_ext_header_t ) * _session->_ext_header->_ext_len );
584 assert(_session->_ext_header->_hd_ext);
585 }
586 }
587
588 return SUCCESS;
589}
590
591int rtp_add_framerate_marking ( rtp_session_t* _session, uint32_t _value )
592{
593 if ( !_session )
594 return FAILURE;
595
596 rtp_ext_header_t* _ext_header = _session->_ext_header;
597 _session->_exthdr_framerate = 0;
598
599 if ( ! ( _ext_header ) ) {
600 _session->_ext_header = calloc (sizeof(rtp_ext_header_t), 1);
601 assert(_session->_ext_header);
602
603 _session->_extension = 1;
604 _session->_ext_header->_ext_len = 1;
605 _ext_header = _session->_ext_header;
606 _session->_ext_header->_hd_ext = calloc(sizeof(uint32_t), 1);
607 assert(_session->_ext_header->_hd_ext);
608 } else { /* If there is need for more headers this will be needed to change */
609 if ( !(_ext_header->_ext_type & RTP_EXT_TYPE_FRAMERATE) ){
610 /* it's position is at 2nd place by default */
611 _session->_exthdr_framerate ++;
612
613 /* Update length */
614 _ext_header->_ext_len++;
615
616 /* Allocate the value */
617 _ext_header->_hd_ext = realloc(_ext_header->_hd_ext, sizeof(rtp_ext_header_t) * _ext_header->_ext_len);
618 assert(_ext_header->_hd_ext);
619
620 }
621 }
622
623 /* Add flag */
624 _ext_header->_ext_type |= RTP_EXT_TYPE_FRAMERATE;
625
626 _ext_header->_hd_ext[_session->_exthdr_framerate] = _value;
627
628 return SUCCESS;
629}
630
631
632int rtp_remove_framerate_marking ( rtp_session_t* _session )
633{
634 if ( _session->_extension == 0 || ! ( _session->_ext_header ) ) {
635 t_perror ( RTP_ERROR_INVALID_EXTERNAL_HEADER );
636 return FAILURE;
637 }
638
639 if ( !( _session->_ext_header->_ext_type & RTP_EXT_TYPE_FRAMERATE ) ) {
640 t_perror ( RTP_ERROR_INVALID_EXTERNAL_HEADER );
641 return FAILURE;
642 }
643
644 _session->_ext_header->_ext_type &= ~RTP_EXT_TYPE_FRAMERATE; /* Remove the flag */
645 _session->_exthdr_framerate = -1; /* Remove identifier */
646 _session->_ext_header->_ext_len --;
647
648 /* Check if extension is empty */
649 if ( _session->_ext_header->_ext_type == 0 ){
650
651 free ( _session->_ext_header->_hd_ext );
652 free ( _session->_ext_header );
653
654 _session->_ext_header = NULL; /* It's very important */
655 _session->_extension = 0;
656
657 } else if ( !_session->_ext_header->_ext_len ) {
658
659 /* this will also be needed to change if there are more than 2 headers */
660 _session->_ext_header->_hd_ext = realloc( _session->_ext_header->_hd_ext, sizeof( rtp_ext_header_t ) * _session->_ext_header->_ext_len );
661 assert(_session->_ext_header->_hd_ext);
662
663 }
664
665 return SUCCESS;
666}
667
668uint32_t rtp_get_framerate_marking ( rtp_ext_header_t* _header )
669{
670 if ( _header->_ext_len == 1 ){
671 return _header->_hd_ext[0];
672 } else {
673 return _header->_hd_ext[1];
674 }
675}
676
677int rtp_set_prefix ( rtp_session_t* _session, uint8_t* _prefix, uint16_t _prefix_length )
678{
679 if ( !_session )
680 return FAILURE;
681
682 if ( _session->_prefix ) {
683 free ( _session->_prefix );
684 }
685
686 _session->_prefix = calloc ( ( sizeof * _session->_prefix ), _prefix_length );
687 assert(_session->_prefix);
688
689 t_memcpy ( _session->_prefix, _prefix, _prefix_length );
690 _session->_prefix_length = _prefix_length;
691
692 return SUCCESS;
693}
diff --git a/toxrtp/toxrtp.h b/toxrtp/toxrtp.h
deleted file mode 100644
index 0aa89993..00000000
--- a/toxrtp/toxrtp.h
+++ /dev/null
@@ -1,188 +0,0 @@
1/* rtp_impl.h
2 *
3 * Rtp implementation includes rtp_session_s struct which is a session identifier.
4 * It contains session information and it's a must for every session.
5 * It's best if you don't touch any variable directly but use callbacks to do so. !Red!
6 *
7 *
8 * Copyright (C) 2013 Tox project All Rights Reserved.
9 *
10 * This file is part of Tox.
11 *
12 * Tox is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * Tox is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
27
28#ifndef _RTP__IMPL_H_
29#define _RTP__IMPL_H_
30
31#define RTP_VERSION 2
32#include <inttypes.h>
33#include "tox.h"
34#include <pthread.h>
35/* Extension header flags */
36#define RTP_EXT_TYPE_RESOLUTION 0x01
37#define RTP_EXT_TYPE_FRAMERATE 0x02
38
39/* Some defines */
40
41#define RTP_PACKET 70
42
43/* Payload identifiers */
44
45/* Audio */
46#define _PAYLOAD_OPUS 96
47
48/* Video */
49#define _PAYLOAD_VP8 106
50
51/* End of Payload identifiers */
52
53/* End of defines */
54
55
56/* Our main session descriptor.
57 * It measures the session variables and controls
58 * the entire session. There are functions for manipulating
59 * the session so tend to use those instead of directly accessing
60 * session parameters.
61 */
62typedef struct rtp_session_s {
63 uint8_t _version;
64 uint8_t _padding;
65 uint8_t _extension;
66 uint8_t _cc;
67 uint8_t _marker;
68 uint8_t _payload_type;
69 uint16_t _sequence_number; /* Set when sending */
70 uint16_t _last_sequence_number; /* Check when recving msg */
71 uint32_t _initial_time;
72 uint32_t _time_elapsed;
73 uint32_t _current_timestamp;
74 uint32_t _ssrc;
75 uint32_t *_csrc;
76
77
78 /* If some additional data must be sent via message
79 * apply it here. Only by allocating this member you will be
80 * automatically placing it within a message.
81 */
82
83 struct rtp_ext_header_s *_ext_header;
84 /* External header identifiers */
85 int _exthdr_resolution;
86 int _exthdr_framerate;
87
88 int _max_users; /* -1 undefined */
89
90 uint64_t _packets_sent; /* measure packets */
91 uint64_t _packets_recv;
92
93 uint64_t _bytes_sent;
94 uint64_t _bytes_recv;
95
96 uint64_t _packet_loss;
97
98 const char *_last_error;
99
100 struct rtp_dest_list_s *_dest_list;
101 struct rtp_dest_list_s *_last_user; /* a tail for faster appending */
102
103 struct rtp_msg_s *_oldest_msg;
104 struct rtp_msg_s *_last_msg; /* tail */
105
106 uint16_t _prefix_length;
107 uint8_t *_prefix;
108
109 /* Specifies multiple session use.
110 * When using one session it uses default value ( -1 )
111 * Otherwise it's set to 1 and rtp_register_msg () is required
112 */
113 int _multi_session;
114
115 uint32_t _current_framerate;
116
117 pthread_mutex_t _mutex;
118
119} rtp_session_t;
120
121
122/*
123 * Now i don't believe we need to store this _from thing every time
124 * since we have csrc table but will leave it like this for a while
125 */
126
127
128void rtp_free_msg ( rtp_session_t *_session, struct rtp_msg_s *_msg );
129int rtp_release_session_recv ( rtp_session_t *_session );
130
131/* Functions handling receiving */
132struct rtp_msg_s *rtp_recv_msg ( rtp_session_t *_session );
133void rtp_store_msg ( rtp_session_t *_session, struct rtp_msg_s *_msg );
134
135/*
136 * rtp_msg_parse() stores headers separately from the payload data
137 * and so the _length variable is set accordingly
138 */
139struct rtp_msg_s *rtp_msg_parse ( rtp_session_t *_session, const uint8_t *_data, uint32_t _length );
140
141int rtp_check_late_message (rtp_session_t *_session, struct rtp_msg_s *_msg);
142void rtp_register_msg ( rtp_session_t *_session, struct rtp_msg_s * );
143
144/* Functions handling sending */
145int rtp_send_msg ( rtp_session_t *_session, struct rtp_msg_s *_msg, void *_core_handler );
146
147/*
148 * rtp_msg_new() stores headers and payload data in one container ( _data )
149 * and the _length is set accordingly. Returned message is used for sending only
150 * so there is not much use of the headers there
151 */
152struct rtp_msg_s *rtp_msg_new ( rtp_session_t *_session, const uint8_t *_data, uint32_t _length );
153
154
155/* Convenient functions for creating a header */
156struct rtp_header_s *rtp_build_header ( rtp_session_t *_session );
157
158/* Functions handling session control */
159
160/* Handling an rtp packet */
161/* int rtp_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); */
162
163/* Session initiation and termination.
164 * Set _multi_session to -1 if not using multiple sessions
165 */
166rtp_session_t *rtp_init_session ( int _max_users, int _multi_session );
167int rtp_terminate_session ( rtp_session_t *_session );
168
169/* Adding receiver */
170int rtp_add_receiver ( rtp_session_t *_session, tox_IP_Port *_dest );
171
172/* Convenient functions for marking the resolution */
173int rtp_add_resolution_marking ( rtp_session_t *_session, uint16_t _width, uint16_t _height );
174int rtp_remove_resolution_marking ( rtp_session_t *_session );
175uint16_t rtp_get_resolution_marking_height ( struct rtp_ext_header_s *_header, uint32_t _position );
176uint16_t rtp_get_resolution_marking_width ( struct rtp_ext_header_s *_header, uint32_t _position );
177
178int rtp_add_framerate_marking ( rtp_session_t *_session, uint32_t _value );
179int rtp_remove_framerate_marking ( rtp_session_t *_session );
180uint32_t rtp_get_framerate_marking ( struct rtp_ext_header_s *_header );
181/* Convenient functions for marking the payload */
182void rtp_set_payload_type ( rtp_session_t *_session, uint8_t _payload_value );
183uint32_t rtp_get_payload_type ( rtp_session_t *_session );
184
185/* When using RTP in core be sure to set prefix when sending via rtp_send_msg */
186int rtp_set_prefix ( rtp_session_t *_session, uint8_t *_prefix, uint16_t _prefix_length );
187
188#endif /* _RTP__IMPL_H_ */
diff --git a/toxrtp/toxrtp_error.c b/toxrtp/toxrtp_error.c
deleted file mode 100644
index 3a7ff9a5..00000000
--- a/toxrtp/toxrtp_error.c
+++ /dev/null
@@ -1,68 +0,0 @@
1
2#ifdef HAVE_CONFIG_H
3#include "config.h"
4#endif /* HAVE_CONFIG_H */
5
6#include "toxrtp_error.h"
7#include "toxrtp_helper.h"
8
9#include <stdio.h>
10#include <stdarg.h>
11#include <string.h>
12#include <stdlib.h>
13#include <assert.h>
14
15typedef struct rtp_error_s {
16 char* _message;
17 int _id;
18
19} rtp_error_t;
20
21static rtp_error_t* _register = NULL;
22static size_t _it = 0;
23
24void t_rtperr_register ( int _id, const char* _info )
25{
26 size_t _info_size = strlen ( _info );
27
28 if ( !_register ) {
29 _register = calloc ( sizeof ( rtp_error_t ), 1 );
30 } else {
31 _register = realloc ( _register, sizeof ( rtp_error_t ) * ( _it + 1 ) );
32 }
33 assert(_register);
34
35
36 rtp_error_t* _current = & _register[_it];
37
38 _current->_id = _id;
39 _current->_message = calloc ( sizeof(char), _info_size );
40 assert(_current->_message);
41
42 t_memcpy ( (uint8_t*)_current->_message, (const uint8_t*)_info, _info_size );
43 _it ++;
44}
45
46const char* t_rtperr ( int _errno )
47{
48 if ( !_register )
49 return "Unregistered";
50
51 uint32_t _i;
52
53 for ( _i = _it; _i--; ) {
54 if ( _register[_i]._id == _errno ) {
55 return _register[_i]._message;
56 }
57 }
58
59 return "Invalid error id!";
60}
61
62void t_rtperr_print ( const char* _val, ... )
63{
64 va_list _args;
65 va_start ( _args, _val );
66 vfprintf ( stderr, _val, _args );
67 va_end ( _args );
68}
diff --git a/toxrtp/toxrtp_error.h b/toxrtp/toxrtp_error.h
deleted file mode 100644
index 0e017246..00000000
--- a/toxrtp/toxrtp_error.h
+++ /dev/null
@@ -1,25 +0,0 @@
1#ifndef _RTP_ERROR_
2#define _RTP_ERROR_
3
4#define PRINT_FORMAT "Error %d: %s at %s:%d\n"
5#define PRINT_ARGS( _errno ) _errno, t_rtperr(_errno), __FILE__, __LINE__
6
7
8const char* t_rtperr ( int _errno );
9void t_rtperr_register ( int _id, const char* _info );
10
11void t_invoke_error ( int _id );
12void t_rtperr_print ( const char* _val, ... );
13
14
15#ifdef _USE_ERRORS
16#define t_perror( _errno ) t_rtperr_print ( PRINT_FORMAT, PRINT_ARGS ( _errno ) )
17#else
18#define t_perror( _errno )do { } while(0)
19#endif /* _USE_ERRORS */
20
21#ifdef _STDIO_H
22#define t_errexit( _errno ) exit(-_errno)
23#endif /* _STDIO_H */
24
25#endif /* _RTP_ERROR_ */
diff --git a/toxrtp/toxrtp_error_id.h b/toxrtp/toxrtp_error_id.h
deleted file mode 100644
index 201aa936..00000000
--- a/toxrtp/toxrtp_error_id.h
+++ /dev/null
@@ -1,32 +0,0 @@
1#ifndef _RTP_ERROR_ID_
2#define _RTP_ERROR_ID_
3
4#include "toxrtp_error.h"
5
6typedef enum error_s {
7 RTP_ERROR_PACKET_DROPED = 1,
8 RTP_ERROR_EMPTY_MESSAGE,
9 RTP_ERROR_STD_SEND_FAILURE,
10 RTP_ERROR_NO_EXTERNAL_HEADER,
11 RTP_ERROR_INVALID_EXTERNAL_HEADER,
12 RTP_ERROR_HEADER_PARSING,
13 RTP_ERROR_PAYLOAD_NULL,
14 RTP_ERROR_PAYLOAD_INVALID,
15
16} error_t;
17
18
19/* Only needed to be called once */
20#ifndef REGISTER_RTP_ERRORS
21#define REGISTER_RTP_ERRORS \
22 t_rtperr_register( RTP_ERROR_PACKET_DROPED, "Ivalid sequence number, packet is late" ); \
23 t_rtperr_register( RTP_ERROR_EMPTY_MESSAGE, "Tried to send an empty message" ); \
24 t_rtperr_register( RTP_ERROR_STD_SEND_FAILURE, "Failed call function: sendto" ); \
25 t_rtperr_register( RTP_ERROR_NO_EXTERNAL_HEADER, "While parsing external header" ); \
26 t_rtperr_register( RTP_ERROR_INVALID_EXTERNAL_HEADER, "While parsing external header" ); \
27 t_rtperr_register( RTP_ERROR_HEADER_PARSING, "While parsing header" ); \
28 t_rtperr_register( RTP_ERROR_PAYLOAD_NULL, "Payload is NULL" ); \
29 t_rtperr_register( RTP_ERROR_PAYLOAD_INVALID, "Invalid payload size" );
30#endif /* REGISTER_RTP_ERRORS */
31
32#endif /* _RTP_ERROR_ID_ */
diff --git a/toxrtp/toxrtp_helper.c b/toxrtp/toxrtp_helper.c
deleted file mode 100644
index 6f952359..00000000
--- a/toxrtp/toxrtp_helper.c
+++ /dev/null
@@ -1,208 +0,0 @@
1/* rtp_helper.c
2*
3* Has some standard functions. !Red!
4*
5*
6* Copyright (C) 2013 Tox project All Rights Reserved.
7*
8* This file is part of Tox.
9*
10* Tox is free software: you can redistribute it and/or modify
11* it under the terms of the GNU General Public License as published by
12* the Free Software Foundation, either version 3 of the License, or
13* (at your option) any later version.
14*
15* Tox is distributed in the hope that it will be useful,
16* but WITHOUT ANY WARRANTY; without even the implied warranty of
17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18* GNU General Public License for more details.
19*
20* You should have received a copy of the GNU General Public License
21* along with Tox. If not, see <http://www.gnu.org/licenses/>.
22*
23*/
24
25
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif /* HAVE_CONFIG_H */
30
31#include "toxrtp_helper.h"
32#include "../toxcore/network.h"
33
34#include <assert.h>
35
36#ifdef WIN
37#include <windows.h>
38#endif /* WIN */
39
40
41static int _seed = 0; /* Not initiated */
42
43int t_setipport ( const char *_ip, unsigned short _port, void *_dest )
44{
45 assert(_dest);
46
47 IP_Port *_dest_c = ( IP_Port * ) _dest;
48 ip_init(&_dest_c->ip, 0);
49
50 IP_Port _ipv6_garbage;
51
52 if ( !addr_resolve(_ip, &_dest_c->ip, &_ipv6_garbage.ip) )
53 return FAILURE;
54
55 _dest_c->port = htons ( _port );
56
57 return SUCCESS;
58}
59
60uint32_t t_random ( uint32_t _max )
61{
62 if ( !_seed ) {
63 srand ( t_time() );
64 _seed++;
65 }
66
67 if ( _max <= 0 ) {
68 return ( unsigned ) rand();
69 } else {
70 return ( unsigned ) rand() % _max;
71 }
72}
73
74void t_memcpy ( uint8_t *_dest, const uint8_t *_source, size_t _size )
75{
76 /*
77 * Using countdown to zero method
78 * It's faster than for(_it = 0; _it < _size; _it++);
79 */
80 size_t _it = _size;
81
82 do {
83 _it--;
84 _dest[_it] = _source[_it];
85 } while ( _it );
86
87}
88
89uint8_t *t_memset ( uint8_t *_dest, uint8_t _valu, size_t _size )
90{
91 /*
92 * Again using countdown to zero method
93 */
94 size_t _it = _size;
95
96 do {
97 _it--;
98 _dest[_it] = _valu;
99 } while ( _it );
100
101 return _dest;
102}
103
104size_t t_memlen ( const uint8_t *_valu)
105{
106 const uint8_t *_it;
107 size_t _retu = 0;
108
109 for ( _it = _valu; *_it; ++_it ) ++_retu;
110
111 return _retu;
112}
113
114uint8_t *t_strallcpy ( const uint8_t *_source ) /* string alloc and copy */
115{
116 assert(_source);
117
118 size_t _length = t_memlen(_source) + 1; /* make space for null character */
119
120 uint8_t *_dest = calloc( sizeof ( uint8_t ), _length );
121 assert(_dest);
122
123 t_memcpy(_dest, _source, _length);
124
125 return _dest;
126}
127
128size_t t_strfind ( const uint8_t *_str, const uint8_t *_substr )
129{
130 size_t _pos = 0;
131 size_t _it, _delit = 0;
132
133 for ( _it = 0; _str[_it] != '\0'; _it++ ) {
134 if ( _str[_it] == _substr[_delit] ) {
135 _pos = _it;
136
137 while ( _str[_it] == _substr[_delit] && _str[_it] != '\0' ) {
138 _it ++;
139 _delit++;
140
141 if ( _substr[_delit] == '\0' ) {
142 return _pos;
143 }
144 }
145
146 _delit = 0;
147 _pos = 0;
148 }
149 }
150
151 return _pos;
152}
153
154#ifdef WIN
155#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
156#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
157#else
158#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
159#endif
160
161struct timezone {
162 int tz_minuteswest; /* minutes W of Greenwich */
163 int tz_dsttime; /* type of dst correction */
164};
165
166int gettimeofday(struct timeval *tv, struct timezone *tz)
167{
168 FILETIME ft;
169 unsigned __int64 tmpres = 0;
170 static int tzflag;
171
172 if (NULL != tv) {
173 GetSystemTimeAsFileTime(&ft);
174
175 tmpres |= ft.dwHighDateTime;
176 tmpres <<= 32;
177 tmpres |= ft.dwLowDateTime;
178
179 /*converting file time to unix epoch*/
180 tmpres -= DELTA_EPOCH_IN_MICROSECS;
181 tmpres /= 10; /*convert into microseconds*/
182 tv->tv_sec = (long)(tmpres / 1000000UL);
183 tv->tv_usec = (long)(tmpres % 1000000UL);
184 }
185
186 if (NULL != tz) {
187 if (!tzflag) {
188 _tzset();
189 tzflag++;
190 }
191
192 tz->tz_minuteswest = _timezone / 60;
193 tz->tz_dsttime = _daylight;
194 }
195
196 return 0;
197}
198#endif /* WIN */
199
200
201uint64_t t_time()
202{
203 struct timeval _tv;
204 gettimeofday(&_tv, NULL);
205 uint64_t _retu_usec = _tv.tv_sec % 1000000; /* get 6 digits an leave space for 3 more */
206 _retu_usec = _retu_usec * 1000 + (_tv.tv_usec / 1000 );
207 return _retu_usec;
208}
diff --git a/toxrtp/toxrtp_helper.h b/toxrtp/toxrtp_helper.h
deleted file mode 100644
index c9bcfcca..00000000
--- a/toxrtp/toxrtp_helper.h
+++ /dev/null
@@ -1,77 +0,0 @@
1/* rtp_helper.h
2*
3* Has some standard functions. !Red!
4*
5*
6* Copyright (C) 2013 Tox project All Rights Reserved.
7*
8* This file is part of Tox.
9*
10* Tox is free software: you can redistribute it and/or modify
11* it under the terms of the GNU General Public License as published by
12* the Free Software Foundation, either version 3 of the License, or
13* (at your option) any later version.
14*
15* Tox is distributed in the hope that it will be useful,
16* but WITHOUT ANY WARRANTY; without even the implied warranty of
17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18* GNU General Public License for more details.
19*
20* You should have received a copy of the GNU General Public License
21* along with Tox. If not, see <http://www.gnu.org/licenses/>.
22*
23*/
24
25#ifndef _RTP__HELPER_H_
26#define _RTP__HELPER_H_
27
28#include <time.h>
29#include <inttypes.h>
30
31/* Current time, unix format */
32/*#define _time ((uint32_t)time(NULL))*/
33
34#define SUCCESS 0
35#define FAILURE -1
36
37#define _USE_ERRORS
38
39#define RUN_IN_THREAD(_func, _thread_id, _arg_ptr) \
40if ( pthread_create ( &_thread_id, NULL, _func, _arg_ptr ) ) { \
41 pthread_detach ( _thread_id ); \
42}
43
44/* Core adaptation helper */
45int t_setipport ( const char* _ip, unsigned short _port, void* _cont );
46uint32_t t_random ( uint32_t _max );
47
48/* It's a bit faster than the memcpy it self and more optimized for using
49 * a uint8_t since memcpy has optimizations when copying "words" i.e. long type.
50 * Otherwise it just copies char's while we need only uint8_t
51 */
52void t_memcpy ( uint8_t* _dest, const uint8_t* _source, size_t _size );
53
54
55/* This is our memset. It's also a bit faster than the memset for it
56 * does not cast _dest to char* and uses faster loop algorithm.
57 */
58uint8_t* t_memset ( uint8_t* _dest, uint8_t _valu, size_t _size );
59
60/* Get null terminated len */
61size_t t_memlen ( const uint8_t* _valu );
62
63/* finds location of substring */
64size_t t_strfind ( const uint8_t* _str, const uint8_t* _substr );
65
66/* string alloc and copy ( ! must be null terminated ) */
67uint8_t* t_strallcpy ( const uint8_t* _source );
68
69/* Get current time in milliseconds */
70uint64_t t_time();
71
72
73#endif /* _RTP__HELPER_H_ */
74
75
76
77
diff --git a/toxrtp/toxrtp_message.c b/toxrtp/toxrtp_message.c
deleted file mode 100644
index e7f1f2c0..00000000
--- a/toxrtp/toxrtp_message.c
+++ /dev/null
@@ -1,351 +0,0 @@
1/* rtp_message.c
2 *
3 * Rtp Message handler. It handles message/header parsing.
4 * Refer to RTP: A Transport Protocol for Real-Time Applications ( RFC 3550 ) for more info. !Red!
5 *
6 *
7 * Copyright (C) 2013 Tox project All Rights Reserved.
8 *
9 * This file is part of Tox.
10 *
11 * Tox is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * Tox is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26#ifdef HAVE_CONFIG_H
27#include "config.h"
28#endif /* HAVE_CONFIG_H */
29
30#include "toxrtp_message.h"
31#include "toxrtp.h"
32#include <stdio.h>
33
34#ifdef _USE_ERRORS
35#include "toxrtp_error_id.h"
36#endif /* _USE_ERRORS */
37
38#include <assert.h>
39
40/* Some defines */
41
42/* End of defines */
43
44void rtp_header_print (const rtp_header_t* _header)
45{
46 printf("Header: \n"
47 "Version: %d\n"
48 "Padding: %d\n"
49 "Ext: %d\n"
50 "CC: %d\n"
51 "marker: %d\n"
52 "payload typ:%d\n\n"
53 "sequ num: %d\n"
54 "Timestamp: %d\n"
55 "SSrc: %d\n"
56 "CSrc: %d\n"
57 "Lenght: %d\n"
58 ,rtp_header_get_flag_version(_header)
59 ,rtp_header_get_flag_padding(_header)
60 ,rtp_header_get_flag_extension(_header)
61 ,rtp_header_get_flag_CSRC_count(_header)
62 ,rtp_header_get_setting_marker(_header)
63 ,rtp_header_get_setting_payload_type(_header)
64 ,_header->_sequence_number
65 ,_header->_timestamp
66 ,_header->_ssrc
67 ,_header->_csrc[0]
68 ,_header->_length
69 );
70}
71
72rtp_header_t* rtp_extract_header ( const uint8_t* _payload, size_t _bytes )
73{
74 if ( !_payload ) {
75 t_perror ( RTP_ERROR_PAYLOAD_NULL );
76 return NULL;
77 }
78 const uint8_t* _it = _payload;
79
80 rtp_header_t* _retu = calloc(sizeof(rtp_header_t), 1);
81 assert(_retu);
82
83 _retu->_flags = *_it; ++_it;
84
85 /* This indicates if the first 2 bytes are valid.
86 * Now it my happen that this is out of order but
87 * it cuts down chances of parsing some invalid value
88 */
89 if ( rtp_header_get_flag_version(_retu) != RTP_VERSION ){
90 printf("Invalid version: %d\n", rtp_header_get_flag_version(_retu));
91 //assert(rtp_header_get_flag_version(_retu) == RTP_VERSION);
92 /* Deallocate */
93 //DEALLOCATOR(_retu);
94 //return NULL;
95 }
96
97 /*
98 * Added a check for the size of the header little sooner so
99 * I don't need to parse the other stuff if it's bad
100 */
101 uint8_t cc = rtp_header_get_flag_CSRC_count ( _retu );
102 uint32_t _lenght = _MIN_HEADER_LENGTH + ( cc * 4 );
103
104 if ( _bytes < _lenght ) {
105 t_perror ( RTP_ERROR_PAYLOAD_INVALID );
106 return NULL;
107 }
108
109 if ( cc > 0 ) {
110 _retu->_csrc = calloc ( sizeof ( uint32_t ), cc );
111 assert(_retu->_csrc);
112
113 } else { /* But this should not happen ever */
114 t_perror ( RTP_ERROR_HEADER_PARSING );
115 return NULL;
116 }
117
118
119 _retu->_marker_payload_t = *_it; ++_it;
120 _retu->_length = _lenght;
121 _retu->_sequence_number = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 );
122
123 _it += 2;
124
125 _retu->_timestamp = ( ( uint32_t ) * _it << 24 ) |
126 ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
127 ( ( uint32_t ) * ( _it + 2 ) << 8 ) |
128 ( * ( _it + 3 ) ) ;
129
130 _it += 4;
131
132 _retu->_ssrc = ( ( uint32_t ) * _it << 24 ) |
133 ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
134 ( ( uint32_t ) * ( _it + 2 ) << 8 ) |
135 ( ( uint32_t ) * ( _it + 3 ) ) ;
136
137
138 size_t x;
139 for ( x = 0; x < cc; x++ ) {
140 _it += 4;
141 _retu->_csrc[x] = ( ( uint32_t ) * _it << 24 ) |
142 ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
143 ( ( uint32_t ) * ( _it + 2 ) << 8 ) |
144 ( ( uint32_t ) * ( _it + 3 ) ) ;
145 }
146
147 return _retu;
148}
149
150rtp_ext_header_t* rtp_extract_ext_header ( const uint8_t* _payload, size_t _bytes )
151{
152 if ( !_payload ) {
153 t_perror ( RTP_ERROR_PAYLOAD_NULL );
154 return NULL;
155 }
156
157
158
159 const uint8_t* _it = _payload;
160
161 rtp_ext_header_t* _retu = calloc(sizeof(rtp_ext_header_t), 1);
162 assert(_retu);
163
164 uint16_t _ext_len = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 ); _it += 2;
165
166 if ( _bytes < ( _ext_len * sizeof(uint32_t) ) ) {
167 t_perror ( RTP_ERROR_PAYLOAD_INVALID );
168 return NULL;
169 }
170
171 _retu->_ext_len = _ext_len;
172 _retu->_ext_type = ( ( uint16_t ) * _it << 8 ) | * ( _it + 1 ); _it -= 2;
173
174 _retu->_hd_ext = calloc(sizeof(uint32_t), _ext_len);
175 assert(_retu->_hd_ext);
176
177 uint32_t* _hd_ext = _retu->_hd_ext;
178 size_t i;
179 for ( i = 0; i < _ext_len; i++ ) {
180 _it += 4;
181 _hd_ext[i] = ( ( uint32_t ) * _it << 24 ) |
182 ( ( uint32_t ) * ( _it + 1 ) << 16 ) |
183 ( ( uint32_t ) * ( _it + 2 ) << 8 ) |
184 ( ( uint32_t ) * ( _it + 3 ) ) ;
185 }
186
187 return _retu;
188}
189
190uint8_t* rtp_add_header ( rtp_header_t* _header, uint8_t* _payload )
191{
192 uint8_t cc = rtp_header_get_flag_CSRC_count ( _header );
193
194 uint8_t* _it = _payload;
195
196 *_it = _header->_flags; ++_it;
197 *_it = _header->_marker_payload_t; ++_it;
198
199 *_it = ( _header->_sequence_number >> 8 ); ++_it;
200 *_it = ( _header->_sequence_number ); ++_it;
201
202 uint32_t _timestamp = _header->_timestamp;
203 *_it = ( _timestamp >> 24 ); ++_it;
204 *_it = ( _timestamp >> 16 ); ++_it;
205 *_it = ( _timestamp >> 8 ); ++_it;
206 *_it = ( _timestamp ); ++_it;
207
208 uint32_t _ssrc = _header->_ssrc;
209 *_it = ( _ssrc >> 24 ); ++_it;
210 *_it = ( _ssrc >> 16 ); ++_it;
211 *_it = ( _ssrc >> 8 ); ++_it;
212 *_it = ( _ssrc );
213
214 uint32_t *_csrc = _header->_csrc;
215 size_t x;
216 for ( x = 0; x < cc; x++ ) {
217 ++_it;
218 *_it = ( _csrc[x] >> 24 ); ++_it;
219 *_it = ( _csrc[x] >> 16 ); ++_it;
220 *_it = ( _csrc[x] >> 8 ); ++_it;
221 *_it = ( _csrc[x] );
222 }
223
224 return _it;
225}
226
227uint8_t* rtp_add_extention_header ( rtp_ext_header_t* _header, uint8_t* _payload )
228{
229 uint8_t* _it = _payload;
230
231 *_it = ( _header->_ext_len >> 8 ); _it++;
232 *_it = ( _header->_ext_len ); _it++;
233
234 *_it = ( _header->_ext_type >> 8 ); ++_it;
235 *_it = ( _header->_ext_type );
236
237 size_t x;
238
239 uint32_t* _hd_ext = _header->_hd_ext;
240 for ( x = 0; x < _header->_ext_len; x++ ) {
241 ++_it;
242 *_it = ( _hd_ext[x] >> 24 ); ++_it;
243 *_it = ( _hd_ext[x] >> 16 ); ++_it;
244 *_it = ( _hd_ext[x] >> 8 ); ++_it;
245 *_it = ( _hd_ext[x] );
246 }
247
248 return _it;
249}
250
251size_t rtp_header_get_size ( const rtp_header_t* _header )
252{
253 return ( 8 + ( rtp_header_get_flag_CSRC_count ( _header ) * 4 ) );
254}
255/* Setting flags */
256
257void rtp_header_add_flag_version ( rtp_header_t* _header, uint32_t value )
258{
259 ( _header->_flags ) &= 0x3F;
260 ( _header->_flags ) |= ( ( ( value ) << 6 ) & 0xC0 );
261}
262
263void rtp_header_add_flag_padding ( rtp_header_t* _header, uint32_t value )
264{
265 if ( value > 0 ) {
266 value = 1; /* It can only be 1 */
267 }
268
269 ( _header->_flags ) &= 0xDF;
270 ( _header->_flags ) |= ( ( ( value ) << 5 ) & 0x20 );
271}
272
273void rtp_header_add_flag_extension ( rtp_header_t* _header, uint32_t value )
274{
275 if ( value > 0 ) {
276 value = 1; /* It can only be 1 */
277 }
278
279 ( _header->_flags ) &= 0xEF;
280 ( _header->_flags ) |= ( ( ( value ) << 4 ) & 0x10 );
281}
282
283void rtp_header_add_flag_CSRC_count ( rtp_header_t* _header, uint32_t value )
284{
285 ( _header->_flags ) &= 0xF0;
286 ( _header->_flags ) |= ( ( value ) & 0x0F );
287}
288
289void rtp_header_add_setting_marker ( rtp_header_t* _header, uint32_t value )
290{
291 if ( value > 1 )
292 value = 1;
293
294 ( _header->_marker_payload_t ) &= 0x7F;
295 ( _header->_marker_payload_t ) |= ( ( ( value ) << 7 ) /*& 0x80 */ );
296}
297
298void rtp_header_add_setting_payload ( rtp_header_t* _header, uint32_t value )
299{
300 if ( value > 127 )
301 value = 127; /* Well set to maximum */
302
303 ( _header->_marker_payload_t ) &= 0x80;
304 ( _header->_marker_payload_t ) |= ( ( value ) /* & 0x7F */ );
305}
306
307/* Getting values from flags */
308uint8_t rtp_header_get_flag_version ( const rtp_header_t* _header )
309{
310 return ( _header->_flags & 0xd0 ) >> 6;
311}
312
313uint8_t rtp_header_get_flag_padding ( const rtp_header_t* _header )
314{
315 return ( _header->_flags & 0x20 ) >> 5;
316}
317
318uint8_t rtp_header_get_flag_extension ( const rtp_header_t* _header )
319{
320 return ( _header->_flags & 0x10 ) >> 4;
321}
322
323uint8_t rtp_header_get_flag_CSRC_count ( const rtp_header_t* _header )
324{
325 return ( _header->_flags & 0x0f );
326}
327uint8_t rtp_header_get_setting_marker ( const rtp_header_t* _header )
328{
329 return ( _header->_marker_payload_t ) >> 7;
330}
331uint8_t rtp_header_get_setting_payload_type ( const rtp_header_t* _header )
332{
333 /*
334 uint8_t _retu;
335
336 if ( _header->_marker_payload_t >> 7 == 1 ) {
337 _header->_marker_payload_t ^= 0x80;
338 _retu = _header->_marker_payload_t;
339 _header->_marker_payload_t ^= 0x80;
340 } else {
341 _retu = _header->_marker_payload_t;
342 }
343 */
344 /* return to start value
345 return _retu; */
346 return _header->_marker_payload_t & 0x7f;
347}
348
349/* */
350
351
diff --git a/toxrtp/toxrtp_message.h b/toxrtp/toxrtp_message.h
deleted file mode 100644
index 8feea5d9..00000000
--- a/toxrtp/toxrtp_message.h
+++ /dev/null
@@ -1,111 +0,0 @@
1/* rtp_message.h
2 *
3 * Rtp Message handler. It handles message/header parsing.
4 * Refer to RTP: A Transport Protocol for Real-Time Applications ( RFC 3550 ) for more info. !Red!
5 *
6 *
7 * Copyright (C) 2013 Tox project All Rights Reserved.
8 *
9 * This file is part of Tox.
10 *
11 * Tox is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * Tox is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26#ifndef _RTP__MESSAGE_H_
27#define _RTP__MESSAGE_H_
28
29#include "../toxcore/network.h"
30#include "toxrtp_helper.h"
31#include "../toxcore/tox.h"
32/* Defines */
33
34#define _MAX_SEQU_NUM 65535
35
36/* Minimum size */
37#define _MIN_HEADER_LENGTH 12
38#define _MIN_EXT_HEADER_LENGTH 4
39
40/* End of defines */
41
42
43typedef struct rtp_dest_list_s {
44 tox_IP_Port _dest;
45 struct rtp_dest_list_s* next;
46
47} rtp_dest_list_t;
48
49typedef struct rtp_header_s {
50 uint8_t _flags; /* Version(2),Padding(1), Ext(1), Cc(4) */
51 uint8_t _marker_payload_t; /* Marker(1), PlayLoad Type(7) */
52 uint16_t _sequence_number; /* Sequence Number */
53 uint32_t _timestamp; /* Timestamp */
54 uint32_t _ssrc; /* SSRC */
55 uint32_t* _csrc; /* CSRC's table */
56
57 uint32_t _length; /* A little something for allocation */
58
59} rtp_header_t;
60
61typedef struct rtp_ext_header_s {
62 uint16_t _ext_type; /* Extension profile */
63 uint16_t _ext_len; /* Number of extensions */
64 uint32_t* _hd_ext; /* Extension's table */
65
66
67} rtp_ext_header_t;
68
69typedef struct rtp_msg_s {
70 struct rtp_header_s* _header;
71 struct rtp_ext_header_s* _ext_header;
72 uint32_t _header_lenght;
73
74 uint8_t* _data;
75 uint32_t _length;
76 tox_IP_Port _from;
77
78 struct rtp_msg_s* _next;
79} rtp_msg_t;
80
81/* Extracts the header from the payload starting at _from */
82rtp_header_t* rtp_extract_header ( const uint8_t* _payload, size_t _bytes );
83rtp_ext_header_t* rtp_extract_ext_header ( const uint8_t* _payload, size_t _bytes );
84
85
86uint8_t* rtp_add_header ( rtp_header_t* _header, uint8_t* _payload );
87uint8_t* rtp_add_extention_header ( rtp_ext_header_t* _header, uint8_t* _payload );
88
89/* Gets the size of the header _header in bytes */
90size_t rtp_header_get_size ( const rtp_header_t* _header );
91
92void rtp_header_print (const rtp_header_t* _header);
93
94/* Adding flags and settings */
95void rtp_header_add_flag_version ( rtp_header_t* _header, uint32_t value );
96void rtp_header_add_flag_padding ( rtp_header_t* _header, uint32_t value );
97void rtp_header_add_flag_extension ( rtp_header_t* _header, uint32_t value );
98void rtp_header_add_flag_CSRC_count ( rtp_header_t* _header, uint32_t value );
99void rtp_header_add_setting_marker ( rtp_header_t* _header, uint32_t value );
100void rtp_header_add_setting_payload ( rtp_header_t* _header, uint32_t value );
101
102
103/* Getting values from flags and settings */
104uint8_t rtp_header_get_flag_version ( const rtp_header_t* _header );
105uint8_t rtp_header_get_flag_padding ( const rtp_header_t* _header );
106uint8_t rtp_header_get_flag_extension ( const rtp_header_t* _header );
107uint8_t rtp_header_get_flag_CSRC_count ( const rtp_header_t* _header );
108uint8_t rtp_header_get_setting_marker ( const rtp_header_t* _header );
109uint8_t rtp_header_get_setting_payload_type ( const rtp_header_t* _header );
110
111#endif /* _RTP__MESSAGE_H_ */