-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
151 lines (129 loc) · 5.05 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
let localStream;
let remoteStream;
let localPeerConnection;
let remotePeerConnection;
let dataChannel;
const startButton = document.getElementById('startButton');
const callButton = document.getElementById('callButton');
const hangupButton = document.getElementById('hangupButton');
const muteButton = document.getElementById('muteButton');
const videoButton = document.getElementById('videoButton');
const shareButton = document.getElementById('shareButton');
const sendButton = document.getElementById('sendButton');
const chatInput = document.getElementById('chatInput');
const chatBox = document.getElementById('chatBox');
const localVideo = document.getElementById('localVideo');
const remoteVideo = document.getElementById('remoteVideo');
startButton.onclick = start;
callButton.onclick = call;
hangupButton.onclick = hangup;
muteButton.onclick = toggleMute;
videoButton.onclick = toggleVideo;
shareButton.onclick = shareScreen;
sendButton.onclick = sendMessage;
async function start() {
try {
await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
} catch (error) {
alert('Please grant permission to access camera and microphone.');
console.error('Permission denied for accessing media devices.', error);
return;
}
try {
localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
localVideo.srcObject = localStream;
callButton.disabled = false;
muteButton.disabled = false;
videoButton.disabled = false;
shareButton.disabled = false;
} catch (error) {
console.error('Error accessing media devices.', error);
}
}
async function call() {
callButton.disabled = true;
hangupButton.disabled = false;
const configuration = {
iceServers: [
{
urls: 'stun:stun.l.google.com:19302'
}
]
};
localPeerConnection = new RTCPeerConnection(configuration);
remotePeerConnection = new RTCPeerConnection(configuration);
dataChannel = localPeerConnection.createDataChannel('chat');
dataChannel.onmessage = (event) => {
const message = document.createElement('div');
message.textContent = event.data;
chatBox.appendChild(message);
};
remotePeerConnection.ondatachannel = (event) => {
event.channel.onmessage = (event) => {
const message = document.createElement('div');
message.textContent = event.data;
chatBox.appendChild(message);
};
};
localPeerConnection.addEventListener('icecandidate', event => onIceCandidate(event, remotePeerConnection));
remotePeerConnection.addEventListener('icecandidate', event => onIceCandidate(event, localPeerConnection));
remotePeerConnection.addEventListener('track', event => {
remoteStream = event.streams[0];
remoteVideo.srcObject = remoteStream;
});
localStream.getTracks().forEach(track => localPeerConnection.addTrack(track, localStream));
try {
const offer = await localPeerConnection.createOffer();
await localPeerConnection.setLocalDescription(offer);
await remotePeerConnection.setRemoteDescription(offer);
const answer = await remotePeerConnection.createAnswer();
await remotePeerConnection.setLocalDescription(answer);
await localPeerConnection.setRemoteDescription(answer);
} catch (error) {
console.error('Error creating offer/answer.', error);
}
}
function onIceCandidate(event, peerConnection) {
if (event.candidate) {
peerConnection.addIceCandidate(new RTCIceCandidate(event.candidate))
.catch(error => console.error('Error adding ICE candidate.', error));
}
}
function hangup() {
localPeerConnection.close();
remotePeerConnection.close();
localPeerConnection = null;
remotePeerConnection = null;
hangupButton.disabled = true;
callButton.disabled = false;
}
function toggleMute() {
const audioTrack = localStream.getAudioTracks()[0];
audioTrack.enabled = !audioTrack.enabled;
muteButton.textContent = audioTrack.enabled ? 'Mute' : 'Unmute';
}
function toggleVideo() {
const videoTrack = localStream.getVideoTracks()[0];
videoTrack.enabled = !videoTrack.enabled;
videoButton.textContent = videoTrack.enabled ? 'Disable Video' : 'Enable Video';
}
async function shareScreen() {
try {
const screenStream = await navigator.mediaDevices.getDisplayMedia({ video: true });
const screenTrack = screenStream.getTracks()[0];
localPeerConnection.getSenders().find(sender => sender.track.kind === 'video').replaceTrack(screenTrack);
screenTrack.onended = () => {
localPeerConnection.getSenders().find(sender => sender.track.kind === 'video').replaceTrack(localStream.getTracks().find(track => track.kind === 'video'));
};
} catch (error) {
console.error('Error sharing screen.', error);
}
}
function sendMessage() {
const message = chatInput.value;
dataChannel.send(message);
const messageElement = document.createElement('div');
messageElement.textContent = `You: ${message}`;
chatBox.appendChild(messageElement);
chatInput.value = '';
}