Part2: Output-Selectors and send EOS and Internal GSTREAMER errors...

Angel Martin amartin at vicomtech.org
Tue Jun 4 08:19:01 PDT 2013


Dear all,

I did not achieve to create full legible muxed files for Gstreamer 0.10 on
top of multifilesink or output-selector.

After analysing lots of alternatives my solution takes as code base the
example depicted in:
http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/section-dynamic-pipelines.html

The probes function API has been changed a bit but the solution below works
to create every N seconds different MP4 files:

static GstElement *pipeline = NULL;

// Pipeline -> src                    -> dynamic pipeline
// Pipeline -> capsfilter(f264file)   -> mp4mux(mux0)
          -> filesink(fsink0)
// Pipeline -> elem_before||blockpad| ->
|elem_cur_sinkpad||elem_cur||elem_cur_srcpad -> |elem_cur_sinkpad||elem_cur
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;  // SINK of SINK element

// Last Buffer Timestamp
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; // Switch File Flag

static int counter = 1; // Index filename

// EOS listener to switch to other file destination
static gboolean
event_probe_cb (GstPad * pad, GstEvent * event, gpointer user_data)
{
  g_print ("INSIDE event_probe_cb:%d type:%s\n",probe_id,
  GST_EVENT_TYPE (event)==GST_EVENT_EOS?"EOS":GST_EVENT_TYPE
(event)==GST_EVENT_NEWSEGMENT?"NEWSEGMENT":"OTHER");

  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)
{
  if (GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT))
  {
    return FALSE;
  }
  else if (!GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_IN_CAPS))
  {
    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 user_data)
{
  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(!GST_CLOCK_TIME_IS_VALID(last_ts))
  last_ts=0;

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

  /* remove the probe first */
  gst_pad_remove_buffer_probe (pad, probe_id);

  /* install new probe for EOS */
  probe_id = gst_pad_add_event_probe (elem_after_sinkpad,
G_CALLBACK(event_probe_cb), user_data);

  /* push EOS into the element, the probe will be fired when the
   * EOS leaves the effect and it has thus drained all of its data */
  gst_pad_send_event (elem_cur_sinkpad, gst_event_new_eos ());

  // Wait til the EOS have been processed the Buffer with the Key frame
will be the FIRST
  while(newfile != NO_NEW_FILE)
  Sleep(1);

  // Push the buffer in the pipe flow (false DROP)
  return TRUE;
}

// this timeout is periodically run as part of the mainloop
static gboolean timeout (gpointer user_data)
{
  g_print ("TIMEOUT\n");
  if(!playing)
  return false;
  newfile = NEW_FILE;
  /* install new probe for Keyframe and New File */
  probe_id = gst_pad_add_buffer_probe (blockpad, G_CALLBACK(pad_probe_cb),
pipeline);
  return true;
}

Best,

Angel

2012/12/1 somnyrds <tom.morrison at smith-nephew.com>

> So, this again is the basic idea: I have a c-program that 'sorta' works
> that
> can be reduced to this:
>
>        video_source->output-selector- - - - >video
> encoder->avimux->filesink
>                                                      \
>                                                       \
>                                                        v
>                                                      Fakesink
>
> The selector toggles every 5 seconds. I want to save each 5 second clip to
> a
> unique file.
> So, when the 5 second timer goes off, I send an EOS to video_source (to
> flush and fix
> the AVI file header).
>
>       gst_element_set_state(video_source, GST_STATE_NULL);
>
> My intention is to then wait until the EOS has actually occurred (which
> means its flushed
> and the file has been finalized) and then switch to the fakesink...
>
>   In MVC: mxc_v4l_open
>      device name is Mxc Camera
>   Exiting Pipeline1 because: Error (000149A0): Internal GStreamer error:
> negotiation problem.
>   Please file a bug at
> http://bugzilla.gnome.org/enter_bug.cgi?product=GStreamer.
>   Stopped Playing (EOS) - Set State
>   Now Cleanup/Delete pipeline
>
> This is with gstreamer-0.10.35 (as is part1)
>
> What could I be doing wrong?
>
> Thanks in advance
>
> Sincerely,
>
> Tom Morrison
>
>
>
>
> But, even before I get an EOS in the pipeline callback, I get
>  - so I see 5 seconds worth in the file, and then
> another 5 seconds (5 seconds later)...but, I am getting this:
>
>    GStreamer-CRITICAL **: gst_segment_set_newsegment_full: assertion
> `segment->format == format' failed
>
> Why am I getting this?
>
> Tom
>
> ps: Part (2) will be coming soon - and it involves switching filesink
> locations (filename) every time it switches to live video.
>
>
>
> --
> View this message in context:
> http://gstreamer-devel.966125.n4.nabble.com/Part2-Output-Selectors-and-send-EOS-and-Internal-GSTREAMER-errors-tp4657164.html
> Sent from the GStreamer-devel mailing list archive at Nabble.com.
> _______________________________________________
> gstreamer-devel mailing list
> gstreamer-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/gstreamer-devel/attachments/20130604/466ce1cc/attachment.html>


More information about the gstreamer-devel mailing list