Git Product home page Git Product logo

cpix_cc's Introduction

CPIX C++ Library

This library implements the CPIX 2.2 Specification for the exchange of encryption keys and DRM information.

This library can be used to create, read, and modify CPIX documents.

This is not an officially supported Google product.

Setting up for development

  1. We use bazel to manage the third_party dependencies and to build the library.

  2. Language support

  • STL is required. (e.g. libstdc++, libc++, etc)
  • C++11 is required.
  1. Third party dependencies (downloaded by bazel automatically during compilation) are used when building the library.
  1. Get the source and build
git clone https://github.com/google/cpix_cc.git
cd cpix_cc
bazel build :all
bazel test :all

Quickstart

The CPIXMessage class represents an entire document, so whether you want to create a new document from scratch or read and modify an existing one, start with a CPIXMessage object.

Please refer to examples for examples on how to use the library.

Common Use Cases

Serializing a constructed CPIX document

CPIXMessage message;
...
// Add elements to the message.
...

// Simply call the ToString() function to obtain a std::string of the XML
// generated from the message object.
std::string xml = message.ToString();

Add clear ContentKeys

CPIXMessage message;

std::unique_ptr<ContentKey> key(new ContentKey);
key->set_key_id(kid_raw_bytes);
key->SetKeyValue(key_value_raw_bytes);

message.AddContentKey(std::move(key));

Add encrypted ContentKeys

CPIXMessage message;

std::unique_ptr<ContentKey> key(new ContentKey);
key->set_key_id(kid_raw_bytes);
key->SetKeyValue(key_value_raw_bytes);

message.AddContentKey(std::move(key));

std::unique_ptr<Recipient> rcpt(new Recipient);
rcpt->set_delivery_key(der_formatted_X509_certificate);
message.AddRecipient(std::move(rcpt));

Loading an existing CPIX document

CPIXMessage message;

// After this function call, all content from the supplied document will be
// deserialized into |message|.
message.FromString(string_form_of_cpix_document);

Loading an encrypted CPIX document

CPIXMessage message;

message.FromString(string_form_of_cpix_document);

// If the supplied private key matches one of the certificates in the newly
// deserialized document, the ContentKeys will be decrypted.
message.DecryptWith(der_encoded_private_key);

Add keys with usage rules with different policies

CPIXMessage message;

// First ContentKey and UsageRule
std::unique_ptr<ContentKey> sd_key(new ContentKey);
sd_key->set_key_id(sd_kid_raw_bytes);
sd_key->SetKeyValue(sd_key_value_raw_bytes);

message.AddContentKey(std::move(sd_key));

std::unique_ptr<UsageRule> sd_rule(new UsageRule);

// Create structs (defined in usage_rule.h) for the filters you want.
VideoFilter sd_video_filter;
sd_video_filter.min_pixels = 0;
sd_video_filter.max_pixels = 768 * 576;

// Add filters to the rule. Any valid set of rules, however dense or sparse,
// can be added to a single UsageRule.
sd_rule->AddVideoFilter(sd_video_filter);

// Since each UsageRule maps to a single ContentKey, the |kid_| field MUST be
// populated before adding the UsageRule to a CPIXMessage. See "Adding DRMSystem
// and UsageRule without explicit Key ID" for exception.
sd_rule->set_key_id(sd_key->key_id());

message.AddUsageRule(std::move(sd_rule));

// Second ContentKey and UsageRule
std::unique_ptr<ContentKey> hd_key(new ContentKey);
hd_key->set_key_id(hd_kid_raw_bytes);
hd_key->SetKeyValue(hd_key_value_raw_bytes);

message.AddContentKey(std::move(hd_key));

std::unique_ptr<UsageRule> hd_rule(new UsageRule);

VideoFilter hd_video_filter;
hd_video_filter.min_pixels = 768 * 576 + 1;
hd_video_filter.max_pixels = 1920 * 1080;

hd_rule->AddVideoFilter(hd_video_filter);

hd_rule->set_key_id(hd_key->key_id());

message.AddUsageRule(std::move(hd_rule));

Add keys with usage rules with different labels

CPIXMessage message;

// First ContentKey and UsageRule
std::unique_ptr<ContentKey> sd_key(new ContentKey);
sd_key->set_key_id(sd_kid_raw_bytes);
sd_key->SetKeyValue(sd_key_value_raw_bytes);

message.AddContentKey(std::move(sd_key));

std::unique_ptr<UsageRule> sd_rule(new UsageRule);

sd_rule.AddLabelFilter("SD");

// Since each UsageRule maps to a single ContentKey, the |kid_| field MUST be
// populated before adding the UsageRule to a CPIXMessage. See "Adding DRMSystem
// and UsageRule without explicit Key ID" for exception.
sd_rule->set_key_id(sd_key->key_id());

message.AddUsageRule(std::move(sd_rule));

// Second ContentKey and UsageRule
std::unique_ptr<ContentKey> hd_key(new ContentKey);
hd_key->set_key_id(hd_kid_raw_bytes);
hd_key->SetKeyValue(hd_key_value_raw_bytes);

message.AddContentKey(std::move(hd_key));

std::unique_ptr<UsageRule> hd_rule(new UsageRule);

hd_rule->AddLabelFilter("HD");

hd_rule->set_key_id(hd_key->key_id());

message.AddUsageRule(std::move(hd_rule));

Creating UsageRules with KeyPeriod

// Create a new KeyPeriodFilter and set the period by index or explicit
// time interval.
std::unique_ptr<KeyPeriod> key_period(new KeyPeriod);
key_period->SetIndex(1001);

// Set the KeyPeriod's |id| (to be referenced by a UsageRule).
key_period->set_id("key_period_1");

// Add it to some existing message.
message.AddKeyPeriod(std::move(key_period));

// Create a new UsageRule
std::unique_ptr<UsageRule> rule(new UsageRule);
//Set the Key ID
rule->set_key_id(kid_raw_bytes_of_existing_content_key);

// Add a new KeyPeriodFilter by referencing the |id| of an existing filter.
// Multiple UsageRules may reference the same KeyPeriod.
rule->AddKeyPeriodFilter("key_period_1");

// Add the UsageRule to the same existing message.
message.AddUsageRule(std::move(rule));

Creating DRMSystems

std::unique_ptr<DRMSystem> drm(new DRMSystem);
// System ID is a required field of DRMSystem.
drm->set_system_id(system_id_raw_bytes);

// Any or all of these data fields may be set for DRMSystem. See DRMSystem.h for
// a complete list.
drm->set_content_protection_data(content_protection_data_raw_bytes);
drm->set_hls_signaling_master(hls_signaling_master_raw_bytes);
drm->set_hls_signaling_media(hls_signaling_media_raw_bytes);

// Since each DRMSystem maps to a single ContentKey, the |kid_| field MUST be
// populated before adding the UsageRule to a CPIXMessage. See "Adding DRMSystem
// and UsageRule without explicit Key ID" for exception.
drm->set_key_id(kid_raw_bytes_of_existing_content_key);

// Add the DRMSystem to some existing message.
message.AddDRMSystem(std::move(drm));

Adding DRMSystem and UsageRule without explicit Key ID

CPIXMessage message;

std::unique_ptr<ContentKey> key(new ContentKey);
key->set_key_id(kid_raw_bytes);
key->SetKeyValue(key_value_raw_bytes);

std::unique_ptr<UsageRule> rule(new UsageRule);
...
// Add any filters.
...
// Notice we do not explicitly set the KeyID.

std::vector<std::unique_ptr<UsageRule>> rules;
rules.push_back(std::move(rule));

std::unique_ptr<DRMSystem> drm(new DRMSystem);
drm->set_system_id(system_id_raw_bytes);
...
// Add any DRM signaling information
...
// Notice we do not explicitly set the KeyID.

std::vector<std::unique_ptr<DRMSystem>> drms;
drms.push_back(std::move(drm));

// All DRMSystems and UsageRules in the two supplied vectors have their Key ID
// fields auto-populated to match the Key ID of the supplied ContentKey.
message.AddContentKey(std::move(key), std::move(drms), std::move(rules));

Contributing

If you have improvements or fixes, we would love to have your contributions. See https://github.com/google/cpix_cc/blob/master/CONTRIBUTING.md for details.

cpix_cc's People

Contributors

noahdavis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpix_cc's Issues

Linking issue when building

Following the instructions on the README.md page, so

  • ubuntu 18.04 install (updated)
  • bazel installed (any verson from 0.21.0 to 3.5.0)
  • clone cpix_cc repo
  • bazel build :all
  • bazel test :all

Then attempt to build the example code and you get the following linking errors.
(Only attempted to build the clear_content_keys.cc example)

(.text+0x20): undefined reference to `main'
/tmp/cc4q3HqR.o: In function `cpix::(anonymous namespace)::main()':
clear_content_keys.cc:(.text+0x1ae): undefined reference to `cpix::CPIXMessage::CPIXMessage()'
clear_content_keys.cc:(.text+0x21d): undefined reference to `cpix::ContentKey::SetKeyValue(std::vector<unsigned char, std::allocator<unsigned char> > const&)'
clear_content_keys.cc:(.text+0x25a): undefined reference to `cpix::CPIXMessage::AddContentKey(std::unique_ptr<cpix::ContentKey, std::default_delete<cpix::ContentKey> >)'
clear_content_keys.cc:(.text+0x2d8): undefined reference to `cpix::ContentKey::SetKeyValue(std::vector<unsigned char, std::allocator<unsigned char> > const&)'
clear_content_keys.cc:(.text+0x315): undefined reference to `cpix::CPIXMessage::AddContentKey(std::unique_ptr<cpix::ContentKey, std::default_delete<cpix::ContentKey> >)'
clear_content_keys.cc:(.text+0x39c): undefined reference to `cpix::CPIXMessage::~CPIXMessage()'
clear_content_keys.cc:(.text+0x4c8): undefined reference to `cpix::CPIXMessage::~CPIXMessage()'
/tmp/cc4q3HqR.o: In function `cpix::CPIXMessage::ToString[abi:cxx11]()':
clear_content_keys.cc:(.text._ZN4cpix11CPIXMessage8ToStringB5cxx11Ev[_ZN4cpix11CPIXMessage8ToStringB5cxx11Ev]+0x2e): undefined reference to `cpix::CPIXElement::Serialize[abi:cxx11]()'
/tmp/cc4q3HqR.o: In function `cpix::CPIXElement::CPIXElement()':
clear_content_keys.cc:(.text._ZN4cpix11CPIXElementC2Ev[_ZN4cpix11CPIXElementC5Ev]+0xf): undefined reference to `vtable for cpix::CPIXElement'
/tmp/cc4q3HqR.o: In function `cpix::ContentKey::ContentKey()':
clear_content_keys.cc:(.text._ZN4cpix10ContentKeyC2Ev[_ZN4cpix10ContentKeyC5Ev]+0x1b): undefined reference to `vtable for cpix::ContentKey'
collect2: error: ld returned 1 exit status
./build.sh: line 34: ./out: No such file or directory

Is there something I am doing incorrectly?

I get exactly the same error if I build the library, include it in my own project and attempt to access any of the functions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.