private void SessionRevoked(object sender, ExtendedExecutionForegroundRevokedEventArgs args)
 {
     if (_session != null)
     {
         _session.Dispose();
         _session = null;
     }
 }
Example #2
0
        private void ClearExtendedExecution()
        {
            m_mutex.WaitOne();
            if (m_session != null)
            {
                m_session.Revoked -= Session_Revoked;
                m_session.Dispose();
                m_session = null;
            }

            m_mutex.ReleaseMutex();
        }
        private async void ExtendExecution()
        {
            var session = new ExtendedExecutionForegroundSession {
                Reason = ExtendedExecutionForegroundReason.Unspecified
            };
            var result = await session.RequestExtensionAsync();

            if (result == ExtendedExecutionForegroundResult.Allowed)
            {
                _extendedSession = session;
            }
            else
            {
                session.Dispose();
            }
        }
        private async Task PreventFromSuspending()
        {
            ExtendedExecutionForegroundSession newSession = new ExtendedExecutionForegroundSession();
            newSession.Reason = ExtendedExecutionForegroundReason.Unconstrained;
            newSession.Revoked += SessionRevoked;

            ExtendedExecutionForegroundResult result = await newSession.RequestExtensionAsync();
            switch (result)
            {
                case ExtendedExecutionForegroundResult.Allowed:
                    _session = newSession;
                    break;
                default:
                case ExtendedExecutionForegroundResult.Denied:
                    newSession.Dispose();
                    break;
            }
        }
Example #5
0
        private async void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            var button = (ToggleButton)sender;

            // get storage folder
            if (storageFolder == null)
            {
                var msg = new MessageDialog("Please choose a folder first...");
                await msg.ShowAsync();

                button.IsChecked = false;
                return;
            }

            // check storage folder permissions
            try
            {
                var files = await storageFolder.GetFilesAsync();
            }
            catch
            {
                var dialog = new MessageDialog(
                    "The selected storage directory does not exist anymore or is not accessable. Please select a new directory.",
                    "Directory not found or not enough permissions");

                await dialog.ShowAsync();

                button.IsChecked = false;
                return;
            }

            var requestSuspensionExtension = new ExtendedExecutionForegroundSession();

            requestSuspensionExtension.Reason = ExtendedExecutionForegroundReason.Unspecified;
            var requestExtensionResult = await requestSuspensionExtension.RequestExtensionAsync();

            // get storage files
            var time = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss_");

            var screenFile = await storageFolder.CreateFileAsync(time + "slides.mp4");

            var webcamFile = await storageFolder.CreateFileAsync(time + "talkinghead.mp4");

            var jsonFile = await storageFolder.CreateFileAsync(time + "meta.json");

            // get encoder properties
            var frameRate     = uint.Parse(((string)FrameRateComboBox.SelectedItem).Replace("fps", ""));
            var quality       = (VideoEncodingQuality)Enum.Parse(typeof(VideoEncodingQuality), (string)QualityComboBox.SelectedItem, false);
            var useSourceSize = UseCaptureItemSizeCheckBox.IsChecked.Value;

            var  temp    = MediaEncodingProfile.CreateMp4(quality);
            uint bitrate = 2500000;
            var  width   = temp.Video.Width;
            var  height  = temp.Video.Height;

            // get capture item
            var picker = new GraphicsCapturePicker();

            captureItem = await picker.PickSingleItemAsync();

            if (captureItem == null)
            {
                button.IsChecked = false;
                return;
            }

            // use the capture item's size for the encoding if desired
            if (useSourceSize)
            {
                width  = (uint)captureItem.Size.Width;
                height = (uint)captureItem.Size.Height;
            }

            // we have a screen resolution of more than 4K?
            if (width > 1920)
            {
                var v = width / 1920;
                width   = 1920;
                height /= v;
            }

            // even if we're using the capture item's real size,
            // we still want to make sure the numbers are even.
            width  = EnsureEven(width);
            height = EnsureEven(height);

            // tell the user we've started recording
            var originalBrush = MainTextBlock.Foreground;

            MainTextBlock.Foreground        = new SolidColorBrush(Colors.Red);
            MainProgressBar.IsIndeterminate = true;

            button.IsEnabled = false;

            MainTextBlock.Text = "3";
            await Task.Delay(1000);

            MainTextBlock.Text = "2";
            await Task.Delay(1000);

            MainTextBlock.Text = "1";
            await Task.Delay(1000);

            button.IsEnabled = true;

            MainTextBlock.Text = "● rec";

            _timerCount = 0;
            _timer.Start();

            try
            {
                // start webcam recording
                MediaEncodingProfile webcamEncodingProfile = null;

                if (AdaptBitrateCheckBox.IsChecked.Value)
                {
                    var selectedItem       = WebcamComboBox.SelectedItem as ComboBoxItem;
                    var encodingProperties = (selectedItem.Tag as StreamPropertiesHelper);

                    if (encodingProperties.Height > 720)
                    {
                        webcamEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD1080p);
                        webcamEncodingProfile.Video.Bitrate = 8000000;
                    }
                    else if (encodingProperties.Height > 480)
                    {
                        webcamEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD720p);
                        webcamEncodingProfile.Video.Bitrate = 5000000;
                    }
                    else
                    {
                        webcamEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
                    }
                }
                else
                {
                    webcamEncodingProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto);
                }

                mediaRecording = await mediaCapture.PrepareLowLagRecordToStorageFileAsync(webcamEncodingProfile, webcamFile);

                // kick off the screen encoding parallel
                using (var stream = await screenFile.OpenAsync(FileAccessMode.ReadWrite))
                    using (screenCapture = new Encoder(screenDevice, captureItem))
                    {
                        // webcam recording
                        if (mediaCapture != null)
                        {
                            await mediaRecording.StartAsync();
                        }

                        // screen recording
                        await screenCapture.EncodeAsync(
                            stream,
                            width, height, bitrate,
                            frameRate);
                    }

                MainTextBlock.Foreground = originalBrush;

                // user has finished recording, so stop webcam recording
                await mediaRecording.StopAsync();

                await mediaRecording.FinishAsync();

                _timer.Stop();
            }
            catch (Exception ex)
            {
                _timer.Stop();

                var dialog = new MessageDialog(
                    $"Uh-oh! Something went wrong!\n0x{ex.HResult:X8} - {ex.Message}",
                    "Recording failed");

                await dialog.ShowAsync();

                button.IsChecked                = false;
                MainTextBlock.Text              = "failure";
                MainTextBlock.Foreground        = originalBrush;
                MainProgressBar.IsIndeterminate = false;

                captureItem = null;
                if (mediaRecording != null)
                {
                    await mediaRecording.StopAsync();

                    await mediaRecording.FinishAsync();
                }

                return;
            }

            // at this point the encoding has finished
            MainTextBlock.Text = "saving...";

            // save slide markers
            var recording = new Recording()
            {
                Slides = screenCapture.GetTimestamps()
            };

            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            var json = JsonConvert.SerializeObject(recording, Formatting.Indented, settings);
            await FileIO.WriteTextAsync(jsonFile, json);

            // add metadata
            var recordingMetadataDialog       = new RecordingMetadataDialog();
            var recordingMetadataDialogResult = await recordingMetadataDialog.ShowAsync();

            if (recordingMetadataDialogResult == ContentDialogResult.Primary)
            {
                recording.Description = recordingMetadataDialog.LectureTitle;

                if (recordingMetadataDialog.LectureDate.HasValue)
                {
                    recording.LectureDate = recordingMetadataDialog.LectureDate.Value.DateTime;
                }
            }
            else
            {
                recording.Description = null;
                recording.LectureDate = DateTime.Now;
            }

            json = JsonConvert.SerializeObject(recording, Formatting.Indented, settings);
            await FileIO.WriteTextAsync(jsonFile, json);

            // tell the user we're done
            button.IsChecked   = false;
            MainTextBlock.Text = "done";
            MainProgressBar.IsIndeterminate = false;

            requestSuspensionExtension.Dispose();
        }