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
|
|
|
|
|
|
|
#include "media/mp4/mp4_fragmenter.h"
|
|
|
|
|
|
|
|
#include "media/base/aes_encryptor.h"
|
|
|
|
#include "media/base/buffer_reader.h"
|
|
|
|
#include "media/base/buffer_writer.h"
|
|
|
|
#include "media/base/media_sample.h"
|
|
|
|
#include "media/mp4/box_definitions.h"
|
|
|
|
#include "media/mp4/cenc.h"
|
|
|
|
|
|
|
|
namespace {
|
2014-03-21 17:26:49 +00:00
|
|
|
const int64 kInvalidTime = kint64max;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
// Optimize sample entries table. If all values in |entries| are identical,
|
|
|
|
// then |entries| is cleared and the value is assigned to |default_value|;
|
|
|
|
// otherwise it is a NOP. Return true if the table is optimized.
|
|
|
|
template <typename T>
|
|
|
|
bool OptimizeSampleEntries(std::vector<T>* entries, T* default_value) {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(entries);
|
|
|
|
DCHECK(default_value);
|
2013-11-12 20:37:58 +00:00
|
|
|
DCHECK(!entries->empty());
|
|
|
|
|
|
|
|
typename std::vector<T>::const_iterator it = entries->begin();
|
|
|
|
T value = *it;
|
|
|
|
for (; it < entries->end(); ++it)
|
|
|
|
if (value != *it)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Clear |entries| if it contains only one value.
|
|
|
|
entries->clear();
|
|
|
|
*default_value = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
|
|
|
MP4Fragmenter::MP4Fragmenter(TrackFragment* traf,
|
2014-01-14 01:38:34 +00:00
|
|
|
scoped_ptr<AesCtrEncryptor> encryptor,
|
2013-11-12 20:37:58 +00:00
|
|
|
int64 clear_time,
|
2014-01-22 23:51:26 +00:00
|
|
|
uint8 nalu_length_size,
|
|
|
|
bool normalize_presentation_timestamp)
|
2014-01-14 01:38:34 +00:00
|
|
|
: encryptor_(encryptor.Pass()),
|
2013-11-12 20:37:58 +00:00
|
|
|
nalu_length_size_(nalu_length_size),
|
|
|
|
traf_(traf),
|
|
|
|
fragment_finalized_(false),
|
|
|
|
fragment_duration_(0),
|
2014-01-22 23:51:26 +00:00
|
|
|
normalize_presentation_timestamp_(normalize_presentation_timestamp),
|
|
|
|
presentation_start_time_(kInvalidTime),
|
|
|
|
earliest_presentation_time_(kInvalidTime),
|
|
|
|
first_sap_time_(kInvalidTime),
|
2013-11-12 20:37:58 +00:00
|
|
|
clear_time_(clear_time) {}
|
|
|
|
|
|
|
|
MP4Fragmenter::~MP4Fragmenter() {}
|
|
|
|
|
|
|
|
Status MP4Fragmenter::AddSample(scoped_refptr<MediaSample> sample) {
|
2014-01-23 00:13:41 +00:00
|
|
|
CHECK_GT(sample->duration(), 0);
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
if (ShouldEncryptFragment()) {
|
|
|
|
Status status = EncryptSample(sample);
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in sample parameters. It will be optimized later.
|
|
|
|
traf_->runs[0].sample_sizes.push_back(sample->data_size());
|
|
|
|
traf_->runs[0].sample_durations.push_back(sample->duration());
|
|
|
|
traf_->runs[0].sample_flags.push_back(
|
|
|
|
sample->is_key_frame() ? 0 : TrackFragmentHeader::kNonKeySampleMask);
|
|
|
|
traf_->runs[0]
|
|
|
|
.sample_composition_time_offsets.push_back(sample->pts() - sample->dts());
|
|
|
|
if (sample->pts() != sample->dts()) {
|
|
|
|
traf_->runs[0].flags |=
|
|
|
|
TrackFragmentRun::kSampleCompTimeOffsetsPresentMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_->AppendArray(sample->data(), sample->data_size());
|
|
|
|
fragment_duration_ += sample->duration();
|
|
|
|
|
2014-01-22 23:51:26 +00:00
|
|
|
int64 pts = sample->pts();
|
|
|
|
if (normalize_presentation_timestamp_) {
|
|
|
|
// Normalize PTS to start from 0. Some players do not like non-zero
|
|
|
|
// presentation starting time.
|
2014-03-26 22:09:43 +00:00
|
|
|
// NOTE: The timeline of the remuxed video may not be exactly the same as
|
|
|
|
// the original video. An EditList box may be useful to solve this.
|
2014-01-22 23:51:26 +00:00
|
|
|
if (presentation_start_time_ == kInvalidTime) {
|
|
|
|
presentation_start_time_ = pts;
|
|
|
|
pts = 0;
|
|
|
|
} else {
|
|
|
|
// Can we safely assume the first sample in the media has the earliest
|
|
|
|
// presentation timestamp?
|
|
|
|
DCHECK_GT(pts, presentation_start_time_);
|
|
|
|
pts -= presentation_start_time_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set |earliest_presentation_time_| to |pts| if |pts| is smaller or if it is
|
|
|
|
// not yet initialized (kInvalidTime > pts is always true).
|
|
|
|
if (earliest_presentation_time_ > pts)
|
|
|
|
earliest_presentation_time_ = pts;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
if (sample->is_key_frame()) {
|
2014-01-22 23:51:26 +00:00
|
|
|
if (first_sap_time_ == kInvalidTime)
|
|
|
|
first_sap_time_ = pts;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Fragmenter::InitializeFragment() {
|
|
|
|
fragment_finalized_ = false;
|
|
|
|
traf_->decode_time.decode_time += fragment_duration_;
|
|
|
|
traf_->auxiliary_size.sample_info_sizes.clear();
|
|
|
|
traf_->auxiliary_offset.offsets.clear();
|
|
|
|
traf_->runs.clear();
|
|
|
|
traf_->runs.resize(1);
|
|
|
|
traf_->runs[0].flags = TrackFragmentRun::kDataOffsetPresentMask;
|
|
|
|
traf_->header.flags = TrackFragmentHeader::kDefaultBaseIsMoofMask;
|
|
|
|
fragment_duration_ = 0;
|
|
|
|
earliest_presentation_time_ = kInvalidTime;
|
|
|
|
first_sap_time_ = kInvalidTime;
|
|
|
|
data_.reset(new BufferWriter());
|
|
|
|
aux_data_.reset(new BufferWriter());
|
|
|
|
|
|
|
|
if (ShouldEncryptFragment()) {
|
|
|
|
if (!IsSubsampleEncryptionRequired()) {
|
2014-01-14 01:38:34 +00:00
|
|
|
DCHECK(encryptor_);
|
|
|
|
traf_->auxiliary_size.default_sample_info_size = encryptor_->iv().size();
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Fragmenter::FinalizeFragment() {
|
|
|
|
if (ShouldEncryptFragment()) {
|
2014-01-14 01:38:34 +00:00
|
|
|
DCHECK(encryptor_);
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
// The offset will be adjusted in Segmenter when we know moof size.
|
|
|
|
traf_->auxiliary_offset.offsets.push_back(0);
|
|
|
|
|
|
|
|
// Optimize saiz box.
|
|
|
|
SampleAuxiliaryInformationSize& saiz = traf_->auxiliary_size;
|
|
|
|
saiz.sample_count = traf_->runs[0].sample_sizes.size();
|
|
|
|
if (!saiz.sample_info_sizes.empty()) {
|
|
|
|
if (!OptimizeSampleEntries(&saiz.sample_info_sizes,
|
|
|
|
&saiz.default_sample_info_size)) {
|
|
|
|
saiz.default_sample_info_size = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-01-14 01:38:34 +00:00
|
|
|
} else if (encryptor_ && clear_time_ > 0) {
|
2013-11-12 20:37:58 +00:00
|
|
|
// This fragment should be in clear.
|
|
|
|
// We generate at most two sample description entries, encrypted entry and
|
|
|
|
// clear entry. The 1-based clear entry index is always 2.
|
|
|
|
const uint32 kClearSampleDescriptionIndex = 2;
|
|
|
|
|
|
|
|
traf_->header.flags |=
|
|
|
|
TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
|
|
|
|
traf_->header.sample_description_index = kClearSampleDescriptionIndex;
|
|
|
|
clear_time_ -= fragment_duration_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimize trun box.
|
|
|
|
traf_->runs[0].sample_count = traf_->runs[0].sample_sizes.size();
|
|
|
|
if (OptimizeSampleEntries(&traf_->runs[0].sample_durations,
|
|
|
|
&traf_->header.default_sample_duration)) {
|
|
|
|
traf_->header.flags |=
|
|
|
|
TrackFragmentHeader::kDefaultSampleDurationPresentMask;
|
|
|
|
} else {
|
|
|
|
traf_->runs[0].flags |= TrackFragmentRun::kSampleDurationPresentMask;
|
|
|
|
}
|
|
|
|
if (OptimizeSampleEntries(&traf_->runs[0].sample_sizes,
|
|
|
|
&traf_->header.default_sample_size)) {
|
|
|
|
traf_->header.flags |= TrackFragmentHeader::kDefaultSampleSizePresentMask;
|
|
|
|
} else {
|
|
|
|
traf_->runs[0].flags |= TrackFragmentRun::kSampleSizePresentMask;
|
|
|
|
}
|
|
|
|
if (OptimizeSampleEntries(&traf_->runs[0].sample_flags,
|
|
|
|
&traf_->header.default_sample_flags)) {
|
|
|
|
traf_->header.flags |= TrackFragmentHeader::kDefaultSampleFlagsPresentMask;
|
|
|
|
} else {
|
|
|
|
traf_->runs[0].flags |= TrackFragmentRun::kSampleFlagsPresentMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment_finalized_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Fragmenter::GenerateSegmentReference(SegmentReference* reference) {
|
2014-03-26 22:09:43 +00:00
|
|
|
// NOTE: Daisy chain is not supported currently.
|
2013-11-12 20:37:58 +00:00
|
|
|
reference->reference_type = false;
|
|
|
|
reference->subsegment_duration = fragment_duration_;
|
|
|
|
reference->starts_with_sap = StartsWithSAP();
|
|
|
|
if (kInvalidTime == first_sap_time_) {
|
|
|
|
reference->sap_type = SegmentReference::TypeUnknown;
|
|
|
|
reference->sap_delta_time = 0;
|
|
|
|
} else {
|
|
|
|
reference->sap_type = SegmentReference::Type1;
|
|
|
|
reference->sap_delta_time = first_sap_time_ - earliest_presentation_time_;
|
|
|
|
}
|
|
|
|
reference->earliest_presentation_time = earliest_presentation_time_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Fragmenter::EncryptBytes(uint8* data, uint32 size) {
|
2014-01-14 01:38:34 +00:00
|
|
|
DCHECK(encryptor_);
|
|
|
|
CHECK(encryptor_->Encrypt(data, size, data));
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status MP4Fragmenter::EncryptSample(scoped_refptr<MediaSample> sample) {
|
2014-01-14 01:38:34 +00:00
|
|
|
DCHECK(encryptor_);
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
FrameCENCInfo cenc_info(encryptor_->iv());
|
2013-11-12 20:37:58 +00:00
|
|
|
uint8* data = sample->writable_data();
|
|
|
|
if (!IsSubsampleEncryptionRequired()) {
|
|
|
|
EncryptBytes(data, sample->data_size());
|
|
|
|
} else {
|
|
|
|
BufferReader reader(data, sample->data_size());
|
|
|
|
while (reader.HasBytes(1)) {
|
|
|
|
uint64 nalu_length;
|
|
|
|
if (!reader.ReadNBytesInto8(&nalu_length, nalu_length_size_))
|
|
|
|
return Status(error::MUXER_FAILURE, "Fail to read nalu_length.");
|
|
|
|
|
|
|
|
SubsampleEntry subsample;
|
|
|
|
subsample.clear_bytes = nalu_length_size_ + 1;
|
|
|
|
subsample.cypher_bytes = nalu_length - 1;
|
|
|
|
if (!reader.SkipBytes(nalu_length)) {
|
|
|
|
return Status(error::MUXER_FAILURE,
|
|
|
|
"Sample size does not match nalu_length.");
|
|
|
|
}
|
|
|
|
|
|
|
|
EncryptBytes(data + subsample.clear_bytes, subsample.cypher_bytes);
|
|
|
|
cenc_info.AddSubsample(subsample);
|
|
|
|
data += nalu_length_size_ + nalu_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The length of per-sample auxiliary datum, defined in CENC ch. 7.
|
|
|
|
traf_->auxiliary_size.sample_info_sizes.push_back(cenc_info.ComputeSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
cenc_info.Write(aux_data_.get());
|
2014-01-14 01:38:34 +00:00
|
|
|
encryptor_->UpdateIv();
|
2013-11-12 20:37:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MP4Fragmenter::StartsWithSAP() {
|
|
|
|
DCHECK(!traf_->runs.empty());
|
|
|
|
uint32 start_sample_flag;
|
|
|
|
if (traf_->runs[0].flags & TrackFragmentRun::kSampleFlagsPresentMask) {
|
|
|
|
DCHECK(!traf_->runs[0].sample_flags.empty());
|
|
|
|
start_sample_flag = traf_->runs[0].sample_flags[0];
|
|
|
|
} else {
|
|
|
|
DCHECK(traf_->header.flags &
|
|
|
|
TrackFragmentHeader::kDefaultSampleFlagsPresentMask);
|
|
|
|
start_sample_flag = traf_->header.default_sample_flags;
|
|
|
|
}
|
|
|
|
return (start_sample_flag & TrackFragmentHeader::kNonKeySampleMask) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|