@bddh/starling-realtime-client
Version:
2 lines (1 loc) • 14.2 kB
JavaScript
import{createClass as e,classCallCheck as t,defineProperty as r,objectSpread2 as n,asyncToGenerator as a,regeneratorRuntime as o}from"./_virtual/_rollupPluginBabelHelpers.js";import s from"@bddh/starling-json";import i from"lodash/isEmpty";import c from"lodash/omit";import u from"lodash/pick";import{v4 as l}from"uuid";import{statusEnum as d,errorTypeEnum as h,CONNECT_MAIN_KEY as m}from"./interface.js";import v from"./Server.js";import p from"./BrtcClient.js";import{removeLastFrameCoverWrapper as f,removeHumanDom as S,createHumanDom as C,appendLastFrameCoverSibling as g,appendRtcDomStyle as R,RTC_VIDEO_ID as y,checkIsHuawei as k,setHumanPlayerWrapperVisible as I,setLastFrameCoverVisible as b,captureLastFrameToCover as w,playHumanVideo as D,muteHumanVideo as E,ensureCutoutDomMounted as M}from"./DomControl.js";import H from"./Cutout.js";import P from"./RecordController.js";var x=function(x,_,T){function O(e){var i,p=this;t(this,O),r(this,"token",void 0),r(this,"appKey",void 0),r(this,"appId",void 0),r(this,"connectParams",void 0),r(this,"onDigitalHumanCallback",void 0),r(this,"dhServer",void 0),r(this,"rtcServer",void 0),r(this,"rtcConnectParams",void 0),r(this,"cutoutInstance",void 0),r(this,"hasInitChromaEffects",!1),r(this,"isHuawei",k()),r(this,"rtcServerUrl",void 0),r(this,"rtcInternalIp",void 0),r(this,"recordController",null),r(this,"wsReadyState",-1),r(this,"isPreOpenSession",!1),r(this,"openWsOnly",!1),r(this,"hasConnectUserFigureEffects",!1),r(this,"_isMicMuted",!1),r(this,"mergeConnectRtcExtras",!1),r(this,"emitRawStatusMessage",!1),r(this,"resolveMediaServerByDomain",void 0),r(this,"remoteAutoPlay",!0),r(this,"onDigitalHumanCallbackPlus",(function(e){var t,r,n=e.content;e.status===d.DH_LIB_FULL_STATUS&&"wsState"===(null==n?void 0:n.type)&&(p.wsReadyState=n.readyState,3===n.readyState&&(null===(t=p.cutoutInstance)||void 0===t||t.setPlaying(!1),null===(r=p.rtcServer)||void 0===r||r.closeRtc(),p.canShowStaticFrameCover()&&(I(!1),b(!0))));p.onDigitalHumanCallback&&p.onDigitalHumanCallback(e)})),r(this,"onRemoteVideoComing",(function(e){p.rtcConnectParams&&p.rtcConnectParams.feedId&&e===+p.rtcConnectParams.feedId&&p.rtcServer&&p.rtcServer.client&&p.rtcServer.client.subscribeStreaming("remotevideo500",+p.rtcConnectParams.feedId)})),r(this,"onLocalStream",(function(){(p.connectParams||{}).pullAudioFromRtc&&p.rtcServer&&p.rtcServer.muteMicphone(p._isMicMuted)})),r(this,"onRemoteVideoOn",(function(){setTimeout(a(o().mark((function e(){var t;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t=document.getElementById(y),!p.cutoutInstance||!t){e.next=5;break}return p.cutoutInstance.setPlaying(!0),e.next=5,p.cutoutInstance.updateSource(t);case 5:p.canShowStaticFrameCover()&&(w(),I(!0),b(!1));case 6:case"end":return e.stop()}}),e)}))),100)})),r(this,"onRemoteVideoConnected",(function(e,t){var r;if(t&&!p.remoteAutoPlay){var n=null===(r=p.rtcConnectParams)||void 0===r?void 0:r.feedId;null!=n&&e!==+n||p.onRemoteVideoOn()}})),r(this,"onRemoteVideoOffLastFrame",(function(){var e;null===(e=p.cutoutInstance)||void 0===e||e.setPlaying(!1),p.canShowStaticFrameCover()&&b(!0)})),r(this,"connectListener",(function(e){p.handleConnectResponse(e).catch((function(e){console.warn("[DhRealtimeHuman] handleConnectResponse failed",e)}))})),r(this,"createServer",(function(e){var t,r=n(n(n({token:p.token,appKey:p.appKey,appId:p.appId},e?{sessionId:e}:{}),u(p.connectParams,m)),{},{parameters:c(p.connectParams,m),onConnect:p.connectListener,onDigitalHumanCallback:p.onDigitalHumanCallbackPlus});null!==(t=p.dhServer)&&void 0!==t&&t.preConnectSuccess?(p.dhServer.connectParams=r,p.dhServer&&p.dhServer.sendPreConnect(r)):p.openWsOnly?p.dhServer&&p.dhServer.sendConnect(r):p.dhServer&&p.dhServer.createSocket(r)})),r(this,"checkHumanInstansce",(function(){return p.dhServer?!(p.isPreOpenSession&&!p.dhServer.preConnectSuccess)||(p.onDigitalHumanCallback&&p.onDigitalHumanCallback({status:d.DH_LIB_ERROR,content:{type:h.CONNECT_ERROR,msg:{code:4001,errMsg:"webSocket连接中"}}}),!1):(p.onDigitalHumanCallback&&p.onDigitalHumanCallback({status:d.DH_LIB_ERROR,content:{type:h.CONNECT_ERROR,msg:{code:4002,errMsg:"请先初始化数字人实例"}}}),!1)})),r(this,"sendMessage",(function(e,t){return p.dhServer&&p.dhServer.sendMessage(e,t)})),r(this,"interrupt",a(o().mark((function e(){return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",new Promise((function(e){p.dhServer&&p.dhServer.sendMessage({requestId:l(),action:"TEXT_RENDER",body:"<interrupt></interrupt>",clientTs:(new Date).getTime()},(function(){return e()}))})));case 1:case"end":return e.stop()}}),e)})))),r(this,"textRender",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg;p.checkHumanInstansce()&&p.dhServer&&p.dhServer.sendMessage({requestId:t||l(),action:"TEXT_RENDER",body:r,clientTs:(new Date).getTime()},n)})),r(this,"textStreamRender",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg,a=s.parse(r).first;p.checkHumanInstansce()&&p.dhServer&&p.dhServer.sendMessage({requestId:t||l(),action:"TEXT_STREAM_RENDER",body:r,clientTs:(new Date).getTime()},a?n:null)})),r(this,"audioRender",(function(e){var t=e.body,r=e.onCallbackMsg;p.checkHumanInstansce()&&p.dhServer&&p.dhServer.sendMessage({requestId:l(),action:"AUDIO_RENDER",body:t,clientTs:(new Date).getTime()},r)})),r(this,"audioStreamRender",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg;p.checkHumanInstansce()&&p.dhServer&&p.dhServer.sendMessage({requestId:t||l(),action:"AUDIO_STREAM_RENDER",body:r,clientTs:(new Date).getTime()},n)})),r(this,"textQuery",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg;p.dhServer&&p.dhServer.sendMessage({requestId:t||l(),action:"TEXT_QUERY",body:r,clientTs:(new Date).getTime()},n)})),r(this,"handleDestroyView",(function(e){var t,r;return e?p.canShowStaticFrameCover()?(D(!1),I(!1),void b(!0)):void(null===(t=p.cutoutInstance)||void 0===t||t.setPlaying(!1)):(null===(r=p.cutoutInstance)||void 0===r||r.destroy(),p.cutoutInstance=void 0,S(),void f())})),r(this,"destroy",(function(e){return(i=i||a(o().mark((function e(t){var r,n,a;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(t||{}).keepLastFrame,a=void 0!==n&&n,p.handleDestroyView(a),e.next=4,p.dhServer&&p.dhServer.closeSocket();case 4:p.rtcServer&&p.rtcServer.closeRtc(),null===(r=p.recordController)||void 0===r||r.destroy(),p.dhServer=null,p.rtcServer=null;case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)})),r(this,"pauseHuman",(function(){return D(!1)})),r(this,"playHuman",(function(){return D(!0)})),r(this,"muteHuman",(function(){return E(!0)})),r(this,"unMuteHuman",(function(){var e=E(!1);try{return p.isHuawei?(e.pause(),e.play()):e.paused&&e.play()}catch(e){console.info(e)}}));var C=e.isPreOpenSession,g=void 0!==C&&C,R=e.isOpenWsOnly,M=void 0!==R&&R,H=e.wsUrl,P=e.renderParams,x=e.checkHeartbeatMismatch,_=e.emitRawStatusMessage,T=void 0!==_&&_,A=e.onDigitalHumanCallback;this.isPreOpenSession=g,this.openWsOnly=M,this.emitRawStatusMessage=T,this.dhServer=new v(H||"wss://open.xiling.baidu.com/cloud/digital-human-demonstration",{checkHeartbeatMismatch:x,emitRawStatusMessage:T}),this.onDigitalHumanCallback=function(e){e.status!==d.DH_LIB_FULL_STATUS?A&&A(e):(null==P?void 0:P.fullStatus)&&A&&A(e)},g||M?this.dhServer.createSocket({onDigitalHumanCallback:this.onDigitalHumanCallbackPlus},g):this.init(e)}return e(O,[{key:"canShowStaticFrameCover",value:function(){return!this.hasInitChromaEffects&&!this.hasConnectUserFigureEffects}},{key:"init",value:function(e){var t,r,a=this,o=e.wrapperId,s=e.token,c=e.appKey,u=e.appId,l=e.connectParams,m=e.renderParams,v=void 0===m?{}:m,k=e.rtcConnectParams,I=e.brtcParams,b=e.rtcServerUrl,w=e.rtcInternalIp,D=e.mergeConnectRtcExtras,E=void 0!==D&&D,M=e.emitRawStatusMessage,x=void 0!==M&&M,_=e.remoteAutoPlay,T=e.resolveMediaServerByDomain;if(this.mergeConnectRtcExtras=Boolean(E),this.emitRawStatusMessage=x,null===(t=this.dhServer)||void 0===t||t.setEmitRawStatusMessage(this.emitRawStatusMessage),this.resolveMediaServerByDomain=T,this.remoteAutoPlay=!1!==_,this.hasConnectUserFigureEffects=!1,this.token=s,this.appKey=c,this.appId=u,this.rtcServerUrl=b,this.rtcInternalIp=w,this.connectParams=n({autoChromaKey:(null==v?void 0:v.autoChromaKey)||!1},l),!u&&!c&&!s)throw new Error("appId and appKey or token is required");var O=v.closeLog,A=v.audio3AConstraints;this.hasInitChromaEffects=!i(null==v?void 0:v.chromaEffects)||!(null==v||!v.autoChromaKey),f(),S();var F=(C({autoChromaKey:!!v.autoChromaKey||!i(v.chromaEffects),autoplay:!1!==_})||{}).playerWrapper,U=document.getElementById(o||"");U||console.error("dom with wrapperId is not find"),F&&U&&U.appendChild(F),this.canShowStaticFrameCover()&&U&&("static"===window.getComputedStyle(U).position&&(U.style.position="relative"),g(U)),R();var B=n({remotevideoviewid:o,showvideobps:!1,usingvideo:!1,usingaudio:!(null==l||!l.pullAudioFromRtc),aspublisher:!(null==l||!l.pullAudioFromRtc),autopublish:!(null==l||!l.pullAudioFromRtc),usingdatachannel:!0,autoplaymuted:!1},I);this.rtcServer=new p({closeLog:O,videoWrapperId:y,rtcConfig:B,bizConfig:n({autoReConnect:null===(r=null==l?void 0:l.reConnect)||void 0===r||r},void 0!==_?{remoteAutoPlay:_}:{}),rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback,onLocalStream:this.onLocalStream,onRemoteVideoOn:this.onRemoteVideoOn,onRemoteVideoOff:this.onRemoteVideoOffLastFrame,onRemoteVideoConnected:this.onRemoteVideoConnected}}),(!i(null==v?void 0:v.chromaEffects)||null!=v&&v.autoChromaKey)&&(this.cutoutInstance=new H({sourceId:y,effects:null==v?void 0:v.chromaEffects})),i(k)||(this.rtcConnectParams=k,this.rtcServer.openRtc(k)),null!=l&&l.pullAudioFromRtc?this.recordController=null:this.recordController=new P({pickAudioMode:null==l?void 0:l.pickAudioMode,audio3AConstraints:A,asrSample:null==l?void 0:l.asrSample,onAudioData:function(e){1===a.wsReadyState&&a.sendMessage(e,null)},onError:function(e){a.onDigitalHumanCallback&&a.onDigitalHumanCallback({status:d.DH_LIB_ERROR,content:{type:h.CONNECT_ERROR,msg:{code:-1,errMsg:e.message}}})}})}},{key:"handleConnectResponse",value:function(e){return(x=x||a(o().mark((function e(t){var r,n,a,i,c,u,l,m,v,p,f;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.code,n=t.body,a=t.message,0===r&&this.onDigitalHumanCallback&&this.onDigitalHumanCallback({status:d.DH_LIB_WS_SUCCESS,content:t}),this.emitRawStatusMessage&&this.onDigitalHumanCallback&&this.onDigitalHumanCallback({status:d.DH_LIB_MESSAGE,content:t}),0!==r){e.next=18;break}if(i=s.parse(n),!this.mergeConnectRtcExtras||!this.rtcServer){e.next=14;break}return e.prev=6,e.next=9,this.mergeConnectRtcExtrasFromBody(i);case 9:e.next=14;break;case 11:e.prev=11,e.t0=e.catch(6),console.warn("[DhRealtimeHuman] mergeConnectRtcExtrasFromBody failed",e.t0);case 14:c=i.rtcServerUrl,u=i.appId,l=i.clientToken,m=i.roomName,v=i.clientId,p=i.feedId,f=i.chargeId,u&&m&&(this.rtcConnectParams={server:this.rtcServerUrl||c,appId:u,token:l,roomName:m,userId:v,feedId:p,chargeId:f},this.rtcServer&&this.rtcServer.openRtc(this.rtcConnectParams)),e.next=19;break;case 18:this.onDigitalHumanCallback&&this.onDigitalHumanCallback({status:d.DH_LIB_ERROR,content:{type:h.CONNECT_ERROR,msg:{code:r,errMsg:a}}});case 19:case"end":return e.stop()}}),e,this,[[6,11]])})))).apply(this,arguments)}},{key:"mergeConnectRtcExtrasFromBody",value:function(e){return(_=_||a(o().mark((function e(t){var r,n,a,i,c,u,l;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.rtcServer){e.next=2;break}return e.abrupt("return");case 2:if(r=t.userFigure,n=void 0===r?"":r,a=t.rtcExternalInfo,i=(void 0===a?{}:a).udpDomain||"",c={},this.rtcInternalIp&&(c.mediaserverip=this.rtcInternalIp),!n){e.next=19;break}if(!(u=s.parse(n)).effects){e.next=17;break}return e.prev=9,e.next=12,this.applyConnectChroma(u.effects);case 12:e.next=17;break;case 14:e.prev=14,e.t0=e.catch(9),console.warn("[DhRealtimeHuman] applyConnectChroma failed",e.t0);case 17:e.next=36;break;case 19:if(!i){e.next=36;break}if(l=this.resolveMediaServerByDomain){e.next=26;break}console.warn("[DhRealtimeHuman] udpDomain 解析需传入 resolveMediaServerByDomain"),c.mediaserverip="",e.next=36;break;case 26:return e.prev=26,e.next=29,l(i);case 29:c.mediaserverip=e.sent,e.next=36;break;case 32:e.prev=32,e.t1=e.catch(26),c.mediaserverip="",console.error("resolveMediaServerByDomain failed",e.t1);case 36:this.rtcServer.mergeRtcConfig(c);case 37:case"end":return e.stop()}}),e,this,[[9,14],[26,32]])})))).apply(this,arguments)}},{key:"applyConnectChroma",value:function(e){return(T=T||a(o().mark((function e(t){var r,n,a,s;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!this.hasInitChromaEffects){e.next=2;break}return e.abrupt("return");case 2:if(n=t.chromaKey,a=t.version,null!=n){e.next=5;break}return e.abrupt("return");case 5:if(M()){e.next=7;break}return e.abrupt("return");case 7:if(this.hasConnectUserFigureEffects=!0,f(),b(!1),I(!0),null===(r=this.cutoutInstance)||void 0===r||r.destroy(),this.cutoutInstance=new H({sourceId:y,effects:{version:a,chromaKey:n}}),!(s=document.getElementById(y))){e.next=17;break}return e.next=17,this.cutoutInstance.updateSource(s);case 17:case"end":return e.stop()}}),e,this)})))).apply(this,arguments)}},{key:"startRecord",value:function(){var e;null!==(e=this.connectParams)&&void 0!==e&&e.pullAudioFromRtc?this.rtcServer&&this.rtcServer.muteMicphone(!1):this.recordController&&this.recordController.start()}},{key:"stopRecord",value:function(){var e;if(null!==(e=this.connectParams)&&void 0!==e&&e.pullAudioFromRtc)return this.rtcServer&&this.rtcServer.muteMicphone(!0),void this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:l(),clientTs:Date.now(),body:""},null);this.recordController&&this.recordController.stop(),this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:l(),clientTs:Date.now(),body:""},null)}},{key:"muteMicrophone",value:function(e){var t,r;null!==(t=this.connectParams)&&void 0!==t&&t.pullAudioFromRtc&&(this._isMicMuted=e,null===(r=this.rtcServer)||void 0===r||r.muteMicphone(e))}}]),O}();export{x as default};