Skip to content

Commit 2ca4226

Browse files
committed
Moved classes + retrying chunk request
1 parent 802c1e4 commit 2ca4226

10 files changed

Lines changed: 44 additions & 26 deletions

File tree

core/src/main/java/xyz/gianlu/librespot/cache/CacheManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.concurrent.ConcurrentHashMap;
2020
import java.util.concurrent.TimeUnit;
2121

22-
import static xyz.gianlu.librespot.player.ChannelManager.CHUNK_SIZE;
22+
import static xyz.gianlu.librespot.player.feeders.storage.ChannelManager.CHUNK_SIZE;
2323

2424

2525
/**

core/src/main/java/xyz/gianlu/librespot/cdn/CdnManager.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
import xyz.gianlu.librespot.common.proto.Metadata;
1111
import xyz.gianlu.librespot.core.Session;
1212
import xyz.gianlu.librespot.mercury.MercuryClient;
13-
import xyz.gianlu.librespot.player.*;
13+
import xyz.gianlu.librespot.player.AbsChunckedInputStream;
14+
import xyz.gianlu.librespot.player.GeneralAudioStream;
15+
import xyz.gianlu.librespot.player.GeneralWritableStream;
16+
import xyz.gianlu.librespot.player.StreamId;
1417
import xyz.gianlu.librespot.player.codecs.SuperAudioFormat;
1518
import xyz.gianlu.librespot.player.decrypt.AesAudioDecrypt;
1619
import xyz.gianlu.librespot.player.decrypt.AudioDecrypt;
1720
import xyz.gianlu.librespot.player.decrypt.NoopAudioDecrypt;
21+
import xyz.gianlu.librespot.player.feeders.storage.AudioFileFetch;
1822

1923
import java.io.IOException;
2024
import java.io.InputStream;
@@ -23,7 +27,7 @@
2327
import java.util.concurrent.ExecutorService;
2428
import java.util.concurrent.Executors;
2529

26-
import static xyz.gianlu.librespot.player.ChannelManager.CHUNK_SIZE;
30+
import static xyz.gianlu.librespot.player.feeders.storage.ChannelManager.CHUNK_SIZE;
2731

2832
/**
2933
* @author Gianlu
@@ -203,7 +207,7 @@ public void writeChunk(@NotNull byte[] chunk, int chunkIndex, boolean cached) th
203207
else return "{fileId: " + streamId.getFileId() + "}";
204208
}
205209

206-
private void requestChunk(int index) {
210+
private void requestChunk(int index, boolean retried) {
207211
if (cacheHandler != null) {
208212
try {
209213
if (cacheHandler.hasChunk(index)) {
@@ -219,7 +223,12 @@ private void requestChunk(int index) {
219223
InternalResponse resp = request(index);
220224
writeChunk(resp.buffer, index, false);
221225
} catch (IOException ex) {
222-
LOGGER.fatal(String.format("Failed requesting chunk from network, index: %d", index), ex);
226+
LOGGER.fatal(String.format("Failed requesting chunk from network, index: %d, retried: %b", index, retried), ex);
227+
if (retried) {
228+
// TODO: Fatal
229+
} else {
230+
requestChunk(index, true);
231+
}
223232
}
224233
}
225234

@@ -274,7 +283,7 @@ protected int chunks() {
274283

275284
@Override
276285
protected void requestChunkFromStream(int index) {
277-
executorService.execute(() -> requestChunk(index));
286+
executorService.execute(() -> requestChunk(index, false));
278287
}
279288
}
280289
}

core/src/main/java/xyz/gianlu/librespot/core/Session.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import xyz.gianlu.librespot.crypto.Packet;
1919
import xyz.gianlu.librespot.mercury.MercuryClient;
2020
import xyz.gianlu.librespot.player.AudioKeyManager;
21-
import xyz.gianlu.librespot.player.ChannelManager;
2221
import xyz.gianlu.librespot.player.Player;
22+
import xyz.gianlu.librespot.player.feeders.storage.ChannelManager;
2323
import xyz.gianlu.librespot.spirc.SpotifyIrc;
2424

2525
import javax.crypto.Cipher;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.io.InputStream;
77
import java.util.concurrent.atomic.AtomicInteger;
88

9-
import static xyz.gianlu.librespot.player.ChannelManager.CHUNK_SIZE;
9+
import static xyz.gianlu.librespot.player.feeders.storage.ChannelManager.CHUNK_SIZE;
1010

1111
/**
1212
* @author Gianlu

core/src/main/java/xyz/gianlu/librespot/player/decrypt/AesAudioDecrypt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.math.BigInteger;
1010
import java.security.GeneralSecurityException;
1111

12-
import static xyz.gianlu.librespot.player.ChannelManager.CHUNK_SIZE;
12+
import static xyz.gianlu.librespot.player.feeders.storage.ChannelManager.CHUNK_SIZE;
1313

1414
/**
1515
* @author Gianlu

core/src/main/java/xyz/gianlu/librespot/player/feeders/StorageFeeder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import xyz.gianlu.librespot.core.Session;
77
import xyz.gianlu.librespot.crypto.Packet;
88
import xyz.gianlu.librespot.mercury.model.PlayableId;
9-
import xyz.gianlu.librespot.player.AudioFileStreaming;
109
import xyz.gianlu.librespot.player.NormalizationData;
10+
import xyz.gianlu.librespot.player.feeders.storage.AudioFileStreaming;
1111

1212
import java.io.IOException;
1313
import java.io.InputStream;

core/src/main/java/xyz/gianlu/librespot/player/AudioFile.java renamed to core/src/main/java/xyz/gianlu/librespot/player/feeders/storage/AudioFile.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package xyz.gianlu.librespot.player;
1+
package xyz.gianlu.librespot.player.feeders.storage;
2+
3+
import xyz.gianlu.librespot.player.GeneralWritableStream;
24

35
import java.io.Closeable;
46
import java.io.IOException;
@@ -11,5 +13,5 @@ public interface AudioFile extends Closeable, GeneralWritableStream {
1113

1214
void writeHeader(byte id, byte[] bytes, boolean cached) throws IOException;
1315

14-
void streamError(short code);
16+
void streamError(int chunkIndex, short code);
1517
}

core/src/main/java/xyz/gianlu/librespot/player/AudioFileFetch.java renamed to core/src/main/java/xyz/gianlu/librespot/player/feeders/storage/AudioFileFetch.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package xyz.gianlu.librespot.player;
1+
package xyz.gianlu.librespot.player.feeders.storage;
22

33
import org.apache.log4j.Logger;
44
import org.jetbrains.annotations.Nullable;
@@ -9,7 +9,7 @@
99
import java.nio.ByteBuffer;
1010
import java.sql.SQLException;
1111

12-
import static xyz.gianlu.librespot.player.ChannelManager.CHUNK_SIZE;
12+
import static xyz.gianlu.librespot.player.feeders.storage.ChannelManager.CHUNK_SIZE;
1313

1414
/**
1515
* @author Gianlu
@@ -54,8 +54,8 @@ public synchronized void writeHeader(byte id, byte[] bytes, boolean cached) thro
5454
}
5555

5656
@Override
57-
public void streamError(short code) {
58-
LOGGER.fatal(String.format("Stream error, code: %d", code));
57+
public void streamError(int chunkIndex, short code) {
58+
LOGGER.fatal(String.format("Stream error, index: %d, code: %d", chunkIndex, code)); // TODO: Fatal
5959
}
6060

6161
synchronized void waitChunk() {

core/src/main/java/xyz/gianlu/librespot/player/AudioFileStreaming.java renamed to core/src/main/java/xyz/gianlu/librespot/player/feeders/storage/AudioFileStreaming.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package xyz.gianlu.librespot.player;
1+
package xyz.gianlu.librespot.player.feeders.storage;
22

33
import com.google.protobuf.ByteString;
44
import org.apache.log4j.Logger;
@@ -8,6 +8,8 @@
88
import xyz.gianlu.librespot.common.Utils;
99
import xyz.gianlu.librespot.common.proto.Metadata;
1010
import xyz.gianlu.librespot.core.Session;
11+
import xyz.gianlu.librespot.player.AbsChunckedInputStream;
12+
import xyz.gianlu.librespot.player.GeneralAudioStream;
1113
import xyz.gianlu.librespot.player.codecs.SuperAudioFormat;
1214
import xyz.gianlu.librespot.player.decrypt.AesAudioDecrypt;
1315
import xyz.gianlu.librespot.player.decrypt.AudioDecrypt;
@@ -20,7 +22,7 @@
2022
import java.util.concurrent.ExecutorService;
2123
import java.util.concurrent.Executors;
2224

23-
import static xyz.gianlu.librespot.player.ChannelManager.CHUNK_SIZE;
25+
import static xyz.gianlu.librespot.player.feeders.storage.ChannelManager.CHUNK_SIZE;
2426

2527
/**
2628
* @author Gianlu
@@ -58,12 +60,17 @@ public InputStream stream() {
5860
return chunksBuffer.stream();
5961
}
6062

61-
private void requestChunk(@NotNull ByteString fileId, int index, @NotNull AudioFile file) {
63+
private void requestChunk(@NotNull ByteString fileId, int index, @NotNull AudioFile file, boolean retried) {
6264
if (cacheHandler == null || !tryCacheChunk(index)) {
6365
try {
6466
session.channel().requestChunk(fileId, index, file);
6567
} catch (IOException ex) {
66-
LOGGER.fatal(String.format("Failed requesting chunk from network, index: %d", index), ex);
68+
LOGGER.fatal(String.format("Failed requesting chunk from network, index: %d, retried: %b", index, retried), ex);
69+
if (retried) {
70+
// TODO: Fatal
71+
} else {
72+
requestChunk(fileId, index, file, true);
73+
}
6774
}
6875
}
6976
}
@@ -98,7 +105,7 @@ private boolean tryCacheHeaders(@NotNull AudioFileFetch fetch) throws IOExceptio
98105
private AudioFileFetch requestHeaders() throws IOException {
99106
AudioFileFetch fetch = new AudioFileFetch(cacheHandler);
100107
if (cacheHandler == null || !tryCacheHeaders(fetch))
101-
requestChunk(file.getFileId(), 0, fetch);
108+
requestChunk(file.getFileId(), 0, fetch, false);
102109

103110
fetch.waitChunk();
104111
return fetch;
@@ -119,7 +126,7 @@ public void open() throws IOException {
119126
}
120127

121128
private void requestChunk(int index) {
122-
requestChunk(file.getFileId(), index, this);
129+
requestChunk(file.getFileId(), index, this, false);
123130
chunksBuffer.requested[index] = true; // Just to be sure
124131
}
125132

@@ -142,8 +149,8 @@ public void writeHeader(byte id, byte[] bytes, boolean cached) {
142149
}
143150

144151
@Override
145-
public void streamError(short code) {
146-
LOGGER.fatal(String.format("Stream error, code: %d", code));
152+
public void streamError(int chunkIndex, short code) {
153+
LOGGER.fatal(String.format("Stream error, index: %d, code: %d", chunkIndex, code)); // TODO: Fatal
147154
}
148155

149156
@Override

core/src/main/java/xyz/gianlu/librespot/player/ChannelManager.java renamed to core/src/main/java/xyz/gianlu/librespot/player/feeders/storage/ChannelManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package xyz.gianlu.librespot.player;
1+
package xyz.gianlu.librespot.player.feeders.storage;
22

33
import com.google.protobuf.ByteString;
44
import org.apache.log4j.Logger;
@@ -153,7 +153,7 @@ private void addToQueue(@NotNull ByteBuffer payload) {
153153
}
154154

155155
void streamError(short code) {
156-
file.streamError(code);
156+
file.streamError(chunkIndex, code);
157157
}
158158

159159
private class Handler implements Runnable {

0 commit comments

Comments
 (0)