2010-05-12 01:44:00 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Intel License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
2011-04-24 05:24:41 +08:00
|
|
|
// Copyright (C) 2008, 2011, Nils Hasler, all rights reserved.
|
2010-05-12 01:44:00 +08:00
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of Intel Corporation may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \file cap_gstreamer.cpp
|
|
|
|
* \author Nils Hasler <hasler@mpi-inf.mpg.de>
|
|
|
|
* Max-Planck-Institut Informatik
|
|
|
|
* \author Dirk Van Haerenborgh <vhdirk@gmail.com>
|
|
|
|
*
|
|
|
|
* \brief Use GStreamer to read/write video
|
|
|
|
*/
|
2010-05-12 01:44:00 +08:00
|
|
|
#include "precomp.hpp"
|
2015-12-09 22:08:19 +08:00
|
|
|
#ifndef _MSC_VER
|
2010-05-12 01:44:00 +08:00
|
|
|
#include <unistd.h>
|
2015-12-09 22:08:19 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <gst/gst.h>
|
2013-03-21 17:17:34 +08:00
|
|
|
#include <gst/gstbuffer.h>
|
2010-05-12 01:44:00 +08:00
|
|
|
#include <gst/video/video.h>
|
|
|
|
#include <gst/app/gstappsink.h>
|
|
|
|
#include <gst/app/gstappsrc.h>
|
|
|
|
#include <gst/riff/riff-media.h>
|
2013-03-21 17:17:34 +08:00
|
|
|
#include <gst/pbutils/missing-plugins.h>
|
2013-03-26 16:27:39 +08:00
|
|
|
|
|
|
|
#define VERSION_NUM(major, minor, micro) (major * 1000000 + minor * 1000 + micro)
|
|
|
|
#define FULL_GST_VERSION VERSION_NUM(GST_VERSION_MAJOR, GST_VERSION_MINOR, GST_VERSION_MICRO)
|
|
|
|
|
|
|
|
#if FULL_GST_VERSION >= VERSION_NUM(0,10,32)
|
|
|
|
#include <gst/pbutils/encoding-profile.h>
|
2013-03-21 17:17:34 +08:00
|
|
|
//#include <gst/base/gsttypefindhelper.h>
|
2013-03-26 16:27:39 +08:00
|
|
|
#endif
|
2013-03-21 17:17:34 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#define CV_WARN(message)
|
|
|
|
#else
|
|
|
|
#define CV_WARN(message) fprintf(stderr, "warning: %s (%s:%d)\n", message, __FILE__, __LINE__)
|
|
|
|
#endif
|
|
|
|
|
2014-09-23 18:15:36 +08:00
|
|
|
#if GST_VERSION_MAJOR == 0
|
2013-03-21 17:17:34 +08:00
|
|
|
#define COLOR_ELEM "ffmpegcolorspace"
|
2015-08-17 02:01:22 +08:00
|
|
|
#define COLOR_ELEM_NAME "ffmpegcsp"
|
2014-09-23 18:15:36 +08:00
|
|
|
#else
|
2017-11-23 20:03:20 +08:00
|
|
|
#define COLOR_ELEM "videoconvert"
|
2015-08-17 02:01:22 +08:00
|
|
|
#define COLOR_ELEM_NAME COLOR_ELEM
|
2013-03-21 17:17:34 +08:00
|
|
|
#endif
|
|
|
|
|
2015-12-09 22:08:19 +08:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2017-08-20 05:51:02 +08:00
|
|
|
#if defined(__MINGW32__)
|
|
|
|
inline char *realpath(const char *path, char *resolved_path)
|
|
|
|
{
|
|
|
|
return _fullpath(resolved_path,path,PATH_MAX);
|
|
|
|
}
|
|
|
|
#endif
|
2015-12-09 22:08:19 +08:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#define vsnprintf _vsnprintf
|
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#define strncasecmp _strnicmp
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
void toFraction(double decimal, double &numerator, double &denominator);
|
|
|
|
void handleMessage(GstElement * pipeline);
|
|
|
|
|
|
|
|
|
2012-11-19 20:44:23 +08:00
|
|
|
static cv::Mutex gst_initializer_mutex;
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief The gst_initializer class
|
|
|
|
* Initializes gstreamer once in the whole process
|
2013-03-21 23:11:53 +08:00
|
|
|
*/
|
|
|
|
class gst_initializer
|
2012-11-19 20:44:23 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void init()
|
|
|
|
{
|
|
|
|
gst_initializer_mutex.lock();
|
|
|
|
static gst_initializer init;
|
|
|
|
gst_initializer_mutex.unlock();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
gst_initializer()
|
|
|
|
{
|
|
|
|
gst_init(NULL, NULL);
|
2013-03-31 17:26:21 +08:00
|
|
|
// gst_debug_set_active(1);
|
|
|
|
// gst_debug_set_colored(1);
|
2013-03-21 17:17:34 +08:00
|
|
|
// gst_debug_set_default_threshold(GST_LEVEL_INFO);
|
2012-11-19 20:44:23 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief The CvCapture_GStreamer class
|
|
|
|
* Use GStreamer to capture video
|
|
|
|
*/
|
2018-03-15 21:16:53 +08:00
|
|
|
class CvCapture_GStreamer CV_FINAL : public CvCapture
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CvCapture_GStreamer() { init(); }
|
|
|
|
virtual ~CvCapture_GStreamer() { close(); }
|
|
|
|
|
|
|
|
virtual bool open( int type, const char* filename );
|
|
|
|
virtual void close();
|
|
|
|
|
2018-03-15 21:16:53 +08:00
|
|
|
virtual double getProperty(int) const CV_OVERRIDE;
|
|
|
|
virtual bool setProperty(int, double) CV_OVERRIDE;
|
|
|
|
virtual bool grabFrame() CV_OVERRIDE;
|
|
|
|
virtual IplImage* retrieveFrame(int) CV_OVERRIDE;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
protected:
|
2010-05-12 01:44:00 +08:00
|
|
|
void init();
|
2011-04-13 15:15:35 +08:00
|
|
|
bool reopen();
|
2013-03-21 17:17:34 +08:00
|
|
|
bool isPipelinePlaying();
|
|
|
|
void startPipeline();
|
|
|
|
void stopPipeline();
|
2010-05-12 01:44:00 +08:00
|
|
|
void restartPipeline();
|
2013-10-03 16:11:50 +08:00
|
|
|
void setFilter(const char* prop, int type, int v1, int v2 = 0);
|
2010-05-12 01:44:00 +08:00
|
|
|
void removeFilter(const char *filter);
|
2013-03-21 17:17:34 +08:00
|
|
|
static void newPad(GstElement *myelement,
|
|
|
|
GstPad *pad,
|
|
|
|
gpointer data);
|
|
|
|
GstElement* pipeline;
|
|
|
|
GstElement* uridecodebin;
|
2015-08-17 02:25:36 +08:00
|
|
|
GstElement* v4l2src;
|
2013-03-21 17:17:34 +08:00
|
|
|
GstElement* color;
|
|
|
|
GstElement* sink;
|
|
|
|
#if GST_VERSION_MAJOR > 0
|
|
|
|
GstSample* sample;
|
|
|
|
#endif
|
|
|
|
GstBuffer* buffer;
|
|
|
|
GstCaps* caps;
|
|
|
|
IplImage* frame;
|
2014-09-10 14:38:43 +08:00
|
|
|
gint64 duration;
|
2014-09-25 18:40:54 +08:00
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
double fps;
|
2017-07-05 18:45:08 +08:00
|
|
|
|
|
|
|
bool isPosFramesSupported;
|
|
|
|
bool isPosFramesEmulated;
|
|
|
|
gint64 emulatedFrameNumber;
|
2017-11-02 23:27:30 +08:00
|
|
|
|
|
|
|
bool isOutputByteBuffer;
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
2011-04-23 15:27:04 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::init
|
|
|
|
* inits the class
|
|
|
|
*/
|
2010-05-12 01:44:00 +08:00
|
|
|
void CvCapture_GStreamer::init()
|
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
pipeline = NULL;
|
2014-07-21 16:12:25 +08:00
|
|
|
uridecodebin = NULL;
|
2015-08-17 02:25:36 +08:00
|
|
|
v4l2src = NULL;
|
2014-07-21 16:12:25 +08:00
|
|
|
color = NULL;
|
|
|
|
sink = NULL;
|
2013-03-21 17:17:34 +08:00
|
|
|
#if GST_VERSION_MAJOR > 0
|
|
|
|
sample = NULL;
|
|
|
|
#endif
|
2014-07-21 16:12:25 +08:00
|
|
|
buffer = NULL;
|
|
|
|
caps = NULL;
|
|
|
|
frame = NULL;
|
2014-09-10 14:38:43 +08:00
|
|
|
duration = -1;
|
2014-09-25 18:40:54 +08:00
|
|
|
width = -1;
|
|
|
|
height = -1;
|
|
|
|
fps = -1;
|
2017-07-05 18:45:08 +08:00
|
|
|
|
|
|
|
isPosFramesSupported = false;
|
|
|
|
isPosFramesEmulated = false;
|
|
|
|
emulatedFrameNumber = -1;
|
2017-11-02 23:27:30 +08:00
|
|
|
|
|
|
|
isOutputByteBuffer = false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::close
|
|
|
|
* Closes the pipeline and destroys all instances
|
|
|
|
*/
|
|
|
|
void CvCapture_GStreamer::close()
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
if (isPipelinePlaying())
|
|
|
|
this->stopPipeline();
|
2011-04-13 15:15:35 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
if(pipeline) {
|
|
|
|
gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_NULL);
|
|
|
|
gst_object_unref(GST_OBJECT(pipeline));
|
2014-07-24 01:15:16 +08:00
|
|
|
pipeline = NULL;
|
2013-03-21 17:17:34 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2014-09-10 14:38:43 +08:00
|
|
|
duration = -1;
|
2014-09-25 18:40:54 +08:00
|
|
|
width = -1;
|
|
|
|
height = -1;
|
|
|
|
fps = -1;
|
2017-07-05 18:45:08 +08:00
|
|
|
isPosFramesSupported = false;
|
|
|
|
isPosFramesEmulated = false;
|
|
|
|
emulatedFrameNumber = -1;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::grabFrame
|
|
|
|
* \return
|
|
|
|
* Grabs a sample from the pipeline, awaiting consumation by retreiveFrame.
|
|
|
|
* The pipeline is started if it was not running yet
|
|
|
|
*/
|
2010-05-12 01:44:00 +08:00
|
|
|
bool CvCapture_GStreamer::grabFrame()
|
|
|
|
{
|
2011-04-13 15:15:35 +08:00
|
|
|
if(!pipeline)
|
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
// start the pipeline if it was not in playing state yet
|
|
|
|
if(!this->isPipelinePlaying())
|
|
|
|
this->startPipeline();
|
|
|
|
|
|
|
|
// bail out if EOS
|
2011-04-24 05:24:41 +08:00
|
|
|
if(gst_app_sink_is_eos(GST_APP_SINK(sink)))
|
2011-04-13 15:15:35 +08:00
|
|
|
return false;
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
#if GST_VERSION_MAJOR == 0
|
2011-04-13 15:15:35 +08:00
|
|
|
if(buffer)
|
|
|
|
gst_buffer_unref(buffer);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-04-24 05:24:41 +08:00
|
|
|
buffer = gst_app_sink_pull_buffer(GST_APP_SINK(sink));
|
2013-03-21 17:17:34 +08:00
|
|
|
#else
|
|
|
|
if(sample)
|
|
|
|
gst_sample_unref(sample);
|
|
|
|
|
|
|
|
sample = gst_app_sink_pull_sample(GST_APP_SINK(sink));
|
|
|
|
|
|
|
|
if(!sample)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
buffer = gst_sample_get_buffer(sample);
|
|
|
|
#endif
|
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
if(!buffer)
|
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-07-05 18:45:08 +08:00
|
|
|
if (isPosFramesEmulated)
|
|
|
|
emulatedFrameNumber++;
|
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
return true;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::retrieveFrame
|
|
|
|
* \return IplImage pointer. [Transfer Full]
|
2018-02-14 00:28:11 +08:00
|
|
|
* Retrieve the previously grabbed buffer, and wrap it in an IPLImage structure
|
2013-03-21 17:17:34 +08:00
|
|
|
*/
|
2010-05-12 01:44:00 +08:00
|
|
|
IplImage * CvCapture_GStreamer::retrieveFrame(int)
|
|
|
|
{
|
2011-04-13 15:15:35 +08:00
|
|
|
if(!buffer)
|
2012-09-05 19:51:51 +08:00
|
|
|
return 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
//construct a frame header if we did not have any yet
|
|
|
|
if(!frame)
|
|
|
|
{
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
2014-09-25 18:40:54 +08:00
|
|
|
GstCaps* buffer_caps = gst_buffer_get_caps(buffer);
|
2013-03-21 17:17:34 +08:00
|
|
|
#else
|
2014-09-25 18:40:54 +08:00
|
|
|
GstCaps* buffer_caps = gst_sample_get_caps(sample);
|
2013-03-21 17:17:34 +08:00
|
|
|
#endif
|
|
|
|
// bail out in no caps
|
|
|
|
assert(gst_caps_get_size(buffer_caps) == 1);
|
|
|
|
GstStructure* structure = gst_caps_get_structure(buffer_caps, 0);
|
|
|
|
|
|
|
|
// bail out if width or height are 0
|
2011-04-13 15:15:35 +08:00
|
|
|
if(!gst_structure_get_int(structure, "width", &width) ||
|
2013-03-21 17:17:34 +08:00
|
|
|
!gst_structure_get_int(structure, "height", &height))
|
|
|
|
{
|
2014-09-25 18:40:54 +08:00
|
|
|
gst_caps_unref(buffer_caps);
|
2013-03-21 17:17:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int depth = 3;
|
2017-07-15 01:11:30 +08:00
|
|
|
bool height_extend = false;
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
#if GST_VERSION_MAJOR > 0
|
|
|
|
depth = 0;
|
|
|
|
const gchar* name = gst_structure_get_name(structure);
|
|
|
|
const gchar* format = gst_structure_get_string(structure, "format");
|
|
|
|
|
2016-03-03 00:22:49 +08:00
|
|
|
if (!name)
|
2012-09-05 19:51:51 +08:00
|
|
|
return 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-07-15 01:11:30 +08:00
|
|
|
// we support 11 types of data:
|
2013-03-21 17:17:34 +08:00
|
|
|
// video/x-raw, format=BGR -> 8bit, 3 channels
|
|
|
|
// video/x-raw, format=GRAY8 -> 8bit, 1 channel
|
2017-07-15 01:11:30 +08:00
|
|
|
// video/x-raw, format=UYVY -> 8bit, 2 channel
|
|
|
|
// video/x-raw, format=YUY2 -> 8bit, 2 channel
|
|
|
|
// video/x-raw, format=YVYU -> 8bit, 2 channel
|
|
|
|
// video/x-raw, format=NV12 -> 8bit, 1 channel (height is 1.5x larger than true height)
|
|
|
|
// video/x-raw, format=NV21 -> 8bit, 1 channel (height is 1.5x larger than true height)
|
|
|
|
// video/x-raw, format=YV12 -> 8bit, 1 channel (height is 1.5x larger than true height)
|
|
|
|
// video/x-raw, format=I420 -> 8bit, 1 channel (height is 1.5x larger than true height)
|
2013-03-21 17:17:34 +08:00
|
|
|
// video/x-bayer -> 8bit, 1 channel
|
2016-03-03 00:22:49 +08:00
|
|
|
// image/jpeg -> 8bit, mjpeg: buffer_size x 1 x 1
|
2013-03-21 17:17:34 +08:00
|
|
|
// bayer data is never decoded, the user is responsible for that
|
|
|
|
// everything is 8 bit, so we just test the caps for bit depth
|
|
|
|
|
|
|
|
if (strcasecmp(name, "video/x-raw") == 0)
|
|
|
|
{
|
2016-03-03 00:22:49 +08:00
|
|
|
if (!format)
|
|
|
|
return 0;
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
if (strcasecmp(format, "BGR") == 0) {
|
|
|
|
depth = 3;
|
|
|
|
}
|
2017-07-15 01:11:30 +08:00
|
|
|
else if( (strcasecmp(format, "UYVY") == 0) || (strcasecmp(format, "YUY2") == 0) || (strcasecmp(format, "YVYU") == 0) ){
|
|
|
|
depth = 2;
|
|
|
|
}
|
|
|
|
else if( (strcasecmp(format, "NV12") == 0) || (strcasecmp(format, "NV21") == 0) || (strcasecmp(format, "YV12") == 0) || (strcasecmp(format, "I420") == 0) ){
|
|
|
|
depth = 1;
|
|
|
|
height_extend = true;
|
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
else if(strcasecmp(format, "GRAY8") == 0){
|
|
|
|
depth = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcasecmp(name, "video/x-bayer") == 0)
|
|
|
|
{
|
|
|
|
depth = 1;
|
2016-03-03 00:22:49 +08:00
|
|
|
} else if(strcasecmp(name, "image/jpeg") == 0) {
|
|
|
|
depth = 1;
|
|
|
|
// the correct size will be set once the first frame arrives
|
2017-11-02 23:27:30 +08:00
|
|
|
isOutputByteBuffer = true;
|
2013-03-21 17:17:34 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (depth > 0) {
|
2017-07-15 01:11:30 +08:00
|
|
|
if(height_extend){
|
|
|
|
frame = cvCreateImageHeader(cvSize(width, height*3/2), IPL_DEPTH_8U, depth);
|
|
|
|
}else{
|
|
|
|
frame = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, depth);
|
|
|
|
}
|
2014-09-25 18:40:54 +08:00
|
|
|
} else {
|
|
|
|
gst_caps_unref(buffer_caps);
|
2013-03-21 17:17:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-09-25 18:40:54 +08:00
|
|
|
|
|
|
|
gst_caps_unref(buffer_caps);
|
2011-04-13 15:15:35 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
// gstreamer expects us to handle the memory at this point
|
|
|
|
// so we can just wrap the raw buffer and be done with it
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
2011-04-23 15:27:04 +08:00
|
|
|
frame->imageData = (char *)GST_BUFFER_DATA(buffer);
|
2013-03-21 17:17:34 +08:00
|
|
|
#else
|
2015-10-05 18:28:47 +08:00
|
|
|
// info.data ptr is valid until next grabFrame where the associated sample is unref'd
|
|
|
|
GstMapInfo info = GstMapInfo();
|
|
|
|
gboolean success = gst_buffer_map(buffer,&info, (GstMapFlags)GST_MAP_READ);
|
2016-03-03 00:22:49 +08:00
|
|
|
|
|
|
|
// with MJPEG streams frame size can change arbitrarily
|
2017-11-02 23:27:30 +08:00
|
|
|
if (isOutputByteBuffer && (size_t)info.size != (size_t)frame->imageSize)
|
|
|
|
{
|
2016-03-03 00:22:49 +08:00
|
|
|
cvReleaseImageHeader(&frame);
|
|
|
|
frame = cvCreateImageHeader(cvSize(info.size, 1), IPL_DEPTH_8U, 1);
|
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
if (!success){
|
|
|
|
//something weird went wrong here. abort. abort.
|
|
|
|
//fprintf(stderr,"GStreamer: unable to map buffer");
|
|
|
|
return 0;
|
|
|
|
}
|
2015-10-05 18:28:47 +08:00
|
|
|
frame->imageData = (char*)info.data;
|
|
|
|
gst_buffer_unmap(buffer,&info);
|
2013-03-21 17:17:34 +08:00
|
|
|
#endif
|
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
return frame;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::isPipelinePlaying
|
|
|
|
* \return if the pipeline is currently playing.
|
|
|
|
*/
|
|
|
|
bool CvCapture_GStreamer::isPipelinePlaying()
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
GstState current, pending;
|
|
|
|
GstClockTime timeout = 5*GST_SECOND;
|
|
|
|
if(!GST_IS_ELEMENT(pipeline)){
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
GstStateChangeReturn ret = gst_element_get_state(GST_ELEMENT(pipeline),¤t, &pending, timeout);
|
|
|
|
if (!ret){
|
|
|
|
//fprintf(stderr, "GStreamer: unable to query pipeline state\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current == GST_STATE_PLAYING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::startPipeline
|
|
|
|
* Start the pipeline by setting it to the playing state
|
|
|
|
*/
|
|
|
|
void CvCapture_GStreamer::startPipeline()
|
|
|
|
{
|
|
|
|
CV_FUNCNAME("icvStartPipeline");
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
__BEGIN__;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
//fprintf(stderr, "relinked, pausing\n");
|
2014-09-10 14:38:43 +08:00
|
|
|
GstStateChangeReturn status = gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
|
|
|
|
if (status == GST_STATE_CHANGE_ASYNC)
|
|
|
|
{
|
|
|
|
// wait for status update
|
2015-08-15 23:30:08 +08:00
|
|
|
status = gst_element_get_state(pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
|
2014-09-10 14:38:43 +08:00
|
|
|
}
|
|
|
|
if (status == GST_STATE_CHANGE_FAILURE)
|
|
|
|
{
|
|
|
|
handleMessage(pipeline);
|
2013-03-21 17:17:34 +08:00
|
|
|
gst_object_unref(pipeline);
|
2014-09-10 14:38:43 +08:00
|
|
|
pipeline = NULL;
|
|
|
|
CV_ERROR(CV_StsError, "GStreamer: unable to start pipeline\n");
|
2011-04-13 15:15:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2017-07-05 18:45:08 +08:00
|
|
|
if (isPosFramesEmulated)
|
|
|
|
emulatedFrameNumber = 0;
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
//printf("state now playing\n");
|
|
|
|
handleMessage(pipeline);
|
|
|
|
__END__;
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::stopPipeline
|
|
|
|
* Stop the pipeline by setting it to NULL
|
|
|
|
*/
|
|
|
|
void CvCapture_GStreamer::stopPipeline()
|
|
|
|
{
|
|
|
|
CV_FUNCNAME("icvStopPipeline");
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
__BEGIN__;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
//fprintf(stderr, "restarting pipeline, going to ready\n");
|
|
|
|
if(gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_NULL) ==
|
|
|
|
GST_STATE_CHANGE_FAILURE) {
|
|
|
|
CV_ERROR(CV_StsError, "GStreamer: unable to stop pipeline\n");
|
|
|
|
gst_object_unref(pipeline);
|
2014-09-10 14:38:43 +08:00
|
|
|
pipeline = NULL;
|
2011-04-13 15:15:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
__END__;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::restartPipeline
|
|
|
|
* Restart the pipeline
|
|
|
|
*/
|
|
|
|
void CvCapture_GStreamer::restartPipeline()
|
|
|
|
{
|
|
|
|
handleMessage(pipeline);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
this->stopPipeline();
|
|
|
|
this->startPipeline();
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::setFilter
|
|
|
|
* \param prop the property name
|
|
|
|
* \param type glib property type
|
|
|
|
* \param v1 the value
|
|
|
|
* \param v2 second value of property type requires it, else NULL
|
|
|
|
* Filter the output formats by setting appsink caps properties
|
|
|
|
*/
|
|
|
|
void CvCapture_GStreamer::setFilter(const char *prop, int type, int v1, int v2)
|
|
|
|
{
|
|
|
|
//printf("GStreamer: setFilter \n");
|
|
|
|
if(!caps || !( GST_IS_CAPS (caps) ))
|
|
|
|
{
|
2011-04-13 15:15:35 +08:00
|
|
|
if(type == G_TYPE_INT)
|
2013-03-21 17:17:34 +08:00
|
|
|
{
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
|
|
|
caps = gst_caps_new_simple("video/x-raw-rgb", prop, type, v1, NULL);
|
|
|
|
#else
|
|
|
|
caps = gst_caps_new_simple("video/x-raw","format",G_TYPE_STRING,"BGR", prop, type, v1, NULL);
|
|
|
|
#endif
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
else
|
2013-03-21 17:17:34 +08:00
|
|
|
{
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
|
|
|
caps = gst_caps_new_simple("video/x-raw-rgb", prop, type, v1, v2, NULL);
|
|
|
|
#else
|
|
|
|
caps = gst_caps_new_simple("video/x-raw","format",G_TYPE_STRING,"BGR", prop, type, v1, v2, NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if GST_VERSION_MAJOR > 0
|
|
|
|
if (! gst_caps_is_writable(caps))
|
|
|
|
caps = gst_caps_make_writable (caps);
|
|
|
|
#endif
|
|
|
|
if(type == G_TYPE_INT){
|
|
|
|
gst_caps_set_simple(caps, prop, type, v1, NULL);
|
|
|
|
}else{
|
|
|
|
gst_caps_set_simple(caps, prop, type, v1, v2, NULL);
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
#if GST_VERSION_MAJOR > 0
|
|
|
|
caps = gst_caps_fixate(caps);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
gst_app_sink_set_caps(GST_APP_SINK(sink), caps);
|
|
|
|
//printf("filtering with %s\n", gst_caps_to_string(caps));
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::removeFilter
|
|
|
|
* \param filter filter to remove
|
|
|
|
* remove the specified filter from the appsink template caps
|
|
|
|
*/
|
2010-05-12 01:44:00 +08:00
|
|
|
void CvCapture_GStreamer::removeFilter(const char *filter)
|
|
|
|
{
|
2011-04-13 15:15:35 +08:00
|
|
|
if(!caps)
|
|
|
|
return;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
#if GST_VERSION_MAJOR > 0
|
|
|
|
if (! gst_caps_is_writable(caps))
|
|
|
|
caps = gst_caps_make_writable (caps);
|
|
|
|
#endif
|
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
GstStructure *s = gst_caps_get_structure(caps, 0);
|
|
|
|
gst_structure_remove_field(s, filter);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
gst_app_sink_set_caps(GST_APP_SINK(sink), caps);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::newPad link dynamic padd
|
|
|
|
* \param pad
|
|
|
|
* \param data
|
|
|
|
* decodebin creates pads based on stream information, which is not known upfront
|
|
|
|
* on receiving the pad-added signal, we connect it to the colorspace conversion element
|
|
|
|
*/
|
|
|
|
void CvCapture_GStreamer::newPad(GstElement * /*elem*/,
|
|
|
|
GstPad *pad,
|
|
|
|
gpointer data)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-05-07 03:06:33 +08:00
|
|
|
GstPad *sinkpad;
|
|
|
|
GstElement *color = (GstElement *) data;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-05-07 03:06:33 +08:00
|
|
|
sinkpad = gst_element_get_static_pad (color, "sink");
|
2013-03-21 17:17:34 +08:00
|
|
|
if (!sinkpad){
|
|
|
|
//fprintf(stderr, "Gstreamer: no pad named sink\n");
|
|
|
|
return;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-05-07 03:06:33 +08:00
|
|
|
gst_pad_link (pad, sinkpad);
|
|
|
|
gst_object_unref (sinkpad);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::open Open the given file with gstreamer
|
|
|
|
* \param type CvCapture type. One of CV_CAP_GSTREAMER_*
|
|
|
|
* \param filename Filename to open in case of CV_CAP_GSTREAMER_FILE
|
2015-05-23 21:26:18 +08:00
|
|
|
* \return boolean. Specifies if opening was successful.
|
2013-03-21 17:17:34 +08:00
|
|
|
*
|
|
|
|
* In case of CV_CAP_GSTREAMER_V4L(2), a pipelin is constructed as follows:
|
|
|
|
* v4l2src ! autoconvert ! appsink
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* The 'filename' parameter is not limited to filesystem paths, and may be one of the following:
|
|
|
|
*
|
|
|
|
* - a normal filesystem path:
|
|
|
|
* e.g. video.avi or /path/to/video.avi or C:\\video.avi
|
|
|
|
* - an uri:
|
|
|
|
* e.g. file:///path/to/video.avi or rtsp:///path/to/stream.asf
|
|
|
|
* - a gstreamer pipeline description:
|
|
|
|
* e.g. videotestsrc ! videoconvert ! appsink
|
|
|
|
* the appsink name should be either 'appsink0' (the default) or 'opencvsink'
|
|
|
|
*
|
|
|
|
* When dealing with a file, CvCapture_GStreamer will not drop frames if the grabbing interval
|
|
|
|
* larger than the framerate period. (Unlike the uri or manual pipeline description, which assume
|
|
|
|
* a live source)
|
|
|
|
*
|
|
|
|
* The pipeline will only be started whenever the first frame is grabbed. Setting pipeline properties
|
|
|
|
* is really slow if we need to restart the pipeline over and over again.
|
|
|
|
*
|
|
|
|
* TODO: the 'type' parameter is imo unneeded. for v4l2, filename 'v4l2:///dev/video0' can be used.
|
|
|
|
* I expect this to be the same for CV_CAP_GSTREAMER_1394. Is anyone actually still using v4l (v1)?
|
|
|
|
*
|
|
|
|
*/
|
2010-05-12 01:44:00 +08:00
|
|
|
bool CvCapture_GStreamer::open( int type, const char* filename )
|
|
|
|
{
|
|
|
|
CV_FUNCNAME("cvCaptureFromCAM_GStreamer");
|
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
__BEGIN__;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2012-11-19 20:44:23 +08:00
|
|
|
gst_initializer::init();
|
|
|
|
|
2014-09-16 21:05:36 +08:00
|
|
|
bool file = false;
|
2011-04-13 15:15:35 +08:00
|
|
|
bool stream = false;
|
|
|
|
bool manualpipeline = false;
|
|
|
|
char *uri = NULL;
|
|
|
|
uridecodebin = NULL;
|
2013-03-21 17:17:34 +08:00
|
|
|
GstElementFactory * testfac;
|
2014-09-10 14:38:43 +08:00
|
|
|
GstStateChangeReturn status;
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
if (type == CV_CAP_GSTREAMER_V4L){
|
|
|
|
testfac = gst_element_factory_find("v4lsrc");
|
|
|
|
if (!testfac){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
g_object_unref(G_OBJECT(testfac));
|
2015-03-08 07:19:25 +08:00
|
|
|
filename = "v4lsrc ! " COLOR_ELEM " ! appsink";
|
2013-03-21 17:17:34 +08:00
|
|
|
}
|
|
|
|
if (type == CV_CAP_GSTREAMER_V4L2){
|
|
|
|
testfac = gst_element_factory_find("v4l2src");
|
|
|
|
if (!testfac){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
g_object_unref(G_OBJECT(testfac));
|
2015-03-08 07:19:25 +08:00
|
|
|
filename = "v4l2src ! " COLOR_ELEM " ! appsink";
|
2011-04-13 15:15:35 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
// test if we have a valid uri. If so, open it with an uridecodebin
|
|
|
|
// else, we might have a file or a manual pipeline.
|
|
|
|
// if gstreamer cannot parse the manual pipeline, we assume we were given and
|
|
|
|
// ordinary file path.
|
2015-12-09 22:08:19 +08:00
|
|
|
if (!gst_uri_is_valid(filename))
|
2013-03-21 17:17:34 +08:00
|
|
|
{
|
2015-12-09 22:08:19 +08:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
uri = new char[2048];
|
|
|
|
DWORD pathSize = GetFullPathName(filename, 2048, uri, NULL);
|
|
|
|
struct stat buf;
|
|
|
|
if (pathSize == 0 || stat(uri, &buf) != 0)
|
|
|
|
{
|
2016-03-02 04:40:05 +08:00
|
|
|
delete[] uri;
|
2015-12-09 22:08:19 +08:00
|
|
|
uri = NULL;
|
|
|
|
}
|
|
|
|
#else
|
2011-04-13 15:15:35 +08:00
|
|
|
uri = realpath(filename, NULL);
|
2015-12-09 22:08:19 +08:00
|
|
|
#endif
|
2013-03-21 17:17:34 +08:00
|
|
|
stream = false;
|
|
|
|
if(uri)
|
|
|
|
{
|
2011-04-13 15:15:35 +08:00
|
|
|
uri = g_filename_to_uri(uri, NULL, NULL);
|
2014-09-16 21:05:36 +08:00
|
|
|
if(uri)
|
|
|
|
{
|
|
|
|
file = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-13 15:15:35 +08:00
|
|
|
CV_WARN("GStreamer: Error opening file\n");
|
2015-07-22 17:11:58 +08:00
|
|
|
CV_WARN(filename);
|
|
|
|
CV_WARN(uri);
|
2011-04-13 15:15:35 +08:00
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-13 15:15:35 +08:00
|
|
|
GError *err = NULL;
|
2011-04-24 17:48:19 +08:00
|
|
|
uridecodebin = gst_parse_launch(filename, &err);
|
2014-09-16 21:05:36 +08:00
|
|
|
if(!uridecodebin)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "GStreamer: Error opening bin: %s\n", err->message);
|
2011-04-13 15:15:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
stream = true;
|
|
|
|
manualpipeline = true;
|
|
|
|
}
|
2015-08-15 23:30:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-13 15:15:35 +08:00
|
|
|
stream = true;
|
|
|
|
uri = g_strdup(filename);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
bool element_from_uri = false;
|
|
|
|
if(!uridecodebin)
|
|
|
|
{
|
|
|
|
// At this writing, the v4l2 element (and maybe others too) does not support caps renegotiation.
|
|
|
|
// This means that we cannot use an uridecodebin when dealing with v4l2, since setting
|
|
|
|
// capture properties will not work.
|
|
|
|
// The solution (probably only until gstreamer 1.2) is to make an element from uri when dealing with v4l2.
|
|
|
|
gchar * protocol = gst_uri_get_protocol(uri);
|
|
|
|
if (!strcasecmp(protocol , "v4l2"))
|
|
|
|
{
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
|
|
|
uridecodebin = gst_element_make_from_uri(GST_URI_SRC, uri, "src");
|
|
|
|
#else
|
|
|
|
uridecodebin = gst_element_make_from_uri(GST_URI_SRC, uri, "src", NULL);
|
|
|
|
#endif
|
|
|
|
element_from_uri = true;
|
2015-08-15 23:30:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-10 14:38:43 +08:00
|
|
|
uridecodebin = gst_element_factory_make("uridecodebin", NULL);
|
2014-09-16 21:05:36 +08:00
|
|
|
g_object_set(G_OBJECT(uridecodebin), "uri", uri, NULL);
|
2013-03-21 17:17:34 +08:00
|
|
|
}
|
|
|
|
g_free(protocol);
|
|
|
|
|
2015-08-15 23:30:08 +08:00
|
|
|
if(!uridecodebin)
|
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
//fprintf(stderr, "GStreamer: Error opening bin: %s\n", err->message);
|
2011-05-07 03:06:33 +08:00
|
|
|
close();
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
}
|
|
|
|
|
2015-08-15 23:30:08 +08:00
|
|
|
if (manualpipeline)
|
2013-03-21 17:17:34 +08:00
|
|
|
{
|
2015-08-17 02:01:22 +08:00
|
|
|
GstIterator *it = gst_bin_iterate_elements(GST_BIN(uridecodebin));
|
2013-06-13 17:16:33 +08:00
|
|
|
|
|
|
|
GstElement *element = NULL;
|
2015-08-17 02:01:22 +08:00
|
|
|
gboolean done = false;
|
2013-06-13 17:16:33 +08:00
|
|
|
gchar* name = NULL;
|
2015-08-17 02:01:22 +08:00
|
|
|
#if GST_VERSION_MAJOR > 0
|
2013-06-13 17:16:33 +08:00
|
|
|
GValue value = G_VALUE_INIT;
|
2015-08-17 02:01:22 +08:00
|
|
|
#endif
|
2013-06-13 17:16:33 +08:00
|
|
|
|
2015-08-15 23:30:08 +08:00
|
|
|
while (!done)
|
|
|
|
{
|
2015-08-17 02:01:22 +08:00
|
|
|
#if GST_VERSION_MAJOR > 0
|
2015-08-15 23:30:08 +08:00
|
|
|
switch (gst_iterator_next (it, &value))
|
|
|
|
{
|
2014-09-23 23:02:37 +08:00
|
|
|
case GST_ITERATOR_OK:
|
2015-08-15 23:30:08 +08:00
|
|
|
element = GST_ELEMENT (g_value_get_object (&value));
|
2015-08-17 02:01:22 +08:00
|
|
|
#else
|
|
|
|
switch (gst_iterator_next (it, (gpointer *)&element))
|
|
|
|
{
|
2013-06-13 17:16:33 +08:00
|
|
|
case GST_ITERATOR_OK:
|
2015-08-17 02:01:22 +08:00
|
|
|
#endif
|
2015-08-15 23:30:08 +08:00
|
|
|
name = gst_element_get_name(element);
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
if (strstr(name, "opencvsink") != NULL || strstr(name, "appsink") != NULL)
|
|
|
|
{
|
|
|
|
sink = GST_ELEMENT ( gst_object_ref (element) );
|
|
|
|
}
|
2015-08-17 02:01:22 +08:00
|
|
|
else if (strstr(name, COLOR_ELEM_NAME) != NULL)
|
2015-08-15 23:30:08 +08:00
|
|
|
{
|
|
|
|
color = GST_ELEMENT ( gst_object_ref (element) );
|
2015-08-17 02:25:36 +08:00
|
|
|
}
|
|
|
|
else if (strstr(name, "v4l") != NULL)
|
|
|
|
{
|
|
|
|
v4l2src = GST_ELEMENT ( gst_object_ref (element) );
|
2015-08-15 23:30:08 +08:00
|
|
|
}
|
|
|
|
g_free(name);
|
2015-08-17 02:25:36 +08:00
|
|
|
|
|
|
|
done = sink && color && v4l2src;
|
2013-06-13 17:16:33 +08:00
|
|
|
}
|
2015-08-17 02:01:22 +08:00
|
|
|
#if GST_VERSION_MAJOR > 0
|
2015-08-15 23:30:08 +08:00
|
|
|
g_value_unset (&value);
|
2015-08-17 02:01:22 +08:00
|
|
|
#endif
|
2013-06-13 17:16:33 +08:00
|
|
|
|
2015-08-15 23:30:08 +08:00
|
|
|
break;
|
2013-06-13 17:16:33 +08:00
|
|
|
case GST_ITERATOR_RESYNC:
|
2015-08-15 23:30:08 +08:00
|
|
|
gst_iterator_resync (it);
|
|
|
|
break;
|
2013-06-13 17:16:33 +08:00
|
|
|
case GST_ITERATOR_ERROR:
|
|
|
|
case GST_ITERATOR_DONE:
|
2015-08-15 23:30:08 +08:00
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2011-04-24 17:48:19 +08:00
|
|
|
}
|
2013-06-13 17:16:33 +08:00
|
|
|
gst_iterator_free (it);
|
|
|
|
|
2015-08-15 23:30:08 +08:00
|
|
|
if (!sink)
|
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
CV_ERROR(CV_StsError, "GStreamer: cannot find appsink in manual pipeline\n");
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-17 02:01:22 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
pipeline = uridecodebin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-10 14:38:43 +08:00
|
|
|
pipeline = gst_pipeline_new(NULL);
|
|
|
|
// videoconvert (in 0.10: ffmpegcolorspace, in 1.x autovideoconvert)
|
|
|
|
//automatically selects the correct colorspace conversion based on caps.
|
2013-03-21 17:17:34 +08:00
|
|
|
color = gst_element_factory_make(COLOR_ELEM, NULL);
|
2011-04-24 17:48:19 +08:00
|
|
|
sink = gst_element_factory_make("appsink", NULL);
|
|
|
|
|
|
|
|
gst_bin_add_many(GST_BIN(pipeline), uridecodebin, color, sink, NULL);
|
2013-03-21 17:17:34 +08:00
|
|
|
|
2015-08-15 23:30:08 +08:00
|
|
|
if(element_from_uri)
|
|
|
|
{
|
|
|
|
if(!gst_element_link(uridecodebin, color))
|
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
CV_ERROR(CV_StsError, "GStreamer: cannot link color -> sink\n");
|
|
|
|
gst_object_unref(pipeline);
|
2014-09-10 14:38:43 +08:00
|
|
|
pipeline = NULL;
|
2013-03-21 17:17:34 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-15 23:30:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
g_signal_connect(uridecodebin, "pad-added", G_CALLBACK(newPad), color);
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2015-08-15 23:30:08 +08:00
|
|
|
if(!gst_element_link(color, sink))
|
|
|
|
{
|
2011-04-24 17:48:19 +08:00
|
|
|
CV_ERROR(CV_StsError, "GStreamer: cannot link color -> sink\n");
|
|
|
|
gst_object_unref(pipeline);
|
2014-09-10 14:38:43 +08:00
|
|
|
pipeline = NULL;
|
2011-04-24 17:48:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
//TODO: is 1 single buffer really high enough?
|
2011-04-24 17:48:19 +08:00
|
|
|
gst_app_sink_set_max_buffers (GST_APP_SINK(sink), 1);
|
|
|
|
gst_app_sink_set_drop (GST_APP_SINK(sink), stream);
|
2013-03-21 17:17:34 +08:00
|
|
|
//do not emit signals: all calls will be synchronous and blocking
|
2013-03-31 17:26:21 +08:00
|
|
|
gst_app_sink_set_emit_signals (GST_APP_SINK(sink), 0);
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
2012-12-15 19:11:13 +08:00
|
|
|
caps = gst_caps_new_simple("video/x-raw-rgb",
|
2014-05-12 07:00:14 +08:00
|
|
|
"bpp", G_TYPE_INT, 24,
|
2012-12-15 19:11:13 +08:00
|
|
|
"red_mask", G_TYPE_INT, 0x0000FF,
|
|
|
|
"green_mask", G_TYPE_INT, 0x00FF00,
|
|
|
|
"blue_mask", G_TYPE_INT, 0xFF0000,
|
|
|
|
NULL);
|
2013-03-21 17:17:34 +08:00
|
|
|
#else
|
2017-07-15 01:11:30 +08:00
|
|
|
|
2016-03-03 00:22:49 +08:00
|
|
|
caps = gst_caps_from_string("video/x-raw, format=(string){BGR, GRAY8}; video/x-bayer,format=(string){rggb,bggr,grbg,gbrg}; image/jpeg");
|
2017-07-15 01:11:30 +08:00
|
|
|
|
|
|
|
if(manualpipeline){
|
|
|
|
GstPad* sink_pad = gst_element_get_static_pad(sink, "sink");
|
|
|
|
GstCaps* peer_caps = gst_pad_peer_query_caps(sink_pad,NULL);
|
|
|
|
if (!gst_caps_can_intersect(caps, peer_caps)) {
|
|
|
|
gst_caps_unref(caps);
|
|
|
|
caps = gst_caps_from_string("video/x-raw, format=(string){UYVY,YUY2,YVYU,NV12,NV21,YV12,I420}");
|
|
|
|
}
|
|
|
|
gst_object_unref(sink_pad);
|
|
|
|
gst_caps_unref(peer_caps);
|
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
#endif
|
2012-12-15 19:11:13 +08:00
|
|
|
gst_app_sink_set_caps(GST_APP_SINK(sink), caps);
|
2011-04-24 17:48:19 +08:00
|
|
|
gst_caps_unref(caps);
|
|
|
|
|
2014-09-10 14:38:43 +08:00
|
|
|
{
|
2015-08-15 23:30:08 +08:00
|
|
|
status = gst_element_set_state(GST_ELEMENT(pipeline),
|
|
|
|
file ? GST_STATE_PAUSED : GST_STATE_PLAYING);
|
2014-09-10 14:38:43 +08:00
|
|
|
if (status == GST_STATE_CHANGE_ASYNC)
|
|
|
|
{
|
|
|
|
// wait for status update
|
2015-08-15 23:30:08 +08:00
|
|
|
status = gst_element_get_state(pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
|
2014-09-10 14:38:43 +08:00
|
|
|
}
|
|
|
|
if (status == GST_STATE_CHANGE_FAILURE)
|
|
|
|
{
|
|
|
|
handleMessage(pipeline);
|
|
|
|
gst_object_unref(pipeline);
|
|
|
|
pipeline = NULL;
|
|
|
|
CV_ERROR(CV_StsError, "GStreamer: unable to start pipeline\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstFormat format;
|
|
|
|
|
2014-09-16 17:57:44 +08:00
|
|
|
format = GST_FORMAT_DEFAULT;
|
2014-09-10 14:38:43 +08:00
|
|
|
#if GST_VERSION_MAJOR == 0
|
2014-09-16 17:57:44 +08:00
|
|
|
if(!gst_element_query_duration(sink, &format, &duration))
|
2014-09-10 14:38:43 +08:00
|
|
|
#else
|
2014-09-16 17:57:44 +08:00
|
|
|
if(!gst_element_query_duration(sink, format, &duration))
|
2014-09-10 14:38:43 +08:00
|
|
|
#endif
|
|
|
|
{
|
2014-09-16 21:05:36 +08:00
|
|
|
handleMessage(pipeline);
|
2014-09-10 14:38:43 +08:00
|
|
|
CV_WARN("GStreamer: unable to query duration of stream");
|
|
|
|
duration = -1;
|
|
|
|
}
|
2014-09-25 18:40:54 +08:00
|
|
|
|
2017-07-05 18:45:08 +08:00
|
|
|
handleMessage(pipeline);
|
|
|
|
|
2016-03-23 23:24:10 +08:00
|
|
|
GstPad* pad = gst_element_get_static_pad(sink, "sink");
|
2014-09-25 18:51:34 +08:00
|
|
|
#if GST_VERSION_MAJOR == 0
|
2014-09-25 18:40:54 +08:00
|
|
|
GstCaps* buffer_caps = gst_pad_get_caps(pad);
|
2014-09-25 18:51:34 +08:00
|
|
|
#else
|
|
|
|
GstCaps* buffer_caps = gst_pad_get_current_caps(pad);
|
|
|
|
#endif
|
2014-09-25 18:40:54 +08:00
|
|
|
const GstStructure *structure = gst_caps_get_structure (buffer_caps, 0);
|
|
|
|
|
|
|
|
if (!gst_structure_get_int (structure, "width", &width))
|
2014-09-25 18:51:34 +08:00
|
|
|
{
|
2014-09-25 18:40:54 +08:00
|
|
|
CV_WARN("Cannot query video width\n");
|
2014-09-25 18:51:34 +08:00
|
|
|
}
|
2014-09-25 18:40:54 +08:00
|
|
|
|
|
|
|
if (!gst_structure_get_int (structure, "height", &height))
|
2014-09-25 18:51:34 +08:00
|
|
|
{
|
2018-02-14 00:28:11 +08:00
|
|
|
CV_WARN("Cannot query video height\n");
|
2014-09-25 18:51:34 +08:00
|
|
|
}
|
2014-09-25 18:40:54 +08:00
|
|
|
|
|
|
|
gint num = 0, denom=1;
|
|
|
|
if(!gst_structure_get_fraction(structure, "framerate", &num, &denom))
|
2014-09-25 18:51:34 +08:00
|
|
|
{
|
2014-09-25 18:40:54 +08:00
|
|
|
CV_WARN("Cannot query video fps\n");
|
2014-09-25 18:51:34 +08:00
|
|
|
}
|
2014-09-25 18:40:54 +08:00
|
|
|
|
|
|
|
fps = (double)num/(double)denom;
|
|
|
|
|
2017-07-05 18:45:08 +08:00
|
|
|
{
|
|
|
|
GstFormat format_;
|
|
|
|
gint64 value_ = -1;
|
|
|
|
gboolean status_;
|
|
|
|
|
|
|
|
format_ = GST_FORMAT_DEFAULT;
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
|
|
|
#define FORMAT &format_
|
|
|
|
#else
|
|
|
|
#define FORMAT format_
|
|
|
|
#endif
|
|
|
|
status_ = gst_element_query_position(pipeline, FORMAT, &value_);
|
|
|
|
#undef FORMAT
|
|
|
|
if (!status_ || value_ != 0 || duration < 0)
|
|
|
|
{
|
|
|
|
CV_WARN(cv::format("Cannot query video position: status=%d value=%lld duration=%lld\n",
|
|
|
|
(int)status_, (long long int)value_, (long long int)duration).c_str());
|
|
|
|
isPosFramesSupported = false;
|
|
|
|
isPosFramesEmulated = true;
|
|
|
|
emulatedFrameNumber = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
isPosFramesSupported = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline");
|
2014-09-10 14:38:43 +08:00
|
|
|
}
|
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
__END__;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
return true;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2011-04-24 05:24:41 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
2018-02-14 00:28:11 +08:00
|
|
|
* \brief CvCapture_GStreamer::getProperty retrieve the requested property from the pipeline
|
2013-03-21 17:17:34 +08:00
|
|
|
* \param propId requested property
|
|
|
|
* \return property value
|
|
|
|
*
|
2018-02-14 00:28:11 +08:00
|
|
|
* There are two ways the properties can be retrieved. For seek-based properties we can query the pipeline.
|
2013-03-21 17:17:34 +08:00
|
|
|
* For frame-based properties, we use the caps of the lasst receivef sample. This means that some properties
|
|
|
|
* are not available until a first frame was received
|
|
|
|
*/
|
2014-12-11 01:17:35 +08:00
|
|
|
double CvCapture_GStreamer::getProperty( int propId ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
GstFormat format;
|
|
|
|
gint64 value;
|
|
|
|
gboolean status;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
#if GST_VERSION_MAJOR == 0
|
|
|
|
#define FORMAT &format
|
|
|
|
#else
|
|
|
|
#define FORMAT format
|
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
if(!pipeline) {
|
|
|
|
CV_WARN("GStreamer: no pipeline");
|
2015-08-17 02:25:36 +08:00
|
|
|
return 0;
|
2011-04-23 15:27:04 +08:00
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
switch(propId) {
|
|
|
|
case CV_CAP_PROP_POS_MSEC:
|
|
|
|
format = GST_FORMAT_TIME;
|
|
|
|
status = gst_element_query_position(sink, FORMAT, &value);
|
|
|
|
if(!status) {
|
2017-07-05 18:45:08 +08:00
|
|
|
handleMessage(pipeline);
|
2013-03-21 17:17:34 +08:00
|
|
|
CV_WARN("GStreamer: unable to query position of stream");
|
2015-08-17 02:25:36 +08:00
|
|
|
return 0;
|
2011-04-13 15:15:35 +08:00
|
|
|
}
|
|
|
|
return value * 1e-6; // nano seconds to milli seconds
|
|
|
|
case CV_CAP_PROP_POS_FRAMES:
|
2017-07-05 18:45:08 +08:00
|
|
|
if (!isPosFramesSupported)
|
|
|
|
{
|
|
|
|
if (isPosFramesEmulated)
|
|
|
|
return emulatedFrameNumber;
|
|
|
|
return 0; // TODO getProperty() "unsupported" value should be changed
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
format = GST_FORMAT_DEFAULT;
|
2013-03-21 17:17:34 +08:00
|
|
|
status = gst_element_query_position(sink, FORMAT, &value);
|
|
|
|
if(!status) {
|
2017-07-05 18:45:08 +08:00
|
|
|
handleMessage(pipeline);
|
2011-04-13 15:15:35 +08:00
|
|
|
CV_WARN("GStreamer: unable to query position of stream");
|
2015-08-17 02:25:36 +08:00
|
|
|
return 0;
|
2011-04-13 15:15:35 +08:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
case CV_CAP_PROP_POS_AVI_RATIO:
|
|
|
|
format = GST_FORMAT_PERCENT;
|
2013-03-21 17:17:34 +08:00
|
|
|
status = gst_element_query_position(sink, FORMAT, &value);
|
|
|
|
if(!status) {
|
2017-07-05 18:45:08 +08:00
|
|
|
handleMessage(pipeline);
|
2011-04-13 15:15:35 +08:00
|
|
|
CV_WARN("GStreamer: unable to query position of stream");
|
2015-08-17 02:25:36 +08:00
|
|
|
return 0;
|
2011-04-13 15:15:35 +08:00
|
|
|
}
|
|
|
|
return ((double) value) / GST_FORMAT_PERCENT_MAX;
|
2014-09-25 18:40:54 +08:00
|
|
|
case CV_CAP_PROP_FRAME_WIDTH:
|
2013-03-21 17:17:34 +08:00
|
|
|
return width;
|
2014-09-25 18:40:54 +08:00
|
|
|
case CV_CAP_PROP_FRAME_HEIGHT:
|
2013-03-21 17:17:34 +08:00
|
|
|
return height;
|
2014-09-25 18:40:54 +08:00
|
|
|
case CV_CAP_PROP_FPS:
|
|
|
|
return fps;
|
2011-04-13 15:15:35 +08:00
|
|
|
case CV_CAP_PROP_FOURCC:
|
|
|
|
break;
|
|
|
|
case CV_CAP_PROP_FRAME_COUNT:
|
2014-09-10 14:38:43 +08:00
|
|
|
return duration;
|
2011-04-13 15:15:35 +08:00
|
|
|
case CV_CAP_PROP_FORMAT:
|
|
|
|
case CV_CAP_PROP_MODE:
|
|
|
|
case CV_CAP_PROP_BRIGHTNESS:
|
|
|
|
case CV_CAP_PROP_CONTRAST:
|
|
|
|
case CV_CAP_PROP_SATURATION:
|
|
|
|
case CV_CAP_PROP_HUE:
|
2015-08-17 02:25:36 +08:00
|
|
|
if (v4l2src)
|
|
|
|
{
|
|
|
|
const gchar * propName =
|
|
|
|
propId == CV_CAP_PROP_BRIGHTNESS ? "brightness" :
|
|
|
|
propId == CV_CAP_PROP_CONTRAST ? "contrast" :
|
|
|
|
propId == CV_CAP_PROP_SATURATION ? "saturation" :
|
|
|
|
propId == CV_CAP_PROP_HUE ? "hue" : NULL;
|
|
|
|
|
|
|
|
if (propName)
|
|
|
|
{
|
|
|
|
gint32 value32 = 0;
|
|
|
|
g_object_get(G_OBJECT(v4l2src), propName, &value32, NULL);
|
|
|
|
return value32;
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
case CV_CAP_PROP_GAIN:
|
|
|
|
case CV_CAP_PROP_CONVERT_RGB:
|
|
|
|
break;
|
2011-04-23 15:27:04 +08:00
|
|
|
case CV_CAP_GSTREAMER_QUEUE_LENGTH:
|
2011-04-24 17:48:19 +08:00
|
|
|
if(!sink) {
|
2013-03-21 17:17:34 +08:00
|
|
|
CV_WARN("GStreamer: there is no sink yet");
|
|
|
|
return false;
|
2011-04-24 17:48:19 +08:00
|
|
|
}
|
2011-04-23 15:27:04 +08:00
|
|
|
return gst_app_sink_get_max_buffers(GST_APP_SINK(sink));
|
2011-04-13 15:15:35 +08:00
|
|
|
default:
|
|
|
|
CV_WARN("GStreamer: unhandled property");
|
|
|
|
break;
|
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
#undef FORMAT
|
|
|
|
|
2015-08-17 02:25:36 +08:00
|
|
|
return 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
/*!
|
|
|
|
* \brief CvCapture_GStreamer::setProperty
|
|
|
|
* \param propId
|
|
|
|
* \param value
|
|
|
|
* \return success
|
|
|
|
* Sets the desired property id with val. If the pipeline is running,
|
|
|
|
* it is briefly stopped and started again after the property was set
|
|
|
|
*/
|
2010-05-12 01:44:00 +08:00
|
|
|
bool CvCapture_GStreamer::setProperty( int propId, double value )
|
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
GstFormat format;
|
2011-04-13 15:15:35 +08:00
|
|
|
GstSeekFlags flags;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
if(!pipeline) {
|
|
|
|
CV_WARN("GStreamer: no pipeline");
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
bool wasPlaying = this->isPipelinePlaying();
|
|
|
|
if (wasPlaying)
|
|
|
|
this->stopPipeline();
|
|
|
|
|
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
switch(propId) {
|
|
|
|
case CV_CAP_PROP_POS_MSEC:
|
|
|
|
format = GST_FORMAT_TIME;
|
|
|
|
flags = (GstSeekFlags) (GST_SEEK_FLAG_FLUSH|GST_SEEK_FLAG_ACCURATE);
|
|
|
|
if(!gst_element_seek_simple(GST_ELEMENT(pipeline), format,
|
2013-03-21 17:17:34 +08:00
|
|
|
flags, (gint64) (value * GST_MSECOND))) {
|
2017-07-05 18:45:08 +08:00
|
|
|
handleMessage(pipeline);
|
2011-04-13 15:15:35 +08:00
|
|
|
CV_WARN("GStreamer: unable to seek");
|
|
|
|
}
|
2017-07-05 18:45:08 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (isPosFramesEmulated)
|
|
|
|
{
|
|
|
|
if (value == 0)
|
|
|
|
{
|
|
|
|
emulatedFrameNumber = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isPosFramesEmulated = false; // reset frame counter emulation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
break;
|
|
|
|
case CV_CAP_PROP_POS_FRAMES:
|
2017-07-05 18:45:08 +08:00
|
|
|
{
|
|
|
|
if (!isPosFramesSupported)
|
|
|
|
{
|
|
|
|
if (isPosFramesEmulated)
|
|
|
|
{
|
|
|
|
if (value == 0)
|
|
|
|
{
|
|
|
|
restartPipeline();
|
|
|
|
emulatedFrameNumber = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
format = GST_FORMAT_DEFAULT;
|
|
|
|
flags = (GstSeekFlags) (GST_SEEK_FLAG_FLUSH|GST_SEEK_FLAG_ACCURATE);
|
|
|
|
if(!gst_element_seek_simple(GST_ELEMENT(pipeline), format,
|
2013-03-21 17:17:34 +08:00
|
|
|
flags, (gint64) value)) {
|
2017-07-05 18:45:08 +08:00
|
|
|
handleMessage(pipeline);
|
2011-04-13 15:15:35 +08:00
|
|
|
CV_WARN("GStreamer: unable to seek");
|
2017-07-05 18:45:08 +08:00
|
|
|
break;
|
2011-04-13 15:15:35 +08:00
|
|
|
}
|
2017-07-05 18:45:08 +08:00
|
|
|
// wait for status update
|
|
|
|
gst_element_get_state(pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);
|
|
|
|
return true;
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
case CV_CAP_PROP_POS_AVI_RATIO:
|
|
|
|
format = GST_FORMAT_PERCENT;
|
|
|
|
flags = (GstSeekFlags) (GST_SEEK_FLAG_FLUSH|GST_SEEK_FLAG_ACCURATE);
|
|
|
|
if(!gst_element_seek_simple(GST_ELEMENT(pipeline), format,
|
2013-03-21 17:17:34 +08:00
|
|
|
flags, (gint64) (value * GST_FORMAT_PERCENT_MAX))) {
|
2017-07-05 18:45:08 +08:00
|
|
|
handleMessage(pipeline);
|
2011-04-13 15:15:35 +08:00
|
|
|
CV_WARN("GStreamer: unable to seek");
|
|
|
|
}
|
2017-07-05 18:45:08 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (isPosFramesEmulated)
|
|
|
|
{
|
|
|
|
if (value == 0)
|
|
|
|
{
|
|
|
|
emulatedFrameNumber = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isPosFramesEmulated = false; // reset frame counter emulation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
break;
|
|
|
|
case CV_CAP_PROP_FRAME_WIDTH:
|
|
|
|
if(value > 0)
|
|
|
|
setFilter("width", G_TYPE_INT, (int) value, 0);
|
|
|
|
else
|
|
|
|
removeFilter("width");
|
|
|
|
break;
|
|
|
|
case CV_CAP_PROP_FRAME_HEIGHT:
|
|
|
|
if(value > 0)
|
|
|
|
setFilter("height", G_TYPE_INT, (int) value, 0);
|
|
|
|
else
|
|
|
|
removeFilter("height");
|
|
|
|
break;
|
|
|
|
case CV_CAP_PROP_FPS:
|
|
|
|
if(value > 0) {
|
2013-03-21 17:17:34 +08:00
|
|
|
double num=0, denom = 1;
|
|
|
|
toFraction(value, num, denom);
|
|
|
|
setFilter("framerate", GST_TYPE_FRACTION, value, denom);
|
2011-04-13 15:15:35 +08:00
|
|
|
} else
|
|
|
|
removeFilter("framerate");
|
|
|
|
break;
|
|
|
|
case CV_CAP_PROP_FOURCC:
|
|
|
|
case CV_CAP_PROP_FRAME_COUNT:
|
|
|
|
case CV_CAP_PROP_FORMAT:
|
|
|
|
case CV_CAP_PROP_MODE:
|
|
|
|
case CV_CAP_PROP_BRIGHTNESS:
|
|
|
|
case CV_CAP_PROP_CONTRAST:
|
|
|
|
case CV_CAP_PROP_SATURATION:
|
|
|
|
case CV_CAP_PROP_HUE:
|
2015-08-17 02:25:36 +08:00
|
|
|
if (v4l2src)
|
|
|
|
{
|
|
|
|
const gchar * propName =
|
|
|
|
propId == CV_CAP_PROP_BRIGHTNESS ? "brightness" :
|
|
|
|
propId == CV_CAP_PROP_CONTRAST ? "contrast" :
|
|
|
|
propId == CV_CAP_PROP_SATURATION ? "saturation" :
|
|
|
|
propId == CV_CAP_PROP_HUE ? "hue" : NULL;
|
|
|
|
|
|
|
|
if (propName)
|
|
|
|
{
|
|
|
|
gint32 value32 = cv::saturate_cast<gint32>(value);
|
|
|
|
g_object_set(G_OBJECT(v4l2src), propName, &value32, NULL);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 15:15:35 +08:00
|
|
|
case CV_CAP_PROP_GAIN:
|
|
|
|
case CV_CAP_PROP_CONVERT_RGB:
|
|
|
|
break;
|
2011-04-23 15:27:04 +08:00
|
|
|
case CV_CAP_GSTREAMER_QUEUE_LENGTH:
|
|
|
|
if(!sink)
|
2011-04-24 17:48:19 +08:00
|
|
|
break;
|
|
|
|
gst_app_sink_set_max_buffers(GST_APP_SINK(sink), (guint) value);
|
|
|
|
break;
|
2011-04-13 15:15:35 +08:00
|
|
|
default:
|
|
|
|
CV_WARN("GStreamer: unhandled property");
|
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
if (wasPlaying)
|
|
|
|
this->startPipeline();
|
|
|
|
|
2011-04-13 15:15:35 +08:00
|
|
|
return false;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief cvCreateCapture_GStreamer
|
|
|
|
* \param type
|
|
|
|
* \param filename
|
|
|
|
* \return
|
|
|
|
*/
|
2010-05-12 01:44:00 +08:00
|
|
|
CvCapture* cvCreateCapture_GStreamer(int type, const char* filename )
|
|
|
|
{
|
|
|
|
CvCapture_GStreamer* capture = new CvCapture_GStreamer;
|
|
|
|
|
|
|
|
if( capture->open( type, filename ))
|
|
|
|
return capture;
|
|
|
|
|
|
|
|
delete capture;
|
2012-09-05 19:51:51 +08:00
|
|
|
return 0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The CvVideoWriter_GStreamer class
|
|
|
|
* Use Gstreamer to write video
|
|
|
|
*/
|
|
|
|
class CvVideoWriter_GStreamer : public CvVideoWriter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CvVideoWriter_GStreamer() { init(); }
|
2018-03-15 21:16:53 +08:00
|
|
|
virtual ~CvVideoWriter_GStreamer() CV_OVERRIDE { close(); }
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
virtual bool open( const char* filename, int fourcc,
|
|
|
|
double fps, CvSize frameSize, bool isColor );
|
|
|
|
virtual void close();
|
2018-03-15 21:16:53 +08:00
|
|
|
virtual bool writeFrame( const IplImage* image ) CV_OVERRIDE;
|
2013-03-21 17:17:34 +08:00
|
|
|
protected:
|
|
|
|
void init();
|
|
|
|
const char* filenameToMimetype(const char* filename);
|
|
|
|
GstElement* pipeline;
|
|
|
|
GstElement* source;
|
|
|
|
GstElement* encodebin;
|
|
|
|
GstElement* file;
|
|
|
|
|
|
|
|
GstBuffer* buffer;
|
|
|
|
int input_pix_fmt;
|
|
|
|
int num_frames;
|
|
|
|
double framerate;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief CvVideoWriter_GStreamer::init
|
|
|
|
* initialise all variables
|
|
|
|
*/
|
|
|
|
void CvVideoWriter_GStreamer::init()
|
|
|
|
{
|
|
|
|
pipeline = NULL;
|
|
|
|
source = NULL;
|
|
|
|
encodebin = NULL;
|
|
|
|
file = NULL;
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
|
|
num_frames = 0;
|
|
|
|
framerate = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief CvVideoWriter_GStreamer::close
|
|
|
|
* ends the pipeline by sending EOS and destroys the pipeline and all
|
|
|
|
* elements afterwards
|
|
|
|
*/
|
|
|
|
void CvVideoWriter_GStreamer::close()
|
|
|
|
{
|
2014-09-10 14:38:43 +08:00
|
|
|
GstStateChangeReturn status;
|
2013-03-21 23:11:53 +08:00
|
|
|
if (pipeline)
|
|
|
|
{
|
2014-09-16 17:57:44 +08:00
|
|
|
handleMessage(pipeline);
|
2014-09-10 14:38:43 +08:00
|
|
|
|
|
|
|
if (gst_app_src_end_of_stream(GST_APP_SRC(source)) != GST_FLOW_OK)
|
2014-09-16 17:57:44 +08:00
|
|
|
{
|
2014-09-10 14:38:43 +08:00
|
|
|
CV_WARN("Cannot send EOS to GStreamer pipeline\n");
|
2014-09-16 17:57:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
//wait for EOS to trickle down the pipeline. This will let all elements finish properly
|
|
|
|
GstBus* bus = gst_element_get_bus(pipeline);
|
|
|
|
GstMessage *msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
|
2014-09-10 14:38:43 +08:00
|
|
|
if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ERROR)
|
2014-09-16 17:57:44 +08:00
|
|
|
{
|
2014-09-10 14:38:43 +08:00
|
|
|
CV_WARN("Error during VideoWriter finalization\n");
|
2014-09-16 17:57:44 +08:00
|
|
|
return;
|
|
|
|
}
|
2014-09-10 14:38:43 +08:00
|
|
|
|
|
|
|
if(msg != NULL)
|
|
|
|
{
|
2013-03-21 17:17:34 +08:00
|
|
|
gst_message_unref(msg);
|
|
|
|
g_object_unref(G_OBJECT(bus));
|
|
|
|
}
|
|
|
|
|
2014-09-10 14:38:43 +08:00
|
|
|
status = gst_element_set_state (pipeline, GST_STATE_NULL);
|
|
|
|
if (status == GST_STATE_CHANGE_ASYNC)
|
|
|
|
{
|
|
|
|
// wait for status update
|
|
|
|
GstState st1;
|
|
|
|
GstState st2;
|
|
|
|
status = gst_element_get_state(pipeline, &st1, &st2, GST_CLOCK_TIME_NONE);
|
|
|
|
}
|
|
|
|
if (status == GST_STATE_CHANGE_FAILURE)
|
|
|
|
{
|
|
|
|
handleMessage (pipeline);
|
|
|
|
gst_object_unref (GST_OBJECT (pipeline));
|
|
|
|
pipeline = NULL;
|
|
|
|
CV_WARN("Unable to stop gstreamer pipeline\n");
|
|
|
|
return;
|
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
gst_object_unref (GST_OBJECT (pipeline));
|
2014-09-10 14:38:43 +08:00
|
|
|
pipeline = NULL;
|
2013-03-21 17:17:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief CvVideoWriter_GStreamer::filenameToMimetype
|
|
|
|
* \param filename
|
|
|
|
* \return mimetype
|
|
|
|
* Resturns a container mime type for a given filename by looking at it's extension
|
|
|
|
*/
|
|
|
|
const char* CvVideoWriter_GStreamer::filenameToMimetype(const char *filename)
|
|
|
|
{
|
|
|
|
//get extension
|
|
|
|
const char *ext = strrchr(filename, '.');
|
|
|
|
if(!ext || ext == filename) return NULL;
|
|
|
|
ext += 1; //exclude the dot
|
|
|
|
|
|
|
|
// return a container mime based on the given extension.
|
|
|
|
// gstreamer's function returns too much possibilities, which is not useful to us
|
|
|
|
|
|
|
|
//return the appropriate mime
|
|
|
|
if (strncasecmp(ext,"avi", 3) == 0)
|
|
|
|
return (const char*)"video/x-msvideo";
|
|
|
|
|
|
|
|
if (strncasecmp(ext,"mkv", 3) == 0 || strncasecmp(ext,"mk3d",4) == 0 || strncasecmp(ext,"webm",4) == 0 )
|
|
|
|
return (const char*)"video/x-matroska";
|
|
|
|
|
|
|
|
if (strncasecmp(ext,"wmv", 3) == 0)
|
|
|
|
return (const char*)"video/x-ms-asf";
|
|
|
|
|
|
|
|
if (strncasecmp(ext,"mov", 3) == 0)
|
|
|
|
return (const char*)"video/x-quicktime";
|
|
|
|
|
|
|
|
if (strncasecmp(ext,"ogg", 3) == 0 || strncasecmp(ext,"ogv", 3) == 0)
|
|
|
|
return (const char*)"application/ogg";
|
|
|
|
|
|
|
|
if (strncasecmp(ext,"rm", 3) == 0)
|
|
|
|
return (const char*)"vnd.rn-realmedia";
|
|
|
|
|
|
|
|
if (strncasecmp(ext,"swf", 3) == 0)
|
|
|
|
return (const char*)"application/x-shockwave-flash";
|
|
|
|
|
|
|
|
if (strncasecmp(ext,"mp4", 3) == 0)
|
|
|
|
return (const char*)"video/x-quicktime, variant=(string)iso";
|
|
|
|
|
|
|
|
//default to avi
|
|
|
|
return (const char*)"video/x-msvideo";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief CvVideoWriter_GStreamer::open
|
|
|
|
* \param filename filename to output to
|
|
|
|
* \param fourcc desired codec fourcc
|
|
|
|
* \param fps desired framerate
|
|
|
|
* \param frameSize the size of the expected frames
|
|
|
|
* \param is_color color or grayscale
|
|
|
|
* \return success
|
|
|
|
*
|
|
|
|
* We support 2 modes of operation. Either the user enters a filename and a fourcc
|
|
|
|
* code, or enters a manual pipeline description like in CvVideoCapture_Gstreamer.
|
|
|
|
* In the latter case, we just push frames on the appsink with appropriate caps.
|
|
|
|
* In the former case, we try to deduce the correct container from the filename,
|
|
|
|
* and the correct encoder from the fourcc profile.
|
|
|
|
*
|
|
|
|
* If the file extension did was not recognize, an avi container is used
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool CvVideoWriter_GStreamer::open( const char * filename, int fourcc,
|
|
|
|
double fps, CvSize frameSize, bool is_color )
|
|
|
|
{
|
|
|
|
CV_FUNCNAME("CvVideoWriter_GStreamer::open");
|
|
|
|
|
|
|
|
// check arguments
|
|
|
|
assert (filename);
|
|
|
|
assert (fps > 0);
|
|
|
|
assert (frameSize.width > 0 && frameSize.height > 0);
|
|
|
|
|
|
|
|
// init gstreamer
|
|
|
|
gst_initializer::init();
|
|
|
|
|
|
|
|
// init vars
|
|
|
|
bool manualpipeline = true;
|
|
|
|
int bufsize = 0;
|
|
|
|
GError *err = NULL;
|
|
|
|
const char* mime = NULL;
|
|
|
|
GstStateChangeReturn stateret;
|
|
|
|
|
|
|
|
GstCaps* caps = NULL;
|
|
|
|
GstCaps* videocaps = NULL;
|
2013-03-26 16:27:39 +08:00
|
|
|
|
|
|
|
#if FULL_GST_VERSION >= VERSION_NUM(0,10,32)
|
2013-03-21 17:17:34 +08:00
|
|
|
GstCaps* containercaps = NULL;
|
|
|
|
GstEncodingContainerProfile* containerprofile = NULL;
|
|
|
|
GstEncodingVideoProfile* videoprofile = NULL;
|
2013-03-26 16:27:39 +08:00
|
|
|
#endif
|
2013-03-21 17:17:34 +08:00
|
|
|
|
2014-09-16 21:05:36 +08:00
|
|
|
GstIterator* it = NULL;
|
2014-09-23 18:00:53 +08:00
|
|
|
gboolean done = FALSE;
|
|
|
|
GstElement *element = NULL;
|
|
|
|
gchar* name = NULL;
|
2014-09-25 18:51:34 +08:00
|
|
|
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
2014-09-23 18:00:53 +08:00
|
|
|
GstElement* splitter = NULL;
|
|
|
|
GstElement* combiner = NULL;
|
2014-09-25 18:51:34 +08:00
|
|
|
#endif
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
// we first try to construct a pipeline from the given string.
|
|
|
|
// if that fails, we assume it is an ordinary filename
|
|
|
|
|
|
|
|
__BEGIN__;
|
|
|
|
|
|
|
|
encodebin = gst_parse_launch(filename, &err);
|
2014-09-16 21:05:36 +08:00
|
|
|
manualpipeline = (encodebin != NULL);
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
if(manualpipeline)
|
|
|
|
{
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
|
|
|
it = gst_bin_iterate_sources(GST_BIN(encodebin));
|
|
|
|
if(gst_iterator_next(it, (gpointer *)&source) != GST_ITERATOR_OK) {
|
|
|
|
CV_ERROR(CV_StsError, "GStreamer: cannot find appsink in manual pipeline\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
2013-06-13 17:16:33 +08:00
|
|
|
it = gst_bin_iterate_sources (GST_BIN(encodebin));
|
|
|
|
GValue value = G_VALUE_INIT;
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
switch (gst_iterator_next (it, &value)) {
|
|
|
|
case GST_ITERATOR_OK:
|
|
|
|
element = GST_ELEMENT (g_value_get_object (&value));
|
|
|
|
name = gst_element_get_name(element);
|
|
|
|
if (name){
|
|
|
|
if(strstr(name, "opencvsrc") != NULL || strstr(name, "appsrc") != NULL) {
|
|
|
|
source = GST_ELEMENT ( gst_object_ref (element) );
|
|
|
|
done = TRUE;
|
|
|
|
}
|
|
|
|
g_free(name);
|
|
|
|
}
|
|
|
|
g_value_unset (&value);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case GST_ITERATOR_RESYNC:
|
|
|
|
gst_iterator_resync (it);
|
|
|
|
break;
|
|
|
|
case GST_ITERATOR_ERROR:
|
|
|
|
case GST_ITERATOR_DONE:
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2013-03-21 17:17:34 +08:00
|
|
|
}
|
2013-06-13 17:16:33 +08:00
|
|
|
gst_iterator_free (it);
|
2013-06-12 22:58:16 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
if (!source){
|
|
|
|
CV_ERROR(CV_StsError, "GStreamer: cannot find appsrc in manual pipeline\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
pipeline = encodebin;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pipeline = gst_pipeline_new (NULL);
|
|
|
|
|
|
|
|
// we just got a filename and a fourcc code.
|
|
|
|
// first, try to guess the container from the filename
|
|
|
|
//encodebin = gst_element_factory_make("encodebin", NULL);
|
|
|
|
|
|
|
|
//proxy old non existing fourcc ids. These were used in previous opencv versions,
|
|
|
|
//but do not even exist in gstreamer any more
|
|
|
|
if (fourcc == CV_FOURCC('M','P','1','V')) fourcc = CV_FOURCC('M', 'P', 'G' ,'1');
|
|
|
|
if (fourcc == CV_FOURCC('M','P','2','V')) fourcc = CV_FOURCC('M', 'P', 'G' ,'2');
|
|
|
|
if (fourcc == CV_FOURCC('D','R','A','C')) fourcc = CV_FOURCC('d', 'r', 'a' ,'c');
|
|
|
|
|
2014-09-16 21:05:36 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
//create encoder caps from fourcc
|
2014-09-16 21:05:36 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
videocaps = gst_riff_create_video_caps(fourcc, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
if (!videocaps){
|
|
|
|
CV_ERROR( CV_StsUnsupportedFormat, "Gstreamer Opencv backend does not support this codec.");
|
|
|
|
}
|
|
|
|
|
|
|
|
//create container caps from file extension
|
|
|
|
mime = filenameToMimetype(filename);
|
|
|
|
if (!mime) {
|
|
|
|
CV_ERROR( CV_StsUnsupportedFormat, "Gstreamer Opencv backend does not support this file type.");
|
|
|
|
}
|
2013-03-26 16:27:39 +08:00
|
|
|
|
|
|
|
#if FULL_GST_VERSION >= VERSION_NUM(0,10,32)
|
2013-03-21 17:17:34 +08:00
|
|
|
containercaps = gst_caps_from_string(mime);
|
|
|
|
|
|
|
|
//create encodebin profile
|
|
|
|
containerprofile = gst_encoding_container_profile_new("container", "container", containercaps, NULL);
|
|
|
|
videoprofile = gst_encoding_video_profile_new(videocaps, NULL, NULL, 1);
|
|
|
|
gst_encoding_container_profile_add_profile(containerprofile, (GstEncodingProfile *) videoprofile);
|
2013-03-26 16:27:39 +08:00
|
|
|
#endif
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
//create pipeline elements
|
|
|
|
encodebin = gst_element_factory_make("encodebin", NULL);
|
2014-09-16 21:05:36 +08:00
|
|
|
|
2013-03-26 16:27:39 +08:00
|
|
|
#if FULL_GST_VERSION >= VERSION_NUM(0,10,32)
|
2013-03-21 17:17:34 +08:00
|
|
|
g_object_set(G_OBJECT(encodebin), "profile", containerprofile, NULL);
|
2013-03-26 16:27:39 +08:00
|
|
|
#endif
|
2013-03-21 17:17:34 +08:00
|
|
|
source = gst_element_factory_make("appsrc", NULL);
|
|
|
|
file = gst_element_factory_make("filesink", NULL);
|
|
|
|
g_object_set(G_OBJECT(file), "location", filename, NULL);
|
|
|
|
}
|
|
|
|
|
2016-03-19 01:02:19 +08:00
|
|
|
if (fourcc == CV_FOURCC('M','J','P','G') && frameSize.height == 1)
|
|
|
|
{
|
|
|
|
#if GST_VERSION_MAJOR > 0
|
2016-04-14 19:11:26 +08:00
|
|
|
input_pix_fmt = GST_VIDEO_FORMAT_ENCODED;
|
2016-03-19 01:02:19 +08:00
|
|
|
caps = gst_caps_new_simple("image/jpeg",
|
|
|
|
"framerate", GST_TYPE_FRACTION, int(fps), 1,
|
|
|
|
NULL);
|
|
|
|
caps = gst_caps_fixate(caps);
|
|
|
|
#else
|
|
|
|
CV_ERROR( CV_StsUnsupportedFormat, "Gstreamer 0.10 Opencv backend does not support writing encoded MJPEG data.");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if(is_color)
|
2013-03-21 17:17:34 +08:00
|
|
|
{
|
|
|
|
input_pix_fmt = GST_VIDEO_FORMAT_BGR;
|
|
|
|
bufsize = frameSize.width * frameSize.height * 3;
|
|
|
|
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
|
|
|
caps = gst_video_format_new_caps(GST_VIDEO_FORMAT_BGR,
|
|
|
|
frameSize.width,
|
|
|
|
frameSize.height,
|
|
|
|
int(fps), 1,
|
|
|
|
1, 1);
|
|
|
|
#else
|
|
|
|
caps = gst_caps_new_simple("video/x-raw",
|
|
|
|
"format", G_TYPE_STRING, "BGR",
|
|
|
|
"width", G_TYPE_INT, frameSize.width,
|
|
|
|
"height", G_TYPE_INT, frameSize.height,
|
|
|
|
"framerate", GST_TYPE_FRACTION, int(fps), 1,
|
|
|
|
NULL);
|
|
|
|
caps = gst_caps_fixate(caps);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-26 16:27:39 +08:00
|
|
|
#if FULL_GST_VERSION >= VERSION_NUM(0,10,29)
|
2013-03-21 17:17:34 +08:00
|
|
|
input_pix_fmt = GST_VIDEO_FORMAT_GRAY8;
|
|
|
|
bufsize = frameSize.width * frameSize.height;
|
|
|
|
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
|
|
|
caps = gst_video_format_new_caps(GST_VIDEO_FORMAT_GRAY8,
|
|
|
|
frameSize.width,
|
|
|
|
frameSize.height,
|
|
|
|
int(fps), 1,
|
|
|
|
1, 1);
|
|
|
|
#else
|
|
|
|
caps = gst_caps_new_simple("video/x-raw",
|
|
|
|
"format", G_TYPE_STRING, "GRAY8",
|
|
|
|
"width", G_TYPE_INT, frameSize.width,
|
|
|
|
"height", G_TYPE_INT, frameSize.height,
|
|
|
|
"framerate", GST_TYPE_FRACTION, int(fps), 1,
|
|
|
|
NULL);
|
|
|
|
caps = gst_caps_fixate(caps);
|
2013-03-26 16:27:39 +08:00
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
CV_Assert(!"Gstreamer 0.10.29 or newer is required for grayscale input");
|
2013-03-21 17:17:34 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_app_src_set_caps(GST_APP_SRC(source), caps);
|
|
|
|
gst_app_src_set_stream_type(GST_APP_SRC(source), GST_APP_STREAM_TYPE_STREAM);
|
|
|
|
gst_app_src_set_size (GST_APP_SRC(source), -1);
|
|
|
|
|
|
|
|
g_object_set(G_OBJECT(source), "format", GST_FORMAT_TIME, NULL);
|
2013-03-31 17:26:21 +08:00
|
|
|
g_object_set(G_OBJECT(source), "block", 1, NULL);
|
|
|
|
g_object_set(G_OBJECT(source), "is-live", 0, NULL);
|
2014-09-16 21:05:36 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
if(!manualpipeline)
|
|
|
|
{
|
|
|
|
g_object_set(G_OBJECT(file), "buffer-size", bufsize, NULL);
|
|
|
|
gst_bin_add_many(GST_BIN(pipeline), source, encodebin, file, NULL);
|
|
|
|
if(!gst_element_link_many(source, encodebin, file, NULL)) {
|
|
|
|
CV_ERROR(CV_StsError, "GStreamer: cannot link elements\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 18:00:53 +08:00
|
|
|
#if GST_VERSION_MAJOR == 0
|
2014-09-16 21:05:36 +08:00
|
|
|
// HACK: remove streamsplitter and streamcombiner from
|
|
|
|
// encodebin pipeline to prevent early EOF event handling
|
|
|
|
// We always fetch BGR or gray-scale frames, so combiner->spliter
|
|
|
|
// endge in graph is useless.
|
|
|
|
it = gst_bin_iterate_recurse (GST_BIN(encodebin));
|
|
|
|
while (!done) {
|
2014-09-23 18:00:53 +08:00
|
|
|
switch (gst_iterator_next (it, (void**)&element)) {
|
2014-09-16 21:05:36 +08:00
|
|
|
case GST_ITERATOR_OK:
|
2014-09-23 18:00:53 +08:00
|
|
|
name = gst_element_get_name(element);
|
2014-09-16 21:05:36 +08:00
|
|
|
if (strstr(name, "streamsplitter"))
|
2014-09-23 18:00:53 +08:00
|
|
|
splitter = element;
|
2014-09-16 21:05:36 +08:00
|
|
|
else if (strstr(name, "streamcombiner"))
|
2014-09-23 18:00:53 +08:00
|
|
|
combiner = element;
|
2014-09-16 21:05:36 +08:00
|
|
|
break;
|
|
|
|
case GST_ITERATOR_RESYNC:
|
|
|
|
gst_iterator_resync (it);
|
|
|
|
break;
|
|
|
|
case GST_ITERATOR_ERROR:
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
case GST_ITERATOR_DONE:
|
|
|
|
done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_iterator_free (it);
|
|
|
|
|
|
|
|
if (splitter && combiner)
|
|
|
|
{
|
|
|
|
gst_element_unlink(splitter, combiner);
|
|
|
|
|
|
|
|
GstPad* src = gst_element_get_pad(combiner, "src");
|
|
|
|
GstPad* sink = gst_element_get_pad(combiner, "encodingsink");
|
|
|
|
|
|
|
|
GstPad* srcPeer = gst_pad_get_peer(src);
|
|
|
|
GstPad* sinkPeer = gst_pad_get_peer(sink);
|
|
|
|
|
|
|
|
gst_pad_unlink(sinkPeer, sink);
|
|
|
|
gst_pad_unlink(src, srcPeer);
|
|
|
|
|
|
|
|
gst_pad_link(sinkPeer, srcPeer);
|
|
|
|
|
|
|
|
src = gst_element_get_pad(splitter, "encodingsrc");
|
|
|
|
sink = gst_element_get_pad(splitter, "sink");
|
|
|
|
|
|
|
|
srcPeer = gst_pad_get_peer(src);
|
|
|
|
sinkPeer = gst_pad_get_peer(sink);
|
|
|
|
|
|
|
|
gst_pad_unlink(sinkPeer, sink);
|
|
|
|
gst_pad_unlink(src, srcPeer);
|
|
|
|
|
|
|
|
gst_pad_link(sinkPeer, srcPeer);
|
|
|
|
}
|
2014-09-23 18:00:53 +08:00
|
|
|
#endif
|
2014-09-16 21:05:36 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
stateret = gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
|
|
|
|
if(stateret == GST_STATE_CHANGE_FAILURE) {
|
2014-09-10 14:38:43 +08:00
|
|
|
handleMessage(pipeline);
|
2013-03-21 17:17:34 +08:00
|
|
|
CV_ERROR(CV_StsError, "GStreamer: cannot put pipeline to play\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
framerate = fps;
|
|
|
|
num_frames = 0;
|
|
|
|
|
2014-09-10 14:38:43 +08:00
|
|
|
handleMessage(pipeline);
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
__END__;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief CvVideoWriter_GStreamer::writeFrame
|
|
|
|
* \param image
|
|
|
|
* \return
|
|
|
|
* Pushes the given frame on the pipeline.
|
|
|
|
* The timestamp for the buffer is generated from the framerate set in open
|
|
|
|
* and ensures a smooth video
|
|
|
|
*/
|
|
|
|
bool CvVideoWriter_GStreamer::writeFrame( const IplImage * image )
|
|
|
|
{
|
|
|
|
CV_FUNCNAME("CvVideoWriter_GStreamer::writerFrame");
|
|
|
|
|
|
|
|
GstClockTime duration, timestamp;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
__BEGIN__;
|
2014-09-10 14:38:43 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
handleMessage(pipeline);
|
|
|
|
|
2016-04-14 19:11:26 +08:00
|
|
|
#if GST_VERSION_MAJOR > 0
|
2016-03-19 01:02:19 +08:00
|
|
|
if (input_pix_fmt == GST_VIDEO_FORMAT_ENCODED) {
|
|
|
|
if (image->nChannels != 1 || image->depth != IPL_DEPTH_8U || image->height != 1) {
|
|
|
|
CV_ERROR(CV_StsUnsupportedFormat, "cvWriteFrame() needs images with depth = IPL_DEPTH_8U, nChannels = 1 and height = 1.");
|
|
|
|
}
|
|
|
|
}
|
2016-04-14 19:11:26 +08:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if(input_pix_fmt == GST_VIDEO_FORMAT_BGR) {
|
2013-03-21 17:17:34 +08:00
|
|
|
if (image->nChannels != 3 || image->depth != IPL_DEPTH_8U) {
|
|
|
|
CV_ERROR(CV_StsUnsupportedFormat, "cvWriteFrame() needs images with depth = IPL_DEPTH_8U and nChannels = 3.");
|
|
|
|
}
|
|
|
|
}
|
2013-03-26 16:27:39 +08:00
|
|
|
#if FULL_GST_VERSION >= VERSION_NUM(0,10,29)
|
2013-03-21 17:17:34 +08:00
|
|
|
else if (input_pix_fmt == GST_VIDEO_FORMAT_GRAY8) {
|
|
|
|
if (image->nChannels != 1 || image->depth != IPL_DEPTH_8U) {
|
|
|
|
CV_ERROR(CV_StsUnsupportedFormat, "cvWriteFrame() needs images with depth = IPL_DEPTH_8U and nChannels = 1.");
|
|
|
|
}
|
|
|
|
}
|
2013-03-26 16:27:39 +08:00
|
|
|
#endif
|
2013-03-21 17:17:34 +08:00
|
|
|
else {
|
2014-09-16 21:05:36 +08:00
|
|
|
CV_ERROR(CV_StsUnsupportedFormat, "cvWriteFrame() needs BGR or grayscale images\n");
|
2014-09-10 14:38:43 +08:00
|
|
|
return false;
|
2013-03-21 17:17:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size = image->imageSize;
|
|
|
|
duration = ((double)1/framerate) * GST_SECOND;
|
|
|
|
timestamp = num_frames * duration;
|
|
|
|
|
|
|
|
//gst_app_src_push_buffer takes ownership of the buffer, so we need to supply it a copy
|
|
|
|
#if GST_VERSION_MAJOR == 0
|
2014-09-16 21:05:36 +08:00
|
|
|
buffer = gst_buffer_try_new_and_alloc (size);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
CV_ERROR(CV_StsBadSize, "Cannot create GStreamer buffer");
|
|
|
|
}
|
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
memcpy(GST_BUFFER_DATA (buffer), (guint8*)image->imageData, size);
|
|
|
|
GST_BUFFER_DURATION(buffer) = duration;
|
|
|
|
GST_BUFFER_TIMESTAMP(buffer) = timestamp;
|
|
|
|
#else
|
|
|
|
buffer = gst_buffer_new_allocate (NULL, size, NULL);
|
|
|
|
GstMapInfo info;
|
|
|
|
gst_buffer_map(buffer, &info, (GstMapFlags)GST_MAP_READ);
|
|
|
|
memcpy(info.data, (guint8*)image->imageData, size);
|
|
|
|
gst_buffer_unmap(buffer, &info);
|
|
|
|
GST_BUFFER_DURATION(buffer) = duration;
|
|
|
|
GST_BUFFER_PTS(buffer) = timestamp;
|
|
|
|
GST_BUFFER_DTS(buffer) = timestamp;
|
|
|
|
#endif
|
|
|
|
//set the current number in the frame
|
|
|
|
GST_BUFFER_OFFSET(buffer) = num_frames;
|
|
|
|
|
|
|
|
ret = gst_app_src_push_buffer(GST_APP_SRC(source), buffer);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
2014-09-10 14:38:43 +08:00
|
|
|
CV_WARN("Error pushing buffer to GStreamer pipeline");
|
|
|
|
return false;
|
2013-03-21 17:17:34 +08:00
|
|
|
}
|
2014-09-16 21:05:36 +08:00
|
|
|
|
|
|
|
//GST_DEBUG_BIN_TO_DOT_FILE(GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline");
|
2014-09-10 14:38:43 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
++num_frames;
|
|
|
|
|
|
|
|
__END__;
|
2014-09-10 14:38:43 +08:00
|
|
|
|
2013-03-21 17:17:34 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief cvCreateVideoWriter_GStreamer
|
|
|
|
* \param filename
|
|
|
|
* \param fourcc
|
|
|
|
* \param fps
|
|
|
|
* \param frameSize
|
|
|
|
* \param isColor
|
|
|
|
* \return
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
CvVideoWriter* cvCreateVideoWriter_GStreamer(const char* filename, int fourcc, double fps,
|
|
|
|
CvSize frameSize, int isColor )
|
|
|
|
{
|
|
|
|
CvVideoWriter_GStreamer* wrt = new CvVideoWriter_GStreamer;
|
|
|
|
if( wrt->open(filename, fourcc, fps,frameSize, isColor))
|
|
|
|
return wrt;
|
|
|
|
|
|
|
|
delete wrt;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// utility functions
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief toFraction
|
|
|
|
* \param decimal
|
|
|
|
* \param numerator
|
|
|
|
* \param denominator
|
|
|
|
* Split a floating point value into numerator and denominator
|
|
|
|
*/
|
|
|
|
void toFraction(double decimal, double &numerator, double &denominator)
|
|
|
|
{
|
|
|
|
double dummy;
|
|
|
|
double whole;
|
|
|
|
decimal = modf (decimal, &whole);
|
|
|
|
for (denominator = 1; denominator<=100; denominator++){
|
|
|
|
if (modf(denominator * decimal, &dummy) < 0.001f)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
numerator = denominator * decimal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief handleMessage
|
|
|
|
* Handles gstreamer bus messages. Mainly for debugging purposes and ensuring clean shutdown on error
|
|
|
|
*/
|
|
|
|
void handleMessage(GstElement * pipeline)
|
|
|
|
{
|
|
|
|
CV_FUNCNAME("handlemessage");
|
|
|
|
|
|
|
|
GError *err = NULL;
|
|
|
|
gchar *debug = NULL;
|
|
|
|
GstBus* bus = NULL;
|
|
|
|
GstStreamStatusType tp;
|
|
|
|
GstElement * elem = NULL;
|
|
|
|
GstMessage* msg = NULL;
|
|
|
|
|
|
|
|
__BEGIN__;
|
|
|
|
bus = gst_element_get_bus(pipeline);
|
|
|
|
|
|
|
|
while(gst_bus_have_pending(bus)) {
|
|
|
|
msg = gst_bus_pop(bus);
|
|
|
|
|
2017-07-05 18:45:08 +08:00
|
|
|
//printf("\t\tGot %s message\n", GST_MESSAGE_TYPE_NAME(msg));
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
if(gst_is_missing_plugin_message(msg))
|
|
|
|
{
|
|
|
|
CV_ERROR(CV_StsError, "GStreamer: your gstreamer installation is missing a required plugin\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (GST_MESSAGE_TYPE (msg)) {
|
|
|
|
case GST_MESSAGE_STATE_CHANGED:
|
|
|
|
GstState oldstate, newstate, pendstate;
|
|
|
|
gst_message_parse_state_changed(msg, &oldstate, &newstate, &pendstate);
|
2017-07-05 18:45:08 +08:00
|
|
|
//fprintf(stderr, "\t\t%s: state changed from %s to %s (pending: %s)\n",
|
|
|
|
// gst_element_get_name(GST_MESSAGE_SRC (msg)),
|
|
|
|
// gst_element_state_get_name(oldstate),
|
2013-03-21 17:17:34 +08:00
|
|
|
// gst_element_state_get_name(newstate), gst_element_state_get_name(pendstate));
|
|
|
|
break;
|
|
|
|
case GST_MESSAGE_ERROR:
|
|
|
|
gst_message_parse_error(msg, &err, &debug);
|
2017-07-05 18:45:08 +08:00
|
|
|
//fprintf(stderr, "\t\tGStreamer Plugin: Embedded video playback halted; module %s reported: %s\n",
|
|
|
|
// gst_element_get_name(GST_MESSAGE_SRC (msg)), err->message);
|
2013-03-21 17:17:34 +08:00
|
|
|
|
|
|
|
g_error_free(err);
|
|
|
|
g_free(debug);
|
|
|
|
|
|
|
|
gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_NULL);
|
|
|
|
break;
|
|
|
|
case GST_MESSAGE_EOS:
|
2017-07-05 18:45:08 +08:00
|
|
|
//fprintf(stderr, "\t\treached the end of the stream.");
|
2013-03-21 17:17:34 +08:00
|
|
|
break;
|
|
|
|
case GST_MESSAGE_STREAM_STATUS:
|
|
|
|
gst_message_parse_stream_status(msg,&tp,&elem);
|
2017-07-05 18:45:08 +08:00
|
|
|
//fprintf(stderr, "\t\tstream status: elem %s, %i\n", GST_ELEMENT_NAME(elem), tp);
|
2013-03-21 17:17:34 +08:00
|
|
|
break;
|
|
|
|
default:
|
2017-07-05 18:45:08 +08:00
|
|
|
//fprintf(stderr, "\t\tunhandled message %s\n",GST_MESSAGE_TYPE_NAME(msg));
|
2013-03-21 17:17:34 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gst_message_unref(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_object_unref(GST_OBJECT(bus));
|
|
|
|
|
|
|
|
__END__
|
|
|
|
}
|