From c0b5be41ef188ec58d1770251f74e06aec1b7817 Mon Sep 17 00:00:00 2001 From: Sharad Binjola Date: Tue, 9 Jan 2024 13:31:10 -0800 Subject: [PATCH] Creating new Strings for jstrings, more logging, reduced wait times --- .../jni/com/chip/casting/TvCastingApp.java | 4 +-- .../jni/com/chip/casting/VideoPlayer.java | 11 +++++--- .../app/src/main/jni/cpp/ConversionUtils.cpp | 15 ++++++----- .../include/CHIPProjectAppConfig.h | 4 +-- .../include/TargetVideoPlayerInfo.h | 26 ++++++++++++++++++- 5 files changed, 45 insertions(+), 15 deletions(-) diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/TvCastingApp.java b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/TvCastingApp.java index 3344397facae4e..025cf0ed00d46f 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/TvCastingApp.java +++ b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/TvCastingApp.java @@ -44,7 +44,7 @@ public class TvCastingApp { Arrays.asList(35L); // Video player = 35; // delay before which we assume undiscovered cached players may be in STR mode - private static final long CHIP_DEVICE_CONFIG_STR_DISCOVERY_DELAY_SEC = 5; + private static final long CHIP_DEVICE_CONFIG_STR_DISCOVERY_DELAY_SEC = 3; private static TvCastingApp sInstance; private Context applicationContext; @@ -132,7 +132,7 @@ public void discoverVideoPlayerCommissioners( SuccessCallback discoverySuccessCallback, FailureCallback discoveryFailureCallback) { synchronized (discoveryLock) { - Log.d(TAG, "TvCastingApp.discoverVideoPlayerCommissioners called"); + Log.d(TAG, "TvCastingApp.discoverVideoPlayerCommissioners called (WoL)"); if (this.discoveryStarted) { Log.d(TAG, "Discovery already started, stopping before starting again"); diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/VideoPlayer.java b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/VideoPlayer.java index fab9b027a78cc4..9c93b264eeb33a 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/VideoPlayer.java +++ b/examples/tv-casting-app/android/App/app/src/main/jni/com/chip/casting/VideoPlayer.java @@ -17,6 +17,7 @@ */ package com.chip.casting; +import android.util.Log; import java.net.InetAddress; import java.util.HashSet; import java.util.List; @@ -63,9 +64,11 @@ public VideoPlayer( String MACAddress, boolean isAsleep, boolean isConnected) { + Log.d(TAG, "VideoPlayer() called with MACAddress = " + MACAddress); + this.nodeId = nodeId; this.fabricIndex = fabricIndex; - this.deviceName = deviceName; + this.deviceName = deviceName != null ? new String(deviceName) : null; this.vendorId = vendorId; this.productId = productId; this.deviceType = deviceType; @@ -73,10 +76,10 @@ public VideoPlayer( this.isConnected = isConnected; this.numIPs = numIPs; this.ipAddresses = ipAddresses; - this.hostName = hostName; - this.MACAddress = MACAddress; + this.hostName = hostName != null ? new String(hostName) : null; + this.MACAddress = MACAddress != null ? new String(MACAddress) : null; this.lastDiscoveredMs = lastDiscoveredMs; - this.instanceName = instanceName; + this.instanceName = instanceName != null ? new String(instanceName) : null; this.port = port; this.isAsleep = isAsleep; this.isInitialized = true; diff --git a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp index 7a8bc6a7a212ff..a3489f3c144365 100644 --- a/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp +++ b/examples/tv-casting-app/android/App/app/src/main/jni/cpp/ConversionUtils.cpp @@ -121,6 +121,7 @@ CHIP_ERROR convertTargetEndpointInfoToJContentApp(TargetEndpointInfo * targetEnd outContentApp = env->NewObject(jContentAppClass, jContentAppConstructor, targetEndpointInfo->GetEndpointId(), jClustersArrayList); } + ChipLogProgress(AppServer, "convertTargetEndpointInfoToJContentApp returning CHIP_NO_ERROR"); return CHIP_NO_ERROR; } @@ -170,19 +171,19 @@ CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, Targe jfieldID jLastDiscoveredMs = env->GetFieldID(jVideoPlayerClass, "lastDiscoveredMs", "J"); long lastDiscoveredMs = static_cast(env->GetLongField(videoPlayer, jLastDiscoveredMs)); - jfieldID getMACAddressField = env->GetFieldID(jVideoPlayerClass, "MACAddress", "Ljava/lang/String;"); - jstring jMACAddress = static_cast(env->GetObjectField(videoPlayer, getMACAddressField)); - const char * MACAddress = jMACAddress == nullptr ? nullptr : env->GetStringUTFChars(jMACAddress, 0); - jfieldID jIsAsleep = env->GetFieldID(jVideoPlayerClass, "isAsleep", "Z"); bool isAsleep = static_cast(env->GetBooleanField(videoPlayer, jIsAsleep)); outTargetVideoPlayerInfo.Initialize(nodeId, fabricIndex, nullptr, nullptr, vendorId, productId, deviceType, deviceName, hostName, 0, nullptr, port, instanceName, chip::System::Clock::Timestamp(lastDiscoveredMs)); + jfieldID getMACAddressField = env->GetFieldID(jVideoPlayerClass, "MACAddress", "Ljava/lang/String;"); + jstring jMACAddress = static_cast(env->GetObjectField(videoPlayer, getMACAddressField)); + const char * MACAddress = jMACAddress == nullptr ? nullptr : env->GetStringUTFChars(jMACAddress, 0); if (MACAddress != nullptr) { - chip::CharSpan MACAddressSpan(MACAddress, 2 * 2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength); + ChipLogProgress(AppServer, "convertJVideoPlayerToTargetVideoPlayerInfo MACAddress: %s", MACAddress); + chip::CharSpan MACAddressSpan(MACAddress, 2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength); outTargetVideoPlayerInfo.SetMACAddress(MACAddressSpan); } @@ -252,6 +253,7 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta jstring instanceName = targetVideoPlayerInfo->GetInstanceName() == nullptr ? nullptr : env->NewStringUTF(targetVideoPlayerInfo->GetInstanceName()); + ChipLogProgress(AppServer, "convertTargetVideoPlayerInfoToJVideoPlayer created jstring instanceName"); jstring MACAddress = nullptr; if (targetVideoPlayerInfo->GetMACAddress() != nullptr && targetVideoPlayerInfo->GetMACAddress()->data() != nullptr) @@ -260,7 +262,8 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta memcpy(MACAddressWithNil, targetVideoPlayerInfo->GetMACAddress()->data(), targetVideoPlayerInfo->GetMACAddress()->size()); MACAddressWithNil[targetVideoPlayerInfo->GetMACAddress()->size()] = '\0'; - MACAddress = env->NewStringUTF(MACAddressWithNil); + ChipLogProgress(AppServer, "convertTargetVideoPlayerInfoToJVideoPlayer MACAddressWithNil: %s", MACAddressWithNil); + MACAddress = env->NewStringUTF(MACAddressWithNil); } jobject jIPAddressList = nullptr; diff --git a/examples/tv-casting-app/tv-casting-common/include/CHIPProjectAppConfig.h b/examples/tv-casting-app/tv-casting-common/include/CHIPProjectAppConfig.h index cfa3f4cd160e13..642666d5102c85 100644 --- a/examples/tv-casting-app/tv-casting-common/include/CHIPProjectAppConfig.h +++ b/examples/tv-casting-app/tv-casting-common/include/CHIPProjectAppConfig.h @@ -69,13 +69,13 @@ #define CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT 4 // cached players that were seen before this window (in hours) will not be surfaced as "discovered" -#define CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_HOURS 7 * 24 +//#define CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_HOURS 7 * 24 // time (in sec) assumed to be required for player to wake up after sending WoL magic packet #define CHIP_DEVICE_CONFIG_STR_WAKE_UP_DELAY_SEC 10 // delay (in sec) before which we assume undiscovered cached players may be in STR mode -#define CHIP_DEVICE_CONFIG_STR_DISCOVERY_DELAY_SEC 5 +#define CHIP_DEVICE_CONFIG_STR_DISCOVERY_DELAY_SEC 3 // Include the CHIPProjectConfig from config/standalone // Add this at the end so that we can hit our #defines first 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 8be9054178a873..7b7c9749351c16 100644 --- a/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h +++ b/examples/tv-casting-app/tv-casting-common/include/TargetVideoPlayerInfo.h @@ -90,11 +90,35 @@ class TargetVideoPlayerInfo uint16_t GetPort() const { return mPort; } const char * GetInstanceName() const { return mInstanceName; } - chip::CharSpan * GetMACAddress() { return &mMACAddress; } + chip::CharSpan * GetMACAddress() + { + if (mMACAddress.data() != nullptr && mMACAddress.size() > 0) + { + ChipLogProgress(AppServer, "TargetVideoPlayerInfo.GetMACAddress: %.*s", static_cast(mMACAddress.size()), + mMACAddress.data()); + } + else + { + ChipLogProgress(AppServer, "TargetVideoPlayerInfo.GetMACAddress returning ref to empty CharSpan"); + } + + return &mMACAddress; + } + void SetIsAsleep(bool isAsleep) { mIsAsleep = isAsleep; } bool IsAsleep() { return mIsAsleep; } void SetMACAddress(chip::CharSpan MACAddress) { + if (MACAddress.data() != nullptr && MACAddress.size() > 0) + { + ChipLogProgress(AppServer, "TargetVideoPlayerInfo.SetMACAddress: %.*s", static_cast(MACAddress.size()), + MACAddress.data()); + } + else + { + ChipLogProgress(AppServer, "TargetVideoPlayerInfo.SetMACAddress to empty CharSpan"); + } + memcpy(mMACAddressBuf, MACAddress.data(), sizeof(mMACAddressBuf)); mMACAddress = chip::CharSpan(mMACAddressBuf, sizeof(mMACAddressBuf)); }