MediaCapture 效果因 "Not enough memory resources are available to complete this operation." 而失败

MediaCapture Effect failed by "Not enough memory resources are available to complete this operation."

我正在使用 MediaCapture(Windows.Media.Capture) class 为网络摄像头输入帧添加一些效果。为了应用效果,我正在使用 Win2D 库。在我像下面的代码一样清除并重新应用几次效果后,出现异常并且网络摄像头无法工作。

MediaCapture 的失败事件显示如下参数:

MediaCaptureFailedEventArgs
Code : 2147942414
Message : "Not enough memory resources are available to complete this operation."

但是,任务管理器中的性能计数器显示有足够的​​内存量可供使用。

//Reset Effect Method
private async Task ApplyAllEffects()
{
  await ClearAllEffects();
  await ApplyRemoveGreenEffect();
}

//Clear All Effects Method
private async Task ClearAllEffects()
{
  //_previewMediaCapture is a MediaCapture object
  if (_previewMediaCapture == null) return;
  await _previewMediaCapture.ClearEffectsAsync(MediaStreamType.VideoPreview);
}

//Add Effect Method
private async Task ApplyRemoveGreenEffect()
{
  if (_previewMediaCapture == null) return;

  var properties = new PropertySet();
  properties[nameof(RemoveGreenMediaExtension.Value)] = (float)Preferences.RemoveGreen;

  var definition = new VideoEffectDefinition(typeof(RemoveGreenVideoEffect).FullName, properties);

  await _previewMediaCapture.AddVideoEffectAsync(definition, MediaStreamType.VideoPreview);
}

我查看了MS提供的Win2d Sample的程序和代码。 它也有同样的问题。 没有办法避免这个问题吗?

我能猜到为什么会这样。

在我初始化 MediaCapture 对象并对其应用 Win2D 效果之前,我通过调用 MediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(...) 方法为 cam 使用了 MJPG 编解码器。

我的网络摄像头 (Logitech C920) 支持 1080p 30fps。 它导致 GPU 内存泄漏。当我对输入帧使用原始 (YUV) 编解码器或 NV12 编解码器时,不会发生 GPU 内存泄漏。

我认为,MJPG 编码过程被添加到 cam 的图形管道中,并且由于某些原因无法正确释放管道。