Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for cenc and cbcs encryption for fmp4 output #12

Merged
merged 7 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/packager/live_packager.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ struct LiveConfig {
NONE,
SAMPLE_AES,
AES_128,
CBCS,
CENC,
};

OutputFormat format;
Expand All @@ -85,7 +87,7 @@ struct LiveConfig {
std::vector<uint8_t> iv;
std::vector<uint8_t> key;
std::vector<uint8_t> key_id;
EncryptionScheme protection_scheme;
EncryptionScheme protection_scheme = EncryptionScheme::NONE;

/// User-specified segment number.
/// For FMP4 output:
Expand Down
43 changes: 28 additions & 15 deletions packager/live_packager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <packager/chunking_params.h>
#include <packager/file.h>
#include <packager/live_packager.h>
#include <packager/macros/compiler.h>
#include <packager/media/base/aes_encryptor.h>
#include <packager/packager.h>

Expand Down Expand Up @@ -249,6 +250,8 @@ Status LivePackager::PackageInit(const Segment& init_segment,
packaging_params.chunking_params.segment_duration_in_seconds =
config_.segment_duration_sec;

packaging_params.mp4_output_params.include_pssh_in_stream = false;

// in order to enable init packaging as a separate execution.
packaging_params.init_segment_only = true;

Expand Down Expand Up @@ -309,6 +312,7 @@ Status LivePackager::Package(const Segment& init_segment,
config_.segment_duration_sec;

packaging_params.mp4_output_params.sequence_number = config_.segment_number;
packaging_params.mp4_output_params.include_pssh_in_stream = false;

EncryptionParams& encryption_params = packaging_params.encryption_params;
// As a side effect of InitializeEncryption, encryption_params will be
Expand Down Expand Up @@ -345,25 +349,34 @@ int64_t SegmentManager::OnSegmentWrite(const std::string& name,
Status SegmentManager::InitializeEncryption(
const LiveConfig& config,
EncryptionParams& encryption_params) {
// TODO: encryption for fmp4 will be added later
if (config.protection_scheme == LiveConfig::EncryptionScheme::SAMPLE_AES) {
// Internally shaka maps this to an internal code for sample aes
//
// This is a fake protection scheme fourcc code to indicate Apple Sample
// AES. FOURCC_cbca = 0x63626361,
//
switch (config.protection_scheme) {
case LiveConfig::EncryptionScheme::NONE:
return Status::OK;
// Internally shaka maps sample-aes to cbcs.
// Additionally this seems to be the recommended protection schema to when
// using the shaka CLI:
// https://shaka-project.github.io/shaka-packager/html/tutorials/raw_key.html
encryption_params.protection_scheme =
EncryptionParams::kProtectionSchemeCbcs;

encryption_params.key_provider = KeyProvider::kRawKey;
RawKeyParams::KeyInfo& key_info = encryption_params.raw_key.key_map[""];
key_info.key = config.key;
key_info.key_id = config.key_id;
key_info.iv = config.iv;
case LiveConfig::EncryptionScheme::SAMPLE_AES:
FALLTHROUGH_INTENDED;
case LiveConfig::EncryptionScheme::CBCS:
encryption_params.protection_scheme =
EncryptionParams::kProtectionSchemeCbcs;
break;
case LiveConfig::EncryptionScheme::CENC:
encryption_params.protection_scheme =
EncryptionParams::kProtectionSchemeCenc;
break;
default:
return Status(error::INVALID_ARGUMENT,
"invalid encryption scheme provided to LivePackager.");
}

encryption_params.key_provider = KeyProvider::kRawKey;
RawKeyParams::KeyInfo& key_info = encryption_params.raw_key.key_map[""];
key_info.key = config.key;
key_info.key_id = config.key_id;
key_info.iv = config.iv;

return Status::OK;
}

Expand Down
Loading