@bddh/starling-realtime-client 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/BrtcClient.js +1 -1
- package/cjs/Client.d.ts +1 -1
- package/cjs/Client.js +1 -1
- package/cjs/Cutout.js +1 -1
- package/cjs/Server.js +1 -1
- package/es/BrtcClient.js +1 -1
- package/es/Client.d.ts +1 -1
- package/es/Client.js +1 -1
- package/es/Cutout.js +1 -1
- package/es/Server.js +1 -1
- package/package.json +2 -2
package/cjs/BrtcClient.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@bddh/starling-brtc"),e=require("@bddh/starling-timeout-controller"),o=require("@bddh/starling-json"),i=require("lodash/isEmpty"),n=require("lodash/isFunction"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@bddh/starling-brtc"),e=require("@bddh/starling-timeout-controller"),o=require("@bddh/starling-json"),i=require("lodash/isEmpty"),n=require("lodash/isFunction"),c=require("./interface.js");exports.default=class{client;isRtcVideoOn=!1;rtcConnection;rtcConfig;bizConfig;rtcCallback;restarting=!1;timeoutEvent;reopenTimer;restartCount=0;closeLog=!0;haveVideoOn=!1;rtcStateCallback=null;constructor(t){const{rtcConnection:o,rtcConfig:i,closeLog:n=!0,rtcCallback:c}=t;this.rtcConnection=o,this.rtcConfig=i,this.rtcCallback=c,this.closeLog=n,this.timeoutEvent=new e.TimeoutController(6e4,(()=>{if(!this.rtcConnection)return!this.closeLog&&console.warn("remotevideoon事件超时, rtcConnectionRef is null");this.timeoutEvent.clear(),this.restartRtc&&this.restartRtc(this.rtcConnection)}))}openRtc=e=>{if(this.restarting)!this.closeLog&&console.warn("rtc restarting");else if(e&&(this.rtcConnection=e),this.rtcConnection&&!i(this.rtcConnection)){const{server:i,appId:s,token:a,roomName:r,userId:l}=this.rtcConnection,{onRemotedata:h,onLocalStream:d,onRemoteVideoComing:u,onLocalVideoPublished:C,onLocalVideoPublishing:m,onRemoteVideoOn:b,onRemoteVideoOff:g,onRemoteVideoLoading:R,onSuccess:L,onError:S,onLocalVideoConnected:f,onRemoteVideoConnected:k,onRemoteMediaState:y,onPlayingError:v,onDigitalHumanCallback:E}=this.rtcCallback||{};this.timeoutEvent.start(),this.rtcStateCallback=t=>{const{body:e,action:o}=t;E&&E({status:c.statusEnum.DH_LIB_FULL_STATUS,content:{type:"rtcState",action:o,body:e}})},this.client=new t({roomName:r,token:a,userId:l,appId:s,server:i,rtcConfig:this.rtcConfig,callback:{onRemoteVideoConnected:(t,o)=>{if(!o&&!this.haveVideoOn){let t=setTimeout((()=>{this.restartCount<3?(this.restartCount=++this.restartCount,this.restartRtc&&this.restartRtc(e)):(this.restartCount=0,this.closeRtc()),clearTimeout(t)}),500)}if(o){const t=document.getElementById("remotevideo500");t.muted=!1;let e=t.play();void 0!==e&&e.catch((()=>{t.muted=!0,t.play().catch((e=>{!this.closeLog&&console.info("videoElement play error",e),v&&v(t),E&&E({status:c.statusEnum.DH_LIB_ERROR,content:{type:c.errorTypeEnum.LOCAL_VIDEO_ERROR}})})),E&&E({status:c.statusEnum.DH_LIB_STATUS,content:{type:c.errorTypeEnum.LOCAL_VIDEO_MUTED}})}))}!this.closeLog&&console.info("remoteVideoConnected",t,o,this.restartCount),this.rtcStateCallback&&this.rtcStateCallback({action:"remoteVideoConnected",body:{id:t,connected:o,restartCount:this.restartCount}}),n(k)&&k(t,o)},onRemoteVideoOn:t=>{this.haveVideoOn=!0,!this.closeLog&&console.info("remotevideoon by id: ",t),this.timeoutEvent.clear();const e=document.querySelector("#starlingRtcContainer"),o=e&&e.lastChild;o&&(o.style.display="none"),this.rtcStateCallback&&this.rtcStateCallback({action:"remotevideoon",body:t}),E&&E({status:c.statusEnum.DH_LIB_OPEN}),n(b)&&b(t)},onRemoteMediaState:(t,e,o)=>{!this.closeLog&&console.info("remoteMediaState",t,e,o),this.rtcStateCallback&&this.rtcStateCallback({action:"remoteMediaState",body:{id:t,medium:e,connected:o}}),n(y)&&y(t,e,o)},onLocalVideoConnected:t=>{!this.closeLog&&console.info("localVideoConnected",t),this.rtcStateCallback&&this.rtcStateCallback({action:"localVideoConnected",body:{connected:t}}),n(f)&&f(t)},onRemotedata:t=>{if(this.rtcConfig&&this.rtcConfig.usingdatachannel){const e=o.parse(t);!this.closeLog&&console.info("RTC - receive:",e),n(h)&&h(e)}},onLocalStream:(t,e)=>{!this.closeLog&&console.info("rtc - onlocalstream by name: "+e),this.rtcStateCallback&&this.rtcStateCallback({action:"onlocalstream",body:{stream:t,name:e}}),n(d)&&d(t,e)},onLocalVideoPublished:()=>{!this.closeLog&&console.info("LocalVideoPublished 本地视频发布成功"),this.rtcStateCallback&&this.rtcStateCallback({action:"localvideopublished_ok",body:!0}),n(C)&&C()},onLocalVideoPublishing:()=>{!this.closeLog&&console.info("LocalVideoPublishing 本地视频发布成功"),this.rtcStateCallback&&this.rtcStateCallback({action:"localvideopublishing",body:!0}),n(m)&&m()},onRemoteVideoComing:t=>{!this.closeLog&&console.info("remotevideocoming",t),this.rtcStateCallback&&this.rtcStateCallback({action:"remotevideocoming",body:t}),n(u)&&u(t)},onRemoteVideoLoading:t=>{!this.closeLog&&console.info("remotevideoloading by id: ",t),this.rtcStateCallback&&this.rtcStateCallback({action:"remotevideoloading",body:t}),n(R)&&R(t)},onRemoteVideoOff:t=>{!this.closeLog&&console.info("remotevideooff: ",t),this.isRtcVideoOn=!1,this.rtcStateCallback&&this.rtcStateCallback({action:"remotevideooff",body:t}),E&&E({status:c.statusEnum.DH_LIB_CLOSE}),n(g)&&g(t),this.timeoutEvent.start()},onSuccess:()=>{!this.closeLog&&console.info("rtc - success"),this.rtcStateCallback&&this.rtcStateCallback({action:"success",body:!0}),n(L)&&L()},onError:t=>{this.isRtcVideoOn=!1,!this.closeLog&&console.error("rtc - error:",t),this.rtcStateCallback&&this.rtcStateCallback({action:"error",body:t}),E&&E({status:c.statusEnum.DH_LIB_ERROR,content:{type:c.errorTypeEnum.RTC_ERROR}}),n(S)&&S(t),(t.includes("Lost connection to the server")||t.includes("Is the server down"))&&this.restartRtc&&this.restartRtc(e)}}})}};closeRtc=()=>{this.timeoutEvent.clear(),clearTimeout(this.reopenTimer),this.client&&this.client.destroy(),this.restarting=!1};restartRtc=t=>{!this.restarting&&this.bizConfig&&this.bizConfig.autoReConnect&&(this.rtcStateCallback&&this.rtcStateCallback({action:"restartRtc",body:t}),this.restarting=!0,this.closeRtc(),this.reopenTimer=setTimeout((()=>{!this.closeLog&&console.error("拉流失败,开始重启"),this.restarting=!1,this.openRtc(t)}),2e3))}};
|
package/cjs/Client.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export default class DhRealtimeHuman {
|
|
|
23
23
|
connectListener: (data: ConnectDataType) => void;
|
|
24
24
|
createServer: (sessionId?: string) => void;
|
|
25
25
|
checkHumanInstansce: () => void;
|
|
26
|
-
sendMessage: (message: DHServerInterface, listener: RenderCallback | null) => Promise<void> |
|
|
26
|
+
sendMessage: (message: DHServerInterface, listener: RenderCallback | null) => Promise<void> | null;
|
|
27
27
|
interrupt: () => Promise<void>;
|
|
28
28
|
textRender: (renderData: {
|
|
29
29
|
requestId: string;
|
package/cjs/Client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@bddh/starling-json"),t=require("lodash/isEmpty"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@bddh/starling-json"),t=require("lodash/isEmpty"),r=require("lodash/omit"),s=require("lodash/pick"),a=require("uuid"),n=require("./interface.js"),i=require("./Server.js"),o=require("./BrtcClient.js"),c=require("./DomControl.js"),d=require("./Cutout.js");exports.default=class{token;appKey;appId;connectParams;onDigitalHumanCallback;dhServer;rtcServer;rtcConnectParams;cutoutInstance;isHuawei=c.checkIsHuawei();rtcServerUrl;constructor(e){const{wrapperId:r,token:s,appKey:a,appId:u,connectParams:h,renderParams:l,rtcConnectParams:m,brtcParams:p,wsUrl:I,rtcServerUrl:v,onDigitalHumanCallback:g}=e;this.token=s,this.appKey=a,this.appId=u,this.rtcServerUrl=v,this.connectParams={autoChromaKey:l.autoChromaKey,...h};const{fullStatus:S,closeLog:C}=l;this.onDigitalHumanCallback=e=>{const{status:t}=e;t!==n.statusEnum.DH_LIB_FULL_STATUS?g&&g(e):S&&g&&g(e)},this.dhServer=new i.default(I||"wss://open.xiling.baidu.com/cloud/digital-human-demonstration");const{playerWrapper:y}=c.createHumanDom({autoChromaKey:!!l.autoChromaKey})||{},D=document.getElementById(r);D||console.error("dom with wrapperId is not find"),y&&D&&D.appendChild(y),this.rtcServer=new o.default({closeLog:C,videoWrapperId:c.RTC_VIDEO_ID,rtcConfig:{remotevideoviewid:r,showvideobps:!1,usingvideo:!1,usingaudio:!1,aspublisher:!1,usingdatachannel:!0,autoplaymuted:!1,...p},bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback}}),this.cutoutInstance=new d.default({sourceId:c.RTC_VIDEO_ID}),t(m)||(this.rtcConnectParams=m,this.rtcServer.openRtc(m))}onRemoteVideoComing=e=>{this.rtcConnectParams&&this.rtcConnectParams.feedId&&e===+this.rtcConnectParams.feedId&&this.rtcServer&&this.rtcServer.client&&this.rtcServer.client.subscribeStreaming("remotevideo500",+this.rtcConnectParams.feedId)};onRemoteVideoOn=()=>{setTimeout((async()=>{const e=document.getElementById(c.RTC_VIDEO_ID);if(await(this.cutoutInstance&&this.cutoutInstance.updateSource(c.RTC_VIDEO_ID)),e){e.setAttribute("data-playing","true");const{videoWidth:t,videoHeight:r}=e,s=document.getElementById(c.DEFAULT_CANVAS_ID),a=s.getContext("2d");s&&a&&(s.width=t,s.height=r,a.drawImage(e,0,0,t,r))}}),100)};connectListener=t=>{const{code:r,body:s,message:a}=t;if(0===r&&this.onDigitalHumanCallback&&this.onDigitalHumanCallback({status:n.statusEnum.DH_LIB_WS_SUCCESS,content:t}),0===r){const t=e.parse(s),{rtcServerUrl:r,appId:a,clientToken:n,roomName:i,clientId:o,feedId:c,chargeId:d}=t;a&&i&&(this.rtcConnectParams={server:this.rtcServerUrl||r,appId:a,token:n,roomName:i,userId:o,feedId:c,chargeId:d},this.rtcServer&&this.rtcServer.openRtc(this.rtcConnectParams))}else this.onDigitalHumanCallback&&this.onDigitalHumanCallback({status:n.statusEnum.DH_LIB_ERROR,content:{type:n.errorTypeEnum.CONNECT_ERROR,msg:{code:r,errMsg:a}}})};createServer=e=>{this.dhServer&&this.dhServer.createSocket({token:this.token,appKey:this.appKey,appId:this.appId,...e?{sessionId:e}:{},...s(this.connectParams,n.CONNECT_MAIN_KEY),parameters:r(this.connectParams,n.CONNECT_MAIN_KEY),onConnect:this.connectListener,onDigitalHumanCallback:this.onDigitalHumanCallback})};checkHumanInstansce=()=>{this.dhServer||console.warn("请先初始化数字人示例")};sendMessage=(e,t)=>this.dhServer&&this.dhServer.sendMessage(e,t);interrupt=async()=>new Promise((e=>{this.dhServer&&this.dhServer.sendMessage({requestId:a.v4(),action:"TEXT_RENDER",body:"<interrupt></interrupt>",clientTs:(new Date).getTime()},(()=>e()))}));textRender=e=>{const{requestId:t,body:r,onCallbackMsg:s}=e;this.checkHumanInstansce(),this.dhServer&&this.dhServer.sendMessage({requestId:t||a.v4(),action:"TEXT_RENDER",body:r,clientTs:(new Date).getTime()},s)};textStreamRender=t=>{const{requestId:r,body:s,onCallbackMsg:n}=t,{first:i}=e.parse(s);this.checkHumanInstansce(),this.dhServer&&this.dhServer.sendMessage({requestId:r||a.v4(),action:"TEXT_STREAM_RENDER",body:s,clientTs:(new Date).getTime()},i?n:null)};audioRender=e=>{const{body:t,onCallbackMsg:r}=e;this.checkHumanInstansce(),this.dhServer&&this.dhServer.sendMessage({requestId:a.v4(),action:"AUDIO_RENDER",body:t,clientTs:(new Date).getTime()},r)};audioStreamRender=e=>{const{requestId:t,body:r,onCallbackMsg:s}=e;this.checkHumanInstansce(),this.dhServer&&this.dhServer.sendMessage({requestId:t||a.v4(),action:"AUDIO_STREAM_RENDER",body:r,clientTs:(new Date).getTime()},s)};destory=async()=>{await(this.dhServer&&this.dhServer.closeSocket()),this.rtcServer&&this.rtcServer.closeRtc(),c.removeHumanDom(),this.dhServer=null,this.rtcServer=null};pauseHuman=()=>c.playHumanVideo(!1);playHuman=()=>c.playHumanVideo(!0);muteHuman=()=>c.muteHumanVideo(!0);unMuteHuman=()=>{const e=c.muteHumanVideo(!1);try{return this.isHuawei?(e.pause(),e.play()):e.paused&&e.play()}catch(e){console.info(e)}}};
|
package/cjs/Cutout.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@bddh/starling-cutout/es/Seriously");const e={version:2,chromaKey:{screen:[53,186,123],similarity:90,edgeShrink:3,smoothness:80,spill:100,opacity:1,contrast:0,brightness:0,gamma:0}};exports.DEFAULT_EFFECTS=e,exports.default=class{cutoutInstance;constructor(s){const{effects:o,sourceId:r}=s;this.cutoutInstance=new t("starling-cutout-canvas",r||"starling-cutout-video",o
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@bddh/starling-cutout/es/Seriously");const e={version:2,chromaKey:{screen:[53,186,123],similarity:90,edgeShrink:3,smoothness:80,spill:100,opacity:1,contrast:0,brightness:0,gamma:0}};exports.DEFAULT_EFFECTS=e,exports.default=class{cutoutInstance;constructor(s){const{effects:o,sourceId:r}=s;this.cutoutInstance=new t("starling-cutout-canvas",r||"starling-cutout-video",o&&o.chromaKey||e.chromaKey,o&&o.version||e.version)}setOptions(t){t&&this.cutoutInstance&&this.cutoutInstance.setOptions(t)}destroy(){return this.cutoutInstance.destroy()}updateSource(t){return this.cutoutInstance.updateSource(t)}};
|
package/cjs/Server.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("uuid"),t=require("@bddh/starling-web-socket/es/create-web-socket"),s=require("lodash/pick"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("uuid"),t=require("@bddh/starling-web-socket/es/create-web-socket"),s=require("lodash/pick"),r=require("lodash/omit"),o=require("./interface.js");exports.default=class{dhServerSocket=null;socketClosingPromise=null;url;firstConnect=!0;connectParams=null;constructor(e){this.url=e}createSocket=async s=>{this.connectParams=s;const{token:r,sessionId:n,onConnect:i,onDigitalHumanCallback:c}=s;return this.socketClosingPromise&&await this.socketClosingPromise,this.dhServerSocket&&await this.closeSocket(),new Promise(((s,a)=>{const h=t.createWebSocket(this.url,{reopen:!0,keepAlive:"heartbeat",combine:(e,t)=>e&&e.requestId===t&&t.requestId,onOpen:()=>{if(this.dhServerSocket=h,n&&this.firstConnect)return h.request({action:"RE_CONNECT",requestId:e.v4(),body:JSON.stringify({token:r,sessionId:n})}).on(((e,t)=>{i(e),0!==e.code&&(t(),this.handleConnect()),s(h)})),void(this.firstConnect=!1);this.handleConnect()},onClose:()=>{this.dhServerSocket=null,this.socketClosingPromise=null},onError:e=>{a(e)}});h.onReadyStateChange((e=>{0===e&&c&&c({status:o.statusEnum.DH_LIB_INIT}),c&&c({status:o.statusEnum.DH_LIB_FULL_STATUS,content:{type:"wsState",readyState:e,body:o.WS_STATUS_MAP[e]}})})),h.onMessage((e=>{const{action:t}=e;[o.errorTypeEnum.TIMEOUT_EXIT,o.errorTypeEnum.DISCONNECT_ALERT].includes(t)&&c&&c({status:o.statusEnum.DH_LIB_STATUS,content:{type:t}})})),h.open()}))};closeSocket=async()=>{this.dhServerSocket&&(this.socketClosingPromise=this.dhServerSocket.close(),await this.socketClosingPromise,this.dhServerSocket=null)};handleConnect=async()=>{if(!this.connectParams)return;const{appId:t,appKey:n,token:i,parameters:c,onConnect:a}=this.connectParams;(t||n||i)&&this.dhServerSocket&&this.dhServerSocket.request({action:"CONNECT",clientTs:(new Date).getTime(),requestId:e.v4(),body:JSON.stringify({token:i,appId:t,appKey:n,...s(this.connectParams,o.CONNECT_MAIN_KEY),parameters:{...r(c,["parameters"]),...c&&c.parameters?c.parameters:{}}})}).on(((e,t)=>{a(e),0!==e.code&&t()}))};async sendMessage(e,t){try{this.dhServerSocket&&this.dhServerSocket&&this.dhServerSocket.request(e).on((e=>{t&&t(e)}))}catch(s){t&&t({code:-11,body:null,action:"RENDER_ERROR",requestId:e.requestId,message:JSON.stringify(s)})}}};
|
package/es/BrtcClient.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"@bddh/starling-brtc";import{TimeoutController as o}from"@bddh/starling-timeout-controller";import e from"@bddh/starling-json";import i from"lodash/isEmpty";import
|
|
1
|
+
import t from"@bddh/starling-brtc";import{TimeoutController as o}from"@bddh/starling-timeout-controller";import e from"@bddh/starling-json";import i from"lodash/isEmpty";import c from"lodash/isFunction";import{statusEnum as n,errorTypeEnum as s}from"./interface.js";class a{client;isRtcVideoOn=!1;rtcConnection;rtcConfig;bizConfig;rtcCallback;restarting=!1;timeoutEvent;reopenTimer;restartCount=0;closeLog=!0;haveVideoOn=!1;rtcStateCallback=null;constructor(t){const{rtcConnection:e,rtcConfig:i,closeLog:c=!0,rtcCallback:n}=t;this.rtcConnection=e,this.rtcConfig=i,this.rtcCallback=n,this.closeLog=c,this.timeoutEvent=new o(6e4,(()=>{if(!this.rtcConnection)return!this.closeLog&&console.warn("remotevideoon事件超时, rtcConnectionRef is null");this.timeoutEvent.clear(),this.restartRtc&&this.restartRtc(this.rtcConnection)}))}openRtc=o=>{if(this.restarting)!this.closeLog&&console.warn("rtc restarting");else if(o&&(this.rtcConnection=o),this.rtcConnection&&!i(this.rtcConnection)){const{server:i,appId:a,token:r,roomName:l,userId:h}=this.rtcConnection,{onRemotedata:d,onLocalStream:m,onRemoteVideoComing:C,onLocalVideoPublished:b,onLocalVideoPublishing:g,onRemoteVideoOn:u,onRemoteVideoOff:R,onRemoteVideoLoading:f,onSuccess:L,onError:S,onLocalVideoConnected:k,onRemoteVideoConnected:y,onRemoteMediaState:V,onPlayingError:v,onDigitalHumanCallback:p}=this.rtcCallback||{};this.timeoutEvent.start(),this.rtcStateCallback=t=>{const{body:o,action:e}=t;p&&p({status:n.DH_LIB_FULL_STATUS,content:{type:"rtcState",action:e,body:o}})},this.client=new t({roomName:l,token:r,userId:h,appId:a,server:i,rtcConfig:this.rtcConfig,callback:{onRemoteVideoConnected:(t,e)=>{if(!e&&!this.haveVideoOn){let t=setTimeout((()=>{this.restartCount<3?(this.restartCount=++this.restartCount,this.restartRtc&&this.restartRtc(o)):(this.restartCount=0,this.closeRtc()),clearTimeout(t)}),500)}if(e){const t=document.getElementById("remotevideo500");t.muted=!1;let o=t.play();void 0!==o&&o.catch((()=>{t.muted=!0,t.play().catch((o=>{!this.closeLog&&console.info("videoElement play error",o),v&&v(t),p&&p({status:n.DH_LIB_ERROR,content:{type:s.LOCAL_VIDEO_ERROR}})})),p&&p({status:n.DH_LIB_STATUS,content:{type:s.LOCAL_VIDEO_MUTED}})}))}!this.closeLog&&console.info("remoteVideoConnected",t,e,this.restartCount),this.rtcStateCallback&&this.rtcStateCallback({action:"remoteVideoConnected",body:{id:t,connected:e,restartCount:this.restartCount}}),c(y)&&y(t,e)},onRemoteVideoOn:t=>{this.haveVideoOn=!0,!this.closeLog&&console.info("remotevideoon by id: ",t),this.timeoutEvent.clear();const o=document.querySelector("#starlingRtcContainer"),e=o&&o.lastChild;e&&(e.style.display="none"),this.rtcStateCallback&&this.rtcStateCallback({action:"remotevideoon",body:t}),p&&p({status:n.DH_LIB_OPEN}),c(u)&&u(t)},onRemoteMediaState:(t,o,e)=>{!this.closeLog&&console.info("remoteMediaState",t,o,e),this.rtcStateCallback&&this.rtcStateCallback({action:"remoteMediaState",body:{id:t,medium:o,connected:e}}),c(V)&&V(t,o,e)},onLocalVideoConnected:t=>{!this.closeLog&&console.info("localVideoConnected",t),this.rtcStateCallback&&this.rtcStateCallback({action:"localVideoConnected",body:{connected:t}}),c(k)&&k(t)},onRemotedata:t=>{if(this.rtcConfig&&this.rtcConfig.usingdatachannel){const o=e.parse(t);!this.closeLog&&console.info("RTC - receive:",o),c(d)&&d(o)}},onLocalStream:(t,o)=>{!this.closeLog&&console.info("rtc - onlocalstream by name: "+o),this.rtcStateCallback&&this.rtcStateCallback({action:"onlocalstream",body:{stream:t,name:o}}),c(m)&&m(t,o)},onLocalVideoPublished:()=>{!this.closeLog&&console.info("LocalVideoPublished 本地视频发布成功"),this.rtcStateCallback&&this.rtcStateCallback({action:"localvideopublished_ok",body:!0}),c(b)&&b()},onLocalVideoPublishing:()=>{!this.closeLog&&console.info("LocalVideoPublishing 本地视频发布成功"),this.rtcStateCallback&&this.rtcStateCallback({action:"localvideopublishing",body:!0}),c(g)&&g()},onRemoteVideoComing:t=>{!this.closeLog&&console.info("remotevideocoming",t),this.rtcStateCallback&&this.rtcStateCallback({action:"remotevideocoming",body:t}),c(C)&&C(t)},onRemoteVideoLoading:t=>{!this.closeLog&&console.info("remotevideoloading by id: ",t),this.rtcStateCallback&&this.rtcStateCallback({action:"remotevideoloading",body:t}),c(f)&&f(t)},onRemoteVideoOff:t=>{!this.closeLog&&console.info("remotevideooff: ",t),this.isRtcVideoOn=!1,this.rtcStateCallback&&this.rtcStateCallback({action:"remotevideooff",body:t}),p&&p({status:n.DH_LIB_CLOSE}),c(R)&&R(t),this.timeoutEvent.start()},onSuccess:()=>{!this.closeLog&&console.info("rtc - success"),this.rtcStateCallback&&this.rtcStateCallback({action:"success",body:!0}),c(L)&&L()},onError:t=>{this.isRtcVideoOn=!1,!this.closeLog&&console.error("rtc - error:",t),this.rtcStateCallback&&this.rtcStateCallback({action:"error",body:t}),p&&p({status:n.DH_LIB_ERROR,content:{type:s.RTC_ERROR}}),c(S)&&S(t),(t.includes("Lost connection to the server")||t.includes("Is the server down"))&&this.restartRtc&&this.restartRtc(o)}}})}};closeRtc=()=>{this.timeoutEvent.clear(),clearTimeout(this.reopenTimer),this.client&&this.client.destroy(),this.restarting=!1};restartRtc=t=>{!this.restarting&&this.bizConfig&&this.bizConfig.autoReConnect&&(this.rtcStateCallback&&this.rtcStateCallback({action:"restartRtc",body:t}),this.restarting=!0,this.closeRtc(),this.reopenTimer=setTimeout((()=>{!this.closeLog&&console.error("拉流失败,开始重启"),this.restarting=!1,this.openRtc(t)}),2e3))}}export{a as default};
|
package/es/Client.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export default class DhRealtimeHuman {
|
|
|
23
23
|
connectListener: (data: ConnectDataType) => void;
|
|
24
24
|
createServer: (sessionId?: string) => void;
|
|
25
25
|
checkHumanInstansce: () => void;
|
|
26
|
-
sendMessage: (message: DHServerInterface, listener: RenderCallback | null) => Promise<void> |
|
|
26
|
+
sendMessage: (message: DHServerInterface, listener: RenderCallback | null) => Promise<void> | null;
|
|
27
27
|
interrupt: () => Promise<void>;
|
|
28
28
|
textRender: (renderData: {
|
|
29
29
|
requestId: string;
|
package/es/Client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"@bddh/starling-json";import t from"lodash/isEmpty";import
|
|
1
|
+
import e from"@bddh/starling-json";import t from"lodash/isEmpty";import r from"lodash/omit";import s from"lodash/pick";import{v4 as a}from"uuid";import{statusEnum as n,errorTypeEnum as o,CONNECT_MAIN_KEY as i}from"./interface.js";import c from"./Server.js";import d from"./BrtcClient.js";import{checkIsHuawei as h,createHumanDom as m,RTC_VIDEO_ID as l,DEFAULT_CANVAS_ID as u,removeHumanDom as p,playHumanVideo as g,muteHumanVideo as S}from"./DomControl.js";import v from"./Cutout.js";class I{token;appKey;appId;connectParams;onDigitalHumanCallback;dhServer;rtcServer;rtcConnectParams;cutoutInstance;isHuawei=h();rtcServerUrl;constructor(e){const{wrapperId:r,token:s,appKey:a,appId:o,connectParams:i,renderParams:h,rtcConnectParams:u,brtcParams:p,wsUrl:g,rtcServerUrl:S,onDigitalHumanCallback:I}=e;this.token=s,this.appKey=a,this.appId=o,this.rtcServerUrl=S,this.connectParams={autoChromaKey:h.autoChromaKey,...i};const{fullStatus:C,closeLog:b}=h;this.onDigitalHumanCallback=e=>{const{status:t}=e;t!==n.DH_LIB_FULL_STATUS?I&&I(e):C&&I&&I(e)},this.dhServer=new c(g||"wss://open.xiling.baidu.com/cloud/digital-human-demonstration");const{playerWrapper:y}=m({autoChromaKey:!!h.autoChromaKey})||{},k=document.getElementById(r);k||console.error("dom with wrapperId is not find"),y&&k&&k.appendChild(y),this.rtcServer=new d({closeLog:b,videoWrapperId:l,rtcConfig:{remotevideoviewid:r,showvideobps:!1,usingvideo:!1,usingaudio:!1,aspublisher:!1,usingdatachannel:!0,autoplaymuted:!1,...p},bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback}}),this.cutoutInstance=new v({sourceId:l}),t(u)||(this.rtcConnectParams=u,this.rtcServer.openRtc(u))}onRemoteVideoComing=e=>{this.rtcConnectParams&&this.rtcConnectParams.feedId&&e===+this.rtcConnectParams.feedId&&this.rtcServer&&this.rtcServer.client&&this.rtcServer.client.subscribeStreaming("remotevideo500",+this.rtcConnectParams.feedId)};onRemoteVideoOn=()=>{setTimeout((async()=>{const e=document.getElementById(l);if(await(this.cutoutInstance&&this.cutoutInstance.updateSource(l)),e){e.setAttribute("data-playing","true");const{videoWidth:t,videoHeight:r}=e,s=document.getElementById(u),a=s.getContext("2d");s&&a&&(s.width=t,s.height=r,a.drawImage(e,0,0,t,r))}}),100)};connectListener=t=>{const{code:r,body:s,message:a}=t;if(0===r&&this.onDigitalHumanCallback&&this.onDigitalHumanCallback({status:n.DH_LIB_WS_SUCCESS,content:t}),0===r){const t=e.parse(s),{rtcServerUrl:r,appId:a,clientToken:n,roomName:o,clientId:i,feedId:c,chargeId:d}=t;a&&o&&(this.rtcConnectParams={server:this.rtcServerUrl||r,appId:a,token:n,roomName:o,userId:i,feedId:c,chargeId:d},this.rtcServer&&this.rtcServer.openRtc(this.rtcConnectParams))}else this.onDigitalHumanCallback&&this.onDigitalHumanCallback({status:n.DH_LIB_ERROR,content:{type:o.CONNECT_ERROR,msg:{code:r,errMsg:a}}})};createServer=e=>{this.dhServer&&this.dhServer.createSocket({token:this.token,appKey:this.appKey,appId:this.appId,...e?{sessionId:e}:{},...s(this.connectParams,i),parameters:r(this.connectParams,i),onConnect:this.connectListener,onDigitalHumanCallback:this.onDigitalHumanCallback})};checkHumanInstansce=()=>{this.dhServer||console.warn("请先初始化数字人示例")};sendMessage=(e,t)=>this.dhServer&&this.dhServer.sendMessage(e,t);interrupt=async()=>new Promise((e=>{this.dhServer&&this.dhServer.sendMessage({requestId:a(),action:"TEXT_RENDER",body:"<interrupt></interrupt>",clientTs:(new Date).getTime()},(()=>e()))}));textRender=e=>{const{requestId:t,body:r,onCallbackMsg:s}=e;this.checkHumanInstansce(),this.dhServer&&this.dhServer.sendMessage({requestId:t||a(),action:"TEXT_RENDER",body:r,clientTs:(new Date).getTime()},s)};textStreamRender=t=>{const{requestId:r,body:s,onCallbackMsg:n}=t,{first:o}=e.parse(s);this.checkHumanInstansce(),this.dhServer&&this.dhServer.sendMessage({requestId:r||a(),action:"TEXT_STREAM_RENDER",body:s,clientTs:(new Date).getTime()},o?n:null)};audioRender=e=>{const{body:t,onCallbackMsg:r}=e;this.checkHumanInstansce(),this.dhServer&&this.dhServer.sendMessage({requestId:a(),action:"AUDIO_RENDER",body:t,clientTs:(new Date).getTime()},r)};audioStreamRender=e=>{const{requestId:t,body:r,onCallbackMsg:s}=e;this.checkHumanInstansce(),this.dhServer&&this.dhServer.sendMessage({requestId:t||a(),action:"AUDIO_STREAM_RENDER",body:r,clientTs:(new Date).getTime()},s)};destory=async()=>{await(this.dhServer&&this.dhServer.closeSocket()),this.rtcServer&&this.rtcServer.closeRtc(),p(),this.dhServer=null,this.rtcServer=null};pauseHuman=()=>g(!1);playHuman=()=>g(!0);muteHuman=()=>S(!0);unMuteHuman=()=>{const e=S(!1);try{return this.isHuawei?(e.pause(),e.play()):e.paused&&e.play()}catch(e){console.info(e)}}}export{I as default};
|
package/es/Cutout.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"@bddh/starling-cutout/es/Seriously";const s={version:2,chromaKey:{screen:[53,186,123],similarity:90,edgeShrink:3,smoothness:80,spill:100,opacity:1,contrast:0,brightness:0,gamma:0}};class e{cutoutInstance;constructor(e){const{effects:o,sourceId:n}=e;this.cutoutInstance=new t("starling-cutout-canvas",n||"starling-cutout-video",o
|
|
1
|
+
import t from"@bddh/starling-cutout/es/Seriously";const s={version:2,chromaKey:{screen:[53,186,123],similarity:90,edgeShrink:3,smoothness:80,spill:100,opacity:1,contrast:0,brightness:0,gamma:0}};class e{cutoutInstance;constructor(e){const{effects:o,sourceId:n}=e;this.cutoutInstance=new t("starling-cutout-canvas",n||"starling-cutout-video",o&&o.chromaKey||s.chromaKey,o&&o.version||s.version)}setOptions(t){t&&this.cutoutInstance&&this.cutoutInstance.setOptions(t)}destroy(){return this.cutoutInstance.destroy()}updateSource(t){return this.cutoutInstance.updateSource(t)}}export{s as DEFAULT_EFFECTS,e as default};
|
package/es/Server.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{v4 as e}from"uuid";import{createWebSocket as t}from"@bddh/starling-web-socket/es/create-web-socket";import s from"lodash/pick";import o from"lodash/omit";import{statusEnum as n,WS_STATUS_MAP as r,errorTypeEnum as i,CONNECT_MAIN_KEY as c}from"./interface.js";class a{dhServerSocket=null;socketClosingPromise=null;url;firstConnect=!0;connectParams=null;constructor(e){this.url=e}createSocket=async s=>{this.connectParams=s;const{token:o,sessionId:c,onConnect:a,onDigitalHumanCallback:h}=s;return this.socketClosingPromise&&await this.socketClosingPromise,this.dhServerSocket&&await this.closeSocket(),new Promise(((s,d)=>{const l=t(this.url,{reopen:!0,keepAlive:"heartbeat",combine:(e,t)=>e
|
|
1
|
+
import{v4 as e}from"uuid";import{createWebSocket as t}from"@bddh/starling-web-socket/es/create-web-socket";import s from"lodash/pick";import o from"lodash/omit";import{statusEnum as n,WS_STATUS_MAP as r,errorTypeEnum as i,CONNECT_MAIN_KEY as c}from"./interface.js";class a{dhServerSocket=null;socketClosingPromise=null;url;firstConnect=!0;connectParams=null;constructor(e){this.url=e}createSocket=async s=>{this.connectParams=s;const{token:o,sessionId:c,onConnect:a,onDigitalHumanCallback:h}=s;return this.socketClosingPromise&&await this.socketClosingPromise,this.dhServerSocket&&await this.closeSocket(),new Promise(((s,d)=>{const l=t(this.url,{reopen:!0,keepAlive:"heartbeat",combine:(e,t)=>e&&e.requestId===t&&t.requestId,onOpen:()=>{if(this.dhServerSocket=l,c&&this.firstConnect)return l.request({action:"RE_CONNECT",requestId:e(),body:JSON.stringify({token:o,sessionId:c})}).on(((e,t)=>{a(e),0!==e.code&&(t(),this.handleConnect()),s(l)})),void(this.firstConnect=!1);this.handleConnect()},onClose:()=>{this.dhServerSocket=null,this.socketClosingPromise=null},onError:e=>{d(e)}});l.onReadyStateChange((e=>{0===e&&h&&h({status:n.DH_LIB_INIT}),h&&h({status:n.DH_LIB_FULL_STATUS,content:{type:"wsState",readyState:e,body:r[e]}})})),l.onMessage((e=>{const{action:t}=e;[i.TIMEOUT_EXIT,i.DISCONNECT_ALERT].includes(t)&&h&&h({status:n.DH_LIB_STATUS,content:{type:t}})})),l.open()}))};closeSocket=async()=>{this.dhServerSocket&&(this.socketClosingPromise=this.dhServerSocket.close(),await this.socketClosingPromise,this.dhServerSocket=null)};handleConnect=async()=>{if(!this.connectParams)return;const{appId:t,appKey:n,token:r,parameters:i,onConnect:a}=this.connectParams;(t||n||r)&&this.dhServerSocket&&this.dhServerSocket.request({action:"CONNECT",clientTs:(new Date).getTime(),requestId:e(),body:JSON.stringify({token:r,appId:t,appKey:n,...s(this.connectParams,c),parameters:{...o(i,["parameters"]),...i&&i.parameters?i.parameters:{}}})}).on(((e,t)=>{a(e),0!==e.code&&t()}))};async sendMessage(e,t){try{this.dhServerSocket&&this.dhServerSocket&&this.dhServerSocket.request(e).on((e=>{t&&t(e)}))}catch(s){t&&t({code:-11,body:null,action:"RENDER_ERROR",requestId:e.requestId,message:JSON.stringify(s)})}}}export{a as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bddh/starling-realtime-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"react",
|
|
6
6
|
"indexDB"
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"lodash": "^4.17.21",
|
|
45
45
|
"uuid": "^9.0.0"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "13384790efae052fbe644f33c3d6ae1089951784"
|
|
48
48
|
}
|