7 #include "packager/app/packager_util.h"
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"
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:
33 signer.signer_name, signer.aes.key, signer.aes.iv));
35 case WidevineSigner::SigningKeyType::kRsa:
39 case WidevineSigner::SigningKeyType::kNone:
43 LOG(ERROR) <<
"Failed to create the signer object.";
44 return request_signer;
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>();
60 if (widevine.content_id.empty()) {
61 LOG(ERROR) <<
"'content_id' should not be empty.";
62 return std::unique_ptr<KeySource>();
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));
72 return std::unique_ptr<KeySource>();
73 widevine_key_source->set_signer(std::move(request_signer));
75 widevine_key_source->set_group_id(widevine.group_id);
78 widevine_key_source->FetchKeys(widevine.content_id, widevine.policy);
80 LOG(ERROR) <<
"Widevine encryption key source failed to fetch keys: "
82 return std::unique_ptr<KeySource>();
84 encryption_key_source = std::move(widevine_key_source);
87 case KeyProvider::kRawKey: {
88 const RawKeyEncryptionParams& raw_key = encryption_params.raw_key;
89 const std::string kDefaultTrackType;
92 raw_key.key_map.find(
"")->second.key_id,
93 raw_key.key_map.find(
"")->second.key, raw_key.pssh, raw_key.iv);
96 case KeyProvider::kPlayready: {
97 const PlayreadyEncryptionParams& playready = encryption_params.playready;
98 if (!playready.key_id.empty() && !playready.key.empty()) {
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));
112 playready_key_source.reset(
113 new PlayReadyKeySource(playready.key_server_url));
115 if (!playready.ca_file.empty()) {
116 playready_key_source->
SetCaFile(playready.ca_file);
118 playready_key_source->FetchKeysWithProgramIdentifier(
119 playready.program_identifier);
120 encryption_key_source = std::move(playready_key_source);
122 LOG(ERROR) <<
"Error creating PlayReady key source.";
123 return std::unique_ptr<KeySource>();
127 case KeyProvider::kNone:
130 return encryption_key_source;
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>();
143 std::unique_ptr<WidevineKeySource> widevine_key_source(
144 new WidevineKeySource(widevine.key_server_url,
146 if (!widevine.signer.signer_name.empty()) {
147 std::unique_ptr<RequestSigner> request_signer(
148 CreateSigner(widevine.signer));
150 return std::unique_ptr<KeySource>();
151 widevine_key_source->set_signer(std::move(request_signer));
154 decryption_key_source = std::move(widevine_key_source);
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;
162 raw_key.key_map.find(
"")->second.key_id,
163 raw_key.key_map.find(
"")->second.key, kNoPssh, kNoIv);
166 case KeyProvider::kNone:
167 case KeyProvider::kPlayready:
170 return decryption_key_source;
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)
181 mpd_options.mpd_params = mpd_params;
185 Status ConnectHandlers(std::vector<std::shared_ptr<MediaHandler>>& handlers) {
186 size_t num_handlers = handlers.size();
188 for (
size_t i = 1; i < num_handlers; ++i) {
189 status.Update(handlers[i - 1]->AddHandler(handlers[i]));