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) {
84 MP4Muxer::~MP4Muxer() {}
86 Status MP4Muxer::Initialize() {
87 DCHECK(!streams().empty());
89 scoped_ptr<FileType> ftyp(
new FileType);
90 scoped_ptr<Movie> moov(
new Movie);
92 ftyp->major_brand = FOURCC_DASH;
93 ftyp->compatible_brands.push_back(FOURCC_ISO6);
94 ftyp->compatible_brands.push_back(FOURCC_MP41);
95 if (streams().size() == 1 &&
96 streams()[0]->info()->stream_type() == kStreamVideo) {
97 const FourCC codec_fourcc = VideoCodecToFourCC(
98 static_cast<VideoStreamInfo*>(streams()[0]->info().
get())->codec());
99 if (codec_fourcc != FOURCC_NULL)
100 ftyp->compatible_brands.push_back(codec_fourcc);
103 moov->header.creation_time = IsoTimeNow();
104 moov->header.modification_time = IsoTimeNow();
105 moov->header.next_track_id = streams().size() + 1;
107 moov->tracks.resize(streams().size());
108 moov->extends.tracks.resize(streams().size());
111 for (uint32_t i = 0; i < streams().size(); ++i) {
112 Track& trak = moov->tracks[i];
113 trak.header.track_id = i + 1;
115 TrackExtends& trex = moov->extends.tracks[i];
116 trex.track_id = trak.header.track_id;
117 trex.default_sample_description_index = 1;
119 switch (streams()[i]->info()->stream_type()) {
122 static_cast<VideoStreamInfo*>(streams()[i]->info().
get()),
128 static_cast<AudioStreamInfo*>(streams()[i]->info().
get()),
133 NOTIMPLEMENTED() <<
"Not implemented for stream type: "
134 << streams()[i]->info()->stream_type();
138 if (options().single_segment) {
140 new SingleSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
143 new MultiSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
146 Status segmenter_initialized =
147 segmenter_->Initialize(streams(),
150 encryption_key_source(),
152 clear_lead_in_seconds(),
153 crypto_period_duration_in_seconds());
155 if (!segmenter_initialized.ok())
156 return segmenter_initialized;
158 FireOnMediaStartEvent();
162 Status MP4Muxer::Finalize() {
164 Status segmenter_finalized = segmenter_->Finalize();
166 if (!segmenter_finalized.ok())
167 return segmenter_finalized;
169 FireOnMediaEndEvent();
174 Status MP4Muxer::DoAddSample(
const MediaStream* stream,
175 scoped_refptr<MediaSample> sample) {
177 return segmenter_->AddSample(stream, sample);
180 void MP4Muxer::InitializeTrak(
const StreamInfo* info, Track* trak) {
181 int64_t now = IsoTimeNow();
182 trak->header.creation_time = now;
183 trak->header.modification_time = now;
184 trak->header.duration = 0;
185 trak->media.header.creation_time = now;
186 trak->media.header.modification_time = now;
187 trak->media.header.timescale = info->time_scale();
188 trak->media.header.duration = 0;
189 if (!info->language().empty()) {
190 const size_t language_size = arraysize(trak->media.header.language) - 1;
191 if (info->language().size() != language_size) {
192 LOG(WARNING) <<
"'" << info->language() <<
"' is not a valid ISO-639-2 "
193 <<
"language code, ignoring.";
195 memcpy(trak->media.header.language, info->language().c_str(),
201 void MP4Muxer::GenerateVideoTrak(
const VideoStreamInfo* video_info,
204 InitializeTrak(video_info, trak);
208 uint32_t pixel_width = video_info->pixel_width();
209 uint32_t pixel_height = video_info->pixel_height();
210 if (pixel_width == 0 || pixel_height == 0) {
211 LOG(WARNING) <<
"pixel width/height are not set. Assuming 1:1.";
215 const double sample_aspect_ratio =
216 static_cast<double>(pixel_width) / pixel_height;
217 trak->header.width = video_info->width() * sample_aspect_ratio * 0x10000;
218 trak->header.height = video_info->height() * 0x10000;
220 trak->media.handler.type = kVideo;
222 VideoSampleEntry video;
223 video.format = VideoCodecToFourCC(video_info->codec());
224 video.width = video_info->width();
225 video.height = video_info->height();
226 video.codec_config_record.data = video_info->extra_data();
227 if (pixel_width != 1 || pixel_height != 1) {
228 video.pixel_aspect.h_spacing = pixel_width;
229 video.pixel_aspect.v_spacing = pixel_height;
232 SampleDescription& sample_description =
233 trak->media.information.sample_table.description;
234 sample_description.type = kVideo;
235 sample_description.video_entries.push_back(video);
238 void MP4Muxer::GenerateAudioTrak(
const AudioStreamInfo* audio_info,
241 InitializeTrak(audio_info, trak);
243 trak->header.volume = 0x100;
244 trak->media.handler.type = kAudio;
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();
273 audio.channelcount = audio_info->num_channels();
274 audio.samplesize = audio_info->sample_bits();
275 audio.samplerate = audio_info->sampling_frequency();
276 SampleDescription& sample_description =
277 trak->media.information.sample_table.description;
278 sample_description.type = kAudio;
279 sample_description.audio_entries.push_back(audio);
282 bool MP4Muxer::GetInitRangeStartAndEnd(uint32_t* start, uint32_t* end) {
283 DCHECK(start && end);
284 size_t range_offset = 0;
285 size_t range_size = 0;
286 const bool has_range = segmenter_->GetInitRange(&range_offset, &range_size);
291 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
295 bool MP4Muxer::GetIndexRangeStartAndEnd(uint32_t* start, uint32_t* end) {
296 DCHECK(start && end);
297 size_t range_offset = 0;
298 size_t range_size = 0;
299 const bool has_range = segmenter_->GetIndexRange(&range_offset, &range_size);
304 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
308 void MP4Muxer::FireOnMediaStartEvent() {
309 if (!muxer_listener())
312 if (streams().size() > 1) {
313 LOG(ERROR) <<
"MuxerListener cannot take more than 1 stream.";
316 DCHECK(!streams().empty()) <<
"Media started without a stream.";
318 const uint32_t timescale = segmenter_->GetReferenceTimeScale();
319 muxer_listener()->OnMediaStart(options(),
320 *streams().front()->info(),
322 MuxerListener::kContainerMp4);
325 void MP4Muxer::FireOnMediaEndEvent() {
326 if (!muxer_listener())
329 uint32_t init_range_start = 0;
330 uint32_t init_range_end = 0;
331 const bool has_init_range =
332 GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
334 uint32_t index_range_start = 0;
335 uint32_t index_range_end = 0;
336 const bool has_index_range =
337 GetIndexRangeStartAndEnd(&index_range_start, &index_range_end);
339 const float duration_seconds =
static_cast<float>(segmenter_->GetDuration());
341 const int64_t file_size =
343 if (file_size <= 0) {
344 LOG(ERROR) <<
"Invalid file size: " << file_size;
348 muxer_listener()->OnMediaEnd(has_init_range,
358 uint64_t MP4Muxer::IsoTimeNow() {
360 const uint64_t kIsomTimeOffset = 2082844800l;
361 return kIsomTimeOffset +
362 (clock() ? clock()->Now() : base::Time::Now()).ToDoubleT();