[Gstreamer] 01_Hello world

[Gstreamer] 01_Hello world

날짜
생성자
ShalomShalom
카테고리
gstreamer
작성일
2023년 05월 31일
태그
rust
gstreamer
미디어 스트리밍을 다루기 위한 라이브러리로는 대표적으로 두 개가 있습니다
  • FFMPEG
  • GStreamer
그 중에서 GStreamer에 대한 튜토리얼을 번역하여 작성하려합니다

Why?

GStreamer는 glib을 통해 만들어진 라이브러리 입니다
glib을 선호하는 이유는 gtk-rs라는 프로젝트를 통해 wrapping이 잘되어 있습니다
위 사이트의 release의 업데이트를 보시면 개발이 활발히 이루어지고 있습니다
또한, 사용할 때에 Rust스럽게 사용할 수 있도록 Crate가 구현이 되어 있습니다

What?

오늘은 gstreamer의 예제를 한국어 번역과 Rust로 진행에 보겠습니다
원문은 다음 사이트에 있습니다

코드

use gst::prelude::*; fn main() { gst::init().unwrap(); // Build the pipeline let uri = "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm"; let pipeline = gst::parse_launch(&format!("playbin uri={uri}")).unwrap(); // Start playing pipeline .set_state(gst::State::Playing) .expect("Unable to set the pipeline to the `Playing` state"); // Wait until error or EOS let bus = pipeline.bus().unwrap(); for msg in bus.iter_timed(gst::ClockTime::NONE) { use gst::MessageView; match msg.view() { MessageView::Eos(..) => break, MessageView::Error(err) => { println!( "Error from {:?}: {} ({:?})", err.src().map(|s| s.path_string()), err.error(), err.debug() ); break; } _ => (), } } // Shutdown pipeline pipeline .set_state(gst::State::Null) .expect("Unable to set the pipeline to the `Null` state"); }
해당 코드는 uri를 새로운 윈도우에 출력하는 코드입니다

실행이 안된다면

gst-plugin들이 제대로 설치가 안되어서 나타나는 현상입니다
확인은 gst-inspect-1.0이라는 명령어를 통해 확인할 수 있습니다
예를 들어 gst-inspect-1.0.exe playbin 와 같이 powershell에 명령어를 입력하면
다음과 같은 결과가 나와야 합니다
Factory Details: Rank none (0) Long-name Player Bin 2 Klass Generic/Bin/Player Description Autoplug and play media from an uri Author Wim Taymans <wim.taymans@gmail.com> Documentation https://gstreamer.freedesktop.org/documentation/playback/playbin.html Plugin Details: Name playback Description various playback elements Filename C:\deploy\lib\gstreamer-1.0\gstplayback.dll Version 1.22.3 License LGPL Source module gst-plugins-base Documentation https://gstreamer.freedesktop.org/documentation/playback/ Source release date 2023-05-19 Binary package GStreamer Base Plug-ins source release Origin URL Unknown package origin
PlayBin 플러그인이 설치되었다고 완료가 된 것은 아니고 Bin을 구성하는 Element들 또한 제대로설치가 되어있어야 합니다
💡
또는 인터넷의 속도에 따라 동영상이 멈출 수 있습니다

코드 분석

  • gst::init()
    • gstreamer를 이용하려면 항상 먼저 실행해주어야하는 함수 입니다
    • 해당 함수를 통해 gstreamer에 대한 내부 구조를 초기화 할 수 있습니다
  • gst::parse_launch(&format!("playbin uri={uri}")).unwrap()
    • 먼저 파이프라인이란 개념에대해서 알아야합니다
    • 파이프라인이란 미디어 소스(url)에서 미디어 싱크(화면)까지 데이터가 이동하는 프로세스를 이야기합니다
    • 예를 들면 아래의 사진과 같습니다
    • notion image
    • 파이프라인을 powershell이나 shell를 통해서 다음과 같이 실행할 수 있습니다
      • gst-launch-1.0.exe playbin uri=https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm
  • 그리고 코드에선 gst::parse_launch()로 파이프라인을 생성합니다
  • playbin
    • playbin은 src와 sink의 역할을 모두 수행할 수있는 Bin입니다
    • playbin안에는 http://또는file:// 과 같은 uri 스키마에 따라 element를 사용하도록 구현되어 있습니다
    • 💡
      Element와 Bin, Pipeline에 대하여 더 자세한 내용은 나중에 다루겠습니다
    • playbin에서 에러가 나는 경우는 스키마에 따른 element가 제대로 설치가 안되어 있어서 나는 에러입니다
  • pipeline.set_state(gst::State::Playing)
    • pipeline은 생성이 되었다고 바로 재생을 하는 것이 아니라 state를 Playing으로 바꿔줘야 합니다
  • // Wait until error or EOS
    • 해당 영역의 코드는 pipeline은 실행이되고
      • main thread는 for문을 돌면서 블록킹 되는 구간입니다
    • for문에서 pipeline의 상태를 받습니다
    • pipeline의 상태가 EoS(End-Of-Stream)나 Error 가되면 for문을 빠져나갑니다
  • pipeline.set_state(gst::State::Null)
    • pipeline을 종료합니다
    •  

댓글

guest