2 * Copyright (c) 2010-2024 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.amplipi.internal.audio;
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.binding.amplipi.internal.AmpliPiHandler;
23 import org.openhab.core.audio.AudioFormat;
24 import org.openhab.core.audio.AudioSinkSync;
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.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * This is an audio sink that allows to do public announcements on the AmpliPi.
39 * @author Kai Kreuzer - Initial contribution
43 public class PAAudioSink extends AudioSinkSync implements ThingHandlerService {
45 private final Logger logger = LoggerFactory.getLogger(PAAudioSink.class);
47 private static final Set<AudioFormat> SUPPORTED_AUDIO_FORMATS = Set.of(AudioFormat.MP3, AudioFormat.WAV);
48 private static final Set<Class<? extends AudioStream>> SUPPORTED_AUDIO_STREAMS = Set.of(AudioStream.class);
50 private @Nullable AmpliPiHandler handler;
52 private @Nullable PercentType volume;
55 protected void processSynchronously(@Nullable AudioStream audioStream)
56 throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
57 if (audioStream == null) {
58 // in case the audioStream is null, this should be interpreted as a request to end any currently playing
60 logger.debug("AmpliPi sink does not support stopping the currently playing stream.");
63 AmpliPiHandler localHandler = this.handler;
64 if (localHandler == null) {
65 tryClose(audioStream);
68 logger.debug("Received audio stream of format {}", audioStream.getFormat());
69 String callbackUrl = localHandler.getCallbackUrl();
71 if (audioStream instanceof URLAudioStream urlAudioStream) {
72 // it is an external URL, so we can directly pass this on.
73 audioUrl = urlAudioStream.getURL();
74 tryClose(audioStream);
75 } else if (callbackUrl != null) {
76 // we need to serve it for a while
77 StreamServed streamServed;
79 streamServed = localHandler.getAudioHTTPServer().serve(audioStream, 10, true);
80 } catch (IOException e) {
81 tryClose(audioStream);
82 throw new UnsupportedAudioStreamException(
83 "AmpliPi was not able to handle the audio stream (cache on disk failed).",
84 audioStream.getClass(), e);
86 audioUrl = callbackUrl + streamServed.url();
88 logger.warn("We do not have any callback url, so AmpliPi cannot play the audio stream!");
89 tryClose(audioStream);
92 localHandler.playPA(audioUrl, volume);
93 // we reset the volume value again, so that a next invocation without a volume will again use the zones
98 private void tryClose(@Nullable InputStream is) {
102 } catch (IOException ignored) {
108 public Set<AudioFormat> getSupportedFormats() {
109 return SUPPORTED_AUDIO_FORMATS;
113 public Set<Class<? extends AudioStream>> getSupportedStreams() {
114 return SUPPORTED_AUDIO_STREAMS;
118 public String getId() {
119 if (handler != null) {
120 return handler.getThing().getUID().toString();
122 throw new IllegalStateException();
127 public @Nullable String getLabel(@Nullable Locale locale) {
128 if (handler != null) {
129 return handler.getThing().getLabel();
136 public PercentType getVolume() throws IOException {
137 PercentType vol = volume;
141 throw new IOException("Audio sink does not support reporting the volume.");
146 public void setVolume(final PercentType volume) throws IOException {
147 this.volume = volume;
151 public void setThingHandler(ThingHandler handler) {
152 this.handler = (AmpliPiHandler) handler;
156 public @Nullable ThingHandler getThingHandler() {