Commit a64e57df authored by damienv@chromium.org's avatar damienv@chromium.org

Mpeg2 TS - Fail when no valid timestamp in the ADTS parser.

BUG=None

Review URL: https://codereview.chromium.org/399433003

Cr-Commit-Position: refs/heads/master@{#289944}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289944 0039d316-1c4b-4281-b951-d872f2087c98
parent 0fe90c4a
...@@ -512,7 +512,10 @@ test("media_unittests") { ...@@ -512,7 +512,10 @@ test("media_unittests") {
"formats/common/stream_parser_test_base.cc", "formats/common/stream_parser_test_base.cc",
"formats/common/stream_parser_test_base.h", "formats/common/stream_parser_test_base.h",
"formats/mp2t/es_adapter_video_unittest.cc", "formats/mp2t/es_adapter_video_unittest.cc",
"formats/mp2t/es_parser_adts_unittest.cc",
"formats/mp2t/es_parser_h264_unittest.cc", "formats/mp2t/es_parser_h264_unittest.cc",
"formats/mp2t/es_parser_test_base.cc",
"formats/mp2t/es_parser_test_base.h",
"formats/mp2t/mp2t_stream_parser_unittest.cc", "formats/mp2t/mp2t_stream_parser_unittest.cc",
"formats/mp4/aac_unittest.cc", "formats/mp4/aac_unittest.cc",
"formats/mp4/avc_unittest.cc", "formats/mp4/avc_unittest.cc",
......
...@@ -151,6 +151,10 @@ bool EsParserAdts::Parse(const uint8* buf, int size, ...@@ -151,6 +151,10 @@ bool EsParserAdts::Parse(const uint8* buf, int size,
pts_list_.pop_front(); pts_list_.pop_front();
} }
if (audio_timestamp_helper_->base_timestamp() == kNoTimestamp()) {
DVLOG(1) << "Audio frame with unknown timestamp";
return false;
}
base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp(); base::TimeDelta current_pts = audio_timestamp_helper_->GetTimestamp();
base::TimeDelta frame_duration = base::TimeDelta frame_duration =
audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame); audio_timestamp_helper_->GetFrameDuration(kSamplesPerAACFrame);
...@@ -246,7 +250,8 @@ bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) { ...@@ -246,7 +250,8 @@ bool EsParserAdts::UpdateAudioConfiguration(const uint8* adts_header) {
DVLOG(1) << "Channel config: " << channel_configuration; DVLOG(1) << "Channel config: " << channel_configuration;
DVLOG(1) << "Adts profile: " << adts_profile; DVLOG(1) << "Adts profile: " << adts_profile;
// Reset the timestamp helper to use a new time scale. // Reset the timestamp helper to use a new time scale.
if (audio_timestamp_helper_) { if (audio_timestamp_helper_ &&
audio_timestamp_helper_->base_timestamp() != kNoTimestamp()) {
base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp(); base::TimeDelta base_timestamp = audio_timestamp_helper_->GetTimestamp();
audio_timestamp_helper_.reset( audio_timestamp_helper_.reset(
new AudioTimestampHelper(samples_per_second)); new AudioTimestampHelper(samples_per_second));
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "media/base/audio_decoder_config.h" #include "media/base/audio_decoder_config.h"
#include "media/base/media_export.h"
#include "media/formats/mp2t/es_parser.h" #include "media/formats/mp2t/es_parser.h"
namespace media { namespace media {
...@@ -25,7 +26,7 @@ class StreamParserBuffer; ...@@ -25,7 +26,7 @@ class StreamParserBuffer;
namespace media { namespace media {
namespace mp2t { namespace mp2t {
class EsParserAdts : public EsParser { class MEDIA_EXPORT EsParserAdts : public EsParser {
public: public:
typedef base::Callback<void(const AudioDecoderConfig&)> NewAudioConfigCB; typedef base::Callback<void(const AudioDecoderConfig&)> NewAudioConfigCB;
......
// 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 <vector>
#include "base/bind.h"
#include "base/logging.h"
#include "base/time/time.h"
#include "media/base/buffers.h"
#include "media/base/stream_parser_buffer.h"
#include "media/formats/mp2t/es_parser_adts.h"
#include "media/formats/mp2t/es_parser_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
class AudioDecoderConfig;
namespace mp2t {
class EsParserAdtsTest : public EsParserTestBase,
public testing::Test {
public:
EsParserAdtsTest();
virtual ~EsParserAdtsTest() {}
protected:
bool Process(const std::vector<Packet>& pes_packets, bool force_timing);
std::vector<Packet> GenerateFixedSizePesPacket(size_t pes_size);
private:
DISALLOW_COPY_AND_ASSIGN(EsParserAdtsTest);
};
EsParserAdtsTest::EsParserAdtsTest() {
}
bool EsParserAdtsTest::Process(
const std::vector<Packet>& pes_packets,
bool force_timing) {
EsParserAdts es_parser(
base::Bind(&EsParserAdtsTest::NewAudioConfig, base::Unretained(this)),
base::Bind(&EsParserAdtsTest::EmitBuffer, base::Unretained(this)),
false);
return ProcessPesPackets(&es_parser, pes_packets, force_timing);
}
std::vector<EsParserTestBase::Packet>
EsParserAdtsTest::GenerateFixedSizePesPacket(size_t pes_size) {
DCHECK_GT(stream_.size(), 0u);
std::vector<Packet> pes_packets;
Packet cur_pes_packet;
cur_pes_packet.offset = 0;
cur_pes_packet.pts = kNoTimestamp();
while (cur_pes_packet.offset < stream_.size()) {
pes_packets.push_back(cur_pes_packet);
cur_pes_packet.offset += pes_size;
}
ComputePacketSize(&pes_packets);
return pes_packets;
}
TEST_F(EsParserAdtsTest, NoInitialPts) {
LoadStream("bear.adts");
std::vector<Packet> pes_packets = GenerateFixedSizePesPacket(512);
EXPECT_FALSE(Process(pes_packets, false));
EXPECT_EQ(0u, buffer_count_);
}
TEST_F(EsParserAdtsTest, SinglePts) {
LoadStream("bear.adts");
std::vector<Packet> pes_packets = GenerateFixedSizePesPacket(512);
pes_packets.front().pts = base::TimeDelta::FromSeconds(10);
EXPECT_TRUE(Process(pes_packets, false));
EXPECT_EQ(1u, config_count_);
EXPECT_EQ(45u, buffer_count_);
}
} // namespace mp2t
} // namespace media
// 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 "media/formats/mp2t/es_parser_test_base.h"
#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "media/base/buffers.h"
#include "media/base/stream_parser_buffer.h"
#include "media/base/test_data_util.h"
#include "media/formats/mp2t/es_parser.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
namespace mp2t {
EsParserTestBase::Packet::Packet()
: offset(0u),
size(0u),
pts(kNoTimestamp()) {
}
EsParserTestBase::EsParserTestBase()
: config_count_(0u),
buffer_count_(0u) {
}
EsParserTestBase::~EsParserTestBase() {
}
void EsParserTestBase::LoadStream(const char* filename) {
base::FilePath file_path = GetTestDataFilePath(filename);
base::MemoryMappedFile stream;
ASSERT_TRUE(stream.Initialize(file_path))
<< "Couldn't open stream file: " << file_path.MaybeAsASCII();
stream_.resize(stream.length());
memcpy(&stream_[0], stream.data(), stream_.size());
}
void EsParserTestBase::NewAudioConfig(const AudioDecoderConfig& config) {
config_count_++;
}
void EsParserTestBase::NewVideoConfig(const VideoDecoderConfig& config) {
config_count_++;
}
void EsParserTestBase::EmitBuffer(scoped_refptr<StreamParserBuffer> buffer) {
buffer_timestamps_stream_ << "("
<< buffer->timestamp().InMilliseconds()
<< ") ";
buffer_count_++;
}
bool EsParserTestBase::ProcessPesPackets(
EsParser* es_parser,
const std::vector<Packet>& pes_packets,
bool force_timing) {
DCHECK(es_parser);
buffer_count_ = 0;
config_count_ = 0;
buffer_timestamps_stream_.str(std::string());
for (size_t k = 0; k < pes_packets.size(); k++) {
size_t cur_pes_offset = pes_packets[k].offset;
size_t cur_pes_size = pes_packets[k].size;
base::TimeDelta pts = kNoTimestamp();
DecodeTimestamp dts = kNoDecodeTimestamp();
if (pes_packets[k].pts >= base::TimeDelta() || force_timing)
pts = pes_packets[k].pts;
DCHECK_LT(cur_pes_offset, stream_.size());
if (!es_parser->Parse(&stream_[cur_pes_offset], cur_pes_size, pts, dts))
return false;
}
es_parser->Flush();
buffer_timestamps_ = buffer_timestamps_stream_.str();
base::TrimWhitespaceASCII(
buffer_timestamps_, base::TRIM_ALL, &buffer_timestamps_);
return true;
}
void EsParserTestBase::ComputePacketSize(std::vector<Packet>* packets) {
DCHECK(packets);
if (packets->size() == 0u)
return;
Packet* cur = &(*packets)[0];
for (size_t k = 0; k < packets->size() - 1; k++) {
Packet* next = &(*packets)[k + 1];
DCHECK_GE(next->offset, cur->offset);
cur->size = next->offset - cur->offset;
cur = next;
}
DCHECK_GE(stream_.size(), cur->offset);
cur->size = stream_.size() - cur->offset;
}
} // namespace mp2t
} // namespace media
// 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.
#ifndef MEDIA_FORMATS_MP2T_ES_PARSER_TEST_BASE_H_
#define MEDIA_FORMATS_MP2T_ES_PARSER_TEST_BASE_H_
#include <sstream>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/time/time.h"
namespace media {
class AudioDecoderConfig;
class StreamParserBuffer;
class VideoDecoderConfig;
namespace mp2t {
class EsParser;
class EsParserTestBase {
public:
struct Packet {
Packet();
// Offset in the stream.
size_t offset;
// Size of the packet.
size_t size;
// Timestamp of the packet.
base::TimeDelta pts;
};
EsParserTestBase();
virtual ~EsParserTestBase();
protected:
void LoadStream(const char* filename);
// ES parser callbacks.
void NewAudioConfig(const AudioDecoderConfig& config);
void NewVideoConfig(const VideoDecoderConfig& config);
void EmitBuffer(scoped_refptr<StreamParserBuffer> buffer);
// Process the PES packets using the given ES parser.
// When |force_timing| is true, even the invalid negative timestamps will be
// given to the ES parser.
// Return true if successful, false otherwise.
bool ProcessPesPackets(EsParser* es_parser,
const std::vector<Packet>& pes_packets,
bool force_timing);
// Assume the offsets are known, compute the size of each packet.
// The last packet is assumed to cover the end of the stream.
// Packets are assumed to be in stream order.
void ComputePacketSize(std::vector<Packet>* packets);
// ES stream.
std::vector<uint8> stream_;
// Number of decoder configs received from the ES parser.
size_t config_count_;
// Number of buffers generated while parsing the ES stream.
size_t buffer_count_;
// Timestamps of buffers generated while parsing the ES stream.
std::string buffer_timestamps_;
private:
// Timestamps of buffers generated while parsing the ES stream.
std::stringstream buffer_timestamps_stream_;
DISALLOW_COPY_AND_ASSIGN(EsParserTestBase);
};
} // namespace mp2t
} // namespace media
#endif // MEDIA_FORMATS_MP2T_ES_PARSER_TEST_BASE_H_
...@@ -1253,7 +1253,10 @@ ...@@ -1253,7 +1253,10 @@
'formats/common/stream_parser_test_base.cc', 'formats/common/stream_parser_test_base.cc',
'formats/common/stream_parser_test_base.h', 'formats/common/stream_parser_test_base.h',
'formats/mp2t/es_adapter_video_unittest.cc', 'formats/mp2t/es_adapter_video_unittest.cc',
'formats/mp2t/es_parser_adts_unittest.cc',
'formats/mp2t/es_parser_h264_unittest.cc', 'formats/mp2t/es_parser_h264_unittest.cc',
'formats/mp2t/es_parser_test_base.cc',
'formats/mp2t/es_parser_test_base.h',
'formats/mp2t/mp2t_stream_parser_unittest.cc', 'formats/mp2t/mp2t_stream_parser_unittest.cc',
'formats/mp4/aac_unittest.cc', 'formats/mp4/aac_unittest.cc',
'formats/mp4/avc_unittest.cc', 'formats/mp4/avc_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment