VideoPage.xaml.cs 14.3 KB
Newer Older
Ghislain MARY's avatar
Ghislain MARY committed
1 2 3 4 5
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
Ghislain MARY's avatar
Ghislain MARY committed
6 7
using System.Threading.Tasks;
using Windows.ApplicationModel.Calls;
Ghislain MARY's avatar
Ghislain MARY committed
8 9 10 11 12 13 14 15 16 17 18 19
using Windows.Devices.Sensors;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
20
using BelledonneCommunications.Mediastreamer2.Tester;
Ghislain MARY's avatar
Ghislain MARY committed
21 22 23
using ms2_tester.Helpers;
using MS2TesterTasks;
using System.Diagnostics;
Ghislain MARY's avatar
Ghislain MARY committed
24 25 26 27 28 29 30 31 32 33 34 35 36

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace MS2Tester
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class VideoPage : Page
    {
        public VideoPage()
        {
            this.InitializeComponent();
Ghislain MARY's avatar
Ghislain MARY committed
37
            InitVideoPage();
Ghislain MARY's avatar
Ghislain MARY committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        }

        override protected void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            displayOrientation = ApplicationView.GetForCurrentView().Orientation;
            displayInformation = DisplayInformation.GetForCurrentView();
            deviceOrientation = SimpleOrientation.NotRotated;
            orientationSensor = SimpleOrientationSensor.GetDefault();
            if (orientationSensor != null)
            {
                deviceOrientation = orientationSensor.GetCurrentOrientation();
                orientationSensor.OrientationChanged += OrientationSensor_OrientationChanged;
            }
            Window.Current.SizeChanged += Current_SizeChanged;
Ghislain MARY's avatar
Ghislain MARY committed
53 54 55 56 57 58
            this.Loaded += VideoPage_Loaded;
        }

        private void VideoPage_Loaded(object sender, RoutedEventArgs e)
        {
            AdaptVideoSize();
Ghislain MARY's avatar
Ghislain MARY committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            if (orientationSensor != null)
            {
                orientationSensor.OrientationChanged -= OrientationSensor_OrientationChanged;
            }
            Window.Current.SizeChanged -= Current_SizeChanged;
        }

        private async void OrientationSensor_OrientationChanged(SimpleOrientationSensor sender, SimpleOrientationSensorOrientationChangedEventArgs args)
        {
            // Keep previous orientation when the user puts its device faceup or facedown
            if ((args.Orientation != SimpleOrientation.Faceup) && (args.Orientation != SimpleOrientation.Facedown))
            {
                deviceOrientation = args.Orientation;
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => SetVideoOrientation());
            }
        }

        private async void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
Ghislain MARY's avatar
Ghislain MARY committed
83
            AdaptVideoSize();
Ghislain MARY's avatar
Ghislain MARY committed
84 85 86 87 88 89
            displayOrientation = ApplicationView.GetForCurrentView().Orientation;
            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => SetVideoOrientation());
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
Ghislain MARY's avatar
Ghislain MARY committed
90
            UninitVideo();
Ghislain MARY's avatar
Ghislain MARY committed
91 92 93
            ((Frame)Window.Current.Content).GoBack();
        }

Ghislain MARY's avatar
Ghislain MARY committed
94
        private void VideoToggleButton_Checked(object sender, RoutedEventArgs e)
Ghislain MARY's avatar
Ghislain MARY committed
95
        {
Ghislain MARY's avatar
Ghislain MARY committed
96 97 98 99 100 101 102 103 104 105
            ToggleButton b = sender as ToggleButton;
            if (b.IsChecked == true)
            {
                String camera = (CameraComboBox.SelectedItem as ComboBoxItem).Content as String;
                String codec = (CodecComboBox.SelectedItem as ComboBoxItem).Content as String;
                String videoSize = (VideoSizeComboBox.SelectedItem as ComboBoxItem).Content as String;
                UInt32 frameRate = 25;
                UInt32.TryParse((FramerateComboBox.SelectedItem as ComboBoxItem).Content as String, out frameRate);
                UInt32 bitRate = 1500;
                UInt32.TryParse(BitrateTextBox.Text, out bitRate);
106 107
                Boolean usePreviewStream = UsePreviewStreamToggleSwitch.IsOn;
                StartVideoStream(camera, codec, videoSize, frameRate, bitRate, usePreviewStream);
Ghislain MARY's avatar
Ghislain MARY committed
108 109 110 111 112
            }
            else
            {
                StopVideoStream();
            }
Ghislain MARY's avatar
Ghislain MARY committed
113 114
        }

Ghislain MARY's avatar
Ghislain MARY committed
115
        private async void InitVideoPage()
Ghislain MARY's avatar
Ghislain MARY committed
116
        {
Ghislain MARY's avatar
Ghislain MARY committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            // Perform the video capture & display in a background task. Set to false to run in foreground.
            MS2TesterHelper.RunInBackground = true;

            await InitVideo();
            await FillCameraComboBox();
        }

        private async Task FillCameraComboBox()
        {
            bool isSelected = true;
            List<String> videoDevices = await GetVideoDevices();
            foreach (String device in videoDevices)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Content = device;
                item.IsSelected = isSelected;
                isSelected = false;
                CameraComboBox.Items.Add(item);
            }
        }

        private async Task InitVideo()
        {
            try
            {
                OperationResult result = await MS2TesterHelper.InitVideo();
                if (result == OperationResult.Succeeded)
                {
                    Debug.WriteLine("InitVideo: success");
                } else
                {
                    Debug.WriteLine("InitVideo: failure");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("InitVideo: Exception {0}", e.Message));
            }
        }

        private async void UninitVideo()
        {
            try
            {
                OperationResult result = await MS2TesterHelper.UninitVideo();
                if (result == OperationResult.Succeeded)
                {
                    Debug.WriteLine("UninitVideo: success");
                }
                else
                {
                    Debug.WriteLine("UninitVideo: failure");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("UninitVideo: Exception {0}", e.Message));
            }
Ghislain MARY's avatar
Ghislain MARY committed
175 176
        }

Ghislain MARY's avatar
Ghislain MARY committed
177
        private async Task<List<String>> GetVideoDevices()
Ghislain MARY's avatar
Ghislain MARY committed
178
        {
Ghislain MARY's avatar
Ghislain MARY committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
            List<String> result = null;
            try
            {
                result = await MS2TesterHelper.GetVideoDevices();
                if (result != null)
                {
                    Debug.WriteLine("GetVideoDevices: success");
                }
                else
                {
                    Debug.WriteLine("GetVideoDevices: failure");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("GetVideoDevices: Exception {0}", e.Message));
            }
            return result;
Ghislain MARY's avatar
Ghislain MARY committed
197 198
        }

199
        private async void StartVideoStream(String camera, String codec, String videoSize, UInt32 frameRate, UInt32 bitRate, Boolean usePreviewStream)
Ghislain MARY's avatar
Ghislain MARY committed
200
        {
Ghislain MARY's avatar
Ghislain MARY committed
201 202 203 204 205 206 207 208 209 210 211 212
            try
            {
                _videoSource = new MSWinRTVideo.SwapChainPanelSource();
                _videoSource.Start(VideoSwapChainPanel);
                _previewSource = new MSWinRTVideo.SwapChainPanelSource();
                _previewSource.Start(PreviewSwapChainPanel);
                var vcc = VoipCallCoordinator.GetDefault();
                var entryPoint = typeof(PhoneCallTask).FullName;
                var status = await vcc.ReserveCallResourcesAsync(entryPoint);
                var capabilities = VoipPhoneCallMedia.Audio | VoipPhoneCallMedia.Video;
                call = vcc.RequestNewOutgoingCall("FooContext", "FooContact", "MS2Tester", capabilities);
                call.NotifyCallActive();
213
                OperationResult result = await MS2TesterHelper.StartVideoStream(VideoSwapChainPanel.Name, PreviewSwapChainPanel.Name, camera, codec, videoSize, frameRate, bitRate, usePreviewStream);
Ghislain MARY's avatar
Ghislain MARY committed
214 215 216 217 218 219 220 221 222 223 224 225 226
                if (result == OperationResult.Succeeded)
                {
                    Debug.WriteLine("StartVideoStream: success");
                }
                else
                {
                    Debug.WriteLine("StartVideoStream: failure");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("StartVideoStream: Exception {0}", e.Message));
            }
Ghislain MARY's avatar
Ghislain MARY committed
227 228
        }

Ghislain MARY's avatar
Ghislain MARY committed
229
        private async void StopVideoStream()
Ghislain MARY's avatar
Ghislain MARY committed
230
        {
Ghislain MARY's avatar
Ghislain MARY committed
231 232 233 234 235 236 237 238 239 240 241
            try
            {
                OperationResult result = await MS2TesterHelper.StopVideoStream();
                if (result == OperationResult.Succeeded)
                {
                    Debug.WriteLine("StopVideoStream: success");
                }
                else
                {
                    Debug.WriteLine("StopVideoStream: failure");
                }
242 243 244 245 246 247 248 249 250 251
                if (_videoSource != null)
                {
                    _videoSource.Stop();
                    _videoSource = null;
                }
                if (_previewSource != null)
                {
                    _previewSource.Stop();
                    _previewSource = null;
                }
Ghislain MARY's avatar
Ghislain MARY committed
252 253 254 255 256 257 258
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("StopVideoStream: Exception {0}", e.Message));
            }
            call.NotifyCallEnded();
            call = null;
Ghislain MARY's avatar
Ghislain MARY committed
259 260
        }

Ghislain MARY's avatar
Ghislain MARY committed
261
        private async void ChangeCamera(String camera)
Ghislain MARY's avatar
Ghislain MARY committed
262
        {
Ghislain MARY's avatar
Ghislain MARY committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
            try
            {
                OperationResult result = await MS2TesterHelper.ChangeCamera(camera);
                if (result == OperationResult.Succeeded)
                {
                    Debug.WriteLine("ChangeCamera: success");
                }
                else
                {
                    Debug.WriteLine("ChangeCamera: failure");
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("ChangeCamera: Exception {0}", e.Message));
            }
Ghislain MARY's avatar
Ghislain MARY committed
279 280
        }

Ghislain MARY's avatar
Ghislain MARY committed
281
        private async Task<int> GetOrientation()
Ghislain MARY's avatar
Ghislain MARY committed
282
        {
Ghislain MARY's avatar
Ghislain MARY committed
283 284 285 286 287 288 289 290 291 292
            int result = 0;
            try
            {
                result = await MS2TesterHelper.GetOrientation();
            }
            catch (Exception e)
            {
                Debug.WriteLine(String.Format("GetVideoDevices: Exception {0}", e.Message));
            }
            return result;
Ghislain MARY's avatar
Ghislain MARY committed
293 294
        }

Ghislain MARY's avatar
Ghislain MARY committed
295
        private async void SetOrientation(int degrees)
Ghislain MARY's avatar
Ghislain MARY committed
296
        {
Ghislain MARY's avatar
Ghislain MARY committed
297
            try
Ghislain MARY's avatar
Ghislain MARY committed
298
            {
Ghislain MARY's avatar
Ghislain MARY committed
299 300 301 302 303 304 305 306 307
                OperationResult result = await MS2TesterHelper.SetOrientation(degrees);
                if (result == OperationResult.Succeeded)
                {
                    Debug.WriteLine("SetOrientation: success");
                }
                else
                {
                    Debug.WriteLine("SetOrientation: failure");
                }
Ghislain MARY's avatar
Ghislain MARY committed
308
            }
Ghislain MARY's avatar
Ghislain MARY committed
309
            catch (Exception e)
Ghislain MARY's avatar
Ghislain MARY committed
310
            {
Ghislain MARY's avatar
Ghislain MARY committed
311
                Debug.WriteLine(String.Format("SetOrientation: Exception {0}", e.Message));
Ghislain MARY's avatar
Ghislain MARY committed
312 313 314 315 316 317
            }
        }

        private void ChangeCameraButton_Click(object sender, RoutedEventArgs e)
        {
            String camera = (CameraComboBox.SelectedItem as ComboBoxItem).Content as String;
Ghislain MARY's avatar
Ghislain MARY committed
318
            ChangeCamera(camera);
Ghislain MARY's avatar
Ghislain MARY committed
319 320
        }

321 322 323 324 325 326 327 328
        private async void RotateCameraButton_Click(object sender, RoutedEventArgs e)
        {
            int degrees = await GetOrientation();
            degrees += 90;
            degrees %= 360;
            SetOrientation(degrees);
        }

Ghislain MARY's avatar
Ghislain MARY committed
329
        private async void SetVideoOrientation()
Ghislain MARY's avatar
Ghislain MARY committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
        {
            SimpleOrientation orientation = deviceOrientation;
            if (displayInformation.NativeOrientation == DisplayOrientations.Portrait)
            {
                switch (orientation)
                {
                    case SimpleOrientation.Rotated90DegreesCounterclockwise:
                        orientation = SimpleOrientation.NotRotated;
                        break;
                    case SimpleOrientation.Rotated180DegreesCounterclockwise:
                        orientation = SimpleOrientation.Rotated90DegreesCounterclockwise;
                        break;
                    case SimpleOrientation.Rotated270DegreesCounterclockwise:
                        orientation = SimpleOrientation.Rotated180DegreesCounterclockwise;
                        break;
                    case SimpleOrientation.NotRotated:
                    default:
                        orientation = SimpleOrientation.Rotated270DegreesCounterclockwise;
                        break;
                }
            }
            int degrees = 0;
            switch (orientation)
            {
                case SimpleOrientation.Rotated90DegreesCounterclockwise:
                    degrees = 90;
                    break;
                case SimpleOrientation.Rotated180DegreesCounterclockwise:
                    degrees = 180;
                    break;
                case SimpleOrientation.Rotated270DegreesCounterclockwise:
                    degrees = 270;
                    break;
                case SimpleOrientation.NotRotated:
                default:
                    degrees = 0;
                    break;
            }

Ghislain MARY's avatar
Ghislain MARY committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382
            int currentDegrees = await GetOrientation();
            if (currentDegrees != degrees)
            {
                SetOrientation(degrees);
            }
        }

        private void AdaptVideoSize()
        {
            if (ActualWidth > 640)
            {
                VideoGrid.Width = 640;
            }
            else
Ghislain MARY's avatar
Ghislain MARY committed
383
            {
Ghislain MARY's avatar
Ghislain MARY committed
384
                VideoGrid.Width = ActualWidth;
Ghislain MARY's avatar
Ghislain MARY committed
385
            }
Ghislain MARY's avatar
Ghislain MARY committed
386 387 388
            VideoGrid.Height = VideoGrid.Width * 3 / 4;
            PreviewSwapChainPanel.Width = VideoGrid.Width / 4;
            PreviewSwapChainPanel.Height = VideoGrid.Height / 4;
Ghislain MARY's avatar
Ghislain MARY committed
389 390 391
        }


Ghislain MARY's avatar
Ghislain MARY committed
392
        private VoipPhoneCall call;
Ghislain MARY's avatar
Ghislain MARY committed
393 394 395 396
        private ApplicationViewOrientation displayOrientation;
        private DisplayInformation displayInformation;
        private SimpleOrientationSensor orientationSensor;
        private SimpleOrientation deviceOrientation;
Ghislain MARY's avatar
Ghislain MARY committed
397 398
        private MSWinRTVideo.SwapChainPanelSource _videoSource;
        private MSWinRTVideo.SwapChainPanelSource _previewSource;
Ghislain MARY's avatar
Ghislain MARY committed
399 400
    }
}