]> git.basschouten.com Git - openhab-addons.git/blob
cfe93f7a391c5761ba4c47d569d43e9e8c92aeca
[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.onkyo.internal.handler;
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.core.audio.AudioFormat;
23 import org.openhab.core.audio.AudioHTTPServer;
24 import org.openhab.core.audio.AudioSinkAsync;
25 import org.openhab.core.audio.AudioStream;
26 import org.openhab.core.audio.StreamServed;
27 import org.openhab.core.audio.URLAudioStream;
28 import org.openhab.core.audio.UnsupportedAudioFormatException;
29 import org.openhab.core.audio.UnsupportedAudioStreamException;
30 import org.openhab.core.library.types.PercentType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * * The {@link OnkyoAudioSink} implements the AudioSink interface.
36  *
37  * @author Paul Frank - Initial contribution
38  * @author Laurent Garnier - Extracted from UpnpAudioSinkHandler to extend AudioSinkAsync
39  */
40 @NonNullByDefault
41 public class OnkyoAudioSink extends AudioSinkAsync {
42
43     private final Logger logger = LoggerFactory.getLogger(OnkyoAudioSink.class);
44
45     private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.WAV, AudioFormat.MP3);
46     private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Set.of(AudioStream.class);
47
48     private OnkyoHandler handler;
49     private AudioHTTPServer audioHTTPServer;
50     private @Nullable String callbackUrl;
51
52     public OnkyoAudioSink(OnkyoHandler handler, AudioHTTPServer audioHTTPServer, @Nullable String callbackUrl) {
53         this.handler = handler;
54         this.audioHTTPServer = audioHTTPServer;
55         this.callbackUrl = callbackUrl;
56     }
57
58     @Override
59     public Set<AudioFormat> getSupportedFormats() {
60         return SUPPORTED_FORMATS;
61     }
62
63     @Override
64     public Set<Class<? extends AudioStream>> getSupportedStreams() {
65         return SUPPORTED_STREAMS;
66     }
67
68     @Override
69     public String getId() {
70         return handler.getThing().getUID().toString();
71     }
72
73     @Override
74     public @Nullable String getLabel(@Nullable Locale locale) {
75         return handler.getThing().getLabel();
76     }
77
78     @Override
79     protected void processAsynchronously(@Nullable AudioStream audioStream)
80             throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
81         if (audioStream == null) {
82             handler.stop();
83             return;
84         }
85
86         String url;
87         if (audioStream instanceof URLAudioStream urlAudioStream) {
88             // it is an external URL, the speaker can access it itself and play it.
89             url = urlAudioStream.getURL();
90             tryClose(audioStream);
91         } else if (callbackUrl != null) {
92             // we serve it on our own HTTP server
93             StreamServed streamServed;
94             try {
95                 streamServed = audioHTTPServer.serve(audioStream, 10, true);
96             } catch (IOException e) {
97                 tryClose(audioStream);
98                 throw new UnsupportedAudioStreamException(
99                         "Onkyo was not able to handle the audio stream (cache on disk failed).", audioStream.getClass(),
100                         e);
101             }
102             url = callbackUrl + streamServed.url();
103             streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream));
104         } else {
105             logger.warn("We do not have any callback url, so Onkyo cannot play the audio stream!");
106             tryClose(audioStream);
107             return;
108         }
109         handler.playMedia(url);
110     }
111
112     private void tryClose(@Nullable InputStream is) {
113         if (is != null) {
114             try {
115                 is.close();
116             } catch (IOException ignored) {
117             }
118         }
119     }
120
121     @Override
122     public PercentType getVolume() throws IOException {
123         return handler.getVolume();
124     }
125
126     @Override
127     public void setVolume(PercentType volume) throws IOException {
128         handler.setVolume(volume);
129     }
130 }