This commit is contained in:
irungentoo 2014-06-22 08:39:26 -04:00
commit 87be366f79
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
6 changed files with 74 additions and 23 deletions

View File

@ -10,8 +10,8 @@ libtoxav_la_SOURCES = ../toxav/event.h \
../toxav/rtp.c \ ../toxav/rtp.c \
../toxav/msi.h \ ../toxav/msi.h \
../toxav/msi.c \ ../toxav/msi.c \
../toxav/media.h \ ../toxav/codec.h \
../toxav/media.c \ ../toxav/codec.c \
../toxav/toxav.h \ ../toxav/toxav.h \
../toxav/toxav.c ../toxav/toxav.c

View File

@ -34,7 +34,10 @@
#include <assert.h> #include <assert.h>
#include "rtp.h" #include "rtp.h"
#include "media.h" #include "codec.h"
const uint16_t min_jbuf_size = 10;
const uint16_t min_readiness_idx = 6; /* when is buffer ready to dqq */
int empty_queue(JitterBuffer *q) int empty_queue(JitterBuffer *q)
{ {
@ -65,7 +68,7 @@ JitterBuffer *create_queue(int capacity)
} }
q->size = 0; q->size = 0;
q->capacity = capacity; q->capacity = capacity >= min_jbuf_size ? capacity : min_jbuf_size;
q->front = 0; q->front = 0;
q->rear = -1; q->rear = -1;
q->queue_ready = 0; q->queue_ready = 0;
@ -141,14 +144,12 @@ void queue(JitterBuffer *q, RTPMessage *pk)
empty_queue(q); empty_queue(q);
} }
if (q->size > 8) if (q->size >= min_readiness_idx) q->queue_ready = 1;
q->queue_ready = 1;
++q->size; ++q->size;
++q->rear; ++q->rear;
if (q->rear == q->capacity) if (q->rear == q->capacity) q->rear = 0;
q->rear = 0;
q->queue[q->rear] = pk; q->queue[q->rear] = pk;
@ -175,8 +176,7 @@ void queue(JitterBuffer *q, RTPMessage *pk)
a -= 1; a -= 1;
if (a < 0) if (a < 0) a += q->capacity;
a += q->capacity;
} }
} }
@ -264,6 +264,7 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
uint16_t audio_frame_duration, uint16_t audio_frame_duration,
uint32_t audio_sample_rate, uint32_t audio_sample_rate,
uint32_t audio_channels, uint32_t audio_channels,
uint32_t audio_VAD_tolerance_ms,
uint16_t video_width, uint16_t video_width,
uint16_t video_height, uint16_t video_height,
uint32_t video_bitrate ) uint32_t video_bitrate )
@ -292,6 +293,10 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
return NULL; return NULL;
} }
retu->EVAD_tolerance = audio_VAD_tolerance_ms > audio_frame_duration ?
audio_VAD_tolerance_ms / audio_frame_duration : audio_frame_duration;
return retu; return retu;
} }
@ -303,13 +308,36 @@ void codec_terminate_session ( CodecState *cs )
if ( cs->audio_decoder ) if ( cs->audio_decoder )
opus_decoder_destroy(cs->audio_decoder); opus_decoder_destroy(cs->audio_decoder);
/* TODO: Terminate video
* Do what?
*/
if ( cs->capabilities & v_decoding ) if ( cs->capabilities & v_decoding )
vpx_codec_destroy(&cs->v_decoder); vpx_codec_destroy(&cs->v_decoder);
if ( cs->capabilities & v_encoding ) if ( cs->capabilities & v_encoding )
vpx_codec_destroy(&cs->v_encoder); vpx_codec_destroy(&cs->v_encoder);
} }
inline float calculate_sum_sq (int16_t *n, uint16_t k)
{
float result = 0;
uint16_t i = 0;
for ( ; i < k; i ++) result += (float) (n[i] * n[i]);
return result;
}
int energy_VAD(CodecState *cs, int16_t *PCM, uint16_t frame_size, float energy)
{
float frame_energy = sqrt(calculate_sum_sq(PCM, frame_size)) / frame_size;
if ( frame_energy > energy) {
cs->EVAD_tolerance_cr = cs->EVAD_tolerance; /* Reset counter */
return 1;
}
if ( cs->EVAD_tolerance_cr ) {
cs->EVAD_tolerance_cr --;
return 1;
}
return 0;
}

View File

@ -21,8 +21,8 @@
* *
*/ */
#ifndef _AVCODEC_H_ #ifndef _CODEC_H_
#define _AVCODEC_H_ #define _CODEC_H_
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
@ -46,6 +46,8 @@ typedef enum _Capabilities {
v_decoding = 1 << 3 v_decoding = 1 << 3
} Capabilities; } Capabilities;
extern const uint16_t min_jbuf_size;
typedef struct _CodecState { typedef struct _CodecState {
/* video encoding */ /* video encoding */
@ -65,6 +67,9 @@ typedef struct _CodecState {
uint64_t capabilities; /* supports*/ uint64_t capabilities; /* supports*/
/* Voice activity detection */
uint32_t EVAD_tolerance; /* In frames */
uint32_t EVAD_tolerance_cr;
} CodecState; } CodecState;
@ -90,10 +95,15 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
uint16_t audio_frame_duration, uint16_t audio_frame_duration,
uint32_t audio_sample_rate, uint32_t audio_sample_rate,
uint32_t audio_channels, uint32_t audio_channels,
uint32_t audio_VAD_tolerance_ms,
uint16_t video_width, uint16_t video_width,
uint16_t video_height, uint16_t video_height,
uint32_t video_bitrate); uint32_t video_bitrate);
void codec_terminate_session(CodecState *cs); void codec_terminate_session(CodecState *cs);
#endif
/* Calculate energy and return 1 if has voice, 0 if not */
int energy_VAD(CodecState *cs, int16_t *PCM, uint16_t frame_size, float energy);
#endif /* _CODEC_H_ */

View File

@ -257,7 +257,7 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte
_it += 3; /* place it at the field value beginning */ _it += 3; /* place it at the field value beginning */
size_max -= 3; size_max -= 3;
switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */ switch ( _size ) { /* Compare the size of the hardcoded values ( very convenient ) */
case 4: { /* INFO header */ case 4: { /* INFO header */
if ON_HEADER ( _it, size_max, msg->info, INFO_FIELD, 4 ) if ON_HEADER ( _it, size_max, msg->info, INFO_FIELD, 4 )

View File

@ -27,13 +27,12 @@
#define _GNU_SOURCE /* implicit declaration warning */ #define _GNU_SOURCE /* implicit declaration warning */
#include "rtp.h" #include "rtp.h"
#include "media.h" #include "codec.h"
#include "msi.h" #include "msi.h"
#include "toxav.h" #include "toxav.h"
#include "../toxcore/logger.h" #include "../toxcore/logger.h"
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -78,7 +77,9 @@ const ToxAvCodecSettings av_DefaultSettings = {
20, 20,
48000, 48000,
1, 1,
20 600,
10
}; };
@ -331,6 +332,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
codec_settings->audio_frame_duration, codec_settings->audio_frame_duration,
codec_settings->audio_sample_rate, codec_settings->audio_sample_rate,
codec_settings->audio_channels, codec_settings->audio_channels,
codec_settings->audio_VAD_tolerance,
codec_settings->video_width, codec_settings->video_width,
codec_settings->video_height, codec_settings->video_height,
codec_settings->video_bitrate); codec_settings->video_bitrate);
@ -737,3 +739,10 @@ inline__ Tox *toxav_get_tox(ToxAv *av)
{ {
return (Tox *)av->messenger; return (Tox *)av->messenger;
} }
int toxav_has_activity(ToxAv *av, int32_t call_index, int16_t *PCM, uint16_t frame_size, float ref_energy)
{
if ( !av->calls[call_index].cs ) return ErrorInvalidCodecState;
return energy_VAD(av->calls[call_index].cs, PCM, frame_size, ref_energy);
}

View File

@ -90,6 +90,7 @@ typedef enum {
ErrorTerminatingAudioRtp = -9, /* Returned in toxav_kill_transmission() */ ErrorTerminatingAudioRtp = -9, /* Returned in toxav_kill_transmission() */
ErrorTerminatingVideoRtp = -10, /* Returned in toxav_kill_transmission() */ ErrorTerminatingVideoRtp = -10, /* Returned in toxav_kill_transmission() */
ErrorPacketTooLarge = -11, /* Buffer exceeds size while encoding */ ErrorPacketTooLarge = -11, /* Buffer exceeds size while encoding */
ErrorInvalidCodecState = -12, /* Codec state not initialized */
} ToxAvError; } ToxAvError;
@ -117,6 +118,7 @@ typedef struct _ToxAvCodecSettings {
uint16_t audio_frame_duration; /* In ms */ uint16_t audio_frame_duration; /* In ms */
uint32_t audio_sample_rate; /* In Hz */ uint32_t audio_sample_rate; /* In Hz */
uint32_t audio_channels; uint32_t audio_channels;
uint32_t audio_VAD_tolerance; /* In ms */
uint32_t jbuf_capacity; /* Size of jitter buffer */ uint32_t jbuf_capacity; /* Size of jitter buffer */
} ToxAvCodecSettings; } ToxAvCodecSettings;
@ -373,6 +375,8 @@ int toxav_set_video_queue_limit ( ToxAv *av, int32_t call_index, uint64_t limit
Tox *toxav_get_tox(ToxAv *av); Tox *toxav_get_tox(ToxAv *av);
int toxav_has_activity ( ToxAv *av, int32_t call_index, int16_t *PCM, uint16_t frame_size, float ref_energy );
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif