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
|
2014-01-15 22:44:11 +00:00
|
|
|
|
2014-10-15 21:56:12 +00:00
|
|
|
#include "packager/app/packager_util.h"
|
|
|
|
|
2014-01-15 22:44:11 +00:00
|
|
|
#include <gflags/gflags.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/app/fixed_key_encryption_flags.h"
|
|
|
|
#include "packager/app/mpd_flags.h"
|
|
|
|
#include "packager/app/muxer_flags.h"
|
|
|
|
#include "packager/app/widevine_encryption_flags.h"
|
|
|
|
#include "packager/base/logging.h"
|
|
|
|
#include "packager/base/strings/string_number_conversions.h"
|
|
|
|
#include "packager/media/base/media_stream.h"
|
|
|
|
#include "packager/media/base/muxer.h"
|
|
|
|
#include "packager/media/base/muxer_options.h"
|
|
|
|
#include "packager/media/base/request_signer.h"
|
|
|
|
#include "packager/media/base/stream_info.h"
|
|
|
|
#include "packager/media/base/widevine_key_source.h"
|
|
|
|
#include "packager/media/file/file.h"
|
|
|
|
#include "packager/mpd/base/mpd_builder.h"
|
2014-01-15 22:44:11 +00:00
|
|
|
|
|
|
|
DEFINE_bool(dump_stream_info, false, "Dump demuxed stream info.");
|
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2014-01-15 22:44:11 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
void DumpStreamInfo(const std::vector<MediaStream*>& streams) {
|
2014-03-21 17:26:49 +00:00
|
|
|
printf("Found %zu stream(s).\n", streams.size());
|
2014-01-15 22:44:11 +00:00
|
|
|
for (size_t i = 0; i < streams.size(); ++i)
|
2014-03-21 17:26:49 +00:00
|
|
|
printf("Stream [%zu] %s\n", i, streams[i]->info()->ToString().c_str());
|
2014-01-15 22:44:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
scoped_ptr<RequestSigner> CreateSigner() {
|
|
|
|
scoped_ptr<RequestSigner> signer;
|
2014-10-13 22:06:48 +00:00
|
|
|
|
|
|
|
if (!FLAGS_aes_signing_key.empty()) {
|
|
|
|
signer.reset(AesRequestSigner::CreateSigner(
|
|
|
|
FLAGS_signer, FLAGS_aes_signing_key, FLAGS_aes_signing_iv));
|
|
|
|
if (!signer) {
|
|
|
|
LOG(ERROR) << "Cannot create an AES signer object from '"
|
|
|
|
<< FLAGS_aes_signing_key << "':'" << FLAGS_aes_signing_iv
|
|
|
|
<< "'.";
|
|
|
|
return scoped_ptr<RequestSigner>();
|
|
|
|
}
|
|
|
|
} else if (!FLAGS_rsa_signing_key_path.empty()) {
|
|
|
|
std::string rsa_private_key;
|
|
|
|
if (!File::ReadFileToString(FLAGS_rsa_signing_key_path.c_str(),
|
|
|
|
&rsa_private_key)) {
|
|
|
|
LOG(ERROR) << "Failed to read from '" << FLAGS_rsa_signing_key_path
|
|
|
|
<< "'.";
|
|
|
|
return scoped_ptr<RequestSigner>();
|
|
|
|
}
|
|
|
|
signer.reset(RsaRequestSigner::CreateSigner(FLAGS_signer, rsa_private_key));
|
|
|
|
if (!signer) {
|
|
|
|
LOG(ERROR) << "Cannot create a RSA signer object from '"
|
|
|
|
<< FLAGS_rsa_signing_key_path << "'.";
|
|
|
|
return scoped_ptr<RequestSigner>();
|
2014-01-15 22:44:11 +00:00
|
|
|
}
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
|
|
|
return signer.Pass();
|
|
|
|
}
|
2014-01-15 22:44:11 +00:00
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
scoped_ptr<KeySource> CreateEncryptionKeySource() {
|
|
|
|
scoped_ptr<KeySource> encryption_key_source;
|
|
|
|
if (FLAGS_enable_widevine_encryption) {
|
2014-10-15 00:47:25 +00:00
|
|
|
scoped_ptr<WidevineKeySource> widevine_key_source(
|
|
|
|
new WidevineKeySource(FLAGS_key_server_url));
|
|
|
|
if (!FLAGS_signer.empty()) {
|
|
|
|
scoped_ptr<RequestSigner> request_signer(CreateSigner());
|
|
|
|
if (!request_signer)
|
|
|
|
return scoped_ptr<KeySource>();
|
|
|
|
widevine_key_source->set_signer(request_signer.Pass());
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> content_id;
|
2014-08-20 23:51:15 +00:00
|
|
|
if (!base::HexStringToBytes(FLAGS_content_id, &content_id)) {
|
|
|
|
LOG(ERROR) << "Invalid content_id hex string specified.";
|
|
|
|
return scoped_ptr<KeySource>();
|
|
|
|
}
|
2014-10-15 00:47:25 +00:00
|
|
|
Status status = widevine_key_source->FetchKeys(content_id, FLAGS_policy);
|
2014-05-08 23:34:45 +00:00
|
|
|
if (!status.ok()) {
|
2014-08-20 23:51:15 +00:00
|
|
|
LOG(ERROR) << "Widevine encryption key source failed to fetch keys: "
|
2014-05-08 23:34:45 +00:00
|
|
|
<< status.ToString();
|
2014-08-20 23:51:15 +00:00
|
|
|
return scoped_ptr<KeySource>();
|
2014-05-08 23:34:45 +00:00
|
|
|
}
|
2014-10-15 00:47:25 +00:00
|
|
|
encryption_key_source = widevine_key_source.Pass();
|
2014-01-15 22:44:11 +00:00
|
|
|
} else if (FLAGS_enable_fixed_key_encryption) {
|
2014-08-20 23:51:15 +00:00
|
|
|
encryption_key_source = KeySource::CreateFromHexStrings(
|
2015-09-25 22:48:18 +00:00
|
|
|
FLAGS_key_id, FLAGS_key, FLAGS_pssh, FLAGS_iv);
|
2014-01-15 22:44:11 +00:00
|
|
|
}
|
2014-04-16 01:09:32 +00:00
|
|
|
return encryption_key_source.Pass();
|
2014-01-15 22:44:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
scoped_ptr<KeySource> CreateDecryptionKeySource() {
|
|
|
|
scoped_ptr<KeySource> decryption_key_source;
|
|
|
|
if (FLAGS_enable_widevine_decryption) {
|
2014-10-15 00:47:25 +00:00
|
|
|
scoped_ptr<WidevineKeySource> widevine_key_source(
|
|
|
|
new WidevineKeySource(FLAGS_key_server_url));
|
|
|
|
if (!FLAGS_signer.empty()) {
|
|
|
|
scoped_ptr<RequestSigner> request_signer(CreateSigner());
|
|
|
|
if (!request_signer)
|
|
|
|
return scoped_ptr<KeySource>();
|
|
|
|
widevine_key_source->set_signer(request_signer.Pass());
|
|
|
|
}
|
|
|
|
|
|
|
|
decryption_key_source = widevine_key_source.Pass();
|
2014-08-25 22:51:19 +00:00
|
|
|
} else if (FLAGS_enable_fixed_key_decryption) {
|
2014-10-15 00:47:25 +00:00
|
|
|
decryption_key_source =
|
|
|
|
KeySource::CreateFromHexStrings(FLAGS_key_id, FLAGS_key, "", "");
|
2014-08-20 23:51:15 +00:00
|
|
|
}
|
|
|
|
return decryption_key_source.Pass();
|
|
|
|
}
|
|
|
|
|
2014-06-26 17:42:12 +00:00
|
|
|
bool AssignFlagsFromProfile() {
|
|
|
|
bool single_segment = FLAGS_single_segment;
|
|
|
|
if (FLAGS_profile == "on-demand") {
|
|
|
|
single_segment = true;
|
|
|
|
} else if (FLAGS_profile == "live") {
|
|
|
|
single_segment = false;
|
|
|
|
} else if (FLAGS_profile != "") {
|
|
|
|
fprintf(stderr, "ERROR: --profile '%s' is not supported.\n",
|
|
|
|
FLAGS_profile.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FLAGS_single_segment != single_segment) {
|
|
|
|
FLAGS_single_segment = single_segment;
|
|
|
|
fprintf(stdout, "Profile %s: set --single_segment to %s.\n",
|
|
|
|
FLAGS_profile.c_str(), single_segment ? "true" : "false");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:44:11 +00:00
|
|
|
bool GetMuxerOptions(MuxerOptions* muxer_options) {
|
|
|
|
DCHECK(muxer_options);
|
|
|
|
|
|
|
|
muxer_options->single_segment = FLAGS_single_segment;
|
|
|
|
muxer_options->segment_duration = FLAGS_segment_duration;
|
|
|
|
muxer_options->fragment_duration = FLAGS_fragment_duration;
|
|
|
|
muxer_options->segment_sap_aligned = FLAGS_segment_sap_aligned;
|
|
|
|
muxer_options->fragment_sap_aligned = FLAGS_fragment_sap_aligned;
|
|
|
|
muxer_options->num_subsegments_per_sidx = FLAGS_num_subsegments_per_sidx;
|
2014-05-08 21:02:36 +00:00
|
|
|
muxer_options->temp_dir = FLAGS_temp_dir;
|
2014-01-15 22:44:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-26 01:33:09 +00:00
|
|
|
bool GetMpdOptions(MpdOptions* mpd_options) {
|
|
|
|
DCHECK(mpd_options);
|
|
|
|
|
|
|
|
mpd_options->availability_time_offset = FLAGS_availability_time_offset;
|
|
|
|
mpd_options->minimum_update_period = FLAGS_minimum_update_period;
|
|
|
|
mpd_options->min_buffer_time = FLAGS_min_buffer_time;
|
|
|
|
mpd_options->time_shift_buffer_depth = FLAGS_time_shift_buffer_depth;
|
|
|
|
mpd_options->suggested_presentation_delay =
|
|
|
|
FLAGS_suggested_presentation_delay;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:44:11 +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);
|
|
|
|
}
|
|
|
|
|
2014-05-08 21:02:36 +00:00
|
|
|
bool AddStreamToMuxer(const std::vector<MediaStream*>& streams,
|
|
|
|
const std::string& stream_selector,
|
2015-02-02 19:27:32 +00:00
|
|
|
const std::string& language_override,
|
2014-05-08 21:02:36 +00:00
|
|
|
Muxer* muxer) {
|
2014-01-15 22:44:11 +00:00
|
|
|
DCHECK(muxer);
|
|
|
|
|
2014-03-17 18:36:41 +00:00
|
|
|
MediaStream* stream = NULL;
|
2014-05-08 21:02:36 +00:00
|
|
|
if (stream_selector == "video") {
|
2014-03-17 18:36:41 +00:00
|
|
|
stream = FindFirstVideoStream(streams);
|
2014-05-08 21:02:36 +00:00
|
|
|
} else if (stream_selector == "audio") {
|
2014-03-17 18:36:41 +00:00
|
|
|
stream = FindFirstAudioStream(streams);
|
|
|
|
} else {
|
2014-05-08 21:02:36 +00:00
|
|
|
// Expect stream_selector to be a zero based stream id.
|
2014-03-17 18:36:41 +00:00
|
|
|
size_t stream_id;
|
2014-05-08 21:02:36 +00:00
|
|
|
if (!base::StringToSizeT(stream_selector, &stream_id) ||
|
2014-03-17 18:36:41 +00:00
|
|
|
stream_id >= streams.size()) {
|
2014-05-08 21:02:36 +00:00
|
|
|
LOG(ERROR) << "Invalid argument --stream=" << stream_selector << "; "
|
2014-03-17 18:36:41 +00:00
|
|
|
<< "should be 'audio', 'video', or a number within [0, "
|
|
|
|
<< streams.size() - 1 << "].";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stream = streams[stream_id];
|
|
|
|
DCHECK(stream);
|
2014-01-15 22:44:11 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 21:02:36 +00:00
|
|
|
// This could occur only if stream_selector=audio|video and the corresponding
|
2014-03-17 18:36:41 +00:00
|
|
|
// stream does not exist in the input.
|
2014-01-15 22:44:11 +00:00
|
|
|
if (!stream) {
|
2014-05-08 21:02:36 +00:00
|
|
|
LOG(ERROR) << "No " << stream_selector << " stream found in the input.";
|
2014-01-15 22:44:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-02 19:27:32 +00:00
|
|
|
|
|
|
|
if (!language_override.empty()) {
|
|
|
|
stream->info()->set_language(language_override);
|
|
|
|
}
|
|
|
|
|
2014-04-09 17:34:55 +00:00
|
|
|
muxer->AddStream(stream);
|
2014-01-15 22:44:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|