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"
28 void SetStartAndEndFromOffsetAndSize(
size_t offset,
33 *start =
static_cast<uint32_t
>(offset);
35 *end = *start +
static_cast<uint32_t
>(size) - 1;
39 namespace edash_packager {
44 MP4Muxer::~MP4Muxer() {}
46 Status MP4Muxer::Initialize() {
47 DCHECK(!streams().empty());
49 scoped_ptr<FileType> ftyp(
new FileType);
50 scoped_ptr<Movie> moov(
new Movie);
52 ftyp->major_brand = FOURCC_DASH;
53 ftyp->compatible_brands.push_back(FOURCC_ISO6);
54 ftyp->compatible_brands.push_back(FOURCC_MP41);
55 if (streams().size() == 1 &&
56 streams()[0]->info()->stream_type() == kStreamVideo)
57 ftyp->compatible_brands.push_back(FOURCC_AVC1);
59 moov->header.creation_time = IsoTimeNow();
60 moov->header.modification_time = IsoTimeNow();
61 moov->header.next_track_id = streams().size() + 1;
63 moov->tracks.resize(streams().size());
64 moov->extends.tracks.resize(streams().size());
67 for (uint32_t i = 0; i < streams().size(); ++i) {
68 Track& trak = moov->tracks[i];
69 trak.header.track_id = i + 1;
71 TrackExtends& trex = moov->extends.tracks[i];
72 trex.track_id = trak.header.track_id;
73 trex.default_sample_description_index = 1;
75 switch (streams()[i]->info()->stream_type()) {
78 static_cast<VideoStreamInfo*>(streams()[i]->info().
get()),
84 static_cast<AudioStreamInfo*>(streams()[i]->info().
get()),
89 NOTIMPLEMENTED() <<
"Not implemented for stream type: "
90 << streams()[i]->info()->stream_type();
94 if (options().single_segment) {
96 new SingleSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
99 new MultiSegmentSegmenter(options(), ftyp.Pass(), moov.Pass()));
102 Status segmenter_initialized =
103 segmenter_->Initialize(streams(),
106 encryption_key_source(),
108 clear_lead_in_seconds(),
109 crypto_period_duration_in_seconds());
111 if (!segmenter_initialized.ok())
112 return segmenter_initialized;
114 FireOnMediaStartEvent();
118 Status MP4Muxer::Finalize() {
120 Status segmenter_finalized = segmenter_->Finalize();
122 if (!segmenter_finalized.ok())
123 return segmenter_finalized;
125 FireOnMediaEndEvent();
130 Status MP4Muxer::DoAddSample(
const MediaStream* stream,
131 scoped_refptr<MediaSample> sample) {
133 return segmenter_->AddSample(stream, sample);
136 void MP4Muxer::InitializeTrak(
const StreamInfo* info, Track* trak) {
137 int64_t now = IsoTimeNow();
138 trak->header.creation_time = now;
139 trak->header.modification_time = now;
140 trak->header.duration = 0;
141 trak->media.header.creation_time = now;
142 trak->media.header.modification_time = now;
143 trak->media.header.timescale = info->time_scale();
144 trak->media.header.duration = 0;
145 if (!info->language().empty()) {
146 const size_t language_size = arraysize(trak->media.header.language) - 1;
147 if (info->language().size() != language_size) {
148 LOG(WARNING) <<
"'" << info->language() <<
"' is not a valid ISO-639-2 "
149 <<
"language code, ignoring.";
151 memcpy(trak->media.header.language, info->language().c_str(),
157 void MP4Muxer::GenerateVideoTrak(
const VideoStreamInfo* video_info,
160 InitializeTrak(video_info, trak);
164 uint32_t pixel_width = video_info->pixel_width();
165 uint32_t pixel_height = video_info->pixel_height();
166 if (pixel_width == 0 || pixel_height == 0) {
167 LOG(WARNING) <<
"pixel width/height are not set. Assuming 1:1.";
171 const double sample_aspect_ratio =
172 static_cast<double>(pixel_width) / pixel_height;
173 trak->header.width = video_info->width() * sample_aspect_ratio * 0x10000;
174 trak->header.height = video_info->height() * 0x10000;
176 trak->media.handler.type = kVideo;
178 VideoSampleEntry video;
179 video.format = FOURCC_AVC1;
180 video.width = video_info->width();
181 video.height = video_info->height();
182 video.avcc.data = video_info->extra_data();
183 if (pixel_width != 1 || pixel_height != 1) {
184 video.pixel_aspect.h_spacing = pixel_width;
185 video.pixel_aspect.v_spacing = pixel_height;
188 SampleDescription& sample_description =
189 trak->media.information.sample_table.description;
190 sample_description.type = kVideo;
191 sample_description.video_entries.push_back(video);
194 void MP4Muxer::GenerateAudioTrak(
const AudioStreamInfo* audio_info,
197 InitializeTrak(audio_info, trak);
199 trak->header.volume = 0x100;
200 trak->media.handler.type = kAudio;
202 AudioSampleEntry audio;
203 audio.format = FOURCC_MP4A;
204 audio.channelcount = audio_info->num_channels();
205 audio.samplesize = audio_info->sample_bits();
206 audio.samplerate = audio_info->sampling_frequency();
209 audio.esds.es_descriptor.set_object_type(kISO_14496_3);
214 audio.esds.es_descriptor.set_esid(track_id);
215 audio.esds.es_descriptor.set_decoder_specific_info(audio_info->extra_data());
217 SampleDescription& sample_description =
218 trak->media.information.sample_table.description;
219 sample_description.type = kAudio;
220 sample_description.audio_entries.push_back(audio);
223 bool MP4Muxer::GetInitRangeStartAndEnd(uint32_t* start, uint32_t* end) {
224 DCHECK(start && end);
225 size_t range_offset = 0;
226 size_t range_size = 0;
227 const bool has_range = segmenter_->GetInitRange(&range_offset, &range_size);
232 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
236 bool MP4Muxer::GetIndexRangeStartAndEnd(uint32_t* start, uint32_t* end) {
237 DCHECK(start && end);
238 size_t range_offset = 0;
239 size_t range_size = 0;
240 const bool has_range = segmenter_->GetIndexRange(&range_offset, &range_size);
245 SetStartAndEndFromOffsetAndSize(range_offset, range_size, start, end);
249 void MP4Muxer::FireOnMediaStartEvent() {
250 if (!muxer_listener())
253 if (streams().size() > 1) {
254 LOG(ERROR) <<
"MuxerListener cannot take more than 1 stream.";
257 DCHECK(!streams().empty()) <<
"Media started without a stream.";
259 const uint32_t timescale = segmenter_->GetReferenceTimeScale();
260 muxer_listener()->OnMediaStart(options(),
261 *streams().front()->info(),
263 MuxerListener::kContainerMp4);
266 void MP4Muxer::FireOnMediaEndEvent() {
267 if (!muxer_listener())
270 uint32_t init_range_start = 0;
271 uint32_t init_range_end = 0;
272 const bool has_init_range =
273 GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
275 uint32_t index_range_start = 0;
276 uint32_t index_range_end = 0;
277 const bool has_index_range =
278 GetIndexRangeStartAndEnd(&index_range_start, &index_range_end);
280 const float duration_seconds =
static_cast<float>(segmenter_->GetDuration());
282 const int64_t file_size =
284 if (file_size <= 0) {
285 LOG(ERROR) <<
"Invalid file size: " << file_size;
289 muxer_listener()->OnMediaEnd(has_init_range,
299 uint64_t MP4Muxer::IsoTimeNow() {
301 const uint64_t kIsomTimeOffset = 2082844800l;
302 return kIsomTimeOffset +
303 (clock() ? clock()->Now() : base::Time::Now()).ToDoubleT();