Shaka Packager SDK
packager_util.cc
1 // Copyright 2014 Google LLC. 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 <absl/log/log.h>
10 
11 #include <packager/file.h>
12 #include <packager/media/base/media_handler.h>
13 #include <packager/media/base/muxer_options.h>
14 #include <packager/media/base/playready_key_source.h>
15 #include <packager/media/base/raw_key_source.h>
16 #include <packager/media/base/request_signer.h>
17 #include <packager/media/base/widevine_key_source.h>
18 #include <packager/mpd/base/mpd_options.h>
19 
20 namespace shaka {
21 namespace media {
22 namespace {
23 
24 std::unique_ptr<RequestSigner> CreateSigner(const WidevineSigner& signer) {
25  std::unique_ptr<RequestSigner> request_signer;
26  switch (signer.signing_key_type) {
27  case WidevineSigner::SigningKeyType::kAes:
28  request_signer.reset(AesRequestSigner::CreateSigner(
29  signer.signer_name, signer.aes.key, signer.aes.iv));
30  break;
31  case WidevineSigner::SigningKeyType::kRsa:
32  request_signer.reset(
33  RsaRequestSigner::CreateSigner(signer.signer_name, signer.rsa.key));
34  break;
35  case WidevineSigner::SigningKeyType::kNone:
36  break;
37  }
38  if (!request_signer)
39  LOG(ERROR) << "Failed to create the signer object.";
40  return request_signer;
41 }
42 
43 } // namespace
44 
45 std::unique_ptr<KeySource> CreateEncryptionKeySource(
46  FourCC protection_scheme,
47  const EncryptionParams& encryption_params) {
48  std::unique_ptr<KeySource> encryption_key_source;
49  switch (encryption_params.key_provider) {
50  case KeyProvider::kWidevine: {
51  const WidevineEncryptionParams& widevine = encryption_params.widevine;
52  if (widevine.key_server_url.empty()) {
53  LOG(ERROR) << "'key_server_url' should not be empty.";
54  return nullptr;
55  }
56  if (widevine.content_id.empty()) {
57  LOG(ERROR) << "'content_id' should not be empty.";
58  return nullptr;
59  }
60  std::unique_ptr<WidevineKeySource> widevine_key_source(
61  new WidevineKeySource(widevine.key_server_url,
62  encryption_params.protection_systems,
63  protection_scheme));
64  if (!widevine.signer.signer_name.empty()) {
65  std::unique_ptr<RequestSigner> request_signer(
66  CreateSigner(widevine.signer));
67  if (!request_signer)
68  return nullptr;
69  widevine_key_source->set_signer(std::move(request_signer));
70  }
71  widevine_key_source->set_group_id(widevine.group_id);
72  widevine_key_source->set_enable_entitlement_license(
73  widevine.enable_entitlement_license);
74 
75  Status status =
76  widevine_key_source->FetchKeys(widevine.content_id, widevine.policy);
77  if (!status.ok()) {
78  LOG(ERROR) << "Widevine encryption key source failed to fetch keys: "
79  << status.ToString();
80  return nullptr;
81  }
82  encryption_key_source = std::move(widevine_key_source);
83  break;
84  }
85  case KeyProvider::kRawKey: {
86  encryption_key_source = RawKeySource::Create(encryption_params.raw_key);
87  break;
88  }
89  case KeyProvider::kPlayReady: {
90  const PlayReadyEncryptionParams& playready = encryption_params.playready;
91  if (!playready.key_server_url.empty() ||
92  !playready.program_identifier.empty()) {
93  if (playready.key_server_url.empty() ||
94  playready.program_identifier.empty()) {
95  LOG(ERROR) << "Either PlayReady key_server_url or program_identifier "
96  "is not set.";
97  return nullptr;
98  }
99  std::unique_ptr<PlayReadyKeySource> playready_key_source;
100  // private_key_password is allowed to be empty for unencrypted key.
101  playready_key_source.reset(new PlayReadyKeySource(
102  playready.key_server_url, encryption_params.protection_systems));
103  Status status = playready_key_source->FetchKeysWithProgramIdentifier(
104  playready.program_identifier);
105  if (!status.ok()) {
106  LOG(ERROR) << "PlayReady encryption key source failed to fetch keys: "
107  << status.ToString();
108  return nullptr;
109  }
110  encryption_key_source = std::move(playready_key_source);
111  } else {
112  LOG(ERROR) << "Error creating PlayReady key source.";
113  return nullptr;
114  }
115  break;
116  }
117  default:
118  break;
119  }
120  return encryption_key_source;
121 }
122 
123 std::unique_ptr<KeySource> CreateDecryptionKeySource(
124  const DecryptionParams& decryption_params) {
125  std::unique_ptr<KeySource> decryption_key_source;
126  switch (decryption_params.key_provider) {
127  case KeyProvider::kWidevine: {
128  const WidevineDecryptionParams& widevine = decryption_params.widevine;
129  if (widevine.key_server_url.empty()) {
130  LOG(ERROR) << "'key_server_url' should not be empty.";
131  return std::unique_ptr<KeySource>();
132  }
133  std::unique_ptr<WidevineKeySource> widevine_key_source(
134  new WidevineKeySource(
135  widevine.key_server_url,
136  ProtectionSystem::kWidevine /* value does not matter here */,
137  FOURCC_NULL /* value does not matter here */));
138  if (!widevine.signer.signer_name.empty()) {
139  std::unique_ptr<RequestSigner> request_signer(
140  CreateSigner(widevine.signer));
141  if (!request_signer)
142  return std::unique_ptr<KeySource>();
143  widevine_key_source->set_signer(std::move(request_signer));
144  }
145 
146  decryption_key_source = std::move(widevine_key_source);
147  break;
148  }
149  case KeyProvider::kRawKey: {
150  decryption_key_source = RawKeySource::Create(decryption_params.raw_key);
151  break;
152  }
153  default:
154  break;
155  }
156  return decryption_key_source;
157 }
158 
159 MpdOptions GetMpdOptions(bool on_demand_profile, const MpdParams& mpd_params) {
160  MpdOptions mpd_options;
161  mpd_options.dash_profile =
162  on_demand_profile ? DashProfile::kOnDemand : DashProfile::kLive;
163  mpd_options.mpd_type =
164  (on_demand_profile || mpd_params.generate_static_live_mpd)
165  ? MpdType::kStatic
166  : MpdType::kDynamic;
167  mpd_options.mpd_params = mpd_params;
168  return mpd_options;
169 }
170 
171 } // namespace media
172 } // namespace shaka
static AesRequestSigner * CreateSigner(const std::string &signer_name, const std::vector< uint8_t > &aes_key, const std::vector< uint8_t > &iv)
static std::unique_ptr< RawKeySource > Create(const RawKeyParams &raw_key)
static RsaRequestSigner * CreateSigner(const std::string &signer_name, const std::string &pkcs1_rsa_key)
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66