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/key_source.h"
14 #include "packager/media/base/media_sample.h"
15 #include "packager/media/base/media_stream.h"
16 #include "packager/media/base/video_stream_info.h"
17 #include "packager/media/event/muxer_listener.h"
18 #include "packager/media/file/file.h"
19 #include "packager/media/formats/mp4/box_definitions.h"
20 #include "packager/media/formats/mp4/es_descriptor.h"
21 #include "packager/media/formats/mp4/fourccs.h"
22 #include "packager/media/formats/mp4/multi_segment_segmenter.h"
23 #include "packager/media/formats/mp4/single_segment_segmenter.h"
25 namespace edash_packager {
33 void SetStartAndEndFromOffsetAndSize(
size_t offset,
38 *start =
static_cast<uint32_t
>(offset);
40 *end = *start +
static_cast<uint32_t
>(size) - 1;
43 FourCC VideoCodecToFourCC(VideoCodec codec) {
62 FourCC AudioCodecToFourCC(AudioCodec codec) {
88 MP4Muxer::~MP4Muxer() {}
90 Status MP4Muxer::Initialize() {
91 DCHECK(!streams().empty());
93 scoped_ptr<FileType> ftyp(
new FileType);
94 scoped_ptr<Movie> moov(
new Movie);
96 ftyp->major_brand = FOURCC_DASH;
97 ftyp->compatible_brands.push_back(FOURCC_ISO6);
98 ftyp->compatible_brands.push_back(FOURCC_MP41);
99 if (streams().size() == 1 &&
100 streams()[0]->info()->stream_type() == kStreamVideo) {
101 const FourCC codec_fourcc = VideoCodecToFourCC(
102 static_cast<VideoStreamInfo*>(streams()[0]->info().
get())->codec());
103 if (codec_fourcc != FOURCC_NULL)
104 ftyp->compatible_brands.push_back(codec_fourcc);
107 moov->header.creation_time = IsoTimeNow();
108 moov->header.modification_time = IsoTimeNow();
109 moov->header.next_track_id = streams().size() + 1;
111 moov->tracks.resize(streams().size());
112 moov->extends.tracks.resize(streams().size());
115 for (uint32_t i = 0; i < streams().size(); ++i) {
116 Track& trak = moov->tracks[i];
117 trak.header.track_id = i + 1;
119 TrackExtends& trex = moov->extends.tracks[i];
120 trex.track_id = trak.header.track_id;
121 trex.default_sample_description_index = 1;
123 switch (streams()[i]->info()->stream_type()) {
126 static_cast<VideoStreamInfo*>(streams()[i]->info().
get()),
132 static_cast<AudioStreamInfo*>(streams()[i]->info().
get()),
137 NOTIMPLEMENTED() <<
"Not implemented for stream type: "
138 << streams()[i]->info()->stream_type();
142 if (options().single_segment) {
144 new SingleSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
147 new MultiSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
150 Status segmenter_initialized =
151 segmenter_->Initialize(streams(),
154 encryption_key_source(),
156 clear_lead_in_seconds(),
157 crypto_period_duration_in_seconds());
159 if (!segmenter_initialized.ok())
160 return segmenter_initialized;
162 FireOnMediaStartEvent();
166 Status MP4Muxer::Finalize() {
168 Status segmenter_finalized = segmenter_->Finalize();
170 if (!segmenter_finalized.ok())
171 return segmenter_finalized;
173 FireOnMediaEndEvent();
178 Status MP4Muxer::DoAddSample(
const MediaStream* stream,
179 scoped_refptr<MediaSample> sample) {
181 return segmenter_->AddSample(stream, sample);
184 void MP4Muxer::InitializeTrak(
const StreamInfo* info, Track* trak) {
185 int64_t now = IsoTimeNow();
186 trak->header.creation_time = now;
187 trak->header.modification_time = now;
188 trak->header.duration = 0;
189 trak->media.header.creation_time = now;
190 trak->media.header.modification_time = now;
191 trak->media.header.timescale = info->time_scale();
192 trak->media.header.duration = 0;
193 if (!info->language().empty()) {
195 if (info->language().size() != 3) {
196 LOG(WARNING) <<
"'" << info->language() <<
"' is not a valid ISO-639-2 "
197 <<
"language code, ignoring.";
199 trak->media.header.language.code = info->language();
204 void MP4Muxer::GenerateVideoTrak(
const VideoStreamInfo* video_info,
207 InitializeTrak(video_info, trak);
211 uint32_t pixel_width = video_info->pixel_width();
212 uint32_t pixel_height = video_info->pixel_height();
213 if (pixel_width == 0 || pixel_height == 0) {
214 LOG(WARNING) <<
"pixel width/height are not set. Assuming 1:1.";
218 const double sample_aspect_ratio =
219 static_cast<double>(pixel_width) / pixel_height;
220 trak->header.width = video_info->width() * sample_aspect_ratio * 0x10000;
221 trak->header.height = video_info->height() * 0x10000;
223 VideoSampleEntry video;
224 video.format = VideoCodecToFourCC(video_info->codec());
225 video.width = video_info->width();
226 video.height = video_info->height();
227 video.codec_config_record.data = video_info->extra_data();
228 if (pixel_width != 1 || pixel_height != 1) {
229 video.pixel_aspect.h_spacing = pixel_width;
230 video.pixel_aspect.v_spacing = pixel_height;
233 SampleDescription& sample_description =
234 trak->media.information.sample_table.description;
235 sample_description.type = kVideo;
236 sample_description.video_entries.push_back(video);
239 void MP4Muxer::GenerateAudioTrak(
const AudioStreamInfo* audio_info,
242 InitializeTrak(audio_info, trak);
244 trak->header.volume = 0x100;
246 AudioSampleEntry audio;
247 audio.format = AudioCodecToFourCC(audio_info->codec());
248 switch(audio_info->codec()){
250 audio.esds.es_descriptor.set_object_type(kISO_14496_3);
251 audio.esds.es_descriptor.set_esid(track_id);
252 audio.esds.es_descriptor.set_decoder_specific_info(
253 audio_info->extra_data());
254 audio.esds.es_descriptor.set_max_bitrate(audio_info->max_bitrate());
255 audio.esds.es_descriptor.set_avg_bitrate(audio_info->avg_bitrate());
262 audio.ddts.extra_data = audio_info->extra_data();
263 audio.ddts.max_bitrate = audio_info->max_bitrate();
264 audio.ddts.avg_bitrate = audio_info->avg_bitrate();
265 audio.ddts.sampling_frequency = audio_info->sampling_frequency();
266 audio.ddts.pcm_sample_depth = audio_info->sample_bits();
269 audio.dac3.data = audio_info->extra_data();
272 audio.dec3.data = audio_info->extra_data();
279 audio.channelcount = audio_info->num_channels();
280 audio.samplesize = audio_info->sample_bits();
281 audio.samplerate = audio_info->sampling_frequency();
282 SampleDescription& sample_description =
283 trak->media.information.sample_table.description;
284 sample_description.type = kAudio;
285 sample_description.audio_entries.push_back(audio);
288 bool MP4Muxer::GetInitRangeStartAndEnd(uint32_t* start, uint32_t* end) {
289 DCHECK(start && end);
290 size_t range_offset = 0;
291 size_t range_size = 0;
292 const bool has_range = segmenter_->GetInitRange(&range_offset, &range_size);
297 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
301 bool MP4Muxer::GetIndexRangeStartAndEnd(uint32_t* start, uint32_t* end) {
302 DCHECK(start && end);
303 size_t range_offset = 0;
304 size_t range_size = 0;
305 const bool has_range = segmenter_->GetIndexRange(&range_offset, &range_size);
310 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
314 void MP4Muxer::FireOnMediaStartEvent() {
315 if (!muxer_listener())
318 if (streams().size() > 1) {
319 LOG(ERROR) <<
"MuxerListener cannot take more than 1 stream.";
322 DCHECK(!streams().empty()) <<
"Media started without a stream.";
324 const uint32_t timescale = segmenter_->GetReferenceTimeScale();
325 muxer_listener()->OnMediaStart(options(),
326 *streams().front()->info(),
328 MuxerListener::kContainerMp4);
331 void MP4Muxer::FireOnMediaEndEvent() {
332 if (!muxer_listener())
335 uint32_t init_range_start = 0;
336 uint32_t init_range_end = 0;
337 const bool has_init_range =
338 GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
340 uint32_t index_range_start = 0;
341 uint32_t index_range_end = 0;
342 const bool has_index_range =
343 GetIndexRangeStartAndEnd(&index_range_start, &index_range_end);
345 const float duration_seconds =
static_cast<float>(segmenter_->GetDuration());
347 const int64_t file_size =
349 if (file_size <= 0) {
350 LOG(ERROR) <<
"Invalid file size: " << file_size;
354 muxer_listener()->OnMediaEnd(has_init_range,
364 uint64_t MP4Muxer::IsoTimeNow() {
366 const uint64_t kIsomTimeOffset = 2082844800l;
367 return kIsomTimeOffset +
368 (clock() ? clock()->Now() : base::Time::Now()).ToDoubleT();