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"
19 EsParserTestBase::Packet::Packet()
25 EsParserTestBase::EsParserTestBase()
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
) {
48 void EsParserTestBase::NewVideoConfig(const VideoDecoderConfig
& config
) {
52 void EsParserTestBase::EmitBuffer(scoped_refptr
<StreamParserBuffer
> buffer
) {
53 buffer_timestamps_stream_
<< "("
54 << buffer
->timestamp().InMilliseconds()
59 bool EsParserTestBase::ProcessPesPackets(
61 const std::vector
<Packet
>& pes_packets
,
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
))
84 buffer_timestamps_
= buffer_timestamps_stream_
.str();
85 base::TrimWhitespaceASCII(
86 buffer_timestamps_
, base::TRIM_ALL
, &buffer_timestamps_
);
90 void EsParserTestBase::ComputePacketSize(std::vector
<Packet
>* packets
) {
92 if (packets
->size() == 0u)
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
;
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
);