pub struct Session { /* private fields */ }
Expand description

An encoding session to create input/output buffers and encode frames.

You need to call Encoder::start_session before you can encode frames using the session. On drop, the session will automatically send an empty EOS frame to flush the encoder.

Implementations§

source§

impl Session

Functions for creating input and output buffers.

source

pub fn create_input_buffer(&self) -> Result<Buffer<'_>, EncodeError>

Create a Buffer.

See NVIDIA docs.

Errors

Could error if the width, height, or buffer_format is invalid, or if we run out of memory.

Examples
//* Create encoder. *//

//* Set `encode_guid` and `buffer_format`, and check that H.264 encoding and the ARGB format are supported. *//

//* Begin encoder session. *//

// Create an input buffer.
let _input_buffer = session
    .create_input_buffer()
    .unwrap();
source

pub fn create_output_bitstream(&self) -> Result<Bitstream<'_>, EncodeError>

Create a Bitstream.

See NVIDIA docs.

Errors

Could error is we run out of memory.

Examples
//* Create encoder. *//

//* Set `encode_guid` and `buffer_format`, and check that H.264 encoding and the ARGB format are supported. *//

//* Begin encoder session. *//

// Create an output bitstream buffer.
let _output_bitstream = session
    .create_output_bitstream()
    .unwrap();
source

pub fn register_cuda_resource( &self, pitch: u32, mapped_buffer: MappedBuffer ) -> Result<RegisteredResource<'_, MappedBuffer>, EncodeError>

Create a RegisteredResource from a [MappedBuffer].

See Session::register_generic_resource.

pitch should be set to the value obtained from cuMemAllocPitch(), or to the width in bytes (if this resource was created by using cuMemAlloc()). This value must be a multiple of 4.

Errors

Could error if registration or mapping fails, if the resource is invalid, or if we run out of memory.

source

pub fn register_generic_resource<T>( &self, marker: T, resource_type: NV_ENC_INPUT_RESOURCE_TYPE, resource_to_register: *mut c_void, pitch: u32 ) -> Result<RegisteredResource<'_, T>, EncodeError>

Create a RegisteredResource.

This function is generic in the marker. This is so that you can optionally put a value on the RegisteredResource to make sure that value does not get dropped while the resource is registered. You should prefer using specific functions for the resource you are registering, such as Session::register_cuda_resource, when they are available.

See NVIDIA docs.

Errors

Could error if registration or mapping fails, if the resource is invalid, or if we run out of memory.

source§

impl Session

source

pub fn get_encoder(&self) -> &Encoder

Get the encoder used for this session.

This might be useful if you want to use some of the functions on Encoder.

Examples
//* Create encoder. *//

//* Set `encode_guid` and check that H.264 encoding is supported. *//

let session = encoder
    .start_session(
        NV_ENC_BUFFER_FORMAT_ARGB,
        NV_ENC_INITIALIZE_PARAMS::new(encode_guid, 1920, 1080),
    )
    .unwrap();
// We can still use the encoder like this:
let _input_formats = session
    .get_encoder()
    .get_supported_input_formats(encode_guid);
source

pub fn encode_picture<I: EncoderInput, O: EncoderOutput>( &self, input_buffer: &mut I, output_bitstream: &mut O, params: EncodePictureParams ) -> Result<(), EncodeError>

Encode a frame.

See NVIDIA docs.

Errors

Could error if the encode picture parameters were invalid or otherwise incorrect, or if we run out memory.

There are two recoverable errors:

  • If this returns an error with ErrorKind::EncoderBusy then you should retry after a few milliseconds.
  • If this returns an error with ErrorKind::NeedMoreInput, the client should not lock the output bitstream yet. They should continue encoding until this function returns Ok, and then lock the bitstreams in the order in which they were originally used.
Panics

Panics if codec specific parameters are provided for a different codec than the one used in the session.

Examples
//* Create encoder. *//

//* Set `encode_guid` and `buffer_format`, and check that H.264 encoding and the ARGB format are supported. *//

// Begin encoder session.
let mut initialize_params = NV_ENC_INITIALIZE_PARAMS::new(encode_guid, WIDTH, HEIGHT);
initialize_params.display_aspect_ratio(16, 9)
    .framerate(30, 1)
    .enable_picture_type_decision();
let session = encoder.start_session(
    buffer_format,
    initialize_params,
).unwrap();

//* Create input and output buffers. *//

// Encode frame.
unsafe { input_buffer.lock().unwrap().write(&[0; DATA_LEN]) };
session
    .encode_picture(
        &mut input_buffer,
        &mut output_bitstream,
        // Optional picture parameters
        EncodePictureParams {
            input_timestamp: 42,
            ..Default::default()
        }
    )
    .unwrap();
let _data = output_bitstream.lock().unwrap().data();
source

pub fn end_of_stream(&self) -> Result<(), EncodeError>

Send an EOS notifications to flush the encoder.

This function is called automatically on drop, but if you wish to get the data after flushing, you should call this function yourself.

Errors

Could error if we run out of memory.

If this returns an error with ErrorKind::EncoderBusy then you should retry after a few milliseconds.

Trait Implementations§

source§

impl Debug for Session

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Session

Send an EOS notifications on drop to flush the encoder.

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.