Beispiel #1
0
        /// <summary>
        /// Initialize the peer connection.
        /// </summary>
        /// <returns>A task that completes once the peer connection is ready to be used.</returns>
        public async Task InitializePeerConnectionAsync()
        {
            Logger.Log("Initializing the peer connection...");

            // Cannot run in UI thread on UWP because this will initialize the global factory
            // (first library call) which needs to be done on a background thread.
            await ThreadHelper.RunOnWorkerThread(() => Library.ShutdownOptions = Library.ShutdownOptionsFlags.LogLiveObjects);

            // Initialize the native peer connection object
            try
            {
                var config = new PeerConnectionConfiguration
                {
                    SdpSemantic = _sdpSemantic,
                    IceServers  = new List <IceServer> {
                        _iceServer
                    }
                };
                await _peerConnection.InitializeAsync(config);

                IsPeerInitialized = true;
                RaisePropertyChanged("IsPeerInitialized");
            }
            catch (Exception ex)
            {
                Logger.Log($"WebRTC native plugin init failed: {ex.Message}");
                throw ex;
            }
            Logger.Log("Peer connection initialized.");
            OnPeerInitialized();

            // 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
            await _peerConnection.AddDataChannelAsync(ChatChannelID, "chat", true, true);

            //_videoPlayer.CurrentStateChanged += OnMediaStateChanged;
            //_videoPlayer.MediaOpened += OnMediaOpened;
            //_videoPlayer.MediaFailed += OnMediaFailed;
            //_videoPlayer.MediaEnded += OnMediaEnded;
            //_videoPlayer.RealTimePlayback = true;
            //_videoPlayer.AutoPlay = false;

            // Bind the XAML UI control (videoPlayerElement) to the MediaFoundation rendering pipeline (_videoPlayer)
            // so that the former can render in the UI the video frames produced in the background by the latter.
            //videoPlayerElement.SetMediaPlayer(_videoPlayer);

            //// Uncomment to initialize local transceivers and tracks.
            //if (Utils.IsFirstInstance())
            //{
            //    // Add transceivers
            //    var transceiverA = AddTransceiver(MediaKind.Audio,
            //        new TransceiverInitSettings { Name = "audio_transceiver" });

            //    var transceiverV = AddTransceiver(MediaKind.Video,
            //        new TransceiverInitSettings { Name = "video_transceiver", });

            //    // Add audio track
            //    var sourceA = await DeviceAudioTrackSource.CreateAsync(new LocalAudioDeviceInitConfig());
            //    var trackA = LocalAudioTrack.CreateFromSource(sourceA,
            //        new LocalAudioTrackInitConfig { trackName = "local_audio" });
            //    AddAudioTrack(trackA, "Audio Device");

            //    // Add the track to the transceiver.
            //    {
            //        var transceiverVM = Transceivers.First(t => t.Transceiver == transceiverA);
            //        var trackVM = transceiverVM.AvailableSenders.Last();
            //        transceiverVM.Sender = trackVM;
            //    }

            //    // Add video track
            //    var sourceV = await DeviceVideoTrackSource.CreateAsync(
            //        new LocalVideoDeviceInitConfig
            //        {
            //            videoDevice = new VideoCaptureDevice
            //            {
            //                id = @"<insert_device_id>"
            //            },
            //            videoProfileId = string.Empty,
            //            width = 640,
            //            height = 480,
            //            framerate = 30
            //        });
            //    // Crate the track
            //    var trackV = LocalVideoTrack.CreateFromSource(sourceV,
            //        new LocalVideoTrackInitConfig { trackName = "local_video" });
            //    AddVideoTrack(trackV, "Video Device");

            //    // Add the track to the transceiver.
            //    {
            //        var transceiverVM = Transceivers.First(t => t.Transceiver == transceiverV);
            //        var trackVM = transceiverVM.AvailableSenders.Last();
            //        transceiverVM.Sender = trackVM;
            //    }
            //}
        }