/// <summary> /// The callback that is triggered when new video preview frame arrives. In this function, /// video frame is saved for Unity UI if videoPreview is enabled, tracking task is triggered /// in this function call, and video FPS is recorded. [internal use] /// </summary> /// <param name="sender">MediaFrameReader object</param> /// <param name="args">arguments not used here</param> private void OnFrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args) { ARUWPUtils.VideoTick(); using (var frame = sender.TryAcquireLatestFrame()) { if (frame != null) { float[] cameraToWorldMatrixAsFloat; if (TryGetCameraToWorldMatrix(frame, out cameraToWorldMatrixAsFloat) == false) { return; } Interlocked.Exchange(ref _cameraToWorldMatrix, cameraToWorldMatrixAsFloat); var originalSoftwareBitmap = frame.VideoMediaFrame.SoftwareBitmap; var softwareBitmap = SoftwareBitmap.Convert(originalSoftwareBitmap, BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore); originalSoftwareBitmap?.Dispose(); if (videoPreview) { Interlocked.Exchange(ref _bitmap, softwareBitmap); controller.ProcessFrameAsync(SoftwareBitmap.Copy(softwareBitmap)); } else { controller.ProcessFrameAsync(softwareBitmap); } signalTrackingUpdated = true; } } }
/// <summary> /// The callback that is triggered when new video preview frame arrives. In this function, /// video frame is saved for Unity UI if videoPreview is enabled, tracking task is triggered /// in this function call, and video FPS is recorded. [internal use] /// </summary> /// <param name="sender">MediaFrameReader object</param> /// <param name="args">arguments not used here</param> private void OnFrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args) { ARUWPUtils.VideoTick(); using (var frame = sender.TryAcquireLatestFrame()) { if (frame != null) { var softwareBitmap = SoftwareBitmap.Convert(frame.VideoMediaFrame.SoftwareBitmap, BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore); if (videoPreview) { Interlocked.Exchange(ref _bitmap, softwareBitmap); controller.ProcessFrameAsync(SoftwareBitmap.Copy(softwareBitmap)); } else { controller.ProcessFrameAsync(softwareBitmap); } signalTrackingUpdated = true; } } }
/// <summary> /// The callback that is triggered when new video preview frame arrives. In this function, /// video frame is saved for Unity UI if videoPreview is enabled, tracking task is triggered /// in this function call, and video FPS is recorded. [internal use] /// </summary> /// <param name="sender">MediaFrameReader object</param> /// <param name="args">arguments not used here</param> private void OnFrameArrived(MediaFrameReader sender, MediaFrameArrivedEventArgs args) { ARUWPUtils.VideoTick(); using (var frame = sender.TryAcquireLatestFrame()) { if (frame != null) { float[] cameraToWorldMatrixAsFloat; if (TryGetCameraToWorldMatrix(frame, out cameraToWorldMatrixAsFloat) == false) { return; } Matrix4x4 latestLocatableCameraToWorld = ConvertFloatArrayToMatrix4x4(cameraToWorldMatrixAsFloat); var originalSoftwareBitmap = frame.VideoMediaFrame.SoftwareBitmap; var softwareBitmap = SoftwareBitmap.Convert(originalSoftwareBitmap, BitmapPixelFormat.Rgba8, BitmapAlphaMode.Ignore); originalSoftwareBitmap?.Dispose(); if (videoPreview) { Interlocked.Exchange(ref _bitmap, softwareBitmap); controller.ProcessFrameAsync(SoftwareBitmap.Copy(softwareBitmap), latestLocatableCameraToWorld); } else { controller.ProcessFrameAsync(SoftwareBitmap.Copy(softwareBitmap), latestLocatableCameraToWorld); } signalTrackingUpdated = true; if (isPublishing) { TimeSpan sampledCurrentTime = HololensRobotController.Utilities.Timer.SampleCurrentStopwatch(); double elapsedTotalSeconds = HololensRobotController.Utilities.Timer.GetElapsedTimeInSeconds(sampledCurrentTime); if (elapsedTotalSeconds >= nextPublishTime) { SoftwareBitmap publishedBitmap; if (Config.convertColorToGrayscale) { publishedBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Gray8); originalSoftwareBitmap?.Dispose(); } else { publishedBitmap = softwareBitmap; } IBuffer ibuffer = publishBuffer.AsBuffer(); publishedBitmap.CopyToBuffer(ibuffer); OnFrameReadyToPublish( new HololensRobotController.WindowsSensors.FrameEventArgs( (uint)controller.frameHeight, (uint)controller.frameWidth, HololensRobotController.WindowsSensors.CameraHandler.GetCameraSourceEncoding(MediaFrameSourceKind.Color), (uint)publishRowSize, publishBuffer, sampledCurrentTime + HololensRobotController.Utilities.Timer.GetOffsetUTC())); nextPublishTime = nextPublishTime + publishPeriod; } } } } }