Merge pull request #3220 from asmorkalov:ocv_gstreamer
This commit is contained in:
commit
4a0364ded5
@ -74,7 +74,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if GST_VERSION_MAJOR > 0
|
#if GST_VERSION_MAJOR > 0
|
||||||
#define COLOR_ELEM "videoconvert"
|
#define COLOR_ELEM "autovideoconvert"
|
||||||
#else
|
#else
|
||||||
#define COLOR_ELEM "ffmpegcolorspace"
|
#define COLOR_ELEM "ffmpegcolorspace"
|
||||||
#endif
|
#endif
|
||||||
@ -151,6 +151,7 @@ protected:
|
|||||||
GstCaps* caps;
|
GstCaps* caps;
|
||||||
GstCaps* buffer_caps;
|
GstCaps* buffer_caps;
|
||||||
IplImage* frame;
|
IplImage* frame;
|
||||||
|
gint64 duration;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -171,6 +172,7 @@ void CvCapture_GStreamer::init()
|
|||||||
caps = NULL;
|
caps = NULL;
|
||||||
buffer_caps = NULL;
|
buffer_caps = NULL;
|
||||||
frame = NULL;
|
frame = NULL;
|
||||||
|
duration = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -187,42 +189,8 @@ void CvCapture_GStreamer::close()
|
|||||||
gst_object_unref(GST_OBJECT(pipeline));
|
gst_object_unref(GST_OBJECT(pipeline));
|
||||||
pipeline = NULL;
|
pipeline = NULL;
|
||||||
}
|
}
|
||||||
if(uridecodebin){
|
|
||||||
gst_object_unref(GST_OBJECT(uridecodebin));
|
|
||||||
uridecodebin = NULL;
|
|
||||||
}
|
|
||||||
if(color){
|
|
||||||
gst_object_unref(GST_OBJECT(color));
|
|
||||||
color = NULL;
|
|
||||||
}
|
|
||||||
if(sink){
|
|
||||||
gst_object_unref(GST_OBJECT(sink));
|
|
||||||
sink = NULL;
|
|
||||||
}
|
|
||||||
if(buffer) {
|
|
||||||
gst_buffer_unref(buffer);
|
|
||||||
buffer = NULL;
|
|
||||||
}
|
|
||||||
if(frame) {
|
|
||||||
frame->imageData = 0;
|
|
||||||
cvReleaseImage(&frame);
|
|
||||||
frame = NULL;
|
|
||||||
}
|
|
||||||
if(caps){
|
|
||||||
gst_caps_unref(caps);
|
|
||||||
caps = NULL;
|
|
||||||
}
|
|
||||||
if(buffer_caps){
|
|
||||||
gst_caps_unref(buffer_caps);
|
|
||||||
buffer_caps = NULL;
|
|
||||||
}
|
|
||||||
#if GST_VERSION_MAJOR > 0
|
|
||||||
if(sample){
|
|
||||||
gst_sample_unref(sample);
|
|
||||||
sample = NULL;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
duration = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -394,10 +362,20 @@ void CvCapture_GStreamer::startPipeline()
|
|||||||
__BEGIN__;
|
__BEGIN__;
|
||||||
|
|
||||||
//fprintf(stderr, "relinked, pausing\n");
|
//fprintf(stderr, "relinked, pausing\n");
|
||||||
if(gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING) ==
|
GstStateChangeReturn status = gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
|
||||||
GST_STATE_CHANGE_FAILURE) {
|
if (status == GST_STATE_CHANGE_ASYNC)
|
||||||
CV_ERROR(CV_StsError, "GStreamer: unable to start pipeline\n");
|
{
|
||||||
|
// 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(pipeline);
|
gst_object_unref(pipeline);
|
||||||
|
pipeline = NULL;
|
||||||
|
CV_ERROR(CV_StsError, "GStreamer: unable to start pipeline\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -422,6 +400,7 @@ void CvCapture_GStreamer::stopPipeline()
|
|||||||
GST_STATE_CHANGE_FAILURE) {
|
GST_STATE_CHANGE_FAILURE) {
|
||||||
CV_ERROR(CV_StsError, "GStreamer: unable to stop pipeline\n");
|
CV_ERROR(CV_StsError, "GStreamer: unable to stop pipeline\n");
|
||||||
gst_object_unref(pipeline);
|
gst_object_unref(pipeline);
|
||||||
|
pipeline = NULL;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
__END__;
|
__END__;
|
||||||
@ -581,6 +560,7 @@ bool CvCapture_GStreamer::open( int type, const char* filename )
|
|||||||
char *uri = NULL;
|
char *uri = NULL;
|
||||||
uridecodebin = NULL;
|
uridecodebin = NULL;
|
||||||
GstElementFactory * testfac;
|
GstElementFactory * testfac;
|
||||||
|
GstStateChangeReturn status;
|
||||||
|
|
||||||
if (type == CV_CAP_GSTREAMER_V4L){
|
if (type == CV_CAP_GSTREAMER_V4L){
|
||||||
testfac = gst_element_factory_find("v4lsrc");
|
testfac = gst_element_factory_find("v4lsrc");
|
||||||
@ -717,8 +697,8 @@ bool CvCapture_GStreamer::open( int type, const char* filename )
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
pipeline = gst_pipeline_new(NULL);
|
pipeline = gst_pipeline_new(NULL);
|
||||||
// videoconvert (in 0.10: ffmpegcolorspace) automatically selects the correct colorspace
|
// videoconvert (in 0.10: ffmpegcolorspace, in 1.x autovideoconvert)
|
||||||
// conversion based on caps.
|
//automatically selects the correct colorspace conversion based on caps.
|
||||||
color = gst_element_factory_make(COLOR_ELEM, NULL);
|
color = gst_element_factory_make(COLOR_ELEM, NULL);
|
||||||
sink = gst_element_factory_make("appsink", NULL);
|
sink = gst_element_factory_make("appsink", NULL);
|
||||||
|
|
||||||
@ -728,6 +708,7 @@ bool CvCapture_GStreamer::open( int type, const char* filename )
|
|||||||
if(!gst_element_link(uridecodebin, color)) {
|
if(!gst_element_link(uridecodebin, color)) {
|
||||||
CV_ERROR(CV_StsError, "GStreamer: cannot link color -> sink\n");
|
CV_ERROR(CV_StsError, "GStreamer: cannot link color -> sink\n");
|
||||||
gst_object_unref(pipeline);
|
gst_object_unref(pipeline);
|
||||||
|
pipeline = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
@ -737,6 +718,7 @@ bool CvCapture_GStreamer::open( int type, const char* filename )
|
|||||||
if(!gst_element_link(color, sink)) {
|
if(!gst_element_link(color, sink)) {
|
||||||
CV_ERROR(CV_StsError, "GStreamer: cannot link color -> sink\n");
|
CV_ERROR(CV_StsError, "GStreamer: cannot link color -> sink\n");
|
||||||
gst_object_unref(pipeline);
|
gst_object_unref(pipeline);
|
||||||
|
pipeline = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -761,8 +743,45 @@ bool CvCapture_GStreamer::open( int type, const char* filename )
|
|||||||
gst_app_sink_set_caps(GST_APP_SINK(sink), caps);
|
gst_app_sink_set_caps(GST_APP_SINK(sink), caps);
|
||||||
gst_caps_unref(caps);
|
gst_caps_unref(caps);
|
||||||
|
|
||||||
//we do not start recording here just yet.
|
// For video files only: set pipeline to PAUSED state to get its duration
|
||||||
// the user probably wants to set capture properties first, so start recording whenever the first frame is requested
|
if (stream)
|
||||||
|
{
|
||||||
|
status = gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PAUSED);
|
||||||
|
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(pipeline);
|
||||||
|
pipeline = NULL;
|
||||||
|
CV_ERROR(CV_StsError, "GStreamer: unable to start pipeline\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
GstFormat format;
|
||||||
|
|
||||||
|
format = GST_FORMAT_DEFAULT;
|
||||||
|
#if GST_VERSION_MAJOR == 0
|
||||||
|
if(!gst_element_query_duration(sink, &format, &duration))
|
||||||
|
#else
|
||||||
|
if(!gst_element_query_duration(sink, format, &duration))
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
CV_WARN("GStreamer: unable to query duration of stream");
|
||||||
|
duration = -1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
duration = -1;
|
||||||
|
}
|
||||||
|
|
||||||
__END__;
|
__END__;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -864,13 +883,7 @@ double CvCapture_GStreamer::getProperty( int propId )
|
|||||||
case CV_CAP_PROP_FOURCC:
|
case CV_CAP_PROP_FOURCC:
|
||||||
break;
|
break;
|
||||||
case CV_CAP_PROP_FRAME_COUNT:
|
case CV_CAP_PROP_FRAME_COUNT:
|
||||||
format = GST_FORMAT_DEFAULT;
|
return duration;
|
||||||
status = gst_element_query_position(sink, FORMAT, &value);
|
|
||||||
if(!status) {
|
|
||||||
CV_WARN("GStreamer: unable to query position of stream");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
case CV_CAP_PROP_FORMAT:
|
case CV_CAP_PROP_FORMAT:
|
||||||
case CV_CAP_PROP_MODE:
|
case CV_CAP_PROP_MODE:
|
||||||
case CV_CAP_PROP_BRIGHTNESS:
|
case CV_CAP_PROP_BRIGHTNESS:
|
||||||
@ -1059,28 +1072,51 @@ void CvVideoWriter_GStreamer::init()
|
|||||||
*/
|
*/
|
||||||
void CvVideoWriter_GStreamer::close()
|
void CvVideoWriter_GStreamer::close()
|
||||||
{
|
{
|
||||||
|
GstStateChangeReturn status;
|
||||||
if (pipeline)
|
if (pipeline)
|
||||||
{
|
{
|
||||||
gst_app_src_end_of_stream(GST_APP_SRC(source));
|
handleMessage(pipeline);
|
||||||
|
|
||||||
|
if (gst_app_src_end_of_stream(GST_APP_SRC(source)) != GST_FLOW_OK)
|
||||||
|
{
|
||||||
|
CV_WARN("Cannot send EOS to GStreamer pipeline\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//wait for EOS to trickle down the pipeline. This will let all elements finish properly
|
//wait for EOS to trickle down the pipeline. This will let all elements finish properly
|
||||||
GstBus* bus = gst_element_get_bus(pipeline);
|
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));
|
GstMessage *msg = gst_bus_timed_pop_filtered(bus, GST_CLOCK_TIME_NONE, (GstMessageType)(GST_MESSAGE_ERROR | GST_MESSAGE_EOS));
|
||||||
if(msg != NULL){
|
if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ERROR)
|
||||||
|
{
|
||||||
|
CV_WARN("Error during VideoWriter finalization\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(msg != NULL)
|
||||||
|
{
|
||||||
gst_message_unref(msg);
|
gst_message_unref(msg);
|
||||||
g_object_unref(G_OBJECT(bus));
|
g_object_unref(G_OBJECT(bus));
|
||||||
}
|
}
|
||||||
|
|
||||||
gst_element_set_state (pipeline, GST_STATE_NULL);
|
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);
|
handleMessage (pipeline);
|
||||||
|
gst_object_unref (GST_OBJECT (pipeline));
|
||||||
|
pipeline = NULL;
|
||||||
|
CV_WARN("Unable to stop gstreamer pipeline\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
gst_object_unref (GST_OBJECT (pipeline));
|
gst_object_unref (GST_OBJECT (pipeline));
|
||||||
|
pipeline = NULL;
|
||||||
if (source)
|
|
||||||
gst_object_unref (GST_OBJECT (source));
|
|
||||||
|
|
||||||
if (file)
|
|
||||||
gst_object_unref (GST_OBJECT (file));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1353,13 +1389,15 @@ bool CvVideoWriter_GStreamer::open( const char * filename, int fourcc,
|
|||||||
|
|
||||||
stateret = gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
|
stateret = gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
|
||||||
if(stateret == GST_STATE_CHANGE_FAILURE) {
|
if(stateret == GST_STATE_CHANGE_FAILURE) {
|
||||||
|
handleMessage(pipeline);
|
||||||
CV_ERROR(CV_StsError, "GStreamer: cannot put pipeline to play\n");
|
CV_ERROR(CV_StsError, "GStreamer: cannot put pipeline to play\n");
|
||||||
}
|
}
|
||||||
handleMessage(pipeline);
|
|
||||||
|
|
||||||
framerate = fps;
|
framerate = fps;
|
||||||
num_frames = 0;
|
num_frames = 0;
|
||||||
|
|
||||||
|
handleMessage(pipeline);
|
||||||
|
|
||||||
__END__;
|
__END__;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -1376,7 +1414,6 @@ bool CvVideoWriter_GStreamer::open( const char * filename, int fourcc,
|
|||||||
*/
|
*/
|
||||||
bool CvVideoWriter_GStreamer::writeFrame( const IplImage * image )
|
bool CvVideoWriter_GStreamer::writeFrame( const IplImage * image )
|
||||||
{
|
{
|
||||||
|
|
||||||
CV_FUNCNAME("CvVideoWriter_GStreamer::writerFrame");
|
CV_FUNCNAME("CvVideoWriter_GStreamer::writerFrame");
|
||||||
|
|
||||||
GstClockTime duration, timestamp;
|
GstClockTime duration, timestamp;
|
||||||
@ -1384,6 +1421,7 @@ bool CvVideoWriter_GStreamer::writeFrame( const IplImage * image )
|
|||||||
int size;
|
int size;
|
||||||
|
|
||||||
__BEGIN__;
|
__BEGIN__;
|
||||||
|
|
||||||
handleMessage(pipeline);
|
handleMessage(pipeline);
|
||||||
|
|
||||||
if (input_pix_fmt == GST_VIDEO_FORMAT_BGR) {
|
if (input_pix_fmt == GST_VIDEO_FORMAT_BGR) {
|
||||||
@ -1399,7 +1437,8 @@ bool CvVideoWriter_GStreamer::writeFrame( const IplImage * image )
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else {
|
else {
|
||||||
assert(false);
|
CV_WARN("Invalid video format!\n");
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
size = image->imageSize;
|
size = image->imageSize;
|
||||||
@ -1427,13 +1466,15 @@ bool CvVideoWriter_GStreamer::writeFrame( const IplImage * image )
|
|||||||
|
|
||||||
ret = gst_app_src_push_buffer(GST_APP_SRC(source), buffer);
|
ret = gst_app_src_push_buffer(GST_APP_SRC(source), buffer);
|
||||||
if (ret != GST_FLOW_OK) {
|
if (ret != GST_FLOW_OK) {
|
||||||
/* something wrong, stop pushing */
|
CV_WARN("Error pushing buffer to GStreamer pipeline");
|
||||||
assert(false);
|
return false;
|
||||||
}
|
}
|
||||||
//gst_debug_bin_to_dot_file (GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline");
|
//gst_debug_bin_to_dot_file (GST_BIN(pipeline), GST_DEBUG_GRAPH_SHOW_ALL, "pipeline");
|
||||||
|
|
||||||
++num_frames;
|
++num_frames;
|
||||||
|
|
||||||
__END__;
|
__END__;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1518,7 +1559,6 @@ void handleMessage(GstElement * pipeline)
|
|||||||
break;
|
break;
|
||||||
case GST_MESSAGE_ERROR:
|
case GST_MESSAGE_ERROR:
|
||||||
gst_message_parse_error(msg, &err, &debug);
|
gst_message_parse_error(msg, &err, &debug);
|
||||||
|
|
||||||
//fprintf(stderr, "GStreamer Plugin: Embedded video playback halted; module %s reported: %s\n",
|
//fprintf(stderr, "GStreamer Plugin: Embedded video playback halted; module %s reported: %s\n",
|
||||||
// gst_element_get_name(GST_MESSAGE_SRC (msg)), err->message);
|
// gst_element_get_name(GST_MESSAGE_SRC (msg)), err->message);
|
||||||
|
|
||||||
@ -1531,12 +1571,11 @@ void handleMessage(GstElement * pipeline)
|
|||||||
//fprintf(stderr, "reached the end of the stream.");
|
//fprintf(stderr, "reached the end of the stream.");
|
||||||
break;
|
break;
|
||||||
case GST_MESSAGE_STREAM_STATUS:
|
case GST_MESSAGE_STREAM_STATUS:
|
||||||
|
|
||||||
gst_message_parse_stream_status(msg,&tp,&elem);
|
gst_message_parse_stream_status(msg,&tp,&elem);
|
||||||
//fprintf(stderr, "stream status: elem %s, %i\n", GST_ELEMENT_NAME(elem), tp);
|
//fprintf(stderr, "stream status: elem %s, %i\n", GST_ELEMENT_NAME(elem), tp);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
//fprintf(stderr, "unhandled message\n");
|
//fprintf(stderr, "unhandled message %s\n",GST_MESSAGE_TYPE_NAME(msg));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user