如何从 ARGB 帧渲染视频

How to render video from ARGB Frames

我正在使用 Microsoft.MixedReality.WebRTC 库,并计划将其用于我的下一个项目 - 实时视频聊天应用程序。 我已经能够建立连接并传递视频帧。

如何正确渲染这些帧并将它们显示为视频?

使用 WPF 的 MediaElement 似乎很容易,但我只能输入一个 Uri 对象作为源,我无法为其提供单帧,AFAIK。

我读到绘制位图是一种可能的解决方案,但我相信这将意味着需要花费很多时间重新发明轮子并进行测试,我不喜欢这样做,除非没有其他方法。

图书馆的工作方式如下: 每次客户端接收到新帧时,都会引发 Argb32VideoFrameReady 事件。然后将 Argb32VideoFrame 结构对象传递给回调,其中包含原始数据的 IntPtr。还提供 HeightWidthStride

More Information on the specific struct here

我可以通过哪些方式实现这一目标?

我打算使用 WPF。 该解决方案应针对 Windows 7+ 和 .Net Framework 4.6.2.

提前致谢。

在 XAML

中有图像元素
<Image x:Name="image"/>

下面的简单方法会直接将帧复制到分配给图像源的 WriteableBitmap 属性。

private void UpdateImage(Argb32VideoFrame frame)
{
    var bitmap = image.Source as WriteableBitmap;
    var width = (int)frame.width;
    var height = (int)frame.height;

    if (bitmap == null ||
        bitmap.PixelWidth != width ||
        bitmap.PixelHeight != height)
    {
        bitmap = new WriteableBitmap(
            width, height, 96, 96, PixelFormats.Bgra32, null);
        image.Source = bitmap;
    }

    bitmap.WritePixels(
        new Int32Rect(0, 0, width, height),
        frame.data, height * frame.stride, frame.stride);
}

来自此处的 ARGBVideoFrame:https://github.com/microsoft/MixedReality-WebRTC/blob/master/libs/Microsoft.MixedReality.WebRTC/VideoFrame.cs

PixelFormats.Bgra32 似乎是正确的格式,由于对结构的评论:

The ARGB components are in the order of a little endian 32-bit integer, so 0xAARRGGBB, or (B, G, R, A) as a sequence of bytes in memory with B first and A last.