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.upnpcontrol.internal.audiosink;
15 import java.io.IOException;
16 import java.util.Locale;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
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;
38 * @author Mark Herwege - Initial contribution
41 public class UpnpAudioSink implements AudioSink {
43 private final Logger logger = LoggerFactory.getLogger(UpnpAudioSink.class);
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;
51 public UpnpAudioSink(UpnpRendererHandler handler, AudioHTTPServer audioHTTPServer, String callbackUrl) {
52 this.handler = handler;
53 this.audioHTTPServer = audioHTTPServer;
54 this.callbackUrl = callbackUrl;
58 public String getId() {
59 return handler.getThing().getUID().toString();
63 public @Nullable String getLabel(@Nullable Locale locale) {
64 return handler.getThing().getLabel();
68 public void process(@Nullable AudioStream audioStream)
69 throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
70 if (audioStream == 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;
85 logger.warn("We do not have any callback url, so {} cannot play the audio stream!", handler.getUDN());
92 public Set<AudioFormat> getSupportedFormats() {
93 return handler.getSupportedAudioFormats();
97 public Set<Class<? extends AudioStream>> getSupportedStreams() {
98 return SUPPORTED_STREAMS;
102 public PercentType getVolume() throws IOException {
103 return handler.getCurrentVolume();
107 public void setVolume(@Nullable PercentType volume) throws IOException {
108 if (volume != null) {
109 handler.setVolume(volume);
113 protected void stopMedia() {
117 protected void playMedia(String url) {
119 if (!url.startsWith("x-") && !url.startsWith("http")) {
120 newUrl = "x-file-cifs:" + url;
122 handler.setCurrentURI(newUrl, "");