UNPKG

@bddh/starling-realtime-client

Version:

2 lines (1 loc) 5.77 kB
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 d from"lodash/pick";import{v4 as u}from"uuid";import{statusEnum as m,errorTypeEnum as l,CONNECT_MAIN_KEY as h}from"./interface.js";import p from"./Server.js";import v from"./BrtcClient.js";import{createHumanDom as g,appendRtcDomStyle as S,RTC_VIDEO_ID as f,checkIsHuawei as C,playHumanVideo as I,muteHumanVideo as b,removeHumanDom as k,DEFAULT_CANVAS_ID as y}from"./DomControl.js";import R from"./Cutout.js";var w=e((function e(w){var D=this;t(this,e),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,"onRemoteVideoComing",(function(e){D.rtcConnectParams&&D.rtcConnectParams.feedId&&e===+D.rtcConnectParams.feedId&&D.rtcServer&&D.rtcServer.client&&D.rtcServer.client.subscribeStreaming("remotevideo500",+D.rtcConnectParams.feedId)})),r(this,"onRemoteVideoOn",(function(){setTimeout(a(o().mark((function e(){var t,r,n,a,s;return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=document.getElementById(f),e.next=3,D.cutoutInstance&&D.cutoutInstance.updateSource(f);case 3:t&&(t.setAttribute("data-playing","true"),r=t.videoWidth,n=t.videoHeight,a=document.getElementById(y),s=a.getContext("2d"),a&&s&&(a.width=r,a.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&&D.onDigitalHumanCallback&&D.onDigitalHumanCallback({status:m.DH_LIB_WS_SUCCESS,content:e}),0===t){var a=s.parse(r),o=a.rtcServerUrl,i=a.appId,c=a.clientToken,d=a.roomName,u=a.clientId,h=a.feedId,p=a.chargeId;i&&d&&(D.rtcConnectParams={server:D.rtcServerUrl||o,appId:i,token:c,roomName:d,userId:u,feedId:h,chargeId:p},D.rtcServer&&D.rtcServer.openRtc(D.rtcConnectParams))}else D.onDigitalHumanCallback&&D.onDigitalHumanCallback({status:m.DH_LIB_ERROR,content:{type:l.CONNECT_ERROR,msg:{code:t,errMsg:n}}})})),r(this,"createServer",(function(e){D.dhServer&&D.dhServer.createSocket(n(n(n({token:D.token,appKey:D.appKey,appId:D.appId},e?{sessionId:e}:{}),d(D.connectParams,h)),{},{parameters:c(D.connectParams,h),onConnect:D.connectListener,onDigitalHumanCallback:D.onDigitalHumanCallback}))})),r(this,"checkHumanInstansce",(function(){D.dhServer||console.warn("请先初始化数字人示例")})),r(this,"sendMessage",(function(e,t){return D.dhServer&&D.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){D.dhServer&&D.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;D.checkHumanInstansce(),D.dhServer&&D.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,a=s.parse(r).first;D.checkHumanInstansce(),D.dhServer&&D.dhServer.sendMessage({requestId:t||u(),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;D.checkHumanInstansce(),D.dhServer&&D.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;D.checkHumanInstansce(),D.dhServer&&D.dhServer.sendMessage({requestId:t||u(),action:"AUDIO_STREAM_RENDER",body:r,clientTs:(new Date).getTime()},n)})),r(this,"destory",a(o().mark((function e(){return o().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,D.dhServer&&D.dhServer.closeSocket();case 2:D.rtcServer&&D.rtcServer.closeRtc(),k(),D.dhServer=null,D.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 b(!0)})),r(this,"unMuteHuman",(function(){var e=b(!1);try{return D.isHuawei?(e.pause(),e.play()):e.paused&&e.play()}catch(e){console.info(e)}}));var H=w.wrapperId,T=w.token,E=w.appKey,P=w.appId,_=w.connectParams,M=w.renderParams,U=w.rtcConnectParams,x=w.brtcParams,K=w.wsUrl,L=w.rtcServerUrl,N=w.onDigitalHumanCallback;this.token=T,this.appKey=E,this.appId=P,this.rtcServerUrl=L,this.connectParams=n({autoChromaKey:M.autoChromaKey},_);var j=M.fullStatus,q=M.closeLog;this.onDigitalHumanCallback=function(e){e.status!==m.DH_LIB_FULL_STATUS?N&&N(e):j&&N&&N(e)},this.dhServer=new p(K||"wss://open.xiling.baidu.com/cloud/digital-human-demonstration");var B=(g({autoChromaKey:!!M.autoChromaKey})||{}).playerWrapper,A=document.getElementById(H);A||console.error("dom with wrapperId is not find"),B&&A&&A.appendChild(B),S(),this.rtcServer=new v({closeLog:q,videoWrapperId:f,rtcConfig:n({remotevideoviewid:H,showvideobps:!1,usingvideo:!1,usingaudio:!1,aspublisher:!1,usingdatachannel:!0,autoplaymuted:!1},x),bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback}}),this.cutoutInstance=new R({sourceId:f}),i(U)||(this.rtcConnectParams=U,this.rtcServer.openRtc(U))}));export{w as default};