<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 22, 2017 at 10:02 AM, hungbattran <span dir="ltr"><<a target="_blank" href="mailto:tranhung93.it@gmail.com">tranhung93.it@gmail.com</a>></span> wrote:<br><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">Dear all,<br>
<br>
This is my code that I try to get position and duration of video. This code<br>
can play video but it can't query to get position and duration of video.<br>
Please check for me. I'm a newbee of gstreamer and this code I modify from<br>
example 13.<br></blockquote><div><br></div><div>See my suggestion below.<br></div><div><br> </div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">
Thank so much.<br>
<br>
#include <string.h><br>
#include <stdio.h><br>
#include <gst/gst.h><br>
<br>
<br>
typedef struct _CustomData {<br>
  GstElement *pipeline;<br>
  GstElement *video_sink;<br>
  GMainLoop *loop;<br>
<br>
  gint64 position;<br>
  GstEvent *seek_event;<br>
<br>
  gboolean playing;  /* Playing or Paused */<br>
  gdouble rate;      /* Current playback rate (can be negative) */<br>
} CustomData;<br>
<br>
int main(int argc, char *argv[]) {<br>
  CustomData data;<br>
  GstStateChangeReturn ret;<br>
  GIOChannel *io_stdin;<br>
  GstFormat format = GST_FORMAT_TIME;<br>
<br>
  GstEvent *seek_event;<br>
  gint64 position;<br>
  gint64 duration;<br>
<br>
<br>
<br>
  /* Initialize GStreamer */<br>
  gst_init (&argc, &argv);his is my code that I try to get position and duration of video. This code<br>
can play video but it can't query to get position and duration of video.<br>
Please check for me. I'm a newbee of gstreamer and this code I modify from<br>
example 13.<br>
Thank so much.<br>
<br>
  /* Initialize our data structure */<br>
  memset (&data, 0, sizeof (data));<br>
<br>
  /* Print usage map */<br>
<br>
<br>
  /* Build the pipeline */<br>
  data.pipeline = gst_parse_launch ("playbin<br>
uri=file:///home/hungtran/<wbr>Desktop/sintel_trailer-480p.<wbr>webm", NULL);<br>
  /* Start playing */<br>
  ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);<br>
  if (ret == GST_STATE_CHANGE_FAILURE) {<br>
    g_printerr ("Unable to set the pipeline to the playing state.\n");<br>
    gst_object_unref (data.pipeline);<br>
    return -1;<br>
  }<br>
  data.playing = TRUE;<br>
  data.rate = 1.0;<br>
<br></blockquote><div><br></div><div>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.<br><br></div><div>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.<br></div><div> </div><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">
  /* Obtain the current position, needed for the seek event */<br>
  if (!gst_element_query_position (data.pipeline, GST_FORMAT_TIME,<br>
&position)) {<br>
    g_printerr ("Unable to retrieve current position.\n");<br>
    return;<br>
  }<br>
<br>
 if (!gst_element_query_duration (data.pipeline, GST_FORMAT_TIME,<br>
&duration)) {<br>
    g_printerr ("Unable to retrieve current position.\n");<br>
    return;<br>
  }<br>
<br>
    printf("[right] previous=%"GST_TIME_FORMAT"\n"<wbr>,<br>
GST_TIME_ARGS(position));<br>
    printf("[right] previous=%"GST_TIME_FORMAT"\n"<wbr>,<br>
GST_TIME_ARGS(duration));<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
  /* Create a GLib Main Loop and set it to run */<br>
  data.loop = g_main_loop_new (NULL, FALSE);<br>
  g_main_loop_run (data.loop);<br>
<br>
  /* Free resources */<br>
  g_main_loop_unref (data.loop);<br>
  g_io_channel_unref (io_stdin);<br>
  gst_element_set_state (data.pipeline, GST_STATE_NULL);<br>
  if (data.video_sink != NULL)<br>
    gst_object_unref (data.video_sink);<br>
  gst_object_unref (data.pipeline);<br>
  return 0;<br>
}<br>
<br>
<br>
<br>
--<br>
View this message in context: <a target="_blank" rel="noreferrer" href="http://gstreamer-devel.966125.n4.nabble.com/Can-t-query-to-get-Position-and-Duration-GStreamer-tp4682359.html">http://gstreamer-devel.966125.<wbr>n4.nabble.com/Can-t-query-to-<wbr>get-Position-and-Duration-<wbr>GStreamer-tp4682359.html</a><br>
Sent from the GStreamer-devel mailing list archive at Nabble.com.<br>
______________________________<wbr>_________________<br>
gstreamer-devel mailing list<br>
<a href="mailto:gstreamer-devel@lists.freedesktop.org">gstreamer-devel@lists.<wbr>freedesktop.org</a><br>
<a target="_blank" rel="noreferrer" href="https://lists.freedesktop.org/mailman/listinfo/gstreamer-devel">https://lists.freedesktop.org/<wbr>mailman/listinfo/gstreamer-<wbr>devel</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">Thiago Sousa Santos</div>
</div></div>