From 4b1c6cc8b7e193a1ec357ee21b69b1d76397c661 Mon Sep 17 00:00:00 2001 From: Gwendal Roulleau Date: Fri, 28 Jul 2023 00:03:36 +0200 Subject: [PATCH 1/2] [pulseaudio] For source, default timeout should be disconnection ASAP To avoid weird behaviour, such as pulseaudio server storing audio data between inputstream get/read and then sending the backlog all at once when finally reading, we must disconnect immediately thereafter. Signed-off-by: Gwendal Roulleau --- .../pulseaudio/internal/PulseAudioAudioSource.java | 6 +++--- .../binding/pulseaudio/internal/PulseaudioClient.java | 1 - .../internal/PulseaudioSimpleProtocolStream.java | 9 +++++++-- .../src/main/resources/OH-INF/i18n/pulseaudio.properties | 4 ++-- .../src/main/resources/OH-INF/thing/sink.xml | 3 ++- .../src/main/resources/OH-INF/thing/source.xml | 6 ++++-- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseAudioAudioSource.java b/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseAudioAudioSource.java index bb2731e482aad..887e06c814166 100644 --- a/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseAudioAudioSource.java +++ b/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseAudioAudioSource.java @@ -162,13 +162,13 @@ private synchronized void startPipeWriteSynchronized() { // task has been ended while writing return; } - logger.warn("InterruptedIOException while writing to from pulse source pipe: {}", + logger.warn("InterruptedIOException while writing from pulse source to pipe: {}", getExceptionMessage(e)); } catch (IOException e) { - logger.warn("IOException while writing to from pulse source pipe: {}", + logger.warn("IOException while writing from pulse source to pipe: {}", getExceptionMessage(e)); } catch (RuntimeException e) { - logger.warn("RuntimeException while writing to pulse source pipe: {}", + logger.warn("RuntimeException while writing from pulse source to pipe: {}", getExceptionMessage(e)); } } diff --git a/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseaudioClient.java b/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseaudioClient.java index a30491f9d5d6d..eeab4962b6077 100644 --- a/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseaudioClient.java +++ b/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseaudioClient.java @@ -637,7 +637,6 @@ private String sendRawRequest(String command) { lc++; if (retRead > 0) { String line = new String(buff, 0, retRead); - // System.out.println("'"+line+"'"); if (line.endsWith(">>> ") && lc > 1) { result += line.substring(0, line.length() - 4); break; diff --git a/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseaudioSimpleProtocolStream.java b/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseaudioSimpleProtocolStream.java index 897517cc008dc..b5b09c94924f3 100644 --- a/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseaudioSimpleProtocolStream.java +++ b/bundles/org.openhab.binding.pulseaudio/src/main/java/org/openhab/binding/pulseaudio/internal/PulseaudioSimpleProtocolStream.java @@ -101,8 +101,13 @@ private void scheduleDisconnectIfNoClient() { } int idleTimeout = pulseaudioHandler.getIdleTimeout(); if (idleTimeout > -1) { - logger.debug("Scheduling next disconnect"); - scheduledDisconnection = scheduler.schedule(this::disconnect, idleTimeout, TimeUnit.MILLISECONDS); + if (idleTimeout == 0) { + this.disconnect(); + } else { + logger.debug("Scheduling next disconnect"); + scheduledDisconnection = scheduler.schedule(this::disconnect, idleTimeout, + TimeUnit.MILLISECONDS); + } } } } finally { diff --git a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/i18n/pulseaudio.properties b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/i18n/pulseaudio.properties index 16281ba959f3e..425cb5ac694d6 100644 --- a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/i18n/pulseaudio.properties +++ b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/i18n/pulseaudio.properties @@ -48,7 +48,7 @@ thing-type.config.pulseaudio.sink.name.description = The name of one specific de thing-type.config.pulseaudio.sink.simpleProtocolSOTimeout.label = Simple Protocol SO Timeout thing-type.config.pulseaudio.sink.simpleProtocolSOTimeout.description = Socket SO timeout when connecting to pulseaudio server though module-simple-protocol-tcp. You can tune this option if the socket disconnect frequently. thing-type.config.pulseaudio.sink.simpleProtocolSinkIdleTimeout.label = Idle Timeout -thing-type.config.pulseaudio.sink.simpleProtocolSinkIdleTimeout.description = Timeout in ms after which the connection will be closed when no stream is running. This ensures that your speaker is not on all the time and the pulseaudio sink can go to idle mode. -1 for no disconnection. +thing-type.config.pulseaudio.sink.simpleProtocolSinkIdleTimeout.description = Timeout in ms after which the connection will be closed when no stream is running. This ensures that your speaker is not on all the time and the pulseaudio sink can go to idle mode. -1 for no disconnection (improve latency). 0 for immediate disconnection. thing-type.config.pulseaudio.sink.simpleProtocolSinkPort.label = Simple Protocol Port thing-type.config.pulseaudio.sink.simpleProtocolSinkPort.description = Default Port to allocate for use by module-simple-protocol-tcp on the pulseaudio server thing-type.config.pulseaudio.sinkInput.additionalFilters.label = Additional Filters @@ -75,7 +75,7 @@ thing-type.config.pulseaudio.source.simpleProtocolSourceFormat.option.s24be = PC thing-type.config.pulseaudio.source.simpleProtocolSourceFormat.option.s32le = PCM signed 32-bit little-endian thing-type.config.pulseaudio.source.simpleProtocolSourceFormat.option.s32be = PCM signed 32-bit big-endian thing-type.config.pulseaudio.source.simpleProtocolSourceIdleTimeout.label = Idle Timeout -thing-type.config.pulseaudio.source.simpleProtocolSourceIdleTimeout.description = Timeout in ms after which the connection will be closed when no stream is running. This ensures that your speaker is not on all the time and the pulseaudio source can go to idle mode. -1 for no disconnection. +thing-type.config.pulseaudio.source.simpleProtocolSourceIdleTimeout.description = Timeout in ms after which the connection will be closed when no stream is running. This ensures that your mic is not on all the time and the pulseaudio source can go to idle mode. -1 for no disconnection. 0 for immediate disconnection (recommended value, or expect strange behaviour !). thing-type.config.pulseaudio.source.simpleProtocolSourcePort.label = Simple Protocol Port thing-type.config.pulseaudio.source.simpleProtocolSourcePort.description = Default Port to allocate to be used by module-simple-protocol-tcp on the pulseaudio server thing-type.config.pulseaudio.source.simpleProtocolSourceRate.label = Simple Protocol Rate diff --git a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/sink.xml b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/sink.xml index 9348fa3427fa1..e81f4a353d66f 100644 --- a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/sink.xml +++ b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/sink.xml @@ -45,7 +45,8 @@ Timeout in ms after which the connection will be closed when no stream is running. This ensures that - your speaker is not on all the time and the pulseaudio sink can go to idle mode. -1 for no disconnection. + your speaker is not on all the time and the pulseaudio sink can go to idle mode. -1 for no disconnection (improve + latency). 0 for immediate disconnection. 30000 diff --git a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/source.xml b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/source.xml index 385c5d266a4a3..5309ecbd57bc1 100644 --- a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/source.xml +++ b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/source.xml @@ -42,11 +42,13 @@ 4710 + true Timeout in ms after which the connection will be closed when no stream is running. This ensures that - your speaker is not on all the time and the pulseaudio source can go to idle mode. -1 for no disconnection. + your mic is not on all the time and the pulseaudio source can go to idle mode. -1 for no disconnection. 0 for + immediate disconnection (recommended value, or expect strange behaviour !) - 30000 + 0 From b36be91a68cb76298e8f62a5ee92c9046cd98cf5 Mon Sep 17 00:00:00 2001 From: Gwendal Roulleau Date: Thu, 3 Aug 2023 10:16:43 +0200 Subject: [PATCH 2/2] [pulseaudio] For source, default timeout should be disconnection ASAP Apply revision Signed-off-by: Gwendal Roulleau --- .../src/main/resources/OH-INF/i18n/pulseaudio.properties | 2 +- .../src/main/resources/OH-INF/thing/source.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/i18n/pulseaudio.properties b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/i18n/pulseaudio.properties index 425cb5ac694d6..e92a40913b1dd 100644 --- a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/i18n/pulseaudio.properties +++ b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/i18n/pulseaudio.properties @@ -75,7 +75,7 @@ thing-type.config.pulseaudio.source.simpleProtocolSourceFormat.option.s24be = PC thing-type.config.pulseaudio.source.simpleProtocolSourceFormat.option.s32le = PCM signed 32-bit little-endian thing-type.config.pulseaudio.source.simpleProtocolSourceFormat.option.s32be = PCM signed 32-bit big-endian thing-type.config.pulseaudio.source.simpleProtocolSourceIdleTimeout.label = Idle Timeout -thing-type.config.pulseaudio.source.simpleProtocolSourceIdleTimeout.description = Timeout in ms after which the connection will be closed when no stream is running. This ensures that your mic is not on all the time and the pulseaudio source can go to idle mode. -1 for no disconnection. 0 for immediate disconnection (recommended value, or expect strange behaviour !). +thing-type.config.pulseaudio.source.simpleProtocolSourceIdleTimeout.description = Timeout in ms after which the connection will be closed when no stream is running. This ensures that your mic is not on all the time and the pulseaudio source can go to idle mode. -1 for no disconnection. 0 for immediate disconnection (recommended value to avoid capturing unwanted buffered audio). thing-type.config.pulseaudio.source.simpleProtocolSourcePort.label = Simple Protocol Port thing-type.config.pulseaudio.source.simpleProtocolSourcePort.description = Default Port to allocate to be used by module-simple-protocol-tcp on the pulseaudio server thing-type.config.pulseaudio.source.simpleProtocolSourceRate.label = Simple Protocol Rate diff --git a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/source.xml b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/source.xml index 5309ecbd57bc1..cb8fceb134af1 100644 --- a/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/source.xml +++ b/bundles/org.openhab.binding.pulseaudio/src/main/resources/OH-INF/thing/source.xml @@ -46,7 +46,7 @@ Timeout in ms after which the connection will be closed when no stream is running. This ensures that your mic is not on all the time and the pulseaudio source can go to idle mode. -1 for no disconnection. 0 for - immediate disconnection (recommended value, or expect strange behaviour !) + immediate disconnection (recommended value to avoid capturing unwanted buffered audio) 0