////////////////////////////////////////////////// // For video streaming ////////////////////////////////////////////////// private void App_Suspending(object sender, SuspendingEventArgs e) { if (_peerConnection != null) { _peerConnection.Close(); _peerConnection.Dispose(); _peerConnection = null; } //localVideoPlayerElement.SetMediaPlayer(null); if (_signaler != null) { _signaler.StopPollingAsync(); _signaler = null; } remoteVideoPlayerElement.SetMediaPlayer(null); }
private async void OnLoaded(object sender, RoutedEventArgs e) { // Request access to microphone and camera var settings = new MediaCaptureInitializationSettings(); settings.StreamingCaptureMode = StreamingCaptureMode.AudioAndVideo; var capture = new MediaCapture(); await capture.InitializeAsync(settings); // Retrieve a list of available video capture devices (webcams). List <VideoCaptureDevice> deviceList = await PeerConnection.GetVideoCaptureDevicesAsync(); // Get the device list and, for example, print them to the debugger console foreach (var device in deviceList) { // This message will show up in the Output window of Visual Studio Debugger.Log(0, "", $"Webcam {device.name} (id: {device.id})\n"); } _peerConnection = new PeerConnection(); var config = new PeerConnectionConfiguration { IceServers = new List <IceServer> { new IceServer { Urls = { "stun:stun.l.google.com:19302" } } } }; await _peerConnection.InitializeAsync(config); Debugger.Log(0, "", "Peer connection initialized successfully.\n"); await _peerConnection.AddLocalAudioTrackAsync(); if (Settings.m_showLocal) { //LocalVideoTrack _localVideoTrack; await _peerConnection.AddLocalVideoTrackAsync(); _peerConnection.I420LocalVideoFrameReady += Peer_LocalI420AFrameReady; await _peerConnection.AddLocalAudioTrackAsync(); remoteVideoPlayerElement.Visibility = Visibility.Visible; } else { _peerConnection.Connected += () => { Debugger.Log(0, "", "PeerConnection: connected.\n"); }; _peerConnection.IceStateChanged += (IceConnectionState newState) => { Debugger.Log(0, "", $"ICE state: {newState}\n"); }; _peerConnection.I420RemoteVideoFrameReady += Peer_RemoteI420AFrameReady; _peerConnection.LocalSdpReadytoSend += Peer_LocalSdpReadytoSend; _peerConnection.IceCandidateReadytoSend += Peer_IceCandidateReadytoSend; // Initialize the signaler _signaler = new NodeDssSignaler() { HttpServerAddress = "http://10.44.160.22:3000/", LocalPeerId = "DINF-D60015-43A", RemotePeerId = "HOLOLENS-RO2J0", }; _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: var parts = msg.Data.Split(new string[] { msg.IceDataSeparator }, StringSplitOptions.RemoveEmptyEntries); // Note the inverted arguments for historical reasons. // 'candidate' is last in AddIceCandidate(), but first in the message. string sdpMid = parts[2]; int sdpMlineindex = int.Parse(parts[1]); string candidate = parts[0]; _peerConnection.AddIceCandidate(sdpMid, sdpMlineindex, candidate); break; } }; _signaler.StartPollingAsync(); } // Interactions Debug.WriteLine("[Playback::start] Waiting for a connection..."); m_dsUdpSocket = new DatagramSocket(); m_dsUdpSocket.MessageReceived += Socket_MessageReceived; // Initialization UDP communication try { await m_dsUdpSocket.ConnectAsync(new EndpointPair(new HostName(Settings.m_sIPLocalUDP), Settings.m_sPortLocalUDP, new HostName(Settings.m_sIPRemoteUDP), Settings.m_sPortRemoteUDP)); m_output = new DataWriter(m_dsUdpSocket.OutputStream); Debug.WriteLine("[Playback::start] UDP connection initialization ok"); } catch (Exception) { Debug.WriteLine("[Playback::start] Error - UDP connection initialization "); } }