diff --git a/wvdecrypter/cdm/media/cdm/cdm_adapter.cc b/wvdecrypter/cdm/media/cdm/cdm_adapter.cc index 96a4b6982..c48f2aade 100644 --- a/wvdecrypter/cdm/media/cdm/cdm_adapter.cc +++ b/wvdecrypter/cdm/media/cdm/cdm_adapter.cc @@ -5,7 +5,6 @@ #include "cdm_adapter.h" #include #include -#include #define DCHECK(condition) assert(condition) @@ -135,6 +134,7 @@ CdmAdapter::CdmAdapter( , cdm_config_(cdm_config) , active_buffer_(0) , cdm9_(0), cdm10_(0), cdm11_(0) +, session_active_(false) { //DCHECK(!key_system_.empty()); Initialize(); @@ -328,10 +328,16 @@ void CdmAdapter::UpdateSession(uint32_t promise_id, response, response_size); } +void CdmAdapter::SetSessionActive() +{ + session_active_ = true; +} + void CdmAdapter::CloseSession(uint32_t promise_id, const char* session_id, uint32_t session_id_size) { + session_active_ = false; exit_thread_flag = true; while (timer_thread_running) { @@ -506,8 +512,11 @@ cdm::Buffer* CdmAdapter::Allocate(uint32_t capacity) void CdmAdapter::SetTimer(int64_t delay_ms, void* context) { //LICENSERENEWAL - exit_thread_flag = false; - std::thread(timerfunc, shared_from_this(), delay_ms, context).detach(); + if (session_active_) + { + exit_thread_flag = false; + std::thread(timerfunc, shared_from_this(), delay_ms, context).detach(); + } } cdm::Time CdmAdapter::GetCurrentWallTime() diff --git a/wvdecrypter/cdm/media/cdm/cdm_adapter.h b/wvdecrypter/cdm/media/cdm/cdm_adapter.h index d722f6cf0..89afef688 100644 --- a/wvdecrypter/cdm/media/cdm/cdm_adapter.h +++ b/wvdecrypter/cdm/media/cdm/cdm_adapter.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "../../base/native_library.h" #include "../../base/compiler_specific.h" @@ -100,6 +101,8 @@ class CdmAdapter : public std::enable_shared_from_this const uint8_t* response, uint32_t response_size); + void SetSessionActive(); + void CloseSession(uint32_t promise_id, const char* session_id, uint32_t session_id_size); @@ -238,6 +241,8 @@ class CdmAdapter : public std::enable_shared_from_this cdm::ContentDecryptionModule_10 *cdm10_; cdm::ContentDecryptionModule_11 *cdm11_; + std::atomic session_active_; + DISALLOW_COPY_AND_ASSIGN(CdmAdapter); }; diff --git a/wvdecrypter/wvdecrypter.cpp b/wvdecrypter/wvdecrypter.cpp index b3f37fa31..08a3d30ad 100644 --- a/wvdecrypter/wvdecrypter.cpp +++ b/wvdecrypter/wvdecrypter.cpp @@ -174,12 +174,15 @@ class WV_CencSingleSampleDecrypter : public AP4_CencSingleSampleDecrypter void GetCapabilities(const uint8_t* key, uint32_t media, SSD_DECRYPTER::SSD_CAPS &caps); virtual const char *GetSessionId() override; + void SetSessionActive(); + void CloseSessionId(); void SetSession(const char* session, uint32_t session_size, const uint8_t *data, size_t data_size) { std::lock_guard lock(renewal_lock_); session_ = std::string(session, session_size); challenge_.SetData(data, data_size); + Log(SSD_HOST::LL_DEBUG, "%s: opened session with Id: %s", __func__, session_.c_str()); } void AddSessionKey(const uint8_t *data, size_t data_size, uint32_t status); @@ -388,7 +391,10 @@ void WV_DRM::OnCDMMessage(const char* session, uint32_t session_size, CDMADPMSG return; if (msg == CDMADPMSG::kSessionMessage) + { (*b)->SetSession(session, session_size, data, data_size); + (*b)->SetSessionActive(); + } else if (msg == CDMADPMSG::kSessionKeysChange) (*b)->AddSessionKey(data, data_size, status); }; @@ -477,8 +483,7 @@ WV_CencSingleSampleDecrypter::WV_CencSingleSampleDecrypter(WV_DRM &drm, AP4_Data if (keys_.empty()) { Log(SSD_HOST::LL_ERROR, "License update not successful (no keys)"); - drm_.GetCdmAdapter()->CloseSession(++promise_id_, session_.data(), session_.size()); - session_.clear(); + CloseSessionId(); return; } Log(SSD_HOST::LL_DEBUG, "License update successful"); @@ -486,8 +491,6 @@ WV_CencSingleSampleDecrypter::WV_CencSingleSampleDecrypter(WV_DRM &drm, AP4_Data WV_CencSingleSampleDecrypter::~WV_CencSingleSampleDecrypter() { - if (!session_.empty()) - drm_.GetCdmAdapter()->CloseSession(++promise_id_, session_.data(), session_.size()); drm_.removessd(this); free(subsample_buffer_decrypt_); free(subsample_buffer_video_); @@ -573,6 +576,23 @@ const char *WV_CencSingleSampleDecrypter::GetSessionId() return session_.empty()? nullptr : session_.c_str(); } +void WV_CencSingleSampleDecrypter::SetSessionActive() +{ + drm_.GetCdmAdapter()->SetSessionActive(); +} + +void WV_CencSingleSampleDecrypter::CloseSessionId() +{ + if (!session_.empty()) + { + Log(SSD_HOST::LL_DEBUG, "%s: close session with Id: %s", __func__, session_.c_str()); + drm_.GetCdmAdapter()->CloseSession(++promise_id_, session_.data(), session_.size()); + session_.clear(); + + Log(SSD_HOST::LL_DEBUG, "%s: session closed", __func__); + } +} + void WV_CencSingleSampleDecrypter::CheckLicenseRenewal() { { @@ -1418,7 +1438,11 @@ class WVDecrypter : public SSD_DECRYPTER virtual void DestroySingleSampleDecrypter(AP4_CencSingleSampleDecrypter* decrypter) override { if (decrypter) + { + // close session before dispose + static_cast(decrypter)->CloseSessionId(); delete static_cast(decrypter); + } } virtual void GetCapabilities(AP4_CencSingleSampleDecrypter* decrypter, const uint8_t *keyid, uint32_t media, SSD_DECRYPTER::SSD_CAPS &caps) override