From 4400f3a7a12fbb01fe275f1cbc04cae14db2a47e Mon Sep 17 00:00:00 2001 From: Vladimir Romashchenko Date: Thu, 19 Sep 2024 21:18:18 -0400 Subject: [PATCH] Don't call gst::init if initialized, add non-str pipeline support --- Cargo.lock | 2 +- src/lib.rs | 1 + src/video.rs | 29 +++++++++++++++++++++++------ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d18209c..9c0143c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1419,7 +1419,7 @@ dependencies = [ [[package]] name = "iced_video_player" -version = "0.1.3" +version = "0.3.0" dependencies = [ "glib", "gstreamer", diff --git a/src/lib.rs b/src/lib.rs index a25fad8..3d7a9bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ //! use iced_video_player::{Video, VideoPlayer}; //! use iced::{Sandbox, Element}; //! +//! # #![allow(clippy::needless_doctest_main)] //! fn main() { //! App::run(Default::default()); //! } diff --git a/src/video.rs b/src/video.rs index ba62d92..0adacd0 100644 --- a/src/video.rs +++ b/src/video.rs @@ -1,8 +1,7 @@ use crate::Error; -use gst::prelude::*; -use gst_base::prelude::*; use gstreamer as gst; use gstreamer_app as gst_app; +use gstreamer_app::prelude::*; use gstreamer_base as gst_base; use iced::widget::image as img; use std::cell::RefCell; @@ -118,16 +117,34 @@ impl Video { Self::from_pipeline(pipeline, None) } + /// Creates a new video based on GStreamer pipeline in a same format as used in gst-launch-1.0. + /// Expects an appsink plugin to be present with name set to `iced_video` and caps to + /// `video/x-raw,format=RGBA,pixel-aspect-ratio=1/1` pub fn from_pipeline>(pipeline: S, is_live: Option) -> Result { - static NEXT_ID: AtomicU64 = AtomicU64::new(0); - let id = NEXT_ID.fetch_add(1, Ordering::SeqCst); - - gst::init()?; + if !gst::INITIALIZED.load(Ordering::SeqCst) { + gst::init()?; + } let pipeline = gst::parse::launch(pipeline.as_ref())? .downcast::() .map_err(|_| Error::Cast)?; + Self::from_gst_pipeline(pipeline, is_live) + } + + /// Creates a new video based on GStreamer pipeline. + /// Expects an appsink plugin to be present with name set to `iced_video` and caps to + /// `video/x-raw,format=RGBA,pixel-aspect-ratio=1/1` + pub fn from_gst_pipeline( + pipeline: gst::Pipeline, + is_live: Option, + ) -> Result { + static NEXT_ID: AtomicU64 = AtomicU64::new(0); + let id = NEXT_ID.fetch_add(1, Ordering::SeqCst); + if !gst::INITIALIZED.load(Ordering::SeqCst) { + gst::init()?; + } + let mut live = false; match is_live {