UNPKG

@bddh/starling-realtime-client

Version:

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