cranpose-services 0.28.0

Multiplatform service contracts and platform integrations for Cranpose apps
Documentation
//! The Android decoder: an `AMediaCodec` fed by the app's thread and drained by a thread of its
//! own.
//!
//! Neither thread sleeps. The feeding thread waits in the codec for a free input buffer, and the
//! draining thread waits in the codec for an output. Each wakes when the codec has something,
//! and the draining thread also wakes at an interval to see that the decoder stopped.
#![expect(
    unsafe_code,
    reason = "AMediaCodec is a thread-safe C object that the ndk crate does not mark Send"
)]

use std::{
    collections::VecDeque,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    thread,
    time::{Duration, Instant},
};

use ndk::{
    media::{
        media_codec::{
            DequeuedInputBufferResult, DequeuedOutputBufferInfoResult, MediaCodec,
            MediaCodecDirection, OutputBuffer,
        },
        media_format::MediaFormat,
    },
    native_window::NativeWindow,
};
use parking_lot::Mutex;

use super::{
    AccessUnit, DecoderConfig, DecoderError, DecoderOutput, FrameInfo, VideoCodec, VideoFrame,
    admission::Admission,
    frame_queue::{FrameQueue, Frames},
    image_reader::{Acquired, Reader},
    pending_units::{PendingUnit, PendingUnits},
};

// How long the draining thread waits for an output before it checks that the decoder is still
// wanted. The wait ends at once when an output comes.
const OUTPUT_WAIT: Duration = Duration::from_millis(20);

// How long the draining thread waits for an output while a rendered picture's image has not
// reached the reader. The image comes a few milliseconds after the render. A reader that does not
// take it before the next picture's image comes can lose it.
const IMAGE_WAIT: Duration = Duration::from_millis(1);

// How long a rendered picture's image may take before the decoder counts the picture as dropped.
const IMAGE_LIMIT: Duration = Duration::from_millis(100);

// The reports of the pictures sent to a surface that the decoder keeps for the app.
const REPORTS: usize = 32;

// The operating rate that asks the codec for its top speed, in frames a second.
const OPERATING_RATE: i32 = i16::MAX as i32;

// An AMediaCodec that two threads use: one dequeues its inputs, and the other its outputs.
struct Codec(MediaCodec);

// SAFETY: AMediaCodec's calls are thread-safe. The decoder lets one thread wait for an input
// and another thread wait for an output.
unsafe impl Send for Codec {}
// SAFETY: as for Send
unsafe impl Sync for Codec {}

// What the feeding thread, the draining thread and the app's handles share.
struct Shared {
    pending: Mutex<PendingUnits>,
    queue: Arc<FrameQueue>,
    stop: AtomicBool,
    // The most pictures that wait for the app
    depth: usize,
}

// The feeding side of a decoder.
pub(super) struct Backend {
    codec: Arc<Codec>,
    shared: Arc<Shared>,
    admission: Admission,
    input_wait: Duration,
}

impl Backend {
    pub(super) fn new(config: DecoderConfig) -> Result<Self, DecoderError> {
        let DecoderConfig {
            codec: kind,
            width,
            height,
            output,
            input_wait,
        } = config;
        let (reader, depth) = match &output {
            DecoderOutput::Surface(_) => (None, 0),
            DecoderOutput::Frames { depth } => {
                let depth = (*depth).max(2);
                (Some(Arc::new(Reader::new(width, height, depth)?)), depth)
            }
        };
        let window = match (&output, &reader) {
            (DecoderOutput::Surface(surface), _) => surface.window(),
            (_, Some(reader)) => reader.window(),
            (_, None) => return Err(DecoderError::Unavailable),
        };
        let input_limit = (width as usize) * (height as usize);
        let codec = Arc::new(Codec(configure(kind, width, height, input_limit, window)?));
        let shared = Arc::new(Shared {
            pending: Mutex::new(PendingUnits::new()),
            queue: Arc::new(FrameQueue::new(depth, REPORTS)),
            stop: AtomicBool::new(false),
            depth,
        });
        let output = Output {
            codec: Arc::clone(&codec),
            shared: Arc::clone(&shared),
            reader,
        };
        thread::Builder::new()
            .name("video-decoder-output".into())
            .spawn(move || output.run())
            .map_err(|error| DecoderError::Failed(format!("the output thread: {error}")))?;
        Ok(Self {
            codec,
            shared,
            admission: Admission::new(input_limit),
            input_wait,
        })
    }

    pub(super) fn submit(&mut self, unit: AccessUnit<'_>) -> Result<(), DecoderError> {
        let pts_us = self.admission.check(&unit)?;
        let mut buffer = match self
            .codec
            .0
            .dequeue_input_buffer(self.input_wait)
            .map_err(|error| DecoderError::Failed(error.to_string()))?
        {
            DequeuedInputBufferResult::Buffer(buffer) => buffer,
            DequeuedInputBufferResult::TryAgainLater => {
                self.shared.queue.stats.refused();
                return Err(DecoderError::Full);
            }
        };
        let room = buffer.buffer_mut().len();
        // The unit is registered before the codec can output its picture
        self.shared.pending.lock().push(PendingUnit {
            pts_us,
            timestamp: unit.timestamp,
            metadata: unit.metadata,
            submitted: Instant::now(),
        });
        let size = if unit.data.len() <= room {
            buffer.buffer_mut()[..unit.data.len()].write_copy_of_slice(unit.data);
            unit.data.len()
        } else {
            0
        };
        self.codec
            .0
            .queue_input_buffer(buffer, 0, size, pts_us, 0)
            .map_err(|error| DecoderError::Failed(error.to_string()))?;
        if size < unit.data.len() {
            return Err(DecoderError::AccessUnitTooLarge {
                size: unit.data.len(),
                limit: room,
            });
        }
        self.admission.accept(pts_us);
        self.shared.queue.stats.submitted();
        Ok(())
    }

    pub(super) fn frames(&self) -> Frames {
        Frames(Arc::clone(&self.shared.queue))
    }
}

impl Drop for Backend {
    fn drop(&mut self) {
        // The draining thread stops the codec when it sees the flag, and ends. The drop does
        // not wait for it.
        self.shared.stop.store(true, Ordering::Release);
    }
}

// Makes a started decoder that renders into `window`. A codec that refuses the operating rate
// is made again without it.
fn configure(
    kind: VideoCodec,
    width: u32,
    height: u32,
    input_limit: usize,
    window: &NativeWindow,
) -> Result<MediaCodec, DecoderError> {
    let mut failure = String::new();
    for with_rate in [true, false] {
        let codec =
            MediaCodec::from_decoder_type(kind.mime()).ok_or(DecoderError::NoDecoder(kind))?;
        let mut format = MediaFormat::new();
        format.set_str("mime", kind.mime());
        format.set_i32("width", width as i32);
        format.set_i32("height", height as i32);
        format.set_i32("max-input-size", input_limit as i32);
        // The decoder outputs each picture at once and does not wait to reorder
        format.set_i32("low-latency", 1);
        format.set_i32("priority", 0);
        if with_rate {
            format.set_i32("operating-rate", OPERATING_RATE);
        }
        let started = codec
            .configure(&format, Some(window), MediaCodecDirection::Decoder)
            .and_then(|()| codec.start());
        match started {
            Ok(()) => return Ok(codec),
            Err(error) => {
                failure = error.to_string();
                log::warn!(
                    "video decoder: configure failed (operating rate {with_rate}): {failure}"
                );
            }
        }
    }
    Err(DecoderError::Configure(failure))
}

// A picture that left the codec for the reader, and waits for its image.
struct Rendered {
    pts_us: u64,
    info: FrameInfo,
    at: Instant,
}

// The draining thread's state.
struct Output {
    codec: Arc<Codec>,
    shared: Arc<Shared>,
    reader: Option<Arc<Reader>>,
}

impl Output {
    fn run(self) {
        // The pictures that left the codec and wait for their images, with their reports
        let mut released: VecDeque<Rendered> = VecDeque::with_capacity(self.shared.depth + 4);
        while !self.shared.stop.load(Ordering::Acquire) {
            let wait = if released.is_empty() {
                OUTPUT_WAIT
            } else {
                IMAGE_WAIT
            };
            match self.codec.0.dequeue_output_buffer(wait) {
                Ok(DequeuedOutputBufferInfoResult::Buffer(buffer)) => {
                    self.release(buffer, &mut released);
                }
                Ok(DequeuedOutputBufferInfoResult::TryAgainLater) => self.collect(&mut released),
                Ok(_) => {}
                Err(error) => {
                    log::error!("video decoder: output: {error}");
                    break;
                }
            }
        }
        // The codec stops before the reader goes
        if let Err(error) = self.codec.0.stop() {
            log::warn!("video decoder: stop: {error}");
        }
    }

    // Renders the picture of `buffer` and records it.
    fn release(&self, buffer: OutputBuffer<'_>, released: &mut VecDeque<Rendered>) {
        let pts_us = u64::try_from(buffer.info().presentation_time_us()).unwrap_or(0);
        let unit = self.shared.pending.lock().take(pts_us);
        let info = unit.map_or_else(
            || orphan(pts_us),
            |unit| FrameInfo {
                timestamp: unit.timestamp,
                metadata: unit.metadata,
                decode_time: unit.submitted.elapsed(),
            },
        );
        if let Err(error) = self.codec.0.release_output_buffer(buffer, true) {
            log::error!("video decoder: render: {error}");
            self.shared.queue.stats.dropped();
            return;
        }
        self.shared.queue.stats.decoded(info.decode_time);
        if self.reader.is_none() {
            let mut reports = self.shared.queue.reports.lock();
            if reports.len() == REPORTS {
                reports.pop_front();
            }
            reports.push_back(info);
            return;
        }
        if released.len() == released.capacity() {
            released.pop_front();
            self.shared.queue.stats.dropped();
        }
        released.push_back(Rendered {
            pts_us,
            info,
            at: Instant::now(),
        });
        self.collect(released);
        // A picture whose image has not come yet reaches the app after the draining thread's
        // next short wait
        if released.iter().any(|rendered| rendered.pts_us == pts_us) {
            self.shared.queue.stats.late();
        }
    }

    // Takes the images that the decoder rendered into the reader, and queues them for the app
    // with their reports.
    fn collect(&self, released: &mut VecDeque<Rendered>) {
        let Some(reader) = &self.reader else {
            return;
        };
        loop {
            match reader.acquire() {
                Acquired::Image(image) => {
                    let info = take_info(released, image.timestamp_ns());
                    let frame = VideoFrame::new(info, image.size(), image.crop(), image);
                    let mut frames = self.shared.queue.frames.lock();
                    if frames.len() >= self.shared.depth {
                        frames.pop_front();
                        self.shared.queue.stats.dropped();
                    }
                    frames.push_back(frame);
                    drop(frames);
                    self.shared.queue.wake();
                }
                Acquired::Empty => break,
                Acquired::Full => {
                    // The app and the queue hold every image. The oldest queued picture goes, so
                    // the newest comes in.
                    let mut frames = self.shared.queue.frames.lock();
                    if frames.pop_front().is_none() {
                        break;
                    }
                    self.shared.queue.stats.dropped();
                }
                Acquired::Failed(status) => {
                    log::error!("video decoder: image reader status {status}");
                    break;
                }
            }
        }
        // The reader lost the image of a picture that waits this long
        while released
            .front()
            .is_some_and(|rendered| rendered.at.elapsed() > IMAGE_LIMIT)
        {
            released.pop_front();
            self.shared.queue.stats.dropped();
        }
    }
}

// The report of the picture whose image has `timestamp_ns`. The codec's presentation time in
// microseconds is the image's time in nanoseconds divided by a thousand. An image whose time
// matches no report takes the oldest report.
fn take_info(released: &mut VecDeque<Rendered>, timestamp_ns: i64) -> FrameInfo {
    let pts_us = u64::try_from(timestamp_ns / 1000).unwrap_or(0);
    let index = released
        .iter()
        .position(|rendered| rendered.pts_us == pts_us)
        .unwrap_or(0);
    released
        .remove(index)
        .map_or_else(|| orphan(pts_us), |rendered| rendered.info)
}

// The report of a picture that no submitted unit claims.
fn orphan(pts_us: u64) -> FrameInfo {
    FrameInfo {
        timestamp: Duration::from_micros(pts_us),
        metadata: 0,
        decode_time: Duration::ZERO,
    }
}