Skip to content

Instantly share code, notes, and snippets.

@panitaxx
Created January 22, 2016 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panitaxx/2b1ee226e10a1645cdbe to your computer and use it in GitHub Desktop.
Save panitaxx/2b1ee226e10a1645cdbe to your computer and use it in GitHub Desktop.
gstreamer delay ?
#include <gst/gst.h>
#include <string.h>
GstElement *pipeline;
GMainLoop* loop;
GstBus *bus;
guint bus_watch_id;
GstElement *t2;
gboolean first=TRUE;
gboolean timeout_eos_cb(gpointer data)
{
gst_element_send_event (pipeline, gst_event_new_eos());
return FALSE;
}
gboolean timeout_cb(gpointer data)
{
GstElement *q,*enc,*mux,*fs;
q = gst_element_factory_make("queue",NULL);
enc = gst_element_factory_make("vp8enc",NULL);
mux = gst_element_factory_make("matroskamux",NULL);
fs = gst_element_factory_make("filesink",NULL);
g_object_set(G_OBJECT(fs),"location","test2.mkv",NULL);
gst_bin_add_many(GST_BIN(pipeline),q,enc,mux,fs,NULL);
gst_element_link_many(q,enc,mux,fs,NULL);
GstPad *pad = gst_element_get_request_pad (t2,"src_%u");
GstPad *pad2 = gst_element_get_static_pad(q,"sink");
g_assert(pad);
g_assert(gst_pad_link(pad,pad2) == GST_PAD_LINK_OK);
g_print ("Recording 2\n");
gst_element_sync_state_with_parent(fs);
gst_element_sync_state_with_parent(mux);
gst_element_sync_state_with_parent(enc);
gst_element_sync_state_with_parent(q);
g_timeout_add_seconds(60, timeout_eos_cb,NULL);
return FALSE;
}
static gboolean
bus_call (GstBus *bus,
GstMessage *msg,
gpointer data)
{
GMainLoop *loop = (GMainLoop *) data;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_EOS:
g_print ("End of stream\n");
g_main_loop_quit (loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error (msg, &error, &debug);
g_free (debug);
g_printerr ("Error: %s\n", error->message);
g_error_free (error);
g_main_loop_quit (loop);
break;
}
case GST_MESSAGE_STATE_CHANGED: {
GstState old_state, new_state;
gst_message_parse_state_changed (msg, &old_state, &new_state, NULL);
if(first && strcmp("pipeline0",GST_OBJECT_NAME (msg->src))==0 &&
new_state == GST_STATE_PLAYING)
{
first=FALSE;
g_print("adding timeout\n");
g_timeout_add_seconds(20, timeout_cb,NULL);
}
break;
}
default:
break;
}
return TRUE;
}
int main()
{
GError* err=NULL;
gst_init(NULL,NULL);
pipeline = gst_parse_launch ("videotestsrc ! clockoverlay ! tee name=t1 ! queue ! vp8enc ! matroskamux ! filesink location=test1.mkv",&err);
loop=g_main_loop_new(NULL,FALSE);
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
bus_watch_id = gst_bus_add_watch (bus, bus_call, loop);
gst_object_unref (bus);
GstElement *t1=gst_bin_get_by_name(GST_BIN(pipeline),"t1");
GstPad *pad = gst_element_get_request_pad (t1,"src_%u");
GstElement *q=gst_element_factory_make("queue",NULL);
GstElement *q2=gst_element_factory_make("queue",NULL);
GstPad *pad2 = gst_element_get_static_pad(q,"sink");
t2= gst_element_factory_make("tee",NULL);
GstElement *fake = gst_element_factory_make("fakesink",NULL);
gst_bin_add_many(GST_BIN(pipeline),q,q2,t2,fake,NULL);
gst_pad_link(pad,pad2);
gst_object_unref(pad);
gst_object_unref(pad2);
gst_element_link(q,t2);
pad = gst_element_get_request_pad (t2,"src_%u");
pad2 = gst_element_get_static_pad(q2,"sink");
gst_pad_link(pad,pad2);
gst_object_unref(pad);
gst_object_unref(pad2);
gst_element_link(q2,fake);
//this does not work
g_object_set(q,"min-threshold-time",10*GST_SECOND,NULL);
gst_element_set_state(pipeline,GST_STATE_PLAYING);
g_print ("Recording\n");
g_main_loop_run(loop);
gst_element_set_state(pipeline,GST_STATE_NULL);
gst_object_unref (GST_OBJECT (pipeline));
g_source_remove (bus_watch_id);
g_main_loop_unref (loop);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment