123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using Unity.WebRTC;
- using System.Linq;
- using Unity.WebRTC.Samples;
- class StatsSample : MonoBehaviour
- {
- #pragma warning disable 0649
- [SerializeField] private Button callButton;
- [SerializeField] private Button hangupButton;
- [SerializeField] private InputField text;
- [SerializeField] private Dropdown dropdown;
- [SerializeField] private Dropdown dropdownFreq;
- [SerializeField] private AudioSource source;
- [SerializeField] private Camera cam;
- #pragma warning restore 0649
- private RTCPeerConnection pc1, pc2;
- private RTCDataChannel dataChannel, remoteDataChannel;
- private DelegateOnIceConnectionChange pc1OnIceConnectionChange = null;
- private DelegateOnIceConnectionChange pc2OnIceConnectionChange = null;
- private DelegateOnIceCandidate pc1OnIceCandidate = null;
- private DelegateOnIceCandidate pc2OnIceCandidate = null;
- private DelegateOnMessage onDataChannelMessage = null;
- private DelegateOnOpen onDataChannelOpen = null;
- private DelegateOnClose onDataChannelClose = null;
- private DelegateOnDataChannel onDataChannel = null;
- private int currentValue = -1;
- private WaitForSeconds wait;
- private Dictionary<string, float> dictFrequency = new Dictionary<string, float>()
- {
- { "60ms", 0.06f},
- { "120ms", 0.12f},
- { "300ms", 0.3f},
- { "1000ms", 1f},
- };
- private void Awake()
- {
- WebRTC.Initialize(WebRTCSettings.LimitTextureSize);
- callButton.onClick.AddListener(() =>
- {
- callButton.interactable = false;
- hangupButton.interactable = true;
- StartCoroutine(Call());
- });
- hangupButton.onClick.AddListener(() =>
- {
- callButton.interactable = true;
- hangupButton.interactable = false;
- Dispose();
- });
- }
- private void OnDestroy()
- {
- WebRTC.Dispose();
- }
- private void Start()
- {
- dropdown.interactable = false;
- callButton.interactable = true;
- pc1OnIceConnectionChange = state => { OnIceConnectionChange(pc1, state); };
- pc2OnIceConnectionChange = state => { OnIceConnectionChange(pc2, state); };
- pc1OnIceCandidate = candidate => { OnIceCandidate(pc1, candidate); };
- pc2OnIceCandidate = candidate => { OnIceCandidate(pc1, candidate); };
- onDataChannel = channel =>
- {
- remoteDataChannel = channel;
- remoteDataChannel.OnMessage = onDataChannelMessage;
- };
- onDataChannelOpen = ()=> { };
- onDataChannelClose = () => { };
- dropdownFreq.options = dictFrequency.Select(pair => new Dropdown.OptionData(pair.Key)).ToList();
- dropdownFreq.value = 0;
- dropdownFreq.onValueChanged.AddListener(OnValueChangedFreq);
- OnValueChangedFreq(0);
- StartCoroutine(LoopGetStats());
- }
- void OnValueChangedFreq(int value)
- {
- wait = new WaitForSeconds(dictFrequency.ElementAt(value).Value);
- }
- RTCConfiguration GetSelectedSdpSemantics()
- {
- RTCConfiguration config = default;
- config.iceServers = new RTCIceServer[]
- {
- new RTCIceServer { urls = new string[] { "stun:stun.l.google.com:19302" } }
- };
- return config;
- }
- void OnIceConnectionChange(RTCPeerConnection pc, RTCIceConnectionState state)
- {
- switch (state)
- {
- case RTCIceConnectionState.New:
- Debug.Log($"{GetName(pc)} IceConnectionState: New");
- break;
- case RTCIceConnectionState.Checking:
- Debug.Log($"{GetName(pc)} IceConnectionState: Checking");
- break;
- case RTCIceConnectionState.Closed:
- Debug.Log($"{GetName(pc)} IceConnectionState: Closed");
- break;
- case RTCIceConnectionState.Completed:
- Debug.Log($"{GetName(pc)} IceConnectionState: Completed");
- break;
- case RTCIceConnectionState.Connected:
- Debug.Log($"{GetName(pc)} IceConnectionState: Connected");
- break;
- case RTCIceConnectionState.Disconnected:
- Debug.Log($"{GetName(pc)} IceConnectionState: Disconnected");
- break;
- case RTCIceConnectionState.Failed:
- Debug.Log($"{GetName(pc)} IceConnectionState: Failed");
- break;
- case RTCIceConnectionState.Max:
- Debug.Log($"{GetName(pc)} IceConnectionState: Max");
- break;
- default:
- break;
- }
- }
- IEnumerator Call()
- {
- callButton.interactable = false;
- Debug.Log("GetSelectedSdpSemantics");
- var configuration = GetSelectedSdpSemantics();
- pc1 = new RTCPeerConnection(ref configuration);
- Debug.Log("Created local peer connection object pc1");
- pc1.OnIceCandidate = pc1OnIceCandidate;
- pc1.OnIceConnectionChange = pc1OnIceConnectionChange;
- pc2 = new RTCPeerConnection(ref configuration);
- Debug.Log("Created remote peer connection object pc2");
- pc2.OnIceCandidate = pc2OnIceCandidate;
- pc2.OnIceConnectionChange = pc2OnIceConnectionChange;
- pc2.OnDataChannel = onDataChannel;
- dataChannel = pc1.CreateDataChannel("data");
- dataChannel.OnOpen = onDataChannelOpen;
- var audioTrack = new AudioStreamTrack(source);
- var videoTrack = cam.CaptureStreamTrack(1280, 720);
- yield return 0;
- pc1.AddTrack(audioTrack);
- pc1.AddTrack(videoTrack);
- Debug.Log("pc1 createOffer start");
- var op = pc1.CreateOffer();
- yield return op;
- if (!op.IsError)
- {
- yield return StartCoroutine(OnCreateOfferSuccess(op.Desc));
- }
- else
- {
- OnCreateSessionDescriptionError(op.Error);
- }
- }
- void Dispose()
- {
- dropdown.options = new List<Dropdown.OptionData>();
- dropdown.interactable = false;
- text.text = string.Empty;
- text.interactable = false;
- dataChannel.Dispose();
- pc1.Dispose();
- pc2.Dispose();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="pc"></param>
- /// <param name="streamEvent"></param>
- void OnIceCandidate(RTCPeerConnection pc, RTCIceCandidate candidate)
- {
- GetOtherPc(pc).AddIceCandidate(candidate);
- Debug.Log($"{GetName(pc)} ICE candidate:\n {candidate.Candidate}");
- }
- string GetName(RTCPeerConnection pc)
- {
- return (pc == pc1) ? "pc1" : "pc2";
- }
- RTCPeerConnection GetOtherPc(RTCPeerConnection pc)
- {
- return (pc == pc1) ? pc2 : pc1;
- }
- IEnumerator OnCreateOfferSuccess(RTCSessionDescription desc)
- {
- Debug.Log($"Offer from pc1\n{desc.sdp}");
- Debug.Log("pc1 setLocalDescription start");
- var op = pc1.SetLocalDescription(ref desc);
- yield return op;
- if (!op.IsError)
- {
- OnSetLocalSuccess(pc1);
- }
- else
- {
- var error = op.Error;
- OnSetSessionDescriptionError(ref error);
- }
- Debug.Log("pc2 setRemoteDescription start");
- var op2 = pc2.SetRemoteDescription(ref desc);
- yield return op2;
- if (!op2.IsError)
- {
- OnSetRemoteSuccess(pc2);
- }
- else
- {
- var error = op2.Error;
- OnSetSessionDescriptionError(ref error);
- }
- Debug.Log("pc2 createAnswer start");
- // Since the 'remote' side has no media stream we need
- // to pass in the right constraints in order for it to
- // accept the incoming offer of audio and video.
- var op3 = pc2.CreateAnswer();
- yield return op3;
- if (!op3.IsError)
- {
- yield return OnCreateAnswerSuccess(op3.Desc);
- }
- else
- {
- OnCreateSessionDescriptionError(op3.Error);
- }
- }
- void OnSetLocalSuccess(RTCPeerConnection pc)
- {
- Debug.Log($"{GetName(pc)} SetLocalDescription complete");
- }
- void OnSetSessionDescriptionError(ref RTCError error) { }
- void OnSetRemoteSuccess(RTCPeerConnection pc)
- {
- Debug.Log($"{GetName(pc)} SetRemoteDescription complete");
- }
- IEnumerator OnCreateAnswerSuccess(RTCSessionDescription desc)
- {
- Debug.Log($"Answer from pc2:\n{desc.sdp}");
- Debug.Log("pc2 setLocalDescription start");
- var op = pc2.SetLocalDescription(ref desc);
- yield return op;
- if (!op.IsError)
- {
- OnSetLocalSuccess(pc2);
- }
- else
- {
- var error = op.Error;
- OnSetSessionDescriptionError(ref error);
- }
- Debug.Log("pc1 setRemoteDescription start");
- var op2 = pc1.SetRemoteDescription(ref desc);
- yield return op2;
- if (!op2.IsError)
- {
- OnSetRemoteSuccess(pc1);
- }
- else
- {
- var error = op2.Error;
- OnSetSessionDescriptionError(ref error);
- }
- }
- IEnumerator LoopGetStats()
- {
- while (true)
- {
- yield return wait;
- if (callButton.interactable)
- continue;
- var op1 = pc1.GetStats();
- var op2 = pc2.GetStats();
- yield return op1;
- yield return op2;
- if(op1.IsError || op2.IsError)
- continue;
- if(dropdown.options.Count == 0)
- {
- List<string> options = new List<string>();
- foreach (var stat in op1.Value.Stats.Keys)
- {
- options.Add($"{stat}");
- }
- dropdown.ClearOptions();
- dropdown.AddOptions(options);
- dropdown.interactable = true;
- }
- if (currentValue != dropdown.value)
- {
- currentValue = dropdown.value;
- }
- var id = dropdown.options[currentValue].text;
- text.text = "Id:" + op1.Value.Stats[id].Id + "\n";
- text.text += "Timestamp:" + op1.Value.Stats[id].Timestamp + "\n";
- text.interactable = true;
- if (op1.Value.TryGetValue(id, out RTCStats stats))
- text.text += stats.Dict.Aggregate(string.Empty,(str, next) =>
- str + next.Key + ":" + (next.Value == null ? string.Empty : next.Value.ToString()) + "\n");
- op1.Value.Dispose();
- op2.Value.Dispose();
- }
- }
- void OnAddIceCandidateSuccess(RTCPeerConnection pc)
- {
- Debug.Log($"{GetName(pc)} addIceCandidate success");
- }
- void OnAddIceCandidateError(RTCPeerConnection pc, RTCError error)
- {
- Debug.Log($"{GetName(pc)} failed to add ICE Candidate: ${error}");
- }
- void OnCreateSessionDescriptionError(RTCError e)
- {
- }
- }
|