命令实现:
gst-launch-1.0 v4l2src device=/dev/videoX ! autovideosink
代码实现:
#include <gst/gst.h>
#define VIDEODEV "/dev/videox"
int main(int argc, char** argv)
{
//Initialize GStreamer
gst_init(&argc, &argv);
//Create the GST pipeline
GstElement* pipeline = gst_pipeline_new("video-pipeline");
GstElement* v4l2src = gst_element_factory_make("v4l2src", "source");
GstElement* autovideosink = gst_element_factory_make("autovideosink", "sink");
if(!pipeline || !v4l2src || !autovideosink){
g_printerr("One or more elements could't be created. Exiting\n");
return -1;
}
//Set the device for v4l2src
g_object_set(G_OBJECT(v4l2src), "device", VIDEODEV, NULL);
//Add elements to the pipeline
gst_bin_add(GST_BIN(pipeline), v4l2src);
gst_bin_add(GST_BIN(pipeline), autovideosink);
//Link
if(!gst_element_link(v4l2src, autovideosink)){
g_printerr("Link err. Exiting\n");
return -1;
}
//Set pipeline state to Playing
gst_element_set_state(pipeline, GST_STATE_PLAYING);
//Start the GMainLoop for handling events
GMainLoop* loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
//Free
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(GST_OBJECT(pipeline));
g_main_loop_unref(loop);
return 0;
}
gst转opencv并输出:
#include <gst/gst.h>
#include <gst/app/gstappsink.h>
#include <opencv2/opencv.hpp>
#define CVSHOW 1
#define WIDTH 1920
#define HEIGHT 1080
int main(int argc, char** argv)
{
gst_init(&argc, &argv);
//pipeline
const gchar* pipeline_str = "v4l2src device=/dev/video0 ! videoconvert ! appsink name=sink";
GError* error = NULL;
//pipeline create
GstElement* pipeline = gst_parse_launch(pipeline_str, &error);
if(error != NULL){
g_printerr("Error creating pipeline: %s\n", error->message);
g_clear_error(&error);
return -1;
}
//Get appsink element
GstElement* appsink = gst_bin_get_by_name(GST_BIN(pipeline), "sink");
if(!appsink){
g_printerr("Error creating appsink: %s\n", error->message);
gst_object_unref(pipeline);
return -1;
}
//Set pipeline to playing state
if(gst_element_set_state(pipeline, GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE)
{
g_printerr("Fail to start pipeline\n");
gst_object_unref(pipeline);
gst_object_unref(appsink);
return -1;
}
//Main loop
GstSample* sample;
GstBuffer* buffer;
GstMapInfo map;
cv::Mat frame;
while(1){
//Wait for sample
g_signal_emit_by_name(appsink, "pull-sample", &sample);
if(sample == NULL){
g_printerr("Fail to get sample\n");
break;
}
//get buffer from sample
buffer = gst_sample_get_buffer(sample);
if(!buffer){
g_printerr("Failed to get buffer\n");
gst_sample_unref(sample);
break;
}
//Map buffer to read data
if(!gst_buffer_map(buffer, &map, GST_MAP_READ)){
g_printerr("Failed to map buffer.\n");
gst_sample_unref(sample);
break;
}
//convert to Opencv Mat
frame = cv::Mat(HEIGHT, WIDTH, CV_8UC3, (char*)map.data);
#if CVSHOW
cv::imshow("Frame", frame);
cv::waitKey(1);
#endif
//free
gst_buffer_unmap(buffer, &map);
gst_sample_unref(sample);
}
if(gst_element_set_state(pipeline, GST_STATE_NULL) == GST_STATE_CHANGE_FAILURE){
g_printerr("Failed to stop pipeline\n");
}
//Clean
gst_element_set_state(pipeline, GST_STATE_NULL);
gst_object_unref(pipeline);
}