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  // Encrypt a VPx frame with size |source_size|. |dest| should have at least
51  // |source_size| bytes.
52  bool EncryptVpxFrame(const std::vector<VPxFrameInfo>& vpx_frames,
53  const uint8_t* source,
54  size_t source_size,
55  uint8_t* dest,
56  DecryptConfig* decrypt_config);
57  // Encrypt a NAL unit frame with size |source_size|. |dest| should have at
58  // least |source_size| bytes.
59  bool EncryptNalFrame(const uint8_t* source,
60  size_t source_size,
61  uint8_t* dest,
62  DecryptConfig* decrypt_config);
63  // Encrypt an array with size |source_size|. |dest| should have at
64  // least |source_size| bytes.
65  void EncryptBytes(const uint8_t* source, size_t source_size, uint8_t* dest);
66 
67  // Testing injections.
68  void InjectVpxParserForTesting(std::unique_ptr<VPxParser> vpx_parser);
69  void InjectVideoSliceHeaderParserForTesting(
70  std::unique_ptr<VideoSliceHeaderParser> header_parser);
71 
72  const EncryptionParams encryption_params_;
73  const FourCC protection_scheme_ = FOURCC_NULL;
74  KeySource* key_source_ = nullptr;
75  std::string stream_label_;
76  // Current encryption config and encryptor.
77  std::shared_ptr<EncryptionConfig> encryption_config_;
78  std::unique_ptr<AesCryptor> encryptor_;
79  Codec codec_ = kUnknownCodec;
80  // Specifies the size of NAL unit length in bytes. Can be 1, 2 or 4 bytes. 0
81  // if it is not a NAL structured video.
82  uint8_t nalu_length_size_ = 0;
83  // For Sample AES, 32 bytes for Video and 16 bytes for audio.
84  size_t leading_clear_bytes_size_ = 0;
85  // For Sample AES, 48+1 bytes for video NAL and 16+1 bytes for audio.
86  size_t min_protected_data_size_ = 0;
87  // Remaining clear lead in the stream's time scale.
88  int64_t remaining_clear_lead_ = 0;
89  // Crypto period duration in the stream's time scale.
90  uint64_t crypto_period_duration_ = 0;
91  // Previous crypto period index if key rotation is enabled.
92  int64_t prev_crypto_period_index_ = -1;
93  bool check_new_crypto_period_ = false;
94 
95  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
96  uint8_t crypt_byte_block_ = 0;
98  uint8_t skip_byte_block_ = 0;
99 
100  // VPx parser for VPx streams.
101  std::unique_ptr<VPxParser> vpx_parser_;
102  // Video slice header parser for NAL strucutred streams.
103  std::unique_ptr<VideoSliceHeaderParser> header_parser_;
104 };
105 
106 } // namespace media
107 } // namespace shaka
108 
109 #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