@bddh/starling-realtime-client 1.0.0-beta.1

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.
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @file lib/Brtc
3
+ * @description brtc拉流相关
4
+ * @author zhangyue49
5
+ */
6
+ import type { BRtcInstance } from '@bddh/starling-brtc';
7
+ import { ChromaEffectOptions } from '@bddh/starling-cutout';
8
+ import { CallbackMsgType } from './interface';
9
+ export interface RtcConnectionType {
10
+ server: string;
11
+ appId: string;
12
+ token: string;
13
+ roomName: string;
14
+ userId: string;
15
+ feedId?: number;
16
+ chargeId?: number;
17
+ }
18
+ interface RtcConfig {
19
+ [x: string]: any;
20
+ videoprofile?: Record<string, any>;
21
+ mediaserverip?: string;
22
+ displayname?: string;
23
+ remotevideoviewid?: string;
24
+ localvideoviewid?: string;
25
+ bitrate?: number;
26
+ showvideobps?: boolean;
27
+ usingvideo?: boolean;
28
+ usingaudio?: boolean;
29
+ aspublisher?: boolean;
30
+ usingdatachannel?: boolean;
31
+ autoplaymuted?: boolean;
32
+ autosubscribe?: boolean;
33
+ uploadVideoResolution?: {
34
+ width: number;
35
+ height: number;
36
+ };
37
+ uploadVideoBitrate?: number;
38
+ }
39
+ export interface BizConfig {
40
+ autoCheck?: boolean;
41
+ isCrash?: boolean;
42
+ showMessage?: boolean;
43
+ autoReConnect?: boolean;
44
+ autoStart?: boolean;
45
+ showLocalVideo?: boolean;
46
+ welcomeContent?: string;
47
+ autoChromaKey?: boolean;
48
+ chromaEffects?: ChromaEffectOptions;
49
+ }
50
+ export interface RtcCallback {
51
+ [x: string]: any;
52
+ onLocalStream?: (stream: MediaStream, name: string) => void;
53
+ onLocalVideoPublished?: () => void;
54
+ onRemoteVideoComing?: (id: number) => void;
55
+ onRemoteVideoOn?: (id: number) => void;
56
+ onRemoteVideoOff?: (id: number) => void;
57
+ onRemoteVideoLoading?: (id: number) => void;
58
+ onSuccess?: () => void;
59
+ onError?: (error: string) => void;
60
+ onRemoteVideoConnected?: (id: number, on: boolean) => void;
61
+ onRemoteMediaState?: (id: number, medium: MediaStream, on: boolean) => void;
62
+ onLocalVideoConnected?: (on: boolean) => void;
63
+ onPlayingError: (element: HTMLVideoElement) => void;
64
+ onRemotedata: (data: any) => void;
65
+ onLocalVideoPublishing: () => void;
66
+ onDigitalHumanCallback: (data: CallbackMsgType) => void;
67
+ }
68
+ interface BrtcClientType {
69
+ brtcParams: any;
70
+ videoWrapperId: string;
71
+ bizConfig: BizConfig;
72
+ closeLog?: boolean;
73
+ rtcConnection?: RtcConnectionType;
74
+ preConnectInfo?: RtcConnectionType;
75
+ rtcConfig?: RtcConfig;
76
+ rtcCallback?: Partial<RtcCallback>;
77
+ }
78
+ declare class BrtcClient {
79
+ client?: BRtcInstance;
80
+ isRtcVideoOn: boolean;
81
+ private readonly brtcParams?;
82
+ private rtcConnection;
83
+ private readonly rtcConfig?;
84
+ private readonly bizConfig?;
85
+ private readonly rtcCallback?;
86
+ private restarting;
87
+ private readonly timeoutEvent;
88
+ private reopenTimer;
89
+ private restartCount;
90
+ private closeLog;
91
+ private haveVideoOn;
92
+ private rtcStateCallback;
93
+ constructor(props: BrtcClientType);
94
+ openRtc: (rtcConnection: RtcConnectionType) => void;
95
+ closeRtc: () => void;
96
+ restartRtc: (rtcConnection: RtcConnectionType) => void;
97
+ }
98
+ export default BrtcClient;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@bddh/starling-brtc"),e=require("@bddh/starling-timeout-controller"),o=require("./packages/json/es/index.js"),i=require("lodash/isEmpty"),n=require("lodash/isFunction"),s=require("./interface.js");exports.default=class{client;isRtcVideoOn=!1;brtcParams;rtcConnection;rtcConfig;bizConfig;rtcCallback;restarting=!1;timeoutEvent;reopenTimer;restartCount=0;closeLog=!0;haveVideoOn=!1;rtcStateCallback=null;constructor(t){const{brtcParams:o,rtcConnection:i,rtcConfig:n,closeLog:s=!0,rtcCallback:c}=t;this.brtcParams=o,this.rtcConnection=i,this.rtcConfig=n,this.rtcCallback=c,this.closeLog=s,this.timeoutEvent=new e.TimeoutController(6e4,(()=>{if(!this.rtcConnection)return!this.closeLog&&console.warn("remotevideoon事件超时, rtcConnectionRef is null");this.timeoutEvent.clear(),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:c,token:r,roomName:a,userId:l}=this.rtcConnection,{onRemotedata:d,onLocalStream:h,onRemoteVideoComing:u,onLocalVideoPublished:m,onLocalVideoPublishing:C,onRemoteVideoOn:b,onRemoteVideoOff:g,onRemoteVideoLoading:L,onSuccess:R,onError:f,onLocalVideoConnected:y,onRemoteVideoConnected:v,onRemoteMediaState:E,onPlayingError:S,onDigitalHumanCallback:V}=this.rtcCallback||{};this.timeoutEvent.start(),this.rtcStateCallback=t=>{const{body:e,action:o}=t;V&&V({status:s.statusEnum.DH_LIB_FULL_STATUS,content:{type:"rtcState",action:o,body:e}})},this.client=new t({roomName:a,token:r,userId:l,appId:c,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?.(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&&(console.log("promise",e),e.catch((()=>{t.muted=!0,t.play().catch((e=>{!this.closeLog&&console.info("videoElement play error",e),S?.(t),V&&V({status:s.statusEnum.DH_LIB_ERROR,content:{type:s.errorTypeEnum.LOCAL_VIDEO_ERROR}})})),V&&V({status:s.statusEnum.DH_LIB_STATUS,content:{type:s.errorTypeEnum.LOCAL_VIDEO_MUTED}})})))}!this.closeLog&&console.info("remoteVideoConnected",t,o,this.restartCount),this.rtcStateCallback?.({action:"remoteVideoConnected",body:{id:t,connected:o,restartCount:this.restartCount}}),n(v)&&v(t,o)},onRemoteVideoOn:t=>{this.haveVideoOn=!0,!this.closeLog&&console.info("remotevideoon by id: ",t),this.timeoutEvent.clear();const e=document.querySelector("#starlingRtcContainer")?.lastChild;e&&(e.style.display="none"),this.rtcStateCallback?.({action:"remotevideoon",body:t}),V&&V({status:s.statusEnum.DH_LIB_OPEN}),n(b)&&b(t)},onRemoteMediaState:(t,e,o)=>{!this.closeLog&&console.info("remoteMediaState",t,e,o),this.rtcStateCallback?.({action:"remoteMediaState",body:{id:t,medium:e,connected:o}}),n(E)&&E(t,e,o)},onLocalVideoConnected:t=>{!this.closeLog&&console.info("localVideoConnected",t),this.rtcStateCallback?.({action:"localVideoConnected",body:{connected:t}}),n(y)&&y(t)},onRemotedata:t=>{if(this.rtcConfig?.usingdatachannel){const e=o.default.parse(t);!this.closeLog&&console.info("RTC - receive:",e),n(d)&&d(e)}},onLocalStream:(t,e)=>{!this.closeLog&&console.info("rtc - onlocalstream by name: "+e),this.rtcStateCallback?.({action:"onlocalstream",body:{stream:t,name:e}}),n(h)&&h(t,e)},onLocalVideoPublished:()=>{!this.closeLog&&console.info("LocalVideoPublished 本地视频发布成功"),this.rtcStateCallback?.({action:"localvideopublished_ok",body:!0}),n(m)&&m()},onLocalVideoPublishing:()=>{!this.closeLog&&console.info("LocalVideoPublishing 本地视频发布成功"),this.rtcStateCallback?.({action:"localvideopublishing",body:!0}),n(C)&&C()},onRemoteVideoComing:t=>{!this.closeLog&&console.info("remotevideocoming",t),this.rtcStateCallback?.({action:"remotevideocoming",body:t}),n(u)&&u(t)},onRemoteVideoLoading:t=>{!this.closeLog&&console.info("remotevideoloading by id: ",t),this.rtcStateCallback?.({action:"remotevideoloading",body:t}),n(L)&&L(t)},onRemoteVideoOff:t=>{!this.closeLog&&console.info("remotevideooff: ",t),this.isRtcVideoOn=!1,this.rtcStateCallback?.({action:"remotevideooff",body:t}),V&&V({status:s.statusEnum.DH_LIB_CLOSE}),n(g)&&g(t),this.timeoutEvent.start()},onSuccess:()=>{!this.closeLog&&console.info("rtc - success"),this.rtcStateCallback?.({action:"success",body:!0}),n(R)&&R()},onError:t=>{this.isRtcVideoOn=!1,!this.closeLog&&console.error("rtc - error:",t),this.rtcStateCallback?.({action:"error",body:t}),V&&V({status:s.statusEnum.DH_LIB_ERROR,content:{type:s.errorTypeEnum.RTC_ERROR}}),n(f)&&f(t),(t.includes("Lost connection to the server")||t.includes("Is the server down"))&&this.restartRtc?.(e)}}})}};closeRtc=()=>{this.timeoutEvent.clear(),clearTimeout(this.reopenTimer),this.client?.destroy(),this.restarting=!1};restartRtc=t=>{!this.restarting&&this.bizConfig?.autoReConnect&&(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))}};
@@ -0,0 +1,50 @@
1
+ /**
2
+ * @file dh-realtime-human
3
+ * @description 云渲染数字人sdk
4
+ * @author zhangyue49
5
+ */
6
+ import { DhRealtimeHumanType, ConnectDataType } from './interface';
7
+ import { DHServerInterface, RenderCallback } from './Server';
8
+ export default class DhRealtimeHuman {
9
+ private readonly token;
10
+ private readonly appKey;
11
+ private readonly appId;
12
+ private readonly connectParams;
13
+ private readonly renderParams;
14
+ private readonly onDigitalHumanCallback;
15
+ private dhServer;
16
+ private rtcServer;
17
+ private rtcConnectParams;
18
+ private readonly cutoutInstance;
19
+ private readonly isHuawei;
20
+ constructor(props: DhRealtimeHumanType);
21
+ onRemoteVideoComing: (id: number) => void;
22
+ onRemoteVideoOn: () => void;
23
+ connectListener: (data: ConnectDataType) => void;
24
+ createServer: (sessionId?: string) => void;
25
+ checkHumanInstansce: () => void;
26
+ sendMessage: (message: DHServerInterface, listener: RenderCallback | null) => Promise<void> | undefined;
27
+ textRender: (renderData: {
28
+ requestId: string;
29
+ body: string;
30
+ onCallbackMsg: RenderCallback;
31
+ }) => void;
32
+ textStreamRender: (renderData: {
33
+ requestId: string;
34
+ body: string;
35
+ onCallbackMsg: RenderCallback;
36
+ }) => void;
37
+ audioRender: (renderData: {
38
+ body: string;
39
+ onCallbackMsg: RenderCallback;
40
+ }) => void;
41
+ audioStreamRender: (renderData: {
42
+ body: string;
43
+ onCallbackMsg: RenderCallback;
44
+ }) => void;
45
+ destory: () => Promise<void>;
46
+ pauseHuman: () => void | Promise<void>;
47
+ playHuman: () => void | Promise<void>;
48
+ muteHuman: () => HTMLVideoElement;
49
+ unMuteHuman: () => false | Promise<void> | undefined;
50
+ }
package/cjs/Client.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./packages/json/es/index.js"),t=require("lodash/isEmpty"),a=require("uuid"),n=require("./interface.js"),s=require("./Server.js"),r=require("./BrtcClient.js"),o=require("./DomControl.js"),i=require("./Cutout.js");exports.default=class{token;appKey;appId;connectParams;renderParams;onDigitalHumanCallback;dhServer;rtcServer;rtcConnectParams;cutoutInstance;isHuawei=o.checkIsHuawei();constructor(e){const{wrapperId:a,token:c,appKey:d,appId:u,connectParams:m,renderParams:l,rtcConnectParams:h,onDigitalHumanCallback:p}=e;this.token=c,this.appKey=d,this.appId=u,this.connectParams=m,this.renderParams=l;const{fullStatus:I,closeLog:g}=l;this.onDigitalHumanCallback=e=>{const{status:t}=e;t!==n.statusEnum.DH_LIB_FULL_STATUS?p&&p(e):I&&p&&p(e)},this.dhServer=new s.default("wss://persona.baidu.com:8850/cloud/digital-human-demonstration");const{playerWrapper:C}=o.createHumanDom({autoChromaKey:!!l.autoChromaKey})||{},v=document.getElementById(a);v||console.error("dom with wrapperId is not find"),C&&v?.appendChild(C),this.rtcServer=new r.default({closeLog:g,brtcParams:{},videoWrapperId:o.RTC_VIDEO_ID,rtcConfig:{remotevideoviewid:a,showvideobps:!1,usingvideo:!1,usingaudio:!1,aspublisher:!1,usingdatachannel:!0,autoplaymuted:!1},bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback}}),this.cutoutInstance=new i.default({sourceId:o.RTC_VIDEO_ID}),t(h)||(this.rtcConnectParams=h,this.rtcServer.openRtc(h))}onRemoteVideoComing=e=>{this.rtcConnectParams&&this.rtcConnectParams.feedId&&e===+this.rtcConnectParams.feedId&&this.rtcServer?.client?.subscribeStreaming("remotevideo500",+this.rtcConnectParams.feedId)};onRemoteVideoOn=()=>{setTimeout((async()=>{const e=document.getElementById(o.RTC_VIDEO_ID);if(await(this.cutoutInstance?.updateSource?.(o.RTC_VIDEO_ID)),e){e.setAttribute?.("data-playing","true");const{videoWidth:t,videoHeight:a}=e,n=document.getElementById(o.DEFAULT_CANVAS_ID),s=n.getContext("2d");n&&s&&(n.width=t,n.height=a,s.drawImage(e,0,0,t,a))}}),100)};connectListener=t=>{const{code:a,body:n}=t;if(0===a){const t=e.default.parse(n),{rtcServerUrl:a,appId:s,clientToken:r,roomName:o,clientId:i,feedId:c,chargeId:d}=t;this.rtcConnectParams={server:a,appId:s,token:r,roomName:o,userId:i,feedId:c,chargeId:d},s&&o&&this.rtcServer?.openRtc(this.rtcConnectParams)}};createServer=e=>{this.dhServer?.createSocket({token:this.token,appKey:this.appKey,appId:this.appId,...e?{sessionId:e}:{},parameters:this.connectParams,onConnect:this.connectListener,onDigitalHumanCallback:this.onDigitalHumanCallback})};checkHumanInstansce=()=>{this.dhServer||console.warn("请先初始化数字人示例")};sendMessage=(e,t)=>this.dhServer?.sendMessage(e,t);textRender=e=>{const{requestId:t,body:n,onCallbackMsg:s}=e;this.checkHumanInstansce(),this.dhServer?.sendMessage({requestId:t||a.v4(),action:"TEXT_RENDER",body:n,clientTs:(new Date).getTime()},s)};textStreamRender=t=>{const{requestId:n,body:s,onCallbackMsg:r}=t,{first:o}=e.default.parse(s);this.checkHumanInstansce(),this.dhServer?.sendMessage({requestId:n||a.v4(),action:"TEXT_STREAM_RENDER",body:s,clientTs:(new Date).getTime()},o?r:null)};audioRender=e=>{const{body:t,onCallbackMsg:n}=e;this.checkHumanInstansce(),this.dhServer?.sendMessage({requestId:a.v4(),action:"AUDIO_RENDER",body:t,clientTs:(new Date).getTime()},n)};audioStreamRender=e=>{const{body:t,onCallbackMsg:n}=e;this.checkHumanInstansce(),this.dhServer?.sendMessage({requestId:a.v4(),action:"AUDIO_STREAM_RENDER",body:t,clientTs:(new Date).getTime()},n)};destory=async()=>{await(this.dhServer?.closeSocket()),this.rtcServer?.closeRtc(),o.removeHumanDom(),this.dhServer=null,this.rtcServer=null};pauseHuman=()=>o.playHumanVideo(!1);playHuman=()=>o.playHumanVideo(!0);muteHuman=()=>o.muteHumanVideo(!0);unMuteHuman=()=>{const e=o.muteHumanVideo(!1);if(!this.isHuawei)return e.paused&&e.play();e.pause(),e.play()}};
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @file lib/Cutout
3
+ * @description 视频抠绿相关
4
+ * @author zhangyue49
5
+ */
6
+ import { ChromaEffectOptions, Seriously } from '@bddh/starling-cutout';
7
+ interface CutoutType {
8
+ effects?: {
9
+ version: number;
10
+ chromaKey: Partial<ChromaEffectOptions>;
11
+ };
12
+ sourceId: string;
13
+ }
14
+ export declare const DEFAULT_EFFECTS: {
15
+ version: number;
16
+ chromaKey: {
17
+ screen: number[];
18
+ similarity: number;
19
+ edgeShrink: number;
20
+ smoothness: number;
21
+ spill: number;
22
+ opacity: number;
23
+ contrast: number;
24
+ brightness: number;
25
+ gamma: number;
26
+ };
27
+ };
28
+ declare class Cutout {
29
+ cutoutInstance: Seriously;
30
+ constructor(props: CutoutType);
31
+ setOptions(options: Partial<ChromaEffectOptions>): void;
32
+ destroy(): any;
33
+ updateSource(video: HTMLVideoElement | string): any;
34
+ }
35
+ export default Cutout;
package/cjs/Cutout.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@bddh/starling-cutout");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.Seriously("starling-cutout-canvas",r||"starling-cutout-video",o?.chromaKey||e.chromaKey,o?.version||e.version)}setOptions(t){t&&this.cutoutInstance?.setOptions(t)}destroy(){return this.cutoutInstance.destroy()}updateSource(t){return this.cutoutInstance.updateSource(t)}};
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @file DomControl
3
+ * @description 一些js创建dom的方法
4
+ */
5
+ interface CreateVideoDomOptions {
6
+ id?: string;
7
+ autoplay?: boolean;
8
+ autoChromaKey?: boolean;
9
+ }
10
+ interface CreateVideoDomResult {
11
+ playerWrapper: HTMLDivElement;
12
+ video: HTMLVideoElement;
13
+ }
14
+ export declare const DEFAULT_CANVAS_ID = "starling-cutout-canvas";
15
+ export declare const HUMAN_WRAPPER_ID = "cloud-brtc-player-wrapper";
16
+ export declare const RTC_VIDEO_ID = "remotevideo500";
17
+ export declare function createHumanDom(options: CreateVideoDomOptions): CreateVideoDomResult | null;
18
+ export declare function removeHumanDom(): void;
19
+ export declare function muteHumanVideo(muted: boolean): HTMLVideoElement;
20
+ export declare function playHumanVideo(play: boolean): void | Promise<void>;
21
+ export declare function checkIsHuawei(): boolean;
22
+ export {};
@@ -0,0 +1 @@
1
+ "use strict";const e="starling-cutout-canvas",t="cloud-brtc-player-wrapper",n="remotevideo500";exports.DEFAULT_CANVAS_ID=e,exports.HUMAN_WRAPPER_ID=t,exports.RTC_VIDEO_ID=n,exports.checkIsHuawei=function(){const e=navigator.userAgent||navigator.vendor;return/huawei/i.test(e)||/honor/i.test(e)},exports.createHumanDom=function(o){const{id:i=n,autoChromaKey:r=!1,autoplay:a=!0}=o;if(document.getElementById(i))return console.error(`DOM with id "${i}" already exists.`),null;const s=document.createElement("div");s.className=t,s.id=t,s.style.position="relative";const u=document.createElement("video");u.id=i,u.width=0,u.height=0,u.setAttribute("width","100%"),u.setAttribute("height","100%"),u.setAttribute("data-playing","true"),u.setAttribute("playsinline","");const d=document.createElement("canvas");return d.id=e,Object.assign(d.style,{position:"absolute",top:"0",left:"0",height:"100%",width:"100%",zIndex:"1",display:r?"block":"none"}),a&&u.setAttribute("autoplay",""),r&&(u.style.visibility="hidden"),s.appendChild(u),s.appendChild(d),{playerWrapper:s,video:u}},exports.muteHumanVideo=function(e){const t=document.getElementById(n);return t&&(t.muted=e),t},exports.playHumanVideo=function(e){const t=document.getElementById(n);if(t)return e?t.play():t.pause()},exports.removeHumanDom=function(){const e=document.getElementById(t);e&&e.parentNode&&e.parentNode.removeChild(e)};
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @file lib/Server
3
+ * @description ws服务逻辑处理
4
+ * @author zhangyue49
5
+ */
6
+ import { WebSocketClient } from '@bddh/starling-web-socket/es/interface';
7
+ import { CallbackMsgType, ConnectDataType, ConnectParamsType } from './interface';
8
+ export interface SteamDataType {
9
+ first: boolean;
10
+ last: boolean;
11
+ audio?: string;
12
+ body?: string;
13
+ }
14
+ export interface DHServerInterface {
15
+ action: string;
16
+ requestId: string;
17
+ clientTs: number;
18
+ body: string;
19
+ }
20
+ export interface DHErrServerInterface {
21
+ action: string;
22
+ requestId: string;
23
+ code?: number;
24
+ message?: string;
25
+ error: Error;
26
+ }
27
+ type ConnectListener = (data: ConnectDataType, off?: () => void) => void;
28
+ export interface createSocketParamsType {
29
+ token?: string;
30
+ appKey?: string;
31
+ appId?: string;
32
+ sessionId?: string;
33
+ parameters?: ConnectParamsType;
34
+ onConnect: ConnectListener;
35
+ onDigitalHumanCallback?: (data: CallbackMsgType) => void;
36
+ }
37
+ export interface RenderCallbackRes {
38
+ requestId: string;
39
+ action: string;
40
+ code: number;
41
+ body: string | null;
42
+ message: string;
43
+ }
44
+ export type RenderCallback = (res: RenderCallbackRes) => void;
45
+ declare class Server {
46
+ dhServerSocket: WebSocketClient | null;
47
+ private socketClosingPromise;
48
+ private readonly url;
49
+ private firstConnect;
50
+ private connectParams;
51
+ constructor(url: string);
52
+ createSocket: (props: createSocketParamsType) => Promise<WebSocketClient | null>;
53
+ closeSocket: () => Promise<void>;
54
+ handleConnect: () => Promise<void>;
55
+ sendMessage(message: DHServerInterface, listener: RenderCallback | null): Promise<void>;
56
+ }
57
+ export default Server;
package/cjs/Server.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("uuid"),t=require("./packages/web-socket/es/create-web-socket.js"),s=require("./interface.js");exports.default=class{dhServerSocket=null;socketClosingPromise=null;url;firstConnect=!0;connectParams=null;constructor(e){this.url=e}createSocket=async n=>{this.connectParams=n;const{token:o,sessionId:r,onConnect:c,onDigitalHumanCallback:i}=n;return this.socketClosingPromise&&await this.socketClosingPromise,this.dhServerSocket&&await this.closeSocket(),new Promise(((n,a)=>{const u=t.createWebSocket(this.url,{reopen:!0,keepAlive:"heartbeat",combine:(e,t)=>e?.requestId===t?.requestId,onOpen:()=>{if(this.dhServerSocket=u,r&&this.firstConnect)return u.request({action:"RE_CONNECT",requestId:e.v4(),body:JSON.stringify({token:o,sessionId:r})}).on(((e,t)=>{c(e),0!==e.code&&(t(),this.handleConnect()),n(u)})),void(this.firstConnect=!1);this.handleConnect()},onClose:()=>{this.dhServerSocket=null,this.socketClosingPromise=null},onError:e=>{a(e)}});u.onReadyStateChange((e=>{-1===e&&i&&i({status:s.statusEnum.DH_LIB_INIT}),i&&i({status:s.statusEnum.DH_LIB_FULL_STATUS,content:{type:"wsState",readyState:e,body:s.WS_STATUS_MAP[e]}})})),u.onMessage((e=>{const{action:t}=e;[s.errorTypeEnum.TIMEOUT_EXIT,s.errorTypeEnum.DISCONNECT_ALERT].includes(t)&&i&&i({status:s.statusEnum.DH_LIB_STATUS,content:{type:t}})})),u.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:s,token:n,parameters:o,onConnect:r}=this.connectParams;(t||s||n)&&this.dhServerSocket?.request({action:"CONNECT",clientTs:(new Date).getTime(),requestId:e.v4(),body:JSON.stringify({token:n,appId:t,appKey:s,parameters:o})}).on(((e,t)=>{r(e),0!==e.code&&t()}))};async sendMessage(e,t){try{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)})}}};
@@ -0,0 +1 @@
1
+ "use strict";function r(r,t){for(var e=0;e<t.length;e++){var n=t[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(r,u(n.key),n)}}function t(r){if(Array.isArray(r))return o(r)}function e(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function n(r,t){if(r){if("string"==typeof r)return o(r,t);var e=Object.prototype.toString.call(r).slice(8,-1);return"Object"===e&&r.constructor&&(e=r.constructor.name),"Map"===e||"Set"===e?Array.from(r):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?o(r,t):void 0}}function o(r,t){(null==t||t>r.length)&&(t=r.length);for(var e=0,n=new Array(t);e<t;e++)n[e]=r[e];return n}function a(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function i(r,t){if("object"!=typeof r||null===r)return r;var e=r[Symbol.toPrimitive];if(void 0!==e){var n=e.call(r,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(r)}function u(r){var t=i(r,"string");return"symbol"==typeof t?t:String(t)}exports.arrayLikeToArray=o,exports.arrayWithoutHoles=t,exports.classCallCheck=function(r,t){if(!(r instanceof t))throw new TypeError("Cannot call a class as a function")},exports.createClass=function(t,e,n){return e&&r(t.prototype,e),n&&r(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t},exports.createForOfIteratorHelper=function(r,t){var e="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(!e){if(Array.isArray(r)||(e=n(r))||t&&r&&"number"==typeof r.length){e&&(r=e);var o=0,a=function(){};return{s:a,n:function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}},e:function(r){throw r},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,u=!0,l=!1;return{s:function(){e=e.call(r)},n:function(){var r=e.next();return u=r.done,r},e:function(r){l=!0,i=r},f:function(){try{u||null==e.return||e.return()}finally{if(l)throw i}}}},exports.iterableToArray=e,exports.nonIterableSpread=a,exports.toConsumableArray=function(r){return t(r)||e(r)||n(r)||a()},exports.toPrimitive=i,exports.toPropertyKey=u,exports.unsupportedIterableToArray=n;
package/cjs/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @file 云渲染SDK
3
+ * @author zhangyue49
4
+ */
5
+ import HumanClient from './Client';
6
+ export default HumanClient;
7
+ export * from './interface';
package/cjs/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./Client.js"),r=require("./interface.js");exports.default=e.default,exports.WS_STATUS_MAP=r.WS_STATUS_MAP,Object.defineProperty(exports,"errorTypeEnum",{enumerable:!0,get:function(){return r.errorTypeEnum}}),Object.defineProperty(exports,"statusEnum",{enumerable:!0,get:function(){return r.statusEnum}});
@@ -0,0 +1,87 @@
1
+ import { RtcConnectionType } from './BrtcClient';
2
+ export interface ConnectDataType {
3
+ action: string;
4
+ body: string;
5
+ code: number;
6
+ message: string;
7
+ }
8
+ export interface ConnectParamsType {
9
+ figureId: string | number;
10
+ resolutionHeight: number;
11
+ resolutionWidth: number;
12
+ cameraId?: string | number;
13
+ characterOffset?: string;
14
+ pickAudioMode?: string;
15
+ autoChromaKey?: boolean;
16
+ usingVideo?: boolean;
17
+ ttsPer?: string | number;
18
+ ttsPitch?: number;
19
+ ttsSpeed?: number;
20
+ ttsVolume?: number;
21
+ ttsLan?: string;
22
+ pullAudioFromRtc?: boolean;
23
+ inactiveDisconnectSec?: number;
24
+ preAlertSec?: number;
25
+ x264BitRate?: number;
26
+ }
27
+ export interface RenderParamsType {
28
+ chromaEffects?: object;
29
+ autoChromaKey?: boolean;
30
+ closeLog?: boolean;
31
+ fullStatus?: boolean;
32
+ }
33
+ export declare enum statusEnum {
34
+ DH_LIB_INIT = "DH_LIB_INIT",
35
+ DH_LIB_WS_SUCCESS = "DH_LIB_WS_SUCCESS",
36
+ DH_LIB_OPEN = "DH_LIB_OPEN",
37
+ DH_LIB_STATUS = "DH_LIB_STATUS",
38
+ DH_LIB_CLOSE = "DH_LIB_CLOSE",
39
+ DH_LIB_ERROR = "DH_LIB_ERROR",
40
+ DH_LIB_WARNING = "DH_LIB_WARNING",
41
+ DH_LIB_FULL_STATUS = "DH_LIB_FULL_STATUS"
42
+ }
43
+ export declare enum errorTypeEnum {
44
+ CONNECT_ERROR = "CONNECT_ERROR",
45
+ DISCONNECT_ALERT = "DISCONNECT_ALERT",
46
+ TIMEOUT_EXIT = "TIMEOUT_EXIT",
47
+ LOCAL_VIDEO_MUTED = "LOCAL_VIDEO_MUTED",
48
+ LOCAL_VIDEO_ERROR = "LOCAL_VIDEO_ERROR",
49
+ RTC_ERROR = "RTC_ERROR"
50
+ }
51
+ export interface ErrorType {
52
+ type: errorTypeEnum;
53
+ msg?: {
54
+ code: string | number;
55
+ };
56
+ }
57
+ export interface RtcStateType {
58
+ action: string;
59
+ type: string;
60
+ body: any;
61
+ }
62
+ export declare const WS_STATUS_MAP: {
63
+ '-1': string;
64
+ '0': string;
65
+ '1': string;
66
+ '2': string;
67
+ '3': string;
68
+ };
69
+ export interface WsStateType {
70
+ readyState: number;
71
+ type: string;
72
+ body: string;
73
+ }
74
+ export interface CallbackMsgType {
75
+ status: statusEnum;
76
+ content?: ErrorType | WsStateType | RtcStateType;
77
+ }
78
+ export interface DhRealtimeHumanType {
79
+ token: string;
80
+ appKey?: string;
81
+ appId?: string;
82
+ wrapperId: string;
83
+ connectParams: ConnectParamsType;
84
+ renderParams: RenderParamsType;
85
+ rtcConnectParams?: RtcConnectionType;
86
+ onDigitalHumanCallback: (data: CallbackMsgType) => void;
87
+ }
@@ -0,0 +1 @@
1
+ "use strict";var _,E;exports.statusEnum=void 0,(_=exports.statusEnum||(exports.statusEnum={})).DH_LIB_INIT="DH_LIB_INIT",_.DH_LIB_WS_SUCCESS="DH_LIB_WS_SUCCESS",_.DH_LIB_OPEN="DH_LIB_OPEN",_.DH_LIB_STATUS="DH_LIB_STATUS",_.DH_LIB_CLOSE="DH_LIB_CLOSE",_.DH_LIB_ERROR="DH_LIB_ERROR",_.DH_LIB_WARNING="DH_LIB_WARNING",_.DH_LIB_FULL_STATUS="DH_LIB_FULL_STATUS",exports.errorTypeEnum=void 0,(E=exports.errorTypeEnum||(exports.errorTypeEnum={})).CONNECT_ERROR="CONNECT_ERROR",E.DISCONNECT_ALERT="DISCONNECT_ALERT",E.TIMEOUT_EXIT="TIMEOUT_EXIT",E.LOCAL_VIDEO_MUTED="LOCAL_VIDEO_MUTED",E.LOCAL_VIDEO_ERROR="LOCAL_VIDEO_ERROR",E.RTC_ERROR="RTC_ERROR";exports.WS_STATUS_MAP={"-1":"UNINSTANTIATED",0:"CONNECTING",1:"OPEN",2:"CLOSING",3:"CLOSED"};
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var r=require("../../../_virtual/_rollupPluginBabelHelpers.js"),e=require("lodash/isString"),t=require("lodash/isObject"),n=require("lodash/cloneDeep"),a=function(){function a(){r.classCallCheck(this,a)}return r.createClass(a,null,[{key:"isJsonStr",value:function(r){if(e(r))try{var n=JSON.parse(r);return!!t(n)}catch(r){return!1}return!1}},{key:"parse",value:function(r){var e={};try{e=JSON.parse(r||"{}")}catch(e){console.error("JSON.parse Error:",r)}return e||{}}},{key:"stringify",value:function(r){var e="";try{e=JSON.stringify(r||{})}catch(e){console.error("JSON.stringify error:",r)}return e||"{}"}},{key:"getParseValue",value:function(r){try{return r&&e(r)?a.parse(r):r}catch(e){return console.info("JSON parse error",r),r}}},{key:"parseObj",value:function(r,e){var t=n(r);return e.forEach((function(e){if(Object.keys(r).includes(e))try{t[e]=a.parse(r[e]),console.log(e,t[e])}catch(r){console.error('Failed to parse JSON for key "'.concat(e,'":'),r)}})),t}},{key:"stringifyObj",value:function(r,e){var t=n(r);return e.forEach((function(e){Object.keys(r).includes(e)&&(t[e]=a.stringify(r[e]))})),t}}]),a}();exports.default=a;
@@ -0,0 +1 @@
1
+ "use strict";var e=require("../../../_virtual/_rollupPluginBabelHelpers.js"),t=require("../../../node_modules/worker-timers/build/es2019/module.js"),n=require("./interface.js"),r=require("./utils.js"),o=5e3;exports.DEFAULT_RECONNECT_ATTEMPTS=10,exports.DEFAULT_RECONNECT_DELAY=o,exports.createWebSocket=function(i){var s,a=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},u=i,c=0,l=0,d=0,h=!1,v=null!==(s=null==a?void 0:a.reopenDelay)&&void 0!==s?s:o,f=!0,p=!1,E=0,y=new Set,g=new Set,S=new Set;function R(){var e;h=!1,c=0,f=!0,v=null!==(e=null==a?void 0:a.reopenDelay)&&void 0!==e?e:o}function m(e){var n=a.reopen,r=void 0===n||n,o=a.reopenAttempts,i=void 0===o?10:o,s=a.reopenDelayIncrease,u=a.onReopenAttemptEnd;if(c>=i||!r)return R(),f&&l&&t.clearTimeout(l),void("function"==typeof u&&u(f));v="function"==typeof s?s({reopenCount:c,preDelay:v,isServerClosed:f}):v,d=t.setTimeout((function(){h=!0,e.open(),c++}),v)}var _={socket:null,messages:[],get readyState(){return this.socket?this.socket.readyState:n.ReadyState.UNINSTANTIATED},get isOpened(){return n.ReadyState.OPEN===this.readyState},get isActive(){return n.ReadyState.CONNECTING===this.readyState||n.ReadyState.OPEN===this.readyState},open:function(){var e=this;if(this.isActive)return this;d&&!h&&t.clearTimeout(d);var n=this.socket=new WebSocket(u,null==a?void 0:a.protocols);return this._dispchReadyStateChangeEvent(),n.addEventListener("open",this._dispchReadyStateChangeEvent),n.addEventListener("close",this._dispchReadyStateChangeEvent),n.addEventListener("error",this._dispchReadyStateChangeEvent),n.onopen=function(r){var i=a.onOpen,s=a.onMessage,u=a.combine,c=a.reopenDelay,d=void 0===c?o:c;h&&(f?(l&&t.clearTimeout(l),l=t.setTimeout(R,d>1e3?d+100:1e3)):R()),i&&i(r),n.onmessage=function(t){s&&s(t);var n=e.deserialization(t.data);u&&y.forEach((function(e){u(e.requestData,n)&&(e.then?(e.then(n),y.delete(e)):e.on?e.on(n):e.responseData=n)})),g.forEach((function(e){return e(n)}))},e.messages.flush(),e._ping()},n.onerror=function(t){a.onError&&a.onError(t),f=!1,m(e)},n.onclose=function(r){r.isInterruptWithNewUrl=p,p=!1,E&&t.clearTimeout(E);var o=a.reopenWhenServerClosed,i=void 0===o||o,s=a.onClose,u=a.onServerClose;f&&u&&u(r),s&&s(r),e._dispose(n),f&&i&&m(e)},this},openWithNewUrl:function(e){var t;return e&&this.isActive&&e===u?this:(u=e||u,n.ReadyState.CONNECTING===this.readyState?(this._dispose(),null===(t=this.socket)||void 0===t||t.close()):this.isActive&&this.close(n.CloseEventCode.CLOSE_INTERRUPT_WITH_NEW_URL,"interrupt by new url"),this.open())},close:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1e3,r=arguments.length>1?arguments[1]:void 0;return this.socket?new Promise((function(o){var i,s;f=!1,n.CloseEventCode.CLOSE_INTERRUPT_WITH_NEW_URL===t&&(p=!0);null!==(i=e.socket)&&void 0!==i&&i.addEventListener("close",(function t(){var n;null!==(n=e.socket)&&void 0!==n&&n.removeEventListener("close",t),e._dispose(e.socket),o()})),null!==(s=e.socket)&&void 0!==s&&s.close(t,r),e._dispchReadyStateChangeEvent()})):Promise.resolve()},connect:function(){return this.open()},disconnect:function(e,t){return this.close(e,t)},send:function(e){var t=this.socket;if(!t)return this;if(n.ReadyState.CONNECTING===t.readyState)return this.messages.push(e),this;e instanceof Function&&(e=e(r.createRequestId()));var o=this.serialization(e);return t.send(o),this},request:function(e){if(null==a||!a.combine)throw new Error("Request failed! unconfigured combine, this configuration is required to use the request api.");e instanceof Function&&(e=e(r.createRequestId())),this.send(e);var t={requestData:e,responseData:null};return y.add(t),{then:function(e){if(t.on&&console.warn('You have already registered for the "on" callback.'),t.responseData){var n=e(t.responseData);return n instanceof Promise?n:Promise.resolve(n)}return new Promise((function(e){t.then=function(t){return e(t)}})).then((function(t){return e(t)}))},on:function(e){var n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];t.then&&console.warn('You have already registered for the "then" callback.'),t.on=function(n){e(n,(function(){y.delete(t)}))},t.responseData&&n&&t.on(t.responseData)}}},receive:function(){var e=this;return new Promise((function(t){var n=e.onReceive((function(e){n(),t(e)}))}))},onReceive:function(e){return g.add(e),function(){g.delete(e)}},onMessage:function(e){return this.onReceive(e)},_dispchReadyStateChangeEvent:function(){var e=this;S.forEach((function(t){return t(e.readyState)}))},_dispose:function(e){var t;(e=null!==(t=e)&&void 0!==t?t:this.socket)&&(e.removeEventListener("open",this._dispchReadyStateChangeEvent),e.removeEventListener("close",this._dispchReadyStateChangeEvent),e.removeEventListener("error",this._dispchReadyStateChangeEvent),e.onopen=null,e.onclose=null,e.onerror=null)},_ping:function(){var e=this,n=a.keepAlive,o=void 0===n?"none":n;E&&t.clearTimeout(E),"none"!==o&&this.isOpened&&(E=t.setTimeout((function(){if(e.isOpened){var t=null!=a&&a.getKeepAliveMessage?null==a?void 0:a.getKeepAliveMessage(r.createRequestId()):{requestId:r.createRequestId(),action:"heartbeat"===o?"HEART_BEAT":"PING"};"heartbeat"===a.keepAlive?(e.send(t),e._ping()):e.request(t).then((function(){e._ping()})).catch((function(e){console.error(e)}))}}),(null==a?void 0:a.keepAliveDelay)||3e3))},onReadyStateChange:function(e){return S.add(e),function(){S.delete(e)}},serialization:function(e){return"[object Object]"===Object.prototype.toString.call(e)||Array.isArray(e)||"number"==typeof e?JSON.stringify(e):e},deserialization:function(e){if("string"==typeof e){var t=e;try{t=JSON.parse(e)}catch(n){t=e}return t}return e}};return _.messages.flush=function(){var t=e.toConsumableArray(this);if(this.length=0,t.length){var n,r=e.createForOfIteratorHelper(t);try{for(r.s();!(n=r.n()).done;){var o=n.value;_.send(o)}}catch(e){r.e(e)}finally{r.f()}}},_};
@@ -0,0 +1 @@
1
+ "use strict";var e;exports.ReadyState=void 0,exports.CloseEventCode=void 0,(e=exports.ReadyState||(exports.ReadyState={}))[e.UNINSTANTIATED=-1]="UNINSTANTIATED",e[e.CONNECTING=0]="CONNECTING",e[e.OPEN=1]="OPEN",e[e.CLOSING=2]="CLOSING",e[e.CLOSED=3]="CLOSED",function(e){e[e.CLOSE_INTERRUPT_WITH_NEW_URL=4e3]="CLOSE_INTERRUPT_WITH_NEW_URL"}(exports.CloseEventCode||(exports.CloseEventCode={}));
@@ -0,0 +1 @@
1
+ "use strict";var e=require("uuid");exports.createRequestId=function(){return e.v4()};
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @file lib/Brtc
3
+ * @description brtc拉流相关
4
+ * @author zhangyue49
5
+ */
6
+ import type { BRtcInstance } from '@bddh/starling-brtc';
7
+ import { ChromaEffectOptions } from '@bddh/starling-cutout';
8
+ import { CallbackMsgType } from './interface';
9
+ export interface RtcConnectionType {
10
+ server: string;
11
+ appId: string;
12
+ token: string;
13
+ roomName: string;
14
+ userId: string;
15
+ feedId?: number;
16
+ chargeId?: number;
17
+ }
18
+ interface RtcConfig {
19
+ [x: string]: any;
20
+ videoprofile?: Record<string, any>;
21
+ mediaserverip?: string;
22
+ displayname?: string;
23
+ remotevideoviewid?: string;
24
+ localvideoviewid?: string;
25
+ bitrate?: number;
26
+ showvideobps?: boolean;
27
+ usingvideo?: boolean;
28
+ usingaudio?: boolean;
29
+ aspublisher?: boolean;
30
+ usingdatachannel?: boolean;
31
+ autoplaymuted?: boolean;
32
+ autosubscribe?: boolean;
33
+ uploadVideoResolution?: {
34
+ width: number;
35
+ height: number;
36
+ };
37
+ uploadVideoBitrate?: number;
38
+ }
39
+ export interface BizConfig {
40
+ autoCheck?: boolean;
41
+ isCrash?: boolean;
42
+ showMessage?: boolean;
43
+ autoReConnect?: boolean;
44
+ autoStart?: boolean;
45
+ showLocalVideo?: boolean;
46
+ welcomeContent?: string;
47
+ autoChromaKey?: boolean;
48
+ chromaEffects?: ChromaEffectOptions;
49
+ }
50
+ export interface RtcCallback {
51
+ [x: string]: any;
52
+ onLocalStream?: (stream: MediaStream, name: string) => void;
53
+ onLocalVideoPublished?: () => void;
54
+ onRemoteVideoComing?: (id: number) => void;
55
+ onRemoteVideoOn?: (id: number) => void;
56
+ onRemoteVideoOff?: (id: number) => void;
57
+ onRemoteVideoLoading?: (id: number) => void;
58
+ onSuccess?: () => void;
59
+ onError?: (error: string) => void;
60
+ onRemoteVideoConnected?: (id: number, on: boolean) => void;
61
+ onRemoteMediaState?: (id: number, medium: MediaStream, on: boolean) => void;
62
+ onLocalVideoConnected?: (on: boolean) => void;
63
+ onPlayingError: (element: HTMLVideoElement) => void;
64
+ onRemotedata: (data: any) => void;
65
+ onLocalVideoPublishing: () => void;
66
+ onDigitalHumanCallback: (data: CallbackMsgType) => void;
67
+ }
68
+ interface BrtcClientType {
69
+ brtcParams: any;
70
+ videoWrapperId: string;
71
+ bizConfig: BizConfig;
72
+ closeLog?: boolean;
73
+ rtcConnection?: RtcConnectionType;
74
+ preConnectInfo?: RtcConnectionType;
75
+ rtcConfig?: RtcConfig;
76
+ rtcCallback?: Partial<RtcCallback>;
77
+ }
78
+ declare class BrtcClient {
79
+ client?: BRtcInstance;
80
+ isRtcVideoOn: boolean;
81
+ private readonly brtcParams?;
82
+ private rtcConnection;
83
+ private readonly rtcConfig?;
84
+ private readonly bizConfig?;
85
+ private readonly rtcCallback?;
86
+ private restarting;
87
+ private readonly timeoutEvent;
88
+ private reopenTimer;
89
+ private restartCount;
90
+ private closeLog;
91
+ private haveVideoOn;
92
+ private rtcStateCallback;
93
+ constructor(props: BrtcClientType);
94
+ openRtc: (rtcConnection: RtcConnectionType) => void;
95
+ closeRtc: () => void;
96
+ restartRtc: (rtcConnection: RtcConnectionType) => void;
97
+ }
98
+ export default BrtcClient;
@@ -0,0 +1 @@
1
+ import t from"@bddh/starling-brtc";import{TimeoutController as o}from"@bddh/starling-timeout-controller";import e from"./packages/json/es/index.js";import i from"lodash/isEmpty";import n from"lodash/isFunction";import{statusEnum as c,errorTypeEnum as s}from"./interface.js";class a{client;isRtcVideoOn=!1;brtcParams;rtcConnection;rtcConfig;bizConfig;rtcCallback;restarting=!1;timeoutEvent;reopenTimer;restartCount=0;closeLog=!0;haveVideoOn=!1;rtcStateCallback=null;constructor(t){const{brtcParams:e,rtcConnection:i,rtcConfig:n,closeLog:c=!0,rtcCallback:s}=t;this.brtcParams=e,this.rtcConnection=i,this.rtcConfig=n,this.rtcCallback=s,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.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:d}=this.rtcConnection,{onRemotedata:h,onLocalStream:m,onRemoteVideoComing:C,onLocalVideoPublished:u,onLocalVideoPublishing:g,onRemoteVideoOn:b,onRemoteVideoOff:L,onRemoteVideoLoading:f,onSuccess:R,onError:y,onLocalVideoConnected:p,onRemoteVideoConnected:S,onRemoteMediaState:V,onPlayingError:v,onDigitalHumanCallback:k}=this.rtcCallback||{};this.timeoutEvent.start(),this.rtcStateCallback=t=>{const{body:o,action:e}=t;k&&k({status:c.DH_LIB_FULL_STATUS,content:{type:"rtcState",action:e,body:o}})},this.client=new t({roomName:l,token:r,userId:d,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?.(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&&(console.log("promise",o),o.catch((()=>{t.muted=!0,t.play().catch((o=>{!this.closeLog&&console.info("videoElement play error",o),v?.(t),k&&k({status:c.DH_LIB_ERROR,content:{type:s.LOCAL_VIDEO_ERROR}})})),k&&k({status:c.DH_LIB_STATUS,content:{type:s.LOCAL_VIDEO_MUTED}})})))}!this.closeLog&&console.info("remoteVideoConnected",t,e,this.restartCount),this.rtcStateCallback?.({action:"remoteVideoConnected",body:{id:t,connected:e,restartCount:this.restartCount}}),n(S)&&S(t,e)},onRemoteVideoOn:t=>{this.haveVideoOn=!0,!this.closeLog&&console.info("remotevideoon by id: ",t),this.timeoutEvent.clear();const o=document.querySelector("#starlingRtcContainer")?.lastChild;o&&(o.style.display="none"),this.rtcStateCallback?.({action:"remotevideoon",body:t}),k&&k({status:c.DH_LIB_OPEN}),n(b)&&b(t)},onRemoteMediaState:(t,o,e)=>{!this.closeLog&&console.info("remoteMediaState",t,o,e),this.rtcStateCallback?.({action:"remoteMediaState",body:{id:t,medium:o,connected:e}}),n(V)&&V(t,o,e)},onLocalVideoConnected:t=>{!this.closeLog&&console.info("localVideoConnected",t),this.rtcStateCallback?.({action:"localVideoConnected",body:{connected:t}}),n(p)&&p(t)},onRemotedata:t=>{if(this.rtcConfig?.usingdatachannel){const o=e.parse(t);!this.closeLog&&console.info("RTC - receive:",o),n(h)&&h(o)}},onLocalStream:(t,o)=>{!this.closeLog&&console.info("rtc - onlocalstream by name: "+o),this.rtcStateCallback?.({action:"onlocalstream",body:{stream:t,name:o}}),n(m)&&m(t,o)},onLocalVideoPublished:()=>{!this.closeLog&&console.info("LocalVideoPublished 本地视频发布成功"),this.rtcStateCallback?.({action:"localvideopublished_ok",body:!0}),n(u)&&u()},onLocalVideoPublishing:()=>{!this.closeLog&&console.info("LocalVideoPublishing 本地视频发布成功"),this.rtcStateCallback?.({action:"localvideopublishing",body:!0}),n(g)&&g()},onRemoteVideoComing:t=>{!this.closeLog&&console.info("remotevideocoming",t),this.rtcStateCallback?.({action:"remotevideocoming",body:t}),n(C)&&C(t)},onRemoteVideoLoading:t=>{!this.closeLog&&console.info("remotevideoloading by id: ",t),this.rtcStateCallback?.({action:"remotevideoloading",body:t}),n(f)&&f(t)},onRemoteVideoOff:t=>{!this.closeLog&&console.info("remotevideooff: ",t),this.isRtcVideoOn=!1,this.rtcStateCallback?.({action:"remotevideooff",body:t}),k&&k({status:c.DH_LIB_CLOSE}),n(L)&&L(t),this.timeoutEvent.start()},onSuccess:()=>{!this.closeLog&&console.info("rtc - success"),this.rtcStateCallback?.({action:"success",body:!0}),n(R)&&R()},onError:t=>{this.isRtcVideoOn=!1,!this.closeLog&&console.error("rtc - error:",t),this.rtcStateCallback?.({action:"error",body:t}),k&&k({status:c.DH_LIB_ERROR,content:{type:s.RTC_ERROR}}),n(y)&&y(t),(t.includes("Lost connection to the server")||t.includes("Is the server down"))&&this.restartRtc?.(o)}}})}};closeRtc=()=>{this.timeoutEvent.clear(),clearTimeout(this.reopenTimer),this.client?.destroy(),this.restarting=!1};restartRtc=t=>{!this.restarting&&this.bizConfig?.autoReConnect&&(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 ADDED
@@ -0,0 +1,50 @@
1
+ /**
2
+ * @file dh-realtime-human
3
+ * @description 云渲染数字人sdk
4
+ * @author zhangyue49
5
+ */
6
+ import { DhRealtimeHumanType, ConnectDataType } from './interface';
7
+ import { DHServerInterface, RenderCallback } from './Server';
8
+ export default class DhRealtimeHuman {
9
+ private readonly token;
10
+ private readonly appKey;
11
+ private readonly appId;
12
+ private readonly connectParams;
13
+ private readonly renderParams;
14
+ private readonly onDigitalHumanCallback;
15
+ private dhServer;
16
+ private rtcServer;
17
+ private rtcConnectParams;
18
+ private readonly cutoutInstance;
19
+ private readonly isHuawei;
20
+ constructor(props: DhRealtimeHumanType);
21
+ onRemoteVideoComing: (id: number) => void;
22
+ onRemoteVideoOn: () => void;
23
+ connectListener: (data: ConnectDataType) => void;
24
+ createServer: (sessionId?: string) => void;
25
+ checkHumanInstansce: () => void;
26
+ sendMessage: (message: DHServerInterface, listener: RenderCallback | null) => Promise<void> | undefined;
27
+ textRender: (renderData: {
28
+ requestId: string;
29
+ body: string;
30
+ onCallbackMsg: RenderCallback;
31
+ }) => void;
32
+ textStreamRender: (renderData: {
33
+ requestId: string;
34
+ body: string;
35
+ onCallbackMsg: RenderCallback;
36
+ }) => void;
37
+ audioRender: (renderData: {
38
+ body: string;
39
+ onCallbackMsg: RenderCallback;
40
+ }) => void;
41
+ audioStreamRender: (renderData: {
42
+ body: string;
43
+ onCallbackMsg: RenderCallback;
44
+ }) => void;
45
+ destory: () => Promise<void>;
46
+ pauseHuman: () => void | Promise<void>;
47
+ playHuman: () => void | Promise<void>;
48
+ muteHuman: () => HTMLVideoElement;
49
+ unMuteHuman: () => false | Promise<void> | undefined;
50
+ }
package/es/Client.js ADDED
@@ -0,0 +1 @@
1
+ import e from"./packages/json/es/index.js";import t from"lodash/isEmpty";import{v4 as a}from"uuid";import{statusEnum as n}from"./interface.js";import s from"./Server.js";import r from"./BrtcClient.js";import{checkIsHuawei as o,createHumanDom as i,RTC_VIDEO_ID as c,DEFAULT_CANVAS_ID as d,removeHumanDom as m,playHumanVideo as h,muteHumanVideo as u}from"./DomControl.js";import l from"./Cutout.js";class p{token;appKey;appId;connectParams;renderParams;onDigitalHumanCallback;dhServer;rtcServer;rtcConnectParams;cutoutInstance;isHuawei=o();constructor(e){const{wrapperId:a,token:o,appKey:d,appId:m,connectParams:h,renderParams:u,rtcConnectParams:p,onDigitalHumanCallback:g}=e;this.token=o,this.appKey=d,this.appId=m,this.connectParams=h,this.renderParams=u;const{fullStatus:I,closeLog:C}=u;this.onDigitalHumanCallback=e=>{const{status:t}=e;t!==n.DH_LIB_FULL_STATUS?g&&g(e):I&&g&&g(e)},this.dhServer=new s("wss://persona.baidu.com:8850/cloud/digital-human-demonstration");const{playerWrapper:S}=i({autoChromaKey:!!u.autoChromaKey})||{},v=document.getElementById(a);v||console.error("dom with wrapperId is not find"),S&&v?.appendChild(S),this.rtcServer=new r({closeLog:C,brtcParams:{},videoWrapperId:c,rtcConfig:{remotevideoviewid:a,showvideobps:!1,usingvideo:!1,usingaudio:!1,aspublisher:!1,usingdatachannel:!0,autoplaymuted:!1},bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback}}),this.cutoutInstance=new l({sourceId:c}),t(p)||(this.rtcConnectParams=p,this.rtcServer.openRtc(p))}onRemoteVideoComing=e=>{this.rtcConnectParams&&this.rtcConnectParams.feedId&&e===+this.rtcConnectParams.feedId&&this.rtcServer?.client?.subscribeStreaming("remotevideo500",+this.rtcConnectParams.feedId)};onRemoteVideoOn=()=>{setTimeout((async()=>{const e=document.getElementById(c);if(await(this.cutoutInstance?.updateSource?.(c)),e){e.setAttribute?.("data-playing","true");const{videoWidth:t,videoHeight:a}=e,n=document.getElementById(d),s=n.getContext("2d");n&&s&&(n.width=t,n.height=a,s.drawImage(e,0,0,t,a))}}),100)};connectListener=t=>{const{code:a,body:n}=t;if(0===a){const t=e.parse(n),{rtcServerUrl:a,appId:s,clientToken:r,roomName:o,clientId:i,feedId:c,chargeId:d}=t;this.rtcConnectParams={server:a,appId:s,token:r,roomName:o,userId:i,feedId:c,chargeId:d},s&&o&&this.rtcServer?.openRtc(this.rtcConnectParams)}};createServer=e=>{this.dhServer?.createSocket({token:this.token,appKey:this.appKey,appId:this.appId,...e?{sessionId:e}:{},parameters:this.connectParams,onConnect:this.connectListener,onDigitalHumanCallback:this.onDigitalHumanCallback})};checkHumanInstansce=()=>{this.dhServer||console.warn("请先初始化数字人示例")};sendMessage=(e,t)=>this.dhServer?.sendMessage(e,t);textRender=e=>{const{requestId:t,body:n,onCallbackMsg:s}=e;this.checkHumanInstansce(),this.dhServer?.sendMessage({requestId:t||a(),action:"TEXT_RENDER",body:n,clientTs:(new Date).getTime()},s)};textStreamRender=t=>{const{requestId:n,body:s,onCallbackMsg:r}=t,{first:o}=e.parse(s);this.checkHumanInstansce(),this.dhServer?.sendMessage({requestId:n||a(),action:"TEXT_STREAM_RENDER",body:s,clientTs:(new Date).getTime()},o?r:null)};audioRender=e=>{const{body:t,onCallbackMsg:n}=e;this.checkHumanInstansce(),this.dhServer?.sendMessage({requestId:a(),action:"AUDIO_RENDER",body:t,clientTs:(new Date).getTime()},n)};audioStreamRender=e=>{const{body:t,onCallbackMsg:n}=e;this.checkHumanInstansce(),this.dhServer?.sendMessage({requestId:a(),action:"AUDIO_STREAM_RENDER",body:t,clientTs:(new Date).getTime()},n)};destory=async()=>{await(this.dhServer?.closeSocket()),this.rtcServer?.closeRtc(),m(),this.dhServer=null,this.rtcServer=null};pauseHuman=()=>h(!1);playHuman=()=>h(!0);muteHuman=()=>u(!0);unMuteHuman=()=>{const e=u(!1);if(!this.isHuawei)return e.paused&&e.play();e.pause(),e.play()}}export{p as default};
package/es/Cutout.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @file lib/Cutout
3
+ * @description 视频抠绿相关
4
+ * @author zhangyue49
5
+ */
6
+ import { ChromaEffectOptions, Seriously } from '@bddh/starling-cutout';
7
+ interface CutoutType {
8
+ effects?: {
9
+ version: number;
10
+ chromaKey: Partial<ChromaEffectOptions>;
11
+ };
12
+ sourceId: string;
13
+ }
14
+ export declare const DEFAULT_EFFECTS: {
15
+ version: number;
16
+ chromaKey: {
17
+ screen: number[];
18
+ similarity: number;
19
+ edgeShrink: number;
20
+ smoothness: number;
21
+ spill: number;
22
+ opacity: number;
23
+ contrast: number;
24
+ brightness: number;
25
+ gamma: number;
26
+ };
27
+ };
28
+ declare class Cutout {
29
+ cutoutInstance: Seriously;
30
+ constructor(props: CutoutType);
31
+ setOptions(options: Partial<ChromaEffectOptions>): void;
32
+ destroy(): any;
33
+ updateSource(video: HTMLVideoElement | string): any;
34
+ }
35
+ export default Cutout;
package/es/Cutout.js ADDED
@@ -0,0 +1 @@
1
+ import{Seriously as t}from"@bddh/starling-cutout";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?.chromaKey||s.chromaKey,o?.version||s.version)}setOptions(t){t&&this.cutoutInstance?.setOptions(t)}destroy(){return this.cutoutInstance.destroy()}updateSource(t){return this.cutoutInstance.updateSource(t)}}export{s as DEFAULT_EFFECTS,e as default};
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @file DomControl
3
+ * @description 一些js创建dom的方法
4
+ */
5
+ interface CreateVideoDomOptions {
6
+ id?: string;
7
+ autoplay?: boolean;
8
+ autoChromaKey?: boolean;
9
+ }
10
+ interface CreateVideoDomResult {
11
+ playerWrapper: HTMLDivElement;
12
+ video: HTMLVideoElement;
13
+ }
14
+ export declare const DEFAULT_CANVAS_ID = "starling-cutout-canvas";
15
+ export declare const HUMAN_WRAPPER_ID = "cloud-brtc-player-wrapper";
16
+ export declare const RTC_VIDEO_ID = "remotevideo500";
17
+ export declare function createHumanDom(options: CreateVideoDomOptions): CreateVideoDomResult | null;
18
+ export declare function removeHumanDom(): void;
19
+ export declare function muteHumanVideo(muted: boolean): HTMLVideoElement;
20
+ export declare function playHumanVideo(play: boolean): void | Promise<void>;
21
+ export declare function checkIsHuawei(): boolean;
22
+ export {};
@@ -0,0 +1 @@
1
+ const t="starling-cutout-canvas",e="cloud-brtc-player-wrapper",n="remotevideo500";function i(i){const{id:o=n,autoChromaKey:r=!1,autoplay:a=!0}=i;if(document.getElementById(o))return console.error(`DOM with id "${o}" already exists.`),null;const d=document.createElement("div");d.className=e,d.id=e,d.style.position="relative";const s=document.createElement("video");s.id=o,s.width=0,s.height=0,s.setAttribute("width","100%"),s.setAttribute("height","100%"),s.setAttribute("data-playing","true"),s.setAttribute("playsinline","");const u=document.createElement("canvas");return u.id=t,Object.assign(u.style,{position:"absolute",top:"0",left:"0",height:"100%",width:"100%",zIndex:"1",display:r?"block":"none"}),a&&s.setAttribute("autoplay",""),r&&(s.style.visibility="hidden"),d.appendChild(s),d.appendChild(u),{playerWrapper:d,video:s}}function o(){const t=document.getElementById(e);t&&t.parentNode&&t.parentNode.removeChild(t)}function r(t){const e=document.getElementById(n);return e&&(e.muted=t),e}function a(t){const e=document.getElementById(n);if(e)return t?e.play():e.pause()}function d(){const t=navigator.userAgent||navigator.vendor;return/huawei/i.test(t)||/honor/i.test(t)}export{t as DEFAULT_CANVAS_ID,e as HUMAN_WRAPPER_ID,n as RTC_VIDEO_ID,d as checkIsHuawei,i as createHumanDom,r as muteHumanVideo,a as playHumanVideo,o as removeHumanDom};
package/es/Server.d.ts ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @file lib/Server
3
+ * @description ws服务逻辑处理
4
+ * @author zhangyue49
5
+ */
6
+ import { WebSocketClient } from '@bddh/starling-web-socket/es/interface';
7
+ import { CallbackMsgType, ConnectDataType, ConnectParamsType } from './interface';
8
+ export interface SteamDataType {
9
+ first: boolean;
10
+ last: boolean;
11
+ audio?: string;
12
+ body?: string;
13
+ }
14
+ export interface DHServerInterface {
15
+ action: string;
16
+ requestId: string;
17
+ clientTs: number;
18
+ body: string;
19
+ }
20
+ export interface DHErrServerInterface {
21
+ action: string;
22
+ requestId: string;
23
+ code?: number;
24
+ message?: string;
25
+ error: Error;
26
+ }
27
+ type ConnectListener = (data: ConnectDataType, off?: () => void) => void;
28
+ export interface createSocketParamsType {
29
+ token?: string;
30
+ appKey?: string;
31
+ appId?: string;
32
+ sessionId?: string;
33
+ parameters?: ConnectParamsType;
34
+ onConnect: ConnectListener;
35
+ onDigitalHumanCallback?: (data: CallbackMsgType) => void;
36
+ }
37
+ export interface RenderCallbackRes {
38
+ requestId: string;
39
+ action: string;
40
+ code: number;
41
+ body: string | null;
42
+ message: string;
43
+ }
44
+ export type RenderCallback = (res: RenderCallbackRes) => void;
45
+ declare class Server {
46
+ dhServerSocket: WebSocketClient | null;
47
+ private socketClosingPromise;
48
+ private readonly url;
49
+ private firstConnect;
50
+ private connectParams;
51
+ constructor(url: string);
52
+ createSocket: (props: createSocketParamsType) => Promise<WebSocketClient | null>;
53
+ closeSocket: () => Promise<void>;
54
+ handleConnect: () => Promise<void>;
55
+ sendMessage(message: DHServerInterface, listener: RenderCallback | null): Promise<void>;
56
+ }
57
+ export default Server;
package/es/Server.js ADDED
@@ -0,0 +1 @@
1
+ import{v4 as e}from"uuid";import{createWebSocket as t}from"./packages/web-socket/es/create-web-socket.js";import{statusEnum as s,WS_STATUS_MAP as o,errorTypeEnum as n}from"./interface.js";class r{dhServerSocket=null;socketClosingPromise=null;url;firstConnect=!0;connectParams=null;constructor(e){this.url=e}createSocket=async r=>{this.connectParams=r;const{token:c,sessionId:i,onConnect:a,onDigitalHumanCallback:h}=r;return this.socketClosingPromise&&await this.socketClosingPromise,this.dhServerSocket&&await this.closeSocket(),new Promise(((r,l)=>{const d=t(this.url,{reopen:!0,keepAlive:"heartbeat",combine:(e,t)=>e?.requestId===t?.requestId,onOpen:()=>{if(this.dhServerSocket=d,i&&this.firstConnect)return d.request({action:"RE_CONNECT",requestId:e(),body:JSON.stringify({token:c,sessionId:i})}).on(((e,t)=>{a(e),0!==e.code&&(t(),this.handleConnect()),r(d)})),void(this.firstConnect=!1);this.handleConnect()},onClose:()=>{this.dhServerSocket=null,this.socketClosingPromise=null},onError:e=>{l(e)}});d.onReadyStateChange((e=>{-1===e&&h&&h({status:s.DH_LIB_INIT}),h&&h({status:s.DH_LIB_FULL_STATUS,content:{type:"wsState",readyState:e,body:o[e]}})})),d.onMessage((e=>{const{action:t}=e;[n.TIMEOUT_EXIT,n.DISCONNECT_ALERT].includes(t)&&h&&h({status:s.DH_LIB_STATUS,content:{type:t}})})),d.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:s,token:o,parameters:n,onConnect:r}=this.connectParams;(t||s||o)&&this.dhServerSocket?.request({action:"CONNECT",clientTs:(new Date).getTime(),requestId:e(),body:JSON.stringify({token:o,appId:t,appKey:s,parameters:n})}).on(((e,t)=>{r(e),0!==e.code&&t()}))};async sendMessage(e,t){try{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{r as default};
@@ -0,0 +1 @@
1
+ function r(r,t){if(!(r instanceof t))throw new TypeError("Cannot call a class as a function")}function t(r,t){for(var e=0;e<t.length;e++){var n=t[e];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(r,y(n.key),n)}}function e(r,e,n){return e&&t(r.prototype,e),n&&t(r,n),Object.defineProperty(r,"prototype",{writable:!1}),r}function n(r){return o(r)||a(r)||i(r)||l()}function o(r){if(Array.isArray(r))return u(r)}function a(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function i(r,t){if(r){if("string"==typeof r)return u(r,t);var e=Object.prototype.toString.call(r).slice(8,-1);return"Object"===e&&r.constructor&&(e=r.constructor.name),"Map"===e||"Set"===e?Array.from(r):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?u(r,t):void 0}}function u(r,t){(null==t||t>r.length)&&(t=r.length);for(var e=0,n=new Array(t);e<t;e++)n[e]=r[e];return n}function l(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function f(r,t){var e="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(!e){if(Array.isArray(r)||(e=i(r))||t&&r&&"number"==typeof r.length){e&&(r=e);var n=0,o=function(){};return{s:o,n:function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}},e:function(r){throw r},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,u=!0,l=!1;return{s:function(){e=e.call(r)},n:function(){var r=e.next();return u=r.done,r},e:function(r){l=!0,a=r},f:function(){try{u||null==e.return||e.return()}finally{if(l)throw a}}}}function c(r,t){if("object"!=typeof r||null===r)return r;var e=r[Symbol.toPrimitive];if(void 0!==e){var n=e.call(r,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(r)}function y(r){var t=c(r,"string");return"symbol"==typeof t?t:String(t)}export{u as arrayLikeToArray,o as arrayWithoutHoles,r as classCallCheck,e as createClass,f as createForOfIteratorHelper,a as iterableToArray,l as nonIterableSpread,n as toConsumableArray,c as toPrimitive,y as toPropertyKey,i as unsupportedIterableToArray};
package/es/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @file 云渲染SDK
3
+ * @author zhangyue49
4
+ */
5
+ import HumanClient from './Client';
6
+ export default HumanClient;
7
+ export * from './interface';
package/es/index.js ADDED
@@ -0,0 +1 @@
1
+ import r from"./Client.js";export{WS_STATUS_MAP,errorTypeEnum,statusEnum}from"./interface.js";export{r as default};
@@ -0,0 +1,87 @@
1
+ import { RtcConnectionType } from './BrtcClient';
2
+ export interface ConnectDataType {
3
+ action: string;
4
+ body: string;
5
+ code: number;
6
+ message: string;
7
+ }
8
+ export interface ConnectParamsType {
9
+ figureId: string | number;
10
+ resolutionHeight: number;
11
+ resolutionWidth: number;
12
+ cameraId?: string | number;
13
+ characterOffset?: string;
14
+ pickAudioMode?: string;
15
+ autoChromaKey?: boolean;
16
+ usingVideo?: boolean;
17
+ ttsPer?: string | number;
18
+ ttsPitch?: number;
19
+ ttsSpeed?: number;
20
+ ttsVolume?: number;
21
+ ttsLan?: string;
22
+ pullAudioFromRtc?: boolean;
23
+ inactiveDisconnectSec?: number;
24
+ preAlertSec?: number;
25
+ x264BitRate?: number;
26
+ }
27
+ export interface RenderParamsType {
28
+ chromaEffects?: object;
29
+ autoChromaKey?: boolean;
30
+ closeLog?: boolean;
31
+ fullStatus?: boolean;
32
+ }
33
+ export declare enum statusEnum {
34
+ DH_LIB_INIT = "DH_LIB_INIT",
35
+ DH_LIB_WS_SUCCESS = "DH_LIB_WS_SUCCESS",
36
+ DH_LIB_OPEN = "DH_LIB_OPEN",
37
+ DH_LIB_STATUS = "DH_LIB_STATUS",
38
+ DH_LIB_CLOSE = "DH_LIB_CLOSE",
39
+ DH_LIB_ERROR = "DH_LIB_ERROR",
40
+ DH_LIB_WARNING = "DH_LIB_WARNING",
41
+ DH_LIB_FULL_STATUS = "DH_LIB_FULL_STATUS"
42
+ }
43
+ export declare enum errorTypeEnum {
44
+ CONNECT_ERROR = "CONNECT_ERROR",
45
+ DISCONNECT_ALERT = "DISCONNECT_ALERT",
46
+ TIMEOUT_EXIT = "TIMEOUT_EXIT",
47
+ LOCAL_VIDEO_MUTED = "LOCAL_VIDEO_MUTED",
48
+ LOCAL_VIDEO_ERROR = "LOCAL_VIDEO_ERROR",
49
+ RTC_ERROR = "RTC_ERROR"
50
+ }
51
+ export interface ErrorType {
52
+ type: errorTypeEnum;
53
+ msg?: {
54
+ code: string | number;
55
+ };
56
+ }
57
+ export interface RtcStateType {
58
+ action: string;
59
+ type: string;
60
+ body: any;
61
+ }
62
+ export declare const WS_STATUS_MAP: {
63
+ '-1': string;
64
+ '0': string;
65
+ '1': string;
66
+ '2': string;
67
+ '3': string;
68
+ };
69
+ export interface WsStateType {
70
+ readyState: number;
71
+ type: string;
72
+ body: string;
73
+ }
74
+ export interface CallbackMsgType {
75
+ status: statusEnum;
76
+ content?: ErrorType | WsStateType | RtcStateType;
77
+ }
78
+ export interface DhRealtimeHumanType {
79
+ token: string;
80
+ appKey?: string;
81
+ appId?: string;
82
+ wrapperId: string;
83
+ connectParams: ConnectParamsType;
84
+ renderParams: RenderParamsType;
85
+ rtcConnectParams?: RtcConnectionType;
86
+ onDigitalHumanCallback: (data: CallbackMsgType) => void;
87
+ }
@@ -0,0 +1 @@
1
+ var _,I;!function(_){_.DH_LIB_INIT="DH_LIB_INIT",_.DH_LIB_WS_SUCCESS="DH_LIB_WS_SUCCESS",_.DH_LIB_OPEN="DH_LIB_OPEN",_.DH_LIB_STATUS="DH_LIB_STATUS",_.DH_LIB_CLOSE="DH_LIB_CLOSE",_.DH_LIB_ERROR="DH_LIB_ERROR",_.DH_LIB_WARNING="DH_LIB_WARNING",_.DH_LIB_FULL_STATUS="DH_LIB_FULL_STATUS"}(_||(_={})),function(_){_.CONNECT_ERROR="CONNECT_ERROR",_.DISCONNECT_ALERT="DISCONNECT_ALERT",_.TIMEOUT_EXIT="TIMEOUT_EXIT",_.LOCAL_VIDEO_MUTED="LOCAL_VIDEO_MUTED",_.LOCAL_VIDEO_ERROR="LOCAL_VIDEO_ERROR",_.RTC_ERROR="RTC_ERROR"}(I||(I={}));const E={"-1":"UNINSTANTIATED",0:"CONNECTING",1:"OPEN",2:"CLOSING",3:"CLOSED"};export{E as WS_STATUS_MAP,I as errorTypeEnum,_ as statusEnum};
@@ -0,0 +1 @@
1
+ import{createClass as r,classCallCheck as e}from"../../../_virtual/_rollupPluginBabelHelpers.js";import t from"lodash/isString";import n from"lodash/isObject";import o from"lodash/cloneDeep";var a=function(){function a(){e(this,a)}return r(a,null,[{key:"isJsonStr",value:function(r){if(t(r))try{var e=JSON.parse(r);return!!n(e)}catch(r){return!1}return!1}},{key:"parse",value:function(r){var e={};try{e=JSON.parse(r||"{}")}catch(e){console.error("JSON.parse Error:",r)}return e||{}}},{key:"stringify",value:function(r){var e="";try{e=JSON.stringify(r||{})}catch(e){console.error("JSON.stringify error:",r)}return e||"{}"}},{key:"getParseValue",value:function(r){try{return r&&t(r)?a.parse(r):r}catch(e){return console.info("JSON parse error",r),r}}},{key:"parseObj",value:function(r,e){var t=o(r);return e.forEach((function(e){if(Object.keys(r).includes(e))try{t[e]=a.parse(r[e]),console.log(e,t[e])}catch(r){console.error('Failed to parse JSON for key "'.concat(e,'":'),r)}})),t}},{key:"stringifyObj",value:function(r,e){var t=o(r);return e.forEach((function(e){Object.keys(r).includes(e)&&(t[e]=a.stringify(r[e]))})),t}}]),a}();export{a as default};
@@ -0,0 +1 @@
1
+ import{toConsumableArray as e,createForOfIteratorHelper as t}from"../../../_virtual/_rollupPluginBabelHelpers.js";import{setTimeout as n,clearTimeout as o}from"../../../node_modules/worker-timers/build/es2019/module.js";import{ReadyState as r,CloseEventCode as i}from"./interface.js";import{createRequestId as s}from"./utils.js";var a=10,c=5e3;function u(a){var u,l=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},d=a,h=0,v=0,f=0,p=!1,E=null!==(u=null==l?void 0:l.reopenDelay)&&void 0!==u?u:c,g=!0,y=!1,S=0,m=new Set,_=new Set,C=new Set;function N(){var e;p=!1,h=0,g=!0,E=null!==(e=null==l?void 0:l.reopenDelay)&&void 0!==e?e:c}function R(e){var t=l.reopen,r=void 0===t||t,i=l.reopenAttempts,s=void 0===i?10:i,a=l.reopenDelayIncrease,c=l.onReopenAttemptEnd;if(h>=s||!r)return N(),g&&v&&o(v),void("function"==typeof c&&c(g));E="function"==typeof a?a({reopenCount:h,preDelay:E,isServerClosed:g}):E,f=n((function(){p=!0,e.open(),h++}),E)}var A={socket:null,messages:[],get readyState(){return this.socket?this.socket.readyState:r.UNINSTANTIATED},get isOpened(){return r.OPEN===this.readyState},get isActive(){return r.CONNECTING===this.readyState||r.OPEN===this.readyState},open:function(){var e=this;if(this.isActive)return this;f&&!p&&o(f);var t=this.socket=new WebSocket(d,null==l?void 0:l.protocols);return this._dispchReadyStateChangeEvent(),t.addEventListener("open",this._dispchReadyStateChangeEvent),t.addEventListener("close",this._dispchReadyStateChangeEvent),t.addEventListener("error",this._dispchReadyStateChangeEvent),t.onopen=function(r){var i=l.onOpen,s=l.onMessage,a=l.combine,u=l.reopenDelay,d=void 0===u?c:u;p&&(g?(v&&o(v),v=n(N,d>1e3?d+100:1e3)):N()),i&&i(r),t.onmessage=function(t){s&&s(t);var n=e.deserialization(t.data);a&&m.forEach((function(e){a(e.requestData,n)&&(e.then?(e.then(n),m.delete(e)):e.on?e.on(n):e.responseData=n)})),_.forEach((function(e){return e(n)}))},e.messages.flush(),e._ping()},t.onerror=function(t){l.onError&&l.onError(t),g=!1,R(e)},t.onclose=function(n){n.isInterruptWithNewUrl=y,y=!1,S&&o(S);var r=l.reopenWhenServerClosed,i=void 0===r||r,s=l.onClose,a=l.onServerClose;g&&a&&a(n),s&&s(n),e._dispose(t),g&&i&&R(e)},this},openWithNewUrl:function(e){var t;return e&&this.isActive&&e===d?this:(d=e||d,r.CONNECTING===this.readyState?(this._dispose(),null===(t=this.socket)||void 0===t||t.close()):this.isActive&&this.close(i.CLOSE_INTERRUPT_WITH_NEW_URL,"interrupt by new url"),this.open())},close:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1e3,n=arguments.length>1?arguments[1]:void 0;return this.socket?new Promise((function(o){var r,s;g=!1,i.CLOSE_INTERRUPT_WITH_NEW_URL===t&&(y=!0);null!==(r=e.socket)&&void 0!==r&&r.addEventListener("close",(function t(){var n;null!==(n=e.socket)&&void 0!==n&&n.removeEventListener("close",t),e._dispose(e.socket),o()})),null!==(s=e.socket)&&void 0!==s&&s.close(t,n),e._dispchReadyStateChangeEvent()})):Promise.resolve()},connect:function(){return this.open()},disconnect:function(e,t){return this.close(e,t)},send:function(e){var t=this.socket;if(!t)return this;if(r.CONNECTING===t.readyState)return this.messages.push(e),this;e instanceof Function&&(e=e(s()));var n=this.serialization(e);return t.send(n),this},request:function(e){if(null==l||!l.combine)throw new Error("Request failed! unconfigured combine, this configuration is required to use the request api.");e instanceof Function&&(e=e(s())),this.send(e);var t={requestData:e,responseData:null};return m.add(t),{then:function(e){if(t.on&&console.warn('You have already registered for the "on" callback.'),t.responseData){var n=e(t.responseData);return n instanceof Promise?n:Promise.resolve(n)}return new Promise((function(e){t.then=function(t){return e(t)}})).then((function(t){return e(t)}))},on:function(e){var n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];t.then&&console.warn('You have already registered for the "then" callback.'),t.on=function(n){e(n,(function(){m.delete(t)}))},t.responseData&&n&&t.on(t.responseData)}}},receive:function(){var e=this;return new Promise((function(t){var n=e.onReceive((function(e){n(),t(e)}))}))},onReceive:function(e){return _.add(e),function(){_.delete(e)}},onMessage:function(e){return this.onReceive(e)},_dispchReadyStateChangeEvent:function(){var e=this;C.forEach((function(t){return t(e.readyState)}))},_dispose:function(e){var t;(e=null!==(t=e)&&void 0!==t?t:this.socket)&&(e.removeEventListener("open",this._dispchReadyStateChangeEvent),e.removeEventListener("close",this._dispchReadyStateChangeEvent),e.removeEventListener("error",this._dispchReadyStateChangeEvent),e.onopen=null,e.onclose=null,e.onerror=null)},_ping:function(){var e=this,t=l.keepAlive,r=void 0===t?"none":t;S&&o(S),"none"!==r&&this.isOpened&&(S=n((function(){if(e.isOpened){var t=null!=l&&l.getKeepAliveMessage?null==l?void 0:l.getKeepAliveMessage(s()):{requestId:s(),action:"heartbeat"===r?"HEART_BEAT":"PING"};"heartbeat"===l.keepAlive?(e.send(t),e._ping()):e.request(t).then((function(){e._ping()})).catch((function(e){console.error(e)}))}}),(null==l?void 0:l.keepAliveDelay)||3e3))},onReadyStateChange:function(e){return C.add(e),function(){C.delete(e)}},serialization:function(e){return"[object Object]"===Object.prototype.toString.call(e)||Array.isArray(e)||"number"==typeof e?JSON.stringify(e):e},deserialization:function(e){if("string"==typeof e){var t=e;try{t=JSON.parse(e)}catch(n){t=e}return t}return e}};return A.messages.flush=function(){var n=e(this);if(this.length=0,n.length){var o,r=t(n);try{for(r.s();!(o=r.n()).done;){var i=o.value;A.send(i)}}catch(e){r.e(e)}finally{r.f()}}},A}export{a as DEFAULT_RECONNECT_ATTEMPTS,c as DEFAULT_RECONNECT_DELAY,u as createWebSocket};
@@ -0,0 +1 @@
1
+ var N,E;!function(N){N[N.UNINSTANTIATED=-1]="UNINSTANTIATED",N[N.CONNECTING=0]="CONNECTING",N[N.OPEN=1]="OPEN",N[N.CLOSING=2]="CLOSING",N[N.CLOSED=3]="CLOSED"}(N||(N={})),function(N){N[N.CLOSE_INTERRUPT_WITH_NEW_URL=4e3]="CLOSE_INTERRUPT_WITH_NEW_URL"}(E||(E={}));export{E as CloseEventCode,N as ReadyState};
@@ -0,0 +1 @@
1
+ import{v4 as e}from"uuid";function r(){return e()}export{r as createRequestId};
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@bddh/starling-realtime-client",
3
+ "version": "1.0.0-beta.1",
4
+ "keywords": [
5
+ "react",
6
+ "indexDB"
7
+ ],
8
+ "homepage": "",
9
+ "license": "MIT",
10
+ "module": "es/index.js",
11
+ "main": "cjs/index.js",
12
+ "typings": "es/index.d.ts",
13
+ "files": [
14
+ "cjs",
15
+ "es"
16
+ ],
17
+ "publishConfig": {
18
+ "registry": "https://registry.npmjs.org"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "ssh://zhangyue49@icode.baidu.com:8235/baidu/acu-ai-fe/react-common-lib"
23
+ },
24
+ "scripts": {
25
+ "build": "rm -rf cjs es && rollup -c --bundleConfigAsCjs",
26
+ "test": "npx jest --passWithNoTests",
27
+ "deploy": "npm publish --registry=https://registry.npmjs.org --access public"
28
+ },
29
+ "peerDependencies": {
30
+ "lodash": "^4.17.21",
31
+ "uuid": "^9.0.0",
32
+ "@bddh/starling-brtc": "^1.0.2",
33
+ "@bddh/starling-timeout-controller": "^1.0.1"
34
+ },
35
+ "dependencies": {
36
+ "@bddh/starling-web-socket": "^1.0.10",
37
+ "lodash": "^4.17.21",
38
+ "uuid": "^9.0.0",
39
+ "@bddh/starling-json": "^1.0.1",
40
+ "@bddh/starling-brtc": "^1.0.2",
41
+ "@bddh/starling-timeout-controller": "^1.0.1"
42
+ }
43
+ }