From 676f53b55abf30005cb27d8b18546a256c705104 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sun, 2 Jul 2023 11:23:41 +0200 Subject: [PATCH] =?UTF-8?q?[upnpcontrol]=20Support=20for=20more=20audio=20?= =?UTF-8?q?streams=20through=20the=20HTTP=20audio=20s=E2=80=A6=20(#15122)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [upnpcontrol] Support for more audio streams through the HTTP audio servlet Related to #15113 Signed-off-by: Laurent Garnier --- .../internal/audiosink/UpnpAudioSink.java | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/bundles/org.openhab.binding.upnpcontrol/src/main/java/org/openhab/binding/upnpcontrol/internal/audiosink/UpnpAudioSink.java b/bundles/org.openhab.binding.upnpcontrol/src/main/java/org/openhab/binding/upnpcontrol/internal/audiosink/UpnpAudioSink.java index fcb39dba2..7a7425699 100644 --- a/bundles/org.openhab.binding.upnpcontrol/src/main/java/org/openhab/binding/upnpcontrol/internal/audiosink/UpnpAudioSink.java +++ b/bundles/org.openhab.binding.upnpcontrol/src/main/java/org/openhab/binding/upnpcontrol/internal/audiosink/UpnpAudioSink.java @@ -15,17 +15,15 @@ package org.openhab.binding.upnpcontrol.internal.audiosink; import java.io.IOException; import java.util.Locale; 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.Nullable; import org.openhab.binding.upnpcontrol.internal.handler.UpnpRendererHandler; import org.openhab.core.audio.AudioFormat; 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.FixedLengthAudioStream; +import org.openhab.core.audio.StreamServed; import org.openhab.core.audio.URLAudioStream; import org.openhab.core.audio.UnsupportedAudioFormatException; import org.openhab.core.audio.UnsupportedAudioStreamException; @@ -36,14 +34,14 @@ import org.slf4j.LoggerFactory; /** * * @author Mark Herwege - Initial contribution + * @author Laurent Garnier - Support for more audio streams through the HTTP audio servlet */ @NonNullByDefault -public class UpnpAudioSink implements AudioSink { +public class UpnpAudioSink extends AudioSinkAsync { private final Logger logger = LoggerFactory.getLogger(UpnpAudioSink.class); - private static final Set> SUPPORTED_STREAMS = Stream - .of(AudioStream.class, FixedLengthAudioStream.class).collect(Collectors.toSet()); + private static final Set> SUPPORTED_STREAMS = Set.of(AudioStream.class); protected UpnpRendererHandler handler; protected AudioHTTPServer audioHTTPServer; protected String callbackUrl; @@ -65,27 +63,41 @@ public class UpnpAudioSink implements AudioSink { } @Override - public void process(@Nullable AudioStream audioStream) + protected void processAsynchronously(@Nullable AudioStream audioStream) throws UnsupportedAudioFormatException, UnsupportedAudioStreamException { if (audioStream == null) { stopMedia(); return; } - String url = null; - if (audioStream instanceof URLAudioStream) { - URLAudioStream urlAudioStream = (URLAudioStream) audioStream; - url = urlAudioStream.getURL(); + if (audioStream instanceof URLAudioStream urlAudioStream) { + playMedia(urlAudioStream.getURL()); + try { + audioStream.close(); + } catch (IOException e) { + } } else if (!callbackUrl.isEmpty()) { - String relativeUrl = audioStream instanceof FixedLengthAudioStream - ? audioHTTPServer.serve((FixedLengthAudioStream) audioStream, 20) - : audioHTTPServer.serve(audioStream); - url = String.valueOf(this.callbackUrl) + relativeUrl; + StreamServed streamServed; + try { + streamServed = audioHTTPServer.serve(audioStream, 5, true); + } 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 { 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