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 CodecToFourCC(VideoCodec codec) {
65 MP4Muxer::~MP4Muxer() {}
67 Status MP4Muxer::Initialize() {
68 DCHECK(!streams().empty());
70 scoped_ptr<FileType> ftyp(
new FileType);
71 scoped_ptr<Movie> moov(
new Movie);
73 ftyp->major_brand = FOURCC_DASH;
74 ftyp->compatible_brands.push_back(FOURCC_ISO6);
75 ftyp->compatible_brands.push_back(FOURCC_MP41);
76 if (streams().size() == 1 &&
77 streams()[0]->info()->stream_type() == kStreamVideo) {
78 const FourCC codec_fourcc = CodecToFourCC(
79 static_cast<VideoStreamInfo*>(streams()[0]->info().
get())->codec());
80 if (codec_fourcc != FOURCC_NULL)
81 ftyp->compatible_brands.push_back(codec_fourcc);
84 moov->header.creation_time = IsoTimeNow();
85 moov->header.modification_time = IsoTimeNow();
86 moov->header.next_track_id = streams().size() + 1;
88 moov->tracks.resize(streams().size());
89 moov->extends.tracks.resize(streams().size());
92 for (uint32_t i = 0; i < streams().size(); ++i) {
93 Track& trak = moov->tracks[i];
94 trak.header.track_id = i + 1;
96 TrackExtends& trex = moov->extends.tracks[i];
97 trex.track_id = trak.header.track_id;
98 trex.default_sample_description_index = 1;
100 switch (streams()[i]->info()->stream_type()) {
103 static_cast<VideoStreamInfo*>(streams()[i]->info().
get()),
109 static_cast<AudioStreamInfo*>(streams()[i]->info().
get()),
114 NOTIMPLEMENTED() <<
"Not implemented for stream type: "
115 << streams()[i]->info()->stream_type();
119 if (options().single_segment) {
121 new SingleSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
124 new MultiSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
127 Status segmenter_initialized =
128 segmenter_->Initialize(streams(),
131 encryption_key_source(),
133 clear_lead_in_seconds(),
134 crypto_period_duration_in_seconds());
136 if (!segmenter_initialized.ok())
137 return segmenter_initialized;
139 FireOnMediaStartEvent();
143 Status MP4Muxer::Finalize() {
145 Status segmenter_finalized = segmenter_->Finalize();
147 if (!segmenter_finalized.ok())
148 return segmenter_finalized;
150 FireOnMediaEndEvent();
155 Status MP4Muxer::DoAddSample(
const MediaStream* stream,
156 scoped_refptr<MediaSample> sample) {
158 return segmenter_->AddSample(stream, sample);
161 void MP4Muxer::InitializeTrak(
const StreamInfo* info, Track* trak) {
162 int64_t now = IsoTimeNow();
163 trak->header.creation_time = now;
164 trak->header.modification_time = now;
165 trak->header.duration = 0;
166 trak->media.header.creation_time = now;
167 trak->media.header.modification_time = now;
168 trak->media.header.timescale = info->time_scale();
169 trak->media.header.duration = 0;
170 if (!info->language().empty()) {
171 const size_t language_size = arraysize(trak->media.header.language) - 1;
172 if (info->language().size() != language_size) {
173 LOG(WARNING) <<
"'" << info->language() <<
"' is not a valid ISO-639-2 "
174 <<
"language code, ignoring.";
176 memcpy(trak->media.header.language, info->language().c_str(),
182 void MP4Muxer::GenerateVideoTrak(
const VideoStreamInfo* video_info,
185 InitializeTrak(video_info, trak);
189 uint32_t pixel_width = video_info->pixel_width();
190 uint32_t pixel_height = video_info->pixel_height();
191 if (pixel_width == 0 || pixel_height == 0) {
192 LOG(WARNING) <<
"pixel width/height are not set. Assuming 1:1.";
196 const double sample_aspect_ratio =
197 static_cast<double>(pixel_width) / pixel_height;
198 trak->header.width = video_info->width() * sample_aspect_ratio * 0x10000;
199 trak->header.height = video_info->height() * 0x10000;
201 trak->media.handler.type = kVideo;
203 VideoSampleEntry video;
204 video.format = CodecToFourCC(video_info->codec());
205 video.width = video_info->width();
206 video.height = video_info->height();
207 video.codec_config_record.data = video_info->extra_data();
208 if (pixel_width != 1 || pixel_height != 1) {
209 video.pixel_aspect.h_spacing = pixel_width;
210 video.pixel_aspect.v_spacing = pixel_height;
213 SampleDescription& sample_description =
214 trak->media.information.sample_table.description;
215 sample_description.type = kVideo;
216 sample_description.video_entries.push_back(video);
219 void MP4Muxer::GenerateAudioTrak(
const AudioStreamInfo* audio_info,
222 InitializeTrak(audio_info, trak);
224 trak->header.volume = 0x100;
225 trak->media.handler.type = kAudio;
227 AudioSampleEntry audio;
228 switch(audio_info->codec()){
230 audio.format = FOURCC_MP4A;
231 audio.esds.es_descriptor.set_object_type(kISO_14496_3);
232 audio.esds.es_descriptor.set_esid(track_id);
233 audio.esds.es_descriptor.set_decoder_specific_info(
234 audio_info->extra_data());
237 audio.format = FOURCC_DTSC;
238 audio.ddts.data = audio_info->extra_data();
241 audio.format = FOURCC_DTSH;
242 audio.ddts.data = audio_info->extra_data();
245 audio.format = FOURCC_DTSL;
246 audio.ddts.data = audio_info->extra_data();
249 audio.format = FOURCC_DTSE;
250 audio.ddts.data = audio_info->extra_data();
253 audio.format = FOURCC_DTSM;
254 audio.ddts.data = audio_info->extra_data();
261 audio.channelcount = audio_info->num_channels();
262 audio.samplesize = audio_info->sample_bits();
263 audio.samplerate = audio_info->sampling_frequency();
264 SampleDescription& sample_description =
265 trak->media.information.sample_table.description;
266 sample_description.type = kAudio;
267 sample_description.audio_entries.push_back(audio);
270 bool MP4Muxer::GetInitRangeStartAndEnd(uint32_t* start, uint32_t* end) {
271 DCHECK(start && end);
272 size_t range_offset = 0;
273 size_t range_size = 0;
274 const bool has_range = segmenter_->GetInitRange(&range_offset, &range_size);
279 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
283 bool MP4Muxer::GetIndexRangeStartAndEnd(uint32_t* start, uint32_t* end) {
284 DCHECK(start && end);
285 size_t range_offset = 0;
286 size_t range_size = 0;
287 const bool has_range = segmenter_->GetIndexRange(&range_offset, &range_size);
292 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
296 void MP4Muxer::FireOnMediaStartEvent() {
297 if (!muxer_listener())
300 if (streams().size() > 1) {
301 LOG(ERROR) <<
"MuxerListener cannot take more than 1 stream.";
304 DCHECK(!streams().empty()) <<
"Media started without a stream.";
306 const uint32_t timescale = segmenter_->GetReferenceTimeScale();
307 muxer_listener()->OnMediaStart(options(),
308 *streams().front()->info(),
310 MuxerListener::kContainerMp4);
313 void MP4Muxer::FireOnMediaEndEvent() {
314 if (!muxer_listener())
317 uint32_t init_range_start = 0;
318 uint32_t init_range_end = 0;
319 const bool has_init_range =
320 GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
322 uint32_t index_range_start = 0;
323 uint32_t index_range_end = 0;
324 const bool has_index_range =
325 GetIndexRangeStartAndEnd(&index_range_start, &index_range_end);
327 const float duration_seconds =
static_cast<float>(segmenter_->GetDuration());
329 const int64_t file_size =
331 if (file_size <= 0) {
332 LOG(ERROR) <<
"Invalid file size: " << file_size;
336 muxer_listener()->OnMediaEnd(has_init_range,
346 uint64_t MP4Muxer::IsoTimeNow() {
348 const uint64_t kIsomTimeOffset = 2082844800l;
349 return kIsomTimeOffset +
350 (clock() ? clock()->Now() : base::Time::Now()).ToDoubleT();