Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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/file/file.h"
13 #include "packager/media/base/fixed_key_source.h"
14 #include "packager/media/base/media_handler.h"
15 #include "packager/media/base/muxer_options.h"
16 #include "packager/media/base/playready_key_source.h"
17 #include "packager/media/base/request_signer.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/mpd/base/mpd_options.h"
22 #include "packager/status.h"
23 
24 namespace shaka {
25 namespace media {
26 namespace {
27 
28 std::unique_ptr<RequestSigner> CreateSigner(const WidevineSigner& signer) {
29  std::unique_ptr<RequestSigner> request_signer;
30  switch (signer.signing_key_type) {
31  case WidevineSigner::SigningKeyType::kAes:
32  request_signer.reset(AesRequestSigner::CreateSigner(
33  signer.signer_name, signer.aes.key, signer.aes.iv));
34  break;
35  case WidevineSigner::SigningKeyType::kRsa:
36  request_signer.reset(
37  RsaRequestSigner::CreateSigner(signer.signer_name, signer.rsa.key));
38  break;
39  case WidevineSigner::SigningKeyType::kNone:
40  break;
41  }
42  if (!request_signer)
43  LOG(ERROR) << "Failed to create the signer object.";
44  return request_signer;
45 }
46 
47 } // namespace
48 
49 std::unique_ptr<KeySource> CreateEncryptionKeySource(
50  FourCC protection_scheme,
51  const EncryptionParams& encryption_params) {
52  std::unique_ptr<KeySource> encryption_key_source;
53  switch (encryption_params.key_provider) {
54  case KeyProvider::kWidevine: {
55  const WidevineEncryptionParams& widevine = encryption_params.widevine;
56  if (widevine.key_server_url.empty()) {
57  LOG(ERROR) << "'key_server_url' should not be empty.";
58  return std::unique_ptr<KeySource>();
59  }
60  if (widevine.content_id.empty()) {
61  LOG(ERROR) << "'content_id' should not be empty.";
62  return std::unique_ptr<KeySource>();
63  }
64  std::unique_ptr<WidevineKeySource> widevine_key_source(
65  new WidevineKeySource(widevine.key_server_url,
66  widevine.include_common_pssh));
67  widevine_key_source->set_protection_scheme(protection_scheme);
68  if (!widevine.signer.signer_name.empty()) {
69  std::unique_ptr<RequestSigner> request_signer(
70  CreateSigner(widevine.signer));
71  if (!request_signer)
72  return std::unique_ptr<KeySource>();
73  widevine_key_source->set_signer(std::move(request_signer));
74  }
75  widevine_key_source->set_group_id(widevine.group_id);
76 
77  Status status =
78  widevine_key_source->FetchKeys(widevine.content_id, widevine.policy);
79  if (!status.ok()) {
80  LOG(ERROR) << "Widevine encryption key source failed to fetch keys: "
81  << status.ToString();
82  return std::unique_ptr<KeySource>();
83  }
84  encryption_key_source = std::move(widevine_key_source);
85  break;
86  }
87  case KeyProvider::kRawKey: {
88  const RawKeyEncryptionParams& raw_key = encryption_params.raw_key;
89  const std::string kDefaultTrackType;
90  // TODO(kqyang): Refactor FixedKeySource.
91  encryption_key_source = FixedKeySource::Create(
92  raw_key.key_map.find("")->second.key_id,
93  raw_key.key_map.find("")->second.key, raw_key.pssh, raw_key.iv);
94  break;
95  }
96  case KeyProvider::kPlayready: {
97  const PlayreadyEncryptionParams& playready = encryption_params.playready;
98  if (!playready.key_id.empty() && !playready.key.empty()) {
99  encryption_key_source = PlayReadyKeySource::CreateFromKeyAndKeyId(
100  playready.key_id, playready.key);
101  } else if (!playready.key_server_url.empty() &&
102  !playready.program_identifier.empty()) {
103  std::unique_ptr<PlayReadyKeySource> playready_key_source;
104  if (!playready.client_cert_file.empty() &&
105  !playready.client_cert_private_key_file.empty() &&
106  !playready.client_cert_private_key_password.empty()) {
107  playready_key_source.reset(new PlayReadyKeySource(
108  playready.key_server_url, playready.client_cert_file,
109  playready.client_cert_private_key_file,
110  playready.client_cert_private_key_password));
111  } else {
112  playready_key_source.reset(
113  new PlayReadyKeySource(playready.key_server_url));
114  }
115  if (!playready.ca_file.empty()) {
116  playready_key_source->SetCaFile(playready.ca_file);
117  }
118  playready_key_source->FetchKeysWithProgramIdentifier(
119  playready.program_identifier);
120  encryption_key_source = std::move(playready_key_source);
121  } else {
122  LOG(ERROR) << "Error creating PlayReady key source.";
123  return std::unique_ptr<KeySource>();
124  }
125  break;
126  }
127  case KeyProvider::kNone:
128  break;
129  }
130  return encryption_key_source;
131 }
132 
133 std::unique_ptr<KeySource> CreateDecryptionKeySource(
134  const DecryptionParams& decryption_params) {
135  std::unique_ptr<KeySource> decryption_key_source;
136  switch (decryption_params.key_provider) {
137  case KeyProvider::kWidevine: {
138  const WidevineDecryptionParams& widevine = decryption_params.widevine;
139  if (widevine.key_server_url.empty()) {
140  LOG(ERROR) << "'key_server_url' should not be empty.";
141  return std::unique_ptr<KeySource>();
142  }
143  std::unique_ptr<WidevineKeySource> widevine_key_source(
144  new WidevineKeySource(widevine.key_server_url,
145  true /* commmon pssh, does not matter here */));
146  if (!widevine.signer.signer_name.empty()) {
147  std::unique_ptr<RequestSigner> request_signer(
148  CreateSigner(widevine.signer));
149  if (!request_signer)
150  return std::unique_ptr<KeySource>();
151  widevine_key_source->set_signer(std::move(request_signer));
152  }
153 
154  decryption_key_source = std::move(widevine_key_source);
155  break;
156  }
157  case KeyProvider::kRawKey: {
158  const RawKeyDecryptionParams& raw_key = decryption_params.raw_key;
159  const std::vector<uint8_t> kNoPssh;
160  const std::vector<uint8_t> kNoIv;
161  decryption_key_source = FixedKeySource::Create(
162  raw_key.key_map.find("")->second.key_id,
163  raw_key.key_map.find("")->second.key, kNoPssh, kNoIv);
164  break;
165  }
166  case KeyProvider::kNone:
167  case KeyProvider::kPlayready:
168  break;
169  }
170  return decryption_key_source;
171 }
172 
173 MpdOptions GetMpdOptions(bool on_demand_profile, const MpdParams& mpd_params) {
174  MpdOptions mpd_options;
175  mpd_options.dash_profile =
176  on_demand_profile ? DashProfile::kOnDemand : DashProfile::kLive;
177  mpd_options.mpd_type =
178  (on_demand_profile || mpd_params.generate_static_live_mpd)
179  ? MpdType::kStatic
180  : MpdType::kDynamic;
181  mpd_options.mpd_params = mpd_params;
182  return mpd_options;
183 }
184 
185 Status ConnectHandlers(std::vector<std::shared_ptr<MediaHandler>>& handlers) {
186  size_t num_handlers = handlers.size();
187  Status status;
188  for (size_t i = 1; i < num_handlers; ++i) {
189  status.Update(handlers[i - 1]->AddHandler(handlers[i]));
190  }
191  return status;
192 }
193 
194 } // namespace media
195 } // 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:152
void SetCaFile(const std::string &ca_file)
Sets the Certificate Authority file for validating self-signed certificates.
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)