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 #include "packager/media/base/stream_info.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 
28  FourCC protection_scheme = FOURCC_cenc;
32  uint32_t max_sd_pixels = 0;
36  uint32_t max_hd_pixels = 0;
41  uint32_t max_uhd1_pixels = 0;
45 };
46 
48  public:
49  EncryptionHandler(const EncryptionOptions& encryption_options,
50  KeySource* key_source);
51 
52  ~EncryptionHandler() override;
53 
54  protected:
57  Status InitializeInternal() override;
58  Status Process(std::unique_ptr<StreamData> stream_data) override;
60 
61  private:
62  friend class EncryptionHandlerTest;
63 
64  EncryptionHandler(const EncryptionHandler&) = delete;
65  EncryptionHandler& operator=(const EncryptionHandler&) = delete;
66 
67  // Processes |stream_info| and sets up stream specific variables.
68  Status ProcessStreamInfo(StreamInfo* stream_info);
69  // Processes media sample end encrypts it if needed.
70  Status ProcessMediaSample(MediaSample* sample);
71 
72  bool CreateEncryptor(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  EncryptionOptions encryption_options_;
85  KeySource* key_source_ = nullptr;
86  KeySource::TrackType track_type_ = KeySource::TRACK_TYPE_UNKNOWN;
87  std::unique_ptr<AesCryptor> encryptor_;
88  // Specifies the size of NAL unit length in bytes. Can be 1, 2 or 4 bytes. 0
89  // if it is not a NAL structured video.
90  uint8_t nalu_length_size_ = 0;
91  Codec video_codec_ = kUnknownCodec;
92  // Remaining clear lead in the stream's time scale.
93  int64_t remaining_clear_lead_ = 0;
94  // Crypto period duration in the stream's time scale.
95  uint64_t crypto_period_duration_ = 0;
96  // Previous crypto period index if key rotation is enabled.
97  int64_t prev_crypto_period_index_ = -1;
98  bool new_segment_ = true;
99 
100  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
101  uint8_t crypt_byte_block_ = 0;
103  uint8_t skip_byte_block_ = 0;
104 
105  // Current key id.
106  std::vector<uint8_t> key_id_;
107  // VPx parser for VPx streams.
108  std::unique_ptr<VPxParser> vpx_parser_;
109  // Video slice header parser for NAL strucutred streams.
110  std::unique_ptr<VideoSliceHeaderParser> header_parser_;
111 };
112 
113 } // namespace media
114 } // namespace shaka
115 
116 #endif // PACKAGER_MEDIA_CRYPTO_ENCRYPTION_HANDLER_H_
Abstract class holds stream information.
Definition: stream_info.h:51
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