WebRtcApe.js
7.8 KB
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
//*
// WebRtc
// */
import Emiter from 'Emiter';
import Loger from "Loger";
import $ from "jquery";
import GlobalConfig from 'GlobalConfig';
import ApeConsts from './ApeConsts';
import EngineUtils from 'EngineUtils';
import MessageTypes from 'MessageTypes';
var AgoraRTC = require('../AgoraRTCSDK-1.12.0');
let loger = Loger.getLoger('WebRtcApe');
class WebRtcApe extends Emiter {
constructor() {
super();
this.appId = 'eb253cc7b40c4a8b82f0a5b6f93c2ce0';
this.appCertificate = "";
this.appRecordingKey = "";
this.channelId = "";
this.uid = 0;
this.info = ""
this.mode = "interop";
this.client = null;
this.localStream = null;
this.cameras = [];
this.microphones = [];
this.curCamera = "";
this.curMicrophone = "";
this.videoResolution = "240P";
this.isOpenVideo = true;
this.localViewId = "";
this.localStyle = "";
this.remoteViewId = "";
this.remoteStyle = "";
this.invisibleViewId ="";
this.invisibleStyle ="";
this.xdyRemote = "xdy_remote";
//webRtc sdk
this.client = AgoraRTC.createClient({mode: this.mode});
this.getDevices();
}
initApp(_params) {
loger.log("AgoraRTC client init");
if (_params) {
this.appId = _params.appId;
}
if (this.client) {
this.client.init(this.appId, () => {
loger.log("AgoraRTC client init success");
}, (err)=> {
loger.error("AgoraRTC client init failed", err);
});
this.addEvent();
}
}
addEvent() {
loger.log("AgoraRTC addEvent");
if (!this.client) {
loger.log("addEvent error:client is null");
return;
}
this.channelKey = "";
this.client.on('error', (err) => {
loger.log("Got error msg:", err.reason);
if (err.reason === 'DYNAMIC_KEY_TIMEOUT') {
this.client.renewChannelKey(this.channelKey, ()=> {
loger.log("Renew channel key successfully");
}, (err)=> {
loger.log("Renew channel key failed: ", err);
});
}
});
this.client.on('stream-added', (evt)=> {
let stream = evt.stream;
loger.log("New stream added: " + stream.getId());
loger.log("Subscribe ", stream);
this.client.subscribe(stream, (err)=> {
loger.log("Subscribe stream failed", err);
});
});
this.client.on('stream-subscribed', (evt)=> {
let stream = evt.stream;
loger.log("Subscribe remote stream successfully: " + stream.getId());
if(GlobalConfig.getUserRoleFromeNodeId(stream.getId())==ApeConsts.invisible){
//显示隐藏用户
$(this.invisibleViewId).append('<div id="' + this.xdyRemote + stream.getId() + '" style="float:left; width:320px;height:240px;"></div>');
}else {
$(this.remoteViewId).append('<div id="' + this.xdyRemote + stream.getId() + '" style="float:left; width:320px;height:240px;"></div>');
}
stream.play('' + this.xdyRemote + stream.getId());
});
this.client.on('stream-removed', (evt)=> {
let stream = evt.stream;
stream.stop();
$('#' + this.xdyRemote + stream.getId()).remove();
loger.log("Remote stream is removed " + stream.getId());
});
this.client.on('peer-leave', (evt)=> {
let stream = evt.stream;
if (stream) {
stream.stop();
$('#' + this.xdyRemote + stream.getId()).remove();
loger.log(evt.uid + " leaved from this channel");
}
});
this.client.on("active-speaker", (evt)=> {
let uid = evt.uid;
loger.log("update active speaker: client" + uid);
});
}
joinChannel(_params) {
this.channelId = _params.channelId||"";
this.uid = parseInt(_params.uid)||0;
this.info =_params.info || "";
let dynamic_key = null;
loger.log("AgoraRTC joinChannel","channelId:"+this.channelId,"uid:"+this.uid);
this.client.join(dynamic_key, ""+this.channelId, this.uid, (uid)=> {
this.uid = uid;
loger.log("User " + uid + " join channel successfully");
this.openLoaclStream();
}, (err)=> {
loger.log("Join channel failed", err);
});
}
openLoaclStream() {
loger.log("摄像头", this.curCamera);
loger.log("麦克风", this.curMicrophone);
this.localStream = AgoraRTC.createStream({
streamID: this.uid,
audio: true,
microphoneId: this.curMicrophone,
cameraId: this.curCamera,
video: this.isOpenVideo,
screen: false
});
if (this.isOpenVideo) {
this.localStream.setVideoProfile(this.videoResolution);
}
}
leaveChannel() {
loger.log("AgoraRTC joinChannel");
if (!this.client) {
return;
}
this.client.leaveChannel(() => {
loger.log("Leavel channel successfully");
}, (err)=> {
loger.log("Leave channel failed");
});
}
publish() {
if (!this.client||!this.localStream) {
return;
}
this.localStream.init(()=> {
let viewName = this.localViewId.replace("#", "");
loger.log("getUserMedia successfully", this.localViewId);
this.localStream.play(viewName);
this.client.publish(this.localStream, (err)=> {
loger.log("Publish local stream error: " + err);
GlobalConfig.openCamera =0;
GlobalConfig.openMicrophones =0;
});
GlobalConfig.openCamera = EngineUtils.creatTimestamp();
GlobalConfig.openMicrophones = GlobalConfig.openCamera;
this._emit(MessageTypes.USER_DEVICE_STATUS_CHAANGE, {
nodeId: GlobalConfig.nodeId,
userRole: GlobalConfig.userRole,
userName: GlobalConfig.userName,
userId: GlobalConfig.userId,
openCamera: GlobalConfig.openCamera,
openMicrophones: GlobalConfig.openMicrophones
});
}, (err)=> {
loger.log("getUserMedia failed", err);
});
}
unpublish() {
if (!this.client||!this.localStream) {
return;
}
this.client.unpublish(this.localStream, (err)=> {
loger.log("Unpublish local stream failed" + err);
});
this.localStream.close();
$(this.localViewId).html("");
GlobalConfig.openCamera =0;
GlobalConfig.openMicrophones =0;
this._emit(MessageTypes.USER_DEVICE_STATUS_CHAANGE, {
nodeId: GlobalConfig.nodeId,
userRole: GlobalConfig.userRole,
userName: GlobalConfig.userName,
userId: GlobalConfig.userId,
openCamera: GlobalConfig.openCamera,
openMicrophones: GlobalConfig.openMicrophones
});
}
/*
* 设置本地回显视图
* */
setLoaclView(_params) {
loger.log("设置本地回显视图");
this.localViewId = _params.divId||"";;
this.localStyle = _params.styleStr||"";;
}
/*
* 设置其他人的video视图容器
* */
setRemoteView(_params) {
loger.log("设置其他人的video视图容器");
this.remoteViewId = _params.divId||"";
this.remoteStyle = _params.styleStr||"";
}
/*
* 设置隐藏用户的video视图容器
* */
setInvisibleMediaView(_params) {
loger.log("设置隐藏用户的video视图容器");
this.invisibleViewId = _params.divId||"";
this.invisibleStyle = _params.styleStr||"";
}
getDevices(_params,_callback) {
AgoraRTC.getDevices((devices)=> {
console.log("devices", devices)
for (let i = 0; i < devices.length; i++) {
let device = devices[i];
if (device.kind === 'audioinput') {
this.microphones.push(device);
} else if (device.kind === 'videoinput') {
this.cameras.push(device);
} else {
loger.warn('未知的设备: ', device);
}
}
if (this.cameras && this.cameras.length > 0) {
this.curCamera = this.cameras[0].deviceId || "";
}
if (this.microphones && this.microphones.length > 0) {
this.curMicrophone = this.microphones[0].deviceId || "";
}
if(_callback){
_callback({cameras:this.cameras,microphones:this.microphones});
}
});
}
}
export default new WebRtcApe;