feat: Add keybinds to minimal example

This commit is contained in:
uttarayan21
2025-12-25 21:43:55 +05:30
parent a2491695b3
commit 4ed15c97f0
13 changed files with 156 additions and 113 deletions

View File

@@ -10,7 +10,37 @@ pub fn main() -> iced::Result {
)
.with(tracing_subscriber::EnvFilter::from_default_env())
.init();
iced::application(State::new, update, view).run()
iced::application(State::new, update, view)
.subscription(keyboard_event)
.run()
}
fn keyboard_event(state: &State) -> iced::Subscription<Message> {
use iced::keyboard::{Key, key::Named};
iced::keyboard::listen().map(move |event| match event {
iced::keyboard::Event::KeyPressed { key, .. } => {
let key = key.as_ref();
match key {
Key::Named(Named::Escape) | Key::Character("q") => Message::Quit,
Key::Named(Named::Space) => Message::Toggle,
_ => Message::Load,
}
// if key == &space {
// // Toggle play/pause
// let is_playing = state
// .video
// .source()
// .is_playing()
// .expect("Failed to get playing state");
// if is_playing {
// Message::Pause
// } else {
// Message::Play
// }
// }
}
_ => Message::Load,
})
}
#[derive(Debug, Clone)]
@@ -22,6 +52,8 @@ impl State {
pub fn new() -> Self {
let video = VideoHandle::new("https://jellyfin.tsuba.darksailor.dev/Items/6010382cf25273e624d305907010d773/Download?api_key=036c140222464878862231ef66a2bc9c")
.expect("Failed to create video handle");
// let video = VideoHandle::new("file:///run/user/1000/gvfs/smb-share:server=tsuba.darksailor.dev,share=nas/Movies/Spider-Man - No Way Home (2021)/Spider-Man.No.Way.Home.2021.UHD.BluRay.2160p.TrueHD.Atmos.7.1.DV.HEVC.REMUX-FraMeSToR.mkv")
// .expect("Failed to create video handle");
Self { video }
}
}
@@ -30,16 +62,18 @@ impl State {
pub enum Message {
Play,
Pause,
Loaded,
Toggle,
Load,
Quit,
}
pub fn update(state: &mut State, message: Message) -> iced::Task<Message> {
match message {
Message::Load => {
// does stuff
let src = state.video.source().clone();
iced::Task::perform(src.wait(), |_| Message::Loaded)
// let src = state.video.source().clone();
// iced::Task::perform(src.wait(), |_| Message::Loaded)
iced::Task::none()
}
Message::Play => {
state.video.source().play().expect("Failed to play video");
@@ -49,10 +83,14 @@ pub fn update(state: &mut State, message: Message) -> iced::Task<Message> {
state.video.source().pause().expect("Failed to pause video");
iced::Task::none()
}
Message::Loaded => {
// Video loaded
Message::Toggle => {
state.video.source().toggle().expect("Failed to stop video");
iced::Task::none()
}
Message::Quit => {
state.video.source().stop().expect("Failed to stop video");
std::process::exit(0);
}
}
}

View File

@@ -9,7 +9,7 @@ pub struct VideoFrame {
pub id: id::Id,
pub size: wgpu::Extent3d,
pub ready: Arc<AtomicBool>,
pub frame: Arc<Mutex<gst::app::Sample>>,
pub frame: Arc<Mutex<gst::Sample>>,
}
impl iced_wgpu::Primitive for VideoFrame {
@@ -97,7 +97,6 @@ impl iced_wgpu::Primitive for VideoFrame {
video.bind_group = new_bind_group;
}
if video.ready.load(std::sync::atomic::Ordering::SeqCst) {
let now = std::time::Instant::now();
let frame = self.frame.lock().expect("BUG: Mutex poisoned");
let buffer = frame
.buffer()
@@ -110,7 +109,6 @@ impl iced_wgpu::Primitive for VideoFrame {
video
.ready
.store(false, std::sync::atomic::Ordering::SeqCst);
tracing::info!("{:?} Taken to write to surface texture", now.elapsed());
}
}
@@ -148,12 +146,7 @@ impl iced_wgpu::Primitive for VideoFrame {
view: target,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.1,
g: 0.2,
b: 0.3,
a: 1.0,
}),
load: wgpu::LoadOp::Load,
store: wgpu::StoreOp::Store,
},
depth_slice: None,

View File

@@ -13,11 +13,11 @@ use std::sync::{Arc, Mutex, atomic::AtomicBool};
#[derive(Debug, Clone)]
pub struct VideoSource {
pub(crate) playbin: Playbin3,
// pub(crate) videoconvert: VideoConvert,
pub(crate) videoconvert: VideoConvert,
pub(crate) appsink: AppSink,
pub(crate) bus: Bus,
pub(crate) ready: Arc<AtomicBool>,
pub(crate) frame: Arc<Mutex<gst::app::Sample>>,
pub(crate) frame: Arc<Mutex<gst::Sample>>,
}
impl VideoSource {
@@ -26,31 +26,35 @@ impl VideoSource {
/// now.
pub fn new(url: impl AsRef<str>) -> Result<Self> {
Gst::new();
// let videoconvert = VideoConvert::new("iced-video-convert")
// // .change_context(Error)?
// // .with_output_format(gst::plugins::videoconvertscale::VideoFormat::Rgba)
// .change_context(Error)?;
let appsink = AppSink::new("iced-video-sink")
.change_context(Error)?
.with_drop(true);
// .with_caps(
// Caps::builder(CapsType::Video)
// .field("format", "RGBA")
// .build(),
// );
// let video_sink = videoconvert.link(&appsink).change_context(Error)?;
let videoconvert = VideoConvert::new("iced-video-convert")
// .change_context(Error)?
// .with_output_format(gst::plugins::videoconvertscale::VideoFormat::Rgba)
.change_context(Error)?;
let mut appsink = AppSink::new("iced-video-sink").change_context(Error)?;
appsink
.drop(true)
.sync(true)
.async_(true)
.emit_signals(true)
.caps(
Caps::builder(CapsType::Video)
.field("format", "RGBA")
.build(),
);
let video_sink = videoconvert.link(&appsink).change_context(Error)?;
let playbin = gst::plugins::playback::Playbin3::new("iced-video")
.change_context(Error)?
.with_uri(url.as_ref())
.with_buffer_duration(core::time::Duration::from_secs(2))
.with_buffer_size(2000000)
.with_video_sink(&appsink);
.with_buffer_size(4096 * 4096 * 4 * 3)
.with_ring_buffer_max_size(4096 * 4096 * 4 * 3)
.with_video_sink(&video_sink);
let bus = playbin.bus().change_context(Error)?;
playbin.pause().change_context(Error)?;
let ready = Arc::new(AtomicBool::new(false));
let frame = Arc::new(Mutex::new(gst::app::Sample::new()));
let frame = Arc::new(Mutex::new(gst::Sample::new()));
let appsink = appsink.on_new_frame({
appsink.on_new_frame({
let ready = Arc::clone(&ready);
let frame = Arc::clone(&frame);
move |appsink| {
@@ -63,14 +67,13 @@ impl VideoSource {
core::mem::replace(&mut *guard, sample);
ready.store(true, std::sync::atomic::Ordering::Relaxed);
}
Ok(())
}
});
Ok(Self {
playbin,
// videoconvert,
videoconvert,
appsink,
bus,
ready,
@@ -86,6 +89,23 @@ impl VideoSource {
.attach("Failed to wait for video initialisation")
}
pub fn is_playing(&self) -> Result<bool> {
let state = self
.playbin
.state(core::time::Duration::from_millis(0))
.change_context(Error)?;
Ok(state == gst::State::Playing)
}
pub fn toggle(&self) -> Result<()> {
if self.is_playing()? {
self.pause()?;
} else {
self.play()?;
}
Ok(())
}
pub fn play(&self) -> Result<()> {
self.playbin
.play()
@@ -100,6 +120,13 @@ impl VideoSource {
.attach("Failed to pause video")
}
pub fn stop(&self) -> Result<()> {
self.playbin
.stop()
.change_context(Error)
.attach("Failed to stop video")
}
pub fn size(&self) -> Result<(i32, i32)> {
let caps = self
.appsink