Skip to content

Commit

Permalink
Remove thiserror dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
hgaiser committed Jun 20, 2022
1 parent c11120c commit 6781f28
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 145 deletions.
32 changes: 0 additions & 32 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion nvfbc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ repository = "https://github.com/hgaiser/nvfbc-rs"

[dependencies]
nvfbc-sys = { path = "../nvfbc-sys" }
thiserror = "1.0.24"
18 changes: 9 additions & 9 deletions nvfbc/src/cuda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl FrontBufferToCuda {
&mut params
)};
if ret != _NVFBCSTATUS_NVFBC_SUCCESS {
return Err(Error::InternalError(ret.into(), None));
return Err(Error::new(ret, None));
}

Ok(handle)
Expand All @@ -38,16 +38,16 @@ impl FrontBufferToCuda {
params.dwVersion = nvfbc_sys::NVFBC_DESTROY_HANDLE_PARAMS_VER;
let ret = unsafe { nvfbc_sys::NvFBCDestroyHandle(self.handle, &mut params) };
if ret != _NVFBCSTATUS_NVFBC_SUCCESS {
return Err(Error::InternalError(ret.into(), self.get_last_error().ok()));
return Err(Error::new(ret, self.get_last_error()));
}

Ok(())
}

fn get_last_error(&self) -> Result<String, Error> {
fn get_last_error(&self) -> Option<String> {
let error = unsafe { nvfbc_sys::NvFBCGetLastErrorStr(self.handle) };
let error = unsafe { CStr::from_ptr(error) };
Ok(error.to_str()?.to_string())
error.to_str().ok().map(|e| e.to_string())
}

fn setup(&self, buffer_format: BufferFormat) -> Result<(), Error> {
Expand All @@ -56,7 +56,7 @@ impl FrontBufferToCuda {
params.eBufferFormat = buffer_format as u32;
let ret = unsafe { nvfbc_sys::NvFBCToCudaSetUp(self.handle, &mut params) };
if ret != _NVFBCSTATUS_NVFBC_SUCCESS {
return Err(Error::InternalError(ret.into(), self.get_last_error().ok()));
return Err(Error::new(ret, self.get_last_error()));
}

Ok(())
Expand All @@ -67,7 +67,7 @@ impl FrontBufferToCuda {
params.dwVersion = nvfbc_sys::NVFBC_GET_STATUS_PARAMS_VER;
let ret = unsafe { nvfbc_sys::NvFBCGetStatus(self.handle, &mut params) };
if ret != _NVFBCSTATUS_NVFBC_SUCCESS {
return Err(Error::InternalError(ret.into(), self.get_last_error().ok()));
return Err(Error::new(ret, self.get_last_error()));
}

Ok(params.into())
Expand All @@ -82,7 +82,7 @@ impl FrontBufferToCuda {
params.eTrackingType = nvfbc_sys::NVFBC_TRACKING_TYPE_NVFBC_TRACKING_DEFAULT;
let ret = unsafe { nvfbc_sys::NvFBCCreateCaptureSession(self.handle, &mut params) };
if ret != _NVFBCSTATUS_NVFBC_SUCCESS {
return Err(Error::InternalError(ret.into(), self.get_last_error().ok()));
return Err(Error::new(ret, self.get_last_error()));
}

Ok(())
Expand All @@ -93,7 +93,7 @@ impl FrontBufferToCuda {
params.dwVersion = nvfbc_sys::NVFBC_DESTROY_CAPTURE_SESSION_PARAMS_VER;
let ret = unsafe { nvfbc_sys::NvFBCDestroyCaptureSession(self.handle, &mut params) };
if ret != _NVFBCSTATUS_NVFBC_SUCCESS {
return Err(Error::InternalError(ret.into(), self.get_last_error().ok()));
return Err(Error::new(ret, self.get_last_error()));
}

Ok(())
Expand All @@ -109,7 +109,7 @@ impl FrontBufferToCuda {
params.pCUDADeviceBuffer = &mut device_buffer as *mut _ as *mut c_void;
let ret = unsafe { nvfbc_sys::NvFBCToCudaGrabFrame(self.handle, &mut params) };
if ret != _NVFBCSTATUS_NVFBC_SUCCESS {
return Err(Error::InternalError(ret.into(), self.get_last_error().ok()));
return Err(Error::new(ret, self.get_last_error()));
}

Ok(CudaFrameInfo {
Expand Down
141 changes: 38 additions & 103 deletions nvfbc/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,111 +1,46 @@
use nvfbc_sys::NVFBCSTATUS;
use thiserror::Error;
use std::fmt;

#[derive(Error, Debug)]
pub enum Error {
#[error("internal nvfbc error")]
InternalError(NvFbcSysError, Option<String>),

#[error("a utf-8 error occured")]
Utf8(#[from] std::str::Utf8Error),
#[derive(Debug)]
pub struct Error {
code: u32,
message: Option<String>,
}

#[derive(Error, Debug)]
pub enum NvFbcSysError {
/// This indicates that the API version between the client and the library is not compatible.
#[error("invalid API version")]
ApiVersion = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_API_VERSION as isize,
/// An internal error occurred.
#[error("internal error occurred")]
Internal = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INTERNAL as isize,
/// This indicates that one or more of the parameter passed to the API call is invalid.
#[error("received invalid parameter")]
InvalidParam = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INVALID_PARAM as isize,
/// This indicates that one or more of the pointers passed to the API call is invalid.
#[error("received invalid pointer")]
InvalidPtr = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INVALID_PTR as isize,
/// This indicates that the handle passed to the API call to identify the client is invalid.
#[error("received invalid handle")]
InvalidHandle = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INVALID_HANDLE as isize,
/// This indicates that the maximum number of threaded clients of the same process has been reached.
/// The limit is 10 threads per process. There is no limit on the number of process.
#[error("reached maximum number of threaded clients")]
MaxClients = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_MAX_CLIENTS as isize,
/// This indicates that the requested feature is not currently supported by the library.
#[error("the requested feature is unsupported")]
Unsupported = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_UNSUPPORTED as isize,
/// This indicates that the API call failed because it was unable to allocate
/// enough memory to perform the requested operation.
#[error("unable to allocate enough memory")]
OutOfMemory = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_OUT_OF_MEMORY as isize,
/// This indicates that the API call was not expected.
/// This happens when API calls are performed in a wrong order,
/// such as trying to capture a frame prior to creating a new capture session;
/// or trying to set up a capture to video memory although a capture session to system memory was created.
#[error("received unexpected API call")]
BadRequest = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_BAD_REQUEST as isize,
/// This indicates an X error, most likely meaning that the X server has
/// been terminated. When this error is returned, the only resort is to
/// create another FBC handle using NvFBCCreateHandle().
///
/// The previous handle should still be freed with NvFBCDestroyHandle(), but
/// it might leak resources, in particular X, GLX, and GL resources since
/// it is no longer possible to communicate with an X server to free them
/// through the driver.
///
/// The best course of action to eliminate this potential leak is to close
/// the OpenGL driver, close the forked process running the capture, or
/// restart the application.
#[error("an X error occured")]
X = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_X as isize,
/// This indicates a GLX error.
#[error("a GLX error occured")]
Glx = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_GLX as isize,
/// This indicates an OpenGL error.
#[error("an OpenGL error occured")]
Gl = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_GL as isize,
/// This indicates a CUDA error.
#[error("a CUDA error occured")]
Cuda = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_CUDA as isize,
/// This indicates a HW encoder error.
#[error("an encoder error occured")]
Encoder = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_ENCODER as isize,
/// This indicates an NvFBC context error.
#[error("an NvFBC context error occured")]
Context = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_CONTEXT as isize,
/// This indicates that the application must recreate the capture session.
///
/// This error can be returned if a modeset event occurred while capturing
/// frames, and NVFBC_CREATE_HANDLE_PARAMS::bDisableAutoModesetRecovery
/// was set to NVFBC_TRUE.
#[error("must recreate capture session")]
MustRecreate = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_MUST_RECREATE as isize,
/// This indicates a Vulkan error.
#[error("a vulkan error occured")]
Vulkan = nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_VULKAN as isize,
impl Error {
pub fn new(code: u32, message: Option<String>) -> Self {
Error { code, message }
}
}

impl From<NVFBCSTATUS> for NvFbcSysError {
fn from(error: NVFBCSTATUS) -> Self {
match error {
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_API_VERSION => NvFbcSysError::ApiVersion,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INTERNAL => NvFbcSysError::Internal,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INVALID_PARAM => NvFbcSysError::InvalidParam,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INVALID_PTR => NvFbcSysError::InvalidPtr,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INVALID_HANDLE => NvFbcSysError::InvalidHandle,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_MAX_CLIENTS => NvFbcSysError::MaxClients,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_UNSUPPORTED => NvFbcSysError::Unsupported,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_OUT_OF_MEMORY => NvFbcSysError::OutOfMemory,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_BAD_REQUEST => NvFbcSysError::BadRequest,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_X => NvFbcSysError::X,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_GLX => NvFbcSysError::Glx,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_GL => NvFbcSysError::Gl,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_CUDA => NvFbcSysError::Cuda,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_ENCODER => NvFbcSysError::Encoder,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_CONTEXT => NvFbcSysError::Context,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_MUST_RECREATE => NvFbcSysError::MustRecreate,
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_VULKAN => NvFbcSysError::Vulkan,
_ => panic!("Unknown error code: {}", error),
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let error_code_message = match self.code {
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_API_VERSION => "The API version between the client and the library is not compatible".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INTERNAL => "An internal error occurred".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INVALID_PARAM => "One or more of the parameter passed to the API call is invalid".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INVALID_PTR => "One or more of the pointers passed to the API call is invalid".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_INVALID_HANDLE => "The handle passed to the API call to identify the client is invalid".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_MAX_CLIENTS => "The maximum number of threaded clients (10) of the same process has been reached".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_UNSUPPORTED => "The requested feature is not currently supported by the library".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_OUT_OF_MEMORY => "Unable to allocate enough memory to perform the requested operation".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_BAD_REQUEST => "The API call was not expected".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_X => "An unknown X error has occurred".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_GLX => "An unknown GLX error has occurred".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_GL => "An unknown OpenGL error has occurred".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_CUDA => "An unknown CUDA error has occurred".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_ENCODER => "A hardware encoder error has occurred".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_CONTEXT => "An NVFBC context error has occurred".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_MUST_RECREATE => "The capture session must be recreated".to_string(),
nvfbc_sys::_NVFBCSTATUS_NVFBC_ERR_VULKAN => "A Vulkan error has occurred".to_string(),
code => format!("Un unknown error code ({}) was returned", code),
};

if let Some(message) = &self.message {
write!(f, "{}: {}", error_code_message, message)?;
} else {
write!(f, "{}", error_code_message)?;
}

Ok(())
}
}

0 comments on commit 6781f28

Please sign in to comment.