2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.allplay.internal;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Locale;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.allplay.internal.handler.AllPlayHandler;
22 import org.openhab.core.audio.AudioFormat;
23 import org.openhab.core.audio.AudioHTTPServer;
24 import org.openhab.core.audio.AudioSink;
25 import org.openhab.core.audio.AudioSinkAsync;
26 import org.openhab.core.audio.AudioStream;
27 import org.openhab.core.audio.StreamServed;
28 import org.openhab.core.audio.URLAudioStream;
29 import org.openhab.core.audio.UnsupportedAudioFormatException;
30 import org.openhab.core.audio.UnsupportedAudioStreamException;
31 import org.openhab.core.library.types.PercentType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import de.kaizencode.tchaikovsky.exception.SpeakerException;
38 * The {@link AllPlayAudioSink} make AllPlay speakers available as an {@link AudioSink}.
40 * @author Dominic Lerbs - Initial contribution
42 public class AllPlayAudioSink extends AudioSinkAsync {
44 private final Logger logger = LoggerFactory.getLogger(AllPlayAudioSink.class);
46 private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV);
47 private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Set.of(AudioStream.class);
48 private final AllPlayHandler handler;
49 private final AudioHTTPServer audioHTTPServer;
50 private final String callbackUrl;
53 * @param handler The related {@link AllPlayHandler}
54 * @param audioHTTPServer The {@link AudioHTTPServer} for serving the stream
55 * @param callbackUrl The callback URL to stream the audio from
57 public AllPlayAudioSink(AllPlayHandler handler, AudioHTTPServer audioHTTPServer, String callbackUrl) {
58 this.handler = handler;
59 this.audioHTTPServer = audioHTTPServer;
60 this.callbackUrl = callbackUrl;
64 public String getId() {
65 return handler.getThing().getUID().toString();
69 public String getLabel(Locale locale) {
70 return handler.getThing().getLabel();
74 protected void processAsynchronously(@Nullable AudioStream audioStream)
75 throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
76 if (audioStream == null) {
80 if (audioStream instanceof URLAudioStream urlAudioStream) {
81 // it is an external URL, the speaker can access it itself and play it
82 url = urlAudioStream.getURL();
83 tryClose(audioStream);
84 } else if (callbackUrl != null) {
85 StreamServed streamServed;
87 streamServed = audioHTTPServer.serve(audioStream, 10, true);
88 } catch (IOException e) {
89 tryClose(audioStream);
90 throw new UnsupportedAudioStreamException(
91 "AllPlay was not able to handle the audio stream (cache on disk failed).",
92 audioStream.getClass(), e);
94 url = callbackUrl + streamServed.url();
95 streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream));
97 logger.warn("We do not have any callback url, so AllPlay cannot play the audio stream!");
98 tryClose(audioStream);
102 handler.playUrl(url);
103 } catch (SpeakerException e) {
104 if (logger.isDebugEnabled()) {
105 logger.warn("Unable to play audio stream on speaker {}", getId(), e);
107 logger.warn("Unable to play audio stream on speaker {}: {}", getId(), e.getMessage());
113 public Set<AudioFormat> getSupportedFormats() {
114 return SUPPORTED_FORMATS;
118 public Set<Class<? extends AudioStream>> getSupportedStreams() {
119 return SUPPORTED_STREAMS;
123 public PercentType getVolume() throws IOException {
125 return handler.getVolume();
126 } catch (SpeakerException e) {
127 throw new IOException(e);
132 public void setVolume(PercentType volume) throws IOException {
134 handler.handleVolumeCommand(volume);
135 } catch (SpeakerException e) {
136 throw new IOException(e);
140 private void tryClose(@Nullable InputStream is) {
144 } catch (IOException ignored) {