2017-05-22 20:28:10 +00:00
|
|
|
// Copyright 2017 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
|
|
|
|
|
|
|
|
#include "packager/packager.h"
|
|
|
|
|
2017-09-30 00:45:00 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2017-12-12 21:05:12 +00:00
|
|
|
#include "packager/app/job_manager.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/app/libcrypto_threading.h"
|
2018-01-03 18:16:08 +00:00
|
|
|
#include "packager/app/muxer_factory.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/app/packager_util.h"
|
|
|
|
#include "packager/app/stream_descriptor.h"
|
2017-05-23 02:41:26 +00:00
|
|
|
#include "packager/base/at_exit.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/base/files/file_path.h"
|
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/path_service.h"
|
2018-01-10 00:04:40 +00:00
|
|
|
#include "packager/base/strings/string_util.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/base/strings/stringprintf.h"
|
|
|
|
#include "packager/base/threading/simple_thread.h"
|
|
|
|
#include "packager/base/time/clock.h"
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/hls/base/hls_notifier.h"
|
|
|
|
#include "packager/hls/base/simple_hls_notifier.h"
|
2017-11-21 21:24:13 +00:00
|
|
|
#include "packager/media/ad_cue_generator/ad_cue_generator.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/media/base/container_names.h"
|
|
|
|
#include "packager/media/base/fourccs.h"
|
|
|
|
#include "packager/media/base/key_source.h"
|
2017-09-30 00:45:00 +00:00
|
|
|
#include "packager/media/base/language_utils.h"
|
2018-01-03 18:16:08 +00:00
|
|
|
#include "packager/media/base/muxer.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/media/base/muxer_options.h"
|
|
|
|
#include "packager/media/base/muxer_util.h"
|
|
|
|
#include "packager/media/chunking/chunking_handler.h"
|
2018-03-20 17:12:18 +00:00
|
|
|
#include "packager/media/chunking/cue_alignment_handler.h"
|
2018-03-23 22:28:30 +00:00
|
|
|
#include "packager/media/chunking/text_chunker.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/media/crypto/encryption_handler.h"
|
|
|
|
#include "packager/media/demuxer/demuxer.h"
|
2017-12-12 22:13:47 +00:00
|
|
|
#include "packager/media/event/muxer_listener_factory.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/media/event/vod_media_info_dump_muxer_listener.h"
|
2018-03-29 18:00:14 +00:00
|
|
|
#include "packager/media/formats/webvtt/text_padder.h"
|
2017-09-14 16:15:24 +00:00
|
|
|
#include "packager/media/formats/webvtt/text_readers.h"
|
|
|
|
#include "packager/media/formats/webvtt/webvtt_output_handler.h"
|
|
|
|
#include "packager/media/formats/webvtt/webvtt_parser.h"
|
2018-01-03 18:16:08 +00:00
|
|
|
#include "packager/media/formats/webvtt/webvtt_to_mp4_handler.h"
|
2017-08-28 22:27:05 +00:00
|
|
|
#include "packager/media/replicator/replicator.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
#include "packager/media/trick_play/trick_play_handler.h"
|
|
|
|
#include "packager/mpd/base/media_info.pb.h"
|
|
|
|
#include "packager/mpd/base/mpd_builder.h"
|
|
|
|
#include "packager/mpd/base/simple_mpd_notifier.h"
|
2018-03-29 17:32:13 +00:00
|
|
|
#include "packager/status_macros.h"
|
2017-05-23 02:41:26 +00:00
|
|
|
#include "packager/version/version.h"
|
2017-05-22 20:28:10 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
|
|
|
|
// TODO(kqyang): Clean up namespaces.
|
|
|
|
using media::Demuxer;
|
2018-03-22 06:24:17 +00:00
|
|
|
using media::JobManager;
|
2017-05-22 20:28:10 +00:00
|
|
|
using media::KeySource;
|
|
|
|
using media::MuxerOptions;
|
2018-03-20 17:12:18 +00:00
|
|
|
using media::SyncPointQueue;
|
2017-05-22 20:28:10 +00:00
|
|
|
|
|
|
|
namespace media {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kMediaInfoSuffix[] = ".media_info";
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
MuxerOptions CreateMuxerOptions(const StreamDescriptor& stream,
|
|
|
|
const PackagingParams& params) {
|
|
|
|
MuxerOptions options;
|
|
|
|
|
|
|
|
options.mp4_params = params.mp4_output_params;
|
|
|
|
options.temp_dir = params.temp_dir;
|
|
|
|
options.bandwidth = stream.bandwidth;
|
|
|
|
options.output_file_name = stream.output;
|
|
|
|
options.segment_template = stream.segment_template;
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:13:47 +00:00
|
|
|
MuxerListenerFactory::StreamData ToMuxerListenerData(
|
|
|
|
const StreamDescriptor& stream) {
|
|
|
|
MuxerListenerFactory::StreamData data;
|
|
|
|
data.media_info_output = stream.output;
|
|
|
|
data.hls_group_id = stream.hls_group_id;
|
|
|
|
data.hls_name = stream.hls_name;
|
|
|
|
data.hls_playlist_name = stream.hls_playlist_name;
|
2018-02-05 19:21:28 +00:00
|
|
|
data.hls_iframe_playlist_name = stream.hls_iframe_playlist_name;
|
2017-12-12 22:13:47 +00:00
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2017-05-22 20:28:10 +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,
|
2017-08-23 15:46:16 +00:00
|
|
|
// CreateAllJobs() shouldn't treat text as a special case.
|
2018-02-26 20:13:28 +00:00
|
|
|
bool DetermineTextFileCodec(const std::string& file, std::string* out) {
|
|
|
|
CHECK(out);
|
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
std::string content;
|
|
|
|
if (!File::ReadFileToString(file.c_str(), &content)) {
|
|
|
|
LOG(ERROR) << "Failed to open file " << file
|
|
|
|
<< " to determine file format.";
|
2018-02-26 20:13:28 +00:00
|
|
|
return false;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
2018-02-26 20:13:28 +00:00
|
|
|
|
|
|
|
const uint8_t* content_data =
|
|
|
|
reinterpret_cast<const uint8_t*>(content.data());
|
|
|
|
MediaContainerName container_name =
|
|
|
|
DetermineContainer(content_data, content.size());
|
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
if (container_name == CONTAINER_WEBVTT) {
|
2018-02-26 20:13:28 +00:00
|
|
|
*out = "wvtt";
|
|
|
|
return true;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 20:13:28 +00:00
|
|
|
if (container_name == CONTAINER_TTML) {
|
|
|
|
*out = "ttml";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaContainerName GetOutputFormat(const StreamDescriptor& descriptor) {
|
|
|
|
MediaContainerName output_format = CONTAINER_UNKNOWN;
|
|
|
|
if (!descriptor.output_format.empty()) {
|
|
|
|
output_format = DetermineContainerFromFormatName(descriptor.output_format);
|
|
|
|
if (output_format == CONTAINER_UNKNOWN) {
|
|
|
|
LOG(ERROR) << "Unable to determine output format from '"
|
|
|
|
<< descriptor.output_format << "'.";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const std::string& output_name = descriptor.output.empty()
|
|
|
|
? descriptor.segment_template
|
|
|
|
: descriptor.output;
|
|
|
|
if (output_name.empty())
|
|
|
|
return CONTAINER_UNKNOWN;
|
|
|
|
output_format = DetermineContainerFromFileName(output_name);
|
|
|
|
if (output_format == CONTAINER_UNKNOWN) {
|
|
|
|
LOG(ERROR) << "Unable to determine output format from '" << output_name
|
|
|
|
<< "'.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output_format;
|
|
|
|
}
|
|
|
|
|
2017-08-29 17:54:29 +00:00
|
|
|
Status ValidateStreamDescriptor(bool dump_stream_info,
|
|
|
|
const StreamDescriptor& stream) {
|
|
|
|
if (stream.input.empty()) {
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Stream input not specified.");
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
2017-08-29 17:54:29 +00:00
|
|
|
|
|
|
|
// The only time a stream can have no outputs, is when dump stream info is
|
|
|
|
// set.
|
|
|
|
if (dump_stream_info && stream.output.empty() &&
|
|
|
|
stream.segment_template.empty()) {
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream.output.empty() && stream.segment_template.empty()) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Streams must specify 'output' or 'segment template'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whenever there is output, a stream must be selected.
|
|
|
|
if (stream.stream_selector.empty()) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Stream stream_selector not specified.");
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 17:54:29 +00:00
|
|
|
// If a segment template is provided, it must be valid.
|
|
|
|
if (stream.segment_template.length()) {
|
|
|
|
Status template_check = ValidateSegmentTemplate(stream.segment_template);
|
|
|
|
if (!template_check.ok()) {
|
|
|
|
return template_check;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are some specifics that must be checked based on which format
|
|
|
|
// we are writing to.
|
|
|
|
const MediaContainerName output_format = GetOutputFormat(stream);
|
|
|
|
|
|
|
|
if (output_format == CONTAINER_UNKNOWN) {
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Unsupported output format.");
|
|
|
|
} else if (output_format == MediaContainerName::CONTAINER_MPEG2TS) {
|
|
|
|
if (stream.segment_template.empty()) {
|
2018-01-16 22:26:18 +00:00
|
|
|
return Status(
|
|
|
|
error::INVALID_ARGUMENT,
|
|
|
|
"Please specify 'segment_template'. Single file TS output is "
|
|
|
|
"not supported.");
|
2017-08-29 17:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Right now the init segment is saved in |output| for multi-segment
|
|
|
|
// content. However, for TS all segments must be self-initializing so
|
|
|
|
// there cannot be an init segment.
|
|
|
|
if (stream.output.length()) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"All TS segments must be self-initializing. Stream "
|
|
|
|
"descriptors 'output' or 'init_segment' are not allowed.");
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
2018-01-16 22:26:18 +00:00
|
|
|
} else if (output_format == CONTAINER_WEBVTT) {
|
|
|
|
// There is no need for an init segment when outputting to WebVTT because
|
|
|
|
// there is no initialization data.
|
|
|
|
if (stream.segment_template.length() && stream.output.length()) {
|
|
|
|
return Status(
|
|
|
|
error::INVALID_ARGUMENT,
|
|
|
|
"Segmented WebVTT output cannot have an init segment. Do not specify "
|
|
|
|
"stream descriptors 'output' or 'init_segment' when using "
|
|
|
|
"'segment_template' with WebVtt.");
|
|
|
|
}
|
2017-05-22 20:28:10 +00:00
|
|
|
} else {
|
2017-08-29 17:54:29 +00:00
|
|
|
// For any other format, if there is a segment template, there must be an
|
|
|
|
// init segment provided.
|
|
|
|
if (stream.segment_template.length() && stream.output.empty()) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Please specify 'init_segment'. All non-TS multi-segment "
|
|
|
|
"content must provide an init segment.");
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-29 17:54:29 +00:00
|
|
|
|
|
|
|
return Status::OK;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 17:54:29 +00:00
|
|
|
Status ValidateParams(const PackagingParams& packaging_params,
|
|
|
|
const std::vector<StreamDescriptor>& stream_descriptors) {
|
2017-05-22 20:28:10 +00:00
|
|
|
if (!packaging_params.chunking_params.segment_sap_aligned &&
|
|
|
|
packaging_params.chunking_params.subsegment_sap_aligned) {
|
2017-08-29 17:54:29 +00:00
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Setting segment_sap_aligned to false but "
|
|
|
|
"subsegment_sap_aligned to true is not allowed.");
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_descriptors.empty()) {
|
2017-08-29 17:54:29 +00:00
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Stream descriptors cannot be empty.");
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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& descriptor : stream_descriptors) {
|
|
|
|
if (on_demand_dash_profile != descriptor.segment_template.empty()) {
|
2017-08-29 17:54:29 +00:00
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Inconsistent stream descriptor specification: "
|
2017-05-22 20:28:10 +00:00
|
|
|
"segment_template should be specified for none or all "
|
2017-08-29 17:54:29 +00:00
|
|
|
"stream descriptors.");
|
|
|
|
}
|
|
|
|
|
|
|
|
Status stream_check = ValidateStreamDescriptor(
|
|
|
|
packaging_params.test_params.dump_stream_info, descriptor);
|
|
|
|
|
|
|
|
if (!stream_check.ok()) {
|
|
|
|
return stream_check;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-29 17:54:29 +00:00
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
if (packaging_params.output_media_info && !on_demand_dash_profile) {
|
|
|
|
// TODO(rkuroiwa, kqyang): Support partial media info dump for live.
|
2017-08-29 17:54:29 +00:00
|
|
|
return Status(error::UNIMPLEMENTED,
|
|
|
|
"--output_media_info is only supported for on-demand profile "
|
|
|
|
"(not using segment_template).");
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 17:54:29 +00:00
|
|
|
return Status::OK;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 00:45:00 +00:00
|
|
|
bool StreamDescriptorCompareFn(const StreamDescriptor& a,
|
|
|
|
const StreamDescriptor& b) {
|
|
|
|
if (a.input == b.input) {
|
|
|
|
if (a.stream_selector == b.stream_selector) {
|
|
|
|
// The MPD notifier requires that the main track comes first, so make
|
|
|
|
// sure that happens.
|
|
|
|
if (a.trick_play_factor == 0 || b.trick_play_factor == 0) {
|
|
|
|
return a.trick_play_factor == 0;
|
2017-08-28 22:27:05 +00:00
|
|
|
} else {
|
2017-09-30 00:45:00 +00:00
|
|
|
return a.trick_play_factor > b.trick_play_factor;
|
2017-08-28 22:27:05 +00:00
|
|
|
}
|
2017-09-30 00:45:00 +00:00
|
|
|
} else {
|
|
|
|
return a.stream_selector < b.stream_selector;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 00:45:00 +00:00
|
|
|
return a.input < b.input;
|
|
|
|
}
|
2017-05-22 20:28:10 +00:00
|
|
|
|
|
|
|
// A fake clock that always return time 0 (epoch). Should only be used for
|
|
|
|
// testing.
|
|
|
|
class FakeClock : public base::Clock {
|
|
|
|
public:
|
|
|
|
base::Time Now() override { return base::Time(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
bool StreamInfoToTextMediaInfo(const StreamDescriptor& stream_descriptor,
|
|
|
|
MediaInfo* text_media_info) {
|
2018-02-26 20:13:28 +00:00
|
|
|
std::string codec;
|
|
|
|
if (!DetermineTextFileCodec(stream_descriptor.input, &codec)) {
|
2017-05-22 20:28:10 +00:00
|
|
|
LOG(ERROR) << "Failed to determine the text file format for "
|
|
|
|
<< stream_descriptor.input;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-26 20:13:28 +00:00
|
|
|
MediaInfo::TextInfo* text_info = text_media_info->mutable_text_info();
|
|
|
|
text_info->set_codec(codec);
|
|
|
|
|
|
|
|
const std::string& language = stream_descriptor.language;
|
|
|
|
if (!language.empty()) {
|
|
|
|
text_info->set_language(language);
|
|
|
|
}
|
|
|
|
|
2017-08-28 23:55:10 +00:00
|
|
|
text_media_info->set_media_file_name(stream_descriptor.output);
|
2017-05-22 20:28:10 +00:00
|
|
|
text_media_info->set_container_type(MediaInfo::CONTAINER_TEXT);
|
|
|
|
|
2017-08-28 23:55:10 +00:00
|
|
|
if (stream_descriptor.bandwidth != 0) {
|
|
|
|
text_media_info->set_bandwidth(stream_descriptor.bandwidth);
|
2017-05-22 20:28:10 +00:00
|
|
|
} 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.
|
|
|
|
const int kDefaultTextBandwidth = 256;
|
|
|
|
text_media_info->set_bandwidth(kDefaultTextBandwidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-21 22:53:24 +00:00
|
|
|
/// Create a new demuxer handler for the given stream. If a demuxer cannot be
|
|
|
|
/// created, an error will be returned. If a demuxer can be created, this
|
|
|
|
/// |new_demuxer| will be set and Status::OK will be returned.
|
|
|
|
Status CreateDemuxer(const StreamDescriptor& stream,
|
|
|
|
const PackagingParams& packaging_params,
|
|
|
|
std::shared_ptr<Demuxer>* new_demuxer) {
|
|
|
|
std::shared_ptr<Demuxer> demuxer = std::make_shared<Demuxer>(stream.input);
|
|
|
|
demuxer->set_dump_stream_info(packaging_params.test_params.dump_stream_info);
|
|
|
|
|
|
|
|
if (packaging_params.decryption_params.key_provider != KeyProvider::kNone) {
|
|
|
|
std::unique_ptr<KeySource> decryption_key_source(
|
|
|
|
CreateDecryptionKeySource(packaging_params.decryption_params));
|
|
|
|
if (!decryption_key_source) {
|
|
|
|
return Status(
|
|
|
|
error::INVALID_ARGUMENT,
|
|
|
|
"Must define decryption key source when defining key provider");
|
|
|
|
}
|
|
|
|
demuxer->SetKeySource(std::move(decryption_key_source));
|
2017-08-28 23:55:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 22:53:24 +00:00
|
|
|
*new_demuxer = std::move(demuxer);
|
|
|
|
return Status::OK;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 18:00:45 +00:00
|
|
|
std::shared_ptr<MediaHandler> CreateEncryptionHandler(
|
2017-08-29 20:39:43 +00:00
|
|
|
const PackagingParams& packaging_params,
|
|
|
|
const StreamDescriptor& stream,
|
|
|
|
KeySource* key_source) {
|
|
|
|
if (stream.skip_encryption) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key_source) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a copy so that we can modify it for this specific stream.
|
|
|
|
EncryptionParams encryption_params = packaging_params.encryption_params;
|
|
|
|
|
|
|
|
// 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 (GetOutputFormat(stream) == CONTAINER_MPEG2TS) {
|
|
|
|
VLOG(1) << "Use Apple Sample AES encryption for MPEG2TS.";
|
|
|
|
encryption_params.protection_scheme = kAppleSampleAesProtectionScheme;
|
|
|
|
}
|
|
|
|
|
2017-09-20 22:49:00 +00:00
|
|
|
if (!stream.drm_label.empty()) {
|
|
|
|
const std::string& drm_label = stream.drm_label;
|
|
|
|
encryption_params.stream_label_func =
|
|
|
|
[drm_label](const EncryptionParams::EncryptedStreamAttributes&) {
|
|
|
|
return drm_label;
|
|
|
|
};
|
|
|
|
} else if (!encryption_params.stream_label_func) {
|
2017-08-29 20:39:43 +00:00
|
|
|
const int kDefaultMaxSdPixels = 768 * 576;
|
|
|
|
const int kDefaultMaxHdPixels = 1920 * 1080;
|
|
|
|
const int kDefaultMaxUhd1Pixels = 4096 * 2160;
|
|
|
|
encryption_params.stream_label_func = std::bind(
|
|
|
|
&Packager::DefaultStreamLabelFunction, kDefaultMaxSdPixels,
|
|
|
|
kDefaultMaxHdPixels, kDefaultMaxUhd1Pixels, std::placeholders::_1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_shared<EncryptionHandler>(encryption_params, key_source);
|
|
|
|
}
|
|
|
|
|
2017-12-12 22:13:47 +00:00
|
|
|
Status CreateMp4ToMp4TextJob(const StreamDescriptor& stream,
|
2017-12-21 22:53:24 +00:00
|
|
|
const PackagingParams& packaging_params,
|
2018-01-03 18:16:08 +00:00
|
|
|
std::unique_ptr<MuxerListener> muxer_listener,
|
2018-03-20 17:12:18 +00:00
|
|
|
SyncPointQueue* sync_points,
|
2017-12-21 22:53:24 +00:00
|
|
|
MuxerFactory* muxer_factory,
|
|
|
|
std::shared_ptr<OriginHandler>* root) {
|
2018-03-20 17:12:18 +00:00
|
|
|
// TODO(kqyang): This need to be integrated back to media pipeline since we
|
|
|
|
// may want to get not only text streams from the demuxer, in which case, the
|
|
|
|
// same demuxer should be used to get all streams instead of having a demuxer
|
|
|
|
// specifically for text.
|
|
|
|
// TODO(kqyang): Support Cue Alignment if |sync_points| is not null.
|
|
|
|
|
2017-12-21 22:53:24 +00:00
|
|
|
Status status;
|
|
|
|
std::shared_ptr<Demuxer> demuxer;
|
|
|
|
|
|
|
|
status.Update(CreateDemuxer(stream, packaging_params, &demuxer));
|
|
|
|
if (!stream.language.empty()) {
|
|
|
|
demuxer->SetLanguageOverride(stream.stream_selector, stream.language);
|
|
|
|
}
|
|
|
|
|
2018-03-20 17:12:18 +00:00
|
|
|
auto chunker =
|
|
|
|
std::make_shared<ChunkingHandler>(packaging_params.chunking_params);
|
2017-12-21 22:53:24 +00:00
|
|
|
std::shared_ptr<Muxer> muxer =
|
2018-01-03 18:16:08 +00:00
|
|
|
muxer_factory->CreateMuxer(GetOutputFormat(stream), stream);
|
|
|
|
muxer->SetMuxerListener(std::move(muxer_listener));
|
2017-12-21 22:53:24 +00:00
|
|
|
|
|
|
|
status.Update(chunker->AddHandler(std::move(muxer)));
|
2018-03-20 17:12:18 +00:00
|
|
|
status.Update(
|
|
|
|
demuxer->SetHandler(stream.stream_selector, std::move(chunker)));
|
2017-12-21 22:53:24 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
Status CreateHlsTextJob(const StreamDescriptor& stream,
|
|
|
|
const PackagingParams& packaging_params,
|
2017-12-12 22:13:47 +00:00
|
|
|
std::unique_ptr<MuxerListener> muxer_listener,
|
2018-03-20 17:12:18 +00:00
|
|
|
SyncPointQueue* sync_points,
|
2017-09-14 16:15:24 +00:00
|
|
|
JobManager* job_manager) {
|
2017-12-12 22:13:47 +00:00
|
|
|
DCHECK(muxer_listener);
|
|
|
|
DCHECK(job_manager);
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
if (stream.segment_template.empty()) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Cannot output text (" + stream.input +
|
|
|
|
") to HLS with no segment template");
|
|
|
|
}
|
|
|
|
|
|
|
|
const float segment_length_in_seconds =
|
|
|
|
packaging_params.chunking_params.segment_duration_in_seconds;
|
|
|
|
const uint64_t segment_length_in_ms =
|
|
|
|
static_cast<uint64_t>(segment_length_in_seconds * 1000);
|
|
|
|
|
|
|
|
// 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 if it is missing.
|
|
|
|
MuxerOptions muxer_options = CreateMuxerOptions(stream, packaging_params);
|
|
|
|
muxer_options.bandwidth = stream.bandwidth ? stream.bandwidth : 256;
|
|
|
|
|
2018-03-20 17:12:18 +00:00
|
|
|
auto output = std::make_shared<WebVttSegmentedOutputHandler>(
|
|
|
|
muxer_options, std::move(muxer_listener));
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
std::unique_ptr<FileReader> reader;
|
2018-03-29 17:32:13 +00:00
|
|
|
RETURN_IF_ERROR(FileReader::Open(stream.input, &reader));
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2018-03-29 18:00:14 +00:00
|
|
|
const int64_t kNoDuration = 0;
|
2018-03-20 17:12:18 +00:00
|
|
|
auto parser =
|
|
|
|
std::make_shared<WebVttParser>(std::move(reader), stream.language);
|
2018-03-29 18:00:14 +00:00
|
|
|
auto padder = std::make_shared<TextPadder>(kNoDuration);
|
2018-03-29 17:32:13 +00:00
|
|
|
auto chunker = std::make_shared<TextChunker>(segment_length_in_ms);
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
// Build in reverse to allow us to move the pointers.
|
2018-03-29 17:32:13 +00:00
|
|
|
if (sync_points) {
|
|
|
|
auto cue_aligner = std::make_shared<CueAlignmentHandler>(sync_points);
|
|
|
|
RETURN_IF_ERROR(chunker->AddHandler(std::move(output)));
|
|
|
|
RETURN_IF_ERROR(cue_aligner->AddHandler(std::move(chunker)));
|
2018-03-29 18:00:14 +00:00
|
|
|
RETURN_IF_ERROR(padder->AddHandler(std::move(cue_aligner)));
|
|
|
|
RETURN_IF_ERROR(parser->AddHandler(std::move(padder)));
|
2018-03-29 17:32:13 +00:00
|
|
|
} else {
|
|
|
|
RETURN_IF_ERROR(chunker->AddHandler(std::move(output)));
|
2018-03-29 18:00:14 +00:00
|
|
|
RETURN_IF_ERROR(padder->AddHandler(std::move(chunker)));
|
|
|
|
RETURN_IF_ERROR(parser->AddHandler(std::move(padder)));
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
job_manager->Add("Segmented Text Job", std::move(parser));
|
2018-03-29 17:32:13 +00:00
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2018-01-03 18:16:08 +00:00
|
|
|
Status CreateWebVttToMp4TextJob(const StreamDescriptor& stream,
|
|
|
|
const PackagingParams& packaging_params,
|
|
|
|
std::unique_ptr<MuxerListener> muxer_listener,
|
2018-03-20 17:12:18 +00:00
|
|
|
SyncPointQueue* sync_points,
|
2018-01-03 18:16:08 +00:00
|
|
|
MuxerFactory* muxer_factory,
|
|
|
|
std::shared_ptr<OriginHandler>* root) {
|
2018-03-20 17:12:18 +00:00
|
|
|
// TODO(kqyang): Support Cue Alignment if |sync_points| is not null.
|
|
|
|
|
2018-01-03 18:16:08 +00:00
|
|
|
std::unique_ptr<FileReader> reader;
|
2018-03-29 18:00:14 +00:00
|
|
|
RETURN_IF_ERROR(FileReader::Open(stream.input, &reader));
|
2018-01-03 18:16:08 +00:00
|
|
|
|
2018-03-29 18:00:14 +00:00
|
|
|
const int64_t kNoDuration = 0;
|
2018-03-20 17:12:18 +00:00
|
|
|
auto parser =
|
|
|
|
std::make_shared<WebVttParser>(std::move(reader), stream.language);
|
2018-03-29 18:00:14 +00:00
|
|
|
auto padder = std::make_shared<TextPadder>(kNoDuration);
|
2018-03-20 17:12:18 +00:00
|
|
|
auto text_to_mp4 = std::make_shared<WebVttToMp4Handler>();
|
|
|
|
auto chunker =
|
|
|
|
std::make_shared<ChunkingHandler>(packaging_params.chunking_params);
|
2018-03-29 18:00:14 +00:00
|
|
|
auto muxer = muxer_factory->CreateMuxer(GetOutputFormat(stream), stream);
|
2018-01-03 18:16:08 +00:00
|
|
|
muxer->SetMuxerListener(std::move(muxer_listener));
|
|
|
|
|
2018-03-29 18:00:14 +00:00
|
|
|
RETURN_IF_ERROR(chunker->AddHandler(std::move(muxer)));
|
|
|
|
RETURN_IF_ERROR(text_to_mp4->AddHandler(std::move(chunker)));
|
|
|
|
RETURN_IF_ERROR(padder->AddHandler(std::move(text_to_mp4)));
|
|
|
|
RETURN_IF_ERROR(parser->AddHandler(std::move(padder)));
|
2018-01-03 18:16:08 +00:00
|
|
|
|
|
|
|
*root = std::move(parser);
|
|
|
|
|
2018-03-29 18:00:14 +00:00
|
|
|
return Status::OK;
|
2018-01-03 18:16:08 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 15:46:16 +00:00
|
|
|
Status CreateTextJobs(
|
|
|
|
const std::vector<std::reference_wrapper<const StreamDescriptor>>& streams,
|
|
|
|
const PackagingParams& packaging_params,
|
2018-03-20 17:12:18 +00:00
|
|
|
SyncPointQueue* sync_points,
|
2017-12-12 22:13:47 +00:00
|
|
|
MuxerListenerFactory* muxer_listener_factory,
|
2017-12-21 22:53:24 +00:00
|
|
|
MuxerFactory* muxer_factory,
|
2017-08-23 15:46:16 +00:00
|
|
|
MpdNotifier* mpd_notifier,
|
2017-12-12 21:05:12 +00:00
|
|
|
JobManager* job_manager) {
|
2017-12-12 22:13:47 +00:00
|
|
|
DCHECK(muxer_listener_factory);
|
2017-12-12 21:05:12 +00:00
|
|
|
DCHECK(job_manager);
|
2017-08-23 15:46:16 +00:00
|
|
|
for (const StreamDescriptor& stream : streams) {
|
2018-01-03 18:16:08 +00:00
|
|
|
if (GetOutputFormat(stream) == CONTAINER_MOV) {
|
2018-01-03 18:16:08 +00:00
|
|
|
std::unique_ptr<MuxerListener> muxer_listener =
|
|
|
|
muxer_listener_factory->CreateListener(ToMuxerListenerData(stream));
|
|
|
|
|
2017-12-21 22:53:24 +00:00
|
|
|
std::shared_ptr<OriginHandler> root;
|
2018-01-03 18:16:08 +00:00
|
|
|
Status status;
|
|
|
|
|
|
|
|
switch (DetermineContainerFromFileName(stream.input)) {
|
|
|
|
case CONTAINER_WEBVTT:
|
2018-03-20 17:12:18 +00:00
|
|
|
status.Update(CreateWebVttToMp4TextJob(
|
|
|
|
stream, packaging_params, std::move(muxer_listener), sync_points,
|
|
|
|
muxer_factory, &root));
|
2018-01-03 18:16:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTAINER_MOV:
|
2018-03-20 17:12:18 +00:00
|
|
|
status.Update(CreateMp4ToMp4TextJob(
|
|
|
|
stream, packaging_params, std::move(muxer_listener), sync_points,
|
|
|
|
muxer_factory, &root));
|
2018-01-03 18:16:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
status.Update(
|
|
|
|
Status(error::INVALID_ARGUMENT,
|
|
|
|
"Text output format is not support for " + stream.input));
|
|
|
|
break;
|
|
|
|
}
|
2017-12-21 22:53:24 +00:00
|
|
|
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
job_manager->Add("MP4 text job", std::move(root));
|
2017-08-23 15:46:16 +00:00
|
|
|
} else {
|
2017-12-12 22:13:47 +00:00
|
|
|
std::unique_ptr<MuxerListener> hls_listener =
|
|
|
|
muxer_listener_factory->CreateHlsListener(
|
|
|
|
ToMuxerListenerData(stream));
|
|
|
|
|
|
|
|
// Check input to ensure that output is possible.
|
|
|
|
if (hls_listener) {
|
|
|
|
if (stream.segment_template.empty() || !stream.output.empty()) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"segment_template needs to be specified for HLS text "
|
|
|
|
"output. Single file output is not supported yet.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mpd_notifier && !stream.segment_template.empty()) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Cannot create text output for MPD with segment output.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are outputting to HLS, then create the HLS test pipeline that
|
|
|
|
// will create segmented text output.
|
|
|
|
if (hls_listener) {
|
2018-03-20 17:12:18 +00:00
|
|
|
Status status =
|
|
|
|
CreateHlsTextJob(stream, packaging_params, std::move(hls_listener),
|
|
|
|
sync_points, job_manager);
|
2017-09-14 16:15:24 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:26:18 +00:00
|
|
|
if (!stream.output.empty()) {
|
|
|
|
if (!File::Copy(stream.input.c_str(), stream.output.c_str())) {
|
|
|
|
std::string error;
|
|
|
|
base::StringAppendF(
|
|
|
|
&error, "Failed to copy the input file (%s) to output file (%s).",
|
|
|
|
stream.input.c_str(), stream.output.c_str());
|
|
|
|
return Status(error::FILE_FAILURE, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaInfo text_media_info;
|
|
|
|
if (!StreamInfoToTextMediaInfo(stream, &text_media_info)) {
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Could not create media info for stream.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are outputting to MPD, just add the input to the outputted
|
|
|
|
// manifest.
|
|
|
|
if (mpd_notifier) {
|
|
|
|
uint32_t unused;
|
|
|
|
if (mpd_notifier->NotifyNewContainer(text_media_info, &unused)) {
|
|
|
|
mpd_notifier->Flush();
|
|
|
|
} else {
|
|
|
|
return Status(error::PARSER_FAILURE,
|
|
|
|
"Failed to process text file " + stream.input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packaging_params.output_media_info) {
|
|
|
|
VodMediaInfoDumpMuxerListener::WriteMediaInfoToFile(
|
|
|
|
text_media_info, stream.output + kMediaInfoSuffix);
|
|
|
|
}
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-23 15:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CreateAudioVideoJobs(
|
|
|
|
const std::vector<std::reference_wrapper<const StreamDescriptor>>& streams,
|
|
|
|
const PackagingParams& packaging_params,
|
|
|
|
KeySource* encryption_key_source,
|
2018-03-20 17:12:18 +00:00
|
|
|
SyncPointQueue* sync_points,
|
2017-12-12 22:13:47 +00:00
|
|
|
MuxerListenerFactory* muxer_listener_factory,
|
2017-12-21 22:53:24 +00:00
|
|
|
MuxerFactory* muxer_factory,
|
2017-12-12 21:05:12 +00:00
|
|
|
JobManager* job_manager) {
|
2017-12-12 22:13:47 +00:00
|
|
|
DCHECK(muxer_listener_factory);
|
|
|
|
DCHECK(muxer_factory);
|
2017-12-12 21:05:12 +00:00
|
|
|
DCHECK(job_manager);
|
2017-08-23 15:46:16 +00:00
|
|
|
|
|
|
|
// Demuxers are shared among all streams with the same input.
|
|
|
|
std::shared_ptr<Demuxer> demuxer;
|
2018-03-20 17:12:18 +00:00
|
|
|
// When |sync_points| is not null, there should be one CueAlignmentHandler per
|
|
|
|
// input. All CueAlignmentHandler shares the same |sync_points|, which allows
|
|
|
|
// sync points / cues to be aligned across streams, whether they are from the
|
|
|
|
// same input or not.
|
|
|
|
std::shared_ptr<CueAlignmentHandler> cue_aligner;
|
2017-08-23 15:46:16 +00:00
|
|
|
// Replicators are shared among all streams with the same input and stream
|
|
|
|
// selector.
|
2018-03-20 17:12:18 +00:00
|
|
|
std::shared_ptr<Replicator> replicator;
|
2017-08-23 15:46:16 +00:00
|
|
|
|
|
|
|
std::string previous_input;
|
|
|
|
std::string previous_selector;
|
|
|
|
|
|
|
|
for (const StreamDescriptor& stream : streams) {
|
2017-08-28 22:27:05 +00:00
|
|
|
// If we changed our input files, we need a new demuxer.
|
2018-01-10 00:04:40 +00:00
|
|
|
const bool new_input_file = stream.input != previous_input;
|
|
|
|
if (new_input_file) {
|
2017-12-21 22:53:24 +00:00
|
|
|
Status status = CreateDemuxer(stream, packaging_params, &demuxer);
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
2017-08-28 22:27:05 +00:00
|
|
|
|
2017-12-12 21:05:12 +00:00
|
|
|
job_manager->Add("RemuxJob", demuxer);
|
2018-01-10 00:04:40 +00:00
|
|
|
|
2018-03-20 17:12:18 +00:00
|
|
|
if (sync_points)
|
|
|
|
cue_aligner = std::make_shared<CueAlignmentHandler>(sync_points);
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
2017-08-28 22:27:05 +00:00
|
|
|
|
|
|
|
if (!stream.language.empty()) {
|
|
|
|
demuxer->SetLanguageOverride(stream.stream_selector, stream.language);
|
|
|
|
}
|
|
|
|
|
2018-01-10 00:04:40 +00:00
|
|
|
const bool new_stream =
|
|
|
|
new_input_file || previous_selector != stream.stream_selector;
|
2017-08-28 22:27:05 +00:00
|
|
|
previous_input = stream.input;
|
|
|
|
previous_selector = stream.stream_selector;
|
|
|
|
|
|
|
|
// If the stream has no output, then there is no reason setting-up the rest
|
|
|
|
// of the pipeline.
|
|
|
|
if (stream.output.empty() && stream.segment_template.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-02 00:22:35 +00:00
|
|
|
if (new_stream) {
|
2018-03-20 17:12:18 +00:00
|
|
|
auto chunker =
|
|
|
|
std::make_shared<ChunkingHandler>(packaging_params.chunking_params);
|
2017-08-28 22:27:05 +00:00
|
|
|
|
|
|
|
std::shared_ptr<MediaHandler> encryptor = CreateEncryptionHandler(
|
|
|
|
packaging_params, stream, encryption_key_source);
|
|
|
|
|
2018-01-10 00:04:40 +00:00
|
|
|
replicator = std::make_shared<Replicator>();
|
|
|
|
|
2017-08-28 22:27:05 +00:00
|
|
|
Status status;
|
2018-03-20 17:12:18 +00:00
|
|
|
if (cue_aligner) {
|
|
|
|
status.Update(demuxer->SetHandler(stream.stream_selector, cue_aligner));
|
|
|
|
status.Update(cue_aligner->AddHandler(chunker));
|
2017-11-21 21:24:13 +00:00
|
|
|
} else {
|
2017-08-28 22:27:05 +00:00
|
|
|
status.Update(demuxer->SetHandler(stream.stream_selector, chunker));
|
2017-11-21 21:24:13 +00:00
|
|
|
}
|
|
|
|
if (encryptor) {
|
2017-08-28 22:27:05 +00:00
|
|
|
status.Update(chunker->AddHandler(encryptor));
|
|
|
|
status.Update(encryptor->AddHandler(replicator));
|
|
|
|
} else {
|
|
|
|
status.Update(chunker->AddHandler(replicator));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream.language.empty()) {
|
|
|
|
demuxer->SetLanguageOverride(stream.stream_selector, stream.language);
|
|
|
|
}
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 23:55:10 +00:00
|
|
|
// Create the muxer (output) for this track.
|
2017-12-12 22:13:47 +00:00
|
|
|
std::unique_ptr<MuxerListener> muxer_listener =
|
|
|
|
muxer_listener_factory->CreateListener(ToMuxerListenerData(stream));
|
2017-12-21 22:53:24 +00:00
|
|
|
std::shared_ptr<Muxer> muxer =
|
2018-01-03 18:16:08 +00:00
|
|
|
muxer_factory->CreateMuxer(GetOutputFormat(stream), stream);
|
|
|
|
muxer->SetMuxerListener(std::move(muxer_listener));
|
2017-08-28 23:55:10 +00:00
|
|
|
|
|
|
|
if (!muxer) {
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Failed to create muxer for " +
|
2017-08-28 22:27:05 +00:00
|
|
|
stream.input + ":" +
|
|
|
|
stream.stream_selector);
|
2017-08-29 20:42:33 +00:00
|
|
|
}
|
2017-05-22 20:28:10 +00:00
|
|
|
|
2017-08-28 22:27:05 +00:00
|
|
|
std::shared_ptr<MediaHandler> trick_play;
|
|
|
|
if (stream.trick_play_factor) {
|
|
|
|
trick_play = std::make_shared<TrickPlayHandler>(stream.trick_play_factor);
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 22:27:05 +00:00
|
|
|
Status status;
|
|
|
|
if (trick_play) {
|
|
|
|
status.Update(replicator->AddHandler(trick_play));
|
|
|
|
status.Update(trick_play->AddHandler(muxer));
|
2017-05-22 20:28:10 +00:00
|
|
|
} else {
|
2017-08-28 22:27:05 +00:00
|
|
|
status.Update(replicator->AddHandler(muxer));
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!status.ok()) {
|
2017-08-23 17:30:43 +00:00
|
|
|
return status;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 15:46:16 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status CreateAllJobs(const std::vector<StreamDescriptor>& stream_descriptors,
|
|
|
|
const PackagingParams& packaging_params,
|
|
|
|
MpdNotifier* mpd_notifier,
|
2017-12-12 22:13:47 +00:00
|
|
|
KeySource* encryption_key_source,
|
2018-03-20 17:12:18 +00:00
|
|
|
SyncPointQueue* sync_points,
|
2017-12-12 22:13:47 +00:00
|
|
|
MuxerListenerFactory* muxer_listener_factory,
|
|
|
|
MuxerFactory* muxer_factory,
|
2017-12-12 21:05:12 +00:00
|
|
|
JobManager* job_manager) {
|
2017-12-12 22:13:47 +00:00
|
|
|
DCHECK(muxer_factory);
|
|
|
|
DCHECK(muxer_listener_factory);
|
2017-12-12 21:05:12 +00:00
|
|
|
DCHECK(job_manager);
|
2017-08-23 15:46:16 +00:00
|
|
|
|
|
|
|
// Group all streams based on which pipeline they will use.
|
|
|
|
std::vector<std::reference_wrapper<const StreamDescriptor>> text_streams;
|
|
|
|
std::vector<std::reference_wrapper<const StreamDescriptor>>
|
|
|
|
audio_video_streams;
|
|
|
|
|
|
|
|
for (const StreamDescriptor& stream : stream_descriptors) {
|
|
|
|
// TODO: Find a better way to determine what stream type a stream
|
|
|
|
// descriptor is as |stream_selector| may use an index. This would
|
|
|
|
// also allow us to use a simpler audio pipeline.
|
|
|
|
if (stream.stream_selector == "text") {
|
|
|
|
text_streams.push_back(stream);
|
|
|
|
} else {
|
|
|
|
audio_video_streams.push_back(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Audio/Video streams need to be in sorted order so that demuxers and trick
|
|
|
|
// play handlers get setup correctly.
|
|
|
|
std::sort(audio_video_streams.begin(), audio_video_streams.end(),
|
|
|
|
media::StreamDescriptorCompareFn);
|
|
|
|
|
2017-12-21 22:53:24 +00:00
|
|
|
Status status;
|
2018-03-20 17:12:18 +00:00
|
|
|
status.Update(CreateTextJobs(text_streams, packaging_params, sync_points,
|
2017-12-12 22:13:47 +00:00
|
|
|
muxer_listener_factory, muxer_factory,
|
|
|
|
mpd_notifier, job_manager));
|
|
|
|
status.Update(CreateAudioVideoJobs(
|
2018-03-20 17:12:18 +00:00
|
|
|
audio_video_streams, packaging_params, encryption_key_source, sync_points,
|
2017-12-12 22:13:47 +00:00
|
|
|
muxer_listener_factory, muxer_factory, job_manager));
|
|
|
|
|
2017-08-23 15:46:16 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize processing graph.
|
2017-12-12 21:05:12 +00:00
|
|
|
status.Update(job_manager->InitializeJobs());
|
2017-05-22 20:28:10 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace media
|
|
|
|
|
2017-06-13 22:16:08 +00:00
|
|
|
struct Packager::PackagerInternal {
|
2017-05-22 20:28:10 +00:00
|
|
|
media::FakeClock fake_clock;
|
|
|
|
std::unique_ptr<KeySource> encryption_key_source;
|
|
|
|
std::unique_ptr<MpdNotifier> mpd_notifier;
|
|
|
|
std::unique_ptr<hls::HlsNotifier> hls_notifier;
|
2017-07-17 18:15:07 +00:00
|
|
|
BufferCallbackParams buffer_callback_params;
|
2018-03-22 06:24:17 +00:00
|
|
|
std::unique_ptr<media::JobManager> job_manager;
|
2017-05-22 20:28:10 +00:00
|
|
|
};
|
|
|
|
|
2017-06-13 22:16:08 +00:00
|
|
|
Packager::Packager() {}
|
2017-05-22 20:28:10 +00:00
|
|
|
|
2017-06-13 22:16:08 +00:00
|
|
|
Packager::~Packager() {}
|
2017-05-22 20:28:10 +00:00
|
|
|
|
2017-06-13 22:16:08 +00:00
|
|
|
Status Packager::Initialize(
|
2017-05-22 20:28:10 +00:00
|
|
|
const PackagingParams& packaging_params,
|
|
|
|
const std::vector<StreamDescriptor>& stream_descriptors) {
|
2017-05-23 02:41:26 +00:00
|
|
|
// Needed by base::WorkedPool used in ThreadedIoFile.
|
|
|
|
static base::AtExitManager exit;
|
2017-05-22 20:28:10 +00:00
|
|
|
static media::LibcryptoThreading libcrypto_threading;
|
|
|
|
|
|
|
|
if (internal_)
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Already initialized.");
|
|
|
|
|
2017-08-29 17:54:29 +00:00
|
|
|
Status param_check =
|
|
|
|
media::ValidateParams(packaging_params, stream_descriptors);
|
|
|
|
if (!param_check.ok()) {
|
|
|
|
return param_check;
|
|
|
|
}
|
2017-05-22 20:28:10 +00:00
|
|
|
|
2017-05-23 02:41:26 +00:00
|
|
|
if (!packaging_params.test_params.injected_library_version.empty()) {
|
|
|
|
SetPackagerVersionForTesting(
|
|
|
|
packaging_params.test_params.injected_library_version);
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
std::unique_ptr<PackagerInternal> internal(new PackagerInternal);
|
|
|
|
|
|
|
|
// Create encryption key source if needed.
|
|
|
|
if (packaging_params.encryption_params.key_provider != KeyProvider::kNone) {
|
2017-07-05 23:47:55 +00:00
|
|
|
internal->encryption_key_source = CreateEncryptionKeySource(
|
|
|
|
static_cast<media::FourCC>(
|
|
|
|
packaging_params.encryption_params.protection_scheme),
|
|
|
|
packaging_params.encryption_params);
|
2017-05-22 20:28:10 +00:00
|
|
|
if (!internal->encryption_key_source)
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Failed to create key source.");
|
|
|
|
}
|
|
|
|
|
2017-07-17 18:15:07 +00:00
|
|
|
// Store callback params to make it available during packaging.
|
|
|
|
internal->buffer_callback_params = packaging_params.buffer_callback_params;
|
|
|
|
|
2017-12-12 22:13:47 +00:00
|
|
|
// Update MPD output and HLS output if callback param is specified.
|
2017-07-17 18:15:07 +00:00
|
|
|
MpdParams mpd_params = packaging_params.mpd_params;
|
|
|
|
HlsParams hls_params = packaging_params.hls_params;
|
|
|
|
if (internal->buffer_callback_params.write_func) {
|
|
|
|
mpd_params.mpd_output = File::MakeCallbackFileName(
|
|
|
|
internal->buffer_callback_params, mpd_params.mpd_output);
|
|
|
|
hls_params.master_playlist_output = File::MakeCallbackFileName(
|
|
|
|
internal->buffer_callback_params, hls_params.master_playlist_output);
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
if (!mpd_params.mpd_output.empty()) {
|
2017-08-02 20:37:47 +00:00
|
|
|
const bool on_demand_dash_profile =
|
|
|
|
stream_descriptors.begin()->segment_template.empty();
|
2017-12-19 20:02:28 +00:00
|
|
|
const MpdOptions mpd_options =
|
2017-07-17 18:15:07 +00:00
|
|
|
media::GetMpdOptions(on_demand_dash_profile, mpd_params);
|
2017-12-19 20:02:28 +00:00
|
|
|
internal->mpd_notifier.reset(new SimpleMpdNotifier(mpd_options));
|
2017-05-22 20:28:10 +00:00
|
|
|
if (!internal->mpd_notifier->Init()) {
|
|
|
|
LOG(ERROR) << "MpdNotifier failed to initialize.";
|
|
|
|
return Status(error::INVALID_ARGUMENT,
|
|
|
|
"Failed to initialize MpdNotifier.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hls_params.master_playlist_output.empty()) {
|
2018-01-17 23:43:41 +00:00
|
|
|
internal->hls_notifier.reset(new hls::SimpleHlsNotifier(hls_params));
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 06:24:17 +00:00
|
|
|
std::unique_ptr<SyncPointQueue> sync_points;
|
2018-03-20 17:12:18 +00:00
|
|
|
if (!packaging_params.ad_cue_generator_params.cue_points.empty()) {
|
2018-03-22 06:24:17 +00:00
|
|
|
sync_points.reset(
|
2018-03-20 17:12:18 +00:00
|
|
|
new SyncPointQueue(packaging_params.ad_cue_generator_params));
|
|
|
|
}
|
2018-03-22 06:24:17 +00:00
|
|
|
internal->job_manager.reset(new JobManager(std::move(sync_points)));
|
2018-03-20 17:12:18 +00:00
|
|
|
|
2017-08-23 15:46:16 +00:00
|
|
|
std::vector<StreamDescriptor> streams_for_jobs;
|
|
|
|
|
|
|
|
for (const StreamDescriptor& descriptor : stream_descriptors) {
|
|
|
|
// We may need to overwrite some values, so make a copy first.
|
|
|
|
StreamDescriptor copy = descriptor;
|
|
|
|
|
2017-09-30 00:45:00 +00:00
|
|
|
if (internal->buffer_callback_params.read_func) {
|
2017-08-23 15:46:16 +00:00
|
|
|
copy.input = File::MakeCallbackFileName(internal->buffer_callback_params,
|
|
|
|
descriptor.input);
|
2017-09-30 00:45:00 +00:00
|
|
|
}
|
2017-08-23 15:46:16 +00:00
|
|
|
|
2017-09-30 00:45:00 +00:00
|
|
|
if (internal->buffer_callback_params.write_func) {
|
2017-08-23 15:46:16 +00:00
|
|
|
copy.output = File::MakeCallbackFileName(internal->buffer_callback_params,
|
|
|
|
descriptor.output);
|
|
|
|
copy.segment_template = File::MakeCallbackFileName(
|
2017-09-30 00:45:00 +00:00
|
|
|
internal->buffer_callback_params, descriptor.segment_template);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update language to ISO_639_2 code if set.
|
2017-08-23 15:46:16 +00:00
|
|
|
if (!copy.language.empty()) {
|
|
|
|
copy.language = LanguageToISO_639_2(descriptor.language);
|
|
|
|
if (copy.language == "und") {
|
2017-09-30 00:45:00 +00:00
|
|
|
return Status(
|
|
|
|
error::INVALID_ARGUMENT,
|
|
|
|
"Unknown/invalid language specified: " + descriptor.language);
|
2017-07-17 18:15:07 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-23 15:46:16 +00:00
|
|
|
|
|
|
|
streams_for_jobs.push_back(copy);
|
2017-07-17 18:15:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 22:13:47 +00:00
|
|
|
media::MuxerFactory muxer_factory(packaging_params);
|
|
|
|
if (packaging_params.test_params.inject_fake_clock) {
|
|
|
|
muxer_factory.OverrideClock(&internal->fake_clock);
|
|
|
|
}
|
|
|
|
|
|
|
|
media::MuxerListenerFactory muxer_listener_factory(
|
|
|
|
packaging_params.output_media_info, internal->mpd_notifier.get(),
|
|
|
|
internal->hls_notifier.get());
|
|
|
|
|
2017-08-23 15:46:16 +00:00
|
|
|
Status status = media::CreateAllJobs(
|
2017-12-12 22:13:47 +00:00
|
|
|
streams_for_jobs, packaging_params, internal->mpd_notifier.get(),
|
2018-03-22 06:24:17 +00:00
|
|
|
internal->encryption_key_source.get(),
|
|
|
|
internal->job_manager->sync_points(), &muxer_listener_factory,
|
|
|
|
&muxer_factory, internal->job_manager.get());
|
2017-08-23 17:30:43 +00:00
|
|
|
|
2017-08-23 15:46:16 +00:00
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
2017-08-23 17:30:43 +00:00
|
|
|
|
2017-08-23 15:46:16 +00:00
|
|
|
internal_ = std::move(internal);
|
|
|
|
return Status::OK;
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 22:16:08 +00:00
|
|
|
Status Packager::Run() {
|
2017-05-22 20:28:10 +00:00
|
|
|
if (!internal_)
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Not yet initialized.");
|
2017-12-12 21:05:12 +00:00
|
|
|
|
2018-03-22 06:24:17 +00:00
|
|
|
Status status = internal_->job_manager->RunJobs();
|
2017-05-22 20:28:10 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
|
|
|
|
if (internal_->hls_notifier) {
|
|
|
|
if (!internal_->hls_notifier->Flush())
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Failed to flush Hls.");
|
|
|
|
}
|
|
|
|
if (internal_->mpd_notifier) {
|
|
|
|
if (!internal_->mpd_notifier->Flush())
|
|
|
|
return Status(error::INVALID_ARGUMENT, "Failed to flush Mpd.");
|
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-06-13 22:16:08 +00:00
|
|
|
void Packager::Cancel() {
|
2017-05-22 20:28:10 +00:00
|
|
|
if (!internal_) {
|
|
|
|
LOG(INFO) << "Not yet initialized. Return directly.";
|
|
|
|
return;
|
|
|
|
}
|
2018-03-22 06:24:17 +00:00
|
|
|
internal_->job_manager->CancelJobs();
|
2017-05-22 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 22:16:08 +00:00
|
|
|
std::string Packager::GetLibraryVersion() {
|
2017-05-23 02:41:26 +00:00
|
|
|
return GetPackagerVersion();
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:47:55 +00:00
|
|
|
std::string Packager::DefaultStreamLabelFunction(
|
|
|
|
int max_sd_pixels,
|
|
|
|
int max_hd_pixels,
|
|
|
|
int max_uhd1_pixels,
|
|
|
|
const EncryptionParams::EncryptedStreamAttributes& stream_attributes) {
|
|
|
|
if (stream_attributes.stream_type ==
|
|
|
|
EncryptionParams::EncryptedStreamAttributes::kAudio)
|
|
|
|
return "AUDIO";
|
|
|
|
if (stream_attributes.stream_type ==
|
|
|
|
EncryptionParams::EncryptedStreamAttributes::kVideo) {
|
|
|
|
const int pixels = stream_attributes.oneof.video.width *
|
|
|
|
stream_attributes.oneof.video.height;
|
2017-08-23 17:30:43 +00:00
|
|
|
if (pixels <= max_sd_pixels)
|
|
|
|
return "SD";
|
|
|
|
if (pixels <= max_hd_pixels)
|
|
|
|
return "HD";
|
|
|
|
if (pixels <= max_uhd1_pixels)
|
|
|
|
return "UHD1";
|
2017-07-05 23:47:55 +00:00
|
|
|
return "UHD2";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-05-22 20:28:10 +00:00
|
|
|
} // namespace shaka
|