UNPKG

@bddh/starling-realtime-client

Version:

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