Don't call gst::init if initialized, add non-str pipeline support

This commit is contained in:
Vladimir Romashchenko 2024-09-19 21:18:18 -04:00
parent 11631efd12
commit 4400f3a7a1
No known key found for this signature in database
GPG key ID: E5B7EA4A9E1D48F4
3 changed files with 25 additions and 7 deletions

2
Cargo.lock generated
View file

@ -1419,7 +1419,7 @@ dependencies = [
[[package]]
name = "iced_video_player"
version = "0.1.3"
version = "0.3.0"
dependencies = [
"glib",
"gstreamer",

View file

@ -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());
//! }

View file

@ -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<S: AsRef<str>>(pipeline: S, is_live: Option<bool>) -> Result<Self, Error> {
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 pipeline = gst::parse::launch(pipeline.as_ref())?
.downcast::<gst::Pipeline>()
.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<bool>,
) -> Result<Self, Error> {
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 {