Skip to content

Commit da8c938

Browse files
committed
Fixed issue with image not being sent via metadata pipe (#182)
1 parent 53f8c17 commit da8c938

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/mercury/model/ImageId.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.spotify.connectstate.Player;
44
import com.spotify.metadata.Metadata;
55
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
67
import xyz.gianlu.librespot.common.Utils;
78

89
import java.util.regex.Matcher;
@@ -32,6 +33,17 @@ public static ImageId fromHex(@NotNull String hex) {
3233
return new ImageId(hex);
3334
}
3435

36+
@Nullable
37+
public static ImageId biggestImage(@NotNull Metadata.ImageGroup group) {
38+
Metadata.Image biggest = null;
39+
for (Metadata.Image image : group.getImageList()) {
40+
if (biggest == null || biggest.getSize().getNumber() < image.getSize().getNumber())
41+
biggest = image;
42+
}
43+
44+
return biggest == null ? null : fromHex(Utils.bytesToHex(biggest.getFileId()));
45+
}
46+
3547
public static void putAsMetadata(@NotNull Player.ProvidedTrack.Builder builder, @NotNull Metadata.ImageGroup group) {
3648
for (Metadata.Image image : group.getImageList()) {
3749
String key;
@@ -52,7 +64,7 @@ public static void putAsMetadata(@NotNull Player.ProvidedTrack.Builder builder,
5264
continue;
5365
}
5466

55-
builder.putMetadata(key, ImageId.fromHex(Utils.bytesToHex(image.getFileId())).toSpotifyUri());
67+
builder.putMetadata(key, fromHex(Utils.bytesToHex(image.getFileId())).toSpotifyUri());
5668
}
5769
}
5870

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import okhttp3.Response;
1010
import okhttp3.ResponseBody;
1111
import org.apache.log4j.Logger;
12+
import org.jetbrains.annotations.Contract;
1213
import org.jetbrains.annotations.NotNull;
1314
import org.jetbrains.annotations.Nullable;
1415
import org.jetbrains.annotations.Range;
@@ -789,21 +790,37 @@ private class EventsDispatcher {
789790
}
790791
}
791792

792-
private void sendImage() {
793-
PlayableId id = state.getCurrentPlayable();
794-
if (id == null) return;
793+
@Contract("null, null -> fail")
794+
private void sendImage(@Nullable Metadata.Track track, @Nullable Metadata.Episode episode) {
795+
Metadata.ImageGroup group = null;
796+
if (track != null) {
797+
if (track.hasAlbum() && track.getAlbum().hasCoverGroup())
798+
group = track.getAlbum().getCoverGroup();
799+
} else if (episode != null) {
800+
if (episode.hasCoverImage())
801+
group = episode.getCoverImage();
802+
} else {
803+
throw new IllegalStateException();
804+
}
795805

796-
Map<String, String> metadata = state.metadataFor(id);
797806
ImageId image = null;
798-
for (String key : ImageId.IMAGE_SIZES_URLS) {
799-
if (metadata.containsKey(key)) {
800-
image = ImageId.fromUri(metadata.get(key));
801-
break;
807+
if (group == null) {
808+
PlayableId id = state.getCurrentPlayable();
809+
if (id == null) return;
810+
811+
Map<String, String> metadata = state.metadataFor(id);
812+
for (String key : ImageId.IMAGE_SIZES_URLS) {
813+
if (metadata.containsKey(key)) {
814+
image = ImageId.fromUri(metadata.get(key));
815+
break;
816+
}
802817
}
818+
} else {
819+
image = ImageId.biggestImage(group);
803820
}
804821

805822
if (image == null) {
806-
LOGGER.warn("No image found in metadata: " + id);
823+
LOGGER.warn("No image found in metadata.");
807824
return;
808825
}
809826

@@ -814,9 +831,9 @@ private void sendImage() {
814831
if (resp.code() == 200 && (body = resp.body()) != null)
815832
metadataPipe.safeSend(MetadataPipe.TYPE_SSNC, MetadataPipe.CODE_PICT, body.bytes());
816833
else
817-
LOGGER.warn(String.format("Failed download image. {id: %s, code: %d}", image.hexId(), resp.code()));
834+
LOGGER.warn(String.format("Failed downloading image. {id: %s, code: %d}", image.hexId(), resp.code()));
818835
} catch (IOException ex) {
819-
LOGGER.warn("Failed download image.", ex);
836+
LOGGER.warn("Failed downloading image.", ex);
820837
}
821838
}
822839

@@ -922,7 +939,7 @@ void metadataAvailable() {
922939
metadataPipe.safeSend(MetadataPipe.TYPE_CORE, MetadataPipe.CODE_ASAR, artist);
923940

924941
sendProgress();
925-
sendImage();
942+
sendImage(track, episode);
926943
}
927944
}
928945

0 commit comments

Comments
 (0)