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.chromecast.internal;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Locale;
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;
36 * Handles the AudioSink portion of the Chromecast add-on.
38 * @author Jason Holmes - Initial contribution
41 public class ChromecastAudioSink extends AudioSinkAsync {
42 private final Logger logger = LoggerFactory.getLogger(ChromecastAudioSink.class);
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);
47 private static final String MIME_TYPE_AUDIO_WAV = "audio/wav";
48 private static final String MIME_TYPE_AUDIO_MPEG = "audio/mpeg";
50 private final ChromecastHandler handler;
51 private final AudioHTTPServer audioHTTPServer;
52 private final @Nullable String callbackUrl;
54 public ChromecastAudioSink(ChromecastHandler handler, AudioHTTPServer audioHTTPServer,
55 @Nullable String callbackUrl) {
56 this.handler = handler;
57 this.audioHTTPServer = audioHTTPServer;
58 this.callbackUrl = callbackUrl;
62 public String getId() {
63 return handler.getThing().getUID().toString();
67 public @Nullable String getLabel(@Nullable Locale locale) {
68 return handler.getThing().getLabel();
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
77 logger.trace("Stop currently playing stream.");
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);
86 if (callbackUrl != null) {
87 // we serve it on our own HTTP server
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);
100 url = callbackUrl + relativeUrl;
102 logger.warn("We do not have any callback url, so Chromecast cannot play the audio stream!");
103 tryClose(audioStream);
107 handler.playURL("Notification", url,
108 AudioFormat.MP3.isCompatible(audioStream.getFormat()) ? MIME_TYPE_AUDIO_MPEG : MIME_TYPE_AUDIO_WAV);
112 private void tryClose(@Nullable InputStream is) {
116 } catch (IOException ignored) {
122 public Set<AudioFormat> getSupportedFormats() {
123 return SUPPORTED_FORMATS;
127 public Set<Class<? extends AudioStream>> getSupportedStreams() {
128 return SUPPORTED_STREAMS;
132 public PercentType getVolume() throws IOException {
133 return handler.getVolume();
137 public void setVolume(PercentType percentType) throws IOException {
138 handler.setVolume(percentType);