UNPKG

@bddh/starling-realtime-client

Version:

2 lines (1 loc) 7.61 kB
import{createClass as e,classCallCheck as t,defineProperty as r,objectSpread2 as n,asyncToGenerator as o,regeneratorRuntime as a}from"./_virtual/_rollupPluginBabelHelpers.js";import s from"@bddh/starling-json";import i from"lodash/isEmpty";import c from"lodash/omit";import d from"lodash/pick";import{v4 as u}from"uuid";import{statusEnum as l,errorTypeEnum as m,CONNECT_MAIN_KEY as h}from"./interface.js";import p from"./Server.js";import v from"./BrtcClient.js";import{createHumanDom as S,appendRtcDomStyle as f,RTC_VIDEO_ID as g,checkIsHuawei as C,playHumanVideo as I,muteHumanVideo as R,removeHumanDom as y,DEFAULT_CANVAS_ID as b}from"./DomControl.js";import k from"./Cutout.js";import D from"./RecordController.js";var w=function(){function w(e){var T=this;t(this,w),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,"isHuawei",C()),r(this,"rtcServerUrl",void 0),r(this,"recordController",null),r(this,"wsReadyState",-1),r(this,"onRemoteVideoComing",(function(e){T.rtcConnectParams&&T.rtcConnectParams.feedId&&e===+T.rtcConnectParams.feedId&&T.rtcServer&&T.rtcServer.client&&T.rtcServer.client.subscribeStreaming("remotevideo500",+T.rtcConnectParams.feedId)})),r(this,"onLocalStream",(function(){T.connectParams.pullAudioFromRtc&&T.rtcServer&&T.rtcServer.muteMicphone(!0)})),r(this,"onRemoteVideoOn",(function(){setTimeout(o(a().mark((function e(){var t,r,n,o,s;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=document.getElementById(g),e.next=3,T.cutoutInstance&&T.cutoutInstance.updateSource(g);case 3:t&&(t.setAttribute("data-playing","true"),r=t.videoWidth,n=t.videoHeight,o=document.getElementById(b),s=o.getContext("2d"),o&&s&&(o.width=r,o.height=n,s.drawImage(t,0,0,r,n)));case 4:case"end":return e.stop()}}),e)}))),100)})),r(this,"connectListener",(function(e){var t=e.code,r=e.body,n=e.message;if(0===t&&T.onDigitalHumanCallback&&T.onDigitalHumanCallback({status:l.DH_LIB_WS_SUCCESS,content:e}),0===t){var o=s.parse(r),a=o.rtcServerUrl,i=o.appId,c=o.clientToken,d=o.roomName,u=o.clientId,h=o.feedId,p=o.chargeId;i&&d&&(T.rtcConnectParams={server:T.rtcServerUrl||a,appId:i,token:c,roomName:d,userId:u,feedId:h,chargeId:p},T.rtcServer&&T.rtcServer.openRtc(T.rtcConnectParams))}else T.onDigitalHumanCallback&&T.onDigitalHumanCallback({status:l.DH_LIB_ERROR,content:{type:m.CONNECT_ERROR,msg:{code:t,errMsg:n}}})})),r(this,"createServer",(function(e){T.dhServer&&T.dhServer.createSocket(n(n(n({token:T.token,appKey:T.appKey,appId:T.appId},e?{sessionId:e}:{}),d(T.connectParams,h)),{},{parameters:c(T.connectParams,h),onConnect:T.connectListener,onDigitalHumanCallback:function(e){var t;e.status===l.DH_LIB_FULL_STATUS&&"wsState"===(null===(t=e.content)||void 0===t?void 0:t.type)&&(T.wsReadyState=e.content.readyState),T.onDigitalHumanCallback&&T.onDigitalHumanCallback(e)}}))})),r(this,"checkHumanInstansce",(function(){T.dhServer||console.warn("请先初始化数字人示例")})),r(this,"sendMessage",(function(e,t){return T.dhServer&&T.dhServer.sendMessage(e,t)})),r(this,"interrupt",o(a().mark((function e(){return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",new Promise((function(e){T.dhServer&&T.dhServer.sendMessage({requestId:u(),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;T.checkHumanInstansce(),T.dhServer&&T.dhServer.sendMessage({requestId:t||u(),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,o=s.parse(r).first;T.checkHumanInstansce(),T.dhServer&&T.dhServer.sendMessage({requestId:t||u(),action:"TEXT_STREAM_RENDER",body:r,clientTs:(new Date).getTime()},o?n:null)})),r(this,"audioRender",(function(e){var t=e.body,r=e.onCallbackMsg;T.checkHumanInstansce(),T.dhServer&&T.dhServer.sendMessage({requestId:u(),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;T.checkHumanInstansce(),T.dhServer&&T.dhServer.sendMessage({requestId:t||u(),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;T.dhServer&&T.dhServer.sendMessage({requestId:t||u(),action:"TEXT_QUERY",body:r,clientTs:(new Date).getTime()},n)})),r(this,"destory",o(a().mark((function e(){return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,T.dhServer&&T.dhServer.closeSocket();case 2:T.rtcServer&&T.rtcServer.closeRtc(),y(),T.dhServer=null,T.rtcServer=null;case 6:case"end":return e.stop()}}),e)})))),r(this,"pauseHuman",(function(){return I(!1)})),r(this,"playHuman",(function(){return I(!0)})),r(this,"muteHuman",(function(){return R(!0)})),r(this,"unMuteHuman",(function(){var e=R(!1);try{return T.isHuawei?(e.pause(),e.play()):e.paused&&e.play()}catch(e){console.info(e)}}));var E=e.wrapperId,H=e.token,_=e.appKey,M=e.appId,P=e.connectParams,U=e.renderParams,A=void 0===U?{}:U,L=e.rtcConnectParams,q=e.brtcParams,N=e.wsUrl,x=e.rtcServerUrl,O=e.onDigitalHumanCallback;if(this.token=H,this.appKey=_,this.appId=M,this.rtcServerUrl=x,this.connectParams=n({autoChromaKey:(null==A?void 0:A.autoChromaKey)||!1},P),!M&&!_&&!H)throw new Error("appId and appKey or token is required");var B=A.fullStatus,K=A.closeLog;this.onDigitalHumanCallback=function(e){e.status!==l.DH_LIB_FULL_STATUS?O&&O(e):B&&O&&O(e)},this.dhServer=new p(N||"wss://open.xiling.baidu.com/cloud/digital-human-demonstration");var j=(S({autoChromaKey:!!A.autoChromaKey||!i(A.chromaEffects)})||{}).playerWrapper,F=document.getElementById(E);F||console.error("dom with wrapperId is not find"),j&&F&&F.appendChild(j),f();var Q=n({remotevideoviewid:E,showvideobps:!1,usingvideo:!1,usingaudio:!(null==P||!P.pullAudioFromRtc),aspublisher:!(null==P||!P.pullAudioFromRtc),autopublish:!(null==P||!P.pullAudioFromRtc),usingdatachannel:!0,autoplaymuted:!1},q);this.rtcServer=new v({closeLog:K,videoWrapperId:g,rtcConfig:Q,bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback,onLocalStream:this.onLocalStream}}),this.cutoutInstance=new k({sourceId:g,effects:null==A?void 0:A.chromaEffects}),i(L)||(this.rtcConnectParams=L,this.rtcServer.openRtc(L)),null!=P&&P.pullAudioFromRtc?this.recordController=null:this.recordController=new D({pickAudioMode:null==P?void 0:P.pickAudioMode,onAudioData:function(e){1===T.wsReadyState&&T.sendMessage(e,null)},onError:function(e){T.onDigitalHumanCallback&&T.onDigitalHumanCallback({status:l.DH_LIB_ERROR,content:{type:m.CONNECT_ERROR,msg:{code:-1,errMsg:e.message}}})}})}return e(w,[{key:"startRecord",value:function(){this.connectParams.pullAudioFromRtc?this.rtcServer&&this.rtcServer.muteMicphone(!1):this.recordController&&this.recordController.start()}},{key:"stopRecord",value:function(){if(this.connectParams.pullAudioFromRtc)return this.rtcServer&&this.rtcServer.muteMicphone(!0),void this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:u(),clientTs:Date.now(),body:""},null);this.recordController&&this.recordController.stop(),this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:u(),clientTs:Date.now(),body:""},null)}}]),w}();export{w as default};