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/base/stl_util.h"
|
2016-04-08 18:41:17 +00:00
|
|
|
#include "packager/media/base/aes_cryptor.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/buffer_writer.h"
|
|
|
|
#include "packager/media/base/key_source.h"
|
|
|
|
#include "packager/media/base/media_sample.h"
|
|
|
|
#include "packager/media/base/media_stream.h"
|
|
|
|
#include "packager/media/base/muxer_options.h"
|
2016-04-27 22:04:50 +00:00
|
|
|
#include "packager/media/base/muxer_util.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/video_stream_info.h"
|
2015-07-08 00:52:28 +00:00
|
|
|
#include "packager/media/event/muxer_listener.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"
|
|
|
|
#include "packager/media/formats/mp4/key_rotation_fragmenter.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 {
|
2014-04-18 18:49:49 +00:00
|
|
|
const size_t kCencKeyIdSize = 16u;
|
2014-04-18 22:00:30 +00:00
|
|
|
|
|
|
|
// The version of cenc implemented here. CENC 4.
|
|
|
|
const int kCencSchemeVersion = 0x00010000;
|
|
|
|
|
2015-09-24 22:03:52 +00:00
|
|
|
// The default KID for key rotation is all 0s.
|
|
|
|
const uint8_t kKeyRotationDefaultKeyId[] = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0
|
|
|
|
};
|
2016-03-17 17:03:19 +00:00
|
|
|
|
2016-05-26 22:44:37 +00:00
|
|
|
// Defines protection pattern for pattern-based encryption.
|
|
|
|
struct ProtectionPattern {
|
|
|
|
uint8_t crypt_byte_block;
|
|
|
|
uint8_t skip_byte_block;
|
|
|
|
};
|
|
|
|
|
2015-09-24 22:03:52 +00:00
|
|
|
COMPILE_ASSERT(arraysize(kKeyRotationDefaultKeyId) == kCencKeyIdSize,
|
|
|
|
cenc_key_id_must_be_size_16);
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-26 22:44:37 +00:00
|
|
|
ProtectionPattern GetProtectionPattern(FourCC protection_scheme,
|
|
|
|
TrackType track_type) {
|
|
|
|
ProtectionPattern pattern;
|
|
|
|
if (protection_scheme != FOURCC_cbcs && protection_scheme != FOURCC_cens) {
|
|
|
|
// Not using pattern encryption.
|
|
|
|
pattern.crypt_byte_block = 0u;
|
|
|
|
pattern.skip_byte_block = 0u;
|
|
|
|
} else if (track_type != kVideo) {
|
|
|
|
// Tracks other than video are protected using whole-block full-sample
|
|
|
|
// encryption, which is essentially a pattern of 1:0. Note that this may not
|
|
|
|
// be the same as the non-pattern based encryption counterparts, e.g. in
|
|
|
|
// 'cens' for full sample encryption, the whole sample is encrypted up to
|
|
|
|
// the last 16-byte boundary, see 23001-7:2016(E) 9.7; while in 'cenc' for
|
|
|
|
// full sample encryption, the last partial 16-byte block is also encrypted,
|
|
|
|
// see 23001-7:2016(E) 9.4.2. Another difference is the use of constant iv.
|
|
|
|
pattern.crypt_byte_block = 1u;
|
|
|
|
pattern.skip_byte_block = 0u;
|
|
|
|
} else {
|
|
|
|
// Use 1:9 pattern for video.
|
|
|
|
const uint8_t kCryptByteBlock = 1u;
|
|
|
|
const uint8_t kSkipByteBlock = 9u;
|
|
|
|
pattern.crypt_byte_block = kCryptByteBlock;
|
|
|
|
pattern.skip_byte_block = kSkipByteBlock;
|
|
|
|
}
|
|
|
|
return pattern;
|
2016-04-06 00:28:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
void GenerateSinf(const EncryptionKey& encryption_key,
|
|
|
|
FourCC old_type,
|
2016-04-08 18:41:17 +00:00
|
|
|
FourCC protection_scheme,
|
2016-05-26 22:44:37 +00:00
|
|
|
ProtectionPattern pattern,
|
2014-04-18 22:00:30 +00:00
|
|
|
ProtectionSchemeInfo* sinf) {
|
|
|
|
sinf->format.format = old_type;
|
2016-03-17 17:03:19 +00:00
|
|
|
|
2016-04-08 18:41:17 +00:00
|
|
|
DCHECK_NE(protection_scheme, FOURCC_NULL);
|
|
|
|
sinf->type.type = protection_scheme;
|
2014-04-18 22:00:30 +00:00
|
|
|
sinf->type.version = kCencSchemeVersion;
|
2016-04-08 18:41:17 +00:00
|
|
|
|
|
|
|
auto& track_encryption = sinf->info.track_encryption;
|
2016-04-12 22:39:10 +00:00
|
|
|
track_encryption.default_is_protected = 1;
|
2016-04-08 18:41:17 +00:00
|
|
|
DCHECK(!encryption_key.iv.empty());
|
2016-04-06 00:28:09 +00:00
|
|
|
if (protection_scheme == FOURCC_cbcs) {
|
|
|
|
// ISO/IEC 23001-7:2016 10.4.1
|
|
|
|
// For 'cbcs' scheme, Constant IVs SHALL be used.
|
|
|
|
track_encryption.default_per_sample_iv_size = 0;
|
|
|
|
track_encryption.default_constant_iv = encryption_key.iv;
|
|
|
|
} else {
|
|
|
|
track_encryption.default_per_sample_iv_size = encryption_key.iv.size();
|
|
|
|
}
|
2016-05-26 22:44:37 +00:00
|
|
|
track_encryption.default_crypt_byte_block = pattern.crypt_byte_block;
|
|
|
|
track_encryption.default_skip_byte_block = pattern.skip_byte_block;
|
2016-04-08 18:41:17 +00:00
|
|
|
track_encryption.default_kid = encryption_key.key_id;
|
2014-04-18 22:00:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateEncryptedSampleEntry(const EncryptionKey& encryption_key,
|
|
|
|
double clear_lead_in_seconds,
|
2016-04-08 18:41:17 +00:00
|
|
|
FourCC protection_scheme,
|
2016-05-26 22:44:37 +00:00
|
|
|
ProtectionPattern pattern,
|
2014-04-18 22:00:30 +00:00
|
|
|
SampleDescription* description) {
|
|
|
|
DCHECK(description);
|
|
|
|
if (description->type == kVideo) {
|
|
|
|
DCHECK_EQ(1u, description->video_entries.size());
|
|
|
|
|
|
|
|
// Add a second entry for clear content if needed.
|
|
|
|
if (clear_lead_in_seconds > 0)
|
|
|
|
description->video_entries.push_back(description->video_entries[0]);
|
|
|
|
|
|
|
|
// Convert the first entry to an encrypted entry.
|
|
|
|
VideoSampleEntry& entry = description->video_entries[0];
|
2016-05-26 22:44:37 +00:00
|
|
|
GenerateSinf(encryption_key, entry.format, protection_scheme, pattern,
|
|
|
|
&entry.sinf);
|
2016-04-06 23:21:45 +00:00
|
|
|
entry.format = FOURCC_encv;
|
2014-04-18 22:00:30 +00:00
|
|
|
} else {
|
|
|
|
DCHECK_EQ(kAudio, description->type);
|
|
|
|
DCHECK_EQ(1u, description->audio_entries.size());
|
|
|
|
|
|
|
|
// Add a second entry for clear content if needed.
|
|
|
|
if (clear_lead_in_seconds > 0)
|
|
|
|
description->audio_entries.push_back(description->audio_entries[0]);
|
|
|
|
|
|
|
|
// Convert the first entry to an encrypted entry.
|
|
|
|
AudioSampleEntry& entry = description->audio_entries[0];
|
2016-05-26 22:44:37 +00:00
|
|
|
GenerateSinf(encryption_key, entry.format, protection_scheme, pattern,
|
|
|
|
&entry.sinf);
|
2016-04-06 23:21:45 +00:00
|
|
|
entry.format = FOURCC_enca;
|
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,
|
|
|
|
scoped_ptr<FileType> ftyp,
|
|
|
|
scoped_ptr<Movie> moov)
|
2013-11-12 20:37:58 +00:00
|
|
|
: options_(options),
|
2014-01-08 19:56:59 +00:00
|
|
|
ftyp_(ftyp.Pass()),
|
|
|
|
moov_(moov.Pass()),
|
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
|
|
|
muxer_listener_(NULL),
|
|
|
|
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
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
Segmenter::~Segmenter() { STLDeleteElements(&fragmenters_); }
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
Status Segmenter::Initialize(const std::vector<MediaStream*>& streams,
|
2015-05-07 21:06:16 +00:00
|
|
|
MuxerListener* muxer_listener,
|
2015-05-11 21:07:10 +00:00
|
|
|
ProgressListener* progress_listener,
|
2014-08-20 23:51:15 +00:00
|
|
|
KeySource* encryption_key_source,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t max_sd_pixels,
|
2014-04-18 18:49:49 +00:00
|
|
|
double clear_lead_in_seconds,
|
2016-03-17 17:03:19 +00:00
|
|
|
double crypto_period_duration_in_seconds,
|
2016-04-08 18:41:17 +00:00
|
|
|
FourCC protection_scheme) {
|
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());
|
|
|
|
segment_durations_.resize(streams.size());
|
|
|
|
fragmenters_.resize(streams.size());
|
2015-09-24 22:03:52 +00:00
|
|
|
const bool key_rotation_enabled = crypto_period_duration_in_seconds != 0;
|
|
|
|
const bool kInitialEncryptionInfo = true;
|
|
|
|
|
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
|
|
|
stream_map_[streams[i]] = i;
|
|
|
|
moof_->tracks[i].header.track_id = i + 1;
|
|
|
|
if (streams[i]->info()->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;
|
|
|
|
}
|
2014-04-18 18:49:49 +00:00
|
|
|
if (!encryption_key_source) {
|
2016-05-06 23:56:50 +00:00
|
|
|
fragmenters_[i] = new Fragmenter(streams[i]->info(), &moof_->tracks[i]);
|
2014-04-18 18:49:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:51:15 +00:00
|
|
|
KeySource::TrackType track_type =
|
2014-04-24 21:32:18 +00:00
|
|
|
GetTrackTypeForEncryption(*streams[i]->info(), max_sd_pixels);
|
2014-04-18 18:49:49 +00:00
|
|
|
SampleDescription& description =
|
|
|
|
moov_->tracks[i].media.information.sample_table.description;
|
2016-05-26 22:44:37 +00:00
|
|
|
ProtectionPattern pattern =
|
|
|
|
GetProtectionPattern(protection_scheme, description.type);
|
2014-04-18 18:49:49 +00:00
|
|
|
|
|
|
|
if (key_rotation_enabled) {
|
2015-09-24 22:03:52 +00:00
|
|
|
// Fill encrypted sample entry with default key.
|
|
|
|
EncryptionKey encryption_key;
|
|
|
|
encryption_key.key_id.assign(
|
|
|
|
kKeyRotationDefaultKeyId,
|
|
|
|
kKeyRotationDefaultKeyId + arraysize(kKeyRotationDefaultKeyId));
|
2016-05-26 22:44:37 +00:00
|
|
|
if (!AesCryptor::GenerateRandomIv(protection_scheme,
|
2016-04-06 00:28:09 +00:00
|
|
|
&encryption_key.iv)) {
|
2016-04-08 18:41:17 +00:00
|
|
|
return Status(error::INTERNAL_ERROR, "Failed to generate random iv.");
|
2016-04-06 00:28:09 +00:00
|
|
|
}
|
2015-09-24 22:03:52 +00:00
|
|
|
GenerateEncryptedSampleEntry(encryption_key, clear_lead_in_seconds,
|
2016-05-26 22:44:37 +00:00
|
|
|
protection_scheme, pattern, &description);
|
2015-09-24 22:03:52 +00:00
|
|
|
if (muxer_listener_) {
|
|
|
|
muxer_listener_->OnEncryptionInfoReady(
|
2016-05-26 22:44:37 +00:00
|
|
|
kInitialEncryptionInfo, protection_scheme, encryption_key.key_id,
|
|
|
|
encryption_key.iv, encryption_key.key_system_info);
|
2015-09-24 22:03:52 +00:00
|
|
|
}
|
2014-04-18 18:49:49 +00:00
|
|
|
|
|
|
|
fragmenters_[i] = new KeyRotationFragmenter(
|
2016-02-03 22:13:57 +00:00
|
|
|
moof_.get(), streams[i]->info(), &moof_->tracks[i],
|
|
|
|
encryption_key_source, track_type,
|
2014-04-18 18:49:49 +00:00
|
|
|
crypto_period_duration_in_seconds * streams[i]->info()->time_scale(),
|
2016-02-03 22:13:57 +00:00
|
|
|
clear_lead_in_seconds * streams[i]->info()->time_scale(),
|
2016-05-26 22:44:37 +00:00
|
|
|
protection_scheme, pattern.crypt_byte_block, pattern.skip_byte_block,
|
|
|
|
muxer_listener_);
|
2014-04-18 18:49:49 +00:00
|
|
|
continue;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
2014-04-18 18:49:49 +00:00
|
|
|
|
|
|
|
scoped_ptr<EncryptionKey> encryption_key(new EncryptionKey());
|
|
|
|
Status status =
|
2014-04-24 21:32:18 +00:00
|
|
|
encryption_key_source->GetKey(track_type, encryption_key.get());
|
2014-04-18 18:49:49 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
2016-04-08 18:41:17 +00:00
|
|
|
if (encryption_key->iv.empty()) {
|
2016-05-26 22:44:37 +00:00
|
|
|
if (!AesCryptor::GenerateRandomIv(protection_scheme,
|
2016-04-06 00:28:09 +00:00
|
|
|
&encryption_key->iv)) {
|
2016-04-08 18:41:17 +00:00
|
|
|
return Status(error::INTERNAL_ERROR, "Failed to generate random iv.");
|
2016-04-06 00:28:09 +00:00
|
|
|
}
|
2016-04-08 18:41:17 +00:00
|
|
|
}
|
2014-04-18 18:49:49 +00:00
|
|
|
|
2015-09-24 22:03:52 +00:00
|
|
|
GenerateEncryptedSampleEntry(*encryption_key, clear_lead_in_seconds,
|
2016-05-26 22:44:37 +00:00
|
|
|
protection_scheme, pattern, &description);
|
2014-04-18 18:49:49 +00:00
|
|
|
|
|
|
|
if (moov_->pssh.empty()) {
|
2016-02-17 22:03:43 +00:00
|
|
|
moov_->pssh.resize(encryption_key->key_system_info.size());
|
|
|
|
for (size_t i = 0; i < encryption_key->key_system_info.size(); i++) {
|
|
|
|
moov_->pssh[i].raw_box = encryption_key->key_system_info[i].CreateBox();
|
|
|
|
}
|
2015-07-08 00:52:28 +00:00
|
|
|
|
|
|
|
if (muxer_listener_) {
|
2016-04-21 23:21:16 +00:00
|
|
|
muxer_listener_->OnEncryptionInfoReady(
|
2016-05-26 22:44:37 +00:00
|
|
|
kInitialEncryptionInfo, protection_scheme, encryption_key->key_id,
|
|
|
|
encryption_key->iv, encryption_key->key_system_info);
|
2015-07-08 00:52:28 +00:00
|
|
|
}
|
2014-04-18 18:49:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 20:53:08 +00:00
|
|
|
fragmenters_[i] = new EncryptingFragmenter(
|
2016-02-03 22:13:57 +00:00
|
|
|
streams[i]->info(), &moof_->tracks[i], encryption_key.Pass(),
|
2016-03-17 17:03:19 +00:00
|
|
|
clear_lead_in_seconds * streams[i]->info()->time_scale(),
|
2016-07-05 23:32:19 +00:00
|
|
|
protection_scheme, pattern.crypt_byte_block, pattern.skip_byte_block,
|
|
|
|
muxer_listener_);
|
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;
|
|
|
|
sidx_->timescale = streams[GetReferenceStreamId()]->info()->time_scale();
|
|
|
|
|
2015-05-11 21:07:10 +00:00
|
|
|
// Use media duration as progress target.
|
|
|
|
progress_target_ = streams[GetReferenceStreamId()]->info()->duration();
|
|
|
|
|
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.
|
|
|
|
moov_->metadata.handler.handler_type = FOURCC_ID32;
|
|
|
|
moov_->metadata.id3v2.language.code = "eng";
|
|
|
|
moov_->metadata.id3v2.private_frame.owner =
|
2016-05-20 21:28:13 +00:00
|
|
|
"https://github.com/google/shaka-packager";
|
2015-12-21 23:10:17 +00:00
|
|
|
moov_->metadata.id3v2.private_frame.value = options_.packager_version_string;
|
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() {
|
|
|
|
for (std::vector<Fragmenter*>::iterator it = fragmenters_.begin();
|
2013-11-12 20:37:58 +00:00
|
|
|
it != fragmenters_.end();
|
|
|
|
++it) {
|
2015-08-04 00:28:11 +00:00
|
|
|
Status status = FinalizeFragment(true, *it);
|
2013-11-12 20:37:58 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
Status Segmenter::AddSample(const MediaStream* stream,
|
|
|
|
scoped_refptr<MediaSample> sample) {
|
2013-11-12 20:37:58 +00:00
|
|
|
// Find the fragmenter for this stream.
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(stream);
|
|
|
|
DCHECK(stream_map_.find(stream) != stream_map_.end());
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t stream_id = stream_map_[stream];
|
2014-04-08 20:21:07 +00:00
|
|
|
Fragmenter* fragmenter = fragmenters_[stream_id];
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fragmenter->fragment_finalized()) {
|
|
|
|
return Status(error::FRAGMENT_FINALIZED,
|
|
|
|
"Current fragment is finalized already.");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool finalize_fragment = false;
|
|
|
|
if (fragmenter->fragment_duration() >=
|
|
|
|
options_.fragment_duration * stream->info()->time_scale()) {
|
|
|
|
if (sample->is_key_frame() || !options_.fragment_sap_aligned) {
|
|
|
|
finalize_fragment = true;
|
|
|
|
}
|
|
|
|
}
|
2015-08-04 00:28:11 +00:00
|
|
|
bool finalize_segment = false;
|
2013-11-12 20:37:58 +00:00
|
|
|
if (segment_durations_[stream_id] >=
|
|
|
|
options_.segment_duration * stream->info()->time_scale()) {
|
|
|
|
if (sample->is_key_frame() || !options_.segment_sap_aligned) {
|
2015-08-04 00:28:11 +00:00
|
|
|
finalize_segment = true;
|
2013-11-12 20:37:58 +00:00
|
|
|
finalize_fragment = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status status;
|
|
|
|
if (finalize_fragment) {
|
2015-08-04 00:28:11 +00:00
|
|
|
status = FinalizeFragment(finalize_segment, fragmenter);
|
2013-11-12 20:37:58 +00:00
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = fragmenter->AddSample(sample);
|
|
|
|
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();
|
|
|
|
segment_durations_[stream_id] += sample->duration();
|
2015-08-04 00:28:11 +00:00
|
|
|
DCHECK_GE(segment_durations_[stream_id], fragmenter->fragment_duration());
|
2013-11-12 20:37:58 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t Segmenter::GetReferenceTimeScale() const {
|
2013-12-12 23:49:31 +00:00
|
|
|
return moov_->header.timescale;
|
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
double Segmenter::GetDuration() const {
|
2013-12-12 23:49:31 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-11 21:07:10 +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);
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:28:11 +00:00
|
|
|
Status Segmenter::FinalizeSegment() {
|
|
|
|
Status status = DoFinalizeSegment();
|
|
|
|
|
|
|
|
// Reset segment information to initial state.
|
2013-11-12 20:37:58 +00:00
|
|
|
sidx_->references.clear();
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint64_t>::iterator it = segment_durations_.begin();
|
2013-11-12 20:37:58 +00:00
|
|
|
for (; it != segment_durations_.end(); ++it)
|
|
|
|
*it = 0;
|
|
|
|
|
2015-08-04 00:28:11 +00:00
|
|
|
return status;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t Segmenter::GetReferenceStreamId() {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(sidx_);
|
2013-11-12 20:37:58 +00:00
|
|
|
return sidx_->reference_id - 1;
|
|
|
|
}
|
|
|
|
|
2015-08-04 00:28:11 +00:00
|
|
|
Status Segmenter::FinalizeFragment(bool finalize_segment,
|
|
|
|
Fragmenter* fragmenter) {
|
2013-11-12 20:37:58 +00:00
|
|
|
fragmenter->FinalizeFragment();
|
|
|
|
|
|
|
|
// Check if all tracks are ready for fragmentation.
|
2014-04-08 20:21:07 +00:00
|
|
|
for (std::vector<Fragmenter*>::iterator it = fragmenters_.begin();
|
2013-11-12 20:37:58 +00:00
|
|
|
it != fragmenters_.end();
|
|
|
|
++it) {
|
|
|
|
if (!(*it)->fragment_finalized())
|
|
|
|
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;
|
|
|
|
mdat.data_size += 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());
|
|
|
|
for (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
|
|
|
|
2015-08-04 00:28:11 +00:00
|
|
|
if (finalize_segment)
|
2013-11-12 20:37:58 +00:00
|
|
|
return FinalizeSegment();
|
|
|
|
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|