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"
|
2018-02-01 20:25:07 +00:00
|
|
|
#include "packager/media/formats/mp4/key_frame_info.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()),
|
2017-03-15 19:42:00 +00:00
|
|
|
sidx_(new SegmentIndex()) {}
|
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(
|
2017-09-12 17:24:24 +00:00
|
|
|
const std::vector<std::shared_ptr<const StreamInfo>>& streams,
|
2017-02-21 18:36:50 +00:00
|
|
|
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());
|
2017-03-15 19:42:00 +00:00
|
|
|
stream_durations_.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
|
|
|
}
|
|
|
|
|
2017-08-18 18:57:34 +00:00
|
|
|
if (options_.mp4_params.use_decoding_timestamp_in_timeline) {
|
2016-07-12 20:48:28 +00:00
|
|
|
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() {
|
2017-03-15 19:42:00 +00:00
|
|
|
// Set movie duration. Note that the duration in mvhd, tkhd, mdhd should not
|
|
|
|
// be touched, i.e. kept at 0. The updated moov box will be written to output
|
2018-03-05 17:44:22 +00:00
|
|
|
// file for VOD and static live case only.
|
2017-03-15 19:42:00 +00:00
|
|
|
moov_->extends.header.fragment_duration = 0;
|
|
|
|
for (size_t i = 0; i < stream_durations_.size(); ++i) {
|
|
|
|
uint64_t duration =
|
|
|
|
Rescale(stream_durations_[i], moov_->tracks[i].media.header.timescale,
|
|
|
|
moov_->header.timescale);
|
|
|
|
if (duration > moov_->extends.header.fragment_duration)
|
|
|
|
moov_->extends.header.fragment_duration = 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-09-12 17:24:24 +00:00
|
|
|
Status Segmenter::AddSample(size_t stream_id, const 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 =
|
2017-09-12 17:24:24 +00:00
|
|
|
sample.duration();
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2017-09-12 17:24:24 +00:00
|
|
|
sample_duration_ = sample.duration();
|
|
|
|
stream_durations_[stream_id] += sample.duration();
|
2013-11-12 20:37:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
Status Segmenter::FinalizeSegment(size_t stream_id,
|
2017-09-12 17:24:24 +00:00
|
|
|
const SegmentInfo& segment_info) {
|
|
|
|
if (segment_info.key_rotation_encryption_config) {
|
2017-03-11 02:49:55 +00:00
|
|
|
FinalizeFragmentForKeyRotation(
|
2017-09-12 17:24:24 +00:00
|
|
|
stream_id, segment_info.is_encrypted,
|
|
|
|
*segment_info.key_rotation_encryption_config);
|
2017-03-11 02:49:55 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-02-05 19:21:28 +00:00
|
|
|
const uint64_t moof_start_offset = fragment_buffer_->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());
|
2018-02-05 19:21:28 +00:00
|
|
|
|
|
|
|
bool first_key_frame = true;
|
2018-02-01 20:25:07 +00:00
|
|
|
for (const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_) {
|
2018-02-05 19:21:28 +00:00
|
|
|
// https://goo.gl/xcFus6 6. Trick play requirements
|
|
|
|
// 6.10. If using fMP4, I-frame segments must include the 'moof' header
|
|
|
|
// associated with the I-frame. It also implies that only the first key
|
|
|
|
// frame can be included.
|
|
|
|
if (!fragmenter->key_frame_infos().empty() && first_key_frame) {
|
|
|
|
const KeyFrameInfo& key_frame_info =
|
|
|
|
fragmenter->key_frame_infos().front();
|
|
|
|
first_key_frame = false;
|
|
|
|
key_frame_infos_.push_back(
|
|
|
|
{key_frame_info.timestamp, moof_start_offset,
|
|
|
|
fragment_buffer_->Size() - moof_start_offset + key_frame_info.size});
|
2018-02-01 20:25:07 +00:00
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
fragment_buffer_->AppendBuffer(*fragmenter->data());
|
2018-02-01 20:25:07 +00:00
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
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-09-12 17:24:24 +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();
|
2018-02-01 20:25:07 +00:00
|
|
|
key_frame_infos_.clear();
|
2017-02-24 01:17:47 +00:00
|
|
|
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 {
|
2017-03-15 19:42:00 +00:00
|
|
|
uint64_t duration = moov_->extends.header.fragment_duration;
|
|
|
|
if (duration == 0) {
|
2017-02-24 01:17:47 +00:00
|
|
|
// Handling the case where this is not properly initialized.
|
|
|
|
return 0.0;
|
|
|
|
}
|
2017-03-15 19:42:00 +00:00
|
|
|
return static_cast<double>(duration) / moov_->header.timescale;
|
2017-02-24 01:17:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-08-18 18:57:34 +00:00
|
|
|
if (options_.mp4_params.include_pssh_in_stream) {
|
2017-03-15 19:42:00 +00:00
|
|
|
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();
|
|
|
|
} else {
|
|
|
|
LOG(WARNING)
|
|
|
|
<< "Key rotation and no pssh in stream may not work well together.";
|
|
|
|
}
|
2017-03-11 02:49:55 +00:00
|
|
|
|
|
|
|
// 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
|