styx 0.1.0

Sync-first, zero-copy capture→decode→process→encode media stack.
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
use std::io::{BufRead, BufReader};
use std::num::NonZeroU32;
use std::sync::Arc;
use std::time::{Duration, Instant};

const NETCAM_MAX_JPEG_BYTES: usize = 32 << 20; // 32 MiB safety cap.

#[cfg(feature = "netcam-video")]
use ffmpeg_next::{
    codec, format,
    frame::Video as FfFrame,
    media::Type as StreamType,
    software::scaling::{context::Context as ScalingContext, flag::Flags},
    util::format::pixel::Pixel as PixelFormat,
};
#[cfg(feature = "async")]
use futures_core::Stream;
#[cfg(feature = "async")]
use futures_util::TryStreamExt;
use reqwest::blocking::Client as BlockingClient;
use styx_core::prelude::*;
#[cfg(feature = "async")]
use tokio_util::bytes::Bytes;
#[cfg(feature = "async")]
use tokio_util::io::StreamReader;

#[cfg(feature = "netcam-video")]
use crate::capture_api::ffmpeg_util::blit_rgba_frame;
use crate::capture_api::{
    CaptureDescriptor, CaptureError, CaptureHandle, ControlPlane, WorkerHandle,
};
use crate::metrics::StageMetrics;
use crate::prelude::{Interval, Mode};
use crate::{BackendHandle, BackendKind, ProbedBackend};

/// Basic MJPEG-over-HTTP backend. Expects `multipart/x-mixed-replace` with JPEG parts.
pub(super) fn start_netcam(
    backend: &ProbedBackend,
    mode: Mode,
    _interval: Option<Interval>,
    descriptor: CaptureDescriptor,
) -> Result<CaptureHandle, CaptureError> {
    let (url, width, height, fps) = match &backend.handle {
        BackendHandle::Netcam {
            url,
            width,
            height,
            fps,
        } => (url.clone(), *width, *height, *fps),
        _ => return Err(CaptureError::Backend("netcam url missing".into())),
    };

    let queue_depth = crate::capture_api::capture_queue_depth();
    let (tx_raw, rx) = styx_core::queue::bounded(queue_depth);
    let tx = Arc::new(tx_raw);
    let url_for_thread = url.clone();
    let tx_for_thread = tx.clone();
    let tunables = crate::capture_api::netcam_tunables();
    let worker_fn = move || {
        let client = match BlockingClient::builder()
            .timeout(Duration::from_secs(tunables.request_timeout_secs))
            .build()
        {
            Ok(c) => c,
            Err(_) => return,
        };
        let start = Instant::now();
        let mut frame_idx: u64 = 0;
        let mut backoff = Duration::from_millis(tunables.backoff_start_ms);
        let mut consecutive_failures: u32 = 0;
        loop {
            // First try MJPEG.
            if let Ok(resp) = client.get(&url_for_thread).send() {
                if let Some(boundary) = resp
                    .headers()
                    .get("content-type")
                    .and_then(|h| h.to_str().ok())
                    .and_then(parse_boundary)
                {
                    if mjpeg_loop(
                        resp,
                        tx_for_thread.as_ref(),
                        &boundary,
                        width,
                        height,
                        fps,
                        &start,
                        &mut frame_idx,
                    ) {
                        return;
                    }
                }
            }
            // Fallback to FFmpeg for H264/H265/other container streams.
            #[cfg(feature = "netcam-video")]
            {
                if ffmpeg_loop(
                    &url_for_thread,
                    tx_for_thread.as_ref(),
                    &start,
                    &mut frame_idx,
                )
                .is_ok()
                {
                    consecutive_failures = 0;
                    continue;
                }
            }
            std::thread::sleep(backoff);
            consecutive_failures = consecutive_failures.saturating_add(1);
            backoff = (backoff * 2).min(Duration::from_millis(tunables.backoff_max_ms));
            if consecutive_failures >= 5 {
                // Periodically reset backoff to avoid long stalls when the source recovers.
                backoff = Duration::from_millis(tunables.backoff_start_ms);
                consecutive_failures = 0;
            }
        }
    };
    let worker = {
        #[cfg(feature = "async")]
        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            WorkerHandle::Async(handle.spawn(async_netcam_worker(
                url,
                width,
                height,
                fps,
                tx.clone(),
                tunables,
            )))
        } else {
            WorkerHandle::Thread(std::thread::spawn(worker_fn))
        }
        #[cfg(not(feature = "async"))]
        {
            WorkerHandle::Thread(std::thread::spawn(worker_fn))
        }
    };

    Ok(CaptureHandle {
        backend: BackendKind::Netcam,
        control: ControlPlane::None,
        descriptor,
        mode,
        interval: Some(Interval {
            numerator: NonZeroU32::new(1).unwrap(),
            denominator: NonZeroU32::new(fps.max(1)).unwrap(),
        }),
        rx,
        stop_tx: None,
        worker: Some(worker),
        metrics: StageMetrics::default(),
    })
}

#[cfg(feature = "async")]
async fn async_netcam_worker(
    url: String,
    width: u32,
    height: u32,
    fps: u32,
    tx: Arc<styx_core::queue::BoundedTx<FrameLease>>,
    tunables: crate::capture_api::NetcamTunables,
) {
    use tokio::time::sleep;

    let client = match reqwest::Client::builder()
        .timeout(Duration::from_secs(tunables.request_timeout_secs))
        .build()
    {
        Ok(c) => c,
        Err(_) => return,
    };

    let start = Instant::now();
    let mut frame_idx: u64 = 0;
    let mut backoff = Duration::from_millis(tunables.backoff_start_ms);
    let mut consecutive_failures: u32 = 0;
    loop {
        if let Ok(resp) = client.get(&url).send().await {
            if let Some(boundary) = resp
                .headers()
                .get("content-type")
                .and_then(|h| h.to_str().ok())
                .and_then(parse_boundary)
            {
                let stream = resp
                    .bytes_stream()
                    .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e));
                let reader = StreamReader::new(stream);
                let mut reader = tokio::io::BufReader::new(reader);
                if async_mjpeg_loop(
                    &mut reader,
                    tx.as_ref(),
                    &boundary,
                    width,
                    height,
                    fps,
                    &start,
                    &mut frame_idx,
                )
                .await
                {
                    return;
                } else {
                    continue;
                }
            }
        }
        #[cfg(feature = "netcam-video")]
        {
            if let Ok(()) = tokio::task::spawn_blocking({
                let url = url.clone();
                let tx = tx.clone();
                let start = start.clone();
                let mut frame_idx = frame_idx;
                move || ffmpeg_loop(&url, &tx, &start, &mut frame_idx)
            })
            .await
            .unwrap_or(Err(CaptureError::Backend("ffmpeg join error".into())))
            {
                consecutive_failures = 0;
                continue;
            }
        }
        sleep(backoff).await;
        consecutive_failures = consecutive_failures.saturating_add(1);
        backoff = (backoff * 2).min(Duration::from_millis(tunables.backoff_max_ms));
        if consecutive_failures >= 5 {
            backoff = Duration::from_millis(tunables.backoff_start_ms);
            consecutive_failures = 0;
        }
    }
}

#[cfg(feature = "async")]
async fn async_mjpeg_loop<S>(
    reader: &mut tokio::io::BufReader<StreamReader<S, Bytes>>,
    tx: &styx_core::queue::BoundedTx<FrameLease>,
    boundary: &str,
    width: u32,
    height: u32,
    fps: u32,
    start: &Instant,
    frame_idx: &mut u64,
) -> bool
where
    S: Stream<Item = Result<Bytes, std::io::Error>> + Unpin,
{
    use tokio::io::AsyncBufReadExt;

    let expected_pixels = if width > 0 && height > 0 {
        width as usize * height as usize
    } else {
        // Unknown resolution: pick a reasonable default for pool sizing.
        1280usize * 720usize
    };
    let (pool_min, pool_bytes, pool_spare) =
        crate::capture_api::capture_pool_limits(4, expected_pixels.saturating_mul(3), 8);
    let pool = BufferPool::with_limits(pool_min, pool_bytes, pool_spare);
    let mut line = Vec::with_capacity(1024);
    let mut buf = Vec::with_capacity(
        (expected_pixels.saturating_mul(3)).min(NETCAM_MAX_JPEG_BYTES),
    );
    loop {
        line.clear();
        if reader
            .read_until(b'\n', &mut line)
            .await
            .ok()
            .filter(|&n| n > 0)
            .is_none()
        {
            break;
        }
        if !line.starts_with(boundary.as_bytes()) {
            continue;
        }
        let mut content_length: Option<usize> = None;
        loop {
            line.clear();
            if reader
                .read_until(b'\n', &mut line)
                .await
                .ok()
                .filter(|&n| n > 0)
                .is_none()
            {
                break;
            }
            if line.iter().all(|b| b.is_ascii_whitespace()) {
                break;
            }
            if let Some(rest) = line
                .strip_prefix(b"Content-Length:")
                .or_else(|| line.strip_prefix(b"content-length:"))
            {
                if let Some(v) = std::str::from_utf8(rest)
                    .ok()
                    .and_then(|s| s.trim().parse::<usize>().ok())
                {
                    content_length = Some(v.min(NETCAM_MAX_JPEG_BYTES));
                }
            }
        }
        buf.clear();
        match content_length {
            Some(len) => {
                let target = len.min(NETCAM_MAX_JPEG_BYTES);
                while buf.len() < target {
                    let take = match reader.fill_buf().await {
                        Ok(data) => {
                            if data.is_empty() {
                                None
                            } else {
                                let need = target - buf.len();
                                let take = data.len().min(need);
                                buf.extend_from_slice(&data[..take]);
                                Some(take)
                            }
                        }
                        Err(_) => None,
                    };
                    let Some(take) = take else { break };
                    if take == 0 {
                        break;
                    }
                    reader.consume(take);
                }
            }
            None => loop {
                let outcome = match reader.fill_buf().await {
                    Ok(data) => {
                        if data.is_empty() {
                            None
                        } else if let Some(idx) = find_subslice(data, boundary.as_bytes()) {
                            buf.extend_from_slice(&data[..idx]);
                            Some((idx, true))
                        } else {
                            let take = data
                                .len()
                                .min(NETCAM_MAX_JPEG_BYTES.saturating_sub(buf.len()));
                            if take == 0 {
                                None
                            } else {
                                buf.extend_from_slice(&data[..take]);
                                Some((take, false))
                            }
                        }
                    }
                    Err(_) => None,
                };
                let Some((take, hit_boundary)) = outcome else {
                    break;
                };
                reader.consume(take);
                if hit_boundary {
                    break;
                }
            },
        }
        if buf.is_empty() {
            continue;
        }
        let res = Resolution::new(width, height)
            .or_else(|| jpeg_dimensions(&buf).and_then(|(w, h)| Resolution::new(w, h)))
            .or_else(|| Resolution::new(1, 1))
            .unwrap();
        let layout = PlaneLayout {
            offset: 0,
            len: buf.len(),
            stride: buf.len(),
        };
        let mut lease = pool.lease();
        lease.resize(buf.len());
        lease.as_mut_slice().copy_from_slice(&buf);
        let timestamp = start
            .elapsed()
            .as_nanos()
            .saturating_sub(0)
            .min(u64::MAX as u128) as u64;
        let frame = FrameLease::single_plane(
            FrameMeta::new(
                MediaFormat::new(FourCc::new(*b"MJPG"), res, ColorSpace::Srgb),
                timestamp,
            ),
            lease,
            layout.len,
            layout.stride,
        );
        *frame_idx = frame_idx.saturating_add(1);
        if let SendOutcome::Closed = tx.send(frame) {
            return true;
        }
        if fps > 0 {
            tokio::time::sleep(Duration::from_millis((1_000 / fps.max(1)) as u64)).await;
        }
    }
    false
}

fn parse_boundary(content_type: &str) -> Option<String> {
    for part in content_type.split(';').map(|s| s.trim()) {
        if let Some(b) = part.strip_prefix("boundary=") {
            return Some(format!("--{}", b.trim_matches('"')));
        }
    }
    None
}

fn jpeg_dimensions(buf: &[u8]) -> Option<(u32, u32)> {
    // Minimal JPEG SOF parser: find Start Of Frame marker and read dimensions.
    // Handles common SOF markers (baseline/progressive/etc).
    let mut i = 0usize;
    while i + 4 < buf.len() {
        if buf[i] != 0xFF {
            i += 1;
            continue;
        }
        // Skip fill bytes.
        let mut j = i + 1;
        while j < buf.len() && buf[j] == 0xFF {
            j += 1;
        }
        if j >= buf.len() {
            break;
        }
        let marker = buf[j];
        // SOF0..SOF15 (except DHT/DAC/etc) - include baseline/progressive lossless variants.
        let is_sof = matches!(
            marker,
            0xC0 | 0xC1 | 0xC2 | 0xC3 | 0xC5 | 0xC6 | 0xC7 | 0xC9 | 0xCA | 0xCB | 0xCD | 0xCE | 0xCF
        );
        // Standalone markers without length.
        let has_length = !matches!(marker, 0xD8 | 0xD9) && !(0xD0..=0xD7).contains(&marker);
        if !has_length {
            i = j + 1;
            continue;
        }
        if j + 2 >= buf.len() {
            break;
        }
        let seg_len = u16::from_be_bytes([buf[j + 1], buf[j + 2]]) as usize;
        if seg_len < 2 {
            break;
        }
        if is_sof {
            // Segment layout: len(2) precision(1) height(2) width(2) ...
            if j + 2 + 1 + 4 >= buf.len() {
                break;
            }
            let h = u16::from_be_bytes([buf[j + 4], buf[j + 5]]) as u32;
            let w = u16::from_be_bytes([buf[j + 6], buf[j + 7]]) as u32;
            if w > 0 && h > 0 {
                return Some((w, h));
            }
        }
        // Skip to next segment.
        i = j + 1 + seg_len;
    }
    None
}

fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack.windows(needle.len()).position(|w| w == needle)
}

fn mjpeg_loop(
    resp: reqwest::blocking::Response,
    tx: &styx_core::queue::BoundedTx<FrameLease>,
    boundary: &str,
    width: u32,
    height: u32,
    fps: u32,
    start: &Instant,
    frame_idx: &mut u64,
) -> bool {
    let mut reader = BufReader::new(resp);
    let expected_pixels = if width > 0 && height > 0 {
        width as usize * height as usize
    } else {
        1280usize * 720usize
    };
    let (pool_min, pool_bytes, pool_spare) = crate::capture_api::capture_pool_limits(4, expected_pixels.saturating_mul(3), 8);
    let pool = BufferPool::with_limits(pool_min, pool_bytes, pool_spare);
    let mut line = Vec::with_capacity(1024);
    let mut buf = Vec::with_capacity(
        (expected_pixels.saturating_mul(3)).min(NETCAM_MAX_JPEG_BYTES),
    );
    loop {
        line.clear();
        if reader
            .read_until(b'\n', &mut line)
            .ok()
            .filter(|&n| n > 0)
            .is_none()
        {
            break;
        }
        if !line.starts_with(boundary.as_bytes()) {
            continue;
        }
        // Skip headers until empty line.
        let mut content_length: Option<usize> = None;
        loop {
            line.clear();
            if reader
                .read_until(b'\n', &mut line)
                .ok()
                .filter(|&n| n > 0)
                .is_none()
            {
                break;
            }
            if line.iter().all(|b| b.is_ascii_whitespace()) {
                break;
            }
            // Track Content-Length to bound reads when provided.
            if let Some(rest) = line
                .strip_prefix(b"Content-Length:")
                .or_else(|| line.strip_prefix(b"content-length:"))
            {
                if let Some(v) = std::str::from_utf8(rest)
                    .ok()
                    .and_then(|s| s.trim().parse::<usize>().ok())
                {
                    content_length = Some(v.min(NETCAM_MAX_JPEG_BYTES));
                }
            }
        }
        // Read JPEG until next boundary.
        buf.clear();
        match content_length {
            Some(len) => {
                let target = len.min(NETCAM_MAX_JPEG_BYTES);
                while buf.len() < target {
                    let chunk = match reader.fill_buf() {
                        Ok(data) => data,
                        Err(_) => break,
                    };
                    let chunk_len = chunk.len();
                    if chunk_len == 0 {
                        break;
                    }
                    let need = target - buf.len();
                    let take = chunk_len.min(need);
                    buf.extend_from_slice(&chunk[..take]);
                    reader.consume(take);
                }
            }
            None => loop {
                match reader.fill_buf() {
                    Ok(data) if data.is_empty() => break,
                    Ok(data) => {
                        if let Some(idx) = find_subslice(data, boundary.as_bytes()) {
                            buf.extend_from_slice(&data[..idx]);
                            reader.consume(idx);
                            break;
                        } else {
                            let take = data
                                .len()
                                .min(NETCAM_MAX_JPEG_BYTES.saturating_sub(buf.len()));
                            if take == 0 {
                                break;
                            }
                            buf.extend_from_slice(&data[..take]);
                            reader.consume(take);
                            continue;
                        }
                    }
                    Err(_) => break,
                };
            },
        }
        if buf.is_empty() {
            continue;
        }
        let res = Resolution::new(width, height)
            .or_else(|| jpeg_dimensions(&buf).and_then(|(w, h)| Resolution::new(w, h)))
            .or_else(|| Resolution::new(1, 1))
            .unwrap();
        let layout = PlaneLayout {
            offset: 0,
            len: buf.len(),
            stride: buf.len(),
        };
        let mut lease = pool.lease();
        lease.resize(buf.len());
        lease.as_mut_slice().copy_from_slice(&buf);
        let timestamp = start
            .elapsed()
            .as_nanos()
            .saturating_sub(0)
            .min(u64::MAX as u128) as u64;
        let frame = FrameLease::single_plane(
            FrameMeta::new(
                MediaFormat::new(FourCc::new(*b"MJPG"), res, ColorSpace::Srgb),
                timestamp,
            ),
            lease,
            layout.len,
            layout.stride,
        );
        *frame_idx = frame_idx.saturating_add(1);
        if let SendOutcome::Closed = tx.send(frame) {
            return true;
        }
        if fps > 0 {
            std::thread::sleep(Duration::from_millis((1_000 / fps.max(1)) as u64));
        }
    }
    false
}

#[cfg(feature = "netcam-video")]
fn ffmpeg_loop(
    url: &str,
    tx: &styx_core::queue::BoundedTx<FrameLease>,
    start: &Instant,
    frame_idx: &mut u64,
) -> Result<(), CaptureError> {
    ffmpeg_next::init().map_err(|e| CaptureError::Backend(e.to_string()))?;
    let mut pool: Option<BufferPool> = None;
    loop {
        let mut ictx = match format::input(url) {
            Ok(ctx) => ctx,
            Err(e) => return Err(CaptureError::Backend(e.to_string())),
        };
        let stream_idx = match ictx.streams().best(StreamType::Video).map(|s| s.index()) {
            Some(idx) => idx,
            None => return Err(CaptureError::Backend("no video stream".into())),
        };
        let stream = ictx
            .stream(stream_idx)
            .ok_or_else(|| CaptureError::Backend("stream missing".into()))?;
        let ctx_decoder = codec::Context::from_parameters(stream.parameters())
            .map_err(|e| CaptureError::Backend(e.to_string()))?;
        let mut decoder = ctx_decoder
            .decoder()
            .video()
            .map_err(|e| CaptureError::Backend(e.to_string()))?;
        let mut scaler = ScalingContext::get(
            decoder.format(),
            decoder.width(),
            decoder.height(),
            PixelFormat::RGBA,
            decoder.width(),
            decoder.height(),
            Flags::BILINEAR,
        )
        .map_err(|e| CaptureError::Backend(e.to_string()))?;
        let mut decoded = FfFrame::empty();
        let mut rgb = FfFrame::empty();
        rgb.set_format(PixelFormat::RGBA);
        rgb.set_width(decoder.width());
        rgb.set_height(decoder.height());
        unsafe {
            rgb.alloc(PixelFormat::RGBA, decoder.width(), decoder.height());
        }
        let res = Resolution::new(decoder.width() as u32, decoder.height() as u32)
            .ok_or_else(|| CaptureError::Backend("invalid video resolution".into()))?;
        let layout = plane_layout_from_dims(res.width, res.height, 4);
        let pool_ref = pool.get_or_insert_with(|| {
            let (pool_min, pool_bytes, pool_spare) =
                crate::capture_api::capture_pool_limits(4, layout.len, 8);
            BufferPool::with_limits(pool_min, pool_bytes, pool_spare)
        });
        for (stream, packet) in ictx.packets() {
            if stream.index() != stream_idx {
                continue;
            }
            if let Err(_) = decoder.send_packet(&packet) {
                break;
            }
            while decoder.receive_frame(&mut decoded).is_ok() {
                if scaler.run(&decoded, &mut rgb).is_err() {
                    continue;
                }
                let ts = start.elapsed().as_nanos().min(u64::MAX as u128) as u64;
                let frame = blit_rgba_frame(&rgb, res, layout, pool_ref, ts);
                *frame_idx = frame_idx.saturating_add(1);
                if let SendOutcome::Closed = tx.send(frame) {
                    return Ok(());
                }
            }
        }
        decoder.send_eof().ok();
        while decoder.receive_frame(&mut decoded).is_ok() {
            if scaler.run(&decoded, &mut rgb).is_err() {
                continue;
            }
            let ts = start.elapsed().as_nanos().min(u64::MAX as u128) as u64;
            let frame = blit_rgba_frame(&rgb, res, layout, pool_ref, ts);
            *frame_idx = frame_idx.saturating_add(1);
            if let SendOutcome::Closed = tx.send(frame) {
                return Ok(());
            }
        }
        // Loop and reconnect on exit.
    }
}