/
GstPlayer.cpp
1284 lines (1099 loc) · 34.1 KB
/
GstPlayer.cpp
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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Quell the GL macro redefined warnings.
#if defined( __CLANG__ )
#pragma diagnostic push
#pragma diagnostic ignored "-Wmacro-redefined"
#endif
#include "cinder/linux/GstPlayer.h"
#if defined( CINDER_GST_HAS_GL ) && defined( CINDER_LINUX )
#if ! defined( CINDER_LINUX_EGL_ONLY )
// These files will include a glfw_config.h that's custom to Cinder.
#include "glad/glad.h"
#include "glfw/glfw3.h"
#include "glfw/glfw3native.h"
#endif
#endif
#include "cinder/app/AppBase.h"
#include "cinder/Log.h"
#include "cinder/gl/Environment.h"
#include <chrono>
namespace gst { namespace video {
#if defined( CINDER_GST_HAS_GL )
static GstGLDisplay* sGstGLDisplay = nullptr;
static GstGLContext* sGstGLContext = nullptr;
static gulong sSharedCtxSignalHandler = 0;
static GstGLContext* createGstGLContext( GstGLDisplay* display, GstGLContext* context, gpointer userData ) {
return sGstGLContext;
}
#endif
static bool sUseGstGl = false;
static const int sEnableAsyncStateChange = false;
GstData::GstData()
: isPaused( false )
, isBuffering( false )
, isLive( false )
, position( -1 )
, width( -1 )
, height( -1 )
, videoHasChanged( false )
, videoFormat( GST_VIDEO_FORMAT_RGBA )
, currentState( GST_STATE_NULL )
, targetState( GST_STATE_NULL )
, isPrerolled( false )
, duration( -1 )
, isDone( false )
, isLoaded( false )
, isPlayable( false )
, requestedSeekTime( 0.0f )
, requestedSeek( false )
, isSeeking( false )
, loop( false )
, palindrome( false )
, rate( 1.0f )
, isStream( false )
, frameRate( -1.0f )
, numFrames( 0 )
, fpsNom( -1 )
, fpsDenom( -1 )
, pixelAspectRatio( 0.0f )
, player( nullptr )
{
}
GstData::~GstData()
{
if( player ) player = nullptr;
}
void GstData::reset()
{
player = nullptr;
}
void GstData::prepareForNewVideo()
{
videoHasChanged = true;
isPaused = false;
isBuffering = false;
isLive = false;
isStream = false;
videoFormat = GST_VIDEO_FORMAT_RGBA;
position = -1;
isPrerolled = false;
isBuffering = false;
requestedSeekTime = 0.0f;
isSeeking = false;
requestedSeek = false;
isDone = false;
position = 0;
width = -1;
height = -1;
rate = 1.0f;
frameRate = -1.0f;
numFrames = 0;
fpsNom = -1;
fpsDenom = -1;
pixelAspectRatio = 0.0f;
loop = false;
palindrome = false;
}
void GstData::updateState( const GstState& current )
{
currentState = current;
switch ( static_cast<int>( currentState ) ) {
case GST_STATE_NULL: {
reset();
break;
}
case GST_STATE_READY: {
prepareForNewVideo();
break;
}
case GST_STATE_PAUSED: {
isPrerolled = true;
isPaused = true;
isLoaded = true;
isPlayable = true;
break;
}
case GST_STATE_PLAYING: {
isDone = false;
isPaused = false;
isPlayable = true;
break;
}
default: break;
}
}
GstBusSyncReply checkBusMessagesSync( GstBus* bus, GstMessage* message, gpointer userData )
{
if( ! userData ) {
return GST_BUS_DROP;
}
GstData& data = *( static_cast<GstData*>( userData ) );
switch( GST_MESSAGE_TYPE( message ) )
{
#if defined( CINDER_GST_HAS_GL )
case GST_MESSAGE_NEED_CONTEXT: {
const gchar *context_type = nullptr;
gst_message_parse_context_type( message, &context_type );
CI_LOG_I( "Need context : " << context_type << " from element : " << GST_ELEMENT_NAME( GST_MESSAGE_SRC( message ) ) );
GstContext* context = nullptr;
if( g_strcmp0( context_type, GST_GL_DISPLAY_CONTEXT_TYPE ) == 0 ) {
context = gst_context_new( GST_GL_DISPLAY_CONTEXT_TYPE, TRUE );
gst_context_set_gl_display( context, sGstGLDisplay );
gst_element_set_context( GST_ELEMENT( message->src ), context );
}
else if( g_strcmp0( context_type, "gst.gl.app_context" ) == 0 ) {
context = gst_context_new( "gst.gl.app_context", TRUE );
GstStructure *s = gst_context_writable_structure( context );
#if defined( GST_TYPE_GL_CONTEXT )
gst_structure_set( s, "context", GST_TYPE_GL_CONTEXT, sGstGLContext, nullptr );
#else
gst_structure_set( s, "context", GST_GL_TYPE_CONTEXT, sGstGLContext, nullptr );
#endif
gst_element_set_context( GST_ELEMENT( message->src ), context );
}
if( context ) {
gst_context_unref( context );
}
break;
}
default: break;
#endif
}
return GST_BUS_PASS;
}
gboolean checkBusMessagesAsync( GstBus* bus, GstMessage* message, gpointer userData )
{
if( ! userData ){
return true;
}
GstData& data = *( static_cast<GstData*>( userData ) );
switch( GST_MESSAGE_TYPE( message ) )
{
case GST_MESSAGE_ERROR: {
GError *err = nullptr;
gchar *dbg = nullptr;
gst_message_parse_error( message, &err, &dbg );
CI_LOG_E( "ERROR from element " << GST_OBJECT_NAME( message->src ) << " : " << err->message );
if( dbg )
CI_LOG_E( "Debugging info : " << dbg );
g_error_free ( err );
g_free ( dbg );
GstStateChangeReturn state = gst_element_set_state( data.pipeline, GST_STATE_NULL );
if( state == GST_STATE_CHANGE_FAILURE ) {
CI_LOG_E( "Failed to set the pipeline to NULL state after ERROR occured." );
}
break;
}
case GST_MESSAGE_HAVE_CONTEXT: {
GstContext *context = nullptr;
const gchar *context_type = nullptr;
gchar *context_str = nullptr;
gst_message_parse_have_context( message, &context );
context_type = gst_context_get_context_type( context );
context_str = gst_structure_to_string( gst_context_get_structure( context ) );
CI_LOG_I( "Have context : " << context_type << " from element : " << GST_ELEMENT_NAME( GST_MESSAGE_SRC( message ) ) );
g_free( context_str );
if( context ) {
gst_context_unref( context );
}
break;
}
case GST_MESSAGE_BUFFERING: {
if( data.isLive ) break; // No buffering for live sources.
gint percent = 0;
gst_message_parse_buffering( message, &percent );
CI_LOG_I( "Buffering : " << percent );
if( percent == 100 ) {
data.isBuffering = false;
CI_LOG_I( "Buffering Complete !" );
if( data.targetState == GST_STATE_PLAYING ) {
gst_element_set_state( data.pipeline, GST_STATE_PLAYING );
}
}
else {
if( ! data.isBuffering && data.targetState == GST_STATE_PLAYING ) {
gst_element_set_state( data.pipeline, GST_STATE_PAUSED );
CI_LOG_I( "Buffering in process...." );
}
data.isBuffering = true;
}
break;
}
// Possible due to network connection error when streaming..
case GST_MESSAGE_CLOCK_LOST: {
// Get a new clock
gst_element_set_state( data.pipeline, GST_STATE_PAUSED );
gst_element_set_state( data.pipeline, GST_STATE_PLAYING );
break;
}
case GST_MESSAGE_DURATION_CHANGED: {
break;
}
case GST_MESSAGE_STATE_CHANGED: {
if( GST_MESSAGE_SRC( message ) == GST_OBJECT( data.pipeline ) ) {
GstState old, current, pending;
gst_message_parse_state_changed( message, &old, ¤t, &pending );
if( old != current ) {
CI_LOG_I( "Pipeline state changed from : " << gst_element_state_get_name( old ) << " to : " << gst_element_state_get_name ( current ) << " with pending : " << gst_element_state_get_name( pending) );
}
data.updateState( current );
if( data.targetState != data.currentState && pending == GST_STATE_VOID_PENDING ) {
if( data.targetState == GST_STATE_PAUSED )
data.player->stop();
else if( data.targetState == GST_STATE_PLAYING )
data.player->play();
}
}
break;
}
case GST_MESSAGE_ASYNC_DONE: {
switch( static_cast<int>( data.currentState ) ) {
case GST_STATE_PAUSED: {
if( data.isSeeking ) {
if( data.requestedSeek ) {
data.position = data.requestedSeekTime * GST_SECOND;
if( data.player ) data.player->seekToTime( data.requestedSeekTime, true );
data.requestedSeek = false;
}
else {
data.isSeeking = false;
}
}
}
default: break;
}
break;
}
case GST_MESSAGE_EOS: {
if( data.loop ) {
if( data.palindrome && ! data.isStream && ! data.isLive ) {
// Toggle the direction we are playing.
if( data.player ) data.player->setRate( -data.player->getRate() );
}
else {
// If playing back on reverse start the loop from the
// end of the file
if( data.rate < 0 ){
if( data.player ) data.player->seekToTime( data.player->getDurationSeconds() );
}
else{
// otherwise restart from beginning.
if( data.player ) data.player->seekToTime( 0 );
}
}
}
data.videoHasChanged = false;
data.isDone = true;
break;
}
default: break;
}
return true;
}
GstPlayer::GstPlayer()
: mGMainLoop( nullptr )
, mGstBus( nullptr )
, mNewFrame( false )
, mUsingCustomPipeline( false )
{
bool success = initializeGStreamer();
if( success ) {
mGstData.player = this;
startGMainLoopThread();
}
}
GstPlayer::~GstPlayer()
{
cleanup();
}
void GstPlayer::cleanup()
{
if( mGstData.pipeline != nullptr ) {
// Flush and remove the bus since we are about to destroy our pipeline.
resetBus();
resetPipeline();
}
resetVideoBuffers();
if( g_main_loop_is_running( mGMainLoop ) ) {
g_main_loop_quit( mGMainLoop );
}
g_main_loop_unref( mGMainLoop );
if( mGMainLoopThread.joinable() ){
mGMainLoopThread.join();
}
}
void GstPlayer::startGMainLoopThread()
{
mGMainLoop = g_main_loop_new( nullptr, false );
mGMainLoopThread = std::thread( &GstPlayer::startGMainLoop, this, mGMainLoop );
}
void GstPlayer::startGMainLoop( GMainLoop* loop )
{
g_main_loop_run( loop );
}
bool GstPlayer::initializeGStreamer()
{
if( ! gst_is_initialized() ) {
guint major;
guint minor;
guint micro;
guint nano;
gst_version( &major, &minor, µ, &nano );
GError* err;
/// If we havent already initialized GStreamer do this.
if( ! gst_init_check( nullptr, nullptr, &err ) ) {
if( err->message ) {
CI_LOG_E( "FAILED to initialize GStreamer : " << err->message );
}
else {
CI_LOG_E( "FAILED to initialize GStreamer due to unknown error." );
}
return false;
}
else {
if( major >= 1 && minor >= 6 ) {
sUseGstGl = true;
}
else {
sUseGstGl = false;
}
CI_LOG_I( "Initialized GStreamer version : " << major << "." << minor << "." << micro << "." << nano );
return true;
}
}
return true;
}
void GstPlayer::resetPipeline()
{
if( ! mGstData.pipeline ) {
return;
}
gst_element_set_state( mGstData.pipeline, GST_STATE_NULL );
gst_element_get_state( mGstData.pipeline, nullptr, nullptr, GST_CLOCK_TIME_NONE );
gst_object_unref( GST_OBJECT( mGstData.pipeline ) );
mGstData.pipeline = nullptr;
mGstData.videoBin = nullptr;
if( sUseGstGl ) {
#if defined( CINDER_GST_HAS_GL )
// Pipeline will unref and destroy its children..
mGstData.glupload = nullptr;
mGstData.glcolorconvert = nullptr;
#endif
}
}
void GstPlayer::resetBus()
{
gst_bus_set_flushing( mGstBus, true );
bool success = g_source_remove( mBusId );
if( success ) {
mBusId = -1;
}
mGstBus = nullptr;
}
void GstPlayer::setCustomPipeline( const GstCustomPipelineData &customPipeline )
{
// Similar to loading and maybe merged at some point.
// If we have a custom pipeline we reset before re-using.
if( mGstData.pipeline ) {
// Flush and remove the bus since we are about to destroy our pipeline.
resetBus();
resetPipeline();
}
GError *error = nullptr;
mGstData.pipeline = gst_parse_launch( customPipeline.pipeline.c_str(), &error );
if( error != nullptr ) {
CI_LOG_E( "Could not construct custom pipeline : " << error->message );
g_error_free (error);
}
// Prepare for loading custom pipeline.
// We lock until we switch to GST_STATE_READY since there is not much to do if we dont reach at least there.
gst_element_set_state( mGstData.pipeline, GST_STATE_READY );
gst_element_get_state( mGstData.pipeline, NULL, NULL, GST_CLOCK_TIME_NONE );
if( mGstData.pipeline ) {
// Currently assumes that the pipeline has an appsink named 'videosink'.
// This is just for testing and it has to be more generic.
mGstData.appSink = gst_bin_get_by_name( GST_BIN( mGstData.pipeline ), "videosink" );
}
addBusWatch( mGstData.pipeline );
if( mGstData.appSink ) {
GstAppSinkCallbacks appSinkCallbacks;
appSinkCallbacks.eos = onGstEos;
appSinkCallbacks.new_preroll = onGstPreroll;
appSinkCallbacks.new_sample = onGstSample;
gst_app_sink_set_callbacks( GST_APP_SINK( mGstData.appSink ), &appSinkCallbacks, this, 0 );
}
mUsingCustomPipeline = true;
// Reset the buffers for the next pre-roll.
resetVideoBuffers();
// async pre-roll
gst_element_set_state( mGstData.pipeline, GST_STATE_PAUSED );
}
void GstPlayer::addBusWatch( GstElement* pipeline )
{
mGstBus = gst_pipeline_get_bus( GST_PIPELINE( pipeline ) );
mBusId = gst_bus_add_watch( mGstBus, checkBusMessagesAsync, &mGstData );
gst_bus_set_sync_handler( mGstBus, checkBusMessagesSync, &mGstData, nullptr );
gst_object_unref( mGstBus );
}
bool GstPlayer::initialize()
{
#if defined( CINDER_GST_HAS_GL )
if( sUseGstGl ) {
GstGLContext* sharedCtx = nullptr;
GError* err = nullptr;
#if defined( CINDER_LINUX )
auto platformData = std::dynamic_pointer_cast<ci::gl::PlatformDataLinux>( ci::gl::context()->getPlatformData() );
#if defined( CINDER_LINUX_EGL_ONLY )
if( ! sGstGLDisplay )
sGstGLDisplay = (GstGLDisplay*) gst_gl_display_egl_new_with_egl_display( platformData->mDisplay );
if( ! sGstGLContext ) {
ci::gl::env()->makeContextCurrent( nullptr );
sGstGLContext = (GstGLContext*)gst_object_ref( gst_gl_context_new( sGstGLDisplay ) );
sharedCtx = gst_gl_context_new_wrapped( sGstGLDisplay, (guintptr)platformData->mContext, GST_GL_PLATFORM_EGL, GST_GL_API_GLES2 );
ci::gl::context()->makeCurrent( true );
}
#elif defined( CINDER_HEADLESS_GL_OSMESA )
if( ! sGstGLDisplay )
sGstGLDisplay = gst_gl_display_new();
if( ! sGstGLContext ) {
ci::gl::env()->makeContextCurrent( nullptr );
sGstGLContext = (GstGLContext*)gst_object_ref( gst_gl_context_new( sGstGLDisplay ) );
sharedCtx = gst_gl_context_new_wrapped( sGstGLDisplay, (guintptr)platformData->mContext, GST_GL_PLATFORM_ANY, GST_GL_API_ANY );
ci::gl::context()->makeCurrent( true );
}
#else
if( ! sGstGLDisplay )
sGstGLDisplay = (GstGLDisplay*)gst_gl_display_x11_new_with_display( ::glfwGetX11Display() );
if( ! sGstGLContext ) {
ci::gl::env()->makeContextCurrent( nullptr );
sGstGLContext = (GstGLContext*)gst_object_ref( gst_gl_context_new( sGstGLDisplay ) );
#if defined( CINDER_GL_ES )
sharedCtx = gst_gl_context_new_wrapped( sGstGLDisplay, (guintptr)::glfwGetEGLContext( platformData->mContext ), GST_GL_PLATFORM_GLX, GST_GL_API_GLES2 );
#else
sharedCtx = gst_gl_context_new_wrapped( sGstGLDisplay, (guintptr)::glfwGetGLXContext( platformData->mContext ), GST_GL_PLATFORM_GLX, GST_GL_API_OPENGL );
#endif
ci::gl::context()->makeCurrent( true );
}
#endif
#elif defined( CINDER_MAC )
auto platformData = std::dynamic_pointer_cast<ci::gl::PlatformDataMac>( ci::gl::context()->getPlatformData() );
if( ! sGstGLDisplay )
sGstGLDisplay = gst_gl_display_new ();
if( ! sGstGLContext ) {
ci::gl::env()->makeContextCurrent( nullptr );
sGstGLContext = (GstGLContext*)gst_object_ref( gst_gl_context_new( sGstGLDisplay ) );
sharedCtx = gst_gl_context_new_wrapped( sGstGLDisplay, (guintptr)platformData->mCglContext, GST_GL_PLATFORM_CGL, GST_GL_API_OPENGL );
ci::gl::context()->makeCurrent( true );
}
#elif defined( CINDER_MSW )
auto platformData = std::dynamic_pointer_cast<ci::gl::PlatformDataMsw>( ci::gl::context()->getPlatformData() );
if( ! sGstGLDisplay )
sGstGLDisplay = gst_gl_display_new();
if( ! sGstGLContext ) {
ci::gl::env()->makeContextCurrent( nullptr );
sGstGLContext = (GstGLContext*)gst_object_ref( gst_gl_context_new( sGstGLDisplay ) );
sharedCtx = gst_gl_context_new_wrapped( sGstGLDisplay, (guintptr)platformData->mGlrc, GST_GL_PLATFORM_WGL, GST_GL_API_OPENGL );
ci::gl::context()->makeCurrent( true );
}
#endif
ci::gl::env()->makeContextCurrent( nullptr );
if( sharedCtx ) {
if( ! gst_gl_context_create( sGstGLContext, sharedCtx, &err ) && err ) {
CI_LOG_E( "Failed to create shared context between Cinder and GStreamer : " << err->message );
g_free( err );
}
gst_object_unref( sharedCtx );
}
ci::gl::context()->makeCurrent( true );
if( sGstGLContext && ( sSharedCtxSignalHandler == 0 ) ) {
sSharedCtxSignalHandler = g_signal_connect( sGstGLDisplay, "create-context", G_CALLBACK( createGstGLContext ), nullptr );
}
}
#endif // defined( CINDER_GST_HAS_GL )
return true;
}
void GstPlayer::resetCustomPipeline()
{
// In case we use load after we 've set a custom pipeline ..
if( mGstData.pipeline ) {
resetPipeline(); // reset the custom pipeline
resetBus(); // reset the associated bus
mUsingCustomPipeline = false;
}
}
void GstPlayer::constructPipeline()
{
if( mGstData.pipeline ) return;
mGstData.pipeline = gst_element_factory_make( "playbin", "playbinsink" );
if( ! mGstData.pipeline ) {
// Not much we can do without at least playbin...
CI_LOG_E( "Failed to create playbin pipeline!" );
return;
}
mGstData.videoBin = gst_bin_new( "cinder-vbin" );
if( ! mGstData.videoBin ) CI_LOG_E( "Failed to create video bin!" );
mGstData.appSink = gst_element_factory_make( "appsink", "videosink" );
if( ! mGstData.appSink ) {
CI_LOG_E( "Failed to create app sink element!" );
}
else {
gst_app_sink_set_max_buffers( GST_APP_SINK( mGstData.appSink ), 1 );
gst_app_sink_set_drop( GST_APP_SINK( mGstData.appSink ), true );
gst_base_sink_set_qos_enabled( GST_BASE_SINK( mGstData.appSink ), true );
gst_base_sink_set_sync( GST_BASE_SINK( mGstData.appSink ), true );
gst_base_sink_set_max_lateness( GST_BASE_SINK( mGstData.appSink ), 20 * GST_MSECOND );
GstAppSinkCallbacks appSinkCallbacks;
appSinkCallbacks.eos = onGstEos;
appSinkCallbacks.new_preroll = onGstPreroll;
appSinkCallbacks.new_sample = onGstSample;
std::string capsDescr = "video/x-raw(memory:GLMemory), format=RGBA";
if( ! sUseGstGl ) {
capsDescr = "video/x-raw, format=RGBA";
}
gst_app_sink_set_callbacks( GST_APP_SINK( mGstData.appSink ), &appSinkCallbacks, this, 0 );
GstCaps* caps = gst_caps_from_string( capsDescr.c_str() );
gst_app_sink_set_caps( GST_APP_SINK( mGstData.appSink ), caps );
gst_caps_unref( caps );
}
GstPad *pad = nullptr;
if( sUseGstGl ) {
#if defined( CINDER_GST_HAS_GL )
mGstData.glupload = gst_element_factory_make( "glupload", "upload" );
if( ! mGstData.glupload ) CI_LOG_E( "Failed to create GL upload element!" );
mGstData.glcolorconvert = gst_element_factory_make( "glcolorconvert", "convert" );
if( ! mGstData.glcolorconvert ) CI_LOG_E( "Failed to create GL convert element!" );
mGstData.rawCapsFilter = gst_element_factory_make( "capsfilter", "rawcapsfilter" );
GstCaps* caps = nullptr;
#if defined( CINDER_LINUX_EGL_ONLY ) && defined( CINDER_GST_HAS_GL )
if( mGstData.rawCapsFilter ) {
caps = gst_caps_from_string( "video/x-raw(memory:GLMemory)" );
}
#else
if( mGstData.rawCapsFilter ) {
caps = gst_caps_from_string( "video/x-raw" );
}
#endif
else {
CI_LOG_E( "Failed to create raw caps filter element!" );
}
if( caps ) {
g_object_set( G_OBJECT( mGstData.rawCapsFilter ), "caps", caps, nullptr );
gst_caps_unref( caps );
}
gst_bin_add_many( GST_BIN( mGstData.videoBin ), mGstData.rawCapsFilter, mGstData.glupload, mGstData.glcolorconvert, mGstData.appSink, nullptr );
if( ! gst_element_link_many( mGstData.rawCapsFilter, mGstData.glupload, mGstData.glcolorconvert, mGstData.appSink, nullptr ) ) {
CI_LOG_E( "Failed to link video elements!" );
}
pad = gst_element_get_static_pad( mGstData.rawCapsFilter, "sink" );
gst_element_add_pad( mGstData.videoBin, gst_ghost_pad_new( "sink", pad ) );
#endif
}
else{
gst_bin_add( GST_BIN( mGstData.videoBin ), mGstData.appSink );
pad = gst_element_get_static_pad( mGstData.appSink, "sink" );
gst_element_add_pad( mGstData.videoBin, gst_ghost_pad_new( "sink", pad ) );
}
if( pad ) {
gst_object_unref( pad );
pad = nullptr;
}
g_object_set( G_OBJECT( mGstData.pipeline ), "video-sink", mGstData.videoBin, nullptr );
addBusWatch( mGstData.pipeline );
}
void GstPlayer::load( const std::string& path )
{
// If we are already using a custom pipeline we have to reset for now.
if( mUsingCustomPipeline ) {
resetCustomPipeline();
}
// Construct and link the elements of our pipeline if this is our first run..
if( ! mGstData.pipeline ) {
constructPipeline();
}
// Prepare for (re)loading....
setPipelineState( GST_STATE_READY );
// Reset the buffers after we have reached GST_STATE_READY.
resetVideoBuffers();
std::string uriPath;
if( path.find( "://", 0 ) == std::string::npos ) {
GError* err = nullptr;
gchar* uri = gst_filename_to_uri( path.c_str(), &err );
uriPath = std::string( static_cast<const char*>( uri ) );
g_free( uri );
if( err ) {
g_free( err );
}
mGstData.isStream = false;
}
else {
mGstData.isStream = true;
}
// set the new movie path
const gchar* uri = ( ! uriPath.empty() ) ? uriPath.c_str() : path.c_str();
g_object_set( G_OBJECT ( mGstData.pipeline ), "uri", uri, nullptr );
// and preroll.
setPipelineState( GST_STATE_PAUSED );
}
void GstPlayer::play()
{
setPipelineState( GST_STATE_PLAYING );
}
void GstPlayer::stop()
{
setPipelineState( GST_STATE_PAUSED );
}
bool GstPlayer::newVideo() const
{
return mGstData.videoHasChanged;
}
int GstPlayer::width() const
{
return mGstData.width;
}
int GstPlayer::height() const
{
return mGstData.height;
}
bool GstPlayer::isPaused() const
{
return mGstData.isPaused;
}
bool GstPlayer::isPlayable() const
{
return mGstData.isPlayable;
}
bool GstPlayer::isLoaded() const
{
return mGstData.isLoaded;
}
bool GstPlayer::isLiveSource() const
{
return mGstData.isLive;
}
bool GstPlayer::isBuffering() const
{
return mGstData.isBuffering;
}
bool GstPlayer::isPrerolled() const
{
return mGstData.isPrerolled;
}
GstVideoFormat GstPlayer::format() const
{
return mGstData.videoFormat;
}
float GstPlayer::getFramerate() const
{
return mGstData.frameRate;
}
bool GstPlayer::hasAudio() const
{
if( ! mGstData.pipeline )
return false;
gint numAudioChannels = 0;
g_object_get( G_OBJECT( mGstData.pipeline ), "n-audio", &numAudioChannels, nullptr );
return ( numAudioChannels > 0 ) ? true : false;
}
bool GstPlayer::hasVisuals() const
{
if( ! mGstData.pipeline )
return false;
gint numVideoStreams = 0;
g_object_get( G_OBJECT( mGstData.pipeline ), "n-video", &numVideoStreams, nullptr );
return ( numVideoStreams > 0 ) ? true : false;
}
gint64 GstPlayer::getDurationNanos()
{
if( ! mGstData.pipeline )
return -1;
gint64 duration = 0;
if( isPrerolled() ) {
if( ! gst_element_query_duration( mGstData.pipeline, GST_FORMAT_TIME, &duration ) ) {
CI_LOG_W( "Cannot query duration." );
return -1;
}
mGstData.duration = duration;
}
else {
mGstData.duration = -1;
CI_LOG_W( "Cannot query duration ! Pipeline is NOT PRE-ROLLED" );
}
return mGstData.duration;
}
float GstPlayer::getDurationSeconds()
{
gint64 duration = getDurationNanos();
return (float)duration / 1000000000.0f ;
}
int GstPlayer::getNumFrames()
{
mGstData.numFrames = (int)( getDurationNanos() * (float)mGstData.fpsNom / (float)mGstData.fpsDenom / (float) GST_SECOND );
return mGstData.numFrames;
}
float GstPlayer::getPixelAspectRatio() const
{
return mGstData.pixelAspectRatio;
}
gint64 GstPlayer::getPositionNanos()
{
if( ! mGstData.pipeline )
return -1;
if( isPrerolled() ) {
gint64 pos = 0;
if( ! gst_element_query_position( mGstData.pipeline, GST_FORMAT_TIME, &pos ) ) {
// Prerolled and we cannot query position we are hitting an async state change
// most probably from a pending seek. This may happen when scrubbing extremely fast.
// If that is the case fallback to the requested seek time as new position.
if( mGstData.requestedSeek ) {
pos = mGstData.requestedSeekTime * GST_SECOND;
}
else {
CI_LOG_W( "Cannot query position!" );
pos = -1;
}
}
mGstData.position = pos;
}
else {
mGstData.position = -1;
CI_LOG_W( "Cannot query position! Pipeline is not prerolled." );
}
return mGstData.position;
}
float GstPlayer::getPositionSeconds()
{
gint64 posNanos = getPositionNanos();
return (float)posNanos / 1000000000.0f;
}
void GstPlayer::setVolume( float targetVolume )
{
if( ! mGstData.pipeline )
return;
g_object_set( G_OBJECT( mGstData.pipeline ), "volume", (gdouble)targetVolume, nullptr );
}
float GstPlayer::getVolume()
{
if( ! mGstData.pipeline )
return -1;
float currentVolume;
g_object_get( G_OBJECT( mGstData.pipeline ), "volume", ¤tVolume, nullptr );
return currentVolume;
}
bool GstPlayer::isDone() const
{
return mGstData.isDone;
}
void GstPlayer::seekToTime( float seconds, bool forceSeek )
{
if( ! mGstData.pipeline )
return;
mGstData.isSeeking = true;
mGstData.isDone = false;
// When doing flushing seeks the pipeline will pre-roll
// which means that we might be in a GST_STATE_CHANGE_ASYNC when we request the seek ( ..when fast-seeking ).
// If thats the case then we should wait for GST_MESSAGE_ASYNC_DONE before executing the
// next seek.
if( ( getStateChange() == GST_STATE_CHANGE_ASYNC || isBuffering() ) && ! forceSeek ) {
mGstData.requestedSeek = true;
mGstData.requestedSeekTime = seconds;
return;
}
auto timeToSeek = seconds*GST_SECOND;
sendSeekEvent( timeToSeek );
}
void GstPlayer::seekToFrame( int frame )
{
if( ! mGstData.pipeline )
return;
auto timeToSeek = (float)frame * (float)mGstData.fpsDenom / (float)mGstData.fpsNom;
seekToTime( timeToSeek );
}
bool GstPlayer::stepForward()
{
bool handled = gst_element_send_event( mGstData.pipeline, gst_event_new_step ( GST_FORMAT_BUFFERS, 1, getRate(), TRUE, FALSE ) );
return handled;
}
void GstPlayer::setLoop( bool loop, bool palindrome )
{
mGstData.loop = loop;
mGstData.palindrome = palindrome;
}
bool GstPlayer::setRate( float rate )
{
if( rate == getRate() )
return true; // Avoid unnecessary rate change;
// A rate equal to 0 is not valid and has to be handled by pausing the pipeline.
if( rate == 0.0f ) {
return setPipelineState( GST_STATE_PAUSED );
}
if( rate < 0.0f && isStream() ) {
CI_LOG_W( "No reverse playback supported for streams!" );
return false;
}
mGstData.rate = rate;
// We need the position in case we have switched
// to reverse playeback i.e rate < 0
gint64 timeToSeek = getPositionNanos();
return sendSeekEvent( timeToSeek );
}
float GstPlayer::getRate() const
{
return mGstData.rate;
}
bool GstPlayer::hasNewFrame() const
{
return mNewFrame;
}
bool GstPlayer::isStream() const
{
return mGstData.isStream;
}
bool GstPlayer::sendSeekEvent( gint64 seekTime )
{
GstEvent* seekEvent;
GstSeekFlags seekFlags = GstSeekFlags( GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE );
if( getRate() > 0.0 ) {
seekEvent = gst_event_new_seek( getRate(), GST_FORMAT_TIME, seekFlags, GST_SEEK_TYPE_SET, seekTime, GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE );
}
else {
seekEvent = gst_event_new_seek( getRate(), GST_FORMAT_TIME, seekFlags, GST_SEEK_TYPE_SET, 0, GST_SEEK_TYPE_SET, seekTime );
}