UNPKG

@bddh/starling-realtime-client

Version:

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