]> git.basschouten.com Git - openhab-addons.git/blob
fe1d17a1988271e5a93f0ffa9c5b4d928363e243
[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.heos.internal.api;
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.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;
39
40 /**
41  * This makes HEOS to serve as an {@link AudioSink}.
42  *
43  * @author Johannes Einig - Initial contribution
44  * @author Laurent Garnier - Extend AudioSinkAsync
45  */
46 @NonNullByDefault
47 public class HeosAudioSink extends AudioSinkAsync {
48     private final Logger logger = LoggerFactory.getLogger(HeosAudioSink.class);
49
50     private static final Set<AudioFormat> SUPPORTED_AUDIO_FORMATS = Set.of(AudioFormat.WAV, AudioFormat.MP3,
51             AudioFormat.AAC);
52     private static final Set<Class<? extends AudioStream>> SUPPORTED_AUDIO_STREAMS = Set.of(AudioStream.class);
53
54     private final HeosThingBaseHandler handler;
55     private final AudioHTTPServer audioHTTPServer;
56     private @Nullable final String callbackUrl;
57
58     public HeosAudioSink(HeosThingBaseHandler handler, AudioHTTPServer audioHTTPServer, @Nullable String callbackUrl) {
59         this.handler = handler;
60         this.audioHTTPServer = audioHTTPServer;
61         this.callbackUrl = callbackUrl;
62     }
63
64     @Override
65     public String getId() {
66         return handler.getThing().getUID().toString();
67     }
68
69     @Override
70     public @Nullable String getLabel(@Nullable Locale locale) {
71         return handler.getThing().getLabel();
72     }
73
74     @Override
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);
81             return;
82         }
83
84         if (audioStream == null) {
85             return;
86         }
87
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.",
93                     audioFormat);
94         }
95
96         String url;
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;
103             try {
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(),
109                         e);
110             }
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;
118             }
119             streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream));
120         } else {
121             logger.warn("We do not have any callback url, so HEOS cannot play the audio stream!");
122             tryClose(audioStream);
123             return;
124         }
125         try {
126             handler.playURL(url);
127         } catch (IOException | ReadException e) {
128             logger.warn("Failed to play audio stream: {}", e.getMessage());
129         }
130     }
131
132     private void tryClose(@Nullable InputStream is) {
133         if (is != null) {
134             try {
135                 is.close();
136             } catch (IOException ignored) {
137             }
138         }
139     }
140
141     @Override
142     public Set<AudioFormat> getSupportedFormats() {
143         return SUPPORTED_AUDIO_FORMATS;
144     }
145
146     @Override
147     public Set<Class<? extends AudioStream>> getSupportedStreams() {
148         return SUPPORTED_AUDIO_STREAMS;
149     }
150
151     @Override
152     public PercentType getVolume() {
153         return handler.getNotificationSoundVolume();
154     }
155
156     @Override
157     public void setVolume(PercentType volume) {
158         handler.setNotificationSoundVolume(volume);
159     }
160 }