static async ValueTask <(bool[] success, Pose[] poses)> TryGetFramePoseAsync(string[] ids) { bool[] success = null; Pose[] poses = null; using (SemaphoreSlim signal = new SemaphoreSlim(0)) { GameThread.Post(() => { try { List <bool> successList = new List <bool>(); List <Pose> posesList = new List <Pose>(); foreach (string id in ids) { if (!TfListener.TryGetFrame(id, out var frame)) { successList.Add(false); posesList.Add(Pose.Identity); } else { successList.Add(true); posesList.Add(frame.OriginWorldPose.Unity2RosPose()); } } success = successList.ToArray(); poses = posesList.ToArray(); } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { Logger.Error("ControllerService: Unexpected timeout in TryGetFramePoseAsync"); return(new bool[ids.Length], new Pose[ids.Length]); } } return(success, poses); }
internal static async Task CaptureScreenshotAsync([NotNull] CaptureScreenshot srv) { if (Settings.ScreenCaptureManager == null) { srv.Response.Success = false; srv.Response.Message = "No screenshot manager has been set for this platform"; return; } string errorMessage = null; Screenshot ss = null; Pose? pose = null; using (SemaphoreSlim signal = new SemaphoreSlim(0)) { GameThread.Post(async() => { try { var assetHolder = UnityEngine.Resources .Load <GameObject>("App Asset Holder") .GetComponent <AppAssetHolder>(); AudioSource.PlayClipAtPoint(assetHolder.Screenshot, Settings.MainCamera.transform.position); ss = await Settings.ScreenCaptureManager.CaptureColorAsync(); pose = ss != null ? TfListener.RelativePoseToFixedFrame(ss.CameraPose).Unity2RosPose().ToCameraFrame() : (Pose?)null; } catch (Exception e) { errorMessage = e.Message; } finally { signal.Release(); } }); if (!await signal.WaitAsync(DefaultTimeoutInMs)) { Logger.Error("ControllerService: Unexpected timeout in CaptureScreenshotAsync"); srv.Response.Success = false; srv.Response.Message = "Request timed out"; return; } } if (errorMessage != null) { srv.Response.Success = false; srv.Response.Message = errorMessage; return; } if (ss == null) { srv.Response.Success = false; srv.Response.Message = "Captured failed for unknown reason"; return; } srv.Response.Success = true; srv.Response.Width = ss.Width; srv.Response.Height = ss.Height; srv.Response.Bpp = ss.Bpp; srv.Response.Header = (screenshotSeq++, ss.Timestamp, TfListener.FixedFrameId); srv.Response.Intrinsics = ss.Intrinsic.ToArray(); srv.Response.Pose = pose ?? Pose.Identity; srv.Response.Data = await CompressAsync(ss); }