2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2017 Google LLC. All rights reserved.
|
2017-02-02 18:28:29 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#ifndef PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
|
|
|
|
#define PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/crypto_params.h>
|
|
|
|
#include <packager/media/base/key_source.h>
|
|
|
|
#include <packager/media/base/media_handler.h>
|
2017-02-02 18:28:29 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class AesCryptor;
|
2018-10-04 20:24:21 +00:00
|
|
|
class AesEncryptorFactory;
|
2018-10-02 23:08:32 +00:00
|
|
|
class SubsampleGenerator;
|
2017-02-02 18:28:29 +00:00
|
|
|
struct EncryptionKey;
|
|
|
|
|
|
|
|
class EncryptionHandler : public MediaHandler {
|
|
|
|
public:
|
2017-07-05 23:47:55 +00:00
|
|
|
EncryptionHandler(const EncryptionParams& encryption_params,
|
2017-02-02 18:28:29 +00:00
|
|
|
KeySource* key_source);
|
|
|
|
|
|
|
|
~EncryptionHandler() override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// @name MediaHandler implementation overrides.
|
|
|
|
/// @{
|
|
|
|
Status InitializeInternal() override;
|
|
|
|
Status Process(std::unique_ptr<StreamData> stream_data) override;
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class EncryptionHandlerTest;
|
|
|
|
|
|
|
|
EncryptionHandler(const EncryptionHandler&) = delete;
|
|
|
|
EncryptionHandler& operator=(const EncryptionHandler&) = delete;
|
|
|
|
|
|
|
|
// Processes |stream_info| and sets up stream specific variables.
|
2017-09-12 17:24:24 +00:00
|
|
|
Status ProcessStreamInfo(const StreamInfo& stream_info);
|
Implement ChunkingHandler
This handler is a multi-in multi-out handler. If more than one input is
provided, there should be one and only one video stream; also, all inputs
should come from the same thread and are synchronized.
There can be multiple chunking handler running in different threads or even
different processes, we use the "consistent chunking algorithm" to make sure
the chunks in different streams are aligned without explicit communcating
with each other - which is not efficient and often difficult.
Consistent Chunking Algorithm:
1. Find the consistent chunkable boundary
Let the timestamps for video frames be (t1, t2, t3, ...). Then a
consistent chunkable boundary is simply the first chunkable boundary after
(tk / N) != (tk-1 / N), where '/' denotes integer division, and N is the
intended chunk duration.
2. Chunk only at the consistent chunkable boundary
This algorithm will make sure the chunks from different video streams are
aligned if they have aligned GoPs. However, this algorithm will only work
for video streams. To be able to chunk non video streams at similar
positions as video streams, ChunkingHandler is designed to accept one video
input and multiple non video inputs, the non video inputs are chunked when
the video input is chunked. If the inputs are synchronized - which is true
if the inputs come from the same demuxer, the video and non video chunks
are aligned.
Change-Id: Id3bad51ab14f311efdb8713b6cd36d36cf9e4639
2017-02-07 18:58:47 +00:00
|
|
|
// Processes media sample and encrypts it if needed.
|
2017-09-12 17:24:24 +00:00
|
|
|
Status ProcessMediaSample(std::shared_ptr<const MediaSample> clear_sample);
|
2017-02-02 18:28:29 +00:00
|
|
|
|
2018-10-02 23:08:32 +00:00
|
|
|
void SetupProtectionPattern(StreamType stream_type);
|
2017-03-11 02:48:04 +00:00
|
|
|
bool CreateEncryptor(const EncryptionKey& encryption_key);
|
2017-11-07 22:08:25 +00:00
|
|
|
// Encrypt an E-AC3 frame with size |source_size| according to SAMPLE-AES
|
|
|
|
// specification. |dest| should have at least |source_size| bytes.
|
|
|
|
bool SampleAesEncryptEac3Frame(const uint8_t* source,
|
|
|
|
size_t source_size,
|
|
|
|
uint8_t* dest);
|
2017-09-18 23:31:00 +00:00
|
|
|
// Encrypt an array with size |source_size|. |dest| should have at
|
|
|
|
// least |source_size| bytes.
|
2023-12-01 17:32:19 +00:00
|
|
|
void EncryptBytes(const uint8_t* source,
|
|
|
|
size_t source_size,
|
|
|
|
uint8_t* dest,
|
|
|
|
size_t dest_size);
|
2017-02-02 18:28:29 +00:00
|
|
|
|
2017-11-07 22:08:25 +00:00
|
|
|
// An E-AC3 frame comprises of one or more syncframes. This function extracts
|
|
|
|
// the syncframe sizes from the source bytes.
|
|
|
|
// Returns false if the frame is not well formed.
|
|
|
|
bool ExtractEac3SyncframeSizes(const uint8_t* source,
|
|
|
|
size_t source_size,
|
|
|
|
std::vector<size_t>* syncframe_sizes);
|
|
|
|
|
2017-02-02 18:28:29 +00:00
|
|
|
// Testing injections.
|
2018-10-02 23:08:32 +00:00
|
|
|
void InjectSubsampleGeneratorForTesting(
|
|
|
|
std::unique_ptr<SubsampleGenerator> generator);
|
2018-10-04 20:24:21 +00:00
|
|
|
void InjectEncryptorFactoryForTesting(
|
|
|
|
std::unique_ptr<AesEncryptorFactory> encryptor_factory);
|
2017-02-02 18:28:29 +00:00
|
|
|
|
2017-07-05 23:47:55 +00:00
|
|
|
const EncryptionParams encryption_params_;
|
|
|
|
const FourCC protection_scheme_ = FOURCC_NULL;
|
2017-02-02 18:28:29 +00:00
|
|
|
KeySource* key_source_ = nullptr;
|
2017-06-13 21:54:12 +00:00
|
|
|
std::string stream_label_;
|
2017-03-11 02:48:04 +00:00
|
|
|
// Current encryption config and encryptor.
|
|
|
|
std::shared_ptr<EncryptionConfig> encryption_config_;
|
2017-02-02 18:28:29 +00:00
|
|
|
std::unique_ptr<AesCryptor> encryptor_;
|
2017-03-11 02:48:04 +00:00
|
|
|
Codec codec_ = kUnknownCodec;
|
2017-02-02 18:28:29 +00:00
|
|
|
// Remaining clear lead in the stream's time scale.
|
|
|
|
int64_t remaining_clear_lead_ = 0;
|
|
|
|
// Crypto period duration in the stream's time scale.
|
2021-08-04 18:56:44 +00:00
|
|
|
int64_t crypto_period_duration_ = 0;
|
2017-02-02 18:28:29 +00:00
|
|
|
// Previous crypto period index if key rotation is enabled.
|
|
|
|
int64_t prev_crypto_period_index_ = -1;
|
2017-03-11 02:48:04 +00:00
|
|
|
bool check_new_crypto_period_ = false;
|
2017-02-02 18:28:29 +00:00
|
|
|
|
2018-10-02 23:08:32 +00:00
|
|
|
std::unique_ptr<SubsampleGenerator> subsample_generator_;
|
|
|
|
std::unique_ptr<AesEncryptorFactory> encryptor_factory_;
|
2017-02-02 18:28:29 +00:00
|
|
|
// Number of encrypted blocks (16-byte-block) in pattern based encryption.
|
|
|
|
uint8_t crypt_byte_block_ = 0;
|
|
|
|
/// Number of unencrypted blocks (16-byte-block) in pattern based encryption.
|
|
|
|
uint8_t skip_byte_block_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|
|
|
|
|
|
|
|
#endif // PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
|