|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using System.Runtime.Versioning; |
| 3 | +using System.Threading; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | + |
| 6 | +namespace PipeWire.NET.Tests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// End-to-end DMA-BUF zero-copy round-trip that depends on neither GStreamer nor any StreamWeaver/VAAPI |
| 10 | +/// code: a <see cref="PipeWireVideoOutput"/> dmabuf producer is backed by a real dmabuf allocated through |
| 11 | +/// libgbm on the render node, and a <see cref="PipeWireVideoCapture"/> consumer connects with the matching |
| 12 | +/// DRM modifier and must receive frames whose <see cref="PipeWireBufferType"/> is <c>DmaBuf</c> with a valid |
| 13 | +/// fd. This is the regression guard for the dmabuf path (modifier negotiation + fixation, plane layout, fd |
| 14 | +/// passing) - gst's <c>pipewiresink</c> dmabuf export is unreliable across distro/driver builds (some |
| 15 | +/// advertise no modifier at all), so the capability is exercised in-process instead. |
| 16 | +/// Skips where there is no render node or libgbm (e.g. GPU-less CI runners). |
| 17 | +/// </summary> |
| 18 | +[TestClass] |
| 19 | +[SupportedOSPlatform("linux")] |
| 20 | +public sealed class DmaBufRoundTripTests |
| 21 | +{ |
| 22 | + [TestMethod] |
| 23 | + [TestCategory("Integration")] |
| 24 | + [TestCategory("RequiresDaemon")] |
| 25 | + [TestCategory("RequiresGpu")] |
| 26 | + public async Task DmaBufProducer_ToConsumer_DeliversDmaBufFrames() |
| 27 | + { |
| 28 | + if (!File.Exists("/dev/dri/renderD128")) |
| 29 | + Assert.Inconclusive("No GPU render node (/dev/dri/renderD128) - skipping dmabuf round-trip."); |
| 30 | + |
| 31 | + const int width = 320, height = 240, poolCap = 8; |
| 32 | + GbmAllocator gbm; |
| 33 | + try |
| 34 | + { |
| 35 | + gbm = new GbmAllocator("/dev/dri/renderD128"); |
| 36 | + } |
| 37 | + catch (Exception ex) |
| 38 | + { |
| 39 | + Assert.Inconclusive($"libgbm unavailable ({ex.Message}) - skipping dmabuf round-trip."); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + var buffers = new List<GbmAllocator.Buffer>(); |
| 44 | + try |
| 45 | + { |
| 46 | + await using var ctx = new PipeWireContext("test", ConsoleTestLoggerFactory.Instance); |
| 47 | + await ctx.StartAsync(); |
| 48 | + |
| 49 | + long modifier = (long)GbmAllocator.LinearModifier; |
| 50 | + bool streaming = false; |
| 51 | + int framesConsumed = 0, dmaBufConsumed = 0; |
| 52 | + long firstFd = -1; |
| 53 | + |
| 54 | + await using var output = new PipeWireVideoOutput(ctx, "stx-dmabuf-roundtrip", width, height, PixelFormat.Bgra, 30); |
| 55 | + output.AllocateDmaBuf += (_, index, w, h, _, planes) => |
| 56 | + { |
| 57 | + if (index >= poolCap) return 0; |
| 58 | + while (buffers.Count <= index) buffers.Add(gbm.CreateBgra(width, height)); |
| 59 | + GbmAllocator.Buffer b = buffers[index]; |
| 60 | + planes[0] = new VideoPlane(b.Fd, b.Offset, b.Stride, b.Size); |
| 61 | + return 1; |
| 62 | + }; |
| 63 | + output.FillDmaBuf += (_, _) => true; |
| 64 | + output.StateChanged += (_, _, s) => streaming = s == PipeWireStreamState.Streaming; |
| 65 | + output.ConnectDmaBuf([modifier]); |
| 66 | + |
| 67 | + uint nodeId = PipeWireVideoCapture.AnyNode; |
| 68 | + for (int i = 0; i < 50 && nodeId == PipeWireVideoCapture.AnyNode; i++) |
| 69 | + { |
| 70 | + nodeId = output.NodeId; |
| 71 | + if (nodeId == PipeWireVideoCapture.AnyNode) await Task.Delay(50); |
| 72 | + } |
| 73 | + |
| 74 | + Assert.AreNotEqual(PipeWireVideoCapture.AnyNode, nodeId, "producer node should be assigned an id"); |
| 75 | + |
| 76 | + await using var capture = new PipeWireVideoCapture(ctx, "stx-dmabuf-roundtrip-sink"); |
| 77 | + capture.FrameReady += (_, frame) => |
| 78 | + { |
| 79 | + Interlocked.Increment(ref framesConsumed); |
| 80 | + if (frame.BufferType == PipeWireBufferType.DmaBuf) |
| 81 | + { |
| 82 | + Interlocked.Increment(ref dmaBufConsumed); |
| 83 | + Interlocked.CompareExchange(ref firstFd, frame.Fd, -1); |
| 84 | + } |
| 85 | + }; |
| 86 | + capture.Connect(nodeId, [PixelFormat.Bgra], modifiers: [modifier]); |
| 87 | + |
| 88 | + // DRIVER producer: pace it at ~30fps once streaming so the consumer sees a steady frame flow. |
| 89 | + using var driver = new Timer(_ => { if (streaming) output.TriggerFrame(); }, null, 100, 33); |
| 90 | + |
| 91 | + await Task.Delay(TimeSpan.FromSeconds(4)); |
| 92 | + |
| 93 | + Assert.IsTrue(dmaBufConsumed >= 10, |
| 94 | + $"expected >=10 DMA-BUF frames through the dmabuf path, got {dmaBufConsumed} dmabuf of {framesConsumed} total"); |
| 95 | + Assert.IsTrue(firstFd >= 0, "a delivered DMA-BUF frame must expose a valid fd for zero-copy import"); |
| 96 | + } |
| 97 | + finally |
| 98 | + { |
| 99 | + foreach (GbmAllocator.Buffer b in buffers) b.Dispose(); |
| 100 | + gbm.Dispose(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Minimal libgbm allocator: opens the render node and hands out LINEAR-modifier BGRA buffers exported |
| 106 | + /// as dmabuf fds. Just enough to back a PipeWire dmabuf producer in-process - not a general GBM wrapper. |
| 107 | + /// </summary> |
| 108 | + [SupportedOSPlatform("linux")] |
| 109 | + private sealed class GbmAllocator : IDisposable |
| 110 | + { |
| 111 | + public const ulong LinearModifier = 0; // DRM_FORMAT_MOD_LINEAR |
| 112 | + private const uint GbmFormatArgb8888 = 0x34325241; // fourcc('A','R','2','4') == BGRA byte order (LE) |
| 113 | + |
| 114 | + private readonly int _drmFd; |
| 115 | + private readonly IntPtr _device; |
| 116 | + |
| 117 | + public GbmAllocator(string renderNode) |
| 118 | + { |
| 119 | + _drmFd = open(renderNode, 2 /* O_RDWR */); |
| 120 | + if (_drmFd < 0) throw new InvalidOperationException($"open({renderNode}) failed errno={Marshal.GetLastPInvokeError()}"); |
| 121 | + _device = gbm_create_device(_drmFd); |
| 122 | + if (_device == IntPtr.Zero) { close(_drmFd); throw new InvalidOperationException("gbm_create_device failed"); } |
| 123 | + } |
| 124 | + |
| 125 | + public Buffer CreateBgra(int width, int height) |
| 126 | + { |
| 127 | + ulong mod = LinearModifier; |
| 128 | + IntPtr bo = gbm_bo_create_with_modifiers(_device, (uint)width, (uint)height, GbmFormatArgb8888, ref mod, 1); |
| 129 | + if (bo == IntPtr.Zero) throw new InvalidOperationException("gbm_bo_create_with_modifiers failed (LINEAR BGRA)"); |
| 130 | + int fd = gbm_bo_get_fd(bo); |
| 131 | + uint stride = gbm_bo_get_stride(bo); |
| 132 | + uint offset = gbm_bo_get_offset(bo, 0); |
| 133 | + return new Buffer(bo, fd, offset, (int)stride, stride * (uint)height); |
| 134 | + } |
| 135 | + |
| 136 | + public void Dispose() |
| 137 | + { |
| 138 | + if (_device != IntPtr.Zero) gbm_device_destroy(_device); |
| 139 | + if (_drmFd >= 0) close(_drmFd); |
| 140 | + } |
| 141 | + |
| 142 | + public sealed class Buffer(IntPtr bo, int fd, uint offset, int stride, uint size) : IDisposable |
| 143 | + { |
| 144 | + public long Fd { get; } = fd; |
| 145 | + public uint Offset { get; } = offset; |
| 146 | + public int Stride { get; } = stride; |
| 147 | + public uint Size { get; } = size; |
| 148 | + |
| 149 | + public void Dispose() |
| 150 | + { |
| 151 | + if (fd >= 0) close(fd); |
| 152 | + if (bo != IntPtr.Zero) gbm_bo_destroy(bo); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + [DllImport("libc", SetLastError = true)] private static extern int open(string path, int flags); |
| 157 | + [DllImport("libc")] private static extern int close(int fd); |
| 158 | + [DllImport("libgbm.so.1")] private static extern IntPtr gbm_create_device(int fd); |
| 159 | + [DllImport("libgbm.so.1")] private static extern void gbm_device_destroy(IntPtr dev); |
| 160 | + [DllImport("libgbm.so.1")] private static extern IntPtr gbm_bo_create_with_modifiers(IntPtr dev, uint w, uint h, uint format, ref ulong modifiers, uint count); |
| 161 | + [DllImport("libgbm.so.1")] private static extern int gbm_bo_get_fd(IntPtr bo); |
| 162 | + [DllImport("libgbm.so.1")] private static extern uint gbm_bo_get_stride(IntPtr bo); |
| 163 | + [DllImport("libgbm.so.1")] private static extern uint gbm_bo_get_offset(IntPtr bo, int plane); |
| 164 | + [DllImport("libgbm.so.1")] private static extern void gbm_bo_destroy(IntPtr bo); |
| 165 | + } |
| 166 | +} |
0 commit comments