From 18e506f17dc3fe8eb38412a96a68853acec6ec64 Mon Sep 17 00:00:00 2001 From: kvfasil Date: Tue, 31 Dec 2024 09:49:39 +0000 Subject: [PATCH] update: updated MediaSessionExt.cpp and removed unused code. --- MediaSessionExt.cpp | 396 ++++++++++++++++++-------------------------- 1 file changed, 159 insertions(+), 237 deletions(-) diff --git a/MediaSessionExt.cpp b/MediaSessionExt.cpp index 2247376..12961ce 100644 --- a/MediaSessionExt.cpp +++ b/MediaSessionExt.cpp @@ -1,34 +1,18 @@ -/* - * If not stated otherwise in this file or this component's LICENSE file the - * following copyright and licenses apply: - * - * Copyright 2020 RDK Management - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - #include "MediaSession.h" #include #include #include +#ifdef USE_SVP +#include "gst_svp_meta.h" +#endif + using namespace std; #include -using namespace WPEFramework; -using SafeCriticalSection = Core::SafeSyncType; -extern Core::CriticalSection drmAppContextMutex_; +using SafeCriticalSection = WPEFramework::Core::SafeSyncType; +extern WPEFramework::Core::CriticalSection drmAppContextMutex_; const DRM_WCHAR PLAY[] = { ONE_WCHAR('P', '\0'), ONE_WCHAR('l', '\0'), @@ -45,20 +29,110 @@ namespace CDMi { std::map mBindMap; static const DRM_CONST_STRING* RIGHTS[] = { &PLAY_RIGHT }; +/* +MediaKeySession::MediaKeySession(const uint8_t drmHeader[], uint32_t drmHeaderLength, DRM_APP_CONTEXT * poAppContext, bool initiateChallengeGeneration = false ) + : m_pbRevocationBuffer(nullptr) + , m_eKeyState(KEY_CLOSED) + , m_pbChallenge(nullptr) + , m_cbChallenge(0) + , m_pchSilentURL(nullptr) + , m_piCallback(nullptr) + , mSessionId(0) + , mInitiateChallengeGeneration(initiateChallengeGeneration) + , m_cHeaderKIDs(0) + , m_pdstrHeaderKIDs( nullptr ) + , m_eHeaderVersion( DRM_HEADER_VERSION_UNKNOWN ) + , m_oBatchID( DRM_ID_EMPTY ) + , m_currentDecryptContext( nullptr ) +#ifdef USE_SVP + , m_pSVPContext(nullptr) + , m_rpcID(0) +#endif + , m_fCommit(false) + , m_poAppContext(poAppContext) + , m_decryptInited(false) + , m_bDRMInitializedLocally(false) +{ +#ifdef USE_SVP + gst_svp_ext_get_context(&m_pSVPContext, Client, m_rpcID); +#endif + + mDrmHeader.resize(drmHeaderLength); + memcpy(&mDrmHeader[0], drmHeader, drmHeaderLength); + + m_eKeyState = KEY_INIT; +} +*/ + uint32_t MediaKeySession::GetSessionIdExt() const { - fprintf(stderr,"#FASIL# %s: %s: %d\n", __FILE__,__func__,__LINE__); return mSessionId; } CDMi_RESULT MediaKeySession::SetDrmHeader(const uint8_t drmHeader[], uint32_t drmHeaderLength) { - fprintf(stderr,"#FASIL# %s: %s: %d\n", __FILE__,__func__,__LINE__); mDrmHeader.resize(drmHeaderLength); memcpy(&mDrmHeader[0], drmHeader, drmHeaderLength); return CDMi_SUCCESS; } +CDMi_RESULT MediaKeySession::BindKeyNow(DECRYPT_CONTEXT decryptContext) +{ + + DRM_VOID * pvData = nullptr; + DRMPFNPOLICYCALLBACK pfnOPLCallback = nullptr; + DECRYPT_CONTEXT tmpDecryptContext; + DRM_RESULT dr; + + if ( CDMi_SUCCESS != SetKeyIdProperty( decryptContext->keyId ) ) + { + return CDMi_S_FALSE; + } + + dr = ReaderBind( + RIGHTS, + sizeof(RIGHTS) / sizeof(DRM_CONST_STRING*), + _PolicyCallback, + pvData, + &(decryptContext->oDrmDecryptContext) ); + + if ( DRM_FAILED( dr ) ){ + fprintf(stderr, "[%s:%d] ReaderBind failed. 0x%X - %s",__FUNCTION__,__LINE__,dr,DRM_ERR_NAME(dr)); + return CDMi_S_FALSE; + } else { + dr = Drm_Reader_Commit(m_poAppContext, _PolicyCallback, pvData); + if (DRM_FAILED(dr)) + { + fprintf(stderr, "[%s:%d] Drm_Reader_Commit failed. 0x%X - %s",__FUNCTION__,__LINE__,dr,DRM_ERR_NAME(dr)); + return CDMi_S_FALSE; + } + } + if ( nullptr == ( tmpDecryptContext = GetDecryptCtx( decryptContext->keyId ) ) ){ + // m_DecryptContextVector.push_back(decryptContext); + } + return CDMi_SUCCESS; +} + +CDMi_RESULT MediaKeySession::BindKey(KeyId keyId) +{ + DECRYPT_CONTEXT decryptContext; + decryptContext = NEW_DECRYPT_CONTEXT(); + decryptContext->keyId = keyId; + + auto it = mBindMap.find(keyId); + if (it != mBindMap.end()) + { + it->second = decryptContext; + return CDMi_SUCCESS; + } + else + { + BindKeyNow(decryptContext); + mBindMap.insert(std::make_pair(decryptContext->keyId, std::shared_ptr<__DECRYPT_CONTEXT>())); + } + return CDMi_SUCCESS; +} + CDMi_RESULT MediaKeySession::StoreLicenseData(const uint8_t f_rgbLicenseData[], uint32_t f_cbLicenseDataSize, uint8_t * f_pSecureStopId) { DRM_RESULT err = DRM_SUCCESS; @@ -143,6 +217,66 @@ CDMi_RESULT MediaKeySession::StoreLicenseData(const uint8_t f_rgbLicenseData[], return CDMi_SUCCESS; } +CDMi_RESULT MediaKeySession::SelectKeyId( const uint8_t f_keyLength, const uint8_t f_keyId[] ) +{ + SafeCriticalSection systemLock(drmAppContextMutex_); + DRM_RESULT err; + DRMPFNPOLICYCALLBACK pfnOPLCallback = nullptr; + DRM_VOID * pvData = nullptr; + + pfnOPLCallback = _PolicyCallback; + + if ( f_keyId == nullptr || f_keyLength != DRM_ID_SIZE ) + { + fprintf(stderr, "[%s:%d] Bad value for keyId arg ",__FUNCTION__,__LINE__); + return CDMi_S_FALSE; + } + + KeyId keyId(&f_keyId[0],KeyId::KEYID_ORDER_UUID_BE); + std::string keyIdHex(keyId.HexStr()); + + if ( nullptr != ( m_currentDecryptContext = GetDecryptCtx( keyId ) ) ){ + return CDMi_SUCCESS; + } + + if ( CDMi_SUCCESS != SetKeyIdProperty( keyId ) ) + { + fprintf(stderr, "[%s:%d] SetKeyIdProperty failed",__FUNCTION__,__LINE__); + return CDMi_S_FALSE; + } + + CDMi_RESULT result = CDMi_SUCCESS; + + DECRYPT_CONTEXT decryptContext = NEW_DECRYPT_CONTEXT(); + err = ReaderBind( + RIGHTS, + sizeof(RIGHTS) / sizeof(DRM_CONST_STRING*), + pfnOPLCallback, + pvData, + &(decryptContext->oDrmDecryptContext ) ); + + if (DRM_FAILED(err)) + { + fprintf(stderr, "[%s:%d] ReaderBind failed. 0x%X - %s",__FUNCTION__,__LINE__,err,DRM_ERR_NAME(err)); + result = CDMi_S_FALSE; + } else { + err = Drm_Reader_Commit(m_poAppContext, pfnOPLCallback, pvData); + if (DRM_FAILED(err)) + { + fprintf(stderr, "[%s:%d] Drm_Reader_Commit failed. 0x%X - %s",__FUNCTION__,__LINE__,err,DRM_ERR_NAME(err)); + result = CDMi_S_FALSE; + } + } + if (result == CDMi_SUCCESS) { + m_fCommit = TRUE; + m_decryptInited = true; + decryptContext->keyId = keyId; + // m_DecryptContextVector.push_back(decryptContext); + m_currentDecryptContext = decryptContext; + } + return result; +} + CDMi_RESULT MediaKeySession::GetChallengeDataExt(uint8_t * f_pChallenge, uint32_t & f_ChallengeSize, uint32_t f_isLDL) { DRM_RESULT err; @@ -265,216 +399,4 @@ CDMi_RESULT MediaKeySession::CleanDecryptContext() m_decryptInited = false; return CDMi_SUCCESS; } - - - - - - - -CDMi_RESULT MediaKeySession::SetKeyId(DRM_APP_CONTEXT *pDrmAppCtx, const uint8_t keyLength, const uint8_t keyId[]){ - fprintf(stderr,"#FASIL# %s: %s: %d\n", __FILE__,__func__,__LINE__); - return CDMi_SUCCESS; -} - - -CDMi_RESULT MediaKeySession::BindKeyNow(DECRYPT_CONTEXT decryptContext) -{ - - DRM_VOID * pvData = nullptr; - DRMPFNPOLICYCALLBACK pfnOPLCallback = nullptr; - DECRYPT_CONTEXT tmpDecryptContext; - DRM_RESULT dr; - - if ( CDMi_SUCCESS != SetKeyIdProperty( decryptContext->keyId ) ) - { - return CDMi_S_FALSE; - } - - dr = ReaderBind( - RIGHTS, - sizeof(RIGHTS) / sizeof(DRM_CONST_STRING*), - _PolicyCallback, - pvData, - &(decryptContext->oDrmDecryptContext) ); - - if ( DRM_FAILED( dr ) ){ - fprintf(stderr, "[%s:%d] ReaderBind failed. 0x%X - %s",__FUNCTION__,__LINE__,dr,DRM_ERR_NAME(dr)); - return CDMi_S_FALSE; - } else { - dr = Drm_Reader_Commit(m_poAppContext, _PolicyCallback, pvData); - if (DRM_FAILED(dr)) - { - fprintf(stderr, "[%s:%d] Drm_Reader_Commit failed. 0x%X - %s",__FUNCTION__,__LINE__,dr,DRM_ERR_NAME(dr)); - return CDMi_S_FALSE; - } - } - if ( nullptr == ( tmpDecryptContext = GetDecryptCtx( decryptContext->keyId ) ) ){ - // m_DecryptContextVector.push_back(decryptContext); - } - return CDMi_SUCCESS; -} - -CDMi_RESULT MediaKeySession::BindKey(KeyId keyId) -{ - DECRYPT_CONTEXT decryptContext; - decryptContext = NEW_DECRYPT_CONTEXT(); - decryptContext->keyId = keyId; - - auto it = mBindMap.find(keyId); - if (it != mBindMap.end()) - { - it->second = decryptContext; - return CDMi_SUCCESS; - } - else - { - BindKeyNow(decryptContext); - mBindMap.insert(std::make_pair(decryptContext->keyId, std::shared_ptr<__DECRYPT_CONTEXT>())); - } - return CDMi_SUCCESS; -} - - - -CDMi_RESULT MediaKeySession::SelectKeyId( const uint8_t f_keyLength, const uint8_t f_keyId[] ) -{ - SafeCriticalSection systemLock(drmAppContextMutex_); - DRM_RESULT err; - DRMPFNPOLICYCALLBACK pfnOPLCallback = nullptr; - DRM_VOID * pvData = nullptr; - - pfnOPLCallback = _PolicyCallback; - - if ( f_keyId == nullptr || f_keyLength != DRM_ID_SIZE ) - { - fprintf(stderr, "[%s:%d] Bad value for keyId arg ",__FUNCTION__,__LINE__); - return CDMi_S_FALSE; - } - - KeyId keyId(&f_keyId[0],KeyId::KEYID_ORDER_UUID_BE); - std::string keyIdHex(keyId.HexStr()); - - if ( nullptr != ( m_currentDecryptContext = GetDecryptCtx( keyId ) ) ){ - return CDMi_SUCCESS; - } - - if ( CDMi_SUCCESS != SetKeyIdProperty( keyId ) ) - { - fprintf(stderr, "[%s:%d] SetKeyIdProperty failed",__FUNCTION__,__LINE__); - return CDMi_S_FALSE; - } - - CDMi_RESULT result = CDMi_SUCCESS; - - DECRYPT_CONTEXT decryptContext = NEW_DECRYPT_CONTEXT(); - err = ReaderBind( - RIGHTS, - sizeof(RIGHTS) / sizeof(DRM_CONST_STRING*), - pfnOPLCallback, - pvData, - &(decryptContext->oDrmDecryptContext ) ); - - if (DRM_FAILED(err)) - { - fprintf(stderr, "[%s:%d] ReaderBind failed. 0x%X - %s",__FUNCTION__,__LINE__,err,DRM_ERR_NAME(err)); - result = CDMi_S_FALSE; - } else { - err = Drm_Reader_Commit(m_poAppContext, pfnOPLCallback, pvData); - if (DRM_FAILED(err)) - { - fprintf(stderr, "[%s:%d] Drm_Reader_Commit failed. 0x%X - %s",__FUNCTION__,__LINE__,err,DRM_ERR_NAME(err)); - result = CDMi_S_FALSE; - } - } - if (result == CDMi_SUCCESS) { - m_fCommit = TRUE; - m_decryptInited = true; - decryptContext->keyId = keyId; - // m_DecryptContextVector.push_back(decryptContext); - m_currentDecryptContext = decryptContext; - } - return result; -} - -CDMi_RESULT MediaKeySession::SelectDrmHeader(DRM_APP_CONTEXT *pDrmAppCtx, - const uint32_t headerLength, const uint8_t header[]) -{ - fprintf(stderr,"#FASIL# %s: %s: %d\n", __FILE__,__func__,__LINE__); - // Make the current app context contain the DRM header for this session. - ocdm_log("Drm_Content_SetProperty DRM_CSP_AUTODETECT_HEADER\n"); - - return CDMi_SUCCESS; -} - -CDMi_RESULT MediaKeySession::SetOutputMode(DRM_APP_CONTEXT *pDrmAppCtx, DRM_DWORD dwDecryptionMode) -{ - - return CDMi_SUCCESS; - -} - -struct CallbackInfo -{ - IMediaKeySessionCallback * _callback; - uint16_t _compressedVideo; - uint16_t _uncompressedVideo; - uint16_t _analogVideo; - uint16_t _compressedAudio; - uint16_t _uncompressedAudio; -}; - -static void * PlayLevelUpdateCallback(void * data) -{ - fprintf(stderr,"#FASIL# %s: %s: %d\n", __FILE__,__func__,__LINE__); - CallbackInfo * callbackInfo = static_cast(data); - - stringstream keyMessage; - keyMessage << "{"; - keyMessage << "\"compressed-video\": " << callbackInfo->_compressedVideo << ","; - keyMessage << "\"uncompressed-video\": " << callbackInfo->_uncompressedVideo << ","; - keyMessage << "\"analog-video\": " << callbackInfo->_analogVideo << ","; - keyMessage << "\"compressed-audio\": " << callbackInfo->_compressedAudio << ","; - keyMessage << "\"uncompressed-audio\": " << callbackInfo->_uncompressedAudio; - keyMessage << "}"; - - string keyMessageStr = keyMessage.str(); - const uint8_t * messageBytes = reinterpret_cast(keyMessageStr.c_str()); - - char urlBuffer[64]; - strcpy(urlBuffer, "properties"); - callbackInfo->_callback->OnKeyMessage(messageBytes, keyMessageStr.length() + 1, urlBuffer); - - delete callbackInfo; - return nullptr; -} - -static DRM_RESULT opencdm_output_levels_callback(const DRM_VOID *outputLevels, DRM_POLICY_CALLBACK_TYPE callbackType, const DRM_VOID *data) { - fprintf(stderr,"#FASIL# %s: %s: %d\n", __FILE__,__func__,__LINE__); - // We only care about the play callback. - if (callbackType != DRM_PLAY_OPL_CALLBACK) - return DRM_SUCCESS; - return DRM_SUCCESS; - const IMediaKeySessionCallback * constSessionCallback = reinterpret_cast(data); - if (constSessionCallback != nullptr) { - CallbackInfo * callbackInfo = new CallbackInfo; - callbackInfo->_callback = const_cast(constSessionCallback); - - // Pull out the protection levels. - const DRM_PLAY_OPL_EX2* playLevels = static_cast(outputLevels); - callbackInfo->_compressedVideo = playLevels->minOPL.wCompressedDigitalVideo; - callbackInfo->_uncompressedVideo = playLevels->minOPL.wUncompressedDigitalVideo; - callbackInfo->_analogVideo = playLevels->minOPL.wAnalogVideo; - callbackInfo->_compressedAudio = playLevels->minOPL.wCompressedDigitalAudio; - callbackInfo->_uncompressedAudio = playLevels->minOPL.wUncompressedDigitalAudio; - - // Run on a new thread, so we don't go too deep in the IPC callstack. - pthread_t threadId; - pthread_create(&threadId, nullptr, PlayLevelUpdateCallback, callbackInfo); - - } - // All done. - return DRM_SUCCESS; -} - -} +} \ No newline at end of file