Skip to content

Commit

Permalink
feat: include unit test for PTS < DTS fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lee.fordyce committed Feb 3, 2024
1 parent e95a9b7 commit 9011f88
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
105 changes: 105 additions & 0 deletions packager/live_packager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <packager/media/base/media_sample.h>
#include <packager/media/base/raw_key_source.h>
#include <packager/media/base/stream_info.h>
#include <packager/media/formats/mp2t/mp2t_media_parser.h>
#include <packager/media/formats/mp2t/program_map_table_writer.h>
#include <packager/media/formats/mp2t/ts_packet.h>
#include <packager/media/formats/mp2t/ts_section.h>
Expand Down Expand Up @@ -461,6 +462,76 @@ class LivePackagerBaseTest : public ::testing::Test {
std::vector<uint8_t> key_id_;
};

class LivePackagerMp2tTest : public LivePackagerBaseTest {
public:
LivePackagerMp2tTest() : LivePackagerBaseTest() {}

void SetUp() override {
LivePackagerBaseTest::SetUp();
parser_.reset(new media::mp2t::Mp2tMediaParser());
InitializeParser();
}

protected:
typedef std::map<int, std::shared_ptr<media::StreamInfo>> StreamMap;

std::unique_ptr<media::mp2t::Mp2tMediaParser> parser_;
StreamMap stream_map_;

bool AppendData(const uint8_t* data, size_t length) {
return parser_->Parse(data, static_cast<int>(length));
}

bool AppendDataInPieces(const uint8_t* data,
size_t length,
size_t piece_size) {
const uint8_t* start = data;
const uint8_t* end = data + length;
while (start < end) {
size_t append_size =
std::min(piece_size, static_cast<size_t>(end - start));
if (!AppendData(start, append_size))
return false;
start += append_size;
}
return true;
}

void OnInit(
const std::vector<std::shared_ptr<media::StreamInfo>>& stream_infos) {
for (const auto& stream_info : stream_infos) {
stream_map_[stream_info->track_id()] = stream_info;
}
}

bool OnNewSample(uint32_t track_id,
std::shared_ptr<media::MediaSample> sample) {
StreamMap::const_iterator stream = stream_map_.find(track_id);
EXPECT_NE(stream_map_.end(), stream);
if (stream != stream_map_.end()) {
if (stream->second->stream_type() == media::kStreamVideo) {
EXPECT_GE(sample->pts(), sample->dts());
}
}
return true;
}

bool OnNewTextSample(uint32_t track_id,
std::shared_ptr<media::TextSample> sample) {
return false;
}

void InitializeParser() {
parser_->Init(
std::bind(&LivePackagerMp2tTest::OnInit, this, std::placeholders::_1),
std::bind(&LivePackagerMp2tTest::OnNewSample, this,
std::placeholders::_1, std::placeholders::_2),
std::bind(&LivePackagerMp2tTest::OnNewTextSample, this,
std::placeholders::_1, std::placeholders::_2),
NULL);
}
};

TEST_F(LivePackagerBaseTest, InitSegmentOnly) {
std::vector<uint8_t> init_segment_buffer = ReadTestDataFile("input/init.mp4");
ASSERT_FALSE(init_segment_buffer.empty());
Expand Down Expand Up @@ -670,6 +741,40 @@ TEST_F(LivePackagerBaseTest, CheckContinutityCounter) {
}
}

TEST_F(LivePackagerMp2tTest, Mp2TSNegativeCTS) {
std::vector<uint8_t> init_segment_buffer = ReadTestDataFile("input/init.mp4");
ASSERT_FALSE(init_segment_buffer.empty());

FullSegmentBuffer actual_buf;

for (unsigned int i = 0; i < kNumSegments; i++) {
std::string segment_num = absl::StrFormat("input/%04d.m4s", i);
std::vector<uint8_t> segment_buffer = ReadTestDataFile(segment_num);
ASSERT_FALSE(segment_buffer.empty());

SegmentData init_seg(init_segment_buffer.data(),
init_segment_buffer.size());
SegmentData media_seg(segment_buffer.data(), segment_buffer.size());

FullSegmentBuffer out;

LiveConfig live_config;
live_config.format = LiveConfig::OutputFormat::TS;
live_config.track_type = LiveConfig::TrackType::VIDEO;
live_config.protection_scheme = LiveConfig::EncryptionScheme::NONE;
live_config.segment_number = i;

SetupLivePackagerConfig(live_config);
ASSERT_EQ(Status::OK, live_packager_->Package(init_seg, media_seg, out));
ASSERT_GT(out.SegmentSize(), 0);
actual_buf.AppendData(out.SegmentData(), out.SegmentSize());
}

ASSERT_TRUE(
AppendDataInPieces(actual_buf.Data(), actual_buf.SegmentSize(), 512));
EXPECT_TRUE(parser_->Flush());
}

TEST_F(LivePackagerBaseTest, CustomMoofSequenceNumber) {
std::vector<uint8_t> init_segment_buffer = ReadTestDataFile("input/init.mp4");
ASSERT_FALSE(init_segment_buffer.empty());
Expand Down
2 changes: 2 additions & 0 deletions packager/media/formats/mp4/mp4_media_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ bool MP4MediaParser::ParseMoov(BoxReader* reader) {
return false;
// TODO: have this be conditional based on parameter setting
runs_.reset(new TrackRunIteratorExt(moov_.get()));
// runs_.reset(new TrackRunIterator(moov_.get()));
RCHECK(runs_->Init());
ChangeState(kEmittingSamples);
return true;
Expand All @@ -763,6 +764,7 @@ bool MP4MediaParser::ParseMoof(BoxReader* reader) {
if (!runs_)
// TODO: have this be conditional based on parameter setting
runs_.reset(new TrackRunIteratorExt(moov_.get()));
// runs_.reset(new TrackRunIterator(moov_.get()));
RCHECK(runs_->Init(moof));
if (!FetchKeysIfNecessary(moof.pssh))
return false;
Expand Down
4 changes: 0 additions & 4 deletions packager/media/formats/mp4/track_run_iterator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ static void PopulateSampleInfo(const TrackExtends& trex,

if (i < trun.sample_composition_time_offsets.size()) {
sample_info->cts_offset = trun.sample_composition_time_offsets[i];
// TODO: should adjustment be made here?
if (sample_info->cts_offset < 0) {
LOG(WARNING) << " negative cts offset: " << sample_info->cts_offset;
}
} else {
sample_info->cts_offset = 0;
}
Expand Down

0 comments on commit 9011f88

Please sign in to comment.