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  bool CreateEncryptor(EncryptionKey* encryption_key);
72  bool EncryptVpxFrame(const std::vector<VPxFrameInfo>& vpx_frames,
73  MediaSample* sample,
74  DecryptConfig* decrypt_config);
75  bool EncryptNalFrame(MediaSample* sample, DecryptConfig* decrypt_config);
76  void EncryptBytes(uint8_t* data, size_t size);
77 
78  // Testing injections.
79  void InjectVpxParserForTesting(std::unique_ptr<VPxParser> vpx_parser);
80  void InjectVideoSliceHeaderParserForTesting(
81  std::unique_ptr<VideoSliceHeaderParser> header_parser);
82 
83  const EncryptionOptions encryption_options_;
84  KeySource* key_source_ = nullptr;
85  KeySource::TrackType track_type_ = KeySource::TRACK_TYPE_UNKNOWN;
86  std::unique_ptr<AesCryptor> encryptor_;
87  // Specifies the size of NAL unit length in bytes. Can be 1, 2 or 4 bytes. 0
88  // if it is not a NAL structured video.
89  uint8_t nalu_length_size_ = 0;
90  Codec video_codec_ = kUnknownCodec;
91  // Remaining clear lead in the stream's time scale.
92  int64_t remaining_clear_lead_ = 0;
93  // Crypto period duration in the stream's time scale.
94  uint64_t crypto_period_duration_ = 0;
95  // Previous crypto period index if key rotation is enabled.
96  int64_t prev_crypto_period_index_ = -1;
97  bool new_segment_ = true;
98 
99  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
100  uint8_t crypt_byte_block_ = 0;
102  uint8_t skip_byte_block_ = 0;
103 
104  // Current key id.
105  std::vector<uint8_t> key_id_;
106  // VPx parser for VPx streams.
107  std::unique_ptr<VPxParser> vpx_parser_;
108  // Video slice header parser for NAL strucutred streams.
109  std::unique_ptr<VideoSliceHeaderParser> header_parser_;
110 };
111 
112 } // namespace media
113 } // namespace shaka
114 
115 #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:30