7 #include "packager/media/formats/mp4/mp4_muxer.h"
9 #include "packager/base/time/clock.h"
10 #include "packager/base/time/time.h"
11 #include "packager/media/base/aes_encryptor.h"
12 #include "packager/media/base/audio_stream_info.h"
13 #include "packager/media/base/fourccs.h"
14 #include "packager/media/base/key_source.h"
15 #include "packager/media/base/media_sample.h"
16 #include "packager/media/base/media_stream.h"
17 #include "packager/media/base/video_stream_info.h"
18 #include "packager/media/codecs/es_descriptor.h"
19 #include "packager/media/event/muxer_listener.h"
20 #include "packager/media/file/file.h"
21 #include "packager/media/formats/mp4/box_definitions.h"
22 #include "packager/media/formats/mp4/multi_segment_segmenter.h"
23 #include "packager/media/formats/mp4/single_segment_segmenter.h"
33 void SetStartAndEndFromOffsetAndSize(
size_t offset,
38 *start =
static_cast<uint32_t
>(offset);
40 *end = *start +
static_cast<uint32_t
>(size) - 1;
43 FourCC CodecToFourCC(Codec codec) {
83 MP4Muxer::~MP4Muxer() {}
85 Status MP4Muxer::Initialize() {
86 DCHECK(!streams().empty());
88 scoped_ptr<FileType> ftyp(
new FileType);
89 scoped_ptr<Movie> moov(
new Movie);
91 ftyp->major_brand = FOURCC_dash;
92 ftyp->compatible_brands.push_back(FOURCC_iso6);
93 ftyp->compatible_brands.push_back(FOURCC_mp41);
94 if (streams().size() == 1 &&
95 streams()[0]->info()->stream_type() == kStreamVideo) {
96 const FourCC codec_fourcc = CodecToFourCC(
97 static_cast<VideoStreamInfo*>(streams()[0]->info().
get())->codec());
98 if (codec_fourcc != FOURCC_NULL)
99 ftyp->compatible_brands.push_back(codec_fourcc);
102 moov->header.creation_time = IsoTimeNow();
103 moov->header.modification_time = IsoTimeNow();
104 moov->header.next_track_id = streams().size() + 1;
106 moov->tracks.resize(streams().size());
107 moov->extends.tracks.resize(streams().size());
110 for (uint32_t i = 0; i < streams().size(); ++i) {
111 Track& trak = moov->tracks[i];
112 trak.header.track_id = i + 1;
114 TrackExtends& trex = moov->extends.tracks[i];
115 trex.track_id = trak.header.track_id;
116 trex.default_sample_description_index = 1;
118 switch (streams()[i]->info()->stream_type()) {
121 static_cast<VideoStreamInfo*>(streams()[i]->info().
get()),
127 static_cast<AudioStreamInfo*>(streams()[i]->info().
get()),
132 NOTIMPLEMENTED() <<
"Not implemented for stream type: "
133 << streams()[i]->info()->stream_type();
137 if (options().single_segment) {
139 new SingleSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
142 new MultiSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
145 const Status segmenter_initialized = segmenter_->Initialize(
146 streams(), muxer_listener(), progress_listener(), encryption_key_source(),
147 max_sd_pixels(), clear_lead_in_seconds(),
148 crypto_period_duration_in_seconds(), protection_scheme());
150 if (!segmenter_initialized.ok())
151 return segmenter_initialized;
153 FireOnMediaStartEvent();
157 Status MP4Muxer::Finalize() {
159 Status segmenter_finalized = segmenter_->Finalize();
161 if (!segmenter_finalized.ok())
162 return segmenter_finalized;
164 FireOnMediaEndEvent();
169 Status MP4Muxer::DoAddSample(
const MediaStream* stream,
170 scoped_refptr<MediaSample> sample) {
172 return segmenter_->AddSample(stream, sample);
175 void MP4Muxer::InitializeTrak(
const StreamInfo* info, Track* trak) {
176 int64_t now = IsoTimeNow();
177 trak->header.creation_time = now;
178 trak->header.modification_time = now;
179 trak->header.duration = 0;
180 trak->media.header.creation_time = now;
181 trak->media.header.modification_time = now;
182 trak->media.header.timescale = info->time_scale();
183 trak->media.header.duration = 0;
184 if (!info->language().empty()) {
186 std::string main_language = info->language();
187 size_t dash = main_language.find(
'-');
188 if (dash != std::string::npos) {
189 main_language.erase(dash);
193 if (main_language.size() != 3) {
194 LOG(WARNING) <<
"'" << main_language <<
"' is not a valid ISO-639-2 "
195 <<
"language code, ignoring.";
197 trak->media.header.language.code = main_language;
202 void MP4Muxer::GenerateVideoTrak(
const VideoStreamInfo* video_info,
205 InitializeTrak(video_info, trak);
209 uint32_t pixel_width = video_info->pixel_width();
210 uint32_t pixel_height = video_info->pixel_height();
211 if (pixel_width == 0 || pixel_height == 0) {
212 LOG(WARNING) <<
"pixel width/height are not set. Assuming 1:1.";
216 const double sample_aspect_ratio =
217 static_cast<double>(pixel_width) / pixel_height;
218 trak->header.width = video_info->width() * sample_aspect_ratio * 0x10000;
219 trak->header.height = video_info->height() * 0x10000;
221 VideoSampleEntry video;
222 video.format = CodecToFourCC(video_info->codec());
223 video.width = video_info->width();
224 video.height = video_info->height();
225 video.codec_configuration.data = video_info->codec_config();
226 if (pixel_width != 1 || pixel_height != 1) {
227 video.pixel_aspect.h_spacing = pixel_width;
228 video.pixel_aspect.v_spacing = pixel_height;
231 SampleDescription& sample_description =
232 trak->media.information.sample_table.description;
233 sample_description.type = kVideo;
234 sample_description.video_entries.push_back(video);
237 void MP4Muxer::GenerateAudioTrak(
const AudioStreamInfo* audio_info,
240 InitializeTrak(audio_info, trak);
242 trak->header.volume = 0x100;
244 AudioSampleEntry audio;
245 audio.format = CodecToFourCC(audio_info->codec());
246 switch(audio_info->codec()){
248 audio.esds.es_descriptor.set_object_type(kISO_14496_3);
249 audio.esds.es_descriptor.set_esid(track_id);
250 audio.esds.es_descriptor.set_decoder_specific_info(
251 audio_info->codec_config());
252 audio.esds.es_descriptor.set_max_bitrate(audio_info->max_bitrate());
253 audio.esds.es_descriptor.set_avg_bitrate(audio_info->avg_bitrate());
260 audio.ddts.extra_data = audio_info->codec_config();
261 audio.ddts.max_bitrate = audio_info->max_bitrate();
262 audio.ddts.avg_bitrate = audio_info->avg_bitrate();
263 audio.ddts.sampling_frequency = audio_info->sampling_frequency();
264 audio.ddts.pcm_sample_depth = audio_info->sample_bits();
267 audio.dac3.data = audio_info->codec_config();
270 audio.dec3.data = audio_info->codec_config();
273 audio.dops.opus_identification_header = audio_info->codec_config();
280 audio.channelcount = audio_info->num_channels();
281 audio.samplesize = audio_info->sample_bits();
282 audio.samplerate = audio_info->sampling_frequency();
283 SampleTable& sample_table = trak->media.information.sample_table;
284 SampleDescription& sample_description = sample_table.description;
285 sample_description.type = kAudio;
286 sample_description.audio_entries.push_back(audio);
290 if (audio_info->codec() == kCodecOpus) {
291 sample_table.sample_group_descriptions.resize(1);
292 SampleGroupDescription& sample_group_description =
293 sample_table.sample_group_descriptions.back();
294 sample_group_description.grouping_type = FOURCC_roll;
295 sample_group_description.audio_roll_recovery_entries.resize(1);
298 const uint64_t kNanosecondsPerSecond = 1000000000ull;
299 sample_group_description.audio_roll_recovery_entries[0].roll_distance =
300 (0 - (audio_info->seek_preroll_ns() * audio.samplerate +
301 kNanosecondsPerSecond / 2)) /
302 kNanosecondsPerSecond;
304 sample_table.sample_to_groups.resize(1);
305 SampleToGroup& sample_to_group = sample_table.sample_to_groups.back();
306 sample_to_group.grouping_type = FOURCC_roll;
308 sample_to_group.entries.resize(1);
309 SampleToGroupEntry& sample_to_group_entry = sample_to_group.entries.back();
311 sample_to_group_entry.sample_count = 0;
312 sample_to_group_entry.group_description_index =
313 SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
314 }
else if (audio_info->seek_preroll_ns() != 0) {
315 LOG(WARNING) <<
"Unexpected seek preroll for codec " << audio_info->codec();
320 bool MP4Muxer::GetInitRangeStartAndEnd(uint32_t* start, uint32_t* end) {
321 DCHECK(start && end);
322 size_t range_offset = 0;
323 size_t range_size = 0;
324 const bool has_range = segmenter_->GetInitRange(&range_offset, &range_size);
329 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
333 bool MP4Muxer::GetIndexRangeStartAndEnd(uint32_t* start, uint32_t* end) {
334 DCHECK(start && end);
335 size_t range_offset = 0;
336 size_t range_size = 0;
337 const bool has_range = segmenter_->GetIndexRange(&range_offset, &range_size);
342 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
346 void MP4Muxer::FireOnMediaStartEvent() {
347 if (!muxer_listener())
350 if (streams().size() > 1) {
351 LOG(ERROR) <<
"MuxerListener cannot take more than 1 stream.";
354 DCHECK(!streams().empty()) <<
"Media started without a stream.";
356 const uint32_t timescale = segmenter_->GetReferenceTimeScale();
358 *streams().front()->info(),
360 MuxerListener::kContainerMp4);
363 void MP4Muxer::FireOnMediaEndEvent() {
364 if (!muxer_listener())
367 uint32_t init_range_start = 0;
368 uint32_t init_range_end = 0;
369 const bool has_init_range =
370 GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
372 uint32_t index_range_start = 0;
373 uint32_t index_range_end = 0;
374 const bool has_index_range =
375 GetIndexRangeStartAndEnd(&index_range_start, &index_range_end);
377 const float duration_seconds =
static_cast<float>(segmenter_->GetDuration());
379 const int64_t file_size =
381 if (file_size <= 0) {
382 LOG(ERROR) <<
"Invalid file size: " << file_size;
396 uint64_t MP4Muxer::IsoTimeNow() {
398 const uint64_t kIsomTimeOffset = 2082844800l;
399 return kIsomTimeOffset +
400 (clock() ? clock()->Now() : base::Time::Now()).ToDoubleT();