-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
executable file
·323 lines (288 loc) · 11.5 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* eslint-disable require-jsdoc */
$(function() {
// Peer object
const peer = new Peer({
key: window.__SKYWAY_KEY__,
debug: 3
});
let localStream;
let existingCall;
let timer;
peer.on('open', () => {
$('#my-id').text(peer.id);
step1();
});
// Receiving a call
peer.on('call', call => {
// Answer the call automatically (instead of prompting user) for demo purposes
call.answer(localStream, {
// 発信側のコーデックを優先
//videoCodec: $('#videoCodec').val()
});
step3(call);
});
peer.on('error', err => {
alert(err.message);
// Return to step 2 if error occurs
step2();
});
$('#make-call').on('submit', e => {
e.preventDefault();
// Initiate a call!
console.log($('#callto-id').val());
const call = peer.call($('#callto-id').val(), localStream, {
videoCodec: $('#videoCodec').val()
});
step3(call);
});
$('#end-call').on('click', () => {
existingCall.close();
clearInterval(timer);
step2();
});
// Retry if getUserMedia fails
$('#step1-retry').on('click', () => {
$('#step1-error').hide();
step1();
});
// Getting Stats
$('#getting-stats').on('click', () => {
const _PC = existingCall.getPeerConnection();
timer = setInterval(()=>{
getRTCStats(_PC.getStats())
},1000);
step4();
});
// Stop acquiring stats
$('#stop-acquiring-stats').on('click', () => {
clearInterval(timer);
step5();
});
// set up audio and video input selectors
const audioSelect = $('#audioSource');
const videoSelect = $('#videoSource');
const selectors = [audioSelect, videoSelect];
navigator.mediaDevices.enumerateDevices()
.then(deviceInfos => {
const values = selectors.map(select => select.val() || '');
selectors.forEach(select => {
const children = select.children(':first');
while (children.length) {
select.remove(children);
}
});
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
const option = $('<option>').val(deviceInfo.deviceId);
if (deviceInfo.kind === 'audioinput') {
option.text(deviceInfo.label ||
'Microphone ' + (audioSelect.children().length + 1));
audioSelect.append(option);
} else if (deviceInfo.kind === 'videoinput') {
option.text(deviceInfo.label ||
'Camera ' + (videoSelect.children().length + 1));
videoSelect.append(option);
}
}
selectors.forEach((select, selectorIndex) => {
if (Array.prototype.slice.call(select.children()).some(n => {
return n.value === values[selectorIndex];
})) {
select.val(values[selectorIndex]);
}
});
videoSelect.on('change', step1);
audioSelect.on('change', step1);
});
function step1() {
// Get audio/video stream
const audioSource = $('#audioSource').val();
const videoSource = $('#videoSource').val();
const constraints = {
audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
video: {deviceId: videoSource ? {exact: videoSource} : undefined},
};
navigator.mediaDevices.getUserMedia(constraints).then(stream => {
$('#my-video').get(0).srcObject = stream;
localStream = stream;
if (existingCall) {
existingCall.replaceStream(stream);
return;
}
step2();
}).catch(err => {
$('#step1-error').show();
console.error(err);
});
}
function step2() {
$('#step1, #step3').hide();
$('#step2').show();
$('#callto-id').focus();
$('#getting-stats').show();
$('#stop-acquiring-stats').hide();
$('#local-candidate').text('');
$('#remote-candidate').text('');
$('#inbound-codec').text('');
$('#outbound-codec').text('');
$('#inbound-audio').text('');
$('#inbound-video').text('');
$('#outbound-audio').text('');
$('#outbound-video').text('');
$('#local-audio-video').text('');
$('#remote-audio-video').text('');
}
function step3(call) {
// Hang up on an existing call if present
if (existingCall) {
existingCall.close();
}
// Wait for stream on the call, then set peer video display
call.on('stream', stream => {
$('#their-video').get(0).srcObject = stream;
});
// UI stuff
existingCall = call;
$('#their-id').text(call.remoteId);
call.on('close', step2);
$('#step1, #step2').hide();
$('#step3').show();
}
function step4() {
$('#getting-stats').hide();
$('#stop-acquiring-stats').show();
}
function step5() {
$('#getting-stats').show();
$('#stop-acquiring-stats').hide();
}
async function getRTCStats(statsObject){
let trasportArray = [];
let candidateArray = [];
let candidatePairArray = [];
let inboundRTPAudioStreamArray = [];
let inboundRTPVideoStreamArray = [];
let outboundRTPAudioStreamArray = [];
let outboundRTPVideoStreamArray = [];
let codecArray = [];
let mediaStreamTrack_senderArray = [];
let mediaStreamTrack_receiverArray = [];
let mediaStreamTrack_local_audioArray = []
let mediaStreamTrack_remote_audioArray = []
let mediaStreamTrack_local_videoArray = []
let mediaStreamTrack_remote_videoArray = []
let candidatePairId = '';
let localCandidateId = '';
let remoteCandidateId = '';
let localCandidate = {};
let remoteCandidate = {};
let inboundAudioCodec = {};
let inboundVideoCodec = {};
let outboundAudioCode = {};
let outboundVideoCode = {};
let stats = await statsObject;
stats.forEach(stat => {
if(stat.id.indexOf('RTCTransport') !== -1){
trasportArray.push(stat);
}
if(stat.id.indexOf('RTCIceCandidatePair') !== -1){
candidatePairArray.push(stat);
}
if(stat.id.indexOf('RTCIceCandidate_') !== -1){
candidateArray.push(stat);
}
if(stat.id.indexOf('RTCInboundRTPAudioStream') !== -1){
inboundRTPAudioStreamArray.push(stat);
}
if(stat.id.indexOf('RTCInboundRTPVideoStream') !== -1){
inboundRTPVideoStreamArray.push(stat);
}
if(stat.id.indexOf('RTCOutboundRTPAudioStream') !== -1){
outboundRTPAudioStreamArray.push(stat);
}
if(stat.id.indexOf('RTCOutboundRTPVideoStream') !== -1){
outboundRTPVideoStreamArray.push(stat);
}
if(stat.id.indexOf('RTCMediaStreamTrack_sender') !== -1){
mediaStreamTrack_senderArray.push(stat);
}
if(stat.id.indexOf('RTCMediaStreamTrack_receiver') !== -1){
mediaStreamTrack_receiverArray.push(stat);
}
if(stat.id.indexOf('RTCCodec') !== -1){
codecArray.push(stat);
}
});
trasportArray.forEach(transport => {
if(transport.dtlsState === 'connected'){
candidatePairId = transport.selectedCandidatePairId;
}
});
candidatePairArray.forEach(candidatePair => {
if(candidatePair.state === 'succeeded' && candidatePair.id === candidatePairId){
localCandidateId = candidatePair.localCandidateId;
remoteCandidateId = candidatePair.remoteCandidateId;
}
});
candidateArray.forEach(candidate => {
if(candidate.id === localCandidateId){
localCandidate = candidate;
}
if(candidate.id === remoteCandidateId){
remoteCandidate = candidate;
}
});
inboundRTPAudioStreamArray.forEach(inboundRTPAudioStream => {
codecArray.forEach(codec => {
if(inboundRTPAudioStream.codecId === codec.id){
inboundAudioCodec = codec;
}
});
});
inboundRTPVideoStreamArray.forEach(inboundRTPVideoStream => {
codecArray.forEach(codec => {
if(inboundRTPVideoStream.codecId === codec.id){
inboundVideoCodec = codec;
}
});
});
outboundRTPAudioStreamArray.forEach(outboundRTPAudioStream => {
codecArray.forEach(codec => {
if(outboundRTPAudioStream.codecId === codec.id){
outboundAudioCodec = codec;
}
});
});
outboundRTPVideoStreamArray.forEach(outboundRTPVideo => {
codecArray.forEach(codec => {
if(outboundRTPVideo.codecId === codec.id){
outboundVideoCodec = codec;
}
});
});
mediaStreamTrack_senderArray.forEach(mediaStreamTrack => {
if(mediaStreamTrack.kind === 'audio'){
mediaStreamTrack_local_audioArray.push(mediaStreamTrack)
}else if(mediaStreamTrack.kind === 'video'){
mediaStreamTrack_local_videoArray.push(mediaStreamTrack)
}
});
mediaStreamTrack_receiverArray.forEach(mediaStreamTrack => {
if(mediaStreamTrack.kind === 'audio'){
mediaStreamTrack_remote_audioArray.push(mediaStreamTrack)
}else if(mediaStreamTrack.kind === 'video'){
mediaStreamTrack_remote_videoArray.push(mediaStreamTrack)
}
});
$('#local-candidate').html(localCandidate.ip + ':' + localCandidate.port + '(' +localCandidate.protocol + ')' + '<BR>type:' + localCandidate.candidateType);
$('#remote-candidate').html(remoteCandidate.ip + ':' + remoteCandidate.port + '(' +remoteCandidate.protocol + ')' + '<BR>type:' + remoteCandidate.candidateType);
$('#inbound-codec').html(inboundVideoCodec.mimeType + '<BR>' + inboundAudioCodec.mimeType);
$('#outbound-codec').html(outboundVideoCodec.mimeType + '<BR>' + outboundAudioCodec.mimeType)
$('#inbound-audio').html('bytesReceived:' + inboundRTPAudioStreamArray[0].bytesReceived + '<BR>jitter:' + inboundRTPAudioStreamArray[0].jitter + '<BR>fractionLost:' + inboundRTPAudioStreamArray[0].fractionLost);
$('#inbound-video').html('bytesReceived:' + inboundRTPVideoStreamArray[0].bytesReceived + '<BR>fractionLost:' + inboundRTPVideoStreamArray[0].fractionLost);
$('#outbound-audio').html('bytesSent:' + outboundRTPAudioStreamArray[0].bytesSent);
$('#outbound-video').html('bytesSent:' + outboundRTPVideoStreamArray[0].bytesSent);
$('#local-audio-video').html('audioLevel:' + mediaStreamTrack_local_audioArray[0].audioLevel + '<BR>frameHeight:' + mediaStreamTrack_local_videoArray[0].frameHeight + '<BR>frameWidth:' + mediaStreamTrack_local_videoArray[0].frameWidth + '<BR>framesSent:' + mediaStreamTrack_local_videoArray[0].framesSent);
$('#remote-audio-video').html('audioLevel:' + mediaStreamTrack_remote_audioArray[0].audioLevel + '<BR>frameHeight:' + mediaStreamTrack_local_videoArray[0].frameHeight + '<BR>frameWidth:' + mediaStreamTrack_remote_videoArray[0].frameWidth);
}
});