-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathscript.js
181 lines (151 loc) · 3.87 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
/* global Peer */
console.log('getting code');
/**
* VARIABLES
*/
/**
* Instantiate a new Peer, passing to it an alphanumeric string as an ID and options obj
* @type {Peer}
*/
const peer = new Peer(''+Math.floor(Math.random()*2**18).toString(36).padStart(4,0), {
host: location.hostname,
debug: 1,
path: '/myapp'
});
window.peer = peer;
const callBtn = document.querySelector('.call-btn');
const audioContainer = document.querySelector('.call-container');
const hangUpBtn = document.querySelector('.hangup-btn');
let conn;
let code;
/**
* FUNCTIONS
*/
/**
* Gets connection code/peer id from caller
* @returns {string} - the code retrieved
*/
function getStreamCode() {
code = window.prompt('Please enter the sharing code');
return code;
}
/**
* Gets the local audio stream of the current caller
* @param callbacks - an object to set the success/error behaviour
* @returns {void}
*/
function getLocalStream() {
const constraints = {video: false, audio: true}
navigator.mediaDevices.getUserMedia(constraints).then( stream => {
console.log(stream);
setLocalStream(stream);
}).catch( err => {
console.log("u got an error:" + err)
});
}
/**
* Sets the src of the HTML element on the page to the local stream
* @param stream
* @returns {void}
*/
function setLocalStream(stream) {
window.localAudio.srcObject = stream;
window.localAudio.autoplay = true;
window.localStream = stream;
}
/**
* Sets the src of the HTML element on the page to the remote stream
* @param stream
* @returns {void}
*/
function setRemoteStream(stream) {
window.remoteAudio.srcObject = stream;
window.remoteAudio.autoplay = true;
window.peerStream = stream;
}
/**
* Displays the audio controls and correct copy
* @returns{void}
*/
function showConnectedContent() {
window.caststatus.textContent = `You're connected`;
callBtn.hidden = true;
audioContainer.hidden = false;
}
/**
* Displays the call button and peer ID
* @returns{void}
*/
function showCallContent() {
window.caststatus.textContent = `Your device ID is: ${peer.id}`;
callBtn.hidden = false;
audioContainer.hidden = true;
}
/**
* Connect the peers
* @returns {void}
*/
function connectPeers() {
conn = peer.connect(code)
}
/**
* EVENTS
*/
/**
* Get the connection code, connect peers and create a call
*/
callBtn.addEventListener('click', function(){
getStreamCode();
connectPeers();
const call = peer.call(code, window.localStream);
/**
* when the call is streaming, set the remote stream for the caller
*/
call.on('stream', function(stream) {
showConnectedContent();
console.log(peer);
setRemoteStream(stream);
});
})
/**
* Close the connection between peers
*/
hangUpBtn.addEventListener('click', function (){
conn.close();
showCallContent();
})
/**
* When the peer has connected to the server, diplay the peer ID
*/
peer.on('open', function () {
console.log('ready to receive cast')
window.caststatus.textContent = `Your device ID is: ${peer.id}`;
});
/**
* When a data connection between peers is open, get the connecting peer's details
*/
peer.on('connection', function(connection){
conn = connection;
peer_id = connection.peer;
});
/**
* When a call has been created, answer it and set the remote stream for the person being called
*/
peer.on('call', function(call) {
const answerCall = confirm("Do you want to answer?")
if(answerCall){
call.answer(window.localStream)
showConnectedContent();
call.on('stream', function(stream) {
console.log('Stream Received');
setRemoteStream(stream);
});
conn.on('close', function (){
showCallContent();
})
} else {
console.log("call denied");
}
});
peer.on('error', err => console.error(err));
getLocalStream();