UNPKG

@bddh/starling-realtime-client

Version:

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