DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
packager_util.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/app/packager_util.h"
8 
9 #include <gflags/gflags.h>
10 #include <iostream>
11 
12 #include "packager/app/crypto_flags.h"
13 #include "packager/app/fixed_key_encryption_flags.h"
14 #include "packager/app/playready_key_encryption_flags.h"
15 #include "packager/app/mpd_flags.h"
16 #include "packager/app/muxer_flags.h"
17 #include "packager/app/widevine_encryption_flags.h"
18 #include "packager/base/logging.h"
19 #include "packager/base/strings/string_number_conversions.h"
20 #include "packager/media/base/fixed_key_source.h"
21 #include "packager/media/base/muxer_options.h"
22 #include "packager/media/base/playready_key_source.h"
23 #include "packager/media/base/request_signer.h"
24 #include "packager/media/base/widevine_key_source.h"
25 #include "packager/media/chunking/chunking_handler.h"
26 #include "packager/media/crypto/encryption_handler.h"
27 #include "packager/media/file/file.h"
28 #include "packager/mpd/base/mpd_options.h"
29 
30 DEFINE_bool(mp4_use_decoding_timestamp_in_timeline,
31  false,
32  "If set, decoding timestamp instead of presentation timestamp will "
33  "be used when generating media timeline, e.g. timestamps in sidx "
34  "and mpd. This is to workaround a Chromium bug that decoding "
35  "timestamp is used in buffered range, https://crbug.com/398130.");
36 DEFINE_bool(dump_stream_info, false, "Dump demuxed stream info.");
37 
38 namespace shaka {
39 namespace media {
40 namespace {
41 
42 FourCC GetProtectionScheme(const std::string& protection_scheme) {
43  if (protection_scheme == "cenc") {
44  return FOURCC_cenc;
45  } else if (protection_scheme == "cens") {
46  return FOURCC_cens;
47  } else if (protection_scheme == "cbc1") {
48  return FOURCC_cbc1;
49  } else if (protection_scheme == "cbcs") {
50  return FOURCC_cbcs;
51  } else {
52  LOG(ERROR) << "Unknown protection scheme: " << protection_scheme;
53  return FOURCC_NULL;
54  }
55 }
56 
57 } // namespace
58 
59 std::unique_ptr<RequestSigner> CreateSigner() {
60  std::unique_ptr<RequestSigner> signer;
61 
62  if (!FLAGS_aes_signing_key.empty()) {
63  signer.reset(AesRequestSigner::CreateSigner(
64  FLAGS_signer, FLAGS_aes_signing_key, FLAGS_aes_signing_iv));
65  if (!signer) {
66  LOG(ERROR) << "Cannot create an AES signer object from '"
67  << FLAGS_aes_signing_key << "':'" << FLAGS_aes_signing_iv
68  << "'.";
69  return std::unique_ptr<RequestSigner>();
70  }
71  } else if (!FLAGS_rsa_signing_key_path.empty()) {
72  std::string rsa_private_key;
73  if (!File::ReadFileToString(FLAGS_rsa_signing_key_path.c_str(),
74  &rsa_private_key)) {
75  LOG(ERROR) << "Failed to read from '" << FLAGS_rsa_signing_key_path
76  << "'.";
77  return std::unique_ptr<RequestSigner>();
78  }
79  signer.reset(RsaRequestSigner::CreateSigner(FLAGS_signer, rsa_private_key));
80  if (!signer) {
81  LOG(ERROR) << "Cannot create a RSA signer object from '"
82  << FLAGS_rsa_signing_key_path << "'.";
83  return std::unique_ptr<RequestSigner>();
84  }
85  }
86  return signer;
87 }
88 
89 std::unique_ptr<KeySource> CreateEncryptionKeySource() {
90  std::unique_ptr<KeySource> encryption_key_source;
91  if (FLAGS_enable_widevine_encryption) {
92  std::unique_ptr<WidevineKeySource> widevine_key_source(
93  new WidevineKeySource(FLAGS_key_server_url, FLAGS_include_common_pssh));
94  if (!FLAGS_signer.empty()) {
95  std::unique_ptr<RequestSigner> request_signer(CreateSigner());
96  if (!request_signer)
97  return std::unique_ptr<KeySource>();
98  widevine_key_source->set_signer(std::move(request_signer));
99  }
100 
101  std::vector<uint8_t> content_id;
102  if (!base::HexStringToBytes(FLAGS_content_id, &content_id)) {
103  LOG(ERROR) << "Invalid content_id hex string specified.";
104  return std::unique_ptr<KeySource>();
105  }
106  Status status = widevine_key_source->FetchKeys(content_id, FLAGS_policy);
107  if (!status.ok()) {
108  LOG(ERROR) << "Widevine encryption key source failed to fetch keys: "
109  << status.ToString();
110  return std::unique_ptr<KeySource>();
111  }
112  encryption_key_source = std::move(widevine_key_source);
113  } else if (FLAGS_enable_fixed_key_encryption) {
114  encryption_key_source = FixedKeySource::CreateFromHexStrings(
115  FLAGS_key_id, FLAGS_key, FLAGS_pssh, FLAGS_iv);
116  } else if (FLAGS_enable_playready_encryption) {
117  if (!FLAGS_playready_key_id.empty() && !FLAGS_playready_key.empty()) {
118  encryption_key_source = PlayReadyKeySource::CreateFromKeyAndKeyId(
119  FLAGS_playready_key_id, FLAGS_playready_key);
120  } else if (!FLAGS_playready_server_url.empty() &&
121  !FLAGS_program_identifier.empty()) {
122  std::unique_ptr<PlayReadyKeySource> playready_key_source;
123  if (!FLAGS_client_cert_file.empty() &&
124  !FLAGS_client_cert_private_key_file.empty() &&
125  !FLAGS_client_cert_private_key_password.empty()) {
126  playready_key_source.reset(new PlayReadyKeySource(
127  FLAGS_playready_server_url,
128  FLAGS_client_cert_file,
129  FLAGS_client_cert_private_key_file,
130  FLAGS_client_cert_private_key_password));
131  } else {
132  playready_key_source.reset(new PlayReadyKeySource(
133  FLAGS_playready_server_url));
134  }
135  if (!FLAGS_ca_file.empty()) {
136  playready_key_source->SetCaFile(FLAGS_ca_file);
137  }
138  playready_key_source->FetchKeysWithProgramIdentifier(FLAGS_program_identifier);
139  encryption_key_source = std::move(playready_key_source);
140  } else {
141  LOG(ERROR) << "Error creating PlayReady key source.";
142  return std::unique_ptr<KeySource>();
143  }
144  }
145  return encryption_key_source;
146 }
147 
148 std::unique_ptr<KeySource> CreateDecryptionKeySource() {
149  std::unique_ptr<KeySource> decryption_key_source;
150  if (FLAGS_enable_widevine_decryption) {
151  std::unique_ptr<WidevineKeySource> widevine_key_source(
152  new WidevineKeySource(FLAGS_key_server_url, FLAGS_include_common_pssh));
153  if (!FLAGS_signer.empty()) {
154  std::unique_ptr<RequestSigner> request_signer(CreateSigner());
155  if (!request_signer)
156  return std::unique_ptr<KeySource>();
157  widevine_key_source->set_signer(std::move(request_signer));
158  }
159 
160  decryption_key_source = std::move(widevine_key_source);
161  } else if (FLAGS_enable_fixed_key_decryption) {
162  const char kNoPssh[] = "";
163  const char kNoIv[] = "";
164  decryption_key_source = FixedKeySource::CreateFromHexStrings(
165  FLAGS_key_id, FLAGS_key, kNoPssh, kNoIv);
166  }
167  return decryption_key_source;
168 }
169 
170 ChunkingOptions GetChunkingOptions() {
171  ChunkingOptions chunking_options;
172  chunking_options.segment_duration_in_seconds = FLAGS_segment_duration;
173  chunking_options.subsegment_duration_in_seconds = FLAGS_fragment_duration;
174  chunking_options.segment_sap_aligned = FLAGS_segment_sap_aligned;
175  chunking_options.subsegment_sap_aligned = FLAGS_fragment_sap_aligned;
176  return chunking_options;
177 }
178 
179 EncryptionOptions GetEncryptionOptions() {
180  EncryptionOptions encryption_options;
181  encryption_options.clear_lead_in_seconds = FLAGS_clear_lead;
182  encryption_options.protection_scheme =
183  GetProtectionScheme(FLAGS_protection_scheme);
184  encryption_options.max_sd_pixels = FLAGS_max_sd_pixels;
185  encryption_options.max_hd_pixels = FLAGS_max_hd_pixels;
186  encryption_options.max_uhd1_pixels = FLAGS_max_uhd1_pixels;
187  encryption_options.crypto_period_duration_in_seconds =
188  FLAGS_crypto_period_duration;
189  encryption_options.vp9_subsample_encryption = FLAGS_vp9_subsample_encryption;
190  return encryption_options;
191 }
192 
193 MuxerOptions GetMuxerOptions() {
194  MuxerOptions muxer_options;
195  muxer_options.num_subsegments_per_sidx = FLAGS_num_subsegments_per_sidx;
196  muxer_options.mp4_include_pssh_in_stream = FLAGS_mp4_include_pssh_in_stream;
197  if (FLAGS_mp4_use_decoding_timestamp_in_timeline) {
198  LOG(WARNING) << "Flag --mp4_use_decoding_timestamp_in_timeline is set. "
199  "Note that it is a temporary hack to workaround Chromium "
200  "bug https://crbug.com/398130. The flag may be removed "
201  "when the Chromium bug is fixed.";
202  }
203  muxer_options.mp4_use_decoding_timestamp_in_timeline =
204  FLAGS_mp4_use_decoding_timestamp_in_timeline;
205  muxer_options.temp_dir = FLAGS_temp_dir;
206  return muxer_options;
207 }
208 
209 MpdOptions GetMpdOptions(bool on_demand_profile) {
210  MpdOptions mpd_options;
211  mpd_options.dash_profile =
212  on_demand_profile ? DashProfile::kOnDemand : DashProfile::kLive;
213  mpd_options.mpd_type = (on_demand_profile || FLAGS_generate_static_mpd)
214  ? MpdType::kStatic
215  : MpdType::kDynamic;
216  mpd_options.availability_time_offset = FLAGS_availability_time_offset;
217  mpd_options.minimum_update_period = FLAGS_minimum_update_period;
218  mpd_options.min_buffer_time = FLAGS_min_buffer_time;
219  mpd_options.time_shift_buffer_depth = FLAGS_time_shift_buffer_depth;
220  mpd_options.suggested_presentation_delay = FLAGS_suggested_presentation_delay;
221  mpd_options.default_language = FLAGS_default_language;
222  return mpd_options;
223 }
224 
225 } // namespace media
226 } // namespace shaka
static RsaRequestSigner * CreateSigner(const std::string &signer_name, const std::string &pkcs1_rsa_key)
static AesRequestSigner * CreateSigner(const std::string &signer_name, const std::string &aes_key_hex, const std::string &iv_hex)
static std::unique_ptr< FixedKeySource > CreateFromHexStrings(const std::string &key_id_hex, const std::string &key_hex, const std::string &pssh_boxes_hex, const std::string &iv_hex)
void SetCaFile(const std::string &ca_file)
Sets the Certificate Authority file for validating self-signed certificates.
double clear_lead_in_seconds
Clear lead duration in seconds.
double segment_duration_in_seconds
Segment duration in seconds.
static bool ReadFileToString(const char *file_name, std::string *contents)
Definition: file.cc:185
static std::unique_ptr< PlayReadyKeySource > CreateFromKeyAndKeyId(const std::string &key_id_hex, const std::string &key_hex)