Skip to content

Commit d8aa741

Browse files
uvjustindevgianlu
andauthored
Runtime init of metadata pipe and threaded send (#186)
* Runtime init of metadata pipe and threaded send * Minor refactoring Co-authored-by: devgianlu <[email protected]>
1 parent 6ff8d35 commit d8aa741

4 files changed

Lines changed: 53 additions & 36 deletions

File tree

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ language: java
22
os: linux
33

44
before_install:
5-
- [ "${TRAVIS_PULL_REQUEST}" = "false" ] && echo $GPG_SECRET_KEYS | base64 --decode | $GPG_EXECUTABLE --import
6-
- [ "${TRAVIS_PULL_REQUEST}" = "false" ] && echo $GPG_OWNERTRUST | base64 --decode | $GPG_EXECUTABLE --import-ownertrust
5+
- chmod +x .travis/*
6+
- .travis/before_install.sh
77

88
script:
99
- mvn clean test -Pdebug -B -U -Dgpg.skip -Dmaven.javadoc.skip=true
1010

1111
after_script:
12-
- [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ "${$TRAVIS_BRANCH}" = "dev" ] && mvn deploy --settings .maven.xml -B -U -Prelease
12+
- .travis/after_script.sh
1313

1414
before_deploy:
1515
- mvn help:evaluate -N -Dexpression=project.version|grep -v '\['

.travis/after_script.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if ( [ "${TRAVIS_PULL_REQUEST}" = "false" ] && [ "${$TRAVIS_BRANCH}" = "dev" ] ) then;
2+
mvn deploy --settings .maven.xml -B -U -Prelease
3+
fi

.travis/before_install.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then
2+
echo $GPG_SECRET_KEYS | base64 --decode | $GPG_EXECUTABLE --import
3+
echo $GPG_OWNERTRUST | base64 --decode | $GPG_EXECUTABLE --import-ownertrust
4+
fi

core/src/main/java/xyz/gianlu/librespot/player/Player.java

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
import xyz.gianlu.librespot.player.codecs.Codec;
2929
import xyz.gianlu.librespot.player.contexts.AbsSpotifyContext;
3030

31-
import java.io.*;
31+
import java.io.Closeable;
32+
import java.io.File;
33+
import java.io.FileOutputStream;
34+
import java.io.IOException;
3235
import java.nio.charset.StandardCharsets;
3336
import java.util.ArrayList;
3437
import java.util.Base64;
@@ -781,12 +784,11 @@ private static class MetadataPipe {
781784
private static final String CODE_PVOL = "70766f6c";
782785
private static final String CODE_PRGR = "70726772";
783786
private static final String CODE_PICT = "50494354";
784-
private final FileOutputStream out;
787+
private final File file;
788+
private FileOutputStream out;
785789

786-
MetadataPipe(@NotNull Configuration conf) throws FileNotFoundException {
787-
File file = conf.metadataPipe();
788-
if (file != null) out = new FileOutputStream(file);
789-
else out = null;
790+
MetadataPipe(@NotNull Configuration conf) {
791+
file = conf.metadataPipe();
790792
}
791793

792794
void safeSend(@NotNull String type, @NotNull String code, @Nullable String payload) {
@@ -805,8 +807,9 @@ void safeSend(@NotNull String type, @NotNull String code, @Nullable byte[] paylo
805807
}
806808
}
807809

808-
private void send(@NotNull String type, @NotNull String code, @Nullable byte[] payload) throws IOException {
809-
if (out == null) return;
810+
private synchronized void send(@NotNull String type, @NotNull String code, @Nullable byte[] payload) throws IOException {
811+
if (file == null) return;
812+
if (out == null) out = new FileOutputStream(file);
810813

811814
if (payload != null) {
812815
out.write(String.format("<item><type>%s</type><code>%s</code><length>%d</length>\n<data encoding=\"base64\">%s</data></item>\n", type, code,
@@ -817,7 +820,7 @@ private void send(@NotNull String type, @NotNull String code, @Nullable byte[] p
817820
}
818821

819822
boolean enabled() {
820-
return out != null;
823+
return file != null;
821824
}
822825
}
823826

@@ -827,11 +830,7 @@ private class EventsDispatcher {
827830
private final List<EventsListener> listeners = new ArrayList<>();
828831

829832
EventsDispatcher(@NotNull Configuration conf) {
830-
try {
831-
metadataPipe = new MetadataPipe(conf);
832-
} catch (FileNotFoundException ex) {
833-
throw new IllegalArgumentException(ex);
834-
}
833+
metadataPipe = new MetadataPipe(conf);
835834
}
836835

837836
private void sendImage() {
@@ -872,6 +871,29 @@ private void sendProgress() {
872871
metadataPipe.safeSend(MetadataPipe.TYPE_SSNC, MetadataPipe.CODE_PRGR, data);
873872
}
874873

874+
private void sendTrackInfo() {
875+
Metadata.Track track = currentTrack();
876+
Metadata.Episode episode = currentEpisode();
877+
if (track == null && episode == null) return;
878+
879+
String title = track != null ? track.getName() : episode.getName();
880+
metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_MINM, title);
881+
882+
String album = track != null ? track.getAlbum().getName() : episode.getShow().getName();
883+
metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_ASAL, album);
884+
885+
String artist = track != null ? Utils.artistsToString(track.getArtistList()) : episode.getShow().getPublisher();
886+
metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_ASAR, artist);
887+
}
888+
889+
private void sendVolume(int value) {
890+
float xmlValue;
891+
if (value == 0) xmlValue = 144.0f;
892+
else xmlValue = (value - PlayerRunner.VOLUME_MAX) * 30.0f / (PlayerRunner.VOLUME_MAX - 1);
893+
String volData = String.format("%.2f,0.00,0.00,0.00", xmlValue);
894+
metadataPipe.safeSend(MetadataPipe.TYPE_SSNC, MetadataPipe.CODE_PVOL, volData);
895+
}
896+
875897
void playbackPaused() {
876898
long trackTime = state.getPosition();
877899
for (EventsListener l : new ArrayList<>(listeners))
@@ -914,7 +936,7 @@ void seeked(int pos) {
914936
for (EventsListener l : new ArrayList<>(listeners))
915937
executorService.execute(() -> l.onTrackSeeked(pos));
916938

917-
if (metadataPipe.enabled()) sendProgress();
939+
if (metadataPipe.enabled()) executorService.execute(this::sendProgress);
918940
}
919941

920942
void volumeChanged(@Range(from = 0, to = PlayerRunner.VOLUME_MAX) int value) {
@@ -923,13 +945,7 @@ void volumeChanged(@Range(from = 0, to = PlayerRunner.VOLUME_MAX) int value) {
923945
for (EventsListener l : new ArrayList<>(listeners))
924946
executorService.execute(() -> l.onVolumeChanged(volume));
925947

926-
if (metadataPipe.enabled()) {
927-
float xmlValue;
928-
if (value == 0) xmlValue = 144.0f;
929-
else xmlValue = (value - PlayerRunner.VOLUME_MAX) * 30.0f / (PlayerRunner.VOLUME_MAX - 1);
930-
String volData = String.format("%.2f,0.00,0.00,0.00", xmlValue);
931-
metadataPipe.safeSend(MetadataPipe.TYPE_SSNC, MetadataPipe.CODE_PVOL, volData);
932-
}
948+
if (metadataPipe.enabled()) executorService.execute(() -> sendVolume(value));
933949
}
934950

935951
void metadataAvailable() {
@@ -943,17 +959,11 @@ void metadataAvailable() {
943959
executorService.execute(() -> l.onMetadataAvailable(track, episode));
944960

945961
if (metadataPipe.enabled()) {
946-
String title = track != null ? track.getName() : episode.getName();
947-
metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_MINM, title);
948-
949-
String album = track != null ? track.getAlbum().getName() : episode.getShow().getName();
950-
metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_ASAL, album);
951-
952-
String artist = track != null ? Utils.artistsToString(track.getArtistList()) : episode.getShow().getPublisher();
953-
metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_ASAR, artist);
954-
955-
sendProgress();
956-
sendImage();
962+
executorService.execute(() -> {
963+
sendTrackInfo();
964+
sendProgress();
965+
sendImage();
966+
});
957967
}
958968
}
959969

0 commit comments

Comments
 (0)