]> git.basschouten.com Git - openhab-addons.git/blob
fcb39dba272ad3c145227648666b11581ba6da9f
[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.upnpcontrol.internal.audiosink;
14
15 import java.io.IOException;
16 import java.util.Locale;
17 import java.util.Set;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.upnpcontrol.internal.handler.UpnpRendererHandler;
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.AudioStream;
28 import org.openhab.core.audio.FixedLengthAudioStream;
29 import org.openhab.core.audio.URLAudioStream;
30 import org.openhab.core.audio.UnsupportedAudioFormatException;
31 import org.openhab.core.audio.UnsupportedAudioStreamException;
32 import org.openhab.core.library.types.PercentType;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  *
38  * @author Mark Herwege - Initial contribution
39  */
40 @NonNullByDefault
41 public class UpnpAudioSink implements AudioSink {
42
43     private final Logger logger = LoggerFactory.getLogger(UpnpAudioSink.class);
44
45     private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Stream
46             .of(AudioStream.class, FixedLengthAudioStream.class).collect(Collectors.toSet());
47     protected UpnpRendererHandler handler;
48     protected AudioHTTPServer audioHTTPServer;
49     protected String callbackUrl;
50
51     public UpnpAudioSink(UpnpRendererHandler handler, AudioHTTPServer audioHTTPServer, String callbackUrl) {
52         this.handler = handler;
53         this.audioHTTPServer = audioHTTPServer;
54         this.callbackUrl = callbackUrl;
55     }
56
57     @Override
58     public String getId() {
59         return handler.getThing().getUID().toString();
60     }
61
62     @Override
63     public @Nullable String getLabel(@Nullable Locale locale) {
64         return handler.getThing().getLabel();
65     }
66
67     @Override
68     public void process(@Nullable AudioStream audioStream)
69             throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
70         if (audioStream == null) {
71             stopMedia();
72             return;
73         }
74
75         String url = null;
76         if (audioStream instanceof URLAudioStream) {
77             URLAudioStream urlAudioStream = (URLAudioStream) audioStream;
78             url = urlAudioStream.getURL();
79         } else if (!callbackUrl.isEmpty()) {
80             String relativeUrl = audioStream instanceof FixedLengthAudioStream
81                     ? audioHTTPServer.serve((FixedLengthAudioStream) audioStream, 20)
82                     : audioHTTPServer.serve(audioStream);
83             url = String.valueOf(this.callbackUrl) + relativeUrl;
84         } else {
85             logger.warn("We do not have any callback url, so {} cannot play the audio stream!", handler.getUDN());
86             return;
87         }
88         playMedia(url);
89     }
90
91     @Override
92     public Set<AudioFormat> getSupportedFormats() {
93         return handler.getSupportedAudioFormats();
94     }
95
96     @Override
97     public Set<Class<? extends AudioStream>> getSupportedStreams() {
98         return SUPPORTED_STREAMS;
99     }
100
101     @Override
102     public PercentType getVolume() throws IOException {
103         return handler.getCurrentVolume();
104     }
105
106     @Override
107     public void setVolume(@Nullable PercentType volume) throws IOException {
108         if (volume != null) {
109             handler.setVolume(volume);
110         }
111     }
112
113     protected void stopMedia() {
114         handler.stop();
115     }
116
117     protected void playMedia(String url) {
118         String newUrl = url;
119         if (!url.startsWith("x-") && !url.startsWith("http")) {
120             newUrl = "x-file-cifs:" + url;
121         }
122         handler.setCurrentURI(newUrl, "");
123         handler.play();
124     }
125 }