Changing filesink location after a mux with many inputs.

gagankumarnigam gagankumarnigam at
Wed Oct 14 03:03:58 PDT 2015

Hi amartin ,
I tried your solution but not getting any success. After compiling the code
i am getting error like that:
Now playing.........set state : 2 
gst_pad_add_buffer_probe : 2 

	INSIDE pad_probe_cb:2 newfile KEYframe

(test4:5576): GStreamer-CRITICAL **: gst_pad_add_event_probe_full: assertion
`GST_IS_PAD (pad)' failed

(test4:5576): GStreamer-CRITICAL **: gst_pad_send_event: assertion
`GST_IS_PAD (pad)' failed

Can u plz check what wrong i am doing here. Below is my code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gst/gst.h>
#include <glib.h>

#define true 1
#define false 0

GstElement *pipeline, *vsource,  *conv,  *acodec, *h264enc, *mux,*sink, *q1;
GstPad *q1_srcpad;
char name_buf[128];

GStaticRecMutex mutex;

static gulong probe_id;             // probe ID
static GstElement *elem_before;     // SRC of dynamic pipeline
static GstElement *elem_after;      // SINK of dynamic pipeline
static GstElement *elem_cur;        // Main element of dynamic pipeline
static GstPad *blockpad;            // SRC pad to be blocked
static GstPad *elem_cur_srcpad;     // SRC pad where check EOS
static GstPad *elem_cur_sinkpad;    // SINK of dynamic pipeline
static GstPad *elem_after_sinkpad;
static gboolean playing = FALSE;
static GstClockTime last_ts = 0;

typedef enum 
	NO_NEW_FILE, // Keep current file destination 
	NEW_FILE, // Switch file destination
} NewFileStatus;

static NewFileStatus newfile = NO_NEW_FILE;
int counter = 1;

static gboolean timeout_cb (gpointer user_data);

gboolean bus_call(GstBus *bus, GstMessage *msg, void *data) 
    gchar           *debug; 
    GError          *err; 
    GMainLoop       *loop = (GMainLoop*)data; 

    switch (GST_MESSAGE_TYPE(msg)) 
            g_print("APP received on OBJ NAME
        case GST_MESSAGE_EOS: 
            g_print("EOS received on OBJ NAME
            g_main_loop_quit (loop); 
        case GST_MESSAGE_ERROR: 
            gst_message_parse_error(msg, &err, &debug); 
            g_print("BUS CALL %s\n", err->message); 
            g_main_loop_quit (loop); 
    return TRUE; 

static void on_pad_added (GstElement *element, GstPad *pad, gpointer data)
    GstPad *sinkpad;
    GstElement *decoder = (GstElement *) data;    
    g_print ("Dynamic pad created, linking demuxer/decoder\n");
    sinkpad = gst_element_get_static_pad (decoder, "sink");
    gst_pad_link (pad, sinkpad);
    gst_object_unref (sinkpad);

int main (int argc, char *argv[])
    int ret = 0;
    GMainLoop *loop;

    GstBus *bus;
    pthread_t *thread;
    GError *err = NULL;

    GStaticRecMutex mut = G_STATIC_REC_MUTEX_INIT;
    mutex = mut;
	int counter =0;
    /* Initialisation */
    gst_init (&argc, &argv);
    loop = g_main_loop_new (NULL, FALSE);    

    pipeline = gst_pipeline_new ("re-recording");
    vsource = gst_element_factory_make ("v4l2src", "v4l-source");
    h264enc = gst_element_factory_make ("ffenc_mpeg4", "encode");   
    mux = gst_element_factory_make ("avimux", "muxer");
    sink = gst_element_factory_make ("filesink", "fsink");

    q1 = gst_element_factory_make ("queue", "q1");

    if (!pipeline || !vsource || !h264enc || !mux )
        g_print ("One element could not be created. Exiting.\n");
        return -1;

    g_object_set (G_OBJECT (sink), "location","test.avi", NULL);
    bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
    gst_bus_add_watch (bus, bus_call, loop);
    gst_object_unref (bus);

    gst_bin_add_many (GST_BIN (pipeline), vsource, h264enc, q1, mux,
    gst_element_link_many (vsource,h264enc, q1, mux, sink, NULL);
    g_signal_connect (q1, "pad-added", G_CALLBACK (on_pad_added), mux);

    g_print ("Now playing.........");
    ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
    g_print("set state : %d \n", ret);

    playing = TRUE;
    q1_srcpad =   gst_element_get_static_pad( q1, "src" ); 
    g_print ("Running...\n");

    g_main_loop_run (loop);
    g_print ("Returned, stopping playback\n");
    gst_element_set_state (pipeline, GST_STATE_NULL);
    g_print ("Deleting pipeline\n");
    gst_object_unref (GST_OBJECT (pipeline));
    return 0;
static gboolean event_probe_cb (GstPad * pad, GstEvent * event, gpointer
  g_print ("INSIDE event_probe_cb:%d type:%s\n",probe_id,

  if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)
    // Push the event in the pipe flow (false DROP)
    return TRUE;
  // remove the probe first
  gst_pad_remove_event_probe (pad, probe_id);

  gst_object_unref (elem_cur_srcpad);
  gst_object_unref (elem_after_sinkpad);
  gst_element_release_request_pad(elem_cur, elem_cur_sinkpad);

  gst_element_set_state (elem_cur, GST_STATE_NULL);
  gst_element_set_state (elem_after, GST_STATE_NULL);

  // remove unlinks automatically
  GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, elem_cur);
  gst_bin_remove (GST_BIN (pipeline), elem_cur);
  GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, elem_after);
  gst_bin_remove (GST_BIN (pipeline), elem_after);

  GstElement * mux0 = gst_element_factory_make("mp4mux", "mux0");
  GstElement * fsink0 = gst_element_factory_make("filesink", "fsink0");
  elem_cur = mux0;
  elem_after = fsink0;

  if(!mux0 || !fsink0)
    printf("mising elements\n");

  GST_DEBUG_OBJECT (pipeline, "adding   %" GST_PTR_FORMAT, elem_cur);
  gst_bin_add (GST_BIN (pipeline), elem_cur);
  GST_DEBUG_OBJECT (pipeline, "adding   %" GST_PTR_FORMAT, elem_after);
  gst_bin_add (GST_BIN (pipeline), elem_after);

  char buffer[128];
  sprintf(buffer, "test_%d.mp4", counter++);
  g_print ("File Switching %s\n", buffer);
  g_object_set(G_OBJECT(elem_after), "location", buffer, NULL);

  GST_DEBUG_OBJECT (pipeline, "linking..");
  elem_cur_srcpad = gst_element_get_static_pad (elem_cur, "src");
  elem_cur_sinkpad = gst_element_get_request_pad (elem_cur, "video_%d");
  elem_after_sinkpad = gst_element_get_static_pad (elem_after, "sink");

  if(gst_pad_link(blockpad, elem_cur_sinkpad) != GST_PAD_LINK_OK)
    printf("linking output 0 failed\n");
    return -1;
  if(gst_pad_link(elem_cur_srcpad, elem_after_sinkpad) != GST_PAD_LINK_OK)
    printf("linking output 1 failed\n");
    return -1;

  g_print ("Moving to PLAYING\n");
  gst_element_set_state (elem_cur, GST_STATE_PLAYING);
  gst_element_set_state (elem_after, GST_STATE_PLAYING);

  GST_DEBUG_OBJECT (pipeline, "done");

  newfile = NO_NEW_FILE;
  // Push the event in the pipe flow (false DROP)
  return TRUE;

// Check if Buffer contains a KEY FRAME
static gboolean is_sync_frame (GstBuffer * buffer)
    return FALSE;
    return TRUE;

// Block source and launch EOS to MUXER to achieve a full muxed file
static gboolean pad_probe_cb (GstPad * pad, GstBuffer * buffer, gpointer
  g_print ("\n\tINSIDE pad_probe_cb:%d %s %s\n",probe_id,
(newfile?"newfile":"thesame"),(is_sync_frame (buffer)?"KEYframe":"frame"));
  GST_DEBUG_OBJECT (pad, "pad is blocked now");

  last_ts = GST_BUFFER_TIMESTAMP(buffer);

  if((newfile==NO_NEW_FILE) || !is_sync_frame (buffer))
    return TRUE;

  gst_pad_remove_buffer_probe (pad, probe_id);

  probe_id = gst_pad_add_event_probe (elem_after_sinkpad,
G_CALLBACK(event_probe_cb), user_data);

  gst_pad_send_event (elem_cur_sinkpad, gst_event_new_eos ());

  while(newfile != NO_NEW_FILE)


  return TRUE;

static gboolean timeout_cb (gpointer user_data)
  g_print ("TIMEOUT\n");
      return false;
  newfile = NEW_FILE;
 probe_id = gst_pad_add_buffer_probe (q1_srcpad, G_CALLBACK(pad_probe_cb),
 g_print("gst_pad_add_buffer_probe : %d \n", probe_id );
  return true;

View this message in context:
Sent from the GStreamer-devel mailing list archive at

More information about the gstreamer-devel mailing list