@bddh/starling-realtime-client
Version:
2 lines (1 loc) • 5.82 kB
JavaScript
import{createClass as t,classCallCheck as o,defineProperty as e}from"./_virtual/_rollupPluginBabelHelpers.js";import n from"@bddh/starling-brtc";import{TimeoutController as c}from"@bddh/starling-timeout-controller";import a from"@bddh/starling-json";import i from"lodash/isEmpty";import r from"lodash/isFunction";import{statusEnum as l,errorTypeEnum as s}from"./interface.js";var d=t((function t(d){var m=this;o(this,t),e(this,"client",void 0),e(this,"isRtcVideoOn",!1),e(this,"rtcConnection",void 0),e(this,"rtcConfig",void 0),e(this,"bizConfig",void 0),e(this,"rtcCallback",void 0),e(this,"restarting",!1),e(this,"timeoutEvent",void 0),e(this,"reopenTimer",void 0),e(this,"restartCount",0),e(this,"closeLog",!0),e(this,"haveVideoOn",!1),e(this,"rtcStateCallback",null),e(this,"openRtc",(function(t){if(m.restarting)!m.closeLog&&console.warn("rtc restarting");else if(t&&(m.rtcConnection=t),m.rtcConnection&&!i(m.rtcConnection)){var o=m.rtcConnection,e=o.server,c=o.appId,d=o.token,u=o.roomName,C=o.userId,f=m.rtcCallback||{},b=f.onRemotedata,g=f.onLocalStream,v=f.onRemoteVideoComing,R=f.onLocalVideoPublished,h=f.onLocalVideoPublishing,L=f.onRemoteVideoOn,S=f.onRemoteVideoOff,k=f.onRemoteVideoLoading,y=f.onSuccess,p=f.onError,V=f.onLocalVideoConnected,E=f.onRemoteVideoConnected,_=f.onRemoteMediaState,O=f.onPlayingError,I=f.onDigitalHumanCallback;m.timeoutEvent.start(),m.rtcStateCallback=function(t){var o=t.body,e=t.action;I&&I({status:l.DH_LIB_FULL_STATUS,content:{type:"rtcState",action:e,body:o}})},m.client=new n({roomName:u,token:d,userId:C,appId:c,server:e,rtcConfig:m.rtcConfig,callback:{onRemoteVideoConnected:function(o,e){if(!e&&!m.haveVideoOn)var n=setTimeout((function(){m.restartCount<3?(m.restartCount=++m.restartCount,m.restartRtc&&m.restartRtc(t)):(m.restartCount=0,m.closeRtc()),clearTimeout(n)}),500);if(e){var c=document.getElementById("remotevideo500");c.muted=!1;var a=c.play();void 0!==a&&a.catch((function(){c.muted=!0,c.play().catch((function(t){!m.closeLog&&console.info("videoElement play error",t),O&&O(c),I&&I({status:l.DH_LIB_ERROR,content:{type:s.LOCAL_VIDEO_ERROR}})})),I&&I({status:l.DH_LIB_STATUS,content:{type:s.LOCAL_VIDEO_MUTED}})}))}!m.closeLog&&console.info("remoteVideoConnected",o,e,m.restartCount),m.rtcStateCallback&&m.rtcStateCallback({action:"remoteVideoConnected",body:{id:o,connected:e,restartCount:m.restartCount}}),r(E)&&E(o,e)},onRemoteVideoOn:function(t){m.haveVideoOn=!0,!m.closeLog&&console.info("remotevideoon by id: ",t),m.timeoutEvent.clear();var o=document.querySelector("#starlingRtcContainer"),e=o&&o.lastChild;e&&(e.style.display="none"),m.rtcStateCallback&&m.rtcStateCallback({action:"remotevideoon",body:t}),I&&I({status:l.DH_LIB_OPEN}),r(L)&&L(t)},onRemoteMediaState:function(t,o,e){!m.closeLog&&console.info("remoteMediaState",t,o,e),m.rtcStateCallback&&m.rtcStateCallback({action:"remoteMediaState",body:{id:t,medium:o,connected:e}}),r(_)&&_(t,o,e)},onLocalVideoConnected:function(t){!m.closeLog&&console.info("localVideoConnected",t),m.rtcStateCallback&&m.rtcStateCallback({action:"localVideoConnected",body:{connected:t}}),r(V)&&V(t)},onRemotedata:function(t){if(m.rtcConfig&&m.rtcConfig.usingdatachannel){var o=a.parse(t);!m.closeLog&&console.info("RTC - receive:",o),r(b)&&b(o)}},onLocalStream:function(t,o){!m.closeLog&&console.info("rtc - onlocalstream by name: "+o),m.rtcStateCallback&&m.rtcStateCallback({action:"onlocalstream",body:{stream:t,name:o}}),r(g)&&g(t,o)},onLocalVideoPublished:function(){!m.closeLog&&console.info("LocalVideoPublished 本地视频发布成功"),m.rtcStateCallback&&m.rtcStateCallback({action:"localvideopublished_ok",body:!0}),r(R)&&R()},onLocalVideoPublishing:function(){!m.closeLog&&console.info("LocalVideoPublishing 本地视频发布成功"),m.rtcStateCallback&&m.rtcStateCallback({action:"localvideopublishing",body:!0}),r(h)&&h()},onRemoteVideoComing:function(t){!m.closeLog&&console.info("remotevideocoming",t),m.rtcStateCallback&&m.rtcStateCallback({action:"remotevideocoming",body:t}),r(v)&&v(t)},onRemoteVideoLoading:function(t){!m.closeLog&&console.info("remotevideoloading by id: ",t),m.rtcStateCallback&&m.rtcStateCallback({action:"remotevideoloading",body:t}),r(k)&&k(t)},onRemoteVideoOff:function(t){var o=document.getElementById("remotevideo500");o&&o.pause(),!m.closeLog&&console.info("remotevideooff: ",t),m.isRtcVideoOn=!1,m.rtcStateCallback&&m.rtcStateCallback({action:"remotevideooff",body:t}),I&&I({status:l.DH_LIB_CLOSE}),r(S)&&S(t),m.timeoutEvent.start()},onSuccess:function(){!m.closeLog&&console.info("rtc - success"),m.rtcStateCallback&&m.rtcStateCallback({action:"success",body:!0}),r(y)&&y()},onError:function(o){m.isRtcVideoOn=!1,!m.closeLog&&console.error("rtc - error:",o),m.rtcStateCallback&&m.rtcStateCallback({action:"error",body:o}),I&&I({status:l.DH_LIB_ERROR,content:{type:s.RTC_ERROR}}),r(p)&&p(o),(o.includes("Lost connection to the server")||o.includes("Is the server down"))&&m.restartRtc&&m.restartRtc(t)}}})}})),e(this,"closeRtc",(function(){m.timeoutEvent.clear(),clearTimeout(m.reopenTimer),m.client&&m.client.destroy(),m.restarting=!1})),e(this,"restartRtc",(function(t){!m.restarting&&m.bizConfig&&m.bizConfig.autoReConnect&&(m.rtcStateCallback&&m.rtcStateCallback({action:"restartRtc",body:t}),m.restarting=!0,m.closeRtc(),m.reopenTimer=setTimeout((function(){!m.closeLog&&console.error("拉流失败,开始重启"),m.restarting=!1,m.openRtc(t)}),2e3))}));var u=d.rtcConnection,C=d.rtcConfig,f=d.closeLog,b=void 0===f||f,g=d.rtcCallback;this.rtcConnection=u,this.rtcConfig=C,this.rtcCallback=g,this.closeLog=b,this.timeoutEvent=new c(6e4,(function(){if(!m.rtcConnection)return!m.closeLog&&console.warn("remotevideoon事件超时, rtcConnectionRef is null");m.timeoutEvent.clear(),m.restartRtc&&m.restartRtc(m.rtcConnection)}))}));export{d as default};