Skip to main content

AllocatedReceiver

Struct AllocatedReceiver 

Source
pub struct AllocatedReceiver<M: ReceiverMode = RawCapture> { /* private fields */ }
Expand description

A receiver whose frames can be received into application memory.

The default RawCapture mode retains ordinary receiver capture. AllocatorService::attach_packets returns the PacketAttached mode, which captures through capture_any and capture_any_ref. receiver lends the receiver in its existing mode; controls permits exclusive stream controls and KVM without allowing that receiver to be replaced.

Dropping this owner, or taking the receiver back, returns it to the SDK’s own memory first. The receiver may live on in a RecvAdvertiser registration; allocations it still holds are freed through the process-wide token registry. The registration retains the exact receiver core, so resetting always reaches the original native instance.

Setting an allocator replaces the previous one for frames allocated from then on. Frames allocated earlier stay in the memory the earlier policy supplied, and that policy is kept until the SDK has freed the last of them. Only a call of the earlier policy still running when the replacement is installed is declined.

§Threading

Both returned modes are Send and Sync: capturing, changing allocators and dropping the owner can happen on any thread. Captures borrow it shared; changing allocators requires an exclusive borrow. The SDK calls policies on its own threads, possibly concurrently, and frees each allocation through the callback that created it. Registry access is synchronized.

§Examples

use std::time::Duration;
use grafton_ndi::{
    advanced::{AllocationBuffer, AllocationRequest, AllocatorLimits, AllocatorService},
    Receiver, ReceiverOptions, Ndi,
};

let ndi = Ndi::new()?;
let receiver = Receiver::new(&ndi, &ReceiverOptions::disconnected())?;
let service = AllocatorService::new(ndi.process_lifetime(), &AllocatorLimits::default())?;
let mut receiver = service.attach(receiver)?;

// Pad every video line to a multiple of 256 bytes.
receiver.set_video_allocator(|request: AllocationRequest| {
    let stride = request.stride_bytes().next_multiple_of(256);
    AllocationBuffer::zeroed(request.with_stride_bytes(stride)?)
})?;
if let Some(frame) = receiver.receiver().capture_video(Duration::from_secs(1))? {
    println!("line stride: {} bytes", frame.line_stride_bytes());
}

Application allocator code follows the ReceiveAllocator callback contract.

Implementations§

Source§

impl<M: ReceiverMode> AllocatedReceiver<M>

Source

pub fn receiver(&self) -> &Receiver<M>

The receiver, for capture permitted by its mode and shared controls.

Source

pub fn controls(&mut self) -> ReceiverControls<'_, M>

Borrow exclusive stream controls or start KVM without permitting receiver replacement. This allocates nothing and makes no SDK call. Capture through the KVM session while inputs are held.

Source

pub fn set_video_allocator<P: ReceiveAllocator>( &mut self, policy: P, ) -> Result<(), Rejected<P>>

Receive video into memory from policy.

§Errors

Returns the policy with Error::InvalidInput when the process already has AllocatorLimits::max_generations policies. The current allocator then stays in place.

Source

pub fn set_audio_allocator<P: ReceiveAllocator>( &mut self, policy: P, ) -> Result<(), Rejected<P>>

Receive audio into memory from policy.

§Errors

As for set_video_allocator.

Source

pub fn reset_video_allocator(&mut self)

Return to the SDK’s own video memory.

Source

pub fn reset_audio_allocator(&mut self)

Return to the SDK’s own audio memory.

Source§

impl AllocatedReceiver<RawCapture>

Source

pub fn into_receiver(self) -> Receiver

Return to the SDK’s own memory and give the receiver back.

Source§

impl AllocatedReceiver<PacketAttached>

Source

pub fn capture_any(&self, timeout: Duration) -> Result<Option<PacketItem>>

Wait up to timeout for the next frame or event; see PacketReceiver::capture_any.

§Errors

As for PacketReceiver::capture_any, including crate::Error::UnexpectedSdkResult once per failed status publication, whose event remains pending.

Source

pub fn capture_any_ref( &self, timeout: Duration, ) -> Result<Option<PacketItemRef<'_>>>

Borrow a packet directly from SDK or application-provided storage. A held item keeps its custom allocation charged until the SDK releases it after the item is dropped. See PacketReceiver::capture_any_ref.

§Errors

As for PacketReceiver::capture_any_ref, including crate::Error::UnexpectedSdkResult once per failed status publication, whose event remains pending.

Source

pub fn into_packet_receiver(self) -> PacketReceiver

Return to the SDK’s own memory and give the packet receiver back.

Trait Implementations§

Source§

impl<M: ReceiverMode> Debug for AllocatedReceiver<M>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.