|
1 | 1 | using Avalonia; |
2 | 2 | using Avalonia.Controls; |
3 | 3 | using Avalonia.Media.Imaging; |
| 4 | +using Avalonia.Platform; |
4 | 5 | using Avalonia.Rendering.Composition; |
5 | 6 | using Avalonia.Threading; |
6 | 7 | using NewTek; // NDIlib + nested types |
@@ -202,6 +203,7 @@ private struct PendingFrame |
202 | 203 | public int FrameRateNum, FrameRateDen; |
203 | 204 | public int Scale; |
204 | 205 | public bool DropShadow; |
| 206 | + public NDIlib.FourCC_type_e FourCC; |
205 | 207 | } |
206 | 208 |
|
207 | 209 | // ------------------------------------------------------------------------- |
@@ -434,6 +436,14 @@ public async Task TriggerCaptureAsync() |
434 | 436 | if (snapshot == null || _disposed) |
435 | 437 | return; |
436 | 438 |
|
| 439 | + // Derive the NDI pixel format from the snapshot's reported format rather |
| 440 | + // than inferring it from the OS. The compositor backend decides channel |
| 441 | + // order (BGRA on Windows/D3D, RGBA on macOS/Metal), and reading it here |
| 442 | + // handles any future backend changes automatically. |
| 443 | + NDIlib.FourCC_type_e ndiFormat = snapshot.Format == PixelFormat.Rgba8888 |
| 444 | + ? NDIlib.FourCC_type_e.FourCC_type_RGBA |
| 445 | + : NDIlib.FourCC_type_e.FourCC_type_BGRA; |
| 446 | + |
437 | 447 | int width = snapshot.PixelSize.Width; |
438 | 448 | int height = snapshot.PixelSize.Height; |
439 | 449 | int stride = width * 4; |
@@ -488,6 +498,7 @@ public async Task TriggerCaptureAsync() |
488 | 498 | FrameRateDen = NdiFrameRateDenominator, |
489 | 499 | Scale = Math.Max(NdiIntegerScale, 1), |
490 | 500 | DropShadow = NdiDropShadow, |
| 501 | + FourCC = ndiFormat, |
491 | 502 | }); |
492 | 503 |
|
493 | 504 | LogCaptureHeartbeat(width, height); |
@@ -575,12 +586,10 @@ private void ProcessOneFrame() |
575 | 586 | { |
576 | 587 | xres = scaledWidth, |
577 | 588 | yres = scaledHeight, |
578 | | - // Avalonia's Metal backend (macOS/Linux) produces RGBA pixels; |
579 | | - // the Windows D3D/Skia backend produces BGRA. Tell NDI which |
580 | | - // layout the bytes are actually in so colours render correctly. |
581 | | - FourCC = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) |
582 | | - ? NDIlib.FourCC_type_e.FourCC_type_BGRA |
583 | | - : NDIlib.FourCC_type_e.FourCC_type_RGBA, |
| 589 | + // FourCC is derived from the snapshot's reported pixel format at |
| 590 | + // capture time — see TriggerCaptureAsync — so it matches whatever |
| 591 | + // the compositor backend actually produced. |
| 592 | + FourCC = frame.FourCC, |
584 | 593 | frame_rate_N = frame.FrameRateNum, |
585 | 594 | frame_rate_D = frame.FrameRateDen, |
586 | 595 | picture_aspect_ratio = (float)frame.Width / frame.Height, |
|
0 commit comments