-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathindex.js
383 lines (367 loc) · 9.9 KB
/
index.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// create Agora client
var client = AgoraRTC.createClient({
mode: "rtc",
codec: "vp8",
});
AgoraRTC.enableLogUpload();
var localTracks = {
videoTrack: null,
audioTrack: null,
};
var remoteUsers = {};
// Agora client options
var options = getOptionsFromLocal();
let statsInterval;
$("#join-form").submit(async function (e) {
e.preventDefault();
$("#join").attr("disabled", true);
try {
options.channel = $("#channel").val();
options.uid = Number($("#uid").val());
options.token = await agoraGetAppData(options);
await join();
setOptionsToLocal(options);
message.success("join channel success!");
} catch (error) {
console.error(error);
message.error(error.message);
} finally {
$("#leave").attr("disabled", false);
}
});
$("#leave").click(function (e) {
leave();
});
async function join() {
// add event listener to play remote tracks when remote user publishs.
client.on("user-published", handleUserPublished);
client.on("user-unpublished", handleUserUnpublished);
// start Proxy if needed
const mode = Number(options.proxyMode);
if (mode != 0 && !isNaN(mode)) {
client.startProxyServer(mode);
}
// join the channel
options.uid = await client.join(
options.appid,
options.channel,
options.token || null,
options.uid || null,
);
if (!localTracks.audioTrack) {
localTracks.audioTrack = await AgoraRTC.createMicrophoneAudioTrack({
encoderConfig: "music_standard",
});
}
if (!localTracks.videoTrack) {
localTracks.videoTrack = await AgoraRTC.createCameraVideoTrack();
}
// play local video track
localTracks.videoTrack.play("local-player");
$("#local-player-name").text(`uid: ${options.uid}`);
// publish local tracks to channel
await client.publish(Object.values(localTracks));
console.log("publish success");
initStats();
}
async function leave() {
for (trackName in localTracks) {
var track = localTracks[trackName];
if (track) {
track.stop();
track.close();
localTracks[trackName] = undefined;
}
}
destructStats();
// remove remote users and player views
remoteUsers = {};
$("#remote-playerlist").html("");
// leave the channel
await client.leave();
$("#local-player-name").text("");
$("#join").attr("disabled", false);
$("#leave").attr("disabled", true);
console.log("client leaves channel success");
}
async function subscribe(user, mediaType) {
const uid = user.uid;
// subscribe to a remote user
await client.subscribe(user, mediaType);
console.log("subscribe success");
if (mediaType === "video") {
const player = $(`
<div id="player-wrapper-${uid}">
<div class="player-with-stats">
<div id="player-${uid}" class="player">
<div class="remote-player-name">uid: ${uid}</div>
</div>
<div class="track-stats stats"></div>
</div>
</div>
`);
$("#remote-playerlist").append(player);
user.videoTrack.play(`player-${uid}`);
}
if (mediaType === "audio") {
user.audioTrack.play();
}
}
function handleUserPublished(user, mediaType) {
const id = user.uid;
remoteUsers[id] = user;
subscribe(user, mediaType);
}
function handleUserUnpublished(user, mediaType) {
if (mediaType === "video") {
const id = user.uid;
delete remoteUsers[id];
$(`#player-wrapper-${id}`).remove();
}
}
// start collect and show stats information
function initStats() {
statsInterval = setInterval(flushStats, 1000);
}
// stop collect and show stats information
function destructStats() {
clearInterval(statsInterval);
$("#session-stats").html("");
$("#transport-stats").html("");
$("#client-stats").html("");
$("#local-stats").html("");
}
// flush stats views
function flushStats() {
// get the client stats message
const clientStats = client.getRTCStats();
const clientStatsList = [
{
description: "Number of users in channel",
value: clientStats.UserCount,
unit: "",
},
{
description: "Duration in channel",
value: clientStats.Duration,
unit: "s",
},
{
description: "Bit rate receiving",
value: clientStats.RecvBitrate,
unit: "bps",
},
{
description: "Bit rate being sent",
value: clientStats.SendBitrate,
unit: "bps",
},
{
description: "Total bytes received",
value: clientStats.RecvBytes,
unit: "bytes",
},
{
description: "Total bytes sent",
value: clientStats.SendBytes,
unit: "bytes",
},
{
description: "Outgoing available bandwidth",
value: clientStats.OutgoingAvailableBandwidth.toFixed(3),
unit: "kbps",
},
{
description: "RTT from SDK to SD-RTN access node",
value: clientStats.RTT,
unit: "ms",
},
];
$("#client-stats").html(`
<div>client-stats:</div>
${clientStatsList
.map((stat) => `<div class="fs-6">${stat.description}: ${stat.value} ${stat.unit}</div>`)
.join("")}
`);
// get the local track stats message
const localStats = {
video: client.getLocalVideoStats(),
audio: client.getLocalAudioStats(),
};
const localStatsList = [
{
description: "Send audio bit rate",
value: localStats.audio.sendBitrate,
unit: "bps",
},
{
description: "Total audio bytes sent",
value: localStats.audio.sendBytes,
unit: "bytes",
},
{
description: "Total audio packets sent",
value: localStats.audio.sendPackets,
unit: "",
},
{
description: "Total audio packets loss",
value: localStats.audio.sendPacketsLost,
unit: "",
},
{
description: "Video capture resolution height",
value: localStats.video.captureResolutionHeight,
unit: "",
},
{
description: "Video capture resolution width",
value: localStats.video.captureResolutionWidth,
unit: "",
},
{
description: "Video send resolution height",
value: localStats.video.sendResolutionHeight,
unit: "",
},
{
description: "Video send resolution width",
value: localStats.video.sendResolutionWidth,
unit: "",
},
{
description: "video encode delay",
value: Number(localStats.video.encodeDelay).toFixed(2),
unit: "ms",
},
{
description: "Send video bit rate",
value: localStats.video.sendBitrate,
unit: "bps",
},
{
description: "Total video bytes sent",
value: localStats.video.sendBytes,
unit: "bytes",
},
{
description: "Total video packets sent",
value: localStats.video.sendPackets,
unit: "",
},
{
description: "Total video packets loss",
value: localStats.video.sendPacketsLost,
unit: "",
},
{
description: "Video duration",
value: localStats.video.totalDuration,
unit: "s",
},
{
description: "Total video freeze time",
value: localStats.video.totalFreezeTime,
unit: "s",
},
];
$("#local-stats").html(`
${localStatsList
.map((stat) => `<div class="fs-6">${stat.description}: ${stat.value} ${stat.unit}</div>`)
.join("")}
`);
Object.keys(remoteUsers).forEach((uid) => {
// get the remote track stats message
const remoteTracksStats = {
video: client.getRemoteVideoStats()[uid],
audio: client.getRemoteAudioStats()[uid],
};
const remoteTracksStatsList = [
{
description: "Delay of audio from sending to receiving",
value: Number(remoteTracksStats.audio?.receiveDelay).toFixed(2),
unit: "ms",
},
{
description: "Delay of video from sending to receiving",
value: Number(remoteTracksStats.video?.receiveDelay).toFixed(2),
unit: "ms",
},
{
description: "Total audio bytes received",
value: remoteTracksStats.audio.receiveBytes,
unit: "bytes",
},
{
description: "Total audio packets received",
value: remoteTracksStats.audio.receivePackets,
unit: "",
},
{
description: "Total audio packets loss",
value: remoteTracksStats.audio.receivePacketsLost,
unit: "",
},
{
description: "Total audio packets loss rate",
value: Number(remoteTracksStats.audio.packetLossRate).toFixed(3),
unit: "%",
},
{
description: "Video received resolution height",
value: remoteTracksStats.video.receiveResolutionHeight,
unit: "",
},
{
description: "Video received resolution width",
value: remoteTracksStats.video.receiveResolutionWidth,
unit: "",
},
{
description: "Receiving video bit rate",
value: remoteTracksStats.video.receiveBitrate,
unit: "bps",
},
{
description: "Total video bytes received",
value: remoteTracksStats.video.receiveBytes,
unit: "bytes",
},
{
description: "Total video packets received",
value: remoteTracksStats.video.receivePackets,
unit: "",
},
{
description: "Total video packets loss",
value: remoteTracksStats.video.receivePacketsLost,
unit: "",
},
{
description: "Total video packets loss rate",
value: Number(remoteTracksStats.video.receivePacketsLost).toFixed(3),
unit: "%",
},
{
description: "Video duration",
value: remoteTracksStats.video.totalDuration,
unit: "s",
},
{
description: "Total video freeze time",
value: remoteTracksStats.video.totalFreezeTime,
unit: "s",
},
{
description: "video freeze rate",
value: Number(remoteTracksStats.video.freezeRate).toFixed(3),
unit: "%",
},
];
$(`#player-wrapper-${uid} .track-stats`).html(`
${remoteTracksStatsList
.map((stat) => `<div class="fs-6">${stat.description}: ${stat.value} ${stat.unit}</div>`)
.join("")}
`);
});
}