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
|
|
|
#include "media/formats/mp4/segmenter.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "base/stl_util.h"
|
|
|
|
#include "media/base/buffer_writer.h"
|
2014-04-16 01:09:32 +00:00
|
|
|
#include "media/base/encryption_key_source.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
#include "media/base/media_sample.h"
|
|
|
|
#include "media/base/media_stream.h"
|
|
|
|
#include "media/base/muxer_options.h"
|
|
|
|
#include "media/base/video_stream_info.h"
|
2014-04-10 21:42:38 +00:00
|
|
|
#include "media/formats/mp4/box_definitions.h"
|
2014-04-18 18:49:49 +00:00
|
|
|
#include "media/formats/mp4/key_rotation_fragmenter.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
namespace {
|
2014-04-18 22:00:30 +00:00
|
|
|
|
|
|
|
// Generate 64bit IV by default.
|
|
|
|
const size_t kDefaultIvSize = 8u;
|
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;
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
uint64 Rescale(uint64 time_in_old_scale, uint32 old_scale, uint32 new_scale) {
|
|
|
|
return static_cast<double>(time_in_old_scale) / old_scale * new_scale;
|
|
|
|
}
|
|
|
|
|
2014-04-18 22:00:30 +00:00
|
|
|
void GenerateSinf(const EncryptionKey& encryption_key,
|
|
|
|
FourCC old_type,
|
|
|
|
ProtectionSchemeInfo* sinf) {
|
|
|
|
sinf->format.format = old_type;
|
|
|
|
sinf->type.type = FOURCC_CENC;
|
|
|
|
sinf->type.version = kCencSchemeVersion;
|
|
|
|
sinf->info.track_encryption.is_encrypted = true;
|
|
|
|
sinf->info.track_encryption.default_iv_size =
|
|
|
|
encryption_key.iv.empty() ? kDefaultIvSize : encryption_key.iv.size();
|
|
|
|
sinf->info.track_encryption.default_kid = encryption_key.key_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateEncryptedSampleEntry(const EncryptionKey& encryption_key,
|
|
|
|
double clear_lead_in_seconds,
|
|
|
|
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];
|
|
|
|
GenerateSinf(encryption_key, entry.format, &entry.sinf);
|
|
|
|
entry.format = FOURCC_ENCV;
|
|
|
|
} 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];
|
|
|
|
GenerateSinf(encryption_key, entry.format, &entry.sinf);
|
|
|
|
entry.format = FOURCC_ENCA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:49:49 +00:00
|
|
|
void GenerateEncryptedSampleEntryForKeyRotation(
|
|
|
|
double clear_lead_in_seconds,
|
|
|
|
SampleDescription* description) {
|
|
|
|
// Fill encrypted sample entry with default key.
|
|
|
|
EncryptionKey encryption_key;
|
|
|
|
encryption_key.key_id.assign(kCencKeyIdSize, 0);
|
|
|
|
GenerateEncryptedSampleEntry(
|
|
|
|
encryption_key, clear_lead_in_seconds, description);
|
|
|
|
}
|
|
|
|
|
2014-04-24 21:32:18 +00:00
|
|
|
uint8 GetNaluLengthSize(const StreamInfo& stream_info) {
|
|
|
|
if (stream_info.stream_type() != kStreamVideo)
|
|
|
|
return 0;
|
|
|
|
const VideoStreamInfo& video_stream_info =
|
|
|
|
static_cast<const VideoStreamInfo&>(stream_info);
|
|
|
|
return video_stream_info.nalu_length_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
EncryptionKeySource::TrackType GetTrackTypeForEncryption(
|
|
|
|
const StreamInfo& stream_info, uint32 max_sd_pixels) {
|
|
|
|
if (stream_info.stream_type() == kStreamAudio)
|
|
|
|
return EncryptionKeySource::TRACK_TYPE_AUDIO;
|
|
|
|
|
|
|
|
DCHECK_EQ(kStreamVideo, stream_info.stream_type());
|
|
|
|
const VideoStreamInfo& video_stream_info =
|
|
|
|
static_cast<const VideoStreamInfo&>(stream_info);
|
|
|
|
uint32 pixels = video_stream_info.width() * video_stream_info.height();
|
|
|
|
return (pixels > max_sd_pixels) ? EncryptionKeySource::TRACK_TYPE_HD
|
|
|
|
: EncryptionKeySource::TRACK_TYPE_SD;
|
|
|
|
}
|
|
|
|
|
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()),
|
|
|
|
segment_initialized_(false),
|
2014-05-22 18:51:55 +00:00
|
|
|
end_of_segment_(false),
|
|
|
|
muxer_listener_(NULL) {}
|
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,
|
2014-05-22 18:51:55 +00:00
|
|
|
event::MuxerListener* muxer_listener,
|
2014-04-16 01:09:32 +00:00
|
|
|
EncryptionKeySource* encryption_key_source,
|
2014-04-24 21:32:18 +00:00
|
|
|
uint32 max_sd_pixels,
|
2014-04-18 18:49:49 +00:00
|
|
|
double clear_lead_in_seconds,
|
|
|
|
double crypto_period_duration_in_seconds) {
|
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;
|
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());
|
|
|
|
for (uint32 i = 0; i < streams.size(); ++i) {
|
|
|
|
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) {
|
|
|
|
fragmenters_[i] = new Fragmenter(
|
|
|
|
&moof_->tracks[i], options_.normalize_presentation_timestamp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-04-24 21:32:18 +00:00
|
|
|
uint8 nalu_length_size = GetNaluLengthSize(*streams[i]->info());
|
|
|
|
EncryptionKeySource::TrackType track_type =
|
|
|
|
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;
|
|
|
|
|
|
|
|
const bool key_rotation_enabled = crypto_period_duration_in_seconds != 0;
|
|
|
|
if (key_rotation_enabled) {
|
|
|
|
GenerateEncryptedSampleEntryForKeyRotation(clear_lead_in_seconds,
|
|
|
|
&description);
|
|
|
|
|
|
|
|
fragmenters_[i] = new KeyRotationFragmenter(
|
|
|
|
moof_.get(),
|
|
|
|
&moof_->tracks[i],
|
|
|
|
options_.normalize_presentation_timestamp,
|
|
|
|
encryption_key_source,
|
2014-04-24 21:32:18 +00:00
|
|
|
track_type,
|
2014-04-18 18:49:49 +00:00
|
|
|
crypto_period_duration_in_seconds * streams[i]->info()->time_scale(),
|
|
|
|
clear_lead_in_seconds * streams[i]->info()->time_scale(),
|
|
|
|
nalu_length_size);
|
|
|
|
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;
|
|
|
|
|
|
|
|
GenerateEncryptedSampleEntry(
|
|
|
|
*encryption_key, clear_lead_in_seconds, &description);
|
|
|
|
|
2014-05-08 20:53:08 +00:00
|
|
|
// One and only one pssh box is needed.
|
2014-04-18 18:49:49 +00:00
|
|
|
if (moov_->pssh.empty()) {
|
|
|
|
moov_->pssh.resize(1);
|
|
|
|
moov_->pssh[0].raw_box = encryption_key->pssh;
|
|
|
|
}
|
|
|
|
|
2014-05-08 20:53:08 +00:00
|
|
|
fragmenters_[i] = new EncryptingFragmenter(
|
|
|
|
&moof_->tracks[i],
|
|
|
|
options_.normalize_presentation_timestamp,
|
|
|
|
encryption_key.Pass(),
|
|
|
|
clear_lead_in_seconds * streams[i]->info()->time_scale(),
|
|
|
|
nalu_length_size);
|
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();
|
|
|
|
|
|
|
|
// Use the reference stream's time scale as movie time scale.
|
|
|
|
moov_->header.timescale = sidx_->timescale;
|
2014-05-02 23:45:15 +00:00
|
|
|
Status status = InitializeFragments();
|
|
|
|
return status.ok() ? DoInitialize() : status;
|
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
|
|
|
end_of_segment_ = true;
|
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) {
|
|
|
|
Status status = FinalizeFragment(*it);
|
|
|
|
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-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());
|
2013-11-12 20:37:58 +00:00
|
|
|
uint32 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 (!segment_initialized_) {
|
|
|
|
InitializeSegment();
|
|
|
|
segment_initialized_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (segment_durations_[stream_id] >=
|
|
|
|
options_.segment_duration * stream->info()->time_scale()) {
|
|
|
|
if (sample->is_key_frame() || !options_.segment_sap_aligned) {
|
|
|
|
end_of_segment_ = true;
|
|
|
|
finalize_fragment = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status status;
|
|
|
|
if (finalize_fragment) {
|
|
|
|
status = FinalizeFragment(fragmenter);
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = fragmenter->AddSample(sample);
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
|
|
|
|
|
|
|
moov_->tracks[stream_id].media.header.duration += sample->duration();
|
|
|
|
segment_durations_[stream_id] += sample->duration();
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
uint32 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;
|
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
void Segmenter::InitializeSegment() {
|
2013-11-12 20:37:58 +00:00
|
|
|
sidx_->references.clear();
|
|
|
|
end_of_segment_ = false;
|
|
|
|
std::vector<uint64>::iterator it = segment_durations_.begin();
|
|
|
|
for (; it != segment_durations_.end(); ++it)
|
|
|
|
*it = 0;
|
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
Status Segmenter::FinalizeSegment() {
|
2013-11-12 20:37:58 +00:00
|
|
|
segment_initialized_ = false;
|
2014-04-18 22:00:30 +00:00
|
|
|
return DoFinalizeSegment();
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
uint32 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;
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:49:49 +00:00
|
|
|
Status Segmenter::InitializeFragments() {
|
2013-11-12 20:37:58 +00:00
|
|
|
++moof_->header.sequence_number;
|
2014-04-18 18:49:49 +00:00
|
|
|
Status status;
|
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) {
|
2014-04-18 18:49:49 +00:00
|
|
|
status = (*it)->InitializeFragment();
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
2014-04-18 18:49:49 +00:00
|
|
|
return Status::OK;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 20:21:07 +00:00
|
|
|
Status Segmenter::FinalizeFragment(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;
|
|
|
|
// Fill in data offsets. Data offset base is moof size + mdat box size.
|
|
|
|
// (mdat is still empty, mdat size is the same as mdat box size).
|
|
|
|
uint64 base = moof_->ComputeSize() + mdat.ComputeSize();
|
|
|
|
for (uint i = 0; i < moof_->tracks.size(); ++i) {
|
|
|
|
TrackFragment& traf = moof_->tracks[i];
|
2014-04-08 20:21:07 +00:00
|
|
|
Fragmenter* fragmenter = fragmenters_[i];
|
2013-11-12 20:37:58 +00:00
|
|
|
if (fragmenter->aux_data()->Size() > 0) {
|
|
|
|
traf.auxiliary_offset.offsets[0] += base;
|
|
|
|
base += fragmenter->aux_data()->Size();
|
|
|
|
}
|
|
|
|
traf.runs[0].data_offset += base;
|
|
|
|
base += fragmenter->data()->Size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate segment reference.
|
|
|
|
sidx_->references.resize(sidx_->references.size() + 1);
|
|
|
|
fragmenters_[GetReferenceStreamId()]->GenerateSegmentReference(
|
|
|
|
&sidx_->references[sidx_->references.size() - 1]);
|
|
|
|
sidx_->references[sidx_->references.size() - 1].referenced_size = base;
|
|
|
|
|
|
|
|
// Write the fragment to buffer.
|
|
|
|
moof_->Write(fragment_buffer_.get());
|
|
|
|
|
|
|
|
for (uint i = 0; i < moof_->tracks.size(); ++i) {
|
2014-04-08 20:21:07 +00:00
|
|
|
Fragmenter* fragmenter = fragmenters_[i];
|
2013-11-12 20:37:58 +00:00
|
|
|
mdat.data_size =
|
|
|
|
fragmenter->aux_data()->Size() + fragmenter->data()->Size();
|
|
|
|
mdat.Write(fragment_buffer_.get());
|
|
|
|
if (fragmenter->aux_data()->Size()) {
|
|
|
|
fragment_buffer_->AppendBuffer(*fragmenter->aux_data());
|
|
|
|
}
|
|
|
|
fragment_buffer_->AppendBuffer(*fragmenter->data());
|
|
|
|
}
|
|
|
|
|
2014-05-02 23:45:15 +00:00
|
|
|
Status status = InitializeFragments();
|
|
|
|
if (!status.ok())
|
|
|
|
return status;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
if (end_of_segment_)
|
|
|
|
return FinalizeSegment();
|
|
|
|
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|