MultiVideoReceiveSample.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using Random = UnityEngine.Random;
  8. namespace Unity.WebRTC.Samples
  9. {
  10. class MultiVideoReceiveSample : MonoBehaviour
  11. {
  12. #pragma warning disable 0649
  13. [SerializeField] private Button callButton;
  14. [SerializeField] private Button hangUpButton;
  15. [SerializeField] private Button addVideoObjectButton;
  16. [SerializeField] private Button addTracksButton;
  17. [SerializeField] private Transform cameraParent;
  18. [SerializeField] private Transform sourceImageParent;
  19. [SerializeField] private Transform receiveImageParent;
  20. [SerializeField] private List<Camera> cameras;
  21. [SerializeField] private List<RawImage> sourceImages;
  22. [SerializeField] private List<RawImage> receiveImages;
  23. [SerializeField] private Transform rotateObject;
  24. #pragma warning restore 0649
  25. private RTCPeerConnection _pc1, _pc2;
  26. private List<VideoStreamTrack> videoStreamTrackList;
  27. private List<RTCRtpSender> sendingSenderList;
  28. private DelegateOnIceConnectionChange pc1OnIceConnectionChange;
  29. private DelegateOnIceConnectionChange pc2OnIceConnectionChange;
  30. private DelegateOnIceCandidate pc1OnIceCandidate;
  31. private DelegateOnIceCandidate pc2OnIceCandidate;
  32. private DelegateOnTrack pc2Ontrack;
  33. private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
  34. private DelegateOnNegotiationNeeded pc2OnNegotiationNeeded;
  35. private bool videoUpdateStarted;
  36. private int objectIndex = 0;
  37. private int videoIndex = 0;
  38. private void Awake()
  39. {
  40. WebRTC.Initialize(WebRTCSettings.LimitTextureSize);
  41. callButton.onClick.AddListener(Call);
  42. hangUpButton.onClick.AddListener(HangUp);
  43. addVideoObjectButton.onClick.AddListener(AddVideoObject);
  44. addTracksButton.onClick.AddListener(AddTracks);
  45. }
  46. private void OnDestroy()
  47. {
  48. WebRTC.Dispose();
  49. }
  50. private void Start()
  51. {
  52. videoStreamTrackList = new List<VideoStreamTrack>();
  53. sendingSenderList = new List<RTCRtpSender>();
  54. callButton.interactable = true;
  55. hangUpButton.interactable = false;
  56. pc1OnIceConnectionChange = state => { OnIceConnectionChange(_pc1, state); };
  57. pc2OnIceConnectionChange = state => { OnIceConnectionChange(_pc2, state); };
  58. pc1OnIceCandidate = candidate => { OnIceCandidate(_pc1, candidate); };
  59. pc2OnIceCandidate = candidate => { OnIceCandidate(_pc2, candidate); };
  60. pc2Ontrack = e =>
  61. {
  62. if (e.Track is VideoStreamTrack track)
  63. {
  64. track.OnVideoReceived += tex =>
  65. {
  66. receiveImages[videoIndex].texture = tex;
  67. videoIndex++;
  68. };
  69. }
  70. };
  71. pc1OnNegotiationNeeded = () => { StartCoroutine(PeerNegotiationNeeded(_pc1)); };
  72. pc2OnNegotiationNeeded = () => { StartCoroutine(PeerNegotiationNeeded(_pc2)); };
  73. }
  74. private void Update()
  75. {
  76. if (rotateObject != null)
  77. {
  78. rotateObject.Rotate(1, 2, 3);
  79. }
  80. }
  81. private static RTCConfiguration GetSelectedSdpSemantics()
  82. {
  83. RTCConfiguration config = default;
  84. config.iceServers = new[] {new RTCIceServer {urls = new[] {"stun:stun.l.google.com:19302"}}};
  85. return config;
  86. }
  87. private void OnIceConnectionChange(RTCPeerConnection pc, RTCIceConnectionState state)
  88. {
  89. switch (state)
  90. {
  91. case RTCIceConnectionState.New:
  92. Debug.Log($"{GetName(pc)} IceConnectionState: New");
  93. break;
  94. case RTCIceConnectionState.Checking:
  95. Debug.Log($"{GetName(pc)} IceConnectionState: Checking");
  96. break;
  97. case RTCIceConnectionState.Closed:
  98. Debug.Log($"{GetName(pc)} IceConnectionState: Closed");
  99. break;
  100. case RTCIceConnectionState.Completed:
  101. Debug.Log($"{GetName(pc)} IceConnectionState: Completed");
  102. break;
  103. case RTCIceConnectionState.Connected:
  104. Debug.Log($"{GetName(pc)} IceConnectionState: Connected");
  105. break;
  106. case RTCIceConnectionState.Disconnected:
  107. Debug.Log($"{GetName(pc)} IceConnectionState: Disconnected");
  108. break;
  109. case RTCIceConnectionState.Failed:
  110. Debug.Log($"{GetName(pc)} IceConnectionState: Failed");
  111. break;
  112. case RTCIceConnectionState.Max:
  113. Debug.Log($"{GetName(pc)} IceConnectionState: Max");
  114. break;
  115. default:
  116. throw new ArgumentOutOfRangeException(nameof(state), state, null);
  117. }
  118. }
  119. IEnumerator PeerNegotiationNeeded(RTCPeerConnection pc)
  120. {
  121. Debug.Log($"{GetName(pc)} createOffer start");
  122. var op = pc.CreateOffer();
  123. yield return op;
  124. if (!op.IsError)
  125. {
  126. if (pc.SignalingState != RTCSignalingState.Stable)
  127. {
  128. Debug.LogError($"{GetName(pc)} signaling state is not stable.");
  129. yield break;
  130. }
  131. yield return StartCoroutine(OnCreateOfferSuccess(pc, op.Desc));
  132. }
  133. else
  134. {
  135. OnCreateSessionDescriptionError(op.Error);
  136. }
  137. }
  138. private void AddVideoObject()
  139. {
  140. var newCam = new GameObject($"Camera{objectIndex}").AddComponent<Camera>();
  141. newCam.backgroundColor = Random.ColorHSV();
  142. newCam.transform.SetParent(cameraParent);
  143. cameras.Add(newCam);
  144. var newSource = new GameObject($"SourceImage{objectIndex}").AddComponent<RawImage>();
  145. newSource.transform.SetParent(sourceImageParent);
  146. sourceImages.Add(newSource);
  147. var newReceive = new GameObject($"ReceiveImage{objectIndex}").AddComponent<RawImage>();
  148. newReceive.transform.SetParent(receiveImageParent);
  149. receiveImages.Add(newReceive);
  150. try
  151. {
  152. videoStreamTrackList.Add(newCam.CaptureStreamTrack(WebRTCSettings.StreamSize.x, WebRTCSettings.StreamSize.y));
  153. newSource.texture = newCam.targetTexture;
  154. }
  155. catch (Exception e)
  156. {
  157. Debug.LogError(e.Message);
  158. HangUp();
  159. return;
  160. }
  161. objectIndex++;
  162. addTracksButton.interactable = true;
  163. }
  164. private void Call()
  165. {
  166. callButton.interactable = false;
  167. hangUpButton.interactable = true;
  168. addVideoObjectButton.interactable = true;
  169. addTracksButton.interactable = false;
  170. Debug.Log("GetSelectedSdpSemantics");
  171. var configuration = GetSelectedSdpSemantics();
  172. _pc1 = new RTCPeerConnection(ref configuration);
  173. Debug.Log("Created local peer connection object pc1");
  174. _pc1.OnIceCandidate = pc1OnIceCandidate;
  175. _pc1.OnIceConnectionChange = pc1OnIceConnectionChange;
  176. _pc1.OnNegotiationNeeded = pc1OnNegotiationNeeded;
  177. _pc2 = new RTCPeerConnection(ref configuration);
  178. Debug.Log("Created remote peer connection object pc2");
  179. _pc2.OnIceCandidate = pc2OnIceCandidate;
  180. _pc2.OnIceConnectionChange = pc2OnIceConnectionChange;
  181. _pc2.OnTrack = pc2Ontrack;
  182. _pc2.OnNegotiationNeeded = pc2OnNegotiationNeeded;
  183. if (!videoUpdateStarted)
  184. {
  185. StartCoroutine(WebRTC.Update());
  186. videoUpdateStarted = true;
  187. }
  188. }
  189. private void AddTracks()
  190. {
  191. Debug.Log("Add not added tracks");
  192. foreach (var track in videoStreamTrackList.Where(x =>
  193. !sendingSenderList.Exists(y => y.Track.Id == x.Id)))
  194. {
  195. var sender = _pc1.AddTrack(track);
  196. sendingSenderList.Add(sender);
  197. }
  198. if (WebRTCSettings.UseVideoCodec != null)
  199. {
  200. var codecs = new[] {WebRTCSettings.UseVideoCodec};
  201. foreach (var transceiver in _pc1.GetTransceivers())
  202. {
  203. if (sendingSenderList.Contains(transceiver.Sender))
  204. {
  205. transceiver.SetCodecPreferences(codecs);
  206. }
  207. }
  208. }
  209. }
  210. private void HangUp()
  211. {
  212. foreach (var image in receiveImages.Concat(sourceImages))
  213. {
  214. image.texture = null;
  215. DestroyImmediate(image.gameObject);
  216. }
  217. receiveImages.Clear();
  218. sourceImages.Clear();
  219. foreach (var cam in cameras)
  220. {
  221. DestroyImmediate(cam.gameObject);
  222. }
  223. cameras.Clear();
  224. foreach (var track in videoStreamTrackList)
  225. {
  226. track.Dispose();
  227. }
  228. videoStreamTrackList.Clear();
  229. sendingSenderList.Clear();
  230. _pc1.Close();
  231. _pc2.Close();
  232. Debug.Log("Close local/remote peer connection");
  233. _pc1.Dispose();
  234. _pc2.Dispose();
  235. _pc1 = null;
  236. _pc2 = null;
  237. videoIndex = 0;
  238. objectIndex = 0;
  239. callButton.interactable = true;
  240. hangUpButton.interactable = false;
  241. addVideoObjectButton.interactable = false;
  242. addTracksButton.interactable = false;
  243. }
  244. private void OnIceCandidate(RTCPeerConnection pc, RTCIceCandidate candidate)
  245. {
  246. GetOtherPc(pc).AddIceCandidate(candidate);
  247. Debug.Log($"{GetName(pc)} ICE candidate:\n {candidate.Candidate}");
  248. }
  249. private string GetName(RTCPeerConnection pc)
  250. {
  251. return (pc == _pc1) ? "pc1" : "pc2";
  252. }
  253. private RTCPeerConnection GetOtherPc(RTCPeerConnection pc)
  254. {
  255. return (pc == _pc1) ? _pc2 : _pc1;
  256. }
  257. private IEnumerator OnCreateOfferSuccess(RTCPeerConnection pc, RTCSessionDescription desc)
  258. {
  259. Debug.Log($"Offer from {GetName(pc)}\n{desc.sdp}");
  260. Debug.Log($"{GetName(pc)} setLocalDescription start");
  261. var op = pc.SetLocalDescription(ref desc);
  262. yield return op;
  263. if (!op.IsError)
  264. {
  265. OnSetLocalSuccess(pc);
  266. }
  267. else
  268. {
  269. var error = op.Error;
  270. OnSetSessionDescriptionError(ref error);
  271. }
  272. var otherPc = GetOtherPc(pc);
  273. Debug.Log($"{GetName(otherPc)} setRemoteDescription start");
  274. var op2 = otherPc.SetRemoteDescription(ref desc);
  275. yield return op2;
  276. if (!op2.IsError)
  277. {
  278. OnSetRemoteSuccess(otherPc);
  279. }
  280. else
  281. {
  282. var error = op2.Error;
  283. OnSetSessionDescriptionError(ref error);
  284. }
  285. Debug.Log($"{GetName(otherPc)} createAnswer start");
  286. // Since the 'remote' side has no media stream we need
  287. // to pass in the right constraints in order for it to
  288. // accept the incoming offer of audio and video.
  289. var op3 = otherPc.CreateAnswer();
  290. yield return op3;
  291. if (!op3.IsError)
  292. {
  293. yield return OnCreateAnswerSuccess(otherPc, op3.Desc);
  294. }
  295. else
  296. {
  297. OnCreateSessionDescriptionError(op3.Error);
  298. }
  299. }
  300. private void OnSetLocalSuccess(RTCPeerConnection pc)
  301. {
  302. Debug.Log($"{GetName(pc)} SetLocalDescription complete");
  303. }
  304. static void OnSetSessionDescriptionError(ref RTCError error)
  305. {
  306. Debug.LogError($"Error Detail Type: {error.message}");
  307. }
  308. private void OnSetRemoteSuccess(RTCPeerConnection pc)
  309. {
  310. Debug.Log($"{GetName(pc)} SetRemoteDescription complete");
  311. }
  312. IEnumerator OnCreateAnswerSuccess(RTCPeerConnection pc, RTCSessionDescription desc)
  313. {
  314. Debug.Log($"Answer from {GetName(pc)}:\n{desc.sdp}");
  315. Debug.Log($"{GetName(pc)} setLocalDescription start");
  316. var op = pc.SetLocalDescription(ref desc);
  317. yield return op;
  318. if (!op.IsError)
  319. {
  320. OnSetLocalSuccess(pc);
  321. }
  322. else
  323. {
  324. var error = op.Error;
  325. OnSetSessionDescriptionError(ref error);
  326. }
  327. var otherPc = GetOtherPc(pc);
  328. Debug.Log($"{GetName(otherPc)} setRemoteDescription start");
  329. var op2 = otherPc.SetRemoteDescription(ref desc);
  330. yield return op2;
  331. if (!op2.IsError)
  332. {
  333. OnSetRemoteSuccess(otherPc);
  334. }
  335. else
  336. {
  337. var error = op2.Error;
  338. OnSetSessionDescriptionError(ref error);
  339. }
  340. }
  341. private static void OnCreateSessionDescriptionError(RTCError error)
  342. {
  343. Debug.LogError($"Error Detail Type: {error.message}");
  344. }
  345. }
  346. }