Skip to content

Commit 87be366

Browse files
committed
Merge branch 'master' of https://github.com/mannol1/toxcore
2 parents e6699f6 + 7715008 commit 87be366

File tree

6 files changed

+74
-23
lines changed

6 files changed

+74
-23
lines changed

toxav/Makefile.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ libtoxav_la_SOURCES = ../toxav/event.h \
1010
../toxav/rtp.c \
1111
../toxav/msi.h \
1212
../toxav/msi.c \
13-
../toxav/media.h \
14-
../toxav/media.c \
13+
../toxav/codec.h \
14+
../toxav/codec.c \
1515
../toxav/toxav.h \
1616
../toxav/toxav.c
1717

toxav/media.c renamed to toxav/codec.c

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
#include <assert.h>
3535

3636
#include "rtp.h"
37-
#include "media.h"
37+
#include "codec.h"
38+
39+
const uint16_t min_jbuf_size = 10;
40+
const uint16_t min_readiness_idx = 6; /* when is buffer ready to dqq */
3841

3942
int empty_queue(JitterBuffer *q)
4043
{
@@ -65,7 +68,7 @@ JitterBuffer *create_queue(int capacity)
6568
}
6669

6770
q->size = 0;
68-
q->capacity = capacity;
71+
q->capacity = capacity >= min_jbuf_size ? capacity : min_jbuf_size;
6972
q->front = 0;
7073
q->rear = -1;
7174
q->queue_ready = 0;
@@ -141,14 +144,12 @@ void queue(JitterBuffer *q, RTPMessage *pk)
141144
empty_queue(q);
142145
}
143146

144-
if (q->size > 8)
145-
q->queue_ready = 1;
147+
if (q->size >= min_readiness_idx) q->queue_ready = 1;
146148

147149
++q->size;
148150
++q->rear;
149151

150-
if (q->rear == q->capacity)
151-
q->rear = 0;
152+
if (q->rear == q->capacity) q->rear = 0;
152153

153154
q->queue[q->rear] = pk;
154155

@@ -175,8 +176,7 @@ void queue(JitterBuffer *q, RTPMessage *pk)
175176

176177
a -= 1;
177178

178-
if (a < 0)
179-
a += q->capacity;
179+
if (a < 0) a += q->capacity;
180180
}
181181
}
182182

@@ -264,6 +264,7 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
264264
uint16_t audio_frame_duration,
265265
uint32_t audio_sample_rate,
266266
uint32_t audio_channels,
267+
uint32_t audio_VAD_tolerance_ms,
267268
uint16_t video_width,
268269
uint16_t video_height,
269270
uint32_t video_bitrate )
@@ -292,6 +293,10 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
292293
return NULL;
293294
}
294295

296+
297+
retu->EVAD_tolerance = audio_VAD_tolerance_ms > audio_frame_duration ?
298+
audio_VAD_tolerance_ms / audio_frame_duration : audio_frame_duration;
299+
295300
return retu;
296301
}
297302

@@ -303,13 +308,36 @@ void codec_terminate_session ( CodecState *cs )
303308
if ( cs->audio_decoder )
304309
opus_decoder_destroy(cs->audio_decoder);
305310

306-
307-
/* TODO: Terminate video
308-
* Do what?
309-
*/
310311
if ( cs->capabilities & v_decoding )
311312
vpx_codec_destroy(&cs->v_decoder);
312313

313314
if ( cs->capabilities & v_encoding )
314315
vpx_codec_destroy(&cs->v_encoder);
315316
}
317+
318+
inline float calculate_sum_sq (int16_t *n, uint16_t k)
319+
{
320+
float result = 0;
321+
uint16_t i = 0;
322+
323+
for ( ; i < k; i ++) result += (float) (n[i] * n[i]);
324+
325+
return result;
326+
}
327+
328+
int energy_VAD(CodecState *cs, int16_t *PCM, uint16_t frame_size, float energy)
329+
{
330+
float frame_energy = sqrt(calculate_sum_sq(PCM, frame_size)) / frame_size;
331+
332+
if ( frame_energy > energy) {
333+
cs->EVAD_tolerance_cr = cs->EVAD_tolerance; /* Reset counter */
334+
return 1;
335+
}
336+
337+
if ( cs->EVAD_tolerance_cr ) {
338+
cs->EVAD_tolerance_cr --;
339+
return 1;
340+
}
341+
342+
return 0;
343+
}

toxav/media.h renamed to toxav/codec.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
*
2222
*/
2323

24-
#ifndef _AVCODEC_H_
25-
#define _AVCODEC_H_
24+
#ifndef _CODEC_H_
25+
#define _CODEC_H_
2626

2727
#include <stdio.h>
2828
#include <math.h>
@@ -46,6 +46,8 @@ typedef enum _Capabilities {
4646
v_decoding = 1 << 3
4747
} Capabilities;
4848

49+
extern const uint16_t min_jbuf_size;
50+
4951
typedef struct _CodecState {
5052

5153
/* video encoding */
@@ -65,6 +67,9 @@ typedef struct _CodecState {
6567

6668
uint64_t capabilities; /* supports*/
6769

70+
/* Voice activity detection */
71+
uint32_t EVAD_tolerance; /* In frames */
72+
uint32_t EVAD_tolerance_cr;
6873
} CodecState;
6974

7075

@@ -90,10 +95,15 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
9095
uint16_t audio_frame_duration,
9196
uint32_t audio_sample_rate,
9297
uint32_t audio_channels,
98+
uint32_t audio_VAD_tolerance_ms,
9399
uint16_t video_width,
94100
uint16_t video_height,
95-
uint32_t video_bitrate );
101+
uint32_t video_bitrate);
96102

97103
void codec_terminate_session(CodecState *cs);
98104

99-
#endif
105+
106+
/* Calculate energy and return 1 if has voice, 0 if not */
107+
int energy_VAD(CodecState *cs, int16_t *PCM, uint16_t frame_size, float energy);
108+
109+
#endif /* _CODEC_H_ */

toxav/msi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte
257257
_it += 3; /* place it at the field value beginning */
258258
size_max -= 3;
259259

260-
switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */
260+
switch ( _size ) { /* Compare the size of the hardcoded values ( very convenient ) */
261261

262262
case 4: { /* INFO header */
263263
if ON_HEADER ( _it, size_max, msg->info, INFO_FIELD, 4 )

toxav/toxav.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
#define _GNU_SOURCE /* implicit declaration warning */
2828

2929
#include "rtp.h"
30-
#include "media.h"
30+
#include "codec.h"
3131
#include "msi.h"
3232
#include "toxav.h"
3333

3434
#include "../toxcore/logger.h"
3535

36-
3736
#include <assert.h>
3837
#include <stdlib.h>
3938
#include <string.h>
@@ -78,7 +77,9 @@ const ToxAvCodecSettings av_DefaultSettings = {
7877
20,
7978
48000,
8079
1,
81-
20
80+
600,
81+
82+
10
8283
};
8384

8485

@@ -331,6 +332,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
331332
codec_settings->audio_frame_duration,
332333
codec_settings->audio_sample_rate,
333334
codec_settings->audio_channels,
335+
codec_settings->audio_VAD_tolerance,
334336
codec_settings->video_width,
335337
codec_settings->video_height,
336338
codec_settings->video_bitrate);
@@ -736,4 +738,11 @@ inline__ int toxav_set_video_queue_limit(ToxAv *av, int32_t call_index, uint64_t
736738
inline__ Tox *toxav_get_tox(ToxAv *av)
737739
{
738740
return (Tox *)av->messenger;
739-
}
741+
}
742+
743+
int toxav_has_activity(ToxAv *av, int32_t call_index, int16_t *PCM, uint16_t frame_size, float ref_energy)
744+
{
745+
if ( !av->calls[call_index].cs ) return ErrorInvalidCodecState;
746+
747+
return energy_VAD(av->calls[call_index].cs, PCM, frame_size, ref_energy);
748+
}

toxav/toxav.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ typedef enum {
9090
ErrorTerminatingAudioRtp = -9, /* Returned in toxav_kill_transmission() */
9191
ErrorTerminatingVideoRtp = -10, /* Returned in toxav_kill_transmission() */
9292
ErrorPacketTooLarge = -11, /* Buffer exceeds size while encoding */
93+
ErrorInvalidCodecState = -12, /* Codec state not initialized */
9394

9495
} ToxAvError;
9596

@@ -117,6 +118,7 @@ typedef struct _ToxAvCodecSettings {
117118
uint16_t audio_frame_duration; /* In ms */
118119
uint32_t audio_sample_rate; /* In Hz */
119120
uint32_t audio_channels;
121+
uint32_t audio_VAD_tolerance; /* In ms */
120122

121123
uint32_t jbuf_capacity; /* Size of jitter buffer */
122124
} ToxAvCodecSettings;
@@ -373,6 +375,8 @@ int toxav_set_video_queue_limit ( ToxAv *av, int32_t call_index, uint64_t limit
373375

374376
Tox *toxav_get_tox(ToxAv *av);
375377

378+
int toxav_has_activity ( ToxAv *av, int32_t call_index, int16_t *PCM, uint16_t frame_size, float ref_energy );
379+
376380
#ifdef __cplusplus
377381
}
378382
#endif

0 commit comments

Comments
 (0)