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 };
45 
47  public:
48  EncryptionHandler(const EncryptionOptions& encryption_options,
49  KeySource* key_source);
50 
51  ~EncryptionHandler() override;
52 
53  protected:
56  Status InitializeInternal() override;
57  Status Process(std::unique_ptr<StreamData> stream_data) override;
59 
60  private:
61  friend class EncryptionHandlerTest;
62 
63  EncryptionHandler(const EncryptionHandler&) = delete;
64  EncryptionHandler& operator=(const EncryptionHandler&) = delete;
65 
66  // Processes |stream_info| and sets up stream specific variables.
67  Status ProcessStreamInfo(StreamInfo* stream_info);
68  // Processes media sample and encrypts it if needed.
69  Status ProcessMediaSample(MediaSample* sample);
70 
71  Status SetupProtectionPattern(StreamType stream_type);
72  bool CreateEncryptor(const EncryptionKey& encryption_key);
73  bool EncryptVpxFrame(const std::vector<VPxFrameInfo>& vpx_frames,
74  MediaSample* sample,
75  DecryptConfig* decrypt_config);
76  bool EncryptNalFrame(MediaSample* sample, DecryptConfig* decrypt_config);
77  void EncryptBytes(uint8_t* data, size_t size);
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 EncryptionOptions encryption_options_;
85  KeySource* key_source_ = nullptr;
86  KeySource::TrackType track_type_ = KeySource::TRACK_TYPE_UNKNOWN;
87  // Current encryption config and encryptor.
88  std::shared_ptr<EncryptionConfig> encryption_config_;
89  std::unique_ptr<AesCryptor> encryptor_;
90  Codec codec_ = kUnknownCodec;
91  // Specifies the size of NAL unit length in bytes. Can be 1, 2 or 4 bytes. 0
92  // if it is not a NAL structured video.
93  uint8_t nalu_length_size_ = 0;
94  // For Sample AES, 32 bytes for Video and 16 bytes for audio.
95  size_t leading_clear_bytes_size_ = 0;
96  // For Sample AES, 48+1 bytes for video NAL and 16+1 bytes for audio.
97  size_t min_protected_data_size_ = 0;
98  // Remaining clear lead in the stream's time scale.
99  int64_t remaining_clear_lead_ = 0;
100  // Crypto period duration in the stream's time scale.
101  uint64_t crypto_period_duration_ = 0;
102  // Previous crypto period index if key rotation is enabled.
103  int64_t prev_crypto_period_index_ = -1;
104  bool check_new_crypto_period_ = false;
105 
106  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
107  uint8_t crypt_byte_block_ = 0;
109  uint8_t skip_byte_block_ = 0;
110 
111  // VPx parser for VPx streams.
112  std::unique_ptr<VPxParser> vpx_parser_;
113  // Video slice header parser for NAL strucutred streams.
114  std::unique_ptr<VideoSliceHeaderParser> header_parser_;
115 };
116 
117 } // namespace media
118 } // namespace shaka
119 
120 #endif // PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
Abstract class holds stream information.
Definition: stream_info.h:60
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:30