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