From 1cafb91105b8b2b889f0327ab756dc5234068f3d Mon Sep 17 00:00:00 2001 From: jazzfool Date: Sat, 10 Oct 2020 13:05:34 +1100 Subject: [PATCH] support live streaming --- examples/minimal.rs | 5 ++++- src/lib.rs | 24 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/examples/minimal.rs b/examples/minimal.rs index 987f425..d7d9d71 100644 --- a/examples/minimal.rs +++ b/examples/minimal.rs @@ -36,6 +36,7 @@ impl Application for App { .unwrap(), ) .unwrap(), + false, ) .unwrap(), pause_btn: Default::default(), @@ -53,7 +54,9 @@ impl Application for App { Message::TogglePause => { self.video.set_paused(!self.video.paused()); } - Message::VideoPlayerMessage(msg) => self.video.update(msg), + Message::VideoPlayerMessage(msg) => { + self.video.update(msg); + } } Command::none() diff --git a/src/lib.rs b/src/lib.rs index 77010cd..22070fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -95,7 +95,11 @@ impl Drop for VideoPlayer { impl VideoPlayer { /// Create a new video player from a given video which loads from `uri`. - pub fn new(uri: &url::Url) -> Result { + /// + /// If `live` is set then no duration is queried (as this will result in an error and is non-sensical for live streams). + /// Set `live` if the streaming source is indefinite (e.g. a live stream). + /// Note that this will cause the duration to be zero. + pub fn new(uri: &url::Url, live: bool) -> Result { gst::init()?; let source = gst::parse_launch(&format!("playbin uri=\"{}\" video-sink=\"videoconvert ! videoscale ! appsink name=app_sink caps=video/x-raw,format=BGRA,pixel-aspect-ratio=1/1\"", uri.as_str()))?; @@ -179,13 +183,17 @@ impl VideoPlayer { .map_err(|_| Error::Caps)? .ok_or(Error::Caps)?; - let duration = std::time::Duration::from_nanos( - source - .query_duration::() - .ok_or(Error::Duration)? - .nanoseconds() - .ok_or(Error::Duration)?, - ); + let duration = if !live { + std::time::Duration::from_nanos( + source + .query_duration::() + .ok_or(Error::Duration)? + .nanoseconds() + .ok_or(Error::Duration)?, + ) + } else { + std::time::Duration::from_secs(0) + }; Ok(VideoPlayer { bus: source.get_bus().unwrap(),