-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
125 lines (109 loc) · 3.34 KB
/
index.html
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
<html>
<head>
<style>
#video-grid {
display: grid;
grid-template-columns: repeat(auto-fill, 400px);
grid-auto-rows: 400px
}
video {
width: 100%;
height: 100%;
object-fit: cover;
}
</style>
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
<script>
var dataConnections = {};
var userMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
var params = new URLSearchParams(location.search);
var peer = new Peer(params.get('id') ?? undefined, {
host: 'peer.webrtc.expertcollege.com',
secure: true,
debug: 3,
pingInterval: 500,
config: {
iceServers: [
{ url: 'stun:stun.webrtc.expertcollege.com:5349'},
{ url: 'turn:turn.webrtc.expertcollege.com:5349',
username: 'guest',
credential: 'somepassword'
}
]}
});
peer.on('open', function () {
document.getElementById("peer_id").value=peer.id;
});
peer.on('error', function(err){
alert("An error ocurred with peer: " + err);
console.error(err);
});
userMedia({video: true, audio: true},
function (myStream) {
var myVideo = document.createElement('video');
myVideo.muted = true;
addVideoStream(myVideo, myStream);
//hook up connection event
peer.on('connection', function (connection) {
var pid = connection.peer;
console.log(pid + " has connected");
//call connected peer sending my stream
var mediaConnection = peer.call(pid, myStream)
var remoteVideo = document.createElement('video');
//show remote stream when it becomes available
mediaConnection.on('stream', function(remoteStream) {
addVideoStream(remoteVideo, remoteStream);
});
//close remote stream when it becomes unavailable
mediaConnection.on('close', function() {
console.log("in onclose");
console.log(remoteVideo);
remoteVideo.remove();
});
});
//hook up incoming call event
peer.on('call', function(mediaConnection) {
//answer by sending my stream
mediaConnection.answer(myStream);
//add caller's stream to video grid when available
var remoteVideo = document.createElement('video');
mediaConnection.on('stream', function (remoteStream) {
addVideoStream(remoteVideo, remoteStream);
});
mediaConnection.on('close', function() {
console.log("in onclose");
console.log(remoteVideo);
remoteVideo.remove();
});
});
}, function(err) {
console.error('Failed to get local stream' ,err);
});
function addVideoStream(video, stream) {
video.srcObject = stream;
video.addEventListener('loadedmetadata', () => {
video.play();
document.getElementById('video-grid').append(video);
} );
video.addEventListener('click', () => {
video.requestFullscreen();
} );
}
function doConnect() {
var remoteId= window.prompt('Enter remote peer ID');
var dataConnection = peer.connect(remoteId, { reliable: true} );
dataConnection.on('close', function() {
console.log('onclose data channel of ' + remoteId)
});
dataConnections[remoteId] = dataConnection;
}
</script>
</head>
<body>
<div>
<input type="text" id="peer_id">
<button onclick="doConnect()">Add user</button>
</div>
<div id="video-grid"></div>
</body>
</html>