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/packager.h"
23 #include "packager/status.h"
29 std::unique_ptr<RequestSigner> CreateSigner(
const WidevineSigner& signer) {
30 std::unique_ptr<RequestSigner> request_signer;
31 switch (signer.signing_key_type) {
32 case WidevineSigner::SigningKeyType::kAes:
34 signer.signer_name, signer.aes.key, signer.aes.iv));
36 case WidevineSigner::SigningKeyType::kRsa:
40 case WidevineSigner::SigningKeyType::kNone:
44 LOG(ERROR) <<
"Failed to create the signer object.";
45 return request_signer;
50 std::unique_ptr<KeySource> CreateEncryptionKeySource(
51 FourCC protection_scheme,
52 const EncryptionParams& encryption_params) {
53 std::unique_ptr<KeySource> encryption_key_source;
54 switch (encryption_params.key_provider) {
55 case KeyProvider::kWidevine: {
56 const WidevineEncryptionParams& widevine = encryption_params.widevine;
57 if (widevine.key_server_url.empty()) {
58 LOG(ERROR) <<
"'key_server_url' should not be empty.";
59 return std::unique_ptr<KeySource>();
61 if (widevine.content_id.empty()) {
62 LOG(ERROR) <<
"'content_id' should not be empty.";
63 return std::unique_ptr<KeySource>();
65 std::unique_ptr<WidevineKeySource> widevine_key_source(
66 new WidevineKeySource(widevine.key_server_url,
67 widevine.include_common_pssh));
68 widevine_key_source->set_protection_scheme(protection_scheme);
69 if (!widevine.signer.signer_name.empty()) {
70 std::unique_ptr<RequestSigner> request_signer(
71 CreateSigner(widevine.signer));
73 return std::unique_ptr<KeySource>();
74 widevine_key_source->set_signer(std::move(request_signer));
76 widevine_key_source->set_group_id(widevine.group_id);
79 widevine_key_source->FetchKeys(widevine.content_id, widevine.policy);
81 LOG(ERROR) <<
"Widevine encryption key source failed to fetch keys: "
83 return std::unique_ptr<KeySource>();
85 encryption_key_source = std::move(widevine_key_source);
88 case KeyProvider::kRawKey: {
89 const RawKeyEncryptionParams& raw_key = encryption_params.raw_key;
90 const std::string kDefaultTrackType;
93 raw_key.key_map.find(
"")->second.key_id,
94 raw_key.key_map.find(
"")->second.key, raw_key.pssh, raw_key.iv);
97 case KeyProvider::kPlayready: {
98 const PlayreadyEncryptionParams& playready = encryption_params.playready;
99 if (!playready.key_id.empty() && !playready.key.empty()) {
101 playready.key_id, playready.key);
102 }
else if (!playready.key_server_url.empty() &&
103 !playready.program_identifier.empty()) {
104 std::unique_ptr<PlayReadyKeySource> playready_key_source;
105 if (!playready.client_cert_file.empty() &&
106 !playready.client_cert_private_key_file.empty() &&
107 !playready.client_cert_private_key_password.empty()) {
108 playready_key_source.reset(
new PlayReadyKeySource(
109 playready.key_server_url, playready.client_cert_file,
110 playready.client_cert_private_key_file,
111 playready.client_cert_private_key_password));
113 playready_key_source.reset(
114 new PlayReadyKeySource(playready.key_server_url));
116 if (!playready.ca_file.empty()) {
117 playready_key_source->
SetCaFile(playready.ca_file);
119 playready_key_source->FetchKeysWithProgramIdentifier(
120 playready.program_identifier);
121 encryption_key_source = std::move(playready_key_source);
123 LOG(ERROR) <<
"Error creating PlayReady key source.";
124 return std::unique_ptr<KeySource>();
128 case KeyProvider::kNone:
131 return encryption_key_source;
134 std::unique_ptr<KeySource> CreateDecryptionKeySource(
135 const DecryptionParams& decryption_params) {
136 std::unique_ptr<KeySource> decryption_key_source;
137 switch (decryption_params.key_provider) {
138 case KeyProvider::kWidevine: {
139 const WidevineDecryptionParams& widevine = decryption_params.widevine;
140 if (widevine.key_server_url.empty()) {
141 LOG(ERROR) <<
"'key_server_url' should not be empty.";
142 return std::unique_ptr<KeySource>();
144 std::unique_ptr<WidevineKeySource> widevine_key_source(
145 new WidevineKeySource(widevine.key_server_url,
147 if (!widevine.signer.signer_name.empty()) {
148 std::unique_ptr<RequestSigner> request_signer(
149 CreateSigner(widevine.signer));
151 return std::unique_ptr<KeySource>();
152 widevine_key_source->set_signer(std::move(request_signer));
155 decryption_key_source = std::move(widevine_key_source);
158 case KeyProvider::kRawKey: {
159 const RawKeyDecryptionParams& raw_key = decryption_params.raw_key;
160 const std::vector<uint8_t> kNoPssh;
161 const std::vector<uint8_t> kNoIv;
163 raw_key.key_map.find(
"")->second.key_id,
164 raw_key.key_map.find(
"")->second.key, kNoPssh, kNoIv);
167 case KeyProvider::kNone:
168 case KeyProvider::kPlayready:
171 return decryption_key_source;
174 MpdOptions GetMpdOptions(
bool on_demand_profile,
const MpdParams& mpd_params) {
175 MpdOptions mpd_options;
176 mpd_options.dash_profile =
177 on_demand_profile ? DashProfile::kOnDemand : DashProfile::kLive;
178 mpd_options.mpd_type =
179 (on_demand_profile || mpd_params.generate_static_live_mpd)
182 mpd_options.mpd_params = mpd_params;
186 Status ConnectHandlers(std::vector<std::shared_ptr<MediaHandler>>& handlers) {
187 size_t num_handlers = handlers.size();
189 for (
size_t i = 1; i < num_handlers; ++i) {
190 status.Update(handlers[i - 1]->AddHandler(handlers[i]));