//Suspending-EventHandler of app: executes when app turns into suspending-state private void App_Suspending(object sender, SuspendingEventArgs e) { if (peerConnection != null) { //Closing of PeerConnection peerConnection.Close(); //Disposing of PeerConnection-Resources peerConnection.Dispose(); peerConnection = null; Debugger.Log(0, "", "Peer conection disposed successfully\n"); } //Disposing of MediaPlayers Mpe_localVideo.SetMediaPlayer(null); Mpe_remoteVideo.SetMediaPlayer(null); //Disposing of signaler if (signaler != null) { signaler.StopPollingAsync(); signaler = null; } }
//Loaded-EventHandler of ui: executes after ui-loading private async void OnLoaded(object sender, RoutedEventArgs e) { //Request access to mic and cam MediaCaptureInitializationSettings settings = new MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo; MediaCapture capture = new MediaCapture(); await capture.InitializeAsync(settings); //Get list of cams camList = await PeerConnection.GetVideoCaptureDevicesAsync(); //Print list to log foreach (var cam in camList) { Debugger.Log(0, "", $"Webcam: {cam.name} (id: {cam.id})\n"); } //Ask user for ids (show MessageBox) await ShowIdInputBoxAsync(); //New PeerConnection (Access to WebRTC) peerConnection = new PeerConnection(); //Create PeerConnection-config PeerConnectionConfiguration config = new PeerConnectionConfiguration() { IceServers = new List <IceServer>() { //Using google stun server for testing new IceServer() { Urls = { "stun:stun.l.google.com:19302" } } } }; //Initialize PeerContection await peerConnection.InitializeAsync(config); //Event fires, when local video frame is captured and ready for rendering peerConnection.I420LocalVideoFrameReady += Peer_LocalI420FrameReady; //Event fires, when remote video frame is receved and ready for rendering peerConnection.I420RemoteVideoFrameReady += Peer_RemoteI420FrameReady; //Events fires, when SdpMessage is ready for sending peerConnection.LocalSdpReadytoSend += Peer_LocalSdpReadytoSend; //Event fires, when IceCandidate is ready for sending peerConnection.IceCandidateReadytoSend += Peer_IceCandidateReadytoSend; //Set DebuggingLog-messages peerConnection.Connected += () => Debugger.Log(0, "", "PeerConnection: connected\n"); peerConnection.IceStateChanged += (IceConnectionState newState) => Debugger.Log(0, "", $"ICE state: {newState}\n"); Debugger.Log(0, "", "Peer conection initialized successfully\n"); //Adds cam-tracks from standart (first) devices [add parameter to specify cam-device or -specifications] await peerConnection.AddLocalVideoTrackAsync(new PeerConnection.LocalVideoTrackSettings() { videoDevice = cam }); //Same for mic [no specifications possible: always uses the first mic in list] await peerConnection.AddLocalAudioTrackAsync(); //Initialize the signaler (Properties from MessageBox) signaler = new NodeDssSignaler() { HttpServerAddress = nodeDssServerIp, LocalPeerId = localId, RemotePeerId = remoteId }; signaler.OnMessage += (NodeDssSignaler.Message msg) => { switch (msg.MessageType) { case NodeDssSignaler.Message.WireMessageType.Offer: peerConnection.SetRemoteDescription("offer", msg.Data); peerConnection.CreateAnswer(); break; case NodeDssSignaler.Message.WireMessageType.Answer: peerConnection.SetRemoteDescription("answer", msg.Data); break; case NodeDssSignaler.Message.WireMessageType.Ice: string[] parts = msg.Data.Split(new string[] { msg.IceDataSeparator }, StringSplitOptions.RemoveEmptyEntries); //Changing order of parts string sdpMid = parts[2]; int sdpMlineindex = int.Parse(parts[1]); string candidate = parts[0]; peerConnection.AddIceCandidate(sdpMid, sdpMlineindex, candidate); break; } }; signaler.StartPollingAsync(); }