Search code examples
c#uwpwin2d

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


I am using MediaCapture(Windows.Media.Capture) class to add some effects to webcam input frames. For applying effects, I am working with Win2D library. After I clear and re-apply effects like below code few times, the exception is raised and the web cam doesn't work.

The MediaCapture's Failed event shows arguments like this :

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

However, The performance counter in Task manager shows sufficient memory amount to use.

//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);
}

I checked the program and code of Win2d Sample offered by MS. It has same problem too. There's no way to avoid this problem?


Solution

  • I can guess why this things are happened.

    Before I initialize the MediaCapture object and apply Win2D effects to it, I used MJPG codec for cam's by calling MediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(...) method.

    It's supported one by my web cam(Logitech C920) for 30fps in 1080p. It caused GPU Memory leaks. When I use raw(YUV) codec or NV12 codec for input frames, GPU memory leak isn't happend.

    I think, MJPG encoding process is added to cam's graphical pipe-line and by some reasons with it that pipe-line can't be released properly.