Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix av off sync for mp4 dvr. #4230

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions trunk/src/kernel/srs_kernel_mp4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4820,6 +4820,10 @@ uint32_t SrsMp4Sample::pts_ms()

SrsMp4SampleManager::SrsMp4SampleManager()
{
video_start_dts_ = 0;
audio_start_dts_ = 0;
has_first_video_ = false;
has_first_audio_ = false;
}

SrsMp4SampleManager::~SrsMp4SampleManager()
Expand Down Expand Up @@ -4900,6 +4904,16 @@ SrsMp4Sample* SrsMp4SampleManager::at(uint32_t index)

void SrsMp4SampleManager::append(SrsMp4Sample* sample)
{
if (!has_first_audio_ && sample->type == SrsFrameTypeAudio) {
has_first_audio_ = true;
audio_start_dts_ = sample->dts;
}

if (!has_first_video_ && sample->type == SrsFrameTypeVideo) {
has_first_video_ = true;
video_start_dts_ = sample->dts;
}

samples.push_back(sample);
}

Expand All @@ -4920,7 +4934,7 @@ srs_error_t SrsMp4SampleManager::write(SrsMp4MovieBox* moov)
}

SrsMp4SampleTableBox* stbl = vide->stbl();

SrsMp4DecodingTime2SampleBox* stts = new SrsMp4DecodingTime2SampleBox();
stbl->set_stts(stts);

Expand Down Expand Up @@ -4950,7 +4964,7 @@ srs_error_t SrsMp4SampleManager::write(SrsMp4MovieBox* moov)
co = new SrsMp4ChunkLargeOffsetBox();
stbl->set_co64(static_cast<SrsMp4ChunkLargeOffsetBox*>(co));
}

if ((err = write_track(SrsFrameTypeVideo, stts, stss, ctts, stsc, stsz, co)) != srs_success) {
return srs_error_wrap(err, "write vide track");
}
Expand Down Expand Up @@ -5077,6 +5091,11 @@ srs_error_t SrsMp4SampleManager::write_track(SrsFrameType track,
} else {
// The first sample always in the STTS table.
stts_entry.sample_count++;
if (track == SrsFrameTypeVideo) {
stts_entry.sample_delta = video_start_dts_ > audio_start_dts_ ? video_start_dts_ - audio_start_dts_ : 0;
} else if (track == SrsFrameTypeAudio) {
stts_entry.sample_delta = audio_start_dts_ > video_start_dts_ ? audio_start_dts_ - video_start_dts_ : 0;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions trunk/src/kernel/srs_kernel_mp4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,11 @@ class SrsMp4Sample
// The keyframe is specified by stss.
class SrsMp4SampleManager
{
private:
uint64_t video_start_dts_;
uint64_t audio_start_dts_;
bool has_first_video_;
bool has_first_audio_;
public:
std::vector<SrsMp4Sample*> samples;
public:
Expand Down
Loading