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.onkyo.internal.handler;
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.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;
35 * * The {@link OnkyoAudioSink} implements the AudioSink interface.
37 * @author Paul Frank - Initial contribution
38 * @author Laurent Garnier - Extracted from UpnpAudioSinkHandler to extend AudioSinkAsync
41 public class OnkyoAudioSink extends AudioSinkAsync {
43 private final Logger logger = LoggerFactory.getLogger(OnkyoAudioSink.class);
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);
48 private OnkyoHandler handler;
49 private AudioHTTPServer audioHTTPServer;
50 private @Nullable String callbackUrl;
52 public OnkyoAudioSink(OnkyoHandler handler, AudioHTTPServer audioHTTPServer, @Nullable String callbackUrl) {
53 this.handler = handler;
54 this.audioHTTPServer = audioHTTPServer;
55 this.callbackUrl = callbackUrl;
59 public Set<AudioFormat> getSupportedFormats() {
60 return SUPPORTED_FORMATS;
64 public Set<Class<? extends AudioStream>> getSupportedStreams() {
65 return SUPPORTED_STREAMS;
69 public String getId() {
70 return handler.getThing().getUID().toString();
74 public @Nullable String getLabel(@Nullable Locale locale) {
75 return handler.getThing().getLabel();
79 protected void processAsynchronously(@Nullable AudioStream audioStream)
80 throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
81 if (audioStream == null) {
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;
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(),
102 url = callbackUrl + streamServed.url();
103 streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream));
105 logger.warn("We do not have any callback url, so Onkyo cannot play the audio stream!");
106 tryClose(audioStream);
109 handler.playMedia(url);
112 private void tryClose(@Nullable InputStream is) {
116 } catch (IOException ignored) {
122 public PercentType getVolume() throws IOException {
123 return handler.getVolume();
127 public void setVolume(PercentType volume) throws IOException {
128 handler.setVolume(volume);