forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pulseaudio] Add pulseaudio sink as openhab audio sink (openhab#1895) (…
…openhab#10423) * [pulseaudio] Add pulseaudio sink as openhab audio sink (openhab#1895) This add to the pulseaudio binding the capability to use "pulseaudio sink" as an "openhab sink" to output sound from openhab to a pulse audio server on the network. You need to load module-simple-protocol-tcp sink in addition to the usual module-cli-protocol-tcp, and enable the sink in the thing configuration. Closes openhab#1895 Signed-off-by: Gwendal Roulleau <[email protected]> * Small corrections after review And getting rid of some other compilation warnings Signed-off-by: Gwendal Roulleau <[email protected]> * Fix some registration errors and allow the binding to load the simple module remotely Signed-off-by: Gwendal Roulleau <[email protected]> * Small corrections after reviews initialize audiosink in a thread with scheduler.submit clear some warning related code. Signed-off-by: Gwendal Roulleau <[email protected]> Better interruptexception handling * Fix two small concurrency bugs Signed-off-by: Gwendal Roulleau <[email protected]> Co-authored-by: Gwendal Roulleau <[email protected]> Signed-off-by: John Marshall <[email protected]>
- Loading branch information
1 parent
9ee32b7
commit 25185a8
Showing
12 changed files
with
502 additions
and
40 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
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
205 changes: 205 additions & 0 deletions
205
...pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseAudioAudioSink.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,205 @@ | ||
/** | ||
* Copyright (c) 2010-2021 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.pulseaudio.internal; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.Socket; | ||
import java.util.HashSet; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
import javazoom.spi.mpeg.sampled.convert.MpegFormatConversionProvider; | ||
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader; | ||
|
||
import javax.sound.sampled.AudioInputStream; | ||
import javax.sound.sampled.UnsupportedAudioFileException; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.binding.pulseaudio.internal.handler.PulseaudioHandler; | ||
import org.openhab.core.audio.AudioFormat; | ||
import org.openhab.core.audio.AudioSink; | ||
import org.openhab.core.audio.AudioStream; | ||
import org.openhab.core.audio.FixedLengthAudioStream; | ||
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 audio sink for openhab, implemented by a connection to a pulseaudio sink | ||
* | ||
* @author Gwendal Roulleau - Initial contribution | ||
* | ||
*/ | ||
@NonNullByDefault | ||
public class PulseAudioAudioSink implements AudioSink { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(PulseAudioAudioSink.class); | ||
|
||
private static final HashSet<AudioFormat> SUPPORTED_FORMATS = new HashSet<>(); | ||
private static final HashSet<Class<? extends AudioStream>> SUPPORTED_STREAMS = new HashSet<>(); | ||
|
||
private PulseaudioHandler pulseaudioHandler; | ||
|
||
private @Nullable Socket clientSocket; | ||
|
||
static { | ||
SUPPORTED_FORMATS.add(AudioFormat.WAV); | ||
SUPPORTED_FORMATS.add(AudioFormat.MP3); | ||
SUPPORTED_STREAMS.add(FixedLengthAudioStream.class); | ||
} | ||
|
||
public PulseAudioAudioSink(PulseaudioHandler pulseaudioHandler) { | ||
this.pulseaudioHandler = pulseaudioHandler; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return pulseaudioHandler.getThing().getUID().toString(); | ||
} | ||
|
||
@Override | ||
public @Nullable String getLabel(@Nullable Locale locale) { | ||
return pulseaudioHandler.getThing().getLabel(); | ||
} | ||
|
||
/** | ||
* Convert MP3 to PCM, as this is the only possible format | ||
* | ||
* @param input | ||
* @return | ||
*/ | ||
private @Nullable InputStream getPCMStreamFromMp3Stream(InputStream input) { | ||
try { | ||
MpegAudioFileReader mpegAudioFileReader = new MpegAudioFileReader(); | ||
AudioInputStream sourceAIS = mpegAudioFileReader.getAudioInputStream(input); | ||
javax.sound.sampled.AudioFormat sourceFormat = sourceAIS.getFormat(); | ||
|
||
MpegFormatConversionProvider mpegconverter = new MpegFormatConversionProvider(); | ||
javax.sound.sampled.AudioFormat convertFormat = new javax.sound.sampled.AudioFormat( | ||
javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, | ||
sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false); | ||
|
||
return mpegconverter.getAudioInputStream(convertFormat, sourceAIS); | ||
|
||
} catch (IOException | UnsupportedAudioFileException e) { | ||
logger.warn("Cannot convert this mp3 stream to pcm stream: {}", e.getMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Connect to pulseaudio with the simple protocol | ||
* | ||
* @throws IOException | ||
* @throws InterruptedException when interrupted during the loading module wait | ||
*/ | ||
public void connectIfNeeded() throws IOException, InterruptedException { | ||
Socket clientSocketLocal = clientSocket; | ||
if (clientSocketLocal == null || !clientSocketLocal.isConnected() || clientSocketLocal.isClosed()) { | ||
String host = pulseaudioHandler.getHost(); | ||
int port = pulseaudioHandler.getSimpleTcpPort(); | ||
clientSocket = new Socket(host, port); | ||
clientSocket.setSoTimeout(500); | ||
} | ||
} | ||
|
||
/** | ||
* Disconnect the socket to pulseaudio simple protocol | ||
*/ | ||
public void disconnect() { | ||
if (clientSocket != null) { | ||
try { | ||
clientSocket.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void process(@Nullable AudioStream audioStream) | ||
throws UnsupportedAudioFormatException, UnsupportedAudioStreamException { | ||
|
||
if (audioStream == null) { | ||
return; | ||
} | ||
|
||
InputStream audioInputStream = null; | ||
try { | ||
|
||
if (AudioFormat.MP3.isCompatible(audioStream.getFormat())) { | ||
audioInputStream = getPCMStreamFromMp3Stream(audioStream); | ||
} else if (AudioFormat.WAV.isCompatible(audioStream.getFormat())) { | ||
audioInputStream = audioStream; | ||
} else { | ||
throw new UnsupportedAudioFormatException("pulseaudio audio sink can only play pcm or mp3 stream", | ||
audioStream.getFormat()); | ||
} | ||
|
||
for (int countAttempt = 1; countAttempt <= 2; countAttempt++) { // two attempts allowed | ||
try { | ||
connectIfNeeded(); | ||
if (audioInputStream != null && clientSocket != null) { | ||
// send raw audio to the socket and to pulse audio | ||
audioInputStream.transferTo(clientSocket.getOutputStream()); | ||
break; | ||
} | ||
} catch (IOException e) { | ||
disconnect(); // disconnect force to clear connection in case of socket not cleanly shutdown | ||
if (countAttempt == 2) { // we won't retry : log and quit | ||
if (logger.isWarnEnabled()) { | ||
String port = clientSocket != null ? Integer.toString(clientSocket.getPort()) : "unknown"; | ||
logger.warn( | ||
"Error while trying to send audio to pulseaudio audio sink. Cannot connect to {}:{}, error: {}", | ||
pulseaudioHandler.getHost(), port, e.getMessage()); | ||
} | ||
break; | ||
} | ||
} catch (InterruptedException ie) { | ||
logger.info("Interrupted during sink audio connection: {}", ie.getMessage()); | ||
break; | ||
} | ||
} | ||
} finally { | ||
try { | ||
if (audioInputStream != null) { | ||
audioInputStream.close(); | ||
} | ||
audioStream.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Set<AudioFormat> getSupportedFormats() { | ||
return SUPPORTED_FORMATS; | ||
} | ||
|
||
@Override | ||
public Set<Class<? extends AudioStream>> getSupportedStreams() { | ||
return SUPPORTED_STREAMS; | ||
} | ||
|
||
@Override | ||
public PercentType getVolume() { | ||
return new PercentType(pulseaudioHandler.getLastVolume()); | ||
} | ||
|
||
@Override | ||
public void setVolume(PercentType volume) { | ||
pulseaudioHandler.setVolume(volume.intValue()); | ||
} | ||
} |
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
Oops, something went wrong.