Inheritance: MonoBehaviour
Exemple #1
0
 /// <summary>
 /// Displays the recording UI controls
 /// </summary>
 private void ShowControls()
 {
     RecordingControls.SetActive(true);
 }
        private static async Task <ScreenRecording?> PerformScreenRecording(HSSettings settingsContext)
        {
            _currentRecordingCts = new CancellationTokenSource();
            _throwAwayResult     = false;

            try
            {
                var ffmpegPath = EnsureAvailableFFmpegAndPotentiallyStartSetup();
                if (ffmpegPath == null)
                {
                    return(null); // We don't have ffmpeg available and the user didn't do anything to fix this. We act like it was aborted.
                }
                var effectiveFormat = settingsContext.VideoOutputFormat;
                if (effectiveFormat == VideoCaptureFormat.AskBeforeRecording)
                {
                    var formatToUse = VideoCaptureFormatSelection.PromptFormat();
                    if (formatToUse == null)
                    {
                        return(null); //Use clicked "cancel"
                    }
                    effectiveFormat = formatToUse.Value;

                    // TODO: It would be better to rewrite the settings context here, so other code can just work with that.
                    // For that to work, we should refactor HSSettings to be a record
                }

                var(selectionBackground, _) = Drawing.ScreenshotCreator.CaptureScreenshot(SystemInformation.VirtualScreen, settingsContext.CaptureCursor);
                using (selectionBackground)
                {
                    using var selector = Selection.AreaSelector.Create(selectionBackground, false, settingsContext);

                    var(selectedArea, windowInfo) = await selector.PromptSelectionAsync();

                    var recorder = ScreenRecorderSelector.CreateScreenRecorderForCurrentPlatform(ffmpegPath);

                    var tempRecordingDir = Path.Combine(Path.GetTempPath(), "hs-" + Path.GetRandomFileName());
                    Directory.CreateDirectory(tempRecordingDir);

                    var extension  = VideoUploadPayload.GetExtensionForVideoFormat(effectiveFormat);
                    var targetFile = Path.Combine(tempRecordingDir, "HS" + extension);

                    if (windowInfo != null)
                    {
                        // See GH#78
                        // The number 500 is just a guess. If it fails, it isn't such a problem and won't cause any harm
                        // As soon as we've got our own recording frame, we can be more precise on when to invoke the SetForegroundWindow
                        _ = Task.Delay(500).ContinueWith(t =>
                        {
                            Debug.WriteLine($"Set FG window to {windowInfo.Title ?? "<no title>"}");
                            Native.User32.SetForegroundWindow(windowInfo.Handle);
                        });
                    }

                    using var recordingControls = new RecordingControls(selectedArea, _currentRecordingCts);
                    recordingControls.Show();

                    var recording = await recorder.Invoke(selectedArea, targetFile, effectiveFormat, settingsContext, _currentRecordingCts.Token);

                    if (_throwAwayResult)
                    {
                        // The user cancelled the video recording
                        try
                        {
                            if (File.Exists(targetFile))
                            {
                                File.Delete(targetFile);
                            }
                        }
                        catch
                        {
                            // Not _that_ important to handle this because it's in a temp dir and willb e gone on reboot anyway
                        }
                        return(null);
                    }
                    return(recording);
                }
            }
            finally
            {
                _currentRecordingCts = null;
                _throwAwayResult     = false;
            }
        }