private async void LatencyModeToggle_Toggled(object sender, RoutedEventArgs e)
        {
            if (currentState == State.Streaming)
            {
                try
                {
                    LocalhostVideo.Stop();

                    await device.StopRecordingAsync();

                    mode = (mode == LatencyMode.NormalLatency) ? LatencyMode.LowLatency : LatencyMode.NormalLatency;
                    await StartRecordingToCustomSink();

                    HiddenVideo.Source    = null;
                    LocalhostVideo.Source = new Uri("stsp://localhost");

                    LocalhostVideo.Visibility = Windows.UI.Xaml.Visibility.Visible;
                    HiddenVideo.Visibility    = Windows.UI.Xaml.Visibility.Collapsed;
                }
                catch (Exception exc)
                {
                    rootPage.NotifyUser(exc.Message, NotifyType.ErrorMessage);
                }
            }
        }
        private async Task CleanupAsync()
        {
            try
            {
                if (currentState == State.Previewing || currentState == State.Streaming)
                {
                    await device.CaptureSource.StopPreviewAsync();

                    if (currentState == State.Streaming)
                    {
                        LocalhostVideo.Stop();
                    }
                }


                await device.CleanUpAsync();
            }
            catch (Exception)
            {
            }
            finally
            {
                LocalhostVideo.Source = null;
            }
        }
        private async Task Streaming_PreviewButtonClicked()
        {
            LocalhostVideo.Stop();
            await device.StopRecordingAsync();

            LocalhostVideo.Source = null;
            await Previewing_PreviewButtonClicked();

            mode = defaultMode;
            LatencyModeToggle.IsOn      = (mode == LatencyMode.LowLatency);
            LatencyModeToggle.IsEnabled = false;

            LocalhostVideo.Visibility = Windows.UI.Xaml.Visibility.Visible;
            HiddenVideo.Visibility    = Windows.UI.Xaml.Visibility.Collapsed;
        }