DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 
13 namespace shaka {
14 namespace media {
15 
16 class AesCryptor;
17 class VideoSliceHeaderParser;
18 class VPxParser;
19 struct EncryptionKey;
20 struct VPxFrameInfo;
21 
27  FourCC protection_scheme = FOURCC_cenc;
31  uint32_t max_sd_pixels = 0;
35  uint32_t max_hd_pixels = 0;
40  uint32_t max_uhd1_pixels = 0;
44  // Enable/disable subsample encryption for VP9.
45  bool vp9_subsample_encryption = true;
46 };
47 
49  public:
50  EncryptionHandler(const EncryptionOptions& encryption_options,
51  KeySource* key_source);
52 
53  ~EncryptionHandler() override;
54 
55  protected:
58  Status InitializeInternal() override;
59  Status Process(std::unique_ptr<StreamData> stream_data) override;
61 
62  private:
63  friend class EncryptionHandlerTest;
64 
65  EncryptionHandler(const EncryptionHandler&) = delete;
66  EncryptionHandler& operator=(const EncryptionHandler&) = delete;
67 
68  // Processes |stream_info| and sets up stream specific variables.
69  Status ProcessStreamInfo(StreamInfo* stream_info);
70  // Processes media sample and encrypts it if needed.
71  Status ProcessMediaSample(MediaSample* sample);
72 
73  Status SetupProtectionPattern(StreamType stream_type);
74  bool CreateEncryptor(const EncryptionKey& encryption_key);
75  bool EncryptVpxFrame(const std::vector<VPxFrameInfo>& vpx_frames,
76  MediaSample* sample,
77  DecryptConfig* decrypt_config);
78  bool EncryptNalFrame(MediaSample* sample, DecryptConfig* decrypt_config);
79  void EncryptBytes(uint8_t* data, size_t size);
80 
81  // Testing injections.
82  void InjectVpxParserForTesting(std::unique_ptr<VPxParser> vpx_parser);
83  void InjectVideoSliceHeaderParserForTesting(
84  std::unique_ptr<VideoSliceHeaderParser> header_parser);
85 
86  const EncryptionOptions encryption_options_;
87  KeySource* key_source_ = nullptr;
88  KeySource::TrackType track_type_ = KeySource::TRACK_TYPE_UNKNOWN;
89  // Current encryption config and encryptor.
90  std::shared_ptr<EncryptionConfig> encryption_config_;
91  std::unique_ptr<AesCryptor> encryptor_;
92  Codec codec_ = kUnknownCodec;
93  // Specifies the size of NAL unit length in bytes. Can be 1, 2 or 4 bytes. 0
94  // if it is not a NAL structured video.
95  uint8_t nalu_length_size_ = 0;
96  // For Sample AES, 32 bytes for Video and 16 bytes for audio.
97  size_t leading_clear_bytes_size_ = 0;
98  // For Sample AES, 48+1 bytes for video NAL and 16+1 bytes for audio.
99  size_t min_protected_data_size_ = 0;
100  // Remaining clear lead in the stream's time scale.
101  int64_t remaining_clear_lead_ = 0;
102  // Crypto period duration in the stream's time scale.
103  uint64_t crypto_period_duration_ = 0;
104  // Previous crypto period index if key rotation is enabled.
105  int64_t prev_crypto_period_index_ = -1;
106  bool check_new_crypto_period_ = false;
107 
108  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
109  uint8_t crypt_byte_block_ = 0;
111  uint8_t skip_byte_block_ = 0;
112 
113  // VPx parser for VPx streams.
114  std::unique_ptr<VPxParser> vpx_parser_;
115  // Video slice header parser for NAL strucutred streams.
116  std::unique_ptr<VideoSliceHeaderParser> header_parser_;
117 };
118 
119 } // namespace media
120 } // namespace shaka
121 
122 #endif // PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
Abstract class holds stream information.
Definition: stream_info.h:57
Status Process(std::unique_ptr< StreamData > stream_data) override
This structure defines encryption options.
double clear_lead_in_seconds
Clear lead duration in seconds.
FourCC protection_scheme
The protection scheme: 'cenc', 'cens', 'cbc1', 'cbcs'.
Class to hold a media sample.
Definition: media_sample.h:22
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:45