I want to add some opencv processes to the gstreamer pipeline and then send it via udpsink.
I can read frames from gstreamer as follows:
// may add some plugins to the pipeline later cv::VideoCapture cap("v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! videoconvert ! appsink"); cv::Mat frame; while(ture){ cap >> frame; // do some processing to the frame }
But I canβt understand how to transfer the processed frame to the following pipeline: appsrc ! x264enc ! mpegtsmux ! udpsink host=localhost port=5000 appsrc ! x264enc ! mpegtsmux ! udpsink host=localhost port=5000
I tried
cv::VideoWriter writer = cv::VideoWriter("appsrc ! x264enc ! mpegtsmux ! udpsink host=localhost port=5000", 0, (double)30, cv::Size(640, 480), true); writer << processedFrame;
However, the recipient side does not receive anything. (I am using the $gst-launch-1.0 udpsrc port=5000 ! tsparse ! tsdemux ! h264parse ! avdec_h264 ! videoconvert ! ximagesink sync=false as the receiver)
My question is: can I transfer the processed opencv Mat to the gstreamer pipeline and let it do some encoding and then send it over the network via udpsink? If so, how do I achieve this?
Side question, is there a way to debug VideoWriter? For example, checking whether frames are actually written to it.
Please note that I am using opencv 2.4.12 and gstreamer 1.2 on ubuntu 14.04.
Any help is great!
EDIT: To provide more information, I checked the following code and gave GStreamer Plugin: Embedded video playback halted; module appsrc0 reported: Internal data flow error. GStreamer Plugin: Embedded video playback halted; module appsrc0 reported: Internal data flow error.
#include <stdio.h> #include <opencv2/highgui/highgui.hpp> #include <opencv2/opencv.hpp> int main(int argc, char *argv[]){ cv::VideoCapture cap("v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! videoconvert ! appsink"); if (!cap.isOpened()) { printf("=ERR= can't create capture\n"); return -1; } cv::VideoWriter writer; // problem here writer.open("appsrc ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! autovideoconvert ! ximagesink sync=false", 0, (double)30, cv::Size(640, 480), true); if (!writer.isOpened()) { printf("=ERR= can't create writer\n"); return -1; } cv::Mat frame; int key; while (true) { cap >> frame; if (frame.empty()) { printf("no frame\n"); break; } writer << frame; key = cv::waitKey( 30 ); } cv::destroyWindow( "video" ); }
There seems to be something wrong with the appsrc pipeline, but I have no idea what went wrong, because the pipeline is gst-launch-1.0 v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! videoconvert ! ximagesink sync=false gst-launch-1.0 v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! videoconvert ! ximagesink sync=false gst-launch-1.0 v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! videoconvert ! ximagesink sync=false works fine.