From fac16cbf0afe986a9b78ff05ed1bf9ecd91e16ef Mon Sep 17 00:00:00 2001 From: Haoming Chen Date: Wed, 17 Jan 2018 10:53:58 -0800 Subject: [PATCH] Implement multi DRM support. (Part 2) - Allow including Widevine and Playready PSSH boxes for RawKeySource. Partially addresses issue #245 Change-Id: I8a48d6dbbbc21c76cf2c44384dd70286ccf80363 --- packager/app/packager_util.cc | 8 ++-- .../base/protection_system_specific_info.h | 1 + packager/media/base/raw_key_source.cc | 44 +++++++++++-------- packager/media/base/raw_key_source.h | 8 ++-- .../media/base/raw_key_source_unittest.cc | 10 +++-- 5 files changed, 42 insertions(+), 29 deletions(-) diff --git a/packager/app/packager_util.cc b/packager/app/packager_util.cc index 19b825745d..7905690306 100644 --- a/packager/app/packager_util.cc +++ b/packager/app/packager_util.cc @@ -93,8 +93,8 @@ std::unique_ptr CreateEncryptionKeySource( break; } case KeyProvider::kRawKey: { - // TODO(hmchen): add multiple DRM support for raw key source. - encryption_key_source = RawKeySource::Create(encryption_params.raw_key); + encryption_key_source = RawKeySource::Create(encryption_params.raw_key, + protection_systems_flags); break; } case KeyProvider::kPlayready: { @@ -176,7 +176,9 @@ std::unique_ptr CreateDecryptionKeySource( break; } case KeyProvider::kRawKey: { - decryption_key_source = RawKeySource::Create(decryption_params.raw_key); + decryption_key_source = RawKeySource::Create( + decryption_params.raw_key, + COMMON_PROTECTION_SYSTEM_FLAG /* value does not matter here*/); break; } case KeyProvider::kNone: diff --git a/packager/media/base/protection_system_specific_info.h b/packager/media/base/protection_system_specific_info.h index 76d80de225..6f28e90b34 100644 --- a/packager/media/base/protection_system_specific_info.h +++ b/packager/media/base/protection_system_specific_info.h @@ -13,6 +13,7 @@ #include "packager/base/logging.h" #include "packager/media/base/buffer_reader.h" +#define NO_PROTECTION_SYSTEM_FLAG 0x00 #define COMMON_PROTECTION_SYSTEM_FLAG 0x01 #define PLAYREADY_PROTECTION_SYSTEM_FLAG 0x02 #define WIDEVINE_PROTECTION_SYSTEM_FLAG 0x04 diff --git a/packager/media/base/raw_key_source.cc b/packager/media/base/raw_key_source.cc index c43061e1ab..50996550f2 100644 --- a/packager/media/base/raw_key_source.cc +++ b/packager/media/base/raw_key_source.cc @@ -9,6 +9,7 @@ #include #include "packager/base/logging.h" #include "packager/base/strings/string_number_conversions.h" +#include "packager/media/base/key_source.h" #include "packager/media/base/raw_key_pssh_generator.h" namespace { @@ -78,6 +79,12 @@ Status RawKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index, key->key.end()); for (auto& key_system : key->key_system_info) { + if (key_system.system_id() != + std::vector(std::begin(kCommonSystemId), + std::end(kCommonSystemId))) { + LOG(WARNING) << "For key rotation with raw key source, only common key " + "system is supported."; + } std::vector pssh_data = key_system.pssh_data(); if (!pssh_data.empty()) { std::rotate(pssh_data.begin(), @@ -103,25 +110,17 @@ Status RawKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index, } std::unique_ptr RawKeySource::Create( - const RawKeyParams& raw_key) { + const RawKeyParams& raw_key, + int protection_systems_flags) { std::vector key_system_info; + bool pssh_provided = false; if (!raw_key.pssh.empty()) { + pssh_provided = true; if (!ProtectionSystemSpecificInfo::ParseBoxes( raw_key.pssh.data(), raw_key.pssh.size(), &key_system_info)) { LOG(ERROR) << "--pssh argument should be full PSSH boxes."; return std::unique_ptr(); } - } else { - // If there aren't any PSSH boxes given, create one with the common system - // ID. - key_system_info.resize(1); - for (const auto& entry : raw_key.key_map) { - const RawKeyParams::KeyInfo& key_pair = entry.second; - key_system_info.back().add_key_id(key_pair.key_id); - } - key_system_info.back().set_system_id(kCommonSystemId, - arraysize(kCommonSystemId)); - key_system_info.back().set_pssh_box_version(1); } EncryptionKeyMap encryption_key_map; @@ -149,14 +148,23 @@ std::unique_ptr RawKeySource::Create( encryption_key_map[drm_label] = std::move(encryption_key); } - return std::unique_ptr( - new RawKeySource(std::move(encryption_key_map))); + // Generate common protection system if no other protection system is + // specified. + if (!pssh_provided && protection_systems_flags == NO_PROTECTION_SYSTEM_FLAG) { + protection_systems_flags = COMMON_PROTECTION_SYSTEM_FLAG; + } + + return std::unique_ptr(new RawKeySource( + std::move(encryption_key_map), protection_systems_flags)); } -RawKeySource::RawKeySource() : KeySource(COMMON_PROTECTION_SYSTEM_FLAG) {} -RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map) - : KeySource(COMMON_PROTECTION_SYSTEM_FLAG), - encryption_key_map_(std::move(encryption_key_map)) {} +RawKeySource::RawKeySource() : KeySource(NO_PROTECTION_SYSTEM_FLAG) {} +RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map, + int protection_systems_flags) + : KeySource(protection_systems_flags), + encryption_key_map_(std::move(encryption_key_map)) { + UpdateProtectionSystemInfo(&encryption_key_map_); +} } // namespace media } // namespace shaka diff --git a/packager/media/base/raw_key_source.h b/packager/media/base/raw_key_source.h index 1f5e83a4b5..37237ba925 100644 --- a/packager/media/base/raw_key_source.h +++ b/packager/media/base/raw_key_source.h @@ -43,16 +43,16 @@ class RawKeySource : public KeySource { /// Creates a new RawKeySource from the given data. Returns null /// if the parameter is malformed. /// @param raw_key contains parameters to setup the key source. - static std::unique_ptr Create(const RawKeyParams& raw_key); + static std::unique_ptr Create(const RawKeyParams& raw_key, + int protection_system_flags); protected: // Allow default constructor for mock key sources. RawKeySource(); private: - typedef std::map> - EncryptionKeyMap; - explicit RawKeySource(EncryptionKeyMap&& encryption_key_map); + explicit RawKeySource(EncryptionKeyMap&& encryption_key_map, + int protection_systems_flags); RawKeySource(const RawKeySource&) = delete; RawKeySource& operator=(const RawKeySource&) = delete; diff --git a/packager/media/base/raw_key_source_unittest.cc b/packager/media/base/raw_key_source_unittest.cc index 14421d636e..50ab6dc586 100644 --- a/packager/media/base/raw_key_source_unittest.cc +++ b/packager/media/base/raw_key_source_unittest.cc @@ -52,6 +52,8 @@ const char kDrmLabel[] = "SomeDrmLabel"; const char kAnotherDrmLabel[] = "AnotherDrmLabel"; const char kEmptyDrmLabel[] = ""; +const int kNoProtectionSystemFlag = NO_PROTECTION_SYSTEM_FLAG; + std::vector HexStringToVector(const std::string& str) { std::vector vec; CHECK(base::HexStringToBytes(str, &vec)); @@ -69,7 +71,7 @@ TEST(RawKeySourceTest, Success) { raw_key_params.pssh = HexStringToVector(std::string(kPsshBox1Hex) + kPsshBox2Hex); std::unique_ptr key_source = - RawKeySource::Create(raw_key_params); + RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag); ASSERT_NE(nullptr, key_source); EncryptionKey key_from_drm_label; @@ -111,7 +113,7 @@ TEST(RawKeySourceTest, EmptyPssh) { raw_key_params.key_map[kAnotherDrmLabel].key = HexStringToVector(kKey2Hex); raw_key_params.iv = HexStringToVector(kIvHex); std::unique_ptr key_source = - RawKeySource::Create(raw_key_params); + RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag); ASSERT_NE(nullptr, key_source); EncryptionKey key; @@ -132,13 +134,13 @@ TEST(RawKeySourceTest, Failure) { raw_key_params.pssh = HexStringToVector(kPsshBox1Hex); raw_key_params.iv = HexStringToVector(kIvHex); std::unique_ptr key_source = - RawKeySource::Create(raw_key_params); + RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag); EXPECT_EQ(nullptr, key_source); // Invalid pssh box. raw_key_params.key_map[kEmptyDrmLabel].key_id = HexStringToVector(kKeyIdHex); raw_key_params.pssh = HexStringToVector("000102030405"); - key_source = RawKeySource::Create(raw_key_params); + key_source = RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag); EXPECT_EQ(nullptr, key_source); }