Skip to content

Instantly share code, notes, and snippets.

@tetkuz
Created May 10, 2016 11:44
Show Gist options
  • Save tetkuz/1af46c2f8599a2855ed3fddc78ff8bc3 to your computer and use it in GitHub Desktop.
Save tetkuz/1af46c2f8599a2855ed3fddc78ff8bc3 to your computer and use it in GitHub Desktop.
Simple seek tester
#include <stdio.h>
#include <signal.h>
#include <gst/gst.h>
#include <gst/video/gstvideodecoder.h>
GstElement *pipeline;
GstElement *identity;
GstElement *decoder;
GArray *garray;
static void sigint_handler (int arg)
{
GstQuery *query;
GstEvent *seek;
gboolean res;
gint64 cur;
(void)arg;
/* query current potision */
query = gst_query_new_position (GST_FORMAT_TIME);
res = gst_element_query (pipeline, query);
if (res) {
gst_query_parse_position (query, NULL, &cur);
g_print ("current position = %"GST_TIME_FORMAT"\n", GST_TIME_ARGS (cur));
} else {
g_print ("position query failed...\n");
}
g_print ("seek to: ");
scanf("%u", &cur);
/* simple seek to -0.5 sec position */
seek = gst_event_new_seek ((gdouble)1.0, GST_FORMAT_TIME,
GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE,
/* start */ GST_SEEK_TYPE_SET, cur,
/* stop */ GST_SEEK_TYPE_SET, GST_CLOCK_TIME_NONE);
if (seek) {
gdouble rate;
GstFormat format;
GstSeekFlags flags;
GstSeekType start_type;
gint64 start;
GstSeekType stop_type;
gint64 stop;
gst_event_parse_seek (
seek, &rate, &format, &flags, &start_type, &start, &stop_type, &stop);
g_print ("seek position = %"GST_TIME_FORMAT, GST_TIME_ARGS (start));
/* do seek */
gst_element_send_event (pipeline, seek);
}
gst_query_unref (query);
}
void handoff_cb (GstElement* object, GstBuffer* arg0, gpointer user_data)
{
static int counter = 0;
guint i, array_size, buffer_size, f=0;
GstSegment *inseg;
GstSegment *outseg;
GstVideoDecoder *dec = GST_VIDEO_DECODER(decoder);
(void)object;
(void)arg0;
(void)user_data;
buffer_size = gst_buffer_get_size (arg0);
g_print ("tamura: %03d Received new data of size %u pts: %u\n", counter++, buffer_size, GST_BUFFER_PTS(arg0));
for (i = 0; i < garray->len; i++) {
if (g_array_index(garray, guint, i) == buffer_size) {
g_print (" tamura: same buffer size [%u]: %u \n", i, buffer_size);
f=1;
break;
}
}
if (f==0) {
g_array_append_val(garray, buffer_size);
/* for (i = 0; i < garray->len; i++) g_print (" tamura: buffer list [%u]: %u \n", i, g_array_index(garray, guint, i)); */
} else {
f=0;
}
inseg = gst_segment_copy(&dec->input_segment);
outseg = gst_segment_copy(&dec->output_segment);
if (inseg) {
g_print("in start %" G_GUINT64_FORMAT "\n", inseg->start);
g_print("in end %" G_GUINT64_FORMAT "\n", inseg->stop);
g_print("in pos %" G_GUINT64_FORMAT "\n", inseg->position);
g_print("in dur %" G_GUINT64_FORMAT "\n", inseg->duration);
}
if (outseg) {
g_print("out start %" G_GUINT64_FORMAT "\n", outseg->start);
g_print("out end %" G_GUINT64_FORMAT "\n", outseg->stop);
g_print("out pos %" G_GUINT64_FORMAT "\n", outseg->position);
g_print("out dur %" G_GUINT64_FORMAT "\n", outseg->duration);
}
gst_segment_free (inseg);
gst_segment_free (outseg);
getchar();
}
void run(gchar *file)
{
garray = g_array_new (FALSE, FALSE, sizeof (guint));
GstBus *bus;
GstMessage *msg;
gchar launch_str[500];
sprintf (launch_str,
"filesrc location=%s ! qtdemux ! identity name=idty ! "
"avdec_h264 name=dec ! videoscale ! video/x-raw,width=800,height=480 ! "
"videoconvert ! ximagesink sync=false max-lateness=-1", file);
pipeline = gst_parse_launch (launch_str, NULL);
if (pipeline == NULL) {
g_print("parse fail\n");
}
identity = gst_bin_get_by_name (GST_BIN(pipeline), "idty");
decoder = gst_bin_get_by_name (GST_BIN(pipeline), "dec");
g_signal_connect(identity, "handoff", G_CALLBACK(handoff_cb), NULL);
gst_element_set_state (pipeline, GST_STATE_PLAYING);
bus = gst_element_get_bus(pipeline);
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
if (msg != NULL)
gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
g_array_free (garray, TRUE);
}
int main(int argc, char *argv[])
{
signal(SIGINT, sigint_handler);
gst_init(&argc, &argv);
if (argv[1]) {
g_print("load file: %s\n", argv[1]);
run(argv[1]);
} else {
g_print("load file: 4.mp4\n");
run("4.mp4");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment