Skip to main content

FrameSource

Trait FrameSource 

Source
pub trait FrameSource: Send + Sync {
    // Required methods
    fn id(&self) -> &FrameSourceId;
    fn frames<'life0, 'async_trait>(
        &'life0 self,
        sampling: Sampling,
    ) -> Pin<Box<dyn Future<Output = Result<FrameStream>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}
Expand description

A source of decoded frames: a camera, a file, a screen, a browser or the web home’s camera (spec 3.7 §7.13).

§Example

use adk_vision::stream::{FrameSource, FrameStream};
use adk_vision::{DecodedImage, Frame, FramePixels, FrameSourceId, Sampling};
use futures::StreamExt;
use std::sync::Arc;
use std::time::Duration;

/// Ten grey frames, 100 ms apart, sampled at the rate asked for.
struct Grey(FrameSourceId);

#[async_trait::async_trait]
impl FrameSource for Grey {
    fn id(&self) -> &FrameSourceId {
        &self.0
    }

    async fn frames(&self, sampling: Sampling) -> adk_vision::Result<FrameStream> {
        let mut sampler = adk_vision::stream::Sampler::new(sampling)?;
        let source = self.0.clone();
        let frames = (0..10u64).filter_map(move |index| {
            let pts = Duration::from_millis(index * 100);
            sampler.admit(pts).then(|| {
                Ok(Frame {
                    source: source.clone(),
                    index,
                    pts,
                    captured_at: chrono::Utc::now(),
                    pixels: FramePixels::Ephemeral(Arc::new(DecodedImage::filled(4, 4, [128; 4]))),
                })
            })
        });
        Ok(futures::stream::iter(frames.collect::<Vec<_>>()).boxed())
    }
}

let source = Grey(FrameSourceId("grey".into()));
let frames: Vec<_> = source
    .frames(Sampling { fps: 5.0, max_long_edge: None })
    .await
    .unwrap()
    .collect()
    .await;
// 10 frames a second sampled at 5 a second.
assert_eq!(frames.len(), 5);

Required Methods§

Source

fn id(&self) -> &FrameSourceId

The source’s identifier, which names it in frames, records and wakes. It never holds a credential.

Source

fn frames<'life0, 'async_trait>( &'life0 self, sampling: Sampling, ) -> Pin<Box<dyn Future<Output = Result<FrameStream>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Decoded frames at the requested rate and size; the source drops what it cannot keep up with.

§Errors

VisionError::InvalidInput for a sampling rate that is not a positive number, or the source’s own refusal to open: an unreachable camera, a missing ffmpeg, a file that is not a video.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§