Commit ffacf567 authored by Simon Morlat's avatar Simon Morlat
Browse files

implement a better algorithm for sending of I frames at startup of video stream

parent 21471e31
......@@ -63,6 +63,36 @@ void ms_ffmpeg_check_init(){
}
}
/* the goal of this small object is to tell when to send I frames at startup:
at 2 and 4 seconds*/
typedef struct VideoStarter{
uint64_t next_time;
int i_frame_count;
}VideoStarter;
static void video_starter_init(VideoStarter *vs){
vs->next_time=0;
vs->i_frame_count=0;
}
static void video_starter_first_frame(VideoStarter *vs, uint64_t curtime){
vs->next_time=curtime+2000;
}
static bool_t video_starter_need_i_frame(VideoStarter *vs, uint64_t curtime){
if (vs->next_time==0) return FALSE;
if (curtime>=vs->next_time){
vs->i_frame_count++;
if (vs->i_frame_count==1){
vs->next_time+=2000;
}else{
vs->next_time=0;
}
return TRUE;
}
return FALSE;
}
typedef struct EncState{
AVCodecContext av_context;
AVCodec *av_codec;
......@@ -75,6 +105,7 @@ typedef struct EncState{
int maxbr;
int qmin;
uint32_t framenum;
VideoStarter starter;
bool_t req_vfu;
}EncState;
......@@ -314,8 +345,10 @@ static void enc_preprocess(MSFilter *f){
ms_error("avcodec_open() failed: %i",error);
return;
}
video_starter_init(&s->starter);
ms_debug("image format is %i.",s->av_context.pix_fmt);
ms_message("qmin=%i qmax=%i",s->av_context.qmin,s->av_context.qmax);
s->framenum=0;
}
static void enc_postprocess(MSFilter *f){
......@@ -737,7 +770,7 @@ static void process_frame(MSFilter *f, mblk_t *inm){
/* timestamp used by ffmpeg, unset here */
pict.pts=AV_NOPTS_VALUE;
if (s->framenum==(int)(s->fps*2.0) || s->framenum==(int)(s->fps*4.0)){
if (video_starter_need_i_frame (&s->starter,f->ticker->time)){
/*sends an I frame at 2 seconds and 4 seconds after the beginning of the call*/
s->req_vfu=TRUE;
}
......@@ -757,6 +790,9 @@ static void process_frame(MSFilter *f, mblk_t *inm){
if (error<=0) ms_warning("ms_AVencoder_process: error %i.",error);
else{
s->framenum++;
if (s->framenum==1){
video_starter_first_frame (&s->starter,f->ticker->time);
}
if (c->coded_frame->pict_type==FF_I_TYPE){
ms_message("Emitting I-frame");
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment