]> git.basschouten.com Git - openhab-addons.git/blob
04decc6065df59c48fff7c8cead04095c3f7ca3a
[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.chromecast.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.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.chromecast.internal.handler.ChromecastHandler;
23 import org.openhab.core.audio.AudioFormat;
24 import org.openhab.core.audio.AudioHTTPServer;
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 /**
36  * Handles the AudioSink portion of the Chromecast add-on.
37  *
38  * @author Jason Holmes - Initial contribution
39  */
40 @NonNullByDefault
41 public class ChromecastAudioSink extends AudioSinkAsync {
42     private final Logger logger = LoggerFactory.getLogger(ChromecastAudioSink.class);
43
44     private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV);
45     private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Set.of(AudioStream.class);
46
47     private static final String MIME_TYPE_AUDIO_WAV = "audio/wav";
48     private static final String MIME_TYPE_AUDIO_MPEG = "audio/mpeg";
49
50     private final ChromecastHandler handler;
51     private final AudioHTTPServer audioHTTPServer;
52     private final @Nullable String callbackUrl;
53
54     public ChromecastAudioSink(ChromecastHandler handler, AudioHTTPServer audioHTTPServer,
55             @Nullable String callbackUrl) {
56         this.handler = handler;
57         this.audioHTTPServer = audioHTTPServer;
58         this.callbackUrl = callbackUrl;
59     }
60
61     @Override
62     public String getId() {
63         return handler.getThing().getUID().toString();
64     }
65
66     @Override
67     public @Nullable String getLabel(@Nullable Locale locale) {
68         return handler.getThing().getLabel();
69     }
70
71     @Override
72     public void processAsynchronously(@Nullable AudioStream audioStream)
73             throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
74         if (audioStream == null) {
75             // in case the audioStream is null, this should be interpreted as a request to end any currently playing
76             // stream.
77             logger.trace("Stop currently playing stream.");
78             handler.stop();
79         } else {
80             final String url;
81             if (audioStream instanceof URLAudioStream urlAudioStream) {
82                 // it is an external URL, the speaker can access it itself and play it
83                 url = urlAudioStream.getURL();
84                 tryClose(audioStream);
85             } else {
86                 if (callbackUrl != null) {
87                     // we serve it on our own HTTP server
88                     String relativeUrl;
89                     try {
90                         StreamServed streamServed = audioHTTPServer.serve(audioStream, 10, true);
91                         relativeUrl = streamServed.url();
92                         // we have to run the delayed task when the server has completely played the stream
93                         streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream));
94                     } catch (IOException e) {
95                         tryClose(audioStream);
96                         throw new UnsupportedAudioStreamException(
97                                 "Chromecast binding was not able to handle the audio stream (cache on disk failed)",
98                                 audioStream.getClass(), e);
99                     }
100                     url = callbackUrl + relativeUrl;
101                 } else {
102                     logger.warn("We do not have any callback url, so Chromecast cannot play the audio stream!");
103                     tryClose(audioStream);
104                     return;
105                 }
106             }
107             handler.playURL("Notification", url,
108                     AudioFormat.MP3.isCompatible(audioStream.getFormat()) ? MIME_TYPE_AUDIO_MPEG : MIME_TYPE_AUDIO_WAV);
109         }
110     }
111
112     private void tryClose(@Nullable InputStream is) {
113         if (is != null) {
114             try {
115                 is.close();
116             } catch (IOException ignored) {
117             }
118         }
119     }
120
121     @Override
122     public Set<AudioFormat> getSupportedFormats() {
123         return SUPPORTED_FORMATS;
124     }
125
126     @Override
127     public Set<Class<? extends AudioStream>> getSupportedStreams() {
128         return SUPPORTED_STREAMS;
129     }
130
131     @Override
132     public PercentType getVolume() throws IOException {
133         return handler.getVolume();
134     }
135
136     @Override
137     public void setVolume(PercentType percentType) throws IOException {
138         handler.setVolume(percentType);
139     }
140 }