]> git.basschouten.com Git - openhab-addons.git/blob
80a6ab3c3a6fc7bd1cd120c6294b2a6cd2244fc7
[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) {
82                 // it is an external URL, the speaker can access it itself and play it.
83                 URLAudioStream urlAudioStream = (URLAudioStream) audioStream;
84                 url = urlAudioStream.getURL();
85                 tryClose(audioStream);
86             } else {
87                 if (callbackUrl != null) {
88                     // we serve it on our own HTTP server
89                     String relativeUrl;
90                     try {
91                         StreamServed streamServed = audioHTTPServer.serve(audioStream, 10, true);
92                         relativeUrl = streamServed.url();
93                         // we have to run the delayed task when the server has completely played the stream
94                         streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream));
95                     } catch (IOException e) {
96                         tryClose(audioStream);
97                         throw new UnsupportedAudioStreamException(
98                                 "Chromecast binding was not able to handle the audio stream (cache on disk failed)",
99                                 audioStream.getClass(), e);
100                     }
101                     url = callbackUrl + relativeUrl;
102                 } else {
103                     logger.warn("We do not have any callback url, so Chromecast cannot play the audio stream!");
104                     tryClose(audioStream);
105                     return;
106                 }
107             }
108             handler.playURL("Notification", url,
109                     AudioFormat.MP3.isCompatible(audioStream.getFormat()) ? MIME_TYPE_AUDIO_MPEG : MIME_TYPE_AUDIO_WAV);
110         }
111     }
112
113     private void tryClose(@Nullable InputStream is) {
114         if (is != null) {
115             try {
116                 is.close();
117             } catch (IOException ignored) {
118             }
119         }
120     }
121
122     @Override
123     public Set<AudioFormat> getSupportedFormats() {
124         return SUPPORTED_FORMATS;
125     }
126
127     @Override
128     public Set<Class<? extends AudioStream>> getSupportedStreams() {
129         return SUPPORTED_STREAMS;
130     }
131
132     @Override
133     public PercentType getVolume() throws IOException {
134         return handler.getVolume();
135     }
136
137     @Override
138     public void setVolume(PercentType percentType) throws IOException {
139         handler.setVolume(percentType);
140     }
141 }