Advertisement
Chatenium

Untitled

Dec 26th, 2023 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.69 KB | None | 0 0
  1. // CALL //
  2. this.callData.peerconnection.ondatachannel = e => {
  3. const receiveChannel = e.channel;
  4. receiveChannel.onopen = e => {
  5. this.incomingCall = {
  6. chatid: "",
  7. callerDisplayname: "",
  8. callerId: "",
  9. callerPfp: "",
  10. }
  11. listenp2p(receiveChannel);
  12. }
  13. receiveChannel.onerror = e => console.error(e)
  14. this.callData.datachannel = receiveChannel;
  15. }
  16.  
  17. this.callData.peerconnection.oniceconnectionstatechange = e => {
  18. console.log('ICE Connection State:', this.callData.peerconnection.iceConnectionState);
  19. };
  20.  
  21. this.callData.peerconnection.onicecandidate = e => {
  22. console.log("ice")
  23. console.log(e)
  24. this.checkloginservice.socket$.next({
  25. Action: "icecandidate",
  26. Value: JSON.stringify({
  27. for: this.callData.peerdata.userid,
  28. action: "icecandidate",
  29. icecandidate: JSON.stringify(e.candidate)
  30. })
  31. })
  32. }
  33.  
  34. this.callData.peerconnection.ontrack = e => {
  35. console.log(e)
  36. const [remoteStream] = e.streams;
  37. this.callData.peerdata.stream = remoteStream;
  38. this.callData.settings.audio = true;
  39. this.callData.peerdata.audio = true;
  40. this.callData.peerdata.connected = true
  41. }
  42.  
  43. // After both sides opened data channel, then its set to calldata, onMessage has its seperate joint function
  44.  
  45. this.checkloginservice.socket$.subscribe(
  46. async (message) => {
  47. let action = (message as websocketDef).action
  48. switch (action) {
  49. case "offer":
  50. let data = message as incomingCallData
  51.  
  52. this.incomingCall = {
  53. callerDisplayname: data.displayName,
  54. callerId: data.userid,
  55. callerPfp: data.pfp,
  56. chatid: data.chatid,
  57. }
  58.  
  59. setTimeout(() => {
  60. this.document.querySelector(".ans")?.addEventListener("click", async () => {
  61. this.callData.peerdata.userid = data.userid
  62. this.callData.peerdata.username = data.username;
  63. this.callData.peerdata.displayName = data.displayName;
  64. this.callData.peerdata.pfp = data.pfp;
  65. this.callData.chatid = data.chatid;
  66. await this.callData.peerconnection.setRemoteDescription(new RTCSessionDescription(JSON.parse(data.offer)))
  67. await addTracks();
  68. const answer = await this.callData.peerconnection.createAnswer();
  69. await this.callData.peerconnection.setLocalDescription(answer);
  70. this.checkloginservice.socket$.next({
  71. Action: "answer",
  72. Value: JSON.stringify({
  73. for: data.userid,
  74. answer: JSON.stringify(answer),
  75. action: "answer"
  76. })
  77. })
  78. })
  79. }, 10);
  80. break;
  81. case "answer":
  82. let answerData = message as answerCallData
  83. const remoteDesc = new RTCSessionDescription(JSON.parse(answerData.answer));
  84. await this.callData.peerconnection.setRemoteDescription(remoteDesc);
  85. break;
  86. case "icecandidate":
  87. let icecandidateData = message as iceCandData
  88. await this.callData.peerconnection.addIceCandidate(JSON.parse(icecandidateData.icecandidate))
  89. break;
  90. case "offerTrack":
  91. let offerTrackData = message as incomingCallData
  92. await this.callData.peerconnection.setRemoteDescription(JSON.parse(offerTrackData.offer));
  93. const answer = await this.callData.peerconnection.createAnswer();
  94. await this.callData.peerconnection.setLocalDescription(answer);
  95. this.checkloginservice.socket$.next({
  96. Action: "answerTrack",
  97. Value: JSON.stringify({
  98. for: this.callData.peerdata.userid,
  99. answer: JSON.stringify(answer),
  100. action: "answerTrack"
  101. })
  102. })
  103. break;
  104. case "answerTrack":
  105. let answerTrackData = message as answerCallData
  106. const remoteDescTrack = new RTCSessionDescription(JSON.parse(answerTrackData.answer));
  107. await this.callData.peerconnection.setRemoteDescription(remoteDescTrack);
  108. break;
  109. }
  110. }
  111. )
  112.  
  113. this.msgService.createOffer$.subscribe(async (calldata) => {
  114. if (calldata.friendid != '') {
  115. let dataChannel = this.callData.peerconnection.createDataChannel("DMCall");
  116. dataChannel.onopen = e => {
  117. listenp2p(dataChannel);
  118. }
  119. dataChannel.onerror = e => console.error(e)
  120. this.callData.datachannel = dataChannel;
  121. this.callData.peerdata.userid = calldata.friendid
  122. this.callData.peerdata.username = calldata.friendname;
  123. this.callData.peerdata.displayName = calldata.friendDisplayName;
  124. this.callData.peerdata.pfp = calldata.friendpfp;
  125. this.callData.chatid = calldata.chatid;
  126. await addTracks();
  127. const offer = await this.callData.peerconnection.createOffer();
  128. await this.callData.peerconnection.setLocalDescription(offer)
  129. this.checkloginservice.socket$.next({
  130. Action: 'offer',
  131. Value: JSON.stringify({
  132. for: calldata.friendid,
  133. offer: JSON.stringify(offer),
  134. username: userData.username,
  135. userid: userData.userid,
  136. displayName: userData.displayName,
  137. pfp: userData.pfp,
  138. chatid: calldata.chatid,
  139. action: "offer"
  140. })
  141. })
  142. }
  143. })
  144.  
  145. const addTracks = async () => {
  146. this.callData.dummytracks = [];
  147. const localstream = await this.getUserMedia(this.callData.settings.video);
  148.  
  149. const createDummyTrack = (id: string, kind: string) => {
  150. const audioContext = new AudioContext();
  151. const dummyTrack = kind === "audio" ? audioContext.createMediaStreamDestination().stream.getAudioTracks()[0] : document.createElement('canvas').captureStream().getVideoTracks()[0];
  152.  
  153. this.callData.dummytracks.push({
  154. id: id,
  155. track: dummyTrack,
  156. });
  157.  
  158. return dummyTrack;
  159. };
  160.  
  161. const dummyVideoTrack = createDummyTrack("video", "video");
  162. const dummyScreenShareTrack = createDummyTrack("screenshare", "video");
  163.  
  164. this.callData.peerconnection.addTrack(dummyVideoTrack);
  165. this.callData.peerconnection.addTrack(dummyScreenShareTrack);
  166.  
  167. localstream.getTracks().forEach((track) => {
  168. this.callData.peerconnection.addTrack(track, localstream);
  169. });
  170. };
  171.  
  172. const listenp2p = async (dataChannel: RTCDataChannel) => {
  173. dataChannel.onmessage = e => {
  174. this.ngZone.run(() => { // Run the code within Angular's zone
  175. switch (e.data) {
  176. case "toggleMute":
  177. this.callData.peerdata.audio = !this.callData.peerdata.audio;
  178. break;
  179. }
  180. });
  181. }
  182. }
  183.  
  184. async toggleVideo() {
  185. this.callData.settings.video = !this.callData.settings.video;
  186.  
  187. let dummyVideoTrack = this.callData.dummytracks.find((dummyTrack) => dummyTrack.id == "video");
  188. if (dummyVideoTrack) {
  189. let VideoTrack = this.callData.peerconnection.getSenders().find((sender) => sender.track == dummyVideoTrack?.track);
  190.  
  191. if (VideoTrack) {
  192. const realVideoStream = await this.getUserMedia(true);
  193. const realVideoTrack = realVideoStream.getVideoTracks()[0];
  194.  
  195. VideoTrack.replaceTrack(realVideoTrack);
  196. dummyVideoTrack.track = realVideoTrack;
  197.  
  198. console.log(this.callData.peerconnection.getSenders())
  199.  
  200. const offer = await this.callData.peerconnection.createOffer();
  201. await this.callData.peerconnection.setLocalDescription(offer);
  202.  
  203. this.checkloginservice.socket$.next({
  204. Action: 'offerTrack',
  205. Value: JSON.stringify({
  206. for: this.callData.peerdata.userid,
  207. offer: JSON.stringify(offer),
  208. action: "offerTrack"
  209. })
  210. })
  211. }
  212. }
  213. }
  214.  
  215. incomingCall = {
  216. chatid: "",
  217. callerId: "",
  218. callerDisplayname: "",
  219. callerPfp: "",
  220. }
  221.  
  222. callData: callDataType = <callDataType>{
  223. peerconnection: new RTCPeerConnection({
  224. iceServers: [
  225. {
  226. urls: 'stun:stun.l.google.com:19302',
  227. },
  228. ],
  229. }),
  230. peerdata: {
  231. audio: false,
  232. video: false,
  233. connected: false,
  234. },
  235. settings: {
  236. audio: false,
  237. video: false,
  238. },
  239. }
  240.  
  241.  
  242. interface incomingCallData {
  243. displayName: string;
  244. username: string;
  245. pfp: string;
  246. offer: string;
  247. userid: string;
  248. chatid: string;
  249. }
  250.  
  251. interface answerCallData {
  252. answer: string;
  253. for: string;
  254. action: string;
  255. }
  256.  
  257. interface iceCandData {
  258. for: string;
  259. action: string;
  260. icecandidate: string;
  261. }
  262.  
  263. interface callDataType {
  264. chatid: string;
  265. settings: callSettingsType;
  266. peerdata: callPeerdataType;
  267. datachannel: RTCDataChannel;
  268. callPanelMinimized: boolean;
  269. peerconnection: RTCPeerConnection;
  270. dummytracks: dummyTrack[]
  271. }
  272.  
  273. interface dummyTrack {
  274. id: string,
  275. track: MediaStreamTrack,
  276. }
  277.  
  278. interface callSettingsType {
  279. audio: boolean;
  280. video: boolean;
  281. }
  282.  
  283. interface callPeerdataType {
  284. connected: boolean;
  285. stream: MediaStream;
  286. call: string;
  287. userid: string;
  288. username: string;
  289. displayName: string;
  290. pfp: string;
  291. audio: boolean;
  292. video: boolean;
  293. }
  294.  
  295.  
  296.  
  297. ////////////////////////////////////
  298. /////////////BACKEND////////////////
  299. ////////////////////////////////////
  300.  
  301. func HandleWebSocket(c *gin.Context) {
  302. conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
  303. if err != nil {
  304. return
  305. }
  306.  
  307. client := &Client{
  308. Connection: conn,
  309. }
  310.  
  311. for {
  312. _, p, err := conn.ReadMessage()
  313.  
  314. if err != nil {
  315. Rm.removeUser(client.Userid)
  316. return
  317. }
  318.  
  319. var message config.Websocket_DefaultMessage
  320. if err := json.Unmarshal(p, &message); err != nil {
  321. return
  322. }
  323.  
  324. switch message.Action {
  325. case "joinRealtime":
  326.  
  327. Rm.connectUser(conn, message.Value)
  328. client.Userid = message.Value
  329. Rm.Broadcast("Connected to WebSocket /user", message.Value)
  330.  
  331. case "offer":
  332. type offerData struct {
  333. For string `json:"for"`
  334. Offer string `json:"offer"`
  335. Username string `json:"username"`
  336. Userid string `json:"userid"`
  337. DisplayName string `json:"displayName"`
  338. Pfp string `json:"pfp"`
  339. Chatid string `json:"chatid"`
  340. Action string `json:"action"`
  341. }
  342.  
  343. var offer offerData
  344.  
  345. if err := json.Unmarshal([]byte(message.Value), &offer); err != nil {
  346. return
  347. }
  348. Rm.Broadcast(offer, offer.For)
  349.  
  350. case "answer":
  351. type answerData struct {
  352. For string `json:"for"`
  353. Answer string `json:"answer"`
  354. Action string `json:"action"`
  355. }
  356.  
  357. var answer answerData
  358.  
  359. if err := json.Unmarshal([]byte(message.Value), &answer); err != nil {
  360. return
  361. }
  362.  
  363. Rm.Broadcast(answer, answer.For)
  364.  
  365. case "icecandidate":
  366. type iceCandData struct {
  367. For string `json:"for"`
  368. Action string `json:"action"`
  369. Icecandidate string `json:"icecandidate"`
  370. }
  371.  
  372. var iceCand iceCandData
  373.  
  374. if err := json.Unmarshal([]byte(message.Value), &iceCand); err != nil {
  375. return
  376. }
  377. Rm.Broadcast(iceCand, iceCand.For)
  378. case "offerTrack":
  379. type iceCandData struct {
  380. For string `json:"for"`
  381. Action string `json:"action"`
  382. Offer string `json:"offer"`
  383. }
  384.  
  385. var iceCand iceCandData
  386.  
  387. if err := json.Unmarshal([]byte(message.Value), &iceCand); err != nil {
  388. return
  389. }
  390. Rm.Broadcast(iceCand, iceCand.For)
  391. case "answerTrack":
  392. type answerData struct {
  393. For string `json:"for"`
  394. Answer string `json:"answer"`
  395. Action string `json:"action"`
  396. }
  397.  
  398. var answer answerData
  399.  
  400. if err := json.Unmarshal([]byte(message.Value), &answer); err != nil {
  401. return
  402. }
  403.  
  404. Rm.Broadcast(answer, answer.For)
  405.  
  406. }
  407. }
  408. }
  409.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement