From a9e7ca4691fa60739cc8e49f7af00e9099eff643 Mon Sep 17 00:00:00 2001 From: CastagnaIT Date: Wed, 30 Oct 2024 11:54:19 +0100 Subject: [PATCH] [FragmentedSampleReader] Check default_isProtected before add senc --- src/common/AdaptiveUtils.cpp | 5 ++++- src/samplereader/FragmentedSampleReader.cpp | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/common/AdaptiveUtils.cpp b/src/common/AdaptiveUtils.cpp index 63024cffd..c6976a831 100644 --- a/src/common/AdaptiveUtils.cpp +++ b/src/common/AdaptiveUtils.cpp @@ -133,7 +133,10 @@ AP4_Movie* PLAYLIST::CreateMovieAtom(adaptive::AdaptiveStream& adStream, defaultKid = DRM::ConvertKidStrToBytes(psshSet.defaultKID_); AP4_ContainerAtom schi{AP4_ATOM_TYPE_SCHI}; - schi.AddChild(new AP4_TencAtom(AP4_CENC_CIPHER_AES_128_CTR, 8, defaultKid.data())); + // Note TENC default_isProtected parameter is intentionally set to 0 (not encrypted) + // this is to prevent CFragmentedSampleReader::ProcessMoof from trying to create a SENC atom + // and so avoid decrypting with a CAdaptiveCencSampleDecrypter + schi.AddChild(new AP4_TencAtom(0, 8, defaultKid.data())); sampleDesc = new AP4_ProtectedSampleDescription(0, sampleDesc, 0, AP4_PROTECTION_SCHEME_TYPE_PIFF, 0, "", &schi); } diff --git a/src/samplereader/FragmentedSampleReader.cpp b/src/samplereader/FragmentedSampleReader.cpp index d323145b3..72bffc23f 100644 --- a/src/samplereader/FragmentedSampleReader.cpp +++ b/src/samplereader/FragmentedSampleReader.cpp @@ -388,10 +388,24 @@ AP4_Result CFragmentedSampleReader::ProcessMoof(AP4_ContainerAtom* moof, if (!m_protectedDesc || !traf) return AP4_ERROR_INVALID_FORMAT; + bool isDefaultProtected{true}; + AP4_ContainerAtom* schi = m_protectedDesc->GetSchemeInfo()->GetSchiAtom(); + if (schi) + { + AP4_CencTrackEncryption* trackEnc = + AP4_DYNAMIC_CAST(AP4_CencTrackEncryption, schi->GetChild(AP4_ATOM_TYPE_TENC)); + if (!trackEnc) + trackEnc = AP4_DYNAMIC_CAST(AP4_CencTrackEncryption, + schi->GetChild(AP4_UUID_PIFF_TRACK_ENCRYPTION_ATOM)); + if (trackEnc) + isDefaultProtected = trackEnc->GetDefaultIsProtected() != 0; + } + // If the boxes saiz, saio, senc are missing, the stream does not conform to the specs and // may not be decrypted, so try create an empty senc where all samples will use the same default IV - if (!traf->GetChild(AP4_ATOM_TYPE_SAIO) && !traf->GetChild(AP4_ATOM_TYPE_SAIZ) && - !traf->GetChild(AP4_ATOM_TYPE_SENC)) + if (isDefaultProtected && !traf->GetChild(AP4_ATOM_TYPE_SAIO) && + !traf->GetChild(AP4_ATOM_TYPE_SAIZ) && !traf->GetChild(AP4_ATOM_TYPE_SENC) && + !traf->GetChild(AP4_UUID_PIFF_SAMPLE_ENCRYPTION_ATOM)) { traf->AddChild(new AP4_SencAtom()); }