forked from openhab/openhab-addons
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)
Signed-off-by: Gwendal Roulleau <gwendal.roulleau@gmail.com> This add to the pulseaudio binding the capability to use "pulseaudio sink" as an "openhab sink" to output sound from openhab. 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
- Loading branch information
1 parent
506ef2e
commit 217b962
Showing
6 changed files
with
265 additions
and
7 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
152 changes: 152 additions & 0 deletions
152
...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,152 @@ | ||
package org.openhab.binding.pulseaudio.internal; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.Socket; | ||
import java.net.UnknownHostException; | ||
import java.util.HashSet; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
|
||
import javax.sound.sampled.AudioInputStream; | ||
import javax.sound.sampled.UnsupportedAudioFileException; | ||
|
||
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; | ||
|
||
import javazoom.spi.mpeg.sampled.convert.MpegFormatConversionProvider; | ||
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader; | ||
|
||
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 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(); | ||
} | ||
|
||
private 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.error("Cannot convert this mp3 stream to pcm stream", e); | ||
} | ||
return null; | ||
} | ||
|
||
private void connect() throws UnknownHostException, IOException { | ||
if (clientSocket == null || !clientSocket.isConnected() || clientSocket.isClosed()) { | ||
String host = pulseaudioHandler.getHost(); | ||
int port = pulseaudioHandler.getSimpleTcpPort(); | ||
clientSocket = new Socket(host, port); | ||
} | ||
} | ||
|
||
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; | ||
} | ||
|
||
try { | ||
InputStream audioInputStream = null; | ||
if (AudioFormat.MP3.isCompatible(audioStream.getFormat())) { | ||
audioInputStream = getPCMStreamFromMp3Stream(audioStream); | ||
} else if (AudioFormat.WAV.isCompatible(audioStream.getFormat())) { | ||
audioInputStream = audioStream; | ||
} | ||
|
||
if (audioInputStream == null) { | ||
throw new UnsupportedAudioFormatException("pulseaudio audio sink can only play pcm or mp3 stream", | ||
audioStream.getFormat()); | ||
} | ||
|
||
try { | ||
connect(); | ||
audioInputStream.transferTo(clientSocket.getOutputStream()); | ||
} catch (IOException e) { | ||
logger.error("Error while trying to send audio to pulseaudio audio sink. Cannot connect to " | ||
+ pulseaudioHandler.getHost() + ":" + pulseaudioHandler.getSimpleTcpPort()); | ||
} | ||
} finally { | ||
try { | ||
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
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