Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>xyz.gianlu.librespot</groupId>
<artifactId>librespot-java</artifactId>
<version>1.5.3-SNAPSHOT</version>
<relativePath>../</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>librespot-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/xyz/gianlu/librespot/api/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static Map<String, Deque<String>> readParameters(@NotNull HttpServerExcha
byte[] buffer = new byte[1024];
int count;
while ((count = in.read(buffer)) > 0) out.write(buffer, 0, count);
body = new String(out.toByteArray());
body = out.toString();
}

return QueryParameterUtils.mergeQueryParametersWithNewQueryString(map, body, "UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,11 @@ private static void setVolume(HttpServerExchange exchange, @NotNull Player playe
return;
}

if (val > 0) {
player.volumeUp(val);
} else if (val < 0) {
player.volumeDown(Math.abs(val));
} else {
Utils.invalidParameter(exchange, "step", "Must be non zero");
return;
}
if (val > 0) player.volumeUp(val);
else if (val < 0) player.volumeDown(Math.abs(val));
else Utils.invalidParameter(exchange, "step", "Must be non zero");
} else {
Utils.invalidParameter(exchange, "volume");
return;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>xyz.gianlu.librespot</groupId>
<artifactId>librespot-java</artifactId>
<version>1.5.3-SNAPSHOT</version>
<relativePath>../</relativePath>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>librespot-lib</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.gianlu.librespot.common.Utils;
import xyz.gianlu.librespot.core.PacketsManager;
import xyz.gianlu.librespot.core.PacketsReceiver;
import xyz.gianlu.librespot.core.Session;
import xyz.gianlu.librespot.crypto.Packet;

Expand All @@ -22,15 +22,16 @@
/**
* @author Gianlu
*/
public final class AudioKeyManager extends PacketsManager {
public final class AudioKeyManager implements PacketsReceiver {
private static final byte[] ZERO_SHORT = new byte[]{0, 0};
private static final Logger LOGGER = LogManager.getLogger(AudioKeyManager.class);
private static final long AUDIO_KEY_REQUEST_TIMEOUT = 2000;
private final AtomicInteger seqHolder = new AtomicInteger(0);
private final Map<Integer, Callback> callbacks = Collections.synchronizedMap(new HashMap<>());
private final Session session;

public AudioKeyManager(@NotNull Session session) {
super(session, "audio-keys");
this.session = session;
}

@NotNull
Expand Down Expand Up @@ -67,7 +68,7 @@ else throw new AesKeyException(String.format("Failed fetching audio key! {gid: %
}

@Override
protected void handle(@NotNull Packet packet) {
public void dispatch(@NotNull Packet packet) {
ByteBuffer payload = ByteBuffer.wrap(packet.payload);
int seq = payload.getInt();

Expand All @@ -89,11 +90,6 @@ protected void handle(@NotNull Packet packet) {
}
}

@Override
protected void exception(@NotNull Exception ex) {
LOGGER.fatal("Failed handling packet!", ex);
}

private interface Callback {
void key(byte[] key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AesAudioDecrypt(byte[] key) {

@Override
public synchronized void decryptChunk(int chunkIndex, @NotNull byte[] buffer) throws IOException {
BigInteger iv = IV_INT.add(BigInteger.valueOf(CHUNK_SIZE * chunkIndex / 16));
BigInteger iv = IV_INT.add(BigInteger.valueOf((long) CHUNK_SIZE * chunkIndex / 16));
try {
long start = System.nanoTime();
for (int i = 0; i < buffer.length; i += 4096) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import org.jetbrains.annotations.NotNull;
import xyz.gianlu.librespot.common.NameThreadFactory;
import xyz.gianlu.librespot.common.Utils;
import xyz.gianlu.librespot.core.PacketsManager;
import xyz.gianlu.librespot.core.PacketsReceiver;
import xyz.gianlu.librespot.core.Session;
import xyz.gianlu.librespot.crypto.Packet;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
Expand All @@ -25,15 +26,16 @@
/**
* @author Gianlu
*/
public class ChannelManager extends PacketsManager {
public class ChannelManager implements Closeable, PacketsReceiver {
public static final int CHUNK_SIZE = 128 * 1024;
private static final Logger LOGGER = LogManager.getLogger(ChannelManager.class);
private final Map<Short, Channel> channels = new HashMap<>();
private final AtomicInteger seqHolder = new AtomicInteger(0);
private final ExecutorService executorService = Executors.newCachedThreadPool(new NameThreadFactory(r -> "channel-queue-" + r.hashCode()));
private final Session session;

public ChannelManager(@NotNull Session session) {
super(session, "channels");
this.session = session;
}

void requestChunk(@NotNull ByteString fileId, int index, @NotNull AudioFile file) throws IOException {
Expand All @@ -59,12 +61,7 @@ void requestChunk(@NotNull ByteString fileId, int index, @NotNull AudioFile file
}

@Override
protected void handle(@NotNull Packet packet) {
throw new UnsupportedOperationException();
}

@Override
protected void appendToQueue(@NotNull Packet packet) {
public void dispatch(@NotNull Packet packet) {
ByteBuffer payload = ByteBuffer.wrap(packet.payload);
if (packet.is(Packet.Type.StreamChunkRes)) {
short id = payload.getShort();
Expand All @@ -89,15 +86,9 @@ protected void appendToQueue(@NotNull Packet packet) {
}
}

@Override
protected void exception(@NotNull Exception ex) {
LOGGER.fatal("Failed handling packet!", ex);
}

@Override
public void close() {
executorService.shutdown();
super.close();
}

public class Channel {
Expand Down
10 changes: 5 additions & 5 deletions lib/src/main/java/xyz/gianlu/librespot/cache/CacheJournal.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ List<String> getEntries() throws IOException {

int i = 0;
while (true) {
io.seek(i * JOURNAL_ENTRY_SIZE);
io.seek((long) i * JOURNAL_ENTRY_SIZE);

int first = io.read();
if (first == -1) // EOF
Expand Down Expand Up @@ -178,7 +178,7 @@ private Entry find(@NotNull String id) throws IOException {

int i = 0;
while (true) {
io.seek(i * JOURNAL_ENTRY_SIZE);
io.seek((long) i * JOURNAL_ENTRY_SIZE);

int first = io.read();
if (first == -1) // EOF
Expand Down Expand Up @@ -208,7 +208,7 @@ void createIfNeeded(@NotNull String id) throws IOException {

int i = 0;
while (true) {
io.seek(i * JOURNAL_ENTRY_SIZE);
io.seek((long) i * JOURNAL_ENTRY_SIZE);

int first = io.read();
if (first == 0 || first == -1) { // First empty spot or EOF
Expand Down Expand Up @@ -280,7 +280,7 @@ void setHeader(int id, @NotNull String value) throws IOException {
if (index == -1) throw new IllegalStateException();
}

io.seek(offset + MAX_ID_LENGTH + MAX_CHUNKS_SIZE + index * (MAX_HEADER_LENGTH + 1));
io.seek(offset + MAX_ID_LENGTH + MAX_CHUNKS_SIZE + (long) index * (MAX_HEADER_LENGTH + 1));
io.write(id);
io.write(value.getBytes(StandardCharsets.US_ASCII));
}
Expand Down Expand Up @@ -308,7 +308,7 @@ JournalHeader getHeader(int id) throws IOException {
int index = findHeader(id);
if (index == -1) return null;

io.seek(offset + MAX_ID_LENGTH + MAX_CHUNKS_SIZE + index * (MAX_HEADER_LENGTH + 1) + 1);
io.seek(offset + MAX_ID_LENGTH + MAX_CHUNKS_SIZE + (long) index * (MAX_HEADER_LENGTH + 1) + 1);
byte[] read = new byte[MAX_HEADER_LENGTH];
io.read(read);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public boolean hasChunk(int index) throws IOException {
updateTimestamp();

synchronized (io) {
if (io.length() < (index + 1) * CHUNK_SIZE)
if (io.length() < (long) (index + 1) * CHUNK_SIZE)
return false;
}

Expand All @@ -219,7 +219,7 @@ public byte[] readChunk(int index) throws IOException, BadChunkHashException {
updateTimestamp();

synchronized (io) {
io.seek(index * CHUNK_SIZE);
io.seek((long) index * CHUNK_SIZE);

byte[] buffer = new byte[CHUNK_SIZE];
int read = io.read(buffer);
Expand Down Expand Up @@ -248,7 +248,7 @@ public byte[] readChunk(int index) throws IOException, BadChunkHashException {

public void writeChunk(byte[] buffer, int index) throws IOException {
synchronized (io) {
io.seek(index * CHUNK_SIZE);
io.seek((long) index * CHUNK_SIZE);
io.write(buffer);
}

Expand Down
36 changes: 23 additions & 13 deletions lib/src/main/java/xyz/gianlu/librespot/common/ProtoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ public static Player.PlayOrigin convertPlayOrigin(@Nullable PlayOrigin po) {

Player.PlayOrigin.Builder builder = Player.PlayOrigin.newBuilder();

Optional.ofNullable(po.getFeatureIdentifier()).ifPresent(builder::setFeatureIdentifier);
Optional.ofNullable(po.getFeatureVersion()).ifPresent(builder::setFeatureVersion);
Optional.ofNullable(po.getViewUri()).ifPresent(builder::setViewUri);
Optional.ofNullable(po.getExternalReferrer()).ifPresent(builder::setExternalReferrer);
Optional.ofNullable(po.getReferrerIdentifier()).ifPresent(builder::setReferrerIdentifier);
Optional.ofNullable(po.getDeviceIdentifier()).ifPresent(builder::setDeviceIdentifier);
if (po.hasFeatureIdentifier()) builder.setFeatureIdentifier(po.getFeatureIdentifier());
if (po.hasFeatureVersion()) builder.setFeatureVersion(po.getFeatureVersion());
if (po.hasViewUri()) builder.setViewUri(po.getViewUri());
if (po.hasExternalReferrer()) builder.setExternalReferrer(po.getExternalReferrer());
if (po.hasReferrerIdentifier()) builder.setReferrerIdentifier(po.getReferrerIdentifier());
if (po.hasDeviceIdentifier()) builder.setDeviceIdentifier(po.getDeviceIdentifier());

if (po.getFeatureClassesCount() > 0)
for (String feature : po.getFeatureClassesList())
Expand Down Expand Up @@ -254,9 +254,11 @@ public static int indexOfTrackByUri(@NotNull List<ContextTrack> tracks, @NotNull
}

public static boolean isQueued(@NotNull ContextTrack track) {
String value = track.getMetadataOrDefault("is_queued", null);
if (value == null) return false;
else return Boolean.parseBoolean(value);
try {
return Boolean.parseBoolean(track.getMetadataOrThrow("is_queued"));
} catch (IllegalArgumentException ex) {
return false;
}
}

public static void enrichTrack(@NotNull ContextTrack.Builder subject, @NotNull ContextTrack track) {
Expand All @@ -283,10 +285,18 @@ public static Player.ProvidedTrack convertToProvidedTrack(@Nullable ContextTrack

Player.ProvidedTrack.Builder builder = Player.ProvidedTrack.newBuilder();
builder.setProvider("context");
Optional.ofNullable(track.getUri()).ifPresent(builder::setUri);
Optional.ofNullable(track.getUid()).ifPresent(builder::setUid);
Optional.ofNullable(track.getMetadataOrDefault("album_uri", null)).ifPresent(builder::setAlbumUri);
Optional.ofNullable(track.getMetadataOrDefault("artist_uri", null)).ifPresent(builder::setArtistUri);
if (track.hasUri()) builder.setUri(track.getUri());
if (track.hasUid()) builder.setUid(track.getUid());

try {
builder.setAlbumUri(track.getMetadataOrThrow("album_uri"));
} catch (IllegalArgumentException ignored) {
}

try {
builder.setArtistUri(track.getMetadataOrThrow("artist_uri"));
} catch (IllegalArgumentException ignored) {
}

builder.putAllMetadata(track.getMetadataMap());

Expand Down
50 changes: 0 additions & 50 deletions lib/src/main/java/xyz/gianlu/librespot/core/PacketsManager.java

This file was deleted.

11 changes: 11 additions & 0 deletions lib/src/main/java/xyz/gianlu/librespot/core/PacketsReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package xyz.gianlu.librespot.core;

import org.jetbrains.annotations.NotNull;
import xyz.gianlu.librespot.crypto.Packet;

/**
* @author Gianlu
*/
public interface PacketsReceiver {
void dispatch(@NotNull Packet packet);
}
Loading