RTCPeerConnection CreatePeerConnection(string connectionId, bool isOffer) { if (_mapConnectionIdAndPeer.TryGetValue(connectionId, out var peer)) { peer.Close(); } var pc = new RTCPeerConnection(); _mapConnectionIdAndPeer[connectionId] = pc; pc.SetConfiguration(ref _config); pc.OnDataChannel = channel => { OnDataChannel(connectionId, channel); }; pc.OnIceCandidate = candidate => { _signaling.SendCandidate(connectionId, candidate); }; pc.OnIceConnectionChange = state => OnIceConnectionChange(connectionId, state); pc.OnTrack = trackEvent => { onAddReceiver?.Invoke(connectionId, trackEvent.Receiver); }; pc.OnNegotiationNeeded = () => OnNegotiationNeeded(connectionId, isOffer); return(pc); }
public IEnumerator OnCandidate() { bool startRaised1 = false; bool startRaised2 = false; bool offerRaised = false; bool answerRaised = false; bool candidateRaised1 = false; bool candidateRaised2 = false; const string connectionId = "12345"; string connectionId1 = null; string connectionId2 = null; signaling1.OnStart += s => { startRaised1 = true; }; signaling2.OnStart += s => { startRaised2 = true; }; signaling1.Start(); signaling2.Start(); yield return(new WaitUntil(() => startRaised1 && startRaised2)); signaling1.OnCreateConnection += (s, id, peerExists) => { connectionId1 = id; }; signaling1.OpenConnection(connectionId); signaling2.OnCreateConnection += (s, id, peerExists) => { connectionId2 = id; }; signaling2.OpenConnection(connectionId); yield return(new WaitUntil(() => !string.IsNullOrEmpty(connectionId1) && !string.IsNullOrEmpty(connectionId2))); signaling2.OnOffer += (s, e) => { offerRaised = true; }; signaling1.SendOffer(connectionId1, m_DescOffer); yield return(new WaitUntil(() => offerRaised)); signaling1.OnAnswer += (s, e) => { answerRaised = true; }; signaling2.SendAnswer(connectionId1, m_DescAnswer); yield return(new WaitUntil(() => answerRaised)); signaling2.OnIceCandidate += (s, e) => { candidateRaised1 = true; }; signaling1.SendCandidate(connectionId1, m_candidate); yield return(new WaitUntil(() => candidateRaised1)); signaling1.OnIceCandidate += (s, e) => { candidateRaised2 = true; }; signaling2.SendCandidate(connectionId1, m_candidate); yield return(new WaitUntil(() => candidateRaised2)); signaling1.CloseConnection(connectionId1); signaling2.CloseConnection(connectionId2); signaling1.Stop(); signaling2.Stop(); yield return(new WaitForSeconds(1)); }
public IEnumerator OnCandidate() { bool startRaised1 = false; bool startRaised2 = false; bool offerRaised = false; bool answerRaised = false; bool candidateRaised1 = false; bool candidateRaised2 = false; string connectionId1 = null; string connectionId2 = null; signaling1.OnStart += s => { startRaised1 = true; }; signaling2.OnStart += s => { startRaised2 = true; }; signaling1.Start(); signaling2.Start(); yield return(new WaitUntil(() => startRaised1 && startRaised2)); signaling1.OnCreateConnection += (s, connectionId, polite) => { connectionId1 = connectionId; }; signaling1.OpenConnection(Guid.NewGuid().ToString()); signaling2.OnCreateConnection += (s, connectionId, polite) => { connectionId2 = connectionId; }; signaling2.OpenConnection(Guid.NewGuid().ToString()); yield return(new WaitUntil(() => !string.IsNullOrEmpty(connectionId1) && !string.IsNullOrEmpty(connectionId2))); signaling2.OnOffer += (s, e) => { offerRaised = true; }; signaling1.SendOffer(connectionId1, m_DescOffer); yield return(new WaitUntil(() => offerRaised)); signaling1.OnAnswer += (s, e) => { answerRaised = true; }; signaling2.SendAnswer(connectionId1, m_DescAnswer); yield return(new WaitUntil(() => answerRaised)); signaling2.OnIceCandidate += (s, e) => { candidateRaised1 = true; }; signaling1.SendCandidate(connectionId1, m_candidate); yield return(new WaitUntil(() => candidateRaised1)); signaling1.OnIceCandidate += (s, e) => { candidateRaised2 = true; }; signaling2.SendCandidate(connectionId1, m_candidate); yield return(new WaitUntil(() => candidateRaised2)); }
RTCPeerConnection CreatePeerConnection(ISignaling signaling, string connectionId, bool isOffer) { if (m_mapConnectionIdAndPeer.TryGetValue(connectionId, out var peer)) { peer.Close(); } var pc = new RTCPeerConnection(); m_mapConnectionIdAndPeer[connectionId] = pc; pc.OnDataChannel = new DelegateOnDataChannel(channel => { OnDataChannel(pc, channel); }); pc.SetConfiguration(ref m_conf); pc.OnIceCandidate = new DelegateOnIceCandidate(candidate => { signaling.SendCandidate(connectionId, candidate); }); pc.OnIceConnectionChange = new DelegateOnIceConnectionChange(state => { if (state == RTCIceConnectionState.Disconnected) { pc.Close(); m_mapConnectionIdAndPeer.Remove(connectionId); } }); pc.OnTrack = trackEvent => { foreach (var viewer in m_listVideoReceiveViewer) { viewer.AddTrack(connectionId, trackEvent.Track); } }; pc.OnNegotiationNeeded = () => StartCoroutine(OnNegotiationNeeded(signaling, connectionId, isOffer)); return(pc); }
public void OnCandidate() { bool startRaised1 = false; bool startRaised2 = false; bool offerRaised = false; bool answerRaised = false; bool candidateRaised1 = false; bool candidateRaised2 = false; string connectionId1 = null; signaling1.Start(); signaling2.Start(); signaling1.OnStart += s => { startRaised1 = true; }; signaling2.OnStart += s => { startRaised2 = true; }; Assert.True(Wait(() => startRaised1 && startRaised2)); signaling1.OnCreateConnection += (s, connectionId) => { connectionId1 = connectionId; }; signaling1.CreateConnection(); Assert.True(Wait(() => !string.IsNullOrEmpty(connectionId1))); signaling2.OnOffer += (s, e) => { offerRaised = true; }; signaling1.SendOffer(connectionId1, m_DescOffer); Assert.True(Wait(() => offerRaised)); signaling1.OnAnswer += (s, e) => { answerRaised = true; }; signaling2.SendAnswer(connectionId1, m_DescAnswer); Assert.True(Wait(() => answerRaised)); signaling2.OnIceCandidate += (s, e) => { candidateRaised1 = true; }; signaling1.SendCandidate(connectionId1, m_candidate); Assert.True(Wait(() => candidateRaised1)); signaling1.OnIceCandidate += (s, e) => { candidateRaised2 = true; }; signaling2.SendCandidate(connectionId1, m_candidate); Assert.True(Wait(() => candidateRaised2)); }
void OnOffer(ISignaling signaling, DescData e) { RTCSessionDescription _desc; _desc.type = RTCSdpType.Offer; _desc.sdp = e.sdp; var connectionId = e.connectionId; if (m_mapConnectionIdAndPeer.ContainsKey(connectionId)) { return; } var pc = new RTCPeerConnection(); m_mapConnectionIdAndPeer.Add(e.connectionId, pc); pc.OnDataChannel = new DelegateOnDataChannel(channel => { OnDataChannel(pc, channel); }); pc.SetConfiguration(ref m_conf); pc.OnIceCandidate = new DelegateOnIceCandidate(candidate => { signaling.SendCandidate(e.connectionId, candidate); }); pc.OnIceConnectionChange = new DelegateOnIceConnectionChange(state => { if (state == RTCIceConnectionState.Disconnected) { pc.Close(); m_mapConnectionIdAndPeer.Remove(e.connectionId); } }); pc.SetRemoteDescription(ref _desc); foreach (var track in m_listVideoStreamTrack.Concat(m_audioStream.GetTracks())) { RTCRtpSender sender = pc.AddTrack(track); if (!m_mapTrackAndSenderList.TryGetValue(track, out List <RTCRtpSender> list)) { list = new List <RTCRtpSender>(); m_mapTrackAndSenderList.Add(track, list); } list.Add(sender); } RTCAnswerOptions options = default; var op = pc.CreateAnswer(ref options); while (op.MoveNext()) { } if (op.IsError) { Debug.LogError($"Network Error: {op.Error}"); return; } var desc = op.Desc; var opLocalDesc = pc.SetLocalDescription(ref desc); while (opLocalDesc.MoveNext()) { } if (opLocalDesc.IsError) { Debug.LogError($"Network Error: {opLocalDesc.Error}"); return; } signaling.SendAnswer(connectionId, desc); }
void OnOffer(ISignaling signaling, DescData e) { RTCSessionDescription _desc; _desc.type = RTCSdpType.Offer; _desc.sdp = e.sdp; var connectionId = e.connectionId; if (m_mapConnectionIdAndPeer.ContainsKey(connectionId)) { return; } var pc = new RTCPeerConnection(); m_mapConnectionIdAndPeer.Add(e.connectionId, pc); pc.OnDataChannel = new DelegateOnDataChannel(channel => { OnDataChannel(pc, channel); }); pc.SetConfiguration(ref m_conf); pc.OnIceCandidate = new DelegateOnIceCandidate(candidate => { signaling.SendCandidate(e.connectionId, candidate); }); pc.OnIceConnectionChange = new DelegateOnIceConnectionChange(state => { if (state == RTCIceConnectionState.Disconnected) { pc.Close(); m_mapConnectionIdAndPeer.Remove(e.connectionId); } }); //make video bit rate starts at 16000kbits, and 160000kbits at max. string pattern = @"(a=fmtp:\d+ .*level-asymmetry-allowed=.*)\r\n"; _desc.sdp = Regex.Replace(_desc.sdp, pattern, "$1;x-google-start-bitrate=16000;x-google-max-bitrate=160000\r\n"); pc.SetRemoteDescription(ref _desc); foreach (var track in m_listVideoStreamTrack) { pc.AddTrack(track); } foreach (var track in m_audioStream.GetTracks()) { pc.AddTrack(track); } RTCAnswerOptions options = default; var op = pc.CreateAnswer(ref options); while (op.MoveNext()) { } if (op.IsError) { Debug.LogError($"Network Error: {op.Error}"); return; } var desc = op.Desc; var opLocalDesc = pc.SetLocalDescription(ref desc); while (opLocalDesc.MoveNext()) { } if (opLocalDesc.IsError) { Debug.LogError($"Network Error: {opLocalDesc.Error}"); return; } signaling.SendAnswer(connectionId, desc); }
public IEnumerator OnCandidate() { bool startRaised1 = false; bool startRaised2 = false; bool offerRaised = false; bool answerRaised = false; bool candidateRaised1 = false; bool candidateRaised2 = false; const string connectionId = "12345"; bool raiseOnDestroy1 = false; bool raiseOnDestroy2 = false; string connectionId1 = null; string connectionId2 = null; signaling1.OnStart += s => { startRaised1 = true; }; signaling2.OnStart += s => { startRaised2 = true; }; signaling1.Start(); signaling2.Start(); yield return(new WaitUntil(() => startRaised1 && startRaised2)); signaling1.OnCreateConnection += (s, id, polite) => { connectionId1 = id; }; signaling1.OnDestroyConnection += (signaling, id) => { raiseOnDestroy1 = id == connectionId1; }; signaling1.OpenConnection(connectionId); signaling2.OnCreateConnection += (s, id, polite) => { connectionId2 = id; }; signaling2.OnDestroyConnection += (signaling, id) => { raiseOnDestroy2 = id == connectionId2; }; signaling2.OpenConnection(connectionId); yield return(new WaitUntil(() => !string.IsNullOrEmpty(connectionId1) && !string.IsNullOrEmpty(connectionId2))); Assert.That(connectionId1, Is.EqualTo(connectionId)); Assert.That(connectionId2, Is.EqualTo(connectionId)); signaling2.OnOffer += (s, e) => { offerRaised = true; }; signaling1.SendOffer(connectionId1, m_DescOffer); yield return(new WaitUntil(() => offerRaised)); signaling1.OnAnswer += (s, e) => { answerRaised = true; }; signaling2.SendAnswer(connectionId1, m_DescAnswer); yield return(new WaitUntil(() => answerRaised)); signaling2.OnIceCandidate += (s, e) => { candidateRaised1 = true; }; signaling1.SendCandidate(connectionId1, m_candidate); yield return(new WaitUntil(() => candidateRaised1)); signaling1.OnIceCandidate += (s, e) => { candidateRaised2 = true; }; signaling2.SendCandidate(connectionId1, m_candidate); yield return(new WaitUntil(() => candidateRaised2)); signaling1.CloseConnection(connectionId1); yield return(new WaitUntil(() => raiseOnDestroy1 && raiseOnDestroy2)); Assert.That(raiseOnDestroy1, Is.True); Assert.That(raiseOnDestroy2, Is.True); signaling2.CloseConnection(connectionId2); signaling1.Stop(); signaling2.Stop(); yield return(new WaitForSeconds(1)); }