GstQuery를 이용해서 원소나 pad의 정보를 얻어낸다.

msg = gst_bus_timed_pop_filtered (bus, 100 * GST_MSECOND,
    GST_MESSAGE_STATE_CHANGED | GST_MESSAGE_ERROR | GST_MESSAGE_EOS | GST_MESSAGE_DURATION);

playbin은 파이프라인이기도 한 특수한 원소이다. 파이프라인이기 때문에 이것으로부터 bus를 구해낼 수 있다. 이전과 같이 timeout 값으로 GST_CLOCK_TIME_NONE 을 넘기지 않고, 100 * GST_MSECOND 을 넘겨서 주기적으로 질의를 통해 정보를 얻거나 특정 동작을 할 수 있도록 한다.

User interface refeshing

/* We got no message, this means the timeout expired */
if (data.playing) {

data.playing은 파이프라인이 PLAYING 상태일 때 true로 설정된다.

/* Query the current position of the stream */
if (!gst_element_query_position (data.pipeline, GST_FORMAT_TIME, &current)) {
  g_printerr ("Could not query current position.\n");
}

스트림의 현재 위치를 질의한다.

/* If we didn't know it yet, query the stream duration */
if (!GST_CLOCK_TIME_IS_VALID (data.duration)) {
  if (!gst_element_query_duration (data.pipeline, GST_FORMAT_TIME, &data.duration)) {
     g_printerr ("Could not query current duration.\n");
  }
}

스트림의 길이(duration)를 질의한다.

/* 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));

GST_TIME_FORMAT, GST_TIME_ARGS 매크로를 이용해서 시간 정보를 쉽게 확인할 수 있도록 표시한다.

/* If seeking is enabled, 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.pipeline, GST_FORMAT_TIME,
      GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT, 30 * GST_SECOND);
  data.seek_done = TRUE;
}

현재 위치가 10초가 넘어서게 되면, 30초 위치로 점프(seek)하게 된다.
GST_SEEK_FLAG_FLUSH 를 사용해서 점프할 때 이전에 버퍼링되어 있던 데이터를 비우게 된다. 이 옵션을 넣어주지 않으면, 이전 영상이 한동안 보이게 될 수 있다.
GST_SEEK_FLAG_KEY_UNIT 을 사용하면, 요청한 위치에서 가장 가까운 키프레임으로 이동하게 된다.
GST_SEEK_FLAG_ACCURATE 를 사용하면, 요청한 위치로 가장 가깝게 이동하려고 시도한다. 시간이 걸리는 작업이다.

Message Pump

case GST_MESSAGE_DURATION:
  /* The duration has changed, mark the current one as invalid */
  data->duration = GST_CLOCK_TIME_NONE;
  break;

duration이 변경될 때 처리하게 되는 case 인데, GST_CLOCK_TIME_NONE 로 설정하는 이유는 모르겠다. gst_element_query_duration 함수를 통해서 duration 값을 갱신하는 작업은 앞선 설명에서 하고 있다.

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->pipeline)) {
    g_print ("Pipeline state changed from %s to %s:\n",
        gst_element_state_get_name (old_state), gst_element_state_get_name (new_state));

    /* Remember whether we are in the PLAYING state or not */
    data->playing = (new_state == GST_STATE_PLAYING);

질의는 일반적으로 PAUSED 나 PLAYING 상태에서 가능하다.

if (data->playing) {
  /* We just moved to PLAYING. Check if seeking is possible */
  GstQuery *query;
  gint64 start, end;
  query = gst_query_new_seeking (GST_FORMAT_TIME);
  if (gst_element_query (data->pipeline, query)) {
    gst_query_parse_seeking (query, NULL, &data->seek_enabled, &start, &end);
    if (data->seek_enabled) {
      g_print ("Seeking is ENABLED from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT "\n",
          GST_TIME_ARGS (start), GST_TIME_ARGS (end));
    } else {
      g_print ("Seeking is DISABLED for this stream.\n");
    }
  }
  else {
    g_printerr ("Seeking query failed.");
  }
  gst_query_unref (query);
}

gst_query_new_seeking 를 이용해서 "seeking" 타입의 질의를 생성한다. gst_element_query 를 통해 질의하고, gst_query_parse_seeking 를 통해 질의한 결과를 가져온다.
질의를 생성했다면, gst_query_unref 를 통해 해제를 반드시 해야 한다.

profile
프로그래밍을 좋아하는 평범한 개발자입니다.

0개의 댓글