diff options
author | mannol <eniz_vukovic@hotmail.com> | 2014-06-21 19:04:00 +0200 |
---|---|---|
committer | mannol <eniz_vukovic@hotmail.com> | 2014-06-21 19:04:00 +0200 |
commit | 5c0b6c8117f9f36668f2481883bd8c201641ce8a (patch) | |
tree | ee2edab7fc6787187de77379cc6e7d4dbded1752 /toxav/codec.c | |
parent | d413fef66f01d89626d6696545c6d421eb43b6dd (diff) |
Added tolerance to VAD and lowered Jitter buffer default value
Diffstat (limited to 'toxav/codec.c')
-rw-r--r-- | toxav/codec.c | 48 |
1 files changed, 21 insertions, 27 deletions
diff --git a/toxav/codec.c b/toxav/codec.c index 61486085..90606e0a 100644 --- a/toxav/codec.c +++ b/toxav/codec.c | |||
@@ -36,6 +36,8 @@ | |||
36 | #include "rtp.h" | 36 | #include "rtp.h" |
37 | #include "codec.h" | 37 | #include "codec.h" |
38 | 38 | ||
39 | const uint16_t min_jbuf_size = 10; | ||
40 | const uint16_t min_readiness_idx = 6; /* when is buffer ready to dqq */ | ||
39 | 41 | ||
40 | int empty_queue(JitterBuffer *q) | 42 | int empty_queue(JitterBuffer *q) |
41 | { | 43 | { |
@@ -66,7 +68,7 @@ JitterBuffer *create_queue(int capacity) | |||
66 | } | 68 | } |
67 | 69 | ||
68 | q->size = 0; | 70 | q->size = 0; |
69 | q->capacity = capacity; | 71 | q->capacity = capacity >= min_jbuf_size ? capacity : min_jbuf_size; |
70 | q->front = 0; | 72 | q->front = 0; |
71 | q->rear = -1; | 73 | q->rear = -1; |
72 | q->queue_ready = 0; | 74 | q->queue_ready = 0; |
@@ -142,14 +144,12 @@ void queue(JitterBuffer *q, RTPMessage *pk) | |||
142 | empty_queue(q); | 144 | empty_queue(q); |
143 | } | 145 | } |
144 | 146 | ||
145 | if (q->size > 8) | 147 | if (q->size >= min_readiness_idx) q->queue_ready = 1; |
146 | q->queue_ready = 1; | ||
147 | 148 | ||
148 | ++q->size; | 149 | ++q->size; |
149 | ++q->rear; | 150 | ++q->rear; |
150 | 151 | ||
151 | if (q->rear == q->capacity) | 152 | if (q->rear == q->capacity) q->rear = 0; |
152 | q->rear = 0; | ||
153 | 153 | ||
154 | q->queue[q->rear] = pk; | 154 | q->queue[q->rear] = pk; |
155 | 155 | ||
@@ -177,8 +177,7 @@ void queue(JitterBuffer *q, RTPMessage *pk) | |||
177 | 177 | ||
178 | a -= 1; | 178 | a -= 1; |
179 | 179 | ||
180 | if (a < 0) | 180 | if (a < 0) a += q->capacity; |
181 | a += q->capacity; | ||
182 | } | 181 | } |
183 | } | 182 | } |
184 | 183 | ||
@@ -266,6 +265,7 @@ CodecState *codec_init_session ( uint32_t audio_bitrate, | |||
266 | uint16_t audio_frame_duration, | 265 | uint16_t audio_frame_duration, |
267 | uint32_t audio_sample_rate, | 266 | uint32_t audio_sample_rate, |
268 | uint32_t audio_channels, | 267 | uint32_t audio_channels, |
268 | uint32_t audio_VAD_tolerance_ms, | ||
269 | uint16_t video_width, | 269 | uint16_t video_width, |
270 | uint16_t video_height, | 270 | uint16_t video_height, |
271 | uint32_t video_bitrate ) | 271 | uint32_t video_bitrate ) |
@@ -294,8 +294,9 @@ CodecState *codec_init_session ( uint32_t audio_bitrate, | |||
294 | return NULL; | 294 | return NULL; |
295 | } | 295 | } |
296 | 296 | ||
297 | float frame_duration_sec = audio_frame_duration / 1000; | 297 | |
298 | retu->samples_per_frame = audio_sample_rate * frame_duration_sec; | 298 | retu->EVAD_tolerance = audio_VAD_tolerance_ms > audio_frame_duration ? |
299 | audio_VAD_tolerance_ms / audio_frame_duration : audio_frame_duration; | ||
299 | 300 | ||
300 | return retu; | 301 | return retu; |
301 | } | 302 | } |
@@ -308,10 +309,6 @@ void codec_terminate_session ( CodecState *cs ) | |||
308 | if ( cs->audio_decoder ) | 309 | if ( cs->audio_decoder ) |
309 | opus_decoder_destroy(cs->audio_decoder); | 310 | opus_decoder_destroy(cs->audio_decoder); |
310 | 311 | ||
311 | |||
312 | /* TODO: Terminate video | ||
313 | * Do what? | ||
314 | */ | ||
315 | if ( cs->capabilities & v_decoding ) | 312 | if ( cs->capabilities & v_decoding ) |
316 | vpx_codec_destroy(&cs->v_decoder); | 313 | vpx_codec_destroy(&cs->v_decoder); |
317 | 314 | ||
@@ -319,27 +316,24 @@ void codec_terminate_session ( CodecState *cs ) | |||
319 | vpx_codec_destroy(&cs->v_encoder); | 316 | vpx_codec_destroy(&cs->v_encoder); |
320 | } | 317 | } |
321 | 318 | ||
322 | inline float calculate_sum_sq (int16_t* n, size_t k) | 319 | inline float calculate_sum_sq (int16_t* n, uint16_t k) |
323 | { | 320 | { |
324 | float result = 0; | 321 | float result = 0; |
325 | size_t i = 0; | 322 | uint16_t i = 0; |
326 | |||
327 | for ( ; i < k; i ++) { | ||
328 | result += (float) (n[i] * n[i]); | ||
329 | } | ||
330 | 323 | ||
324 | for ( ; i < k; i ++) result += (float) (n[i] * n[i]); | ||
331 | return result; | 325 | return result; |
332 | } | 326 | } |
333 | 327 | ||
334 | int calculate_VAD_from_PCM( int16_t* PCM, size_t frame_size, float energy) | 328 | int energy_VAD(CodecState* cs, int16_t* PCM, uint16_t frame_size, float energy) |
335 | { | 329 | { |
336 | // int i = 0; | ||
337 | // for (; i < frame_size; i ++) { | ||
338 | LOGGER_DEBUG("Frame size: %d ref: %f", frame_size, energy); | ||
339 | float frame_energy = sqrt(calculate_sum_sq(PCM, frame_size)) / frame_size; | 330 | float frame_energy = sqrt(calculate_sum_sq(PCM, frame_size)) / frame_size; |
340 | LOGGER_DEBUG("Frame energy calculated: %f", frame_energy); | 331 | |
341 | if ( frame_energy > energy) return 1; | 332 | if ( frame_energy > energy) { |
342 | // } | 333 | cs->EVAD_tolerance_cr = cs->EVAD_tolerance; /* Reset counter */ |
343 | 334 | return 1; | |
335 | } | ||
336 | |||
337 | if ( cs->EVAD_tolerance_cr ) { cs->EVAD_tolerance_cr --; return 1; } | ||
344 | return 0; | 338 | return 0; |
345 | } \ No newline at end of file | 339 | } \ No newline at end of file |