Implement multi DRM support. (Part 2)

- Allow including  Widevine and Playready PSSH boxes for RawKeySource.

Partially addresses issue #245

Change-Id: I8a48d6dbbbc21c76cf2c44384dd70286ccf80363
This commit is contained in:
Haoming Chen 2018-01-17 10:53:58 -08:00
parent 56caecd073
commit fac16cbf0a
5 changed files with 42 additions and 29 deletions

View File

@ -93,8 +93,8 @@ std::unique_ptr<KeySource> CreateEncryptionKeySource(
break; break;
} }
case KeyProvider::kRawKey: { 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; break;
} }
case KeyProvider::kPlayready: { case KeyProvider::kPlayready: {
@ -176,7 +176,9 @@ std::unique_ptr<KeySource> CreateDecryptionKeySource(
break; break;
} }
case KeyProvider::kRawKey: { 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; break;
} }
case KeyProvider::kNone: case KeyProvider::kNone:

View File

@ -13,6 +13,7 @@
#include "packager/base/logging.h" #include "packager/base/logging.h"
#include "packager/media/base/buffer_reader.h" #include "packager/media/base/buffer_reader.h"
#define NO_PROTECTION_SYSTEM_FLAG 0x00
#define COMMON_PROTECTION_SYSTEM_FLAG 0x01 #define COMMON_PROTECTION_SYSTEM_FLAG 0x01
#define PLAYREADY_PROTECTION_SYSTEM_FLAG 0x02 #define PLAYREADY_PROTECTION_SYSTEM_FLAG 0x02
#define WIDEVINE_PROTECTION_SYSTEM_FLAG 0x04 #define WIDEVINE_PROTECTION_SYSTEM_FLAG 0x04

View File

@ -9,6 +9,7 @@
#include <algorithm> #include <algorithm>
#include "packager/base/logging.h" #include "packager/base/logging.h"
#include "packager/base/strings/string_number_conversions.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" #include "packager/media/base/raw_key_pssh_generator.h"
namespace { namespace {
@ -78,6 +79,12 @@ Status RawKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index,
key->key.end()); key->key.end());
for (auto& key_system : key->key_system_info) { for (auto& key_system : key->key_system_info) {
if (key_system.system_id() !=
std::vector<uint8_t>(std::begin(kCommonSystemId),
std::end(kCommonSystemId))) {
LOG(WARNING) << "For key rotation with raw key source, only common key "
"system is supported.";
}
std::vector<uint8_t> pssh_data = key_system.pssh_data(); std::vector<uint8_t> pssh_data = key_system.pssh_data();
if (!pssh_data.empty()) { if (!pssh_data.empty()) {
std::rotate(pssh_data.begin(), std::rotate(pssh_data.begin(),
@ -103,25 +110,17 @@ Status RawKeySource::GetCryptoPeriodKey(uint32_t crypto_period_index,
} }
std::unique_ptr<RawKeySource> RawKeySource::Create( std::unique_ptr<RawKeySource> RawKeySource::Create(
const RawKeyParams& raw_key) { const RawKeyParams& raw_key,
int protection_systems_flags) {
std::vector<ProtectionSystemSpecificInfo> key_system_info; std::vector<ProtectionSystemSpecificInfo> key_system_info;
bool pssh_provided = false;
if (!raw_key.pssh.empty()) { if (!raw_key.pssh.empty()) {
pssh_provided = true;
if (!ProtectionSystemSpecificInfo::ParseBoxes( if (!ProtectionSystemSpecificInfo::ParseBoxes(
raw_key.pssh.data(), raw_key.pssh.size(), &key_system_info)) { raw_key.pssh.data(), raw_key.pssh.size(), &key_system_info)) {
LOG(ERROR) << "--pssh argument should be full PSSH boxes."; LOG(ERROR) << "--pssh argument should be full PSSH boxes.";
return std::unique_ptr<RawKeySource>(); return std::unique_ptr<RawKeySource>();
} }
} 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; EncryptionKeyMap encryption_key_map;
@ -149,14 +148,23 @@ std::unique_ptr<RawKeySource> RawKeySource::Create(
encryption_key_map[drm_label] = std::move(encryption_key); encryption_key_map[drm_label] = std::move(encryption_key);
} }
return std::unique_ptr<RawKeySource>( // Generate common protection system if no other protection system is
new RawKeySource(std::move(encryption_key_map))); // specified.
if (!pssh_provided && protection_systems_flags == NO_PROTECTION_SYSTEM_FLAG) {
protection_systems_flags = COMMON_PROTECTION_SYSTEM_FLAG;
}
return std::unique_ptr<RawKeySource>(new RawKeySource(
std::move(encryption_key_map), protection_systems_flags));
} }
RawKeySource::RawKeySource() : KeySource(COMMON_PROTECTION_SYSTEM_FLAG) {} RawKeySource::RawKeySource() : KeySource(NO_PROTECTION_SYSTEM_FLAG) {}
RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map) RawKeySource::RawKeySource(EncryptionKeyMap&& encryption_key_map,
: KeySource(COMMON_PROTECTION_SYSTEM_FLAG), int protection_systems_flags)
encryption_key_map_(std::move(encryption_key_map)) {} : KeySource(protection_systems_flags),
encryption_key_map_(std::move(encryption_key_map)) {
UpdateProtectionSystemInfo(&encryption_key_map_);
}
} // namespace media } // namespace media
} // namespace shaka } // namespace shaka

View File

@ -43,16 +43,16 @@ class RawKeySource : public KeySource {
/// Creates a new RawKeySource from the given data. Returns null /// Creates a new RawKeySource from the given data. Returns null
/// if the parameter is malformed. /// if the parameter is malformed.
/// @param raw_key contains parameters to setup the key source. /// @param raw_key contains parameters to setup the key source.
static std::unique_ptr<RawKeySource> Create(const RawKeyParams& raw_key); static std::unique_ptr<RawKeySource> Create(const RawKeyParams& raw_key,
int protection_system_flags);
protected: protected:
// Allow default constructor for mock key sources. // Allow default constructor for mock key sources.
RawKeySource(); RawKeySource();
private: private:
typedef std::map<std::string, std::unique_ptr<EncryptionKey>> explicit RawKeySource(EncryptionKeyMap&& encryption_key_map,
EncryptionKeyMap; int protection_systems_flags);
explicit RawKeySource(EncryptionKeyMap&& encryption_key_map);
RawKeySource(const RawKeySource&) = delete; RawKeySource(const RawKeySource&) = delete;
RawKeySource& operator=(const RawKeySource&) = delete; RawKeySource& operator=(const RawKeySource&) = delete;

View File

@ -52,6 +52,8 @@ const char kDrmLabel[] = "SomeDrmLabel";
const char kAnotherDrmLabel[] = "AnotherDrmLabel"; const char kAnotherDrmLabel[] = "AnotherDrmLabel";
const char kEmptyDrmLabel[] = ""; const char kEmptyDrmLabel[] = "";
const int kNoProtectionSystemFlag = NO_PROTECTION_SYSTEM_FLAG;
std::vector<uint8_t> HexStringToVector(const std::string& str) { std::vector<uint8_t> HexStringToVector(const std::string& str) {
std::vector<uint8_t> vec; std::vector<uint8_t> vec;
CHECK(base::HexStringToBytes(str, &vec)); CHECK(base::HexStringToBytes(str, &vec));
@ -69,7 +71,7 @@ TEST(RawKeySourceTest, Success) {
raw_key_params.pssh = raw_key_params.pssh =
HexStringToVector(std::string(kPsshBox1Hex) + kPsshBox2Hex); HexStringToVector(std::string(kPsshBox1Hex) + kPsshBox2Hex);
std::unique_ptr<RawKeySource> key_source = std::unique_ptr<RawKeySource> key_source =
RawKeySource::Create(raw_key_params); RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag);
ASSERT_NE(nullptr, key_source); ASSERT_NE(nullptr, key_source);
EncryptionKey key_from_drm_label; EncryptionKey key_from_drm_label;
@ -111,7 +113,7 @@ TEST(RawKeySourceTest, EmptyPssh) {
raw_key_params.key_map[kAnotherDrmLabel].key = HexStringToVector(kKey2Hex); raw_key_params.key_map[kAnotherDrmLabel].key = HexStringToVector(kKey2Hex);
raw_key_params.iv = HexStringToVector(kIvHex); raw_key_params.iv = HexStringToVector(kIvHex);
std::unique_ptr<RawKeySource> key_source = std::unique_ptr<RawKeySource> key_source =
RawKeySource::Create(raw_key_params); RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag);
ASSERT_NE(nullptr, key_source); ASSERT_NE(nullptr, key_source);
EncryptionKey key; EncryptionKey key;
@ -132,13 +134,13 @@ TEST(RawKeySourceTest, Failure) {
raw_key_params.pssh = HexStringToVector(kPsshBox1Hex); raw_key_params.pssh = HexStringToVector(kPsshBox1Hex);
raw_key_params.iv = HexStringToVector(kIvHex); raw_key_params.iv = HexStringToVector(kIvHex);
std::unique_ptr<RawKeySource> key_source = std::unique_ptr<RawKeySource> key_source =
RawKeySource::Create(raw_key_params); RawKeySource::Create(raw_key_params, kNoProtectionSystemFlag);
EXPECT_EQ(nullptr, key_source); EXPECT_EQ(nullptr, key_source);
// Invalid pssh box. // Invalid pssh box.
raw_key_params.key_map[kEmptyDrmLabel].key_id = HexStringToVector(kKeyIdHex); raw_key_params.key_map[kEmptyDrmLabel].key_id = HexStringToVector(kKeyIdHex);
raw_key_params.pssh = HexStringToVector("000102030405"); 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); EXPECT_EQ(nullptr, key_source);
} }