From 6a2679d8f2e26b31bbb85419fa524913d64ffe86 Mon Sep 17 00:00:00 2001 From: Gianlu Date: Thu, 11 Mar 2021 18:51:06 +0100 Subject: [PATCH] Send 'pfls' event when changing track, seeking and pausing + minor refactoring (#325) --- .../java/xyz/gianlu/librespot/player/Player.java | 16 +++++++++++++++- .../librespot/player/metadata/MetadataPipe.java | 16 +++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/player/src/main/java/xyz/gianlu/librespot/player/Player.java b/player/src/main/java/xyz/gianlu/librespot/player/Player.java index 14ce90fe..a477b83a 100644 --- a/player/src/main/java/xyz/gianlu/librespot/player/Player.java +++ b/player/src/main/java/xyz/gianlu/librespot/player/Player.java @@ -988,6 +988,10 @@ private void sendVolume(int value) { metadataPipe.safeSend(MetadataPipe.TYPE_SSNC, MetadataPipe.CODE_PVOL, volData); } + private void sendPipeFlush() { + metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_PFLS); + } + void playbackEnded() { for (EventsListener l : new ArrayList<>(listeners)) executorService.execute(l::onPlaybackEnded); @@ -997,12 +1001,15 @@ void playbackPaused() { long trackTime = state.getPosition(); for (EventsListener l : new ArrayList<>(listeners)) executorService.execute(() -> l.onPlaybackPaused(trackTime)); + + if (metadataPipe.enabled()) executorService.execute(this::sendPipeFlush); } void playbackResumed() { long trackTime = state.getPosition(); for (EventsListener l : new ArrayList<>(listeners)) executorService.execute(() -> l.onPlaybackResumed(trackTime)); + if (metadataPipe.enabled()) { executorService.execute(() -> { sendTrackInfo(); @@ -1027,13 +1034,20 @@ void trackChanged() { TrackOrEpisode metadata = currentMetadata(); for (EventsListener l : new ArrayList<>(listeners)) executorService.execute(() -> l.onTrackChanged(id, metadata)); + + if (metadataPipe.enabled()) executorService.execute(this::sendPipeFlush); } void seeked(int pos) { for (EventsListener l : new ArrayList<>(listeners)) executorService.execute(() -> l.onTrackSeeked(pos)); - if (metadataPipe.enabled()) executorService.execute(this::sendProgress); + if (metadataPipe.enabled()) { + executorService.execute(() -> { + sendPipeFlush(); + sendProgress(); + }); + } } void volumeChanged(@Range(from = 0, to = Player.VOLUME_MAX) int value) { diff --git a/player/src/main/java/xyz/gianlu/librespot/player/metadata/MetadataPipe.java b/player/src/main/java/xyz/gianlu/librespot/player/metadata/MetadataPipe.java index b43a6cb7..08548472 100644 --- a/player/src/main/java/xyz/gianlu/librespot/player/metadata/MetadataPipe.java +++ b/player/src/main/java/xyz/gianlu/librespot/player/metadata/MetadataPipe.java @@ -26,6 +26,7 @@ public final class MetadataPipe { public static final String CODE_PVOL = "70766f6c"; public static final String CODE_PRGR = "70726772"; public static final String CODE_PICT = "50494354"; + public static final String CODE_PFLS = "70666C73"; private static final Logger LOGGER = LogManager.getLogger(MetadataPipe.class); private final File file; private FileOutputStream out; @@ -34,15 +35,12 @@ public MetadataPipe(@NotNull PlayerConfiguration conf) { file = conf.metadataPipe; } - public void safeSend(@NotNull String type, @NotNull String code, @Nullable String payload) { - if (!enabled()) - return; + public void safeSend(@NotNull String type, @NotNull String code) { + safeSend(type, code, (String) null); + } - try { - send(type, code, payload == null ? null : payload.getBytes(StandardCharsets.UTF_8)); - } catch (IOException ex) { - LOGGER.error("Failed sending metadata through pipe!", ex); - } + public void safeSend(@NotNull String type, @NotNull String code, @Nullable String payload) { + safeSend(type, code, payload == null ? null : payload.getBytes(StandardCharsets.UTF_8)); } public void safeSend(@NotNull String type, @NotNull String code, @Nullable byte[] payload) { @@ -60,7 +58,7 @@ private synchronized void send(@NotNull String type, @NotNull String code, @Null if (file == null) return; if (out == null) out = new FileOutputStream(file); - if (payload != null) { + if (payload != null && payload.length > 0) { out.write(String.format("%s%s%d\n%s\n", type, code, payload.length, new String(Base64.getEncoder().encode(payload), StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8)); } else {