2014-05-09 01:23:54 +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
|
|
|
|
|
2015-09-25 22:48:18 +00:00
|
|
|
#include <gflags/gflags.h>
|
2014-05-09 01:23:54 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/app/fixed_key_encryption_flags.h"
|
2016-04-16 22:58:47 +00:00
|
|
|
#include "packager/app/hls_flags.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/app/libcrypto_threading.h"
|
|
|
|
#include "packager/app/mpd_flags.h"
|
|
|
|
#include "packager/app/muxer_flags.h"
|
|
|
|
#include "packager/app/packager_util.h"
|
2017-01-05 17:32:17 +00:00
|
|
|
#include "packager/app/playready_key_encryption_flags.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/app/stream_descriptor.h"
|
2015-10-27 18:52:50 +00:00
|
|
|
#include "packager/app/vlog_flags.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/app/widevine_encryption_flags.h"
|
2015-12-30 01:09:12 +00:00
|
|
|
#include "packager/base/at_exit.h"
|
2015-10-27 18:52:50 +00:00
|
|
|
#include "packager/base/command_line.h"
|
2016-04-16 22:58:47 +00:00
|
|
|
#include "packager/base/files/file_path.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
2016-08-14 22:28:21 +00:00
|
|
|
#include "packager/base/path_service.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/strings/string_split.h"
|
|
|
|
#include "packager/base/strings/stringprintf.h"
|
|
|
|
#include "packager/base/threading/simple_thread.h"
|
2015-09-25 22:48:18 +00:00
|
|
|
#include "packager/base/time/clock.h"
|
2016-04-16 22:58:47 +00:00
|
|
|
#include "packager/hls/base/hls_notifier.h"
|
|
|
|
#include "packager/hls/base/simple_hls_notifier.h"
|
2015-11-05 01:49:35 +00:00
|
|
|
#include "packager/media/base/container_names.h"
|
2016-04-08 18:41:17 +00:00
|
|
|
#include "packager/media/base/fourccs.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/key_source.h"
|
|
|
|
#include "packager/media/base/muxer_options.h"
|
|
|
|
#include "packager/media/base/muxer_util.h"
|
2017-02-24 01:17:47 +00:00
|
|
|
#include "packager/media/chunking/chunking_handler.h"
|
2017-03-11 02:49:55 +00:00
|
|
|
#include "packager/media/crypto/encryption_handler.h"
|
2017-03-28 19:31:32 +00:00
|
|
|
#include "packager/media/demuxer/demuxer.h"
|
2016-04-16 22:58:47 +00:00
|
|
|
#include "packager/media/event/hls_notify_muxer_listener.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/event/mpd_notify_muxer_listener.h"
|
|
|
|
#include "packager/media/event/vod_media_info_dump_muxer_listener.h"
|
2015-11-05 01:49:35 +00:00
|
|
|
#include "packager/media/file/file.h"
|
2016-04-06 08:26:48 +00:00
|
|
|
#include "packager/media/formats/mp2t/ts_muxer.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/mp4_muxer.h"
|
2015-10-28 17:23:08 +00:00
|
|
|
#include "packager/media/formats/webm/webm_muxer.h"
|
2017-03-21 23:14:46 +00:00
|
|
|
#include "packager/media/trick_play/trick_play_handler.h"
|
2015-08-04 22:26:15 +00:00
|
|
|
#include "packager/mpd/base/dash_iop_mpd_notifier.h"
|
2015-11-05 01:49:35 +00:00
|
|
|
#include "packager/mpd/base/media_info.pb.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/mpd/base/mpd_builder.h"
|
|
|
|
#include "packager/mpd/base/simple_mpd_notifier.h"
|
2015-12-21 23:10:17 +00:00
|
|
|
#include "packager/version/version.h"
|
2014-05-09 01:23:54 +00:00
|
|
|
|
2016-08-14 22:28:21 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <codecvt>
|
|
|
|
#include <functional>
|
|
|
|
#include <locale>
|
|
|
|
#endif // defined(OS_WIN)
|
|
|
|
|
2015-09-25 22:48:18 +00:00
|
|
|
DEFINE_bool(use_fake_clock_for_muxer,
|
|
|
|
false,
|
|
|
|
"Set to true to use a fake clock for muxer. With this flag set, "
|
|
|
|
"creation time and modification time in outputs are set to 0. "
|
|
|
|
"Should only be used for testing.");
|
2016-07-07 19:34:07 +00:00
|
|
|
DEFINE_bool(override_version,
|
|
|
|
false,
|
|
|
|
"Override packager version in the generated outputs with "
|
|
|
|
"--test_version if it is set to true. Should be used for "
|
|
|
|
"testing only.");
|
|
|
|
DEFINE_string(test_version,
|
|
|
|
"",
|
|
|
|
"Packager version for testing. Ignored if --override_version is "
|
|
|
|
"false. Should be used for testing only.");
|
2015-09-25 22:48:18 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-05-19 21:38:15 +00:00
|
|
|
namespace media {
|
2014-05-09 01:23:54 +00:00
|
|
|
namespace {
|
2016-05-19 21:38:15 +00:00
|
|
|
|
2014-05-09 01:23:54 +00:00
|
|
|
const char kUsage[] =
|
2016-12-08 20:07:09 +00:00
|
|
|
"%s [flags] <stream_descriptor> ...\n\n"
|
|
|
|
" stream_descriptor consists of comma separated field_name/value pairs:\n"
|
|
|
|
" field_name=value,[field_name=value,]...\n"
|
2017-04-28 23:29:02 +00:00
|
|
|
" Supported field names are as follows (names in parenthesis are alias):\n"
|
2015-12-21 23:10:17 +00:00
|
|
|
" - input (in): Required input/source media file path or network stream\n"
|
|
|
|
" URL.\n"
|
|
|
|
" - stream_selector (stream): Required field with value 'audio',\n"
|
2015-11-23 23:12:04 +00:00
|
|
|
" 'video', 'text', or stream number (zero based).\n"
|
2017-04-28 23:29:02 +00:00
|
|
|
" - output (out,init_segment): Required output file (single file) or\n"
|
|
|
|
" initialization file path (multiple file).\n"
|
2015-12-21 23:10:17 +00:00
|
|
|
" - segment_template (segment): Optional value which specifies the\n"
|
|
|
|
" naming pattern for the segment files, and that the stream should be\n"
|
|
|
|
" split into multiple files. Its presence should be consistent across\n"
|
|
|
|
" streams.\n"
|
|
|
|
" - bandwidth (bw): Optional value which contains a user-specified\n"
|
|
|
|
" content bit rate for the stream, in bits/sec. If specified, this\n"
|
|
|
|
" value is propagated to the $Bandwidth$ template parameter for\n"
|
|
|
|
" segment names. If not specified, its value may be estimated.\n"
|
|
|
|
" - language (lang): Optional value which contains a user-specified\n"
|
|
|
|
" language tag. If specified, this value overrides any language\n"
|
|
|
|
" metadata in the input track.\n"
|
|
|
|
" - output_format (format): Optional value which specifies the format\n"
|
|
|
|
" of the output files (MP4 or WebM). If not specified, it will be\n"
|
2016-04-16 22:58:47 +00:00
|
|
|
" derived from the file extension of the output file.\n"
|
|
|
|
" - hls_name: Required for audio when outputting HLS.\n"
|
|
|
|
" name of the output stream. This is not (necessarily) the same as\n"
|
|
|
|
" output. This is used as the NAME attribute for EXT-X-MEDIA\n"
|
|
|
|
" - hls_group_id: Required for audio when outputting HLS.\n"
|
|
|
|
" The group ID for the output stream. For HLS this is used as the\n"
|
|
|
|
" GROUP-ID attribute for EXT-X-MEDIA.\n"
|
|
|
|
" - playlist_name: Required for HLS output.\n"
|
|
|
|
" Name of the playlist for the stream. Usually ends with '.m3u8'.\n";
|
2014-10-13 22:06:48 +00:00
|
|
|
|
2015-11-05 01:49:35 +00:00
|
|
|
const char kMediaInfoSuffix[] = ".media_info";
|
|
|
|
|
2014-10-13 22:06:48 +00:00
|
|
|
enum ExitStatus {
|
|
|
|
kSuccess = 0,
|
|
|
|
kArgumentValidationFailed,
|
|
|
|
kPackagingFailed,
|
|
|
|
kInternalError,
|
|
|
|
};
|
2015-11-05 01:49:35 +00:00
|
|
|
|
|
|
|
// TODO(rkuroiwa): Write TTML and WebVTT parser (demuxing) for a better check
|
|
|
|
// and for supporting live/segmenting (muxing). With a demuxer and a muxer,
|
|
|
|
// CreateRemuxJobs() shouldn't treat text as a special case.
|
|
|
|
std::string DetermineTextFileFormat(const std::string& file) {
|
|
|
|
std::string content;
|
2016-05-19 21:38:15 +00:00
|
|
|
if (!File::ReadFileToString(file.c_str(), &content)) {
|
2015-11-05 01:49:35 +00:00
|
|
|
LOG(ERROR) << "Failed to open file " << file
|
|
|
|
<< " to determine file format.";
|
|
|
|
return "";
|
|
|
|
}
|
2016-05-19 21:38:15 +00:00
|
|
|
MediaContainerName container_name = DetermineContainer(
|
|
|
|
reinterpret_cast<const uint8_t*>(content.data()), content.size());
|
|
|
|
if (container_name == CONTAINER_WEBVTT) {
|
2015-11-05 01:49:35 +00:00
|
|
|
return "vtt";
|
2016-05-19 21:38:15 +00:00
|
|
|
} else if (container_name == CONTAINER_TTML) {
|
2015-11-05 01:49:35 +00:00
|
|
|
return "ttml";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-05-09 01:23:54 +00:00
|
|
|
} // namespace
|
|
|
|
|
2015-09-25 22:48:18 +00:00
|
|
|
// A fake clock that always return time 0 (epoch). Should only be used for
|
|
|
|
// testing.
|
|
|
|
class FakeClock : public base::Clock {
|
|
|
|
public:
|
2015-07-22 23:40:45 +00:00
|
|
|
base::Time Now() override { return base::Time(); }
|
2015-09-25 22:48:18 +00:00
|
|
|
};
|
|
|
|
|
2014-05-09 01:23:54 +00:00
|
|
|
// Demux, Mux(es) and worker thread used to remux a source file/stream.
|
|
|
|
class RemuxJob : public base::SimpleThread {
|
|
|
|
public:
|
2016-08-17 17:41:40 +00:00
|
|
|
RemuxJob(std::unique_ptr<Demuxer> demuxer)
|
|
|
|
: SimpleThread("RemuxJob"), demuxer_(std::move(demuxer)) {}
|
2014-05-09 01:23:54 +00:00
|
|
|
|
2016-08-30 23:01:19 +00:00
|
|
|
~RemuxJob() override {}
|
2014-05-09 01:23:54 +00:00
|
|
|
|
|
|
|
Demuxer* demuxer() { return demuxer_.get(); }
|
|
|
|
Status status() { return status_; }
|
|
|
|
|
|
|
|
private:
|
2015-07-22 23:40:45 +00:00
|
|
|
void Run() override {
|
2014-05-09 01:23:54 +00:00
|
|
|
DCHECK(demuxer_);
|
|
|
|
status_ = demuxer_->Run();
|
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<Demuxer> demuxer_;
|
2014-05-09 01:23:54 +00:00
|
|
|
Status status_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RemuxJob);
|
|
|
|
};
|
|
|
|
|
2015-11-05 01:49:35 +00:00
|
|
|
bool StreamInfoToTextMediaInfo(const StreamDescriptor& stream_descriptor,
|
|
|
|
const MuxerOptions& stream_muxer_options,
|
|
|
|
MediaInfo* text_media_info) {
|
|
|
|
const std::string& language = stream_descriptor.language;
|
|
|
|
std::string format = DetermineTextFileFormat(stream_descriptor.input);
|
|
|
|
if (format.empty()) {
|
|
|
|
LOG(ERROR) << "Failed to determine the text file format for "
|
|
|
|
<< stream_descriptor.input;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!File::Copy(stream_descriptor.input.c_str(),
|
|
|
|
stream_muxer_options.output_file_name.c_str())) {
|
|
|
|
LOG(ERROR) << "Failed to copy the input file (" << stream_descriptor.input
|
|
|
|
<< ") to output file (" << stream_muxer_options.output_file_name
|
|
|
|
<< ").";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
text_media_info->set_media_file_name(stream_muxer_options.output_file_name);
|
|
|
|
text_media_info->set_container_type(MediaInfo::CONTAINER_TEXT);
|
|
|
|
|
|
|
|
if (stream_muxer_options.bandwidth != 0) {
|
|
|
|
text_media_info->set_bandwidth(stream_muxer_options.bandwidth);
|
|
|
|
} else {
|
|
|
|
// Text files are usually small and since the input is one file; there's no
|
|
|
|
// way for the player to do ranged requests. So set this value to something
|
|
|
|
// reasonable.
|
|
|
|
text_media_info->set_bandwidth(256);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaInfo::TextInfo* text_info = text_media_info->mutable_text_info();
|
|
|
|
text_info->set_format(format);
|
|
|
|
if (!language.empty())
|
|
|
|
text_info->set_language(language);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
std::shared_ptr<Muxer> CreateOutputMuxer(const MuxerOptions& options,
|
2016-08-17 17:41:40 +00:00
|
|
|
MediaContainerName container) {
|
2015-12-16 18:20:13 +00:00
|
|
|
if (container == CONTAINER_WEBM) {
|
2017-02-21 18:36:50 +00:00
|
|
|
return std::shared_ptr<Muxer>(new webm::WebMMuxer(options));
|
2016-04-06 08:26:48 +00:00
|
|
|
} else if (container == CONTAINER_MPEG2TS) {
|
2017-02-21 18:36:50 +00:00
|
|
|
return std::shared_ptr<Muxer>(new mp2t::TsMuxer(options));
|
2015-10-28 17:23:08 +00:00
|
|
|
} else {
|
2015-12-16 18:20:13 +00:00
|
|
|
DCHECK_EQ(container, CONTAINER_MOV);
|
2017-02-21 18:36:50 +00:00
|
|
|
return std::shared_ptr<Muxer>(new mp4::MP4Muxer(options));
|
2015-10-28 17:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:07:36 +00:00
|
|
|
bool CreateRemuxJobs(const StreamDescriptorList& stream_descriptors,
|
2017-02-24 01:17:47 +00:00
|
|
|
const ChunkingOptions& chunking_options,
|
2017-03-11 02:49:55 +00:00
|
|
|
const EncryptionOptions& encryption_options,
|
2014-05-09 01:23:54 +00:00
|
|
|
const MuxerOptions& muxer_options,
|
2015-09-25 22:48:18 +00:00
|
|
|
FakeClock* fake_clock,
|
2017-03-11 02:49:55 +00:00
|
|
|
KeySource* encryption_key_source,
|
2014-05-22 19:39:30 +00:00
|
|
|
MpdNotifier* mpd_notifier,
|
2016-04-16 22:58:47 +00:00
|
|
|
hls::HlsNotifier* hls_notifier,
|
2016-08-30 23:01:19 +00:00
|
|
|
std::vector<std::unique_ptr<RemuxJob>>* remux_jobs) {
|
2016-04-16 22:58:47 +00:00
|
|
|
// No notifiers OR (mpd_notifier XOR hls_notifier); which is NAND.
|
|
|
|
DCHECK(!(mpd_notifier && hls_notifier));
|
2014-05-09 01:23:54 +00:00
|
|
|
DCHECK(remux_jobs);
|
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
std::shared_ptr<TrickPlayHandler> trick_play_handler;
|
|
|
|
|
2014-06-27 23:07:36 +00:00
|
|
|
std::string previous_input;
|
2017-03-21 23:14:46 +00:00
|
|
|
std::string previous_stream_selector;
|
2016-06-30 22:25:52 +00:00
|
|
|
int stream_number = 0;
|
|
|
|
for (StreamDescriptorList::const_iterator
|
|
|
|
stream_iter = stream_descriptors.begin();
|
2014-06-27 23:07:36 +00:00
|
|
|
stream_iter != stream_descriptors.end();
|
2016-06-30 22:25:52 +00:00
|
|
|
++stream_iter, ++stream_number) {
|
2014-05-09 01:23:54 +00:00
|
|
|
// Process stream descriptor.
|
|
|
|
MuxerOptions stream_muxer_options(muxer_options);
|
2014-06-27 23:07:36 +00:00
|
|
|
stream_muxer_options.output_file_name = stream_iter->output;
|
|
|
|
if (!stream_iter->segment_template.empty()) {
|
|
|
|
if (!ValidateSegmentTemplate(stream_iter->segment_template)) {
|
2014-05-14 23:19:35 +00:00
|
|
|
LOG(ERROR) << "ERROR: segment template with '"
|
2014-06-27 23:07:36 +00:00
|
|
|
<< stream_iter->segment_template << "' is invalid.";
|
2014-05-14 23:19:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-06-27 23:07:36 +00:00
|
|
|
stream_muxer_options.segment_template = stream_iter->segment_template;
|
2014-05-14 23:19:35 +00:00
|
|
|
}
|
2014-06-27 23:07:36 +00:00
|
|
|
stream_muxer_options.bandwidth = stream_iter->bandwidth;
|
2014-05-09 01:23:54 +00:00
|
|
|
|
2015-11-23 23:12:04 +00:00
|
|
|
if (stream_iter->stream_selector == "text" &&
|
|
|
|
stream_iter->output_format != CONTAINER_MOV) {
|
2015-11-05 01:49:35 +00:00
|
|
|
MediaInfo text_media_info;
|
|
|
|
if (!StreamInfoToTextMediaInfo(*stream_iter, stream_muxer_options,
|
|
|
|
&text_media_info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mpd_notifier) {
|
2016-08-17 21:42:23 +00:00
|
|
|
uint32_t unused;
|
2015-11-05 01:49:35 +00:00
|
|
|
if (!mpd_notifier->NotifyNewContainer(text_media_info, &unused)) {
|
|
|
|
LOG(ERROR) << "Failed to process text file " << stream_iter->input;
|
|
|
|
} else {
|
|
|
|
mpd_notifier->Flush();
|
|
|
|
}
|
|
|
|
} else if (FLAGS_output_media_info) {
|
|
|
|
VodMediaInfoDumpMuxerListener::WriteMediaInfoToFile(
|
|
|
|
text_media_info,
|
|
|
|
stream_muxer_options.output_file_name + kMediaInfoSuffix);
|
|
|
|
} else {
|
|
|
|
NOTIMPLEMENTED()
|
|
|
|
<< "--mpd_output or --output_media_info flags are "
|
|
|
|
"required for text output. Skipping manifest related output for "
|
|
|
|
<< stream_iter->input;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:07:36 +00:00
|
|
|
if (stream_iter->input != previous_input) {
|
2014-05-09 01:23:54 +00:00
|
|
|
// New remux job needed. Create demux and job thread.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<Demuxer> demuxer(new Demuxer(stream_iter->input));
|
2017-02-21 18:36:50 +00:00
|
|
|
demuxer->set_dump_stream_info(FLAGS_dump_stream_info);
|
2014-10-15 00:47:25 +00:00
|
|
|
if (FLAGS_enable_widevine_decryption ||
|
|
|
|
FLAGS_enable_fixed_key_decryption) {
|
2017-03-11 02:49:55 +00:00
|
|
|
std::unique_ptr<KeySource> decryption_key_source(
|
|
|
|
CreateDecryptionKeySource());
|
|
|
|
if (!decryption_key_source)
|
2014-10-15 00:47:25 +00:00
|
|
|
return false;
|
2017-03-11 02:49:55 +00:00
|
|
|
demuxer->SetKeySource(std::move(decryption_key_source));
|
2014-10-15 00:47:25 +00:00
|
|
|
}
|
2016-08-30 23:01:19 +00:00
|
|
|
remux_jobs->emplace_back(new RemuxJob(std::move(demuxer)));
|
2017-03-21 23:14:46 +00:00
|
|
|
trick_play_handler.reset();
|
2014-06-27 23:07:36 +00:00
|
|
|
previous_input = stream_iter->input;
|
2017-02-21 18:36:50 +00:00
|
|
|
// Skip setting up muxers if output is not needed.
|
2017-03-14 00:02:23 +00:00
|
|
|
if (stream_iter->output.empty() && stream_iter->segment_template.empty())
|
2017-02-21 18:36:50 +00:00
|
|
|
continue;
|
2014-05-09 01:23:54 +00:00
|
|
|
}
|
|
|
|
DCHECK(!remux_jobs->empty());
|
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
// Each stream selector requires an individual trick play handler.
|
|
|
|
// E.g., an input with two video streams needs two trick play handlers.
|
|
|
|
// TODO(hmchen): add a test case in packager_test.py for two video streams
|
|
|
|
// input.
|
|
|
|
if (stream_iter->stream_selector != previous_stream_selector) {
|
|
|
|
previous_stream_selector = stream_iter->stream_selector;
|
|
|
|
trick_play_handler.reset();
|
|
|
|
}
|
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
std::shared_ptr<Muxer> muxer(
|
2016-06-30 22:25:52 +00:00
|
|
|
CreateOutputMuxer(stream_muxer_options, stream_iter->output_format));
|
2015-09-25 22:48:18 +00:00
|
|
|
if (FLAGS_use_fake_clock_for_muxer) muxer->set_clock(fake_clock);
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MuxerListener> muxer_listener;
|
2014-06-11 00:20:00 +00:00
|
|
|
DCHECK(!(FLAGS_output_media_info && mpd_notifier));
|
|
|
|
if (FLAGS_output_media_info) {
|
2015-02-02 19:27:32 +00:00
|
|
|
const std::string output_media_info_file_name =
|
2015-11-05 01:49:35 +00:00
|
|
|
stream_muxer_options.output_file_name + kMediaInfoSuffix;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<VodMediaInfoDumpMuxerListener>
|
2014-06-11 00:20:00 +00:00
|
|
|
vod_media_info_dump_muxer_listener(
|
2015-02-02 19:27:32 +00:00
|
|
|
new VodMediaInfoDumpMuxerListener(output_media_info_file_name));
|
2016-08-17 17:41:40 +00:00
|
|
|
muxer_listener = std::move(vod_media_info_dump_muxer_listener);
|
2014-06-11 00:20:00 +00:00
|
|
|
}
|
2014-05-22 19:39:30 +00:00
|
|
|
if (mpd_notifier) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MpdNotifyMuxerListener> mpd_notify_muxer_listener(
|
2014-05-22 19:39:30 +00:00
|
|
|
new MpdNotifyMuxerListener(mpd_notifier));
|
2016-08-17 17:41:40 +00:00
|
|
|
muxer_listener = std::move(mpd_notify_muxer_listener);
|
2014-06-11 00:20:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 22:58:47 +00:00
|
|
|
if (hls_notifier) {
|
|
|
|
// TODO(rkuroiwa): Do some smart stuff to group the audios, e.g. detect
|
2016-06-30 22:25:52 +00:00
|
|
|
// languages.
|
2016-04-16 22:58:47 +00:00
|
|
|
std::string group_id = stream_iter->hls_group_id;
|
|
|
|
std::string name = stream_iter->hls_name;
|
2016-06-30 22:25:52 +00:00
|
|
|
std::string hls_playlist_name = stream_iter->hls_playlist_name;
|
2016-04-16 22:58:47 +00:00
|
|
|
if (group_id.empty())
|
|
|
|
group_id = "audio";
|
|
|
|
if (name.empty())
|
2016-06-30 22:25:52 +00:00
|
|
|
name = base::StringPrintf("stream_%d", stream_number);
|
|
|
|
if (hls_playlist_name.empty())
|
|
|
|
hls_playlist_name = base::StringPrintf("stream_%d.m3u8", stream_number);
|
2016-04-16 22:58:47 +00:00
|
|
|
|
2016-06-30 22:25:52 +00:00
|
|
|
muxer_listener.reset(new HlsNotifyMuxerListener(hls_playlist_name, name,
|
|
|
|
group_id, hls_notifier));
|
2016-04-16 22:58:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 23:30:14 +00:00
|
|
|
if (muxer_listener)
|
2016-08-17 17:41:40 +00:00
|
|
|
muxer->SetMuxerListener(std::move(muxer_listener));
|
2014-05-22 19:39:30 +00:00
|
|
|
|
2017-03-21 23:14:46 +00:00
|
|
|
// Create a new trick_play_handler. Note that the stream_decriptors
|
|
|
|
// are sorted so that for the same input and stream_selector, the main
|
|
|
|
// stream is always the last one following the trick play streams.
|
|
|
|
if (stream_iter->trick_play_rate > 0) {
|
|
|
|
if (!trick_play_handler) {
|
|
|
|
trick_play_handler.reset(new TrickPlayHandler());
|
|
|
|
}
|
|
|
|
trick_play_handler->SetHandlerForTrickPlay(stream_iter->trick_play_rate,
|
|
|
|
std::move(muxer));
|
|
|
|
if (trick_play_handler->IsConnected())
|
|
|
|
continue;
|
|
|
|
} else if (trick_play_handler) {
|
|
|
|
trick_play_handler->SetHandlerForMainStream(std::move(muxer));
|
|
|
|
DCHECK(trick_play_handler->IsConnected());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<MediaHandler>> handlers;
|
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
auto chunking_handler = std::make_shared<ChunkingHandler>(chunking_options);
|
2017-03-21 23:14:46 +00:00
|
|
|
handlers.push_back(chunking_handler);
|
|
|
|
|
|
|
|
Status status;
|
2017-03-11 02:49:55 +00:00
|
|
|
if (encryption_key_source) {
|
|
|
|
auto new_encryption_options = encryption_options;
|
|
|
|
// Use Sample AES in MPEG2TS.
|
|
|
|
// TODO(kqyang): Consider adding a new flag to enable Sample AES as we
|
|
|
|
// will support CENC in TS in the future.
|
|
|
|
if (stream_iter->output_format == CONTAINER_MPEG2TS) {
|
|
|
|
LOG(INFO) << "Use Apple Sample AES encryption for MPEG2TS.";
|
|
|
|
new_encryption_options.protection_scheme =
|
|
|
|
kAppleSampleAesProtectionScheme;
|
|
|
|
}
|
2017-03-21 23:14:46 +00:00
|
|
|
handlers.emplace_back(
|
|
|
|
new EncryptionHandler(new_encryption_options, encryption_key_source));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If trick_play_handler is available, muxer should already be connected to
|
|
|
|
// trick_play_handler.
|
|
|
|
if (trick_play_handler) {
|
|
|
|
handlers.push_back(trick_play_handler);
|
2017-03-11 02:49:55 +00:00
|
|
|
} else {
|
2017-03-21 23:14:46 +00:00
|
|
|
handlers.push_back(std::move(muxer));
|
2017-03-11 02:49:55 +00:00
|
|
|
}
|
2017-02-24 01:17:47 +00:00
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
auto* demuxer = remux_jobs->back()->demuxer();
|
|
|
|
const std::string& stream_selector = stream_iter->stream_selector;
|
2017-02-24 01:17:47 +00:00
|
|
|
status.Update(demuxer->SetHandler(stream_selector, chunking_handler));
|
2017-03-21 23:14:46 +00:00
|
|
|
status.Update(ConnectHandlers(handlers));
|
2017-02-24 01:17:47 +00:00
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
if (!status.ok()) {
|
2017-02-24 01:17:47 +00:00
|
|
|
LOG(ERROR) << "Failed to setup graph: " << status;
|
2014-05-09 01:23:54 +00:00
|
|
|
return false;
|
2016-04-16 22:58:47 +00:00
|
|
|
}
|
2017-02-21 18:36:50 +00:00
|
|
|
if (!stream_iter->language.empty())
|
|
|
|
demuxer->SetLanguageOverride(stream_selector, stream_iter->language);
|
2014-05-09 01:23:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
// Initialize processing graph.
|
|
|
|
for (const std::unique_ptr<RemuxJob>& job : *remux_jobs) {
|
|
|
|
Status status = job->demuxer()->Initialize();
|
|
|
|
if (!status.ok()) {
|
|
|
|
LOG(ERROR) << "Failed to initialize processing graph " << status;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-05-09 01:23:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-30 23:01:19 +00:00
|
|
|
Status RunRemuxJobs(const std::vector<std::unique_ptr<RemuxJob>>& remux_jobs) {
|
2014-05-09 01:23:54 +00:00
|
|
|
// Start the job threads.
|
2016-08-30 23:01:19 +00:00
|
|
|
for (const std::unique_ptr<RemuxJob>& job : remux_jobs)
|
|
|
|
job->Start();
|
2014-05-09 01:23:54 +00:00
|
|
|
|
|
|
|
// Wait for all jobs to complete or an error occurs.
|
|
|
|
Status status;
|
|
|
|
bool all_joined;
|
|
|
|
do {
|
|
|
|
all_joined = true;
|
2016-08-30 23:01:19 +00:00
|
|
|
for (const std::unique_ptr<RemuxJob>& job : remux_jobs) {
|
|
|
|
if (job->HasBeenJoined()) {
|
|
|
|
status = job->status();
|
2014-05-09 01:23:54 +00:00
|
|
|
if (!status.ok())
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
all_joined = false;
|
2016-08-30 23:01:19 +00:00
|
|
|
job->Join();
|
2014-05-09 01:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (!all_joined && status.ok());
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:07:36 +00:00
|
|
|
bool RunPackager(const StreamDescriptorList& stream_descriptors) {
|
2014-06-11 00:20:00 +00:00
|
|
|
if (FLAGS_output_media_info && !FLAGS_mpd_output.empty()) {
|
|
|
|
NOTIMPLEMENTED() << "ERROR: --output_media_info and --mpd_output do not "
|
|
|
|
"work together.";
|
2014-06-26 17:42:12 +00:00
|
|
|
return false;
|
2014-06-11 00:20:00 +00:00
|
|
|
}
|
2014-06-26 17:42:12 +00:00
|
|
|
|
2016-04-16 22:58:47 +00:00
|
|
|
// Since there isn't a muxer listener that can output both MPD and HLS,
|
|
|
|
// disallow specifying both MPD and HLS flags.
|
|
|
|
if (!FLAGS_mpd_output.empty() && !FLAGS_hls_master_playlist_output.empty()) {
|
|
|
|
LOG(ERROR) << "Cannot output both MPD and HLS.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
ChunkingOptions chunking_options = GetChunkingOptions();
|
2017-03-11 02:49:55 +00:00
|
|
|
EncryptionOptions encryption_options = GetEncryptionOptions();
|
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
MuxerOptions muxer_options = GetMuxerOptions();
|
2014-05-09 01:23:54 +00:00
|
|
|
|
2017-01-07 02:40:37 +00:00
|
|
|
DCHECK(!stream_descriptors.empty());
|
|
|
|
// On demand profile generates single file segment while live profile
|
|
|
|
// generates multiple segments specified using segment template.
|
|
|
|
const bool on_demand_dash_profile =
|
|
|
|
stream_descriptors.begin()->segment_template.empty();
|
|
|
|
for (const auto& stream_descriptor : stream_descriptors) {
|
|
|
|
if (on_demand_dash_profile != stream_descriptor.segment_template.empty()) {
|
|
|
|
LOG(ERROR) << "Inconsistent stream descriptor specification: "
|
|
|
|
"segment_template should be specified for none or all "
|
|
|
|
"stream descriptors.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (FLAGS_output_media_info && !on_demand_dash_profile) {
|
|
|
|
// TODO(rkuroiwa, kqyang): Support partial media info dump for live.
|
|
|
|
NOTIMPLEMENTED() << "ERROR: --output_media_info is only supported for "
|
|
|
|
"on-demand profile (not using segment_template).";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
MpdOptions mpd_options = GetMpdOptions(on_demand_dash_profile);
|
2014-06-26 01:33:09 +00:00
|
|
|
|
2014-05-09 01:23:54 +00:00
|
|
|
// Create encryption key source if needed.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<KeySource> encryption_key_source;
|
2017-01-05 17:32:17 +00:00
|
|
|
if (FLAGS_enable_widevine_encryption || FLAGS_enable_fixed_key_encryption ||
|
|
|
|
FLAGS_enable_playready_encryption) {
|
2017-03-11 02:49:55 +00:00
|
|
|
if (encryption_options.protection_scheme == FOURCC_NULL)
|
|
|
|
return false;
|
2014-05-09 01:23:54 +00:00
|
|
|
encryption_key_source = CreateEncryptionKeySource();
|
|
|
|
if (!encryption_key_source)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<MpdNotifier> mpd_notifier;
|
2014-05-22 19:39:30 +00:00
|
|
|
if (!FLAGS_mpd_output.empty()) {
|
2016-08-17 21:42:23 +00:00
|
|
|
std::vector<std::string> base_urls = base::SplitString(
|
|
|
|
FLAGS_base_urls, ",", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
|
2015-08-04 22:26:15 +00:00
|
|
|
if (FLAGS_generate_dash_if_iop_compliant_mpd) {
|
2016-12-21 23:28:56 +00:00
|
|
|
mpd_notifier.reset(
|
|
|
|
new DashIopMpdNotifier(mpd_options, base_urls, FLAGS_mpd_output));
|
2015-08-04 22:26:15 +00:00
|
|
|
} else {
|
2016-12-21 23:28:56 +00:00
|
|
|
mpd_notifier.reset(
|
|
|
|
new SimpleMpdNotifier(mpd_options, base_urls, FLAGS_mpd_output));
|
2015-08-04 22:26:15 +00:00
|
|
|
}
|
2014-05-22 19:39:30 +00:00
|
|
|
if (!mpd_notifier->Init()) {
|
|
|
|
LOG(ERROR) << "MpdNotifier failed to initialize.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<hls::HlsNotifier> hls_notifier;
|
2016-04-16 22:58:47 +00:00
|
|
|
if (!FLAGS_hls_master_playlist_output.empty()) {
|
2016-08-14 22:28:21 +00:00
|
|
|
base::FilePath master_playlist_path(
|
|
|
|
base::FilePath::FromUTF8Unsafe(FLAGS_hls_master_playlist_output));
|
2016-04-16 22:58:47 +00:00
|
|
|
base::FilePath master_playlist_name = master_playlist_path.BaseName();
|
|
|
|
|
|
|
|
hls_notifier.reset(new hls::SimpleHlsNotifier(
|
|
|
|
hls::HlsNotifier::HlsProfile::kOnDemandProfile, FLAGS_hls_base_url,
|
2016-08-14 22:28:21 +00:00
|
|
|
master_playlist_path.DirName().AsEndingWithSeparator().AsUTF8Unsafe(),
|
|
|
|
master_playlist_name.AsUTF8Unsafe()));
|
2016-04-16 22:58:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 23:01:19 +00:00
|
|
|
std::vector<std::unique_ptr<RemuxJob>> remux_jobs;
|
2015-09-25 22:48:18 +00:00
|
|
|
FakeClock fake_clock;
|
2017-03-11 02:49:55 +00:00
|
|
|
if (!CreateRemuxJobs(stream_descriptors, chunking_options, encryption_options,
|
|
|
|
muxer_options, &fake_clock, encryption_key_source.get(),
|
2017-02-24 01:17:47 +00:00
|
|
|
mpd_notifier.get(), hls_notifier.get(), &remux_jobs)) {
|
2014-05-09 01:23:54 +00:00
|
|
|
return false;
|
2014-05-22 19:39:30 +00:00
|
|
|
}
|
2014-05-09 01:23:54 +00:00
|
|
|
|
|
|
|
Status status = RunRemuxJobs(remux_jobs);
|
|
|
|
if (!status.ok()) {
|
|
|
|
LOG(ERROR) << "Packaging Error: " << status.ToString();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-16 22:58:47 +00:00
|
|
|
if (hls_notifier) {
|
|
|
|
if (!hls_notifier->Flush())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mpd_notifier) {
|
|
|
|
if (!mpd_notifier->Flush())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-09 01:23:54 +00:00
|
|
|
printf("Packaging completed successfully.\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-03 05:17:54 +00:00
|
|
|
int PackagerMain(int argc, char** argv) {
|
2015-12-30 01:09:12 +00:00
|
|
|
base::AtExitManager exit;
|
2015-10-27 18:52:50 +00:00
|
|
|
// Needed to enable VLOG/DVLOG through --vmodule or --v.
|
|
|
|
base::CommandLine::Init(argc, argv);
|
2016-08-14 22:28:21 +00:00
|
|
|
|
|
|
|
// Set up logging.
|
|
|
|
logging::LoggingSettings log_settings;
|
2016-08-31 21:50:19 +00:00
|
|
|
log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
|
2016-08-14 22:28:21 +00:00
|
|
|
CHECK(logging::InitLogging(log_settings));
|
2015-10-27 18:52:50 +00:00
|
|
|
|
2016-12-08 20:07:09 +00:00
|
|
|
google::SetVersionString(GetPackagerVersion());
|
2014-05-09 01:23:54 +00:00
|
|
|
google::SetUsageMessage(base::StringPrintf(kUsage, argv[0]));
|
|
|
|
google::ParseCommandLineFlags(&argc, &argv, true);
|
|
|
|
if (argc < 2) {
|
2016-12-08 20:07:09 +00:00
|
|
|
google::ShowUsageWithFlags("Usage");
|
2015-12-21 23:10:17 +00:00
|
|
|
return kSuccess;
|
2014-05-09 01:23:54 +00:00
|
|
|
}
|
2014-10-13 22:06:48 +00:00
|
|
|
|
2017-01-05 17:32:17 +00:00
|
|
|
if (!ValidateWidevineCryptoFlags() || !ValidateFixedCryptoFlags() ||
|
|
|
|
!ValidatePRCryptoFlags()) {
|
2014-10-13 22:06:48 +00:00
|
|
|
return kArgumentValidationFailed;
|
2017-01-05 17:32:17 +00:00
|
|
|
}
|
2014-10-13 22:06:48 +00:00
|
|
|
|
2016-07-07 19:34:07 +00:00
|
|
|
if (FLAGS_override_version)
|
|
|
|
SetPackagerVersionForTesting(FLAGS_test_version);
|
|
|
|
|
2016-05-19 21:38:15 +00:00
|
|
|
LibcryptoThreading libcrypto_threading;
|
2014-06-27 23:07:36 +00:00
|
|
|
// TODO(tinskip): Make InsertStreamDescriptor a member of
|
|
|
|
// StreamDescriptorList.
|
2014-10-03 05:17:54 +00:00
|
|
|
StreamDescriptorList stream_descriptors;
|
2014-06-27 23:07:36 +00:00
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (!InsertStreamDescriptor(argv[i], &stream_descriptors))
|
2014-10-13 22:06:48 +00:00
|
|
|
return kArgumentValidationFailed;
|
2014-06-27 23:07:36 +00:00
|
|
|
}
|
2014-10-13 22:06:48 +00:00
|
|
|
return RunPackager(stream_descriptors) ? kSuccess : kPackagingFailed;
|
2014-10-03 05:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2014-10-03 05:17:54 +00:00
|
|
|
|
2016-08-14 22:28:21 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
// Windows wmain, which converts wide character arguments to UTF-8.
|
|
|
|
int wmain(int argc, wchar_t* argv[], wchar_t* envp[]) {
|
|
|
|
std::unique_ptr<char* [], std::function<void(char**)>> utf8_argv(
|
|
|
|
new char*[argc], [argc](char** utf8_args) {
|
|
|
|
// TODO(tinskip): This leaks, but if this code is enabled, it crashes.
|
|
|
|
// Figure out why. I suspect gflags does something funny with the
|
|
|
|
// argument array.
|
|
|
|
// for (int idx = 0; idx < argc; ++idx)
|
|
|
|
// delete[] utf8_args[idx];
|
|
|
|
delete[] utf8_args;
|
|
|
|
});
|
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
|
|
|
for (int idx = 0; idx < argc; ++idx) {
|
|
|
|
std::string utf8_arg(converter.to_bytes(argv[idx]));
|
|
|
|
utf8_arg += '\0';
|
|
|
|
utf8_argv[idx] = new char[utf8_arg.size()];
|
|
|
|
memcpy(utf8_argv[idx], &utf8_arg[0], utf8_arg.size());
|
|
|
|
}
|
|
|
|
return shaka::media::PackagerMain(argc, utf8_argv.get());
|
|
|
|
}
|
|
|
|
#else
|
2014-10-03 05:17:54 +00:00
|
|
|
int main(int argc, char** argv) {
|
2016-05-20 21:19:33 +00:00
|
|
|
return shaka::media::PackagerMain(argc, argv);
|
2014-05-09 01:23:54 +00:00
|
|
|
}
|
2016-08-14 22:28:21 +00:00
|
|
|
#endif // defined(OS_WIN)
|