DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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/scoped_ptr.h"
11 #include "packager/media/filters/vpx_parser.h"
12 #include "packager/media/formats/mp4/fragmenter.h"
13 
14 namespace edash_packager {
15 namespace media {
16 
17 class AesCtrEncryptor;
18 struct EncryptionKey;
19 
20 namespace mp4 {
21 
24  public:
35  scoped_ptr<EncryptionKey> encryption_key,
36  int64_t clear_time,
37  VideoCodec video_codec,
38  uint8_t nalu_length_size);
39 
40  ~EncryptingFragmenter() override;
41 
44  Status AddSample(scoped_refptr<MediaSample> sample) override;
45  Status InitializeFragment(int64_t first_sample_dts) override;
46  void FinalizeFragment() override;
48 
49  protected:
52  virtual Status PrepareFragmentForEncryption(bool enable_encryption);
54  virtual void FinalizeFragmentForEncryption();
55 
60 
61  EncryptionKey* encryption_key() { return encryption_key_.get(); }
62  AesCtrEncryptor* encryptor() { return encryptor_.get(); }
63 
64  void set_encryption_key(scoped_ptr<EncryptionKey> encryption_key) {
65  encryption_key_ = encryption_key.Pass();
66  }
67 
68  private:
69  void EncryptBytes(uint8_t* data, uint32_t size);
70  Status EncryptSample(scoped_refptr<MediaSample> sample);
71 
72  // Should we enable subsample encryption?
73  bool IsSubsampleEncryptionRequired() {
74  return vpx_parser_ || nalu_length_size_ != 0;
75  }
76 
77  scoped_ptr<EncryptionKey> encryption_key_;
78  scoped_ptr<AesCtrEncryptor> encryptor_;
79  // For VP8/VP9, uncompressed_header should not be encrypted; for AVC/HEVC,
80  // the size and type NAL units should not be encrypted.
81  VideoCodec video_codec_;
82  // If this stream contains AVC, subsample encryption specifies that the size
83  // and type of NAL units remain unencrypted. This field specifies the size of
84  // the size field. Can be 1, 2 or 4 bytes.
85  const uint8_t nalu_length_size_;
86  int64_t clear_time_;
87 
88  scoped_ptr<VPxParser> vpx_parser_;
89 
90  DISALLOW_COPY_AND_ASSIGN(EncryptingFragmenter);
91 };
92 
93 } // namespace mp4
94 } // namespace media
95 } // namespace edash_packager
96 
97 #endif // MEDIA_FORMATS_MP4_ENCRYPTING_FRAGMENTER_H_
EncryptingFragmenter(TrackFragment *traf, scoped_ptr< EncryptionKey > encryption_key, int64_t clear_time, VideoCodec video_codec, uint8_t nalu_length_size)
EncryptingFragmenter generates MP4 fragments with sample encrypted.
Status InitializeFragment(int64_t first_sample_dts) override
Status AddSample(scoped_refptr< MediaSample > sample) override
void FinalizeFragment() override
Finalize and optimize the fragment.
virtual Status PrepareFragmentForEncryption(bool enable_encryption)
virtual void FinalizeFragmentForEncryption()
Finalize current fragment for encryption.