7 #include "packager/app/packager_util.h"
9 #include <gflags/gflags.h>
12 #include "packager/app/fixed_key_encryption_flags.h"
13 #include "packager/app/playready_key_encryption_flags.h"
14 #include "packager/app/mpd_flags.h"
15 #include "packager/app/muxer_flags.h"
16 #include "packager/app/widevine_encryption_flags.h"
17 #include "packager/base/logging.h"
18 #include "packager/base/strings/string_number_conversions.h"
19 #include "packager/media/base/fixed_key_source.h"
20 #include "packager/media/base/muxer_options.h"
21 #include "packager/media/base/playready_key_source.h"
22 #include "packager/media/base/request_signer.h"
23 #include "packager/media/base/widevine_key_source.h"
24 #include "packager/media/chunking/chunking_handler.h"
25 #include "packager/media/crypto/encryption_handler.h"
26 #include "packager/media/file/file.h"
27 #include "packager/mpd/base/mpd_options.h"
29 DEFINE_bool(mp4_use_decoding_timestamp_in_timeline,
31 "If set, decoding timestamp instead of presentation timestamp will "
32 "be used when generating media timeline, e.g. timestamps in sidx "
33 "and mpd. This is to workaround a Chromium bug that decoding "
34 "timestamp is used in buffered range, https://crbug.com/398130.");
35 DEFINE_bool(dump_stream_info,
false,
"Dump demuxed stream info.");
41 FourCC GetProtectionScheme(
const std::string& protection_scheme) {
42 if (protection_scheme ==
"cenc") {
44 }
else if (protection_scheme ==
"cens") {
46 }
else if (protection_scheme ==
"cbc1") {
48 }
else if (protection_scheme ==
"cbcs") {
51 LOG(ERROR) <<
"Unknown protection scheme: " << protection_scheme;
58 std::unique_ptr<RequestSigner> CreateSigner() {
59 std::unique_ptr<RequestSigner> signer;
61 if (!FLAGS_aes_signing_key.empty()) {
63 FLAGS_signer, FLAGS_aes_signing_key, FLAGS_aes_signing_iv));
65 LOG(ERROR) <<
"Cannot create an AES signer object from '"
66 << FLAGS_aes_signing_key <<
"':'" << FLAGS_aes_signing_iv
68 return std::unique_ptr<RequestSigner>();
70 }
else if (!FLAGS_rsa_signing_key_path.empty()) {
71 std::string rsa_private_key;
74 LOG(ERROR) <<
"Failed to read from '" << FLAGS_rsa_signing_key_path
76 return std::unique_ptr<RequestSigner>();
80 LOG(ERROR) <<
"Cannot create a RSA signer object from '"
81 << FLAGS_rsa_signing_key_path <<
"'.";
82 return std::unique_ptr<RequestSigner>();
88 std::unique_ptr<KeySource> CreateEncryptionKeySource() {
89 std::unique_ptr<KeySource> encryption_key_source;
90 if (FLAGS_enable_widevine_encryption) {
91 std::unique_ptr<WidevineKeySource> widevine_key_source(
92 new WidevineKeySource(FLAGS_key_server_url, FLAGS_include_common_pssh));
93 if (!FLAGS_signer.empty()) {
94 std::unique_ptr<RequestSigner> request_signer(CreateSigner());
96 return std::unique_ptr<KeySource>();
97 widevine_key_source->set_signer(std::move(request_signer));
100 std::vector<uint8_t> content_id;
101 if (!base::HexStringToBytes(FLAGS_content_id, &content_id)) {
102 LOG(ERROR) <<
"Invalid content_id hex string specified.";
103 return std::unique_ptr<KeySource>();
105 Status status = widevine_key_source->FetchKeys(content_id, FLAGS_policy);
107 LOG(ERROR) <<
"Widevine encryption key source failed to fetch keys: "
108 << status.ToString();
109 return std::unique_ptr<KeySource>();
111 encryption_key_source = std::move(widevine_key_source);
112 }
else if (FLAGS_enable_fixed_key_encryption) {
114 FLAGS_key_id, FLAGS_key, FLAGS_pssh, FLAGS_iv);
115 }
else if (FLAGS_enable_playready_encryption) {
116 if (!FLAGS_playready_key_id.empty() && !FLAGS_playready_key.empty()) {
118 FLAGS_playready_key_id, FLAGS_playready_key);
119 }
else if (!FLAGS_playready_server_url.empty() &&
120 !FLAGS_program_identifier.empty()) {
121 std::unique_ptr<PlayReadyKeySource> playready_key_source;
122 if (!FLAGS_client_cert_file.empty() &&
123 !FLAGS_client_cert_private_key_file.empty() &&
124 !FLAGS_client_cert_private_key_password.empty()) {
125 playready_key_source.reset(
new PlayReadyKeySource(
126 FLAGS_playready_server_url,
127 FLAGS_client_cert_file,
128 FLAGS_client_cert_private_key_file,
129 FLAGS_client_cert_private_key_password));
131 playready_key_source.reset(
new PlayReadyKeySource(
132 FLAGS_playready_server_url));
134 if (!FLAGS_ca_file.empty()) {
135 playready_key_source->
SetCaFile(FLAGS_ca_file);
137 playready_key_source->FetchKeysWithProgramIdentifier(FLAGS_program_identifier);
138 encryption_key_source = std::move(playready_key_source);
140 LOG(ERROR) <<
"Error creating PlayReady key source.";
141 return std::unique_ptr<KeySource>();
144 return encryption_key_source;
147 std::unique_ptr<KeySource> CreateDecryptionKeySource() {
148 std::unique_ptr<KeySource> decryption_key_source;
149 if (FLAGS_enable_widevine_decryption) {
150 std::unique_ptr<WidevineKeySource> widevine_key_source(
151 new WidevineKeySource(FLAGS_key_server_url, FLAGS_include_common_pssh));
152 if (!FLAGS_signer.empty()) {
153 std::unique_ptr<RequestSigner> request_signer(CreateSigner());
155 return std::unique_ptr<KeySource>();
156 widevine_key_source->set_signer(std::move(request_signer));
159 decryption_key_source = std::move(widevine_key_source);
160 }
else if (FLAGS_enable_fixed_key_decryption) {
161 const char kNoPssh[] =
"";
162 const char kNoIv[] =
"";
164 FLAGS_key_id, FLAGS_key, kNoPssh, kNoIv);
166 return decryption_key_source;
169 ChunkingOptions GetChunkingOptions() {
170 ChunkingOptions chunking_options;
172 chunking_options.subsegment_duration_in_seconds = FLAGS_fragment_duration;
173 chunking_options.segment_sap_aligned = FLAGS_segment_sap_aligned;
174 chunking_options.subsegment_sap_aligned = FLAGS_fragment_sap_aligned;
175 return chunking_options;
178 EncryptionOptions GetEncryptionOptions() {
179 EncryptionOptions encryption_options;
181 encryption_options.protection_scheme =
182 GetProtectionScheme(FLAGS_protection_scheme);
183 encryption_options.max_sd_pixels = FLAGS_max_sd_pixels;
184 encryption_options.max_hd_pixels = FLAGS_max_hd_pixels;
185 encryption_options.max_uhd1_pixels = FLAGS_max_uhd1_pixels;
186 encryption_options.crypto_period_duration_in_seconds =
187 FLAGS_crypto_period_duration;
188 return encryption_options;
191 MuxerOptions GetMuxerOptions() {
192 MuxerOptions muxer_options;
194 muxer_options.webm_subsample_encryption = FLAGS_webm_subsample_encryption;
195 if (FLAGS_mp4_use_decoding_timestamp_in_timeline) {
196 LOG(WARNING) <<
"Flag --mp4_use_decoding_timestamp_in_timeline is set. "
197 "Note that it is a temporary hack to workaround Chromium "
198 "bug https://crbug.com/398130. The flag may be removed "
199 "when the Chromium bug is fixed.";
201 muxer_options.mp4_use_decoding_timestamp_in_timeline =
202 FLAGS_mp4_use_decoding_timestamp_in_timeline;
203 muxer_options.temp_dir = FLAGS_temp_dir;
204 return muxer_options;
207 MpdOptions GetMpdOptions(
bool on_demand_profile) {
208 MpdOptions mpd_options;
209 mpd_options.dash_profile =
210 on_demand_profile ? DashProfile::kOnDemand : DashProfile::kLive;
211 mpd_options.mpd_type = (on_demand_profile || FLAGS_generate_static_mpd)
214 mpd_options.availability_time_offset = FLAGS_availability_time_offset;
215 mpd_options.minimum_update_period = FLAGS_minimum_update_period;
216 mpd_options.min_buffer_time = FLAGS_min_buffer_time;
217 mpd_options.time_shift_buffer_depth = FLAGS_time_shift_buffer_depth;
218 mpd_options.suggested_presentation_delay = FLAGS_suggested_presentation_delay;
219 mpd_options.default_language = FLAGS_default_language;