From 1196263b9fa404e1837636336a790ca8a06fb641 Mon Sep 17 00:00:00 2001 From: Sharad Binjola Date: Tue, 31 Jan 2023 12:56:12 -0800 Subject: [PATCH] tv-casting-app: Updating the context we pass to FindOrEstablishSession --- .../app/src/main/jni/cpp/TvCastingApp-JNI.cpp | 2 +- .../tv-casting-common/include/CastingServer.h | 1 + .../include/TargetVideoPlayerInfo.h | 96 ++++++++++++++----- .../tv-casting-common/src/CastingServer.cpp | 14 ++- .../src/TargetVideoPlayerInfo.cpp | 21 ++-- 5 files changed, 101 insertions(+), 33 deletions(-) diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp index b6eb30b142d4d6..66a710d137ff7d 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/TvCastingApp-JNI.cpp @@ -209,7 +209,7 @@ JNI_METHOD(jboolean, verifyOrEstablishConnection) [](CHIP_ERROR err) { TvCastingAppJNIMgr().getOnConnectionFailureHandler(true).Handle(err); }, [](TargetEndpointInfo * endpoint) { TvCastingAppJNIMgr().getOnNewOrUpdatedEndpointHandler(true).Handle(endpoint); }); VerifyOrExit(CHIP_NO_ERROR == err, - ChipLogError(AppServer, "CastingServer::OpenBasicCommissioningWindow failed: %" CHIP_ERROR_FORMAT, err.Format())); + ChipLogError(AppServer, "CastingServer::verifyOrEstablishConnection failed: %" CHIP_ERROR_FORMAT, err.Format())); exit: return (err == CHIP_NO_ERROR); diff --git a/examples/tv-casting-app/tv-casting-common/include/CastingServer.h b/examples/tv-casting-app/tv-casting-common/include/CastingServer.h index 8ab5dc5b358d73..3430a4f3bac7f1 100644 --- a/examples/tv-casting-app/tv-casting-common/include/CastingServer.h +++ b/examples/tv-casting-app/tv-casting-common/include/CastingServer.h @@ -97,6 +97,7 @@ class CastingServer std::function onConnectionFailure, std::function onNewOrUpdatedEndpoint); + void LogCachedVideoPlayers(); CHIP_ERROR PurgeVideoPlayerCache(); /** diff --git a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h index 845655a441f77d..c0722dc0f2dcad 100644 --- a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h +++ b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h @@ -25,12 +25,46 @@ constexpr size_t kMaxNumberOfEndpoints = 5; +class TargetVideoPlayerInfo; +class VideoPlayerConnectionContext +{ +public: + VideoPlayerConnectionContext(TargetVideoPlayerInfo * targetVideoPlayerInfo, chip::OnDeviceConnected handleDeviceConnected, + chip::OnDeviceConnectionFailure handleConnectionFailure, + std::function onConnectionSuccess, + std::function onConnectionFailure) + { + mTargetVideoPlayerInfo = targetVideoPlayerInfo; + mOnConnectedCallback = new chip::Callback::Callback(handleDeviceConnected, this); + mOnConnectionFailureCallback = new chip::Callback::Callback(handleConnectionFailure, this); + mOnConnectionSuccessClientCallback = onConnectionSuccess; + mOnConnectionFailureClientCallback = onConnectionFailure; + } + + ~VideoPlayerConnectionContext() + { + if (mOnConnectedCallback != nullptr) + { + delete mOnConnectedCallback; + } + + if (mOnConnectionFailureCallback != nullptr) + { + delete mOnConnectionFailureCallback; + } + } + + TargetVideoPlayerInfo * mTargetVideoPlayerInfo; + chip::Callback::Callback * mOnConnectedCallback = nullptr; + chip::Callback::Callback * mOnConnectionFailureCallback = nullptr; + std::function mOnConnectionSuccessClientCallback = {}; + std::function mOnConnectionFailureClientCallback = {}; +}; + class TargetVideoPlayerInfo { public: - TargetVideoPlayerInfo() : - mOnConnectedCallback(HandleDeviceConnected, this), mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this) - {} + TargetVideoPlayerInfo() {} bool operator==(const TargetVideoPlayerInfo & other) { return this->mNodeId == other.mNodeId; } @@ -49,9 +83,9 @@ class TargetVideoPlayerInfo chip::OperationalDeviceProxy * GetOperationalDeviceProxy() { - if (mDeviceProxy.ConnectionReady()) + if (mDeviceProxy != nullptr && mDeviceProxy->ConnectionReady()) { - return &mDeviceProxy; + return mDeviceProxy; } return nullptr; } @@ -73,19 +107,33 @@ class TargetVideoPlayerInfo static void HandleDeviceConnected(void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) { - TargetVideoPlayerInfo * _this = static_cast(context); - _this->mDeviceProxy = chip::OperationalDeviceProxy(&exchangeMgr, sessionHandle); - _this->mInitialized = true; + ChipLogProgress(AppServer, "tmplog: HandleDeviceConnected called"); + VideoPlayerConnectionContext * connectionContext = static_cast(context); + if (connectionContext == nullptr || connectionContext->mTargetVideoPlayerInfo == nullptr) + { + ChipLogError(AppServer, "HandleDeviceConnected called with null context or null context.targetVideoPlayerInfo"); + return; + } + if (connectionContext->mTargetVideoPlayerInfo->mDeviceProxy != nullptr) + { + ChipLogProgress(AppServer, "HandleDeviceConnected deleting mDeviceProxy"); + delete connectionContext->mTargetVideoPlayerInfo->mDeviceProxy; + ChipLogProgress(AppServer, "HandleDeviceConnected deleted mDeviceProxy"); + } + connectionContext->mTargetVideoPlayerInfo->mDeviceProxy = new chip::OperationalDeviceProxy(&exchangeMgr, sessionHandle); + connectionContext->mTargetVideoPlayerInfo->mInitialized = true; ChipLogProgress(AppServer, "HandleDeviceConnected created an instance of OperationalDeviceProxy for nodeId: 0x" ChipLogFormatX64 ", fabricIndex: %d", - ChipLogValueX64(_this->GetNodeId()), _this->GetFabricIndex()); + ChipLogValueX64(connectionContext->mTargetVideoPlayerInfo->GetNodeId()), + connectionContext->mTargetVideoPlayerInfo->GetFabricIndex()); - if (_this->mOnConnectionSuccessClientCallback) + if (connectionContext->mOnConnectionSuccessClientCallback) { ChipLogProgress(AppServer, "HandleDeviceConnected calling mOnConnectionSuccessClientCallback"); - _this->mOnConnectionSuccessClientCallback(_this); + connectionContext->mOnConnectionSuccessClientCallback(connectionContext->mTargetVideoPlayerInfo); } + delete connectionContext; } static void HandleDeviceConnectionFailure(void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) @@ -94,19 +142,29 @@ class TargetVideoPlayerInfo "HandleDeviceConnectionFailure called for peerId.nodeId: 0x" ChipLogFormatX64 ", peer.fabricIndex: %d with error: %" CHIP_ERROR_FORMAT, ChipLogValueX64(peerId.GetNodeId()), peerId.GetFabricIndex(), error.Format()); - TargetVideoPlayerInfo * _this = static_cast(context); - _this->mDeviceProxy = chip::OperationalDeviceProxy(); - if (_this->mOnConnectionFailureClientCallback) + VideoPlayerConnectionContext * connectionContext = static_cast(context); + if (connectionContext == nullptr || connectionContext->mTargetVideoPlayerInfo == nullptr) + { + ChipLogError(AppServer, "HandleDeviceConnectionFailure called with null context"); + return; + } + if (connectionContext->mTargetVideoPlayerInfo->mDeviceProxy != nullptr) + { + delete connectionContext->mTargetVideoPlayerInfo->mDeviceProxy; + } + connectionContext->mTargetVideoPlayerInfo->mDeviceProxy = new chip::OperationalDeviceProxy(); + if (connectionContext->mOnConnectionFailureClientCallback) { ChipLogProgress(AppServer, "HandleDeviceConnectionFailure calling mOnConnectionFailureClientCallback"); - _this->mOnConnectionFailureClientCallback(error); + connectionContext->mOnConnectionFailureClientCallback(error); } + delete connectionContext; } TargetEndpointInfo mEndpoints[kMaxNumberOfEndpoints]; chip::NodeId mNodeId; chip::FabricIndex mFabricIndex; - chip::OperationalDeviceProxy mDeviceProxy; + chip::OperationalDeviceProxy * mDeviceProxy = nullptr; uint16_t mVendorId = 0; uint16_t mProductId = 0; chip::DeviceTypeId mDeviceType = 0; @@ -114,11 +172,5 @@ class TargetVideoPlayerInfo char mHostName[chip::Dnssd::kHostNameMaxLength + 1] = {}; size_t mNumIPs = 0; // number of valid IP addresses chip::Inet::IPAddress mIpAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses]; - - chip::Callback::Callback mOnConnectedCallback; - chip::Callback::Callback mOnConnectionFailureCallback; - std::function mOnConnectionSuccessClientCallback; - std::function mOnConnectionFailureClientCallback; - bool mInitialized = false; }; diff --git a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp index e1490e6afbb270..b6f90d9a8f5451 100644 --- a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp @@ -295,11 +295,22 @@ TargetVideoPlayerInfo * CastingServer::ReadCachedTargetVideoPlayerInfos() return mCachedTargetVideoPlayerInfo; } +void CastingServer::LogCachedVideoPlayers() +{ + ChipLogProgress(AppServer, "CastingServer:LogCachedVideoPlayers dumping any/all cached video players."); + for (size_t i = 0; i < kMaxCachedVideoPlayers && mCachedTargetVideoPlayerInfo[i].IsInitialized(); i++) + { + mCachedTargetVideoPlayerInfo[i].PrintInfo(); + } +} + CHIP_ERROR CastingServer::VerifyOrEstablishConnection(TargetVideoPlayerInfo & targetVideoPlayerInfo, std::function onConnectionSuccess, std::function onConnectionFailure, std::function onNewOrUpdatedEndpoint) { + LogCachedVideoPlayers(); + if (!targetVideoPlayerInfo.IsInitialized()) { return CHIP_ERROR_INVALID_ARGUMENT; @@ -316,7 +327,8 @@ CHIP_ERROR CastingServer::VerifyOrEstablishConnection(TargetVideoPlayerInfo & ta prevDeviceProxy->Disconnect(); } - return targetVideoPlayerInfo.FindOrEstablishCASESession( + CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo = targetVideoPlayerInfo; + return CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo.FindOrEstablishCASESession( [](TargetVideoPlayerInfo * videoPlayer) { ChipLogProgress(AppServer, "CastingServer::OnConnectionSuccess lambda called"); CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo = *videoPlayer; diff --git a/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp b/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp index 35f95bc84e475f..ae51105a96d3bb 100644 --- a/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp @@ -45,13 +45,13 @@ CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIn memset(mDeviceName, '\0', sizeof(mDeviceName)); if (deviceName != nullptr) { - chip::Platform::CopyString(mDeviceName, chip::Dnssd::kMaxDeviceNameLen + 1, deviceName); + chip::Platform::CopyString(mDeviceName, chip::Dnssd::kMaxDeviceNameLen, deviceName); } memset(mHostName, '\0', sizeof(mHostName)); if (hostName != nullptr) { - chip::Platform::CopyString(mHostName, chip::Dnssd::kHostNameMaxLength + 1, hostName); + chip::Platform::CopyString(mHostName, chip::Dnssd::kHostNameMaxLength, hostName); } for (auto & endpointInfo : mEndpoints) @@ -71,11 +71,14 @@ CHIP_ERROR TargetVideoPlayerInfo::Initialize(NodeId nodeId, FabricIndex fabricIn CHIP_ERROR TargetVideoPlayerInfo::FindOrEstablishCASESession(std::function onConnectionSuccess, std::function onConnectionFailure) { - mOnConnectionSuccessClientCallback = onConnectionSuccess; - mOnConnectionFailureClientCallback = onConnectionFailure; - Server * server = &(chip::Server::GetInstance()); - server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(mNodeId, mFabricIndex), &mOnConnectedCallback, - &mOnConnectionFailureCallback); + ChipLogProgress(AppServer, "TargetVideoPlayerInfo::FindOrEstablishCASESession called"); + + VideoPlayerConnectionContext * connectionContext = new VideoPlayerConnectionContext( + this, HandleDeviceConnected, HandleDeviceConnectionFailure, onConnectionSuccess, onConnectionFailure); + Server * server = &(chip::Server::GetInstance()); + server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(mNodeId, mFabricIndex), + connectionContext->mOnConnectedCallback, + connectionContext->mOnConnectionFailureCallback); return CHIP_NO_ERROR; } @@ -128,8 +131,8 @@ bool TargetVideoPlayerInfo::HasEndpoint(EndpointId endpointId) void TargetVideoPlayerInfo::PrintInfo() { - ChipLogProgress(NotSpecified, " TargetVideoPlayerInfo nodeId=0x" ChipLogFormatX64 " fabric index=%d", ChipLogValueX64(mNodeId), - mFabricIndex); + ChipLogProgress(NotSpecified, " TargetVideoPlayerInfo deviceName=%s nodeId=0x" ChipLogFormatX64 " fabric index=%d", mDeviceName, + ChipLogValueX64(mNodeId), mFabricIndex); for (auto & endpointInfo : mEndpoints) { if (endpointInfo.IsInitialized())