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