From fa929641e55bc9e7b3263229cf9342c2a1be123f Mon Sep 17 00:00:00 2001 From: modeco80 Date: Mon, 9 Sep 2024 22:02:57 -0400 Subject: [PATCH] clean break video code into new module --- server/src/main.rs | 10 ++++++---- server/src/{ => video}/encoder_thread.rs | 19 ++++++++++--------- .../{ffmpeg.rs => video/h264_encoder_sw.rs} | 8 ++++---- server/src/video/mod.rs | 6 ++++++ 4 files changed, 26 insertions(+), 17 deletions(-) rename server/src/{ => video}/encoder_thread.rs (89%) rename server/src/{ffmpeg.rs => video/h264_encoder_sw.rs} (97%) create mode 100644 server/src/video/mod.rs diff --git a/server/src/main.rs b/server/src/main.rs index 00c1dd2..bb5c73a 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,9 +1,11 @@ -mod encoder_thread; -mod ffmpeg; +mod video; mod surface; mod types; mod vnc_engine; +use video::encoder_thread; +use video::ffmpeg; + use std::{ sync::{Arc, Mutex}, time::Duration, @@ -62,8 +64,8 @@ async fn main() -> anyhow::Result<()> { let (engine_input_tx, engine_input_rx) = mpsc::channel(16); // H.264 encoder related - let frame: Arc>> = Arc::new(Mutex::new(None)); - let (mut encoder_rx, encoder_tx) = encoder_thread::encoder_thread_spawn(&frame); + let frame: Arc>> = Arc::new(Mutex::new(None)); + let (mut encoder_rx, encoder_tx) = video::encoder_thread_spawn(&frame); let state = Arc::new(AppState::new(engine_input_tx, encoder_tx)); diff --git a/server/src/encoder_thread.rs b/server/src/video/encoder_thread.rs similarity index 89% rename from server/src/encoder_thread.rs rename to server/src/video/encoder_thread.rs index e8da40c..ec3f7ec 100644 --- a/server/src/encoder_thread.rs +++ b/server/src/video/encoder_thread.rs @@ -4,6 +4,9 @@ use std::{ }; use tokio::sync::mpsc::{self, error::TryRecvError}; +use super::ffmpeg; +use super::h264_encoder_sw::H264EncoderSW; + pub enum EncodeThreadInput { Init { size: crate::types::Size }, ForceKeyframe, @@ -12,20 +15,18 @@ pub enum EncodeThreadInput { #[derive(Clone)] pub enum EncodeThreadOutput { - Frame { packet: ffmpeg_the_third::Packet }, + Frame { packet: ffmpeg::Packet }, } #[inline] fn set_frame_flags(frame: &mut ffmpeg_the_third::Frame, force_keyframe: bool) { unsafe { if force_keyframe { - (*frame.as_mut_ptr()).pict_type = - ffmpeg_the_third::sys::AVPictureType::AV_PICTURE_TYPE_I; + (*frame.as_mut_ptr()).pict_type = ffmpeg::sys::AVPictureType::AV_PICTURE_TYPE_I; (*frame.as_mut_ptr()).flags = ffmpeg_the_third::sys::AV_FRAME_FLAG_KEY; (*frame.as_mut_ptr()).key_frame = 1; } else { - (*frame.as_mut_ptr()).pict_type = - ffmpeg_the_third::sys::AVPictureType::AV_PICTURE_TYPE_NONE; + (*frame.as_mut_ptr()).pict_type = ffmpeg::sys::AVPictureType::AV_PICTURE_TYPE_NONE; (*frame.as_mut_ptr()).flags = 0i32; (*frame.as_mut_ptr()).key_frame = 0; } @@ -35,11 +36,11 @@ fn set_frame_flags(frame: &mut ffmpeg_the_third::Frame, force_keyframe: bool) { fn encoder_thread_main( mut rx: mpsc::Receiver, tx: mpsc::Sender, - frame: &Arc>>, + frame: &Arc>>, ) { - let mut packet = ffmpeg_the_third::Packet::empty(); + let mut packet = ffmpeg::Packet::empty(); - let mut encoder: Option = None; + let mut encoder: Option = None; let mut sws = None; let mut yuv_frame = None; @@ -73,7 +74,7 @@ fn encoder_thread_main( ); encoder = Some( - crate::ffmpeg::H264Encoder::new(size, 60, 3 * (1000 * 1000)) + H264EncoderSW::new(size, 60, 3 * (1000 * 1000)) .expect("Failed to create encoder"), ); } diff --git a/server/src/ffmpeg.rs b/server/src/video/h264_encoder_sw.rs similarity index 97% rename from server/src/ffmpeg.rs rename to server/src/video/h264_encoder_sw.rs index afdfbf6..83d936a 100644 --- a/server/src/ffmpeg.rs +++ b/server/src/video/h264_encoder_sw.rs @@ -1,6 +1,6 @@ +use super::ffmpeg; use anyhow::Context; use ffmpeg::error::EAGAIN; -use ffmpeg_the_third as ffmpeg; use ffmpeg::codec as lavc; // lavc @@ -19,12 +19,12 @@ pub fn create_context_from_codec(codec: ffmpeg::Codec) -> Result anyhow::Result { let encoder = ffmpeg::encoder::find(lavc::Id::H264).expect("could not find libx264"); diff --git a/server/src/video/mod.rs b/server/src/video/mod.rs new file mode 100644 index 0000000..acc3bcd --- /dev/null +++ b/server/src/video/mod.rs @@ -0,0 +1,6 @@ +pub mod h264_encoder_sw; +pub mod encoder_thread; + +pub use ffmpeg_the_third as ffmpeg; + +pub use encoder_thread::*; \ No newline at end of file