From 641417764cd4d7cd59772999ddf1801309b1dba7 Mon Sep 17 00:00:00 2001 From: NiiightmareXD Date: Sun, 21 Apr 2024 20:08:32 +0330 Subject: [PATCH] =?UTF-8?q?Added=20`send=5Fframe=5Fbuffer`=20=F0=9F=90=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/capture.rs | 18 +++-------- src/encoder.rs | 85 +++++++++++++++++++++++++------------------------- 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/capture.rs b/src/capture.rs index 00cb375..e1b06d1 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -191,8 +191,6 @@ pub enum GraphicsCaptureApiError { FailedToJoinThread, #[error("Failed to initialize WinRT")] FailedToInitWinRT, - #[error("Failed to translate message")] - FailedToTranslateMessage, #[error("Failed to create dispatcher queue controller")] FailedToCreateDispatcherQueueController, #[error("Failed to shutdown dispatcher queue")] @@ -273,9 +271,7 @@ pub trait GraphicsCaptureApiHandler: Sized { let mut message = MSG::default(); unsafe { while GetMessageW(&mut message, None, 0, 0).as_bool() { - TranslateMessage(&message) - .ok() - .map_err(|_| GraphicsCaptureApiError::FailedToTranslateMessage)?; + let _ = TranslateMessage(&message); DispatchMessageW(&message); } } @@ -297,9 +293,7 @@ pub trait GraphicsCaptureApiHandler: Sized { let mut message = MSG::default(); unsafe { while GetMessageW(&mut message, None, 0, 0).as_bool() { - TranslateMessage(&message) - .ok() - .map_err(|_| GraphicsCaptureApiError::FailedToTranslateMessage)?; + let _ = TranslateMessage(&message); DispatchMessageW(&message); } } @@ -390,9 +384,7 @@ pub trait GraphicsCaptureApiHandler: Sized { let mut message = MSG::default(); unsafe { while GetMessageW(&mut message, None, 0, 0).as_bool() { - TranslateMessage(&message) - .ok() - .map_err(|_| GraphicsCaptureApiError::FailedToTranslateMessage)?; + let _ = TranslateMessage(&message); DispatchMessageW(&message); } } @@ -417,9 +409,7 @@ pub trait GraphicsCaptureApiHandler: Sized { let mut message = MSG::default(); unsafe { while GetMessageW(&mut message, None, 0, 0).as_bool() { - TranslateMessage(&message) - .ok() - .map_err(|_| GraphicsCaptureApiError::FailedToTranslateMessage)?; + let _ = TranslateMessage(&message); DispatchMessageW(&message); } } diff --git a/src/encoder.rs b/src/encoder.rs index 0b18b49..9665c86 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -178,7 +178,7 @@ pub enum VideoEncoderQuality { /// The `VideoEncoderSource` struct represents all the types that can be send to the encoder. pub enum VideoEncoderSource { DirectX(SendDirectX), - Buffer((SendDirectX<*mut u8>, usize)), + Buffer((SendDirectX<*const u8>, usize)), } /// The `VideoEncoder` struct represents a video encoder that can be used to encode video frames and save them to a specified file path. @@ -542,8 +542,7 @@ impl VideoEncoder { TimeSpan { Duration: 0 } } }; - - let surface = SendDirectX(unsafe { frame.as_raw_surface() }); + let surface = SendDirectX::new(unsafe { frame.as_raw_surface() }); self.frame_sender .send(Some((VideoEncoderSource::DirectX(surface), timespan)))?; @@ -567,46 +566,46 @@ impl VideoEncoder { Ok(()) } - // /// Sends a video frame to the video encoder for encoding. - // /// - // /// # Arguments - // /// - // /// * `frame` - A mutable reference to the `Frame` to be encoded. - // /// - // /// # Returns - // /// - // /// Returns `Ok(())` if the frame is successfully sent for encoding, or a `VideoEncoderError` - // /// if an error occurs. - // pub fn send_frame_buffer( - // &mut self, - // frame: &mut Frame, - // timespan: i64, - // ) -> Result<(), VideoEncoderError> { - // let timespan = TimeSpan { Duration: timespan }; - - // let surface = SendDirectX(unsafe { frame.as_raw_surface() }); - - // self.frame_sender - // .send(Some((VideoEncoderSource::DirectX(surface), timespan)))?; - - // let (lock, cvar) = &*self.frame_notify; - // let mut processed = lock.lock(); - // if !*processed { - // cvar.wait(&mut processed); - // } - // *processed = false; - // drop(processed); - - // if self.error_notify.load(atomic::Ordering::Relaxed) { - // if let Some(transcode_thread) = self.transcode_thread.take() { - // transcode_thread - // .join() - // .expect("Failed to join transcode thread")?; - // } - // } - - // Ok(()) - // } + /// Sends a video frame to the video encoder for encoding. + /// + /// # Arguments + /// + /// * `buffer` - A reference to the byte slice to be encoded Windows API expect this to be Bgra and bottom-top. + /// + /// # Returns + /// + /// Returns `Ok(())` if the frame is successfully sent for encoding, or a `VideoEncoderError` + /// if an error occurs. + pub fn send_frame_buffer( + &mut self, + buffer: &[u8], + timespan: i64, + ) -> Result<(), VideoEncoderError> { + let timespan = TimeSpan { Duration: timespan }; + + self.frame_sender.send(Some(( + VideoEncoderSource::Buffer((SendDirectX::new(buffer.as_ptr()), buffer.len())), + timespan, + )))?; + + let (lock, cvar) = &*self.frame_notify; + let mut processed = lock.lock(); + if !*processed { + cvar.wait(&mut processed); + } + *processed = false; + drop(processed); + + if self.error_notify.load(atomic::Ordering::Relaxed) { + if let Some(transcode_thread) = self.transcode_thread.take() { + transcode_thread + .join() + .expect("Failed to join transcode thread")?; + } + } + + Ok(()) + } /// Finishes encoding the video and performs any necessary cleanup. ///