Shaka Packager SDK
encryption_handler.h
1 // Copyright 2017 Google Inc. 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/media/base/key_source.h"
11 #include "packager/media/base/media_handler.h"
12 #include "packager/media/public/crypto_params.h"
13 
14 namespace shaka {
15 namespace media {
16 
17 class AesCryptor;
18 class AesEncryptorFactory;
19 class SubsampleGenerator;
20 struct 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, size_t source_size, uint8_t* dest);
57 
58  // An E-AC3 frame comprises of one or more syncframes. This function extracts
59  // the syncframe sizes from the source bytes.
60  // Returns false if the frame is not well formed.
61  bool ExtractEac3SyncframeSizes(const uint8_t* source,
62  size_t source_size,
63  std::vector<size_t>* syncframe_sizes);
64 
65  // Testing injections.
66  void InjectSubsampleGeneratorForTesting(
67  std::unique_ptr<SubsampleGenerator> generator);
68  void InjectEncryptorFactoryForTesting(
69  std::unique_ptr<AesEncryptorFactory> encryptor_factory);
70 
71  const EncryptionParams encryption_params_;
72  const FourCC protection_scheme_ = FOURCC_NULL;
73  KeySource* key_source_ = nullptr;
74  std::string stream_label_;
75  // Current encryption config and encryptor.
76  std::shared_ptr<EncryptionConfig> encryption_config_;
77  std::unique_ptr<AesCryptor> encryptor_;
78  Codec codec_ = kUnknownCodec;
79  // Remaining clear lead in the stream's time scale.
80  int64_t remaining_clear_lead_ = 0;
81  // Crypto period duration in the stream's time scale.
82  uint64_t crypto_period_duration_ = 0;
83  // Previous crypto period index if key rotation is enabled.
84  int64_t prev_crypto_period_index_ = -1;
85  bool check_new_crypto_period_ = false;
86 
87  std::unique_ptr<SubsampleGenerator> subsample_generator_;
88  std::unique_ptr<AesEncryptorFactory> encryptor_factory_;
89  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
90  uint8_t crypt_byte_block_ = 0;
92  uint8_t skip_byte_block_ = 0;
93 };
94 
95 } // namespace media
96 } // namespace shaka
97 
98 #endif // PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
shaka
All the methods that are virtual are virtual for mocking.
Definition: gflags_hex_bytes.cc:11
shaka::media::EncryptionHandler
Definition: encryption_handler.h:22
shaka::Status
Definition: status.h:110
shaka::media::MediaHandler
Definition: media_handler.h:154
shaka::media::KeySource
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:51
shaka::media::EncryptionHandler::InitializeInternal
Status InitializeInternal() override
Definition: encryption_handler.cc:175
shaka::EncryptionParams
Encryption parameters.
Definition: crypto_params.h:146
shaka::media::EncryptionHandler::Process
Status Process(std::unique_ptr< StreamData > stream_data) override
Definition: encryption_handler.cc:186
shaka::media::EncryptionKey
Definition: key_source.h:38
shaka::media::StreamInfo
Abstract class holds stream information.
Definition: stream_info.h:65