Problem in accessing the stream of OpenCV appsrc for Gstremer RTSP - Server
I'm working to create a OpenCV appsrc for Gstreamer RTSP-SERVER and the code is able to open the camera and push the buffer when the client requests for stream but I'm not able to view it in VLC media player. Hereby I attach my code, kindly help me in this regards
#include <gstreamer-1.0/gst/rtsp-server/rtsp-server.h>
#include <gstreamer-1.0/gst/app/app.h>
#include <gstreamer-1.0/gst/gst.h>
#include <opencv4/opencv2/opencv.hpp>
#include <bits/stdc++.h>
//need data generates the data from the opencv video capture
static void
need_data(GstElement *appsrc, guint unused){
static GstClockTime timestamp=0;
GstBuffer *buffer;
GstFlowReturn ret;
g_print("video capturing activated");
cv::VideoCapture cap(0);
//setting the frame width and height
cap.set(cv::CAP_PROP_FRAME_WIDTH, 640);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
while(cap.isOpened()){
cv::Mat frame;
cap.read(frame);
guint size = frame.cols * frame.rows * frame.channels();
buffer = gst_buffer_new_allocate(NULL, size, NULL);
GstMapInfo map;
gst_buffer_map(buffer, &map, GST_MAP_WRITE);
memcpy(map.data, frame.data, gst_buffer_get_size( buffer ));
GST_BUFFER_PTS(buffer) = timestamp;
GST_BUFFER_DURATION(buffer) = gst_util_uint64_scale_int (1, GST_SECOND, 2);
timestamp += GST_BUFFER_DURATION(buffer);
g_signal_emit_by_name(appsrc, "push-buffer", buffer, &ret);
}
cap.release();
g_print("The capture stopped");
}
//media configuration handles the "appsrc" creation and data handling
static void
media_configure(GstRTSPMediaFactory *factory, GstRTSPMedia *media){
GstElement *element, *appsrc;
element = gst_rtsp_media_get_element(media);
appsrc = gst_bin_get_by_name_recurse_up(GST_BIN(element), "mysrc");
gst_util_set_object_arg(G_OBJECT(appsrc), "format", "time");
g_object_set(G_OBJECT(appsrc), "caps",
gst_caps_new_simple("video/x-raw",
"format",G_TYPE_STRING,"BGR",
"width",G_TYPE_INT,"640",
"height",G_TYPE_INT,"480",
"framerate",GST_TYPE_FRACTION, 1, 1, NULL), NULL);
g_print("client requested for stream\n");
g_signal_connect(appsrc, "need-data", G_CALLBACK(need_data),NULL);
}
int main(){
GMainLoop *loop;
GstRTSPServer *server;
GstRTSPMountPoints *mounts;
GstRTSPMediaFactory *factory;
gst_init(NULL, NULL);
loop = g_main_loop_new(NULL, FALSE);
server = gst_rtsp_server_new();
mounts = gst_rtsp_server_get_mount_points(server);
factory = gst_rtsp_media_factory_new ();
gst_rtsp_media_factory_set_launch (factory,
"( appsrc name=mysrc is-live=true block=true formate=GST_FORMAT_TIME ! videoconvert ! video/x-raw,format=I420 ! x264enc ! rtph264pay name=pay0 pt=96 ) ! debug name=debug");
/* notify when our media is ready, This is called whenever someone asks for
* the media and a new pipeline with our appsrc is created */
g_signal_connect (factory, "media-configure", (GCallback) media_configure,
NULL);
/* attach the test factory to the /test url */
gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
/* don't need the ref to the mounts anymore */
g_object_unref (mounts);
/* attach the server to the default maincontext */
gst_rtsp_server_attach (server, NULL);
/* start serving */
g_print ("stream ready at rtsp://127.0.0.1:8554/test\n");
g_main_loop_run (loop);
return 0;
}