MungeSDPSample.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.WebRTC;
  4. using Unity.WebRTC.Samples;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. class MungeSDPSample : MonoBehaviour
  8. {
  9. #pragma warning disable 0649
  10. [SerializeField] private Button startButton;
  11. [SerializeField] private Button callButton;
  12. [SerializeField] private Button createOfferButton;
  13. [SerializeField] private Button setOfferButton;
  14. [SerializeField] private Button createAnswerButton;
  15. [SerializeField] private Button setAnswerButton;
  16. [SerializeField] private Button hangUpButton;
  17. [SerializeField] private Camera cam;
  18. [SerializeField] private RawImage sourceImage;
  19. [SerializeField] private RawImage receiveImage;
  20. [SerializeField] private InputField offerSdpInput;
  21. [SerializeField] private InputField answerSdpInput;
  22. [SerializeField] private Transform rotateObject;
  23. #pragma warning restore 0649
  24. private RTCConfiguration configuration = new RTCConfiguration
  25. {
  26. iceServers = new[] {new RTCIceServer {urls = new[] {"stun:stun.l.google.com:19302"}}}
  27. };
  28. private RTCPeerConnection pcLocal, pcRemote;
  29. private MediaStream sourceVideoStream, receiveVideoStream;
  30. private Coroutine updateCoroutine;
  31. private void Awake()
  32. {
  33. WebRTC.Initialize(WebRTCSettings.LimitTextureSize);
  34. }
  35. private void OnDestroy()
  36. {
  37. WebRTC.Dispose();
  38. }
  39. private void Start()
  40. {
  41. startButton.onClick.AddListener(Setup);
  42. callButton.onClick.AddListener(Call);
  43. createOfferButton.onClick.AddListener(() => StartCoroutine(CreateOffer()));
  44. setOfferButton.onClick.AddListener(() => StartCoroutine(SetOffer()));
  45. createAnswerButton.onClick.AddListener(() => StartCoroutine(CreateAnswer()));
  46. setAnswerButton.onClick.AddListener(() => StartCoroutine(SetAnswer()));
  47. hangUpButton.onClick.AddListener(HangUp);
  48. startButton.interactable = true;
  49. callButton.interactable = false;
  50. hangUpButton.interactable = false;
  51. }
  52. private void Update()
  53. {
  54. if (rotateObject != null)
  55. {
  56. rotateObject.Rotate(1, 2, 3);
  57. }
  58. }
  59. private void Setup()
  60. {
  61. Debug.Log("Set up source/receive streams");
  62. sourceVideoStream = cam.CaptureStream(WebRTCSettings.StreamSize.x, WebRTCSettings.StreamSize.y);
  63. sourceImage.texture = cam.targetTexture;
  64. updateCoroutine = StartCoroutine(WebRTC.Update());
  65. receiveVideoStream = new MediaStream();
  66. receiveVideoStream.OnAddTrack = e =>
  67. {
  68. if (e.Track is VideoStreamTrack track)
  69. {
  70. track.OnVideoReceived += tex =>
  71. {
  72. receiveImage.texture = tex;
  73. };
  74. }
  75. };
  76. startButton.interactable = false;
  77. callButton.interactable = true;
  78. }
  79. private void Call()
  80. {
  81. Debug.Log("Starting calls");
  82. pcLocal = new RTCPeerConnection(ref configuration);
  83. pcRemote = new RTCPeerConnection(ref configuration);
  84. pcRemote.OnTrack = e => receiveVideoStream.AddTrack(e.Track);
  85. pcLocal.OnIceCandidate = candidate => pcRemote.AddIceCandidate(candidate);
  86. pcRemote.OnIceCandidate = candidate => pcLocal.AddIceCandidate(candidate);
  87. Debug.Log("pc1: created local and remote peer connection object");
  88. var senders = new List<RTCRtpSender>();
  89. foreach (var track in sourceVideoStream.GetTracks())
  90. {
  91. senders.Add(pcLocal.AddTrack(track, sourceVideoStream));
  92. }
  93. if (WebRTCSettings.UseVideoCodec != null)
  94. {
  95. var codecs = new[] {WebRTCSettings.UseVideoCodec};
  96. foreach (var transceiver in pcLocal.GetTransceivers())
  97. {
  98. if (senders.Contains(transceiver.Sender))
  99. {
  100. transceiver.SetCodecPreferences(codecs);
  101. }
  102. }
  103. }
  104. Debug.Log("Adding local stream to pcLocal");
  105. callButton.interactable = false;
  106. createOfferButton.interactable = true;
  107. createAnswerButton.interactable = true;
  108. setOfferButton.interactable = true;
  109. setAnswerButton.interactable = true;
  110. hangUpButton.interactable = true;
  111. }
  112. private IEnumerator CreateOffer()
  113. {
  114. var op = pcLocal.CreateOffer();
  115. yield return op;
  116. if (op.IsError)
  117. {
  118. OnCreateSessionDescriptionError(op.Error);
  119. yield break;
  120. }
  121. offerSdpInput.text = op.Desc.sdp;
  122. offerSdpInput.interactable = true;
  123. }
  124. private IEnumerator SetOffer()
  125. {
  126. var offer = new RTCSessionDescription {type = RTCSdpType.Offer, sdp = offerSdpInput.text};
  127. Debug.Log($"Modified Offer from LocalPeerConnection\n{offer.sdp}");
  128. var opLocal = pcLocal.SetLocalDescription(ref offer);
  129. yield return opLocal;
  130. if (opLocal.IsError)
  131. {
  132. OnSetSessionDescriptionError(opLocal.Error);
  133. yield break;
  134. }
  135. Debug.Log("Set Local session description success on LocalPeerConnection");
  136. var opRemote = pcRemote.SetRemoteDescription(ref offer);
  137. yield return opRemote;
  138. if (opRemote.IsError)
  139. {
  140. OnSetSessionDescriptionError(opRemote.Error);
  141. yield break;
  142. }
  143. Debug.Log("Set Remote session description success on RemotePeerConnection");
  144. }
  145. private IEnumerator CreateAnswer()
  146. {
  147. var op = pcRemote.CreateAnswer();
  148. yield return op;
  149. if (op.IsError)
  150. {
  151. OnCreateSessionDescriptionError(op.Error);
  152. yield break;
  153. }
  154. answerSdpInput.text = op.Desc.sdp;
  155. answerSdpInput.interactable = true;
  156. }
  157. private IEnumerator SetAnswer()
  158. {
  159. var answer = new RTCSessionDescription {type = RTCSdpType.Answer, sdp = answerSdpInput.text};
  160. Debug.Log($"Modified Answer from RemotePeerConnection\n{answer.sdp}");
  161. var opLocal = pcRemote.SetLocalDescription(ref answer);
  162. yield return opLocal;
  163. if (opLocal.IsError)
  164. {
  165. OnSetSessionDescriptionError(opLocal.Error);
  166. yield break;
  167. }
  168. Debug.Log("Set Local session description success on RemotePeerConnection");
  169. var opRemote = pcLocal.SetRemoteDescription(ref answer);
  170. yield return opRemote;
  171. if (opRemote.IsError)
  172. {
  173. OnSetSessionDescriptionError(opRemote.Error);
  174. yield break;
  175. }
  176. Debug.Log("Set Remote session description success on LocalPeerConnection");
  177. }
  178. private void HangUp()
  179. {
  180. StopCoroutine(updateCoroutine);
  181. updateCoroutine = null;
  182. sourceVideoStream.Dispose();
  183. sourceVideoStream = null;
  184. sourceImage.texture = null;
  185. receiveVideoStream.Dispose();
  186. receiveVideoStream = null;
  187. receiveImage.texture = null;
  188. offerSdpInput.text = string.Empty;
  189. answerSdpInput.text = string.Empty;
  190. pcLocal.Close();
  191. pcRemote.Close();
  192. pcLocal.Dispose();
  193. pcRemote.Dispose();
  194. pcLocal = null;
  195. pcRemote = null;
  196. startButton.interactable = true;
  197. callButton.interactable = false;
  198. createOfferButton.interactable = false;
  199. createAnswerButton.interactable = false;
  200. setOfferButton.interactable = false;
  201. setAnswerButton.interactable = false;
  202. hangUpButton.interactable = false;
  203. offerSdpInput.interactable = false;
  204. answerSdpInput.interactable = false;
  205. }
  206. private static void OnCreateSessionDescriptionError(RTCError error)
  207. {
  208. Debug.LogError($"Failed to create session description: {error.message}");
  209. }
  210. private static void OnSetSessionDescriptionError(RTCError error)
  211. {
  212. Debug.LogError($"Failed to set session description: {error.message}");
  213. }
  214. }