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