Skip to content

Commit

Permalink
Merge pull request #729 from Portisch/fix_kodi_freeze
Browse files Browse the repository at this point in the history
Fix kodi freeze by session close
  • Loading branch information
glennguy authored Jul 9, 2021
2 parents a675541 + 224e49c commit d9af37a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
15 changes: 12 additions & 3 deletions wvdecrypter/cdm/media/cdm/cdm_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "cdm_adapter.h"
#include <chrono>
#include <thread>
#include <atomic>

#define DCHECK(condition) assert(condition)

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions wvdecrypter/cdm/media/cdm/cdm_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <inttypes.h>
#include <memory>
#include <mutex>
#include <atomic>

#include "../../base/native_library.h"
#include "../../base/compiler_specific.h"
Expand Down Expand Up @@ -100,6 +101,8 @@ class CdmAdapter : public std::enable_shared_from_this<CdmAdapter>
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);
Expand Down Expand Up @@ -238,6 +241,8 @@ class CdmAdapter : public std::enable_shared_from_this<CdmAdapter>
cdm::ContentDecryptionModule_10 *cdm10_;
cdm::ContentDecryptionModule_11 *cdm11_;

std::atomic<bool> session_active_;

DISALLOW_COPY_AND_ASSIGN(CdmAdapter);
};

Expand Down
32 changes: 28 additions & 4 deletions wvdecrypter/wvdecrypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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);
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -477,17 +483,14 @@ 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");
}

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_);
Expand Down Expand Up @@ -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()
{
{
Expand Down Expand Up @@ -1418,7 +1438,11 @@ class WVDecrypter : public SSD_DECRYPTER
virtual void DestroySingleSampleDecrypter(AP4_CencSingleSampleDecrypter* decrypter) override
{
if (decrypter)
{
// close session before dispose
static_cast<WV_CencSingleSampleDecrypter*>(decrypter)->CloseSessionId();
delete static_cast<WV_CencSingleSampleDecrypter*>(decrypter);
}
}

virtual void GetCapabilities(AP4_CencSingleSampleDecrypter* decrypter, const uint8_t *keyid, uint32_t media, SSD_DECRYPTER::SSD_CAPS &caps) override
Expand Down

0 comments on commit d9af37a

Please sign in to comment.