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
|
#include "gstvideoloader.h"
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
#include <gst/video/video.h>
#include <glib-object.h>
#include <glib.h>
#include <algorithm>
using namespace std;
void ofGstUtils::startGstMainLoop(){
static bool initialized = false;
if(!initialized){
g_main_loop_new (NULL, FALSE);
initialized=true;
}
}
//-------------------------------------------------
//----------------------------------------- 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){
GstBuffer *buffer = gst_app_sink_pull_buffer (GST_APP_SINK (elt));
return ((ofGstUtils*)data)->buffer_cb(buffer);
}
static GstFlowReturn on_new_preroll_from_source (GstAppSink * elt, void * data){
GstBuffer *buffer = gst_app_sink_pull_preroll(GST_APP_SINK (elt));
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;
if(!g_thread_supported()){
g_thread_init(NULL);
}
if(!gst_inited){
gst_init (NULL, NULL);
gst_inited=true;
cerr <<"ofGstUtils: gstreamer inited"<<endl;
}
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();
}
GstFlowReturn ofGstUtils::preroll_cb(GstBuffer * buffer){
bIsMovieDone = false;
if(appsink) return appsink->on_preroll(buffer);
else return GST_FLOW_OK;
}
GstFlowReturn ofGstUtils::buffer_cb(GstBuffer * buffer){
bIsMovieDone = false;
if(appsink) return appsink->on_buffer(buffer);
else return GST_FLOW_OK;
}
void ofGstUtils::eos_cb(){
bIsMovieDone = true;
if(appsink) appsink->on_eos();
}
bool ofGstUtils::setPipelineWithSink(string pipeline, string sinkname, bool isStream){
ofGstUtils::startGstMainLoop();
gchar* pipeline_string =
g_strdup((pipeline).c_str());
GError * error = NULL;
gstPipeline = gst_parse_launch (pipeline_string, &error);
cerr << "gstreamer pipeline: " <<pipeline_string<<endl;
if(error!=NULL){
cerr <<"couldnt create pipeline: " << string(error->message)<<endl;
return false;
}
gstSink = gst_bin_get_by_name(GST_BIN(gstPipeline),sinkname.c_str());
if(!gstSink){
cerr << "couldn't get sink from string pipeline"<<endl;
}
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 startPipeline();
}
void ofGstUtils::setFrameByFrame(bool _bFrameByFrame){
bFrameByFrame = _bFrameByFrame;
if(gstSink){
g_object_set (G_OBJECT (gstSink), "sync", !bFrameByFrame, (void*)NULL);
}
}
bool ofGstUtils::isFrameByFrame(){
return bFrameByFrame;
}
|