Shaka Packager SDK
Loading...
Searching...
No Matches
encryption_handler.h
1// Copyright 2017 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#ifndef PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
8#define PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
9
10#include <cstdint>
11
12#include <packager/crypto_params.h>
13#include <packager/media/base/key_source.h>
14#include <packager/media/base/media_handler.h>
15
16namespace shaka {
17namespace media {
18
19class AesCryptor;
20class AesEncryptorFactory;
21class SubsampleGenerator;
22struct EncryptionKey;
23
25 public:
26 EncryptionHandler(const EncryptionParams& encryption_params,
27 KeySource* key_source);
28
29 ~EncryptionHandler() override;
30
31 protected:
34 Status InitializeInternal() override;
35 Status Process(std::unique_ptr<StreamData> stream_data) override;
37
38 private:
39 friend class EncryptionHandlerTest;
40
41 EncryptionHandler(const EncryptionHandler&) = delete;
42 EncryptionHandler& operator=(const EncryptionHandler&) = delete;
43
44 // Processes |stream_info| and sets up stream specific variables.
45 Status ProcessStreamInfo(const StreamInfo& stream_info);
46 // Processes media sample and encrypts it if needed.
47 Status ProcessMediaSample(std::shared_ptr<const MediaSample> clear_sample);
48
49 void SetupProtectionPattern(StreamType stream_type);
50 bool CreateEncryptor(const EncryptionKey& encryption_key);
51 // Encrypt an E-AC3 frame with size |source_size| according to SAMPLE-AES
52 // specification. |dest| should have at least |source_size| bytes.
53 bool SampleAesEncryptEac3Frame(const uint8_t* source,
54 size_t source_size,
55 uint8_t* dest);
56 // Encrypt an array with size |source_size|. |dest| should have at
57 // least |source_size| bytes.
58 void EncryptBytes(const uint8_t* source,
59 size_t source_size,
60 uint8_t* dest,
61 size_t dest_size);
62
63 // An E-AC3 frame comprises of one or more syncframes. This function extracts
64 // the syncframe sizes from the source bytes.
65 // Returns false if the frame is not well formed.
66 bool ExtractEac3SyncframeSizes(const uint8_t* source,
67 size_t source_size,
68 std::vector<size_t>* syncframe_sizes);
69
70 // Testing injections.
71 void InjectSubsampleGeneratorForTesting(
72 std::unique_ptr<SubsampleGenerator> generator);
73 void InjectEncryptorFactoryForTesting(
74 std::unique_ptr<AesEncryptorFactory> encryptor_factory);
75
76 const EncryptionParams encryption_params_;
77 const FourCC protection_scheme_ = FOURCC_NULL;
78 KeySource* key_source_ = nullptr;
79 std::string stream_label_;
80 // Current encryption config and encryptor.
81 std::shared_ptr<EncryptionConfig> encryption_config_;
82 std::unique_ptr<AesCryptor> encryptor_;
83 Codec codec_ = kUnknownCodec;
84 // Remaining clear lead in the stream's time scale.
85 int64_t remaining_clear_lead_ = 0;
86 // Crypto period duration in the stream's time scale.
87 int64_t crypto_period_duration_ = 0;
88 // Previous crypto period index if key rotation is enabled.
89 int64_t prev_crypto_period_index_ = -1;
90 bool check_new_crypto_period_ = false;
91
92 std::unique_ptr<SubsampleGenerator> subsample_generator_;
93 std::unique_ptr<AesEncryptorFactory> encryptor_factory_;
94 // Number of encrypted blocks (16-byte-block) in pattern based encryption.
95 uint8_t crypt_byte_block_ = 0;
97 uint8_t skip_byte_block_ = 0;
98};
99
100} // namespace media
101} // namespace shaka
102
103#endif // PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
Status Process(std::unique_ptr< StreamData > stream_data) override
KeySource is responsible for encryption key acquisition.
Definition key_source.h:53
Abstract class holds stream information.
Definition stream_info.h:72
All the methods that are virtual are virtual for mocking.