Gstreamer: trickplay mode in rtsp-server

Igor igor.vl.bondarenko at gmail.com
Tue Sep 4 10:08:22 UTC 2018


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/


More information about the gstreamer-devel mailing list