]> git.basschouten.com Git - openhab-addons.git/blob
774505dd43254687301b30824d75edbb65feb236
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.allplay.internal;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Locale;
18 import java.util.Set;
19
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;
34
35 import de.kaizencode.tchaikovsky.exception.SpeakerException;
36
37 /**
38  * The {@link AllPlayAudioSink} make AllPlay speakers available as an {@link AudioSink}.
39  *
40  * @author Dominic Lerbs - Initial contribution
41  */
42 public class AllPlayAudioSink extends AudioSinkAsync {
43
44     private final Logger logger = LoggerFactory.getLogger(AllPlayAudioSink.class);
45
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;
51
52     /**
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
56      */
57     public AllPlayAudioSink(AllPlayHandler handler, AudioHTTPServer audioHTTPServer, String callbackUrl) {
58         this.handler = handler;
59         this.audioHTTPServer = audioHTTPServer;
60         this.callbackUrl = callbackUrl;
61     }
62
63     @Override
64     public String getId() {
65         return handler.getThing().getUID().toString();
66     }
67
68     @Override
69     public String getLabel(Locale locale) {
70         return handler.getThing().getLabel();
71     }
72
73     @Override
74     protected void processAsynchronously(@Nullable AudioStream audioStream)
75             throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
76         if (audioStream == null) {
77             return;
78         }
79         String url;
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;
86             try {
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);
93             }
94             url = callbackUrl + streamServed.url();
95             streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream));
96         } else {
97             logger.warn("We do not have any callback url, so AllPlay cannot play the audio stream!");
98             tryClose(audioStream);
99             return;
100         }
101         try {
102             handler.playUrl(url);
103         } catch (SpeakerException e) {
104             if (logger.isDebugEnabled()) {
105                 logger.warn("Unable to play audio stream on speaker {}", getId(), e);
106             } else {
107                 logger.warn("Unable to play audio stream on speaker {}: {}", getId(), e.getMessage());
108             }
109         }
110     }
111
112     @Override
113     public Set<AudioFormat> getSupportedFormats() {
114         return SUPPORTED_FORMATS;
115     }
116
117     @Override
118     public Set<Class<? extends AudioStream>> getSupportedStreams() {
119         return SUPPORTED_STREAMS;
120     }
121
122     @Override
123     public PercentType getVolume() throws IOException {
124         try {
125             return handler.getVolume();
126         } catch (SpeakerException e) {
127             throw new IOException(e);
128         }
129     }
130
131     @Override
132     public void setVolume(PercentType volume) throws IOException {
133         try {
134             handler.handleVolumeCommand(volume);
135         } catch (SpeakerException e) {
136             throw new IOException(e);
137         }
138     }
139
140     private void tryClose(@Nullable InputStream is) {
141         if (is != null) {
142             try {
143                 is.close();
144             } catch (IOException ignored) {
145             }
146         }
147     }
148 }