李勇

webRtc增加设备切换

@@ -59,7 +59,7 @@ export default class MessageEntrance extends Emiter { @@ -59,7 +59,7 @@ export default class MessageEntrance extends Emiter {
59 constructor() { 59 constructor() {
60 super(); 60 super();
61 //sdk 信息 61 //sdk 信息
62 - GlobalConfig.sdkVersion = "v2.1.22.20170904"; 62 + GlobalConfig.sdkVersion = "v2.2.7.20170904";
63 loger.warn("sdkVersion:" + GlobalConfig.sdkVersion); 63 loger.warn("sdkVersion:" + GlobalConfig.sdkVersion);
64 64
65 //设置 65 //设置
@@ -131,8 +131,10 @@ export default class MessageEntrance extends Emiter { @@ -131,8 +131,10 @@ export default class MessageEntrance extends Emiter {
131 _confer_ape.on(MessageTypes.CLASS_RUN_EXIT, this._runClassExit.bind(this)); //监听自己的关闭事件 131 _confer_ape.on(MessageTypes.CLASS_RUN_EXIT, this._runClassExit.bind(this)); //监听自己的关闭事件
132 _confer_ape.on(MessageTypes.CLASS_STATUS_INFO_CHANGE, this._onClassStatusInfoChange.bind(this)); //当前课堂状态信息发生改变 132 _confer_ape.on(MessageTypes.CLASS_STATUS_INFO_CHANGE, this._onClassStatusInfoChange.bind(this)); //当前课堂状态信息发生改变
133 _confer_ape.on(MessageTypes.CLASS_DELETE_ROSTER, this._onClassDeleteRoster.bind(this)); //当前课堂人员离开 133 _confer_ape.on(MessageTypes.CLASS_DELETE_ROSTER, this._onClassDeleteRoster.bind(this)); //当前课堂人员离开
  134 + _confer_ape.on(MessageTypes.CLASS_UPDATE_ROSTER, this._onClassUpdateRoster.bind(this)); //当前课堂人员更新信息
134 _confer_ape.on(MessageTypes.CLASS_NONENTITY_ROSTER, this._onClassNonentityRoster.bind(this)); //当前课堂中视频或音频占用channel的nodeId ,在人员列表中不存在 135 _confer_ape.on(MessageTypes.CLASS_NONENTITY_ROSTER, this._onClassNonentityRoster.bind(this)); //当前课堂中视频或音频占用channel的nodeId ,在人员列表中不存在
135 136
  137 +
136 _confer_ape.on(MessageTypes.CLASS_RECORD_START, this._onClassRecordStart.bind(this)); //课堂开始录制 138 _confer_ape.on(MessageTypes.CLASS_RECORD_START, this._onClassRecordStart.bind(this)); //课堂开始录制
137 _confer_ape.on(MessageTypes.CLASS_RECORD_SUCCESS, this._onClassRecordSuccess.bind(this)); //课堂开启录制成功 139 _confer_ape.on(MessageTypes.CLASS_RECORD_SUCCESS, this._onClassRecordSuccess.bind(this)); //课堂开启录制成功
138 140
@@ -277,6 +279,7 @@ export default class MessageEntrance extends Emiter { @@ -277,6 +279,7 @@ export default class MessageEntrance extends Emiter {
277 //webrtc 279 //webrtc
278 this.publishMedia=this._publishMedia.bind(this); 280 this.publishMedia=this._publishMedia.bind(this);
279 this.unpublishMedia=this._unpublishMedia.bind(this); 281 this.unpublishMedia=this._unpublishMedia.bind(this);
  282 + this.changeDevices=this._changeDevices.bind(this);
280 this.setLocalMediaView=this._setLocalMediaView.bind(this); 283 this.setLocalMediaView=this._setLocalMediaView.bind(this);
281 this.setRemoteMediaView=this._setRemoteMediaView.bind(this); 284 this.setRemoteMediaView=this._setRemoteMediaView.bind(this);
282 this.setInvisibleMediaView=this._setInvisibleMediaView.bind(this); 285 this.setInvisibleMediaView=this._setInvisibleMediaView.bind(this);
@@ -469,10 +472,25 @@ export default class MessageEntrance extends Emiter { @@ -469,10 +472,25 @@ export default class MessageEntrance extends Emiter {
469 } 472 }
470 } 473 }
471 474
472 - //人员离开 475 + //人员离开
473 _onClassDeleteRoster(_data) { 476 _onClassDeleteRoster(_data) {
474 477
475 } 478 }
  479 + //人员更新
  480 + _onClassUpdateRoster(_data){
  481 + if(!_data){
  482 + return;
  483 + }
  484 + if(GlobalConfig.deviceType==1||GlobalConfig.deviceType==2||_data.nodeId==GlobalConfig.nodeId){
  485 + return;
  486 + }
  487 + if(_webRtc){
  488 + let user=GlobalConfig.rosters[_data.nodeId];
  489 + if(user&&user.openCamera==0){
  490 + _webRtc.closeRemoteVideoView(_data);
  491 + }
  492 + }
  493 + }
476 494
477 //当前课堂中视频或音频占用channel的nodeId ,在人员列表中不存在,这种情况是占用channel的人员掉线或离开的时候没有释放channel 495 //当前课堂中视频或音频占用channel的nodeId ,在人员列表中不存在,这种情况是占用channel的人员掉线或离开的时候没有释放channel
478 //的占用状态导致,对于这种情况,需要释放掉 496 //的占用状态导致,对于这种情况,需要释放掉
@@ -1544,6 +1562,13 @@ export default class MessageEntrance extends Emiter { @@ -1544,6 +1562,13 @@ export default class MessageEntrance extends Emiter {
1544 return {"code": ApeConsts.RETURN_FAILED, "data": ""}; 1562 return {"code": ApeConsts.RETURN_FAILED, "data": ""};
1545 } 1563 }
1546 1564
  1565 + //离开视频通话频道
  1566 + if(GlobalConfig.deviceType==0||GlobalConfig.deviceType==3){
  1567 + if(_webRtc){
  1568 + _webRtc.leaveChannel();;
  1569 + }
  1570 + }
  1571 +
1547 //停止推流 1572 //停止推流
1548 if (_video_ape) { 1573 if (_video_ape) {
1549 _video_ape.stopPublishVideo(); 1574 _video_ape.stopPublishVideo();
@@ -1704,10 +1729,16 @@ export default class MessageEntrance extends Emiter { @@ -1704,10 +1729,16 @@ export default class MessageEntrance extends Emiter {
1704 loger.warn("开启录制回放流程失败->还未创建模块"); 1729 loger.warn("开启录制回放流程失败->还未创建模块");
1705 } 1730 }
1706 } else { 1731 } else {
  1732 +
  1733 + //初始化音视频通话sdk
  1734 + this._initWebRtcSdk(null,()=>{
  1735 + //音视频通话SDK初始化完成之后,根据用户的userIp获取信息,获取服务列表选点,选点测速完成后才加入MCU
  1736 + this.loadServerJsonAndgetUserIpInfo();
  1737 + });
  1738 + /*
1707 //根据用户的userIp获取信息,获取服务列表选点,选点测速完成后才加入MCU 1739 //根据用户的userIp获取信息,获取服务列表选点,选点测速完成后才加入MCU
1708 this.loadServerJsonAndgetUserIpInfo(); 1740 this.loadServerJsonAndgetUserIpInfo();
1709 - //初始化音视频通话sdk  
1710 - this._initWebRtcSdk(); 1741 + */
1711 } 1742 }
1712 } 1743 }
1713 1744
@@ -2581,13 +2612,26 @@ export default class MessageEntrance extends Emiter { @@ -2581,13 +2612,26 @@ export default class MessageEntrance extends Emiter {
2581 /* 2612 /*
2582 * 初始化webRtc 2613 * 初始化webRtc
2583 * */ 2614 * */
2584 - _initWebRtcSdk(_params){ 2615 + _initWebRtcSdk(_params,_callback){
2585 if(GlobalConfig.deviceType==1||GlobalConfig.deviceType==2){ 2616 if(GlobalConfig.deviceType==1||GlobalConfig.deviceType==2){
2586 loger.warn("移动端不需要处理初始化webRtc"); 2617 loger.warn("移动端不需要处理初始化webRtc");
  2618 + if(_callback){
  2619 + _callback();
  2620 + }
2587 return ; 2621 return ;
2588 } 2622 }
2589 if(_webRtc){ 2623 if(_webRtc){
2590 - _webRtc.initApp(_params) 2624 + _webRtc.initApp(_params,(_callbackData)=>{
  2625 + //_callback({isSuccess:false,error:err});
  2626 + if(_callbackData&&_callbackData.isSuccess==true){
  2627 + this._emit(MessageTypes.WEB_RTC_INIT_SUCCESS,_callbackData);
  2628 + }else {
  2629 + this._emit(MessageTypes.WEB_RTC_INIT_FAILED,_callbackData);
  2630 + }
  2631 + if(_callback){
  2632 + _callback();
  2633 + }
  2634 + });
2591 } 2635 }
2592 } 2636 }
2593 /* 2637 /*
@@ -2661,18 +2705,26 @@ export default class MessageEntrance extends Emiter { @@ -2661,18 +2705,26 @@ export default class MessageEntrance extends Emiter {
2661 _webRtc.unpublish(_params); 2705 _webRtc.unpublish(_params);
2662 } 2706 }
2663 } 2707 }
  2708 +
2664 /* 2709 /*
2665 - * 获取设备信息 2710 + * 切换摄像头和麦克风设备
2666 * */ 2711 * */
2667 - _getDevices(_params, _callback){ 2712 + _changeDevices(_params){
  2713 + if(GlobalConfig.deviceType==1||GlobalConfig.deviceType==2){
  2714 + return;
  2715 + }
2668 if(_webRtc){ 2716 if(_webRtc){
2669 - _webRtc.getDevices(_params,_callback); 2717 + _webRtc.changeDevices(_params);
2670 } 2718 }
2671 } 2719 }
  2720 +
2672 /* 2721 /*
2673 * 设置本地video视图 2722 * 设置本地video视图
2674 * */ 2723 * */
2675 _setLocalMediaView(_params){ 2724 _setLocalMediaView(_params){
  2725 + if(GlobalConfig.deviceType==1||GlobalConfig.deviceType==2){
  2726 + return;
  2727 + }
2676 if(_webRtc){ 2728 if(_webRtc){
2677 _webRtc.setLoaclView(_params); 2729 _webRtc.setLoaclView(_params);
2678 } 2730 }
@@ -2681,6 +2733,9 @@ export default class MessageEntrance extends Emiter { @@ -2681,6 +2733,9 @@ export default class MessageEntrance extends Emiter {
2681 * 设置房间内其他人的视图 2733 * 设置房间内其他人的视图
2682 * */ 2734 * */
2683 _setRemoteMediaView(_params){ 2735 _setRemoteMediaView(_params){
  2736 + if(GlobalConfig.deviceType==1||GlobalConfig.deviceType==2){
  2737 + return;
  2738 + }
2684 if(_webRtc){ 2739 if(_webRtc){
2685 _webRtc.setRemoteView(_params); 2740 _webRtc.setRemoteView(_params);
2686 } 2741 }
@@ -2689,6 +2744,9 @@ export default class MessageEntrance extends Emiter { @@ -2689,6 +2744,9 @@ export default class MessageEntrance extends Emiter {
2689 * 设置监课和需要隐藏显示的用户视图 2744 * 设置监课和需要隐藏显示的用户视图
2690 * */ 2745 * */
2691 _setInvisibleMediaView(_params){ 2746 _setInvisibleMediaView(_params){
  2747 + if(GlobalConfig.deviceType==1||GlobalConfig.deviceType==2){
  2748 + return;
  2749 + }
2692 if(_webRtc){ 2750 if(_webRtc){
2693 _webRtc.setInvisibleMediaView(_params); 2751 _webRtc.setInvisibleMediaView(_params);
2694 } 2752 }
@@ -103,6 +103,15 @@ MessageTypes.UPDATE_QUESTION_TIME = "update_question_time"; //更新答题时间 @@ -103,6 +103,15 @@ MessageTypes.UPDATE_QUESTION_TIME = "update_question_time"; //更新答题时间
103 //白板笔记事件定义 103 //白板笔记事件定义
104 MessageTypes.WHITEBOARD_ANNOTATION_UPDATE = "whiteboard_annotation_update"; // 'whiteboard.annotation.update'; 104 MessageTypes.WHITEBOARD_ANNOTATION_UPDATE = "whiteboard_annotation_update"; // 'whiteboard.annotation.update';
105 105
  106 +//webRtc
  107 +MessageTypes.WEB_RTC_INIT_SUCCESS = "web_rtc_init_success";
  108 +MessageTypes.WEB_RTC_INIT_FAILED = "web_rtc_init_failed";
  109 +MessageTypes.WEB_RTC_JOIN_SUCCESS = "web_rtc_join_success";
  110 +MessageTypes.WEB_RTC_JOIN_FAILED = "web_rtc_join_failed";
  111 +MessageTypes.WEB_RTC_PUBLISH_FAILED = "web_rtc_publish_failed";
  112 +MessageTypes.GET_DEVICES_SUCCESS = "get_devices_success";
  113 +
  114 +
106 //MCU MS 115 //MCU MS
107 MessageTypes.SWITCH_MCU_IP = "switch_mcu_ip"; //切换mcu 重新选点 116 MessageTypes.SWITCH_MCU_IP = "switch_mcu_ip"; //切换mcu 重新选点
108 MessageTypes.SWITCH_MS_IP = "switch_ms_ip"; //切换ms 重新选点 117 MessageTypes.SWITCH_MS_IP = "switch_ms_ip"; //切换ms 重新选点
@@ -28,11 +28,14 @@ class WebRtcApe extends Emiter { @@ -28,11 +28,14 @@ class WebRtcApe extends Emiter {
28 this.localStream = null; 28 this.localStream = null;
29 this.cameras = []; 29 this.cameras = [];
30 this.microphones = []; 30 this.microphones = [];
31 - this.curCamera = "";  
32 - this.curMicrophone = ""; 31 + this.curCameraId = "";
  32 + this.curMicrophoneId = "";
  33 +
33 this.videoResolution = "240P"; 34 this.videoResolution = "240P";
34 this.isOpenVideo = true; 35 this.isOpenVideo = true;
35 36
  37 + this.isPublish=false;//当前是否正在推流
  38 +
36 this.localViewId = ""; 39 this.localViewId = "";
37 this.localStyle = ""; 40 this.localStyle = "";
38 41
@@ -45,10 +48,20 @@ class WebRtcApe extends Emiter { @@ -45,10 +48,20 @@ class WebRtcApe extends Emiter {
45 this.xdyRemote = "xdy_remote"; 48 this.xdyRemote = "xdy_remote";
46 //webRtc sdk 49 //webRtc sdk
47 this.client = AgoraRTC.createClient({mode: this.mode}); 50 this.client = AgoraRTC.createClient({mode: this.mode});
48 - this.getDevices(); 51 +
  52 + this.getDevices(null,(devices)=>{
  53 + if (this.cameras && this.cameras.length > 0) {
  54 + this.curCameraId = this.cameras[0].deviceId || "";
  55 + GlobalConfig.curCamera =this.cameras[0].label || "";
  56 + }
  57 + if (this.microphones && this.microphones.length > 0) {
  58 + this.curMicrophoneId = this.microphones[0].deviceId || "";
  59 + GlobalConfig.curMicrophone = this.microphones[0].label || "";
  60 + }
  61 + });
49 } 62 }
50 63
51 - initApp(_params) { 64 + initApp(_params,_callback) {
52 loger.log("初始化WebRtc"); 65 loger.log("初始化WebRtc");
53 if (_params) { 66 if (_params) {
54 this.appId = _params.appId; 67 this.appId = _params.appId;
@@ -56,9 +69,14 @@ class WebRtcApe extends Emiter { @@ -56,9 +69,14 @@ class WebRtcApe extends Emiter {
56 if (this.client) { 69 if (this.client) {
57 this.client.init(this.appId, () => { 70 this.client.init(this.appId, () => {
58 loger.log("初始化WebRtc->成功"); 71 loger.log("初始化WebRtc->成功");
  72 + if(_callback){
  73 + _callback({isSuccess:true});
  74 + }
59 }, (err)=> { 75 }, (err)=> {
60 loger.error("初始化WebRtc->失败", err); 76 loger.error("初始化WebRtc->失败", err);
61 - 77 + if(_callback){
  78 + _callback({isSuccess:false,error:err});
  79 + }
62 }); 80 });
63 this.addEvent(); 81 this.addEvent();
64 } 82 }
@@ -93,14 +111,18 @@ class WebRtcApe extends Emiter { @@ -93,14 +111,18 @@ class WebRtcApe extends Emiter {
93 111
94 this.client.on('stream-subscribed', (evt)=> { 112 this.client.on('stream-subscribed', (evt)=> {
95 let stream = evt.stream; 113 let stream = evt.stream;
96 - loger.log("Subscribe remote stream successfully: " + stream.getId()); 114 + if(stream){
  115 + loger.log("获取远程视频流成功: " + stream.getId());
  116 + let viewDiv=`<div id="${this.xdyRemote + stream.getId()}" style="width:320px;height:240px;"></div>`;
97 if(GlobalConfig.getUserRoleFromeNodeId(stream.getId())==ApeConsts.invisible){ 117 if(GlobalConfig.getUserRoleFromeNodeId(stream.getId())==ApeConsts.invisible){
98 //显示隐藏用户 118 //显示隐藏用户
99 - $(this.invisibleViewId).append('<div id="' + this.xdyRemote + stream.getId() + '" style="float:left; width:320px;height:240px;"></div>'); 119 + $(this.invisibleViewId).append(viewDiv);
100 }else { 120 }else {
101 - $(this.remoteViewId).append('<div id="' + this.xdyRemote + stream.getId() + '" style="float:left; width:320px;height:240px;"></div>'); 121 + $(this.remoteViewId).append(viewDiv);
  122 + }
  123 + stream.play(this.xdyRemote + stream.getId());
102 } 124 }
103 - stream.play('' + this.xdyRemote + stream.getId()); 125 +
104 }); 126 });
105 127
106 this.client.on('stream-removed', (evt)=> { 128 this.client.on('stream-removed', (evt)=> {
@@ -134,25 +156,45 @@ class WebRtcApe extends Emiter { @@ -134,25 +156,45 @@ class WebRtcApe extends Emiter {
134 this.client.join(this.channelKey , ""+this.channelId, this.uid, (uid)=> { 156 this.client.join(this.channelKey , ""+this.channelId, this.uid, (uid)=> {
135 this.uid = uid; 157 this.uid = uid;
136 loger.log("加入视频通话频道->成功->channelId:"+this.channelId,"uid:"+this.uid); 158 loger.log("加入视频通话频道->成功->channelId:"+this.channelId,"uid:"+this.uid);
  159 + this._emit(MessageTypes.WEB_RTC_JOIN_SUCCESS);
137 this.openLoaclStream(); 160 this.openLoaclStream();
138 }, (err)=> { 161 }, (err)=> {
139 - loger.log("加入视频通话频道->失败->", err); 162 + loger.error("加入视频通话频道->失败->", err);
  163 + this._emit(MessageTypes.WEB_RTC_JOIN_FAILED);
140 }); 164 });
141 } 165 }
  166 + //重新获取摄像头和麦克风并重新推流
  167 + reGetLoaclStream(){
  168 + if(this.isPublish){
  169 + loger.log("重新获取摄像头和麦克风并重新推流");
  170 + this.unpublish();
  171 + this.openLoaclStream();
  172 + //切换设备后自动重推流
  173 + this.rePublishDelay=setTimeout(()=>{
  174 + this.publish();
  175 + },1200);
  176 + this.publish();
  177 + }else {
  178 + this.openLoaclStream();
  179 + }
  180 + }
142 181
143 openLoaclStream() { 182 openLoaclStream() {
144 - loger.log("摄像头", this.curCamera);  
145 - loger.log("麦克风", this.curMicrophone); 183 + /* if(this.localStream){
  184 + this.localStream.close();
  185 + this.localStream=null;
  186 + }*/
146 this.localStream = AgoraRTC.createStream({ 187 this.localStream = AgoraRTC.createStream({
147 streamID: this.uid, 188 streamID: this.uid,
148 audio: true, 189 audio: true,
149 - microphoneId: this.curMicrophone,  
150 - cameraId: this.curCamera, 190 + microphoneId: this.curMicrophoneId,
  191 + cameraId: this.curCameraId,
151 video: this.isOpenVideo, 192 video: this.isOpenVideo,
152 screen: false 193 screen: false
153 }); 194 });
154 195
155 if (this.isOpenVideo) { 196 if (this.isOpenVideo) {
  197 + //设置分辨率
156 this.localStream.setVideoProfile(this.videoResolution); 198 this.localStream.setVideoProfile(this.videoResolution);
157 } 199 }
158 } 200 }
@@ -162,26 +204,41 @@ class WebRtcApe extends Emiter { @@ -162,26 +204,41 @@ class WebRtcApe extends Emiter {
162 if (!this.client) { 204 if (!this.client) {
163 return; 205 return;
164 } 206 }
165 - this.client.leaveChannel(() => {  
166 - loger.log("Leavel channel successfully"); 207 + this.client.leave(() => {
  208 + loger.log("离开视频通话频道->成功");
167 }, (err)=> { 209 }, (err)=> {
168 - loger.log("Leave channel failed"); 210 + loger.log("离开视频通话频道->失败");
169 }); 211 });
170 } 212 }
171 213
172 - publish() { 214 + closeRemoteVideoView(_data){
  215 + if(!_data){
  216 + return;
  217 + }
  218 + //根据nodeId查找视频
  219 + try{
  220 + loger.log("立即删除停止推流人员的视图");
  221 + $('#' + this.xdyRemote + _data.nodeId).remove();
  222 + }catch (err){
  223 +
  224 + }
  225 +
  226 + }
  227 + publish(_params) {
173 if (!this.client||!this.localStream) { 228 if (!this.client||!this.localStream) {
174 return; 229 return;
175 } 230 }
176 this.localStream.init(()=> { 231 this.localStream.init(()=> {
177 - loger.log("webRtc->推流: ");  
178 let viewName = this.localViewId.replace("#", ""); 232 let viewName = this.localViewId.replace("#", "");
  233 + loger.log("webRtc->推流->",viewName);
179 this.localStream.play(viewName); 234 this.localStream.play(viewName);
180 this.client.publish(this.localStream, (err)=> { 235 this.client.publish(this.localStream, (err)=> {
181 loger.log("webRtc->推流失败: " + err); 236 loger.log("webRtc->推流失败: " + err);
182 GlobalConfig.openCamera =0; 237 GlobalConfig.openCamera =0;
183 GlobalConfig.openMicrophones =0; 238 GlobalConfig.openMicrophones =0;
  239 + this.isPublish=false;
184 }); 240 });
  241 + this.isPublish=true;
185 GlobalConfig.openCamera = EngineUtils.creatTimestamp(); 242 GlobalConfig.openCamera = EngineUtils.creatTimestamp();
186 GlobalConfig.openMicrophones = GlobalConfig.openCamera; 243 GlobalConfig.openMicrophones = GlobalConfig.openCamera;
187 244
@@ -195,11 +252,13 @@ class WebRtcApe extends Emiter { @@ -195,11 +252,13 @@ class WebRtcApe extends Emiter {
195 }); 252 });
196 253
197 }, (err)=> { 254 }, (err)=> {
198 - loger.log("webRtc->推流->本地流开启失败", err); 255 + loger.error("webRtc->推流->本地流开启失败", err);
  256 + this._emit(MessageTypes.WEB_RTC_PUBLISH_FAILED,err);
199 }); 257 });
200 } 258 }
201 259
202 unpublish() { 260 unpublish() {
  261 + clearTimeout(this.rePublishDelay);
203 loger.log("webRtc->停止推流 "); 262 loger.log("webRtc->停止推流 ");
204 if (!this.client||!this.localStream) { 263 if (!this.client||!this.localStream) {
205 return; 264 return;
@@ -209,6 +268,7 @@ class WebRtcApe extends Emiter { @@ -209,6 +268,7 @@ class WebRtcApe extends Emiter {
209 }); 268 });
210 this.localStream.close(); 269 this.localStream.close();
211 $(this.localViewId).html(""); 270 $(this.localViewId).html("");
  271 + this.isPublish=false;
212 GlobalConfig.openCamera =0; 272 GlobalConfig.openCamera =0;
213 GlobalConfig.openMicrophones =0; 273 GlobalConfig.openMicrophones =0;
214 this._emit(MessageTypes.USER_DEVICE_STATUS_CHAANGE, { 274 this._emit(MessageTypes.USER_DEVICE_STATUS_CHAANGE, {
@@ -247,29 +307,80 @@ class WebRtcApe extends Emiter { @@ -247,29 +307,80 @@ class WebRtcApe extends Emiter {
247 this.invisibleViewId = _params.divId||""; 307 this.invisibleViewId = _params.divId||"";
248 this.invisibleStyle = _params.styleStr||""; 308 this.invisibleStyle = _params.styleStr||"";
249 } 309 }
  310 + /*
  311 + * 切换当前使用的设备
  312 + * */
  313 + changeDevices(_params){
  314 + loger.log("切换设备->",_params);
  315 + if(!_params){
  316 + return;
  317 + }
  318 + //设置摄像头
  319 + if(_params.curCamera){
  320 + for(let k in this.cameras){
  321 + let item=this.cameras[k];
  322 + if(item&&item.label==_params.curCamera){
  323 + this.curCameraId=item.deviceId;
  324 + GlobalConfig.curCamera=_params.curCamera;
  325 + break;
  326 + }
  327 + }
  328 + }
  329 + //设置麦克风
  330 + if(_params.curMicrophone){
  331 + for(let k in this.microphones){
  332 + let item=this.microphones[k];
  333 + if(item&&item.label==_params.curMicrophone){
  334 + this.curMicrophoneId=item.deviceId;
  335 + GlobalConfig.curMicrophone=_params.curMicrophone;
  336 + break;
  337 + }
  338 + }
  339 + }
  340 + //分辨率
  341 + if(_params.videoResolution){
  342 + this.videoResolution=_params.videoResolution||"240P";//默认是240P 20 320x240 15 200
  343 + }
  344 +
  345 + clearTimeout(this.changeDevicesDelay);
  346 + this.changeDevicesDelay=setTimeout(()=>{
  347 + //重新获取本地视图流
  348 + this.reGetLoaclStream();
  349 + },1400);
  350 + }
250 351
  352 + /*
  353 + * 获取设备信息
  354 + * */
251 getDevices(_params,_callback) { 355 getDevices(_params,_callback) {
252 AgoraRTC.getDevices((devices)=> { 356 AgoraRTC.getDevices((devices)=> {
253 - console.log("devices", devices) 357 + //下面的数组存的是对象
  358 + this.microphones=[];
  359 + this.cameras=[];
  360 +
  361 + //选的数组存的是设备名称
  362 + GlobalConfig.cameras=[];
  363 + GlobalConfig.microphones=[];
  364 +
  365 + loger.log("devices", devices)
254 for (let i = 0; i < devices.length; i++) { 366 for (let i = 0; i < devices.length; i++) {
255 let device = devices[i]; 367 let device = devices[i];
256 - if (device.kind === 'audioinput') { 368 + //{"deviceId":"default","kind":"audiooutput","label":"默认","groupId":"cf49a03ca26700235629fc13d3e6630bd34407c66438d157056a34dd3ae03ef5"}
  369 + if (device.kind == 'audioinput') {
257 this.microphones.push(device); 370 this.microphones.push(device);
258 - } else if (device.kind === 'videoinput') { 371 + GlobalConfig.microphones.push(device.label);
  372 + } else if (device.kind == 'videoinput') {
259 this.cameras.push(device); 373 this.cameras.push(device);
  374 + GlobalConfig.cameras.push(device.label);
260 } else { 375 } else {
261 - loger.warn('未知的设备: ', device);  
262 - } 376 + loger.warn('其他设备: ', device);
263 } 377 }
264 - if (this.cameras && this.cameras.length > 0) {  
265 - this.curCamera = this.cameras[0].deviceId || "";  
266 - }  
267 - if (this.microphones && this.microphones.length > 0) {  
268 - this.curMicrophone = this.microphones[0].deviceId || "";  
269 } 378 }
  379 + let _deviceData={cameras:GlobalConfig.cameras,microphones:GlobalConfig.microphones};
270 if(_callback){ 380 if(_callback){
271 - _callback({cameras:this.cameras,microphones:this.microphones}); 381 + _callback(_deviceData);
272 } 382 }
  383 + this._emit(MessageTypes.GET_DEVICES_SUCCESS,_deviceData);
273 }); 384 });
274 } 385 }
275 } 386 }