728x90

Qt5, Window 10에서 테스트 할 gstreamer 관련 포스팅 입니다. 튜토리얼을 따라가보면서 진행해볼 예정입니다.

 

Tutorials

Tutorials Welcome to the GStreamer Tutorials! The following sections introduce a series of tutorials designed to help you learn how to use GStreamer, the multi-platform, modular, open-source, media streaming framework. Prerequisites Before following these

gstreamer.freedesktop.org

 

Tutorial 진행에 앞서 gstreamer 패키지를 사용하기 위해 헤더파일과 lib 파일을 다운로드 합니다.

 

Download GStreamer

Download GStreamer If you're on Linux or a BSD variant, you can install GStreamer using your package manager. For other platforms, specifically Windows, macOS, Android, and iOS, we provide binary releases in the form of official installers or tarballs main

gstreamer.freedesktop.org

본인이 사용할 컴파일러에 맞게 설치합니다. MSVC 64비트로 설치해 보겠습니다.

두 파일을 모두 다운로드 후 설치해줍니다.

Qt Creator를 실행 해 Console Project를 만들어줍니다.

include 폴더와 lib 폴더, bin 폴더를 프로젝트의 폴더로 복사해줍니다.

프로젝트 파일을 아래와 같이 작성합니다.

QT -= gui

CONFIG += c++17 console
CONFIG -= app_bundle

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

DEPENDPATH += \
    $$PWD/include

INCLUDEPATH += \
    $$PWD/include \
    $$PWD/include/gstreamer-1.0 \
    $$PWD/include/glib-2.0/ \
    $$PWD/include/glib-2.0/include \
    $$PWD/lib/glib-2.0/include \

win32: LIBS += -L$$PWD/lib/ -lgstreamer-1.0 -lgobject-2.0 -lglib-2.0 -lintl

DESTDIR += \
    $$PWD/bin

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#include <QCoreApplication>
#include "gst/gst.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, 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, (GstMessageType) (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 a.exec();
}

실행하면 https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm 의 동영상이 재생되는 것을 볼 수 있습니다.

먼저 gstreamer를 사용하기 앞서 초기화를 해줍니다.

gst_init (&argc, &argv);

gstreamer는 source elements에서 sink elemnet로 미디어를 전송하는데 이런식으로 elements들의 집합을 pipeline이라고 합니다. 이 pipeline을 간단하게 구성하기 위해 gst_parse_launch 함수를 호출합니다. 

pipeline = gst_parse_launch("playbin uri=file:/C:/Users/psy10/Downloads/sintel_trailer-480p.mkv",NULL);
pipeline = gst_parse_launch("playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm",NULL);

이 파이프라인을 구성하는데 playbin이라는 elements를 넘길 수 있습니다. https://를 통해 웹 상에 있는 동영상을 스트리밍하거나 file://을 통해 내부 파일도 재생할 수 있습니다.

 

파이프 라인을 playing 상태로 설정합니다.

gst_element_set_state (pipeline, GST_STATE_PLAYING);

아래 함수를 통해 stream이 종료(GST_MESSAGE_EOS)되거나 에러(GST_MESSAGE_ERROR)가 발생할 때까지 대기합니다.

bus = gst_element_get_bus (pipeline);
msg = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, (GstMessageType) (GST_MESSAGE_ERROR | GST_MESSAGE_EOS));

stream이 종료 후 반환된 Message를 통해 ERROR가 발생했는지 체크합니다.

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.");
}

작업 종료 후 마지막으로 pipeline state를 정리하고 나머지 객체들을 해제시켜 줍니다.

gst_message_unref (msg);
gst_object_unref (bus);
gst_element_set_state (pipeline, GST_STATE_NULL);
gst_object_unref (pipeline);
728x90

+ Recent posts