-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
290 lines (241 loc) · 12.3 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
import './index.css';
const Client = require('./src/Client');
console.log('------------------------------');
var AppController = function () {
document.getElementById('roomId').value = '2001';
document.getElementById('userId').value = Math.ceil(Math.random()*100000).toString();
this.server = document.getElementById('server').value;
this.roomId = document.getElementById('roomId').value.toString();
this.userId = document.getElementById('userId').value.toString();
this.mediaElement = document.getElementById('video_container_publish');
var logTextArea = document.getElementById('logTextArea');
logTextArea.value = '';
console.log("server:", this.server, "roomId:", this.roomId, "userId:", this.userId);
this.openCamera = document.getElementById("opencamera");
this.joinButton = document.getElementById("join");
this.publishButton = document.getElementById("publish");
this.unpublishButtton = document.getElementById("unpublish");
this.openCamera.onclick = this.OpenClicked.bind(this);
this.joinButton.onclick = this.JoinClicked.bind(this);
this.publishButton.onclick = this.PublishClicked.bind(this);
this.unpublishButtton.onclick = this.UnPublishClicked.bind(this);
this._client = new Client();
this._cameraReady = false;
};
AppController.prototype.OpenClicked = async function () {
if (this._cameraReady) {
console.log("the camera is already open");
return;
}
var cameraMediaStream = await this._client.OpenCamera();
this.mediaElement.srcObject = cameraMediaStream;
this.mediaElement.addEventListener("canplay", () => {
if (this.mediaElement) {
console.log("start play the local camera view.");
this.mediaElement.play();
this._cameraReady = true;
writeLogText("camera is ready");
}
});
}
AppController.prototype.JoinClicked = async function () {
this.server = document.getElementById('server').value;
this.roomId = document.getElementById('roomId').value.toString();
this.userId = document.getElementById('userId').value.toString();
var usersInRoom = null;//{"users":[{"uid":"11111"}, {"uid":"22222"}]}
try
{
console.log("call join api userid:", this.userId);
usersInRoom = await this._client.Join({serverHost: this.server,
roomId: this.roomId, userId: this.userId});
}
catch (error)
{
console.log("join error:", error);
return;
}
writeLogText("join ok, users in room: " + JSON.stringify(usersInRoom));
this._client.on('disconected', async(data) => {
writeLogText('websocket is disconnected');
});
this._client.on('userin', async(data) => {
writeLogText('notify userin, data:' + JSON.stringify(data));
});
this._client.on('userout', async(data) => {
var remoteUid = data.uid;
writeLogText('notify userout, data:' + JSON.stringify(data));
removeRemoteUserView(remoteUid);
var publishers = this._client.GetRemoteUserPublishers(remoteUid);
if (publishers != null) {
console.log("start unsubscirbing remote uid:", remoteUid, ", publishers:", publishers);
writeLogText('start unsubscribe remote uid:' + remoteUid + ", publishers:" + JSON.stringify(publishers))
this._client.UnSubscribe(remoteUid, publishers);
}
});
this._client.on('publish', async (data) => {
try {
var remoteUid = data['uid'];
var remotePcId = data['pcid'];
var userType = data['user_type'];
console.log(' receive publish message user type:', userType);
writeLogText('notify publish, data:' + JSON.stringify(data));
var newMediaStream = await this._client.Subscribe(remoteUid, userType, remotePcId, data['publishers']);
var userContainer = document.createElement("div");
userContainer.id = 'userContainer_' + remoteUid;
var userLabel = document.createElement("label");
userLabel.id = 'userLabel_' + remoteUid;
userLabel.innerHTML = 'remote user: ' + remoteUid;
userContainer.appendChild(userLabel);
var mediaContainer = document.createElement("div");
mediaContainer.id = 'mediaContainer_' + remoteUid;
mediaContainer.className = 'videoContainer';
userContainer.appendChild(mediaContainer);
var videoElement = document.createElement("video");
videoElement.id = 'videoElement_' + remoteUid;
videoElement.className = 'videoView';
videoElement.setAttribute("playsinline", "playsinline");
videoElement.setAttribute("autoplay", "autoplay");
videoElement.setAttribute("loop", "loop");
videoElement.setAttribute("controls", "controls");
videoElement.srcObject = newMediaStream;
videoElement.style.width = 320;
videoElement.style.height = 180;
mediaContainer.appendChild(videoElement);
var statsContainer = document.createElement("div");
statsContainer.id = 'statsContainer_' + remoteUid;
statsContainer.className = 'StatsContainer';
mediaContainer.appendChild(statsContainer);
var videoWidthElement = document.createElement("label");
var videoHeightElement = document.createElement("label");
var videoBpsElement = document.createElement("label");
var videoFpsElement = document.createElement("label");
var audioBpsElement = document.createElement("label");
var audioFpsElement = document.createElement("label");
var rttElement = document.createElement("label");
videoWidthElement.id = 'videoWidth_' + remoteUid;
videoHeightElement.id = 'videoHeight_' + remoteUid;
videoHeightElement.style.paddingLeft = '10px';
videoBpsElement.id = 'videoBps_' + remoteUid;
videoFpsElement.id = 'videoFps_' + remoteUid;
videoFpsElement.style.paddingLeft = '10px';
audioBpsElement.id = 'audioBps_' + remoteUid;
audioFpsElement.id = 'audioFps_' + remoteUid;
audioFpsElement.style.paddingLeft = '10px';
rttElement.id = 'rtt_' + remoteUid;
rttElement.style.paddingLeft = '10px';
var statsVideo1Container = document.createElement("div");
statsVideo1Container.id = 'statsVideo1Container_' + remoteUid;
statsVideo1Container.className = 'StatsItemContainer';
statsContainer.appendChild(statsVideo1Container);
statsVideo1Container.appendChild(videoWidthElement);
statsVideo1Container.appendChild(videoHeightElement);
var statsVideo2Container = document.createElement("div");
statsVideo2Container.id = 'statsVideo2Container_' + remoteUid;
statsVideo2Container.className = 'StatsItemContainer';
statsContainer.appendChild(statsVideo2Container);
statsVideo2Container.appendChild(videoBpsElement);
statsVideo2Container.appendChild(videoFpsElement);
var statsAudioContainer = document.createElement("div");
statsAudioContainer.id = 'statsAudioContainer_' + remoteUid;
statsAudioContainer.className = 'StatsItemContainer';
statsContainer.appendChild(statsAudioContainer);
statsAudioContainer.appendChild(audioBpsElement);
statsAudioContainer.appendChild(audioFpsElement);
statsAudioContainer.appendChild(rttElement);
var remoteContainerElement = document.getElementById('remoteContainer');
remoteContainerElement.appendChild(userContainer);
console.log("start play remote uid:", remoteUid);
await videoElement.play();
} catch (error) {
console.log("subscribe error:", error);
return;
}
});
this._client.on('unpublish', async(data) => {
var remoteUid = data.uid;
var publishersInfo = data.publishers;
writeLogText('notify unpublish, data:' + JSON.stringify(data));
try {
this._client.UnSubscribe(remoteUid, publishersInfo);
} catch (error) {
console.log("UnSubscribe error:", error);
throw error;
}
removeRemoteUserView(remoteUid);
});
this._client.on('stats', (data) => {
document.getElementById("videoWidth").value = data.video.width.toString();
document.getElementById("videoHeight").value = data.video.height.toString();
document.getElementById("videoFps").value = parseInt(data.video.fps).toString();
document.getElementById("videoBps").value = parseInt(data.video.bps).toString();
document.getElementById("audioFps").value = parseInt(data.audio.fps).toString();
document.getElementById("audioBps").value = parseInt(data.audio.bps).toString();
document.getElementById("Rtt").value = data.rtt.toString();
});
this._client.on('remoteStats', (data) => {
let remoteUid = data.uid;
let videoWidthId = 'videoWidth_' + remoteUid;
let videoHeightId = 'videoHeight_' + remoteUid;
let videoBpsId = 'videoBps_' + remoteUid;
let videoFpsId = 'videoFps_' + remoteUid;
let audioBpsId = 'audioBps_' + remoteUid;
let audioFpsId = 'audioFps_' + remoteUid;
let rttElementId = 'rtt_' + remoteUid;
document.getElementById(videoWidthId).innerText = 'video width: ' + data.video.width.toString();
document.getElementById(videoHeightId).innerText = 'video height: ' + data.video.height.toString();
document.getElementById(videoBpsId).innerText = 'video bps(kbps): ' + data.video.bps.toString();
document.getElementById(videoFpsId).innerText = 'video fps: ' + data.video.fps.toString();
document.getElementById(audioBpsId).innerText = 'audio bps(kbps): ' + data.audio.bps.toString();
document.getElementById(audioFpsId).innerText = 'audio fps: ' + data.audio.fps.toString();
document.getElementById(rttElementId).innerText = 'rtt(ms): ' + data.rtt.toString();
})
}
AppController.prototype.PublishClicked = async function () {
if (!this._cameraReady) {
console.log("the camera is not open");
alert('the camera is not open');
return;
}
await this._client.PublishCamera({videoEnable: true, audioEnable: true});
writeLogText("publish roomId:" + this.roomId + ", uid:" + this.userId + ", video enable" + ", audio enable");
}
AppController.prototype.UnPublishClicked = async function () {
await this._client.UnPublishCamera({videoDisable: true, audioDisable: true});
writeLogText("publish roomId:" + this.roomId + ", uid:" + this.userId + ", video disable" + ", audio disable");
}
function writeLogText(logInfo) {
var nowData = new Date();
var logTextArea = document.getElementById('logTextArea');
logTextArea.value += '[';
logTextArea.value += nowData.toLocaleString();
logTextArea.value += '] ';
logTextArea.value += logInfo;
logTextArea.value += '\r\n';
logTextArea.scrollTop = logTextArea.scrollHeight;
}
function removeRemoteUserView(remoteUid) {
var userContainerId = 'userContainer_' + remoteUid;
var userContainer = document.getElementById(userContainerId);
var userLabelId = 'userLabel_' + remoteUid;
var userLabel = document.getElementById(userLabelId);
var mediaContainerId = 'mediaContainer_' + remoteUid;
var mediaContainer = document.getElementById(mediaContainerId);
var videoElementId = 'videoElement_' + remoteUid;
var videoElement = document.getElementById(videoElementId);
var remoteContainerElement = document.getElementById('remoteContainer');
if (mediaContainer) {
mediaContainer.removeChild(videoElement);
}
if (userContainer && mediaContainer) {
userContainer.removeChild(mediaContainer);
userContainer.removeChild(userLabel);
}
if (remoteContainerElement && userContainer) {
remoteContainerElement.removeChild(userContainer);
}
videoElement = null;
mediaContainer = null;
userLabel = null;
userContainer = null;
}
var appConntrol = new AppController();