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