Pipeline OpenCV Mat (which includes 8 bit RGB data from IP camera) to GStreamer

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Pipeline OpenCV Mat (which includes 8 bit RGB data from IP camera) to GStreamer

Rashmi
Hello,

Please check whether the following code is correct as I'm new to GStreamer.
I'm trying to create a pipeline from the data obtained by IP camera through
OpenCV which is present in cv::Mat (Mat includes 8bit RGB data of the
camera) and stream it through udp by encoding it using VP8.
Please provide inputs with explanations so that I can understand as I'm
learning GStreamer.


//destroy the cv::Mat buffer
void buffer_destroy(gpointer data) {
  cv::Mat* done = (cv::Mat*)data;
  delete done;
}

//create pipeline from cv::Mat
int createGStreamerPipeline ( cv::Mat *frame )
{
        GstElement *pipeline, *source, *filter, *encode, *pay, *udp;
        GstAppSrc *appsrc;
        GstCaps *filtercaps;
        GstBus *bus;
        GstMessage *msg;
        GMainLoop *loop;
        GstStateChangeReturn ret;
        static GstClockTime timestamp = 0;
        GstBuffer *buffer;
        int buffer_size;
        GstFlowReturn flowRet;

        //initialize all elements
// gst_init (&argc, &argv);
        pipeline = gst_pipeline_new ("pipeline");
// source = gst_element_factory_make ("v4l2src", "source");
        source = gst_element_factory_make ( "appsrc", "source" );
        filter = gst_element_factory_make ("capsfilter", "filter");
        encode = gst_element_factory_make ("vp8enc", "encode");
        pay = gst_element_factory_make ("rtpvp8pay", "pay");
        udp = gst_element_factory_make ("udpsink", "udp");

        //creating source
        /*buffer = gst_buffer_new_and_alloc ( ( gsize ) frame.size( ) );
        GST_BUFFER_TIMESTAMP (buffer) = timestamp;
        GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale_int (frame.size( ),
GST_SECOND, 2);
        timestamp += GST_BUFFER_DURATION (buffer);
        g_signal_emit_by_name (frame.data->appsrc, "push-buffer", buffer,
&flowRet);
        gst_buffer_unref ( buffer );
        frame.data->appsrc = source; */
       
        buffer_size = frame->cols * frame->rows * frame->channels( );
        buffer = gst_buffer_new_wrapped_full( (GstMemoryFlags)0,
(gpointer)(frame->data), buffer_size, 0, buffer_size, frame, buffer_destroy
);
        gst_app_src_push_buffer ( GST_APP_SRC ( appsrc ), buffer);

        //check for null objects
        if (!pipeline || !source || !filter || !encode || !pay || !udp) {
                std::cout << "Failed to create all elements :" << std::endl;
                std::cout << " pipeline : " << !pipeline << std::endl;
                std::cout << "source : " << !source << std::endl;
                std::cout << "filter : " << !filter << std::endl;
                std::cout << "encode : " << !encode << std::endl;
                std::cout << "pay : " << !pay << std::endl;
                std::cout << "udp : " << !udp << std::endl;
                return -1;
        }

        //set video source
// g_object_set (G_OBJECT (source), "device", "/dev/video1", NULL);
        std::cout << "==>set video source." << std::endl;
        g_object_set (G_OBJECT (udp), "host", "10.0.0.6", NULL);
        g_object_set (G_OBJECT (udp), "port", 5004, NULL);
        std::cout << "==>set video sink." << std::endl;
        filtercaps = gst_caps_new_simple ("video/x-raw",
                        "format", G_TYPE_STRING, "RGB",
                        "width", G_TYPE_INT, 640,
                        "height", G_TYPE_INT, 480,
                        "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
                        "framerate", GST_TYPE_FRACTION, 30, 1,
                        NULL);
        g_object_set (G_OBJECT (filter), "caps", filtercaps, NULL);
        gst_caps_unref (filtercaps);

        //add all elements together
        gst_bin_add_many (GST_BIN (pipeline), source, filter, encode, pay, udp,
NULL);
        if (gst_element_link_many (source, filter, encode, pay, udp, NULL) != TRUE
) {
                std::cout << "Elements could not be linked." << std::endl;
          gst_object_unref (pipeline);
          return -1;
        }

        //set the pipeline state to playing
        ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
        if (ret == GST_STATE_CHANGE_FAILURE) {
                std::cout << "Unable to set the pipeline to the playing state." <<
std::endl;
                gst_object_unref (pipeline);
                return -1;
        }
        std::cout << "==>set video to play." << std::endl;

        //get pipeline's bus
        bus = gst_element_get_bus (pipeline);
        std::cout << "==>setup bus." << std::endl;

        loop = g_main_loop_new (NULL, FALSE);
        std::cout << "==>begin stream." << std::endl;
        g_main_loop_run (loop);

        g_main_loop_unref (loop);
        gst_object_unref (bus);
        gst_element_set_state (pipeline, GST_STATE_NULL);
        gst_object_unref (pipeline);

}



--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel