1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/formats/mp2t/es_parser_test_base.h"
7 #include "base/files/memory_mapped_file.h"
8 #include "base/logging.h"
9 #include "base/path_service.h"
10 #include "base/strings/string_util.h"
11 #include "media/base/buffers.h"
12 #include "media/base/stream_parser_buffer.h"
13 #include "media/base/test_data_util.h"
14 #include "media/formats/mp2t/es_parser.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 EsParserTestBase::Packet::Packet()
26 EsParserTestBase::EsParserTestBase()
31 EsParserTestBase::~EsParserTestBase() {
34 void EsParserTestBase::LoadStream(const char* filename) {
35 base::FilePath file_path = GetTestDataFilePath(filename);
37 base::MemoryMappedFile stream;
38 ASSERT_TRUE(stream.Initialize(file_path))
39 << "Couldn't open stream file: " << file_path.MaybeAsASCII();
41 stream_.resize(stream.length());
42 memcpy(&stream_[0], stream.data(), stream_.size());
45 void EsParserTestBase::NewAudioConfig(const AudioDecoderConfig& config) {
49 void EsParserTestBase::NewVideoConfig(const VideoDecoderConfig& config) {
53 void EsParserTestBase::EmitBuffer(scoped_refptr<StreamParserBuffer> buffer) {
54 buffer_timestamps_stream_ << "("
55 << buffer->timestamp().InMilliseconds()
60 bool EsParserTestBase::ProcessPesPackets(
62 const std::vector<Packet>& pes_packets,
68 buffer_timestamps_stream_.str(std::string());
70 for (size_t k = 0; k < pes_packets.size(); k++) {
71 size_t cur_pes_offset = pes_packets[k].offset;
72 size_t cur_pes_size = pes_packets[k].size;
74 base::TimeDelta pts = kNoTimestamp();
75 DecodeTimestamp dts = kNoDecodeTimestamp();
76 if (pes_packets[k].pts >= base::TimeDelta() || force_timing)
77 pts = pes_packets[k].pts;
79 DCHECK_LT(cur_pes_offset, stream_.size());
80 if (!es_parser->Parse(&stream_[cur_pes_offset], cur_pes_size, pts, dts))
85 buffer_timestamps_ = buffer_timestamps_stream_.str();
86 base::TrimWhitespaceASCII(
87 buffer_timestamps_, base::TRIM_ALL, &buffer_timestamps_);
91 void EsParserTestBase::ComputePacketSize(std::vector<Packet>* packets) {
93 if (packets->size() == 0u)
96 Packet* cur = &(*packets)[0];
97 for (size_t k = 0; k < packets->size() - 1; k++) {
98 Packet* next = &(*packets)[k + 1];
99 DCHECK_GE(next->offset, cur->offset);
100 cur->size = next->offset - cur->offset;
103 DCHECK_GE(stream_.size(), cur->offset);
104 cur->size = stream_.size() - cur->offset;