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 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 E-AC3 frame with size |source_size| according to SAMPLE-AES
64  // specification. |dest| should have at least |source_size| bytes.
65  bool SampleAesEncryptEac3Frame(const uint8_t* source,
66  size_t source_size,
67  uint8_t* dest);
68  // Encrypt an array with size |source_size|. |dest| should have at
69  // least |source_size| bytes.
70  void EncryptBytes(const uint8_t* source, size_t source_size, uint8_t* dest);
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 InjectVpxParserForTesting(std::unique_ptr<VPxParser> vpx_parser);
81  void InjectVideoSliceHeaderParserForTesting(
82  std::unique_ptr<VideoSliceHeaderParser> header_parser);
83 
84  const EncryptionParams encryption_params_;
85  const FourCC protection_scheme_ = FOURCC_NULL;
86  KeySource* key_source_ = nullptr;
87  std::string stream_label_;
88  // Current encryption config and encryptor.
89  std::shared_ptr<EncryptionConfig> encryption_config_;
90  std::unique_ptr<AesCryptor> encryptor_;
91  Codec codec_ = kUnknownCodec;
92  // Specifies the size of NAL unit length in bytes. Can be 1, 2 or 4 bytes. 0
93  // if it is not a NAL structured video.
94  uint8_t nalu_length_size_ = 0;
95  // For Sample AES, 32 bytes for Video and 16 bytes for audio.
96  size_t leading_clear_bytes_size_ = 0;
97  // For Sample AES, if the data size is less than this value, none of the bytes
98  // are encrypted. The size is 48+1 bytes for video NAL and 16+15 bytes for
99  // audio according to MPEG-2 Stream Encryption Format for HTTP Live Streaming.
100  size_t min_protected_data_size_ = 0;
101  // Remaining clear lead in the stream's time scale.
102  int64_t remaining_clear_lead_ = 0;
103  // Crypto period duration in the stream's time scale.
104  uint64_t crypto_period_duration_ = 0;
105  // Previous crypto period index if key rotation is enabled.
106  int64_t prev_crypto_period_index_ = -1;
107  bool check_new_crypto_period_ = false;
108 
109  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
110  uint8_t crypt_byte_block_ = 0;
112  uint8_t skip_byte_block_ = 0;
113 
114  // VPx parser for VPx streams.
115  std::unique_ptr<VPxParser> vpx_parser_;
116  // Video slice header parser for NAL strucutred streams.
117  std::unique_ptr<VideoSliceHeaderParser> header_parser_;
118 };
119 
120 } // namespace media
121 } // namespace shaka
122 
123 #endif // PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
Abstract class holds stream information.
Definition: stream_info.h:58
All the methods that are virtual are virtual for mocking.
Status Process(std::unique_ptr< StreamData > stream_data) override
Encryption parameters.
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:45