使用gstreamer将RSTP网络摄像头帧传输到openCV

时间:2018-04-05 06:33:21

标签: c++ opencv gstreamer webcam rtsp

我想编写一个C ++程序,用gstreamer获取RTSP流帧并将其放在openCV mat中。以下是我的代码。尝试从appsink中获取样本时出错。我突出了代码步骤粗体。

您是否知道如何从appsink获取样本?

提前致谢。

#include <gst/gst.h>
#include <opencv2/opencv.hpp>
#include <gstappsink.h>
#ifdef HAVE_GTK
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#endif
#include <stdlib.h>

int main (int argc, char *argv[])
{
    GstElement *pipeline;
    gint width, height;
    gchar *descr;
    GError *error = NULL;
    gint64 duration, position;
    GstStateChangeReturn ret;
    gboolean res;
    GstBus *bus;
    GstMessage *msg;

    gst_init (0, NULL);
    pipeline = gst_parse_launch ("rtspsrc location=rtsp://admin:admin@192.168.1.109:554/stream1 latency=0 ! decodebin ! appsink name=sink ",NULL);

    GstElement *sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
    if(!sink){
        printf("sink is NULL\n");
        exit(1);
    }

    GstAppSink *appsink = GST_APP_SINK(sink);
    if(!appsink){
        printf("appsink is NULL\n");
        exit(1);
    }

   **GstSample *sample = gst_app_sink_pull_sample(appsink);**
    if(!sample){
        printf("sample is NULL\n");
        exit(1);
    }

    GstBuffer *buffer = gst_sample_get_buffer(sample);
    GstMapInfo map;

    gst_buffer_map (buffer, &map, GST_MAP_READ);

    cv::Mat frame(cv::Size(320, 240), CV_8UC3, (char*)map.data, cv::Mat::AUTO_STEP);

    imwrite("XYZ.jpg",frame);
}

0 个答案:

没有答案