diff --git a/media/formats/mp2t/es_parser_adts.cc b/media/formats/mp2t/es_parser_adts.cc index 63aa3acdff860f..7d682c2c5a441d 100644 --- a/media/formats/mp2t/es_parser_adts.cc +++ b/media/formats/mp2t/es_parser_adts.cc @@ -122,7 +122,7 @@ EsParserAdts::EsParserAdts(const NewAudioConfigCB& new_audio_config_cb, emit_buffer_cb_(emit_buffer_cb), #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) get_decrypt_config_cb_(), - use_hls_sample_aes_(false), + init_encryption_scheme_(EncryptionScheme::kUnencrypted), #endif sbr_in_mimetype_(sbr_in_mimetype) { } @@ -131,15 +131,13 @@ EsParserAdts::EsParserAdts(const NewAudioConfigCB& new_audio_config_cb, EsParserAdts::EsParserAdts(const NewAudioConfigCB& new_audio_config_cb, const EmitBufferCB& emit_buffer_cb, const GetDecryptConfigCB& get_decrypt_config_cb, - bool use_hls_sample_aes, + EncryptionScheme init_encryption_scheme, bool sbr_in_mimetype) : new_audio_config_cb_(new_audio_config_cb), emit_buffer_cb_(emit_buffer_cb), get_decrypt_config_cb_(get_decrypt_config_cb), - use_hls_sample_aes_(use_hls_sample_aes), - sbr_in_mimetype_(sbr_in_mimetype) { - DCHECK_EQ(!!get_decrypt_config_cb_, use_hls_sample_aes_); -} + init_encryption_scheme_(init_encryption_scheme), + sbr_in_mimetype_(sbr_in_mimetype) {} #endif EsParserAdts::~EsParserAdts() { @@ -210,7 +208,7 @@ bool EsParserAdts::ParseFromEsQueue() { DecodeTimestamp::FromPresentationTime(current_pts)); stream_parser_buffer->set_duration(frame_duration); #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) - if (use_hls_sample_aes_) { + if (get_decrypt_config_cb_) { const DecryptConfig* base_decrypt_config = get_decrypt_config_cb_.Run(); if (base_decrypt_config) { std::vector subsamples; @@ -262,9 +260,7 @@ bool EsParserAdts::UpdateAudioConfiguration(const uint8_t* adts_header, : orig_sample_rate; EncryptionScheme scheme = EncryptionScheme::kUnencrypted; #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) - if (use_hls_sample_aes_) { - scheme = EncryptionScheme::kCbcs; - } + scheme = init_encryption_scheme_; #endif AudioDecoderConfig audio_decoder_config( kCodecAAC, kSampleFormatS16, channel_layout, extended_samples_per_second, diff --git a/media/formats/mp2t/es_parser_adts.h b/media/formats/mp2t/es_parser_adts.h index 61b368ed14c377..c004469f8bfe29 100644 --- a/media/formats/mp2t/es_parser_adts.h +++ b/media/formats/mp2t/es_parser_adts.h @@ -40,7 +40,7 @@ class MEDIA_EXPORT EsParserAdts : public EsParser { EsParserAdts(const NewAudioConfigCB& new_audio_config_cb, const EmitBufferCB& emit_buffer_cb, const GetDecryptConfigCB& get_decrypt_config_cb, - bool use_hls_sample_aes, + EncryptionScheme init_encryption_scheme, bool sbr_in_mimetype); #endif @@ -82,9 +82,9 @@ class MEDIA_EXPORT EsParserAdts : public EsParser { NewAudioConfigCB new_audio_config_cb_; EmitBufferCB emit_buffer_cb_; #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) - // - to obtain the current decrypt_config. Only called if use_hls_sample_aes_. + // - to obtain the current decrypt_config. GetDecryptConfigCB get_decrypt_config_cb_; - bool use_hls_sample_aes_; + const EncryptionScheme init_encryption_scheme_; #endif // True when AAC SBR extension is signalled in the mimetype diff --git a/media/formats/mp2t/es_parser_h264.cc b/media/formats/mp2t/es_parser_h264.cc index fe03ac6d7b8abc..5532d8f2bfb320 100644 --- a/media/formats/mp2t/es_parser_h264.cc +++ b/media/formats/mp2t/es_parser_h264.cc @@ -185,7 +185,7 @@ EsParserH264::EsParserH264(const NewVideoConfigCB& new_video_config_cb, next_access_unit_pos_(0) #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) , - use_hls_sample_aes_(false), + init_encryption_scheme_(EncryptionScheme::kUnencrypted), get_decrypt_config_cb_() #endif { @@ -194,16 +194,14 @@ EsParserH264::EsParserH264(const NewVideoConfigCB& new_video_config_cb, #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) EsParserH264::EsParserH264(const NewVideoConfigCB& new_video_config_cb, const EmitBufferCB& emit_buffer_cb, - bool use_hls_sample_aes, + EncryptionScheme init_encryption_scheme, const GetDecryptConfigCB& get_decrypt_config_cb) : es_adapter_(new_video_config_cb, emit_buffer_cb), h264_parser_(new H264Parser()), current_access_unit_pos_(0), next_access_unit_pos_(0), - use_hls_sample_aes_(use_hls_sample_aes), - get_decrypt_config_cb_(get_decrypt_config_cb) { - DCHECK_EQ(!!get_decrypt_config_cb_, use_hls_sample_aes_); -} + init_encryption_scheme_(init_encryption_scheme), + get_decrypt_config_cb_(get_decrypt_config_cb) {} #endif EsParserH264::~EsParserH264() { @@ -360,7 +358,7 @@ bool EsParserH264::ParseFromEsQueue() { #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) // With HLS SampleAES, protected blocks in H.264 consist of IDR and non- // IDR slices that are more than 48 bytes in length. - if (use_hls_sample_aes_ && + if (get_decrypt_config_cb_ && get_decrypt_config_cb_.Run() && nalu.size > kSampleAESMaxUnprotectedNALULength) { int64_t nal_begin = nalu.data - es; protected_blocks_.Add(nal_begin, nal_begin + nalu.size); @@ -420,10 +418,7 @@ bool EsParserH264::EmitFrame(int64_t access_unit_pos, return false; EncryptionScheme scheme = EncryptionScheme::kUnencrypted; #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) - if (use_hls_sample_aes_) { - // Note that for SampleAES the (encrypt,skip) pattern is constant. - scheme = EncryptionScheme::kCbcs; - } + scheme = init_encryption_scheme_; #endif RCHECK(UpdateVideoDecoderConfig(sps, scheme)); } @@ -438,14 +433,12 @@ bool EsParserH264::EmitFrame(int64_t access_unit_pos, #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) const DecryptConfig* base_decrypt_config = nullptr; - if (use_hls_sample_aes_) { - DCHECK(get_decrypt_config_cb_); + if (get_decrypt_config_cb_) base_decrypt_config = get_decrypt_config_cb_.Run(); - } std::unique_ptr adjusted_au; std::vector subsamples; - if (use_hls_sample_aes_ && base_decrypt_config) { + if (base_decrypt_config) { adjusted_au = AdjustAUForSampleAES(es, &access_unit_size, protected_blocks_, &subsamples); protected_blocks_.clear(); @@ -462,7 +455,7 @@ bool EsParserH264::EmitFrame(int64_t access_unit_pos, stream_parser_buffer->SetDecodeTimestamp(current_timing_desc.dts); stream_parser_buffer->set_timestamp(current_timing_desc.pts); #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) - if (use_hls_sample_aes_ && base_decrypt_config) { + if (base_decrypt_config) { switch (base_decrypt_config->encryption_scheme()) { case EncryptionScheme::kUnencrypted: // As |base_decrypt_config| is specified, the stream is encrypted, diff --git a/media/formats/mp2t/es_parser_h264.h b/media/formats/mp2t/es_parser_h264.h index 98e6ff0581d65e..76b37dc8c1355a 100644 --- a/media/formats/mp2t/es_parser_h264.h +++ b/media/formats/mp2t/es_parser_h264.h @@ -47,7 +47,7 @@ class MEDIA_EXPORT EsParserH264 : public EsParser { #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) EsParserH264(const NewVideoConfigCB& new_video_config_cb, const EmitBufferCB& emit_buffer_cb, - bool use_hls_sample_aes, + EncryptionScheme init_encryption_scheme, const GetDecryptConfigCB& get_decrypt_config_cb); #endif ~EsParserH264() override; @@ -87,9 +87,8 @@ class MEDIA_EXPORT EsParserH264 : public EsParser { int64_t current_access_unit_pos_; int64_t next_access_unit_pos_; #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) - bool use_hls_sample_aes_; + const EncryptionScheme init_encryption_scheme_; // Callback to obtain the current decrypt_config. - // Only called if use_hls_sample_aes_ is true. GetDecryptConfigCB get_decrypt_config_cb_; Ranges protected_blocks_; #endif diff --git a/media/formats/mp2t/mp2t_stream_parser.cc b/media/formats/mp2t/mp2t_stream_parser.cc index 1d550875339399..2c7165d2cf46c4 100644 --- a/media/formats/mp2t/mp2t_stream_parser.cc +++ b/media/formats/mp2t/mp2t_stream_parser.cc @@ -433,28 +433,35 @@ bool Mp2tStreamParser::ShouldForceEncryptedParser() { } std::unique_ptr Mp2tStreamParser::CreateEncryptedH264Parser( - int pes_pid) { + int pes_pid, + bool emit_clear_buffers) { auto on_video_config_changed = base::Bind( &Mp2tStreamParser::OnVideoConfigChanged, base::Unretained(this), pes_pid); auto on_emit_video_buffer = base::Bind(&Mp2tStreamParser::OnEmitVideoBuffer, base::Unretained(this), pes_pid); auto get_decrypt_config = - base::Bind(&Mp2tStreamParser::GetDecryptConfig, base::Unretained(this)); + emit_clear_buffers ? EsParser::GetDecryptConfigCB() + : base::Bind(&Mp2tStreamParser::GetDecryptConfig, + base::Unretained(this)); return std::make_unique( - on_video_config_changed, on_emit_video_buffer, true, get_decrypt_config); + on_video_config_changed, on_emit_video_buffer, initial_encryption_scheme_, + get_decrypt_config); } std::unique_ptr Mp2tStreamParser::CreateEncryptedAacParser( - int pes_pid) { + int pes_pid, + bool emit_clear_buffers) { auto on_audio_config_changed = base::Bind( &Mp2tStreamParser::OnAudioConfigChanged, base::Unretained(this), pes_pid); auto on_emit_audio_buffer = base::Bind(&Mp2tStreamParser::OnEmitAudioBuffer, base::Unretained(this), pes_pid); auto get_decrypt_config = - base::Bind(&Mp2tStreamParser::GetDecryptConfig, base::Unretained(this)); + emit_clear_buffers ? EsParser::GetDecryptConfigCB() + : base::Bind(&Mp2tStreamParser::GetDecryptConfig, + base::Unretained(this)); return std::make_unique( - on_audio_config_changed, on_emit_audio_buffer, get_decrypt_config, true, - sbr_in_mimetype_); + on_audio_config_changed, on_emit_audio_buffer, get_decrypt_config, + initial_encryption_scheme_, sbr_in_mimetype_); } #endif @@ -478,7 +485,8 @@ void Mp2tStreamParser::RegisterPes(int pes_pid, is_audio = false; #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) if (ShouldForceEncryptedParser()) { - es_parser = CreateEncryptedH264Parser(pes_pid); + es_parser = + CreateEncryptedH264Parser(pes_pid, true /* emit_clear_buffers */); break; } #endif @@ -488,7 +496,8 @@ void Mp2tStreamParser::RegisterPes(int pes_pid, case kStreamTypeAAC: #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) if (ShouldForceEncryptedParser()) { - es_parser = CreateEncryptedAacParser(pes_pid); + es_parser = + CreateEncryptedAacParser(pes_pid, true /* emit_clear_buffers */); break; } #endif @@ -505,7 +514,8 @@ void Mp2tStreamParser::RegisterPes(int pes_pid, if (descriptors.HasPrivateDataIndicator( kSampleAESPrivateDataIndicatorAVC)) { is_audio = false; - es_parser = CreateEncryptedH264Parser(pes_pid); + es_parser = + CreateEncryptedH264Parser(pes_pid, false /* emit_clear_buffers */); } else { VLOG(2) << "HLS: stream_type in PMT indicates AVC with Sample-AES, but " << "corresponding private data indicator is not present."; @@ -515,7 +525,8 @@ void Mp2tStreamParser::RegisterPes(int pes_pid, case kStreamTypeAACWithSampleAES: if (descriptors.HasPrivateDataIndicator( kSampleAESPrivateDataIndicatorAAC)) { - es_parser = CreateEncryptedAacParser(pes_pid); + es_parser = + CreateEncryptedAacParser(pes_pid, false /* emit_clear_buffers */); } else { VLOG(2) << "HLS: stream_type in PMT indicates AAC with Sample-AES, but " << "corresponding private data indicator is not present."; diff --git a/media/formats/mp2t/mp2t_stream_parser.h b/media/formats/mp2t/mp2t_stream_parser.h index 81f127fb430489..f0e8356d4edf2e 100644 --- a/media/formats/mp2t/mp2t_stream_parser.h +++ b/media/formats/mp2t/mp2t_stream_parser.h @@ -109,8 +109,10 @@ class MEDIA_EXPORT Mp2tStreamParser : public StreamParser { #if BUILDFLAG(ENABLE_HLS_SAMPLE_AES) bool ShouldForceEncryptedParser(); - std::unique_ptr CreateEncryptedH264Parser(int pes_pid); - std::unique_ptr CreateEncryptedAacParser(int pes_pid); + std::unique_ptr CreateEncryptedH264Parser(int pes_pid, + bool emit_clear_buffers); + std::unique_ptr CreateEncryptedAacParser(int pes_pid, + bool emit_clear_buffers); std::unique_ptr MakeCatPidState(); void UnregisterCat();