shaka-project / shaka-packager

A media packaging and development framework for VOD and Live DASH and HLS applications, supporting Common Encryption for Widevine and other DRM Systems.
https://shaka-project.github.io/shaka-packager/
Other
1.9k stars 496 forks source link

Packaging fails on MPEG-TS with SAMPLE-AES #1316

Closed chenda6 closed 4 months ago

chenda6 commented 6 months ago

System info

macOS Monterey 12.6 (cmake) https://github.com/shaka-project/shaka-packager/tree/cmake-porting-history

Issue and steps to reproduce the problem

Packager Command:

packager \                                                                                
        in=input.mp4,stream=video,segment_template=\$Number\$.ts,drm_label=sd \
        --enable_raw_key_encryption \                                                     
        --keys label=sd:key_id=abba271e8bcf552bbd2e86a434a9a5d9:key=69eaa802a6763af979e8d1940fb88392:iv=22334455667788990011223344556677 \
        --protection_scheme cbcs \                                                        
        --protection_systems FairPlay \                                                   
        --hls_master_playlist_output h264_master.m3u8 \                                   
        --hls_key_uri skd://testAssetID  

What is the expected result? MPEG-TS segments successfully encrypted and packaged.

What happens instead? Packager terminates early with following stack trace:

E1208 10:04:23.204043       2 aes_encryptor.cc:145] Expecting output size of at least 32 bytes.
F1208 10:04:23.204063       2 encryption_handler.cc:400] Check failed: encryptor_->Crypt(source, source_size, dest, &dest_size) 
*** Check failure stack trace: ***
    @        0x1085cef60  absl::lts_20230125::log_internal::LogMessage::PrepareToDie()
    @        0x1085ce818  absl::lts_20230125::log_internal::LogMessage::SendToLog()
    @        0x1085cdb04  absl::lts_20230125::log_internal::LogMessage::Flush()
    @        0x1085cf42c  absl::lts_20230125::log_internal::LogMessageFatal::~LogMessageFatal()
    @        0x1085cf47c  absl::lts_20230125::log_internal::LogMessageFatal::~LogMessageFatal()
    @        0x107d41800  shaka::media::EncryptionHandler::EncryptBytes()
    @        0x107d40280  shaka::media::EncryptionHandler::ProcessMediaSample()
    @        0x107d3f148  shaka::media::EncryptionHandler::Process()
    @        0x108090244  shaka::media::MediaHandler::Dispatch()
    @        0x107c9fa28  shaka::media::MediaHandler::DispatchMediaSample()
    @        0x107c9ef50  shaka::media::ChunkingHandler::OnMediaSample()
    @        0x107c9e120  shaka::media::ChunkingHandler::Process()
    @        0x108090244  shaka::media::MediaHandler::Dispatch()
    @        0x107c9fa28  shaka::media::MediaHandler::DispatchMediaSample()
    @        0x107d5cfbc  shaka::media::Demuxer::PushMediaSample()
    @        0x107d5c510  shaka::media::Demuxer::NewMediaSampleEvent()
    @        0x107d69514  _ZNSt3__1L8__invokeIRMN5shaka5media7DemuxerEFbjNS_10shared_ptrINS2_11MediaSampleEEEERPS3_JjS6_EvEEDTcldsdescT0_fp0_fp_spscT1_fp1_EEOT_OSC_DpOSD_
    @        0x107d69428  std::__1::__apply_functor<>()
    @        0x107d693ac  std::__1::__bind<>::operator()<>()
    @        0x107d6932c  _ZNSt3__1L8__invokeIRNS_6__bindIMN5shaka5media7DemuxerEFbjNS_10shared_ptrINS3_11MediaSampleEEEEJPS4_RKNS_12placeholders4__phILi1EEERKNSC_ILi2EEEEEEJjS7_EEEDTclscT_fp_spscT0_fp0_EEOSL_DpOSM_
    @        0x107d692d0  std::__1::__invoke_void_return_wrapper<>::__call<>()
    @        0x107d69250  std::__1::__function::__alloc_func<>::operator()()
    @        0x107d67fc0  std::__1::__function::__func<>::operator()()
    @        0x107e867e0  std::__1::__function::__value_func<>::operator()()
    @        0x107e7e3d4  std::__1::function<>::operator()()
    @        0x107e78a78  shaka::media::mp4::MP4MediaParser::EnqueueSample()
    @        0x107e778d4  shaka::media::mp4::MP4MediaParser::Parse()
    @        0x107d5b0ac  shaka::media::Demuxer::Parse()
    @        0x107d59f08  shaka::media::Demuxer::Run()
    @        0x107bed964  shaka::media::Job::Run()
    @        0x107bf0a84  _ZNSt3__1L8__invokeIMN5shaka5media3JobEFRKNS1_6StatusEvEPS3_JEvEEDTcldsdescT0_fp0_fp_spscT1_fp1_EEOT_OSA_DpOSB_
    @        0x107bf09cc  std::__1::__thread_execute<>()
    @        0x107bf0128  std::__1::__thread_proxy<>()
    @        0x197a8826c  _pthread_start
    @        0x197a8308c  thread_start

NOTE: Using packager version 2.6.1 works. This appears to be a regression related to https://github.com/shaka-project/shaka-packager/pull/1119/files

synologic commented 6 months ago

Confirmed, I am experiencing the same situation this effectively breaks Fairplay encryption. OS is Linux, packager version 3e71302ba4-release

synologic commented 6 months ago

At line 145, the check should be: if (*ciphertext_size < required_ciphertext_size - AES_BLOCK_SIZE)

due to RequiredOutputSize function stating: // mbedtls requires a buffer large enough for one extra block.

So required_ciphertext_size is 32 bytes due to mbedtls requirement whereas the actual ciphertext_size is 16 bytes

cosmin commented 6 months ago

@chenda6 can you try this with the fix-integration-tests branch which contains the totality of the 5 most recent pull requests I opened. Specifically the fixes in #1323 should resolve this specific issue.