Skip to content

Commit

Permalink
Improved the filter to avoid buffering or sync problems during video …
Browse files Browse the repository at this point in the history
…transitions in the scheduler.
  • Loading branch information
Keukhan committed Dec 3, 2024
1 parent ad1739a commit 301d728
Show file tree
Hide file tree
Showing 9 changed files with 525 additions and 344 deletions.
7 changes: 7 additions & 0 deletions src/projects/transcoder/filter/filter_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class FilterBase
return _src_height;
}

// If the input track and output track are the same, the filter is used for a single track.
// The main goal of this filter is to handle frame drops.
bool IsSingleTrack() const
{
return (_input_track == _output_track)? true : false;
}

void SetCompleteHandler(CompleteHandler complete_handler) {
_complete_handler = complete_handler;
}
Expand Down
22 changes: 16 additions & 6 deletions src/projects/transcoder/filter/filter_resampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,17 @@ bool FilterResampler::InitializeFilterDescription()
{
std::vector<ov::String> filters;

filters.push_back(ov::String::FormatString("asettb=%s", _output_track->GetTimeBase().GetStringExpr().CStr()));
filters.push_back(ov::String::FormatString("aresample=async=1000"));
filters.push_back(ov::String::FormatString("aresample=%d", _output_track->GetSampleRate()));
filters.push_back(ov::String::FormatString("aformat=sample_fmts=%s:channel_layouts=%s", _output_track->GetSample().GetName(), _output_track->GetChannel().GetName()));
filters.push_back(ov::String::FormatString("asetnsamples=n=%d", _output_track->GetAudioSamplesPerFrame()));
if(IsSingleTrack())
{
filters.push_back(ov::String::FormatString("aresample=async=1"));
filters.push_back(ov::String::FormatString("asetnsamples=n=%d", _output_track->GetAudioSamplesPerFrame()));
}
else
{
filters.push_back(ov::String::FormatString("asettb=%s", _output_track->GetTimeBase().GetStringExpr().CStr()));
filters.push_back(ov::String::FormatString("aresample=%d", _output_track->GetSampleRate()));
filters.push_back(ov::String::FormatString("aformat=sample_fmts=%s:channel_layouts=%s", _output_track->GetSample().GetName(), _output_track->GetChannel().GetName()));
}

if (filters.size() == 0)
{
Expand Down Expand Up @@ -227,7 +233,8 @@ void FilterResampler::Stop()

void FilterResampler::WorkerThread()
{
if(_codec_init_event.Submit(Configure(_input_track, _output_track)) == false)
auto result = Configure(_input_track, _output_track);
if (_codec_init_event.Submit(result) == false)
{
return;
}
Expand Down Expand Up @@ -256,6 +263,8 @@ void FilterResampler::WorkerThread()
break;
}

// logtw("Resampled in frame. pts: %lld, linesize: %d, samples: %d", av_frame->pts, av_frame->linesize[0], av_frame->nb_samples);

ret = ::av_buffersrc_write_frame(_buffersrc_ctx, av_frame);
if (ret < 0)
{
Expand Down Expand Up @@ -291,6 +300,7 @@ void FilterResampler::WorkerThread()
}
else
{
// logti("Resampled out frame. pts: %lld, linesize: %d, samples : %d", _frame->pts, _frame->linesize[0], _frame->nb_samples);
auto output_frame = ffmpeg::Conv::ToMediaFrame(cmn::MediaType::Audio, _frame);
::av_frame_unref(_frame);
if (output_frame == nullptr)
Expand Down
Loading

0 comments on commit 301d728

Please sign in to comment.