Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CALL //
- this.callData.peerconnection.ondatachannel = e => {
- const receiveChannel = e.channel;
- receiveChannel.onopen = e => {
- this.incomingCall = {
- chatid: "",
- callerDisplayname: "",
- callerId: "",
- callerPfp: "",
- }
- listenp2p(receiveChannel);
- }
- receiveChannel.onerror = e => console.error(e)
- this.callData.datachannel = receiveChannel;
- }
- this.callData.peerconnection.oniceconnectionstatechange = e => {
- console.log('ICE Connection State:', this.callData.peerconnection.iceConnectionState);
- };
- this.callData.peerconnection.onicecandidate = e => {
- console.log("ice")
- console.log(e)
- this.checkloginservice.socket$.next({
- Action: "icecandidate",
- Value: JSON.stringify({
- for: this.callData.peerdata.userid,
- action: "icecandidate",
- icecandidate: JSON.stringify(e.candidate)
- })
- })
- }
- this.callData.peerconnection.ontrack = e => {
- console.log(e)
- const [remoteStream] = e.streams;
- this.callData.peerdata.stream = remoteStream;
- this.callData.settings.audio = true;
- this.callData.peerdata.audio = true;
- this.callData.peerdata.connected = true
- }
- // After both sides opened data channel, then its set to calldata, onMessage has its seperate joint function
- this.checkloginservice.socket$.subscribe(
- async (message) => {
- let action = (message as websocketDef).action
- switch (action) {
- case "offer":
- let data = message as incomingCallData
- this.incomingCall = {
- callerDisplayname: data.displayName,
- callerId: data.userid,
- callerPfp: data.pfp,
- chatid: data.chatid,
- }
- setTimeout(() => {
- this.document.querySelector(".ans")?.addEventListener("click", async () => {
- this.callData.peerdata.userid = data.userid
- this.callData.peerdata.username = data.username;
- this.callData.peerdata.displayName = data.displayName;
- this.callData.peerdata.pfp = data.pfp;
- this.callData.chatid = data.chatid;
- await this.callData.peerconnection.setRemoteDescription(new RTCSessionDescription(JSON.parse(data.offer)))
- await addTracks();
- const answer = await this.callData.peerconnection.createAnswer();
- await this.callData.peerconnection.setLocalDescription(answer);
- this.checkloginservice.socket$.next({
- Action: "answer",
- Value: JSON.stringify({
- for: data.userid,
- answer: JSON.stringify(answer),
- action: "answer"
- })
- })
- })
- }, 10);
- break;
- case "answer":
- let answerData = message as answerCallData
- const remoteDesc = new RTCSessionDescription(JSON.parse(answerData.answer));
- await this.callData.peerconnection.setRemoteDescription(remoteDesc);
- break;
- case "icecandidate":
- let icecandidateData = message as iceCandData
- await this.callData.peerconnection.addIceCandidate(JSON.parse(icecandidateData.icecandidate))
- break;
- case "offerTrack":
- let offerTrackData = message as incomingCallData
- await this.callData.peerconnection.setRemoteDescription(JSON.parse(offerTrackData.offer));
- const answer = await this.callData.peerconnection.createAnswer();
- await this.callData.peerconnection.setLocalDescription(answer);
- this.checkloginservice.socket$.next({
- Action: "answerTrack",
- Value: JSON.stringify({
- for: this.callData.peerdata.userid,
- answer: JSON.stringify(answer),
- action: "answerTrack"
- })
- })
- break;
- case "answerTrack":
- let answerTrackData = message as answerCallData
- const remoteDescTrack = new RTCSessionDescription(JSON.parse(answerTrackData.answer));
- await this.callData.peerconnection.setRemoteDescription(remoteDescTrack);
- break;
- }
- }
- )
- this.msgService.createOffer$.subscribe(async (calldata) => {
- if (calldata.friendid != '') {
- let dataChannel = this.callData.peerconnection.createDataChannel("DMCall");
- dataChannel.onopen = e => {
- listenp2p(dataChannel);
- }
- dataChannel.onerror = e => console.error(e)
- this.callData.datachannel = dataChannel;
- this.callData.peerdata.userid = calldata.friendid
- this.callData.peerdata.username = calldata.friendname;
- this.callData.peerdata.displayName = calldata.friendDisplayName;
- this.callData.peerdata.pfp = calldata.friendpfp;
- this.callData.chatid = calldata.chatid;
- await addTracks();
- const offer = await this.callData.peerconnection.createOffer();
- await this.callData.peerconnection.setLocalDescription(offer)
- this.checkloginservice.socket$.next({
- Action: 'offer',
- Value: JSON.stringify({
- for: calldata.friendid,
- offer: JSON.stringify(offer),
- username: userData.username,
- userid: userData.userid,
- displayName: userData.displayName,
- pfp: userData.pfp,
- chatid: calldata.chatid,
- action: "offer"
- })
- })
- }
- })
- const addTracks = async () => {
- this.callData.dummytracks = [];
- const localstream = await this.getUserMedia(this.callData.settings.video);
- const createDummyTrack = (id: string, kind: string) => {
- const audioContext = new AudioContext();
- const dummyTrack = kind === "audio" ? audioContext.createMediaStreamDestination().stream.getAudioTracks()[0] : document.createElement('canvas').captureStream().getVideoTracks()[0];
- this.callData.dummytracks.push({
- id: id,
- track: dummyTrack,
- });
- return dummyTrack;
- };
- const dummyVideoTrack = createDummyTrack("video", "video");
- const dummyScreenShareTrack = createDummyTrack("screenshare", "video");
- this.callData.peerconnection.addTrack(dummyVideoTrack);
- this.callData.peerconnection.addTrack(dummyScreenShareTrack);
- localstream.getTracks().forEach((track) => {
- this.callData.peerconnection.addTrack(track, localstream);
- });
- };
- const listenp2p = async (dataChannel: RTCDataChannel) => {
- dataChannel.onmessage = e => {
- this.ngZone.run(() => { // Run the code within Angular's zone
- switch (e.data) {
- case "toggleMute":
- this.callData.peerdata.audio = !this.callData.peerdata.audio;
- break;
- }
- });
- }
- }
- async toggleVideo() {
- this.callData.settings.video = !this.callData.settings.video;
- let dummyVideoTrack = this.callData.dummytracks.find((dummyTrack) => dummyTrack.id == "video");
- if (dummyVideoTrack) {
- let VideoTrack = this.callData.peerconnection.getSenders().find((sender) => sender.track == dummyVideoTrack?.track);
- if (VideoTrack) {
- const realVideoStream = await this.getUserMedia(true);
- const realVideoTrack = realVideoStream.getVideoTracks()[0];
- VideoTrack.replaceTrack(realVideoTrack);
- dummyVideoTrack.track = realVideoTrack;
- console.log(this.callData.peerconnection.getSenders())
- const offer = await this.callData.peerconnection.createOffer();
- await this.callData.peerconnection.setLocalDescription(offer);
- this.checkloginservice.socket$.next({
- Action: 'offerTrack',
- Value: JSON.stringify({
- for: this.callData.peerdata.userid,
- offer: JSON.stringify(offer),
- action: "offerTrack"
- })
- })
- }
- }
- }
- incomingCall = {
- chatid: "",
- callerId: "",
- callerDisplayname: "",
- callerPfp: "",
- }
- callData: callDataType = <callDataType>{
- peerconnection: new RTCPeerConnection({
- iceServers: [
- {
- urls: 'stun:stun.l.google.com:19302',
- },
- ],
- }),
- peerdata: {
- audio: false,
- video: false,
- connected: false,
- },
- settings: {
- audio: false,
- video: false,
- },
- }
- interface incomingCallData {
- displayName: string;
- username: string;
- pfp: string;
- offer: string;
- userid: string;
- chatid: string;
- }
- interface answerCallData {
- answer: string;
- for: string;
- action: string;
- }
- interface iceCandData {
- for: string;
- action: string;
- icecandidate: string;
- }
- interface callDataType {
- chatid: string;
- settings: callSettingsType;
- peerdata: callPeerdataType;
- datachannel: RTCDataChannel;
- callPanelMinimized: boolean;
- peerconnection: RTCPeerConnection;
- dummytracks: dummyTrack[]
- }
- interface dummyTrack {
- id: string,
- track: MediaStreamTrack,
- }
- interface callSettingsType {
- audio: boolean;
- video: boolean;
- }
- interface callPeerdataType {
- connected: boolean;
- stream: MediaStream;
- call: string;
- userid: string;
- username: string;
- displayName: string;
- pfp: string;
- audio: boolean;
- video: boolean;
- }
- ////////////////////////////////////
- /////////////BACKEND////////////////
- ////////////////////////////////////
- func HandleWebSocket(c *gin.Context) {
- conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
- if err != nil {
- return
- }
- client := &Client{
- Connection: conn,
- }
- for {
- _, p, err := conn.ReadMessage()
- if err != nil {
- Rm.removeUser(client.Userid)
- return
- }
- var message config.Websocket_DefaultMessage
- if err := json.Unmarshal(p, &message); err != nil {
- return
- }
- switch message.Action {
- case "joinRealtime":
- Rm.connectUser(conn, message.Value)
- client.Userid = message.Value
- Rm.Broadcast("Connected to WebSocket /user", message.Value)
- case "offer":
- type offerData struct {
- For string `json:"for"`
- Offer string `json:"offer"`
- Username string `json:"username"`
- Userid string `json:"userid"`
- DisplayName string `json:"displayName"`
- Pfp string `json:"pfp"`
- Chatid string `json:"chatid"`
- Action string `json:"action"`
- }
- var offer offerData
- if err := json.Unmarshal([]byte(message.Value), &offer); err != nil {
- return
- }
- Rm.Broadcast(offer, offer.For)
- case "answer":
- type answerData struct {
- For string `json:"for"`
- Answer string `json:"answer"`
- Action string `json:"action"`
- }
- var answer answerData
- if err := json.Unmarshal([]byte(message.Value), &answer); err != nil {
- return
- }
- Rm.Broadcast(answer, answer.For)
- case "icecandidate":
- type iceCandData struct {
- For string `json:"for"`
- Action string `json:"action"`
- Icecandidate string `json:"icecandidate"`
- }
- var iceCand iceCandData
- if err := json.Unmarshal([]byte(message.Value), &iceCand); err != nil {
- return
- }
- Rm.Broadcast(iceCand, iceCand.For)
- case "offerTrack":
- type iceCandData struct {
- For string `json:"for"`
- Action string `json:"action"`
- Offer string `json:"offer"`
- }
- var iceCand iceCandData
- if err := json.Unmarshal([]byte(message.Value), &iceCand); err != nil {
- return
- }
- Rm.Broadcast(iceCand, iceCand.For)
- case "answerTrack":
- type answerData struct {
- For string `json:"for"`
- Answer string `json:"answer"`
- Action string `json:"action"`
- }
- var answer answerData
- if err := json.Unmarshal([]byte(message.Value), &answer); err != nil {
- return
- }
- Rm.Broadcast(answer, answer.For)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement