Skip to content

Commit 7c7a34f

Browse files
committed
Empty buffers when seeking
1 parent 3fc08ff commit 7c7a34f

4 files changed

Lines changed: 41 additions & 14 deletions

File tree

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,11 @@ private void loadTrack(boolean play, @NotNull PushToMixerReason reason) {
423423
}
424424

425425
state.setState(true, !play, false);
426-
state.updated();
427426
} else {
428427
state.setState(true, !play, true);
429-
state.updated();
430428
}
431429

430+
state.updated();
432431
events.trackChanged();
433432

434433
if (!play) {
@@ -447,17 +446,16 @@ private void loadTrack(boolean play, @NotNull PushToMixerReason reason) {
447446

448447
trackHandler.seek(state.getPosition());
449448
state.setState(true, !play, false);
450-
state.updated();
451449
} else {
452450
state.setState(true, !play, true);
453-
state.updated();
454451
}
452+
455453
} else {
456454
trackHandler = runner.load(id, state.getPosition());
457455
state.setState(true, !play, true);
458-
state.updated();
459456
}
460457

458+
state.updated();
461459
events.trackChanged();
462460

463461
if (play) {

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void run() {
167167

168168
try {
169169
int count = mixing.read(buffer);
170-
output.write(buffer, 0, count);
170+
output.write(buffer, count);
171171
} catch (IOException | LineUnavailableException ex) {
172172
if (closed) break;
173173

@@ -305,6 +305,10 @@ private void acquireLine() throws LineUnavailableException {
305305
if (lastVolume != -1) setVolume(lastVolume);
306306
}
307307

308+
void flush() {
309+
if (line != null) line.flush();
310+
}
311+
308312
void stop() {
309313
if (line != null) line.stop();
310314
}
@@ -318,10 +322,10 @@ boolean start() {
318322
return false;
319323
}
320324

321-
void write(byte[] buffer, int off, int len) throws IOException, LineUnavailableException {
325+
void write(byte[] buffer, int len) throws IOException, LineUnavailableException {
322326
if (type == Type.MIXER) {
323327
acquireLine();
324-
line.write(buffer, off, len);
328+
line.write(buffer, 0, len);
325329
} else if (type == Type.PIPE) {
326330
if (out == null) {
327331
if (!pipe.exists()) {
@@ -341,9 +345,9 @@ void write(byte[] buffer, int off, int len) throws IOException, LineUnavailableE
341345
out = new FileOutputStream(pipe, true);
342346
}
343347

344-
out.write(buffer, off, len);
348+
out.write(buffer, 0, len);
345349
} else if (type == Type.STREAM) {
346-
out.write(buffer, off, len);
350+
out.write(buffer, 0, len);
347351
} else {
348352
throw new IllegalStateException();
349353
}
@@ -471,6 +475,9 @@ public void run() {
471475

472476
if (handler.codec != null) {
473477
if (shouldAbortCrossfade) handler.abortCrossfade();
478+
479+
output.flush();
480+
if (handler.out != null) handler.out.stream().emptyBuffer();
474481
handler.codec.seek((Integer) cmd.args[0]);
475482
}
476483

core/src/main/java/xyz/gianlu/librespot/player/mixing/CircularBuffer.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ protected int readInternal() {
121121
*
122122
* @return a byte from the buffer.
123123
*/
124+
@TestOnly
124125
public int read() throws IOException {
125126
if (closed) return -1;
126127

@@ -173,6 +174,23 @@ public boolean full() {
173174
return tail + 1 == head || (head == 0 && tail == data.length - 1);
174175
}
175176

177+
/**
178+
* Empties the buffer from its data, the data is not erased (will be overridden anyway).
179+
*/
180+
public void empty() {
181+
lock.lock();
182+
183+
try {
184+
head = 0;
185+
tail = 0;
186+
187+
awaitData.signalAll();
188+
awaitSpace.signalAll();
189+
} finally {
190+
lock.unlock();
191+
}
192+
}
193+
176194
@Override
177195
public void close() {
178196
closed = true;

core/src/main/java/xyz/gianlu/librespot/player/mixing/MixingLine.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ public interface MixingOutput {
9292
void clear();
9393

9494
@NotNull
95-
OutputStream stream();
95+
LowLevelStream stream();
9696
}
9797

98-
private static abstract class LowLevelStream extends OutputStream {
98+
public static abstract class LowLevelStream extends OutputStream {
9999
private final CircularBuffer buffer;
100100

101101
LowLevelStream(@NotNull CircularBuffer buffer) {
@@ -111,6 +111,10 @@ public final void write(int b) {
111111
public final void write(@NotNull byte[] b, int off, int len) throws IOException {
112112
buffer.write(b, off, len);
113113
}
114+
115+
public final void emptyBuffer() {
116+
buffer.empty();
117+
}
114118
}
115119

116120
public class FirstOutputStream extends LowLevelStream implements MixingOutput {
@@ -149,7 +153,7 @@ public void clear() {
149153
}
150154

151155
@Override
152-
public @NotNull OutputStream stream() {
156+
public @NotNull LowLevelStream stream() {
153157
if (fout != this) throw new IllegalArgumentException();
154158
return this;
155159
}
@@ -192,7 +196,7 @@ public void clear() {
192196
}
193197

194198
@Override
195-
public @NotNull OutputStream stream() {
199+
public @NotNull LowLevelStream stream() {
196200
if (sout != this) throw new IllegalArgumentException();
197201
return this;
198202
}

0 commit comments

Comments
 (0)