-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscript.js
218 lines (192 loc) · 6.58 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var room_id;
var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var local_stream;
var screenStream;
var peer = null;
var currentPeer = null
var screenSharing = false
function createRoom() {
console.log("Creating Room")
let room = document.getElementById("room-input").value;
if (room == " " || room == "") {
alert("Please enter room number")
return;
}
room_id = room;
peer = new Peer(room_id)
peer.on('open', (id) => {
console.log("Peer Room ID: ", id)
getUserMedia({ video: true, audio: true }, (stream) => {
console.log(stream);
local_stream = stream;
setLocalStream(local_stream)
}, (err) => {
console.log(err)
})
notify("Waiting for peer to join.")
})
peer.on('call', (call) => {
call.answer(local_stream);
call.on('stream', (stream) => {
console.log("got call");
console.log(stream);
setRemoteStream(stream)
})
currentPeer = call;
})
}
function setLocalStream(stream) {
document.getElementById("local-vid-container").hidden = false;
let video = document.getElementById("local-video");
video.srcObject = stream;
video.muted = true;
video.play();
}
function setScreenSharingStream(stream) {
document.getElementById("screenshare-container").hidden = false;
let video = document.getElementById("screenshared-video");
video.srcObject = stream;
video.muted = true;
video.play();
}
function setRemoteStream(stream) {
document.getElementById("remote-vid-container").hidden = false;
let video = document.getElementById("remote-video");
video.srcObject = stream;
video.play();
}
function notify(msg) {
let notification = document.getElementById("notification")
notification.innerHTML = msg
notification.hidden = false
setTimeout(() => {
notification.hidden = true;
}, 3000)
}
function joinRoom() {
console.log("Joining Room")
let room = document.getElementById("room-input").value;
if (room == " " || room == "") {
alert("Please enter room number")
return;
}
room_id = room;
peer = new Peer()
peer.on('open', (id) => {
console.log("Connected room with Id: " + id)
getUserMedia({ video: true, audio: false }, (stream) => {
local_stream = stream;
setLocalStream(local_stream)
notify("Joining peer")
let call = peer.call(room_id, stream)
call.on('stream', (stream) => {
setRemoteStream(stream);
})
currentPeer = call;
}, (err) => {
console.log(err)
})
})
}
function joinRoomWithoutCamShareScreen() {
// join a call and drirectly share screen, without accesing camera
console.log("Joining Room")
let room = document.getElementById("room-input").value;
if (room == " " || room == "") {
alert("Please enter room number")
return;
}
room_id = room;
peer = new Peer()
peer.on('open', (id) => {
console.log("Connected with Id: " + id)
const createMediaStreamFake = () => {
return new MediaStream([createEmptyAudioTrack(), createEmptyVideoTrack({ width: 640, height: 480 })]);
}
const createEmptyAudioTrack = () => {
const ctx = new AudioContext();
const oscillator = ctx.createOscillator();
const dst = oscillator.connect(ctx.createMediaStreamDestination());
oscillator.start();
const track = dst.stream.getAudioTracks()[0];
return Object.assign(track, { enabled: false });
}
const createEmptyVideoTrack = ({ width, height }) => {
const canvas = Object.assign(document.createElement('canvas'), { width, height });
const ctx = canvas.getContext('2d');
ctx.fillStyle = "green";
ctx.fillRect(0, 0, width, height);
const stream = canvas.captureStream();
const track = stream.getVideoTracks()[0];
return Object.assign(track, { enabled: false });
};
notify("Joining peer")
let call = peer.call(room_id, createMediaStreamFake())
call.on('stream', (stream) => {
setRemoteStream(stream);
})
currentPeer = call;
startScreenShare();
})
}
function joinRoomShareVideoAsStream() {
// Play video from local media
console.log("Joining Room")
let room = document.getElementById("room-input").value;
if (room == " " || room == "") {
alert("Please enter room number")
return;
}
room_id = room;
peer = new Peer()
peer.on('open', (id) => {
console.log("Connected with Id: " + id)
document.getElementById("local-mdeia-container").hidden = false;
const video = document.getElementById('local-media');
video.onplay = function () {
const stream = video.captureStream();
notify("Joining peer")
let call = peer.call(room_id, stream)
// Show remote stream on my side
call.on('stream', (stream) => {
setRemoteStream(stream);
})
};
video.play();
})
}
function startScreenShare() {
if (screenSharing) {
stopScreenSharing()
}
navigator.mediaDevices.getDisplayMedia({ video: true }).then((stream) => {
setScreenSharingStream(stream);
screenStream = stream;
let videoTrack = screenStream.getVideoTracks()[0];
videoTrack.onended = () => {
stopScreenSharing()
}
if (peer) {
let sender = currentPeer.peerConnection.getSenders().find(function (s) {
return s.track.kind == videoTrack.kind;
})
sender.replaceTrack(videoTrack)
screenSharing = true
}
console.log(screenStream)
})
}
function stopScreenSharing() {
if (!screenSharing) return;
let videoTrack = local_stream.getVideoTracks()[0];
if (peer) {
let sender = currentPeer.peerConnection.getSenders().find(function (s) {
return s.track.kind == videoTrack.kind;
})
sender.replaceTrack(videoTrack)
}
screenStream.getTracks().forEach(function (track) {
track.stop();
});
screenSharing = false
}