DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
encrypting_fragmenter.h
1 // Copyright 2014 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 MEDIA_FORMATS_MP4_ENCRYPTING_FRAGMENTER_H_
8 #define MEDIA_FORMATS_MP4_ENCRYPTING_FRAGMENTER_H_
9 
10 #include <memory>
11 #include "packager/media/base/fourccs.h"
12 #include "packager/media/codecs/video_slice_header_parser.h"
13 #include "packager/media/codecs/vpx_parser.h"
14 #include "packager/media/event/muxer_listener.h"
15 #include "packager/media/formats/mp4/fragmenter.h"
16 
17 namespace shaka {
18 namespace media {
19 
20 class AesCryptor;
21 class StreamInfo;
22 struct EncryptionKey;
23 
24 namespace mp4 {
25 
28  public:
40  EncryptingFragmenter(std::shared_ptr<StreamInfo> info,
41  TrackFragment* traf,
42  std::unique_ptr<EncryptionKey> encryption_key,
43  int64_t clear_time,
44  FourCC protection_scheme,
45  uint8_t crypt_byte_block,
46  uint8_t skip_byte_block,
47  MuxerListener* listener);
48 
49  ~EncryptingFragmenter() override;
50 
53  Status AddSample(std::shared_ptr<MediaSample> sample) override;
54  Status InitializeFragment(int64_t first_sample_dts) override;
55  void FinalizeFragment() override;
57 
58  protected:
61  virtual Status PrepareFragmentForEncryption(bool enable_encryption);
63  virtual void FinalizeFragmentForEncryption();
64 
69 
70  const EncryptionKey* encryption_key() const { return encryption_key_.get(); }
71  AesCryptor* encryptor() { return encryptor_.get(); }
72  FourCC protection_scheme() const { return protection_scheme_; }
73  uint8_t crypt_byte_block() const { return crypt_byte_block_; }
74  uint8_t skip_byte_block() const { return skip_byte_block_; }
75 
76  void set_encryption_key(std::unique_ptr<EncryptionKey> encryption_key) {
77  encryption_key_ = std::move(encryption_key);
78  }
79 
80  private:
81  void EncryptBytes(uint8_t* data, size_t size);
82  Status EncryptSample(std::shared_ptr<MediaSample> sample);
83 
84  // Should we enable subsample encryption?
85  bool IsSubsampleEncryptionRequired();
86 
87  std::shared_ptr<StreamInfo> info_;
88  std::unique_ptr<EncryptionKey> encryption_key_;
89  std::unique_ptr<AesCryptor> encryptor_;
90  // If this stream contains AVC, subsample encryption specifies that the size
91  // and type of NAL units remain unencrypted. This function returns the size of
92  // the size field in bytes. Can be 1, 2 or 4 bytes.
93  const uint8_t nalu_length_size_;
94  const Codec video_codec_;
95  int64_t clear_time_;
96  const FourCC protection_scheme_;
97  const uint8_t crypt_byte_block_;
98  const uint8_t skip_byte_block_;
99  MuxerListener* listener_;
100 
101  std::unique_ptr<VPxParser> vpx_parser_;
102  std::unique_ptr<VideoSliceHeaderParser> header_parser_;
103 
104  DISALLOW_COPY_AND_ASSIGN(EncryptingFragmenter);
105 };
106 
107 } // namespace mp4
108 } // namespace media
109 } // namespace shaka
110 
111 #endif // MEDIA_FORMATS_MP4_ENCRYPTING_FRAGMENTER_H_
Status InitializeFragment(int64_t first_sample_dts) override
EncryptingFragmenter(std::shared_ptr< StreamInfo > info, TrackFragment *traf, std::unique_ptr< EncryptionKey > encryption_key, int64_t clear_time, FourCC protection_scheme, uint8_t crypt_byte_block, uint8_t skip_byte_block, MuxerListener *listener)
void FinalizeFragment() override
Finalize and optimize the fragment.
Status AddSample(std::shared_ptr< MediaSample > sample) override
virtual void FinalizeFragmentForEncryption()
Finalize current fragment for encryption.
EncryptingFragmenter generates MP4 fragments with sample encrypted.
virtual Status PrepareFragmentForEncryption(bool enable_encryption)