[upnpcontrol] Support for more audio streams through the HTTP audio s… (#15122)

* [upnpcontrol] Support for more audio streams through the HTTP audio servlet

Related to #15113

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2023-07-02 11:23:41 +02:00 committed by GitHub
parent 070de816f3
commit 676f53b55a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 18 deletions

View File

@ -15,17 +15,15 @@ package org.openhab.binding.upnpcontrol.internal.audiosink;
import java.io.IOException; import java.io.IOException;
import java.util.Locale; import java.util.Locale;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.upnpcontrol.internal.handler.UpnpRendererHandler; import org.openhab.binding.upnpcontrol.internal.handler.UpnpRendererHandler;
import org.openhab.core.audio.AudioFormat; import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.AudioHTTPServer; import org.openhab.core.audio.AudioHTTPServer;
import org.openhab.core.audio.AudioSink; import org.openhab.core.audio.AudioSinkAsync;
import org.openhab.core.audio.AudioStream; import org.openhab.core.audio.AudioStream;
import org.openhab.core.audio.FixedLengthAudioStream; import org.openhab.core.audio.StreamServed;
import org.openhab.core.audio.URLAudioStream; import org.openhab.core.audio.URLAudioStream;
import org.openhab.core.audio.UnsupportedAudioFormatException; import org.openhab.core.audio.UnsupportedAudioFormatException;
import org.openhab.core.audio.UnsupportedAudioStreamException; import org.openhab.core.audio.UnsupportedAudioStreamException;
@ -36,14 +34,14 @@ import org.slf4j.LoggerFactory;
/** /**
* *
* @author Mark Herwege - Initial contribution * @author Mark Herwege - Initial contribution
* @author Laurent Garnier - Support for more audio streams through the HTTP audio servlet
*/ */
@NonNullByDefault @NonNullByDefault
public class UpnpAudioSink implements AudioSink { public class UpnpAudioSink extends AudioSinkAsync {
private final Logger logger = LoggerFactory.getLogger(UpnpAudioSink.class); private final Logger logger = LoggerFactory.getLogger(UpnpAudioSink.class);
private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Stream private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Set.of(AudioStream.class);
.of(AudioStream.class, FixedLengthAudioStream.class).collect(Collectors.toSet());
protected UpnpRendererHandler handler; protected UpnpRendererHandler handler;
protected AudioHTTPServer audioHTTPServer; protected AudioHTTPServer audioHTTPServer;
protected String callbackUrl; protected String callbackUrl;
@ -65,27 +63,41 @@ public class UpnpAudioSink implements AudioSink {
} }
@Override @Override
public void process(@Nullable AudioStream audioStream) protected void processAsynchronously(@Nullable AudioStream audioStream)
throws UnsupportedAudioFormatException, UnsupportedAudioStreamException { throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
if (audioStream == null) { if (audioStream == null) {
stopMedia(); stopMedia();
return; return;
} }
String url = null; if (audioStream instanceof URLAudioStream urlAudioStream) {
if (audioStream instanceof URLAudioStream) { playMedia(urlAudioStream.getURL());
URLAudioStream urlAudioStream = (URLAudioStream) audioStream; try {
url = urlAudioStream.getURL(); audioStream.close();
} catch (IOException e) {
}
} else if (!callbackUrl.isEmpty()) { } else if (!callbackUrl.isEmpty()) {
String relativeUrl = audioStream instanceof FixedLengthAudioStream StreamServed streamServed;
? audioHTTPServer.serve((FixedLengthAudioStream) audioStream, 20) try {
: audioHTTPServer.serve(audioStream); streamServed = audioHTTPServer.serve(audioStream, 5, true);
url = String.valueOf(this.callbackUrl) + relativeUrl; } catch (IOException e) {
try {
audioStream.close();
} catch (IOException ex) {
}
throw new UnsupportedAudioStreamException(
handler.getUDN() + " was not able to handle the audio stream (cache on disk failed).",
audioStream.getClass(), e);
}
streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream));
playMedia(callbackUrl + streamServed.url());
} else { } else {
logger.warn("We do not have any callback url, so {} cannot play the audio stream!", handler.getUDN()); logger.warn("We do not have any callback url, so {} cannot play the audio stream!", handler.getUDN());
return; try {
audioStream.close();
} catch (IOException e) {
}
} }
playMedia(url);
} }
@Override @Override