-
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathMovie.java
More file actions
executable file
·895 lines (729 loc) · 24.1 KB
/
Movie.java
File metadata and controls
executable file
·895 lines (729 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-22 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
GStreamer implementation ported from GSVideo library by Andres Colubri
The previous version of this code was developed by Hernando Barragan
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.video;
import processing.core.*;
import java.io.*;
import java.net.URI;
import java.nio.*;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.lang.reflect.*;
import org.freedesktop.gstreamer.*;
import org.freedesktop.gstreamer.Buffer;
import org.freedesktop.gstreamer.elements.*;
import org.freedesktop.gstreamer.event.SeekFlags;
import org.freedesktop.gstreamer.event.SeekType;
/**
* Datatype for storing and playing movies. Movies must be located in the sketch's data folder
* or an accessible place on the network to load without an error.
*
* @webref movie
* @webBrief Datatype for storing and playing movies.
* @usage application
*/
public class Movie extends PImage implements PConstants {
public static String[] supportedProtocols = { "http" };
public String filename;
public PlayBin playbin;
// The source resolution and framerate of the file
public int sourceWidth;
public int sourceHeight;
public float sourceFrameRate;
public float frameRate; // the current playback fps
protected float rate; // speed multiplier (1.0: frameRate = nativeFrameRate)
protected float volume;
protected boolean playing = false;
protected boolean paused = false;
protected boolean repeat = false;
protected Method movieEventMethod;
protected Object eventHandler;
protected boolean available;
protected boolean ready;
protected boolean newFrame;
protected AppSink rgbSink = null;
protected int[] copyPixels = null;
protected boolean firstFrame = true;
protected boolean useBufferSink = false;
protected boolean outdatedPixels = true;
protected Object bufferSink;
protected Method sinkCopyMethod;
protected Method sinkSetMethod;
protected Method sinkDisposeMethod;
protected Method sinkGetMethod;
private NewSampleListener newSampleListener;
private NewPrerollListener newPrerollListener;
private final Lock bufferLock = new ReentrantLock();
/**
* Creates an instance of Movie loading the movie from filename.
*
* @param parent PApplet
* @param filename String
*/
public Movie(PApplet parent, String filename) {
super(0, 0, ARGB);
initGStreamer(parent, filename);
}
/**
* Disposes all the native resources associated to this movie.
*
* NOTE: This is not official API and may/will be removed at any time.
*/
public void dispose() {
if (playbin != null) {
try {
if (playbin.isPlaying()) {
playbin.stop();
playbin.getState();
}
} catch (Exception e) {
}
pixels = null;
rgbSink.disconnect(newSampleListener);
rgbSink.disconnect(newPrerollListener);
rgbSink.dispose();
playbin.setState(org.freedesktop.gstreamer.State.NULL);
playbin.getState();
playbin.getBus().dispose();
playbin.dispose();
parent.g.removeCache(this);
parent.unregisterMethod("dispose", this);
parent.unregisterMethod("post", this);
}
}
/**
* Finalizer of the class.
*/
protected void finalize() throws Throwable {
try {
dispose();
} finally {
// super.finalize();
}
}
/**
* Sets how often frames are read from the movie. Setting the <b>fps</b>
* parameter to 4, for example, will cause 4 frames to be read per second.
*
* @webref movie
* @webBrief Sets how often frames are read from the movie.
* @usage web_application
* @param ifps speed of the movie in frames per second
* @brief Sets the target frame rate
*/
public void frameRate(float ifps) {
// We calculate the target ratio in the case both the
// current and target framerates are valid (greater than
// zero), otherwise we leave it as 1.
float f = (0 < ifps && 0 < frameRate) ? ifps / frameRate : 1;
long t = playbin.queryPosition(TimeUnit.NANOSECONDS);
long start, stop;
if (rate > 0) {
start = t;
stop = -1;
} else {
start = 0;
stop = t;
}
seek(rate * f, start, stop);
frameRate = ifps;
}
/**
* Sets the relative playback speed of the movie. The <b>rate</b>
* parameters sets the speed where 2.0 will play the movie twice as fast,
* 0.5 will play at half the speed, and -1 will play the movie in normal
* speed in reverse.
*
* @webref movie
* @webBrief Sets the relative playback speed of the movie.
* @usage web_application
* @param irate speed multiplier for movie playback
* @brief Sets the relative playback speed
*/
public void speed(float irate) {
// If the frameRate() method is called continuously with very similar
// rate values, playback might become sluggish. This condition attempts
// to take care of that.
if (PApplet.abs(rate - irate) > 0.1) {
rate = irate;
frameRate(frameRate); // The framerate is the same, but the rate (speed) could be different.
}
}
/**
* Returns the length of the movie in seconds. If the movie is 1 minute and
* 20 seconds long the value returned will be 80.0.
*
* @webref movie
* @webBrief Returns the length of the movie in seconds.
* @usage web_application
* @brief Returns length of movie in seconds
*/
public float duration() {
long nanosec = playbin.queryDuration(TimeUnit.NANOSECONDS);
return Video.nanoSecToSecFrac(nanosec);
}
/**
* Returns the location of the playback head in seconds. For example, if
* the movie has been playing for 4 seconds, the number 4.0 will be returned.
*
* @webref movie
* @webBrief Returns the location of the playback head in seconds.
* @usage web_application
* @brief Returns location of playback head in units of seconds
*/
public float time() {
long nanosec = playbin.queryPosition(TimeUnit.NANOSECONDS);
return Video.nanoSecToSecFrac(nanosec);
}
/**
* Jumps to a specific location within a movie. The parameter <b>where</b>
* is in terms of seconds. For example, if the movie is 12.2 seconds long,
* calling <b>jump(6.1)</b> would go to the middle of the movie.
*
* @webref movie
* @webBrief Jumps to a specific location within a movie.
* @usage web_application
* @param where position to jump to specified in seconds
* @brief Jumps to a specific location
*/
public void jump(float where) {
setReady();
// Round the time to a multiple of the source framerate, in
// order to eliminate stutter. Suggested by Daniel Shiffman
if (sourceFrameRate != -1) {
int frame = (int)(where * sourceFrameRate);
where = frame / sourceFrameRate;
}
long pos = Video.secToNanoLong(where);
seek(rate, pos, -1);
}
/**
* Returns "true" when a new movie frame is available to read.
*
* @webref movie
* @webBrief Returns "true" when a new movie frame is available to read.
* @usage web_application
* @brief Returns "true" when a new movie frame is available to read.
*/
public boolean available() {
return available;
}
/**
* Plays a movie one time and stops at the last frame.
*
* @webref movie
* @webBrief Plays a movie one time and stops at the last frame.
* @usage web_application
* @brief Plays movie one time and stops at the last frame
*/
public void play() {
setReady();
playbin.play();
playbin.getState();
playing = true;
paused = false;
}
/**
* Plays a movie continuously, restarting it when it's over.
*
* @webref movie
* @webBrief Plays a movie continuously, restarting it when it's over.
* @usage web_application
* @brief Plays a movie continuously, restarting it when it's over.
*/
public void loop() {
repeat = true;
play();
}
/**
* If a movie is looping, calling noLoop() will cause it to play until the
* end and then stop on the last frame.
*
* @webref movie
* @webBrief If a movie is looping, this will cause it to play until the
* end and then stop on the last frame.
* @usage web_application
* @brief Stops the movie from looping
*/
public void noLoop() {
setReady();
repeat = false;
}
/**
* Pauses a movie during playback. If a movie is started again with play(),
* it will continue from where it was paused.
*
* @webref movie
* @webBrief Pauses a movie during playback.
* @usage web_application
* @brief Pauses the movie
*/
public void pause() {
setReady();
playbin.pause();
playbin.getState();
playing = false;
paused = true;
}
/**
* Stops a movie from continuing. The playback returns to the beginning so
* when a movie is played, it will begin from the beginning.
*
* @webref movie
* @webBrief Stops a movie from continuing.
* @usage web_application
* @brief Stops the movie
*/
public void stop() {
setReady();
playbin.stop();
playbin.getState();
playing = false;
paused = false;
}
/**
* Reads the current frame of the movie.
*
* @webref movie
* @webBrief Reads the current frame of the movie.
* @usage web_application
* @brief Reads the current frame
*/
public synchronized void read() {
if (firstFrame) {
super.init(sourceWidth, sourceHeight, ARGB, 1);
firstFrame = false;
}
if (useBufferSink) {
if (bufferSink == null) {
Object cache = parent.g.getCache(Movie.this);
if (cache != null) {
setBufferSink(cache);
getSinkMethods();
}
}
} else {
int[] temp = pixels;
pixels = copyPixels;
updatePixels();
copyPixels = temp;
}
available = false;
newFrame = true;
}
/**
* Change the volume. Values are from 0 to 1.
*
* @param float v
*/
public void volume(float v) {
if (playing && PApplet.abs(volume - v) > 0.001f) {
playbin.setVolume(v);
playbin.getState();
volume = v;
}
}
/**
* Loads the pixel data for the image into its <b>pixels[]</b> array.
*/
@Override
public synchronized void loadPixels() {
super.loadPixels();
if (useBufferSink && bufferSink != null) {
try {
// sinkGetMethod will copy the latest buffer to the pixels array,
// and the pixels will be copied to the texture when the OpenGL
// renderer needs to draw it.
sinkGetMethod.invoke(bufferSink, new Object[] { pixels });
} catch (Exception e) {
e.printStackTrace();
}
outdatedPixels = false;
}
}
/**
* Reads the color of any pixel or grabs a section of an image.
*/
@Override
public int get(int x, int y) {
if (outdatedPixels) loadPixels();
return super.get(x, y);
}
/**
* @param w width of pixel rectangle to get
* @param h height of pixel rectangle to get
*/
public PImage get(int x, int y, int w, int h) {
if (outdatedPixels) loadPixels();
return super.get(x, y, w, h);
}
@Override
public PImage copy() {
if (outdatedPixels) loadPixels();
return super.copy();
}
@Override
protected void getImpl(int sourceX, int sourceY,
int sourceWidth, int sourceHeight,
PImage target, int targetX, int targetY) {
if (outdatedPixels) loadPixels();
super.getImpl(sourceX, sourceY, sourceWidth, sourceHeight,
target, targetX, targetY);
}
/**
* Check if this movie object is currently playing.
*/
public boolean isPlaying() {
return playing;
}
/**
* Check if this movie object is currently paused.
*/
public boolean isPaused() {
return paused;
}
/**
* Check if this movie object is currently looping.
*/
public boolean isLooping() {
return repeat;
}
////////////////////////////////////////////////////////////
// Initialization methods.
protected void initGStreamer(PApplet parent, String filename) {
this.parent = parent;
Video.init();
playbin = null;
File file;
// First check to see if this can be read locally from a file.
try {
try {
// Try a local file using the dataPath. usually this will
// work ok, but sometimes the dataPath is inside a jar file,
// which is less fun, so this will crap out.
file = new File(parent.dataPath(filename));
if (file.exists()) {
playbin = new PlayBin("Movie Player");
playbin.setInputFile(file);
}
} catch (Exception e) {
e.printStackTrace();
}
// Read from a file just hanging out in the local folder.
// this might happen when the video library is used with some
// other application, or the person enters a full path name
if (playbin == null) {
try {
file = new File(filename);
if (file.exists()) {
playbin = new PlayBin("Movie Player");
playbin.setInputFile(file);
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (playbin == null) {
// Try network read...
for (int i = 0; i < supportedProtocols.length; i++) {
if (filename.startsWith(supportedProtocols[i] + "://")) {
try {
playbin = new PlayBin("Movie Player");
playbin.setURI(URI.create(filename));
break;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
} catch (SecurityException se) {
// online, whups. catch the security exception out here rather than
// doing it three times (or whatever) for each of the cases above.
}
if (playbin == null) {
parent.die("Could not load movie file " + filename, null);
}
initSink();
playbin.setVideoSink(rgbSink);
makeBusConnections(playbin.getBus());
// We've got a valid movie! let's rock.
try {
this.filename = filename; // for error messages
// register methods
parent.registerMethod("dispose", this);
parent.registerMethod("post", this);
setEventHandlerObject(parent);
sourceWidth = sourceHeight = 0;
sourceFrameRate = -1;
frameRate = -1;
rate = 1.0f;
volume = -1;
ready = false;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Uses a generic object as handler of the movie. This object should have a
* movieEvent method that receives a Movie argument. This method will
* be called upon a new frame read event.
*
*/
protected void setEventHandlerObject(Object obj) {
eventHandler = obj;
try {
movieEventMethod = eventHandler.getClass().getMethod("movieEvent", Movie.class);
return;
} catch (Exception e) {
// no such method, or an error... which is fine, just ignore
}
// movieEvent can alternatively be defined as receiving an Object, to allow
// Processing mode implementors to support the video library without linking
// to it at build-time.
try {
movieEventMethod = eventHandler.getClass().getMethod("movieEvent", Object.class);
} catch (Exception e) {
// no such method, or an error... which is fine, just ignore
}
}
protected void initSink() {
rgbSink = new AppSink("movie sink");
rgbSink.set("emit-signals", true);
newSampleListener = new NewSampleListener();
newPrerollListener = new NewPrerollListener();
rgbSink.connect(newSampleListener);
rgbSink.connect(newPrerollListener);
useBufferSink = Video.useGLBufferSink && parent.g.isGL();
if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
if (useBufferSink) {
rgbSink.setCaps(Caps.fromString("video/x-raw, format=RGBA"));
} else {
rgbSink.setCaps(Caps.fromString("video/x-raw, format=BGRA"));
}
} else {
rgbSink.setCaps(Caps.fromString("video/x-raw, format=ARGB"));
}
}
protected void setReady() {
if (!ready) {
playbin.setState(org.freedesktop.gstreamer.State.READY);
newFrame = false;
ready = true;
}
}
private void makeBusConnections(Bus bus) {
bus.connect(new Bus.ERROR() {
public void errorMessage(GstObject arg0, int arg1, String arg2) {
System.err.println(arg0 + " : " + arg2);
}
});
bus.connect(new Bus.EOS() {
public void endOfStream(GstObject arg0) {
if (repeat) {
if (0 < rate) {
// Playing forward, so we return to the beginning
jump(0);
} else {
// Playing backwards, so we go to the end.
jump(duration());
}
// The rate is set automatically to 1 when restarting the
// stream, so we need to call frameRate in order to reset
// to the latest fps rate.
frameRate(frameRate);
} else {
playing = false;
}
}
});
}
////////////////////////////////////////////////////////////
// Stream event handling.
private void seek(double rate, long start, long stop) {
Gst.invokeLater(new Runnable() {
public void run() {
boolean res;
if (stop == -1) {
res = playbin.seek(rate, Format.TIME, EnumSet.of(SeekFlags.FLUSH, SeekFlags.ACCURATE), SeekType.SET, start, SeekType.NONE, stop);
} else {
res = playbin.seek(rate, Format.TIME, EnumSet.of(SeekFlags.FLUSH, SeekFlags.ACCURATE), SeekType.SET, start, SeekType.SET, stop);
}
if (!res) {
PGraphics.showWarning("Seek operation failed.");
}
}
});
}
private void fireMovieEvent() {
if (movieEventMethod != null) {
try {
movieEventMethod.invoke(eventHandler, this);
} catch (Exception e) {
System.err.println("error, disabling movieEvent() for " + filename);
e.printStackTrace();
movieEventMethod = null;
}
}
}
////////////////////////////////////////////////////////////
// Buffer source interface.
/**
* Sets the object to use as destination for the frames read from the stream.
* The color conversion mask is automatically set to the one required to
* copy the frames to OpenGL.
*
* NOTE: This is not official API and may/will be removed at any time.
*
* @param Object dest
*/
public void setBufferSink(Object sink) {
bufferSink = sink;
}
/**
* NOTE: This is not official API and may/will be removed at any time.
*/
public boolean hasBufferSink() {
return bufferSink != null;
}
/**
* NOTE: This is not official API and may/will be removed at any time.
*/
public synchronized void disposeBuffer(Object buf) {
((Buffer)buf).dispose();
}
protected void getSinkMethods() {
try {
sinkCopyMethod = bufferSink.getClass().getMethod("copyBufferFromSource",
new Class[] { Object.class, ByteBuffer.class, int.class, int.class });
} catch (Exception e) {
throw new RuntimeException("Movie: provided sink object doesn't have a " +
"copyBufferFromSource method.");
}
try {
sinkSetMethod = bufferSink.getClass().getMethod("setBufferSource",
new Class[] { Object.class });
sinkSetMethod.invoke(bufferSink, new Object[] { this });
} catch (Exception e) {
throw new RuntimeException("Movie: provided sink object doesn't have a " +
"setBufferSource method.");
}
try {
sinkDisposeMethod = bufferSink.getClass().getMethod("disposeSourceBuffer",
new Class[] { });
} catch (Exception e) {
throw new RuntimeException("Movie: provided sink object doesn't have " +
"a disposeSourceBuffer method.");
}
try {
sinkGetMethod = bufferSink.getClass().getMethod("getBufferPixels",
new Class[] { int[].class });
} catch (Exception e) {
throw new RuntimeException("Movie: provided sink object doesn't have " +
"a getBufferPixels method.");
}
}
public synchronized void post() {
if (useBufferSink && sinkDisposeMethod != null) {
try {
sinkDisposeMethod.invoke(bufferSink, new Object[] {});
} catch (Exception e) {
e.printStackTrace();
}
}
}
////////////////////////////////////////////////////////////
// Listener of GStreamer events.
private class NewSampleListener implements AppSink.NEW_SAMPLE {
@Override
public FlowReturn newSample(AppSink sink) {
Sample sample = sink.pullSample();
// Pull out metadata from caps
Structure capsStruct = sample.getCaps().getStructure(0);
sourceWidth = capsStruct.getInteger("width");
sourceHeight = capsStruct.getInteger("height");
Fraction fps = capsStruct.getFraction("framerate");
sourceFrameRate = (float)fps.numerator / fps.denominator;
// Set the playback rate to the file's native framerate
// unless the user has already set a custom one
if (frameRate == -1.0) {
frameRate = sourceFrameRate;
}
Buffer buffer = sample.getBuffer();
ByteBuffer bb = buffer.map(false);
if (bb != null) {
// If the EDT is still copying data from the buffer, just drop this frame
if (!bufferLock.tryLock()) {
return FlowReturn.OK;
}
available = true;
if (useBufferSink && bufferSink != null) { // The native buffer from GStreamer is copied to the buffer sink.
try {
sinkCopyMethod.invoke(bufferSink, new Object[] { buffer, bb, sourceWidth, sourceHeight });
if (playing) {
fireMovieEvent();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
bufferLock.unlock();
}
} else {
IntBuffer rgb = bb.asIntBuffer();
if (copyPixels == null) {
copyPixels = new int[sourceWidth * sourceHeight];
}
try {
rgb.get(copyPixels, 0, width * height);
if (playing) {
fireMovieEvent();
}
} finally {
bufferLock.unlock();
}
}
buffer.unmap();
}
sample.dispose();
return FlowReturn.OK;
}
}
private class NewPrerollListener implements AppSink.NEW_PREROLL {
@Override
public FlowReturn newPreroll(AppSink sink) {
Sample sample = sink.pullPreroll();
// Pull out metadata from caps
Structure capsStruct = sample.getCaps().getStructure(0);
sourceWidth = capsStruct.getInteger("width");
sourceHeight = capsStruct.getInteger("height");
Fraction fps = capsStruct.getFraction("framerate");
sourceFrameRate = (float)fps.numerator / fps.denominator;
// Set the playback rate to the file's native framerate
// unless the user has already set a custom one
if (frameRate == -1.0) {
frameRate = sourceFrameRate;
}
sample.dispose();
return FlowReturn.OK;
}
}
}