Skip to content

Commit

Permalink
Creating new Strings for jstrings, more logging, reduced wait times
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Jan 9, 2024
1 parent db40ca7 commit c0b5be4
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,7 +132,7 @@ public void discoverVideoPlayerCommissioners(
SuccessCallback<DiscoveredNodeData> 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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.chip.casting;

import android.util.Log;
import java.net.InetAddress;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -63,20 +64,22 @@ 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;
this.contentApps = contentApps;
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -170,19 +171,19 @@ CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, Targe
jfieldID jLastDiscoveredMs = env->GetFieldID(jVideoPlayerClass, "lastDiscoveredMs", "J");
long lastDiscoveredMs = static_cast<long>(env->GetLongField(videoPlayer, jLastDiscoveredMs));

jfieldID getMACAddressField = env->GetFieldID(jVideoPlayerClass, "MACAddress", "Ljava/lang/String;");
jstring jMACAddress = static_cast<jstring>(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<bool>(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<jstring>(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);
}

Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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<int>(MACAddress.size()),
MACAddress.data());
}
else
{
ChipLogProgress(AppServer, "TargetVideoPlayerInfo.SetMACAddress to empty CharSpan");
}

memcpy(mMACAddressBuf, MACAddress.data(), sizeof(mMACAddressBuf));
mMACAddress = chip::CharSpan(mMACAddressBuf, sizeof(mMACAddressBuf));
}
Expand Down

0 comments on commit c0b5be4

Please sign in to comment.