Skip to content

Commit 6fcd57f

Browse files
committed
put seek() calls in invokeLater
1 parent 465e523 commit 6fcd57f

File tree

1 file changed

+37
-51
lines changed

1 file changed

+37
-51
lines changed

src/processing/video/Movie.java

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class Movie extends PImage implements PConstants {
8686
protected int[] copyPixels = null;
8787

8888
protected boolean firstFrame = true;
89-
protected boolean seeking = false;
89+
// protected boolean seeking = false;
9090

9191
protected boolean useBufferSink = false;
9292
protected boolean outdatedPixels = true;
@@ -172,13 +172,36 @@ protected void finalize() throws Throwable {
172172
* @brief Sets the target frame rate
173173
*/
174174
public void frameRate(float ifps) {
175-
if (seeking) return;
175+
// if (seeking) return;
176176

177177
// We calculate the target ratio in the case both the
178178
// current and target framerates are valid (greater than
179179
// zero), otherwise we leave it as 1.
180180
float f = (0 < ifps && 0 < frameRate) ? ifps / frameRate : 1;
181-
181+
182+
long t = playbin.queryPosition(TimeUnit.NANOSECONDS);
183+
long start, stop;
184+
if (rate > 0) {
185+
start = t;
186+
stop = -1;
187+
} else {
188+
start = 0;
189+
stop = t;
190+
}
191+
192+
Gst.invokeLater(new Runnable() {
193+
public void run() {
194+
boolean res = playbin.seek(rate * f, Format.TIME, EnumSet.of(SeekFlags.FLUSH, SeekFlags.ACCURATE), SeekType.SET, start, SeekType.SET, stop);
195+
if (!res) {
196+
PGraphics.showWarning("Seek operation failed.");
197+
}
198+
}
199+
});
200+
201+
frameRate = ifps;
202+
203+
204+
/*
182205
if (playing) {
183206
playbin.pause();
184207
playbin.getState();
@@ -211,6 +234,7 @@ public void frameRate(float ifps) {
211234
}
212235
213236
frameRate = ifps;
237+
*/
214238
}
215239

216240

@@ -291,7 +315,7 @@ public float time() {
291315
* @brief Jumps to a specific location
292316
*/
293317
public void jump(float where) {
294-
if (seeking) return;
318+
// if (seeking) return;
295319

296320
if (!sinkReady) {
297321
initSink();
@@ -304,54 +328,16 @@ public void jump(float where) {
304328
where = frame / nativeFrameRate;
305329
}
306330

307-
boolean res;
308331
long pos = Video.secToNanoLong(where);
309332

310-
res = playbin.seek(rate, Format.TIME, EnumSet.of(SeekFlags.FLUSH, SeekFlags.ACCURATE), SeekType.SET, pos, SeekType.NONE, -1);
311-
312-
if (!res) {
313-
PGraphics.showWarning("Seek operation failed.");
314-
}
315-
316-
// getState() will wait until any async state change
317-
// (like seek in this case) has completed
318-
seeking = true;
319-
playbin.getState();
320-
seeking = false;
321-
/*
322-
if (seeking) return; // don't seek again until the current seek operation is done.
323-
324-
if (!sinkReady) {
325-
initSink();
326-
}
327-
328-
// Round the time to a multiple of the source framerate, in
329-
// order to eliminate stutter. Suggested by Daniel Shiffman
330-
float fps = getSourceFrameRate();
331-
int frame = (int)(where * fps);
332-
final float seconds = frame / fps;
333-
334-
// Put the seek operation inside a thread to avoid blocking the main
335-
// animation thread
336-
Thread seeker = new Thread() {
337-
@Override
333+
Gst.invokeLater(new Runnable() {
338334
public void run() {
339-
long pos = Video.secToNanoLong(seconds);
340-
boolean res = playbin.seek(rate, Format.TIME, SeekFlags.FLUSH,
341-
SeekType.SET, pos, SeekType.NONE, -1);
335+
boolean res = playbin.seek(rate, Format.TIME, EnumSet.of(SeekFlags.FLUSH, SeekFlags.ACCURATE), SeekType.SET, pos, SeekType.NONE, -1);
342336
if (!res) {
343337
PGraphics.showWarning("Seek operation failed.");
344-
}
345-
346-
// getState() will wait until any async state change
347-
// (like seek in this case) has completed
348-
seeking = true;
349-
playbin.getState();
350-
seeking = false;
338+
}
351339
}
352-
};
353-
seeker.start();
354-
*/
340+
});
355341
}
356342

357343

@@ -383,7 +369,7 @@ public boolean available() {
383369
* @brief Plays movie one time and stops at the last frame
384370
*/
385371
public void play() {
386-
if (seeking) return;
372+
// if (seeking) return;
387373

388374
if (!sinkReady) {
389375
initSink();
@@ -408,7 +394,7 @@ public void play() {
408394
* @brief Plays a movie continuously, restarting it when it's over.
409395
*/
410396
public void loop() {
411-
if (seeking) return;
397+
// if (seeking) return;
412398

413399
repeat = true;
414400
play();
@@ -428,7 +414,7 @@ public void loop() {
428414
* @brief Stops the movie from looping
429415
*/
430416
public void noLoop() {
431-
if (seeking) return;
417+
// if (seeking) return;
432418

433419
if (!sinkReady) {
434420
initSink();
@@ -451,7 +437,7 @@ public void noLoop() {
451437
* @brief Pauses the movie
452438
*/
453439
public void pause() {
454-
if (seeking) return;
440+
// if (seeking) return;
455441

456442
if (!sinkReady) {
457443
initSink();
@@ -477,7 +463,7 @@ public void pause() {
477463
* @brief Stops the movie
478464
*/
479465
public void stop() {
480-
if (seeking) return;
466+
// if (seeking) return;
481467

482468
if (!sinkReady) {
483469
initSink();

0 commit comments

Comments
 (0)