[pulseaudio] fix null pointer exception and ensure source bg task stops (#12414)

* [pulseaudio] fix null pointer exception and ensure source bg task stops

Signed-off-by: Miguel Álvarez Díez <miguelwork92@gmail.com>
This commit is contained in:
GiviMAD 2022-03-04 22:40:04 +01:00 committed by GitHub
parent cf0597aea2
commit 3ed28ae745
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 6 deletions

View File

@ -138,7 +138,7 @@ public class PulseAudioAudioSource extends PulseaudioSimpleProtocolStream implem
this.pipeWriteTask = executor.submit(() -> {
int lengthRead;
byte[] buffer = new byte[1024];
while (true) {
while (!pipeOutputs.isEmpty()) {
var stream = getSourceInputStream();
if (stream != null) {
try {
@ -156,6 +156,7 @@ public class PulseAudioAudioSource extends PulseaudioSimpleProtocolStream implem
logger.warn("Unable to get source input stream");
}
}
this.pipeWriteTask = null;
});
}
}

View File

@ -24,6 +24,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.pulseaudio.internal.PulseAudioBindingConfiguration;
import org.openhab.binding.pulseaudio.internal.PulseAudioBindingConfigurationListener;
import org.openhab.binding.pulseaudio.internal.PulseaudioBindingConstants;
@ -115,7 +116,7 @@ public class PulseaudioBridgeHandler extends BaseBridgeHandler implements PulseA
}
}
public AbstractAudioDeviceConfig getDevice(String name) {
public @Nullable AbstractAudioDeviceConfig getDevice(String name) {
return client.getGenericAudioItem(name);
}

View File

@ -281,6 +281,10 @@ public class PulseaudioHandler extends BaseThingHandler implements DeviceStatusL
// refresh to get the current volume level
bridge.getClient().update();
device = bridge.getDevice(name);
if (device == null) {
logger.warn("missing device info, aborting");
return;
}
int oldVolume = device.getVolume();
int newVolume = oldVolume;
if (command.equals(IncreaseDecreaseType.INCREASE)) {
@ -358,11 +362,12 @@ public class PulseaudioHandler extends BaseThingHandler implements DeviceStatusL
public int getLastVolume() {
if (savedVolume == null) {
PulseaudioBridgeHandler bridge = getPulseaudioBridgeHandler();
AbstractAudioDeviceConfig device = bridge.getDevice(name);
// refresh to get the current volume level
bridge.getClient().update();
device = bridge.getDevice(name);
savedVolume = device.getVolume();
AbstractAudioDeviceConfig device = bridge.getDevice(name);
if (device != null) {
savedVolume = device.getVolume();
}
}
return savedVolume == null ? 50 : savedVolume;
}
@ -370,6 +375,10 @@ public class PulseaudioHandler extends BaseThingHandler implements DeviceStatusL
public void setVolume(int volume) {
PulseaudioBridgeHandler bridge = getPulseaudioBridgeHandler();
AbstractAudioDeviceConfig device = bridge.getDevice(name);
if (device == null) {
logger.warn("missing device info, aborting");
return;
}
bridge.getClient().setVolumePercent(device, volume);
updateState(VOLUME_CHANNEL, new PercentType(volume));
savedVolume = volume;
@ -412,11 +421,15 @@ public class PulseaudioHandler extends BaseThingHandler implements DeviceStatusL
* If no module is listening, then it will command the module to load on the pulse audio server,
*
* @return the port on which the pulseaudio server is listening for this sink
* @throws IOException when device info is not available
* @throws InterruptedException when interrupted during the loading module wait
*/
public int getSimpleTcpPort() throws InterruptedException {
public int getSimpleTcpPort() throws IOException, InterruptedException {
var bridgeHandler = getPulseaudioBridgeHandler();
AbstractAudioDeviceConfig device = bridgeHandler.getDevice(name);
if (device == null) {
throw new IOException("missing device info, device appears to be offline");
}
String simpleTcpPortPrefName = (device instanceof Source) ? DEVICE_PARAMETER_AUDIO_SOURCE_PORT
: DEVICE_PARAMETER_AUDIO_SINK_PORT;
BigDecimal simpleTcpPortPref = ((BigDecimal) getThing().getConfiguration().get(simpleTcpPortPrefName));