Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / mp2t / es_parser_test_base.cc
blobcba060fff177f0fdd7c581fa3e3bb33746437890
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/strings/string_util.h"
10 #include "media/base/buffers.h"
11 #include "media/base/stream_parser_buffer.h"
12 #include "media/base/test_data_util.h"
13 #include "media/formats/mp2t/es_parser.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace media {
17 namespace mp2t {
19 EsParserTestBase::Packet::Packet()
20 : offset(0u),
21 size(0u),
22 pts(kNoTimestamp()) {
25 EsParserTestBase::EsParserTestBase()
26 : config_count_(0u),
27 buffer_count_(0u) {
30 EsParserTestBase::~EsParserTestBase() {
33 void EsParserTestBase::LoadStream(const char* filename) {
34 base::FilePath file_path = GetTestDataFilePath(filename);
36 base::MemoryMappedFile stream;
37 ASSERT_TRUE(stream.Initialize(file_path))
38 << "Couldn't open stream file: " << file_path.MaybeAsASCII();
40 stream_.resize(stream.length());
41 memcpy(&stream_[0], stream.data(), stream_.size());
44 void EsParserTestBase::NewAudioConfig(const AudioDecoderConfig& config) {
45 config_count_++;
48 void EsParserTestBase::NewVideoConfig(const VideoDecoderConfig& config) {
49 config_count_++;
52 void EsParserTestBase::EmitBuffer(scoped_refptr<StreamParserBuffer> buffer) {
53 buffer_timestamps_stream_ << "("
54 << buffer->timestamp().InMilliseconds()
55 << ") ";
56 buffer_count_++;
59 bool EsParserTestBase::ProcessPesPackets(
60 EsParser* es_parser,
61 const std::vector<Packet>& pes_packets,
62 bool force_timing) {
63 DCHECK(es_parser);
65 buffer_count_ = 0;
66 config_count_ = 0;
67 buffer_timestamps_stream_.str(std::string());
69 for (size_t k = 0; k < pes_packets.size(); k++) {
70 size_t cur_pes_offset = pes_packets[k].offset;
71 size_t cur_pes_size = pes_packets[k].size;
73 base::TimeDelta pts = kNoTimestamp();
74 DecodeTimestamp dts = kNoDecodeTimestamp();
75 if (pes_packets[k].pts >= base::TimeDelta() || force_timing)
76 pts = pes_packets[k].pts;
78 DCHECK_LT(cur_pes_offset, stream_.size());
79 if (!es_parser->Parse(&stream_[cur_pes_offset], cur_pes_size, pts, dts))
80 return false;
82 es_parser->Flush();
84 buffer_timestamps_ = buffer_timestamps_stream_.str();
85 base::TrimWhitespaceASCII(
86 buffer_timestamps_, base::TRIM_ALL, &buffer_timestamps_);
87 return true;
90 void EsParserTestBase::ComputePacketSize(std::vector<Packet>* packets) {
91 DCHECK(packets);
92 if (packets->size() == 0u)
93 return;
95 Packet* cur = &(*packets)[0];
96 for (size_t k = 0; k < packets->size() - 1; k++) {
97 Packet* next = &(*packets)[k + 1];
98 DCHECK_GE(next->offset, cur->offset);
99 cur->size = next->offset - cur->offset;
100 cur = next;
102 DCHECK_GE(stream_.size(), cur->offset);
103 cur->size = stream_.size() - cur->offset;
106 std::vector<EsParserTestBase::Packet>
107 EsParserTestBase::GenerateFixedSizePesPacket(size_t pes_size) {
108 DCHECK_GT(stream_.size(), 0u);
109 std::vector<Packet> pes_packets;
111 Packet cur_pes_packet;
112 cur_pes_packet.offset = 0;
113 cur_pes_packet.pts = kNoTimestamp();
114 while (cur_pes_packet.offset < stream_.size()) {
115 pes_packets.push_back(cur_pes_packet);
116 cur_pes_packet.offset += pes_size;
118 ComputePacketSize(&pes_packets);
120 return pes_packets;
123 } // namespace mp2t
124 } // namespace media