Skip to content

Commit

Permalink
[freebox] Support for more audio streams through the HTTP audio servlet
Browse files Browse the repository at this point in the history
Related to openhab#15113

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Jun 19, 2023
1 parent 07e6403 commit b1c10ad
Showing 1 changed file with 29 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
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;
Expand All @@ -43,9 +44,10 @@
* This makes an AirPlay device to serve as an {@link AudioSink}-
*
* @author Laurent Garnier - Initial contribution for AudioSink and notifications
* @author Laurent Garnier - Support for more audio streams through the HTTP audio servlet
*/
@NonNullByDefault
public class FreeboxAirPlayAudioSink implements AudioSink {
public class FreeboxAirPlayAudioSink extends AudioSinkAsync {

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

Expand All @@ -59,15 +61,11 @@ public class FreeboxAirPlayAudioSink implements AudioSink {
private static final AudioFormat MP3_320 = new AudioFormat(CONTAINER_NONE, CODEC_MP3, null, null, 320000, null);

private static final Set<AudioFormat> SUPPORTED_FORMATS = new HashSet<>();
private static final HashSet<Class<? extends AudioStream>> SUPPORTED_STREAMS = new HashSet<>();
private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Set.of(AudioStream.class);
private AudioHTTPServer audioHTTPServer;
private FreeboxThingHandler handler;
private @Nullable String callbackUrl;

static {
SUPPORTED_STREAMS.add(AudioStream.class);
}

public FreeboxAirPlayAudioSink(FreeboxThingHandler handler, AudioHTTPServer audioHTTPServer,
@Nullable String callbackUrl) {
this.handler = handler;
Expand Down Expand Up @@ -103,7 +101,7 @@ public String getId() {
}

@Override
public void process(@Nullable AudioStream audioStream)
protected void processAsynchronously(@Nullable AudioStream audioStream)
throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
if (!ThingHandlerHelper.isHandlerInitialized(handler)
|| ((handler.getThing().getStatus() == ThingStatus.OFFLINE)
Expand All @@ -122,36 +120,37 @@ public void process(@Nullable AudioStream audioStream)
return;
}

String url = null;
if (audioStream instanceof URLAudioStream) {
if (audioStream instanceof URLAudioStream urlAudioStream) {
// it is an external URL, we can access it directly
URLAudioStream urlAudioStream = (URLAudioStream) audioStream;
url = urlAudioStream.getURL();
} else {
if (callbackUrl != null) {
// we serve it on our own HTTP server
String relativeUrl;
if (audioStream instanceof FixedLengthAudioStream) {
relativeUrl = audioHTTPServer.serve((FixedLengthAudioStream) audioStream, 20);
} else {
relativeUrl = audioHTTPServer.serve(audioStream);
}
url = callbackUrl + relativeUrl;
} else {
logger.warn("We do not have any callback url, so AirPlay device cannot play the audio stream!");
try {
logger.debug("AirPlay audio sink: process url {}", urlAudioStream.getURL());
handler.playMedia(urlAudioStream.getURL());
} catch (FreeboxException e) {
logger.warn("Audio stream playback failed: {}", e.getMessage());
}
} else if (callbackUrl != null) {
// we serve it on our own HTTP server
StreamServed streamServed;
try {
streamServed = audioHTTPServer.serve(audioStream, 20, true);
} catch (IOException e) {
throw new UnsupportedAudioStreamException("AirPlay device can only handle clonable audio streams.",
audioStream.getClass(), e);
}
try {
logger.debug("AirPlay audio sink: process url {}", callbackUrl + streamServed.url());
handler.playMedia(callbackUrl + streamServed.url());
} catch (FreeboxException e) {
logger.warn("Audio stream playback failed: {}", e.getMessage());
}
} else {
logger.warn("We do not have any callback url, so AirPlay device cannot play the audio stream!");
}
try {
audioStream.close();
} catch (IOException e) {
logger.debug("Exception while closing audioStream", e);
}
try {
logger.debug("AirPlay audio sink: process url {}", url);
handler.playMedia(url);
} catch (FreeboxException e) {
logger.warn("Audio stream playback failed: {}", e.getMessage());
}
}

@Override
Expand Down

0 comments on commit b1c10ad

Please sign in to comment.