Skip to content

Commit

Permalink
Decoupled Netcast and DLNA services (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy White committed Aug 5, 2014
1 parent fccbf5c commit b7c50e8
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 103 deletions.
68 changes: 16 additions & 52 deletions src/com/connectsdk/discovery/DiscoveryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ public boolean deviceIsCompatible(ConnectableDevice device) {
*/
public void registerDefaultDeviceTypes() {
registerDeviceService(WebOSTVService.class, SSDPDiscoveryProvider.class);
// registerDeviceService(NetcastTVService.class, SSDPDiscoveryProvider.class);
registerDeviceService(DLNAService.class, SSDPDiscoveryProvider.class); // includes Netcast
registerDeviceService(NetcastTVService.class, SSDPDiscoveryProvider.class);
registerDeviceService(DLNAService.class, SSDPDiscoveryProvider.class);
registerDeviceService(DIALService.class, SSDPDiscoveryProvider.class);
registerDeviceService(RokuService.class, SSDPDiscoveryProvider.class);
registerDeviceService(CastService.class, CastDiscoveryProvider.class);
Expand Down Expand Up @@ -620,8 +620,10 @@ public boolean isNetcast(ServiceDescription description) {
String modelDescription = description.getModelDescription();

if (modelName != null && modelName.toUpperCase(Locale.US).equals("LG TV")) {
if (modelDescription != null && !(modelDescription.toUpperCase(Locale.US).contains("WEBOS"))) {
isNetcastTV = true;
if (modelDescription != null && !(modelDescription.toUpperCase().contains("WEBOS"))) {
if (description.getServiceID().equals(NetcastTVService.ID)); {
isNetcastTV = true;
}
}
}

Expand Down Expand Up @@ -776,61 +778,23 @@ public void onServiceDiscoveryFailed(DiscoveryProvider provider, ServiceCommandE
public void addServiceDescriptionToDevice(ServiceDescription desc, ConnectableDevice device) {
Log.d("Connect SDK", "Adding service " + desc.getServiceID() + " to device with address " + device.getIpAddress() + " and id " + device.getId());

Class<? extends DeviceService> deviceServiceClass = null;

boolean classReinitializeFlag = false;
Class<? extends DeviceService> deviceServiceClass = (Class<DeviceService>) deviceClasses.get(desc.getServiceID());

if (isNetcast(desc)) {
deviceServiceClass = NetcastTVService.class;
classReinitializeFlag = true;
}

if (classReinitializeFlag) {
Method m;
Object result = null;
try {
m = deviceServiceClass.getMethod("discoveryParameters");
result = m.invoke(null);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}

if (result == null)
return;

JSONObject discoveryParameters = (JSONObject) result;
String serviceId = discoveryParameters.optString("serviceId");
if (deviceServiceClass == DLNAService.class) {
if (desc.getLocationXML() == null)
return;

if (serviceId == null || serviceId.length() == 0)
// we only support LG DLNA devices, currently
if (!desc.getLocationXML().contains("LG"))
return;

desc.setServiceID(serviceId);
} else {
deviceServiceClass = (Class<DeviceService>) deviceClasses.get(desc.getServiceID());
}
} else if (deviceServiceClass == NetcastTVService.class) {
if (!isNetcast(desc))
return;
}

if (deviceServiceClass == null)
return;

if (DLNAService.class.isAssignableFrom(deviceServiceClass)) {
String netcast = "netcast";
String webos = "webos";

String locationXML = desc.getLocationXML().toLowerCase();

int locNet = locationXML.indexOf(netcast);
int locWeb = locationXML.indexOf(webos);

if (locNet == -1 && locWeb == -1)
return;
}

ServiceConfig serviceConfig = null;

if (connectableDeviceStore != null)
Expand Down
86 changes: 54 additions & 32 deletions src/com/connectsdk/discovery/provider/SSDPDiscoveryProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,7 @@ public void sendSearch() {
final ServiceDescription service = foundServices.get(key);

if (service != null) {
Util.runOnUI(new Runnable() {

@Override
public void run() {
for (DiscoveryProviderListener listener : serviceListeners) {
listener.onServiceRemoved(SSDPDiscoveryProvider.this, service);
}
}
});
notifyListenersOfLostService(service);
}

if (foundServices.containsKey(key))
Expand Down Expand Up @@ -335,15 +327,7 @@ private void handleDatagramPacket(final ParsedDatagram pd) {
final ServiceDescription service = foundServices.get(uuid);

if (service != null) {
Util.runOnUI(new Runnable() {

@Override
public void run() {
for (DiscoveryProviderListener listener : serviceListeners) {
listener.onServiceRemoved(SSDPDiscoveryProvider.this, service);
}
}
});
notifyListenersOfLostService(service);
}
} else {
String location = pd.data.get(SSDP.LOCATION);
Expand Down Expand Up @@ -412,7 +396,6 @@ public void run() {
final ServiceDescription service = discoveredServices.get(uuid);

if (service != null) {
service.setServiceID(serviceIdForFilter(serviceFilter));
service.setServiceFilter(serviceFilter);
service.setFriendlyName(device.friendlyName);
service.setModelName(device.modelName);
Expand All @@ -427,15 +410,7 @@ public void run() {

foundServices.put(uuid, service);

Util.runOnUI(new Runnable() {

@Override
public void run() {
for (DiscoveryProviderListener listener : serviceListeners) {
listener.onServiceAdded(SSDPDiscoveryProvider.this, service);
}
}
});
notifyListenersOfNewService(service);
}
}
}
Expand All @@ -447,23 +422,70 @@ public void run() {

}

public String serviceIdForFilter(String filter) {
String serviceId = "";
private void notifyListenersOfNewService(ServiceDescription service) {
List<String> serviceIds = serviceIdsForFilter(service.getServiceFilter());

for (String serviceId : serviceIds) {
ServiceDescription _newService = service.clone();
_newService.setServiceID(serviceId);

final ServiceDescription newService = _newService;

Util.runOnUI(new Runnable() {

@Override
public void run() {

for (DiscoveryProviderListener listener : serviceListeners) {
listener.onServiceAdded(SSDPDiscoveryProvider.this, newService);
}
}
});
}
}

private void notifyListenersOfLostService(ServiceDescription service) {
List<String> serviceIds = serviceIdsForFilter(service.getServiceFilter());

for (String serviceId : serviceIds) {
ServiceDescription _newService = service.clone();
_newService.setServiceID(serviceId);

final ServiceDescription newService = _newService;

Util.runOnUI(new Runnable() {

@Override
public void run() {
for (DiscoveryProviderListener listener : serviceListeners) {
listener.onServiceRemoved(SSDPDiscoveryProvider.this, newService);
}
}
});
}
}

public List<String> serviceIdsForFilter(String filter) {
ArrayList<String> serviceIds = new ArrayList<String>();

for (JSONObject serviceFilter : serviceFilters) {
String ssdpFilter;
try {
ssdpFilter = serviceFilter.getString("filter");

if (ssdpFilter.equals(filter)) {
return serviceFilter.getString("serviceId");
String serviceId = serviceFilter.getString("serviceId");

if (serviceId != null)
serviceIds.add(serviceId);
}
} catch (JSONException e) {
e.printStackTrace();
continue;
}
}

return serviceId;
return serviceIds;
}

public boolean isSearchingForFilter(String filter) {
Expand Down
7 changes: 7 additions & 0 deletions src/com/connectsdk/service/DLNAService.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public DLNAService(ServiceDescription serviceDescription, ServiceConfig serviceC
ClientConnectionManager mgr = httpClient.getConnectionManager();
HttpParams params = httpClient.getParams();
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()), params);

updateControlURL();
}

public static JSONObject discoveryParameters() {
Expand All @@ -99,6 +101,10 @@ public static JSONObject discoveryParameters() {
public void setServiceDescription(ServiceDescription serviceDescription) {
super.setServiceDescription(serviceDescription);

updateControlURL();
}

private void updateControlURL() {
StringBuilder sb = new StringBuilder();
List<Service> serviceList = serviceDescription.getServiceList();

Expand Down Expand Up @@ -580,6 +586,7 @@ protected void updateCapabilities() {

capabilities.add(Display_Image);
capabilities.add(Display_Video);
capabilities.add(Display_Audio);
capabilities.add(Close);

capabilities.add(MetaData_Title);
Expand Down
31 changes: 13 additions & 18 deletions src/com/connectsdk/service/NetcastTVService.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ enum State {
public NetcastTVService(ServiceDescription serviceDescription, ServiceConfig serviceConfig) {
super(serviceDescription, serviceConfig);

dlnaService = new DLNAService(serviceDescription, serviceConfig);

if (serviceDescription.getPort() != 8080)
serviceDescription.setPort(8080);

Expand Down Expand Up @@ -183,9 +181,6 @@ public static JSONObject discoveryParameters() {
public void setServiceDescription(ServiceDescription serviceDescription) {
super.setServiceDescription(serviceDescription);

if (dlnaService != null)
dlnaService.setServiceDescription(serviceDescription);

if (serviceDescription.getPort() != 8080)
serviceDescription.setPort(8080);
}
Expand Down Expand Up @@ -1452,7 +1447,7 @@ public CapabilityPriorityLevel getMediaPlayerCapabilityLevel() {

@Override
public void displayImage(final String url, final String mimeType, final String title, final String description, final String iconSrc, final MediaPlayer.LaunchListener listener) {
if ( dlnaService != null ) {
if ( getDLNAService() != null ) {
final MediaPlayer.LaunchListener launchListener = new LaunchListener() {

@Override
Expand All @@ -1473,7 +1468,7 @@ public void onSuccess(MediaLaunchObject object) {
}
};

dlnaService.displayImage(url, mimeType, title, description, iconSrc, launchListener);
getDLNAService().displayImage(url, mimeType, title, description, iconSrc, launchListener);
}
else {
System.err.println("DLNA Service is not ready yet");
Expand All @@ -1482,7 +1477,7 @@ public void onSuccess(MediaLaunchObject object) {

@Override
public void playMedia(final String url, final String mimeType, final String title, final String description, final String iconSrc, final boolean shouldLoop, final MediaPlayer.LaunchListener listener) {
if ( dlnaService != null ) {
if ( getDLNAService() != null ) {
final MediaPlayer.LaunchListener launchListener = new LaunchListener() {

@Override
Expand All @@ -1503,7 +1498,7 @@ public void onSuccess(MediaLaunchObject object) {
}
};

dlnaService.playMedia(url, mimeType, title, description, iconSrc, shouldLoop, launchListener);
getDLNAService().playMedia(url, mimeType, title, description, iconSrc, shouldLoop, launchListener);
}
else {
System.err.println("DLNA Service is not ready yet");
Expand All @@ -1512,12 +1507,12 @@ public void onSuccess(MediaLaunchObject object) {

@Override
public void closeMedia(LaunchSession launchSession, ResponseListener<Object> listener) {
if (dlnaService == null) {
if (getDLNAService() == null) {
Util.postError(listener, new ServiceCommandError(0, "Service is not connected", null));
return;
}

dlnaService.closeMedia(launchSession, listener);
getDLNAService().closeMedia(launchSession, listener);
}

/******************
Expand All @@ -1526,7 +1521,7 @@ public void closeMedia(LaunchSession launchSession, ResponseListener<Object> lis
@Override
public MediaControl getMediaControl() {
if (DiscoveryManager.getInstance().getPairingLevel() == PairingLevel.OFF)
return this.dlnaService;
return this.getDLNAService();
else
return this;
};
Expand Down Expand Up @@ -1563,8 +1558,8 @@ public void fastForward(ResponseListener<Object> listener) {

@Override
public void seek(long position, ResponseListener<Object> listener) {
if ( dlnaService != null ) {
dlnaService.seek(position, listener);
if ( getDLNAService() != null ) {
getDLNAService().seek(position, listener);
} else {
if (listener != null)
Util.postError(listener, new ServiceCommandError(-1, "Command is not supported", null));
Expand All @@ -1573,8 +1568,8 @@ public void seek(long position, ResponseListener<Object> listener) {

@Override
public void getDuration(DurationListener listener) {
if ( dlnaService != null ) {
dlnaService.getDuration(listener);
if ( getDLNAService() != null ) {
getDLNAService().getDuration(listener);
} else {
if (listener != null)
Util.postError(listener, new ServiceCommandError(-1, "Command is not supported", null));
Expand All @@ -1583,8 +1578,8 @@ public void getDuration(DurationListener listener) {

@Override
public void getPosition(PositionListener listener) {
if ( dlnaService != null ) {
dlnaService.getPosition(listener);
if ( getDLNAService() != null ) {
getDLNAService().getPosition(listener);
} else {
if (listener != null)
Util.postError(listener, new ServiceCommandError(-1, "Command is not supported", null));
Expand Down
Loading

0 comments on commit b7c50e8

Please sign in to comment.