Search code examples
gstreamer

GStreamer pre-recording


I'm trying to implement a pre-record I use a shared memory of 20 seconds as a circular buffer. I use shared memory as a circular buffer to permanently record video in it. When an event occurs, I want to write the entire buffer to the file, and then record the video for 40 seconds.

How can I instantly encode the video from shared memory and write to a file, and then continue to write from memory to the file for some time?


Solution

  • you can ask the gstreamer queue to do the pre-buffering as follows:

    g_object_set (G_OBJECT (queue), "max-size-bytes", 0, NULL);
    g_object_set (G_OBJECT (queue), "max-size-buffers", 0, NULL);
    g_object_set (G_OBJECT (queue),
              "max-size-time", (guint64)threshold_time,
              NULL);
    /* Drop old buffers when max-size-time is reached */
    g_object_set (G_OBJECT (queue), "leaky", 2, NULL);
    

    Install a callback on the pad of the queue:

    gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_BLOCK,
                       (GstPadProbeCallback) callback, NULL, NULL);
    

    Whenever you dont want pass the buffers return GST_PAD_PROBE_DROP in the callback, and when you want to pass the buffers return GST_PAD_PROBE_PASS

    have pipeline something as below:

    appsrc-- > queue --> encode --> mux --> filesink