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-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/segmenter.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2016-03-28 08:23:20 +00:00
|
|
|
#include "packager/base/logging.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/buffer_writer.h"
|
|
|
|
#include "packager/media/base/media_sample.h"
|
|
|
|
#include "packager/media/base/muxer_options.h"
|
2016-04-27 22:04:50 +00:00
|
|
|
#include "packager/media/base/muxer_util.h"
|
2017-03-11 02:49:55 +00:00
|
|
|
#include "packager/media/base/stream_info.h"
|
|
|
|
#include "packager/media/chunking/chunking_handler.h"
|
2015-05-11 21:07:10 +00:00
|
|
|
#include "packager/media/event/progress_listener.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/formats/mp4/box_definitions.h"
|
2017-03-11 02:49:55 +00:00
|
|
|
#include "packager/media/formats/mp4/fragmenter.h"
|
2016-07-07 19:34:07 +00:00
|
|
|
#include "packager/version/version.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-04-18 22:00:30 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
namespace {
|
2015-09-24 22:03:52 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t Rescale(uint64_t time_in_old_scale,
|
|
|
|
uint32_t old_scale,
|
|
|
|
uint32_t new_scale) {
|
2013-11-12 20:37:58 +00:00
|
|
|
return static_cast<double>(time_in_old_scale) / old_scale * new_scale;
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
} // namespace
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
Segmenter::Segmenter(const MuxerOptions& options,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<FileType> ftyp,
|
|
|
|
std::unique_ptr<Movie> moov)
|
2013-11-12 20:37:58 +00:00
|
|
|
: options_(options),
|
2016-08-17 17:41:40 +00:00
|
|
|
ftyp_(std::move(ftyp)),
|
|
|
|
moov_(std::move(moov)),
|
2013-11-12 20:37:58 +00:00
|
|
|
moof_(new MovieFragment()),
|
2014-03-21 17:26:49 +00:00
|
|
|
fragment_buffer_(new BufferWriter()),
|
2013-11-12 20:37:58 +00:00
|
|
|
sidx_(new SegmentIndex()),
|
2015-05-11 21:07:10 +00:00
|
|
|
progress_listener_(NULL),
|
|
|
|
progress_target_(0),
|
2015-06-15 21:12:42 +00:00
|
|
|
accumulated_progress_(0),
|
2015-09-24 22:03:52 +00:00
|
|
|
sample_duration_(0u) {}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2016-08-30 23:01:19 +00:00
|
|
|
Segmenter::~Segmenter() {}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
Status Segmenter::Initialize(
|
|
|
|
const std::vector<std::shared_ptr<StreamInfo>>& streams,
|
|
|
|
MuxerListener* muxer_listener,
|
2017-03-11 02:49:55 +00:00
|
|
|
ProgressListener* progress_listener) {
|
2014-03-21 17:26:49 +00:00
|
|
|
DCHECK_LT(0u, streams.size());
|
2014-05-22 18:51:55 +00:00
|
|
|
muxer_listener_ = muxer_listener;
|
2015-05-11 21:07:10 +00:00
|
|
|
progress_listener_ = progress_listener;
|
2013-11-12 20:37:58 +00:00
|
|
|
moof_->header.sequence_number = 0;
|
|
|
|
|
|
|
|
moof_->tracks.resize(streams.size());
|
|
|
|
fragmenters_.resize(streams.size());
|
2015-09-24 22:03:52 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
for (uint32_t i = 0; i < streams.size(); ++i) {
|
2013-11-12 20:37:58 +00:00
|
|
|
moof_->tracks[i].header.track_id = i + 1;
|
2017-02-21 18:36:50 +00:00
|
|
|
if (streams[i]->stream_type() == kStreamVideo) {
|
2014-04-24 21:32:18 +00:00
|
|
|
// Use the first video stream as the reference stream (which is 1-based).
|
2013-11-12 20:37:58 +00:00
|
|
|
if (sidx_->reference_id == 0)
|
|
|
|
sidx_->reference_id = i + 1;
|
|
|
|
}
|
2017-03-11 02:49:55 +00:00
|
|
|
fragmenters_[i].reset(new Fragmenter(streams[i], &moof_->tracks[i]));
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 20:48:28 +00:00
|
|
|
if (options_.mp4_use_decoding_timestamp_in_timeline) {
|
|
|
|
for (uint32_t i = 0; i < streams.size(); ++i)
|
|
|
|
fragmenters_[i]->set_use_decoding_timestamp_in_timeline(true);
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
// Choose the first stream if there is no VIDEO.
|
|
|
|
if (sidx_->reference_id == 0)
|
|
|
|
sidx_->reference_id = 1;
|
2017-02-21 18:36:50 +00:00
|
|
|
sidx_->timescale = streams[GetReferenceStreamId()]->time_scale();
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2015-05-11 21:07:10 +00:00
|
|
|
// Use media duration as progress target.
|
2017-02-21 18:36:50 +00:00
|
|
|
progress_target_ = streams[GetReferenceStreamId()]->duration();
|
2015-05-11 21:07:10 +00:00
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
// Use the reference stream's time scale as movie time scale.
|
|
|
|
moov_->header.timescale = sidx_->timescale;
|
2014-06-30 16:32:06 +00:00
|
|
|
moof_->header.sequence_number = 1;
|
2015-12-21 23:10:17 +00:00
|
|
|
|
|
|
|
// Fill in version information.
|
2016-07-07 19:34:07 +00:00
|
|
|
const std::string version = GetPackagerVersion();
|
|
|
|
if (!version.empty()) {
|
|
|
|
moov_->metadata.handler.handler_type = FOURCC_ID32;
|
|
|
|
moov_->metadata.id3v2.language.code = "eng";
|
|
|
|
moov_->metadata.id3v2.private_frame.owner = GetPackagerProjectUrl();
|
|
|
|
moov_->metadata.id3v2.private_frame.value = version;
|
|
|
|
}
|
2014-06-30 16:32:06 +00:00
|
|
|
return DoInitialize();
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
Status Segmenter::Finalize() {
|
2013-11-12 20:37:58 +00:00
|
|
|
// Set tracks and moov durations.
|
|
|
|
// Note that the updated moov box will be written to output file for VOD case
|
|
|
|
// only.
|
|
|
|
for (std::vector<Track>::iterator track = moov_->tracks.begin();
|
|
|
|
track != moov_->tracks.end();
|
|
|
|
++track) {
|
|
|
|
track->header.duration = Rescale(track->media.header.duration,
|
|
|
|
track->media.header.timescale,
|
|
|
|
moov_->header.timescale);
|
|
|
|
if (track->header.duration > moov_->header.duration)
|
|
|
|
moov_->header.duration = track->header.duration;
|
|
|
|
}
|
2014-11-06 23:13:35 +00:00
|
|
|
moov_->extends.header.fragment_duration = moov_->header.duration;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
return DoFinalize();
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
Status Segmenter::AddSample(size_t stream_id,
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> sample) {
|
2013-11-12 20:37:58 +00:00
|
|
|
// Set default sample duration if it has not been set yet.
|
|
|
|
if (moov_->extends.tracks[stream_id].default_sample_duration == 0) {
|
|
|
|
moov_->extends.tracks[stream_id].default_sample_duration =
|
|
|
|
sample->duration();
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
DCHECK_LT(stream_id, fragmenters_.size());
|
2017-02-24 01:17:47 +00:00
|
|
|
Fragmenter* fragmenter = fragmenters_[stream_id].get();
|
2013-11-12 20:37:58 +00:00
|
|
|
if (fragmenter->fragment_finalized()) {
|
|
|
|
return Status(error::FRAGMENT_FINALIZED,
|
|
|
|
"Current fragment is finalized already.");
|
|
|
|
}
|
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
Status status = fragmenter->AddSample(sample);
|
2013-11-12 20:37:58 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
|
2015-06-15 21:12:42 +00:00
|
|
|
if (sample_duration_ == 0)
|
|
|
|
sample_duration_ = sample->duration();
|
2013-11-12 20:37:58 +00:00
|
|
|
moov_->tracks[stream_id].media.header.duration += sample->duration();
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
Status Segmenter::FinalizeSegment(size_t stream_id,
|
|
|
|
std::shared_ptr<SegmentInfo> segment_info) {
|
|
|
|
if (segment_info->key_rotation_encryption_config) {
|
|
|
|
FinalizeFragmentForKeyRotation(
|
|
|
|
stream_id, segment_info->is_encrypted,
|
|
|
|
*segment_info->key_rotation_encryption_config);
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
DCHECK_LT(stream_id, fragmenters_.size());
|
2017-02-24 01:17:47 +00:00
|
|
|
Fragmenter* fragmenter = fragmenters_[stream_id].get();
|
|
|
|
DCHECK(fragmenter);
|
2017-03-11 02:49:55 +00:00
|
|
|
Status status = fragmenter->FinalizeFragment();
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
// Check if all tracks are ready for fragmentation.
|
2016-08-30 23:01:19 +00:00
|
|
|
for (const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_) {
|
|
|
|
if (!fragmenter->fragment_finalized())
|
2013-11-12 20:37:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaData mdat;
|
2015-02-06 20:31:46 +00:00
|
|
|
// Data offset relative to 'moof': moof size + mdat header size.
|
|
|
|
// The code will also update box sizes for moof_ and its child boxes.
|
|
|
|
uint64_t data_offset = moof_->ComputeSize() + mdat.HeaderSize();
|
|
|
|
// 'traf' should follow 'mfhd' moof header box.
|
|
|
|
uint64_t next_traf_position = moof_->HeaderSize() + moof_->header.box_size();
|
2015-03-21 02:18:38 +00:00
|
|
|
for (size_t i = 0; i < moof_->tracks.size(); ++i) {
|
2013-11-12 20:37:58 +00:00
|
|
|
TrackFragment& traf = moof_->tracks[i];
|
2015-02-06 20:31:46 +00:00
|
|
|
if (traf.auxiliary_offset.offsets.size() > 0) {
|
|
|
|
DCHECK_EQ(traf.auxiliary_offset.offsets.size(), 1u);
|
|
|
|
DCHECK(!traf.sample_encryption.sample_encryption_entries.empty());
|
|
|
|
|
|
|
|
next_traf_position += traf.box_size();
|
|
|
|
// SampleEncryption 'senc' box should be the last box in 'traf'.
|
|
|
|
// |auxiliary_offset| should point to the data of SampleEncryption.
|
|
|
|
traf.auxiliary_offset.offsets[0] =
|
|
|
|
next_traf_position - traf.sample_encryption.box_size() +
|
|
|
|
traf.sample_encryption.HeaderSize() +
|
|
|
|
sizeof(uint32_t); // for sample count field in 'senc'
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
2015-02-06 20:31:46 +00:00
|
|
|
traf.runs[0].data_offset = data_offset + mdat.data_size;
|
2016-11-09 02:11:13 +00:00
|
|
|
mdat.data_size += static_cast<uint32_t>(fragmenters_[i]->data()->Size());
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate segment reference.
|
|
|
|
sidx_->references.resize(sidx_->references.size() + 1);
|
|
|
|
fragmenters_[GetReferenceStreamId()]->GenerateSegmentReference(
|
|
|
|
&sidx_->references[sidx_->references.size() - 1]);
|
2015-02-06 20:31:46 +00:00
|
|
|
sidx_->references[sidx_->references.size() - 1].referenced_size =
|
|
|
|
data_offset + mdat.data_size;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
// Write the fragment to buffer.
|
|
|
|
moof_->Write(fragment_buffer_.get());
|
2015-02-06 20:31:46 +00:00
|
|
|
mdat.WriteHeader(fragment_buffer_.get());
|
2016-08-30 23:01:19 +00:00
|
|
|
for (const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_)
|
2013-11-12 20:37:58 +00:00
|
|
|
fragment_buffer_->AppendBuffer(*fragmenter->data());
|
|
|
|
|
2014-06-30 16:32:06 +00:00
|
|
|
// Increase sequence_number for next fragment.
|
|
|
|
++moof_->header.sequence_number;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
for (std::unique_ptr<Fragmenter>& fragmenter : fragmenters_)
|
|
|
|
fragmenter->ClearFragmentFinalized();
|
2017-03-11 02:49:55 +00:00
|
|
|
if (!segment_info->is_subsegment) {
|
2017-02-24 01:17:47 +00:00
|
|
|
Status status = DoFinalizeSegment();
|
|
|
|
// Reset segment information to initial state.
|
|
|
|
sidx_->references.clear();
|
|
|
|
return status;
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-02-24 01:17:47 +00:00
|
|
|
uint32_t Segmenter::GetReferenceTimeScale() const {
|
|
|
|
return moov_->header.timescale;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Segmenter::GetDuration() const {
|
|
|
|
if (moov_->header.timescale == 0) {
|
|
|
|
// Handling the case where this is not properly initialized.
|
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<double>(moov_->header.duration) / moov_->header.timescale;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Segmenter::UpdateProgress(uint64_t progress) {
|
|
|
|
accumulated_progress_ += progress;
|
|
|
|
|
|
|
|
if (!progress_listener_) return;
|
|
|
|
if (progress_target_ == 0) return;
|
|
|
|
// It might happen that accumulated progress exceeds progress_target due to
|
|
|
|
// computation errors, e.g. rounding error. Cap it so it never reports > 100%
|
|
|
|
// progress.
|
|
|
|
if (accumulated_progress_ >= progress_target_) {
|
|
|
|
progress_listener_->OnProgress(1.0);
|
|
|
|
} else {
|
|
|
|
progress_listener_->OnProgress(static_cast<double>(accumulated_progress_) /
|
|
|
|
progress_target_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Segmenter::SetComplete() {
|
|
|
|
if (!progress_listener_) return;
|
|
|
|
progress_listener_->OnProgress(1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Segmenter::GetReferenceStreamId() {
|
|
|
|
DCHECK(sidx_);
|
|
|
|
return sidx_->reference_id - 1;
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
void Segmenter::FinalizeFragmentForKeyRotation(
|
|
|
|
size_t stream_id,
|
|
|
|
bool fragment_encrypted,
|
|
|
|
const EncryptionConfig& encryption_config) {
|
|
|
|
const std::vector<ProtectionSystemSpecificInfo>& system_info =
|
|
|
|
encryption_config.key_system_info;
|
|
|
|
moof_->pssh.resize(system_info.size());
|
|
|
|
for (size_t i = 0; i < system_info.size(); i++)
|
|
|
|
moof_->pssh[i].raw_box = system_info[i].CreateBox();
|
|
|
|
|
|
|
|
// Skip the following steps if the current fragment is not going to be
|
|
|
|
// encrypted. 'pssh' box needs to be included in the fragment, which is
|
|
|
|
// performed above, regardless of whether the fragment is encrypted. This is
|
|
|
|
// necessary for two reasons: 1) Requesting keys before reaching encrypted
|
|
|
|
// content avoids playback delay due to license requests; 2) In Chrome, CDM
|
|
|
|
// must be initialized before starting the playback and CDM can only be
|
|
|
|
// initialized with a valid 'pssh'.
|
|
|
|
if (!fragment_encrypted)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DCHECK_LT(stream_id, moof_->tracks.size());
|
|
|
|
TrackFragment& traf = moof_->tracks[stream_id];
|
|
|
|
traf.sample_group_descriptions.resize(traf.sample_group_descriptions.size() +
|
|
|
|
1);
|
|
|
|
SampleGroupDescription& sample_group_description =
|
|
|
|
traf.sample_group_descriptions.back();
|
|
|
|
sample_group_description.grouping_type = FOURCC_seig;
|
|
|
|
|
|
|
|
sample_group_description.cenc_sample_encryption_info_entries.resize(1);
|
|
|
|
CencSampleEncryptionInfoEntry& sample_group_entry =
|
|
|
|
sample_group_description.cenc_sample_encryption_info_entries.back();
|
|
|
|
sample_group_entry.is_protected = 1;
|
|
|
|
sample_group_entry.per_sample_iv_size = encryption_config.per_sample_iv_size;
|
|
|
|
sample_group_entry.constant_iv = encryption_config.constant_iv;
|
|
|
|
sample_group_entry.crypt_byte_block = encryption_config.crypt_byte_block;
|
|
|
|
sample_group_entry.skip_byte_block = encryption_config.skip_byte_block;
|
|
|
|
sample_group_entry.key_id = encryption_config.key_id;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|