trtc-room.js 33 KB

1
  1. import UserController from"controller/user-controller.js";import Pusher from"model/pusher.js";import{EVENT,DEFAULT_COMPONENT_CONFIG}from"common/constants.js";import Event from"utils/event.js";import*as ENV from"utils/environment.js";import MTA from"libs/mta_analysis.js";const TAG_NAME="TRTC-ROOM";let touchX=0,touchY=0;Component({properties:{config:{type:Object,value:{sdkAppID:"",userID:"",userSig:"",template:"",debugMode:!1,enableIM:!1},observer:function(e,t){this._propertyObserver({name:"config",newVal:e,oldVal:t})}},teacherNameplateInfo:{type:Object,value:{type:"video",avatar:"",nickName:"nick"}},viewerNameplateInfo:{type:Object,value:{type:"video",avatar:"",nickName:"nick"}},direction:{type:String,value:"horizontal"}},data:{testinfo:{},pusher:null,debugPanel:!0,debug:!1,streamList:[],visibleStreamList:[],userList:[],template:"",cameraPosition:"",panelName:"",localVolume:0,remoteVolumeList:[],enableIM:!1,showIMPanel:!1,exitIMThrottle:!1,messageContent:"",messageList:[],maxMessageListLength:10,messageListScrollTop:0,appVersion:ENV.APP_VERSION,libVersion:ENV.LIB_VERSION,hasGridPageTipsShow:!1,gridPageCount:0,gridCurrentPage:1,gridPlayerPerPage:4,gridPagePlaceholderStreamList:[],isFullscreenDevice:ENV.IS_FULLSCREEN_DEVICE,isShowMoreMenu:!1,MICVolume:50,BGMVolume:50,BGMProgress:0,beautyStyle:"smooth",beautyStyleArray:[{value:"smooth",label:"光滑",checked:!0},{value:"nature",label:"自然",checked:!1},{value:"close",label:"关闭",checked:!1}],filterIndex:0,filterArray:[{value:"standard",label:"标准"},{value:"pink",label:"粉嫩"},{value:"nostalgia",label:"怀旧"},{value:"blues",label:"蓝调"},{value:"romantic",label:"浪漫"},{value:"cool",label:"清凉"},{value:"fresher",label:"清新"},{value:"solor",label:"日系"},{value:"aestheticism",label:"唯美"},{value:"whitening",label:"美白"},{value:"cerisered",label:"樱红"}],audioReverbType:0,audioReverbTypeArray:["关闭","KTV","小房间","大会堂","低沉","洪亮","金属声","磁性"]},lifetimes:{created:function(){MTA.App.init({appID:"500710685",eventID:"500710697",autoReport:!0,statParam:!0})},attached:function(){this._init(),MTA.Page.stat()},ready:function(){},detached:function(){this.exitRoom()},error:function(e){}},pageLifetimes:{show:function(){if(this.status.isPending)this.status.isPending=!1,this.enterRoom({roomID:this.data.config.roomID}).then((()=>{}));else if(ENV.IS_ANDROID&&"hide"===this.status.pageLife&&this.status.isOnHideAddStream&&this.data.streamList.length>0){const e=this.data.streamList;let t=[];for(let s=0;s<e.length;s++)if(e[s].isOnHideAdd&&e[s].playerContext){const a=e[s];t.push(a),a.playerContext=void 0,e.splice(s,1)}this._setList({streamList:e}).then((()=>{for(let s=0;s<t.length;s++)e.push(t[s]);this._setList({streamList:e}).then((()=>{for(let e=0;e<t.length;e++)t[e]=wx.createLivePlayerContext(t[e].streamID,this);t=[]}))})),this.status.isOnHideAddStream=!1}this.status.pageLife="show"},hide:function(){this.status.pageLife="hide"},resize:function(e){}},methods:{_init(){this.userController=new UserController(this),this._emitter=new Event,this.EVENT=EVENT,this._initStatus(),this._bindEvent(),this._gridBindEvent(),this._keepScreenOn()},_initStatus(){this.status={isPush:!1,isPending:!1,pageLife:"",isOnHideAddStream:!1},this._lastTapTime=0,this._beforeLastTapTime=0,this._lastTapCoordinate={x:0,y:0},this._isFullscreen=!1},_propertyObserver(e){if("config"===e.name){const t=Object.assign({},DEFAULT_COMPONENT_CONFIG,e.newVal);"string"==typeof t.debugMode&&(t.debugMode="true"===t.debugMode),t.enableIM&&t.sdkAppID&&this._initIM(t),t.sdkAppID&&e.oldVal.sdkAppID!==t.sdkAppID&&MTA&&MTA.Event.stat("sdkAppID",{value:t.sdkAppID}),this.setData({enableIM:t.enableIM,template:t.template,debugMode:t.debugMode||!1,debug:t.debugMode||!1}),this._setPusherConfig(t)}},enterRoom(e){return new Promise(((t,s)=>{console.log(TAG_NAME,"config",this.data.config),e&&(Object.assign(this.data.pusher,e),Object.assign(this.data.config,e)),this._checkParam(this.data.config)?(this._getPushUrl(this.data.config).then((e=>{this.data.pusher.url=e,this.setData({pusher:this.data.pusher},(()=>{this.data.pusher.getPusherContext().start(),this.status.isPush=!0,t()}))})).catch((e=>{console.error(TAG_NAME,"enterRoom error",e),s(e)})),this._loginIM({...this.data.config,roomID:e.roomID})):s(new Error("缺少必要参数"))}))},exitRoom(){return"hide"===this.status.pageLife&&console.warn(TAG_NAME,"小程序最小化时不能调用 exitRoom,如果不想听到远端声音,可以调用取消订阅,如果不想远端听到声音,可以调用取消发布"),new Promise(((e,t)=>{this._exitIM(),this.data.pusher.reset(),this.status.isPush=!1;const s=this.userController.reset();this.setData({pusher:this.data.pusher,userList:s.userList,streamList:s.streamList,visibleStreamList:this._filterVisibleStream(s.streamList)},(()=>{e({userList:this.data.userList,streamList:this.data.streamList}),this._emitter.emit(EVENT.LOCAL_LEAVE,{userID:this.data.pusher.userID})}))}))},publishLocalVideo(){return this._setPusherConfig({enableCamera:!0})},unpublishLocalVideo(){return this._setPusherConfig({enableCamera:!1})},publishLocalAudio(){return this._setPusherConfig({enableMic:!0})},unpublishLocalAudio(){return this._setPusherConfig({enableMic:!1})},disableVideo(){this.unpublishLocalVideo(),this.setViewerMediaMute("video",!1)},enableVideo(){this.publishLocalVideo(),this.setViewerMediaMute("video",!0)},disableAudio(){this.unpublishLocalAudio(),this.setViewerMediaMute("audio",!1)},enableAudio(){this.publishLocalAudio(),this.setViewerMediaMute("audio",!0)},setViewerMediaMute(e,t){const{viewerNameplateInfo:s}=this.data;"video"===e?(s.video=t,s.type=t?"video":"audio"):s.audio=t,this.setData({viewerNameplateInfo:s})},setTeacherMediaMute(e,t){const{teacherNameplateInfo:s}=this.data;"video"===e?(s.video=t,s.type=t?"video":"audio"):s.audio=t,this.setData({teacherNameplateInfo:s})},subscribeRemoteVideo(e){const t=this.data.config.roomID;if("1v1"===this.data.template&&t!==e.userID)return;const s={muteVideo:!1},a="small"===e.streamType?"main":e.streamType,i=this.userController.getStream({userID:e.userID,streamType:a});return i.muteVideoPrev=!1,"small"!==e.streamType&&"main"!==e.streamType||i&&"main"===i.streamType&&("small"===e.streamType?(s.src=i.src.replace("main","small"),s._definitionType="small"):"main"===e.streamType&&(i.src=i.src.replace("small","main"),s._definitionType="main")),this._setPlayerConfig({userID:e.userID,streamType:a,config:s})},unsubscribeRemoteVideo(e){return this.userController.getStream({userID:e.userID,streamType:e.streamType}).muteVideoPrev=!0,this._setPlayerConfig({userID:e.userID,streamType:e.streamType,config:{muteVideo:!0}})},subscribeRemoteAudio(e){const t=this.data.config.roomID;if("1v1"!==this.data.template||t===e.userID)return this._setPlayerConfig({userID:e.userID,streamType:"main",config:{muteAudio:!1}})},unsubscribeRemoteAudio(e){return this._setPlayerConfig({userID:e.userID,streamType:"main",config:{muteAudio:!0}})},on(e,t,s){this._emitter.on(e,t,s)},off(e,t){this._emitter.off(e,t)},getRemoteUserList(){return this.data.userList},switchCamera(){this.data.cameraPosition||(this.data.cameraPosition=this.data.pusher.frontCamera),this.data.cameraPosition="front"===this.data.cameraPosition?"back":"front",this.setData({cameraPosition:this.data.cameraPosition},(()=>{})),this.data.pusher.getPusherContext().switchCamera()},setViewRect(e){return"custom"!==this.data.template&&console.warn(`如需使用setViewRect方法,请初始化时设置template:"custom", 当前 template:"${this.data.template}"`),console.info("不建议使用该方法动态修改样式,避免引起微信小程序渲染问题,建议直接修改 wxml wxss 进行样式定制化"),this.data.pusher.userID===e.userID?this._setPusherConfig({xAxis:e.xAxis,yAxis:e.yAxis,width:e.width,height:e.height}):this._setPlayerConfig({userID:e.userID,streamType:e.streamType,config:{xAxis:e.xAxis,yAxis:e.yAxis,width:e.width,height:e.height}})},setViewVisible(e){return"custom"!==this.data.template&&console.warn(`如需使用setViewVisible方法,请初始化时设置template:"custom", 当前 template:"${this.data.template}"`),console.info("不建议使用该方法动态修改样式,避免引起微信小程序渲染问题,建议直接修改 wxml wxss 进行样式定制化"),this.data.pusher.userID===e.userID?this._setPusherConfig({isVisible:e.isVisible}):this._setPlayerConfig({userID:e.userID,streamType:e.streamType,config:{isVisible:e.isVisible}})},setViewZIndex(e){return"custom"!==this.data.template&&console.warn(`如需使用setViewZIndex方法,请初始化时设置template:"custom", 当前 template:"${this.data.template}"`),console.info("不建议使用该方法动态修改样式,避免引起微信小程序渲染问题,建议直接修改 wxml wxss 进行样式定制化"),this.data.pusher.userID===e.userID?this._setPusherConfig({zIndex:e.zindex||e.zIndex}):this._setPlayerConfig({userID:e.userID,streamType:e.streamType,config:{zIndex:e.zindex||e.zIndex}})},playBGM(e){return new Promise(((t,s)=>{this.data.pusher.getPusherContext().playBGM({url:e.url,success:()=>{t()},fail:()=>{this._emitter.emit(EVENT.BGM_PLAY_FAIL),s(new Error("播放背景音失败"))}})}))},stopBGM(){this.data.pusher.getPusherContext().stopBGM()},pauseBGM(){this.data.pusher.getPusherContext().pauseBGM()},resumeBGM(){this.data.pusher.getPusherContext().resumeBGM()},setBGMVolume(e){this.data.pusher.getPusherContext().setBGMVolume({volume:e.volume})},setMICVolume(e){this.data.pusher.getPusherContext().setMICVolume({volume:e.volume})},sendSEI(e){return new Promise(((t,s)=>{this.data.pusher.getPusherContext().sendMessage({msg:e.message,success:function(e){t(e)}})}))},snapshot(e){return new Promise(((t,s)=>{this.captureSnapshot(e).then((e=>{wx.saveImageToPhotosAlbum({filePath:e.tempImagePath,success(s){wx.showToast({title:"已保存到相册"}),t(e)},fail:function(e){wx.showToast({icon:"none",title:"保存失败"}),s(e)}})})).catch((e=>{s(e)}))}))},captureSnapshot(e){return new Promise(((t,s)=>{e.userID===this.data.pusher.userID?this.data.pusher.getPusherContext().snapshot({quality:"raw",complete:e=>{e.tempImagePath?t(e):s(new Error("截图失败"))}}):this.userController.getStream(e).playerContext.snapshot({quality:"raw",complete:e=>{e.tempImagePath?t(e):s(new Error("截图失败"))}})}))},enterFullscreen(e){return new Promise(((t,s)=>{this.userController.getStream(e).playerContext.requestFullScreen({direction:e.direction||0,success:e=>{t(e)},fail:e=>{s(e)}})}))},exitFullscreen(e){return new Promise(((t,s)=>{this.userController.getStream(e).playerContext.exitFullScreen({success:e=>{t(e)},fail:e=>{s(e)}})}))},setRemoteOrientation(e){return this._setPlayerConfig({userID:e.userID,streamType:e.streamType,config:{orientation:e.orientation}})},setViewOrientation(e){return this._setPlayerConfig({userID:e.userID,streamType:e.streamType,config:{orientation:e.orientation}})},setRemoteFillMode(e){return this._setPlayerConfig({userID:e.userID,streamType:e.streamType,config:{objectFit:e.fillMode}})},setViewFillMode(e){return this._setPlayerConfig({userID:e.userID,streamType:e.streamType,config:{objectFit:e.fillMode}})},sendC2CTextMessage(e){if(!this.tim)return void console.warn(TAG_NAME,"未开启IM功能,该方法无法使用",e);const t=this.tim.createTextMessage({to:e.userID+"",payload:{text:e.message}}),s=this.tim.sendMessage(t);return s.then((function(e){})).catch((function(e){console.warn(TAG_NAME,"sendC2CTextMessage error:",e)})),s},sendC2CCustomMessage(e){if(!this.tim)return void console.warn(TAG_NAME,"未开启IM功能,该方法无法使用",e);const t=this.tim.createCustomMessage({to:e.userID+"",conversationType:TIM.TYPES.CONV_C2C,payload:e.payload}),s=this.tim.sendMessage(t);return s.then((function(e){})).catch((function(e){console.warn(TAG_NAME,"sendMessage error:",e)})),s},sendGroupTextMessage(e){if(!this.tim)return void console.warn(TAG_NAME,"未开启IM功能,该方法无法使用",e);const t=this.tim.createTextMessage({to:e.roomID+"",conversationType:TIM.TYPES.CONV_GROUP,payload:{text:e.message}}),s=this.tim.sendMessage(t);return s.then((function(e){})).catch((function(e){console.warn(TAG_NAME,"sendGroupTextMessage error:",e)})),s},sendGroupCustomMessage(e){if(!this.tim)return void console.warn(TAG_NAME,"未开启IM功能,该方法无法使用",e);const t=this.tim.createCustomMessage({to:e.roomID+"",conversationType:TIM.TYPES.CONV_GROUP,payload:e.payload}),s=this.tim.sendMessage(t);return s.then((function(e){})).catch((function(e){console.warn(TAG_NAME,"sendMessage error:",e)})),s},_setPusherConfig(e,t=!1){return new Promise(((t,s)=>{this.data.pusher?Object.assign(this.data.pusher,e):this.data.pusher=new Pusher(e),this.setData({pusher:this.data.pusher},(()=>{t(e)}))}))},_setPlayerConfig(e){const t=e.userID,s=e.streamType,a=e.config;return new Promise(((i,r)=>{const o=this.userController.getUser(t);o&&o.streams[s]?(Object.assign(o.streams[s],a),this.setData({streamList:this.data.streamList,visibleStreamList:this._filterVisibleStream(this.data.streamList,!0)},(()=>{i(e)}))):console.warn(TAG_NAME,"指定 userID 或者 streamType 不存在")}))},_setList(e){const{userList:t,streamList:s}=e;return new Promise(((a,i)=>{let r=[];const o={userList:t||this.data.userList,streamList:s||this.data.streamList};"grid"===this.data.template&&(r=this._filterVisibleStream(s),o.visibleStreamList=r||this.data.visibleStreamList,o.gridPagePlaceholderStreamList=this.data.gridPagePlaceholderStreamList,o.gridCurrentPage=this.data.gridCurrentPage,o.gridPageCount=this.data.gridPageCount),this.setData(o,(()=>{a(e)}))}))},_checkParam:e=>e.sdkAppID?void 0===e.roomID?(console.error("未设置 roomID"),!1):e.roomID<1||e.roomID>4294967296?(console.error("roomID 超出取值范围 1 ~ 4294967295"),!1):e.userID?e.userSig?!!e.template||(console.error("未设置 template"),!1):(console.error("未设置 userSig"),!1):(console.error("未设置 userID"),!1):(console.error("未设置 sdkAppID"),!1),_getPushUrl(e){if(ENV.IS_TRTC)return new Promise(((t,s)=>{e.scene=e.scene&&"rtc"!==e.scene?e.scene:"videocall",e.enableBlackStream=e.enableBlackStream||"",e.encsmall=e.encsmall||0,e.cloudenv=e.cloudenv||"PRO",e.streamID=e.streamID||"",e.userDefineRecordID=e.userDefineRecordID||"",e.privateMapKey=e.privateMapKey||"",e.pureAudioMode=e.pureAudioMode||"",e.recvMode=e.recvMode||1;let a="";a="&strroomid="+e.roomID,setTimeout((()=>{const s="room://cloud.tencent.com/rtc?sdkappid="+e.sdkAppID+a+"&userid="+e.userID+"&usersig="+e.userSig+"&appscene="+e.scene+"&encsmall="+e.encsmall+"&cloudenv="+e.cloudenv+"&enableBlackStream="+e.enableBlackStream+"&streamid="+e.streamID+"&userdefinerecordid="+e.userDefineRecordID+"&privatemapkey="+e.privateMapKey+"&pureaudiomode="+e.pureAudioMode+"&recvmode="+e.recvMode;console.warn(TAG_NAME,"getPushUrl result:",s),t(s)}),0)}));console.error(TAG_NAME,"组件仅支持微信 App iOS >=7.0.9, Android >= 7.0.8, 小程序基础库版 >= 2.10.0"),console.error(TAG_NAME,"需要真机运行,开发工具不支持实时音视频")},_requestSigServer(e){const t=e.sdkAppID,s=e.userID,a=e.userSig,i=e.roomID,r=e.privateMapKey;e.useCloud=void 0===e.useCloud||e.useCloud;let o=e.useCloud?"https://official.opensso.tencent-cloud.com/v4/openim/jsonvideoapp":"https://yun.tim.qq.com/v4/openim/jsonvideoapp";o+="?sdkappid="+t+"&identifier="+s+"&usersig="+a+"&random="+Date.now()+"&contenttype=json";const n={Cmd:1,SeqNo:1,BusType:7,GroupId:i},u={PrivMapEncrypt:r,TerminalType:1,FromType:3,SdkVersion:26280566};return new Promise(((a,r)=>{wx.request({url:o,data:{ReqHead:n,ReqBody:u},method:"POST",success:o=>{(o.data.ErrorCode||0!==o.data.RspHead.ErrorCode)&&(console.error("获取roomsig失败"),r(o));const n=JSON.stringify(o.data.RspBody);let u="room://cloud.tencent.com?sdkappid="+t+"&roomid="+i+"&userid="+s+"&roomsig="+encodeURIComponent(n);if(e.pureAudioPushMod||e.recordId){const t={Str_uc_params:{pure_audio_push_mod:0,record_id:0}};e.pureAudioPushMod?t.Str_uc_params.pure_audio_push_mod=e.pureAudioPushMod:delete t.Str_uc_params.pure_audio_push_mod,e.recordId?t.Str_uc_params.record_id=e.recordId:delete t.Str_uc_params.record_id,u+="&bizbuf="+encodeURIComponent(JSON.stringify(t))}a(u)},fail:e=>{r(e)}})}))},_doubleTabToggleFullscreen(e){const t=e.timeStamp,s=this._lastTapTime,a=this._lastTapCoordinate,i=e.detail,r=Math.sqrt(Math.pow(Math.abs(i.x-a.x),2)+Math.pow(Math.abs(i.y-a.y),2));this._lastTapCoordinate=i;const o=this._beforeLastTapTime;if(t-s>0&&t-s<300&&s-o>1500&&r<20){const t=e.currentTarget.dataset.userid,a=e.currentTarget.dataset.streamtype;if(this._isFullscreen)this.exitFullscreen({userID:t,streamType:a}).then((()=>{this._isFullscreen=!1})).catch((()=>{}));else{let e;this.enterFullscreen({userID:t,streamType:a,direction:e}).then((()=>{this._isFullscreen=!0})).catch((()=>{}))}this._beforeLastTapTime=s}this._lastTapTime=t},_bindEvent(){this.userController.on(EVENT.REMOTE_USER_JOIN,(e=>{this.setData({userList:e.data.userList},(()=>{this._emitter.emit(EVENT.REMOTE_USER_JOIN,{userID:e.data.userID})}))})),this.userController.on(EVENT.REMOTE_USER_LEAVE,(e=>{e.data.userID&&this._setList({userList:e.data.userList,streamList:e.data.streamList}).then((()=>{this._emitter.emit(EVENT.REMOTE_USER_LEAVE,{userID:e.data.userID})}))})),this.userController.on(EVENT.REMOTE_VIDEO_ADD,(e=>{const t=e.data.stream,s=this.data.config.roomID;"1v1"===this.data.template&&s!==t.userID||("hide"===this.status.pageLife&&(this.status.isOnHideAddStream=!0,t.isOnHideAdd=!0),this._setList({userList:e.data.userList,streamList:e.data.streamList}).then((()=>{this.setTeacherMediaMute("video",!0),t.playerContext=wx.createLivePlayerContext(t.streamID,this),this._emitter.emit(EVENT.REMOTE_VIDEO_ADD,{userID:t.userID,streamType:t.streamType})})))})),this.userController.on(EVENT.REMOTE_VIDEO_REMOVE,(e=>{const t=e.data.stream;this._setList({userList:e.data.userList,streamList:e.data.streamList}).then((()=>{this.setTeacherMediaMute("video",!1),t.userID&&t.streamType&&this._emitter.emit(EVENT.REMOTE_VIDEO_REMOVE,{userID:t.userID,streamType:t.streamType})}))})),this.userController.on(EVENT.REMOTE_AUDIO_ADD,(e=>{const t=e.data.stream,s=this.data.config.roomID;"1v1"===this.data.template&&s!==t.userID||this._setList({userList:e.data.userList,streamList:e.data.streamList}).then((()=>{this.setTeacherMediaMute("audio",!0),t.playerContext=wx.createLivePlayerContext(t.streamID,this),this._emitter.emit(EVENT.REMOTE_AUDIO_ADD,{userID:t.userID,streamType:t.streamType})}))})),this.userController.on(EVENT.REMOTE_AUDIO_REMOVE,(e=>{const t=e.data.stream;this._setList({userList:e.data.userList,streamList:e.data.streamList}).then((()=>{this.setTeacherMediaMute("audio",!1),t.userID&&t.streamType&&this._emitter.emit(EVENT.REMOTE_AUDIO_REMOVE,{userID:t.userID,streamType:t.streamType})}))}))},_pusherStateChangeHandler(e){const t=e.detail.code,s=e.detail.message;switch(t){case 0:case 1001:case 1002:case 1003:case 1004:case 1005:case 1006:case 1007:case 1008:break;case 1018:this._emitter.emit(EVENT.LOCAL_JOIN,{userID:this.data.pusher.userID});break;case 1019:case 2003:break;case 1020:case 1031:case 1032:case 1033:case 1034:this.userController.userEventHandler(e);break;case-1301:console.error(TAG_NAME,"打开摄像头失败: ",t),this._emitter.emit(EVENT.ERROR,{code:t,message:s});break;case-1302:console.error(TAG_NAME,"打开麦克风失败: ",t),this._emitter.emit(EVENT.ERROR,{code:t,message:s});break;case-1303:console.error(TAG_NAME,"视频编码失败: ",t),this._emitter.emit(EVENT.ERROR,{code:t,message:s});break;case-1304:console.error(TAG_NAME,"音频编码失败: ",t),this._emitter.emit(EVENT.ERROR,{code:t,message:s});break;case-1307:console.error(TAG_NAME,"推流连接断开: ",t),this._emitter.emit(EVENT.ERROR,{code:t,message:s});break;case-100018:console.error(TAG_NAME,"进房失败: userSig 校验失败,请检查 userSig 是否填写正确",t,s),this._emitter.emit(EVENT.ERROR,{code:t,message:s});break;case 5e3:break;case 5001:this.status.isPending=!0,this.status.isPush&&this.exitRoom()}},_pusherNetStatusHandler(e){this._emitter.emit(EVENT.LOCAL_NET_STATE_UPDATE,e)},_pusherErrorHandler(e){console.warn(TAG_NAME,"pusher error",e);try{const t=e.detail.errCode,s=e.detail.errMsg;this._emitter.emit(EVENT.ERROR,{code:t,message:s})}catch(t){console.error(TAG_NAME,"pusher error data parser exception",e,t)}},_pusherBGMStartHandler(e){},_pusherBGMProgressHandler(e){this._emitter.emit(EVENT.BGM_PLAY_PROGRESS,e)},_pusherBGMCompleteHandler(e){this._emitter.emit(EVENT.BGM_PLAY_COMPLETE,e)},_pusherAudioVolumeNotify:function(e){this._emitter.emit(EVENT.LOCAL_AUDIO_VOLUME_UPDATE,e)},_playerStateChange(e){this._emitter.emit(EVENT.REMOTE_STATE_UPDATE,e)},_playerFullscreenChange(e){this._emitter.emit(EVENT.REMOTE_FULLSCREEN_UPDATE,e),this._emitter.emit(EVENT.VIDEO_FULLSCREEN_UPDATE,e)},_playerNetStatus(e){const t=this.userController.getStream({userID:e.currentTarget.dataset.userid,streamType:e.currentTarget.dataset.streamtype});!t||t.videoWidth===e.detail.info.videoWidth&&t.videoHeight===e.detail.info.videoHeight||(t.videoWidth=e.detail.info.videoWidth,t.videoHeight=e.detail.info.videoHeight),this._emitter.emit(EVENT.REMOTE_NET_STATE_UPDATE,e)},_playerAudioVolumeNotify(e){this._emitter.emit(EVENT.REMOTE_AUDIO_VOLUME_UPDATE,e)},_filterVisibleStream(e,t){const s=e.filter((e=>!0));return s.sort(((e,t)=>{const s=e.userID.toUpperCase(),a=t.userID.toUpperCase();return s<a?-1:s>a?1:0})),"grid"!==this.data.template||t||(this._filterGridPageVisibleStream(s),this.data.gridCurrentPage>1&&this.data.gridPagePlaceholderStreamList.length===this.data.gridPlayerPerPage&&this._gridPageToPrev(s)),s},_filterGridPageVisibleStream(e){const t=e.length;this.data.gridPageCount=Math.ceil((t+1)/this.data.gridPlayerPerPage),this.data.gridPagePlaceholderStreamList=[];let s,a=0;s=this.data.gridPlayerPerPage>3?1===this.data.gridCurrentPage?[-1,this.data.gridPlayerPerPage-1]:[this.data.gridCurrentPage*this.data.gridPlayerPerPage-(this.data.gridPlayerPerPage+2),this.data.gridCurrentPage*this.data.gridPlayerPerPage-1]:[this.data.gridCurrentPage*this.data.gridPlayerPerPage-(this.data.gridPlayerPerPage+1),this.data.gridCurrentPage*this.data.gridPlayerPerPage];for(let i=0;i<t;i++)i>s[0]&&i<s[1]?(e[i].isVisible=!0,e[i].muteVideo=void 0===e[i].muteVideoPrev?e[i].muteVideo:e[i].muteVideoPrev,a++):(e[i].isVisible=!1,e[i].muteVideo=!0);if(1!==this.data.gridCurrentPage)for(let e=0;e<this.data.gridPlayerPerPage-a;e++)this.data.gridPagePlaceholderStreamList.push({id:"holder-"+e});return e},_keepScreenOn(){setInterval((()=>{wx.setKeepScreenOn({keepScreenOn:!0})}),2e4)},_initIM(e){e.enableIM&&e.sdkAppID&&this.tim},_loginIM(e){if(this.tim)return this.tim.login({userID:e.userID,userSig:e.userSig})},_logoutIM(){if(this.tim)return this.tim.logout()},_exitIM(){if(this.data.exitIMThrottle||!this.tim)return;this.data.exitIMThrottle=!0;const e=this.getRemoteUserList(),t=this.data.config.roomID,s=this.data.config.userID;this._searchGroup({roomID:t}).then((a=>{a.data.group.ownerID===s&&0===e.length?this._dismissGroup({roomID:t}).then((()=>{this.data.exitIMThrottle=!1,this._logoutIM()})).catch((e=>{this.data.exitIMThrottle=!1,this._logoutIM()})):a.data.group.ownerID===s?(this.data.exitIMThrottle=!1,this._logoutIM()):this._quitGroup({roomID:t}).then((()=>{this.data.exitIMThrottle=!1,this._logoutIM()})).catch((e=>{this.data.exitIMThrottle=!1,this._logoutIM()}))})).catch((e=>{this.data.exitIMThrottle=!1,this._logoutIM()}))},_searchGroup(e){if(!this.tim)return;const t=this.tim.searchGroupByID(e.roomID+"");return t.then((function(e){})).catch((function(e){console.warn(TAG_NAME,"_searchGroup fail,TIM 报错信息不影响后续逻辑,可以忽略",e)})),t},_createGroup(e){if(!this.tim)return;const t=this.tim.createGroup({groupID:e.roomID+"",name:e.roomID+"",type:IM_GROUP_TYPE});return t.then((e=>{})).catch((e=>{console.warn(TAG_NAME,"_createGroup error",e)})),t},_joinGroup(e){if(!this.tim)return;const t=this.tim.joinGroup({groupID:e.roomID+"",type:IM_GROUP_TYPE});return t.then((e=>{switch(e.data.status){case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL:case TIM.TYPES.JOIN_STATUS_SUCCESS:case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP:}})).catch((e=>{console.warn(TAG_NAME,"joinGroup error",e)})),t},_quitGroup(e){if(!this.tim)return;const t=this.tim.quitGroup(e.roomID+"");return t.then((e=>{})).catch((e=>{console.warn(TAG_NAME,"quitGroup error",e)})),t},_dismissGroup(e){if(!this.tim)return;const t=this.tim.dismissGroup(e.roomID+"");return t.then((e=>{})).catch((e=>{console.warn(TAG_NAME,"_dismissGroup error",e)})),t},_onIMReady(e){this._emitter.emit(EVENT.IM_READY,e);const t=this.data.config.roomID;this._searchGroup({roomID:t}).then((e=>{this._joinGroup({roomID:t})})).catch((()=>{this._createGroup({roomID:t}).then((e=>{this._joinGroup({roomID:t})})).catch((e=>{10021===e.code&&this._joinGroup({roomID:t})}))}))},_onIMMessageReceived(e){const t=e.data,s=this.data.config.roomID+"",a=this.data.config.userID+"";for(let e=0;e<t.length;e++){const i=t[e];i.to!==s+""&&i.to!==a||(i.type===TIM.TYPES.MSG_TEXT?this._pushMessageList({name:i.from,message:i.payload.text}):i.type===TIM.TYPES.MSG_GRP_SYS_NOTICE&&2===i.payload.operationType&&this._pushMessageList({name:"系统通知",message:`欢迎 ${a}`}))}this._emitter.emit(EVENT.IM_MESSAGE_RECEIVED,e)},_onIMNotReady(e){this._emitter.emit(EVENT.IM_NOT_READY,e)},_onIMKickedOut(e){this._emitter.emit(EVENT.IM_KICKED_OUT,e)},_onIMError(e){this._emitter.emit(EVENT.IM_ERROR,e)},_toggleVideo(){this.data.pusher.enableCamera?this.unpublishLocalVideo():this.publishLocalVideo()},_toggleAudio(){this.data.pusher.enableMic?this.unpublishLocalAudio():this.publishLocalAudio()},_debugToggleRemoteVideo(e){const t=e.currentTarget.dataset.userID,s=e.currentTarget.dataset.streamType;this.data.streamList.find((e=>e.userID===t&&e.streamType===s)).muteVideo?this.subscribeRemoteVideo({userID:t,streamType:s}):this.unsubscribeRemoteVideo({userID:t,streamType:s})},_debugToggleRemoteAudio(e){const t=e.currentTarget.dataset.userID,s=e.currentTarget.dataset.streamType;this.data.streamList.find((e=>e.userID===t&&e.streamType===s)).muteAudio?this.subscribeRemoteAudio({userID:t}):this.unsubscribeRemoteAudio({userID:t})},_debugToggleVideoDebug(){this.setData({debug:!this.data.debug})},_debugExitRoom(){this.exitRoom()},_debugEnterRoom(){Object.assign(this.data.pusher,this.data.config),this.enterRoom({roomID:this.data.config.roomID}).then((()=>{setTimeout((()=>{this.publishLocalVideo(),this.publishLocalAudio()}),2e3)}))},_debugGoBack(){wx.navigateBack({delta:1})},_debugTogglePanel(){this.setData({debugPanel:!this.data.debugPanel})},_debugSendRandomMessage(){const e=this.getRemoteUserList();if(0===e.length||!this.tim)return!1;const t=this.data.config.roomID,s=`Hello! ${e[0].userID} ${9999*Math.random()}`,a=e[0].userID;this.sendC2CTextMessage({userID:a,message:s});const i=this.sendGroupTextMessage({roomID:t,message:s});this._pushMessageList({name:a,message:s}),i.then((function(e){wx.showToast({title:"发送成功",icon:"success",duration:1e3})})).catch((function(e){console.warn(TAG_NAME,"_debugSendRandomMessage error",e),wx.showToast({title:"发送失败",icon:"none",duration:1e3})}))},_toggleAudioVolumeType(){"voicecall"===this.data.pusher.audioVolumeType?this._setPusherConfig({audioVolumeType:"media"}):this._setPusherConfig({audioVolumeType:"voicecall"})},_toggleSoundMode(){if(0===this.data.userList.length)return;const e=this.userController.getStream({userID:this.data.userList[0].userID,streamType:"main"});e&&("speaker"===e.soundMode?e.soundMode="ear":e.soundMode="speaker",this._setPlayerConfig({userID:e.userID,streamType:"main",config:{soundMode:e.soundMode}}))},_hangUp(){this.exitRoom(),wx.navigateBack({delta:1})},handleSubscribeAudio(){this.data.pusher.enableMic?this.unpublishLocalAudio():this.publishLocalAudio()},_handleSubscribeRemoteVideo(e){const t=e.currentTarget.dataset.userID,s=e.currentTarget.dataset.streamType;this.data.streamList.find((e=>e.userID===t&&e.streamType===s)).muteVideo?this.subscribeRemoteVideo({userID:t,streamType:s}):this.unsubscribeRemoteVideo({userID:t,streamType:s})},_handleSubscribeRemoteAudio(e){const t=e.currentTarget.dataset.userID,s=e.currentTarget.dataset.streamType;this.data.streamList.find((e=>e.userID===t&&e.streamType===s)).muteAudio?this.subscribeRemoteAudio({userID:t}):this.unsubscribeRemoteAudio({userID:t})},_switchMemberListPanel(){this.setData({panelName:"memberlist-panel"!==this.data.panelName?"memberlist-panel":""})},_switchSettingPanel(){this.setData({panelName:"setting-panel"!==this.data.panelName?"setting-panel":""})},_switchBGMPanel(){this.setData({panelName:"bgm-panel"!==this.data.panelName?"bgm-panel":""})},_handleMaskerClick(){this.setData({panelName:""})},_setPuserProperty(e){const t=e.currentTarget.dataset.key,s=e.currentTarget.dataset.valueType;let a=e.currentTarget.dataset.value;const i={};"boolean"===s&&(a="true"===a,i[t]=!this.data.pusher[t]),"number"===s&&a.indexOf("|")>0&&(a=a.split("|"),this.data.pusher[t]===Number(a[0])?i[t]=Number(a[1]):i[t]=Number(a[0])),"string"===s&&a.indexOf("|")>0&&(a=a.split("|"),this.data.pusher[t]===a[0]?i[t]=a[1]:i[t]=a[0]),this._setPusherConfig(i)},_setPlayerProperty(e){const t=e.currentTarget.dataset.userid,s=e.currentTarget.dataset.streamtype,a=e.currentTarget.dataset.key;let i=e.currentTarget.dataset.value;const r=this.userController.getStream({userID:t,streamType:s});if(!r)return;const o={};"true"===i?i=!0:"false"===i&&(i=!1),"boolean"==typeof i?o[a]=!r[a]:"string"==typeof i&&i.indexOf("|")>0&&(i=i.split("|"),r[a]===i[0]?o[a]=i[1]:o[a]=i[0]),this._setPlayerConfig({userID:t,streamType:s,config:o})},_changeProperty(e){const t=e.currentTarget.dataset.propertyName,s={};s[t]=e.detail.value,this.setData(s);const a=s[t]/100;switch(t){case"MICVolume":this.setMICVolume({volume:a});break;case"BGMVolume":this.setBGMVolume({volume:a})}},_switchStreamType(e){const t=e.currentTarget.dataset.userid,s=e.currentTarget.dataset.streamtype,a=this.userController.getStream({userID:t,streamType:s});a&&"main"===a.streamType&&("small"===a._definitionType?this.subscribeRemoteVideo({userID:t,streamType:"main"}):this.subscribeRemoteVideo({userID:t,streamType:"small"}))},_handleSnapshotClick(e){wx.showToast({title:"开始截屏",icon:"none",duration:1e3});const t=e.currentTarget.dataset.userid,s=e.currentTarget.dataset.streamtype;this.snapshot({userID:t,streamType:s})},_gridBindEvent(){this.on(EVENT.REMOTE_AUDIO_VOLUME_UPDATE,(e=>{const t=e.data,s=t.currentTarget.dataset.userid,a=t.currentTarget.dataset.streamtype,i=t.detail.volume,r=this.userController.getStream({userID:s,streamType:"aux"===a?"main":a});r&&(r.volume=i),this.setData({streamList:this.data.streamList,visibleStreamList:this._filterVisibleStream(this.data.streamList,!0)},(()=>{}))})),this.on(EVENT.BGM_PLAY_PROGRESS,(e=>{const t=e.data.detail.progress/e.data.detail.duration*100;this.setData({BGMProgress:t})})),this.on(EVENT.LOCAL_AUDIO_VOLUME_UPDATE,(e=>{const t=e.data.detail.volume;this._setPusherConfig({volume:t},!0)}))},_handleGridTouchStart(e){touchX=e.changedTouches[0].clientX,touchY=e.changedTouches[0].clientY},_handleGridTouchEnd(e){const t=e.changedTouches[0].clientX,s=e.changedTouches[0].clientY;t-touchX>50&&Math.abs(s-touchY)<50?this._gridPagePrev():t-touchX<-50&&Math.abs(s-touchY)<50&&this._gridPageNext()},_gridPageToPrev(e){const t=this._filterGridPageVisibleStream(e);if(this.data.gridPagePlaceholderStreamList.length!==this.data.gridPlayerPerPage)return t;this.data.gridCurrentPage--,this._gridPageToPrev(e)},_gridPageNext(){this.data.gridCurrentPage++,this.data.gridCurrentPage>this.data.gridPageCount&&(this.data.gridCurrentPage=1),this._gridPageSetData()},_gridPagePrev(){this.data.gridCurrentPage--,this.data.gridCurrentPage<1&&(this.data.gridCurrentPage=this.data.gridPageCount),this._gridPageSetData()},_gridPageSetData(){this._gridShowPageTips();const e=this._filterVisibleStream(this.data.streamList);this.setData({gridCurrentPage:this.data.gridCurrentPage,gridPageCount:this.data.gridPageCount,visibleStreamList:e,streamList:this.data.streamList,gridPagePlaceholderStreamList:this.data.gridPagePlaceholderStreamList},(()=>{}))},_gridShowPageTips(e){this.data.gridPageCount<2||(this.data.hasGridPageTipsShow&&clearTimeout(this.data.hasGridPageTipsShow),this.animate(".pages-container",[{opacity:1}],100,(()=>{})),this.data.hasGridPageTipsShow=setTimeout((()=>{this.animate(".pages-container",[{opacity:1},{opacity:.3}],600,(()=>{}))}),3e3))},_toggleFullscreen(e){const t=e.currentTarget.dataset.userID,s=e.currentTarget.dataset.streamType;if(this._isFullscreen)this.exitFullscreen({userID:t,streamType:s}).then((()=>{this._isFullscreen=!1})).catch((()=>{}));else{const e=0;this.enterFullscreen({userID:t,streamType:s,direction:e}).then((()=>{this._isFullscreen=!0})).catch((()=>{}))}},_toggleMoreMenu(){this.setData({isShowMoreMenu:!this.data.isShowMoreMenu})},_toggleIMPanel(){this.data.enableIM||wx.showToast({icon:"none",title:"当前没有开启IM功能,请设置 enableIM:true"}),this.setData({showIMPanel:!this.data.showIMPanel})},_handleBGMOperation(e){const t=e.currentTarget.dataset.operationName;this[t]&&this[t]({url:"https://web.sdk.qcloud.com/trtc/miniapp/asset/bgm-test.mp3"})},_selectBeautyStyle:function(e){const t=e.detail.value;this.setData({beautyStyle:t},(()=>{this._setPusherConfig({beautyLevel:"close"===t?0:9,beautyStyle:"close"===t?"smooth":t})}))},_selectFilter:function(e){const t=parseInt(e.detail.value);this.setData({filterIndex:t},(()=>{this._setPusherConfig({filter:this.data.filterArray[t].value})}))},_selectAudioReverbType:function(e){const t=parseInt(e.detail.value);this._setPusherConfig({audioReverbType:t})},_sendIMMessage(e){if(!this.data.messageContent)return;const t=this.data.config.roomID,s=this.data.messageContent,a=this.data.config.userID;this.sendGroupTextMessage({roomID:t,message:s}),this._pushMessageList({name:a,message:s}),this.setData({messageContent:""})},_inputIMMessage(e){this.setData({messageContent:e.detail.value})},_pushMessageList(e){this.data.messageList.length===this.data.maxMessageListLength&&this.data.messageList.shift(),this.data.messageList.push(e),this.setData({messageList:this.data.messageList,messageListScrollTop:100*this.data.messageList.length},(()=>{}))}}});