summaryrefslogtreecommitdiff
path: root/toxav/audio.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxav/audio.c')
-rw-r--r--toxav/audio.c439
1 files changed, 439 insertions, 0 deletions
diff --git a/toxav/audio.c b/toxav/audio.c
new file mode 100644
index 00000000..ad543502
--- /dev/null
+++ b/toxav/audio.c
@@ -0,0 +1,439 @@
1/** audio.c
2 *
3 * Copyright (C) 2013-2015 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
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif /* HAVE_CONFIG_H */
25
26#include <stdlib.h>
27
28#include "audio.h"
29#include "rtp.h"
30
31#include "../toxcore/logger.h"
32
33static struct JitterBuffer *jbuf_new(uint32_t capacity);
34static void jbuf_clear(struct JitterBuffer *q);
35static void jbuf_free(struct JitterBuffer *q);
36static int jbuf_write(struct JitterBuffer *q, struct RTPMessage *m);
37static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success);
38OpusEncoder *create_audio_encoder (int32_t bit_rate, int32_t sampling_rate, int32_t channel_count);
39bool reconfigure_audio_encoder(OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch,
40 int32_t *old_br, int32_t *old_sr, int32_t *old_ch);
41bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels);
42
43
44
45ACSession *ac_new(ToxAV *av, uint32_t friend_number, toxav_audio_receive_frame_cb *cb, void *cb_data)
46{
47 ACSession *ac = calloc(sizeof(ACSession), 1);
48
49 if (!ac) {
50 LOGGER_WARNING("Allocation failed! Application might misbehave!");
51 return NULL;
52 }
53
54 if (create_recursive_mutex(ac->queue_mutex) != 0) {
55 LOGGER_WARNING("Failed to create recursive mutex!");
56 free(ac);
57 return NULL;
58 }
59
60 int status;
61 ac->decoder = opus_decoder_create(48000, 2, &status);
62
63 if (status != OPUS_OK) {
64 LOGGER_ERROR("Error while starting audio decoder: %s", opus_strerror(status));
65 goto BASE_CLEANUP;
66 }
67
68 if (!(ac->j_buf = jbuf_new(3))) {
69 LOGGER_WARNING("Jitter buffer creaton failed!");
70 opus_decoder_destroy(ac->decoder);
71 goto BASE_CLEANUP;
72 }
73
74 /* Initialize encoders with default values */
75 ac->encoder = create_audio_encoder(48000, 48000, 2);
76
77 if (ac->encoder == NULL)
78 goto DECODER_CLEANUP;
79
80 ac->le_bit_rate = 48000;
81 ac->le_sample_rate = 48000;
82 ac->le_channel_count = 2;
83
84 ac->ld_channel_count = 2;
85 ac->ld_sample_rate = 48000;
86 ac->ldrts = 0; /* Make it possible to reconfigure straight away */
87
88 /* These need to be set in order to properly
89 * do error correction with opus */
90 ac->lp_frame_duration = 120;
91 ac->lp_sampling_rate = 48000;
92 ac->lp_channel_count = 1;
93
94 ac->av = av;
95 ac->friend_number = friend_number;
96 ac->acb.first = cb;
97 ac->acb.second = cb_data;
98
99 return ac;
100
101DECODER_CLEANUP:
102 opus_decoder_destroy(ac->decoder);
103 jbuf_free(ac->j_buf);
104BASE_CLEANUP:
105 pthread_mutex_destroy(ac->queue_mutex);
106 free(ac);
107 return NULL;
108}
109void ac_kill(ACSession *ac)
110{
111 if (!ac)
112 return;
113
114 opus_encoder_destroy(ac->encoder);
115 opus_decoder_destroy(ac->decoder);
116 jbuf_free(ac->j_buf);
117
118 pthread_mutex_destroy(ac->queue_mutex);
119
120 LOGGER_DEBUG("Terminated audio handler: %p", ac);
121 free(ac);
122}
123void ac_iterate(ACSession *ac)
124{
125 if (!ac)
126 return;
127
128 /* TODO fix this and jitter buffering */
129
130 /* Enough space for the maximum frame size (120 ms 48 KHz stereo audio) */
131 int16_t tmp[5760 * 2];
132
133 struct RTPMessage *msg;
134 int rc = 0;
135
136 pthread_mutex_lock(ac->queue_mutex);
137
138 while ((msg = jbuf_read(ac->j_buf, &rc)) || rc == 2) {
139 pthread_mutex_unlock(ac->queue_mutex);
140
141 if (rc == 2) {
142 LOGGER_DEBUG("OPUS correction");
143 int fs = (ac->lp_sampling_rate * ac->lp_frame_duration) / 1000;
144 rc = opus_decode(ac->decoder, NULL, 0, tmp, fs, 1);
145 } else {
146 /* Get values from packet and decode. */
147 /* NOTE: This didn't work very well
148 rc = convert_bw_to_sampling_rate(opus_packet_get_bandwidth(msg->data));
149 if (rc != -1) {
150 cs->last_packet_sampling_rate = rc;
151 } else {
152 LOGGER_WARNING("Failed to load packet values!");
153 rtp_free_msg(msg);
154 continue;
155 }*/
156
157
158 /* Pick up sampling rate from packet */
159 memcpy(&ac->lp_sampling_rate, msg->data, 4);
160 ac->lp_sampling_rate = ntohl(ac->lp_sampling_rate);
161
162 ac->lp_channel_count = opus_packet_get_nb_channels(msg->data + 4);
163
164 /** NOTE: even though OPUS supports decoding mono frames with stereo decoder and vice versa,
165 * it didn't work quite well.
166 */
167 if (!reconfigure_audio_decoder(ac, ac->lp_sampling_rate, ac->lp_channel_count)) {
168 LOGGER_WARNING("Failed to reconfigure decoder!");
169 free(msg);
170 continue;
171 }
172
173 rc = opus_decode(ac->decoder, msg->data + 4, msg->len - 4, tmp, 5760, 0);
174 free(msg);
175 }
176
177 if (rc < 0) {
178 LOGGER_WARNING("Decoding error: %s", opus_strerror(rc));
179 } else if (ac->acb.first) {
180 ac->lp_frame_duration = (rc * 1000) / ac->lp_sampling_rate;
181
182 ac->acb.first(ac->av, ac->friend_number, tmp, rc, ac->lp_channel_count,
183 ac->lp_sampling_rate, ac->acb.second);
184 }
185
186 return;
187 }
188
189 pthread_mutex_unlock(ac->queue_mutex);
190}
191int ac_queue_message(void *acp, struct RTPMessage *msg)
192{
193 if (!acp || !msg)
194 return -1;
195
196 if ((msg->header.pt & 0x7f) == (rtp_TypeAudio + 2) % 128) {
197 LOGGER_WARNING("Got dummy!");
198 free(msg);
199 return 0;
200 }
201
202 if ((msg->header.pt & 0x7f) != rtp_TypeAudio % 128) {
203 LOGGER_WARNING("Invalid payload type!");
204 free(msg);
205 return -1;
206 }
207
208 ACSession *ac = acp;
209
210 pthread_mutex_lock(ac->queue_mutex);
211 int rc = jbuf_write(ac->j_buf, msg);
212 pthread_mutex_unlock(ac->queue_mutex);
213
214 if (rc == -1) {
215 LOGGER_WARNING("Could not queue the message!");
216 free(msg);
217 return -1;
218 }
219
220 return 0;
221}
222int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels)
223{
224 if (!ac || !reconfigure_audio_encoder(&ac->encoder, bit_rate,
225 sampling_rate, channels,
226 &ac->le_bit_rate,
227 &ac->le_sample_rate,
228 &ac->le_channel_count))
229 return -1;
230
231 return 0;
232}
233
234
235
236struct JitterBuffer {
237 struct RTPMessage **queue;
238 uint32_t size;
239 uint32_t capacity;
240 uint16_t bottom;
241 uint16_t top;
242};
243
244static struct JitterBuffer *jbuf_new(uint32_t capacity)
245{
246 unsigned int size = 1;
247
248 while (size <= (capacity * 4)) {
249 size *= 2;
250 }
251
252 struct JitterBuffer *q;
253
254 if (!(q = calloc(sizeof(struct JitterBuffer), 1))) return NULL;
255
256 if (!(q->queue = calloc(sizeof(struct RTPMessage *), size))) {
257 free(q);
258 return NULL;
259 }
260
261 q->size = size;
262 q->capacity = capacity;
263 return q;
264}
265static void jbuf_clear(struct JitterBuffer *q)
266{
267 for (; q->bottom != q->top; ++q->bottom) {
268 if (q->queue[q->bottom % q->size]) {
269 free(q->queue[q->bottom % q->size]);
270 q->queue[q->bottom % q->size] = NULL;
271 }
272 }
273}
274static void jbuf_free(struct JitterBuffer *q)
275{
276 if (!q) return;
277
278 jbuf_clear(q);
279 free(q->queue);
280 free(q);
281}
282static int jbuf_write(struct JitterBuffer *q, struct RTPMessage *m)
283{
284 uint16_t sequnum = m->header.sequnum;
285
286 unsigned int num = sequnum % q->size;
287
288 if ((uint32_t)(sequnum - q->bottom) > q->size) {
289 LOGGER_DEBUG("Clearing filled jitter buffer: %p", q);
290
291 jbuf_clear(q);
292 q->bottom = sequnum - q->capacity;
293 q->queue[num] = m;
294 q->top = sequnum + 1;
295 return 0;
296 }
297
298 if (q->queue[num])
299 return -1;
300
301 q->queue[num] = m;
302
303 if ((sequnum - q->bottom) >= (q->top - q->bottom))
304 q->top = sequnum + 1;
305
306 return 0;
307}
308static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success)
309{
310 if (q->top == q->bottom) {
311 *success = 0;
312 return NULL;
313 }
314
315 unsigned int num = q->bottom % q->size;
316
317 if (q->queue[num]) {
318 struct RTPMessage *ret = q->queue[num];
319 q->queue[num] = NULL;
320 ++q->bottom;
321 *success = 1;
322 return ret;
323 }
324
325 if ((uint32_t)(q->top - q->bottom) > q->capacity) {
326 ++q->bottom;
327 *success = 2;
328 return NULL;
329 }
330
331 *success = 0;
332 return NULL;
333}
334OpusEncoder *create_audio_encoder (int32_t bit_rate, int32_t sampling_rate, int32_t channel_count)
335{
336 int status = OPUS_OK;
337 OpusEncoder *rc = opus_encoder_create(sampling_rate, channel_count, OPUS_APPLICATION_VOIP, &status);
338
339 if (status != OPUS_OK) {
340 LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(status));
341 return NULL;
342 }
343
344 status = opus_encoder_ctl(rc, OPUS_SET_BITRATE(bit_rate));
345
346 if (status != OPUS_OK) {
347 LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status));
348 goto FAILURE;
349 }
350
351 /* Enable in-band forward error correction in codec */
352 status = opus_encoder_ctl(rc, OPUS_SET_INBAND_FEC(1));
353
354 if (status != OPUS_OK) {
355 LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status));
356 goto FAILURE;
357 }
358
359 /* Make codec resistant to up to 10% packet loss
360 * NOTE This could also be adjusted on the fly, rather than hard-coded,
361 * with feedback from the receiving client.
362 */
363 status = opus_encoder_ctl(rc, OPUS_SET_PACKET_LOSS_PERC(10));
364
365 if (status != OPUS_OK) {
366 LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status));
367 goto FAILURE;
368 }
369
370 /* Set algorithm to the highest complexity, maximizing compression */
371 status = opus_encoder_ctl(rc, OPUS_SET_COMPLEXITY(10));
372
373 if (status != OPUS_OK) {
374 LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status));
375 goto FAILURE;
376 }
377
378 return rc;
379
380FAILURE:
381 opus_encoder_destroy(rc);
382 return NULL;
383}
384bool reconfigure_audio_encoder(OpusEncoder **e, int32_t new_br, int32_t new_sr, uint8_t new_ch,
385 int32_t *old_br, int32_t *old_sr, int32_t *old_ch)
386{
387 /* Values are checked in toxav.c */
388 if (*old_sr != new_sr || *old_ch != new_ch) {
389 OpusEncoder *new_encoder = create_audio_encoder(new_br, new_sr, new_ch);
390
391 if (new_encoder == NULL)
392 return false;
393
394 opus_encoder_destroy(*e);
395 *e = new_encoder;
396 } else if (*old_br == new_br)
397 return true; /* Nothing changed */
398 else {
399 int status = opus_encoder_ctl(*e, OPUS_SET_BITRATE(new_br));
400
401 if (status != OPUS_OK) {
402 LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(status));
403 return false;
404 }
405 }
406
407 *old_br = new_br;
408 *old_sr = new_sr;
409 *old_ch = new_ch;
410
411 LOGGER_DEBUG ("Reconfigured audio encoder br: %d sr: %d cc:%d", new_br, new_sr, new_ch);
412 return true;
413}
414bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels)
415{
416 if (sampling_rate != ac->ld_sample_rate || channels != ac->ld_channel_count) {
417 if (current_time_monotonic() - ac->ldrts < 500)
418 return false;
419
420 int status;
421 OpusDecoder *new_dec = opus_decoder_create(sampling_rate, channels, &status);
422
423 if (status != OPUS_OK) {
424 LOGGER_ERROR("Error while starting audio decoder(%d %d): %s", sampling_rate, channels, opus_strerror(status));
425 return false;
426 }
427
428 ac->ld_sample_rate = sampling_rate;
429 ac->ld_channel_count = channels;
430 ac->ldrts = current_time_monotonic();
431
432 opus_decoder_destroy(ac->decoder);
433 ac->decoder = new_dec;
434
435 LOGGER_DEBUG("Reconfigured audio decoder sr: %d cc: %d", sampling_rate, channels);
436 }
437
438 return true;
439}