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.library.types.PercentType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Handles the AudioSink portion of the Chromecast add-on.
37 * @author Jason Holmes - Initial contribution
40 public class ChromecastAudioSink extends AudioSinkAsync {
41 private final Logger logger = LoggerFactory.getLogger(ChromecastAudioSink.class);
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);
46 private static final String MIME_TYPE_AUDIO_WAV = "audio/wav";
47 private static final String MIME_TYPE_AUDIO_MPEG = "audio/mpeg";
49 private final ChromecastHandler handler;
50 private final AudioHTTPServer audioHTTPServer;
51 private final @Nullable String callbackUrl;
53 public ChromecastAudioSink(ChromecastHandler handler, AudioHTTPServer audioHTTPServer,
54 @Nullable String callbackUrl) {
55 this.handler = handler;
56 this.audioHTTPServer = audioHTTPServer;
57 this.callbackUrl = callbackUrl;
61 public String getId() {
62 return handler.getThing().getUID().toString();
66 public @Nullable String getLabel(@Nullable Locale locale) {
67 return handler.getThing().getLabel();
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
75 logger.trace("Stop currently playing stream.");
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);
85 if (callbackUrl != null) {
86 // we serve it on our own HTTP server
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)",
96 tryClose(audioStream);
99 url = callbackUrl + relativeUrl;
101 logger.warn("We do not have any callback url, so Chromecast cannot play the audio stream!");
102 tryClose(audioStream);
106 handler.playURL("Notification", url,
107 AudioFormat.MP3.isCompatible(audioStream.getFormat()) ? MIME_TYPE_AUDIO_MPEG : MIME_TYPE_AUDIO_WAV);
111 private void tryClose(@Nullable InputStream is) {
115 } catch (IOException ignored) {
121 public Set<AudioFormat> getSupportedFormats() {
122 return SUPPORTED_FORMATS;
126 public Set<Class<? extends AudioStream>> getSupportedStreams() {
127 return SUPPORTED_STREAMS;
131 public PercentType getVolume() throws IOException {
132 return handler.getVolume();
136 public void setVolume(PercentType percentType) throws IOException {
137 handler.setVolume(percentType);