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/mp4_muxer.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
#include "base/time/clock.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
#include "base/time/time.h"
|
|
|
|
#include "media/base/aes_encryptor.h"
|
|
|
|
#include "media/base/audio_stream_info.h"
|
|
|
|
#include "media/base/encryptor_source.h"
|
|
|
|
#include "media/base/media_sample.h"
|
|
|
|
#include "media/base/media_stream.h"
|
|
|
|
#include "media/base/video_stream_info.h"
|
2013-12-12 23:49:31 +00:00
|
|
|
#include "media/event/muxer_listener.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
#include "media/file/file.h"
|
2014-04-10 21:42:38 +00:00
|
|
|
#include "media/formats/mp4/box_definitions.h"
|
|
|
|
#include "media/formats/mp4/es_descriptor.h"
|
|
|
|
#include "media/formats/mp4/fourccs.h"
|
|
|
|
#include "media/formats/mp4/multi_segment_segmenter.h"
|
|
|
|
#include "media/formats/mp4/single_segment_segmenter.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
// The version of cenc implemented here. CENC 4.
|
|
|
|
const int kCencSchemeVersion = 0x00010000;
|
|
|
|
|
2013-12-12 23:49:31 +00:00
|
|
|
// Sets the range start and end value from offset and size.
|
|
|
|
// |start| and |end| are for byte-range-spec specified in RFC2616.
|
2014-02-28 02:39:52 +00:00
|
|
|
void SetStartAndEndFromOffsetAndSize(size_t offset,
|
|
|
|
size_t size,
|
|
|
|
uint32* start,
|
|
|
|
uint32* end) {
|
2013-12-12 23:49:31 +00:00
|
|
|
DCHECK(start && end);
|
|
|
|
*start = static_cast<uint32>(offset);
|
|
|
|
// Note that ranges are inclusive. So we need - 1.
|
|
|
|
*end = *start + static_cast<uint32>(size) - 1;
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2014-01-14 01:38:34 +00:00
|
|
|
MP4Muxer::MP4Muxer(const MuxerOptions& options) : Muxer(options) {}
|
2013-11-12 20:37:58 +00:00
|
|
|
MP4Muxer::~MP4Muxer() {}
|
|
|
|
|
|
|
|
Status MP4Muxer::Initialize() {
|
|
|
|
DCHECK(!streams().empty());
|
|
|
|
|
|
|
|
scoped_ptr<FileType> ftyp(new FileType);
|
|
|
|
scoped_ptr<Movie> moov(new Movie);
|
|
|
|
|
|
|
|
ftyp->major_brand = FOURCC_DASH;
|
|
|
|
ftyp->compatible_brands.push_back(FOURCC_ISO6);
|
|
|
|
ftyp->compatible_brands.push_back(FOURCC_MP41);
|
|
|
|
if (streams().size() == 1 &&
|
|
|
|
streams()[0]->info()->stream_type() == kStreamVideo)
|
|
|
|
ftyp->compatible_brands.push_back(FOURCC_AVC1);
|
|
|
|
|
|
|
|
moov->header.creation_time = IsoTimeNow();
|
|
|
|
moov->header.modification_time = IsoTimeNow();
|
|
|
|
moov->header.next_track_id = streams().size() + 1;
|
|
|
|
|
|
|
|
moov->tracks.resize(streams().size());
|
|
|
|
moov->extends.tracks.resize(streams().size());
|
|
|
|
|
|
|
|
// Initialize tracks.
|
|
|
|
for (uint32 i = 0; i < streams().size(); ++i) {
|
|
|
|
Track& trak = moov->tracks[i];
|
|
|
|
trak.header.track_id = i + 1;
|
|
|
|
|
|
|
|
TrackExtends& trex = moov->extends.tracks[i];
|
|
|
|
trex.track_id = trak.header.track_id;
|
|
|
|
trex.default_sample_description_index = 1;
|
|
|
|
|
|
|
|
switch (streams()[i]->info()->stream_type()) {
|
|
|
|
case kStreamVideo:
|
|
|
|
GenerateVideoTrak(
|
|
|
|
static_cast<VideoStreamInfo*>(streams()[i]->info().get()),
|
|
|
|
&trak,
|
|
|
|
i + 1);
|
|
|
|
break;
|
|
|
|
case kStreamAudio:
|
|
|
|
GenerateAudioTrak(
|
|
|
|
static_cast<AudioStreamInfo*>(streams()[i]->info().get()),
|
|
|
|
&trak,
|
|
|
|
i + 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NOTIMPLEMENTED() << "Not implemented for stream type: "
|
|
|
|
<< streams()[i]->info()->stream_type();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsEncryptionRequired()) {
|
|
|
|
moov->pssh.resize(1);
|
|
|
|
GeneratePssh(&moov->pssh[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options().single_segment) {
|
2014-04-08 20:21:07 +00:00
|
|
|
segmenter_.reset(
|
|
|
|
new SingleSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
|
2013-11-12 20:37:58 +00:00
|
|
|
} else {
|
|
|
|
segmenter_.reset(
|
2014-04-08 20:21:07 +00:00
|
|
|
new MultiSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
2013-12-12 23:49:31 +00:00
|
|
|
|
|
|
|
Status segmenter_initialized = segmenter_->Initialize(
|
2014-01-14 01:38:34 +00:00
|
|
|
encryptor_source(), clear_lead_in_seconds(), streams());
|
2013-12-12 23:49:31 +00:00
|
|
|
|
|
|
|
if (!segmenter_initialized.ok())
|
|
|
|
return segmenter_initialized;
|
|
|
|
|
|
|
|
FireOnMediaStartEvent();
|
|
|
|
return Status::OK;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status MP4Muxer::Finalize() {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(segmenter_);
|
2013-12-12 23:49:31 +00:00
|
|
|
Status segmenter_finalized = segmenter_->Finalize();
|
|
|
|
|
|
|
|
if (!segmenter_finalized.ok())
|
|
|
|
return segmenter_finalized;
|
|
|
|
|
|
|
|
FireOnMediaEndEvent();
|
|
|
|
return Status::OK;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 17:34:55 +00:00
|
|
|
Status MP4Muxer::DoAddSample(const MediaStream* stream,
|
|
|
|
scoped_refptr<MediaSample> sample) {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(segmenter_);
|
2013-11-12 20:37:58 +00:00
|
|
|
return segmenter_->AddSample(stream, sample);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::InitializeTrak(const StreamInfo* info, Track* trak) {
|
2013-12-12 23:49:31 +00:00
|
|
|
int64 now = IsoTimeNow();
|
2013-11-12 20:37:58 +00:00
|
|
|
trak->header.creation_time = now;
|
|
|
|
trak->header.modification_time = now;
|
|
|
|
trak->header.duration = 0;
|
|
|
|
trak->media.header.creation_time = now;
|
|
|
|
trak->media.header.modification_time = now;
|
|
|
|
trak->media.header.timescale = info->time_scale();
|
|
|
|
trak->media.header.duration = 0;
|
|
|
|
if (!info->language().empty()) {
|
|
|
|
DCHECK_EQ(info->language().size(),
|
|
|
|
arraysize(trak->media.header.language) - 1);
|
|
|
|
strcpy(trak->media.header.language, info->language().c_str());
|
|
|
|
trak->media.header.language[info->language().size()] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::GenerateVideoTrak(const VideoStreamInfo* video_info,
|
|
|
|
Track* trak,
|
|
|
|
uint32 track_id) {
|
|
|
|
InitializeTrak(video_info, trak);
|
|
|
|
|
|
|
|
trak->header.width = video_info->width();
|
|
|
|
trak->header.height = video_info->height();
|
|
|
|
trak->media.handler.type = kVideo;
|
|
|
|
|
|
|
|
VideoSampleEntry video;
|
|
|
|
video.format = FOURCC_AVC1;
|
|
|
|
video.width = video_info->width();
|
|
|
|
video.height = video_info->height();
|
|
|
|
video.avcc.data = video_info->extra_data();
|
|
|
|
|
|
|
|
SampleDescription& sample_description =
|
|
|
|
trak->media.information.sample_table.description;
|
|
|
|
sample_description.type = kVideo;
|
|
|
|
sample_description.video_entries.push_back(video);
|
|
|
|
|
|
|
|
if (IsEncryptionRequired()) {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(encryptor_source());
|
2013-11-12 20:37:58 +00:00
|
|
|
// Add a second entry for clear content if needed.
|
2014-01-14 01:38:34 +00:00
|
|
|
if (clear_lead_in_seconds() > 0)
|
2013-11-12 20:37:58 +00:00
|
|
|
sample_description.video_entries.push_back(video);
|
|
|
|
|
|
|
|
VideoSampleEntry& encrypted_video = sample_description.video_entries[0];
|
|
|
|
GenerateSinf(&encrypted_video.sinf, encrypted_video.format);
|
|
|
|
encrypted_video.format = FOURCC_ENCV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::GenerateAudioTrak(const AudioStreamInfo* audio_info,
|
|
|
|
Track* trak,
|
|
|
|
uint32 track_id) {
|
|
|
|
InitializeTrak(audio_info, trak);
|
|
|
|
|
|
|
|
trak->header.volume = 0x100;
|
|
|
|
trak->media.handler.type = kAudio;
|
|
|
|
|
|
|
|
AudioSampleEntry audio;
|
|
|
|
audio.format = FOURCC_MP4A;
|
|
|
|
audio.channelcount = audio_info->num_channels();
|
|
|
|
audio.samplesize = audio_info->sample_bits();
|
|
|
|
audio.samplerate = audio_info->sampling_frequency();
|
|
|
|
|
|
|
|
if (kCodecAAC) {
|
|
|
|
audio.esds.es_descriptor.set_object_type(kISO_14496_3); // MPEG4 AAC.
|
|
|
|
} else {
|
|
|
|
// Do we need to support other types?
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
audio.esds.es_descriptor.set_esid(track_id);
|
|
|
|
audio.esds.es_descriptor.set_decoder_specific_info(audio_info->extra_data());
|
|
|
|
|
|
|
|
SampleDescription& sample_description =
|
|
|
|
trak->media.information.sample_table.description;
|
|
|
|
sample_description.type = kAudio;
|
|
|
|
sample_description.audio_entries.push_back(audio);
|
|
|
|
|
|
|
|
if (IsEncryptionRequired()) {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(encryptor_source());
|
2013-11-12 20:37:58 +00:00
|
|
|
// Add a second entry for clear content if needed.
|
2014-01-14 01:38:34 +00:00
|
|
|
if (clear_lead_in_seconds() > 0)
|
2013-11-12 20:37:58 +00:00
|
|
|
sample_description.audio_entries.push_back(audio);
|
|
|
|
|
|
|
|
AudioSampleEntry& encrypted_audio = sample_description.audio_entries[0];
|
|
|
|
GenerateSinf(&encrypted_audio.sinf, encrypted_audio.format);
|
|
|
|
encrypted_audio.format = FOURCC_ENCA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::GeneratePssh(ProtectionSystemSpecificHeader* pssh) {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(encryptor_source());
|
2013-11-12 20:37:58 +00:00
|
|
|
pssh->system_id = encryptor_source()->key_system_id();
|
|
|
|
pssh->data = encryptor_source()->pssh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::GenerateSinf(ProtectionSchemeInfo* sinf, FourCC old_type) {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(encryptor_source());
|
2013-11-12 20:37:58 +00:00
|
|
|
sinf->format.format = old_type;
|
|
|
|
sinf->type.type = FOURCC_CENC;
|
|
|
|
sinf->type.version = kCencSchemeVersion;
|
|
|
|
sinf->info.track_encryption.is_encrypted = true;
|
2014-01-14 01:38:34 +00:00
|
|
|
sinf->info.track_encryption.default_iv_size = encryptor_source()->iv_size();
|
2013-11-12 20:37:58 +00:00
|
|
|
sinf->info.track_encryption.default_kid = encryptor_source()->key_id();
|
|
|
|
}
|
|
|
|
|
2013-12-12 23:49:31 +00:00
|
|
|
void MP4Muxer::GetStreamInfo(std::vector<StreamInfo*>* stream_infos) {
|
|
|
|
DCHECK(stream_infos);
|
|
|
|
const std::vector<MediaStream*>& media_stream_vec = streams();
|
|
|
|
stream_infos->reserve(media_stream_vec.size());
|
|
|
|
for (size_t i = 0; i < media_stream_vec.size(); ++i) {
|
|
|
|
stream_infos->push_back(media_stream_vec[i]->info().get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MP4Muxer::GetInitRangeStartAndEnd(uint32* start, uint32* end) {
|
|
|
|
DCHECK(start && end);
|
|
|
|
size_t range_offset = 0;
|
|
|
|
size_t range_size = 0;
|
2014-02-28 02:39:52 +00:00
|
|
|
const bool has_range = segmenter_->GetInitRange(&range_offset, &range_size);
|
2013-12-12 23:49:31 +00:00
|
|
|
|
|
|
|
if (!has_range)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MP4Muxer::GetIndexRangeStartAndEnd(uint32* start, uint32* end) {
|
|
|
|
DCHECK(start && end);
|
|
|
|
size_t range_offset = 0;
|
|
|
|
size_t range_size = 0;
|
2014-02-28 02:39:52 +00:00
|
|
|
const bool has_range = segmenter_->GetIndexRange(&range_offset, &range_size);
|
2013-12-12 23:49:31 +00:00
|
|
|
|
|
|
|
if (!has_range)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::FireOnMediaStartEvent() {
|
|
|
|
if (!muxer_listener())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<StreamInfo*> stream_info_vec;
|
|
|
|
GetStreamInfo(&stream_info_vec);
|
|
|
|
const uint32 timescale = segmenter_->GetReferenceTimeScale();
|
2014-01-28 08:32:09 +00:00
|
|
|
muxer_listener()->OnMediaStart(options(),
|
|
|
|
stream_info_vec,
|
|
|
|
timescale,
|
|
|
|
event::MuxerListener::kContainerMp4);
|
2013-12-12 23:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::FireOnMediaEndEvent() {
|
|
|
|
if (!muxer_listener())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<StreamInfo*> stream_info_vec;
|
|
|
|
GetStreamInfo(&stream_info_vec);
|
|
|
|
|
|
|
|
uint32 init_range_start = 0;
|
|
|
|
uint32 init_range_end = 0;
|
|
|
|
const bool has_init_range =
|
|
|
|
GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
|
|
|
|
|
|
|
|
uint32 index_range_start = 0;
|
|
|
|
uint32 index_range_end = 0;
|
|
|
|
const bool has_index_range =
|
|
|
|
GetIndexRangeStartAndEnd(&index_range_start, &index_range_end);
|
|
|
|
|
|
|
|
const float duration_seconds = static_cast<float>(segmenter_->GetDuration());
|
|
|
|
|
|
|
|
const int64 file_size = File::GetFileSize(options().output_file_name.c_str());
|
|
|
|
if (file_size <= 0) {
|
|
|
|
LOG(ERROR) << "Invalid file size: " << file_size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
muxer_listener()->OnMediaEnd(stream_info_vec,
|
|
|
|
has_init_range,
|
|
|
|
init_range_start,
|
|
|
|
init_range_end,
|
|
|
|
has_index_range,
|
|
|
|
index_range_start,
|
|
|
|
index_range_end,
|
|
|
|
duration_seconds,
|
2014-02-13 00:35:05 +00:00
|
|
|
file_size,
|
|
|
|
IsEncryptionRequired());
|
2013-12-12 23:49:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 02:39:52 +00:00
|
|
|
uint64 MP4Muxer::IsoTimeNow() {
|
|
|
|
// Time in seconds from Jan. 1, 1904 to epoch time, i.e. Jan. 1, 1970.
|
|
|
|
const uint64 kIsomTimeOffset = 2082844800l;
|
|
|
|
return kIsomTimeOffset +
|
|
|
|
(clock() ? clock()->Now() : base::Time::Now()).ToDoubleT();
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|