컴퓨터 공학

GStreamer 학습 튜토리얼(GStreamer tutorials) - #1 Hello World!

집빈지노 2021. 9. 15. 18:08

* GStreamer : Multiplatform multimedia framework

GStreamer를 쓸 필요가 생겨 공부하게 되었다. GStreamer는 multiplatform에서 작동이 가능하며, 미디어/기기 간 음성이나 영상 송, 수신을 지원하는 프레임워크이다.

Android, RaspberryPi, Linux, Window 모두 지원한다. 소스파일 작성에 쓰이는 언어는 C 이다.

아래는 GStreamer 초보 사용자를 위한 첫 번째 튜토리얼인 Hello World이다. GStreamer의 정식 웹사이트에 가보면 Basic tutorial부터, playback, android, ios tutorial까지 자세히 배우기 좋게 나와있다. 이번 포스트는 Basic tutorial의 첫 번째인 'Hello World'를 나름 요약한 것이다. 번역은 하지 못했다...

1. Hello World!

- official tutorial from : https://gstreamer.freedesktop.org/documentation/tutorials

- filename : basic-tutorial-1.c

#include <gst/gst.h>

int
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://www.freedesktop.org/software/gstreamer-sdk/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);

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

- pipeline contains two key points, gst_parse_launch() and playbin.

- gst_parse_launch()

    Media travels from the "source" elements (the producers) down to the "sink" elements (the consumers), passing through a series of intermediate elements performing many kinds of tasks. The set of all the interconnected elements is called a "pipeline". This function takes a textual representation of a pipeline and turns it into an actual pipeline. 

- playbin

    Playbin is a special element which acts as a source and as a sink, and is a whole pipeline. Internally it creates and connects all the necessary elements to play your media, so you do not have to worry about it.

In this example, we are only passing one parameter to playbin, which is the URI of the media we want to play.

 

- gst_element_set_state (pipeline, GST_STATE_PLAYING);

Every GStreamer element has an associated state, which you can think of as the Play/Pause button in a DVD player. The playback will not start unless you set the pipeline to the "PLAYING" state.

Above line initiates playback.

 

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

These lines will wait until an error occurs or the end of the stream is found. gst_element_get_bus() retrieves the pipeline's bus, and gst_bus_timed_pop_filtered() will block until you receive either an ERROR or an EOS(End-Of-Stream) through that bus.

 

- Execution will end when the media reaches its end (EOS) or an error is encountered. The application can always be stopped by pressing control-C in the console.

 

* Cleanup

Before terminating the application, there is a couple of things we need to do to tidy up correctly.

 

 /* Free resources */
  if (msg != NULL)
    gst_message_unref (msg);
  gst_object_unref (bus);
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gst_object_unref (pipeline);

In this case, gst_bus_timed_pop_filtered() returned a message which needs to be freed with gst_message_unref().

gst_element_get_bus() added a reference to the bus that must be freed with gst_object_unref(). Setting the pipeline to the NULL state will make sure it frees any resources it has allocated. Finally, unreferencing the piepline will destroy it and all its contents.

 

Conclusion

We have learned :

 

 

'컴퓨터 공학' 카테고리의 다른 글

GStreamer 요약  (0) 2021.10.14
VMS(Video Management System)/NVR 소프트웨어 환경 셋업  (0) 2021.09.13
TCP/IP Protocol  (0) 2021.05.20