Skip to content

Commit

Permalink
Small corrections after review
Browse files Browse the repository at this point in the history
  • Loading branch information
dalgwen committed Apr 2, 2021
1 parent f7396ac commit d96fd59
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.pulseaudio/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>mp3spi</artifactId>
<version> 1.9.5.4</version>
<version>1.9.5.4</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
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;
Expand All @@ -37,12 +36,16 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javazoom.spi.mpeg.sampled.convert.MpegFormatConversionProvider;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;

/**
* 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);
Expand All @@ -52,7 +55,7 @@ public class PulseAudioAudioSink implements AudioSink {

private PulseaudioHandler pulseaudioHandler;

private Socket clientSocket;
private @Nullable Socket clientSocket;

static {
SUPPORTED_FORMATS.add(AudioFormat.WAV);
Expand Down Expand Up @@ -80,7 +83,7 @@ public String getId() {
* @param input
* @return
*/
private InputStream getPCMStreamFromMp3Stream(InputStream input) {
private @Nullable InputStream getPCMStreamFromMp3Stream(InputStream input) {
try {
MpegAudioFileReader mpegAudioFileReader = new MpegAudioFileReader();
AudioInputStream sourceAIS = mpegAudioFileReader.getAudioInputStream(input);
Expand All @@ -94,7 +97,7 @@ private InputStream getPCMStreamFromMp3Stream(InputStream input) {
return mpegconverter.getAudioInputStream(convertFormat, sourceAIS);

} catch (IOException | UnsupportedAudioFileException e) {
logger.error("Cannot convert this mp3 stream to pcm stream", e);
logger.warn("Cannot convert this mp3 stream to pcm stream: {}", e.getMessage());
}
return null;
}
Expand All @@ -105,6 +108,7 @@ private InputStream getPCMStreamFromMp3Stream(InputStream input) {
* @throws UnknownHostException
* @throws IOException
*/
@SuppressWarnings("null")
private void connectIfNeeded() throws IOException {
if (clientSocket == null || !clientSocket.isConnected() || clientSocket.isClosed()) {
String host = pulseaudioHandler.getHost();
Expand All @@ -118,7 +122,7 @@ private void connectIfNeeded() throws IOException {
* Disconnect the socket to pulseaudio simple protocol
*/
public void disconnect() {
if (clientSocket == null) {
if (clientSocket != null) {
try {
clientSocket.close();
} catch (IOException e) {
Expand Down Expand Up @@ -147,19 +151,20 @@ public void process(@Nullable AudioStream audioStream)
}

try {
connectIfNeeded();
// send raw audio to the socket and to pulse audio
audioInputStream.transferTo(clientSocket.getOutputStream());
if (audioInputStream != null && clientSocket != null) {
connectIfNeeded();
// send raw audio to the socket and to pulse audio
audioInputStream.transferTo(clientSocket.getOutputStream());
}
} catch (IOException e) {
logger.error("Error while trying to send audio to pulseaudio audio sink. Cannot connect to {} : {}",
logger.warn("Error while trying to send audio to pulseaudio audio sink. Cannot connect to {} : {}",
pulseaudioHandler.getHost(), pulseaudioHandler.getSimpleTcpPort());
}
} finally {
try {
if (audioInputStream != null) {
audioInputStream.close();
}
;
audioStream.close();
} catch (IOException e) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ protected ThingHandler createHandler(Thing thing) {
registerDeviceDiscoveryService(handler);
return handler;
} else if (PulseaudioHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
PulseaudioHandler pulseaudioHandler = new PulseaudioHandler(thing, bundleContext);
return pulseaudioHandler;
return new PulseaudioHandler(thing, bundleContext);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,12 @@ public void onDeviceStateChanged(ThingUID bridge, AbstractAudioDeviceConfig devi

@SuppressWarnings("null")
public String getHost() {
return (String) getBridge().getConfiguration().get(PulseaudioBindingConstants.BRIDGE_PARAMETER_HOST);
if (getBridge() != null) {
return (String) getBridge().getConfiguration().get(PulseaudioBindingConstants.BRIDGE_PARAMETER_HOST);
} else {
logger.error("A bridge must be configured for this pulseaudio thing");
return "null";
}
}

public int getSimpleTcpPort() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@
<label>Name</label>
<description>The name of one specific device.</description>
</parameter>
<parameter name="activateSimpleProtocolSink" type="boolean">
<parameter name="activateSimpleProtocolSink" type="boolean" required="false">
<label>Sink by simple-protocol-tcp</label>
<description>Activation of a corresponding sink in openHAB (needs module-simple-protocol-tcp loaded on the server)</description>
<default>false</default>
<required>false</required>
</parameter>
<parameter name="simpleProtocolSinkPort" type="integer">
<label>Simple protocol port</label>
<parameter name="simpleProtocolSinkPort" type="integer" required="false">
<label>Simple Protocol Port</label>
<description>Port of the module-simple-protocol-tcp listening for this sink</description>
<default>4711</default>
<required>false</required>
</parameter>
</config-description>
</thing-type>
Expand Down

0 comments on commit d96fd59

Please sign in to comment.