summaryrefslogtreecommitdiff
path: root/toxav/codec.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxav/codec.c')
-rw-r--r--toxav/codec.c84
1 files changed, 43 insertions, 41 deletions
diff --git a/toxav/codec.c b/toxav/codec.c
index 43120e0f..e6fe713e 100644
--- a/toxav/codec.c
+++ b/toxav/codec.c
@@ -40,7 +40,6 @@
40#include "rtp.h" 40#include "rtp.h"
41#include "codec.h" 41#include "codec.h"
42 42
43
44#define DEFAULT_JBUF 6 43#define DEFAULT_JBUF 6
45 44
46/* Good quality encode. */ 45/* Good quality encode. */
@@ -125,7 +124,7 @@ static void buffer_free(PayloadBuffer *b)
125} 124}
126 125
127/* JITTER BUFFER WORK */ 126/* JITTER BUFFER WORK */
128typedef struct { 127typedef struct JitterBuffer {
129 RTPMessage **queue; 128 RTPMessage **queue;
130 uint32_t size; 129 uint32_t size;
131 uint32_t capacity; 130 uint32_t capacity;
@@ -260,48 +259,51 @@ void cs_do(CSSession *cs)
260 int success = 0; 259 int success = 0;
261 260
262 pthread_mutex_lock(cs->queue_mutex); 261 pthread_mutex_lock(cs->queue_mutex);
263 RTPMessage *msg;
264 262
265 uint16_t fsize = 5760; /* Max frame size for 48 kHz */ 263 if (cs->audio_decoder) { /* If receiving enabled */
266 int16_t tmp[fsize * 2]; 264 RTPMessage *msg;
267
268 while ((msg = jbuf_read(cs->j_buf, &success)) || success == 2) {
269 pthread_mutex_unlock(cs->queue_mutex);
270 265
271 if (success == 2) { 266 uint16_t fsize = 5760; /* Max frame size for 48 kHz */
272 rc = opus_decode(cs->audio_decoder, 0, 0, tmp, fsize, 1); 267 int16_t tmp[fsize * 2];
273 } else { 268
274 /* Get values from packet and decode. 269 while ((msg = jbuf_read(cs->j_buf, &success)) || success == 2) {
275 * It also checks for validity of an opus packet 270 pthread_mutex_unlock(cs->queue_mutex);
276 */
277 rc = convert_bw_to_sampling_rate(opus_packet_get_bandwidth(msg->data));
278 if (rc != -1) {
279 cs->last_packet_sampling_rate = rc;
280 cs->last_pack_channels = opus_packet_get_nb_channels(msg->data);
281 271
282 cs->last_packet_frame_duration = 272 if (success == 2) {
283 ( opus_packet_get_samples_per_frame(msg->data, cs->last_packet_sampling_rate) * 1000 ) 273 rc = opus_decode(cs->audio_decoder, 0, 0, tmp, fsize, 1);
284 / cs->last_packet_sampling_rate;
285
286 } else { 274 } else {
287 LOGGER_WARNING("Failed to load packet values!"); 275 /* Get values from packet and decode.
276 * It also checks for validity of an opus packet
277 */
278 rc = convert_bw_to_sampling_rate(opus_packet_get_bandwidth(msg->data));
279 if (rc != -1) {
280 cs->last_packet_sampling_rate = rc;
281 cs->last_pack_channels = opus_packet_get_nb_channels(msg->data);
282
283 cs->last_packet_frame_duration =
284 ( opus_packet_get_samples_per_frame(msg->data, cs->last_packet_sampling_rate) * 1000 )
285 / cs->last_packet_sampling_rate;
286
287 } else {
288 LOGGER_WARNING("Failed to load packet values!");
289 rtp_free_msg(NULL, msg);
290 continue;
291 }
292
293 rc = opus_decode(cs->audio_decoder, msg->data, msg->length, tmp, fsize, 0);
288 rtp_free_msg(NULL, msg); 294 rtp_free_msg(NULL, msg);
289 continue;
290 } 295 }
291 296
292 rc = opus_decode(cs->audio_decoder, msg->data, msg->length, tmp, fsize, 0); 297 if (rc < 0) {
293 rtp_free_msg(NULL, msg); 298 LOGGER_WARNING("Decoding error: %s", opus_strerror(rc));
294 } 299 } else if (cs->acb.first) {
295 300 /* Play */
296 if (rc < 0) { 301 cs->acb.first(cs->agent, cs->friend_number, tmp, rc,
297 LOGGER_WARNING("Decoding error: %s", opus_strerror(rc)); 302 cs->last_pack_channels, cs->last_packet_sampling_rate, cs->acb.second);
298 } else if (((ToxAV*)cs->agent)->acb.first) { 303 }
299 /* Play */ 304
300 ((ToxAV*)cs->agent)->acb.first(cs->agent, cs->call_idx, tmp, rc, 305 pthread_mutex_lock(cs->queue_mutex);
301 ((ToxAV*)cs->agent)->acb.second);
302 } 306 }
303
304 pthread_mutex_lock(cs->queue_mutex);
305 } 307 }
306 308
307 if (cs->vbuf_raw && !buffer_empty(cs->vbuf_raw)) { 309 if (cs->vbuf_raw && !buffer_empty(cs->vbuf_raw)) {
@@ -322,11 +324,11 @@ void cs_do(CSSession *cs)
322 324
323 /* Play decoded images */ 325 /* Play decoded images */
324 for (; dest; dest = vpx_codec_get_frame(cs->v_decoder, &iter)) { 326 for (; dest; dest = vpx_codec_get_frame(cs->v_decoder, &iter)) {
325 if (((ToxAV*)cs->agent)->vcb.first) 327 if (cs->vcb.first)
326 ((ToxAV*)cs->agent)->vcb.first(cs->agent, cs->call_idx, dest, 328 cs->vcb.first(cs->agent, cs->call_idx, dest->d_w, dest->d_h,
327 ((ToxAV*)cs->agent)->vcb.second); 329 (const uint8_t**)dest->planes, dest->stride, cs->vcb.second);
328 330
329 vpx_img_free(dest); 331 vpx_img_free(dest);
330 } 332 }
331 } 333 }
332 334