/
ofGstUtils.cpp
1457 lines (1234 loc) · 38.9 KB
/
ofGstUtils.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
#include "ofGstUtils.h"
#ifndef TARGET_ANDROID
#include <gst/app/gstappsink.h>
#include <gst/video/video.h>
#if GST_VERSION_MAJOR>0
#include <gst/video/gstvideometa.h>
#endif
#ifdef OF_USE_GST_GL
#define GST_USE_UNSTABLE_API
#include <gst/gl/gl.h>
#endif
#include <glib-object.h>
#include <glib.h>
#include <algorithm>
#include "ofVideoPlayer.h"
#ifdef OF_USE_GST_GL
#ifndef TARGET_OPENGLES
#include <gst/gl/x11/gstgldisplay_x11.h>
#endif
#include <gst/gl/egl/gstgldisplay_egl.h>
#endif
#ifdef TARGET_WIN32
#include <winbase.h> // to use SetEnvironmentVariableA
#endif
using std::shared_ptr;
using std::string;
ofGstUtils::ofGstMainLoopThread * ofGstUtils::mainLoop = nullptr;
void ofGstUtils::startGstMainLoop(){
static bool initialized = false;
if(!initialized){
mainLoop = new ofGstMainLoopThread;
mainLoop->start();
initialized=true;
}
}
GMainLoop * ofGstUtils::getGstMainLoop(){
return mainLoop->getMainLoop();
}
void ofGstUtils::quitGstMainLoop(){
if(mainLoop){
mainLoop->quit();
delete mainLoop;
mainLoop = nullptr;
}
}
//-------------------------------------------------
//----------------------------------------- gstUtils
//-------------------------------------------------
static bool plugin_registered = false;
static bool gst_inited = false;
// called when the appsink notifies us that there is a new buffer ready for
// processing
static GstFlowReturn on_new_buffer_from_source (GstAppSink * elt, void * data){
#if GST_VERSION_MAJOR==0
shared_ptr<GstBuffer> buffer(gst_app_sink_pull_buffer (GST_APP_SINK (elt)),&gst_buffer_unref);
#else
shared_ptr<GstSample> buffer(gst_app_sink_pull_sample (GST_APP_SINK (elt)),&gst_sample_unref);
#endif
return ((ofGstUtils*)data)->buffer_cb(buffer);
}
static GstFlowReturn on_new_preroll_from_source (GstAppSink * elt, void * data){
#if GST_VERSION_MAJOR==0
shared_ptr<GstBuffer> buffer(gst_app_sink_pull_preroll(GST_APP_SINK (elt)),&gst_buffer_unref);
#else
shared_ptr<GstSample> buffer(gst_app_sink_pull_preroll(GST_APP_SINK (elt)),&gst_sample_unref);
#endif
return ((ofGstUtils*)data)->preroll_cb(buffer);
}
static void on_eos_from_source (GstAppSink * elt, void * data){
((ofGstUtils*)data)->eos_cb();
}
static gboolean appsink_plugin_init (GstPlugin * plugin)
{
gst_element_register (plugin, "appsink", GST_RANK_NONE, GST_TYPE_APP_SINK);
return TRUE;
}
ofGstUtils::ofGstUtils() {
bLoaded = false;
speed = 1;
bPaused = false;
bIsMovieDone = false;
bPlaying = false;
loopMode = OF_LOOP_NONE;
bFrameByFrame = false;
gstPipeline = NULL;
gstSink = NULL;
durationNanos = 0;
isAppSink = false;
isStream = false;
appsink = NULL;
closing = false;
busWatchID = 0;
#if GLIB_MINOR_VERSION<32
if(!g_thread_supported()){
g_thread_init(NULL);
}
#endif
if(!gst_inited){
#ifdef TARGET_WIN32
string gst_path;
if (sizeof(int) == 32){
auto env_path = g_getenv("GSTREAMER_1_0_ROOT_X86");
if (env_path == NULL) {
env_path = (ofGetTargetPlatform() == OF_TARGET_MINGW) ? g_getenv("GSTREAMER_1_0_ROOT_MINGW_X86") : g_getenv("GSTREAMER_1_0_ROOT_MSVC_X86");
}
gst_path = env_path;
}
else {
auto env_path = g_getenv("GSTREAMER_1_0_ROOT_X86_64");
if (env_path == NULL) {
env_path = (ofGetTargetPlatform() == OF_TARGET_MINGW) ? g_getenv("GSTREAMER_1_0_ROOT_MINGW_X86_64") : g_getenv("GSTREAMER_1_0_ROOT_MSVC_X86_64");
}
gst_path = env_path;
}
//putenv(("GST_PLUGIN_PATH_1_0=" + ofFilePath::join(gst_path, "lib\\gstreamer-1.0") + ";.").c_str());
// to make it compatible with gcc and C++11 standard
SetEnvironmentVariableA("GST_PLUGIN_PATH_1_0", ofFilePath::join(gst_path, "lib\\gstreamer-1.0").c_str());
#endif
gst_init (NULL, NULL);
gst_inited=true;
ofLogVerbose("ofGstUtils") << "gstreamer inited";
}
if(!plugin_registered){
gst_plugin_register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
"appsink", (char*)"Element application sink",
appsink_plugin_init, "0.1", "LGPL", "ofVideoPlayer", "openFrameworks",
"http://openframeworks.cc/");
plugin_registered=true;
}
}
ofGstUtils::~ofGstUtils() {
close();
}
#if GST_VERSION_MAJOR==0
GstFlowReturn ofGstUtils::preroll_cb(shared_ptr<GstBuffer> buffer){
#else
GstFlowReturn ofGstUtils::preroll_cb(shared_ptr<GstSample> buffer){
#endif
bIsMovieDone = false;
if(appsink) return appsink->on_preroll(buffer);
else return GST_FLOW_OK;
}
#if GST_VERSION_MAJOR==0
GstFlowReturn ofGstUtils::buffer_cb(shared_ptr<GstBuffer> buffer){
#else
GstFlowReturn ofGstUtils::buffer_cb(shared_ptr<GstSample> buffer){
#endif
bIsMovieDone = false;
if(appsink) return appsink->on_buffer(buffer);
else return GST_FLOW_OK;
}
void ofGstUtils::eos_cb(){
bIsMovieDone = true;
if(appsink && !isAppSink) appsink->on_eos();
if(closing){
std::unique_lock<std::mutex> lck(eosMutex);
eosCondition.notify_all();
}
}
bool ofGstUtils::setPipelineWithSink(string pipeline, string sinkname, bool isStream){
ofGstUtils::startGstMainLoop();
GError * error = NULL;
gstPipeline = gst_parse_launch (pipeline.c_str(), &error);
g_object_ref_sink(gstPipeline);
ofLogNotice("ofGstUtils") << "setPipelineWithSink(): gstreamer pipeline: " << pipeline;
if(error!=NULL){
ofLogError("ofGstUtils") << "setPipelineWithSink(): couldn't create pipeline: " << error->message;
return false;
}
if(sinkname!=""){
gstSink = gst_bin_get_by_name(GST_BIN(gstPipeline),sinkname.c_str());
if(!gstSink){
ofLogError("ofGstUtils") << "setPipelineWithSink(): couldn't get sink from string pipeline";
}
}
return setPipelineWithSink(gstPipeline,gstSink,isStream);
}
bool ofGstUtils::setPipelineWithSink(GstElement * pipeline, GstElement * sink, bool isStream_){
ofGstUtils::startGstMainLoop();
gstPipeline = pipeline;
gstSink = sink;
isStream = isStream_;
if(gstSink){
gst_base_sink_set_sync(GST_BASE_SINK(gstSink), true);
}
if(gstSink && string(gst_plugin_feature_get_name( GST_PLUGIN_FEATURE(gst_element_get_factory(gstSink))))=="appsink"){
isAppSink = true;
}else{
isAppSink = false;
}
return true;
}
void ofGstUtils::setFrameByFrame(bool _bFrameByFrame){
bFrameByFrame = _bFrameByFrame;
if(gstSink){
g_object_set (G_OBJECT (gstSink), "sync", !bFrameByFrame, (void*)NULL);
}
}
bool ofGstUtils::isFrameByFrame() const{
return bFrameByFrame;
}
bool ofGstUtils::startPipeline(){
bPaused = true;
speed = 1.0f;
GstBus * bus = gst_pipeline_get_bus (GST_PIPELINE(gstPipeline));
if(bus){
busWatchID = gst_bus_add_watch (bus, (GstBusFunc) busFunction, this);
}
gst_object_unref(bus);
// pause the pipeline
//GstState targetState;
GstState state;
auto ret = gst_element_set_state(GST_ELEMENT(gstPipeline), GST_STATE_PAUSED);
switch (ret) {
case GST_STATE_CHANGE_FAILURE:
ofLogError("ofGstUtils") << "startPipeline(): unable to pause pipeline";
return false;
break;
case GST_STATE_CHANGE_NO_PREROLL:
ofLogVerbose() << "Pipeline is live and does not need PREROLL waiting PLAY";
gst_element_set_state(GST_ELEMENT(gstPipeline), GST_STATE_PLAYING);
break;
case GST_STATE_CHANGE_ASYNC:
ofLogVerbose() << "Pipeline is PREROLLING";
//targetState = GST_STATE_PAUSED;
if(!isStream && gst_element_get_state(gstPipeline,&state,NULL,5*GST_SECOND)!=GST_STATE_CHANGE_SUCCESS){
ofLogError("ofGstUtils") << "startPipeline(): unable to pause pipeline after 5s";
return false;
}else{
ofLogVerbose() << "Pipeline is PREROLLED";
}
break;
case GST_STATE_CHANGE_SUCCESS:
ofLogVerbose() << "Pipeline is PREROLLED";
break;
}
if(isAppSink){
ofLogVerbose("ofGstUtils") << "startPipeline(): attaching callbacks";
bool bSignals = false;
#if GST_VERSION_MAJOR==0
GstAppSinkCallbacks gstCallbacks;
gstCallbacks.eos = &on_eos_from_source;
gstCallbacks.new_preroll = &on_new_preroll_from_source;
gstCallbacks.new_buffer = &on_new_buffer_from_source;
gst_app_sink_set_callbacks(GST_APP_SINK(gstSink), &gstCallbacks, this, NULL);
#elseif GST_VERSION_MINOR <= 18
GstAppSinkCallbacks gstCallbacks;
gstCallbacks.eos = &on_eos_from_source;
gstCallbacks.new_preroll = &on_new_preroll_from_source;
gstCallbacks.new_sample = &on_new_buffer_from_source;
gst_app_sink_set_callbacks(GST_APP_SINK(gstSink), &gstCallbacks, this, NULL);
#else
//GStreamer 1.20 onwards crashes with the callback approach above.
//Using signals makes it work! This might be a GStreamer bug - but we'll do this for now
g_signal_connect(GST_APP_SINK(gstSink), "eos", G_CALLBACK(on_eos_from_source), this);
g_signal_connect(GST_APP_SINK(gstSink), "new-sample", G_CALLBACK(on_new_buffer_from_source), this);
g_signal_connect(GST_APP_SINK(gstSink), "new-preroll", G_CALLBACK(on_new_preroll_from_source), this);
bSignals = true;
#endif
// set the appsink to not emit signals, we are using callbacks instead
// and frameByFrame to get buffers by polling instead of callback
g_object_set (G_OBJECT (gstSink), "emit-signals", bSignals, "sync", !bFrameByFrame, (void*)NULL);
gst_app_sink_set_max_buffers(GST_APP_SINK(gstSink),3);
gst_app_sink_set_drop (GST_APP_SINK(gstSink),true);
}
// wait for paused state to query the duration
if(!isStream){
bPlaying = true;
bLoaded = true;
}
return true;
}
void ofGstUtils::play(){
setPaused(false);
//this is if we set the speed first but it only can be set when we are playing.
if(!isStream) setSpeed(speed);
}
void ofGstUtils::setPaused(bool _bPause){
bPaused = _bPause;
//timeLastIdle = ofGetElapsedTimeMillis();
if(bLoaded){
if(bPlaying){
if(bPaused){
gst_element_set_state (gstPipeline, GST_STATE_PAUSED);
}else{
gst_element_set_state (gstPipeline, GST_STATE_PLAYING);
}
}else{
GstState state = GST_STATE_PAUSED;
gst_element_set_state (gstPipeline, state);
gst_element_get_state(gstPipeline,&state,NULL,2*GST_SECOND);
if(!bPaused){
gst_element_set_state (gstPipeline, GST_STATE_PLAYING);
}
}
bPlaying = !bPaused;
}
}
void ofGstUtils::stop(){
if(!bLoaded) return;
GstState state;
if(!bPaused){
state = GST_STATE_PAUSED;
gst_element_set_state (gstPipeline, state);
gst_element_get_state(gstPipeline,&state,NULL,2*GST_SECOND);
}
state = GST_STATE_READY;
gst_element_set_state (gstPipeline, state);
gst_element_get_state(gstPipeline,&state,NULL,2*GST_SECOND);
bPlaying = false;
bPaused = false;
}
float ofGstUtils::getPosition() const{
if(gstPipeline){
gint64 pos=0;
#if GST_VERSION_MAJOR==0
GstFormat format=GST_FORMAT_TIME;
if(!gst_element_query_position(GST_ELEMENT(gstPipeline),&format,&pos)){
ofLogVerbose("ofGstUtils") << "getPosition(): couldn't query position";
return -1;
}
#else
if(!gst_element_query_position(GST_ELEMENT(gstPipeline),GST_FORMAT_TIME,&pos)){
ofLogVerbose("ofGstUtils") << "getPosition(): couldn't query position";
return -1;
}
#endif
return (float)pos/(float)durationNanos;
}else{
return -1;
}
}
int64_t ofGstUtils::getPositionNanos() const{
if(gstPipeline){
gint64 pos=0;
#if GST_VERSION_MAJOR==0
GstFormat format=GST_FORMAT_TIME;
if(!gst_element_query_position(GST_ELEMENT(gstPipeline),&format,&pos)){
ofLogVerbose("ofGstUtils") << "getPosition(): couldn't query position";
return -1;
}
#else
if(!gst_element_query_position(GST_ELEMENT(gstPipeline),GST_FORMAT_TIME,&pos)){
ofLogVerbose("ofGstUtils") << "getPosition(): couldn't query position";
return -1;
}
#endif
return pos;
}else{
return -1;
}
}
float ofGstUtils::getSpeed() const{
return speed;
}
float ofGstUtils::getDuration() const{
return (float)getDurationNanos()/(float)GST_SECOND;
}
int64_t ofGstUtils::getDurationNanos() const{
GstFormat format = GST_FORMAT_TIME;
#if GST_VERSION_MAJOR==0
if(!gst_element_query_duration(getPipeline(),&format,&durationNanos))
ofLogWarning("ofGstUtils") << "getDurationNanos(): couldn't query time duration";
#else
if(!gst_element_query_duration(getPipeline(),format,&durationNanos))
ofLogWarning("ofGstUtils") << "getDurationNanos(): couldn't query time duration";
#endif
return durationNanos;
}
bool ofGstUtils::getIsMovieDone() const{
if(isAppSink){
return gst_app_sink_is_eos(GST_APP_SINK(gstSink));
}else{
return bIsMovieDone;
}
}
void ofGstUtils::setPosition(float pct){
//pct = CLAMP(pct, 0,1);// check between 0 and 1;
GstFormat format = GST_FORMAT_TIME;
GstSeekFlags flags = (GstSeekFlags) (GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH);
if(speed > 1 || speed < -1){
flags = (GstSeekFlags)(flags | GST_SEEK_FLAG_SKIP);
}
gint64 pos = (guint64)((double)pct*(double)durationNanos);
/*if(bPaused){
seek_lock();
gst_element_set_state (gstPipeline, GST_STATE_PLAYING);
posChangingPaused=true;
seek_unlock();
}*/
if(speed>0){
if(!gst_element_seek(GST_ELEMENT(gstPipeline),speed, format,
flags,
GST_SEEK_TYPE_SET,
pos,
GST_SEEK_TYPE_SET,
-1)) {
ofLogWarning("ofGstUtils") << "setPosition(): unable to seek";
}
}else{
if(!gst_element_seek(GST_ELEMENT(gstPipeline),speed, format,
flags,
GST_SEEK_TYPE_SET,
0,
GST_SEEK_TYPE_SET,
pos)) {
ofLogWarning("ofGstUtils") << "setPosition(): unable to seek";
}
}
}
void ofGstUtils::setVolume(float volume){
gdouble gvolume = volume;
g_object_set(G_OBJECT(gstPipeline), "volume", gvolume, (void*)NULL);
}
void ofGstUtils::setLoopState(ofLoopType state){
loopMode = state;
}
void ofGstUtils::setSpeed(float _speed){
if(_speed == speed) return;
GstFormat format = GST_FORMAT_TIME;
GstSeekFlags flags = (GstSeekFlags) (GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_FLUSH);
bool needPos = true;
#if GST_VERSION_MAJOR >= 1 && GST_VERSION_MINOR >= 18
//if requested speed is the same direction as we are already going we can do a fast rate change
if( speed * _speed > 0 ){
flags = GST_SEEK_FLAG_INSTANT_RATE_CHANGE;
needPos = false;
}
#endif
if(_speed > 1 || _speed < -1){
flags = (GstSeekFlags)(flags | GST_SEEK_FLAG_SKIP);
}
if(_speed==0){
gst_element_set_state (gstPipeline, GST_STATE_PAUSED);
return;
}
gint64 pos = 0;
GstSeekType seekType = GST_SEEK_TYPE_NONE;
if( needPos ){
seekType = GST_SEEK_TYPE_SET;
pos = getPositionNanos();
if( pos == -1 ){
ofLogError("ofGstUtils") << "setSpeed(): couldn't query position";
return;
}
}
speed = _speed;
if(!bPaused)
gst_element_set_state (gstPipeline, GST_STATE_PLAYING);
if(speed>0){
if(!gst_element_seek(GST_ELEMENT(gstSink), speed, format,
flags,
seekType,
pos,
seekType,
-1)) {
ofLogWarning("ofGstUtils") << "setSpeed(): unable to change speed";
}
}else{
if(!gst_element_seek(GST_ELEMENT(gstSink), speed, format,
flags,
seekType,
0,
seekType,
pos)) {
ofLogWarning("ofGstUtils") << "setSpeed(): unable to change speed";
}
}
ofLogVerbose("ofGstUtils") << "setSpeed(): speed changed to " << speed;
}
void ofGstUtils::close(){
if(bPlaying){
if(!bIsMovieDone && !bPaused && !isStream){
std::unique_lock<std::mutex> lck(eosMutex);
closing = true;
gst_element_send_event(gstPipeline,gst_event_new_eos());
if(eosCondition.wait_for(lck,std::chrono::milliseconds(5000))==std::cv_status::timeout){
ofLogWarning("ofGstUtils") << "didn't received EOS in 5s, closing pipeline anyway";
}
closing = false;
}
}
stop();
if(bLoaded){
gst_element_set_state(GST_ELEMENT(gstPipeline), GST_STATE_NULL);
gst_element_get_state(gstPipeline,NULL,NULL,2*GST_SECOND);
if(busWatchID!=0) g_source_remove(busWatchID);
gst_object_unref(gstPipeline);
gstPipeline = NULL;
gstSink = NULL;
}
bLoaded = false;
}
/*static string getName(GstState state){
switch(state){
case GST_STATE_VOID_PENDING:
return "void pending";
case GST_STATE_NULL:
return "null";
case GST_STATE_READY:
return "ready";
case GST_STATE_PAUSED:
return "paused";
case GST_STATE_PLAYING:
return "playing";
default:
return "";
}
}*/
bool ofGstUtils::busFunction(GstBus * bus, GstMessage * message, ofGstUtils * gstUtils){
return gstUtils->gstHandleMessage(bus,message);
}
bool ofGstUtils::gstHandleMessage(GstBus * bus, GstMessage * msg){
if(appsink && appsink->on_message(msg)) return true;
/*ofLogVerbose("ofGstUtils") << "gstHandleMessage(): got " << GST_MESSAGE_TYPE_NAME(msg)
<< " message from " << GST_MESSAGE_SRC_NAME(msg);*/
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_BUFFERING:
gint pctBuffered;
gst_message_parse_buffering(msg,&pctBuffered);
ofLogVerbose("ofGstUtils") << "gstHandleMessage(): buffering " << pctBuffered;
if(pctBuffered<100){
gst_element_set_state (gstPipeline, GST_STATE_PAUSED);
}else if(!bPaused){
gst_element_set_state (gstPipeline, GST_STATE_PLAYING);
}
break;
#if GST_VERSION_MAJOR==0
case GST_MESSAGE_DURATION:{
GstFormat format=GST_FORMAT_TIME;
gst_element_query_duration(gstPipeline,&format,&durationNanos);
}break;
#else
case GST_MESSAGE_DURATION_CHANGED:
gst_element_query_duration(gstPipeline,GST_FORMAT_TIME,&durationNanos);
break;
#endif
case GST_MESSAGE_STATE_CHANGED:{
GstState oldstate, newstate, pendstate;
gst_message_parse_state_changed(msg, &oldstate, &newstate, &pendstate);
if(isStream && newstate==GST_STATE_PAUSED && !bPlaying ){
bLoaded = true;
bPlaying = true;
if(!bPaused){
//ofLogVerbose("ofGstUtils") << "gstHandleMessage(): setting stream pipeline to play";
play();
}
}
/*ofLogVerbose("ofGstUtils") << "gstHandleMessage(): " << GST_MESSAGE_SRC_NAME(msg) << " state changed from "
<< getName(oldstate) << " to " << getName(newstate) << " (" + getName(pendstate) << ")";*/
}break;
case GST_MESSAGE_ASYNC_DONE:
ofLogVerbose("ofGstUtils") << "gstHandleMessage(): async done";
break;
case GST_MESSAGE_ERROR: {
GError *err;
gchar *debug;
gst_message_parse_error(msg, &err, &debug);
gchar * name = gst_element_get_name(GST_MESSAGE_SRC (msg));
ofLogError("ofGstUtils") << "gstHandleMessage(): embedded video playback halted for plugin, module "
<< name << " reported: " << err->message;
g_free(name);
g_error_free(err);
g_free(debug);
gst_element_set_state(GST_ELEMENT(gstPipeline), GST_STATE_NULL);
}break;
case GST_MESSAGE_EOS:{
ofLogVerbose("ofGstUtils") << "gstHandleMessage(): end of the stream";
bool isClosing = closing;
eos_cb();
if(isClosing){
busWatchID = 0;
return false;
}
switch(loopMode){
case OF_LOOP_NORMAL:{
GstFormat format = GST_FORMAT_TIME;
GstSeekFlags flags = (GstSeekFlags) (GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT);
if(speed>0){
if(!gst_element_seek(GST_ELEMENT(gstPipeline),
speed,
format,
flags,
GST_SEEK_TYPE_SET,
0,
GST_SEEK_TYPE_SET,
-1)) {
ofLogWarning("ofGstUtils") << "gstHandleMessage(): unable to seek";
}
}else if(speed<0){
if(!gst_element_seek(GST_ELEMENT(gstPipeline),speed, format,
flags,
GST_SEEK_TYPE_SET,
0,
GST_SEEK_TYPE_SET,
durationNanos-1000000)) {
ofLogWarning("ofGstUtils") << "gstHandleMessage(): unable to seek";
}
}
}break;
case OF_LOOP_PALINDROME:{
GstFormat format = GST_FORMAT_TIME;
GstSeekFlags flags = (GstSeekFlags) (GST_SEEK_FLAG_FLUSH |GST_SEEK_FLAG_KEY_UNIT);
gint64 pos;
#if GST_VERSION_MAJOR==0
gst_element_query_position(GST_ELEMENT(gstPipeline),&format,&pos);
#else
gst_element_query_position(GST_ELEMENT(gstPipeline),format,&pos);
#endif
float loopSpeed;
if(pos>0)
loopSpeed=-speed;
else
loopSpeed=speed;
if(!gst_element_seek(GST_ELEMENT(gstPipeline),
loopSpeed,
GST_FORMAT_UNDEFINED,
flags,
GST_SEEK_TYPE_NONE,
0,
GST_SEEK_TYPE_NONE,
0)) {
ofLogWarning("ofGstUtils") << "gstHandleMessage(): unable to seek";
}
}break;
default:
break;
}
}break;
case GST_MESSAGE_LATENCY:
gst_bin_recalculate_latency (GST_BIN (getPipeline()));
break;
case GST_MESSAGE_REQUEST_STATE: {
GstState state;
gchar *name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
gst_message_parse_request_state (msg, &state);
gst_element_set_state (getPipeline(), state);
g_free (name);
break;
}
#if GST_VERSION_MAJOR==1 && GST_VERSION_MINOR>=2
case GST_MESSAGE_HAVE_CONTEXT:{
GstContext *context;
const gchar *context_type;
gchar *context_str;
gst_message_parse_have_context (msg, &context);
context_type = gst_context_get_context_type (context);
context_str = gst_structure_to_string (gst_context_get_structure (context));
ofLogNotice("ofGstUtils","Got context from element '%s': %s=%s\n",
GST_ELEMENT_NAME (GST_MESSAGE_SRC (msg)), context_type,
context_str);
g_free (context_str);
gst_context_unref (context);
break;
}
#endif
default:
ofLogVerbose("ofGstUtils") << "gstHandleMessage(): unhandled message from " << GST_MESSAGE_SRC_NAME(msg);
break;
}
return true;
}
GstElement * ofGstUtils::getPipeline() const{
return gstPipeline;
}
GstElement * ofGstUtils::getSink() const{
return gstSink;
}
GstElement * ofGstUtils::getGstElementByName(const string & name) const{
return gst_bin_get_by_name(GST_BIN(gstPipeline),name.c_str());
}
void ofGstUtils::setSinkListener(ofGstAppSink * appsink_){
appsink = appsink_;
}
uint64_t ofGstUtils::getMinLatencyNanos() const{
GstClockTime minlat=0, maxlat=0;
GstQuery * q = gst_query_new_latency();
if (gst_element_query (gstPipeline, q)) {
gboolean live;
gst_query_parse_latency (q, &live, &minlat, &maxlat);
}
gst_query_unref (q);
return minlat;
}
uint64_t ofGstUtils::getMaxLatencyNanos() const{
GstClockTime minlat=0, maxlat=0;
GstQuery * q = gst_query_new_latency();
if (gst_element_query (gstPipeline, q)) {
gboolean live;
gst_query_parse_latency (q, &live, &minlat, &maxlat);
}
gst_query_unref (q);
return maxlat;
}
//-------------------------------------------------
//----------------------------------------- videoUtils
//-------------------------------------------------
ofGstVideoUtils::ofGstVideoUtils(){
bIsFrameNew = false;
bHavePixelsChanged = false;
bBackPixelsChanged = false;
#if GST_VERSION_MAJOR==1
GstMapInfo initMapinfo = {0,};
mapinfo = initMapinfo;
#endif
internalPixelFormat = OF_PIXELS_RGB;
#ifdef OF_USE_GST_GL
glDisplay = NULL;
glContext = NULL;
#endif
copyPixels = false;
}
ofGstVideoUtils::~ofGstVideoUtils(){
close();
}
void ofGstVideoUtils::close(){
ofGstUtils::close();
std::unique_lock<std::mutex> lock(mutex);
pixels.clear();
backPixels.clear();
eventPixels.clear();
bIsFrameNew = false;
bHavePixelsChanged = false;
bBackPixelsChanged = false;
frontBuffer.reset();
backBuffer.reset();
#if GST_VERSION_MAJOR==1
while(!bufferQueue.empty()) bufferQueue.pop();
#endif
}
bool ofGstVideoUtils::isInitialized() const{
return isLoaded();
}
bool ofGstVideoUtils::isFrameNew() const{
return bIsFrameNew;
}
ofPixels& ofGstVideoUtils::getPixels(){
return pixels;
}
const ofPixels & ofGstVideoUtils::getPixels() const{
return pixels;
}
ofTexture * ofGstVideoUtils::getTexture(){
#ifdef OF_USE_GST_GL
if(frontTexture.isAllocated()){
return &frontTexture;
}else{
return NULL;
}
#else
return NULL;
#endif
}
void ofGstVideoUtils::update(){
if (isLoaded()){
if(!isFrameByFrame()){
std::unique_lock<std::mutex> lock(mutex);
bHavePixelsChanged = bBackPixelsChanged;
if (bHavePixelsChanged){
bBackPixelsChanged=false;
std::swap(pixels,backPixels);
#ifdef OF_USE_GST_GL
if(backTexture.isAllocated()){
frontTexture.getTextureData() = backTexture.getTextureData();
frontTexture.setTextureMinMagFilter(GL_LINEAR,GL_LINEAR);
frontTexture.setTextureWrap(GL_CLAMP_TO_EDGE,GL_CLAMP_TO_EDGE);
}
#endif
if(!copyPixels){
frontBuffer = backBuffer;
}
}
}else{
#if GST_VERSION_MAJOR==0
ofLogError() << "frame by frame doesn't work any more in 0.10";
#else
GstBuffer * buffer;
GstSample * sample;
//get the buffer from appsink
if(isPaused()){
sample = gst_app_sink_pull_preroll (GST_APP_SINK (getSink()));
}else{
sample = gst_app_sink_pull_sample (GST_APP_SINK (getSink()));
}
buffer = gst_sample_get_buffer(sample);
if(buffer){
if(pixels.isAllocated()){
gst_buffer_map (buffer, &mapinfo, GST_MAP_READ);
//TODO: stride = mapinfo.size / height;
pixels.setFromExternalPixels(mapinfo.data,pixels.getWidth(),pixels.getHeight(),pixels.getNumChannels());
backBuffer = shared_ptr<GstSample>(sample,gst_sample_unref);
bHavePixelsChanged=true;
gst_buffer_unmap(buffer,&mapinfo);
}
}
#endif
}
}else{
ofLogWarning("ofGstVideoUtils") << "update(): ofGstVideoUtils not loaded";
}
bIsFrameNew = bHavePixelsChanged;
bHavePixelsChanged = false;
}
float ofGstVideoUtils::getHeight() const{
return pixels.getHeight();
}
float ofGstVideoUtils::getWidth() const{
return pixels.getWidth();
}
#if GST_VERSION_MAJOR>0
string ofGstVideoUtils::getGstFormatName(ofPixelFormat format){
switch(format){
case OF_PIXELS_GRAY:
return "GRAY8";
case OF_PIXELS_RGBA:
return "RGBA";
case OF_PIXELS_BGRA:
return "BGRA";
case OF_PIXELS_RGB565:
return "RGB16";
case OF_PIXELS_NV12:
return "NV12";
case OF_PIXELS_NV21:
return "NV21";
case OF_PIXELS_YV12:
return "YV12";
case OF_PIXELS_I420:
return "I420";
case OF_PIXELS_YUY2:
return "YUY2";
case OF_PIXELS_RGB:
return "RGB";
case OF_PIXELS_BGR:
return "BGR";
default:
return "UNKNOWN";
}