Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 VideoSliceHeaderParser;
19 class VPxParser;
20 struct EncryptionKey;
21 struct VPxFrameInfo;
22 
24  public:
25  EncryptionHandler(const EncryptionParams& encryption_params,
26  KeySource* key_source);
27 
28  ~EncryptionHandler() override;
29 
30  protected:
33  Status InitializeInternal() override;
34  Status Process(std::unique_ptr<StreamData> stream_data) override;
36 
37  private:
38  friend class EncryptionHandlerTest;
39 
40  EncryptionHandler(const EncryptionHandler&) = delete;
41  EncryptionHandler& operator=(const EncryptionHandler&) = delete;
42 
43  // Processes |stream_info| and sets up stream specific variables.
44  Status ProcessStreamInfo(const StreamInfo& stream_info);
45  // Processes media sample and encrypts it if needed.
46  Status ProcessMediaSample(std::shared_ptr<const MediaSample> clear_sample);
47 
48  Status SetupProtectionPattern(StreamType stream_type);
49  bool CreateEncryptor(const EncryptionKey& encryption_key);
50  bool EncryptVpxFrame(const std::vector<VPxFrameInfo>& vpx_frames,
51  uint8_t* source,
52  size_t source_size,
53  DecryptConfig* decrypt_config);
54  bool EncryptNalFrame(uint8_t* data,
55  size_t data_length,
56  DecryptConfig* decrypt_config);
57  void EncryptBytes(uint8_t* data,
58  size_t size);
59 
60  // Testing injections.
61  void InjectVpxParserForTesting(std::unique_ptr<VPxParser> vpx_parser);
62  void InjectVideoSliceHeaderParserForTesting(
63  std::unique_ptr<VideoSliceHeaderParser> header_parser);
64 
65  const EncryptionParams encryption_params_;
66  const FourCC protection_scheme_ = FOURCC_NULL;
67  KeySource* key_source_ = nullptr;
68  std::string stream_label_;
69  // Current encryption config and encryptor.
70  std::shared_ptr<EncryptionConfig> encryption_config_;
71  std::unique_ptr<AesCryptor> encryptor_;
72  Codec codec_ = kUnknownCodec;
73  // Specifies the size of NAL unit length in bytes. Can be 1, 2 or 4 bytes. 0
74  // if it is not a NAL structured video.
75  uint8_t nalu_length_size_ = 0;
76  // For Sample AES, 32 bytes for Video and 16 bytes for audio.
77  size_t leading_clear_bytes_size_ = 0;
78  // For Sample AES, 48+1 bytes for video NAL and 16+1 bytes for audio.
79  size_t min_protected_data_size_ = 0;
80  // Remaining clear lead in the stream's time scale.
81  int64_t remaining_clear_lead_ = 0;
82  // Crypto period duration in the stream's time scale.
83  uint64_t crypto_period_duration_ = 0;
84  // Previous crypto period index if key rotation is enabled.
85  int64_t prev_crypto_period_index_ = -1;
86  bool check_new_crypto_period_ = false;
87 
88  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
89  uint8_t crypt_byte_block_ = 0;
91  uint8_t skip_byte_block_ = 0;
92 
93  // VPx parser for VPx streams.
94  std::unique_ptr<VPxParser> vpx_parser_;
95  // Video slice header parser for NAL strucutred streams.
96  std::unique_ptr<VideoSliceHeaderParser> header_parser_;
97 };
98 
99 } // namespace media
100 } // namespace shaka
101 
102 #endif // PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
Abstract class holds stream information.
Definition: stream_info.h:58
Status Process(std::unique_ptr< StreamData > stream_data) override
Encryption parameters.
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:45