]> git.basschouten.com Git - openhab-addons.git/commitdiff
[pulseaudio] fix null pointer exception and ensure source bg task stops (#12414)
authorGiviMAD <GiviMAD@users.noreply.github.com>
Fri, 4 Mar 2022 21:40:04 +0000 (22:40 +0100)
committerGitHub <noreply@github.com>
Fri, 4 Mar 2022 21:40:04 +0000 (22:40 +0100)
* [pulseaudio] fix null pointer exception and ensure source bg task stops

Signed-off-by: Miguel Álvarez Díez <miguelwork92@gmail.com>
bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseAudioAudioSource.java
bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/handler/PulseaudioBridgeHandler.java
bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/handler/PulseaudioHandler.java

index 9a1b39f1e4991fe580e1c142fe7af6a33893c59d..a44b2badcec50a398c1e9422df51075e161841d6 100644 (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;
             });
         }
     }
index 55360c17cfbf07e726b0c9b3a4d55db53c4d6a67..7a90d94a4e1a137ea7b92871c2ce9c7fd5cdced9 100644 (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);
     }
 
index df107b91b22f028c35a641d0a123a2a84a9637cc..2a25df118a0a4879837cddb451d2a3ddbecc0ae6 100644 (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));