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_
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2016-02-03 22:13:57 +00:00
|
|
|
#include "packager/base/memory/ref_counted.h"
|
2016-04-08 18:41:17 +00:00
|
|
|
#include "packager/media/base/fourccs.h"
|
2016-05-25 18:03:17 +00:00
|
|
|
#include "packager/media/codecs/video_slice_header_parser.h"
|
|
|
|
#include "packager/media/codecs/vpx_parser.h"
|
2016-07-05 23:32:19 +00:00
|
|
|
#include "packager/media/event/muxer_listener.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/fragmenter.h"
|
2014-05-08 20:53:08 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-05-08 20:53:08 +00:00
|
|
|
namespace media {
|
|
|
|
|
2016-04-08 18:41:17 +00:00
|
|
|
class AesCryptor;
|
2016-02-03 22:13:57 +00:00
|
|
|
class StreamInfo;
|
2014-05-08 20:53:08 +00:00
|
|
|
struct EncryptionKey;
|
|
|
|
|
|
|
|
namespace mp4 {
|
|
|
|
|
|
|
|
/// EncryptingFragmenter generates MP4 fragments with sample encrypted.
|
|
|
|
class EncryptingFragmenter : public Fragmenter {
|
|
|
|
public:
|
2016-05-06 23:56:50 +00:00
|
|
|
/// @param info contains stream information.
|
2014-05-08 20:53:08 +00:00
|
|
|
/// @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.
|
2016-04-08 18:41:17 +00:00
|
|
|
/// @param protection_scheme specifies the protection scheme: 'cenc', 'cens',
|
|
|
|
/// 'cbc1', 'cbcs'.
|
2016-04-06 00:28:09 +00:00
|
|
|
/// @param crypt_byte_block indicates number of encrypted blocks (16-byte) in
|
|
|
|
/// pattern based encryption.
|
|
|
|
/// @param skip_byte_block indicates number of unencrypted blocks (16-byte)
|
|
|
|
/// in pattern based encryption.
|
2016-02-03 22:13:57 +00:00
|
|
|
EncryptingFragmenter(scoped_refptr<StreamInfo> info,
|
|
|
|
TrackFragment* traf,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<EncryptionKey> encryption_key,
|
2016-03-17 17:03:19 +00:00
|
|
|
int64_t clear_time,
|
2016-04-06 00:28:09 +00:00
|
|
|
FourCC protection_scheme,
|
|
|
|
uint8_t crypt_byte_block,
|
2016-07-05 23:32:19 +00:00
|
|
|
uint8_t skip_byte_block,
|
|
|
|
MuxerListener* listener);
|
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();
|
|
|
|
|
2016-04-08 18:41:17 +00:00
|
|
|
const EncryptionKey* encryption_key() const { return encryption_key_.get(); }
|
|
|
|
AesCryptor* encryptor() { return encryptor_.get(); }
|
|
|
|
FourCC protection_scheme() const { return protection_scheme_; }
|
2016-04-06 00:28:09 +00:00
|
|
|
uint8_t crypt_byte_block() const { return crypt_byte_block_; }
|
|
|
|
uint8_t skip_byte_block() const { return skip_byte_block_; }
|
2014-05-08 20:53:08 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
void set_encryption_key(std::unique_ptr<EncryptionKey> encryption_key) {
|
|
|
|
encryption_key_ = std::move(encryption_key);
|
2014-05-08 20:53:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-09 02:11:13 +00:00
|
|
|
void EncryptBytes(uint8_t* data, size_t size);
|
2014-05-08 20:53:08 +00:00
|
|
|
Status EncryptSample(scoped_refptr<MediaSample> sample);
|
|
|
|
|
|
|
|
// Should we enable subsample encryption?
|
2016-02-03 22:13:57 +00:00
|
|
|
bool IsSubsampleEncryptionRequired();
|
2014-05-08 20:53:08 +00:00
|
|
|
|
2016-02-03 22:13:57 +00:00
|
|
|
scoped_refptr<StreamInfo> info_;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<EncryptionKey> encryption_key_;
|
|
|
|
std::unique_ptr<AesCryptor> encryptor_;
|
2016-02-22 19:11:29 +00:00
|
|
|
// If this stream contains AVC, subsample encryption specifies that the size
|
|
|
|
// and type of NAL units remain unencrypted. This function returns the size of
|
|
|
|
// the size field in bytes. Can be 1, 2 or 4 bytes.
|
|
|
|
const uint8_t nalu_length_size_;
|
2016-07-27 00:51:08 +00:00
|
|
|
const Codec video_codec_;
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t clear_time_;
|
2016-04-06 00:28:09 +00:00
|
|
|
const FourCC protection_scheme_;
|
|
|
|
const uint8_t crypt_byte_block_;
|
|
|
|
const uint8_t skip_byte_block_;
|
2016-07-05 23:32:19 +00:00
|
|
|
MuxerListener* listener_;
|
2014-05-08 20:53:08 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<VPxParser> vpx_parser_;
|
|
|
|
std::unique_ptr<VideoSliceHeaderParser> header_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
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2014-05-08 20:53:08 +00:00
|
|
|
|
|
|
|
#endif // MEDIA_FORMATS_MP4_ENCRYPTING_FRAGMENTER_H_
|