2014-02-14 23:21:05 +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
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-10 21:42:38 +00:00
|
|
|
#ifndef MEDIA_FORMATS_MP4_FRAGMENTER_H_
|
|
|
|
#define MEDIA_FORMATS_MP4_FRAGMENTER_H_
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/memory/ref_counted.h"
|
|
|
|
#include "base/memory/scoped_ptr.h"
|
|
|
|
#include "media/base/status.h"
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
class AesCtrEncryptor;
|
2013-11-12 20:37:58 +00:00
|
|
|
class BufferWriter;
|
|
|
|
class MediaSample;
|
|
|
|
|
2014-04-18 18:49:49 +00:00
|
|
|
struct EncryptionKey;
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
namespace mp4 {
|
|
|
|
|
2014-04-18 18:49:49 +00:00
|
|
|
struct MovieFragment;
|
2014-03-21 17:26:49 +00:00
|
|
|
struct SegmentReference;
|
|
|
|
struct TrackFragment;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
/// Fragmenter is responsible for the generation of MP4 fragments, i.e. traf
|
2014-01-23 22:34:39 +00:00
|
|
|
/// box and corresponding mdat box. The samples are also encrypted if encryption
|
|
|
|
/// is requested.
|
2014-04-08 20:21:07 +00:00
|
|
|
class Fragmenter {
|
2013-11-12 20:37:58 +00:00
|
|
|
public:
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @param traf points to a TrackFragment box.
|
2014-04-18 18:49:49 +00:00
|
|
|
/// @param normalize_presentation_timestamp defines whether PTS should be
|
|
|
|
/// normalized to start from zero.
|
|
|
|
Fragmenter(TrackFragment* traf,
|
|
|
|
bool normalize_presentation_timestamp);
|
|
|
|
|
|
|
|
/// @param traf points to a TrackFragment box.
|
|
|
|
/// @param normalize_presentation_timestamp defines whether PTS should be
|
|
|
|
/// normalized to start from zero.
|
|
|
|
/// @param encryption_key contains the encryption parameters.
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @param clear_time specifies clear lead duration in units of the current
|
|
|
|
/// track's timescale.
|
|
|
|
/// @param nalu_length_size NAL unit length size, in bytes, for subsample
|
|
|
|
/// encryption.
|
2014-04-08 20:21:07 +00:00
|
|
|
Fragmenter(TrackFragment* traf,
|
2014-04-18 18:49:49 +00:00
|
|
|
bool normalize_presentation_timestamp,
|
|
|
|
scoped_ptr<EncryptionKey> encryption_key,
|
2014-04-08 20:21:07 +00:00
|
|
|
int64 clear_time,
|
2014-04-18 18:49:49 +00:00
|
|
|
uint8 nalu_length_size);
|
|
|
|
|
|
|
|
virtual ~Fragmenter();
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Add a sample to the fragmenter.
|
2014-03-21 17:26:49 +00:00
|
|
|
Status AddSample(scoped_refptr<MediaSample> sample);
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Initialize the fragment with default data.
|
2014-04-18 18:49:49 +00:00
|
|
|
/// @return OK on success, an error status otherwise.
|
|
|
|
Status InitializeFragment();
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Finalize and optimize the fragment.
|
2013-11-12 20:37:58 +00:00
|
|
|
void FinalizeFragment();
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Fill @a reference with current fragment information.
|
2013-11-12 20:37:58 +00:00
|
|
|
void GenerateSegmentReference(SegmentReference* reference);
|
|
|
|
|
|
|
|
uint64 fragment_duration() const { return fragment_duration_; }
|
|
|
|
uint64 first_sap_time() const { return first_sap_time_; }
|
|
|
|
uint64 earliest_presentation_time() const {
|
|
|
|
return earliest_presentation_time_;
|
|
|
|
}
|
|
|
|
bool fragment_finalized() const { return fragment_finalized_; }
|
|
|
|
BufferWriter* data() { return data_.get(); }
|
|
|
|
BufferWriter* aux_data() { return aux_data_.get(); }
|
|
|
|
|
2014-04-18 18:49:49 +00:00
|
|
|
protected:
|
|
|
|
/// Prepare current fragment for encryption.
|
|
|
|
/// @return OK on success, an error status otherwise.
|
|
|
|
virtual Status PrepareFragmentForEncryption();
|
|
|
|
/// 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();
|
|
|
|
|
|
|
|
TrackFragment* traf() { return traf_; }
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
private:
|
|
|
|
void EncryptBytes(uint8* data, uint32 size);
|
|
|
|
Status EncryptSample(scoped_refptr<MediaSample> sample);
|
|
|
|
|
|
|
|
// Should we enable subsample encryption?
|
|
|
|
bool IsSubsampleEncryptionRequired() { return nalu_length_size_ != 0; }
|
|
|
|
|
|
|
|
// Check if the current fragment starts with SAP.
|
|
|
|
bool StartsWithSAP();
|
|
|
|
|
2014-04-18 18:49:49 +00:00
|
|
|
TrackFragment* traf_;
|
|
|
|
|
|
|
|
scoped_ptr<EncryptionKey> encryption_key_;
|
2014-01-14 01:38:34 +00:00
|
|
|
scoped_ptr<AesCtrEncryptor> encryptor_;
|
2013-11-12 20:37:58 +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-04-18 18:49:49 +00:00
|
|
|
const uint8 nalu_length_size_;
|
|
|
|
const int64 clear_time_;
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
bool fragment_finalized_;
|
|
|
|
uint64 fragment_duration_;
|
2014-01-22 23:51:26 +00:00
|
|
|
bool normalize_presentation_timestamp_;
|
|
|
|
int64 presentation_start_time_;
|
2014-03-21 17:26:49 +00:00
|
|
|
int64 earliest_presentation_time_;
|
|
|
|
int64 first_sap_time_;
|
2013-11-12 20:37:58 +00:00
|
|
|
scoped_ptr<BufferWriter> data_;
|
|
|
|
scoped_ptr<BufferWriter> aux_data_;
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(Fragmenter);
|
2013-11-12 20:37:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
|
|
|
|
2014-04-10 21:42:38 +00:00
|
|
|
#endif // MEDIA_FORMATS_MP4_FRAGMENTER_H_
|