Ubuntu 20.04에 GStreamer를 설치하고 웹캠에서 영상을 가져와 화면에 출력해봅니다.
보다 자세한 내용은 GStreamer 홈페이지에 있는 튜토리얼 문서를 참고하세요.
https://gstreamer.freedesktop.org/documentation/tutorials/index.html?gi-language=c
우선 진행하기 전에 패키지 목록을 업데이트합니다.
GStreamer를 설치합니다.
$ sudo apt install libgstreamer1.0-0 libgstreamer1.0-dev gstreamer1.0-tools gstreamer1.0-doc 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 |
webnautes@webnautes-PC:~$ gst-inspect-1.0 --version
gst-inspect-1.0 version 1.16.2
GStreamer 1.16.2
https://launchpad.net/distros/ubuntu/+source/gstreamer1.0
터미널에서 GStreamer에서 제공하는 gst-launch-1.0을 사용하여 웹캠 영상을 가져와 화면에 출력해봅니다.
audiovideosrc 플러그인에서 웹캠(/dev/video0)로부터 영상을 가져와 전달하면 audiovideosink 플러그인이 영상을 화면에 출력해줍니다.
$ gst-launch-1.0 autovideosrc 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 ("autovideosrc", "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 (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 |
참고한 곳
https://tutorialforlinux.com/2019/12/06/step-by-step-gstreamer-ubuntu-20-04-focal-installation-guide/
https://medium.com/lifesjourneythroughalens/implementing-gstreamer-webcam-usb-internal-streaming-mac-c-clion-76de0fdb8b34