Skip to content

Commit

Permalink
tv-casting-app: Synchronizing discovery APIs, updating NetworkCommiss…
Browse files Browse the repository at this point in the history
…ioning FeatureMap to 1 (#26150)
  • Loading branch information
sharadb-amazon authored and pull[bot] committed Sep 22, 2023
1 parent 84f9c30 commit 1076147
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void handleDisconnect() {
* TvCastingApp
*/
private boolean initJni() {
tvCastingApp = new TvCastingApp();
tvCastingApp = TvCastingApp.getInstance();

tvCastingApp.setDACProvider(new DACProviderStub());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,30 @@ public class TvCastingApp {
private static final List<Long> DISCOVERY_TARGET_DEVICE_TYPE_FILTER =
Arrays.asList(35L); // Video player = 35;

private static TvCastingApp sInstance;
private Context applicationContext;
private ChipAppServer chipAppServer;
private NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState;
private boolean discoveryStarted = false;
private Object discoveryLock = new Object();

private WifiManager.MulticastLock multicastLock;
private NsdManager nsdManager;
private NsdDiscoveryListener nsdDiscoveryListener;

private TvCastingApp() {}

public static TvCastingApp getInstance() {
if (sInstance == null) {
sInstance = new TvCastingApp();
}
return sInstance;
}

public boolean initApp(Context applicationContext, AppParameters appParameters) {
if (applicationContext == null || appParameters == null) {
if (applicationContext == null
|| appParameters == null
|| appParameters.getConfigurationManager() == null) {
return false;
}

Expand Down Expand Up @@ -106,50 +119,62 @@ public boolean initApp(Context applicationContext, AppParameters appParameters)
public void discoverVideoPlayerCommissioners(
SuccessCallback<DiscoveredNodeData> discoverySuccessCallback,
FailureCallback discoveryFailureCallback) {
Log.d(TAG, "TvCastingApp.discoverVideoPlayerCommissioners called");
synchronized (discoveryLock) {
Log.d(TAG, "TvCastingApp.discoverVideoPlayerCommissioners called");

if (this.discoveryStarted) {
Log.d(TAG, "Discovery already started, stopping before starting again");
stopVideoPlayerDiscovery();
}
if (this.discoveryStarted) {
Log.d(TAG, "Discovery already started, stopping before starting again");
stopVideoPlayerDiscovery();
}

List<VideoPlayer> preCommissionedVideoPlayers = readCachedVideoPlayers();

WifiManager wifiManager =
(WifiManager) applicationContext.getSystemService(Context.WIFI_SERVICE);
multicastLock = wifiManager.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

nsdManager = (NsdManager) applicationContext.getSystemService(Context.NSD_SERVICE);
nsdDiscoveryListener =
new NsdDiscoveryListener(
nsdManager,
DISCOVERY_TARGET_SERVICE_TYPE,
DISCOVERY_TARGET_DEVICE_TYPE_FILTER,
preCommissionedVideoPlayers,
discoverySuccessCallback,
discoveryFailureCallback,
nsdManagerResolverAvailState);

nsdManager.discoverServices(
DISCOVERY_TARGET_SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, nsdDiscoveryListener);
Log.d(TAG, "TvCastingApp.discoverVideoPlayerCommissioners started");
this.discoveryStarted = true;
List<VideoPlayer> preCommissionedVideoPlayers = readCachedVideoPlayers();

WifiManager wifiManager =
(WifiManager) applicationContext.getSystemService(Context.WIFI_SERVICE);
multicastLock = wifiManager.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

nsdManager = (NsdManager) applicationContext.getSystemService(Context.NSD_SERVICE);
nsdDiscoveryListener =
new NsdDiscoveryListener(
nsdManager,
DISCOVERY_TARGET_SERVICE_TYPE,
DISCOVERY_TARGET_DEVICE_TYPE_FILTER,
preCommissionedVideoPlayers,
discoverySuccessCallback,
discoveryFailureCallback,
nsdManagerResolverAvailState);

nsdManager.discoverServices(
DISCOVERY_TARGET_SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, nsdDiscoveryListener);
Log.d(TAG, "TvCastingApp.discoverVideoPlayerCommissioners started");
this.discoveryStarted = true;
}
}

public void stopVideoPlayerDiscovery() {
Log.d(TAG, "TvCastingApp trying to stop video player discovery");
if (this.discoveryStarted
&& nsdManager != null
&& multicastLock != null
&& nsdDiscoveryListener != null) {
Log.d(TAG, "TvCastingApp stopping Video Player commissioner discovery");
nsdManager.stopServiceDiscovery(nsdDiscoveryListener);
if (multicastLock.isHeld()) {
multicastLock.release();
synchronized (discoveryLock) {
Log.d(TAG, "TvCastingApp trying to stop video player discovery");
if (this.discoveryStarted
&& nsdManager != null
&& multicastLock != null
&& nsdDiscoveryListener != null) {
Log.d(TAG, "TvCastingApp stopping Video Player commissioner discovery");
try {
nsdManager.stopServiceDiscovery(nsdDiscoveryListener);
} catch (IllegalArgumentException e) {
Log.w(
TAG,
"TvCastingApp received exception on calling nsdManager.stopServiceDiscovery() "
+ e.getMessage());
}

if (multicastLock.isHeld()) {
multicastLock.release();
}
this.discoveryStarted = false;
}
this.discoveryStarted = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,7 @@ endpoint 0 {
ram attribute lastNetworkingStatus;
ram attribute lastNetworkID;
ram attribute lastConnectErrorValue;
ram attribute featureMap default = 5;
ram attribute featureMap default = 1;
ram attribute clusterRevision default = 1;
}

Expand Down
Loading

0 comments on commit 1076147

Please sign in to comment.