Can't query to get Position and Duration GStreamer

Tim Müller tim at centricular.com
Thu Mar 23 09:22:44 UTC 2017


On Wed, 2017-03-22 at 10:02 -0700, hungbattran wrote:

Hi,

>   /* Build the pipeline */
>   data.pipeline = gst_parse_launch ("playbin uri=...", NULL);  
>   /* Start playing */
>   ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
>   (...)
>   
>   /* 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;
>   }

The problem here is that you will only be able to query the position
and/or duration once the pipeline is prerolled (PAUSED or PLAYING
state).

set_state(PLAYING) will just make GStreamer start things in the
background asynchronously in other threads. When this call returns and
moves on to the following lines like the query_position/duration(),
GStreamer may not have yet started up the pipeline in the background
threads.

To fix this you'll have to wait for the pipeline to be prerolled. You
can do that with:

  get_element_get_state (pipeline, NULL, NULL, GST_CLOCK_TIME_NONE);

(this will block)

or

  msg = gst_bus_timed_pop_filtered (GST_ELEMENT_BUS (pipeline),
      GST_CLOCK_TIME_NONE,
      GST_MESSAGE_ASYNC_DONE | GST_MESSAGE_ERROR);
  .. check if it's an error message ..
  gst_message_unref(msg);

(this will also block).

Ideally you would add a bus watch and then query position/duration in
the callback when you get an ASYNC_DONE message.

Cheers
 -Tim

-- 
Tim Müller, Centricular Ltd - http://www.centricular.com


More information about the gstreamer-devel mailing list