2014-05-08 20:53:08 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#ifndef MEDIA_FORMATS_MP4_ENCRYPTING_FRAGMENTER_H_
|
|
|
|
#define MEDIA_FORMATS_MP4_ENCRYPTING_FRAGMENTER_H_
|
|
|
|
|
2015-11-18 19:51:15 +00:00
|
|
|
#include "packager/base/memory/scoped_ptr.h"
|
2015-11-20 19:28:45 +00:00
|
|
|
#include "packager/media/filters/vpx_parser.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/fragmenter.h"
|
2014-05-08 20:53:08 +00:00
|
|
|
|
2014-09-19 20:41:13 +00:00
|
|
|
namespace edash_packager {
|
2014-05-08 20:53:08 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class AesCtrEncryptor;
|
|
|
|
struct EncryptionKey;
|
|
|
|
|
|
|
|
namespace mp4 {
|
|
|
|
|
|
|
|
/// EncryptingFragmenter generates MP4 fragments with sample encrypted.
|
|
|
|
class EncryptingFragmenter : public Fragmenter {
|
|
|
|
public:
|
|
|
|
/// @param traf points to a TrackFragment box.
|
|
|
|
/// @param encryption_key contains the encryption parameters.
|
|
|
|
/// @param clear_time specifies clear lead duration in units of the current
|
|
|
|
/// track's timescale.
|
2015-11-18 19:51:15 +00:00
|
|
|
/// @param video_codec specifies the codec if input is a video stream; it
|
|
|
|
/// should be set to kUnknownVideoCodec for audio stream. This
|
|
|
|
/// parameter is used for proper subsample encryption.
|
2014-05-08 20:53:08 +00:00
|
|
|
/// @param nalu_length_size specifies the size of NAL unit length, in bytes,
|
|
|
|
/// for subsample encryption.
|
|
|
|
EncryptingFragmenter(TrackFragment* traf,
|
|
|
|
scoped_ptr<EncryptionKey> encryption_key,
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t clear_time,
|
2015-11-18 19:51:15 +00:00
|
|
|
VideoCodec video_codec,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t nalu_length_size);
|
2014-05-08 20:53:08 +00:00
|
|
|
|
2015-07-22 23:40:45 +00:00
|
|
|
~EncryptingFragmenter() override;
|
2014-05-08 20:53:08 +00:00
|
|
|
|
|
|
|
/// @name Fragmenter implementation overrides.
|
|
|
|
/// @{
|
2015-07-22 23:40:45 +00:00
|
|
|
Status AddSample(scoped_refptr<MediaSample> sample) override;
|
|
|
|
Status InitializeFragment(int64_t first_sample_dts) override;
|
|
|
|
void FinalizeFragment() override;
|
2014-05-08 20:53:08 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/// Prepare current fragment for encryption.
|
|
|
|
/// @return OK on success, an error status otherwise.
|
2014-05-08 21:50:38 +00:00
|
|
|
virtual Status PrepareFragmentForEncryption(bool enable_encryption);
|
2014-05-08 20:53:08 +00:00
|
|
|
/// Finalize current fragment for encryption.
|
|
|
|
virtual void FinalizeFragmentForEncryption();
|
|
|
|
|
|
|
|
/// Create the encryptor for the internal encryption key. The existing
|
|
|
|
/// encryptor will be reset if it is not NULL.
|
|
|
|
/// @return OK on success, an error status otherwise.
|
|
|
|
Status CreateEncryptor();
|
|
|
|
|
|
|
|
EncryptionKey* encryption_key() { return encryption_key_.get(); }
|
|
|
|
AesCtrEncryptor* encryptor() { return encryptor_.get(); }
|
|
|
|
|
|
|
|
void set_encryption_key(scoped_ptr<EncryptionKey> encryption_key) {
|
|
|
|
encryption_key_ = encryption_key.Pass();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-09-30 21:52:21 +00:00
|
|
|
void EncryptBytes(uint8_t* data, uint32_t size);
|
2014-05-08 20:53:08 +00:00
|
|
|
Status EncryptSample(scoped_refptr<MediaSample> sample);
|
|
|
|
|
|
|
|
// Should we enable subsample encryption?
|
2015-11-18 19:51:15 +00:00
|
|
|
bool IsSubsampleEncryptionRequired() {
|
2015-11-20 19:28:45 +00:00
|
|
|
return vpx_parser_ || nalu_length_size_ != 0;
|
2015-11-18 19:51:15 +00:00
|
|
|
}
|
2014-05-08 20:53:08 +00:00
|
|
|
|
|
|
|
scoped_ptr<EncryptionKey> encryption_key_;
|
|
|
|
scoped_ptr<AesCtrEncryptor> encryptor_;
|
2015-11-18 19:51:15 +00:00
|
|
|
// For VP8/VP9, uncompressed_header should not be encrypted; for AVC/HEVC,
|
|
|
|
// the size and type NAL units should not be encrypted.
|
|
|
|
VideoCodec video_codec_;
|
2014-05-08 20:53:08 +00:00
|
|
|
// If this stream contains AVC, subsample encryption specifies that the size
|
|
|
|
// and type of NAL units remain unencrypted. This field specifies the size of
|
|
|
|
// the size field. Can be 1, 2 or 4 bytes.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t nalu_length_size_;
|
|
|
|
int64_t clear_time_;
|
2014-05-08 20:53:08 +00:00
|
|
|
|
2015-11-20 19:28:45 +00:00
|
|
|
scoped_ptr<VPxParser> vpx_parser_;
|
2015-11-18 19:51:15 +00:00
|
|
|
|
2014-05-08 20:53:08 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(EncryptingFragmenter);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2014-09-19 20:41:13 +00:00
|
|
|
} // namespace edash_packager
|
2014-05-08 20:53:08 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_FORMATS_MP4_ENCRYPTING_FRAGMENTER_H_
|