Only enable upnp when sonos or dlna is enabled

Signed-off-by: Andrew DeMaria <lostonamountain@gmail.com>
master
Andrew DeMaria 7 years ago
parent 87ed516049
commit 27454e6d41
No known key found for this signature in database
GPG Key ID: 0A3F5E91F8364EDF
  1. 78
      libresonic-main/src/main/java/org/libresonic/player/service/UPnPService.java

@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
/** /**
* @author Sindre Mehus * @author Sindre Mehus
@ -53,24 +54,61 @@ public class UPnPService {
private SettingsService settingsService; private SettingsService settingsService;
private UpnpService upnpService; private UpnpService upnpService;
private FolderBasedContentDirectory folderBasedContentDirectory; private FolderBasedContentDirectory folderBasedContentDirectory;
private AtomicReference<Boolean> running = new AtomicReference<>(false);
public void init() { public void init() {
startService(); if(settingsService.isDlnaEnabled() || settingsService.isSonosEnabled()) {
ensureServiceStarted();
if(settingsService.isDlnaEnabled()) {
// Start DLNA media server?
setMediaServerEnabled(true);
}
}
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
ensureServiceStopped();
}
});
} }
public void startService() { public void ensureServiceStarted() {
Runnable runnable = new Runnable() { running.getAndUpdate(bo -> {
public void run() { if(!bo) {
try { startService();
LOG.info("Starting UPnP service..."); return true;
createService(); } else {
LOG.info("Starting UPnP service - Done!"); return true;
} catch (Throwable x) { }
LOG.error("Failed to start UPnP service: " + x, x); });
}
public void ensureServiceStopped() {
running.getAndUpdate(bo -> {
if (bo) {
if (upnpService != null) {
LOG.info("Disabling UPnP/DLNA media server");
upnpService.getRegistry().removeAllLocalDevices();
System.err.println("Shutting down UPnP service...");
upnpService.shutdown();
System.err.println("Shutting down UPnP service - Done!");
} }
return false;
} else {
return false;
} }
}; });
new Thread(runnable).start();
}
private void startService() {
try {
LOG.info("Starting UPnP service...");
createService();
LOG.info("Starting UPnP service - Done!");
} catch (Throwable x) {
LOG.error("Failed to start UPnP service: " + x, x);
}
} }
private synchronized void createService() throws Exception { private synchronized void createService() throws Exception {
@ -79,21 +117,11 @@ public class UPnPService {
// Asynch search for other devices (most importantly UPnP-enabled routers for port-mapping) // Asynch search for other devices (most importantly UPnP-enabled routers for port-mapping)
upnpService.getControlPoint().search(); upnpService.getControlPoint().search();
// Start DLNA media server?
setMediaServerEnabled(settingsService.isDlnaEnabled());
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.err.println("Shutting down UPnP service...");
upnpService.shutdown();
System.err.println("Shutting down UPnP service - Done!");
}
});
} }
public void setMediaServerEnabled(boolean enabled) { public void setMediaServerEnabled(boolean enabled) {
if (enabled) { if (enabled) {
ensureServiceStarted();
try { try {
upnpService.getRegistry().addDevice(createMediaServerDevice()); upnpService.getRegistry().addDevice(createMediaServerDevice());
LOG.info("Enabling UPnP/DLNA media server"); LOG.info("Enabling UPnP/DLNA media server");
@ -101,8 +129,7 @@ public class UPnPService {
LOG.error("Failed to start UPnP/DLNA media server: " + x, x); LOG.error("Failed to start UPnP/DLNA media server: " + x, x);
} }
} else { } else {
upnpService.getRegistry().removeAllLocalDevices(); ensureServiceStopped();
LOG.info("Disabling UPnP/DLNA media server");
} }
} }
@ -157,6 +184,7 @@ public class UPnPService {
} }
public List<String> getSonosControllerHosts() { public List<String> getSonosControllerHosts() {
ensureServiceStarted();
List<String> result = new ArrayList<String>(); List<String> result = new ArrayList<String>();
for (Device device : upnpService.getRegistry().getDevices(new DeviceType("schemas-upnp-org", "ZonePlayer"))) { for (Device device : upnpService.getRegistry().getDevices(new DeviceType("schemas-upnp-org", "ZonePlayer"))) {
if (device instanceof RemoteDevice) { if (device instanceof RemoteDevice) {

Loading…
Cancel
Save