Fetching contributors…
Cannot retrieve contributors at this time
272 lines (219 sloc) 6.86 KB
#pragma once
#include "ofConstants.h"
#include "ofPixels.h"
#include "ofEvents.h"
#include "ofThread.h"
#include "ofVideoBaseTypes.h"
#include <gst/gst.h>
#include <gst/gstpad.h>
#include <gst/video/video.h>
#include <queue>
#include <condition_variable>
#include <mutex>
//#define OF_USE_GST_GL
#ifdef OF_USE_GST_GL
#include <gst/gl/gl.h>
class ofGstAppSink;
class ofTexture;
typedef struct _GstElement GstElement;
typedef struct _GstBuffer GstBuffer;
typedef struct _GstMessage GstMessage;
//----------------------------------------- ofGstUtils
class ofGstUtils{
virtual ~ofGstUtils();
bool setPipelineWithSink(std::string pipeline, std::string sinkname="sink", bool isStream=false);
bool setPipelineWithSink(GstElement * pipeline, GstElement * sink, bool isStream=false);
bool startPipeline();
void play();
void stop();
void setPaused(bool bPause);
bool isPaused() const {return bPaused;}
bool isLoaded() const {return bLoaded;}
bool isPlaying() const {return bPlaying;}
float getPosition() const;
float getSpeed() const;
float getDuration() const;
int64_t getDurationNanos() const;
bool getIsMovieDone() const;
void setPosition(float pct);
void setVolume(float volume);
void setLoopState(ofLoopType state);
ofLoopType getLoopState() const {return loopMode;}
void setSpeed(float speed);
void setFrameByFrame(bool bFrameByFrame);
bool isFrameByFrame() const;
GstElement * getPipeline() const;
GstElement * getSink() const;
GstElement * getGstElementByName(const std::string & name) const;
uint64_t getMinLatencyNanos() const;
uint64_t getMaxLatencyNanos() const;
virtual void close();
void setSinkListener(ofGstAppSink * appsink);
// callbacks to get called from gstreamer
virtual GstFlowReturn preroll_cb(std::shared_ptr<GstBuffer> buffer);
virtual GstFlowReturn buffer_cb(std::shared_ptr<GstBuffer> buffer);
virtual GstFlowReturn preroll_cb(std::shared_ptr<GstSample> buffer);
virtual GstFlowReturn buffer_cb(std::shared_ptr<GstSample> buffer);
virtual void eos_cb();
static void startGstMainLoop();
static GMainLoop * getGstMainLoop();
static void quitGstMainLoop();
ofGstAppSink * appsink;
bool isStream;
bool closing;
static bool busFunction(GstBus * bus, GstMessage * message, ofGstUtils * app);
bool gstHandleMessage(GstBus * bus, GstMessage * message);
bool bPlaying;
bool bPaused;
bool bIsMovieDone;
bool bLoaded;
bool bFrameByFrame;
ofLoopType loopMode;
GstElement * gstSink;
GstElement * gstPipeline;
float speed;
mutable gint64 durationNanos;
bool isAppSink;
std::condition_variable eosCondition;
std::mutex eosMutex;
guint busWatchID;
class ofGstMainLoopThread: public ofThread{
virtual ~ofGstMainLoopThread(){};
void start(){
main_loop = g_main_loop_new (NULL, FALSE);
void threadedFunction(){
g_main_loop_run (main_loop);
GMainLoop * getMainLoop(){
return main_loop;
void quit(){
GMainLoop *main_loop = nullptr;
static ofGstMainLoopThread * mainLoop;
//----------------------------------------- videoUtils
class ofGstVideoUtils: public ofBaseVideo, public ofGstUtils{
virtual ~ofGstVideoUtils();
bool setPipeline(std::string pipeline, ofPixelFormat pixelFormat=OF_PIXELS_RGB, bool isStream=false, int w=-1, int h=-1);
bool setPixelFormat(ofPixelFormat pixelFormat);
ofPixelFormat getPixelFormat() const;
bool allocate(int w, int h, ofPixelFormat pixelFormat);
void reallocateOnNextFrame();
bool isFrameNew() const;
ofPixels& getPixels();
const ofPixels& getPixels() const;
ofTexture * getTexture();
void update();
float getHeight() const;
float getWidth() const;
void close();
static std::string getGstFormatName(ofPixelFormat format);
static GstVideoFormat getGstFormat(ofPixelFormat format);
static ofPixelFormat getOFFormat(GstVideoFormat format);
bool isInitialized() const;
// copy pixels from gst buffer to avoid
void setCopyPixels(bool copy);
// this events happen in a different thread
// do not use them for opengl stuff
ofEvent<ofPixels> prerollEvent;
ofEvent<ofPixels> bufferEvent;
ofEvent<ofEventArgs> eosEvent;
GstFlowReturn process_buffer(std::shared_ptr<GstBuffer> buffer);
GstFlowReturn preroll_cb(std::shared_ptr<GstBuffer> buffer);
GstFlowReturn buffer_cb(std::shared_ptr<GstBuffer> buffer);
GstFlowReturn process_sample(std::shared_ptr<GstSample> sample);
GstFlowReturn preroll_cb(std::shared_ptr<GstSample> buffer);
GstFlowReturn buffer_cb(std::shared_ptr<GstSample> buffer);
void eos_cb();
ofPixels pixels; // 24 bit: rgb
ofPixels backPixels;
ofPixels eventPixels;
static gboolean sync_bus_call (GstBus * bus, GstMessage * msg, gpointer data);
bool bIsFrameNew; // if we are new
bool bHavePixelsChanged;
bool bBackPixelsChanged;
std::mutex mutex;
std::shared_ptr<GstBuffer> frontBuffer, backBuffer;
std::shared_ptr<GstSample> frontBuffer, backBuffer;
std::queue<std::shared_ptr<GstSample> > bufferQueue;
GstMapInfo mapinfo;
#ifdef OF_USE_GST_GL
ofTexture frontTexture, backTexture;
ofPixelFormat internalPixelFormat;
bool copyPixels; // fix for certain versions bug with v4l2
#ifdef OF_USE_GST_GL
GstGLDisplay * glDisplay;
GstGLContext * glContext;
//----------------------------------------- appsink listener
class ofGstAppSink{
virtual ~ofGstAppSink(){}
virtual GstFlowReturn on_preroll(std::shared_ptr<GstBuffer> buffer){
return GST_FLOW_OK;
virtual GstFlowReturn on_buffer(std::shared_ptr<GstBuffer> buffer){
return GST_FLOW_OK;
virtual GstFlowReturn on_preroll(std::shared_ptr<GstSample> buffer){
return GST_FLOW_OK;
virtual GstFlowReturn on_buffer(std::shared_ptr<GstSample> buffer){
return GST_FLOW_OK;
virtual void on_eos(){}
// return true to set the message as attended so upstream doesn't try to process it
virtual bool on_message(GstMessage* msg){return false;};
// pings when enough data has arrived to be able to get sink properties
virtual void on_stream_prepared(){};