Skip to content

Commit 553fa31

Browse files
committed
Make sure to update pcm_offset when seeking + fixed issue with time bar
1 parent 9c7d5d7 commit 553fa31

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,25 @@ public final synchronized void reset() {
6565
pos = mark;
6666
}
6767

68+
public final synchronized int pos() {
69+
return pos;
70+
}
71+
72+
public final synchronized void seek(int where) throws IOException {
73+
if (where < 0) throw new IllegalArgumentException();
74+
if (closed) throw new IOException("Stream is closed!");
75+
pos = where;
76+
77+
checkAvailability(pos / CHUNK_SIZE, false, false);
78+
}
79+
6880
@Override
6981
public final synchronized long skip(long n) throws IOException {
82+
if (n < 0) throw new IllegalArgumentException();
7083
if (closed) throw new IOException("Stream is closed!");
7184

7285
long k = size() - pos;
73-
if (n < k) k = n < 0 ? 0 : n;
86+
if (n < k) k = n;
7487
pos += k;
7588

7689
int chunk = pos / CHUNK_SIZE;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import org.jetbrains.annotations.NotNull;
44
import xyz.gianlu.librespot.player.codecs.SuperAudioFormat;
55

6-
import java.io.InputStream;
7-
86
/**
97
* @author Gianlu
108
*/
119
public interface GeneralAudioStream {
1210
@NotNull
13-
InputStream stream();
11+
AbsChunkedInputStream stream();
1412

1513
@NotNull
1614
SuperAudioFormat codec();

core/src/main/java/xyz/gianlu/librespot/player/codecs/Codec.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import org.apache.log4j.Logger;
44
import org.jetbrains.annotations.NotNull;
55
import org.jetbrains.annotations.Nullable;
6+
import xyz.gianlu.librespot.player.AbsChunkedInputStream;
67
import xyz.gianlu.librespot.player.GeneralAudioStream;
78
import xyz.gianlu.librespot.player.NormalizationData;
89
import xyz.gianlu.librespot.player.Player;
910

1011
import javax.sound.sampled.AudioFormat;
1112
import java.io.Closeable;
1213
import java.io.IOException;
13-
import java.io.InputStream;
1414
import java.io.OutputStream;
1515

1616
/**
@@ -19,13 +19,14 @@
1919
public abstract class Codec implements Closeable {
2020
public static final int BUFFER_SIZE = 2048;
2121
private static final Logger LOGGER = Logger.getLogger(Codec.class);
22-
protected final InputStream audioIn;
22+
protected final AbsChunkedInputStream audioIn;
2323
protected final float normalizationFactor;
2424
protected final int duration;
2525
private final AudioFormat dstFormat;
2626
protected volatile boolean closed = false;
2727
private AudioFormat format;
2828
private StreamConverter converter = null;
29+
protected int seekZero = 0;
2930

3031
Codec(@NotNull AudioFormat dstFormat, @NotNull GeneralAudioStream audioFile, @Nullable NormalizationData normalizationData, @NotNull Player.Configuration conf, int duration) {
3132
this.dstFormat = dstFormat;
@@ -55,10 +56,6 @@ public final int readSome(@NotNull OutputStream out) throws IOException, CodecEx
5556
*/
5657
public abstract int time() throws CannotGetTimeException;
5758

58-
public final int remaining() throws CannotGetTimeException {
59-
return duration - time();
60-
}
61-
6259
@Override
6360
public void close() throws IOException {
6461
closed = true;
@@ -69,7 +66,7 @@ public void seek(int positionMs) {
6966
if (positionMs < 0) positionMs = 0;
7067

7168
try {
72-
audioIn.reset();
69+
audioIn.seek(seekZero);
7370
if (positionMs > 0) {
7471
int skip = Math.round(audioIn.available() / (float) duration * positionMs);
7572
if (skip > audioIn.available()) skip = audioIn.available();

core/src/main/java/xyz/gianlu/librespot/player/codecs/VorbisCodec.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public VorbisCodec(@NotNull AudioFormat dstFormat, @NotNull GeneralAudioStream a
4949
this.buffer = joggSyncState.data;
5050

5151
readHeader();
52-
53-
audioIn.mark(-1);
52+
seekZero = audioIn.pos();
5453

5554
convertedBuffer = new byte[CONVERTED_BUFFER_SIZE];
5655

@@ -63,6 +62,28 @@ public VorbisCodec(@NotNull AudioFormat dstFormat, @NotNull GeneralAudioStream a
6362
setAudioFormat(new AudioFormat(jorbisInfo.rate, 16, jorbisInfo.channels, true, false));
6463
}
6564

65+
@Override
66+
public void seek(int positionMs) {
67+
super.seek(positionMs);
68+
if (positionMs == 0) return;
69+
70+
audioIn.mark(-1);
71+
long oldOffset = pcm_offset;
72+
while (oldOffset == pcm_offset || pcm_offset <= 0) {
73+
try {
74+
if (readInternal(new OutputStream() {
75+
@Override
76+
public void write(int b) {
77+
// Noop
78+
}
79+
}) == -1) break;
80+
} catch (IOException | CodecException ex) {
81+
break;
82+
}
83+
}
84+
audioIn.reset();
85+
}
86+
6687
@Override
6788
public int time() {
6889
return (int) (((float) pcm_offset / (float) jorbisInfo.rate) * 1000f);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public void writeChunk(@NotNull byte[] chunk, int chunkIndex, boolean cached) th
264264
}
265265

266266
@Override
267-
public @NotNull InputStream stream() {
267+
public @NotNull AbsChunkedInputStream stream() {
268268
return internalStream;
269269
}
270270

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.Closeable;
2121
import java.io.IOException;
22-
import java.io.InputStream;
2322
import java.sql.SQLException;
2423
import java.util.List;
2524
import java.util.concurrent.ExecutorService;
@@ -60,7 +59,7 @@ public class AudioFileStreaming implements AudioFile, GeneralAudioStream {
6059
}
6160

6261
@NotNull
63-
public InputStream stream() {
62+
public AbsChunkedInputStream stream() {
6463
if (chunksBuffer == null) throw new IllegalStateException("Stream not open!");
6564
return chunksBuffer.stream();
6665
}
@@ -191,7 +190,7 @@ void writeChunk(@NotNull byte[] chunk, int chunkIndex) throws IOException {
191190
}
192191

193192
@NotNull
194-
InputStream stream() {
193+
AbsChunkedInputStream stream() {
195194
return internalStream;
196195
}
197196

0 commit comments

Comments
 (0)