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/packager.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;
36  std::function<std::string(
37  const EncryptionParams::EncryptedStreamAttributes& stream_attributes)>
39 };
40 
42  public:
43  EncryptionHandler(const EncryptionOptions& encryption_options,
44  KeySource* key_source);
45 
46  ~EncryptionHandler() override;
47 
48  protected:
51  Status InitializeInternal() override;
52  Status Process(std::unique_ptr<StreamData> stream_data) override;
54 
55  private:
56  friend class EncryptionHandlerTest;
57 
58  EncryptionHandler(const EncryptionHandler&) = delete;
59  EncryptionHandler& operator=(const EncryptionHandler&) = delete;
60 
61  // Processes |stream_info| and sets up stream specific variables.
62  Status ProcessStreamInfo(StreamInfo* stream_info);
63  // Processes media sample and encrypts it if needed.
64  Status ProcessMediaSample(MediaSample* sample);
65 
66  Status SetupProtectionPattern(StreamType stream_type);
67  bool CreateEncryptor(const EncryptionKey& encryption_key);
68  bool EncryptVpxFrame(const std::vector<VPxFrameInfo>& vpx_frames,
69  MediaSample* sample,
70  DecryptConfig* decrypt_config);
71  bool EncryptNalFrame(MediaSample* sample, DecryptConfig* decrypt_config);
72  void EncryptBytes(uint8_t* data, size_t size);
73 
74  // Testing injections.
75  void InjectVpxParserForTesting(std::unique_ptr<VPxParser> vpx_parser);
76  void InjectVideoSliceHeaderParserForTesting(
77  std::unique_ptr<VideoSliceHeaderParser> header_parser);
78 
79  const EncryptionOptions encryption_options_;
80  KeySource* key_source_ = nullptr;
81  KeySource::TrackType track_type_ = KeySource::TRACK_TYPE_UNKNOWN;
82  // Current encryption config and encryptor.
83  std::shared_ptr<EncryptionConfig> encryption_config_;
84  std::unique_ptr<AesCryptor> encryptor_;
85  Codec codec_ = kUnknownCodec;
86  // Specifies the size of NAL unit length in bytes. Can be 1, 2 or 4 bytes. 0
87  // if it is not a NAL structured video.
88  uint8_t nalu_length_size_ = 0;
89  // For Sample AES, 32 bytes for Video and 16 bytes for audio.
90  size_t leading_clear_bytes_size_ = 0;
91  // For Sample AES, 48+1 bytes for video NAL and 16+1 bytes for audio.
92  size_t min_protected_data_size_ = 0;
93  // Remaining clear lead in the stream's time scale.
94  int64_t remaining_clear_lead_ = 0;
95  // Crypto period duration in the stream's time scale.
96  uint64_t crypto_period_duration_ = 0;
97  // Previous crypto period index if key rotation is enabled.
98  int64_t prev_crypto_period_index_ = -1;
99  bool check_new_crypto_period_ = false;
100 
101  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
102  uint8_t crypt_byte_block_ = 0;
104  uint8_t skip_byte_block_ = 0;
105 
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_
std::function< std::string(const EncryptionParams::EncryptedStreamAttributes &stream_attributes)> stream_label_func
Abstract class holds stream information.
Definition: stream_info.h:57
bool vp9_subsample_encryption
Enable/disable subsample encryption for VP9.
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