Can't query to get Position and Duration GStreamer

Thiago Sousa Santos thiagossantos at gmail.com
Wed Mar 22 19:05:37 UTC 2017


On Wed, Mar 22, 2017 at 10:02 AM, hungbattran <tranhung93.it at gmail.com>
wrote:

> Dear all,
>
> This is my code that I try to get position and duration of video. This code
> can play video but it can't query to get position and duration of video.
> Please check for me. I'm a newbee of gstreamer and this code I modify from
> example 13.
>

See my suggestion below.



> Thank so much.
>
> #include <string.h>
> #include <stdio.h>
> #include <gst/gst.h>
>
>
> typedef struct _CustomData {
>   GstElement *pipeline;
>   GstElement *video_sink;
>   GMainLoop *loop;
>
>   gint64 position;
>   GstEvent *seek_event;
>
>   gboolean playing;  /* Playing or Paused */
>   gdouble rate;      /* Current playback rate (can be negative) */
> } CustomData;
>
> int main(int argc, char *argv[]) {
>   CustomData data;
>   GstStateChangeReturn ret;
>   GIOChannel *io_stdin;
>   GstFormat format = GST_FORMAT_TIME;
>
>   GstEvent *seek_event;
>   gint64 position;
>   gint64 duration;
>
>
>
>   /* Initialize GStreamer */
>   gst_init (&argc, &argv);his is my code that I try to get position and
> duration of video. This code
> can play video but it can't query to get position and duration of video.
> Please check for me. I'm a newbee of gstreamer and this code I modify from
> example 13.
> Thank so much.
>
>   /* Initialize our data structure */
>   memset (&data, 0, sizeof (data));
>
>   /* Print usage map */
>
>
>   /* Build the pipeline */
>   data.pipeline = gst_parse_launch ("playbin
> uri=file:///home/hungtran/Desktop/sintel_trailer-480p.webm", NULL);
>   /* Start playing */
>   ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
>   if (ret == GST_STATE_CHANGE_FAILURE) {
>     g_printerr ("Unable to set the pipeline to the playing state.\n");
>     gst_object_unref (data.pipeline);
>     return -1;
>   }
>   data.playing = TRUE;
>   data.rate = 1.0;
>
>
The pipeline will change state asynchronously to PAUSED/PLAYING and there
is no guarantee that it will be ready to reply to your queries at this
point in the code.

You should set up a bus messages listener and wait for a State Changed
message from your pipeline to make sure it is at least in PAUSED to be able
to query it for position and duration. The Message handling will also be
useful to detect the end of stream and errors, so you will need it anyway.


>   /* Obtain the current position, needed for the seek event */
>   if (!gst_element_query_position (data.pipeline, GST_FORMAT_TIME,
> &position)) {
>     g_printerr ("Unable to retrieve current position.\n");
>     return;
>   }
>
>  if (!gst_element_query_duration (data.pipeline, GST_FORMAT_TIME,
> &duration)) {
>     g_printerr ("Unable to retrieve current position.\n");
>     return;
>   }
>
>     printf("[right] previous=%"GST_TIME_FORMAT"\n",
> GST_TIME_ARGS(position));
>     printf("[right] previous=%"GST_TIME_FORMAT"\n",
> GST_TIME_ARGS(duration));
>
>
>
>
>
>
>
>   /* Create a GLib Main Loop and set it to run */
>   data.loop = g_main_loop_new (NULL, FALSE);
>   g_main_loop_run (data.loop);
>
>   /* Free resources */
>   g_main_loop_unref (data.loop);
>   g_io_channel_unref (io_stdin);
>   gst_element_set_state (data.pipeline, GST_STATE_NULL);
>   if (data.video_sink != NULL)
>     gst_object_unref (data.video_sink);
>   gst_object_unref (data.pipeline);
>   return 0;
> }
>
>
>
> --
> View this message in context: http://gstreamer-devel.966125.
> n4.nabble.com/Can-t-query-to-get-Position-and-Duration-
> GStreamer-tp4682359.html
> Sent from the GStreamer-devel mailing list archive at Nabble.com.
> _______________________________________________
> gstreamer-devel mailing list
> gstreamer-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel
>



-- 
Thiago Sousa Santos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/gstreamer-devel/attachments/20170322/eb380ba5/attachment-0001.html>


More information about the gstreamer-devel mailing list