7 #include "packager/media/formats/mp4/segmenter.h"
11 #include "packager/base/logging.h"
12 #include "packager/media/base/aes_cryptor.h"
13 #include "packager/media/base/buffer_writer.h"
14 #include "packager/media/base/key_source.h"
15 #include "packager/media/base/media_sample.h"
16 #include "packager/media/base/media_stream.h"
17 #include "packager/media/base/muxer_options.h"
18 #include "packager/media/base/muxer_util.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"
24 #include "packager/version/version.h"
31 const size_t kCencKeyIdSize = 16u;
34 const int kCencSchemeVersion = 0x00010000;
37 const uint8_t kKeyRotationDefaultKeyId[] = {
38 0, 0, 0, 0, 0, 0, 0, 0,
39 0, 0, 0, 0, 0, 0, 0, 0
43 struct ProtectionPattern {
44 uint8_t crypt_byte_block;
45 uint8_t skip_byte_block;
48 static_assert(arraysize(kKeyRotationDefaultKeyId) == kCencKeyIdSize,
49 "cenc_key_id_must_be_size_16");
51 uint64_t Rescale(uint64_t time_in_old_scale,
54 return static_cast<double>(time_in_old_scale) / old_scale * new_scale;
57 ProtectionPattern GetProtectionPattern(FourCC protection_scheme,
58 TrackType track_type) {
59 ProtectionPattern pattern;
60 if (protection_scheme != FOURCC_cbcs && protection_scheme != FOURCC_cens) {
62 pattern.crypt_byte_block = 0u;
63 pattern.skip_byte_block = 0u;
64 }
else if (track_type != kVideo) {
72 pattern.crypt_byte_block = 1u;
73 pattern.skip_byte_block = 0u;
76 const uint8_t kCryptByteBlock = 1u;
77 const uint8_t kSkipByteBlock = 9u;
78 pattern.crypt_byte_block = kCryptByteBlock;
79 pattern.skip_byte_block = kSkipByteBlock;
84 void GenerateSinf(
const EncryptionKey& encryption_key,
86 FourCC protection_scheme,
87 ProtectionPattern pattern,
88 ProtectionSchemeInfo* sinf) {
89 sinf->format.format = old_type;
91 DCHECK_NE(protection_scheme, FOURCC_NULL);
92 sinf->type.type = protection_scheme;
93 sinf->type.version = kCencSchemeVersion;
95 auto& track_encryption = sinf->info.track_encryption;
96 track_encryption.default_is_protected = 1;
97 DCHECK(!encryption_key.iv.empty());
98 if (protection_scheme == FOURCC_cbcs) {
101 track_encryption.default_per_sample_iv_size = 0;
102 track_encryption.default_constant_iv = encryption_key.iv;
104 track_encryption.default_per_sample_iv_size =
105 static_cast<uint8_t
>(encryption_key.iv.size());
107 track_encryption.default_crypt_byte_block = pattern.crypt_byte_block;
108 track_encryption.default_skip_byte_block = pattern.skip_byte_block;
109 track_encryption.default_kid = encryption_key.key_id;
112 void GenerateEncryptedSampleEntry(
const EncryptionKey& encryption_key,
113 double clear_lead_in_seconds,
114 FourCC protection_scheme,
115 ProtectionPattern pattern,
116 SampleDescription* description) {
118 if (description->type == kVideo) {
119 DCHECK_EQ(1u, description->video_entries.size());
122 if (clear_lead_in_seconds > 0)
123 description->video_entries.push_back(description->video_entries[0]);
126 VideoSampleEntry& entry = description->video_entries[0];
127 GenerateSinf(encryption_key, entry.format, protection_scheme, pattern,
129 entry.format = FOURCC_encv;
131 DCHECK_EQ(kAudio, description->type);
132 DCHECK_EQ(1u, description->audio_entries.size());
135 if (clear_lead_in_seconds > 0)
136 description->audio_entries.push_back(description->audio_entries[0]);
139 AudioSampleEntry& entry = description->audio_entries[0];
140 GenerateSinf(encryption_key, entry.format, protection_scheme, pattern,
142 entry.format = FOURCC_enca;
148 Segmenter::Segmenter(
const MuxerOptions& options,
149 std::unique_ptr<FileType> ftyp,
150 std::unique_ptr<Movie> moov)
152 ftyp_(std::move(ftyp)),
153 moov_(std::move(moov)),
154 moof_(new MovieFragment()),
155 fragment_buffer_(new BufferWriter()),
156 sidx_(new SegmentIndex()),
157 muxer_listener_(NULL),
158 progress_listener_(NULL),
160 accumulated_progress_(0),
161 sample_duration_(0u) {}
163 Segmenter::~Segmenter() {}
169 uint32_t max_sd_pixels,
170 uint32_t max_hd_pixels,
171 uint32_t max_uhd1_pixels,
172 double clear_lead_in_seconds,
173 double crypto_period_duration_in_seconds,
174 FourCC protection_scheme) {
175 DCHECK_LT(0u, streams.size());
176 muxer_listener_ = muxer_listener;
177 progress_listener_ = progress_listener;
178 moof_->header.sequence_number = 0;
180 moof_->tracks.resize(streams.size());
181 segment_durations_.resize(streams.size());
182 fragmenters_.resize(streams.size());
183 const bool key_rotation_enabled = crypto_period_duration_in_seconds != 0;
184 const bool kInitialEncryptionInfo =
true;
186 for (uint32_t i = 0; i < streams.size(); ++i) {
187 stream_map_[streams[i]] = i;
188 moof_->tracks[i].header.track_id = i + 1;
189 if (streams[i]->info()->stream_type() == kStreamVideo) {
191 if (sidx_->reference_id == 0)
192 sidx_->reference_id = i + 1;
194 if (!encryption_key_source) {
195 fragmenters_[i].reset(
196 new Fragmenter(streams[i]->info(), &moof_->tracks[i]));
200 KeySource::TrackType track_type =
201 GetTrackTypeForEncryption(*streams[i]->info(), max_sd_pixels,
202 max_hd_pixels, max_uhd1_pixels);
204 moov_->tracks[i].media.information.sample_table.description;
205 ProtectionPattern pattern =
206 GetProtectionPattern(protection_scheme, description.type);
208 if (key_rotation_enabled) {
211 encryption_key.key_id.assign(
212 kKeyRotationDefaultKeyId,
213 kKeyRotationDefaultKeyId + arraysize(kKeyRotationDefaultKeyId));
215 &encryption_key.iv)) {
216 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
218 GenerateEncryptedSampleEntry(encryption_key, clear_lead_in_seconds,
219 protection_scheme, pattern, &description);
220 if (muxer_listener_) {
221 muxer_listener_->OnEncryptionInfoReady(
222 kInitialEncryptionInfo, protection_scheme, encryption_key.key_id,
223 encryption_key.iv, encryption_key.key_system_info);
227 moof_.get(), streams[i]->info(), &moof_->tracks[i],
228 encryption_key_source, track_type,
229 crypto_period_duration_in_seconds * streams[i]->info()->time_scale(),
230 clear_lead_in_seconds * streams[i]->info()->time_scale(),
231 protection_scheme, pattern.crypt_byte_block, pattern.skip_byte_block,
236 std::unique_ptr<EncryptionKey> encryption_key(
new EncryptionKey());
238 encryption_key_source->
GetKey(track_type, encryption_key.get());
241 if (encryption_key->iv.empty()) {
243 &encryption_key->iv)) {
244 return Status(error::INTERNAL_ERROR,
"Failed to generate random iv.");
248 GenerateEncryptedSampleEntry(*encryption_key, clear_lead_in_seconds,
249 protection_scheme, pattern, &description);
251 if (moov_->pssh.empty()) {
252 moov_->pssh.resize(encryption_key->key_system_info.size());
253 for (
size_t i = 0; i < encryption_key->key_system_info.size(); i++) {
254 moov_->pssh[i].raw_box = encryption_key->key_system_info[i].CreateBox();
257 if (muxer_listener_) {
258 muxer_listener_->OnEncryptionInfoReady(
259 kInitialEncryptionInfo, protection_scheme, encryption_key->key_id,
260 encryption_key->iv, encryption_key->key_system_info);
265 streams[i]->info(), &moof_->tracks[i], std::move(encryption_key),
266 clear_lead_in_seconds * streams[i]->info()->time_scale(),
267 protection_scheme, pattern.crypt_byte_block, pattern.skip_byte_block,
272 for (uint32_t i = 0; i < streams.size(); ++i)
273 fragmenters_[i]->set_use_decoding_timestamp_in_timeline(
true);
277 if (sidx_->reference_id == 0)
278 sidx_->reference_id = 1;
279 sidx_->timescale = streams[GetReferenceStreamId()]->info()->time_scale();
282 progress_target_ = streams[GetReferenceStreamId()]->info()->duration();
285 moov_->header.timescale = sidx_->timescale;
286 moof_->header.sequence_number = 1;
289 const std::string version = GetPackagerVersion();
290 if (!version.empty()) {
291 moov_->metadata.handler.handler_type = FOURCC_ID32;
292 moov_->metadata.id3v2.language.code =
"eng";
293 moov_->metadata.id3v2.private_frame.owner = GetPackagerProjectUrl();
294 moov_->metadata.id3v2.private_frame.value = version;
296 return DoInitialize();
300 for (
const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_) {
301 Status status = FinalizeFragment(
true, fragmenter.get());
309 for (std::vector<Track>::iterator track = moov_->tracks.begin();
310 track != moov_->tracks.end();
312 track->header.duration = Rescale(track->media.header.duration,
313 track->media.header.timescale,
314 moov_->header.timescale);
315 if (track->header.duration > moov_->header.duration)
316 moov_->header.duration = track->header.duration;
318 moov_->extends.header.fragment_duration = moov_->header.duration;
324 scoped_refptr<MediaSample> sample) {
327 DCHECK(stream_map_.find(stream) != stream_map_.end());
328 uint32_t stream_id = stream_map_[stream];
329 Fragmenter* fragmenter = fragmenters_[stream_id].get();
332 if (moov_->extends.tracks[stream_id].default_sample_duration == 0) {
333 moov_->extends.tracks[stream_id].default_sample_duration =
337 if (fragmenter->fragment_finalized()) {
338 return Status(error::FRAGMENT_FINALIZED,
339 "Current fragment is finalized already.");
342 bool finalize_fragment =
false;
343 if (fragmenter->fragment_duration() >=
346 finalize_fragment =
true;
349 bool finalize_segment =
false;
350 if (segment_durations_[stream_id] >=
353 finalize_segment =
true;
354 finalize_fragment =
true;
359 if (finalize_fragment) {
360 status = FinalizeFragment(finalize_segment, fragmenter);
369 if (sample_duration_ == 0)
370 sample_duration_ = sample->duration();
371 moov_->tracks[stream_id].media.header.duration += sample->duration();
372 segment_durations_[stream_id] += sample->duration();
373 DCHECK_GE(segment_durations_[stream_id], fragmenter->fragment_duration());
377 uint32_t Segmenter::GetReferenceTimeScale()
const {
378 return moov_->header.timescale;
382 if (moov_->header.timescale == 0) {
387 return static_cast<double>(moov_->header.duration) / moov_->header.timescale;
391 accumulated_progress_ += progress;
393 if (!progress_listener_)
return;
394 if (progress_target_ == 0)
return;
398 if (accumulated_progress_ >= progress_target_) {
401 progress_listener_->
OnProgress(static_cast<double>(accumulated_progress_) /
406 void Segmenter::SetComplete() {
407 if (!progress_listener_)
return;
411 Status Segmenter::FinalizeSegment() {
412 Status status = DoFinalizeSegment();
415 sidx_->references.clear();
416 std::vector<uint64_t>::iterator it = segment_durations_.begin();
417 for (; it != segment_durations_.end(); ++it)
423 uint32_t Segmenter::GetReferenceStreamId() {
425 return sidx_->reference_id - 1;
428 Status Segmenter::FinalizeFragment(
bool finalize_segment,
429 Fragmenter* fragmenter) {
430 fragmenter->FinalizeFragment();
433 for (
const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_) {
434 if (!fragmenter->fragment_finalized())
441 uint64_t data_offset = moof_->ComputeSize() + mdat.HeaderSize();
443 uint64_t next_traf_position = moof_->HeaderSize() + moof_->header.box_size();
444 for (
size_t i = 0; i < moof_->tracks.size(); ++i) {
445 TrackFragment& traf = moof_->tracks[i];
446 if (traf.auxiliary_offset.offsets.size() > 0) {
447 DCHECK_EQ(traf.auxiliary_offset.offsets.size(), 1u);
448 DCHECK(!traf.sample_encryption.sample_encryption_entries.empty());
450 next_traf_position += traf.box_size();
453 traf.auxiliary_offset.offsets[0] =
454 next_traf_position - traf.sample_encryption.box_size() +
455 traf.sample_encryption.HeaderSize() +
458 traf.runs[0].data_offset = data_offset + mdat.data_size;
459 mdat.data_size +=
static_cast<uint32_t
>(fragmenters_[i]->data()->Size());
463 sidx_->references.resize(sidx_->references.size() + 1);
464 fragmenters_[GetReferenceStreamId()]->GenerateSegmentReference(
465 &sidx_->references[sidx_->references.size() - 1]);
466 sidx_->references[sidx_->references.size() - 1].referenced_size =
467 data_offset + mdat.data_size;
470 moof_->Write(fragment_buffer_.get());
471 mdat.WriteHeader(fragment_buffer_.get());
472 for (
const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_)
473 fragment_buffer_->AppendBuffer(*fragmenter->data());
476 ++moof_->header.sequence_number;
478 if (finalize_segment)
479 return FinalizeSegment();