Example #1
0
        public void MuteAudioIn()
        {
            System.Diagnostics.Debug.WriteLine("audio muted");
            RTCMediaStream localStream = _peerConnection.LocalStreams[0];

            defaultAudioTrack = localStream.AudioTracks[0];
            localStream.RemoveAudioTrack(defaultAudioTrack);
            _peerConnection.RemoveStream(localStream);
            _peerConnection.AddStream(localStream);
        }
Example #2
0
        void StartSignalingIfReady()
        {
            if (!_isTurnComplete || !_isRegisteredWithRoomServer)
            {
                return;
            }

            SetState(ARDAppClientState.Connected);

            RTCMediaConstraints constrains = DefaultPeerConnectionConstraints();

            _peerConnection = _factory.PeerConnectionWithICEServers(_iceServers, constrains, this);

            RTCMediaStream localStream = CreateLocalMediaStream();

            _peerConnection.AddStream(localStream);
            if (_isInitiator)
            {
                SendOffer();
            }
            else
            {
                WaitForAnswer();
            }
        }
Example #3
0
        private async Task <RTCPeerConnection> createPeerConnection()
        {
            this.setWebRtcDeviceAndProfile();

            var connection = new RTCPeerConnection(
                new RTCConfiguration()
            {
                BundlePolicy       = RTCBundlePolicy.Balanced,
                IceTransportPolicy = RTCIceTransportPolicy.All
            }
                );

            var loadVideo =
                this.mediaOptions.ReceiveVideo ||
                this.mediaOptions.SendVideo ||
                this.mediaOptions.LocalLoopback;
            var loadAudio =
                this.mediaOptions.ReceiveAudio ||
                this.mediaOptions.SendAudio;

            this.localMediaStream = await this.media.GetUserMedia(
                new RTCMediaStreamConstraints()
            {
                videoEnabled = loadVideo,
                audioEnabled = loadAudio
            });

            connection.AddStream(this.localMediaStream);

            this.localVideoTrack = this.localMediaStream.GetVideoTracks().FirstOrDefault();

            if (this.mediaOptions.LocalLoopback)
            {
                await this.runOnUI(() =>
                {
                    this.media.AddVideoTrackMediaElementPair(
                        this.localVideoTrack,
                        this.localVideo,
                        "Local");
                });
            }

            connection.OnIceCandidate += this.connection_OnIceCandidate;
            connection.OnAddStream    += this.connection_OnAddStream;

            return(connection);
        }
Example #4
0
        /// <summary>
        /// Creates a peer connection.
        /// </summary>
        /// <returns>True if connection to a peer is successfully created.</returns>
        private async Task <bool> CreatePeerConnection(CancellationToken cancelationToken)
        {
            Debug.Assert(_peerConnection == null);
            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }

            var config = new RTCConfiguration()
            {
                BundlePolicy = RTCBundlePolicy.Balanced,
#if ORTCLIB
                SignalingMode = _signalingMode,
                GatherOptions = new RTCIceGatherOptions()
                {
                    IceServers = new List <RTCIceServer>(_iceServers),
                }
#else
                IceTransportPolicy = RTCIceTransportPolicy.All,
                IceServers         = _iceServers
#endif
            };

            Debug.WriteLine("Conductor: Creating peer connection.");
            _peerConnection = new RTCPeerConnection(config);

            if (_peerConnection == null)
            {
                throw new NullReferenceException("Peer connection is not created.");
            }

#if !ORTCLIB
            _peerConnection.EtwStatsEnabled = _etwStatsEnabled;
            _peerConnection.ConnectionHealthStatsEnabled = _peerConnectionStatsEnabled;
#endif
            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }
#if ORTCLIB
            OrtcStatsManager.Instance.Initialize(_peerConnection);
#endif
            OnPeerConnectionCreated?.Invoke();

            _peerConnection.OnIceCandidate += PeerConnection_OnIceCandidate;
#if ORTCLIB
            _peerConnection.OnTrack     += PeerConnection_OnAddTrack;
            _peerConnection.OnTrackGone += PeerConnection_OnRemoveTrack;
            _peerConnection.OnIceConnectionStateChange += () => { Debug.WriteLine("Conductor: Ice connection state change, state=" + (null != _peerConnection ? _peerConnection.IceConnectionState.ToString() : "closed")); };
#else
            _peerConnection.OnAddStream             += PeerConnection_OnAddStream;
            _peerConnection.OnRemoveStream          += PeerConnection_OnRemoveStream;
            _peerConnection.OnConnectionHealthStats += PeerConnection_OnConnectionHealthStats;
#endif
            Debug.WriteLine("Conductor: Getting user media.");
            RTCMediaStreamConstraints mediaStreamConstraints = new RTCMediaStreamConstraints
            {
                // Always include audio/video enabled in the media stream,
                // so it will be possible to enable/disable audio/video if
                // the call was initiated without microphone/camera
                audioEnabled = true,
                videoEnabled = true
            };

            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }

#if ORTCLIB
            var tracks = await _media.GetUserMedia(mediaStreamConstraints);

            if (tracks != null)
            {
                RTCRtpCapabilities audioCapabilities = RTCRtpSender.GetCapabilities("audio");
                RTCRtpCapabilities videoCapabilities = RTCRtpSender.GetCapabilities("video");

                _mediaStream = new MediaStream(tracks);
                Debug.WriteLine("Conductor: Adding local media stream.");
                IList <MediaStream> mediaStreamList = new List <MediaStream>();
                mediaStreamList.Add(_mediaStream);
                foreach (var mediaStreamTrack in tracks)
                {
                    //Create stream track configuration based on capabilities
                    RTCMediaStreamTrackConfiguration configuration = null;
                    if (mediaStreamTrack.Kind == MediaStreamTrackKind.Audio && audioCapabilities != null)
                    {
                        configuration =
                            await Helper.GetTrackConfigurationForCapabilities(audioCapabilities, AudioCodec);
                    }
                    else if (mediaStreamTrack.Kind == MediaStreamTrackKind.Video && videoCapabilities != null)
                    {
                        configuration =
                            await Helper.GetTrackConfigurationForCapabilities(videoCapabilities, VideoCodec);
                    }
                    if (configuration != null)
                    {
                        _peerConnection.AddTrack(mediaStreamTrack, mediaStreamList, configuration);
                    }
                }
            }
#else
            _mediaStream = await _media.GetUserMedia(mediaStreamConstraints);
#endif

            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }

#if !ORTCLIB
            Debug.WriteLine("Conductor: Adding local media stream.");
            _peerConnection.AddStream(_mediaStream);
#endif
            OnAddLocalStream?.Invoke(new MediaStreamEvent()
            {
                Stream = _mediaStream
            });

            if (cancelationToken.IsCancellationRequested)
            {
                return(false);
            }
            return(true);
        }