VideoReceiveSample.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. namespace Unity.WebRTC.Samples
  8. {
  9. class VideoReceiveSample : MonoBehaviour
  10. {
  11. #pragma warning disable 0649
  12. [SerializeField] private Button callButton;
  13. [SerializeField] private Button hangUpButton;
  14. [SerializeField] private Button addTracksButton;
  15. [SerializeField] private Button removeTracksButton;
  16. [SerializeField] private Toggle useWebCamToggle;
  17. [SerializeField] private Dropdown webCamListDropdown;
  18. [SerializeField] private Toggle useMicToggle;
  19. [SerializeField] private Dropdown micListDropdown;
  20. [SerializeField] private Camera cam;
  21. [SerializeField] private AudioClip clip;
  22. [SerializeField] private RawImage sourceImage;
  23. [SerializeField] private AudioSource sourceAudio;
  24. [SerializeField] private RawImage receiveImage;
  25. [SerializeField] private AudioSource receiveAudio;
  26. [SerializeField] private Transform rotateObject;
  27. #pragma warning restore 0649
  28. private RTCPeerConnection _pc1, _pc2;
  29. private List<RTCRtpSender> pc1Senders;
  30. private VideoStreamTrack videoStreamTrack;
  31. private AudioStreamTrack audioStreamTrack;
  32. private MediaStream receiveAudioStream, receiveVideoStream;
  33. private DelegateOnIceConnectionChange pc1OnIceConnectionChange;
  34. private DelegateOnIceConnectionChange pc2OnIceConnectionChange;
  35. private DelegateOnIceCandidate pc1OnIceCandidate;
  36. private DelegateOnIceCandidate pc2OnIceCandidate;
  37. private DelegateOnTrack pc2Ontrack;
  38. private DelegateOnNegotiationNeeded pc1OnNegotiationNeeded;
  39. private WebCamTexture webCamTexture;
  40. private void Awake()
  41. {
  42. WebRTC.Initialize(WebRTCSettings.LimitTextureSize);
  43. callButton.onClick.AddListener(Call);
  44. hangUpButton.onClick.AddListener(HangUp);
  45. addTracksButton.onClick.AddListener(AddTracks);
  46. removeTracksButton.onClick.AddListener(RemoveTracks);
  47. useWebCamToggle.onValueChanged.AddListener(SwitchUseWebCam);
  48. webCamListDropdown.options = WebCamTexture.devices.Select(x => new Dropdown.OptionData(x.name)).ToList();
  49. useMicToggle.onValueChanged.AddListener(SwitchUseMic);
  50. micListDropdown.options = Microphone.devices.Select(x => new Dropdown.OptionData(x)).ToList();
  51. }
  52. private void OnDestroy()
  53. {
  54. if (webCamTexture != null)
  55. {
  56. webCamTexture.Stop();
  57. webCamTexture = null;
  58. }
  59. WebRTC.Dispose();
  60. }
  61. private void Start()
  62. {
  63. pc1Senders = new List<RTCRtpSender>();
  64. callButton.interactable = true;
  65. hangUpButton.interactable = false;
  66. pc1OnIceConnectionChange = state => { OnIceConnectionChange(_pc1, state); };
  67. pc2OnIceConnectionChange = state => { OnIceConnectionChange(_pc2, state); };
  68. pc1OnIceCandidate = candidate => { OnIceCandidate(_pc1, candidate); };
  69. pc2OnIceCandidate = candidate => { OnIceCandidate(_pc2, candidate); };
  70. pc2Ontrack = e =>
  71. {
  72. if (e.Track is VideoStreamTrack video)
  73. {
  74. video.OnVideoReceived += tex =>
  75. {
  76. receiveImage.texture = tex;
  77. };
  78. }
  79. if (e.Track is AudioStreamTrack audioTrack)
  80. {
  81. receiveAudio.SetTrack(audioTrack);
  82. receiveAudio.loop = true;
  83. receiveAudio.Play();
  84. }
  85. };
  86. pc1OnNegotiationNeeded = () => { StartCoroutine(PeerNegotiationNeeded(_pc1)); };
  87. StartCoroutine(WebRTC.Update());
  88. }
  89. private void Update()
  90. {
  91. if (rotateObject != null)
  92. {
  93. rotateObject.Rotate(1, 2, 3);
  94. }
  95. }
  96. private static RTCConfiguration GetSelectedSdpSemantics()
  97. {
  98. RTCConfiguration config = default;
  99. config.iceServers = new[] {new RTCIceServer {urls = new[] {"stun:stun.l.google.com:19302"}}};
  100. return config;
  101. }
  102. private void OnIceConnectionChange(RTCPeerConnection pc, RTCIceConnectionState state)
  103. {
  104. switch (state)
  105. {
  106. case RTCIceConnectionState.New:
  107. Debug.Log($"{GetName(pc)} IceConnectionState: New");
  108. break;
  109. case RTCIceConnectionState.Checking:
  110. Debug.Log($"{GetName(pc)} IceConnectionState: Checking");
  111. break;
  112. case RTCIceConnectionState.Closed:
  113. Debug.Log($"{GetName(pc)} IceConnectionState: Closed");
  114. break;
  115. case RTCIceConnectionState.Completed:
  116. Debug.Log($"{GetName(pc)} IceConnectionState: Completed");
  117. break;
  118. case RTCIceConnectionState.Connected:
  119. Debug.Log($"{GetName(pc)} IceConnectionState: Connected");
  120. break;
  121. case RTCIceConnectionState.Disconnected:
  122. Debug.Log($"{GetName(pc)} IceConnectionState: Disconnected");
  123. break;
  124. case RTCIceConnectionState.Failed:
  125. Debug.Log($"{GetName(pc)} IceConnectionState: Failed");
  126. break;
  127. case RTCIceConnectionState.Max:
  128. Debug.Log($"{GetName(pc)} IceConnectionState: Max");
  129. break;
  130. default:
  131. throw new ArgumentOutOfRangeException(nameof(state), state, null);
  132. }
  133. }
  134. IEnumerator PeerNegotiationNeeded(RTCPeerConnection pc)
  135. {
  136. Debug.Log($"{GetName(pc)} createOffer start");
  137. var op = pc.CreateOffer();
  138. yield return op;
  139. if (!op.IsError)
  140. {
  141. if (pc.SignalingState != RTCSignalingState.Stable)
  142. {
  143. Debug.LogError($"{GetName(pc)} signaling state is not stable.");
  144. yield break;
  145. }
  146. yield return StartCoroutine(OnCreateOfferSuccess(pc, op.Desc));
  147. }
  148. else
  149. {
  150. OnCreateSessionDescriptionError(op.Error);
  151. }
  152. }
  153. private void AddTracks()
  154. {
  155. var videoSender = _pc1.AddTrack(videoStreamTrack);
  156. pc1Senders.Add(videoSender);
  157. pc1Senders.Add(_pc1.AddTrack(audioStreamTrack));
  158. if (WebRTCSettings.UseVideoCodec != null)
  159. {
  160. var codecs = new[] {WebRTCSettings.UseVideoCodec};
  161. var transceiver = _pc1.GetTransceivers().First(t => t.Sender == videoSender);
  162. transceiver.SetCodecPreferences(codecs);
  163. }
  164. addTracksButton.interactable = false;
  165. removeTracksButton.interactable = true;
  166. }
  167. private void RemoveTracks()
  168. {
  169. var transceivers = _pc1.GetTransceivers();
  170. foreach (var transceiver in transceivers)
  171. {
  172. if(transceiver.Sender != null)
  173. {
  174. transceiver.Stop();
  175. _pc1.RemoveTrack(transceiver.Sender);
  176. }
  177. }
  178. pc1Senders.Clear();
  179. addTracksButton.interactable = true;
  180. removeTracksButton.interactable = false;
  181. }
  182. private void SwitchUseWebCam(bool isOn)
  183. {
  184. webCamListDropdown.interactable = isOn;
  185. }
  186. private void SwitchUseMic(bool isOn)
  187. {
  188. micListDropdown.interactable = isOn;
  189. }
  190. private void Call()
  191. {
  192. useWebCamToggle.interactable = false;
  193. webCamListDropdown.interactable = false;
  194. useMicToggle.interactable = false;
  195. micListDropdown.interactable = false;
  196. callButton.interactable = false;
  197. hangUpButton.interactable = true;
  198. addTracksButton.interactable = true;
  199. removeTracksButton.interactable = false;
  200. Debug.Log("GetSelectedSdpSemantics");
  201. var configuration = GetSelectedSdpSemantics();
  202. _pc1 = new RTCPeerConnection(ref configuration);
  203. Debug.Log("Created local peer connection object pc1");
  204. _pc1.OnIceCandidate = pc1OnIceCandidate;
  205. _pc1.OnIceConnectionChange = pc1OnIceConnectionChange;
  206. _pc1.OnNegotiationNeeded = pc1OnNegotiationNeeded;
  207. _pc2 = new RTCPeerConnection(ref configuration);
  208. Debug.Log("Created remote peer connection object pc2");
  209. _pc2.OnIceCandidate = pc2OnIceCandidate;
  210. _pc2.OnIceConnectionChange = pc2OnIceConnectionChange;
  211. _pc2.OnTrack = pc2Ontrack;
  212. CaptureAudioStart();
  213. StartCoroutine(CaptureVideoStart());
  214. }
  215. private void CaptureAudioStart()
  216. {
  217. if (!useMicToggle.isOn)
  218. {
  219. sourceAudio.clip = clip;
  220. sourceAudio.loop = true;
  221. sourceAudio.Play();
  222. audioStreamTrack = new AudioStreamTrack(sourceAudio);
  223. return;
  224. }
  225. var deviceName = Microphone.devices[micListDropdown.value];
  226. Microphone.GetDeviceCaps(deviceName, out int minFreq, out int maxFreq);
  227. var micClip = Microphone.Start(deviceName, true, 1, 48000);
  228. // set the latency to “0” samples before the audio starts to play.
  229. while (!(Microphone.GetPosition(deviceName) > 0)) {}
  230. sourceAudio.clip = micClip;
  231. sourceAudio.loop = true;
  232. sourceAudio.Play();
  233. audioStreamTrack = new AudioStreamTrack(sourceAudio);
  234. }
  235. private IEnumerator CaptureVideoStart()
  236. {
  237. if (!useWebCamToggle.isOn)
  238. {
  239. videoStreamTrack = cam.CaptureStreamTrack(WebRTCSettings.StreamSize.x, WebRTCSettings.StreamSize.y);
  240. sourceImage.texture = cam.targetTexture;
  241. yield break;
  242. }
  243. if (WebCamTexture.devices.Length == 0)
  244. {
  245. Debug.LogFormat("WebCam device not found");
  246. yield break;
  247. }
  248. yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
  249. if (!Application.HasUserAuthorization(UserAuthorization.WebCam))
  250. {
  251. Debug.LogFormat("authorization for using the device is denied");
  252. yield break;
  253. }
  254. WebCamDevice userCameraDevice = WebCamTexture.devices[webCamListDropdown.value];
  255. webCamTexture = new WebCamTexture(userCameraDevice.name, WebRTCSettings.StreamSize.x, WebRTCSettings.StreamSize.y, 30);
  256. webCamTexture.Play();
  257. yield return new WaitUntil(() => webCamTexture.didUpdateThisFrame);
  258. videoStreamTrack = new VideoStreamTrack(webCamTexture);
  259. sourceImage.texture = webCamTexture;
  260. }
  261. private void HangUp()
  262. {
  263. if (webCamTexture != null)
  264. {
  265. webCamTexture.Stop();
  266. webCamTexture = null;
  267. }
  268. receiveAudioStream?.Dispose();
  269. receiveAudioStream = null;
  270. receiveVideoStream?.Dispose();
  271. receiveVideoStream = null;
  272. videoStreamTrack?.Dispose();
  273. videoStreamTrack = null;
  274. audioStreamTrack?.Dispose();
  275. audioStreamTrack = null;
  276. Debug.Log("Close local/remote peer connection");
  277. _pc1?.Dispose();
  278. _pc2?.Dispose();
  279. _pc1 = null;
  280. _pc2 = null;
  281. sourceImage.texture = null;
  282. sourceAudio.Stop();
  283. sourceAudio.clip = null;
  284. receiveImage.texture = null;
  285. receiveAudio.Stop();
  286. receiveAudio.clip = null;
  287. useWebCamToggle.interactable = true;
  288. webCamListDropdown.interactable = useWebCamToggle.isOn;
  289. useMicToggle.interactable = true;
  290. micListDropdown.interactable = useMicToggle.isOn;
  291. callButton.interactable = true;
  292. hangUpButton.interactable = false;
  293. addTracksButton.interactable = false;
  294. removeTracksButton.interactable = false;
  295. }
  296. private void OnIceCandidate(RTCPeerConnection pc, RTCIceCandidate candidate)
  297. {
  298. GetOtherPc(pc).AddIceCandidate(candidate);
  299. Debug.Log($"{GetName(pc)} ICE candidate:\n {candidate.Candidate}");
  300. }
  301. private string GetName(RTCPeerConnection pc)
  302. {
  303. return (pc == _pc1) ? "pc1" : "pc2";
  304. }
  305. private RTCPeerConnection GetOtherPc(RTCPeerConnection pc)
  306. {
  307. return (pc == _pc1) ? _pc2 : _pc1;
  308. }
  309. private IEnumerator OnCreateOfferSuccess(RTCPeerConnection pc, RTCSessionDescription desc)
  310. {
  311. Debug.Log($"Offer from {GetName(pc)}\n{desc.sdp}");
  312. Debug.Log($"{GetName(pc)} setLocalDescription start");
  313. var op = pc.SetLocalDescription(ref desc);
  314. yield return op;
  315. if (!op.IsError)
  316. {
  317. OnSetLocalSuccess(pc);
  318. }
  319. else
  320. {
  321. var error = op.Error;
  322. OnSetSessionDescriptionError(ref error);
  323. }
  324. var otherPc = GetOtherPc(pc);
  325. Debug.Log($"{GetName(otherPc)} setRemoteDescription start");
  326. var op2 = otherPc.SetRemoteDescription(ref desc);
  327. yield return op2;
  328. if (!op2.IsError)
  329. {
  330. OnSetRemoteSuccess(otherPc);
  331. }
  332. else
  333. {
  334. var error = op2.Error;
  335. OnSetSessionDescriptionError(ref error);
  336. }
  337. Debug.Log($"{GetName(otherPc)} createAnswer start");
  338. // Since the 'remote' side has no media stream we need
  339. // to pass in the right constraints in order for it to
  340. // accept the incoming offer of audio and video.
  341. var op3 = otherPc.CreateAnswer();
  342. yield return op3;
  343. if (!op3.IsError)
  344. {
  345. yield return OnCreateAnswerSuccess(otherPc, op3.Desc);
  346. }
  347. else
  348. {
  349. OnCreateSessionDescriptionError(op3.Error);
  350. }
  351. }
  352. private void OnSetLocalSuccess(RTCPeerConnection pc)
  353. {
  354. Debug.Log($"{GetName(pc)} SetLocalDescription complete");
  355. }
  356. static void OnSetSessionDescriptionError(ref RTCError error)
  357. {
  358. Debug.LogError($"Error Detail Type: {error.message}");
  359. }
  360. private void OnSetRemoteSuccess(RTCPeerConnection pc)
  361. {
  362. Debug.Log($"{GetName(pc)} SetRemoteDescription complete");
  363. }
  364. IEnumerator OnCreateAnswerSuccess(RTCPeerConnection pc, RTCSessionDescription desc)
  365. {
  366. Debug.Log($"Answer from {GetName(pc)}:\n{desc.sdp}");
  367. Debug.Log($"{GetName(pc)} setLocalDescription start");
  368. var op = pc.SetLocalDescription(ref desc);
  369. yield return op;
  370. if (!op.IsError)
  371. {
  372. OnSetLocalSuccess(pc);
  373. }
  374. else
  375. {
  376. var error = op.Error;
  377. OnSetSessionDescriptionError(ref error);
  378. }
  379. var otherPc = GetOtherPc(pc);
  380. Debug.Log($"{GetName(otherPc)} setRemoteDescription start");
  381. var op2 = otherPc.SetRemoteDescription(ref desc);
  382. yield return op2;
  383. if (!op2.IsError)
  384. {
  385. OnSetRemoteSuccess(otherPc);
  386. }
  387. else
  388. {
  389. var error = op2.Error;
  390. OnSetSessionDescriptionError(ref error);
  391. }
  392. }
  393. private static void OnCreateSessionDescriptionError(RTCError error)
  394. {
  395. Debug.LogError($"Error Detail Type: {error.message}");
  396. }
  397. }
  398. }