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.heos.internal.api;
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.heos.internal.handler.HeosThingBaseHandler;
23 import org.openhab.binding.heos.internal.resources.Telnet.ReadException;
24 import org.openhab.core.audio.AudioFormat;
25 import org.openhab.core.audio.AudioHTTPServer;
26 import org.openhab.core.audio.AudioSink;
27 import org.openhab.core.audio.AudioSinkAsync;
28 import org.openhab.core.audio.AudioStream;
29 import org.openhab.core.audio.FileAudioStream;
30 import org.openhab.core.audio.StreamServed;
31 import org.openhab.core.audio.URLAudioStream;
32 import org.openhab.core.audio.UnsupportedAudioFormatException;
33 import org.openhab.core.audio.UnsupportedAudioStreamException;
34 import org.openhab.core.audio.utils.AudioStreamUtils;
35 import org.openhab.core.library.types.PercentType;
36 import org.openhab.core.thing.util.ThingHandlerHelper;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * This makes HEOS to serve as an {@link AudioSink}.
43 * @author Johannes Einig - Initial contribution
44 * @author Laurent Garnier - Extend AudioSinkAsync
47 public class HeosAudioSink extends AudioSinkAsync {
48 private final Logger logger = LoggerFactory.getLogger(HeosAudioSink.class);
50 private static final Set<AudioFormat> SUPPORTED_AUDIO_FORMATS = Set.of(AudioFormat.WAV, AudioFormat.MP3,
52 private static final Set<Class<? extends AudioStream>> SUPPORTED_AUDIO_STREAMS = Set.of(AudioStream.class);
54 private final HeosThingBaseHandler handler;
55 private final AudioHTTPServer audioHTTPServer;
56 private @Nullable final String callbackUrl;
58 public HeosAudioSink(HeosThingBaseHandler handler, AudioHTTPServer audioHTTPServer, @Nullable String callbackUrl) {
59 this.handler = handler;
60 this.audioHTTPServer = audioHTTPServer;
61 this.callbackUrl = callbackUrl;
65 public String getId() {
66 return handler.getThing().getUID().toString();
70 public @Nullable String getLabel(@Nullable Locale locale) {
71 return handler.getThing().getLabel();
75 protected void processAsynchronously(@Nullable AudioStream audioStream)
76 throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
77 if (!ThingHandlerHelper.isHandlerInitialized(handler)) {
78 logger.debug("HEOS speaker '{}' is not initialized - status is {}", handler.getThing().getUID(),
79 handler.getThing().getStatus());
80 tryClose(audioStream);
84 if (audioStream == null) {
88 AudioFormat audioFormat = audioStream.getFormat();
89 if (!AudioFormat.MP3.isCompatible(audioFormat) && !AudioFormat.WAV.isCompatible(audioFormat)
90 && !AudioFormat.AAC.isCompatible(audioFormat)) {
91 tryClose(audioStream);
92 throw new UnsupportedAudioFormatException("HEOS speaker only supports MP3, WAV and AAC formats.",
97 if (audioStream instanceof URLAudioStream urlAudioStream) {
98 // it is an external URL, the speaker can access it itself and play it.
99 url = urlAudioStream.getURL();
100 tryClose(audioStream);
101 } else if (callbackUrl != null) {
102 StreamServed streamServed;
104 streamServed = audioHTTPServer.serve(audioStream, 10, true);
105 } catch (IOException e) {
106 tryClose(audioStream);
107 throw new UnsupportedAudioStreamException(
108 "HEOS was not able to handle the audio stream (cache on disk failed).", audioStream.getClass(),
111 url = callbackUrl + streamServed.url() + AudioStreamUtils.EXTENSION_SEPARATOR;
112 if (AudioFormat.MP3.isCompatible(audioFormat)) {
113 url += FileAudioStream.MP3_EXTENSION;
114 } else if (AudioFormat.WAV.isCompatible(audioFormat)) {
115 url += FileAudioStream.WAV_EXTENSION;
116 } else if (AudioFormat.AAC.isCompatible(audioFormat)) {
117 url += FileAudioStream.AAC_EXTENSION;
119 streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream));
121 logger.warn("We do not have any callback url, so HEOS cannot play the audio stream!");
122 tryClose(audioStream);
126 handler.playURL(url);
127 } catch (IOException | ReadException e) {
128 logger.warn("Failed to play audio stream: {}", e.getMessage());
132 private void tryClose(@Nullable InputStream is) {
136 } catch (IOException ignored) {
142 public Set<AudioFormat> getSupportedFormats() {
143 return SUPPORTED_AUDIO_FORMATS;
147 public Set<Class<? extends AudioStream>> getSupportedStreams() {
148 return SUPPORTED_AUDIO_STREAMS;
152 public PercentType getVolume() {
153 return handler.getNotificationSoundVolume();
157 public void setVolume(PercentType volume) {
158 handler.setNotificationSoundVolume(volume);