public async Task CaptureMedia(long fromUid, bool r = false) { LocalMedia = Media.CreateMedia(); //创建一个Media对象 RTCMediaStreamConstraints mediaStreamConstraints = new RTCMediaStreamConstraints() //设置要获取的流 { audioEnabled = true, videoEnabled = true }; //音频播放 var apd = LocalMedia.GetAudioPlayoutDevices(); if (apd.Count > 0) { LocalMedia.SelectAudioPlayoutDevice(apd[0]); } if (fromUid == 0) { //音频捕获 var acd = LocalMedia.GetAudioCaptureDevices(); if (acd.Count > 0) { mediaStreamConstraints.audioEnabled = true; LocalMedia.SelectAudioCaptureDevice(acd[0]); } //视频捕获 var vcd = LocalMedia.GetVideoCaptureDevices(); if (vcd.Count > 0) { mediaStreamConstraints.videoEnabled = true; LocalMedia.SelectVideoDevice(vcd.First(p => p.Location.Panel == Windows.Devices.Enumeration.Panel.Front));//设置视频捕获设备 } } var mediaStream = await LocalMedia.GetUserMedia(mediaStreamConstraints);//获取视频流 这里视频和音频是一起传输的 if (fromUid == 0) { var videotracs = mediaStream.GetVideoTracks(); if (videotracs.Count > 0) { var source = LocalMedia.CreateMediaSource(videotracs.FirstOrDefault(), mediaStream.Id); //创建播放源 LocalMediaPlayer.SetMediaStreamSource(source); //设置MediaElement的播放源 LocalMediaPlayer.Play(); } await CreatePublisher(mediaStream); //await CreateServer(mediaStream); } else { await CreateReceiver(mediaStream, fromUid); } }
/// <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); }
public SymplePlayerEngineWebRTC(SymplePlayer player) : base(player) { Messenger.Broadcast(SympleLog.LogDebug, "symple:webrtc: init"); #if NETFX_CORE if (!webrtcInitialized) { // needed before calling any webrtc functions http://stackoverflow.com/questions/43331677/webrtc-for-uwp-new-rtcpeerconnection-doesnt-complete-execution if (player.options.CoreDispatcher != null) { WebRTC.Initialize(player.options.CoreDispatcher); } else { WebRTC.Initialize(null); } WebRTC.EnableLogging(LogLevel.LOGLVL_ERROR); WebRTC.EnableLogging(LogLevel.LOGLVL_INFO); WebRTC.EnableLogging(LogLevel.LOGLVL_SENSITIVE); WebRTC.EnableLogging(LogLevel.LOGLVL_VERBOSE); WebRTC.EnableLogging(LogLevel.LOGLVL_WARNING); Messenger.Broadcast(SympleLog.LogInfo, "WebRTC logging enabled, log folder = " + WebRTC.LogFolder.Path + ", filename = " + WebRTC.LogFileName); webrtcInitialized = true; } this.userMediaConstraints = player.options.userMediaConstraints; if (player.options.rtcConfig != null) { this.rtcConfig = player.options.rtcConfig; } else { this.rtcConfig = new RTCConfiguration(); this.rtcConfig.IceServers.Add(new RTCIceServer() { Url = "stun:stun.l.google.com:19302", Username = string.Empty, Credential = string.Empty }); } #endif /* * this.rtcOptions = player.options.rtcOptions || { * optional: [ * { DtlsSrtpKeyAgreement: true } // required for FF <=> Chrome interop * ] * }; */ // Specifies that this client will be the ICE initiator, // and will be sending the initial SDP Offer. this.initiator = player.options.initiator; Messenger.Broadcast(SympleLog.LogDebug, "symple:webrtc: constructor, set this.initiator to " + this.initiator); #if NETFX_CORE // Reference to the active local or remote media stream this.activeStream = null; #endif }
private async Task Initialize() { //Initialization of WebRTC worker threads, etc Org.WebRtc.WebRTC.Initialize(Dispatcher); _media = Media.CreateMedia(); //Selecting video device to use, setting preferred capabilities var videoDevices = _media.GetVideoCaptureDevices(); var selectedVideoDevice = videoDevices.First(); var videoCapabilites = await selectedVideoDevice.GetVideoCaptureCapabilities(); var selectedVideoCapability = videoCapabilites.FirstOrDefault(); //Needed for HoloLens camera, will not set compatible video capability automatically //Hololens Cam default capability: 1280x720x30 Org.WebRtc.WebRTC.SetPreferredVideoCaptureFormat( (int)selectedVideoCapability.Width, (int)selectedVideoCapability.Height, (int)selectedVideoCapability.FrameRate); //Setting up local stream RTCMediaStreamConstraints mediaStreamConstraints = new RTCMediaStreamConstraints { audioEnabled = false, videoEnabled = true }; _localStream = await _media.GetUserMedia(mediaStreamConstraints); _media.SelectVideoDevice(selectedVideoDevice); // Get Video Tracks var videotrac = _localStream.GetVideoTracks(); foreach (var videoTrack in videotrac) //This foreach may not be necessary { videoTrack.Enabled = true; } var selectedVideoTrac = videotrac.FirstOrDefault(); Debug.WriteLine("Creating RTCPeerConnection"); var config = new RTCConfiguration() { BundlePolicy = RTCBundlePolicy.Balanced, IceTransportPolicy = RTCIceTransportPolicy.All, IceServers = GetDefaultList() }; _peerConnection = new RTCPeerConnection(config); _peerConnection.OnIceCandidate += _localRtcPeerConnection_OnIceCandidate; _peerConnection.OnIceConnectionChange += _localRtcPeerConnection_OnIceConnectionChange; _peerConnection.OnAddStream += _peerConnection_OnAddStream; //_peerConnection.AddStream(_localStream); _media.AddVideoTrackMediaElementPair(selectedVideoTrac, _localVideo, _localStream.Id); // Send event started Element.SendStarted(); //Debug.WriteLine("Creating 'remote' RTCPeerConnection"); //_remoteRtcPeerConnection = new RTCPeerConnection(config); //_remoteRtcPeerConnection.OnIceCandidate += _remoteRtcPeerConnection_OnIceCandidate; //_remoteRtcPeerConnection.OnIceConnectionChange += _remoteRtcPeerConnection_OnIceConnectionChange; //// Wait for Stream //_remoteRtcPeerConnection.OnAddStream += _remoteRtcPeerConnection_OnAddStream; }