Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions middleware/InterfacePlayerRDK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,18 @@ void InterfacePlayerRDK::TearDownStream(int type)
else if (mediaType == eGST_MEDIATYPE_SUBTITLE)
{
g_clear_object(&interfacePlayerPriv->gstPrivateContext->subtitle_sink);
pthread_mutex_lock(&stream->sourceLock);
if (stream->sinkbin)
{
MW_LOG_WARN("InterfacePlayerRDK::TearDownStream: CC sinkbin still assigned, clearing");
g_clear_object(&stream->sinkbin);
}
if (stream->source)
{
MW_LOG_WARN("InterfacePlayerRDK::TearDownStream: CC source still assigned, clearing");
g_clear_object(&stream->source);
}
pthread_mutex_unlock(&stream->sourceLock);
}
tearDownCb(false, mediaType);
MW_LOG_MIL("InterfacePlayerRDK::TearDownStream: exit mediaType = %d", mediaType);
Expand Down Expand Up @@ -2258,7 +2270,7 @@ int InterfacePlayerRDK::SetupStream(int streamId, void *playerInstance, std::st
gst_element_add_pad(subtitlebin, gst_ghost_pad_new("sink", gst_element_get_static_pad(vipertransform, "sink")));

g_object_set(stream->sinkbin, "text-sink", subtitlebin, NULL);
interfacePlayerPriv->gstPrivateContext->subtitle_sink = textsink;
interfacePlayerPriv->gstPrivateContext->subtitle_sink = GST_ELEMENT(gst_object_ref(textsink));
MW_LOG_MIL("using rialtomsesubtitlesink muted=%d sink=%p", interfacePlayerPriv->gstPrivateContext->subtitleMuted, interfacePlayerPriv->gstPrivateContext->subtitle_sink);
g_object_set(textsink, "mute", interfacePlayerPriv->gstPrivateContext->subtitleMuted ? TRUE : FALSE, NULL);
}
Expand Down Expand Up @@ -2327,7 +2339,7 @@ int InterfacePlayerRDK::SetupStream(int streamId, void *playerInstance, std::st
MW_LOG_INFO("setting has-drm=false for clear HLS/TS playback");
g_object_set(vidsink, "has-drm", FALSE, NULL);
}
interfacePlayerPriv->gstPrivateContext->video_sink = vidsink;
interfacePlayerPriv->gstPrivateContext->video_sink = GST_ELEMENT(gst_object_ref(vidsink));

// RDKEMW-18286: Set show-video-window=FALSE at sink creation time.
// This is the EARLIEST possible point. The Rialto delegate will queue
Expand Down Expand Up @@ -2357,7 +2369,7 @@ int InterfacePlayerRDK::SetupStream(int streamId, void *playerInstance, std::st
{
MW_LOG_INFO("Created rialtomseaudiosink : %s",GST_ELEMENT_NAME(audSink));
g_object_set(stream->sinkbin, "audio-sink", audSink, NULL);
interfacePlayerPriv->gstPrivateContext->audio_sink = audSink;
interfacePlayerPriv->gstPrivateContext->audio_sink = GST_ELEMENT(gst_object_ref(audSink));
}
else
{
Expand Down Expand Up @@ -3309,16 +3321,30 @@ void InterfacePlayerPriv::SendNewSegmentEvent(int type, GstClockTime startPts ,G
if (gstPrivateContext->usingRialtoSink)
{
GstCaps *currentCaps = gst_app_src_get_caps(GST_APP_SRC(stream->source));
GstSample *sample = gst_sample_new (nullptr, currentCaps, &segment, nullptr);

MW_LOG_INFO("Pushing sample with segment for mediaType[%d]. start %" G_GUINT64_FORMAT " stop %" G_GUINT64_FORMAT" rate %f applied_rate %f", mediaType, segment.start, segment.stop, segment.rate, segment.applied_rate);
if (GST_FLOW_OK != gst_app_src_push_sample(GST_APP_SRC(stream->source), sample))
if (currentCaps != NULL)
{
MW_LOG_ERR("Failed to push sample with segment for mediaType[%d]", mediaType);
GstSample *sample = gst_sample_new (nullptr, currentCaps, &segment, nullptr);
if (sample != NULL)
{
MW_LOG_INFO("Pushing sample with segment for mediaType[%d]. start %" G_GUINT64_FORMAT " stop %" G_GUINT64_FORMAT" rate %f applied_rate %f", mediaType, segment.start, segment.stop, segment.rate, segment.applied_rate);
if (GST_FLOW_OK != gst_app_src_push_sample(GST_APP_SRC(stream->source), sample))
{
MW_LOG_ERR("Failed to push sample with segment for mediaType[%d]", mediaType);
}
gst_sample_unref(sample);
}
else
{
MW_LOG_ERR("Failed to create sample for mediaType[%d]", mediaType);
}
gst_caps_unref(currentCaps);
}
else
{
MW_LOG_WARN("Cannot push segment for mediaType[%d] - caps not yet set on appsrc", mediaType);
}
gst_sample_unref(sample);
gst_caps_unref(currentCaps);
}

else
{
MW_LOG_INFO("Sending segment event for mediaType[%d]. start %" G_GUINT64_FORMAT " stop %" G_GUINT64_FORMAT" rate %f applied_rate %f", mediaType, segment.start, segment.stop, segment.rate, segment.applied_rate);
Expand Down
54 changes: 54 additions & 0 deletions middleware/drm/DrmSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@

#include "DrmSession.h"
#include "PlayerLogManager.h"
#include <chrono>

/**
* @brief Constructor for DrmSession.
*/
DrmSession::DrmSession(const string &keySystem) : m_keySystem(keySystem),m_OutputProtectionEnabled(false)
, mContentSecurityManagerSession()
, mLifecycleMutex()
, mLifecycleCV()
, mActiveOperations(0)
, mMarkedForDestruction(false)
{
}

Expand All @@ -40,6 +45,55 @@ DrmSession::~DrmSession()
{
}

/**
* @brief DELIA-70726 fix: Acquire lifecycle guard before use in decrypt().
*/
bool DrmSession::AcquireForUse()
{
std::lock_guard<std::mutex> lock(mLifecycleMutex);
if (mMarkedForDestruction)
{
return false;
}
mActiveOperations++;
return true;
}

/**
* @brief DELIA-70726 fix: Release lifecycle guard after use in decrypt().
*/
void DrmSession::ReleaseAfterUse()
{
std::lock_guard<std::mutex> lock(mLifecycleMutex);
if (mActiveOperations > 0)
{
mActiveOperations--;
}
if (mActiveOperations == 0)
{
mLifecycleCV.notify_all();
}
}

/**
* @brief DELIA-70726 fix: Block deletion of this session until any decrypt()
* call already in progress (having acquired the guard) has finished.
*/
void DrmSession::PrepareForDestruction(uint32_t timeoutMs)
{
std::unique_lock<std::mutex> lock(mLifecycleMutex);
mMarkedForDestruction = true;
if (mActiveOperations > 0)
{
MW_LOG_WARN("DrmSession::PrepareForDestruction : waiting for %d in-flight decrypt operation(s) to complete before delete", mActiveOperations);
mLifecycleCV.wait_for(lock, std::chrono::milliseconds(timeoutMs), [this]() { return mActiveOperations == 0; });
if (mActiveOperations > 0)
{
MW_LOG_ERR("DrmSession::PrepareForDestruction : timed out waiting for in-flight decrypt operation(s); proceeding with destruction");
}
}
}

/**
* @brief Get the DRM System, ie, UUID for PlayReady WideVine etc..
*/
Expand Down
49 changes: 49 additions & 0 deletions middleware/drm/DrmSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include <stdint.h>
#include <vector>
#include <gst/gst.h>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include "DrmUtils.h"
#include "ContentSecurityManagerSession.h"

Expand Down Expand Up @@ -66,7 +69,53 @@ class DrmSession
std::string m_keySystem;
bool m_OutputProtectionEnabled;
ContentSecurityManagerSession mContentSecurityManagerSession;

/* DELIA-70726 fix:
* Lifecycle guard used to prevent the DrmSession object from being
* deleted (e.g. by DrmSessionManager during DRM session slot
* reuse/eviction on back-to-back channel changes) while a GStreamer
* pipeline thread (multiqueue/decryptor) is concurrently inside
* decrypt()/verifyOutputProtection(). Without this guard, deletion of
* a session that is still referenced by an old, not-yet-fully-torn-down
* pipeline results in a use-after-free SIGSEGV inside
* OCDMSessionAdapter::verifyOutputProtection().
*/
std::mutex mLifecycleMutex;
std::condition_variable mLifecycleCV;
int mActiveOperations;
bool mMarkedForDestruction;

public:
/**
* @fn AcquireForUse
* @brief Must be called by any external caller (e.g. the GStreamer
* decryptor element) before invoking decrypt() on a DrmSession
* obtained via a raw/cached pointer. Returns false if the
* session is already being torn down, in which case decrypt()
* MUST NOT be called on this object.
* @retval true if it is safe to call decrypt(), false otherwise.
*/
bool AcquireForUse();

/**
* @fn ReleaseAfterUse
* @brief Must be called exactly once for every successful AcquireForUse(),
* after the decrypt() call completes.
*/
void ReleaseAfterUse();

/**
* @fn PrepareForDestruction
* @brief Must be called by the owner (DrmSessionManager) before deleting
* this DrmSession. Marks the session so that any new
* AcquireForUse() calls fail fast, and blocks (bounded) until all
* in-flight decrypt() operations that already acquired the guard
* have completed, making it safe to free the object.
* @param timeoutMs maximum time to wait for in-flight operations to drain.
*/
void PrepareForDestruction(uint32_t timeoutMs = 3000);


/**
* @brief Create drm session with given init data
* @param f_pbInitData : pointer to initdata
Expand Down
13 changes: 13 additions & 0 deletions middleware/drm/DrmSessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ void DrmSessionManager::clearSessionData()
{
if (drmSessionContexts != NULL && drmSessionContexts[i].drmSession != NULL)
{
/* DELIA-70726 fix: block until any in-flight decrypt() on this session
* (called from a GStreamer pipeline thread via a cached raw pointer)
* has completed, before freeing the object. */
drmSessionContexts[i].drmSession->PrepareForDestruction();
MW_SAFE_DELETE(drmSessionContexts[i].drmSession);
drmSessionContexts[i] = DrmSessionContext();
}
Expand Down Expand Up @@ -198,6 +202,8 @@ void DrmSessionManager::clearDrmSession(bool forceClearSession)
if (drmSessionContexts[i].drmSession != NULL)
{
MW_LOG_WARN("DrmSessionManager:: Clearing failed Session Data Slot : %d", i);
/* DELIA-70726 fix: see clearSessionData() for rationale. */
drmSessionContexts[i].drmSession->PrepareForDestruction();
MW_SAFE_DELETE(drmSessionContexts[i].drmSession);
}
}
Expand Down Expand Up @@ -703,6 +709,13 @@ KeyState DrmSessionManager::getDrmSession(int &err, std::shared_ptr<DrmHelper> d
MW_LOG_WARN("existing DRM session for %s has different key in slot %d", drmSessionContexts[sessionSlot].drmSession->getKeySystem().c_str(), sessionSlot);
}
MW_LOG_WARN("deleting existing DRM session for %s ", drmSessionContexts[sessionSlot].drmSession->getKeySystem().c_str());
/* DELIA-70726 fix: this slot may still be referenced by a GStreamer
* decryptor element of a previous, not-yet-fully-torn-down pipeline
* (rapid/back-to-back channel change). Block here until any decrypt()
* call already in flight against this session finishes, so the delete
* below cannot race with OCDMSessionAdapter::verifyOutputProtection()/
* decrypt() running on the old pipeline's multiqueue thread. */
drmSessionContexts[sessionSlot].drmSession->PrepareForDestruction();
MW_SAFE_DELETE(drmSessionContexts[sessionSlot].drmSession);
}
this->ProfileUpdateCb();
Expand Down
56 changes: 49 additions & 7 deletions middleware/drm/helper/WidevineDrmHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <memory>
#include <iostream>
#include <cstdlib>

#include "WidevineDrmHelper.h"
#include "DrmUtils.h"
Expand Down Expand Up @@ -184,18 +185,55 @@ void WidevineDrmHelper::setDrmMetaData(const std::string& metaData)

void WidevineDrmHelper::setDefaultKeyID(const std::string& cencData)
{
mDefaultKeySlot = -1;
std::vector<uint8_t> defaultKeyID(cencData.begin(), cencData.end());
// Also convert UUID string (e.g. "f3dff538-b8c9-58e4-e8cd-96cf811d32dc") to 16-byte binary
// for comparison against binary keyIDs parsed from PSSH
std::vector<uint8_t> defaultKeyIDBinary;
std::string uuidHex;
uuidHex.reserve(cencData.size());
for (char c : cencData)
{
if (c != '-')
{
uuidHex += c;
}
}
if (uuidHex.size() == 32)
{
defaultKeyIDBinary.reserve(16);
for (size_t i = 0; i < uuidHex.size(); i += 2)
{
char hexPair[3] = {uuidHex[i], uuidHex[i + 1], '\0'};
char* end = nullptr;
unsigned long v = std::strtoul(hexPair, &end, 16);
if (end != hexPair + 2 || v > 0xFF)
{
MW_LOG_WARN("setDefaultKeyID: invalid hex in cencData at offset %zu", i);
defaultKeyIDBinary.clear();
break;
}
defaultKeyIDBinary.push_back(static_cast<uint8_t>(v));
}
}

if(!mKeyIDs.empty())
{
for(auto& it : mKeyIDs)
{
if(defaultKeyID == it.second)
if(defaultKeyID == it.second || defaultKeyIDBinary == it.second)
{
mDefaultKeySlot = it.first;
MW_LOG_WARN("setDefaultKeyID : %s slot : %d", cencData.c_str(), mDefaultKeySlot);
MW_LOG_WARN("setDefaultKeyID : %s slot : %d", PlayerLogManager::getHexDebugStr(it.second).c_str(), mDefaultKeySlot);
break;
}
}
}
if (mDefaultKeySlot < 0 && !mKeyIDs.empty())
{
mDefaultKeySlot = mKeyIDs.begin()->first;
MW_LOG_WARN("setDefaultKeyID: no match found for cencData, defaulting to first slot %d", mDefaultKeySlot);
}
}


Expand All @@ -212,17 +250,21 @@ void WidevineDrmHelper::createInitData(std::vector<uint8_t>& initData) const
void WidevineDrmHelper::getKey(std::vector<uint8_t>& keyID) const
{
MW_LOG_WARN("WidevineDrmHelper::getKey defaultkey: %d mKeyIDs.size:%zu", mDefaultKeySlot, mKeyIDs.size());
if ((mDefaultKeySlot >= 0) && (mDefaultKeySlot < mKeyIDs.size()))
if ((mDefaultKeySlot >= 0) && (mKeyIDs.find(mDefaultKeySlot) != mKeyIDs.end()))
{
keyID = this->mKeyIDs.at(mDefaultKeySlot);
keyID = mKeyIDs.at(mDefaultKeySlot);
}
else if (mKeyIDs.size() > 0)
else if (!mKeyIDs.empty())
{
keyID = this->mKeyIDs.at(0);
if (mDefaultKeySlot >= 0)
{
MW_LOG_WARN("mDefaultKeySlot(%d) not found in mKeyIDs, falling back to first entry", mDefaultKeySlot);
}
keyID = mKeyIDs.begin()->second;
}
else
{
MW_LOG_ERR("No key");
MW_LOG_ERR("No key available - mKeyIDs is empty");
}
}

Expand Down
Loading