2014-04-01 01:34:59 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/bind_helpers.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/memory/ref_counted.h"
|
2014-04-10 19:57:10 +00:00
|
|
|
#include "media/base/media_sample.h"
|
|
|
|
#include "media/base/stream_info.h"
|
|
|
|
#include "media/base/timestamp.h"
|
|
|
|
#include "media/base/video_stream_info.h"
|
|
|
|
#include "media/formats/mp2t/mp2t_common.h"
|
|
|
|
#include "media/formats/mp2t/mp2t_media_parser.h"
|
|
|
|
#include "media/test/test_data_util.h"
|
2014-04-01 01:34:59 +00:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
namespace mp2t {
|
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
class Mp2tMediaParserTest : public testing::Test {
|
2014-04-01 01:34:59 +00:00
|
|
|
public:
|
2014-04-10 19:57:10 +00:00
|
|
|
Mp2tMediaParserTest()
|
2014-04-01 01:34:59 +00:00
|
|
|
: audio_frame_count_(0),
|
|
|
|
video_frame_count_(0),
|
2014-04-10 19:57:10 +00:00
|
|
|
video_min_dts_(kNoTimestamp),
|
|
|
|
video_max_dts_(kNoTimestamp) {
|
2014-04-16 23:22:31 +00:00
|
|
|
parser_.reset(new Mp2tMediaParser());
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-04-10 19:57:10 +00:00
|
|
|
typedef std::map<int, scoped_refptr<StreamInfo> > StreamMap;
|
|
|
|
|
2014-04-16 23:22:31 +00:00
|
|
|
scoped_ptr<Mp2tMediaParser> parser_;
|
2014-04-10 19:57:10 +00:00
|
|
|
StreamMap stream_map_;
|
2014-04-01 01:34:59 +00:00
|
|
|
int audio_frame_count_;
|
|
|
|
int video_frame_count_;
|
2014-04-10 19:57:10 +00:00
|
|
|
int64 video_min_dts_;
|
|
|
|
int64 video_max_dts_;
|
2014-04-01 01:34:59 +00:00
|
|
|
|
|
|
|
bool AppendData(const uint8* data, size_t length) {
|
|
|
|
return parser_->Parse(data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AppendDataInPieces(const uint8* data, size_t length, size_t piece_size) {
|
|
|
|
const uint8* start = data;
|
|
|
|
const uint8* 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;
|
|
|
|
}
|
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
void OnInit(const std::vector<scoped_refptr<StreamInfo> >& stream_infos) {
|
|
|
|
DVLOG(1) << "OnInit: " << stream_infos.size() << " streams.";
|
|
|
|
for (std::vector<scoped_refptr<StreamInfo> >::const_iterator iter =
|
|
|
|
stream_infos.begin(); iter != stream_infos.end(); ++iter) {
|
|
|
|
DVLOG(1) << (*iter)->ToString();
|
|
|
|
stream_map_[(*iter)->track_id()] = *iter;
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
bool OnNewSample(uint32 track_id, const scoped_refptr<MediaSample>& sample) {
|
|
|
|
std::string stream_type;
|
|
|
|
StreamMap::const_iterator stream = stream_map_.find(track_id);
|
|
|
|
if (stream != stream_map_.end()) {
|
|
|
|
if (stream->second->stream_type() == kStreamAudio) {
|
|
|
|
++audio_frame_count_;
|
|
|
|
stream_type = "audio";
|
|
|
|
} else if (stream->second->stream_type() == kStreamVideo) {
|
|
|
|
++video_frame_count_;
|
|
|
|
stream_type = "video";
|
|
|
|
if (video_min_dts_ == kNoTimestamp)
|
|
|
|
video_min_dts_ = sample->dts();
|
|
|
|
// Verify timestamps are increasing.
|
|
|
|
if (video_max_dts_ == kNoTimestamp)
|
|
|
|
video_max_dts_ = sample->dts();
|
|
|
|
else if (video_max_dts_ >= sample->dts()) {
|
|
|
|
LOG(ERROR) << "Video DTS not strictly increasing.";
|
2014-04-01 01:34:59 +00:00
|
|
|
return false;
|
2014-04-10 19:57:10 +00:00
|
|
|
}
|
|
|
|
video_max_dts_ = sample->dts();
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Missing StreamInfo for track ID " << track_id;
|
|
|
|
return false;
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
void OnKeyNeeded(MediaContainerName container_name,
|
|
|
|
scoped_ptr<uint8[]> init_data,
|
|
|
|
int init_data_size) {
|
|
|
|
DVLOG(1) << "OnKeyNeeded: " << init_data_size;
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitializeParser() {
|
|
|
|
parser_->Init(
|
2014-04-10 19:57:10 +00:00
|
|
|
base::Bind(&Mp2tMediaParserTest::OnInit,
|
2014-04-01 01:34:59 +00:00
|
|
|
base::Unretained(this)),
|
2014-04-10 19:57:10 +00:00
|
|
|
base::Bind(&Mp2tMediaParserTest::OnNewSample,
|
2014-04-01 01:34:59 +00:00
|
|
|
base::Unretained(this)),
|
2014-04-10 19:57:10 +00:00
|
|
|
base::Bind(&Mp2tMediaParserTest::OnKeyNeeded,
|
|
|
|
base::Unretained(this)));
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseMpeg2TsFile(const std::string& filename, int append_bytes) {
|
|
|
|
InitializeParser();
|
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
std::vector<uint8> buffer = ReadTestDataFile(filename);
|
|
|
|
EXPECT_TRUE(AppendDataInPieces(buffer.data(),
|
|
|
|
buffer.size(),
|
2014-04-01 01:34:59 +00:00
|
|
|
append_bytes));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
TEST_F(Mp2tMediaParserTest, UnalignedAppend17) {
|
2014-04-01 01:34:59 +00:00
|
|
|
// Test small, non-segment-aligned appends.
|
|
|
|
ParseMpeg2TsFile("bear-1280x720.ts", 17);
|
2014-04-18 01:57:31 +00:00
|
|
|
EXPECT_EQ(video_frame_count_, 80);
|
2014-04-01 01:34:59 +00:00
|
|
|
parser_->Flush();
|
|
|
|
EXPECT_EQ(video_frame_count_, 82);
|
|
|
|
}
|
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
TEST_F(Mp2tMediaParserTest, UnalignedAppend512) {
|
2014-04-01 01:34:59 +00:00
|
|
|
// Test small, non-segment-aligned appends.
|
|
|
|
ParseMpeg2TsFile("bear-1280x720.ts", 512);
|
2014-04-18 01:57:31 +00:00
|
|
|
EXPECT_EQ(video_frame_count_, 80);
|
2014-04-01 01:34:59 +00:00
|
|
|
parser_->Flush();
|
|
|
|
EXPECT_EQ(video_frame_count_, 82);
|
|
|
|
}
|
|
|
|
|
2014-04-10 19:57:10 +00:00
|
|
|
TEST_F(Mp2tMediaParserTest, TimestampWrapAround) {
|
2014-04-01 01:34:59 +00:00
|
|
|
// "bear-1280x720_ptswraparound.ts" has been transcoded
|
|
|
|
// from bear-1280x720.mp4 by applying a time offset of 95442s
|
|
|
|
// (close to 2^33 / 90000) which results in timestamps wrap around
|
|
|
|
// in the Mpeg2 TS stream.
|
|
|
|
ParseMpeg2TsFile("bear-1280x720_ptswraparound.ts", 512);
|
2014-04-18 01:57:31 +00:00
|
|
|
parser_->Flush();
|
|
|
|
EXPECT_EQ(video_frame_count_, 82);
|
2014-04-10 19:57:10 +00:00
|
|
|
EXPECT_GE(video_min_dts_, (95443 - 1) * kMpeg2Timescale);
|
|
|
|
EXPECT_LE(video_max_dts_,
|
|
|
|
static_cast<int64>((95443 + 4)) * kMpeg2Timescale);
|
2014-04-01 01:34:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|