SinkWriter.WriteSample() 因 E_INVALIDARG 而失败

3 c# sharpdx ms-media-foundation

我将MediaFoundation与 SharpDX一起使用,以便对来自桌面重复帧的视频文件进行编码。

我正在创建纹理并捕获屏幕。然后将此纹理传递给MFCreateVideoSampleFromSurface,调用一次

/* creating the texture */
new Texture2D(/* device */,
  new Texture2DDescription {
  CpuAccessFlags = CpuAccessFlags.Read,
  BindFlags = BindFlags.None,
  Format = Format.B8G8R8A8_UNorm,
  Width = /* width */,
  Height = /* height */,
  OptionFlags = ResourceOptionFlags.None,
  MipLevels = 1,
  ArraySize = 1,
  SampleDescription = { Count = 1, Quality = 0 },
  Usage = ResourceUsage.Staging
})

/* creating the sample */
Evr.CreateVideoSampleFromSurface(SourceTexture, out /* the sample */);
Run Code Online (Sandbox Code Playgroud)

初始化样本和纹理后,我正在调用IDXGIOutputDuplication::AcquireFrame(),将相关区域从屏幕复制到我通过使用预先创建的纹理ID3D11DeviceContext::CopySubresourceRegion(),调整采样时间并调用ISinkWriter::WriteSample(),但失败了ERROR_INVALID_PARAMETER

这些是我传递给的属性SinkWriter

using (var attrs = new MediaAttributes()) {
  attrs.Set(TranscodeAttributeKeys.TranscodeContainertype, /* container type GUID */);
  attrs.Set(SinkWriterAttributeKeys.ReadwriteEnableHardwareTransforms, 1);
  attrs.Set(SinkWriterAttributeKeys.LowLatency, true);

  if (SourceTexture != null) {
    // create and bind a DXGI device manager
    this.dxgiManager = new DXGIDeviceManager();
    this.dxgiManager.ResetDevice(SourceTexture.Device);

    attrs.Set(SinkWriterAttributeKeys.D3DManager, this.dxgiManager);
  }

  this.byteStream = new ByteStream(/* ... */);
  this.sinkWriter = MediaFactory.CreateSinkWriterFromURL(null, this.byteStream.NativePointer, attrs);

  /* ... */
}
Run Code Online (Sandbox Code Playgroud)

这就是我初始化输入媒体类型的方式:

using (var inMediaType = new MediaType()) {
  inMediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);
  inMediaType.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.Rgb32);
  inMediaType.Set(MediaTypeAttributeKeys.InterlaceMode, (int) VideoInterlaceMode.Progressive);
  inMediaType.Set(MediaTypeAttributeKeys.FrameSize,
    ((long) frameSize.Width << 32) | (uint) frameSize.Height);
  inMediaType.Set(MediaTypeAttributeKeys.FrameRate, ((long) FrameRate << 32) | 1);
  inMediaType.Set(MediaTypeAttributeKeys.PixelAspectRatio, 1);

  this.sinkWriter.SetInputMediaType(this.streamIdx, inMediaType, null);
  this.sinkWriter.BeginWriting();
}
Run Code Online (Sandbox Code Playgroud)

mftrace正在显示这个(完整日志):

6532,C24 17:03:29.01758 CMFSinkWriterDetours::WriteSample @000000001ED45D50 Stream Index 0x0, Sample @000000001ED46B30, Time 0ms, Duration 0ms, Buffers 1, Size 0B,
6532,C24 17:03:29.01759 CMFSinkWriterDetours::WriteSample @000000001ED45D50 failed hr=0x80070057 ERROR_INVALID_PARAMETER
Run Code Online (Sandbox Code Playgroud)

(注意:尽管由于一个无关的错误,这里的持续时间为 0ms,我也尝试用不同的值手动指定样本持续时间,但也因相同的错误而失败)

如果这在某种程度上是相关的,那么尝试使用小屏幕区域(600x500 等)编码视频似乎在 1/3 的时间内完美无缺。在这种情况下尝试对 1920x1080 帧进行编码在任何情况下都没有成功,这对我来说没有任何意义(我没有传递任何已处置的资源,也似乎正在读取空闲内存)

我还尝试手动设置缓冲区和样本(使用MFCreateDXGISurfaceBuffer),结果是一样的。

是MediaFoundation编码完整的源文件,包括我是如何创建的纹理和从屏幕捕获,在我不经意间忽略相关代码的情况。

提前致谢。

小智 6

事实证明,MFCreateVideoSampleFromSurface没有正确设置创建的缓冲区的长度。我解决这个问题的方法如下:

  1. Call MFCreateDXGISurfaceBuffer,它创建一个支持IMF2DBuffer接口的媒体缓冲区。
  2. 查询IMF2DBuffer接口和新产生表面缓冲区的长度设定为连续长度IMF2DBuffer(另一种方法将是当前缓冲区长度设定为最大缓冲长度,和因此不需要查询IMF2DBuffer接口,但典型的方式来这是通过查询 的连续长度来实现的IMF2DBuffer,所以我坚持这样做。)
  3. MFCreateVideoSampleFromSurface使用空表面指针调用——我发现它毫无意义——或者只是创建一个IMFSample正常的。
  4. 将表面缓冲区添加到新样本并写入。

这就是我让它与 C#/SharpDX 一起工作的方式:

this.sample?.Dispose();
this.buffer?.Dispose();

MediaFactory.CreateDXGISurfaceBuffer(SourceTexture.GetType().GUID,
  SourceTexture,
  0,
  new RawBool(false),
  out this.buffer);

this.sample = MediaFactory.CreateSample();
this.sample.SampleTime = time;

this.buffer.CurrentLength = this.buffer.QueryInterface<Buffer2D>().ContiguousLength;
this.sample.AddBuffer(this.buffer);
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助任何使用 MediaFoundation 的人。我在互联网上到处找,没有找到关于这个问题的有用信息,这是微不足道的,但违反直觉。