提交 de9025c9 authored 作者: Anthony Minessale's avatar Anthony Minessale 提交者: Mike Jerris

FS-10249: [mod_av] Audio gradually falls behind video in recordings #comment…

FS-10249: [mod_av] Audio gradually falls behind video in recordings #comment Based on that feedback, please try latest master
上级 7c13d0b2
...@@ -75,7 +75,7 @@ typedef struct record_helper_s { ...@@ -75,7 +75,7 @@ typedef struct record_helper_s {
switch_mutex_t *mutex; switch_mutex_t *mutex;
AVFormatContext *fc; AVFormatContext *fc;
MediaStream *video_st; MediaStream *video_st;
switch_timer_t *timer; switch_timer_t *video_timer;
int in_callback; int in_callback;
switch_queue_t *video_queue; switch_queue_t *video_queue;
switch_thread_t *video_thread; switch_thread_t *video_thread;
...@@ -98,6 +98,7 @@ struct av_file_context { ...@@ -98,6 +98,7 @@ struct av_file_context {
switch_buffer_t *buf; switch_buffer_t *buf;
switch_buffer_t *audio_buffer; switch_buffer_t *audio_buffer;
switch_timer_t video_timer; switch_timer_t video_timer;
switch_timer_t audio_timer;
int offset; int offset;
int audio_start; int audio_start;
int aud_ready; int aud_ready;
...@@ -420,6 +421,11 @@ static switch_status_t add_stream(MediaStream *mst, AVFormatContext *fc, AVCodec ...@@ -420,6 +421,11 @@ static switch_status_t add_stream(MediaStream *mst, AVFormatContext *fc, AVCodec
c->sample_rate = mst->sample_rate = mm->samplerate; c->sample_rate = mst->sample_rate = mm->samplerate;
} }
} }
//FUCKER
mst->st->time_base.den = c->sample_rate;
mst->st->time_base.num = 1;
c->time_base.den = c->sample_rate;
c->time_base.num = 1;
break; break;
...@@ -805,7 +811,7 @@ static void *SWITCH_THREAD_FUNC video_thread_run(switch_thread_t *thread, void * ...@@ -805,7 +811,7 @@ static void *SWITCH_THREAD_FUNC video_thread_run(switch_thread_t *thread, void *
} else { } else {
uint64_t delta_tmp; uint64_t delta_tmp;
switch_core_timer_sync(context->eh.timer); switch_core_timer_sync(context->eh.video_timer);
if (context->eh.record_timer_paused) { if (context->eh.record_timer_paused) {
continue; continue;
...@@ -822,10 +828,10 @@ static void *SWITCH_THREAD_FUNC video_thread_run(switch_thread_t *thread, void * ...@@ -822,10 +828,10 @@ static void *SWITCH_THREAD_FUNC video_thread_run(switch_thread_t *thread, void *
context->eh.video_st->frame->pts += delta; context->eh.video_st->frame->pts += delta;
delta_tmp = delta; delta_tmp = delta;
context->eh.record_timer_offset = context->eh.timer->samplecount - context->eh.video_st->frame->pts; context->eh.record_timer_offset = context->eh.video_timer->samplecount - context->eh.video_st->frame->pts;
context->eh.record_timer_offset = context->eh.record_timer_offset > 0 ? context->eh.record_timer_offset : 0; context->eh.record_timer_offset = context->eh.record_timer_offset > 0 ? context->eh.record_timer_offset : 0;
} else { } else {
delta_tmp = context->eh.timer->samplecount - last_ts; delta_tmp = context->eh.video_timer->samplecount - last_ts;
} }
if (delta_tmp != 0) { if (delta_tmp != 0) {
...@@ -842,9 +848,9 @@ static void *SWITCH_THREAD_FUNC video_thread_run(switch_thread_t *thread, void * ...@@ -842,9 +848,9 @@ static void *SWITCH_THREAD_FUNC video_thread_run(switch_thread_t *thread, void *
delta_avg = (int)(double)(delta_sum / delta_i); delta_avg = (int)(double)(delta_sum / delta_i);
} }
context->eh.video_st->frame->pts = context->eh.timer->samplecount - context->eh.record_timer_offset; context->eh.video_st->frame->pts = context->eh.video_timer->samplecount - context->eh.record_timer_offset;
} else { } else {
context->eh.video_st->frame->pts = (context->eh.timer->samplecount) + 1; context->eh.video_st->frame->pts = (context->eh.video_timer->samplecount) + 1;
} }
} }
...@@ -1079,7 +1085,7 @@ SWITCH_STANDARD_APP(record_av_function) ...@@ -1079,7 +1085,7 @@ SWITCH_STANDARD_APP(record_av_function)
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Timer Activation Fail\n"); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Timer Activation Fail\n");
goto end; goto end;
} }
context.eh.timer = &timer; context.eh.video_timer = &timer;
switch_queue_create(&context.eh.video_queue, SWITCH_CORE_QUEUE_LEN, switch_core_session_get_pool(session)); switch_queue_create(&context.eh.video_queue, SWITCH_CORE_QUEUE_LEN, switch_core_session_get_pool(session));
switch_core_session_set_video_read_callback(session, video_read_callback, (void *)&context.eh); switch_core_session_set_video_read_callback(session, video_read_callback, (void *)&context.eh);
...@@ -2006,6 +2012,10 @@ static switch_status_t av_file_open(switch_file_handle_t *handle, const char *pa ...@@ -2006,6 +2012,10 @@ static switch_status_t av_file_open(switch_file_handle_t *handle, const char *pa
switch_core_timer_destroy(&context->video_timer); switch_core_timer_destroy(&context->video_timer);
} }
if (context->audio_timer.interval) {
switch_core_timer_destroy(&context->audio_timer);
}
if (context->audio_buffer) { if (context->audio_buffer) {
switch_buffer_destroy(&context->audio_buffer); switch_buffer_destroy(&context->audio_buffer);
} }
...@@ -2105,14 +2115,21 @@ static switch_status_t av_file_write(switch_file_handle_t *handle, void *data, s ...@@ -2105,14 +2115,21 @@ static switch_status_t av_file_write(switch_file_handle_t *handle, void *data, s
continue; continue;
} }
context->audio_st.tmp_frame->pts = context->audio_st.next_pts; switch_core_timer_sync(&context->audio_timer);
context->audio_st.next_pts += context->audio_st.frame->nb_samples; context->audio_st.tmp_frame->pts = context->audio_timer.samplecount;
context->audio_st.next_pts = context->audio_st.tmp_frame->pts + context->audio_st.frame->nb_samples;
//context->audio_st.tmp_frame->pts = context->audio_st.next_pts;
//context->audio_st.next_pts += context->audio_st.frame->nb_samples;
ret = avcodec_encode_audio2(context->audio_st.st->codec, &pkt, context->audio_st.tmp_frame, &got_packet); ret = avcodec_encode_audio2(context->audio_st.st->codec, &pkt, context->audio_st.tmp_frame, &got_packet);
} else { } else {
av_frame_make_writable(context->audio_st.frame); av_frame_make_writable(context->audio_st.frame);
switch_buffer_read(context->audio_buffer, context->audio_st.frame->data[0], bytes); switch_buffer_read(context->audio_buffer, context->audio_st.frame->data[0], bytes);
context->audio_st.frame->pts = context->audio_st.next_pts; switch_core_timer_sync(&context->audio_timer);
context->audio_st.next_pts += context->audio_st.frame->nb_samples; context->audio_st.frame->pts = context->audio_timer.samplecount;
context->audio_st.next_pts = context->audio_st.frame->pts + context->audio_st.frame->nb_samples;
//context->audio_st.frame->pts = context->audio_st.next_pts;
//context->audio_st.next_pts += context->audio_st.frame->nb_samples;
ret = avcodec_encode_audio2(context->audio_st.st->codec, &pkt, context->audio_st.frame, &got_packet); ret = avcodec_encode_audio2(context->audio_st.st->codec, &pkt, context->audio_st.frame, &got_packet);
} }
...@@ -2233,6 +2250,10 @@ static switch_status_t av_file_close(switch_file_handle_t *handle) ...@@ -2233,6 +2250,10 @@ static switch_status_t av_file_close(switch_file_handle_t *handle)
switch_core_timer_destroy(&context->video_timer); switch_core_timer_destroy(&context->video_timer);
} }
if (context->audio_timer.interval) {
switch_core_timer_destroy(&context->audio_timer);
}
switch_img_free(&context->last_img); switch_img_free(&context->last_img);
switch_buffer_destroy(&context->audio_buffer); switch_buffer_destroy(&context->audio_buffer);
...@@ -2628,7 +2649,8 @@ static switch_status_t av_file_write_video(switch_file_handle_t *handle, switch_ ...@@ -2628,7 +2649,8 @@ static switch_status_t av_file_write_video(switch_file_handle_t *handle, switch_
//switch_threadattr_priority_set(thd_attr, SWITCH_PRI_REALTIME); //switch_threadattr_priority_set(thd_attr, SWITCH_PRI_REALTIME);
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
switch_core_timer_init(&context->video_timer, "soft", 1, 90, context->pool); switch_core_timer_init(&context->video_timer, "soft", 1, 90, context->pool);
context->eh.timer = &context->video_timer; switch_core_timer_init(&context->audio_timer, "soft", 1, handle->samplerate / 1000, context->pool);
context->eh.video_timer = &context->video_timer;
context->audio_st.frame->pts = 0; context->audio_st.frame->pts = 0;
context->audio_st.next_pts = 0; context->audio_st.next_pts = 0;
switch_thread_create(&context->eh.video_thread, thd_attr, video_thread_run, context, handle->memory_pool); switch_thread_create(&context->eh.video_thread, thd_attr, video_thread_run, context, handle->memory_pool);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论