2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
#include "base/file_util.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2014-02-28 02:39:52 +00:00
|
|
|
#include "base/strings/stringprintf.h"
|
|
|
|
#include "base/time/clock.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
#include "media/base/demuxer.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
#include "media/base/fixed_encryptor_source.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
#include "media/base/media_stream.h"
|
|
|
|
#include "media/base/muxer.h"
|
|
|
|
#include "media/base/status_test_util.h"
|
|
|
|
#include "media/base/stream_info.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
#include "media/mp4/mp4_muxer.h"
|
2014-01-09 23:50:18 +00:00
|
|
|
#include "media/test/test_data_util.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
using ::testing::ValuesIn;
|
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
namespace media {
|
2013-10-11 21:44:55 +00:00
|
|
|
namespace {
|
2014-02-28 02:39:52 +00:00
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
const char* kMediaFiles[] = {"bear-1280x720.mp4", "bear-1280x720-av_frag.mp4"};
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
// Muxer options.
|
|
|
|
const double kSegmentDurationInSeconds = 1.0;
|
|
|
|
const double kFragmentDurationInSecodns = 0.1;
|
|
|
|
const bool kSegmentSapAligned = true;
|
|
|
|
const bool kFragmentSapAligned = true;
|
|
|
|
const int kNumSubsegmentsPerSidx = 2;
|
2014-02-28 02:39:52 +00:00
|
|
|
|
|
|
|
const char kOutputVideo[] = "output_video";
|
|
|
|
const char kOutputVideo2[] = "output_video_2";
|
|
|
|
const char kOutputAudio[] = "output_audio";
|
|
|
|
const char kOutputAudio2[] = "output_audio_2";
|
|
|
|
const char kOutputNone[] = "";
|
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
const char kSegmentTemplate[] = "template$Number$.m4s";
|
2014-02-28 02:39:52 +00:00
|
|
|
const char kSegmentTemplateOutputPattern[] = "template%d.m4s";
|
|
|
|
|
|
|
|
const bool kSingleSegment = true;
|
|
|
|
const bool kMultipleSegments = false;
|
|
|
|
const bool kEnableEncryption = true;
|
|
|
|
const bool kDisableEncryption = false;
|
2014-01-14 01:38:34 +00:00
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
// Encryption constants.
|
|
|
|
const char kKeyIdHex[] = "e5007e6e9dcd5ac095202ed3758382cd";
|
|
|
|
const char kKeyHex[] = "6fc96fe628a265b13aeddec0bc421f4d";
|
|
|
|
const char kPsshHex[] =
|
|
|
|
"08011210e5007e6e9dcd5ac095202ed3"
|
|
|
|
"758382cd1a0d7769646576696e655f746573742211544553545f"
|
|
|
|
"434f4e54454e545f49445f312a025344";
|
2014-01-14 01:38:34 +00:00
|
|
|
const double kClearLeadInSeconds = 1.5;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
MediaStream* FindFirstStreamOfType(const std::vector<MediaStream*>& streams,
|
|
|
|
StreamType stream_type) {
|
|
|
|
typedef std::vector<MediaStream*>::const_iterator StreamIterator;
|
|
|
|
for (StreamIterator it = streams.begin(); it != streams.end(); ++it) {
|
|
|
|
if ((*it)->info()->stream_type() == stream_type)
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
MediaStream* FindFirstVideoStream(const std::vector<MediaStream*>& streams) {
|
|
|
|
return FindFirstStreamOfType(streams, kStreamVideo);
|
|
|
|
}
|
|
|
|
MediaStream* FindFirstAudioStream(const std::vector<MediaStream*>& streams) {
|
|
|
|
return FindFirstStreamOfType(streams, kStreamAudio);
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
} // namespace
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
class FakeClock : public base::Clock {
|
|
|
|
public:
|
|
|
|
// Fake the clock to return NULL time.
|
|
|
|
virtual base::Time Now() OVERRIDE { return base::Time(); }
|
|
|
|
};
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
class PackagerTestBasic : public ::testing::TestWithParam<const char*> {
|
2013-10-11 21:44:55 +00:00
|
|
|
public:
|
2014-02-28 02:39:52 +00:00
|
|
|
PackagerTestBasic() : decryptor_source_(NULL) {}
|
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
virtual void SetUp() OVERRIDE {
|
|
|
|
// Create a test directory for testing, will be deleted after test.
|
2014-02-26 23:55:01 +00:00
|
|
|
ASSERT_TRUE(base::CreateNewTempDirectory("packager_", &test_directory_));
|
2014-01-14 01:38:34 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
// Copy the input to test directory for easy reference.
|
|
|
|
ASSERT_TRUE(base::CopyFile(GetTestDataFilePath(GetParam()),
|
|
|
|
test_directory_.AppendASCII(GetParam())));
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
virtual void TearDown() OVERRIDE { base::DeleteFile(test_directory_, true); }
|
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
std::string GetFullPath(const std::string& file_name);
|
|
|
|
// Check if |file1| and |file2| are the same.
|
|
|
|
bool ContentsEqual(const std::string& file1, const std::string file2);
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
MuxerOptions SetupOptions(const std::string& output, bool single_segment);
|
|
|
|
void Remux(const std::string& input,
|
|
|
|
const std::string& video_output,
|
|
|
|
const std::string& audio_output,
|
|
|
|
bool single_segment,
|
|
|
|
bool enable_encryption);
|
2014-01-18 00:03:21 +00:00
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
protected:
|
|
|
|
base::FilePath test_directory_;
|
2014-02-28 02:39:52 +00:00
|
|
|
DecryptorSource* decryptor_source_;
|
|
|
|
FakeClock fake_clock_;
|
2013-10-11 21:44:55 +00:00
|
|
|
};
|
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
std::string PackagerTestBasic::GetFullPath(const std::string& file_name) {
|
|
|
|
return test_directory_.AppendASCII(file_name).value();
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
bool PackagerTestBasic::ContentsEqual(const std::string& file1,
|
|
|
|
const std::string file2) {
|
|
|
|
return base::ContentsEqual(test_directory_.AppendASCII(file1),
|
|
|
|
test_directory_.AppendASCII(file2));
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
MuxerOptions PackagerTestBasic::SetupOptions(const std::string& output,
|
|
|
|
bool single_segment) {
|
|
|
|
MuxerOptions options;
|
|
|
|
options.single_segment = single_segment;
|
|
|
|
|
|
|
|
options.segment_duration = kSegmentDurationInSeconds;
|
|
|
|
options.fragment_duration = kFragmentDurationInSecodns;
|
|
|
|
options.segment_sap_aligned = kSegmentSapAligned;
|
|
|
|
options.fragment_sap_aligned = kFragmentSapAligned;
|
|
|
|
options.num_subsegments_per_sidx = kNumSubsegmentsPerSidx;
|
|
|
|
|
|
|
|
options.output_file_name = GetFullPath(output);
|
|
|
|
options.segment_template = GetFullPath(kSegmentTemplate);
|
|
|
|
options.temp_file_name = GetFullPath(output + ".temp");
|
|
|
|
return options;
|
|
|
|
}
|
2014-01-18 00:03:21 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
void PackagerTestBasic::Remux(const std::string& input,
|
|
|
|
const std::string& video_output,
|
|
|
|
const std::string& audio_output,
|
|
|
|
bool single_segment,
|
|
|
|
bool enable_encryption) {
|
|
|
|
CHECK(!video_output.empty() || !audio_output.empty());
|
2014-01-18 00:03:21 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
Demuxer demuxer(GetFullPath(input), decryptor_source_);
|
2014-01-18 00:03:21 +00:00
|
|
|
ASSERT_OK(demuxer.Initialize());
|
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
FixedEncryptorSource encryptor_source(kKeyIdHex, kKeyHex, kPsshHex);
|
|
|
|
ASSERT_OK(encryptor_source.Initialize());
|
|
|
|
|
|
|
|
scoped_ptr<Muxer> muxer_video;
|
|
|
|
if (!video_output.empty()) {
|
|
|
|
muxer_video.reset(
|
|
|
|
new mp4::MP4Muxer(SetupOptions(video_output, single_segment)));
|
|
|
|
muxer_video->set_clock(&fake_clock_);
|
|
|
|
|
|
|
|
ASSERT_OK(muxer_video->AddStream(FindFirstVideoStream(demuxer.streams())));
|
|
|
|
|
|
|
|
if (enable_encryption)
|
|
|
|
muxer_video->SetEncryptorSource(&encryptor_source, kClearLeadInSeconds);
|
|
|
|
|
|
|
|
ASSERT_OK(muxer_video->Initialize());
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_ptr<Muxer> muxer_audio;
|
|
|
|
if (!audio_output.empty()) {
|
|
|
|
muxer_audio.reset(
|
|
|
|
new mp4::MP4Muxer(SetupOptions(audio_output, single_segment)));
|
|
|
|
muxer_audio->set_clock(&fake_clock_);
|
2014-01-18 00:03:21 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
ASSERT_OK(muxer_audio->AddStream(FindFirstAudioStream(demuxer.streams())));
|
2014-01-18 00:03:21 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
if (enable_encryption)
|
|
|
|
muxer_video->SetEncryptorSource(&encryptor_source, kClearLeadInSeconds);
|
|
|
|
|
|
|
|
ASSERT_OK(muxer_audio->Initialize());
|
|
|
|
}
|
2014-01-18 00:03:21 +00:00
|
|
|
|
|
|
|
// Start remuxing process.
|
|
|
|
ASSERT_OK(demuxer.Run());
|
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
if (muxer_video)
|
|
|
|
ASSERT_OK(muxer_video->Finalize());
|
|
|
|
if (muxer_audio)
|
|
|
|
ASSERT_OK(muxer_audio->Finalize());
|
2014-01-18 00:03:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
TEST_P(PackagerTestBasic, MP4MuxerSingleSegmentUnencrypted) {
|
|
|
|
ASSERT_NO_FATAL_FAILURE(Remux(GetParam(),
|
|
|
|
kOutputVideo,
|
|
|
|
kOutputNone,
|
|
|
|
kSingleSegment,
|
|
|
|
kDisableEncryption));
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
TEST_P(PackagerTestBasic, MP4MuxerSingleSegmentEncrypted) {
|
|
|
|
ASSERT_NO_FATAL_FAILURE(Remux(GetParam(),
|
|
|
|
kOutputVideo,
|
|
|
|
kOutputNone,
|
|
|
|
kSingleSegment,
|
|
|
|
kEnableEncryption));
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
// Expect the output to be encrypted.
|
2014-02-28 02:39:52 +00:00
|
|
|
Demuxer demuxer(GetFullPath(kOutputVideo), decryptor_source_);
|
2014-01-14 01:38:34 +00:00
|
|
|
ASSERT_OK(demuxer.Initialize());
|
|
|
|
ASSERT_EQ(1, demuxer.streams().size());
|
|
|
|
EXPECT_TRUE(demuxer.streams()[0]->info()->is_encrypted());
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
class PackagerTest : public PackagerTestBasic {
|
|
|
|
public:
|
|
|
|
virtual void SetUp() OVERRIDE {
|
|
|
|
PackagerTestBasic::SetUp();
|
|
|
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(Remux(GetParam(),
|
|
|
|
kOutputVideo,
|
|
|
|
kOutputNone,
|
|
|
|
kSingleSegment,
|
|
|
|
kDisableEncryption));
|
|
|
|
|
|
|
|
ASSERT_NO_FATAL_FAILURE(Remux(GetParam(),
|
|
|
|
kOutputNone,
|
|
|
|
kOutputAudio,
|
|
|
|
kSingleSegment,
|
|
|
|
kDisableEncryption));
|
|
|
|
}
|
|
|
|
};
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
TEST_P(PackagerTest, MP4MuxerSingleSegmentUnencryptedAgain) {
|
|
|
|
// Take the muxer output and feed into muxer again. The new muxer output
|
|
|
|
// should contain the same contents as the previous muxer output.
|
|
|
|
ASSERT_NO_FATAL_FAILURE(Remux(kOutputVideo,
|
|
|
|
kOutputVideo2,
|
|
|
|
kOutputNone,
|
|
|
|
kSingleSegment,
|
|
|
|
kDisableEncryption));
|
|
|
|
EXPECT_TRUE(ContentsEqual(kOutputVideo, kOutputVideo2));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(PackagerTest, MP4MuxerSingleSegmentUnencryptedSeparateAudioVideo) {
|
|
|
|
ASSERT_NO_FATAL_FAILURE(Remux(GetParam(),
|
|
|
|
kOutputVideo2,
|
|
|
|
kOutputAudio2,
|
|
|
|
kSingleSegment,
|
|
|
|
kDisableEncryption));
|
|
|
|
|
|
|
|
// Compare the output with single muxer output. They should match.
|
|
|
|
EXPECT_TRUE(ContentsEqual(kOutputVideo, kOutputVideo2));
|
|
|
|
EXPECT_TRUE(ContentsEqual(kOutputAudio, kOutputAudio2));
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
TEST_P(PackagerTest, MP4MuxerMultipleSegmentsUnencrypted) {
|
|
|
|
ASSERT_NO_FATAL_FAILURE(Remux(GetParam(),
|
|
|
|
kOutputVideo2,
|
|
|
|
kOutputNone,
|
|
|
|
kMultipleSegments,
|
|
|
|
kDisableEncryption));
|
|
|
|
|
|
|
|
// Find and concatenates the segments.
|
|
|
|
const std::string kOutputVideoSegmentsCombined =
|
|
|
|
std::string(kOutputVideo) + "_combined";
|
|
|
|
base::FilePath output_path =
|
|
|
|
test_directory_.AppendASCII(kOutputVideoSegmentsCombined);
|
|
|
|
ASSERT_TRUE(
|
|
|
|
base::CopyFile(test_directory_.AppendASCII(kOutputVideo2), output_path));
|
|
|
|
|
|
|
|
const int kStartSegmentIndex = 1; // start from one.
|
|
|
|
int segment_index = kStartSegmentIndex;
|
|
|
|
while (true) {
|
|
|
|
base::FilePath segment_path = test_directory_.AppendASCII(
|
|
|
|
base::StringPrintf(kSegmentTemplateOutputPattern, segment_index));
|
|
|
|
if (!base::PathExists(segment_path))
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::string segment_content;
|
|
|
|
ASSERT_TRUE(file_util::ReadFileToString(segment_path, &segment_content));
|
|
|
|
int bytes_written = file_util::AppendToFile(
|
|
|
|
output_path, segment_content.data(), segment_content.size());
|
|
|
|
ASSERT_EQ(segment_content.size(), bytes_written);
|
|
|
|
|
|
|
|
++segment_index;
|
|
|
|
}
|
|
|
|
// We should have at least one segment.
|
|
|
|
ASSERT_LT(kStartSegmentIndex, segment_index);
|
|
|
|
|
|
|
|
// Feed the combined file into muxer again. The new muxer output should be
|
|
|
|
// the same as by just feeding the input to muxer.
|
|
|
|
ASSERT_NO_FATAL_FAILURE(Remux(kOutputVideoSegmentsCombined,
|
|
|
|
kOutputVideo2,
|
|
|
|
kOutputNone,
|
|
|
|
kSingleSegment,
|
|
|
|
kDisableEncryption));
|
|
|
|
EXPECT_TRUE(ContentsEqual(kOutputVideo, kOutputVideo2));
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(PackagerE2ETest,
|
|
|
|
PackagerTestBasic,
|
|
|
|
ValuesIn(kMediaFiles));
|
2014-01-14 01:38:34 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(PackagerE2ETest, PackagerTest, ValuesIn(kMediaFiles));
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
} // namespace media
|