This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
MainPage.xaml.cs
1338 lines (1203 loc) · 55.1 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.MixedReality.WebRTC;
using TestAppUwp.Video;
using Windows.ApplicationModel;
using Windows.Media.Capture;
using Windows.Media.Core;
using Windows.Media.MediaProperties;
using Windows.Media.Playback;
using Windows.Storage;
using Windows.System.Profile;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace TestAppUwp
{
public class NavLink
{
public string Id { get; set; }
public string Label { get; set; }
public Symbol Symbol { get; set; }
}
public class VideoCaptureDeviceInfo
{
public string Id;
public string DisplayName;
public Symbol Symbol;
public bool SupportsVideoProfiles
{
get
{
if (Id != null)
{
return MediaCapture.IsVideoProfileSupported(Id);
}
return false;
}
}
}
public class ChatChannel
{
public DataChannel DataChannel;
public string Text = "";
public string Label { get { return DataChannel?.Label; } }
}
/// <summary>
/// The main application page.
/// </summary>
public sealed partial class MainPage : Page
{
/// <summary>
/// Indicates whether the native plugin has been successfully initialized,
/// and the <see cref="_peerConnection"/> object can be used.
/// </summary>
public bool PluginInitialized { get; private set; } = false;
private MediaStreamSource localVideoSource = null;
private MediaSource localMediaSource = null;
private MediaPlayer localVideoPlayer = new MediaPlayer();
private bool _isLocalVideoPlaying = false;
private object _isLocalVideoPlayingLock = new object();
private MediaStreamSource remoteVideoSource = null;
private MediaSource remoteMediaSource = null;
private MediaPlayer remoteVideoPlayer = new MediaPlayer();
private bool _isRemoteVideoPlaying = false;
private object _isRemoteVideoPlayingLock = new object();
private uint _remoteAudioChannelCount = 0;
private uint _remoteAudioSampleRate = 0;
private bool _isRemoteAudioPlaying = false;
private object _isRemoteAudioPlayingLock = new object();
/// <summary>
/// The underlying <see cref="PeerConnection"/> object.
/// </summary>
private PeerConnection _peerConnection;
/// <summary>
/// Enable automatically creating a new SDP offer when the renegotiation event is fired.
/// This can be disabled when adding multiple tracks, to bundle all changes together and
/// avoid multiple round trips to the signaler and remote peer.
/// </summary>
private bool _renegotiationOfferEnabled = true;
/// <summary>
/// Predetermined chat data channel ID, negotiated out of band.
/// </summary>
private const int ChatChannelID = 42;
private bool isDssPolling = false;
private NodeDssSignaler dssSignaler = new NodeDssSignaler();
private DispatcherTimer dssStatsTimer = new DispatcherTimer();
public ObservableCollection<VideoCaptureDeviceInfo> VideoCaptureDevices { get; private set; }
= new ObservableCollection<VideoCaptureDeviceInfo>();
public VideoCaptureDeviceInfo SelectedVideoCaptureDevice
{
get
{
var deviceIndex = VideoCaptureDeviceList.SelectedIndex;
if ((deviceIndex < 0) || (deviceIndex >= VideoCaptureDevices.Count))
{
return null;
}
return VideoCaptureDevices[deviceIndex];
}
}
public PeerConnection.VideoProfileKind SelectedVideoProfileKind
{
get
{
var videoProfileKindIndex = KnownVideoProfileKindComboBox.SelectedIndex;
if (videoProfileKindIndex < 0)
{
return PeerConnection.VideoProfileKind.Unspecified;
}
return (PeerConnection.VideoProfileKind)Enum.GetValues(typeof(PeerConnection.VideoProfileKind)).GetValue(videoProfileKindIndex);
}
}
public ObservableCollection<MediaCaptureVideoProfile> VideoProfiles { get; private set; }
= new ObservableCollection<MediaCaptureVideoProfile>();
public MediaCaptureVideoProfile SelectedVideoProfile
{
get
{
var profileIndex = VideoProfileComboBox.SelectedIndex;
if ((profileIndex < 0) || (profileIndex >= VideoProfiles.Count))
{
return null;
}
return VideoProfiles[profileIndex];
}
}
public ObservableCollection<MediaCaptureVideoProfileMediaDescription> RecordMediaDescs { get; private set; }
= new ObservableCollection<MediaCaptureVideoProfileMediaDescription>();
public MediaCaptureVideoProfileMediaDescription SelectedRecordMediaDesc
{
get
{
var descIndex = RecordMediaDescList.SelectedIndex;
if ((descIndex < 0) || (descIndex >= RecordMediaDescs.Count))
{
return null;
}
return RecordMediaDescs[descIndex];
}
}
public ObservableCollection<VideoCaptureFormat> VideoCaptureFormats { get; private set; }
= new ObservableCollection<VideoCaptureFormat>();
public VideoCaptureFormat? SelectedVideoCaptureFormat
{
get
{
var profileIndex = VideoCaptureFormatList.SelectedIndex;
if ((profileIndex < 0) || (profileIndex >= VideoCaptureFormats.Count))
{
return null;
}
return VideoCaptureFormats[profileIndex];
}
}
public ObservableCollection<NavLink> NavLinks { get; }
= new ObservableCollection<NavLink>();
public ObservableCollection<ChatChannel> ChatChannels { get; private set; }
= new ObservableCollection<ChatChannel>();
public ChatChannel SelectedChatChannel
{
get
{
var chatIndex = chatList.SelectedIndex;
if ((chatIndex < 0) || (chatIndex >= ChatChannels.Count))
{
return null;
}
return ChatChannels[chatIndex];
}
}
private VideoBridge localVideoBridge = new VideoBridge(3);
private VideoBridge remoteVideoBridge = new VideoBridge(5);
public static string GetDeviceName()
{
return Environment.MachineName;
}
public MainPage()
{
this.InitializeComponent();
muteLocalVideoStroke.Visibility = Visibility.Collapsed;
muteLocalAudioStroke.Visibility = Visibility.Collapsed;
RestoreLocalAndRemotePeerIDs();
dssSignaler.OnMessage += DssSignaler_OnMessage;
dssSignaler.OnFailure += DssSignaler_OnFailure;
dssSignaler.OnPollingDone += DssSignaler_OnPollingDone;
dssStatsTimer.Tick += OnDssStatsTimerTick;
_peerConnection = new PeerConnection();
_peerConnection.Connected += OnPeerConnected;
_peerConnection.DataChannelAdded += OnDataChannelAdded;
_peerConnection.DataChannelRemoved += OnDataChannelRemoved;
_peerConnection.LocalSdpReadytoSend += OnLocalSdpReadyToSend;
_peerConnection.IceCandidateReadytoSend += OnIceCandidateReadyToSend;
_peerConnection.IceStateChanged += OnIceStateChanged;
_peerConnection.RenegotiationNeeded += OnPeerRenegotiationNeeded;
_peerConnection.TrackAdded += Peer_RemoteTrackAdded;
_peerConnection.TrackRemoved += Peer_RemoteTrackRemoved;
_peerConnection.I420LocalVideoFrameReady += Peer_LocalI420FrameReady;
_peerConnection.I420RemoteVideoFrameReady += Peer_RemoteI420FrameReady;
_peerConnection.LocalAudioFrameReady += Peer_LocalAudioFrameReady;
_peerConnection.RemoteAudioFrameReady += Peer_RemoteAudioFrameReady;
//Window.Current.Closed += Shutdown; // doesn't work
this.Loaded += OnLoaded;
Application.Current.Suspending += App_Suspending;
Application.Current.Resuming += App_Resuming;
}
private void App_Suspending(object sender, SuspendingEventArgs e)
{
// Save local and remote peer IDs for next launch for convenience
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
localSettings.Values["LocalPeerID"] = localPeerUidTextBox.Text;
localSettings.Values["RemotePeerID"] = remotePeerUidTextBox.Text;
}
private void App_Resuming(object sender, object e)
{
RestoreLocalAndRemotePeerIDs();
}
private void RestoreLocalAndRemotePeerIDs()
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
if (localSettings.Values.TryGetValue("LocalPeerID", out object localObj))
{
if (localObj is string str)
{
localPeerUidTextBox.Text = str;
}
}
if (localPeerUidTextBox.Text.Length == 0)
{
localPeerUidTextBox.Text = GetDeviceName();
}
if (localSettings.Values.TryGetValue("RemotePeerID", out object remoteObj))
{
if (remoteObj is string str)
{
remotePeerUidTextBox.Text = str;
}
}
}
private void OnDataChannelAdded(DataChannel channel)
{
LogMessage($"Added data channel '{channel.Label}' (#{channel.ID}).");
RunOnMainThread(() => {
var chat = new ChatChannel { DataChannel = channel };
ChatChannels.Add(chat);
if (ChatChannels.Count == 1)
{
chatList.SelectedIndex = 0;
}
channel.MessageReceived += (byte[] message) =>
RunOnMainThread(() => {
string text = System.Text.Encoding.UTF8.GetString(message);
ChatMessageReceived(chat, text);
});
});
}
private void OnDataChannelRemoved(DataChannel channel)
{
LogMessage($"Removed data channel '{channel.Label}' (#{channel.ID}).");
RunOnMainThread(() => {
var chat = ChatChannels.Where((c) => c.DataChannel == channel).First();
ChatChannels.Remove(chat);
});
}
private void OnLocalSdpReadyToSend(string type, string sdp)
{
var message = new NodeDssSignaler.Message
{
MessageType = NodeDssSignaler.Message.WireMessageTypeFromString(type),
Data = sdp,
IceDataSeparator = "|"
};
dssSignaler.SendMessageAsync(message);
}
private void OnIceCandidateReadyToSend(string candidate, int sdpMlineindex, string sdpMid)
{
var message = new NodeDssSignaler.Message
{
MessageType = NodeDssSignaler.Message.WireMessageType.Ice,
Data = $"{candidate}|{sdpMlineindex}|{sdpMid}", // see DssSignaler_OnMessage
IceDataSeparator = "|"
};
dssSignaler.SendMessageAsync(message);
}
private void OnIceStateChanged(IceConnectionState newState)
{
RunOnMainThread(() => {
LogMessage($"ICE state changed to {newState}.");
iceStateText.Text = newState.ToString();
});
}
private void OnPeerRenegotiationNeeded()
{
// If already connected, update the connection on the fly.
// If not, wait for user action and don't automatically connect.
if (_peerConnection.IsConnected && _renegotiationOfferEnabled)
{
_peerConnection.CreateOffer();
}
}
private async void OnLoaded(object sender, RoutedEventArgs e)
{
LogMessage("Initializing the WebRTC native plugin...");
// Populate the combo box with the PeerConnection.VideoProfileKind enum
{
var values = Enum.GetValues(typeof(PeerConnection.VideoProfileKind));
KnownVideoProfileKindComboBox.ItemsSource = values.Cast<PeerConnection.VideoProfileKind>();
KnownVideoProfileKindComboBox.SelectedIndex = Array.IndexOf(values, PeerConnection.VideoProfileKind.Unspecified);
}
VideoCaptureDeviceList.SelectionChanged += VideoCaptureDeviceList_SelectionChanged;
KnownVideoProfileKindComboBox.SelectionChanged += KnownVideoProfileKindComboBox_SelectionChanged;
VideoProfileComboBox.SelectionChanged += VideoProfileComboBox_SelectionChanged;
//localVideo.TransportControls = localVideoControls;
PluginInitialized = false;
// Ensure that the UWP app was authorized to capture audio (cap:microphone)
// and video (cap:webcam), otherwise the native plugin will fail.
try
{
MediaCapture mediaAccessRequester = new MediaCapture();
var mediaSettings = new MediaCaptureInitializationSettings
{
AudioDeviceId = "",
VideoDeviceId = "",
StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview
};
await mediaAccessRequester.InitializeAsync(mediaSettings);
}
catch (UnauthorizedAccessException uae)
{
LogMessage("Access to A/V denied, check app permissions:: " + uae.Message);
return;
}
catch (Exception ex)
{
LogMessage("Failed to initialize A/V with unknown exception: " + ex.Message);
return;
}
// Populate the list of video capture devices (webcams).
// On UWP this uses internally the API:
// Devices.Enumeration.DeviceInformation.FindAllAsync(VideoCapture)
// Note that there's no API to pass a given device to WebRTC,
// so there's no way to monitor and update that list if a device
// gets plugged or unplugged. Even using DeviceInformation.CreateWatcher()
// would yield some devices that might become unavailable by the time
// WebRTC internally opens the video capture device.
// This is more for demo purpose here because using the UWP API is nicer.
{
var devices = await PeerConnection.GetVideoCaptureDevicesAsync();
VideoCaptureDevices.Clear();
List<VideoCaptureDeviceInfo> vcds = new List<VideoCaptureDeviceInfo>(devices.Count);
foreach (var device in devices)
{
LogMessage($"VCD id={device.id} name={device.name}");
VideoCaptureDevices.Add(new VideoCaptureDeviceInfo()
{
Id = device.id,
DisplayName = device.name,
Symbol = Symbol.Video
});
}
// Select first entry by default
if (VideoCaptureDevices.Count > 0)
{
VideoCaptureDeviceList.SelectedIndex = 0;
}
}
// Initialize the native peer connection object
try
{
var config = new PeerConnectionConfiguration();
config.IceServers.Add(new IceServer { Urls = { "stun:" + stunServer.Text } });
config.SdpSemantic = (sdpSemanticUnifiedPlan.IsChecked.GetValueOrDefault(true)
? SdpSemantic.UnifiedPlan : SdpSemantic.PlanB);
await _peerConnection.InitializeAsync(config);
}
catch(Exception ex)
{
LogMessage($"WebRTC native plugin init failed: {ex.Message}");
return;
}
PluginInitialized = true;
LogMessage("WebRTC native plugin initialized.");
// It is CRUCIAL to add any data channel BEFORE the SDP offer is sent, if data channels are
// to be used at all. Otherwise the SCTP will not be negotiated, and then all channels will
// stay forever in the kConnecting state.
// https://stackoverflow.com/questions/43788872/how-are-data-channels-negotiated-between-two-peers-with-webrtc
var newDataChannel = await _peerConnection.AddDataChannelAsync(ChatChannelID, "chat", true, true);
chatInputBox.IsEnabled = true;
chatSendButton.IsEnabled = true;
createOfferButton.IsEnabled = true;
startLocalMedia.IsEnabled = true;
localVideoPlayer.CurrentStateChanged += OnMediaStateChanged;
localVideoPlayer.MediaOpened += OnMediaOpened;
localVideoPlayer.MediaFailed += OnMediaFailed;
localVideoPlayer.MediaEnded += OnMediaEnded;
localVideoPlayer.RealTimePlayback = true;
localVideoPlayer.AutoPlay = false;
remoteVideoPlayer.CurrentStateChanged += OnMediaStateChanged;
remoteVideoPlayer.MediaOpened += OnMediaOpened;
remoteVideoPlayer.MediaFailed += OnMediaFailed;
remoteVideoPlayer.MediaEnded += OnMediaEnded;
// Bind the XAML UI control (localVideo) to the MediaFoundation rendering pipeline (localVideoPlayer)
// so that the former can render in the UI the video frames produced in the background by the later.
localVideo.SetMediaPlayer(localVideoPlayer);
remoteVideo.SetMediaPlayer(remoteVideoPlayer);
}
/// <summary>
/// Update the list of video profiles stored in <cref>VideoProfiles</cref>
/// when the selected video capture device or known video profile kind change.
/// </summary>
private async void UpdateVideoProfiles()
{
VideoProfiles.Clear();
VideoCaptureFormats.Clear();
// Get the video capture device selected by the user
var deviceIndex = VideoCaptureDeviceList.SelectedIndex;
if (deviceIndex < 0)
{
return;
}
var device = VideoCaptureDevices[deviceIndex];
// Ensure that the video capture device actually supports video profiles
if (MediaCapture.IsVideoProfileSupported(device.Id))
{
// Get the kind of known video profile selected by the user
var videoProfileKindIndex = KnownVideoProfileKindComboBox.SelectedIndex;
if (videoProfileKindIndex < 0)
{
return;
}
var videoProfileKind = (PeerConnection.VideoProfileKind)Enum.GetValues(typeof(PeerConnection.VideoProfileKind)).GetValue(videoProfileKindIndex);
// List all video profiles for the select device (and kind, if any specified)
IReadOnlyList<MediaCaptureVideoProfile> profiles;
if (videoProfileKind == PeerConnection.VideoProfileKind.Unspecified)
{
profiles = MediaCapture.FindAllVideoProfiles(device.Id);
}
else
{
profiles = MediaCapture.FindKnownVideoProfiles(device.Id, (KnownVideoProfile)(videoProfileKind - 1));
}
foreach (var profile in profiles)
{
VideoProfiles.Add(profile);
}
}
else
{
// Device doesn't support video profiles; fall back on flat list of capture formats.
List<VideoCaptureFormat> formatsList = await PeerConnection.GetVideoCaptureFormatsAsync(device.Id);
foreach (var format in formatsList)
{
VideoCaptureFormats.Add(format);
}
// Default to first format, so that user can start the video capture even without selecting
// explicitly a format in a different application tab.
if (formatsList.Count > 0)
{
VideoCaptureFormatList.SelectedIndex = 0;
}
}
}
private void VideoCaptureDeviceList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the video capture device selected by the user
var deviceIndex = VideoCaptureDeviceList.SelectedIndex;
if (deviceIndex < 0)
{
return;
}
var device = VideoCaptureDevices[deviceIndex];
// Select a default video profile kind
var values = Enum.GetValues(typeof(PeerConnection.VideoProfileKind));
if (MediaCapture.IsVideoProfileSupported(device.Id))
{
KnownVideoProfileKindComboBox.SelectedIndex = Array.IndexOf(values, PeerConnection.VideoProfileKind.VideoConferencing);
KnownVideoProfileKindComboBox.IsEnabled = true; //< TODO - Use binding
VideoProfileComboBox.IsEnabled = true;
RecordMediaDescList.IsEnabled = true;
VideoCaptureFormatList.IsEnabled = false;
}
else
{
KnownVideoProfileKindComboBox.SelectedIndex = Array.IndexOf(values, PeerConnection.VideoProfileKind.Unspecified);
KnownVideoProfileKindComboBox.IsEnabled = false;
VideoProfileComboBox.IsEnabled = false;
RecordMediaDescList.IsEnabled = false;
VideoCaptureFormatList.IsEnabled = true;
}
UpdateVideoProfiles();
}
private void KnownVideoProfileKindComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
UpdateVideoProfiles();
}
private void VideoProfileComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
RecordMediaDescs.Clear();
var profile = SelectedVideoProfile;
if (profile == null)
{
return;
}
foreach (var desc in profile.SupportedRecordMediaDescription)
{
RecordMediaDescs.Add(desc);
}
}
//private void Shutdown(object sender, Windows.UI.Core.CoreWindowEventArgs e)
//{
// webRTCNativePlugin.Uninitialize();
//}
/// <summary>
/// Callback invoked when the peer connection is established.
/// </summary>
private void OnPeerConnected()
{
RunOnMainThread(() => {
sessionStatusText.Text = "(session joined)";
chatTextBox.IsEnabled = true;
});
}
private void DssSignaler_OnMessage(NodeDssSignaler.Message message)
{
switch (message.MessageType)
{
case NodeDssSignaler.Message.WireMessageType.Offer:
_peerConnection.SetRemoteDescription("offer", message.Data);
// If we get an offer, we immediately send an answer back
_peerConnection.CreateAnswer();
break;
case NodeDssSignaler.Message.WireMessageType.Answer:
_peerConnection.SetRemoteDescription("answer", message.Data);
break;
case NodeDssSignaler.Message.WireMessageType.Ice:
// TODO - This is NodeDSS-specific
// this "parts" protocol is defined above, in OnIceCandiateReadyToSend listener
var parts = message.Data.Split(new string[] { message.IceDataSeparator }, StringSplitOptions.RemoveEmptyEntries);
// Note the inverted arguments; candidate is last here, but first in OnIceCandiateReadyToSend
_peerConnection.AddIceCandidate(parts[2], int.Parse(parts[1]), parts[0]);
break;
default:
throw new InvalidOperationException($"Unhandled signaler message type '{message.MessageType}'");
}
}
private void DssSignaler_OnFailure(Exception e)
{
RunOnMainThread(() => {
LogMessage($"DSS polling failed: {e.Message}");
// TODO - differentiate between StartPollingAsync() failure and SendMessageAsync() ones!
if (dssSignaler.IsPolling)
{
LogMessage($"Cancelling polling DSS server...");
//pollDssButton.IsEnabled = false; // will be re-enabled when cancellation is completed, see DssSignaler_OnPollingDone
pollDssButton.Content = "Start polling";
dssSignaler.StopPollingAsync();
}
});
}
private void DssSignaler_OnPollingDone()
{
RunOnMainThread(() => {
isDssPolling = false;
pollDssButton.IsEnabled = true;
LogMessage($"Polling DSS server stopped.");
});
}
/// <summary>
/// Log a message to the debugger and to the Debug tab in the UI.
/// This can be called from any thread.
/// </summary>
/// <param name="message">The message to log.</param>
private void LogMessage(string message)
{
Debugger.Log(4, "TestAppUWP", message);
RunOnMainThread(() => {
debugMessages.Text += message + "\n";
});
}
/// <summary>
/// Utility to run some random workload on the main UI thread.
/// </summary>
/// <param name="handler">The workload to run.</param>
private void RunOnMainThread(Windows.UI.Core.DispatchedHandler handler)
{
if (Dispatcher.HasThreadAccess)
{
handler.Invoke();
}
else
{
// Note: use a discard "_" to silence CS4014 warning; we don't need to await the result here
_ = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, handler);
}
}
/// <summary>
/// Utility to run some random workload on a worker thread (not the main UI thread).
/// </summary>
/// <param name="handler">The workload to run.</param>
private Task RunOnWorkerThread(Action handler)
{
if (Dispatcher.HasThreadAccess)
{
return Task.Run(handler);
}
else
{
handler.Invoke();
return Task.CompletedTask;
}
}
/// <summary>
/// Create a new 30-fps NV12-encoded video source for the specified video size.
/// </summary>
/// <param name="width">The width of the video in pixels.</param>
/// <param name="height">The height of the video in pixels.</param>
/// <returns>The newly created video source.</returns>
private MediaStreamSource CreateVideoStreamSource(uint width, uint height, uint framerate)
{
if (width == 0)
{
throw new ArgumentException("Invalid zero width for video stream source.", "width");
}
if (height == 0)
{
throw new ArgumentException("Invalid zero height for video stream source.", "height");
}
// Note: IYUV and I420 have same memory layout (though different FOURCC)
// https://docs.microsoft.com/en-us/windows/desktop/medfound/video-subtype-guids
var videoProperties = VideoEncodingProperties.CreateUncompressed(MediaEncodingSubtypes.Iyuv, width, height);
var videoStreamDesc = new VideoStreamDescriptor(videoProperties);
videoStreamDesc.EncodingProperties.FrameRate.Numerator = framerate;
videoStreamDesc.EncodingProperties.FrameRate.Denominator = 1;
videoStreamDesc.EncodingProperties.Bitrate = (30 * width * height * 8 * 8 / 12); // 30-fps 8bits/byte NV12=12bpp
var videoStreamSource = new MediaStreamSource(videoStreamDesc);
videoStreamSource.BufferTime = TimeSpan.Zero; // TODO : playback breaks if buffering, need to investigate
videoStreamSource.Starting += OnMediaStreamSourceStarting;
videoStreamSource.Closed += OnMediaStreamSourceClosed;
videoStreamSource.Paused += OnMediaStreamSourcePaused;
videoStreamSource.SampleRequested += OnMediaStreamSourceRequested;
videoStreamSource.IsLive = true; // Enables optimizations for live sources
videoStreamSource.CanSeek = false; // Cannot seek live WebRTC video stream
return videoStreamSource;
}
private void OnMediaStreamSourceStarting(MediaStreamSource sender, MediaStreamSourceStartingEventArgs args)
{
Debug.Assert(Dispatcher.HasThreadAccess == false);
if (sender == localVideoSource)
{
LogMessage("Starting local A/V stream...");
//webRTCNativePlugin.Peer.AddStream(audio: true, video_device_index: _video_device_index);
}
else if (sender == remoteVideoSource)
{
LogMessage("Starting remote A/V stream...");
}
args.Request.SetActualStartPosition(TimeSpan.Zero);
}
private void OnMediaStreamSourceClosed(MediaStreamSource sender, MediaStreamSourceClosedEventArgs args)
{
Debug.Assert(Dispatcher.HasThreadAccess == false);
if (sender == localVideoSource)
{
LogMessage("Closing local A/V stream...");
//webRTCNativePlugin.Peer.RemoveStream(audio: true, video: true);
}
else if (sender == remoteVideoSource)
{
LogMessage("Closing remote A/V stream...");
}
}
private void OnMediaStreamSourcePaused(MediaStreamSource sender, object args)
{
Debug.Assert(Dispatcher.HasThreadAccess == false);
if (sender == localVideoSource)
{
LogMessage("Pausing local A/V stream...");
//webRTCNativePlugin.Peer.RemoveStream(audio: true, video: true); // NOT YET!
}
else if (sender == remoteVideoSource)
{
LogMessage("Pausing remote A/V stream...");
}
}
/// <summary>
/// Callback from the Media Foundation pipeline when a new video frame is needed.
/// </summary>
/// <param name="sender">The stream source requesting a new sample.</param>
/// <param name="args">The sample request to fullfil.</param>
private void OnMediaStreamSourceRequested(MediaStreamSource sender, MediaStreamSourceSampleRequestedEventArgs args)
{
VideoBridge videoBridge;
if (sender == localVideoSource)
videoBridge = localVideoBridge;
else if (sender == remoteVideoSource)
videoBridge = remoteVideoBridge;
else
return;
videoBridge.TryServeVideoFrame(args);
}
private void OnMediaStateChanged(Windows.Media.Playback.MediaPlayer sender, object args)
{
RunOnMainThread(() => {
if (sender == localVideoPlayer)
{
localVideoStateText.Text = $"State: {sender.PlaybackSession.PlaybackState}";
}
else if (sender == remoteVideoPlayer)
{
remoteVideoStateText.Text = $"State: {sender.PlaybackSession.PlaybackState}";
}
});
}
/// <summary>
/// Callback on Media Foundation pipeline media successfully opened and
/// ready to be played in a local media player.
/// </summary>
/// <param name="sender">The <see xref="MediaPlayer"/> source object owning the media.</param>
/// <param name="args">(unused)</param>
private void OnMediaOpened(MediaPlayer sender, object args)
{
// Now it is safe to call Play() on the MediaElement
if (sender == localVideoPlayer)
{
RunOnMainThread(() => {
localVideo.MediaPlayer.Play();
//startLocalMedia.IsEnabled = true;
});
}
}
/// <summary>
/// Callback on Media Foundation pipeline media failed to open or to continue playback.
/// </summary>
/// <param name="sender">The <see xref="MediaPlayer"/> source object owning the media.</param>
/// <param name="args">(unused)</param>
private void OnMediaFailed(MediaPlayer sender, MediaPlayerFailedEventArgs args)
{
LogMessage($"MediaElement reported an error: \"{args.ErrorMessage}\" (\"{args.ExtendedErrorCode.Message}\")");
}
/// <summary>
/// Callback on Media Foundation pipeline media ended playback.
/// </summary>
/// <param name="sender">The <see xref="MediaPlayer"/> source object owning the media.</param>
/// <param name="args">(unused)</param>
/// <remarks>This appears to never be called for live sources.</remarks>
private void OnMediaEnded(MediaPlayer sender, object args)
{
RunOnMainThread(() => {
LogMessage("Local MediaElement video playback ended.");
//StopLocalVideo();
sender.Pause();
sender.Source = null;
if (sender == localVideoPlayer)
{
//< TODO - This should never happen. But what to do with
// local channels if it happens?
lock (_isLocalVideoPlayingLock)
{
_isLocalVideoPlaying = false;
}
}
});
}
/// <summary>
/// Stop playback of the local video from the local webcam, and remove the local
/// audio and video tracks from the peer connection.
/// This is called on the UI thread.
/// </summary>
private void StopLocalVideo()
{
lock (_isLocalVideoPlayingLock)
{
if (_isLocalVideoPlaying)
{
localVideo.MediaPlayer.Pause();
localVideo.SetMediaPlayer(null);
localVideoSource = null;
//localMediaSource.Reset();
_isLocalVideoPlaying = false;
// Avoid deadlock in audio processing stack, as this call is delegated to the WebRTC
// signaling thread (and will block the caller thread), and audio processing will
// delegate to the UI thread for UWP operations (and will block the signaling thread).
RunOnWorkerThread(() => {
_renegotiationOfferEnabled = false;
_peerConnection.RemoveLocalAudioTrack();
_peerConnection.RemoveLocalVideoTrack();
_renegotiationOfferEnabled = true;
if (_peerConnection.IsConnected)
{
_peerConnection.CreateOffer();
}
});
}
}
}
/// <summary>
/// Callback on remote media (audio or video) track added.
/// Currently does nothing, as starting the media pipeline is done lazily in the
/// per-frame callback.
/// </summary>
/// <param name="trackKind">The kind of media track added (audio or video only).</param>
/// <seealso cref="Peer_RemoteI420FrameReady"/>
private void Peer_RemoteTrackAdded(PeerConnection.TrackKind trackKind)
{
LogMessage($"Added remote {trackKind} track.");
}
/// <summary>
/// Callback on remote media (audio or video) track removed.
/// </summary>
/// <param name="trackKind">The kind of media track added (audio or video only).</param>
private void Peer_RemoteTrackRemoved(PeerConnection.TrackKind trackKind)
{
LogMessage($"Removed remote {trackKind} track.");
if (trackKind == PeerConnection.TrackKind.Video)
{
// Just double-check that the remote video track is indeed playing
// before scheduling a task to stop it. Currently the remote video
// playback is exclusively controlled by the remote track being present
// or not, so these should be always in sync.
lock (_isRemoteVideoPlayingLock)
{
if (_isRemoteVideoPlaying)
{
// Schedule on the main UI thread to access STA objects.
RunOnMainThread(() => {
// Check that the remote video is still playing.
// This ensures that rapid calls to add/remove the video track
// are serialized, and an earlier remove call doesn't remove the
// track added by a later call, due to delays in scheduling the task.
lock (_isRemoteVideoPlayingLock)
{
if (_isRemoteVideoPlaying)
{
remoteVideo.MediaPlayer.Pause();
_isRemoteVideoPlaying = false;
}
}
});
}
}
}
}
/// <summary>
/// Callback on video frame received from the local video capture device,
/// for local rendering before (or in parallel of) being sent to the remote peer.
/// </summary>
/// <param name="frame">The newly captured video frame.</param>
private void Peer_LocalI420FrameReady(I420AVideoFrame frame)
{
localVideoBridge.HandleIncomingVideoFrame(frame);
}
/// <summary>
/// Callback on video frame received from the remote peer, for local rendering
/// (or any other use).
/// </summary>
/// <param name="frame">The newly received video frame.</param>
private void Peer_RemoteI420FrameReady(I420AVideoFrame frame)
{
// Lazily start the remote video media player when receiving
// the first video frame from the remote peer. Currently there
// is no exposed API to tell once connected that the remote peer
// will be sending some video track.
//< TODO - See if we can add an API to enumerate the remote channels,
// or an On(Audio|Video|Data)Channel(Added|Removed) event?
lock (_isRemoteVideoPlayingLock)
{
if (!_isRemoteVideoPlaying)
{
_isRemoteVideoPlaying = true;
uint width = frame.width;
uint height = frame.height;
// We don't know the remote video framerate yet, so use a default.
uint framerate = 30;
RunOnMainThread(() => {
remoteVideoSource = CreateVideoStreamSource(width, height, framerate);
remoteVideoPlayer.Source = MediaSource.CreateFromMediaStreamSource(remoteVideoSource);
remoteVideoPlayer.Play();
});
}
}
remoteVideoBridge.HandleIncomingVideoFrame(frame);
}
/// <summary>
/// Callback on audio frame received from the local audio capture device (microphone),
/// for local output before (or in parallel of) being sent to the remote peer.
/// </summary>
/// <param name="frame">The newly captured audio frame.</param>
/// <remarks>This is currently never called due to implementation limitations.</remarks>
private void Peer_LocalAudioFrameReady(AudioFrame frame)
{
// The current underlying WebRTC implementation does not support
// local audio frame callbacks, so THIS WILL NEVER BE CALLED until