我有基于此示例的代码; https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CameraGetPreviewFrame。每次上线;
await _mediaCapture.GetPreviewFrameAsync(_currentVideoFrame);
被击中,我们似乎泄漏了内存。我没有收拾什么?我还尝试过每次创建模板框架,并在每个循环中对其进行处理并使其无效-这似乎也不起作用。
我已经回到了Microsoft的原始示例,而且似乎也泄漏了。这是我的代码;
await Task.Run(async () =>
{
try
{
var videoEncodingProperties =
_mediaCapture.VideoDeviceController.GetMediaStreamProperties
(MediaStreamType.VideoPreview) as VideoEncodingProperties;
Debug.Assert(videoEncodingProperties != null, nameof(videoEncodingProperties) + " != null");
_currentVideoFrame = new VideoFrame(BitmapPixelFormat.Gray8,
(int) videoEncodingProperties.Width,
(int) videoEncodingProperties.Height);
TimeSpan? lastFrameTime = null;
while (_mediaCapture.CameraStreamState == CameraStreamState.Streaming)
{
token.ThrowIfCancellationRequested();
await _mediaCapture.GetPreviewFrameAsync(_currentVideoFrame);
if (!lastFrameTime.HasValue ||
lastFrameTime != _currentVideoFrame.RelativeTime)
{
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync
(CoreDispatcherPriority.Normal,
() =>
{
try
{
Debug.Assert(_currentVideoFrame != null,
$"{nameof(_currentVideoFrame)} != null");
var bitmap = _currentVideoFrame.SoftwareBitmap.AsBitmap();
float focalLength = _cameraOptions == CameraOptions.Front ? AppSettings.FrontCameraFocalLength : AppSettings.RearCameraFocalLength;
_frameProcessor.ProcessFrame(bitmap, focalLength);
}
catch (Exception ex)
{
Debug.WriteLine($"Exception: {ex}");
}
});
lastFrameTime = _currentVideoFrame.RelativeTime;
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Exception: {ex}");
}
},
token);
这应该只是获取帧并使其通过_frameProcessor.ProcessFrame()
调用,但是即使那样什么也没做(除了GetPreviewFrameAsync,我什么都剪掉了),它还是泄漏了。
要重复该问题,请从以下位置下载示例: https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/CameraGetPreviewFrame。在Windows 10 v 1903(18362.175)下,使用诊断工具(Debug-> Windows-> Show Diagnostic tools)在调试器中远程运行样本到Surface Pro 4(i5-6300U @ 2.4GHz)。打开显示框复选框,并在按GetPreviewFrameAsync按钮时查看内存。记忆看起来如下,每当我按下按钮时,都会出现这种情况;
答案 0 :(得分:0)
在我们的代码中,使用MediaFrameReader API可以很好地解决此错误,并且响应速度可能会稍微提高。 Microsoft现在已经在GetPreviewFrameAsync documentation page上添加了注释,以指出这一点。
这对我们有用;
...
private MediaFrameReader _mediaFrameReader;
...
private async Task InitializeCameraAsync()
{
if (_mediaCapture == null)
{
_mediaCapture = new MediaCapture();
var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();
var selectedGroup = frameSourceGroups.FirstOrDefault(x => x.Id.Equals(_camera.UwpDeviceInfo.Id));
try
{
var mediaInitSettings = new MediaCaptureInitializationSettings
{
SourceGroup = selectedGroup,
VideoDeviceId = _camera.UwpDeviceInfo.Id,
AudioDeviceId = string.Empty,
StreamingCaptureMode = StreamingCaptureMode.Video,
MemoryPreference = MediaCaptureMemoryPreference.Cpu
};
await _mediaCapture.InitializeAsync(mediaInitSettings);
_isInitialized = true;
}
catch (UnauthorizedAccessException)
{
...
}
catch (Exception ex)
{
...
}
...
// Set-up for frameProcessing
var sourceInfo = selectedGroup?.SourceInfos.FirstOrDefault(info =>
info.SourceKind == MediaFrameSourceKind.Color);
...
var colorFrameSource = _mediaCapture.FrameSources[sourceInfo.Id];
var preferredFormat = colorFrameSource.SupportedFormats
.OrderByDescending(x => x.VideoFormat.Width)
.FirstOrDefault(x => x.VideoFormat.Width <= 1920 &&
x.Subtype.Equals(MediaEncodingSubtypes.Nv12, StringComparison.OrdinalIgnoreCase));
await colorFrameSource.SetFormatAsync(preferredFormat);
_mediaFrameReader = await _mediaCapture.CreateFrameReaderAsync(colorFrameSource);
}
...
}
...
private void _mediaFrameReader_FrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{
...
var mediaFrameReference = sender.TryAcquireLatestFrame();
var videoMediaFrame = mediaFrameReference?.VideoMediaFrame;
var softwareBitmap = videoMediaFrame?.SoftwareBitmap;
if (softwareBitmap != null && _frameProcessor != null)
{
if (_mediaCapture.CameraStreamState == CameraStreamState.Streaming)
{
...
_frameProcessor.ProcessFrame(SoftwareBitmap.Convert(softwareBitmap,
BitmapPixelFormat.Gray8).AsBitmap(), _camera);
...
}
}
...
}
}