Skip to content

Commit

Permalink
Added send_frame_buffer 🐳
Browse files Browse the repository at this point in the history
  • Loading branch information
NiiightmareXD committed Apr 21, 2024
1 parent 62a313f commit 6414177
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 57 deletions.
18 changes: 4 additions & 14 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ pub enum GraphicsCaptureApiError<E> {
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")]
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down
85 changes: 42 additions & 43 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDirect3DSurface>),
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.
Expand Down Expand Up @@ -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)))?;
Expand All @@ -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.
///
Expand Down

0 comments on commit 6414177

Please sign in to comment.