alpha2-host-core 0.6.1

FFmpeg-free native host logic for alpha2: the policies, audio timelines, cursor rules, providers, plugin runtime, protocol frames and request parsing native hosts share, and the decode, export and still pipeline the mobile hosts run.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
//! Encoder sessions behind traits, and the sink that turns an export's pictures and mixed audio
//! into a container's tracks.

use std::collections::VecDeque;
use std::time::{Duration, Instant};

use alpha2_container::{
    AudioCodec, AudioTrack, ContainerWriter, Packet, TrackId, TrackInfo, TrackKind, VideoCodec,
    VideoTrack,
};
use alpha2_media::{
    AudioBlock, AudioBlockSink, CancelToken, ColorMetadata, EncodeError, Orientation, PixelLayout,
    SpeakerLayout, VideoRateControl,
};
use alpha2_time::FrameRate;
use bytes::Bytes;

use crate::audio_frames::EncoderAudioInput;
use crate::rate_control::AppliedRateControl;
use crate::video_stream::Submit;

/// One plane of a rendered picture.
#[derive(Clone, Debug, Default)]
pub struct PlaneBuffer {
    /// The rows, `stride` bytes apart.
    pub bytes: Vec<u8>,
    /// Bytes between rows.
    pub stride: usize,
    /// Width in samples.
    pub width: u32,
    /// Height in rows.
    pub height: u32,
}

/// What a video encoder is asked for.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct VideoEncodeConfig {
    /// The codec.
    pub codec: alpha2_media::VideoCodec,
    /// Picture width in pixels.
    pub width: u32,
    /// Picture height in pixels.
    pub height: u32,
    /// Frames per second.
    pub frame_rate: FrameRate,
    /// The planes each frame arrives in.
    pub layout: PixelLayout,
    /// The rate control the export resolved.
    pub rate: VideoRateControl,
    /// The color the bitstream is tagged with.
    pub color: ColorMetadata,
    /// Frames between forced keyframes.
    pub keyframe_interval: u32,
    /// Ask for a software encoder (a retry after a hardware encoder failed).
    pub software: bool,
}

/// One encoded frame.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EncodedPacket {
    /// Presentation time, in the session's time units.
    pub pts: i64,
    /// Decode time, in the same units.
    pub dts: i64,
    /// A sync sample.
    pub keyframe: bool,
    /// The frame, as the container stores it (length-prefixed NAL units for H.264 and HEVC).
    pub data: Bytes,
}

/// What an encoder session had on a receive.
#[derive(Debug)]
pub enum EncodePoll {
    /// A packet.
    Packet(EncodedPacket),
    /// Nothing yet.
    Pending,
    /// Input ended and every packet was given.
    Drained,
}

/// One video encoder session, used from one thread at a time.
///
/// A session the platform takes away fails the encoder unless the backend can resume it in place
/// with the same configuration record, as `VideoToolbox` can and Android `MediaCodec` cannot.
pub trait VideoEncodeBackend: Send {
    /// The encoder's name, for reports.
    fn name(&self) -> &str;

    /// Whether it runs on hardware.
    fn hardware(&self) -> bool;

    /// Units per second of the timestamps it takes and gives.
    fn timescale(&self) -> u32;

    /// Queues a frame at `pts` whose planes are in the configured layout.
    ///
    /// # Errors
    /// A failed encoder.
    fn submit(
        &mut self,
        pts: i64,
        planes: &[PlaneBuffer],
        keyframe: bool,
    ) -> Result<Submit, EncodeError>;

    /// Takes an encoded packet, in decode order. With `wait`, blocks briefly for one. Once it
    /// answers [`EncodePoll::Drained`] it keeps answering it.
    ///
    /// # Errors
    /// A failed encoder.
    fn receive(&mut self, wait: bool) -> Result<EncodePoll, EncodeError>;

    /// Finishes every frame submitted so far without ending the input, so none is inside the
    /// session while the app is away. The default does nothing.
    ///
    /// # Errors
    /// A failed encoder.
    fn settle(&mut self) -> Result<(), EncodeError> {
        Ok(())
    }

    /// No more frames follow.
    ///
    /// # Errors
    /// A failed encoder.
    fn finish_input(&mut self) -> Result<(), EncodeError>;

    /// The decoder configuration record (`avcC`, `hvcC`, `av1C`), once the first packet is out.
    fn codec_private(&self) -> Option<Bytes>;

    /// Which form of the rate control the encoder runs.
    fn applied_rate_control(&self) -> AppliedRateControl;
}

/// Opens video encoder sessions.
pub trait VideoEncoders: Send + Sync {
    /// A session for `config`.
    ///
    /// # Errors
    /// [`EncodeError::EncodersRefused`] naming every encoder tried.
    fn open(&self, config: &VideoEncodeConfig) -> Result<Box<dyn VideoEncodeBackend>, EncodeError>;
}

/// What an audio encoder is asked for.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AudioEncodeConfig {
    /// The codec.
    pub codec: alpha2_media::AudioCodec,
    /// The project's sample rate, which the audio arrives at.
    pub project_rate: u32,
    /// The speakers, one plane each.
    pub speakers: SpeakerLayout,
    /// Bits per second.
    pub bitrate: u32,
}

/// One audio encoder session, used from one thread at a time.
pub trait AudioEncodeBackend: Send {
    /// The encoder's name, for reports.
    fn name(&self) -> &str;

    /// The rate it encodes at.
    fn sample_rate(&self) -> u32;

    /// Samples per channel in each frame it takes.
    fn frame_size(&self) -> usize;

    /// The bits per second it encodes at: the configured rate, or the nearest the encoder takes.
    fn bitrate(&self) -> u32;

    /// Queues one frame of planar samples starting at sample `pts` of the encoded stream; only
    /// the last frame may be short.
    ///
    /// # Errors
    /// A failed encoder.
    fn submit(&mut self, pts: i64, planes: &[Vec<f32>]) -> Result<Submit, EncodeError>;

    /// Takes an encoded packet, timestamps in samples at [`Self::sample_rate`] counted from the
    /// first decoded sample, priming included. Once it answers [`EncodePoll::Drained`] it keeps
    /// answering it.
    ///
    /// # Errors
    /// A failed encoder.
    fn receive(&mut self, wait: bool) -> Result<EncodePoll, EncodeError>;

    /// No more frames follow.
    ///
    /// # Errors
    /// A failed encoder.
    fn finish_input(&mut self) -> Result<(), EncodeError>;

    /// The codec's setup data (`AudioSpecificConfig`, `OpusHead`), once known.
    fn codec_private(&self) -> Option<Bytes>;

    /// Samples of priming at the start of the decoded stream, which the container's edit list
    /// skips.
    fn priming(&self) -> u32;
}

/// Opens audio encoder sessions.
pub trait AudioEncoders: Send + Sync {
    /// A session for `config`.
    ///
    /// # Errors
    /// [`EncodeError::EncodersRefused`] naming every encoder tried.
    fn open(&self, config: &AudioEncodeConfig) -> Result<Box<dyn AudioEncodeBackend>, EncodeError>;
}

/// How long encoders may take no frame and hand out no packet before the sink gives up on them.
const DRAIN_STALL: Duration = Duration::from_secs(10);

/// The most one turn of a wait adds to a stall, so a process frozen inside a wait adds no more.
const STALL_TURN: Duration = Duration::from_millis(100);

/// The time a wait on the encoders has made no progress, added up turn by turn.
struct Stall {
    last: Instant,
    stalled: Duration,
    limit: Duration,
}

impl Stall {
    fn new(limit: Duration) -> Self {
        Self {
            last: Instant::now(),
            stalled: Duration::ZERO,
            limit,
        }
    }

    /// Records one turn of the wait; true once the turns without progress reach the limit.
    fn turn(&mut self, progressed: bool) -> bool {
        let now = Instant::now();
        let spent = now.saturating_duration_since(self.last).min(STALL_TURN);
        self.last = now;
        self.stalled = if progressed {
            Duration::ZERO
        } else {
            self.stalled + spent
        };
        self.stalled >= self.limit
    }
}

/// The container codec of `codec` encoded in `layout`: `ProRes` is 4444 (`ap4h`) with alpha, else
/// 422 HQ (`apch`), as [`crate::codecs::NativeVideoCodec::ProRes`] encodes it.
fn container_video_codec(codec: alpha2_media::VideoCodec, layout: PixelLayout) -> VideoCodec {
    match codec {
        alpha2_media::VideoCodec::H264 => VideoCodec::H264,
        alpha2_media::VideoCodec::Hevc => VideoCodec::Hevc,
        alpha2_media::VideoCodec::Vp9 => VideoCodec::Vp9,
        alpha2_media::VideoCodec::Av1 => VideoCodec::Av1,
        alpha2_media::VideoCodec::Mpeg4Part2 => VideoCodec::Mpeg4Part2,
        alpha2_media::VideoCodec::ProRes => {
            VideoCodec::ProRes(alpha2_container::FourCc(if layout.alpha {
                *b"ap4h"
            } else {
                *b"apch"
            }))
        }
    }
}

fn container_audio_codec(codec: alpha2_media::AudioCodec) -> AudioCodec {
    match codec {
        alpha2_media::AudioCodec::Aac => AudioCodec::Aac,
        alpha2_media::AudioCodec::Opus => AudioCodec::Opus,
    }
}

/// A track waiting for its encoder's configuration record before it is added.
struct PendingTrack {
    id: Option<TrackId>,
    queued: VecDeque<EncodedPacket>,
}

impl PendingTrack {
    const fn new() -> Self {
        Self {
            id: None,
            queued: VecDeque::new(),
        }
    }
}

/// How far audio written to the container may run ahead of the video written, in seconds.
const AUDIO_LEAD: i64 = 1;

/// Whether audio starting at `start` (in units of `rate` per second) is at most [`AUDIO_LEAD`]
/// past the video packet decoded at `video` (in units of `video_rate`); never before any video.
fn within_lead(start: i64, rate: u32, video: Option<i64>, video_rate: u32) -> bool {
    video.is_some_and(|video| {
        let limit = (i128::from(video) + i128::from(AUDIO_LEAD) * i128::from(video_rate))
            * i128::from(rate);
        i128::from(start) * i128::from(video_rate) <= limit
    })
}

/// Turns an export's pictures and mixed audio into a container's tracks.
///
/// Tracks are added once every encoder has published its configuration record; packets before
/// that wait. Audio goes to the writer at most one second ahead of the video written, so a mix
/// encoded before the pictures still reaches the writer interleaved with them.
pub struct EncodeSink {
    writer: Option<Box<dyn ContainerWriter>>,
    video: Box<dyn VideoEncodeBackend>,
    video_config: VideoEncodeConfig,
    video_track: PendingTrack,
    audio: Option<(
        Box<dyn AudioEncodeBackend>,
        EncoderAudioInput,
        AudioEncodeConfig,
    )>,
    audio_track: PendingTrack,
    /// Samples handed to the audio encoder, at its rate.
    audio_pts: i64,
    /// The decode time of the last video packet written, in the video encoder's units.
    video_written: Option<i64>,
    /// Packets taken from both encoders.
    received: u64,
    /// Every input is in: held audio packets go out.
    finishing: bool,
    /// Stops the waits on a busy encoder.
    cancel: CancelToken,
    /// How long the encoders may make no progress before a wait on them fails.
    stall_limit: Duration,
    /// Frames the video encoder accepted.
    video_frames: u64,
}

impl std::fmt::Debug for EncodeSink {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("EncodeSink")
            .field("video", &self.video.name())
            .finish_non_exhaustive()
    }
}

impl EncodeSink {
    /// A sink writing `video` (and `audio`, when the export has sound) into `writer`; `cancel`
    /// stops its waits on a busy encoder.
    #[must_use]
    pub fn new(
        writer: Box<dyn ContainerWriter>,
        video: Box<dyn VideoEncodeBackend>,
        video_config: VideoEncodeConfig,
        audio: Option<(Box<dyn AudioEncodeBackend>, AudioEncodeConfig)>,
        cancel: CancelToken,
    ) -> Self {
        let audio = audio.map(|(encoder, config)| {
            let input = EncoderAudioInput::new(
                config.project_rate,
                encoder.sample_rate(),
                config.speakers.speakers().len(),
                encoder.frame_size(),
            );
            (encoder, input, config)
        });
        Self {
            writer: Some(writer),
            video,
            video_config,
            video_track: PendingTrack::new(),
            audio,
            audio_track: PendingTrack::new(),
            audio_pts: 0,
            video_written: None,
            received: 0,
            finishing: false,
            cancel,
            stall_limit: DRAIN_STALL,
            video_frames: 0,
        }
    }

    #[cfg(test)]
    fn with_stall_limit(mut self, limit: Duration) -> Self {
        self.stall_limit = limit;
        self
    }

    /// The video encoder's name and the rate control it ran.
    #[must_use]
    pub fn video_encoder(&self) -> (&str, bool, AppliedRateControl) {
        (
            self.video.name(),
            self.video.hardware(),
            self.video.applied_rate_control(),
        )
    }

    fn writer(&mut self) -> Result<&mut Box<dyn ContainerWriter>, EncodeError> {
        self.writer
            .as_mut()
            .ok_or_else(|| EncodeError::Adapter("the output is already finished".into()))
    }

    fn video_info(&self) -> Option<TrackInfo> {
        let private = self.video.codec_private()?;
        let config = &self.video_config;
        Some(TrackInfo {
            id: TrackId(0),
            kind: TrackKind::Video,
            timescale: self.video.timescale(),
            duration: None,
            language: None,
            title: None,
            default: true,
            enabled: true,
            video: Some(VideoTrack {
                codec: container_video_codec(config.codec, config.layout),
                codec_private: private,
                in_band_parameter_sets: false,
                width: config.width,
                height: config.height,
                orientation: Orientation::default(),
                sample_aspect_ratio: None,
                color: Some(config.color),
                clean_aperture: None,
                frame_rate: Some(config.frame_rate),
                alpha: config.layout.alpha,
                reorder_depth: None,
            }),
            audio: None,
        })
    }

    fn audio_info(&self) -> Option<TrackInfo> {
        let (encoder, _, config) = self.audio.as_ref()?;
        let private = encoder.codec_private()?;
        Some(TrackInfo {
            id: TrackId(0),
            kind: TrackKind::Audio,
            timescale: encoder.sample_rate(),
            duration: None,
            language: None,
            title: None,
            default: true,
            enabled: true,
            video: None,
            audio: Some(AudioTrack {
                codec: container_audio_codec(config.codec),
                codec_private: private,
                sample_rate: encoder.sample_rate(),
                channels: u32::try_from(config.speakers.channels()).unwrap_or(u32::MAX),
                speakers: Some(config.speakers),
                bits_per_sample: None,
                priming: encoder.priming(),
                seek_preroll: None,
            }),
        })
    }

    /// Moves packets the encoders have into their tracks.
    fn drain(&mut self, wait_video: bool, wait_audio: bool) -> Result<(bool, bool), EncodeError> {
        let mut video_done = false;
        loop {
            let received = self.video.receive(wait_video);
            match received.map_err(|error| self.rejected(error))? {
                EncodePoll::Packet(packet) => {
                    self.received += 1;
                    self.video_track.queued.push_back(packet);
                }
                EncodePoll::Pending => break,
                EncodePoll::Drained => {
                    video_done = true;
                    break;
                }
            }
        }
        let mut audio_done = self.audio.is_none();
        if let Some((encoder, _, _)) = self.audio.as_mut() {
            loop {
                match encoder.receive(wait_audio)? {
                    EncodePoll::Packet(packet) => {
                        self.received += 1;
                        self.audio_track.queued.push_back(packet);
                    }
                    EncodePoll::Pending => break,
                    EncodePoll::Drained => {
                        audio_done = true;
                        break;
                    }
                }
            }
        }
        self.flush_tracks()?;
        Ok((video_done, audio_done))
    }

    /// Adds tracks whose configuration is known and writes their queued packets.
    fn flush_tracks(&mut self) -> Result<(), EncodeError> {
        // Containers add every track before the first packet; video is always the first track.
        if self.video_track.id.is_none() {
            let Some(video) = self.video_info() else {
                return Ok(());
            };
            let audio = match self.audio {
                Some(_) => match self.audio_info() {
                    Some(info) => Some(info),
                    None => return Ok(()),
                },
                None => None,
            };
            self.video_track.id = Some(self.writer()?.add_track(&video)?);
            if let Some(info) = audio {
                self.audio_track.id = Some(self.writer()?.add_track(&info)?);
            }
        }
        let Some(video) = self.video_track.id else {
            return Ok(());
        };
        let priming = self
            .audio
            .as_ref()
            .map_or(0, |(encoder, _, _)| encoder.priming());
        let frame_duration = i64::from(self.video_config.frame_rate.denominator())
            * i64::from(self.video.timescale())
            / i64::from(self.video_config.frame_rate.numerator().max(1));
        while let Some(packet) = self.video_track.queued.pop_front() {
            let packet = Packet {
                track: video,
                pts: packet.pts,
                dts: packet.dts,
                duration: frame_duration,
                keyframe: packet.keyframe,
                data: packet.data,
                discard: (0, 0),
            };
            self.video_written = Some(packet.dts);
            self.writer()?.write(packet)?;
        }
        if let (Some(audio), Some((encoder, _, _))) = (self.audio_track.id, self.audio.as_ref()) {
            let frame = i64::try_from(encoder.frame_size()).unwrap_or(i64::MAX);
            // Audio ends where the input does: a packet reaching past the samples written so far
            // waits, and at the end its duration stops at the last input sample. Encoders pad
            // their last frames; with the priming taken off, a packet starting at or after the
            // end decodes to padding alone.
            let end = self.audio_pts;
            let rate = encoder.sample_rate();
            let video_rate = self.video.timescale();
            while let Some(packet) = self.audio_track.queued.front() {
                let start = packet.pts - i64::from(priming);
                if !self.finishing
                    && (start + frame > end
                        || !within_lead(start, rate, self.video_written, video_rate))
                {
                    break;
                }
                let Some(packet) = self.audio_track.queued.pop_front() else {
                    break;
                };
                if start >= end {
                    continue;
                }
                let packet = Packet {
                    track: audio,
                    pts: start,
                    dts: packet.dts - i64::from(priming),
                    duration: (end - start).clamp(1, frame),
                    keyframe: true,
                    data: packet.data,
                    discard: (0, 0),
                };
                self.writer()?.write(packet)?;
            }
        }
        Ok(())
    }

    fn submit_audio(&mut self, frames: Vec<Vec<Vec<f32>>>) -> Result<(), EncodeError> {
        for frame in frames {
            let length = i64::try_from(frame.first().map_or(0, Vec::len)).unwrap_or(0);
            let mut stall = Stall::new(self.stall_limit);
            loop {
                let pts = self.audio_pts;
                let Some((encoder, _, _)) = self.audio.as_mut() else {
                    return Ok(());
                };
                match encoder.submit(pts, &frame)? {
                    Submit::Accepted => break,
                    Submit::Busy => self.wait_busy(&mut stall, false, "audio")?,
                }
            }
            self.audio_pts += length;
            self.drain(false, false)?;
        }
        Ok(())
    }

    /// Ends both inputs, drains the encoders and closes the output.
    ///
    /// # Errors
    /// As [`Self::write_frame`]; encoders that give no packet for 10 s while draining, or an
    /// encoder that never published its configuration.
    pub fn finish(mut self) -> Result<(), EncodeError> {
        let tail = self
            .audio
            .as_mut()
            .map(|(_, input, _)| input.finish())
            .unwrap_or_default();
        self.submit_audio(tail)?;
        let finished = self.video.finish_input();
        finished.map_err(|error| self.rejected(error))?;
        if let Some((encoder, _, _)) = self.audio.as_mut() {
            encoder.finish_input()?;
        }
        let mut done = (false, self.audio.is_none());
        let mut stall = Stall::new(self.stall_limit);
        while !(done.0 && done.1) {
            let received = self.received;
            let (video, audio) = self.drain(!done.0, !done.1)?;
            done = (done.0 || video, done.1 || audio);
            if stall.turn(self.received != received) {
                let which = match done {
                    (false, false) => "video and audio encoders",
                    (false, true) => "video encoder",
                    _ => "audio encoder",
                };
                return Err(EncodeError::Adapter(format!(
                    "the {which} gave no packet for {} s while draining",
                    self.stall_limit.as_secs_f64()
                )));
            }
        }
        self.finishing = true;
        self.flush_tracks()?;
        if self.video_track.id.is_none() {
            let which = if self.video_info().is_none() {
                "video"
            } else {
                "audio"
            };
            return Err(EncodeError::Adapter(format!(
                "the {which} encoder published no configuration"
            )));
        }
        let writer = self
            .writer
            .take()
            .ok_or_else(|| EncodeError::Adapter("the output is already finished".into()))?;
        writer.finish().map_err(EncodeError::from)
    }
}

impl EncodeSink {
    /// Encodes frame `index` of the export, its planes in the configured layout.
    ///
    /// # Errors
    /// [`EncodeError::EncoderRejected`] for any failure of the video encoder; a writer or audio
    /// encoder failure as it is; [`EncodeError::Cancelled`] when the token is set while an encoder
    /// is busy; an encoder that takes no frame and gives no packet for 10 s.
    pub fn write_frame(&mut self, index: u64, planes: &[PlaneBuffer]) -> Result<(), EncodeError> {
        let rate = self.video_config.frame_rate;
        let pts = i64::try_from(index).unwrap_or(i64::MAX)
            * i64::from(rate.denominator())
            * i64::from(self.video.timescale())
            / i64::from(rate.numerator().max(1));
        let interval = u64::from(self.video_config.keyframe_interval.max(1));
        let keyframe = index.is_multiple_of(interval);
        let mut stall = Stall::new(self.stall_limit);
        loop {
            let submitted = self.video.submit(pts, planes, keyframe);
            match submitted.map_err(|error| self.rejected(error))? {
                Submit::Accepted => break,
                Submit::Busy => self.wait_busy(&mut stall, true, "video")?,
            }
        }
        self.video_frames += 1;
        self.drain(false, false).map(|_| ())
    }

    /// Lets the video encoder finish the frames it holds and takes their packets.
    ///
    /// # Errors
    /// As [`Self::write_frame`].
    pub fn settle(&mut self) -> Result<(), EncodeError> {
        let settled = self.video.settle();
        settled.map_err(|error| self.rejected(error))?;
        self.drain(false, false).map(|_| ())
    }

    /// One turn of a wait for the `which` encoder to take a frame: drains the encoders, waiting on
    /// the video one when `video`.
    fn wait_busy(
        &mut self,
        stall: &mut Stall,
        video: bool,
        which: &str,
    ) -> Result<(), EncodeError> {
        if self.cancel.is_cancelled() {
            return Err(EncodeError::Cancelled);
        }
        let received = self.received;
        self.drain(video, !video)?;
        if stall.turn(self.received != received) {
            return Err(EncodeError::Adapter(format!(
                "the {which} encoder accepted no frame for {} s",
                self.stall_limit.as_secs_f64()
            )));
        }
        Ok(())
    }

    /// A video encoder failure as one another encoder may not have: anything but a cancel.
    fn rejected(&self, error: EncodeError) -> EncodeError {
        match error {
            EncodeError::Cancelled | EncodeError::EncoderRejected { .. } => error,
            error => EncodeError::EncoderRejected {
                encoder: self.video.name().to_owned(),
                frame: self.video_frames,
                message: error.to_string(),
            },
        }
    }
}

#[cfg(feature = "engine")]
impl crate::export::VideoSink for EncodeSink {
    fn write_video(
        &mut self,
        index: u64,
        _time: alpha2_time::MediaTime,
        _layout: PixelLayout,
        planes: &[PlaneBuffer],
    ) -> Result<(), EncodeError> {
        self.write_frame(index, planes)
    }

    fn settle(&mut self) -> Result<(), EncodeError> {
        Self::settle(self)
    }
}

impl AudioBlockSink for EncodeSink {
    fn write_audio(&mut self, block: &AudioBlock, cancel: &CancelToken) -> Result<(), EncodeError> {
        if cancel.is_cancelled() {
            return Err(EncodeError::Cancelled);
        }
        let Some((_, input, _)) = self.audio.as_mut() else {
            return Ok(());
        };
        let frames = input.push(block)?;
        self.submit_audio(frames)
    }

    fn finish_audio(&mut self, _cancel: &CancelToken) -> Result<(), EncodeError> {
        Ok(())
    }

    fn abort_audio(&mut self) -> Result<(), EncodeError> {
        Ok(())
    }
}

#[cfg(test)]
#[path = "encode_tests.rs"]
mod tests;