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 "packager/base/logging.h"
10 #include "packager/base/strings/string_number_conversions.h"
11 #include "packager/base/strings/string_split.h"
12 #include "packager/media/base/fixed_key_source.h"
13 #include "packager/media/base/media_handler.h"
14 #include "packager/media/base/muxer_options.h"
15 #include "packager/media/base/playready_key_source.h"
16 #include "packager/media/base/request_signer.h"
17 #include "packager/media/base/status.h"
18 #include "packager/media/base/widevine_key_source.h"
19 #include "packager/media/chunking/chunking_handler.h"
20 #include "packager/media/crypto/encryption_handler.h"
21 #include "packager/media/file/file.h"
22 #include "packager/mpd/base/mpd_options.h"
23 #include "packager/packager.h"
24 
25 namespace shaka {
26 namespace media {
27 namespace {
28 
29 FourCC GetProtectionScheme(const std::string& protection_scheme) {
30  if (protection_scheme == "cenc") {
31  return FOURCC_cenc;
32  } else if (protection_scheme == "cens") {
33  return FOURCC_cens;
34  } else if (protection_scheme == "cbc1") {
35  return FOURCC_cbc1;
36  } else if (protection_scheme == "cbcs") {
37  return FOURCC_cbcs;
38  } else {
39  LOG(ERROR) << "Unknown protection scheme: " << protection_scheme;
40  return FOURCC_NULL;
41  }
42 }
43 
44 } // namespace
45 
46 std::unique_ptr<RequestSigner> CreateSigner(const WidevineSigner& signer) {
47  std::unique_ptr<RequestSigner> request_signer;
48  switch (signer.signing_key_type) {
49  case WidevineSigner::SigningKeyType::kAes:
50  request_signer.reset(AesRequestSigner::CreateSigner(
51  signer.signer_name, signer.aes.key, signer.aes.iv));
52  break;
53  case WidevineSigner::SigningKeyType::kRsa:
54  request_signer.reset(
55  RsaRequestSigner::CreateSigner(signer.signer_name, signer.rsa.key));
56  break;
57  case WidevineSigner::SigningKeyType::kNone:
58  break;
59  }
60  if (!request_signer)
61  LOG(ERROR) << "Failed to create the signer object.";
62  return request_signer;
63 }
64 
65 std::unique_ptr<KeySource> CreateEncryptionKeySource(
66  FourCC protection_scheme,
67  const EncryptionParams& encryption_params) {
68  std::unique_ptr<KeySource> encryption_key_source;
69  switch (encryption_params.key_provider) {
70  case KeyProvider::kWidevine: {
71  const WidevineEncryptionParams& widevine = encryption_params.widevine;
72  if (widevine.key_server_url.empty()) {
73  LOG(ERROR) << "'key_server_url' should not be empty.";
74  return std::unique_ptr<KeySource>();
75  }
76  if (widevine.content_id.empty()) {
77  LOG(ERROR) << "'content_id' should not be empty.";
78  return std::unique_ptr<KeySource>();
79  }
80  std::unique_ptr<WidevineKeySource> widevine_key_source(
81  new WidevineKeySource(widevine.key_server_url,
82  widevine.include_common_pssh));
83  widevine_key_source->set_protection_scheme(protection_scheme);
84  if (!widevine.signer.signer_name.empty()) {
85  std::unique_ptr<RequestSigner> request_signer(
86  CreateSigner(widevine.signer));
87  if (!request_signer)
88  return std::unique_ptr<KeySource>();
89  widevine_key_source->set_signer(std::move(request_signer));
90  }
91 
92  Status status =
93  widevine_key_source->FetchKeys(widevine.content_id, widevine.policy);
94  if (!status.ok()) {
95  LOG(ERROR) << "Widevine encryption key source failed to fetch keys: "
96  << status.ToString();
97  return std::unique_ptr<KeySource>();
98  }
99  encryption_key_source = std::move(widevine_key_source);
100  break;
101  }
102  case KeyProvider::kRawKey: {
103  const RawKeyEncryptionParams& raw_key = encryption_params.raw_key;
104  const std::string kDefaultTrackType;
105  // TODO(kqyang): Refactor FixedKeySource.
106  encryption_key_source = FixedKeySource::Create(
107  raw_key.key_map.find("")->second.key_id,
108  raw_key.key_map.find("")->second.key, raw_key.pssh, raw_key.iv);
109  break;
110  }
111  case KeyProvider::kPlayready: {
112  const PlayreadyEncryptionParams& playready = encryption_params.playready;
113  if (!playready.key_id.empty() && !playready.key.empty()) {
114  encryption_key_source = PlayReadyKeySource::CreateFromKeyAndKeyId(
115  playready.key_id, playready.key);
116  } else if (!playready.key_server_url.empty() &&
117  !playready.program_identifier.empty()) {
118  std::unique_ptr<PlayReadyKeySource> playready_key_source;
119  if (!playready.client_cert_file.empty() &&
120  !playready.client_cert_private_key_file.empty() &&
121  !playready.client_cert_private_key_password.empty()) {
122  playready_key_source.reset(new PlayReadyKeySource(
123  playready.key_server_url, playready.client_cert_file,
124  playready.client_cert_private_key_file,
125  playready.client_cert_private_key_password));
126  } else {
127  playready_key_source.reset(
128  new PlayReadyKeySource(playready.key_server_url));
129  }
130  if (!playready.ca_file.empty()) {
131  playready_key_source->SetCaFile(playready.ca_file);
132  }
133  playready_key_source->FetchKeysWithProgramIdentifier(
134  playready.program_identifier);
135  encryption_key_source = std::move(playready_key_source);
136  } else {
137  LOG(ERROR) << "Error creating PlayReady key source.";
138  return std::unique_ptr<KeySource>();
139  }
140  break;
141  }
142  case KeyProvider::kNone:
143  break;
144  }
145  return encryption_key_source;
146 }
147 
148 std::unique_ptr<KeySource> CreateDecryptionKeySource(
149  const DecryptionParams& decryption_params) {
150  std::unique_ptr<KeySource> decryption_key_source;
151  switch (decryption_params.key_provider) {
152  case KeyProvider::kWidevine: {
153  const WidevineDecryptionParams& widevine = decryption_params.widevine;
154  if (widevine.key_server_url.empty()) {
155  LOG(ERROR) << "'key_server_url' should not be empty.";
156  return std::unique_ptr<KeySource>();
157  }
158  std::unique_ptr<WidevineKeySource> widevine_key_source(
159  new WidevineKeySource(widevine.key_server_url,
160  true /* commmon pssh, does not matter here */));
161  if (!widevine.signer.signer_name.empty()) {
162  std::unique_ptr<RequestSigner> request_signer(
163  CreateSigner(widevine.signer));
164  if (!request_signer)
165  return std::unique_ptr<KeySource>();
166  widevine_key_source->set_signer(std::move(request_signer));
167  }
168 
169  decryption_key_source = std::move(widevine_key_source);
170  break;
171  }
172  case KeyProvider::kRawKey: {
173  const RawKeyDecryptionParams& raw_key = decryption_params.raw_key;
174  const std::vector<uint8_t> kNoPssh;
175  const std::vector<uint8_t> kNoIv;
176  decryption_key_source = FixedKeySource::Create(
177  raw_key.key_map.find("")->second.key_id,
178  raw_key.key_map.find("")->second.key, kNoPssh, kNoIv);
179  break;
180  }
181  case KeyProvider::kNone:
182  case KeyProvider::kPlayready:
183  break;
184  }
185  return decryption_key_source;
186 }
187 
188 ChunkingOptions GetChunkingOptions(const ChunkingParams& chunking_params) {
189  ChunkingOptions chunking_options;
190  chunking_options.segment_duration_in_seconds =
191  chunking_params.segment_duration_in_seconds;
192  chunking_options.subsegment_duration_in_seconds =
193  chunking_params.subsegment_duration_in_seconds;
194  chunking_options.segment_sap_aligned = chunking_params.segment_sap_aligned;
195  chunking_options.subsegment_sap_aligned =
196  chunking_params.subsegment_sap_aligned;
197  return chunking_options;
198 }
199 
200 EncryptionOptions GetEncryptionOptions(
201  const EncryptionParams& encryption_params) {
202  EncryptionOptions encryption_options;
203  encryption_options.clear_lead_in_seconds =
204  encryption_params.clear_lead_in_seconds;
205  encryption_options.protection_scheme =
206  GetProtectionScheme(encryption_params.protection_scheme);
207  encryption_options.crypto_period_duration_in_seconds =
208  encryption_params.crypto_period_duration_in_seconds;
209  encryption_options.vp9_subsample_encryption =
210  encryption_params.vp9_subsample_encryption;
211  encryption_options.stream_label_func = encryption_params.stream_label_func;
212  return encryption_options;
213 }
214 
215 MuxerOptions GetMuxerOptions(const std::string& temp_dir,
216  const Mp4OutputParams& mp4_params) {
217  MuxerOptions muxer_options;
218  muxer_options.num_subsegments_per_sidx = mp4_params.num_subsegments_per_sidx;
219  muxer_options.mp4_include_pssh_in_stream = mp4_params.include_pssh_in_stream;
220  muxer_options.mp4_use_decoding_timestamp_in_timeline =
221  mp4_params.use_decoding_timestamp_in_timeline;
222  muxer_options.temp_dir = temp_dir;
223  return muxer_options;
224 }
225 
226 MpdOptions GetMpdOptions(bool on_demand_profile, const MpdParams& mpd_params) {
227  MpdOptions mpd_options;
228  mpd_options.dash_profile =
229  on_demand_profile ? DashProfile::kOnDemand : DashProfile::kLive;
230  mpd_options.mpd_type =
231  (on_demand_profile || mpd_params.generate_static_live_mpd)
232  ? MpdType::kStatic
233  : MpdType::kDynamic;
234  mpd_options.minimum_update_period = mpd_params.minimum_update_period;
235  mpd_options.min_buffer_time = mpd_params.min_buffer_time;
236  mpd_options.time_shift_buffer_depth = mpd_params.time_shift_buffer_depth;
237  mpd_options.suggested_presentation_delay =
238  mpd_params.suggested_presentation_delay;
239  mpd_options.default_language = mpd_params.default_language;
240  return mpd_options;
241 }
242 
243 Status ConnectHandlers(std::vector<std::shared_ptr<MediaHandler>>& handlers) {
244  size_t num_handlers = handlers.size();
245  Status status;
246  for (size_t i = 1; i < num_handlers; ++i) {
247  status.Update(handlers[i - 1]->AddHandler(handlers[i]));
248  }
249  return status;
250 }
251 
252 } // namespace media
253 } // 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::vector< uint8_t > &aes_key, const std::vector< uint8_t > &iv)
void Update(const Status &new_status)
Definition: status.h:130
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 std::unique_ptr< FixedKeySource > Create(const std::vector< uint8_t > &key_id, const std::vector< uint8_t > &key, const std::vector< uint8_t > &pssh_boxes, const std::vector< uint8_t > &iv)
static std::unique_ptr< PlayReadyKeySource > CreateFromKeyAndKeyId(const std::vector< uint8_t > &key_id, const std::vector< uint8_t > &key)