Skip to content

Commit

Permalink
[sonos] Audio sink supporting more audio streams
Browse files Browse the repository at this point in the history
Related to #15113

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Jun 19, 2023
1 parent 3c5ce72 commit 28dd916
Showing 1 changed file with 55 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@
package org.openhab.binding.sonos.internal;

import java.io.IOException;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.concurrent.CompletableFuture;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.sonos.internal.handler.ZonePlayerHandler;
import org.openhab.core.audio.AudioFormat;
import org.openhab.core.audio.AudioHTTPServer;
import org.openhab.core.audio.AudioSink;
import org.openhab.core.audio.AudioSinkSync;
import org.openhab.core.audio.AudioStream;
import org.openhab.core.audio.FileAudioStream;
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;
Expand All @@ -44,17 +43,16 @@
*
* @author Kai Kreuzer - Initial contribution and API
* @author Christoph Weitkamp - Added getSupportedStreams() and UnsupportedAudioStreamException
* @author Laurent Garnier - Support for more audio streams through the HTTP audio servlet
*
*/
@NonNullByDefault
public class SonosAudioSink implements AudioSink {
public class SonosAudioSink extends AudioSinkSync {

private final Logger logger = LoggerFactory.getLogger(SonosAudioSink.class);

private static final Set<AudioFormat> SUPPORTED_AUDIO_FORMATS = Collections
.unmodifiableSet(Stream.of(AudioFormat.MP3, AudioFormat.WAV).collect(Collectors.toSet()));
private static final Set<Class<? extends AudioStream>> SUPPORTED_AUDIO_STREAMS = Collections
.unmodifiableSet(Stream.of(FixedLengthAudioStream.class, URLAudioStream.class).collect(Collectors.toSet()));
private static final Set<AudioFormat> SUPPORTED_AUDIO_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV);
private static final Set<Class<? extends AudioStream>> SUPPORTED_AUDIO_STREAMS = Set.of(AudioStream.class);

private AudioHTTPServer audioHTTPServer;
private ZonePlayerHandler handler;
Expand All @@ -76,29 +74,65 @@ public String getId() {
return handler.getThing().getLabel();
}

@Override
public CompletableFuture<@Nullable Void> processAndComplete(@Nullable AudioStream audioStream) {
if (audioStream instanceof URLAudioStream) {
// Asynchronous handling for URLAudioStream
CompletableFuture<@Nullable Void> completableFuture = new CompletableFuture<@Nullable Void>();
try {
processAsynchronously(audioStream);
} catch (UnsupportedAudioFormatException | UnsupportedAudioStreamException e) {
completableFuture.completeExceptionally(e);
}
return completableFuture;
} else {
return super.processAndComplete(audioStream);
}
}

@Override
public void process(@Nullable AudioStream audioStream)
throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
if (audioStream == null) {
// in case the audioStream is null, this should be interpreted as a request to end any currently playing
// stream.
logger.trace("Stop currently playing stream.");
handler.stopPlaying(OnOffType.ON);
} else if (audioStream instanceof URLAudioStream) {
if (audioStream instanceof URLAudioStream) {
processAsynchronously(audioStream);
} else {
processSynchronously(audioStream);
}
}

private void processAsynchronously(@Nullable AudioStream audioStream)
throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
if (audioStream instanceof URLAudioStream urlAudioStream) {
// it is an external URL, the speaker can access it itself and play it.
URLAudioStream urlAudioStream = (URLAudioStream) audioStream;
handler.playURI(new StringType(urlAudioStream.getURL()));
try {
audioStream.close();
} catch (IOException e) {
}
} else if (audioStream instanceof FixedLengthAudioStream) {
}
}

@Override
protected void processSynchronously(@Nullable AudioStream audioStream)
throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
if (audioStream == null) {
// in case the audioStream is null, this should be interpreted as a request to end any currently playing
// stream.
logger.trace("Stop currently playing stream.");
handler.stopPlaying(OnOffType.ON);
} else if (!(audioStream instanceof URLAudioStream)) {
// we serve it on our own HTTP server and treat it as a notification
// Note that we have to pass a FixedLengthAudioStream, since Sonos does multiple concurrent requests to
// the AudioServlet, so a one time serving won't work.
// Note that Sonos does multiple concurrent requests to the AudioServlet,
// so a one time serving won't work.
if (callbackUrl != null) {
String relativeUrl = audioHTTPServer.serve((FixedLengthAudioStream) audioStream, 10).toString();
String url = callbackUrl + relativeUrl;
StreamServed streamServed;
try {
streamServed = audioHTTPServer.serve(audioStream, 10, true);
} catch (IOException e) {
throw new UnsupportedAudioStreamException("Sonos can only handle clonable audio streams.",
audioStream.getClass(), e);
}
String url = callbackUrl + streamServed.url();

AudioFormat format = audioStream.getFormat();
if (!ThingHandlerHelper.isHandlerInitialized(handler)) {
Expand All @@ -116,16 +150,10 @@ public void process(@Nullable AudioStream audioStream)
} else {
logger.warn("We do not have any callback url, so Sonos cannot play the audio stream!");
}
} else {
try {
audioStream.close();
} catch (IOException e) {
}
throw new UnsupportedAudioStreamException(
"Sonos can only handle FixedLengthAudioStreams and URLAudioStreams.", audioStream.getClass());
// Instead of throwing an exception, we could ourselves try to wrap it into a
// FixedLengthAudioStream, but this might be dangerous as we have no clue, how much data to expect from
// the stream.
}
}

Expand Down

0 comments on commit 28dd916

Please sign in to comment.