Hi,<div><br></div><div>To fully understand the elements/pads structure the right schedule is:</div><div><br></div><div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">
// Pipeline -> src                    -> dynamic pipeline</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">// Pipeline -> capsfilter(f264file)   -> mp4mux(mux0)                                 -> filesink(fsink0)</div>
<div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">// Pipeline -> elem_BEFORE||blockpad| -> |elem_CUR_sinkpad||elem_CUR||elem_CUR_srcpad -> |elem_AFTER_sinkpad||elem_AFTER</div>
<div><br></div><div>Best,</div><div><br></div><div>Angel</div><br><div class="gmail_quote">2013/6/4 Angel Martin <span dir="ltr"><<a href="mailto:amartin@vicomtech.org" target="_blank">amartin@vicomtech.org</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Dear all,</div><div><br></div><div>I did not achieve to create full legible muxed files for Gstreamer 0.10 on top of multifilesink or output-selector.</div>
<div><br></div><div>After analysing lots of alternatives my solution takes as code base the example depicted in:</div>
<div><a href="http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/section-dynamic-pipelines.html" target="_blank">http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/section-dynamic-pipelines.html</a></div>

<div><br></div><div>The probes function API has been changed a bit but the solution below works to create every N seconds different MP4 files:</div><div><br></div><div><div>static GstElement *pipeline = NULL;</div><div><br>

</div><div>// Pipeline -> src                    -> dynamic pipeline</div><div>// Pipeline -> capsfilter(f264file)   -> mp4mux(mux0)                                 -> filesink(fsink0)</div><div>// Pipeline -> elem_before||blockpad| -> |elem_cur_sinkpad||elem_cur||elem_cur_srcpad -> |elem_cur_sinkpad||elem_cur</div>

<div>static gulong probe_id;<span style="white-space:pre-wrap">                           </span>// probe ID</div><div>static GstElement *elem_before;<span style="white-space:pre-wrap">               </span>// SRC of dynamic pipeline</div>
<div>static GstElement *elem_after;<span style="white-space:pre-wrap">            </span>// SINK of dynamic pipeline</div><div>static GstElement *elem_cur;<span style="white-space:pre-wrap">          </span>// Main element of dynamic pipeline</div>

<div>static GstPad *blockpad;<span style="white-space:pre-wrap">                  </span>// SRC pad to be blocked</div><div>static GstPad *elem_cur_srcpad;<span style="white-space:pre-wrap">          </span>// SRC pad where check EOS</div>
<div>static GstPad *elem_cur_sinkpad;<span style="white-space:pre-wrap">  </span>// SINK of dynamic pipeline</div><div>static GstPad *elem_after_sinkpad;  // SINK of SINK element</div><div><br></div><div>
// Last Buffer Timestamp</div><div>static GstClockTime last_ts = 0;</div><div><br></div><div>typedef enum {</div><div>  NO_NEW_FILE,<span style="white-space:pre-wrap">   </span>// Keep current file destination</div>
<div>  NEW_FILE,<span style="white-space:pre-wrap">               </span>// Switch file destination</div><div>} NewFileStatus;</div><div>static NewFileStatus newfile = NO_NEW_FILE; // Switch File Flag</div><div><br></div>
<div>static int counter = 1; // Index filename</div></div><div><br></div><div><div>// EOS listener to switch to other file destination</div><div>static gboolean</div><div>event_probe_cb (GstPad * pad, GstEvent * event, gpointer user_data)</div>

<div>{</div><div>  g_print ("INSIDE event_probe_cb:%d type:%s\n",probe_id,</div><div><span style="white-space:pre-wrap">        </span>  GST_EVENT_TYPE (event)==GST_EVENT_EOS?"EOS":GST_EVENT_TYPE (event)==GST_EVENT_NEWSEGMENT?"NEWSEGMENT":"OTHER");</div>

<div><br></div><div>  if (GST_EVENT_TYPE (event) != GST_EVENT_EOS)</div><div>  {</div><div>    // Push the event in the pipe flow (false DROP)</div><div>    return TRUE;</div><div>  }</div><div><span style="white-space:pre-wrap">       </span></div>

<div>  // remove the probe first</div><div>  gst_pad_remove_event_probe (pad, probe_id);</div><div><br></div><div>  gst_object_unref (elem_cur_srcpad);</div><div>  gst_object_unref (elem_after_sinkpad);</div><div>  gst_element_release_request_pad(elem_cur, elem_cur_sinkpad);</div>

<div><br></div><div>  gst_element_set_state (elem_cur, GST_STATE_NULL);</div><div>  gst_element_set_state (elem_after, GST_STATE_NULL);</div><div><br></div><div>  // remove unlinks automatically</div><div>  GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, elem_cur);</div>

<div>  gst_bin_remove (GST_BIN (pipeline), elem_cur);</div><div>  GST_DEBUG_OBJECT (pipeline, "removing %" GST_PTR_FORMAT, elem_after);</div><div>  gst_bin_remove (GST_BIN (pipeline), elem_after);</div><div><br>

</div><div>  GstElement * mux0 = gst_element_factory_make("mp4mux", "mux0");</div><div>  GstElement * fsink0 = gst_element_factory_make("filesink", "fsink0");</div><div>  elem_cur = mux0;</div>

<div>  elem_after = fsink0;</div><div><br></div><div>  if(!mux0 || !fsink0)</div><div>  {</div><div><span style="white-space:pre-wrap">     </span>printf("mising elements\n");</div><div>  }</div><div>
<br></div><div>  GST_DEBUG_OBJECT (pipeline, "adding   %" GST_PTR_FORMAT, elem_cur);</div><div>  gst_bin_add (GST_BIN (pipeline), elem_cur);</div><div>  GST_DEBUG_OBJECT (pipeline, "adding   %" GST_PTR_FORMAT, elem_after);</div>

<div>  gst_bin_add (GST_BIN (pipeline), elem_after);</div><div><br></div><div>  char buffer[128];</div><div>  sprintf(buffer, "test_%d.mp4", counter++);</div><div>  g_print ("File Switching %s\n", buffer);</div>

<div>  g_object_set(G_OBJECT(elem_after), "location", buffer, NULL);</div><div><br></div><div>  GST_DEBUG_OBJECT (pipeline, "linking..");</div><div>  elem_cur_srcpad = gst_element_get_static_pad (elem_cur, "src");</div>

<div>  elem_cur_sinkpad = gst_element_get_request_pad (elem_cur, "video_%d");</div><div>  elem_after_sinkpad = gst_element_get_static_pad (elem_after, "sink");</div><div><br></div><div>  if(gst_pad_link(blockpad, elem_cur_sinkpad) != GST_PAD_LINK_OK)</div>

<div>  {</div><div><span style="white-space:pre-wrap">        </span>printf("linking output 0 failed\n");</div><div><span style="white-space:pre-wrap">   </span>return -1;</div><div>  }</div>
<div>  if(gst_pad_link(elem_cur_srcpad, elem_after_sinkpad) != GST_PAD_LINK_OK)</div><div>  {</div><div><span style="white-space:pre-wrap">       </span>printf("linking output 1 failed\n");</div><div>
<span style="white-space:pre-wrap">     </span>return -1;</div><div>  }</div><div><br></div><div>  g_print ("Moving to PLAYING\n");</div><div>  gst_element_set_state (elem_cur, GST_STATE_PLAYING);</div>
<div>  gst_element_set_state (elem_after, GST_STATE_PLAYING);</div><div><br></div><div>  GST_DEBUG_OBJECT (pipeline, "done");</div><div><br></div><div>  newfile = NO_NEW_FILE;</div><div>  // Push the event in the pipe flow (false DROP)</div>

<div>  return TRUE;</div><div>}</div><div><br></div><div>// Check if Buffer contains a KEY FRAME</div><div>static gboolean</div><div>is_sync_frame (GstBuffer * buffer)</div><div>{</div><div>  if (GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT)) </div>

<div>  {</div><div>    return FALSE;</div><div>  }</div><div>  else if (!GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_IN_CAPS)) </div><div>  {</div><div>    return TRUE;</div><div>  }</div><div>}</div><div><br></div><div>

// Block source and launch EOS to MUXER to achieve a full muxed file</div><div>static gboolean</div><div>pad_probe_cb (GstPad * pad, GstBuffer * buffer, gpointer user_data)</div><div>{</div><div>  g_print ("\n\tINSIDE pad_probe_cb:%d %s %s\n",probe_id, (newfile?"newfile":"thesame"), </div>

<div><span style="white-space:pre-wrap">  </span>  (is_sync_frame (buffer)?"KEYframe":"frame"));</div><div>  GST_DEBUG_OBJECT (pad, "pad is blocked now");</div><div><br></div>
<div>  last_ts = GST_BUFFER_TIMESTAMP(buffer);</div><div>  if(!GST_CLOCK_TIME_IS_VALID(last_ts))</div><div><span style="white-space:pre-wrap">    </span>  last_ts=0;</div><div><br></div><div>  if((newfile==NO_NEW_FILE) || !is_sync_frame (buffer))</div>

<div><span style="white-space:pre-wrap">  </span>return TRUE;</div><div><br></div><div>  /* remove the probe first */</div><div>  gst_pad_remove_buffer_probe (pad, probe_id);</div><div><br></div><div>  /* install new probe for EOS */</div>

<div>  probe_id = gst_pad_add_event_probe (elem_after_sinkpad, G_CALLBACK(event_probe_cb), user_data);</div><div><br></div><div>  /* push EOS into the element, the probe will be fired when the</div><div>   * EOS leaves the effect and it has thus drained all of its data */</div>

<div>  gst_pad_send_event (elem_cur_sinkpad, gst_event_new_eos ());</div><div><br></div><div>  // Wait til the EOS have been processed the Buffer with the Key frame will be the FIRST</div><div>  while(newfile != NO_NEW_FILE)</div>

<div><span style="white-space:pre-wrap">  </span>  Sleep(1);</div><div><br></div><div>  // Push the buffer in the pipe flow (false DROP)</div><div>  return TRUE;</div><div>}</div><div><br></div><div>// this timeout is periodically run as part of the mainloop</div>

<div>static gboolean timeout (gpointer user_data)</div><div>{</div><div>  g_print ("TIMEOUT\n");</div><div>  if(!playing)</div><div><span style="white-space:pre-wrap"> </span>  return false;</div>
<div>  newfile = NEW_FILE;</div><div>  /* install new probe for Keyframe and New File */</div><div>  probe_id = gst_pad_add_buffer_probe (blockpad, G_CALLBACK(pad_probe_cb), pipeline);</div><div>  return true;</div><div>
}</div>
</div><div><br></div><div>Best,</div><div><br></div><div>Angel</div><div class="HOEnZb"><div class="h5"><div><br></div><div class="gmail_quote">2012/10/19 Paddy <span dir="ltr"><<a href="mailto:pat.blanchon@gmail.com" target="_blank">pat.blanchon@gmail.com</a>></span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Paddy wrote<br>
<div><div>> More progress ...<br>
><br>
> I've recovered the 1.3sec of missing video. This is what I now do when I<br>
> want to change the filesink location on the fly:<br>
> 1)  install/enable a handoff function on an identity element inserted<br>
> between the x264enc & videoQ elements which simply looks for keyframes<br>
> (see above pipeline).<br>
> 2)  when a keyframe is detected block both queue src pads & disable the<br>
> handoff<br>
> 3)  in the first blocked callback, unlink the mux/sink bin from both<br>
> queues & send an EOS event to the audio & video sink pads in the mux/sink<br>
> bin. I have to do this - without the EOS I never see the second callback.<br>
> I can also confirm that both EOS's find their way to the mux/sink bin - I<br>
> missed them in the yards of debug<br>
> 4)  on the second callback a new mux/sink bin is created, added & synced<br>
> to the pipeline & the queues are unblocked<br>
> 5)  the mux/file bin EOS handler function sets the retiring bin state to<br>
> NULL & removes it from the pipeline<br>
><br>
> Hopefully this may help others who want to try this.<br>
><br>
> Meanwhile I still have a couple of questions:<br>
><br>
> Is the missing second block callback expected behaviour or a bug ? I never<br>
> see the second callback unless I send the EOS event.<br>
><br>
> Bug or not: is the above procedure sound ? I'm new to gstreamer & don't<br>
> have much of a grip on its architecture, design & implementation; so<br>
> there's everything chance what I'm doing is fundamentally flawed.<br>
><br>
> Cheer again<br>
<br>
<br>
</div></div>Bumping this up so hopefully my questions can be considered.<br>
<br>
I'll dig some sample code out that shows the pertinent steps for those gents<br>
that have asked<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://gstreamer-devel.966125.n4.nabble.com/Changing-filesink-location-after-a-mux-with-many-inputs-tp4656116p4656654.html" target="_blank">http://gstreamer-devel.966125.n4.nabble.com/Changing-filesink-location-after-a-mux-with-many-inputs-tp4656116p4656654.html</a><br>


<div><div>Sent from the GStreamer-devel mailing list archive at Nabble.com.<br>
_______________________________________________<br>
gstreamer-devel mailing list<br>
<a href="mailto:gstreamer-devel@lists.freedesktop.org" target="_blank">gstreamer-devel@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/gstreamer-devel</a><br>
</div></div></blockquote></div><br>
</div></div></blockquote></div><br></div>