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 7111778a74642a..1c1906a7448aa9 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 d26ae0cab548cd..129144522fd0e4 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 ab519afd340f88..08e2a8f9b5f084 100644 --- a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h +++ b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h @@ -24,12 +24,43 @@ 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; } @@ -48,9 +79,9 @@ class TargetVideoPlayerInfo chip::OperationalDeviceProxy * GetOperationalDeviceProxy() { - if (mDeviceProxy.ConnectionReady()) + if (mDeviceProxy != nullptr && mDeviceProxy->ConnectionReady()) { - return &mDeviceProxy; + return mDeviceProxy; } return nullptr; } @@ -72,19 +103,32 @@ class TargetVideoPlayerInfo static void HandleDeviceConnected(void * context, chip::Messaging::ExchangeManager & exchangeMgr, 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) @@ -93,19 +137,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; uint16_t mDeviceType = 0; @@ -113,11 +167,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 32d09a93951b06..654577e8e6b099 100644 --- a/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp @@ -299,11 +299,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; @@ -320,7 +331,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 9ce23efec6f0c5..61eafb3e6fbc07 100644 --- a/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp +++ b/examples/tv-casting-app/tv-casting-common/src/TargetVideoPlayerInfo.cpp @@ -43,13 +43,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) @@ -69,11 +69,12 @@ 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; } @@ -126,7 +127,7 @@ bool TargetVideoPlayerInfo::HasEndpoint(EndpointId endpointId) void TargetVideoPlayerInfo::PrintInfo() { - ChipLogProgress(NotSpecified, " TargetVideoPlayerInfo nodeId=0x" ChipLogFormatX64 " fabric index=%d", ChipLogValueX64(mNodeId), + ChipLogProgress(NotSpecified, " TargetVideoPlayerInfo deviceName=%s nodeId=0x" ChipLogFormatX64 " fabric index=%d", mDeviceName, ChipLogValueX64(mNodeId), mFabricIndex); for (auto & endpointInfo : mEndpoints) {