ffi-tox 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/ProjectTox-Core/AUTHORS +0 -0
  3. data/ProjectTox-Core/ChangeLog +0 -0
  4. data/ProjectTox-Core/INSTALL +370 -0
  5. data/ProjectTox-Core/INSTALL.md +455 -56
  6. data/ProjectTox-Core/Makefile.am +35 -0
  7. data/ProjectTox-Core/NEWS +0 -0
  8. data/ProjectTox-Core/README +43 -0
  9. data/ProjectTox-Core/README.md +34 -44
  10. data/ProjectTox-Core/auto_tests/Makefile.inc +110 -0
  11. data/ProjectTox-Core/auto_tests/TCP_test.c +519 -0
  12. data/ProjectTox-Core/auto_tests/assoc_test.c +160 -0
  13. data/ProjectTox-Core/auto_tests/crypto_test.c +302 -0
  14. data/ProjectTox-Core/auto_tests/dht_test.c +362 -0
  15. data/ProjectTox-Core/auto_tests/encryptsave_test.c +104 -0
  16. data/ProjectTox-Core/auto_tests/friends_test.c +238 -0
  17. data/ProjectTox-Core/auto_tests/helpers.h +15 -0
  18. data/ProjectTox-Core/auto_tests/messenger_test.c +365 -0
  19. data/ProjectTox-Core/auto_tests/network_test.c +171 -0
  20. data/ProjectTox-Core/auto_tests/onion_test.c +363 -0
  21. data/ProjectTox-Core/auto_tests/skeleton_test.c +49 -0
  22. data/ProjectTox-Core/auto_tests/tox_test.c +454 -0
  23. data/ProjectTox-Core/auto_tests/toxav_basic_test.c +597 -0
  24. data/ProjectTox-Core/auto_tests/toxav_many_test.c +402 -0
  25. data/ProjectTox-Core/autogen.sh +6 -0
  26. data/ProjectTox-Core/build/Makefile.am +14 -0
  27. data/ProjectTox-Core/configure.ac +694 -0
  28. data/ProjectTox-Core/dist-build/android-arm.sh +3 -0
  29. data/ProjectTox-Core/dist-build/android-armv7.sh +3 -0
  30. data/ProjectTox-Core/dist-build/android-build.sh +59 -0
  31. data/ProjectTox-Core/dist-build/android-mips.sh +3 -0
  32. data/ProjectTox-Core/dist-build/android-x86.sh +3 -0
  33. data/ProjectTox-Core/docs/Group-Chats.md +71 -0
  34. data/ProjectTox-Core/docs/Hardening.txt +60 -0
  35. data/ProjectTox-Core/docs/Hardening_docs.txt +30 -0
  36. data/ProjectTox-Core/docs/Prevent_Tracking.txt +160 -0
  37. data/ProjectTox-Core/docs/TCP_Network.txt +154 -0
  38. data/ProjectTox-Core/docs/TODO +62 -0
  39. data/ProjectTox-Core/docs/Tox_middle_level_network_protocol.txt +120 -0
  40. data/ProjectTox-Core/docs/av_api.md +194 -0
  41. data/ProjectTox-Core/libtoxav.pc.in +11 -0
  42. data/ProjectTox-Core/libtoxcore.pc.in +11 -0
  43. data/ProjectTox-Core/m4/ax_have_epoll.m4 +104 -0
  44. data/ProjectTox-Core/m4/ax_pthread.m4 +317 -0
  45. data/ProjectTox-Core/m4/pkg.m4 +199 -0
  46. data/ProjectTox-Core/other/DHT_bootstrap.c +121 -58
  47. data/ProjectTox-Core/other/DHTnodes +3 -0
  48. data/ProjectTox-Core/other/Makefile.inc +20 -0
  49. data/ProjectTox-Core/other/bootstrap_node_packets.c +65 -0
  50. data/ProjectTox-Core/other/tox.png +0 -0
  51. data/ProjectTox-Core/testing/DHT_test.c +170 -98
  52. data/ProjectTox-Core/testing/Makefile.inc +112 -0
  53. data/ProjectTox-Core/testing/Messenger_test.c +133 -69
  54. data/ProjectTox-Core/testing/dns3_test.c +115 -0
  55. data/ProjectTox-Core/testing/misc_tools.c +59 -13
  56. data/ProjectTox-Core/testing/nTox.c +1127 -264
  57. data/ProjectTox-Core/testing/nTox.h +10 -19
  58. data/ProjectTox-Core/testing/tox_shell.c +159 -0
  59. data/ProjectTox-Core/testing/tox_sync.c +299 -0
  60. data/ProjectTox-Core/tools/README +11 -0
  61. data/ProjectTox-Core/tools/astylerc +11 -0
  62. data/ProjectTox-Core/tools/pre-commit +17 -0
  63. data/ProjectTox-Core/toxav/Makefile.inc +36 -0
  64. data/ProjectTox-Core/toxav/codec.c +357 -0
  65. data/ProjectTox-Core/toxav/codec.h +116 -0
  66. data/ProjectTox-Core/toxav/msi.c +1949 -0
  67. data/ProjectTox-Core/toxav/msi.h +267 -0
  68. data/ProjectTox-Core/toxav/rtp.c +600 -0
  69. data/ProjectTox-Core/toxav/rtp.h +196 -0
  70. data/ProjectTox-Core/toxav/toxav.c +1148 -0
  71. data/ProjectTox-Core/toxav/toxav.h +389 -0
  72. data/ProjectTox-Core/toxcore/DHT.c +2521 -0
  73. data/ProjectTox-Core/toxcore/DHT.h +412 -0
  74. data/ProjectTox-Core/toxcore/LAN_discovery.c +322 -0
  75. data/ProjectTox-Core/{core → toxcore}/LAN_discovery.h +17 -12
  76. data/ProjectTox-Core/toxcore/Makefile.inc +67 -0
  77. data/ProjectTox-Core/toxcore/Messenger.c +3006 -0
  78. data/ProjectTox-Core/toxcore/Messenger.h +818 -0
  79. data/ProjectTox-Core/toxcore/TCP_client.c +858 -0
  80. data/ProjectTox-Core/toxcore/TCP_client.h +156 -0
  81. data/ProjectTox-Core/toxcore/TCP_server.c +1332 -0
  82. data/ProjectTox-Core/toxcore/TCP_server.h +181 -0
  83. data/ProjectTox-Core/toxcore/assoc.c +1033 -0
  84. data/ProjectTox-Core/toxcore/assoc.h +104 -0
  85. data/ProjectTox-Core/toxcore/crypto_core.c +278 -0
  86. data/ProjectTox-Core/toxcore/crypto_core.h +151 -0
  87. data/ProjectTox-Core/toxcore/friend_requests.c +175 -0
  88. data/ProjectTox-Core/toxcore/friend_requests.h +83 -0
  89. data/ProjectTox-Core/toxcore/group_chats.c +837 -0
  90. data/ProjectTox-Core/toxcore/group_chats.h +199 -0
  91. data/ProjectTox-Core/toxcore/list.c +256 -0
  92. data/ProjectTox-Core/toxcore/list.h +85 -0
  93. data/ProjectTox-Core/toxcore/logger.c +153 -0
  94. data/ProjectTox-Core/toxcore/logger.h +84 -0
  95. data/ProjectTox-Core/toxcore/misc_tools.h +70 -0
  96. data/ProjectTox-Core/toxcore/net_crypto.c +2753 -0
  97. data/ProjectTox-Core/toxcore/net_crypto.h +410 -0
  98. data/ProjectTox-Core/toxcore/network.c +979 -0
  99. data/ProjectTox-Core/toxcore/network.h +367 -0
  100. data/ProjectTox-Core/toxcore/onion.c +540 -0
  101. data/ProjectTox-Core/toxcore/onion.h +150 -0
  102. data/ProjectTox-Core/toxcore/onion_announce.c +433 -0
  103. data/ProjectTox-Core/toxcore/onion_announce.h +139 -0
  104. data/ProjectTox-Core/toxcore/onion_client.c +1347 -0
  105. data/ProjectTox-Core/toxcore/onion_client.h +253 -0
  106. data/ProjectTox-Core/toxcore/ping.c +346 -0
  107. data/ProjectTox-Core/toxcore/ping.h +47 -0
  108. data/ProjectTox-Core/toxcore/ping_array.c +162 -0
  109. data/ProjectTox-Core/toxcore/ping_array.h +75 -0
  110. data/ProjectTox-Core/toxcore/tox.c +940 -0
  111. data/ProjectTox-Core/toxcore/tox.h +734 -0
  112. data/ProjectTox-Core/toxcore/util.c +193 -0
  113. data/ProjectTox-Core/toxcore/util.h +63 -0
  114. data/ProjectTox-Core/toxdns/Makefile.inc +29 -0
  115. data/ProjectTox-Core/toxdns/toxdns.c +238 -0
  116. data/ProjectTox-Core/toxdns/toxdns.h +88 -0
  117. data/ProjectTox-Core/toxencryptsave/Makefile.inc +45 -0
  118. data/ProjectTox-Core/toxencryptsave/toxencryptsave.c +179 -0
  119. data/ProjectTox-Core/toxencryptsave/toxencryptsave.h +74 -0
  120. data/interfaces/libtox.i +2 -6
  121. data/lib/ffi-tox/libtox.rb +406 -28
  122. metadata +124 -46
  123. data/ProjectTox-Core/CMakeLists.txt +0 -50
  124. data/ProjectTox-Core/cmake/FindLIBCONFIG.cmake +0 -15
  125. data/ProjectTox-Core/cmake/FindNaCl.cmake +0 -17
  126. data/ProjectTox-Core/cmake/FindSODIUM.cmake +0 -15
  127. data/ProjectTox-Core/core/CMakeLists.txt +0 -19
  128. data/ProjectTox-Core/core/DHT.c +0 -1104
  129. data/ProjectTox-Core/core/DHT.h +0 -111
  130. data/ProjectTox-Core/core/LAN_discovery.c +0 -79
  131. data/ProjectTox-Core/core/Lossless_UDP.c +0 -755
  132. data/ProjectTox-Core/core/Lossless_UDP.h +0 -106
  133. data/ProjectTox-Core/core/Messenger.c +0 -596
  134. data/ProjectTox-Core/core/Messenger.h +0 -165
  135. data/ProjectTox-Core/core/friend_requests.c +0 -131
  136. data/ProjectTox-Core/core/friend_requests.h +0 -51
  137. data/ProjectTox-Core/core/net_crypto.c +0 -575
  138. data/ProjectTox-Core/core/net_crypto.h +0 -134
  139. data/ProjectTox-Core/core/network.c +0 -205
  140. data/ProjectTox-Core/core/network.h +0 -134
  141. data/ProjectTox-Core/docs/commands.md +0 -25
  142. data/ProjectTox-Core/docs/start_guide.de.md +0 -40
  143. data/ProjectTox-Core/docs/start_guide.md +0 -38
  144. data/ProjectTox-Core/other/CMakeLists.txt +0 -9
  145. data/ProjectTox-Core/testing/CMakeLists.txt +0 -18
  146. data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +0 -228
  147. data/ProjectTox-Core/testing/DHT_sendfiletest.c +0 -176
  148. data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +0 -214
  149. data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +0 -201
  150. data/ProjectTox-Core/testing/misc_tools.h +0 -29
  151. data/ProjectTox-Core/testing/nTox_win32.c +0 -387
  152. data/ProjectTox-Core/testing/nTox_win32.h +0 -40
  153. data/ProjectTox-Core/testing/rect.py +0 -45
@@ -0,0 +1,11 @@
1
+ This directory can house various tools and utilities.
2
+
3
+ astylerc
4
+ - This file can be used in the precommit hook to try its best
5
+ at making the code conform to the coding style document.
6
+
7
+ pre-commit (*NIX only at the moment)
8
+ - Lints your file in adherence to the coding style doucment as
9
+ best as possible in terms of spacing, indenting, etc.
10
+ - Requires you to have astyle installed.
11
+ - To use, copy this file to ProjectTox-Core/.git/hooks
@@ -0,0 +1,11 @@
1
+ --style=kr
2
+ --pad-header
3
+ --max-code-length=120
4
+ --convert-tabs
5
+ --indent-switches
6
+ --pad-oper
7
+ --align-pointer=name
8
+ --align-reference=name
9
+ --preserve-date
10
+ --lineend=linux
11
+ --break-blocks
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env sh
2
+ #
3
+ # An example hook script to verify what is about to be committed.
4
+ # Called by "git commit" with no arguments. The hook should
5
+ # exit with non-zero status after issuing an appropriate message if
6
+ # it wants to stop the commit.
7
+ #
8
+ # To enable this hook, rename this file to "pre-commit".
9
+
10
+ for file in `git diff-index --diff-filter=ACMR --name-only HEAD`; do
11
+ if [[ $file == *.c || $file == *.h ]]
12
+ then
13
+ echo $file
14
+ `which astyle` $file --options=tools/astylerc
15
+ git add $file
16
+ fi
17
+ done
@@ -0,0 +1,36 @@
1
+ if BUILD_AV
2
+
3
+ lib_LTLIBRARIES += libtoxav.la
4
+ libtoxav_la_include_HEADERS = ../toxav/toxav.h
5
+ libtoxav_la_includedir = $(includedir)/tox
6
+
7
+ libtoxav_la_SOURCES = ../toxav/rtp.h \
8
+ ../toxav/rtp.c \
9
+ ../toxav/msi.h \
10
+ ../toxav/msi.c \
11
+ ../toxav/codec.h \
12
+ ../toxav/codec.c \
13
+ ../toxav/toxav.h \
14
+ ../toxav/toxav.c
15
+
16
+
17
+ libtoxav_la_CFLAGS = -I../toxcore \
18
+ -I../toxav \
19
+ $(LIBSODIUM_CFLAGS) \
20
+ $(NACL_CFLAGS) \
21
+ $(AV_CFLAGS) \
22
+ $(PTHREAD_CFLAGS)
23
+
24
+ libtoxav_la_LDFLAGS = $(TOXAV_LT_LDFLAGS) \
25
+ $(LIBSODIUM_LDFLAGS) \
26
+ $(NACL_LDFLAGS) \
27
+ $(EXTRA_LT_LDFLAGS) \
28
+ $(WINSOCK2_LIBS)
29
+
30
+ libtoxav_la_LIBADD = libtoxcore.la \
31
+ $(LIBSODIUM_LIBS) \
32
+ $(NACL_LIBS) \
33
+ $(PTHREAD_LIBS) \
34
+ $(AV_LIBS)
35
+
36
+ endif
@@ -0,0 +1,357 @@
1
+ /** codec.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 "../toxcore/logger.h"
30
+
31
+ #include <stdio.h>
32
+ #include <stdlib.h>
33
+ #include <math.h>
34
+ #include <assert.h>
35
+
36
+ #include "rtp.h"
37
+ #include "codec.h"
38
+
39
+ JitterBuffer *create_queue(unsigned int capacity)
40
+ {
41
+ unsigned int size = 1;
42
+
43
+ while (size <= capacity) {
44
+ size *= 2;
45
+ }
46
+
47
+ JitterBuffer *q;
48
+
49
+ if ( !(q = calloc(sizeof(JitterBuffer), 1)) ) return NULL;
50
+
51
+ if (!(q->queue = calloc(sizeof(RTPMessage *), size))) {
52
+ free(q);
53
+ return NULL;
54
+ }
55
+
56
+ q->size = size;
57
+ q->capacity = capacity;
58
+ return q;
59
+ }
60
+
61
+ static void clear_queue(JitterBuffer *q)
62
+ {
63
+ for (; q->bottom != q->top; ++q->bottom) {
64
+ if (q->queue[q->bottom % q->size]) {
65
+ rtp_free_msg(NULL, q->queue[q->bottom % q->size]);
66
+ q->queue[q->bottom % q->size] = NULL;
67
+ }
68
+ }
69
+ }
70
+
71
+ void terminate_queue(JitterBuffer *q)
72
+ {
73
+ if (!q) return;
74
+
75
+ clear_queue(q);
76
+ free(q->queue);
77
+ free(q);
78
+ }
79
+
80
+ void queue(JitterBuffer *q, RTPMessage *pk)
81
+ {
82
+ uint16_t sequnum = pk->header->sequnum;
83
+
84
+ unsigned int num = sequnum % q->size;
85
+
86
+ if ((uint32_t)(sequnum - q->bottom) > q->size) {
87
+ clear_queue(q);
88
+ q->bottom = sequnum;
89
+ q->queue[num] = pk;
90
+ q->top = sequnum + 1;
91
+ return;
92
+ }
93
+
94
+ if (q->queue[num])
95
+ return;
96
+
97
+ q->queue[num] = pk;
98
+
99
+ if ((sequnum - q->bottom) >= (q->top - q->bottom))
100
+ q->top = sequnum + 1;
101
+ }
102
+
103
+ /* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */
104
+ RTPMessage *dequeue(JitterBuffer *q, int *success)
105
+ {
106
+ if (q->top == q->bottom) {
107
+ *success = 0;
108
+ return NULL;
109
+ }
110
+
111
+ unsigned int num = q->bottom % q->size;
112
+
113
+ if (q->queue[num]) {
114
+ RTPMessage *ret = q->queue[num];
115
+ q->queue[num] = NULL;
116
+ ++q->bottom;
117
+ *success = 1;
118
+ return ret;
119
+ }
120
+
121
+ if ((uint32_t)(q->top - q->bottom) > q->capacity) {
122
+ ++q->bottom;
123
+ *success = 2;
124
+ return NULL;
125
+ }
126
+
127
+ *success = 0;
128
+ return NULL;
129
+ }
130
+
131
+
132
+ int init_video_decoder(CodecState *cs)
133
+ {
134
+ int rc = vpx_codec_dec_init_ver(&cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0, VPX_DECODER_ABI_VERSION);
135
+
136
+ if ( rc != VPX_CODEC_OK) {
137
+ LOGGER_ERROR("Init video_decoder failed: %s", vpx_codec_err_to_string(rc));
138
+ return -1;
139
+ }
140
+
141
+ return 0;
142
+ }
143
+
144
+ int init_audio_decoder(CodecState *cs, uint32_t audio_channels)
145
+ {
146
+ int rc;
147
+ cs->audio_decoder = opus_decoder_create(cs->audio_sample_rate, audio_channels, &rc );
148
+
149
+ if ( rc != OPUS_OK ) {
150
+ LOGGER_ERROR("Error while starting audio decoder: %s", opus_strerror(rc));
151
+ return -1;
152
+ }
153
+
154
+ cs->audio_decoder_channels = audio_channels;
155
+ return 0;
156
+ }
157
+
158
+ int reconfigure_video_encoder_resolution(CodecState *cs, uint16_t width, uint16_t height)
159
+ {
160
+ vpx_codec_enc_cfg_t cfg = *cs->v_encoder.config.enc;
161
+
162
+ if (cfg.g_w == width && cfg.g_h == height)
163
+ return 0;
164
+
165
+ if (width * height > cs->max_width * cs->max_height)
166
+ return -1;
167
+
168
+ LOGGER_DEBUG("New video resolution: %u %u", width, height);
169
+ cfg.g_w = width;
170
+ cfg.g_h = height;
171
+ int rc = vpx_codec_enc_config_set(&cs->v_encoder, &cfg);
172
+
173
+ if ( rc != VPX_CODEC_OK) {
174
+ LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
175
+ return -1;
176
+ }
177
+
178
+ return 0;
179
+ }
180
+
181
+ int reconfigure_video_encoder_bitrate(CodecState *cs, uint32_t video_bitrate)
182
+ {
183
+ vpx_codec_enc_cfg_t cfg = *cs->v_encoder.config.enc;
184
+
185
+ if (cfg.rc_target_bitrate == video_bitrate)
186
+ return 0;
187
+
188
+ LOGGER_DEBUG("New video bitrate: %u", video_bitrate);
189
+ cfg.rc_target_bitrate = video_bitrate;
190
+ int rc = vpx_codec_enc_config_set(&cs->v_encoder, &cfg);
191
+
192
+ if ( rc != VPX_CODEC_OK) {
193
+ LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
194
+ return -1;
195
+ }
196
+
197
+ return 0;
198
+ }
199
+
200
+ int init_video_encoder(CodecState *cs, uint16_t max_width, uint16_t max_height, uint32_t video_bitrate)
201
+ {
202
+ vpx_codec_enc_cfg_t cfg;
203
+ int rc = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0);
204
+
205
+ if (rc != VPX_CODEC_OK) {
206
+ LOGGER_ERROR("Failed to get config: %s", vpx_codec_err_to_string(rc));
207
+ return -1;
208
+ }
209
+
210
+ cfg.rc_target_bitrate = video_bitrate;
211
+ cfg.g_w = max_width;
212
+ cfg.g_h = max_height;
213
+ cfg.g_pass = VPX_RC_ONE_PASS;
214
+ cfg.g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT | VPX_ERROR_RESILIENT_PARTITIONS;
215
+ cfg.g_lag_in_frames = 0;
216
+ cfg.kf_min_dist = 0;
217
+ cfg.kf_max_dist = 300;
218
+ cfg.kf_mode = VPX_KF_AUTO;
219
+
220
+ cs->max_width = max_width;
221
+ cs->max_height = max_height;
222
+ cs->bitrate = video_bitrate;
223
+
224
+ rc = vpx_codec_enc_init_ver(&cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0, VPX_ENCODER_ABI_VERSION);
225
+
226
+ if ( rc != VPX_CODEC_OK) {
227
+ LOGGER_ERROR("Failed to initialize encoder: %s", vpx_codec_err_to_string(rc));
228
+ return -1;
229
+ }
230
+
231
+ rc = vpx_codec_control(&cs->v_encoder, VP8E_SET_CPUUSED, 7);
232
+
233
+ if ( rc != VPX_CODEC_OK) {
234
+ LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
235
+ return -1;
236
+ }
237
+
238
+ return 0;
239
+ }
240
+
241
+ int init_audio_encoder(CodecState *cs, uint32_t audio_channels)
242
+ {
243
+ int rc = OPUS_OK;
244
+ cs->audio_encoder = opus_encoder_create(cs->audio_sample_rate, audio_channels, OPUS_APPLICATION_AUDIO, &rc);
245
+
246
+ if ( rc != OPUS_OK ) {
247
+ LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(rc));
248
+ return -1;
249
+ }
250
+
251
+ rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_bitrate));
252
+
253
+ if ( rc != OPUS_OK ) {
254
+ LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
255
+ return -1;
256
+ }
257
+
258
+ rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10));
259
+
260
+ if ( rc != OPUS_OK ) {
261
+ LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
262
+ return -1;
263
+ }
264
+
265
+ cs->audio_encoder_channels = audio_channels;
266
+ return 0;
267
+ }
268
+
269
+
270
+ CodecState *codec_init_session ( uint32_t audio_bitrate,
271
+ uint16_t audio_frame_duration,
272
+ uint32_t audio_sample_rate,
273
+ uint32_t encoder_audio_channels,
274
+ uint32_t decoder_audio_channels,
275
+ uint32_t audio_VAD_tolerance_ms,
276
+ uint16_t max_video_width,
277
+ uint16_t max_video_height,
278
+ uint32_t video_bitrate )
279
+ {
280
+ CodecState *retu = calloc(sizeof(CodecState), 1);
281
+
282
+ if (!retu) return NULL;
283
+
284
+ retu->audio_bitrate = audio_bitrate;
285
+ retu->audio_sample_rate = audio_sample_rate;
286
+
287
+ /* Encoders */
288
+ if (!max_video_width || !max_video_height) { /* Disable video */
289
+ /*video_width = 320;
290
+ video_height = 240; */
291
+ } else {
292
+ retu->capabilities |= ( 0 == init_video_encoder(retu, max_video_width, max_video_height,
293
+ video_bitrate) ) ? v_encoding : 0;
294
+ retu->capabilities |= ( 0 == init_video_decoder(retu) ) ? v_decoding : 0;
295
+ }
296
+
297
+ retu->capabilities |= ( 0 == init_audio_encoder(retu, encoder_audio_channels) ) ? a_encoding : 0;
298
+ retu->capabilities |= ( 0 == init_audio_decoder(retu, decoder_audio_channels) ) ? a_decoding : 0;
299
+
300
+ if ( retu->capabilities == 0 ) { /* everything failed */
301
+ free (retu);
302
+ return NULL;
303
+ }
304
+
305
+
306
+ retu->EVAD_tolerance = audio_VAD_tolerance_ms > audio_frame_duration ?
307
+ audio_VAD_tolerance_ms / audio_frame_duration : audio_frame_duration;
308
+
309
+ return retu;
310
+ }
311
+
312
+ void codec_terminate_session ( CodecState *cs )
313
+ {
314
+ if (!cs) return;
315
+
316
+ if ( cs->audio_encoder )
317
+ opus_encoder_destroy(cs->audio_encoder);
318
+
319
+ if ( cs->audio_decoder )
320
+ opus_decoder_destroy(cs->audio_decoder);
321
+
322
+ if ( cs->capabilities & v_decoding )
323
+ vpx_codec_destroy(&cs->v_decoder);
324
+
325
+ if ( cs->capabilities & v_encoding )
326
+ vpx_codec_destroy(&cs->v_encoder);
327
+
328
+ LOGGER_DEBUG("Terminated codec state: %p", cs);
329
+ free(cs);
330
+ }
331
+
332
+ static float calculate_sum_sq (int16_t *n, uint16_t k)
333
+ {
334
+ float result = 0;
335
+ uint16_t i = 0;
336
+
337
+ for ( ; i < k; i ++) result += (float) (n[i] * n[i]);
338
+
339
+ return result;
340
+ }
341
+
342
+ int energy_VAD(CodecState *cs, int16_t *PCM, uint16_t frame_size, float energy)
343
+ {
344
+ float frame_energy = sqrt(calculate_sum_sq(PCM, frame_size)) / frame_size;
345
+
346
+ if ( frame_energy > energy) {
347
+ cs->EVAD_tolerance_cr = cs->EVAD_tolerance; /* Reset counter */
348
+ return 1;
349
+ }
350
+
351
+ if ( cs->EVAD_tolerance_cr ) {
352
+ cs->EVAD_tolerance_cr --;
353
+ return 1;
354
+ }
355
+
356
+ return 0;
357
+ }
@@ -0,0 +1,116 @@
1
+ /** codec.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 _CODEC_H_
25
+ #define _CODEC_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.h>
40
+
41
+ typedef enum _Capabilities {
42
+ none,
43
+ a_encoding = 1 << 0,
44
+ a_decoding = 1 << 1,
45
+ v_encoding = 1 << 2,
46
+ v_decoding = 1 << 3
47
+ } Capabilities;
48
+
49
+ extern const uint16_t min_jbuf_size;
50
+
51
+ typedef struct _CodecState {
52
+
53
+ /* video encoding */
54
+ vpx_codec_ctx_t v_encoder;
55
+ uint32_t frame_counter;
56
+
57
+ /* video decoding */
58
+ vpx_codec_ctx_t v_decoder;
59
+ int bitrate;
60
+ int max_width;
61
+ int max_height;
62
+
63
+ /* audio encoding */
64
+ OpusEncoder *audio_encoder;
65
+ int audio_bitrate;
66
+ int audio_sample_rate;
67
+ int audio_encoder_channels;
68
+
69
+ /* audio decoding */
70
+ OpusDecoder *audio_decoder;
71
+ int audio_decoder_channels;
72
+
73
+ uint64_t capabilities; /* supports*/
74
+
75
+ /* Voice activity detection */
76
+ uint32_t EVAD_tolerance; /* In frames */
77
+ uint32_t EVAD_tolerance_cr;
78
+ } CodecState;
79
+
80
+
81
+ typedef struct _JitterBuffer {
82
+ RTPMessage **queue;
83
+ uint32_t size;
84
+ uint32_t capacity;
85
+ uint16_t bottom;
86
+ uint16_t top;
87
+ } JitterBuffer;
88
+
89
+ JitterBuffer *create_queue(unsigned int capacity);
90
+ void terminate_queue(JitterBuffer *q);
91
+ void queue(JitterBuffer *q, RTPMessage *pk);
92
+ RTPMessage *dequeue(JitterBuffer *q, int *success);
93
+
94
+
95
+ CodecState *codec_init_session ( uint32_t audio_bitrate,
96
+ uint16_t audio_frame_duration,
97
+ uint32_t audio_sample_rate,
98
+ uint32_t encoder_audio_channels,
99
+ uint32_t decoder_audio_channels,
100
+ uint32_t audio_VAD_tolerance_ms,
101
+ uint16_t max_video_width,
102
+ uint16_t max_video_height,
103
+ uint32_t video_bitrate );
104
+
105
+ void codec_terminate_session(CodecState *cs);
106
+
107
+ /* Reconfigure video encoder
108
+ return 0 on success.
109
+ return -1 on failure. */
110
+ int reconfigure_video_encoder_resolution(CodecState *cs, uint16_t width, uint16_t height);
111
+ int reconfigure_video_encoder_bitrate(CodecState *cs, uint32_t video_bitrate);
112
+
113
+ /* Calculate energy and return 1 if has voice, 0 if not */
114
+ int energy_VAD(CodecState *cs, int16_t *PCM, uint16_t frame_size, float energy);
115
+
116
+ #endif /* _CODEC_H_ */