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