1. 程式人生 > >Gstreamer教程 -時間

Gstreamer教程 -時間

#include<gst/gst.h>

/* Structure to contain all our information, so we can passit around */

typedefstruct _CustomData {

  GstElement *playbin2;  /* Our one and only element */

  gboolean playing;      /* Are we in the PLAYING state? */

  gboolean terminate;    /* Should we terminate execution? */

  gboolean seek_enabled; /* Is seeking enabledfor this media? */

  gboolean seek_done;    /* Have we performed the seek already? */

  gint64 duration;       /* How long does this media last, innanoseconds */

}CustomData;

 

/* Forward definition of the message processing function */

staticvoid handle_message (CustomData *data, GstMessage *msg);

 

intmain(int argc, char *argv[]) {

  CustomData data;

  GstBus *bus;

  GstMessage *msg;

  GstStateChangeReturn ret;

 

  data.playing = FALSE;

  data.terminate = FALSE;

  data.seek_enabled = FALSE;

  data.seek_done = FALSE;

  data.duration = GST_CLOCK_TIME_NONE;

 

  /*Initialize GStreamer */

  gst_init (&argc, &argv);

  

  /* Create the elements */

//僅包含playbin2一個element就足夠實現播放,參見教程01

  data.playbin2 = gst_element_factory_make("playbin2", "playbin2");

 

  if (!data.playbin2) {

    g_printerr ("Not all elements could becreated.\n");

    return -1;

  }

 

  /* Set the URI to play */

  g_object_set (data.playbin2, "uri","http://docs.gstreamer.com/media/sintel_trailer-480p.webm",NULL);

 

  /* Start playing */

  ret = gst_element_set_state (data.playbin2,GST_STATE_PLAYING);

  if (ret == GST_STATE_CHANGE_FAILURE) {

    g_printerr ("Unable to set thepipeline to the playing state.\n");

    gst_object_unref (data.playbin2);

    return -1;

  }

 

  /* Listen to the bus*/

  bus = gst_element_get_bus (data.playbin2);

  do {

//設定100毫秒超時,每10s呼叫一次,返回NULL

    msg = gst_bus_timed_pop_filtered(bus, 100 * GST_MSECOND,

        GST_MESSAGE_STATE_CHANGED |GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);

 

    /* Parse message */

    if (msg != NULL) {

      handle_message (&data, msg);

    } else {

      /* We got no message,this means the timeout expired */

      if (data.playing) {

        GstFormat fmt = GST_FORMAT_TIME;

        gint64 current = -1;

       

        /* Query the currentposition of the stream */

    //gst_element_query_position查詢當前播放時間

        if (!gst_element_query_position(data.playbin2, &fmt, ¤t)) {

          g_printerr ("Could not querycurrent position.\n");

        }

       

        /* If we didn't knowit yet, query the stream duration */

     //gst_element_query_duration查詢總時間

        if (!GST_CLOCK_TIME_IS_VALID(data.duration)) {

          if (!gst_element_query_duration(data.playbin2, &fmt, &data.duration)) {

            g_printerr ("Could not querycurrent duration.\n");

          }

        }

       

        /* Print current position and total duration */

        g_print ("Position %" GST_TIME_FORMAT" / %" GST_TIME_FORMAT "\r",

            GST_TIME_ARGS (current),GST_TIME_ARGS (data.duration));

       

        /* If seeking isenabled, we have not done it yet, and the time is right, seek */

        if (data.seek_enabled &&!data.seek_done && current > 10 * GST_SECOND) {

          g_print ("\nReached 10s,performing seek...\n");

          gst_element_seek_simple(data.playbin2, GST_FORMAT_TIME,

              GST_SEEK_FLAG_FLUSH |GST_SEEK_FLAG_KEY_UNIT, 30 * GST_SECOND);

          data.seek_done = TRUE;

        }

      }

    }

  } while (!data.terminate);

 

  /* Free resources */

  gst_object_unref (bus);

  gst_element_set_state (data.playbin2,GST_STATE_NULL);

  gst_object_unref (data.playbin2);

  return 0;

}

 

staticvoid handle_message (CustomData *data, GstMessage *msg) {

  GError *err;

  gchar *debug_info;

 

  switch (GST_MESSAGE_TYPE (msg)) {

    case GST_MESSAGE_ERROR:

      gst_message_parse_error (msg, &err,&debug_info);

      g_printerr ("Error received fromelement %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);

      g_printerr ("Debugging information:%s\n", debug_info ? debug_info : "none");

      g_clear_error (&err);

      g_free (debug_info);

      data->terminate = TRUE;

      break;

    case GST_MESSAGE_EOS:

      g_print ("End-Of-Streamreached.\n");

      data->terminate = TRUE;

      break;

    case GST_MESSAGE_DURATION:

      /* The duration has changed, mark thecurrent one as invalid */

      data->duration = GST_CLOCK_TIME_NONE;

      break;

    case GST_MESSAGE_STATE_CHANGED: {

      GstState old_state, new_state,pending_state;

      gst_message_parse_state_changed (msg,&old_state, &new_state, &pending_state);

      if (GST_MESSAGE_SRC (msg) == GST_OBJECT(data->playbin2)) {

        g_print ("Pipeline state changedfrom %s to %s:\n",

            gst_element_state_get_name(old_state), gst_element_state_get_name (new_state));

       

        /* Remember whether weare in the PLAYING state or not */

        data->playing = (new_state ==GST_STATE_PLAYING);

       

        if (data->playing) {

          /* We just moved to PLAYING. Check ifseeking is possible */

          GstQuery *query;

          gint64 start, end;

 

//建立新seeking物件,gst_element_query將物件傳至管道

          query = gst_query_new_seeking (GST_FORMAT_TIME);

          if (gst_element_query(data->playbin2, query)) {

 

            gst_query_parse_seeking(query, NULL, &data->seek_enabled, &start, &end);

            if (data->seek_enabled) {

              g_print ("Seeking is ENABLEDfrom %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT "\n",

                  GST_TIME_ARGS (start),GST_TIME_ARGS (end));

            } else {

              g_print ("Seeking isDISABLED for this stream.\n");

            }

          }

          else {

            g_printerr ("Seeking queryfailed.");

          }

          gst_query_unref (query);

        }

      }

    } break;

    default:

      /* We should not reach here */

      g_printerr ("Unexpected messagereceived.\n");

      break;

  }

  gst_message_unref (msg);

}


 

跳轉功能

gst_element_seek_simple(

 

讓我們看一下這個方法的各個引數:

      GST_FORMAT_TIME說明我們的操作(跳轉)是針對時間來計算的(有兩種計算方式,時間和資料)。

      然後是GstSeekFlags,這裡主要介紹常見的幾個:

      GST_SEEK_FLAG_FLUSH:跳轉後會丟棄當前pipeline裡面所有的資料,因為要重新解析一段資料,所以會有一個停頓,但在使用者看來可以保證應用的快速響應;反之,就會繼續播放一點內容,然後再跳轉。

      GST_SEEK_FLAG_KEY_UNIT:大部分編碼的視訊流是無法精確定位到某個特定時刻的,只能是到某些幀(稱為key frame)。當這個標誌被置時,會自動定位到最近的一個key frame然後開始播放。如果這個標誌不置,那麼pipeline會跳到最接近的一個key frame,然後開始播放,但此時不輸出任何東西,直到到達設定的位置。綜合來看,不設定這個標誌會更加準確,但是響應時間會顯得較長。

      GST_SEEK_FLAG_ACCURATE:有些時候我們沒法獲得足夠的索引資訊,這個時候跳轉到某個位置會非常耗時。在這種情況下,GStreamer

通常就是估計一下大概的位置(一般都很準確)。如果你實際執行發現這個不夠準確,那麼可以置這個標誌位。必須瞭解的是,這個標誌位一旦設定,跳轉的時間會大大增加。

      最後,我們會跳轉到某個地方,因為我們用的是GST_FORMAT_TIME,時間的單位是用納秒來計算的,所以需要用GST_SECOND巨集來轉換一下。

 

來自 <http://blog.csdn.net/sakulafly/article/details/20992879>