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 audio.format = FOURCC_MP4A;
229 audio.channelcount = audio_info->num_channels();
230 audio.samplesize = audio_info->sample_bits();
231 audio.samplerate = audio_info->sampling_frequency();
234 audio.esds.es_descriptor.set_object_type(kISO_14496_3);
239 audio.esds.es_descriptor.set_esid(track_id);
240 audio.esds.es_descriptor.set_decoder_specific_info(audio_info->extra_data());
242 SampleDescription& sample_description =
243 trak->media.information.sample_table.description;
244 sample_description.type = kAudio;
245 sample_description.audio_entries.push_back(audio);
248 bool MP4Muxer::GetInitRangeStartAndEnd(uint32_t* start, uint32_t* end) {
249 DCHECK(start && end);
250 size_t range_offset = 0;
251 size_t range_size = 0;
252 const bool has_range = segmenter_->GetInitRange(&range_offset, &range_size);
257 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
261 bool MP4Muxer::GetIndexRangeStartAndEnd(uint32_t* start, uint32_t* end) {
262 DCHECK(start && end);
263 size_t range_offset = 0;
264 size_t range_size = 0;
265 const bool has_range = segmenter_->GetIndexRange(&range_offset, &range_size);
270 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
274 void MP4Muxer::FireOnMediaStartEvent() {
275 if (!muxer_listener())
278 if (streams().size() > 1) {
279 LOG(ERROR) <<
"MuxerListener cannot take more than 1 stream.";
282 DCHECK(!streams().empty()) <<
"Media started without a stream.";
284 const uint32_t timescale = segmenter_->GetReferenceTimeScale();
285 muxer_listener()->OnMediaStart(options(),
286 *streams().front()->info(),
288 MuxerListener::kContainerMp4);
291 void MP4Muxer::FireOnMediaEndEvent() {
292 if (!muxer_listener())
295 uint32_t init_range_start = 0;
296 uint32_t init_range_end = 0;
297 const bool has_init_range =
298 GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
300 uint32_t index_range_start = 0;
301 uint32_t index_range_end = 0;
302 const bool has_index_range =
303 GetIndexRangeStartAndEnd(&index_range_start, &index_range_end);
305 const float duration_seconds =
static_cast<float>(segmenter_->GetDuration());
307 const int64_t file_size =
309 if (file_size <= 0) {
310 LOG(ERROR) <<
"Invalid file size: " << file_size;
314 muxer_listener()->OnMediaEnd(has_init_range,
324 uint64_t MP4Muxer::IsoTimeNow() {
326 const uint64_t kIsomTimeOffset = 2082844800l;
327 return kIsomTimeOffset +
328 (clock() ? clock()->Now() : base::Time::Now()).ToDoubleT();