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/mp4_muxer.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/time/clock.h"
|
|
|
|
#include "packager/base/time/time.h"
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/aes_encryptor.h"
|
|
|
|
#include "packager/media/base/audio_stream_info.h"
|
2016-04-06 23:21:45 +00:00
|
|
|
#include "packager/media/base/fourccs.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/key_source.h"
|
|
|
|
#include "packager/media/base/media_sample.h"
|
2015-11-23 23:12:04 +00:00
|
|
|
#include "packager/media/base/text_stream_info.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/video_stream_info.h"
|
2016-05-25 18:03:17 +00:00
|
|
|
#include "packager/media/codecs/es_descriptor.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/event/muxer_listener.h"
|
|
|
|
#include "packager/media/formats/mp4/box_definitions.h"
|
|
|
|
#include "packager/media/formats/mp4/multi_segment_segmenter.h"
|
|
|
|
#include "packager/media/formats/mp4/single_segment_segmenter.h"
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-26 20:50:22 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
namespace {
|
2015-10-26 20:50:22 +00:00
|
|
|
|
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,
|
2017-05-01 23:17:09 +00:00
|
|
|
Range* range) {
|
|
|
|
DCHECK(range);
|
|
|
|
range->start = static_cast<uint32_t>(offset);
|
2013-12-12 23:49:31 +00:00
|
|
|
// Note that ranges are inclusive. So we need - 1.
|
2017-05-01 23:17:09 +00:00
|
|
|
range->end = range->start + static_cast<uint32_t>(size) - 1;
|
2013-12-12 23:49:31 +00:00
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2017-03-23 18:34:20 +00:00
|
|
|
FourCC CodecToFourCC(Codec codec, H26xStreamFormat h26x_stream_format) {
|
2015-10-26 20:50:22 +00:00
|
|
|
switch (codec) {
|
|
|
|
case kCodecH264:
|
2017-03-23 18:34:20 +00:00
|
|
|
return h26x_stream_format ==
|
|
|
|
H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
|
|
|
|
? FOURCC_avc3
|
|
|
|
: FOURCC_avc1;
|
|
|
|
case kCodecH265:
|
|
|
|
return h26x_stream_format ==
|
|
|
|
H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
|
|
|
|
? FOURCC_hev1
|
|
|
|
: FOURCC_hvc1;
|
2015-10-26 20:50:22 +00:00
|
|
|
case kCodecVP8:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_vp08;
|
2015-10-26 20:50:22 +00:00
|
|
|
case kCodecVP9:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_vp09;
|
2015-10-26 20:50:22 +00:00
|
|
|
case kCodecVP10:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_vp10;
|
2016-01-04 18:57:05 +00:00
|
|
|
case kCodecAAC:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_mp4a;
|
2016-01-08 23:56:33 +00:00
|
|
|
case kCodecAC3:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_ac_3;
|
2016-01-04 18:57:05 +00:00
|
|
|
case kCodecDTSC:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_dtsc;
|
2016-01-04 18:57:05 +00:00
|
|
|
case kCodecDTSH:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_dtsh;
|
2016-01-04 18:57:05 +00:00
|
|
|
case kCodecDTSL:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_dtsl;
|
2016-01-04 18:57:05 +00:00
|
|
|
case kCodecDTSE:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_dtse;
|
2016-01-04 18:57:05 +00:00
|
|
|
case kCodecDTSM:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_dtsm;
|
2016-01-15 21:40:44 +00:00
|
|
|
case kCodecEAC3:
|
2016-04-06 23:21:45 +00:00
|
|
|
return FOURCC_ec_3;
|
2016-05-09 18:55:14 +00:00
|
|
|
case kCodecOpus:
|
|
|
|
return FOURCC_Opus;
|
2016-01-04 18:57:05 +00:00
|
|
|
default:
|
|
|
|
return FOURCC_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
void GenerateSinf(FourCC old_type,
|
|
|
|
const EncryptionConfig& encryption_config,
|
|
|
|
ProtectionSchemeInfo* sinf) {
|
|
|
|
sinf->format.format = old_type;
|
|
|
|
|
|
|
|
DCHECK_NE(encryption_config.protection_scheme, FOURCC_NULL);
|
|
|
|
sinf->type.type = encryption_config.protection_scheme;
|
|
|
|
|
|
|
|
// The version of cenc implemented here. CENC 4.
|
|
|
|
const int kCencSchemeVersion = 0x00010000;
|
|
|
|
sinf->type.version = kCencSchemeVersion;
|
|
|
|
|
|
|
|
auto& track_encryption = sinf->info.track_encryption;
|
|
|
|
track_encryption.default_is_protected = 1;
|
|
|
|
track_encryption.default_crypt_byte_block =
|
|
|
|
encryption_config.crypt_byte_block;
|
|
|
|
track_encryption.default_skip_byte_block = encryption_config.skip_byte_block;
|
|
|
|
track_encryption.default_per_sample_iv_size =
|
|
|
|
encryption_config.per_sample_iv_size;
|
|
|
|
track_encryption.default_constant_iv = encryption_config.constant_iv;
|
|
|
|
track_encryption.default_kid = encryption_config.key_id;
|
|
|
|
}
|
|
|
|
|
2015-10-26 20:50:22 +00:00
|
|
|
} // namespace
|
2013-11-12 20:37:58 +00:00
|
|
|
|
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() {}
|
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
Status MP4Muxer::InitializeMuxer() {
|
2013-11-12 20:37:58 +00:00
|
|
|
DCHECK(!streams().empty());
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<FileType> ftyp(new FileType);
|
|
|
|
std::unique_ptr<Movie> moov(new Movie);
|
2013-11-12 20:37:58 +00:00
|
|
|
|
2017-03-15 19:42:00 +00:00
|
|
|
ftyp->major_brand = FOURCC_isom;
|
|
|
|
ftyp->compatible_brands.push_back(FOURCC_iso8);
|
2016-04-06 23:21:45 +00:00
|
|
|
ftyp->compatible_brands.push_back(FOURCC_mp41);
|
2017-03-15 19:42:00 +00:00
|
|
|
ftyp->compatible_brands.push_back(FOURCC_dash);
|
|
|
|
|
|
|
|
if (streams().size() == 1) {
|
|
|
|
FourCC codec_fourcc = FOURCC_NULL;
|
|
|
|
if (streams()[0]->stream_type() == kStreamVideo) {
|
|
|
|
codec_fourcc =
|
|
|
|
CodecToFourCC(streams()[0]->codec(),
|
|
|
|
static_cast<VideoStreamInfo*>(streams()[0].get())
|
|
|
|
->h26x_stream_format());
|
|
|
|
if (codec_fourcc != FOURCC_NULL)
|
|
|
|
ftyp->compatible_brands.push_back(codec_fourcc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CMAF allows only one track/stream per file.
|
|
|
|
// CMAF requires single initialization switching for AVC3/HEV1, which is not
|
|
|
|
// supported yet.
|
|
|
|
if (codec_fourcc != FOURCC_avc3 && codec_fourcc != FOURCC_hev1)
|
|
|
|
ftyp->compatible_brands.push_back(FOURCC_cmfc);
|
2015-10-26 20:50:22 +00:00
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
moov->header.creation_time = IsoTimeNow();
|
|
|
|
moov->header.modification_time = IsoTimeNow();
|
2016-11-09 02:11:13 +00:00
|
|
|
moov->header.next_track_id = static_cast<uint32_t>(streams().size()) + 1;
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
moov->tracks.resize(streams().size());
|
|
|
|
moov->extends.tracks.resize(streams().size());
|
|
|
|
|
|
|
|
// Initialize tracks.
|
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
|
|
|
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;
|
|
|
|
|
2017-02-21 18:36:50 +00:00
|
|
|
switch (streams()[i]->stream_type()) {
|
2013-11-12 20:37:58 +00:00
|
|
|
case kStreamVideo:
|
2017-02-21 18:36:50 +00:00
|
|
|
GenerateVideoTrak(static_cast<VideoStreamInfo*>(streams()[i].get()),
|
|
|
|
&trak, i + 1);
|
2013-11-12 20:37:58 +00:00
|
|
|
break;
|
|
|
|
case kStreamAudio:
|
2017-02-21 18:36:50 +00:00
|
|
|
GenerateAudioTrak(static_cast<AudioStreamInfo*>(streams()[i].get()),
|
|
|
|
&trak, i + 1);
|
2013-11-12 20:37:58 +00:00
|
|
|
break;
|
2015-11-23 23:12:04 +00:00
|
|
|
case kStreamText:
|
|
|
|
GenerateTextTrak(static_cast<TextStreamInfo*>(streams()[i].get()),
|
|
|
|
&trak, i + 1);
|
|
|
|
break;
|
2013-11-12 20:37:58 +00:00
|
|
|
default:
|
|
|
|
NOTIMPLEMENTED() << "Not implemented for stream type: "
|
2017-02-21 18:36:50 +00:00
|
|
|
<< streams()[i]->stream_type();
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
2017-03-11 02:49:55 +00:00
|
|
|
|
2017-08-18 18:57:34 +00:00
|
|
|
if (streams()[i]->is_encrypted() &&
|
|
|
|
options().mp4_params.include_pssh_in_stream) {
|
2017-03-11 02:49:55 +00:00
|
|
|
const auto& key_system_info =
|
|
|
|
streams()[i]->encryption_config().key_system_info;
|
|
|
|
moov->pssh.resize(key_system_info.size());
|
|
|
|
for (size_t j = 0; j < key_system_info.size(); j++)
|
|
|
|
moov->pssh[j].raw_box = key_system_info[j].CreateBox();
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 02:40:37 +00:00
|
|
|
if (options().segment_template.empty()) {
|
2016-08-17 17:41:40 +00:00
|
|
|
segmenter_.reset(new SingleSegmentSegmenter(options(), std::move(ftyp),
|
|
|
|
std::move(moov)));
|
2013-11-12 20:37:58 +00:00
|
|
|
} else {
|
|
|
|
segmenter_.reset(
|
2016-08-17 17:41:40 +00:00
|
|
|
new MultiSegmentSegmenter(options(), std::move(ftyp), std::move(moov)));
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
2013-12-12 23:49:31 +00:00
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
const Status segmenter_initialized =
|
|
|
|
segmenter_->Initialize(streams(), muxer_listener(), progress_listener());
|
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();
|
2015-05-12 00:52:53 +00:00
|
|
|
LOG(INFO) << "MP4 file '" << options().output_file_name << "' finalized.";
|
2013-12-12 23:49:31 +00:00
|
|
|
return Status::OK;
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
Status MP4Muxer::AddSample(size_t stream_id,
|
|
|
|
std::shared_ptr<MediaSample> sample) {
|
2014-01-23 00:13:41 +00:00
|
|
|
DCHECK(segmenter_);
|
2017-02-24 01:17:47 +00:00
|
|
|
return segmenter_->AddSample(stream_id, sample);
|
|
|
|
}
|
|
|
|
|
2017-03-03 00:10:30 +00:00
|
|
|
Status MP4Muxer::FinalizeSegment(size_t stream_id,
|
2017-02-24 01:17:47 +00:00
|
|
|
std::shared_ptr<SegmentInfo> segment_info) {
|
|
|
|
DCHECK(segmenter_);
|
|
|
|
VLOG(3) << "Finalize " << (segment_info->is_subsegment ? "sub" : "")
|
|
|
|
<< "segment " << segment_info->start_timestamp << " duration "
|
|
|
|
<< segment_info->duration;
|
2017-03-11 02:49:55 +00:00
|
|
|
return segmenter_->FinalizeSegment(stream_id, std::move(segment_info));
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::InitializeTrak(const StreamInfo* info, Track* trak) {
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t 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()) {
|
2016-03-08 19:15:12 +00:00
|
|
|
// Strip off the subtag, if any.
|
|
|
|
std::string main_language = info->language();
|
|
|
|
size_t dash = main_language.find('-');
|
|
|
|
if (dash != std::string::npos) {
|
|
|
|
main_language.erase(dash);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ISO-639-2/T main language code should be 3 characters.
|
|
|
|
if (main_language.size() != 3) {
|
|
|
|
LOG(WARNING) << "'" << main_language << "' is not a valid ISO-639-2 "
|
2015-02-02 19:27:32 +00:00
|
|
|
<< "language code, ignoring.";
|
|
|
|
} else {
|
2016-03-08 19:15:12 +00:00
|
|
|
trak->media.header.language.code = main_language;
|
2015-02-02 19:27:32 +00:00
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::GenerateVideoTrak(const VideoStreamInfo* video_info,
|
|
|
|
Track* trak,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t track_id) {
|
2013-11-12 20:37:58 +00:00
|
|
|
InitializeTrak(video_info, trak);
|
|
|
|
|
2015-08-05 23:39:03 +00:00
|
|
|
// width and height specify the track's visual presentation size as
|
|
|
|
// fixed-point 16.16 values.
|
2015-08-20 19:06:02 +00:00
|
|
|
uint32_t pixel_width = video_info->pixel_width();
|
|
|
|
uint32_t pixel_height = video_info->pixel_height();
|
|
|
|
if (pixel_width == 0 || pixel_height == 0) {
|
|
|
|
LOG(WARNING) << "pixel width/height are not set. Assuming 1:1.";
|
|
|
|
pixel_width = 1;
|
|
|
|
pixel_height = 1;
|
|
|
|
}
|
2015-08-05 23:39:03 +00:00
|
|
|
const double sample_aspect_ratio =
|
2015-08-20 19:06:02 +00:00
|
|
|
static_cast<double>(pixel_width) / pixel_height;
|
2015-08-05 23:39:03 +00:00
|
|
|
trak->header.width = video_info->width() * sample_aspect_ratio * 0x10000;
|
|
|
|
trak->header.height = video_info->height() * 0x10000;
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
VideoSampleEntry video;
|
2017-03-23 18:34:20 +00:00
|
|
|
video.format =
|
|
|
|
CodecToFourCC(video_info->codec(), video_info->h26x_stream_format());
|
2013-11-12 20:37:58 +00:00
|
|
|
video.width = video_info->width();
|
|
|
|
video.height = video_info->height();
|
2016-06-27 19:30:32 +00:00
|
|
|
video.codec_configuration.data = video_info->codec_config();
|
2015-08-20 19:06:02 +00:00
|
|
|
if (pixel_width != 1 || pixel_height != 1) {
|
|
|
|
video.pixel_aspect.h_spacing = pixel_width;
|
|
|
|
video.pixel_aspect.v_spacing = pixel_height;
|
2015-08-05 23:39:03 +00:00
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
|
|
|
|
SampleDescription& sample_description =
|
|
|
|
trak->media.information.sample_table.description;
|
|
|
|
sample_description.type = kVideo;
|
|
|
|
sample_description.video_entries.push_back(video);
|
2017-03-11 02:49:55 +00:00
|
|
|
|
|
|
|
if (video_info->is_encrypted()) {
|
2017-05-09 22:49:01 +00:00
|
|
|
if (video_info->has_clear_lead()) {
|
|
|
|
// Add a second entry for clear content.
|
|
|
|
sample_description.video_entries.push_back(video);
|
|
|
|
}
|
2017-03-11 02:49:55 +00:00
|
|
|
// Convert the first entry to an encrypted entry.
|
|
|
|
VideoSampleEntry& entry = sample_description.video_entries[0];
|
|
|
|
GenerateSinf(entry.format, video_info->encryption_config(), &entry.sinf);
|
|
|
|
entry.format = FOURCC_encv;
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::GenerateAudioTrak(const AudioStreamInfo* audio_info,
|
|
|
|
Track* trak,
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t track_id) {
|
2013-11-12 20:37:58 +00:00
|
|
|
InitializeTrak(audio_info, trak);
|
|
|
|
|
|
|
|
trak->header.volume = 0x100;
|
|
|
|
|
|
|
|
AudioSampleEntry audio;
|
2017-03-23 18:34:20 +00:00
|
|
|
audio.format =
|
|
|
|
CodecToFourCC(audio_info->codec(), H26xStreamFormat::kUnSpecified);
|
2015-12-09 00:57:35 +00:00
|
|
|
switch(audio_info->codec()){
|
|
|
|
case kCodecAAC:
|
|
|
|
audio.esds.es_descriptor.set_object_type(kISO_14496_3); // MPEG4 AAC.
|
|
|
|
audio.esds.es_descriptor.set_esid(track_id);
|
|
|
|
audio.esds.es_descriptor.set_decoder_specific_info(
|
2016-06-27 19:30:32 +00:00
|
|
|
audio_info->codec_config());
|
2016-01-04 18:57:05 +00:00
|
|
|
audio.esds.es_descriptor.set_max_bitrate(audio_info->max_bitrate());
|
|
|
|
audio.esds.es_descriptor.set_avg_bitrate(audio_info->avg_bitrate());
|
2015-12-09 00:57:35 +00:00
|
|
|
break;
|
|
|
|
case kCodecDTSC:
|
|
|
|
case kCodecDTSH:
|
|
|
|
case kCodecDTSL:
|
|
|
|
case kCodecDTSE:
|
|
|
|
case kCodecDTSM:
|
2016-06-27 19:30:32 +00:00
|
|
|
audio.ddts.extra_data = audio_info->codec_config();
|
2016-01-04 18:57:05 +00:00
|
|
|
audio.ddts.max_bitrate = audio_info->max_bitrate();
|
|
|
|
audio.ddts.avg_bitrate = audio_info->avg_bitrate();
|
|
|
|
audio.ddts.sampling_frequency = audio_info->sampling_frequency();
|
|
|
|
audio.ddts.pcm_sample_depth = audio_info->sample_bits();
|
2015-12-09 00:57:35 +00:00
|
|
|
break;
|
2016-01-08 23:56:33 +00:00
|
|
|
case kCodecAC3:
|
2016-06-27 19:30:32 +00:00
|
|
|
audio.dac3.data = audio_info->codec_config();
|
2016-01-15 21:40:44 +00:00
|
|
|
break;
|
|
|
|
case kCodecEAC3:
|
2016-06-27 19:30:32 +00:00
|
|
|
audio.dec3.data = audio_info->codec_config();
|
2016-05-09 18:55:14 +00:00
|
|
|
break;
|
|
|
|
case kCodecOpus:
|
2016-06-27 19:30:32 +00:00
|
|
|
audio.dops.opus_identification_header = audio_info->codec_config();
|
2016-01-08 23:56:33 +00:00
|
|
|
break;
|
2015-12-09 00:57:35 +00:00
|
|
|
default:
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
audio.channelcount = audio_info->num_channels();
|
|
|
|
audio.samplesize = audio_info->sample_bits();
|
|
|
|
audio.samplerate = audio_info->sampling_frequency();
|
2016-05-06 23:56:50 +00:00
|
|
|
SampleTable& sample_table = trak->media.information.sample_table;
|
|
|
|
SampleDescription& sample_description = sample_table.description;
|
2013-11-12 20:37:58 +00:00
|
|
|
sample_description.type = kAudio;
|
|
|
|
sample_description.audio_entries.push_back(audio);
|
2016-05-06 23:56:50 +00:00
|
|
|
|
2017-03-11 02:49:55 +00:00
|
|
|
if (audio_info->is_encrypted()) {
|
2017-05-09 22:49:01 +00:00
|
|
|
if (audio_info->has_clear_lead()) {
|
|
|
|
// Add a second entry for clear content.
|
|
|
|
sample_description.audio_entries.push_back(audio);
|
|
|
|
}
|
2017-03-11 02:49:55 +00:00
|
|
|
// Convert the first entry to an encrypted entry.
|
|
|
|
AudioSampleEntry& entry = sample_description.audio_entries[0];
|
|
|
|
GenerateSinf(entry.format, audio_info->encryption_config(), &entry.sinf);
|
|
|
|
entry.format = FOURCC_enca;
|
|
|
|
}
|
|
|
|
|
2016-05-06 23:56:50 +00:00
|
|
|
// Opus requires at least one sample group description box and at least one
|
|
|
|
// sample to group box with grouping type 'roll' within sample table box.
|
|
|
|
if (audio_info->codec() == kCodecOpus) {
|
|
|
|
sample_table.sample_group_descriptions.resize(1);
|
|
|
|
SampleGroupDescription& sample_group_description =
|
|
|
|
sample_table.sample_group_descriptions.back();
|
|
|
|
sample_group_description.grouping_type = FOURCC_roll;
|
|
|
|
sample_group_description.audio_roll_recovery_entries.resize(1);
|
|
|
|
// The roll distance is expressed in sample units and always takes negative
|
|
|
|
// values.
|
|
|
|
const uint64_t kNanosecondsPerSecond = 1000000000ull;
|
|
|
|
sample_group_description.audio_roll_recovery_entries[0].roll_distance =
|
2016-08-14 22:28:21 +00:00
|
|
|
(0 - (audio_info->seek_preroll_ns() * audio.samplerate +
|
|
|
|
kNanosecondsPerSecond / 2)) /
|
2016-05-06 23:56:50 +00:00
|
|
|
kNanosecondsPerSecond;
|
|
|
|
|
|
|
|
sample_table.sample_to_groups.resize(1);
|
|
|
|
SampleToGroup& sample_to_group = sample_table.sample_to_groups.back();
|
|
|
|
sample_to_group.grouping_type = FOURCC_roll;
|
|
|
|
|
|
|
|
sample_to_group.entries.resize(1);
|
|
|
|
SampleToGroupEntry& sample_to_group_entry = sample_to_group.entries.back();
|
|
|
|
// All samples are in track fragments.
|
|
|
|
sample_to_group_entry.sample_count = 0;
|
|
|
|
sample_to_group_entry.group_description_index =
|
|
|
|
SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
|
|
|
|
} else if (audio_info->seek_preroll_ns() != 0) {
|
|
|
|
LOG(WARNING) << "Unexpected seek preroll for codec " << audio_info->codec();
|
|
|
|
return;
|
|
|
|
}
|
2013-11-12 20:37:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 23:12:04 +00:00
|
|
|
void MP4Muxer::GenerateTextTrak(const TextStreamInfo* text_info,
|
|
|
|
Track* trak,
|
|
|
|
uint32_t track_id) {
|
|
|
|
InitializeTrak(text_info, trak);
|
|
|
|
|
|
|
|
if (text_info->codec_string() == "wvtt") {
|
|
|
|
// Handle WebVTT.
|
|
|
|
TextSampleEntry webvtt;
|
|
|
|
webvtt.format = FOURCC_wvtt;
|
|
|
|
webvtt.config.config.assign(text_info->codec_config().begin(),
|
|
|
|
text_info->codec_config().end());
|
|
|
|
// TODO(rkuroiwa): This should be the source file URI(s). Putting bogus
|
|
|
|
// string for now so that the box will be there for samples with overlapping
|
|
|
|
// cues.
|
|
|
|
webvtt.label.source_label = "source_label";
|
|
|
|
SampleDescription& sample_description =
|
|
|
|
trak->media.information.sample_table.description;
|
|
|
|
sample_description.type = kText;
|
|
|
|
sample_description.text_entries.push_back(webvtt);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
NOTIMPLEMENTED() << text_info->codec_string()
|
|
|
|
<< " handling not implemented yet.";
|
|
|
|
}
|
|
|
|
|
2017-05-01 23:17:09 +00:00
|
|
|
base::Optional<Range> MP4Muxer::GetInitRangeStartAndEnd() {
|
2013-12-12 23:49:31 +00:00
|
|
|
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)
|
2017-05-01 23:17:09 +00:00
|
|
|
return base::nullopt;
|
2013-12-12 23:49:31 +00:00
|
|
|
|
2017-05-01 23:17:09 +00:00
|
|
|
Range range;
|
|
|
|
SetStartAndEndFromOffsetAndSize(range_offset, range_size, &range);
|
|
|
|
return range;
|
2013-12-12 23:49:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 23:17:09 +00:00
|
|
|
base::Optional<Range> MP4Muxer::GetIndexRangeStartAndEnd() {
|
2013-12-12 23:49:31 +00:00
|
|
|
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)
|
2017-05-01 23:17:09 +00:00
|
|
|
return base::nullopt;
|
2013-12-12 23:49:31 +00:00
|
|
|
|
2017-05-01 23:17:09 +00:00
|
|
|
Range range;
|
|
|
|
SetStartAndEndFromOffsetAndSize(range_offset, range_size, &range);
|
|
|
|
return range;
|
2013-12-12 23:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::FireOnMediaStartEvent() {
|
|
|
|
if (!muxer_listener())
|
|
|
|
return;
|
|
|
|
|
2015-06-09 23:58:32 +00:00
|
|
|
if (streams().size() > 1) {
|
|
|
|
LOG(ERROR) << "MuxerListener cannot take more than 1 stream.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DCHECK(!streams().empty()) << "Media started without a stream.";
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint32_t timescale = segmenter_->GetReferenceTimeScale();
|
2017-02-21 18:36:50 +00:00
|
|
|
muxer_listener()->OnMediaStart(options(), *streams().front(), timescale,
|
2015-07-08 00:52:28 +00:00
|
|
|
MuxerListener::kContainerMp4);
|
2013-12-12 23:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MP4Muxer::FireOnMediaEndEvent() {
|
|
|
|
if (!muxer_listener())
|
|
|
|
return;
|
|
|
|
|
2017-05-01 23:17:09 +00:00
|
|
|
MuxerListener::MediaRanges media_range;
|
|
|
|
media_range.init_range = GetInitRangeStartAndEnd();
|
|
|
|
media_range.index_range = GetIndexRangeStartAndEnd();
|
|
|
|
media_range.subsegment_ranges = segmenter_->GetSegmentRanges();
|
2013-12-12 23:49:31 +00:00
|
|
|
|
|
|
|
const float duration_seconds = static_cast<float>(segmenter_->GetDuration());
|
2017-07-18 19:23:42 +00:00
|
|
|
muxer_listener()->OnMediaEnd(media_range, duration_seconds);
|
2013-12-12 23:49:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint64_t MP4Muxer::IsoTimeNow() {
|
2014-02-28 02:39:52 +00:00
|
|
|
// Time in seconds from Jan. 1, 1904 to epoch time, i.e. Jan. 1, 1970.
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint64_t kIsomTimeOffset = 2082844800l;
|
2014-02-28 02:39:52 +00:00
|
|
|
return kIsomTimeOffset +
|
|
|
|
(clock() ? clock()->Now() : base::Time::Now()).ToDoubleT();
|
|
|
|
}
|
|
|
|
|
2013-11-12 20:37:58 +00:00
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|