-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code to add a SystemCapturer.
- Loading branch information
Showing
8 changed files
with
717 additions
and
92 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::os::raw::c_uint; | ||
use std::{mem::MaybeUninit, ffi::CStr}; | ||
|
||
use nvfbc_sys::_NVFBCSTATUS_NVFBC_SUCCESS as SUCCESS; | ||
use nvfbc_sys::NVFBC_SESSION_HANDLE; | ||
|
||
use crate::CaptureType; | ||
use crate::Error; | ||
use crate::Status; | ||
|
||
pub type Handle = NVFBC_SESSION_HANDLE; | ||
|
||
pub(crate) fn check_ret(handle: Handle, ret: nvfbc_sys::_NVFBCSTATUS) -> Result<(), Error> { | ||
if ret != SUCCESS { | ||
return Err(Error::new(ret, get_last_error(handle))); | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn create_handle() -> Result<nvfbc_sys::NVFBC_SESSION_HANDLE, Error> { | ||
let mut params: nvfbc_sys::_NVFBC_CREATE_HANDLE_PARAMS = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
params.dwVersion = nvfbc_sys::NVFBC_CREATE_HANDLE_PARAMS_VER; | ||
let mut handle = 0; | ||
let ret = unsafe { nvfbc_sys::NvFBCCreateHandle( | ||
&mut handle, | ||
&mut params | ||
)}; | ||
if ret != SUCCESS { | ||
return Err(Error::new(ret, None)); | ||
} | ||
|
||
Ok(handle) | ||
} | ||
|
||
pub(crate) fn destroy_handle(handle: Handle) -> Result<(), Error> { | ||
let mut params: nvfbc_sys::_NVFBC_DESTROY_HANDLE_PARAMS = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
params.dwVersion = nvfbc_sys::NVFBC_DESTROY_HANDLE_PARAMS_VER; | ||
check_ret(handle, unsafe { nvfbc_sys::NvFBCDestroyHandle(handle, &mut params) }) | ||
} | ||
|
||
pub(crate) fn get_last_error(handle: Handle) -> Option<String> { | ||
let error = unsafe { nvfbc_sys::NvFBCGetLastErrorStr(handle) }; | ||
let error = unsafe { CStr::from_ptr(error) }; | ||
error.to_str().ok().map(|e| e.to_string()) | ||
} | ||
|
||
pub(crate) fn status(handle: Handle) -> Result<Status, Error> { | ||
let mut params: nvfbc_sys::_NVFBC_GET_STATUS_PARAMS = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
params.dwVersion = nvfbc_sys::NVFBC_GET_STATUS_PARAMS_VER; | ||
check_ret(handle, unsafe { nvfbc_sys::NvFBCGetStatus(handle, &mut params) })?; | ||
Ok(params.into()) | ||
} | ||
|
||
pub(crate) fn create_capture_session(handle: Handle, capture_type: CaptureType) -> Result<(), Error> { | ||
let mut params: nvfbc_sys::_NVFBC_CREATE_CAPTURE_SESSION_PARAMS = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
params.dwVersion = nvfbc_sys::NVFBC_CREATE_CAPTURE_SESSION_PARAMS_VER; | ||
params.eCaptureType = capture_type as c_uint; | ||
params.bWithCursor = nvfbc_sys::_NVFBC_BOOL_NVFBC_TRUE; | ||
params.frameSize = nvfbc_sys::NVFBC_SIZE { w: 0, h: 0 }; | ||
params.eTrackingType = nvfbc_sys::NVFBC_TRACKING_TYPE_NVFBC_TRACKING_DEFAULT; | ||
check_ret(handle, unsafe { nvfbc_sys::NvFBCCreateCaptureSession(handle, &mut params) }) | ||
} | ||
|
||
pub(crate) fn destroy_capture_session(handle: Handle) -> Result<(), Error> { | ||
let mut params: nvfbc_sys::_NVFBC_DESTROY_CAPTURE_SESSION_PARAMS = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
params.dwVersion = nvfbc_sys::NVFBC_DESTROY_CAPTURE_SESSION_PARAMS_VER; | ||
check_ret(handle, unsafe { nvfbc_sys::NvFBCDestroyCaptureSession(handle, &mut params) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
mod types; | ||
mod error; | ||
mod common; | ||
mod cuda; | ||
mod error; | ||
mod system; | ||
mod types; | ||
|
||
pub use types::*; | ||
pub use error::Error; | ||
pub use cuda::CudaCapturer; | ||
pub use system::SystemCapturer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::ffi::c_void; | ||
use std::mem::MaybeUninit; | ||
use std::ptr::null_mut; | ||
|
||
use crate::common::{ | ||
Handle, | ||
check_ret, | ||
create_capture_session, | ||
create_handle, | ||
destroy_capture_session, | ||
destroy_handle, | ||
status, | ||
}; | ||
use crate::{ | ||
BufferFormat, | ||
Error, | ||
Status, | ||
CaptureType, | ||
SystemFrameInfo | ||
}; | ||
|
||
pub struct SystemCapturer { | ||
handle: Handle, | ||
buffer: *mut c_void, | ||
} | ||
|
||
impl SystemCapturer { | ||
pub fn new() -> Result<Self, Error> { | ||
let handle = create_handle()?; | ||
let self_ = Self { handle, buffer: null_mut() }; | ||
Ok(self_) | ||
} | ||
|
||
pub fn status(&self) -> Result<Status, Error> { | ||
status(self.handle) | ||
} | ||
|
||
pub fn start(&mut self, buffer_format: BufferFormat) -> Result<(), Error> { | ||
create_capture_session(self.handle, CaptureType::ToSystem)?; | ||
|
||
let mut params: nvfbc_sys::NVFBC_TOSYS_SETUP_PARAMS = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
params.dwVersion = nvfbc_sys::NVFBC_TOSYS_SETUP_PARAMS_VER; | ||
params.eBufferFormat = buffer_format as u32; | ||
params.ppBuffer = &mut self.buffer as *mut _; | ||
check_ret(self.handle, unsafe { nvfbc_sys::NvFBCToSysSetUp(self.handle, &mut params) }) | ||
} | ||
|
||
pub fn stop(&self) -> Result<(), Error> { | ||
destroy_capture_session(self.handle) | ||
} | ||
|
||
pub fn next(&self) -> Result<SystemFrameInfo, Error> { | ||
let mut frame_info: nvfbc_sys::NVFBC_FRAME_GRAB_INFO = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
let mut params: nvfbc_sys::NVFBC_TOSYS_GRAB_FRAME_PARAMS = unsafe { MaybeUninit::zeroed().assume_init() }; | ||
params.dwVersion = nvfbc_sys::NVFBC_TOSYS_GRAB_FRAME_PARAMS_VER; | ||
params.dwFlags = nvfbc_sys::NVFBC_TOSYS_GRAB_FLAGS_NVFBC_TOSYS_GRAB_FLAGS_NOFLAGS; | ||
params.pFrameGrabInfo = &mut frame_info; | ||
check_ret(self.handle, unsafe { nvfbc_sys::NvFBCToSysGrabFrame(self.handle, &mut params) })?; | ||
|
||
Ok(SystemFrameInfo { | ||
buffer: self.buffer, | ||
width: frame_info.dwWidth, | ||
height: frame_info.dwHeight, | ||
byte_size: frame_info.dwByteSize as usize, | ||
current_frame: frame_info.dwCurrentFrame, | ||
}) | ||
} | ||
} | ||
|
||
impl Drop for SystemCapturer { | ||
fn drop(&mut self) { | ||
// TODO: Figure out why this crashes (nvfbc examples also fail here..) | ||
destroy_handle(self.handle).ok(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters