7 #include "packager/media/formats/mp4/segmenter.h"
11 #include "packager/base/logging.h"
12 #include "packager/base/stl_util.h"
13 #include "packager/media/base/aes_cryptor.h"
14 #include "packager/media/base/buffer_writer.h"
15 #include "packager/media/base/key_source.h"
16 #include "packager/media/base/media_sample.h"
17 #include "packager/media/base/media_stream.h"
18 #include "packager/media/base/muxer_options.h"
19 #include "packager/media/base/video_stream_info.h"
20 #include "packager/media/event/muxer_listener.h"
21 #include "packager/media/event/progress_listener.h"
22 #include "packager/media/formats/mp4/box_definitions.h"
23 #include "packager/media/formats/mp4/key_rotation_fragmenter.h"
25 namespace edash_packager {
31 const uint8_t kCryptByteBlock = 1u;
32 const uint8_t kSkipByteBlock = 9u;
34 const size_t kCencKeyIdSize = 16u;
37 const int kCencSchemeVersion = 0x00010000;
40 const uint8_t kKeyRotationDefaultKeyId[] = {
41 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0
45 COMPILE_ASSERT(arraysize(kKeyRotationDefaultKeyId) == kCencKeyIdSize,
46 cenc_key_id_must_be_size_16);
48 uint64_t Rescale(uint64_t time_in_old_scale,
51 return static_cast<double>(time_in_old_scale) / old_scale * new_scale;
54 uint8_t GetCryptByteBlock(FourCC protection_scheme) {
55 return (protection_scheme == FOURCC_cbcs || protection_scheme == FOURCC_cens)
60 uint8_t GetSkipByteBlock(FourCC protection_scheme) {
61 return (protection_scheme == FOURCC_cbcs || protection_scheme == FOURCC_cens)
66 void GenerateSinf(
const EncryptionKey& encryption_key,
68 FourCC protection_scheme,
69 ProtectionSchemeInfo* sinf) {
70 sinf->format.format = old_type;
72 DCHECK_NE(protection_scheme, FOURCC_NULL);
73 sinf->type.type = protection_scheme;
74 sinf->type.version = kCencSchemeVersion;
76 auto& track_encryption = sinf->info.track_encryption;
77 track_encryption.default_is_protected = 1;
78 DCHECK(!encryption_key.iv.empty());
79 if (protection_scheme == FOURCC_cbcs) {
82 track_encryption.default_per_sample_iv_size = 0;
83 track_encryption.default_constant_iv = encryption_key.iv;
85 track_encryption.default_per_sample_iv_size = encryption_key.iv.size();
87 track_encryption.default_crypt_byte_block =
88 GetCryptByteBlock(protection_scheme);
89 track_encryption.default_skip_byte_block =
90 GetSkipByteBlock(protection_scheme);
91 track_encryption.default_kid = encryption_key.key_id;
94 void GenerateEncryptedSampleEntry(
const EncryptionKey& encryption_key,
95 double clear_lead_in_seconds,
96 FourCC protection_scheme,
97 SampleDescription* description) {
99 if (description->type == kVideo) {
100 DCHECK_EQ(1u, description->video_entries.size());
103 if (clear_lead_in_seconds > 0)
104 description->video_entries.push_back(description->video_entries[0]);
107 VideoSampleEntry& entry = description->video_entries[0];
108 GenerateSinf(encryption_key, entry.format, protection_scheme, &entry.sinf);
109 entry.format = FOURCC_encv;
111 DCHECK_EQ(kAudio, description->type);
112 DCHECK_EQ(1u, description->audio_entries.size());
115 if (clear_lead_in_seconds > 0)
116 description->audio_entries.push_back(description->audio_entries[0]);
119 AudioSampleEntry& entry = description->audio_entries[0];
120 GenerateSinf(encryption_key, entry.format, protection_scheme, &entry.sinf);
121 entry.format = FOURCC_enca;
125 KeySource::TrackType GetTrackTypeForEncryption(
const StreamInfo& stream_info,
126 uint32_t max_sd_pixels) {
127 if (stream_info.stream_type() == kStreamAudio)
128 return KeySource::TRACK_TYPE_AUDIO;
130 DCHECK_EQ(kStreamVideo, stream_info.stream_type());
131 const VideoStreamInfo& video_stream_info =
132 static_cast<const VideoStreamInfo&
>(stream_info);
133 uint32_t pixels = video_stream_info.width() * video_stream_info.height();
134 return (pixels > max_sd_pixels) ? KeySource::TRACK_TYPE_HD
135 : KeySource::TRACK_TYPE_SD;
140 Segmenter::Segmenter(
const MuxerOptions& options,
141 scoped_ptr<FileType> ftyp,
142 scoped_ptr<Movie> moov)
146 moof_(new MovieFragment()),
147 fragment_buffer_(new BufferWriter()),
148 sidx_(new SegmentIndex()),
149 muxer_listener_(NULL),
150 progress_listener_(NULL),
152 accumulated_progress_(0),
153 sample_duration_(0u) {}
155 Segmenter::~Segmenter() { STLDeleteElements(&fragmenters_); }
161 uint32_t max_sd_pixels,
162 double clear_lead_in_seconds,
163 double crypto_period_duration_in_seconds,
164 FourCC protection_scheme) {
165 DCHECK_LT(0u, streams.size());
166 muxer_listener_ = muxer_listener;
167 progress_listener_ = progress_listener;
168 moof_->header.sequence_number = 0;
170 moof_->tracks.resize(streams.size());
171 segment_durations_.resize(streams.size());
172 fragmenters_.resize(streams.size());
173 const bool key_rotation_enabled = crypto_period_duration_in_seconds != 0;
174 const bool kInitialEncryptionInfo =
true;
176 for (uint32_t i = 0; i < streams.size(); ++i) {
177 stream_map_[streams[i]] = i;
178 moof_->tracks[i].header.track_id = i + 1;
179 if (streams[i]->info()->stream_type() == kStreamVideo) {
181 if (sidx_->reference_id == 0)
182 sidx_->reference_id = i + 1;
184 if (!encryption_key_source) {
185 fragmenters_[i] =
new Fragmenter(&moof_->tracks[i]);
189 FourCC local_protection_scheme = protection_scheme;
190 if (streams[i]->info()->stream_type() != kStreamVideo) {
193 if (protection_scheme == FOURCC_cbcs)
194 local_protection_scheme = FOURCC_cbc1;
195 else if (protection_scheme == FOURCC_cens)
196 local_protection_scheme = FOURCC_cenc;
199 KeySource::TrackType track_type =
200 GetTrackTypeForEncryption(*streams[i]->info(), max_sd_pixels);
202 moov_->tracks[i].media.information.sample_table.description;
204 if (key_rotation_enabled) {
207 encryption_key.key_id.assign(
208 kKeyRotationDefaultKeyId,
209 kKeyRotationDefaultKeyId + arraysize(kKeyRotationDefaultKeyId));
211 &encryption_key.iv)) {
212 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
214 GenerateEncryptedSampleEntry(encryption_key, clear_lead_in_seconds,
215 local_protection_scheme, &description);
216 if (muxer_listener_) {
217 muxer_listener_->OnEncryptionInfoReady(
218 kInitialEncryptionInfo, local_protection_scheme,
219 encryption_key.key_id, encryption_key.iv,
220 encryption_key.key_system_info);
224 moof_.get(), streams[i]->info(), &moof_->tracks[i],
225 encryption_key_source, track_type,
226 crypto_period_duration_in_seconds * streams[i]->info()->time_scale(),
227 clear_lead_in_seconds * streams[i]->info()->time_scale(),
228 local_protection_scheme, GetCryptByteBlock(local_protection_scheme),
229 GetSkipByteBlock(local_protection_scheme), muxer_listener_);
233 scoped_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
235 encryption_key_source->
GetKey(track_type, encryption_key.get());
238 if (encryption_key->iv.empty()) {
240 &encryption_key->iv)) {
241 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
245 GenerateEncryptedSampleEntry(*encryption_key, clear_lead_in_seconds,
246 local_protection_scheme, &description);
248 if (moov_->pssh.empty()) {
249 moov_->pssh.resize(encryption_key->key_system_info.size());
250 for (
size_t i = 0; i < encryption_key->key_system_info.size(); i++) {
251 moov_->pssh[i].raw_box = encryption_key->key_system_info[i].CreateBox();
254 if (muxer_listener_) {
255 muxer_listener_->OnEncryptionInfoReady(
256 kInitialEncryptionInfo, local_protection_scheme,
257 encryption_key->key_id, encryption_key->iv,
258 encryption_key->key_system_info);
263 streams[i]->info(), &moof_->tracks[i], encryption_key.Pass(),
264 clear_lead_in_seconds * streams[i]->info()->time_scale(),
265 local_protection_scheme, GetCryptByteBlock(local_protection_scheme),
266 GetSkipByteBlock(local_protection_scheme));
270 if (sidx_->reference_id == 0)
271 sidx_->reference_id = 1;
272 sidx_->timescale = streams[GetReferenceStreamId()]->info()->time_scale();
275 progress_target_ = streams[GetReferenceStreamId()]->info()->duration();
278 moov_->header.timescale = sidx_->timescale;
279 moof_->header.sequence_number = 1;
282 moov_->metadata.handler.handler_type = FOURCC_ID32;
283 moov_->metadata.id3v2.language.code =
"eng";
284 moov_->metadata.id3v2.private_frame.owner =
285 "https://github.com/google/edash-packager";
287 return DoInitialize();
291 for (std::vector<Fragmenter*>::iterator it = fragmenters_.begin();
292 it != fragmenters_.end();
294 Status status = FinalizeFragment(
true, *it);
302 for (std::vector<Track>::iterator track = moov_->tracks.begin();
303 track != moov_->tracks.end();
305 track->header.duration = Rescale(track->media.header.duration,
306 track->media.header.timescale,
307 moov_->header.timescale);
308 if (track->header.duration > moov_->header.duration)
309 moov_->header.duration = track->header.duration;
311 moov_->extends.header.fragment_duration = moov_->header.duration;
317 scoped_refptr<MediaSample> sample) {
320 DCHECK(stream_map_.find(stream) != stream_map_.end());
321 uint32_t stream_id = stream_map_[stream];
322 Fragmenter* fragmenter = fragmenters_[stream_id];
325 if (moov_->extends.tracks[stream_id].default_sample_duration == 0) {
326 moov_->extends.tracks[stream_id].default_sample_duration =
330 if (fragmenter->fragment_finalized()) {
331 return Status(error::FRAGMENT_FINALIZED,
332 "Current fragment is finalized already.");
335 bool finalize_fragment =
false;
336 if (fragmenter->fragment_duration() >=
339 finalize_fragment =
true;
342 bool finalize_segment =
false;
343 if (segment_durations_[stream_id] >=
346 finalize_segment =
true;
347 finalize_fragment =
true;
352 if (finalize_fragment) {
353 status = FinalizeFragment(finalize_segment, fragmenter);
362 if (sample_duration_ == 0)
363 sample_duration_ = sample->duration();
364 moov_->tracks[stream_id].media.header.duration += sample->duration();
365 segment_durations_[stream_id] += sample->duration();
366 DCHECK_GE(segment_durations_[stream_id], fragmenter->fragment_duration());
370 uint32_t Segmenter::GetReferenceTimeScale()
const {
371 return moov_->header.timescale;
375 if (moov_->header.timescale == 0) {
380 return static_cast<double>(moov_->header.duration) / moov_->header.timescale;
384 accumulated_progress_ += progress;
386 if (!progress_listener_)
return;
387 if (progress_target_ == 0)
return;
391 if (accumulated_progress_ >= progress_target_) {
394 progress_listener_->
OnProgress(static_cast<double>(accumulated_progress_) /
399 void Segmenter::SetComplete() {
400 if (!progress_listener_)
return;
404 Status Segmenter::FinalizeSegment() {
405 Status status = DoFinalizeSegment();
408 sidx_->references.clear();
409 std::vector<uint64_t>::iterator it = segment_durations_.begin();
410 for (; it != segment_durations_.end(); ++it)
416 uint32_t Segmenter::GetReferenceStreamId() {
418 return sidx_->reference_id - 1;
421 Status Segmenter::FinalizeFragment(
bool finalize_segment,
422 Fragmenter* fragmenter) {
423 fragmenter->FinalizeFragment();
426 for (std::vector<Fragmenter*>::iterator it = fragmenters_.begin();
427 it != fragmenters_.end();
429 if (!(*it)->fragment_finalized())
436 uint64_t data_offset = moof_->ComputeSize() + mdat.HeaderSize();
438 uint64_t next_traf_position = moof_->HeaderSize() + moof_->header.box_size();
439 for (
size_t i = 0; i < moof_->tracks.size(); ++i) {
440 TrackFragment& traf = moof_->tracks[i];
441 if (traf.auxiliary_offset.offsets.size() > 0) {
442 DCHECK_EQ(traf.auxiliary_offset.offsets.size(), 1u);
443 DCHECK(!traf.sample_encryption.sample_encryption_entries.empty());
445 next_traf_position += traf.box_size();
448 traf.auxiliary_offset.offsets[0] =
449 next_traf_position - traf.sample_encryption.box_size() +
450 traf.sample_encryption.HeaderSize() +
453 traf.runs[0].data_offset = data_offset + mdat.data_size;
454 mdat.data_size += fragmenters_[i]->data()->Size();
458 sidx_->references.resize(sidx_->references.size() + 1);
459 fragmenters_[GetReferenceStreamId()]->GenerateSegmentReference(
460 &sidx_->references[sidx_->references.size() - 1]);
461 sidx_->references[sidx_->references.size() - 1].referenced_size =
462 data_offset + mdat.data_size;
465 moof_->Write(fragment_buffer_.get());
466 mdat.WriteHeader(fragment_buffer_.get());
467 for (Fragmenter* fragmenter : fragmenters_)
468 fragment_buffer_->AppendBuffer(*fragmenter->data());
471 ++moof_->header.sequence_number;
473 if (finalize_segment)
474 return FinalizeSegment();