forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[onkyo] Support for more audio streams through the HTTP audio servlet
Related to openhab#15113 Signed-off-by: Laurent Garnier <lg.hc@free.fr>
- Loading branch information
Showing
4 changed files
with
135 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...inding.onkyo/src/main/java/org/openhab/binding/onkyo/internal/handler/OnkyoAudioSink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/** | ||
* Copyright (c) 2010-2023 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.onkyo.internal.handler; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.audio.AudioFormat; | ||
import org.openhab.core.audio.AudioHTTPServer; | ||
import org.openhab.core.audio.AudioSinkAsync; | ||
import org.openhab.core.audio.AudioStream; | ||
import org.openhab.core.audio.StreamServed; | ||
import org.openhab.core.audio.URLAudioStream; | ||
import org.openhab.core.audio.UnsupportedAudioFormatException; | ||
import org.openhab.core.audio.UnsupportedAudioStreamException; | ||
import org.openhab.core.library.types.PercentType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* * The {@link OnkyoAudioSink} implements the AudioSink interface. | ||
* | ||
* @author Paul Frank - Initial contribution | ||
* @author Laurent Garnier - Extracted from UpnpAudioSinkHandler to extend AudioSinkAsync | ||
*/ | ||
@NonNullByDefault | ||
public class OnkyoAudioSink extends AudioSinkAsync { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(OnkyoAudioSink.class); | ||
|
||
private static final Set<AudioFormat> SUPPORTED_FORMATS = Set.of(AudioFormat.WAV, AudioFormat.MP3); | ||
private static final Set<Class<? extends AudioStream>> SUPPORTED_STREAMS = Set.of(AudioStream.class); | ||
|
||
private OnkyoHandler handler; | ||
private AudioHTTPServer audioHTTPServer; | ||
private @Nullable String callbackUrl; | ||
|
||
public OnkyoAudioSink(OnkyoHandler handler, AudioHTTPServer audioHTTPServer, @Nullable String callbackUrl) { | ||
this.handler = handler; | ||
this.audioHTTPServer = audioHTTPServer; | ||
this.callbackUrl = callbackUrl; | ||
} | ||
|
||
@Override | ||
public Set<AudioFormat> getSupportedFormats() { | ||
return SUPPORTED_FORMATS; | ||
} | ||
|
||
@Override | ||
public Set<Class<? extends AudioStream>> getSupportedStreams() { | ||
return SUPPORTED_STREAMS; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return handler.getThing().getUID().toString(); | ||
} | ||
|
||
@Override | ||
public @Nullable String getLabel(@Nullable Locale locale) { | ||
return handler.getThing().getLabel(); | ||
} | ||
|
||
@Override | ||
protected void processAsynchronously(@Nullable AudioStream audioStream) | ||
throws UnsupportedAudioFormatException, UnsupportedAudioStreamException { | ||
if (audioStream == null) { | ||
handler.stop(); | ||
return; | ||
} | ||
|
||
if (audioStream instanceof URLAudioStream urlAudioStream) { | ||
// it is an external URL, the speaker can access it itself and play it. | ||
handler.playMedia(urlAudioStream.getURL()); | ||
} else if (callbackUrl != null) { | ||
StreamServed streamServed; | ||
try { | ||
// we serve it on our own HTTP server | ||
streamServed = audioHTTPServer.serve(audioStream, 20, true); | ||
} catch (IOException e) { | ||
throw new UnsupportedAudioStreamException("Onkyo can only handle clonable audio streams.", | ||
audioStream.getClass(), e); | ||
} | ||
streamServed.playEnd().thenRun(() -> this.playbackFinished(audioStream)); | ||
handler.playMedia(callbackUrl + streamServed.url()); | ||
} else { | ||
logger.warn("We do not have any callback url, so Onkyo cannot play the audio stream!"); | ||
} | ||
try { | ||
audioStream.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
|
||
@Override | ||
public PercentType getVolume() throws IOException { | ||
return handler.getVolume(); | ||
} | ||
|
||
@Override | ||
public void setVolume(PercentType volume) throws IOException { | ||
handler.setVolume(volume); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters