Gstreamer: trickplay mode in rtsp-server

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Gstreamer: trickplay mode in rtsp-server

Igor

I need to implement trickplay mode in rtsp-server by sending seek-event to
an GstElement. Pipeline: (appsrc name=vsrc !h264parse ! rtph264pay pt=96
name=pay0)

But if i send seek-event to any of 3 GstElements - function
gst_element_send_event return 0, so it doesn't work.

What am I doing wrong? Or is there any another approach to implement
trickplay mode on 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;
   }
}





--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: Gstreamer: trickplay mode in rtsp-server

Igor
Reply | Threaded
Open this post in threaded view
|

Re: Gstreamer: trickplay mode in rtsp-server

Igor
Up. Really need to implement fast playback in rtsp-server.



--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: Gstreamer: trickplay mode in rtsp-server

Igor
Now I send seek-event into appsrc, also I set :
 gst_util_set_object_arg(G_OBJECT(vsrc), "stream-type", "seekable");

, connected seek-data:
g_signal_connect(vsrc, "seek-data", G_CALLBACK(seek_data), NULL);


but still gst_element_send_event(pAppsrc, seek_event); returns 0.

What am I doing wrong to implement fast playback in rtsp-server?

Thanks!





--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: Gstreamer: trickplay mode in rtsp-server

Igor
In reply to this post by Igor
Now I send seek-event into appsrc, also I set :
 gst_util_set_object_arg(G_OBJECT(vsrc), "stream-type", "seekable");

, connected seek-data:
g_signal_connect(vsrc, "seek-data", G_CALLBACK(seek_data), NULL);


but still gst_element_send_event(pAppsrc, seek_event); returns 0.

What am I doing wrong to implement fast playback in rtsp-server?

Thanks!





--
Sent from: http://gstreamer-devel.966125.n4.nabble.com/
_______________________________________________
gstreamer-devel mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
Reply | Threaded
Open this post in threaded view
|

Re: Gstreamer: trickplay mode in rtsp-server

Igor
Here working example of trickplay mode for rtsp-server. Thanks to Olivier
CrĂȘte for suggestions!

seek-event should be passed into MediaElement, and appsrc should be seekable
with seek-data connected.


///////////////////////////////////////////////////////////////////////
#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;
    }
}
 



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