Gstreamer:rtsp-server中的特技播放模式

问题描述 投票:0回答:1

我需要通过向GstElement发送seek-event来在rtsp-server中实现特技播放模式。管道:( appsrc name = vsrc!h264parse!rtph264pay pt = 96 name = pay0)

但是如果我向3个GstElements中的任何一个发送seek-event - 函数gst_element_send_event返回0,那么它不起作用。

我究竟做错了什么?或者还有其他方法在rtsp-server上实现特技播放模式吗?

#include <gst/gst.h>

#include <gst/rtsp-server/rtsp-server.h>

#include <string>
#include <fstream>


static GstElement *pMy = NULL;
static GstElement *pMy2 = NULL;

static gboolean timeout(GstRTSPServer * server)
{
   GstRTSPSessionPool *pool;

   pool = gst_rtsp_server_get_session_pool(server);
   gst_rtsp_session_pool_cleanup(pool);
   g_object_unref(pool);

   return TRUE;
}

static void onNeedVideoData(GstElement * appsrc)
{
   static int NN = 0;
   ++NN;

   int Size = sFileSize(NN);
   GstBuffer* buf = gst_buffer_new_and_alloc(Size);

   GstMapInfo map;
   gst_buffer_map(buf, &map, GST_MAP_WRITE);

   FILE *fp = fopen(std::string("C:\\rtsp_files\\body" +    std::to_string(NN) + ".bin").c_str(), "rb");
   fread(map.data, sizeof(unsigned char), Size, fp);
   fclose(fp);

  gst_buffer_unmap(buf, &map);


  //in random moment we send seek-event to some GstElement
  if (NN % 300 == 0){
    double dspeed = 4.;
    gint64 position;


    if (!gst_element_query_position(pMy, GST_FORMAT_TIME, &position)) {
        g_printerr("Unable to retrieve current position.\n");
        return;
    }

    GstEvent * seek_event = gst_event_new_seek(dspeed, GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
        GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_NONE, 0);

    auto res1 = gst_element_send_event(pMy2, seek_event);
    g_print("%d\n", res1);
}

GstFlowReturn ret;
g_signal_emit_by_name(appsrc, "push-buffer", buf, &ret);

gst_buffer_unref(buf);
}

 static void need_video_data(GstElement * appsrc, guint unused) 
{
   onNeedVideoData(appsrc);
}


static void media_constructed(GstRTSPMediaFactory * factory, GstRTSPMedia * media)
{
   GstElement* element = pMy = gst_rtsp_media_get_element(media);
   GstElement* vsrc = gst_bin_get_by_name_recurse_up(GST_BIN(element), "vsrc");     

   g_signal_connect(vsrc, "need-data", (GCallback)need_video_data, NULL);

   pMy2 = gst_bin_get_by_name_recurse_up(GST_BIN(element), "h264parse0");
}


int main(int argc, char *argv[]) 
{
   GMainLoop *loop;
   GstRTSPServer *server;
   GstRTSPMountPoints *mounts;
   GstRTSPMediaFactory *factory;


   gst_init(&argc, &argv);

   loop = g_main_loop_new(NULL, FALSE);

   /* create a server instance */
   server = gst_rtsp_server_new();


   /* get the mount points for this server, every server has a default object
   * that be used to map uri mount points to media factories */
   mounts = gst_rtsp_server_get_mount_points(server);

/* make a media factory for a test stream. The default media factory can use
* gst-launch syntax to create pipelines.
* any launch line works as long as it contains elements named pay%d. Each
* element with pay%d names will be a stream */
   factory = gst_rtsp_media_factory_new();
   gst_rtsp_media_factory_set_launch(factory, "( "
    "appsrc name=vsrc  !"
    "h264parse ! rtph264pay pt=96 name=pay0  )");

   gst_rtsp_media_factory_set_shared(factory, TRUE);

   g_signal_connect(factory, "media-constructed", (GCallback)
    media_constructed, 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 mapper anymore */
   g_object_unref(mounts);

   /* attach the server to the default maincontext */
   if (gst_rtsp_server_attach(server, NULL) == 0)
    goto failed;

   /* add a timeout for the session cleanup */
   g_timeout_add_seconds(2, (GSourceFunc)timeout, server);

   g_print("stream ready at rtsp://127.0.0.1:8554/test\n");

   g_main_loop_run(loop);

   return 0;

/* ERRORS */
   failed:
   {
      g_print("failed to attach the server\n");
      return -1;
   }
}
c++ gstreamer rtsp
1个回答
1
投票

我解决了自己的问题:

#include <gst/gst.h>

#include <gst/rtsp-server/rtsp-server.h>

#include <string>
#include <fstream>


static GstElement *pMediaElement = NULL;

/* this timeout is periodically run to clean up the expired sessions from the
* pool. This needs to be run explicitly currently but might be done
* automatically as part of the mainloop. */
static gboolean
timeout(GstRTSPServer * server)
{
GstRTSPSessionPool *pool;

pool = gst_rtsp_server_get_session_pool(server);
gst_rtsp_session_pool_cleanup(pool);
g_object_unref(pool);

return TRUE;
}


static int sFileSize(const std::string &filename)
{
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
return in.tellg();
}


static void onNeedVideoData(GstElement * appsrc)
{
static int NN = 0;
++NN;


std::string filename = "C:\\rtsp_files\\body" + std::to_string(NN) + ".bin";

int Size = sFileSize(filename);
GstBuffer* buf = gst_buffer_new_and_alloc(Size);

GstMapInfo map;
gst_buffer_map(buf, &map, GST_MAP_WRITE);

FILE *fp = fopen(filename.c_str(), "rb");
fread(map.data, sizeof(unsigned char), Size, fp);
fclose(fp);

gst_buffer_unmap(buf, &map);


//in random moment we send seek-event to MediaElement
if (NN == 300){
    gint64 position;

    if (!gst_element_query_position(pMediaElement, GST_FORMAT_TIME, &position)) {
        g_printerr("Unable to retrieve current position.\n");
        return;
    }

    GstEvent * seek_event = gst_event_new_seek(4., GST_FORMAT_TIME, (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE),
        GST_SEEK_TYPE_SET, position, GST_SEEK_TYPE_NONE, 0);

    auto res = gst_element_send_event(pMediaElement, seek_event);
    g_print("%d\n", res);
}

GstFlowReturn ret;
g_signal_emit_by_name(appsrc, "push-buffer", buf, &ret);

gst_buffer_unref(buf);
}

static void need_video_data(GstElement * appsrc, guint unused)
{
onNeedVideoData(appsrc);
}

/* called when appsrc wants us to return data from a new position with the next
* call to push-buffer. */
static gboolean seek_data(GstElement * appsrc, guint64 position)
{
g_print("seek_data call\n");
//GST_DEBUG("seek to offset %" G_GUINT64_FORMAT, position);
//app->offset = position;

return TRUE;
}

static void media_constructed(GstRTSPMediaFactory * factory, GstRTSPMedia * media)
{
GstElement* element = pMediaElement = gst_rtsp_media_get_element(media);
GstElement* vsrc = gst_bin_get_by_name_recurse_up(GST_BIN(element), "vsrc");     

gst_util_set_object_arg(G_OBJECT(vsrc), "stream-type", "seekable");
g_signal_connect(vsrc, "need-data", (GCallback)need_video_data, NULL);
g_signal_connect(vsrc, "seek-data", G_CALLBACK(seek_data), NULL);
}


int main(int argc, char *argv[])
{
GMainLoop *loop;
GstRTSPServer *server;
GstRTSPMountPoints *mounts;
GstRTSPMediaFactory *factory;


gst_init(&argc, &argv);

loop = g_main_loop_new(NULL, FALSE);

/* create a server instance */
server = gst_rtsp_server_new();


/* get the mount points for this server, every server has a default object
* that be used to map uri mount points to media factories */
mounts = gst_rtsp_server_get_mount_points(server);

/* make a media factory for a test stream. The default media factory can use
* gst-launch syntax to create pipelines.
* any launch line works as long as it contains elements named pay%d. Each
* element with pay%d names will be a stream */
factory = gst_rtsp_media_factory_new();
gst_rtsp_media_factory_set_launch(factory, "( "
    "appsrc name=vsrc  !"
    "h264parse config-interval=1 ! rtph264pay pt=96 name=pay0  )");

gst_rtsp_media_factory_set_shared(factory, TRUE);

g_signal_connect(factory, "media-constructed", (GCallback)
    media_constructed, 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 mapper anymore */
g_object_unref(mounts);

/* attach the server to the default maincontext */
if (gst_rtsp_server_attach(server, NULL) == 0)
    goto failed;

/* add a timeout for the session cleanup */
g_timeout_add_seconds(2, (GSourceFunc)timeout, server);

/* start serving, this never stops */

g_print("stream ready at rtsp://127.0.0.1:8554/test\n");

g_main_loop_run(loop);

return 0;

/* ERRORS */
failed:
{
    g_print("failed to attach the server\n");
    return -1;
}
}
© www.soinside.com 2019 - 2024. All rights reserved.