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/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/file/file.h"
26 #include "packager/mpd/base/mpd_options.h"
27 
28 DEFINE_bool(mp4_use_decoding_timestamp_in_timeline,
29  false,
30  "If set, decoding timestamp instead of presentation timestamp will "
31  "be used when generating media timeline, e.g. timestamps in sidx "
32  "and mpd. This is to workaround a Chromium bug that decoding "
33  "timestamp is used in buffered range, https://crbug.com/398130.");
34 DEFINE_bool(dump_stream_info, false, "Dump demuxed stream info.");
35 
36 namespace shaka {
37 namespace media {
38 
39 std::unique_ptr<RequestSigner> CreateSigner() {
40  std::unique_ptr<RequestSigner> signer;
41 
42  if (!FLAGS_aes_signing_key.empty()) {
43  signer.reset(AesRequestSigner::CreateSigner(
44  FLAGS_signer, FLAGS_aes_signing_key, FLAGS_aes_signing_iv));
45  if (!signer) {
46  LOG(ERROR) << "Cannot create an AES signer object from '"
47  << FLAGS_aes_signing_key << "':'" << FLAGS_aes_signing_iv
48  << "'.";
49  return std::unique_ptr<RequestSigner>();
50  }
51  } else if (!FLAGS_rsa_signing_key_path.empty()) {
52  std::string rsa_private_key;
53  if (!File::ReadFileToString(FLAGS_rsa_signing_key_path.c_str(),
54  &rsa_private_key)) {
55  LOG(ERROR) << "Failed to read from '" << FLAGS_rsa_signing_key_path
56  << "'.";
57  return std::unique_ptr<RequestSigner>();
58  }
59  signer.reset(RsaRequestSigner::CreateSigner(FLAGS_signer, rsa_private_key));
60  if (!signer) {
61  LOG(ERROR) << "Cannot create a RSA signer object from '"
62  << FLAGS_rsa_signing_key_path << "'.";
63  return std::unique_ptr<RequestSigner>();
64  }
65  }
66  return signer;
67 }
68 
69 std::unique_ptr<KeySource> CreateEncryptionKeySource() {
70  std::unique_ptr<KeySource> encryption_key_source;
71  if (FLAGS_enable_widevine_encryption) {
72  std::unique_ptr<WidevineKeySource> widevine_key_source(
73  new WidevineKeySource(FLAGS_key_server_url, FLAGS_include_common_pssh));
74  if (!FLAGS_signer.empty()) {
75  std::unique_ptr<RequestSigner> request_signer(CreateSigner());
76  if (!request_signer)
77  return std::unique_ptr<KeySource>();
78  widevine_key_source->set_signer(std::move(request_signer));
79  }
80 
81  std::vector<uint8_t> content_id;
82  if (!base::HexStringToBytes(FLAGS_content_id, &content_id)) {
83  LOG(ERROR) << "Invalid content_id hex string specified.";
84  return std::unique_ptr<KeySource>();
85  }
86  Status status = widevine_key_source->FetchKeys(content_id, FLAGS_policy);
87  if (!status.ok()) {
88  LOG(ERROR) << "Widevine encryption key source failed to fetch keys: "
89  << status.ToString();
90  return std::unique_ptr<KeySource>();
91  }
92  encryption_key_source = std::move(widevine_key_source);
93  } else if (FLAGS_enable_fixed_key_encryption) {
94  encryption_key_source = FixedKeySource::CreateFromHexStrings(
95  FLAGS_key_id, FLAGS_key, FLAGS_pssh, FLAGS_iv);
96  } else if (FLAGS_enable_playready_encryption) {
97  if (!FLAGS_playready_key_id.empty() && !FLAGS_playready_key.empty()) {
98  encryption_key_source = PlayReadyKeySource::CreateFromKeyAndKeyId(
99  FLAGS_playready_key_id, FLAGS_playready_key);
100  } else if (!FLAGS_playready_server_url.empty() &&
101  !FLAGS_program_identifier.empty()) {
102  std::unique_ptr<PlayReadyKeySource> playready_key_source;
103  if (!FLAGS_client_cert_file.empty() &&
104  !FLAGS_client_cert_private_key_file.empty() &&
105  !FLAGS_client_cert_private_key_password.empty()) {
106  playready_key_source.reset(new PlayReadyKeySource(
107  FLAGS_playready_server_url,
108  FLAGS_client_cert_file,
109  FLAGS_client_cert_private_key_file,
110  FLAGS_client_cert_private_key_password));
111  } else {
112  playready_key_source.reset(new PlayReadyKeySource(
113  FLAGS_playready_server_url));
114  }
115  if (!FLAGS_ca_file.empty()) {
116  playready_key_source->SetCaFile(FLAGS_ca_file);
117  }
118  playready_key_source->FetchKeysWithProgramIdentifier(FLAGS_program_identifier);
119  encryption_key_source = std::move(playready_key_source);
120  } else {
121  LOG(ERROR) << "Error creating PlayReady key source.";
122  return std::unique_ptr<KeySource>();
123  }
124  }
125  return encryption_key_source;
126 }
127 
128 std::unique_ptr<KeySource> CreateDecryptionKeySource() {
129  std::unique_ptr<KeySource> decryption_key_source;
130  if (FLAGS_enable_widevine_decryption) {
131  std::unique_ptr<WidevineKeySource> widevine_key_source(
132  new WidevineKeySource(FLAGS_key_server_url, FLAGS_include_common_pssh));
133  if (!FLAGS_signer.empty()) {
134  std::unique_ptr<RequestSigner> request_signer(CreateSigner());
135  if (!request_signer)
136  return std::unique_ptr<KeySource>();
137  widevine_key_source->set_signer(std::move(request_signer));
138  }
139 
140  decryption_key_source = std::move(widevine_key_source);
141  } else if (FLAGS_enable_fixed_key_decryption) {
142  const char kNoPssh[] = "";
143  const char kNoIv[] = "";
144  decryption_key_source = FixedKeySource::CreateFromHexStrings(
145  FLAGS_key_id, FLAGS_key, kNoPssh, kNoIv);
146  }
147  return decryption_key_source;
148 }
149 
150 ChunkingOptions GetChunkingOptions() {
151  ChunkingOptions chunking_options;
152  chunking_options.segment_duration_in_seconds = FLAGS_segment_duration;
153  chunking_options.subsegment_duration_in_seconds = FLAGS_fragment_duration;
154  chunking_options.segment_sap_aligned = FLAGS_segment_sap_aligned;
155  chunking_options.subsegment_sap_aligned = FLAGS_fragment_sap_aligned;
156  return chunking_options;
157 }
158 
159 MuxerOptions GetMuxerOptions() {
160  MuxerOptions muxer_options;
161  muxer_options.num_subsegments_per_sidx = FLAGS_num_subsegments_per_sidx;
162  muxer_options.webm_subsample_encryption = FLAGS_webm_subsample_encryption;
163  if (FLAGS_mp4_use_decoding_timestamp_in_timeline) {
164  LOG(WARNING) << "Flag --mp4_use_decoding_timestamp_in_timeline is set. "
165  "Note that it is a temporary hack to workaround Chromium "
166  "bug https://crbug.com/398130. The flag may be removed "
167  "when the Chromium bug is fixed.";
168  }
169  muxer_options.mp4_use_decoding_timestamp_in_timeline =
170  FLAGS_mp4_use_decoding_timestamp_in_timeline;
171  muxer_options.temp_dir = FLAGS_temp_dir;
172  return muxer_options;
173 }
174 
175 MpdOptions GetMpdOptions(bool on_demand_profile) {
176  MpdOptions mpd_options;
177  mpd_options.dash_profile =
178  on_demand_profile ? DashProfile::kOnDemand : DashProfile::kLive;
179  mpd_options.mpd_type = (on_demand_profile || FLAGS_generate_static_mpd)
180  ? MpdType::kStatic
181  : MpdType::kDynamic;
182  mpd_options.availability_time_offset = FLAGS_availability_time_offset;
183  mpd_options.minimum_update_period = FLAGS_minimum_update_period;
184  mpd_options.min_buffer_time = FLAGS_min_buffer_time;
185  mpd_options.time_shift_buffer_depth = FLAGS_time_shift_buffer_depth;
186  mpd_options.suggested_presentation_delay = FLAGS_suggested_presentation_delay;
187  mpd_options.default_language = FLAGS_default_language;
188  return mpd_options;
189 }
190 
191 } // namespace media
192 } // 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 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)