Skip to content

Commit

Permalink
tv-casting-app: Added WakeOnLAN support
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Sep 21, 2023
1 parent 741210d commit a1564f3
Show file tree
Hide file tree
Showing 20 changed files with 897 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ public DiscoveredNodeData(NsdServiceInfo serviceInfo) {
this.numIPs = 1;
}

public DiscoveredNodeData(VideoPlayer player) {
this.connectableVideoPlayer = player;
this.instanceName = player.getInstanceName();
this.hostName = player.getHostName();
this.deviceName = player.getDeviceName();
this.deviceType = player.getDeviceType();
this.vendorId = player.getVendorId();
this.productId = player.getProductId();
this.ipAddresses = player.getIpAddresses();
this.port = player.getPort();
}

void setConnectableVideoPlayer(VideoPlayer videoPlayer) {
this.connectableVideoPlayer = videoPlayer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@
import chip.platform.PreferencesKeyValueStoreManager;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class TvCastingApp {
private static final String TAG = TvCastingApp.class.getSimpleName();
private static final String DISCOVERY_TARGET_SERVICE_TYPE = "_matterd._udp.";
private static final List<Long> DISCOVERY_TARGET_DEVICE_TYPE_FILTER =
Arrays.asList(35L); // Video player = 35;

private static final long STR_CACHE_LAST_DISCOVERED_DAYS = 60;

private static TvCastingApp sInstance;
private Context applicationContext;
private ChipAppServer chipAppServer;
Expand All @@ -48,6 +53,7 @@ public class TvCastingApp {
private WifiManager.MulticastLock multicastLock;
private NsdManager nsdManager;
private NsdDiscoveryListener nsdDiscoveryListener;
private ScheduledFuture<?> reportSleepingCommissionersFuture;

private TvCastingApp() {}

Expand Down Expand Up @@ -149,17 +155,58 @@ public void discoverVideoPlayerCommissioners(
nsdManager.discoverServices(
DISCOVERY_TARGET_SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, nsdDiscoveryListener);
Log.d(TAG, "TvCastingApp.discoverVideoPlayerCommissioners started");

this.reportSleepingCommissionersFuture =
Executors.newScheduledThreadPool(1)
.schedule(
() -> {
Log.d(
TAG,
"Scheduling reportSleepingCommissioners with commissioner count "
+ (preCommissionedVideoPlayers != null
? preCommissionedVideoPlayers.size()
: 0));
reportSleepingCommissioners(
preCommissionedVideoPlayers, discoverySuccessCallback);
},
5000,
TimeUnit.MILLISECONDS);

this.discoveryStarted = true;
}
}

private void reportSleepingCommissioners(
List<VideoPlayer> undiscoveredVideoPlayers,
SuccessCallback<DiscoveredNodeData> discoverySuccessCallback) {
Log.d(
TAG,
"reportSleepingCommissioners called with commissioner count "
+ (undiscoveredVideoPlayers != null ? undiscoveredVideoPlayers.size() : 0));
if (undiscoveredVideoPlayers != null) {
for (VideoPlayer player : undiscoveredVideoPlayers) {
Log.d(TAG, "Undisc Video Player: " + player);
// report a player if we got its MAC address previously and it was recently discoverable
if (player.getMACAddress() != null
/* TODO: add a recency check
&& player.getLastDiscoveredMs()
> System.currentTimeMillis()
- STR_CACHE_LAST_DISCOVERED_DAYS * 24 * 60 * 60 * 1000*/ ) {
player.setAsleep(true);
discoverySuccessCallback.handle(new DiscoveredNodeData(player));
}
}
}
}

public void stopVideoPlayerDiscovery() {
synchronized (discoveryLock) {
Log.d(TAG, "TvCastingApp trying to stop video player discovery");
if (this.discoveryStarted
&& nsdManager != null
&& multicastLock != null
&& nsdDiscoveryListener != null) {
&& nsdDiscoveryListener != null
&& reportSleepingCommissionersFuture != null) {
Log.d(TAG, "TvCastingApp stopping Video Player commissioner discovery");
try {
nsdManager.stopServiceDiscovery(nsdDiscoveryListener);
Expand All @@ -173,6 +220,8 @@ public void stopVideoPlayerDiscovery() {
if (multicastLock.isHeld()) {
multicastLock.release();
}

reportSleepingCommissionersFuture.cancel(false);
this.discoveryStarted = false;
}
}
Expand All @@ -192,12 +241,61 @@ public native boolean openBasicCommissioningWindow(

public native List<VideoPlayer> readCachedVideoPlayers();

public native boolean verifyOrEstablishConnection(
public boolean verifyOrEstablishConnection(
VideoPlayer targetVideoPlayer,
SuccessCallback<VideoPlayer> onConnectionSuccess,
FailureCallback onConnectionFailure,
SuccessCallback<ContentApp> onNewOrUpdatedEndpointCallback) {
Log.d(TAG, "TvCastingApp.verifyOrEstablishConnection called");
if (!targetVideoPlayer.isAsleep()) {
// if the player is NOT asleep, establish connection right away
return _verifyOrEstablishConnection(
targetVideoPlayer,
onConnectionSuccess,
onConnectionFailure,
onNewOrUpdatedEndpointCallback);
} else {
boolean status = false;
// targetVideoPlayer is asleep. Send WakeOnLAN packet to it
if (_sendWakeOnLAN(targetVideoPlayer)) {
status = true;
// wait for player to turn on, then establish connection
Log.d(TAG, "Scheduling delayed call to _verifyOrEstablishConnection after sending WoL");
Executors.newScheduledThreadPool(1)
.schedule(
() -> {
boolean callStatus =
_verifyOrEstablishConnection(
targetVideoPlayer,
new SuccessCallback<VideoPlayer>() {
@Override
public void handle(VideoPlayer response) {
response.setAsleep(false);
onConnectionSuccess.handle(response);
}
},
onConnectionFailure,
onNewOrUpdatedEndpointCallback);
if (callStatus == false) {
Log.e(TAG, "_verifyOrEstablishConnection failed after attempting to WoL");
onConnectionFailure.handle(new MatterError(0x03, "CHIP_ERROR_INCORRECT_STATE"));
}
},
10000,
TimeUnit.MILLISECONDS);
}
return status;
}
}

private native boolean _verifyOrEstablishConnection(
VideoPlayer targetVideoPlayer,
SuccessCallback<VideoPlayer> onConnectionSuccess,
FailureCallback onConnectionFailure,
SuccessCallback<ContentApp> onNewOrUpdatedEndpointCallback);

private native boolean _sendWakeOnLAN(VideoPlayer targetVideoPlayer);

public native void shutdownAllSubscriptions();

public native void disconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,20 @@ public class VideoPlayer {
private long nodeId;
private byte fabricIndex;
private String deviceName;
private String instanceName;
private int vendorId;
private int productId;
private int deviceType;
private List<ContentApp> contentApps;
private long lastDiscoveredMs;
private String MACAddress;
private boolean isAsleep = false;
private boolean isConnected = false;

private int numIPs;
private List<InetAddress> ipAddresses;
private String hostName;
private int port;

private boolean isInitialized = false;

Expand All @@ -52,6 +57,10 @@ public VideoPlayer(
int numIPs,
List<InetAddress> ipAddresses,
String hostName,
String instanceName,
int port,
long lastDiscoveredMs,
String MACAddress,
boolean isConnected) {
this.nodeId = nodeId;
this.fabricIndex = fabricIndex;
Expand All @@ -64,6 +73,10 @@ public VideoPlayer(
this.numIPs = numIPs;
this.ipAddresses = ipAddresses;
this.hostName = hostName;
this.MACAddress = MACAddress;
this.lastDiscoveredMs = lastDiscoveredMs;
this.instanceName = instanceName;
this.port = port;
this.isInitialized = true;
}

Expand Down Expand Up @@ -116,8 +129,8 @@ public int hashCode() {
return Objects.hash(super.hashCode(), nodeId, fabricIndex);
}

@java.lang.Override
public java.lang.String toString() {
@Override
public String toString() {
return "VideoPlayer{"
+ "nodeId="
+ nodeId
Expand All @@ -126,6 +139,9 @@ public java.lang.String toString() {
+ ", deviceName='"
+ deviceName
+ '\''
+ ", instanceName='"
+ instanceName
+ '\''
+ ", vendorId="
+ vendorId
+ ", productId="
Expand All @@ -134,16 +150,25 @@ public java.lang.String toString() {
+ deviceType
+ ", contentApps="
+ contentApps
+ ", lastDiscoveredMs="
+ lastDiscoveredMs
+ ", MACAddress='"
+ MACAddress
+ '\''
+ ", isAsleep="
+ isAsleep
+ ", isConnected="
+ isConnected
+ ", numIPs="
+ numIPs
+ ", ipAddresses="
+ ipAddresses
+ ", isInitialized="
+ ", hostName='"
+ hostName
+ '\''
+ ", port="
+ port
+ ", isInitialized="
+ isInitialized
+ '}';
}
Expand Down Expand Up @@ -180,6 +205,42 @@ public int getDeviceType() {
return deviceType;
}

public int getNumIPs() {
return numIPs;
}

public List<InetAddress> getIpAddresses() {
return ipAddresses;
}

public String getHostName() {
return hostName;
}

public int getPort() {
return port;
}

public long getLastDiscoveredMs() {
return lastDiscoveredMs;
}

public String getMACAddress() {
return MACAddress;
}

public String getInstanceName() {
return instanceName;
}

public void setAsleep(boolean asleep) {
isAsleep = asleep;
}

public boolean isAsleep() {
return isAsleep;
}

public boolean isInitialized() {
return isInitialized;
}
Expand Down
Loading

0 comments on commit a1564f3

Please sign in to comment.