5 #include "packager/media/formats/wvm/wvm_media_parser.h"
11 #include "packager/base/stl_util.h"
12 #include "packager/base/strings/string_number_conversions.h"
13 #include "packager/media/base/aes_encryptor.h"
14 #include "packager/media/base/audio_stream_info.h"
15 #include "packager/media/base/key_source.h"
16 #include "packager/media/base/media_sample.h"
17 #include "packager/media/base/status.h"
18 #include "packager/media/base/video_stream_info.h"
19 #include "packager/media/filters/avc_decoder_configuration.h"
20 #include "packager/media/formats/mp2t/adts_header.h"
21 #include "packager/media/formats/mp4/aac_audio_specific_config.h"
22 #include "packager/media/formats/mp4/es_descriptor.h"
24 #define HAS_HEADER_EXTENSION(x) ((x != 0xBC) && (x != 0xBE) && (x != 0xBF) \
25 && (x != 0xF0) && (x != 0xF2) && (x != 0xF8) \
29 const uint32_t kMpeg2ClockRate = 90000;
30 const uint32_t kPesOptPts = 0x80;
31 const uint32_t kPesOptDts = 0x40;
32 const uint32_t kPesOptAlign = 0x04;
33 const uint32_t kPsmStreamId = 0xBC;
34 const uint32_t kPaddingStreamId = 0xBE;
35 const uint32_t kIndexMagic = 0x49444d69;
36 const uint32_t kIndexStreamId = 0xBF;
37 const uint32_t kIndexVersion4HeaderSize = 12;
38 const uint32_t kEcmStreamId = 0xF0;
39 const uint32_t kV2MetadataStreamId = 0xF1;
40 const uint32_t kScramblingBitsMask = 0x30;
41 const uint32_t kStartCode1 = 0x00;
42 const uint32_t kStartCode2 = 0x00;
43 const uint32_t kStartCode3 = 0x01;
44 const uint32_t kStartCode4Pack = 0xBA;
45 const uint32_t kStartCode4System = 0xBB;
46 const uint32_t kStartCode4ProgramEnd = 0xB9;
47 const uint32_t kPesStreamIdVideoMask = 0xF0;
48 const uint32_t kPesStreamIdVideo = 0xE0;
49 const uint32_t kPesStreamIdAudioMask = 0xE0;
50 const uint32_t kPesStreamIdAudio = 0xC0;
51 const uint32_t kVersion4 = 4;
52 const int kAdtsHeaderMinSize = 7;
53 const uint8_t kAacSampleSizeBits = 16;
55 const uint8_t kNaluLengthSize = 4;
58 const uint32_t kDefaultSamplingFrequency = 100;
59 const uint16_t kEcmSizeBytes = 80;
60 const uint32_t kInitializationVectorSizeBytes = 16;
62 const uint32_t kEcmContentKeySizeBytes = 16;
63 const uint32_t kEcmDCPFlagsSizeBytes = 3;
64 const uint32_t kEcmCCIFlagsSizeBytes = 1;
65 const uint32_t kEcmFlagsSizeBytes =
66 kEcmCCIFlagsSizeBytes + kEcmDCPFlagsSizeBytes;
67 const uint32_t kEcmPaddingSizeBytes = 12;
68 const uint32_t kAssetKeySizeBytes = 16;
70 const uint8_t kDefaultAudioStreamId = kPesStreamIdAudio;
71 const uint8_t kDefaultVideoStreamId = kPesStreamIdVideo;
88 namespace edash_packager {
92 WvmMediaParser::WvmMediaParser()
93 : is_initialized_(false),
94 parse_state_(StartCode1),
97 metadata_is_complete_(false),
98 current_program_id_(0),
100 prev_pes_stream_id_(0),
101 pes_packet_bytes_(0),
104 prev_pes_flags_1_(0),
105 pes_header_data_bytes_(0),
109 index_program_id_(0),
111 crypto_unit_start_pos_(0),
113 decryption_key_source_(NULL) {
116 WvmMediaParser::~WvmMediaParser() {}
118 void WvmMediaParser::Init(
const InitCB& init_cb,
119 const NewSampleCB& new_sample_cb,
121 DCHECK(!is_initialized_);
122 DCHECK(!init_cb.is_null());
123 DCHECK(!new_sample_cb.is_null());
124 decryption_key_source_ = decryption_key_source;
126 new_sample_cb_ = new_sample_cb;
129 bool WvmMediaParser::Parse(
const uint8_t* buf,
int size) {
130 uint32_t num_bytes, prev_size;
131 num_bytes = prev_size = 0;
132 const uint8_t* read_ptr = buf;
133 const uint8_t* end = read_ptr + size;
135 while (read_ptr < end) {
136 switch (parse_state_) {
138 if (*read_ptr == kStartCode1) {
139 parse_state_ = StartCode2;
143 if (*read_ptr == kStartCode2) {
144 parse_state_ = StartCode3;
146 parse_state_ = StartCode1;
150 if (*read_ptr == kStartCode3) {
151 parse_state_ = StartCode4;
153 parse_state_ = StartCode1;
158 case kStartCode4Pack:
159 parse_state_ = PackHeader1;
161 case kStartCode4System:
162 parse_state_ = SystemHeader1;
164 case kStartCode4ProgramEnd:
165 parse_state_ = ProgramEnd;
168 parse_state_ = PesStreamId;
173 parse_state_ = PackHeader2;
176 parse_state_ = PackHeader3;
179 parse_state_ = PackHeader4;
182 parse_state_ = PackHeader5;
185 parse_state_ = PackHeader6;
188 parse_state_ = PackHeader7;
191 parse_state_ = PackHeader8;
194 parse_state_ = PackHeader9;
197 parse_state_ = PackHeader10;
200 skip_bytes_ = *read_ptr & 0x07;
201 parse_state_ = PackHeaderStuffingSkip;
204 skip_bytes_ = *read_ptr;
206 parse_state_ = SystemHeader2;
209 skip_bytes_ |= *read_ptr;
210 parse_state_ = SystemHeaderSkip;
212 case PackHeaderStuffingSkip:
213 if ((end - read_ptr) >= (int32_t)skip_bytes_) {
214 read_ptr += skip_bytes_;
216 parse_state_ = StartCode1;
218 skip_bytes_ -= (end - read_ptr);
222 case SystemHeaderSkip:
223 if ((end - read_ptr) >= (int32_t)skip_bytes_) {
224 read_ptr += skip_bytes_;
226 parse_state_ = StartCode1;
228 uint32_t remaining_size = end - read_ptr;
229 skip_bytes_ -= remaining_size;
234 pes_stream_id_ = *read_ptr;
235 if (!metadata_is_complete_ &&
236 (pes_stream_id_ != kPsmStreamId) &&
237 (pes_stream_id_ != kIndexStreamId) &&
238 (pes_stream_id_ != kEcmStreamId) &&
239 (pes_stream_id_ != kV2MetadataStreamId) &&
240 (pes_stream_id_ != kPaddingStreamId)) {
241 metadata_is_complete_ =
true;
243 parse_state_ = PesPacketLength1;
245 case PesPacketLength1:
246 pes_packet_bytes_ = *read_ptr;
247 pes_packet_bytes_ <<= 8;
248 parse_state_ = PesPacketLength2;
250 case PesPacketLength2:
251 pes_packet_bytes_ |= *read_ptr;
252 if (HAS_HEADER_EXTENSION(pes_stream_id_)) {
253 parse_state_ = PesExtension1;
255 pes_flags_1_ = pes_flags_2_ = 0;
256 pes_header_data_bytes_ = 0;
257 parse_state_ = PesPayload;
261 prev_pes_flags_1_ = pes_flags_1_;
262 pes_flags_1_ = *read_ptr;
264 parse_state_ = PesExtension2;
267 pes_flags_2_ = *read_ptr;
269 parse_state_ = PesExtension3;
272 pes_header_data_bytes_ = *read_ptr;
274 if (pes_flags_2_ & kPesOptPts) {
277 parse_state_ = PesHeaderData;
281 timestamp_ = (*read_ptr & 0x0E);
282 --pes_header_data_bytes_;
288 timestamp_ |= *read_ptr;
289 --pes_header_data_bytes_;
295 timestamp_ |= *read_ptr >> 1;
296 --pes_header_data_bytes_;
302 timestamp_ |= *read_ptr;
303 --pes_header_data_bytes_;
309 timestamp_ |= *read_ptr >> 1;
311 --pes_header_data_bytes_;
313 if (pes_flags_2_ & kPesOptDts) {
317 parse_state_ = PesHeaderData;
321 timestamp_ = (*read_ptr & 0x0E);
322 --pes_header_data_bytes_;
328 timestamp_ |= *read_ptr;
329 --pes_header_data_bytes_;
335 timestamp_ |= *read_ptr >> 1;
336 --pes_header_data_bytes_;
342 timestamp_ |= *read_ptr;
343 --pes_header_data_bytes_;
349 timestamp_ |= *read_ptr >> 1;
351 --pes_header_data_bytes_;
353 parse_state_ = PesHeaderData;
356 num_bytes = end - read_ptr;
357 if (num_bytes >= pes_header_data_bytes_) {
358 num_bytes = pes_header_data_bytes_;
359 parse_state_ = PesPayload;
361 pes_header_data_bytes_ -= num_bytes;
362 pes_packet_bytes_ -= num_bytes;
363 read_ptr += num_bytes;
366 switch (pes_stream_id_) {
369 parse_state_ = PsmPayload;
371 case kPaddingStreamId:
372 parse_state_ = Padding;
376 parse_state_ = EcmPayload;
379 parse_state_ = IndexPayload;
382 if (!DemuxNextPes(
false)) {
385 parse_state_ = EsPayload;
389 num_bytes = end - read_ptr;
390 if (num_bytes >= pes_packet_bytes_) {
391 num_bytes = pes_packet_bytes_;
392 parse_state_ = StartCode1;
395 pes_packet_bytes_ -= num_bytes;
396 prev_size = psm_data_.size();
397 psm_data_.resize(prev_size + num_bytes);
398 memcpy(&psm_data_[prev_size], read_ptr, num_bytes);
400 read_ptr += num_bytes;
403 num_bytes = end - read_ptr;
404 if (num_bytes >= pes_packet_bytes_) {
405 num_bytes = pes_packet_bytes_;
406 parse_state_ = StartCode1;
409 pes_packet_bytes_ -= num_bytes;
410 prev_size = ecm_.size();
411 ecm_.resize(prev_size + num_bytes);
412 memcpy(&ecm_[prev_size], read_ptr, num_bytes);
414 if ((pes_packet_bytes_ == 0) && !ecm_.empty()) {
419 read_ptr += num_bytes;
422 num_bytes = end - read_ptr;
423 if (num_bytes >= pes_packet_bytes_) {
424 num_bytes = pes_packet_bytes_;
425 parse_state_ = StartCode1;
428 pes_packet_bytes_ -= num_bytes;
429 prev_size = index_data_.size();
430 index_data_.resize(prev_size + num_bytes);
431 memcpy(&index_data_[prev_size], read_ptr, num_bytes);
433 if (pes_packet_bytes_ == 0 && !index_data_.empty()) {
434 if (!metadata_is_complete_) {
435 if (!ParseIndexEntry()) {
440 read_ptr += num_bytes;
443 num_bytes = end - read_ptr;
444 if (num_bytes >= pes_packet_bytes_) {
445 num_bytes = pes_packet_bytes_;
446 parse_state_ = StartCode1;
448 pes_packet_bytes_ -= num_bytes;
449 if (pes_stream_id_ != kV2MetadataStreamId) {
450 sample_data_.resize(sample_data_.size() + num_bytes);
451 memcpy(&sample_data_[sample_data_.size() - num_bytes], read_ptr,
454 prev_pes_stream_id_ = pes_stream_id_;
455 read_ptr += num_bytes;
458 num_bytes = end - read_ptr;
459 if (num_bytes >= pes_packet_bytes_) {
460 num_bytes = pes_packet_bytes_;
461 parse_state_ = StartCode1;
463 pes_packet_bytes_ -= num_bytes;
464 read_ptr += num_bytes;
467 parse_state_ = StartCode1;
468 metadata_is_complete_ =
true;
469 if (!DemuxNextPes(
true)) {
475 parse_state_ = StartCode1;
476 prev_media_sample_data_.Reset();
477 current_program_id_++;
490 bool WvmMediaParser::EmitLastSample(uint32_t stream_id,
491 scoped_refptr<MediaSample>& new_sample) {
492 std::string key = base::UintToString(current_program_id_)
494 .append(base::UintToString(stream_id));
495 std::map<std::string, uint32_t>::iterator it =
496 program_demux_stream_map_.find(key);
497 if (it == program_demux_stream_map_.end())
499 return EmitSample(stream_id, (*it).second, new_sample,
true);
502 bool WvmMediaParser::EmitPendingSamples() {
504 while (!media_sample_queue_.empty()) {
505 DemuxStreamIdMediaSample& demux_stream_media_sample =
506 media_sample_queue_.front();
507 if (!EmitSample(demux_stream_media_sample.parsed_audio_or_video_stream_id,
508 demux_stream_media_sample.demux_stream_id,
509 demux_stream_media_sample.media_sample,
513 media_sample_queue_.pop_front();
518 void WvmMediaParser::Flush() {
521 if (prev_media_sample_data_.audio_sample != NULL) {
522 if (!EmitLastSample(prev_pes_stream_id_,
523 prev_media_sample_data_.audio_sample)) {
524 LOG(ERROR) <<
"Did not emit last sample for audio stream with ID = "
525 << prev_pes_stream_id_;
528 if (prev_media_sample_data_.video_sample != NULL) {
529 if (!EmitLastSample(prev_pes_stream_id_,
530 prev_media_sample_data_.video_sample)) {
531 LOG(ERROR) <<
"Did not emit last sample for video stream with ID = "
532 << prev_pes_stream_id_;
537 bool WvmMediaParser::ParseIndexEntry() {
540 if (current_program_id_ > 0) {
543 uint32_t index_size = 0;
544 if (index_data_.size() < kIndexVersion4HeaderSize) {
548 const uint8_t* read_ptr = vector_as_array(&index_data_);
549 if (ntohlFromBuffer(read_ptr) != kIndexMagic) {
555 uint32_t version = ntohlFromBuffer(read_ptr);
557 if (version == kVersion4) {
558 index_size = kIndexVersion4HeaderSize + ntohlFromBuffer(read_ptr);
559 if (index_data_.size() < index_size) {
563 read_ptr +=
sizeof(uint32_t);
566 uint32_t index_metadata_max_size = index_size - kIndexVersion4HeaderSize;
567 if (index_metadata_max_size <
sizeof(uint8_t)) {
572 uint64_t track_duration = 0;
573 int16_t trick_play_rate = 0;
574 uint32_t sampling_frequency = kDefaultSamplingFrequency;
575 uint32_t time_scale = kMpeg2ClockRate;
576 uint16_t video_width = 0;
577 uint16_t video_height = 0;
578 uint32_t pixel_width = 0;
579 uint32_t pixel_height = 0;
580 uint8_t nalu_length_size = kNaluLengthSize;
581 uint8_t num_channels = 0;
582 int audio_pes_stream_id = 0;
583 int video_pes_stream_id = 0;
584 bool has_video =
false;
585 bool has_audio =
false;
586 std::vector<uint8_t> audio_codec_config;
587 std::vector<uint8_t> video_codec_config;
588 uint8_t num_index_entries = *read_ptr;
590 --index_metadata_max_size;
592 for (uint8_t idx = 0; idx < num_index_entries; ++idx) {
593 if (index_metadata_max_size < (2 *
sizeof(uint8_t)) +
sizeof(uint32_t)) {
596 uint8_t tag = *read_ptr;
598 uint8_t type = *read_ptr;
600 uint32_t length = ntohlFromBuffer(read_ptr);
601 read_ptr +=
sizeof(uint32_t);
602 index_metadata_max_size -= (2 *
sizeof(uint8_t)) +
sizeof(uint32_t);
603 if (index_metadata_max_size < length) {
608 std::vector<uint8_t> binary_data;
609 switch (Type(type)) {
611 if (length ==
sizeof(uint8_t)) {
612 tagtype = GetTag(tag, length, read_ptr, &value);
618 if (length ==
sizeof(int8_t)) {
619 tagtype = GetTag(tag, length, read_ptr, &value);
625 if (length ==
sizeof(uint16_t)) {
626 tagtype = GetTag(tag, length, read_ptr, &value);
632 if (length ==
sizeof(int16_t)) {
633 tagtype = GetTag(tag, length, read_ptr, &value);
639 if (length ==
sizeof(uint32_t)) {
640 tagtype = GetTag(tag, length, read_ptr, &value);
646 if (length ==
sizeof(int32_t)) {
647 tagtype = GetTag(tag, length, read_ptr, &value);
653 if (length ==
sizeof(uint64_t)) {
654 tagtype = GetTag(tag, length, read_ptr, &value);
660 if (length ==
sizeof(int64_t)) {
661 tagtype = GetTag(tag, length, read_ptr, &value);
667 case Type_BinaryData:
668 binary_data.assign(read_ptr, read_ptr + length);
677 track_duration = value;
679 case TrackTrickPlayRate:
680 trick_play_rate = value;
683 video_pes_stream_id = value;
686 audio_pes_stream_id = value;
689 video_width = (uint16_t)value;
692 video_height = (uint16_t)value;
694 case AudioNumChannels:
695 num_channels = (uint8_t)value;
703 case VideoPixelWidth:
704 pixel_width =
static_cast<uint32_t
>(value);
706 case VideoPixelHeight:
707 pixel_height =
static_cast<uint32_t
>(value);
709 case Audio_EsDescriptor: {
710 mp4::ESDescriptor descriptor;
711 if (!descriptor.Parse(binary_data)) {
713 "Could not extract AudioSpecificConfig from ES_Descriptor";
716 audio_codec_config = descriptor.decoder_specific_info();
719 case Audio_EC3SpecificData:
720 case Audio_DtsSpecificData:
721 case Audio_AC3SpecificData:
722 LOG(ERROR) <<
"Audio type not supported.";
724 case AVCDecoderConfigurationRecord:
725 video_codec_config = binary_data;
732 index_metadata_max_size -= length;
735 index_size = read_ptr - vector_as_array(&index_data_);
738 VideoCodec video_codec = kCodecH264;
739 stream_infos_.push_back(
new VideoStreamInfo(
740 stream_id_count_, time_scale, track_duration, video_codec,
741 std::string(), std::string(), video_width, video_height,
742 pixel_width, pixel_height, trick_play_rate, nalu_length_size,
743 vector_as_array(&video_codec_config), video_codec_config.size(),
745 program_demux_stream_map_[base::UintToString(index_program_id_) +
":" +
746 base::UintToString(video_pes_stream_id ?
747 video_pes_stream_id :
748 kDefaultVideoStreamId)] =
752 AudioCodec audio_codec = kCodecAAC;
753 stream_infos_.push_back(
new AudioStreamInfo(
754 stream_id_count_, time_scale, track_duration, audio_codec,
755 std::string(), std::string(), kAacSampleSizeBits, num_channels,
756 sampling_frequency, vector_as_array(&audio_codec_config),
757 audio_codec_config.size(),
true));
758 program_demux_stream_map_[base::UintToString(index_program_id_) +
":" +
759 base::UintToString(audio_pes_stream_id ?
760 audio_pes_stream_id :
761 kDefaultAudioStreamId)] =
771 bool WvmMediaParser::DemuxNextPes(
bool is_program_end) {
772 bool output_encrypted_sample =
false;
773 if (!sample_data_.empty() && (prev_pes_flags_1_ & kScramblingBitsMask)) {
775 if (!content_decryptor_) {
776 output_encrypted_sample =
true;
778 content_decryptor_->Decrypt(&sample_data_[crypto_unit_start_pos_],
779 sample_data_.size() - crypto_unit_start_pos_,
780 &sample_data_[crypto_unit_start_pos_]);
785 if ((pes_flags_2_ & kPesOptPts) || is_program_end) {
786 if (!sample_data_.empty()) {
787 if (!Output(output_encrypted_sample)) {
791 StartMediaSampleDemux();
794 crypto_unit_start_pos_ = sample_data_.size();
798 void WvmMediaParser::StartMediaSampleDemux() {
799 bool is_key_frame = ((pes_flags_1_ & kPesOptAlign) != 0);
801 media_sample_->set_dts(dts_);
802 media_sample_->set_pts(pts_);
803 media_sample_->set_is_key_frame(is_key_frame);
805 sample_data_.clear();
808 bool WvmMediaParser::Output(
bool output_encrypted_sample) {
809 if (output_encrypted_sample) {
810 media_sample_->set_data(vector_as_array(&sample_data_),
811 sample_data_.size());
812 media_sample_->set_is_encrypted(
true);
814 if ((prev_pes_stream_id_ & kPesStreamIdVideoMask) == kPesStreamIdVideo) {
816 std::vector<uint8_t> nal_unit_stream;
817 if (!byte_to_unit_stream_converter_.ConvertByteStreamToNalUnitStream(
818 vector_as_array(&sample_data_), sample_data_.size(),
820 LOG(ERROR) <<
"Could not convert h.264 byte stream sample";
823 media_sample_->set_data(nal_unit_stream.data(), nal_unit_stream.size());
824 if (!is_initialized_) {
827 std::vector<uint8_t> decoder_config_record;
828 byte_to_unit_stream_converter_.GetAVCDecoderConfigurationRecord(
829 &decoder_config_record);
830 for (uint32_t i = 0; i < stream_infos_.size(); i++) {
831 if (stream_infos_[i]->stream_type() == media::kStreamVideo &&
832 stream_infos_[i]->codec_string().empty()) {
833 const std::vector<uint8_t>* stream_config;
834 if (stream_infos_[i]->extra_data().empty()) {
837 stream_infos_[i]->set_extra_data(decoder_config_record);
838 stream_config = &decoder_config_record;
841 stream_config = &stream_infos_[i]->extra_data();
843 DCHECK(stream_config);
845 VideoStreamInfo* video_stream_info =
846 reinterpret_cast<VideoStreamInfo*
>(stream_infos_[i].get());
847 AVCDecoderConfiguration avc_config;
848 if (!avc_config.Parse(*stream_config)) {
849 LOG(WARNING) <<
"Failed to parse AVCDecoderConfigurationRecord. "
850 "Using computed configuration record instead.";
851 video_stream_info->set_extra_data(decoder_config_record);
852 if (!avc_config.Parse(decoder_config_record)) {
853 LOG(ERROR) <<
"Failed to parse AVCDecoderConfigurationRecord.";
857 video_stream_info->set_codec_string(avc_config.GetCodecString());
859 if (avc_config.pixel_width() != video_stream_info->pixel_width() ||
860 avc_config.pixel_height() !=
861 video_stream_info->pixel_height()) {
862 LOG_IF(WARNING, video_stream_info->pixel_width() != 0 ||
863 video_stream_info->pixel_height() != 0)
864 <<
"Pixel aspect ratio in WVM metadata ("
865 << video_stream_info->pixel_width() <<
","
866 << video_stream_info->pixel_height()
867 <<
") does not match with SAR in "
868 "AVCDecoderConfigurationRecord ("
869 << avc_config.pixel_width() <<
","
870 << avc_config.pixel_height()
871 <<
"). Use AVCDecoderConfigurationRecord.";
872 video_stream_info->set_pixel_width(avc_config.pixel_width());
873 video_stream_info->set_pixel_height(avc_config.pixel_height());
875 if (avc_config.coded_width() != video_stream_info->width() ||
876 avc_config.coded_height() != video_stream_info->height()) {
877 LOG(WARNING) <<
"Resolution in WVM metadata ("
878 << video_stream_info->width() <<
","
879 << video_stream_info->height()
880 <<
") does not match with resolution in "
881 "AVCDecoderConfigurationRecord ("
882 << avc_config.coded_width() <<
","
883 << avc_config.coded_height()
884 <<
"). Use AVCDecoderConfigurationRecord.";
885 video_stream_info->set_width(avc_config.coded_width());
886 video_stream_info->set_height(avc_config.coded_height());
891 }
else if ((prev_pes_stream_id_ & kPesStreamIdAudioMask) ==
895 vector_as_array(&sample_data_), kAdtsHeaderMinSize);
896 media::mp2t::AdtsHeader adts_header;
897 const uint8_t* frame_ptr = vector_as_array(&sample_data_);
898 if (!adts_header.Parse(frame_ptr, frame_size)) {
899 LOG(ERROR) <<
"Could not parse ADTS header";
902 size_t header_size = adts_header.GetAdtsHeaderSize(frame_ptr,
904 media_sample_->set_data(frame_ptr + header_size,
905 frame_size - header_size);
906 if (!is_initialized_) {
907 for (uint32_t i = 0; i < stream_infos_.size(); i++) {
908 if (stream_infos_[i]->stream_type() == media::kStreamAudio &&
909 stream_infos_[i]->codec_string().empty()) {
910 AudioStreamInfo* audio_stream_info =
911 reinterpret_cast<AudioStreamInfo*
>(stream_infos_[i].get());
912 if (audio_stream_info->extra_data().empty()) {
915 audio_stream_info->set_sampling_frequency(
916 adts_header.GetSamplingFrequency());
917 std::vector<uint8_t> audio_specific_config;
918 if (!adts_header.GetAudioSpecificConfig(&audio_specific_config)) {
919 LOG(ERROR) <<
"Could not compute AACaudiospecificconfig";
922 audio_stream_info->set_extra_data(audio_specific_config);
923 audio_stream_info->set_codec_string(
925 kCodecAAC, adts_header.GetObjectType()));
929 mp4::AACAudioSpecificConfig aac_config;
930 if (!aac_config.Parse(stream_infos_[i]->extra_data())) {
931 LOG(ERROR) <<
"Could not parse AACAudioSpecificconfig";
934 audio_stream_info->set_sampling_frequency(aac_config.frequency());
935 audio_stream_info->set_codec_string(
937 kCodecAAC, aac_config.audio_object_type()));
945 if (!is_initialized_) {
946 bool all_streams_have_config =
true;
948 for (uint32_t i = 0; i < stream_infos_.size(); i++) {
949 if (stream_infos_[i]->codec_string().empty()) {
950 all_streams_have_config =
false;
954 if (all_streams_have_config) {
955 init_cb_.Run(stream_infos_);
956 is_initialized_ =
true;
960 DCHECK_GT(media_sample_->data_size(), 0UL);
961 std::string key = base::UintToString(current_program_id_).append(
":")
962 .append(base::UintToString(prev_pes_stream_id_));
963 std::map<std::string, uint32_t>::iterator it =
964 program_demux_stream_map_.find(key);
965 if (it == program_demux_stream_map_.end()) {
970 DemuxStreamIdMediaSample demux_stream_media_sample;
971 demux_stream_media_sample.parsed_audio_or_video_stream_id =
973 demux_stream_media_sample.demux_stream_id = (*it).second;
974 demux_stream_media_sample.media_sample = media_sample_;
976 if (!is_initialized_) {
977 media_sample_queue_.push_back(demux_stream_media_sample);
980 while (!media_sample_queue_.empty()) {
981 if (!EmitPendingSamples())
985 if (!EmitSample(prev_pes_stream_id_, (*it).second, media_sample_,
false))
991 bool WvmMediaParser::EmitSample(uint32_t parsed_audio_or_video_stream_id,
993 scoped_refptr<MediaSample>& new_sample,
997 if ((parsed_audio_or_video_stream_id & kPesStreamIdVideoMask) ==
999 new_sample->set_duration(prev_media_sample_data_.video_sample_duration);
1000 }
else if ((parsed_audio_or_video_stream_id & kPesStreamIdAudioMask) ==
1001 kPesStreamIdAudio) {
1002 new_sample->set_duration(prev_media_sample_data_.audio_sample_duration);
1004 if (!new_sample_cb_.Run(stream_id, new_sample)) {
1005 LOG(ERROR) <<
"Failed to process the last sample.";
1013 if ((parsed_audio_or_video_stream_id & kPesStreamIdVideoMask) ==
1014 kPesStreamIdVideo) {
1015 if (prev_media_sample_data_.video_sample == NULL) {
1016 prev_media_sample_data_.video_sample = new_sample;
1017 prev_media_sample_data_.video_stream_id = stream_id;
1020 prev_media_sample_data_.video_sample->set_duration(
1021 new_sample->dts() - prev_media_sample_data_.video_sample->dts());
1022 prev_media_sample_data_.video_sample_duration =
1023 prev_media_sample_data_.video_sample->duration();
1024 if (!new_sample_cb_.Run(prev_media_sample_data_.video_stream_id,
1025 prev_media_sample_data_.video_sample)) {
1026 LOG(ERROR) <<
"Failed to process the video sample.";
1029 prev_media_sample_data_.video_sample = new_sample;
1030 prev_media_sample_data_.video_stream_id = stream_id;
1031 }
else if ((parsed_audio_or_video_stream_id & kPesStreamIdAudioMask) ==
1032 kPesStreamIdAudio) {
1033 if (prev_media_sample_data_.audio_sample == NULL) {
1034 prev_media_sample_data_.audio_sample = new_sample;
1035 prev_media_sample_data_.audio_stream_id = stream_id;
1038 prev_media_sample_data_.audio_sample->set_duration(
1039 new_sample->dts() - prev_media_sample_data_.audio_sample->dts());
1040 prev_media_sample_data_.audio_sample_duration =
1041 prev_media_sample_data_.audio_sample->duration();
1042 if (!new_sample_cb_.Run(prev_media_sample_data_.audio_stream_id,
1043 prev_media_sample_data_.audio_sample)) {
1044 LOG(ERROR) <<
"Failed to process the audio sample.";
1047 prev_media_sample_data_.audio_sample = new_sample;
1048 prev_media_sample_data_.audio_stream_id = stream_id;
1053 bool WvmMediaParser::GetAssetKey(
const uint32_t asset_id,
1054 EncryptionKey* encryption_key) {
1055 DCHECK(decryption_key_source_);
1056 Status status = decryption_key_source_->FetchKeys(asset_id);
1058 LOG(ERROR) <<
"Fetch Key(s) failed for AssetID = " << asset_id
1059 <<
", error = " << status;
1063 status = decryption_key_source_->GetKey(KeySource::TRACK_TYPE_HD,
1066 LOG(ERROR) <<
"Fetch Key(s) failed for AssetID = " << asset_id
1067 <<
", error = " << status;
1074 bool WvmMediaParser::ProcessEcm() {
1076 if (!decryption_key_source_)
1079 if (current_program_id_ > 0) {
1082 if (ecm_.size() != kEcmSizeBytes) {
1083 LOG(ERROR) <<
"Unexpected ECM size = " << ecm_.size()
1084 <<
", expected size = " << kEcmSizeBytes;
1087 const uint8_t* ecm_data = ecm_.data();
1089 ecm_data +=
sizeof(uint32_t);
1090 ecm_data +=
sizeof(uint32_t);
1091 ecm_data +=
sizeof(uint32_t);
1092 uint32_t asset_id = ntohlFromBuffer(ecm_data);
1093 if (asset_id == 0) {
1094 LOG(ERROR) <<
"AssetID in ECM is not valid.";
1097 ecm_data +=
sizeof(uint32_t);
1098 EncryptionKey encryption_key;
1099 if (!GetAssetKey(asset_id, &encryption_key)) {
1102 if (encryption_key.key.size() < kAssetKeySizeBytes) {
1103 LOG(ERROR) <<
"Asset Key size of " << encryption_key.key.size()
1104 <<
" for AssetID = " << asset_id
1105 <<
" is less than minimum asset key size.";
1111 std::vector<uint8_t> asset_key(
1112 encryption_key.key.begin(),
1113 encryption_key.key.begin() + kAssetKeySizeBytes);
1114 std::vector<uint8_t> iv(kInitializationVectorSizeBytes);
1115 AesCbcCtsDecryptor asset_decryptor;
1116 if (!asset_decryptor.InitializeWithIv(asset_key, iv)) {
1117 LOG(ERROR) <<
"Failed to initialize asset_decryptor.";
1121 const size_t content_key_buffer_size =
1122 kEcmFlagsSizeBytes + kEcmContentKeySizeBytes +
1123 kEcmPaddingSizeBytes;
1124 std::vector<uint8_t> content_key_buffer(content_key_buffer_size);
1125 asset_decryptor.Decrypt(
1126 ecm_data, content_key_buffer_size, vector_as_array(&content_key_buffer));
1128 std::vector<uint8_t> decrypted_content_key_vec(
1129 content_key_buffer.begin() + 4,
1130 content_key_buffer.begin() + 20);
1131 scoped_ptr<AesCbcCtsDecryptor> content_decryptor(
new AesCbcCtsDecryptor);
1132 if (!content_decryptor->InitializeWithIv(decrypted_content_key_vec, iv)) {
1133 LOG(ERROR) <<
"Failed to initialize content decryptor.";
1137 content_decryptor_ = content_decryptor.Pass();
1141 DemuxStreamIdMediaSample::DemuxStreamIdMediaSample() :
1143 parsed_audio_or_video_stream_id(0) {}
1145 DemuxStreamIdMediaSample::~DemuxStreamIdMediaSample() {}
1147 PrevSampleData::PrevSampleData() {
1151 PrevSampleData::~PrevSampleData() {}
1153 void PrevSampleData::Reset() {
1154 audio_sample = NULL;
1155 video_sample = NULL;
1156 audio_stream_id = 0;
1157 video_stream_id = 0;
1158 audio_sample_duration = 0;
1159 video_sample_duration = 0;