Skip to content

Commit

Permalink
Send 'pfls' event when changing track, seeking and pausing + minor re…
Browse files Browse the repository at this point in the history
…factoring (#325)
  • Loading branch information
devgianlu committed Mar 11, 2021
1 parent c9eec6a commit 6a2679d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
16 changes: 15 additions & 1 deletion player/src/main/java/xyz/gianlu/librespot/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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("<item><type>%s</type><code>%s</code><length>%d</length>\n<data encoding=\"base64\">%s</data></item>\n", type, code,
payload.length, new String(Base64.getEncoder().encode(payload), StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8));
} else {
Expand Down

0 comments on commit 6a2679d

Please sign in to comment.