Basic tutorial 1: Hello world!

CrM·2023년 12월 19일

GStreamer

목록 보기
3/3

https://gstreamer.freedesktop.org/documentation/tutorials/basic/hello-world.html?gi-language=c#

Summary

  • 간단한 GStreamer 어플리케이션 만들기
    • 초기화: gst_init()
    • 자동 pipeline 간단 구축: gst_parse_launch("**playbin**", null)
    • 실행 state로 변경: gst_element_set_state(pipeline, **GST_STATE_PLAYING**)
    • 실행 후 종료(Error/EOS 신호) 대기: gst_element_get_bus(), gst_bus_timed_pop_filtered()
    • 종료 후 cleanup: gst_object_unref(pipeline)

Goal

  • 실습
    • GStreamer 앱을 통해 새 창에 인터넷으로 받은 비디오 및 오디오 재생해보기

Execution

Hello World

Code

<basic-tutorial-1.c>

#include <gst/gst.h>

#ifdef __APPLE__
#include <TargetConditionals.h>
#endif

int
tutorial_main (int argc, char *argv[])
{
  GstElement *pipeline;
  GstBus *bus;
  GstMessage *msg;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  /* Build the pipeline */
  pipeline =
      gst_parse_launch
      ("playbin uri=https://gstreamer.freedesktop.org/data/media/sintel_trailer-480p.webm",
      NULL);

  /* Start playing */
  gst_element_set_state (pipeline, GST_STATE_PLAYING);

  /* Wait until error or EOS */
  bus = gst_element_get_bus (pipeline);
  msg =
      gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
      GST_MESSAGE_ERROR | GST_MESSAGE_EOS);

  /* See next tutorial for proper error message handling/parsing */
  if (GST_MESSAGE_TYPE (msg) == GST_MESSAGE_ERROR) {
    g_error ("An error occurred! Re-run with the GST_DEBUG=*:WARN environment "
        "variable set for more details.");
  }

  /* Free resources */
  gst_message_unref (msg);
  gst_object_unref (bus);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
  return 0;
}

int
main (int argc, char *argv[])
{
#if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE
  return gst_macos_main (tutorial_main, argc, argv, NULL);
#else
  return tutorial_main (argc, argv);
#endif
}
  • 실제 동작을 위한 코드는 단 4줄뿐... (나머지는 init 및 cleanup용 코드)

Execute

# build
gcc basic-tutorial-1.c -o basic-tutorial-1 `pkg-config --cflags --libs gstreamer-1.0`

# run
./basic-tutorial-1

Result

Walkthrough

gst_init()

/* Initialize GStreamer */
gst_init (&argc, &argv);
  • GStreamer 초기화(initialize)
  • 항상 모든 GStreamer 코드의 최상단에 있어야 함
  • 세부 역할
    • 모든 내부 구조 초기화
    • 사용 가능한 plugin 확인
    • GStreamer를 위한 모든 command-line option 실행

gst_parse_launch()

 /* Build the pipeline */
pipeline =
    gst_parse_launch
    ("playbin uri=https://gstreamer.freedesktop.org/data/media/sintel_trailer-480p.webm",
    NULL);
  • 간단한 pipeline인 경우 쉽게 생성할 수 있도록 해주는 명령어 (shortcut)
  • 이 때 만들어지는 pipeline은 playbin
  • 텍스트를 실제 pipeline으로 알아서 변환해줘서 편리
    • 원래는 수동으로 직접 여러 개별 요소를 직접 조립해야 함

playbin

  • 단일 element로 이루어진 pipeline
  • 그 자체로 source이자 sink이자 pipeline인 특수한 element
  • 미디어 재생에 필요한 모든 요소를 내부적으로 알아서 만들고 연결시킴 (automatic pipeline)
  • 직접 만드는거 만큼은 아니지만 그래도 어느 정도의 customize는 가능
    • ex) URI를 통해 live stream(http://)이나 파일(file://) 모두 실행할 수 있음

gst_element_set_state()

/* Start playing */
gst_element_set_state (pipeline, GST_STATE_PLAYING);
  • pipeline의 state를 변경
  • GST_STATE_PLAYING
    • pipeline의 state가 PLAYING 이어야 재생됨 (playback initialize)

gst_element_get_bus(), gst_bus_timed_pop_filtered()

  /* Wait until error or EOS */
  bus = gst_element_get_bus (pipeline);
  msg =
      gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE,
      GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
  • gst_element_get_bus()
    • pipeline의 bus를 검색
  • gst_bus_timed_pop_filtered()
    • bus에서 ERROR나 EOS(End-Of-Stream)을 받을 때까지 block

Cleanup

  /* Free resources */
  gst_message_unref (msg);
  gst_object_unref (bus);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);
  return 0;
  • 앱 종료 전, 사용한 자원을 모두 직접 반납해야
    • 항상 사용한 함수의 documentation을 참고하여 해제(free)시켜야 할 자원이 있는지 확인해야 함
  • gst_object_unref()
    • 자원 반납

Conclusion

  • GStreamer 초기화gst_init()
  • 텍스트를 통해 간단히 pipeline 구축: gst_parse_launch()
  • 자동으로 playback pipeline 생성: playbin
  • GStreamer가 실행 상태가 되도록 신호 주기: gst_element_set_state()
  • GStreamer가 알아서 처리하도록 맡기고 기다리기: gst_element_get_bus(), gst_bus_timed_pop_filtered()
  • 실행 완료 후 자원 반납: gst_object_unref()

0개의 댓글