reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofGstUtils.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofConstants.h"
4#ifndef TARGET_ANDROID
5#include "ofPixels.h"
6#include "ofEvents.h"
7#include "ofThread.h"
8#include "ofVideoBaseTypes.h"
9#define GST_DISABLE_DEPRECATED
10#include <gst/gst.h>
11#include <gst/gstpad.h>
12#include <gst/video/video.h>
13#include <queue>
14#include <condition_variable>
15#include <mutex>
16
17//#define OF_USE_GST_GL
18#ifdef OF_USE_GST_GL
19#define GST_USE_UNSTABLE_API
20#include <gst/gl/gl.h>
21#endif
22
23class ofGstAppSink;
24class ofTexture;
25typedef struct _GstElement GstElement;
26typedef struct _GstBuffer GstBuffer;
27typedef struct _GstMessage GstMessage;
28
29//-------------------------------------------------
30//----------------------------------------- ofGstUtils
31//-------------------------------------------------
32
34public:
35 ofGstUtils();
36 virtual ~ofGstUtils();
37
38 bool setPipelineWithSink(std::string pipeline, std::string sinkname="sink", bool isStream=false);
39 bool setPipelineWithSink(GstElement * pipeline, GstElement * sink, bool isStream=false);
40 bool startPipeline();
41
42 void play();
43 void stop();
44 void setPaused(bool bPause);
45 bool isPaused() const {return bPaused;}
46 bool isLoaded() const {return bLoaded;}
47 bool isPlaying() const {return bPlaying;}
48
49 float getPosition() const;
50 int64_t getPositionNanos() const;
51 float getSpeed() const;
52 float getDuration() const;
53 int64_t getDurationNanos() const;
54 bool getIsMovieDone() const;
55
56 void setPosition(float pct);
57 void setVolume(float volume);
58 void setLoopState(ofLoopType state);
59 ofLoopType getLoopState() const {return loopMode;}
60 void setSpeed(float speed);
61
62 void setFrameByFrame(bool bFrameByFrame);
63 bool isFrameByFrame() const;
64
65 GstElement * getPipeline() const;
66 GstElement * getSink() const;
67 GstElement * getGstElementByName(const std::string & name) const;
68 uint64_t getMinLatencyNanos() const;
69 uint64_t getMaxLatencyNanos() const;
70
71 virtual void close();
72
74
75 // callbacks to get called from gstreamer
76#if GST_VERSION_MAJOR==0
77 virtual GstFlowReturn preroll_cb(std::shared_ptr<GstBuffer> buffer);
78 virtual GstFlowReturn buffer_cb(std::shared_ptr<GstBuffer> buffer);
79#else
80 virtual GstFlowReturn preroll_cb(std::shared_ptr<GstSample> buffer);
81 virtual GstFlowReturn buffer_cb(std::shared_ptr<GstSample> buffer);
82#endif
83 virtual void eos_cb();
84
85 static void startGstMainLoop();
86 static GMainLoop * getGstMainLoop();
87 static void quitGstMainLoop();
88protected:
91 bool closing;
92
93private:
94 static bool busFunction(GstBus * bus, GstMessage * message, ofGstUtils * app);
95 bool gstHandleMessage(GstBus * bus, GstMessage * message);
96
97 bool bPlaying;
98 bool bPaused;
99 bool bIsMovieDone;
100 bool bLoaded;
101 bool bFrameByFrame;
102 ofLoopType loopMode;
103
104 GstElement * gstSink;
105 GstElement * gstPipeline;
106
107 float speed;
108 mutable gint64 durationNanos;
109 bool isAppSink;
110 std::condition_variable eosCondition;
111 std::mutex eosMutex;
112 guint busWatchID;
113
114 class ofGstMainLoopThread: public ofThread{
115 public:
116 ofGstMainLoopThread()
117 :main_loop(nullptr)
118 {
119 }
120
121 virtual ~ofGstMainLoopThread(){};
122
123 void start(){
124 main_loop = g_main_loop_new (NULL, FALSE);
125 startThread();
126 }
127 void threadedFunction(){
128 g_main_loop_run (main_loop);
129 }
130
131 GMainLoop * getMainLoop(){
132 return main_loop;
133 }
134
135 void quit(){
136 if(main_loop){
137 g_main_loop_quit(main_loop);
139 }
140 }
141 private:
142 GMainLoop *main_loop = nullptr;
143 };
144
145 static ofGstMainLoopThread * mainLoop;
146};
147
148
149
150
151
152//-------------------------------------------------
153//----------------------------------------- videoUtils
154//-------------------------------------------------
155
157public:
158
160 virtual ~ofGstVideoUtils();
161
162 bool setPipeline(std::string pipeline, ofPixelFormat pixelFormat=OF_PIXELS_RGB, bool isStream=false, int w=-1, int h=-1);
163
164 bool setPixelFormat(ofPixelFormat pixelFormat);
166 bool allocate(int w, int h, ofPixelFormat pixelFormat);
168
169 bool isFrameNew() const;
171 const ofPixels& getPixels() const;
173 void update();
174
175 float getHeight() const;
176 float getWidth() const;
177
178 void close();
179
180#if GST_VERSION_MAJOR>0
181 static std::string getGstFormatName(ofPixelFormat format);
182 static GstVideoFormat getGstFormat(ofPixelFormat format);
183 static ofPixelFormat getOFFormat(GstVideoFormat format);
184#endif
185
186 bool isInitialized() const;
187
188 // copy pixels from gst buffer to avoid
189 // https://bugzilla.gnome.org/show_bug.cgi?id=737427
190 void setCopyPixels(bool copy);
191
192 // this events happen in a different thread
193 // do not use them for opengl stuff
197
198protected:
199#if GST_VERSION_MAJOR==0
200 GstFlowReturn process_buffer(std::shared_ptr<GstBuffer> buffer);
201 GstFlowReturn preroll_cb(std::shared_ptr<GstBuffer> buffer);
202 GstFlowReturn buffer_cb(std::shared_ptr<GstBuffer> buffer);
203#else
204 GstFlowReturn process_sample(std::shared_ptr<GstSample> sample);
205 GstFlowReturn preroll_cb(std::shared_ptr<GstSample> buffer);
206 GstFlowReturn buffer_cb(std::shared_ptr<GstSample> buffer);
207#endif
208 void eos_cb();
209
210
211 ofPixels pixels; // 24 bit: rgb
214private:
215 static gboolean sync_bus_call (GstBus * bus, GstMessage * msg, gpointer data);
216 bool bIsFrameNew; // if we are new
217 bool bHavePixelsChanged;
218 bool bBackPixelsChanged;
219 std::mutex mutex;
220#if GST_VERSION_MAJOR==0
221 std::shared_ptr<GstBuffer> frontBuffer, backBuffer;
222#else
223 std::shared_ptr<GstSample> frontBuffer, backBuffer;
224 std::queue<std::shared_ptr<GstSample> > bufferQueue;
225 GstMapInfo mapinfo;
226 #ifdef OF_USE_GST_GL
227 ofTexture frontTexture, backTexture;
228 #endif
229#endif
230 ofPixelFormat internalPixelFormat;
231 bool copyPixels; // fix for certain versions bug with v4l2
232
233#ifdef OF_USE_GST_GL
234 GstGLDisplay * glDisplay;
235 GstGLContext * glContext;
236#endif
237};
238
239
240//-------------------------------------------------
241//----------------------------------------- appsink listener
242//-------------------------------------------------
243
245public:
246 virtual ~ofGstAppSink(){}
247#if GST_VERSION_MAJOR==0
248 virtual GstFlowReturn on_preroll(std::shared_ptr<GstBuffer> buffer){
249 return GST_FLOW_OK;
250 }
251 virtual GstFlowReturn on_buffer(std::shared_ptr<GstBuffer> buffer){
252 return GST_FLOW_OK;
253 }
254#else
255 virtual GstFlowReturn on_preroll(std::shared_ptr<GstSample> buffer){
256 return GST_FLOW_OK;
257 }
258 virtual GstFlowReturn on_buffer(std::shared_ptr<GstSample> buffer){
259 return GST_FLOW_OK;
260 }
261#endif
262 virtual void on_eos(){}
263
264 // return true to set the message as attended so upstream doesn't try to process it
265 virtual bool on_message(GstMessage* msg){return false;};
266
267 // pings when enough data has arrived to be able to get sink properties
268 virtual void on_stream_prepared(){};
269};
270
271#endif
272
A base class representing a video source.
Definition ofVideoBaseTypes.h:69
Definition ofEvent.h:444
Definition ofGstUtils.h:244
virtual void on_eos()
Definition ofGstUtils.h:262
virtual ~ofGstAppSink()
Definition ofGstUtils.h:246
virtual void on_stream_prepared()
Definition ofGstUtils.h:268
virtual bool on_message(GstMessage *msg)
Definition ofGstUtils.h:265
virtual GstFlowReturn on_preroll(std::shared_ptr< GstBuffer > buffer)
Definition ofGstUtils.h:248
virtual GstFlowReturn on_buffer(std::shared_ptr< GstBuffer > buffer)
Definition ofGstUtils.h:251
Definition ofGstUtils.h:33
void setLoopState(ofLoopType state)
Definition ofGstUtils.cpp:503
virtual GstFlowReturn buffer_cb(std::shared_ptr< GstBuffer > buffer)
Definition ofGstUtils.cpp:179
void setVolume(float volume)
Definition ofGstUtils.cpp:498
void setSpeed(float speed)
Definition ofGstUtils.cpp:507
void setFrameByFrame(bool bFrameByFrame)
Definition ofGstUtils.cpp:241
uint64_t getMinLatencyNanos() const
Definition ofGstUtils.cpp:816
ofGstUtils()
Definition ofGstUtils.cpp:100
int64_t getDurationNanos() const
Definition ofGstUtils.cpp:440
ofLoopType getLoopState() const
Definition ofGstUtils.h:59
ofGstAppSink * appsink
Definition ofGstUtils.h:89
float getPosition() const
Definition ofGstUtils.cpp:389
float getDuration() const
Definition ofGstUtils.cpp:436
static GMainLoop * getGstMainLoop()
Definition ofGstUtils.cpp:44
bool isFrameByFrame() const
Definition ofGstUtils.cpp:248
static void startGstMainLoop()
Definition ofGstUtils.cpp:35
virtual ~ofGstUtils()
Definition ofGstUtils.cpp:163
void play()
Definition ofGstUtils.cpp:343
GstElement * getPipeline() const
Definition ofGstUtils.cpp:800
float getSpeed() const
Definition ofGstUtils.cpp:432
static void quitGstMainLoop()
Definition ofGstUtils.cpp:48
bool isPaused() const
Definition ofGstUtils.h:45
virtual void eos_cb()
Definition ofGstUtils.cpp:188
void setSinkListener(ofGstAppSink *appsink)
Definition ofGstUtils.cpp:812
bool setPipelineWithSink(std::string pipeline, std::string sinkname="sink", bool isStream=false)
int64_t getPositionNanos() const
Definition ofGstUtils.cpp:410
bool startPipeline()
Definition ofGstUtils.cpp:252
bool isPlaying() const
Definition ofGstUtils.h:47
virtual GstFlowReturn preroll_cb(std::shared_ptr< GstBuffer > buffer)
Definition ofGstUtils.cpp:168
bool getIsMovieDone() const
Definition ofGstUtils.cpp:454
bool isStream
Definition ofGstUtils.h:90
void stop()
Definition ofGstUtils.cpp:372
void setPosition(float pct)
Definition ofGstUtils.cpp:462
virtual void close()
Definition ofGstUtils.cpp:574
GstElement * getSink() const
Definition ofGstUtils.cpp:804
bool isLoaded() const
Definition ofGstUtils.h:46
GstElement * getGstElementByName(const std::string &name) const
Definition ofGstUtils.cpp:808
bool closing
Definition ofGstUtils.h:91
void setPaused(bool bPause)
Definition ofGstUtils.cpp:350
uint64_t getMaxLatencyNanos() const
Definition ofGstUtils.cpp:827
Definition ofGstUtils.h:156
virtual ~ofGstVideoUtils()
Definition ofGstUtils.cpp:862
ofPixels eventPixels
Definition ofGstUtils.h:213
bool allocate(int w, int h, ofPixelFormat pixelFormat)
Definition ofGstUtils.cpp:1231
GstFlowReturn preroll_cb(std::shared_ptr< GstBuffer > buffer)
Definition ofGstUtils.cpp:1412
ofTexture * getTexture()
Definition ofGstUtils.cpp:899
ofGstVideoUtils()
Definition ofGstUtils.cpp:846
GstFlowReturn process_buffer(std::shared_ptr< GstBuffer > buffer)
Definition ofGstUtils.cpp:1265
void update()
Update the object's state.
Definition ofGstUtils.cpp:911
void reallocateOnNextFrame()
Definition ofGstUtils.cpp:1250
ofEvent< ofEventArgs > eosEvent
Definition ofGstUtils.h:196
float getWidth() const
Definition ofGstUtils.cpp:968
void setCopyPixels(bool copy)
Definition ofGstUtils.cpp:1127
void eos_cb()
Definition ofGstUtils.cpp:1451
ofPixels backPixels
Definition ofGstUtils.h:212
void close()
Close the video source.
Definition ofGstUtils.cpp:866
ofEvent< ofPixels > bufferEvent
Definition ofGstUtils.h:195
bool setPipeline(std::string pipeline, ofPixelFormat pixelFormat=OF_PIXELS_RGB, bool isStream=false, int w=-1, int h=-1)
Definition ofGstUtils.cpp:1131
ofPixels & getPixels()
Get a reference to the underlying ofPixels.
Definition ofGstUtils.cpp:891
ofEvent< ofPixels > prerollEvent
Definition ofGstUtils.h:194
ofPixels pixels
Definition ofGstUtils.h:211
float getHeight() const
Definition ofGstUtils.cpp:964
bool setPixelFormat(ofPixelFormat pixelFormat)
Set the requested ofPixelFormat.
Definition ofGstUtils.cpp:1222
bool isInitialized() const
Determine if the video source is initialized.
Definition ofGstUtils.cpp:883
GstFlowReturn buffer_cb(std::shared_ptr< GstBuffer > buffer)
Definition ofGstUtils.cpp:1432
ofPixelFormat getPixelFormat() const
Definition ofGstUtils.cpp:1227
bool isFrameNew() const
Definition ofGstUtils.cpp:887
A wrapper class for an OpenGL texture.
Definition ofTexture.h:253
A threaded base class with a built in mutex for convenience.
Definition ofThread.h:70
void startThread()
Start the thread.
Definition ofThread.cpp:38
void waitForThread(bool callStopThread=true, long milliseconds=INFINITE_JOIN_TIMEOUT)
Wait for the thread to exit (aka "joining" the thread).
Definition ofThread.cpp:95
struct _GstElement GstElement
Definition ofGstUtils.h:25
struct _GstBuffer GstBuffer
Definition ofGstUtils.h:26
struct _GstMessage GstMessage
Definition ofGstUtils.h:27
ofPixelFormat
Used to represent the available pixel formats.
Definition ofPixels.h:68
@ OF_PIXELS_RGB
An RGB pixel with no alpha channel.
Definition ofPixels.h:83
ofLoopType
Used to represent the available video looping modes.
Definition ofVideoBaseTypes.h:11