반응형

Ubuntu에 GStreamer를 설치하고 웹캠에서 영상을 가져와 화면에 출력해봅니다.

보다 자세한 내용은 GStreamer 홈페이지에 있는 튜토리얼 문서를 참고하세요.

https://gstreamer.freedesktop.org/documentation/tutorials/index.html?gi-language=c 



2020. 7. 11 최초작성

2022. 9. 12 v4l2src 플러그인 사용하도록 수정

2023. 1. 21 설치 패키지에서 gstreamer1.0-doc 제거 




우선 진행하기 전에 패키지 목록을 업데이트합니다.  

 

$ sudo apt update



GStreamer를 설치합니다. 

 

$ sudo apt install libgstreamer1.0-0 libgstreamer1.0-dev gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly  gstreamer1.0-alsa gstreamer1.0-libav gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio libgstreamer-plugins-base1.0-dev 



다음 처럼 설치된 GStreamer의 버전을 확인할 수 있습니다. 

 

$ gst-inspect-1.0 --version

 

gst-inspect-1.0 version 1.16.3

GStreamer 1.16.3

https://launchpad.net/distros/ubuntu/+source/gstreamer1.0



터미널에서 GStreamer에서 제공하는 gst-launch-1.0을 사용하여 웹캠 영상을 가져와 화면에 출력해봅니다. 

v4l2src 플러그인에서 웹캠(/dev/video0)로부터 영상을 가져와 전달하면 audiovideosink 플러그인이 영상을 화면에 출력해줍니다.

 

$ gst-launch-1.0 v4l2src device=/dev/video0 ! autovideosink

 



간단한 코드를 작성하여 웹캠 영상을 가져와 봅니다. 

다음 코드를 gstreamer_example.cpp 이름으로 저장합니다.

 

#include <gst/gst.h>
#include <iostream>

using namespace std;


int main()
{
    GstElement *pipeline, *source, *sink;
    GstBus *bus;
    GstMessage *msg;
    GstStateChangeReturn ret;


    gst_init(NULL, NULL);

    pipeline = gst_pipeline_new ("pipeline");
    source = gst_element_factory_make ("v4l2src", "source");
    sink = gst_element_factory_make ("autovideosink", "sink");

    if (!pipeline || !source || !sink)
    {
        cout << "not all elements created: pipeline["<< !pipeline<< "]" << "source["<< !source<< "]" << "sink["<< !sink << "]" << endl;
        return -1;
    }

    g_object_set(G_OBJECT(source), "device", "/dev/video0", NULL);
    g_object_set(G_OBJECT (sink), "sync", FALSE, NULL);


    gst_bin_add_many (GST_BIN (pipeline), source, sink, NULL);
    if (gst_element_link (source, sink) != TRUE)
    {
        cout << "Elements could not be linked." << endl;
        gst_object_unref (pipeline);
        return -1;
    }

    ret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
    if (ret == GST_STATE_CHANGE_FAILURE)
    {
        cout << "Unable to set the pipeline to the playing state." << endl;
        gst_object_unref (pipeline);
        return -1;
    }

    bus = gst_element_get_bus (pipeline);

    cout << "press CTRL + C" << endl;

    while(1);

    gst_object_unref (bus);
    gst_element_set_state (pipeline, GST_STATE_NULL);
    gst_object_unref (pipeline);
}



컴파일 하여 실행하면 웹캠 영상을 화면에 보여줍니다. 

 

$ g++ gstreamer_example.cpp -o gstreamer_example `pkg-config --cflags --libs gstreamer-1.0` 

$ ./gstreamer_example




참고한 곳

 

[1] https://tutorialforlinux.com/2019/12/06/step-by-step-gstreamer-ubuntu-20-04-focal-installation-guide/ 

 

[2] https://medium.com/lifesjourneythroughalens/implementing-gstreamer-webcam-usb-internal-streaming-mac-c-clion-76de0fdb8b34 





반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts