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/public/crypto_params.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 
24  public:
25  EncryptionHandler(const EncryptionParams& encryption_params,
26  KeySource* key_source);
27 
28  ~EncryptionHandler() override;
29 
30  protected:
33  Status InitializeInternal() override;
34  Status Process(std::unique_ptr<StreamData> stream_data) override;
36 
37  private:
38  friend class EncryptionHandlerTest;
39 
40  EncryptionHandler(const EncryptionHandler&) = delete;
41  EncryptionHandler& operator=(const EncryptionHandler&) = delete;
42 
43  // Processes |stream_info| and sets up stream specific variables.
44  Status ProcessStreamInfo(StreamInfo* stream_info);
45  // Processes media sample and encrypts it if needed.
46  Status ProcessMediaSample(MediaSample* sample);
47 
48  Status SetupProtectionPattern(StreamType stream_type);
49  bool CreateEncryptor(const EncryptionKey& encryption_key);
50  bool EncryptVpxFrame(const std::vector<VPxFrameInfo>& vpx_frames,
51  MediaSample* sample,
52  DecryptConfig* decrypt_config);
53  bool EncryptNalFrame(MediaSample* sample, DecryptConfig* decrypt_config);
54  void EncryptBytes(uint8_t* data, size_t size);
55 
56  // Testing injections.
57  void InjectVpxParserForTesting(std::unique_ptr<VPxParser> vpx_parser);
58  void InjectVideoSliceHeaderParserForTesting(
59  std::unique_ptr<VideoSliceHeaderParser> header_parser);
60 
61  const EncryptionParams encryption_params_;
62  const FourCC protection_scheme_ = FOURCC_NULL;
63  KeySource* key_source_ = nullptr;
64  std::string stream_label_;
65  // Current encryption config and encryptor.
66  std::shared_ptr<EncryptionConfig> encryption_config_;
67  std::unique_ptr<AesCryptor> encryptor_;
68  Codec codec_ = kUnknownCodec;
69  // Specifies the size of NAL unit length in bytes. Can be 1, 2 or 4 bytes. 0
70  // if it is not a NAL structured video.
71  uint8_t nalu_length_size_ = 0;
72  // For Sample AES, 32 bytes for Video and 16 bytes for audio.
73  size_t leading_clear_bytes_size_ = 0;
74  // For Sample AES, 48+1 bytes for video NAL and 16+1 bytes for audio.
75  size_t min_protected_data_size_ = 0;
76  // Remaining clear lead in the stream's time scale.
77  int64_t remaining_clear_lead_ = 0;
78  // Crypto period duration in the stream's time scale.
79  uint64_t crypto_period_duration_ = 0;
80  // Previous crypto period index if key rotation is enabled.
81  int64_t prev_crypto_period_index_ = -1;
82  bool check_new_crypto_period_ = false;
83 
84  // Number of encrypted blocks (16-byte-block) in pattern based encryption.
85  uint8_t crypt_byte_block_ = 0;
87  uint8_t skip_byte_block_ = 0;
88 
89  // VPx parser for VPx streams.
90  std::unique_ptr<VPxParser> vpx_parser_;
91  // Video slice header parser for NAL strucutred streams.
92  std::unique_ptr<VideoSliceHeaderParser> header_parser_;
93 };
94 
95 } // namespace media
96 } // namespace shaka
97 
98 #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
Encryption parameters.
Class to hold a media sample.
Definition: media_sample.h:22
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:45