From f0adf767194a146b530ae51c8ca8e0c0081e3920 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Tue, 11 Jul 2023 18:46:01 +0200 Subject: [PATCH] HTTP audio servlet: check end of play every 2 seconds (#3695) Instead of 5s before. It helps triggering the end of playback earlier for audio sinks in asynchronous mode. Signed-off-by: Laurent Garnier --- .../java/org/openhab/core/audio/internal/AudioServlet.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioServlet.java b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioServlet.java index 3a5ac9377a..fe5d448a9c 100644 --- a/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioServlet.java +++ b/bundles/org.openhab.core.audio/src/main/java/org/openhab/core/audio/internal/AudioServlet.java @@ -189,6 +189,9 @@ public class AudioServlet extends HttpServlet implements AudioHTTPServer { // update timeout with the sound duration : if (endOfPlayTimestamp != null) { servedStream.timeout().set(Math.max(servedStream.timeout().get(), endOfPlayTimestamp)); + logger.debug( + "doGet endOfPlayTimestamp {} (delay from now {} nanoseconds) => new timeout timestamp {} nanoseconds", + endOfPlayTimestamp, endOfPlayTimestamp - System.nanoTime(), servedStream.timeout().get()); } resp.flushBuffer(); } catch (final AudioException ex) { @@ -234,7 +237,7 @@ public class AudioServlet extends HttpServlet implements AudioHTTPServer { if (!servedStreams.isEmpty()) { if (periodicCleanerLocal == null || periodicCleanerLocal.isDone()) { // reschedule a clean - periodicCleaner = threadPool.scheduleWithFixedDelay(this::removeTimedOutStreams, 5, 5, + periodicCleaner = threadPool.scheduleWithFixedDelay(this::removeTimedOutStreams, 2, 2, TimeUnit.SECONDS); } } else if (periodicCleanerLocal != null) { // no more stream to serve, shut the periodic cleaning thread: @@ -274,6 +277,7 @@ public class AudioServlet extends HttpServlet implements AudioHTTPServer { audioStream = createClonableInputStream(originalStream, streamId); } long timeOut = System.nanoTime() + TimeUnit.SECONDS.toNanos(seconds); + logger.debug("timeout {} seconds => timestamp {} nanoseconds", seconds, timeOut); CompletableFuture<@Nullable Void> playEnd = new CompletableFuture<@Nullable Void>(); StreamServed streamToServe = new StreamServed(getRelativeURL(streamId), audioStream, new AtomicInteger(), new AtomicLong(timeOut), multiTimeStream, playEnd);