1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
// 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 <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <string>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
#include "media/base/audio_decoder_config.h"
#include "media/base/decoder_buffer.h"
#include "media/base/media_track.h"
#include "media/base/media_tracks.h"
#include "media/base/stream_parser_buffer.h"
#include "media/base/test_data_util.h"
#include "media/base/text_track_config.h"
#include "media/base/video_decoder_config.h"
#include "media/formats/mp2t/mp2t_stream_parser.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
namespace mp2t {
namespace {
bool IsMonotonic(const StreamParser::BufferQueue& buffers) {
if (buffers.empty())
return true;
StreamParser::BufferQueue::const_iterator it1 = buffers.begin();
StreamParser::BufferQueue::const_iterator it2 = ++it1;
for ( ; it2 != buffers.end(); ++it1, ++it2) {
if ((*it2)->GetDecodeTimestamp() < (*it1)->GetDecodeTimestamp())
return false;
}
return true;
}
bool IsAlmostEqual(DecodeTimestamp t0, DecodeTimestamp t1) {
base::TimeDelta kMaxDeviation = base::TimeDelta::FromMilliseconds(5);
base::TimeDelta diff = t1 - t0;
return (diff >= -kMaxDeviation && diff <= kMaxDeviation);
}
} // namespace
class Mp2tStreamParserTest : public testing::Test {
public:
Mp2tStreamParserTest()
: segment_count_(0),
config_count_(0),
audio_frame_count_(0),
video_frame_count_(0),
has_video_(true),
audio_min_dts_(kNoDecodeTimestamp()),
audio_max_dts_(kNoDecodeTimestamp()),
video_min_dts_(kNoDecodeTimestamp()),
video_max_dts_(kNoDecodeTimestamp()) {
bool has_sbr = false;
parser_.reset(new Mp2tStreamParser(has_sbr));
}
protected:
scoped_ptr<Mp2tStreamParser> parser_;
int segment_count_;
int config_count_;
int audio_frame_count_;
int video_frame_count_;
bool has_video_;
DecodeTimestamp audio_min_dts_;
DecodeTimestamp audio_max_dts_;
DecodeTimestamp video_min_dts_;
DecodeTimestamp video_max_dts_;
void ResetStats() {
segment_count_ = 0;
config_count_ = 0;
audio_frame_count_ = 0;
video_frame_count_ = 0;
audio_min_dts_ = kNoDecodeTimestamp();
audio_max_dts_ = kNoDecodeTimestamp();
video_min_dts_ = kNoDecodeTimestamp();
video_max_dts_ = kNoDecodeTimestamp();
}
bool AppendData(const uint8_t* data, size_t length) {
return parser_->Parse(data, 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 StreamParser::InitParameters& params) {
DVLOG(1) << "OnInit: dur=" << params.duration.InMilliseconds()
<< ", autoTimestampOffset=" << params.auto_update_timestamp_offset;
}
bool OnNewConfig(scoped_ptr<MediaTracks> tracks,
const StreamParser::TextTrackConfigMap& tc) {
const AudioDecoderConfig& ac = tracks->getFirstAudioConfig();
const VideoDecoderConfig& vc = tracks->getFirstVideoConfig();
DVLOG(1) << "OnNewConfig: media tracks count=" << tracks->tracks().size()
<< ", audio=" << ac.IsValidConfig()
<< ", video=" << vc.IsValidConfig();
config_count_++;
EXPECT_TRUE(ac.IsValidConfig());
EXPECT_EQ(vc.IsValidConfig(), has_video_);
return true;
}
void DumpBuffers(const std::string& label,
const StreamParser::BufferQueue& buffers) {
DVLOG(2) << "DumpBuffers: " << label << " size " << buffers.size();
for (StreamParser::BufferQueue::const_iterator buf = buffers.begin();
buf != buffers.end(); buf++) {
DVLOG(3) << " n=" << buf - buffers.begin()
<< ", size=" << (*buf)->data_size()
<< ", dur=" << (*buf)->duration().InMilliseconds();
}
}
bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
const StreamParser::BufferQueue& video_buffers,
const StreamParser::TextBufferQueueMap& text_map) {
EXPECT_GT(config_count_, 0);
DumpBuffers("audio_buffers", audio_buffers);
DumpBuffers("video_buffers", video_buffers);
// TODO(wolenetz/acolwell): Add text track support to more MSE parsers. See
// http://crbug.com/336926.
if (!text_map.empty())
return false;
// Verify monotonicity.
if (!IsMonotonic(video_buffers))
return false;
if (!IsMonotonic(audio_buffers))
return false;
if (!video_buffers.empty()) {
DecodeTimestamp first_dts = video_buffers.front()->GetDecodeTimestamp();
DecodeTimestamp last_dts = video_buffers.back()->GetDecodeTimestamp();
if (video_max_dts_ != kNoDecodeTimestamp() && first_dts < video_max_dts_)
return false;
if (video_min_dts_ == kNoDecodeTimestamp())
video_min_dts_ = first_dts;
video_max_dts_ = last_dts;
}
if (!audio_buffers.empty()) {
DecodeTimestamp first_dts = audio_buffers.front()->GetDecodeTimestamp();
DecodeTimestamp last_dts = audio_buffers.back()->GetDecodeTimestamp();
if (audio_max_dts_ != kNoDecodeTimestamp() && first_dts < audio_max_dts_)
return false;
if (audio_min_dts_ == kNoDecodeTimestamp())
audio_min_dts_ = first_dts;
audio_max_dts_ = last_dts;
}
audio_frame_count_ += audio_buffers.size();
video_frame_count_ += video_buffers.size();
return true;
}
void OnKeyNeeded(EmeInitDataType type,
const std::vector<uint8_t>& init_data) {
NOTREACHED() << "OnKeyNeeded not expected in the Mpeg2 TS parser";
}
void OnNewSegment() {
DVLOG(1) << "OnNewSegment";
segment_count_++;
}
void OnEndOfSegment() {
NOTREACHED() << "OnEndOfSegment not expected in the Mpeg2 TS parser";
}
void InitializeParser() {
parser_->Init(
base::Bind(&Mp2tStreamParserTest::OnInit, base::Unretained(this)),
base::Bind(&Mp2tStreamParserTest::OnNewConfig, base::Unretained(this)),
base::Bind(&Mp2tStreamParserTest::OnNewBuffers, base::Unretained(this)),
true,
base::Bind(&Mp2tStreamParserTest::OnKeyNeeded, base::Unretained(this)),
base::Bind(&Mp2tStreamParserTest::OnNewSegment, base::Unretained(this)),
base::Bind(&Mp2tStreamParserTest::OnEndOfSegment,
base::Unretained(this)),
new MediaLog());
}
bool ParseMpeg2TsFile(const std::string& filename, int append_bytes) {
scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile(filename);
EXPECT_TRUE(AppendDataInPieces(buffer->data(),
buffer->data_size(),
append_bytes));
return true;
}
};
TEST_F(Mp2tStreamParserTest, UnalignedAppend17) {
// Test small, non-segment-aligned appends.
InitializeParser();
ParseMpeg2TsFile("bear-1280x720.ts", 17);
parser_->Flush();
EXPECT_EQ(video_frame_count_, 82);
// This stream has no mid-stream configuration change.
EXPECT_EQ(config_count_, 1);
EXPECT_EQ(segment_count_, 1);
}
TEST_F(Mp2tStreamParserTest, UnalignedAppend512) {
// Test small, non-segment-aligned appends.
InitializeParser();
ParseMpeg2TsFile("bear-1280x720.ts", 512);
parser_->Flush();
EXPECT_EQ(video_frame_count_, 82);
// This stream has no mid-stream configuration change.
EXPECT_EQ(config_count_, 1);
EXPECT_EQ(segment_count_, 1);
}
TEST_F(Mp2tStreamParserTest, AppendAfterFlush512) {
InitializeParser();
ParseMpeg2TsFile("bear-1280x720.ts", 512);
parser_->Flush();
EXPECT_EQ(video_frame_count_, 82);
EXPECT_EQ(config_count_, 1);
EXPECT_EQ(segment_count_, 1);
ResetStats();
ParseMpeg2TsFile("bear-1280x720.ts", 512);
parser_->Flush();
EXPECT_EQ(video_frame_count_, 82);
EXPECT_EQ(config_count_, 1);
EXPECT_EQ(segment_count_, 1);
}
TEST_F(Mp2tStreamParserTest, TimestampWrapAround) {
// "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.
InitializeParser();
ParseMpeg2TsFile("bear-1280x720_ptswraparound.ts", 512);
parser_->Flush();
EXPECT_EQ(video_frame_count_, 82);
EXPECT_TRUE(IsAlmostEqual(video_min_dts_,
DecodeTimestamp::FromSecondsD(95443.376)));
EXPECT_TRUE(IsAlmostEqual(video_max_dts_,
DecodeTimestamp::FromSecondsD(95446.079)));
// Note: for audio, AdtsStreamParser considers only the PTS (which is then
// used as the DTS).
// TODO(damienv): most of the time, audio streams just have PTS. Here, only
// the first PES packet has a DTS, all the other PES packets have PTS only.
// Reconsider the expected value for |audio_min_dts_| if DTS are used as part
// of the ADTS stream parser.
//
// Note: the last pts for audio is 95445.931 but this PES packet includes
// 9 ADTS frames with 1 AAC frame in each ADTS frame.
// So the PTS of the last AAC frame is:
// 95445.931 + 8 * (1024 / 44100) = 95446.117
EXPECT_TRUE(IsAlmostEqual(audio_min_dts_,
DecodeTimestamp::FromSecondsD(95443.400)));
EXPECT_TRUE(IsAlmostEqual(audio_max_dts_,
DecodeTimestamp::FromSecondsD(95446.117)));
}
TEST_F(Mp2tStreamParserTest, AudioInPrivateStream1) {
// Test small, non-segment-aligned appends.
InitializeParser();
has_video_ = false;
ParseMpeg2TsFile("bear_adts_in_private_stream_1.ts", 512);
parser_->Flush();
EXPECT_EQ(audio_frame_count_, 40);
EXPECT_EQ(video_frame_count_, 0);
// This stream has no mid-stream configuration change.
EXPECT_EQ(config_count_, 1);
EXPECT_EQ(segment_count_, 1);
}
} // namespace mp2t
} // namespace media
|