@bddh/starling-realtime-client 2.0.5 → 2.0.6

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,97 @@
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/es/interface';
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
+ videoWrapperId: string;
70
+ bizConfig: BizConfig;
71
+ closeLog?: boolean;
72
+ rtcConnection?: RtcConnectionType;
73
+ preConnectInfo?: RtcConnectionType;
74
+ rtcConfig?: RtcConfig;
75
+ rtcCallback?: Partial<RtcCallback>;
76
+ }
77
+ declare class BrtcClient {
78
+ client?: BRtcInstance;
79
+ isRtcVideoOn: boolean;
80
+ private rtcConnection;
81
+ private readonly rtcConfig?;
82
+ private readonly bizConfig?;
83
+ private readonly rtcCallback?;
84
+ private restarting;
85
+ private readonly timeoutEvent;
86
+ private reopenTimer;
87
+ private restartCount;
88
+ private readonly closeLog;
89
+ private haveVideoOn;
90
+ private rtcStateCallback;
91
+ constructor(props: BrtcClientType);
92
+ openRtc: (rtcConnection: RtcConnectionType) => void;
93
+ closeRtc: () => void;
94
+ restartRtc: (rtcConnection: RtcConnectionType) => void;
95
+ muteMicphone(mute: boolean): void;
96
+ }
97
+ export default BrtcClient;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _rollupPluginBabelHelpers=require("./_virtual/_rollupPluginBabelHelpers.js"),BrtcInstance=require("@bddh/starling-brtc"),starlingTimeoutController=require("@bddh/starling-timeout-controller"),CustomJSON=require("@bddh/starling-json"),isEmpty=require("lodash/isEmpty"),isFunction=require("lodash/isFunction"),_interface=require("./interface.js"),BrtcClient=function(){function BrtcClient(props){var _this=this;_rollupPluginBabelHelpers.classCallCheck(this,BrtcClient),_rollupPluginBabelHelpers.defineProperty(this,"client",void 0),_rollupPluginBabelHelpers.defineProperty(this,"isRtcVideoOn",!1),_rollupPluginBabelHelpers.defineProperty(this,"rtcConnection",void 0),_rollupPluginBabelHelpers.defineProperty(this,"rtcConfig",void 0),_rollupPluginBabelHelpers.defineProperty(this,"bizConfig",void 0),_rollupPluginBabelHelpers.defineProperty(this,"rtcCallback",void 0),_rollupPluginBabelHelpers.defineProperty(this,"restarting",!1),_rollupPluginBabelHelpers.defineProperty(this,"timeoutEvent",void 0),_rollupPluginBabelHelpers.defineProperty(this,"reopenTimer",void 0),_rollupPluginBabelHelpers.defineProperty(this,"restartCount",0),_rollupPluginBabelHelpers.defineProperty(this,"closeLog",!0),_rollupPluginBabelHelpers.defineProperty(this,"haveVideoOn",!1),_rollupPluginBabelHelpers.defineProperty(this,"rtcStateCallback",null),_rollupPluginBabelHelpers.defineProperty(this,"openRtc",(function(rtcConnection){if(_this.restarting)!_this.closeLog&&console.warn("rtc restarting");else if(rtcConnection&&(_this.rtcConnection=rtcConnection),_this.rtcConnection&&!isEmpty(_this.rtcConnection)){var _this$rtcConnection=_this.rtcConnection,server=_this$rtcConnection.server,appId=_this$rtcConnection.appId,token=_this$rtcConnection.token,roomName=_this$rtcConnection.roomName,userId=_this$rtcConnection.userId,_ref=_this.rtcCallback||{},_onRemotedata=_ref.onRemotedata,_onLocalStream=_ref.onLocalStream,_onRemoteVideoComing=_ref.onRemoteVideoComing,_onLocalVideoPublished=_ref.onLocalVideoPublished,_onLocalVideoPublishing=_ref.onLocalVideoPublishing,_onRemoteVideoOn=_ref.onRemoteVideoOn,_onRemoteVideoOff=_ref.onRemoteVideoOff,_onRemoteVideoLoading=_ref.onRemoteVideoLoading,_onSuccess=_ref.onSuccess,_onError=_ref.onError,_onLocalVideoConnected=_ref.onLocalVideoConnected,_onRemoteVideoConnected=_ref.onRemoteVideoConnected,_onRemoteMediaState=_ref.onRemoteMediaState,onPlayingError=_ref.onPlayingError,onDigitalHumanCallback=_ref.onDigitalHumanCallback;_this.timeoutEvent.start(),_this.rtcStateCallback=function(data){var body=data.body,action=data.action;onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_FULL_STATUS,content:{type:"rtcState",action:action,body:body}})},_this.client=new BrtcInstance({roomName:roomName,token:token,userId:userId,appId:appId,server:server,rtcConfig:_this.rtcConfig,callback:{onRemoteVideoConnected:function(id,connected){if(!connected&&!_this.haveVideoOn)var connectTimer=setTimeout((function(){_this.restartCount<3?(_this.restartCount=++_this.restartCount,_this.restartRtc&&_this.restartRtc(rtcConnection)):(_this.restartCount=0,_this.closeRtc()),clearTimeout(connectTimer)}),500);if(connected){var videoElement=document.getElementById("remotevideo500");videoElement.muted=!1;var promise=videoElement.play();void 0!==promise&&promise.catch((function(){videoElement.muted=!0,videoElement.play().catch((function(e){!_this.closeLog&&console.info("videoElement play error",e),onPlayingError&&onPlayingError(videoElement),onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_ERROR,content:{type:_interface.errorTypeEnum.LOCAL_VIDEO_ERROR}})})),onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_STATUS,content:{type:_interface.errorTypeEnum.LOCAL_VIDEO_MUTED}})}))}!_this.closeLog&&console.info("remoteVideoConnected",id,connected,_this.restartCount),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remoteVideoConnected",body:{id:id,connected:connected,restartCount:_this.restartCount}}),isFunction(_onRemoteVideoConnected)&&_onRemoteVideoConnected(id,connected)},onRemoteVideoOn:function(id){_this.haveVideoOn=!0,!_this.closeLog&&console.info("remotevideoon by id: ",id),_this.timeoutEvent.clear();var rtcContainer=document.querySelector("#starlingRtcContainer"),therevideo=rtcContainer&&rtcContainer.lastChild;therevideo&&(therevideo.style.display="none"),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remotevideoon",body:id}),onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_OPEN}),isFunction(_onRemoteVideoOn)&&_onRemoteVideoOn(id)},onRemoteMediaState:function(id,medium,connected){!_this.closeLog&&console.info("remoteMediaState",id,medium,connected),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remoteMediaState",body:{id:id,medium:medium,connected:connected}}),isFunction(_onRemoteMediaState)&&_onRemoteMediaState(id,medium,connected)},onLocalVideoConnected:function(connected){!_this.closeLog&&console.info("localVideoConnected",connected),_this.rtcStateCallback&&_this.rtcStateCallback({action:"localVideoConnected",body:{connected:connected}}),isFunction(_onLocalVideoConnected)&&_onLocalVideoConnected(connected)},onRemotedata:function(data){if(_this.rtcConfig&&_this.rtcConfig.usingdatachannel){var msg=CustomJSON.parse(data);!_this.closeLog&&console.info("RTC - receive:",msg),isFunction(_onRemotedata)&&_onRemotedata(msg)}},onLocalStream:function(stream,name){!_this.closeLog&&console.info("rtc - onlocalstream by name: "+name),_this.rtcStateCallback&&_this.rtcStateCallback({action:"onlocalstream",body:{stream:stream,name:name}}),isFunction(_onLocalStream)&&_onLocalStream(stream,name)},onLocalVideoPublished:function(){!_this.closeLog&&console.info("LocalVideoPublished 本地视频发布成功"),_this.rtcStateCallback&&_this.rtcStateCallback({action:"localvideopublished_ok",body:!0}),isFunction(_onLocalVideoPublished)&&_onLocalVideoPublished()},onLocalVideoPublishing:function(){!_this.closeLog&&console.info("LocalVideoPublishing 本地视频发布成功"),_this.rtcStateCallback&&_this.rtcStateCallback({action:"localvideopublishing",body:!0}),isFunction(_onLocalVideoPublishing)&&_onLocalVideoPublishing()},onRemoteVideoComing:function(id){!_this.closeLog&&console.info("remotevideocoming",id),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remotevideocoming",body:id}),isFunction(_onRemoteVideoComing)&&_onRemoteVideoComing(id)},onRemoteVideoLoading:function(id){!_this.closeLog&&console.info("remotevideoloading by id: ",id),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remotevideoloading",body:id}),isFunction(_onRemoteVideoLoading)&&_onRemoteVideoLoading(id)},onRemoteVideoOff:function(id){var videoEle=document.getElementById("remotevideo500");videoEle&&videoEle.pause(),!_this.closeLog&&console.info("remotevideooff: ",id),_this.isRtcVideoOn=!1,_this.rtcStateCallback&&_this.rtcStateCallback({action:"remotevideooff",body:id}),onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_CLOSE}),isFunction(_onRemoteVideoOff)&&_onRemoteVideoOff(id),_this.timeoutEvent.start()},onSuccess:function(){!_this.closeLog&&console.info("rtc - success"),_this.rtcStateCallback&&_this.rtcStateCallback({action:"success",body:!0}),isFunction(_onSuccess)&&_onSuccess()},onError:function(e){_this.isRtcVideoOn=!1,!_this.closeLog&&console.error("rtc - error:",e),_this.rtcStateCallback&&_this.rtcStateCallback({action:"error",body:e}),onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_ERROR,content:{type:_interface.errorTypeEnum.RTC_ERROR}}),isFunction(_onError)&&_onError(e),(e.includes("Lost connection to the server")||e.includes("Is the server down"))&&_this.restartRtc&&_this.restartRtc(rtcConnection)}}})}})),_rollupPluginBabelHelpers.defineProperty(this,"closeRtc",(function(){_this.timeoutEvent.clear(),clearTimeout(_this.reopenTimer),_this.client&&_this.client.destroy(),_this.restarting=!1})),_rollupPluginBabelHelpers.defineProperty(this,"restartRtc",(function(rtcConnection){!_this.restarting&&_this.bizConfig&&_this.bizConfig.autoReConnect&&(_this.rtcStateCallback&&_this.rtcStateCallback({action:"restartRtc",body:rtcConnection}),_this.restarting=!0,_this.closeRtc(),_this.reopenTimer=setTimeout((function(){!_this.closeLog&&console.error("拉流失败,开始重启"),_this.restarting=!1,_this.openRtc(rtcConnection)}),2e3))}));var _rtcConnection=props.rtcConnection,rtcConfig=props.rtcConfig,_props$closeLog=props.closeLog,closeLog=void 0===_props$closeLog||_props$closeLog,rtcCallback=props.rtcCallback;this.rtcConnection=_rtcConnection,this.rtcConfig=rtcConfig,this.rtcCallback=rtcCallback,this.closeLog=closeLog,this.timeoutEvent=new starlingTimeoutController.TimeoutController(6e4,(function(){if(!_this.rtcConnection)return!_this.closeLog&&console.warn("remotevideoon事件超时, rtcConnectionRef is null");_this.timeoutEvent.clear(),_this.restartRtc&&_this.restartRtc(_this.rtcConnection)}))}return _rollupPluginBabelHelpers.createClass(BrtcClient,[{key:"muteMicphone",value:function(mute){this.client&&this.client.muteMicphone&&this.client.muteMicphone(mute)}}]),BrtcClient}();exports.default=BrtcClient;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @file dh-realtime-human
3
+ * @description 云渲染数字人sdk
4
+ * @author zhangyue49
5
+ */
6
+ import { DhRealtimeHumanType, ConnectDataType, CallbackMsgType } from './interface';
7
+ import { DHServerInterface, RenderCallback } from './Server';
8
+ export default class DhRealtimeHuman {
9
+ private token;
10
+ private appKey;
11
+ private appId;
12
+ private connectParams;
13
+ private readonly onDigitalHumanCallback?;
14
+ private dhServer;
15
+ private rtcServer?;
16
+ private rtcConnectParams;
17
+ private cutoutInstance?;
18
+ private readonly isHuawei;
19
+ private rtcServerUrl;
20
+ private recordController;
21
+ private wsReadyState;
22
+ private readonly isPreOpenSession;
23
+ constructor(props: DhRealtimeHumanType);
24
+ init(props: DhRealtimeHumanType): void;
25
+ onDigitalHumanCallbackPlus: (data: CallbackMsgType) => void;
26
+ onRemoteVideoComing: (id: number) => void;
27
+ onLocalStream: () => void;
28
+ onRemoteVideoOn: () => void;
29
+ connectListener: (data: ConnectDataType) => void;
30
+ createServer: (sessionId?: string) => void;
31
+ checkHumanInstansce: () => boolean;
32
+ sendMessage: (message: DHServerInterface | ArrayBuffer, listener: RenderCallback | null) => Promise<void> | null;
33
+ interrupt: () => Promise<void>;
34
+ textRender: (renderData: {
35
+ requestId: string;
36
+ body: string;
37
+ onCallbackMsg: RenderCallback;
38
+ }) => void;
39
+ textStreamRender: (renderData: {
40
+ requestId?: string;
41
+ body: string;
42
+ onCallbackMsg: RenderCallback;
43
+ }) => void;
44
+ audioRender: (renderData: {
45
+ body: string;
46
+ onCallbackMsg: RenderCallback;
47
+ }) => void;
48
+ audioStreamRender: (renderData: {
49
+ requestId?: string;
50
+ body: string;
51
+ onCallbackMsg: RenderCallback;
52
+ }) => void;
53
+ textQuery: (queryData: {
54
+ requestId: string;
55
+ body: string;
56
+ onCallbackMsg: RenderCallback;
57
+ }) => void;
58
+ destroy: () => Promise<void>;
59
+ pauseHuman: () => void | Promise<void>;
60
+ playHuman: () => void | Promise<void>;
61
+ muteHuman: () => HTMLVideoElement;
62
+ unMuteHuman: () => false | Promise<void> | undefined;
63
+ startRecord(): void;
64
+ stopRecord(): void;
65
+ }
package/cjs/Client.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _rollupPluginBabelHelpers=require("./_virtual/_rollupPluginBabelHelpers.js"),CustomJSON=require("@bddh/starling-json"),isEmpty=require("lodash/isEmpty"),omit=require("lodash/omit"),pick=require("lodash/pick"),uuid=require("uuid"),_interface=require("./interface.js"),Server=require("./Server.js"),BrtcClient=require("./BrtcClient.js"),DomControl=require("./DomControl.js"),Cutout=require("./Cutout.js"),RecordController=require("./RecordController.js"),DhRealtimeHuman=function(){function DhRealtimeHuman(props){var _this=this;_rollupPluginBabelHelpers.classCallCheck(this,DhRealtimeHuman),_rollupPluginBabelHelpers.defineProperty(this,"token",void 0),_rollupPluginBabelHelpers.defineProperty(this,"appKey",void 0),_rollupPluginBabelHelpers.defineProperty(this,"appId",void 0),_rollupPluginBabelHelpers.defineProperty(this,"connectParams",void 0),_rollupPluginBabelHelpers.defineProperty(this,"onDigitalHumanCallback",void 0),_rollupPluginBabelHelpers.defineProperty(this,"dhServer",void 0),_rollupPluginBabelHelpers.defineProperty(this,"rtcServer",void 0),_rollupPluginBabelHelpers.defineProperty(this,"rtcConnectParams",void 0),_rollupPluginBabelHelpers.defineProperty(this,"cutoutInstance",void 0),_rollupPluginBabelHelpers.defineProperty(this,"isHuawei",DomControl.checkIsHuawei()),_rollupPluginBabelHelpers.defineProperty(this,"rtcServerUrl",void 0),_rollupPluginBabelHelpers.defineProperty(this,"recordController",null),_rollupPluginBabelHelpers.defineProperty(this,"wsReadyState",-1),_rollupPluginBabelHelpers.defineProperty(this,"isPreOpenSession",!1),_rollupPluginBabelHelpers.defineProperty(this,"onDigitalHumanCallbackPlus",(function(data){var _data$content;data.status===_interface.statusEnum.DH_LIB_FULL_STATUS&&"wsState"===(null===(_data$content=data.content)||void 0===_data$content?void 0:_data$content.type)&&(_this.wsReadyState=data.content.readyState),_this.onDigitalHumanCallback&&_this.onDigitalHumanCallback(data)})),_rollupPluginBabelHelpers.defineProperty(this,"onRemoteVideoComing",(function(id){_this.rtcConnectParams&&_this.rtcConnectParams.feedId&&id===+_this.rtcConnectParams.feedId&&_this.rtcServer&&_this.rtcServer.client&&_this.rtcServer.client.subscribeStreaming("remotevideo500",+_this.rtcConnectParams.feedId)})),_rollupPluginBabelHelpers.defineProperty(this,"onLocalStream",(function(){(_this.connectParams||{}).pullAudioFromRtc&&_this.rtcServer&&_this.rtcServer.muteMicphone(!0)})),_rollupPluginBabelHelpers.defineProperty(this,"onRemoteVideoOn",(function(){setTimeout(_rollupPluginBabelHelpers.asyncToGenerator(_rollupPluginBabelHelpers.regeneratorRuntime().mark((function _callee(){var video,videoWidth,videoHeight,canvas,ctx;return _rollupPluginBabelHelpers.regeneratorRuntime().wrap((function(_context){for(;;)switch(_context.prev=_context.next){case 0:return video=document.getElementById(DomControl.RTC_VIDEO_ID),_context.next=3,_this.cutoutInstance&&_this.cutoutInstance.updateSource(DomControl.RTC_VIDEO_ID);case 3:video&&(video.setAttribute("data-playing","true"),videoWidth=video.videoWidth,videoHeight=video.videoHeight,canvas=document.getElementById(DomControl.DEFAULT_CANVAS_ID),ctx=canvas.getContext("2d"),canvas&&ctx&&(canvas.width=videoWidth,canvas.height=videoHeight,ctx.drawImage(video,0,0,videoWidth,videoHeight)));case 4:case"end":return _context.stop()}}),_callee)}))),100)})),_rollupPluginBabelHelpers.defineProperty(this,"connectListener",(function(data){var code=data.code,body=data.body,message=data.message;if(0===code&&_this.onDigitalHumanCallback&&_this.onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_WS_SUCCESS,content:data}),0===code){var _data=CustomJSON.parse(body),server=_data.rtcServerUrl,appId=_data.appId,token=_data.clientToken,roomName=_data.roomName,userId=_data.clientId,feedId=_data.feedId,chargeId=_data.chargeId;appId&&roomName&&(_this.rtcConnectParams={server:_this.rtcServerUrl||server,appId:appId,token:token,roomName:roomName,userId:userId,feedId:feedId,chargeId:chargeId},_this.rtcServer&&_this.rtcServer.openRtc(_this.rtcConnectParams))}else _this.onDigitalHumanCallback&&_this.onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_ERROR,content:{type:_interface.errorTypeEnum.CONNECT_ERROR,msg:{code:code,errMsg:message}}})})),_rollupPluginBabelHelpers.defineProperty(this,"createServer",(function(sessionId){var _this$dhServer,params=_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({token:_this.token,appKey:_this.appKey,appId:_this.appId},sessionId?{sessionId:sessionId}:{}),pick(_this.connectParams,_interface.CONNECT_MAIN_KEY)),{},{parameters:omit(_this.connectParams,_interface.CONNECT_MAIN_KEY),onConnect:_this.connectListener,onDigitalHumanCallback:_this.onDigitalHumanCallbackPlus});null!==(_this$dhServer=_this.dhServer)&&void 0!==_this$dhServer&&_this$dhServer.preConnectSuccess?(_this.dhServer.connectParams=params,_this.dhServer&&_this.dhServer.sendPreConnect(params)):_this.dhServer&&_this.dhServer.createSocket(params)})),_rollupPluginBabelHelpers.defineProperty(this,"checkHumanInstansce",(function(){return _this.dhServer?!(_this.isPreOpenSession&&!_this.dhServer.preConnectSuccess)||(_this.onDigitalHumanCallback&&_this.onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_ERROR,content:{type:_interface.errorTypeEnum.CONNECT_ERROR,msg:{code:4001,errMsg:"webSocket连接中"}}}),!1):(_this.onDigitalHumanCallback&&_this.onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_ERROR,content:{type:_interface.errorTypeEnum.CONNECT_ERROR,msg:{code:4002,errMsg:"请先初始化数字人实例"}}}),!1)})),_rollupPluginBabelHelpers.defineProperty(this,"sendMessage",(function(message,listener){return _this.dhServer&&_this.dhServer.sendMessage(message,listener)})),_rollupPluginBabelHelpers.defineProperty(this,"interrupt",_rollupPluginBabelHelpers.asyncToGenerator(_rollupPluginBabelHelpers.regeneratorRuntime().mark((function _callee2(){return _rollupPluginBabelHelpers.regeneratorRuntime().wrap((function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:return _context2.abrupt("return",new Promise((function(resolve){_this.dhServer&&_this.dhServer.sendMessage({requestId:uuid.v4(),action:"TEXT_RENDER",body:"<interrupt></interrupt>",clientTs:(new Date).getTime()},(function(){return resolve()}))})));case 1:case"end":return _context2.stop()}}),_callee2)})))),_rollupPluginBabelHelpers.defineProperty(this,"textRender",(function(renderData){var requestId=renderData.requestId,body=renderData.body,onCallbackMsg=renderData.onCallbackMsg;_this.checkHumanInstansce()&&_this.dhServer&&_this.dhServer.sendMessage({requestId:requestId||uuid.v4(),action:"TEXT_RENDER",body:body,clientTs:(new Date).getTime()},onCallbackMsg)})),_rollupPluginBabelHelpers.defineProperty(this,"textStreamRender",(function(renderData){var requestId=renderData.requestId,body=renderData.body,onCallbackMsg=renderData.onCallbackMsg,first=CustomJSON.parse(body).first;_this.checkHumanInstansce()&&_this.dhServer&&_this.dhServer.sendMessage({requestId:requestId||uuid.v4(),action:"TEXT_STREAM_RENDER",body:body,clientTs:(new Date).getTime()},first?onCallbackMsg:null)})),_rollupPluginBabelHelpers.defineProperty(this,"audioRender",(function(renderData){var body=renderData.body,onCallbackMsg=renderData.onCallbackMsg;_this.checkHumanInstansce()&&_this.dhServer&&_this.dhServer.sendMessage({requestId:uuid.v4(),action:"AUDIO_RENDER",body:body,clientTs:(new Date).getTime()},onCallbackMsg)})),_rollupPluginBabelHelpers.defineProperty(this,"audioStreamRender",(function(renderData){var requestId=renderData.requestId,body=renderData.body,onCallbackMsg=renderData.onCallbackMsg;_this.checkHumanInstansce()&&_this.dhServer&&_this.dhServer.sendMessage({requestId:requestId||uuid.v4(),action:"AUDIO_STREAM_RENDER",body:body,clientTs:(new Date).getTime()},onCallbackMsg)})),_rollupPluginBabelHelpers.defineProperty(this,"textQuery",(function(queryData){var requestId=queryData.requestId,body=queryData.body,onCallbackMsg=queryData.onCallbackMsg;_this.dhServer&&_this.dhServer.sendMessage({requestId:requestId||uuid.v4(),action:"TEXT_QUERY",body:body,clientTs:(new Date).getTime()},onCallbackMsg)})),_rollupPluginBabelHelpers.defineProperty(this,"destroy",_rollupPluginBabelHelpers.asyncToGenerator(_rollupPluginBabelHelpers.regeneratorRuntime().mark((function _callee3(){return _rollupPluginBabelHelpers.regeneratorRuntime().wrap((function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:return _context3.next=2,_this.dhServer&&_this.dhServer.closeSocket();case 2:_this.rtcServer&&_this.rtcServer.closeRtc(),DomControl.removeHumanDom(),_this.dhServer=null,_this.rtcServer=null;case 6:case"end":return _context3.stop()}}),_callee3)})))),_rollupPluginBabelHelpers.defineProperty(this,"pauseHuman",(function(){return DomControl.playHumanVideo(!1)})),_rollupPluginBabelHelpers.defineProperty(this,"playHuman",(function(){return DomControl.playHumanVideo(!0)})),_rollupPluginBabelHelpers.defineProperty(this,"muteHuman",(function(){return DomControl.muteHumanVideo(!0)})),_rollupPluginBabelHelpers.defineProperty(this,"unMuteHuman",(function(){var humanVideo=DomControl.muteHumanVideo(!1);try{return _this.isHuawei?(humanVideo.pause(),humanVideo.play()):humanVideo.paused&&humanVideo.play()}catch(e){console.info(e)}}));var _props$isPreOpenSessi=props.isPreOpenSession,isPreOpenSession=void 0!==_props$isPreOpenSessi&&_props$isPreOpenSessi,wsUrl=props.wsUrl,renderParams=props.renderParams,onDigitalHumanCallback=props.onDigitalHumanCallback;this.isPreOpenSession=isPreOpenSession,this.dhServer=new Server.default(wsUrl||"wss://open.xiling.baidu.com/cloud/digital-human-demonstration"),this.onDigitalHumanCallback=function(data){data.status!==_interface.statusEnum.DH_LIB_FULL_STATUS?onDigitalHumanCallback&&onDigitalHumanCallback(data):(null==renderParams?void 0:renderParams.fullStatus)&&onDigitalHumanCallback&&onDigitalHumanCallback(data)},isPreOpenSession?this.dhServer.createSocket({onDigitalHumanCallback:this.onDigitalHumanCallbackPlus},isPreOpenSession):this.init(props)}return _rollupPluginBabelHelpers.createClass(DhRealtimeHuman,[{key:"init",value:function(props){var _this2=this,wrapperId=props.wrapperId,token=props.token,appKey=props.appKey,appId=props.appId,connectParams=props.connectParams,_props$renderParams=props.renderParams,renderParams=void 0===_props$renderParams?{}:_props$renderParams,rtcConnectParams=props.rtcConnectParams,brtcParams=props.brtcParams,rtcServerUrl=props.rtcServerUrl;if(this.token=token,this.appKey=appKey,this.appId=appId,this.rtcServerUrl=rtcServerUrl,this.connectParams=_rollupPluginBabelHelpers.objectSpread2({autoChromaKey:(null==renderParams?void 0:renderParams.autoChromaKey)||!1},connectParams),!appId&&!appKey&&!token)throw new Error("appId and appKey or token is required");var closeLog=renderParams.closeLog,playerWrapper=(DomControl.createHumanDom({autoChromaKey:!!renderParams.autoChromaKey||!isEmpty(renderParams.chromaEffects)})||{}).playerWrapper,wrapperEle=document.getElementById(wrapperId||"");wrapperEle||console.error("dom with wrapperId is not find"),playerWrapper&&wrapperEle&&wrapperEle.appendChild(playerWrapper),DomControl.appendRtcDomStyle();var rtcConfig=_rollupPluginBabelHelpers.objectSpread2({remotevideoviewid:wrapperId,showvideobps:!1,usingvideo:!1,usingaudio:!(null==connectParams||!connectParams.pullAudioFromRtc),aspublisher:!(null==connectParams||!connectParams.pullAudioFromRtc),autopublish:!(null==connectParams||!connectParams.pullAudioFromRtc),usingdatachannel:!0,autoplaymuted:!1},brtcParams);this.rtcServer=new BrtcClient.default({closeLog:closeLog,videoWrapperId:DomControl.RTC_VIDEO_ID,rtcConfig:rtcConfig,bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback,onLocalStream:this.onLocalStream}}),this.cutoutInstance=new Cutout.default({sourceId:DomControl.RTC_VIDEO_ID,effects:null==renderParams?void 0:renderParams.chromaEffects}),isEmpty(rtcConnectParams)||(this.rtcConnectParams=rtcConnectParams,this.rtcServer.openRtc(rtcConnectParams)),null!=connectParams&&connectParams.pullAudioFromRtc?this.recordController=null:this.recordController=new RecordController.default({pickAudioMode:null==connectParams?void 0:connectParams.pickAudioMode,onAudioData:function(pcmData){1===_this2.wsReadyState&&_this2.sendMessage(pcmData,null)},onError:function(err){_this2.onDigitalHumanCallback&&_this2.onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_ERROR,content:{type:_interface.errorTypeEnum.CONNECT_ERROR,msg:{code:-1,errMsg:err.message}}})}})}},{key:"startRecord",value:function(){var _this$connectParams;null!==(_this$connectParams=this.connectParams)&&void 0!==_this$connectParams&&_this$connectParams.pullAudioFromRtc?this.rtcServer&&this.rtcServer.muteMicphone(!1):this.recordController&&this.recordController.start()}},{key:"stopRecord",value:function(){var _this$connectParams2;if(null!==(_this$connectParams2=this.connectParams)&&void 0!==_this$connectParams2&&_this$connectParams2.pullAudioFromRtc)return this.rtcServer&&this.rtcServer.muteMicphone(!0),void this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:uuid.v4(),clientTs:Date.now(),body:""},null);this.recordController&&this.recordController.stop(),this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:uuid.v4(),clientTs:Date.now(),body:""},null)}}]),DhRealtimeHuman}();exports.default=DhRealtimeHuman;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @file lib/Cutout
3
+ * @description 视频抠绿相关
4
+ * @author zhangyue49
5
+ */
6
+ import { ChromaEffectOptions } from '@bddh/starling-cutout/es/interface';
7
+ import Seriously from '@bddh/starling-cutout/es/Seriously';
8
+ interface CutoutType {
9
+ effects?: {
10
+ version: number;
11
+ chromaKey: Partial<ChromaEffectOptions>;
12
+ };
13
+ sourceId: string;
14
+ }
15
+ export declare const DEFAULT_EFFECTS: {
16
+ version: number;
17
+ chromaKey: {
18
+ screen: number[];
19
+ similarity: number;
20
+ edgeShrink: number;
21
+ smoothness: number;
22
+ spill: number;
23
+ opacity: number;
24
+ contrast: number;
25
+ brightness: number;
26
+ gamma: number;
27
+ };
28
+ };
29
+ declare class Cutout {
30
+ cutoutInstance: Seriously;
31
+ constructor(props: CutoutType);
32
+ setOptions(options: Partial<ChromaEffectOptions>): void;
33
+ destroy(): void;
34
+ updateSource(video: HTMLVideoElement | string): Promise<void>;
35
+ }
36
+ export default Cutout;
package/cjs/Cutout.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _rollupPluginBabelHelpers=require("./_virtual/_rollupPluginBabelHelpers.js"),Seriously=require("@bddh/starling-cutout/es/Seriously"),DEFAULT_EFFECTS={version:2,chromaKey:{screen:[53,186,123],similarity:90,edgeShrink:3,smoothness:80,spill:100,opacity:1,contrast:0,brightness:0,gamma:0}},Cutout=function(){function Cutout(props){_rollupPluginBabelHelpers.classCallCheck(this,Cutout),_rollupPluginBabelHelpers.defineProperty(this,"cutoutInstance",void 0);var effects=props.effects,sourceId=props.sourceId;this.cutoutInstance=new Seriously("starling-cutout-canvas",sourceId||"starling-cutout-video",effects&&effects.chromaKey||DEFAULT_EFFECTS.chromaKey,effects&&effects.version||DEFAULT_EFFECTS.version)}return _rollupPluginBabelHelpers.createClass(Cutout,[{key:"setOptions",value:function(options){options&&this.cutoutInstance&&this.cutoutInstance.setOptions(options)}},{key:"destroy",value:function(){return this.cutoutInstance.destroy()}},{key:"updateSource",value:function(video){return this.cutoutInstance.updateSource(video)}}]),Cutout}();exports.DEFAULT_EFFECTS=DEFAULT_EFFECTS,exports.default=Cutout;
@@ -0,0 +1,23 @@
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 declare function appendRtcDomStyle(): void;
23
+ export {};
@@ -0,0 +1 @@
1
+ "use strict";exports.DEFAULT_CANVAS_ID="starling-cutout-canvas",exports.HUMAN_WRAPPER_ID="cloud-brtc-player-wrapper",exports.RTC_VIDEO_ID="remotevideo500",exports.appendRtcDomStyle=function(){var style=document.createElement("style");style.appendChild(document.createTextNode('\n [id^="videoremote1-"] {\n display: none !important;\n }\n \n video#remotevideo500 {\n width: auto !important;\n height: auto !important;\n max-width: 100% !important;\n max-height: 100% !important;\n width: 100% !important;\n height: 100% !important;\n object-fit: cover;\n }\n\n .cloud-brtc-player-wrapper {\n width: inherit !important;\n background-size: cover;\n height: inherit;\n display: flex;\n justify-content: center;\n }\n\n #starling-cutout-canvas {\n object-fit: cover;\n }\n ')),document.head.appendChild(style)},exports.checkIsHuawei=function(){var ua=navigator.userAgent||navigator.vendor;return/huawei/i.test(ua)||/honor/i.test(ua)},exports.createHumanDom=function(options){var _options$autoChromaKe=options.autoChromaKey,autoChromaKey=void 0!==_options$autoChromaKe&&_options$autoChromaKe,_options$autoplay=options.autoplay,autoplay=void 0===_options$autoplay||_options$autoplay;if(document.getElementById("remotevideo500"))return console.error('DOM with id "'.concat("remotevideo500",'" already exists.')),null;var playerWrapper=document.createElement("div");playerWrapper.className="cloud-brtc-player-wrapper",playerWrapper.id="cloud-brtc-player-wrapper",playerWrapper.style.position="relative";var video=document.createElement("video");video.id="remotevideo500",video.width=0,video.height=0,video.setAttribute("width","100%"),video.setAttribute("height","100%"),video.setAttribute("data-playing","true"),video.setAttribute("playsinline","");var canvas=document.createElement("canvas");return canvas.id="starling-cutout-canvas",Object.assign(canvas.style,{position:"absolute",top:"0",left:"0",height:"100%",width:"100%",zIndex:"1",display:autoChromaKey?"block":"none"}),autoplay&&video.setAttribute("autoplay",""),autoChromaKey&&(video.style.visibility="hidden"),playerWrapper.appendChild(video),playerWrapper.appendChild(canvas),{playerWrapper:playerWrapper,video:video}},exports.muteHumanVideo=function(muted){var humanVideoDom=document.getElementById("remotevideo500");return humanVideoDom&&(humanVideoDom.muted=muted),humanVideoDom},exports.playHumanVideo=function(play){var humanVideoDom=document.getElementById("remotevideo500");try{if(humanVideoDom)return play?humanVideoDom.play():humanVideoDom.pause()}catch(e){console.info(e)}},exports.removeHumanDom=function(){var humanDom=document.getElementById("cloud-brtc-player-wrapper");humanDom&&humanDom.parentNode&&humanDom.parentNode.removeChild(humanDom)};
@@ -0,0 +1,20 @@
1
+ export type PickAudioMode = 'pressButton' | 'free';
2
+ interface RecordControllerOptions {
3
+ pickAudioMode?: PickAudioMode;
4
+ onAudioData: (pcmData: ArrayBuffer) => void;
5
+ onError?: (err: Error) => void;
6
+ pullAudioFromRtc?: boolean;
7
+ brtcClient?: any;
8
+ }
9
+ declare class RecordController {
10
+ private readonly onAudioData;
11
+ private readonly onError?;
12
+ private audioContext;
13
+ private processor;
14
+ private mediaStream;
15
+ private recording;
16
+ constructor(options: RecordControllerOptions);
17
+ start(): Promise<void>;
18
+ stop(): void;
19
+ }
20
+ export default RecordController;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _rollupPluginBabelHelpers=require("./_virtual/_rollupPluginBabelHelpers.js");function sampleData(pcmDatas,pcmSampleRate,newSampleRate,prevChunkInfo){try{for(var index=(prevChunkInfo=prevChunkInfo||{})&&"number"==typeof prevChunkInfo.index?prevChunkInfo.index:0,offset=prevChunkInfo&&"number"==typeof prevChunkInfo.offset?prevChunkInfo.offset:0,size=0,i=index;i<pcmDatas.length;i++)size+=pcmDatas[i].length;size=Math.max(0,size-Math.floor(offset));var step=pcmSampleRate/newSampleRate;step>1?size=Math.floor(size/step):(step=1,newSampleRate=pcmSampleRate);for(var res=new Int16Array(size),idx=0,nl=pcmDatas.length;index<nl;index++){for(var o=pcmDatas[index],_i=offset,il=o.length;_i<il;){var before=Math.floor(_i),after=Math.ceil(_i),atPoint=_i-before;res[idx]=o[before]+(o[after]-o[before])*atPoint,idx++,_i+=step}offset=_i-il}return{index:index,offset:offset,sampleRate:newSampleRate,data:res}}catch(error){console.log("转音错误了",error)}}function convertBuffer(arrayBuffer){for(var data=arrayBuffer,out=new Int16Array(arrayBuffer.length),i=0;i<data.length;i++){var s=Math.max(-1,Math.min(1,data[i]));out[i]=s<0?32768*s:32767*s}return out}var RecordController=function(_start){function RecordController(options){_rollupPluginBabelHelpers.classCallCheck(this,RecordController),_rollupPluginBabelHelpers.defineProperty(this,"onAudioData",void 0),_rollupPluginBabelHelpers.defineProperty(this,"onError",void 0),_rollupPluginBabelHelpers.defineProperty(this,"audioContext",null),_rollupPluginBabelHelpers.defineProperty(this,"processor",null),_rollupPluginBabelHelpers.defineProperty(this,"mediaStream",null),_rollupPluginBabelHelpers.defineProperty(this,"recording",!1),this.onAudioData=options.onAudioData,this.onError=options.onError}return _rollupPluginBabelHelpers.createClass(RecordController,[{key:"start",value:function(){return(_start=_start||_rollupPluginBabelHelpers.asyncToGenerator(_rollupPluginBabelHelpers.regeneratorRuntime().mark((function _callee(){var source,_this=this;return _rollupPluginBabelHelpers.regeneratorRuntime().wrap((function(_context){for(;;)switch(_context.prev=_context.next){case 0:if(!this.recording){_context.next=2;break}return _context.abrupt("return");case 2:return _context.prev=2,this.audioContext=new(window.AudioContext||window.webkitAudioContext)({sampleRate:16e3}),_context.next=6,navigator.mediaDevices.getUserMedia({audio:!0,video:!1});case 6:this.mediaStream=_context.sent,source=this.audioContext.createMediaStreamSource(this.mediaStream),this.processor=this.audioContext.createScriptProcessor(1024,1,1),source.connect(this.processor),this.processor.connect(this.audioContext.destination),this.processor.onaudioprocess=function(e){if(_this.recording){var resampled=sampleData([convertBuffer(e.inputBuffer.getChannelData(0))],_this.audioContext.sampleRate,16e3,null);resampled&&resampled.data&&_this.onAudioData(resampled.data.buffer)}},this.recording=!0,_context.next=18;break;case 15:_context.prev=15,_context.t0=_context.catch(2),this.onError&&this.onError(_context.t0);case 18:case"end":return _context.stop()}}),_callee,this,[[2,15]])})))).apply(this,arguments)}},{key:"stop",value:function(){this.recording=!1,this.processor&&(this.processor.disconnect(),this.processor.onaudioprocess=null),this.audioContext&&this.audioContext.close(),this.mediaStream&&this.mediaStream.getTracks().forEach((function(track){track.stop()}))}}]),RecordController}();exports.default=RecordController;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @file lib/Server
3
+ * @description ws服务逻辑处理
4
+ * @author zhangyue49
5
+ */
6
+ import { type 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 | ArrayBuffer;
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
+ reConnect?: boolean;
35
+ onConnect?: ConnectListener;
36
+ onDigitalHumanCallback?: (data: CallbackMsgType) => void;
37
+ }
38
+ export interface RenderCallbackRes {
39
+ requestId: string;
40
+ action: string;
41
+ code: number;
42
+ body: string | null;
43
+ message: string;
44
+ }
45
+ export type RenderCallback = (res: RenderCallbackRes) => void;
46
+ declare class Server {
47
+ dhServerSocket: WebSocketClient | null;
48
+ preConnectSuccess: boolean;
49
+ connectParams: createSocketParamsType | null;
50
+ private socketClosingPromise;
51
+ private readonly url;
52
+ private firstConnect;
53
+ constructor(url: string);
54
+ sendPreConnect: (props: createSocketParamsType, resolve?: ((value: any) => void) | undefined) => void;
55
+ createSocket: (props: createSocketParamsType, isPreOpenSession?: boolean) => Promise<WebSocketClient | null>;
56
+ closeSocket: () => Promise<void>;
57
+ handleConnect: () => Promise<void>;
58
+ sendMessage(message: DHServerInterface | ArrayBuffer, listener: RenderCallback | null): Promise<void>;
59
+ }
60
+ export default Server;
package/cjs/Server.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _rollupPluginBabelHelpers=require("./_virtual/_rollupPluginBabelHelpers.js"),uuid=require("uuid"),createWebSocket=require("@bddh/starling-web-socket/es/create-web-socket"),pick=require("lodash/pick"),omit=require("lodash/omit"),get=require("lodash/get"),_interface=require("./interface.js"),Server=function(_sendMessage){function Server(url){var _ref,_this=this;_rollupPluginBabelHelpers.classCallCheck(this,Server),_rollupPluginBabelHelpers.defineProperty(this,"dhServerSocket",null),_rollupPluginBabelHelpers.defineProperty(this,"preConnectSuccess",!1),_rollupPluginBabelHelpers.defineProperty(this,"connectParams",null),_rollupPluginBabelHelpers.defineProperty(this,"socketClosingPromise",null),_rollupPluginBabelHelpers.defineProperty(this,"url",void 0),_rollupPluginBabelHelpers.defineProperty(this,"firstConnect",!0),_rollupPluginBabelHelpers.defineProperty(this,"sendPreConnect",(function(props,resolve){var _this$dhServerSocket,token=props.token,sessionId=props.sessionId,onConnect=props.onConnect;null===(_this$dhServerSocket=_this.dhServerSocket)||void 0===_this$dhServerSocket||_this$dhServerSocket.request({action:"RE_CONNECT",requestId:uuid.v4(),body:JSON.stringify({token:token,sessionId:sessionId})}).on((function(res,off){null==onConnect||onConnect(res),0!==res.code&&(off(),_this.handleConnect()),null==resolve||resolve(_this.dhServerSocket)})),_this.firstConnect=!1})),_rollupPluginBabelHelpers.defineProperty(this,"createSocket",(function(_x,_x2){return(_ref=_ref||_rollupPluginBabelHelpers.asyncToGenerator(_rollupPluginBabelHelpers.regeneratorRuntime().mark((function _callee(props,isPreOpenSession){var sessionId,onDigitalHumanCallback;return _rollupPluginBabelHelpers.regeneratorRuntime().wrap((function(_context){for(;;)switch(_context.prev=_context.next){case 0:if(_this.connectParams=props,sessionId=props.sessionId,onDigitalHumanCallback=props.onDigitalHumanCallback,!_this.socketClosingPromise){_context.next=5;break}return _context.next=5,_this.socketClosingPromise;case 5:if(!_this.dhServerSocket){_context.next=8;break}return _context.next=8,_this.closeSocket();case 8:return _context.abrupt("return",new Promise((function(resolve,reject){var createPamrams={keepAlive:"heartbeat",reopen:get(_this.connectParams,"reConnect",!0),combine:function(req,res){return req&&res&&req.requestId===res.requestId},onOpen:function(){_this.dhServerSocket=socket,sessionId&&_this.firstConnect?_this.sendPreConnect(props,resolve):isPreOpenSession&&_this.firstConnect||_this.handleConnect()},onClose:function(){_this.dhServerSocket=null,_this.socketClosingPromise=null},onError:function(error){reject(error)}},socket=createWebSocket.createWebSocket(_this.url,createPamrams);socket.onReadyStateChange((function(readyState){0===readyState&&onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_INIT}),1===readyState&&isPreOpenSession&&(_this.preConnectSuccess=!0),3===readyState&&(_this.preConnectSuccess=!1),onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_FULL_STATUS,content:{type:"wsState",readyState:readyState,body:_interface.WS_STATUS_MAP[readyState]}})})),socket.onMessage((function(msgData){var action=msgData.action;[_interface.errorTypeEnum.TIMEOUT_EXIT,_interface.errorTypeEnum.DISCONNECT_ALERT].includes(action)&&onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_STATUS,content:{type:action}}),[_interface.errorTypeEnum.TIMEOUT_EXIT,_interface.errorTypeEnum.DISCONNECT_ALERT,"HEART_BEAT","CONNECT"].includes(action)||onDigitalHumanCallback&&onDigitalHumanCallback({status:_interface.statusEnum.DH_LIB_MESSAGE,content:msgData})})),isPreOpenSession&&(_this.dhServerSocket=socket),socket.open()})));case 9:case"end":return _context.stop()}}),_callee)})))).apply(this,arguments)})),_rollupPluginBabelHelpers.defineProperty(this,"closeSocket",_rollupPluginBabelHelpers.asyncToGenerator(_rollupPluginBabelHelpers.regeneratorRuntime().mark((function _callee2(){return _rollupPluginBabelHelpers.regeneratorRuntime().wrap((function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_this.dhServerSocket){_context2.next=2;break}return _context2.abrupt("return");case 2:return _this.socketClosingPromise=_this.dhServerSocket.close(),_context2.next=5,_this.socketClosingPromise;case 5:_this.dhServerSocket=null;case 6:case"end":return _context2.stop()}}),_callee2)})))),_rollupPluginBabelHelpers.defineProperty(this,"handleConnect",_rollupPluginBabelHelpers.asyncToGenerator(_rollupPluginBabelHelpers.regeneratorRuntime().mark((function _callee3(){var _this$connectParams,appId,appKey,token,parameters,onConnect;return _rollupPluginBabelHelpers.regeneratorRuntime().wrap((function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:if(_this.connectParams){_context3.next=2;break}return _context3.abrupt("return");case 2:if(_this$connectParams=_this.connectParams,appId=_this$connectParams.appId,appKey=_this$connectParams.appKey,token=_this$connectParams.token,parameters=_this$connectParams.parameters,onConnect=_this$connectParams.onConnect,appId||appKey||token){_context3.next=5;break}return _context3.abrupt("return");case 5:_this.dhServerSocket&&_this.dhServerSocket.request({action:"CONNECT",clientTs:(new Date).getTime(),requestId:uuid.v4(),body:JSON.stringify(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({token:token,appId:appId,appKey:appKey},pick(_this.connectParams,_interface.CONNECT_MAIN_KEY)),{},{parameters:_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({},omit(parameters,["parameters"])),parameters&&parameters.parameters?parameters.parameters:{})}))}).on((function(res,off){null==onConnect||onConnect(res),0!==res.code&&off()}));case 6:case"end":return _context3.stop()}}),_callee3)})))),this.url=url}return _rollupPluginBabelHelpers.createClass(Server,[{key:"sendMessage",value:function(_x3,_x4){return(_sendMessage=_sendMessage||_rollupPluginBabelHelpers.asyncToGenerator(_rollupPluginBabelHelpers.regeneratorRuntime().mark((function _callee4(message,listener){var requestId;return _rollupPluginBabelHelpers.regeneratorRuntime().wrap((function(_context4){for(;;)switch(_context4.prev=_context4.next){case 0:try{this.dhServerSocket&&this.dhServerSocket&&this.dhServerSocket.request(message).on((function(response){listener&&listener(response)}))}catch(e){requestId="object"===_rollupPluginBabelHelpers.typeof(message)&&"requestId"in message?message.requestId:uuid.v4(),listener&&listener({code:-11,body:null,action:"RENDER_ERROR",requestId:requestId,message:JSON.stringify(e)})}case 1:case"end":return _context4.stop()}}),_callee4,this)})))).apply(this,arguments)}}]),Server}();exports.default=Server;
@@ -0,0 +1 @@
1
+ "use strict";function ownKeys(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);r&&(o=o.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,o)}return t}function _regeneratorRuntime(){_regeneratorRuntime=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",c=i.asyncIterator||"@@asyncIterator",u=i.toStringTag||"@@toStringTag";function define(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{define({},"")}catch(t){define=function(t,e,r){return t[e]=r}}function wrap(t,e,r,n){var i=e&&e.prototype instanceof Generator?e:Generator,a=Object.create(i.prototype),c=new Context(n||[]);return o(a,"_invoke",{value:makeInvokeMethod(t,r,c)}),a}function tryCatch(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=wrap;var h="suspendedStart",l="suspendedYield",f="executing",s="completed",y={};function Generator(){}function GeneratorFunction(){}function GeneratorFunctionPrototype(){}var p={};define(p,a,(function(){return this}));var d=Object.getPrototypeOf,v=d&&d(d(values([])));v&&v!==r&&n.call(v,a)&&(p=v);var g=GeneratorFunctionPrototype.prototype=Generator.prototype=Object.create(p);function defineIteratorMethods(t){["next","throw","return"].forEach((function(e){define(t,e,(function(t){return this._invoke(e,t)}))}))}function AsyncIterator(t,e){function invoke(r,o,i,a){var c=tryCatch(t[r],t,o);if("throw"!==c.type){var u=c.arg,h=u.value;return h&&"object"==typeof h&&n.call(h,"__await")?e.resolve(h.__await).then((function(t){invoke("next",t,i,a)}),(function(t){invoke("throw",t,i,a)})):e.resolve(h).then((function(t){u.value=t,i(u)}),(function(t){return invoke("throw",t,i,a)}))}a(c.arg)}var r;o(this,"_invoke",{value:function(t,n){function callInvokeWithMethodAndArg(){return new e((function(e,r){invoke(t,n,e,r)}))}return r=r?r.then(callInvokeWithMethodAndArg,callInvokeWithMethodAndArg):callInvokeWithMethodAndArg()}})}function makeInvokeMethod(e,r,n){var o=h;return function(i,a){if(o===f)throw new Error("Generator is already running");if(o===s){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var c=n.delegate;if(c){var u=maybeInvokeDelegate(c,n);if(u){if(u===y)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=s,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=f;var p=tryCatch(e,r,n);if("normal"===p.type){if(o=n.done?s:l,p.arg===y)continue;return{value:p.arg,done:n.done}}"throw"===p.type&&(o=s,n.method="throw",n.arg=p.arg)}}}function maybeInvokeDelegate(e,r){var n=r.method,o=e.iterator[n];if(o===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,maybeInvokeDelegate(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var i=tryCatch(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,y;var a=i.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,y):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function pushTryEntry(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function resetTryEntry(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function Context(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(pushTryEntry,this),this.reset(!0)}function values(e){if(e||""===e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function next(){for(;++o<e.length;)if(n.call(e,o))return next.value=e[o],next.done=!1,next;return next.value=t,next.done=!0,next};return i.next=i}}throw new TypeError(typeof e+" is not iterable")}return GeneratorFunction.prototype=GeneratorFunctionPrototype,o(g,"constructor",{value:GeneratorFunctionPrototype,configurable:!0}),o(GeneratorFunctionPrototype,"constructor",{value:GeneratorFunction,configurable:!0}),GeneratorFunction.displayName=define(GeneratorFunctionPrototype,u,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===GeneratorFunction||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,GeneratorFunctionPrototype):(t.__proto__=GeneratorFunctionPrototype,define(t,u,"GeneratorFunction")),t.prototype=Object.create(g),t},e.awrap=function(t){return{__await:t}},defineIteratorMethods(AsyncIterator.prototype),define(AsyncIterator.prototype,c,(function(){return this})),e.AsyncIterator=AsyncIterator,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new AsyncIterator(wrap(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},defineIteratorMethods(g),define(g,u,"Generator"),define(g,a,(function(){return this})),define(g,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function next(){for(;r.length;){var t=r.pop();if(t in e)return next.value=t,next.done=!1,next}return next.done=!0,next}},e.values=values,Context.prototype={constructor:Context,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(resetTryEntry),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function handle(n,o){return a.type="throw",a.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return handle("end");if(i.tryLoc<=this.prev){var c=n.call(i,"catchLoc"),u=n.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return handle(i.catchLoc,!0);if(this.prev<i.finallyLoc)return handle(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return handle(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return handle(i.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),resetTryEntry(r),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;resetTryEntry(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:values(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},e}function _typeof(o){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg),value=info.value}catch(error){return void reject(error)}info.done?resolve(value):Promise.resolve(value).then(_next,_throw)}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,_toPropertyKey(descriptor.key),descriptor)}}function _defineProperty(obj,key,value){return(key=_toPropertyKey(key))in obj?Object.defineProperty(obj,key,{value:value,enumerable:!0,configurable:!0,writable:!0}):obj[key]=value,obj}function _toPrimitive(input,hint){if("object"!=typeof input||null===input)return input;var prim=input[Symbol.toPrimitive];if(void 0!==prim){var res=prim.call(input,hint||"default");if("object"!=typeof res)return res;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===hint?String:Number)(input)}function _toPropertyKey(arg){var key=_toPrimitive(arg,"string");return"symbol"==typeof key?key:String(key)}exports.asyncToGenerator=function(fn){return function(){var self=this,args=arguments;return new Promise((function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value)}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err)}_next(void 0)}))}},exports.classCallCheck=function(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")},exports.createClass=function(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Object.defineProperty(Constructor,"prototype",{writable:!1}),Constructor},exports.defineProperty=_defineProperty,exports.objectSpread2=function(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ownKeys(Object(t),!0).forEach((function(r){_defineProperty(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ownKeys(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e},exports.regeneratorRuntime=_regeneratorRuntime,exports.toPrimitive=_toPrimitive,exports.toPropertyKey=_toPropertyKey,exports.typeof=_typeof;
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 Client=require("./Client.js"),_interface=require("./interface.js");exports.default=Client.default,exports.CONNECT_MAIN_KEY=_interface.CONNECT_MAIN_KEY,exports.WS_STATUS_MAP=_interface.WS_STATUS_MAP,Object.defineProperty(exports,"errorTypeEnum",{enumerable:!0,get:function(){return _interface.errorTypeEnum}}),Object.defineProperty(exports,"statusEnum",{enumerable:!0,get:function(){return _interface.statusEnum}});
@@ -0,0 +1,108 @@
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
+ x264BitRate?: number;
13
+ cameraId?: string | number;
14
+ characterOffset?: string;
15
+ pickAudioMode?: 'pressButton' | 'free';
16
+ autoChromaKey?: boolean;
17
+ usingVideo?: boolean;
18
+ ttsPer?: string | number;
19
+ ttsPitch?: number;
20
+ ttsSpeed?: number;
21
+ ttsVolume?: number;
22
+ ttsLan?: string;
23
+ pullAudioFromRtc?: boolean;
24
+ inactiveDisconnectSec?: number;
25
+ preAlertSec?: number;
26
+ backgroundImageUrl?: string;
27
+ projectId?: string;
28
+ characterConfigId?: string;
29
+ selectors?: any;
30
+ figureName?: string;
31
+ configVersion?: string;
32
+ extraInfo?: any;
33
+ roomName?: string;
34
+ paintSubtitleOnPicture?: boolean;
35
+ h5Embedded?: boolean;
36
+ paintWidgetOnPicture?: boolean;
37
+ extraParameters?: any;
38
+ parameters?: any;
39
+ reConnect?: boolean;
40
+ }
41
+ export declare const CONNECT_MAIN_KEY: string[];
42
+ export interface RenderParamsType {
43
+ chromaEffects?: object;
44
+ autoChromaKey?: boolean;
45
+ closeLog?: boolean;
46
+ fullStatus?: boolean;
47
+ }
48
+ export declare enum statusEnum {
49
+ DH_LIB_INIT = "DH_LIB_INIT",
50
+ DH_LIB_WS_SUCCESS = "DH_LIB_WS_SUCCESS",
51
+ DH_LIB_OPEN = "DH_LIB_OPEN",
52
+ DH_LIB_STATUS = "DH_LIB_STATUS",
53
+ DH_LIB_CLOSE = "DH_LIB_CLOSE",
54
+ DH_LIB_ERROR = "DH_LIB_ERROR",
55
+ DH_LIB_WARNING = "DH_LIB_WARNING",
56
+ DH_LIB_FULL_STATUS = "DH_LIB_FULL_STATUS",
57
+ DH_LIB_MESSAGE = "DH_LIB_MESSAGE"
58
+ }
59
+ export declare enum errorTypeEnum {
60
+ CONNECT_ERROR = "CONNECT_ERROR",
61
+ DISCONNECT_ALERT = "DISCONNECT_ALERT",
62
+ TIMEOUT_EXIT = "TIMEOUT_EXIT",
63
+ LOCAL_VIDEO_MUTED = "LOCAL_VIDEO_MUTED",
64
+ LOCAL_VIDEO_ERROR = "LOCAL_VIDEO_ERROR",
65
+ RTC_ERROR = "RTC_ERROR"
66
+ }
67
+ export interface ErrorType {
68
+ type: errorTypeEnum;
69
+ msg?: {
70
+ code: string | number;
71
+ errMsg: string;
72
+ };
73
+ }
74
+ export interface RtcStateType {
75
+ action: string;
76
+ type: string;
77
+ body: any;
78
+ }
79
+ export declare const WS_STATUS_MAP: {
80
+ '-1': string;
81
+ '0': string;
82
+ '1': string;
83
+ '2': string;
84
+ '3': string;
85
+ };
86
+ export interface WsStateType {
87
+ readyState: number;
88
+ type: string;
89
+ body: string;
90
+ }
91
+ export interface CallbackMsgType {
92
+ status: statusEnum;
93
+ content?: ErrorType | WsStateType | RtcStateType | ConnectDataType;
94
+ }
95
+ export interface DhRealtimeHumanType {
96
+ token?: string;
97
+ wsUrl?: string;
98
+ rtcServerUrl?: string;
99
+ appKey?: string;
100
+ appId?: string;
101
+ wrapperId?: string;
102
+ connectParams?: ConnectParamsType;
103
+ renderParams?: RenderParamsType;
104
+ rtcConnectParams?: RtcConnectionType;
105
+ brtcParams?: any;
106
+ isPreOpenSession?: boolean;
107
+ onDigitalHumanCallback: (data: CallbackMsgType) => void;
108
+ }
@@ -0,0 +1 @@
1
+ "use strict";var statusEnum,errorTypeEnum;exports.statusEnum=void 0,(statusEnum=exports.statusEnum||(exports.statusEnum={})).DH_LIB_INIT="DH_LIB_INIT",statusEnum.DH_LIB_WS_SUCCESS="DH_LIB_WS_SUCCESS",statusEnum.DH_LIB_OPEN="DH_LIB_OPEN",statusEnum.DH_LIB_STATUS="DH_LIB_STATUS",statusEnum.DH_LIB_CLOSE="DH_LIB_CLOSE",statusEnum.DH_LIB_ERROR="DH_LIB_ERROR",statusEnum.DH_LIB_WARNING="DH_LIB_WARNING",statusEnum.DH_LIB_FULL_STATUS="DH_LIB_FULL_STATUS",statusEnum.DH_LIB_MESSAGE="DH_LIB_MESSAGE",exports.errorTypeEnum=void 0,(errorTypeEnum=exports.errorTypeEnum||(exports.errorTypeEnum={})).CONNECT_ERROR="CONNECT_ERROR",errorTypeEnum.DISCONNECT_ALERT="DISCONNECT_ALERT",errorTypeEnum.TIMEOUT_EXIT="TIMEOUT_EXIT",errorTypeEnum.LOCAL_VIDEO_MUTED="LOCAL_VIDEO_MUTED",errorTypeEnum.LOCAL_VIDEO_ERROR="LOCAL_VIDEO_ERROR",errorTypeEnum.RTC_ERROR="RTC_ERROR";exports.CONNECT_MAIN_KEY=["projectId","characterConfigId","configVersion","selectors","figureName","reConnect"],exports.WS_STATUS_MAP={"-1":"UNINSTANTIATED",0:"CONNECTING",1:"OPEN",2:"CLOSING",3:"CLOSED"};
@@ -0,0 +1,97 @@
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/es/interface';
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
+ videoWrapperId: string;
70
+ bizConfig: BizConfig;
71
+ closeLog?: boolean;
72
+ rtcConnection?: RtcConnectionType;
73
+ preConnectInfo?: RtcConnectionType;
74
+ rtcConfig?: RtcConfig;
75
+ rtcCallback?: Partial<RtcCallback>;
76
+ }
77
+ declare class BrtcClient {
78
+ client?: BRtcInstance;
79
+ isRtcVideoOn: boolean;
80
+ private rtcConnection;
81
+ private readonly rtcConfig?;
82
+ private readonly bizConfig?;
83
+ private readonly rtcCallback?;
84
+ private restarting;
85
+ private readonly timeoutEvent;
86
+ private reopenTimer;
87
+ private restartCount;
88
+ private readonly closeLog;
89
+ private haveVideoOn;
90
+ private rtcStateCallback;
91
+ constructor(props: BrtcClientType);
92
+ openRtc: (rtcConnection: RtcConnectionType) => void;
93
+ closeRtc: () => void;
94
+ restartRtc: (rtcConnection: RtcConnectionType) => void;
95
+ muteMicphone(mute: boolean): void;
96
+ }
97
+ export default BrtcClient;
@@ -0,0 +1 @@
1
+ import{createClass as _createClass,classCallCheck as _classCallCheck,defineProperty as _defineProperty}from"./_virtual/_rollupPluginBabelHelpers.js";import BrtcInstance from"@bddh/starling-brtc";import{TimeoutController}from"@bddh/starling-timeout-controller";import CustomJSON from"@bddh/starling-json";import isEmpty from"lodash/isEmpty";import isFunction from"lodash/isFunction";import{statusEnum,errorTypeEnum}from"./interface.js";var BrtcClient=function(){function BrtcClient(props){var _this=this;_classCallCheck(this,BrtcClient),_defineProperty(this,"client",void 0),_defineProperty(this,"isRtcVideoOn",!1),_defineProperty(this,"rtcConnection",void 0),_defineProperty(this,"rtcConfig",void 0),_defineProperty(this,"bizConfig",void 0),_defineProperty(this,"rtcCallback",void 0),_defineProperty(this,"restarting",!1),_defineProperty(this,"timeoutEvent",void 0),_defineProperty(this,"reopenTimer",void 0),_defineProperty(this,"restartCount",0),_defineProperty(this,"closeLog",!0),_defineProperty(this,"haveVideoOn",!1),_defineProperty(this,"rtcStateCallback",null),_defineProperty(this,"openRtc",(function(rtcConnection){if(_this.restarting)!_this.closeLog&&console.warn("rtc restarting");else if(rtcConnection&&(_this.rtcConnection=rtcConnection),_this.rtcConnection&&!isEmpty(_this.rtcConnection)){var _this$rtcConnection=_this.rtcConnection,server=_this$rtcConnection.server,appId=_this$rtcConnection.appId,token=_this$rtcConnection.token,roomName=_this$rtcConnection.roomName,userId=_this$rtcConnection.userId,_ref=_this.rtcCallback||{},_onRemotedata=_ref.onRemotedata,_onLocalStream=_ref.onLocalStream,_onRemoteVideoComing=_ref.onRemoteVideoComing,_onLocalVideoPublished=_ref.onLocalVideoPublished,_onLocalVideoPublishing=_ref.onLocalVideoPublishing,_onRemoteVideoOn=_ref.onRemoteVideoOn,_onRemoteVideoOff=_ref.onRemoteVideoOff,_onRemoteVideoLoading=_ref.onRemoteVideoLoading,_onSuccess=_ref.onSuccess,_onError=_ref.onError,_onLocalVideoConnected=_ref.onLocalVideoConnected,_onRemoteVideoConnected=_ref.onRemoteVideoConnected,_onRemoteMediaState=_ref.onRemoteMediaState,onPlayingError=_ref.onPlayingError,onDigitalHumanCallback=_ref.onDigitalHumanCallback;_this.timeoutEvent.start(),_this.rtcStateCallback=function(data){var body=data.body,action=data.action;onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_FULL_STATUS,content:{type:"rtcState",action:action,body:body}})},_this.client=new BrtcInstance({roomName:roomName,token:token,userId:userId,appId:appId,server:server,rtcConfig:_this.rtcConfig,callback:{onRemoteVideoConnected:function(id,connected){if(!connected&&!_this.haveVideoOn)var connectTimer=setTimeout((function(){_this.restartCount<3?(_this.restartCount=++_this.restartCount,_this.restartRtc&&_this.restartRtc(rtcConnection)):(_this.restartCount=0,_this.closeRtc()),clearTimeout(connectTimer)}),500);if(connected){var videoElement=document.getElementById("remotevideo500");videoElement.muted=!1;var promise=videoElement.play();void 0!==promise&&promise.catch((function(){videoElement.muted=!0,videoElement.play().catch((function(e){!_this.closeLog&&console.info("videoElement play error",e),onPlayingError&&onPlayingError(videoElement),onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_ERROR,content:{type:errorTypeEnum.LOCAL_VIDEO_ERROR}})})),onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_STATUS,content:{type:errorTypeEnum.LOCAL_VIDEO_MUTED}})}))}!_this.closeLog&&console.info("remoteVideoConnected",id,connected,_this.restartCount),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remoteVideoConnected",body:{id:id,connected:connected,restartCount:_this.restartCount}}),isFunction(_onRemoteVideoConnected)&&_onRemoteVideoConnected(id,connected)},onRemoteVideoOn:function(id){_this.haveVideoOn=!0,!_this.closeLog&&console.info("remotevideoon by id: ",id),_this.timeoutEvent.clear();var rtcContainer=document.querySelector("#starlingRtcContainer"),therevideo=rtcContainer&&rtcContainer.lastChild;therevideo&&(therevideo.style.display="none"),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remotevideoon",body:id}),onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_OPEN}),isFunction(_onRemoteVideoOn)&&_onRemoteVideoOn(id)},onRemoteMediaState:function(id,medium,connected){!_this.closeLog&&console.info("remoteMediaState",id,medium,connected),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remoteMediaState",body:{id:id,medium:medium,connected:connected}}),isFunction(_onRemoteMediaState)&&_onRemoteMediaState(id,medium,connected)},onLocalVideoConnected:function(connected){!_this.closeLog&&console.info("localVideoConnected",connected),_this.rtcStateCallback&&_this.rtcStateCallback({action:"localVideoConnected",body:{connected:connected}}),isFunction(_onLocalVideoConnected)&&_onLocalVideoConnected(connected)},onRemotedata:function(data){if(_this.rtcConfig&&_this.rtcConfig.usingdatachannel){var msg=CustomJSON.parse(data);!_this.closeLog&&console.info("RTC - receive:",msg),isFunction(_onRemotedata)&&_onRemotedata(msg)}},onLocalStream:function(stream,name){!_this.closeLog&&console.info("rtc - onlocalstream by name: "+name),_this.rtcStateCallback&&_this.rtcStateCallback({action:"onlocalstream",body:{stream:stream,name:name}}),isFunction(_onLocalStream)&&_onLocalStream(stream,name)},onLocalVideoPublished:function(){!_this.closeLog&&console.info("LocalVideoPublished 本地视频发布成功"),_this.rtcStateCallback&&_this.rtcStateCallback({action:"localvideopublished_ok",body:!0}),isFunction(_onLocalVideoPublished)&&_onLocalVideoPublished()},onLocalVideoPublishing:function(){!_this.closeLog&&console.info("LocalVideoPublishing 本地视频发布成功"),_this.rtcStateCallback&&_this.rtcStateCallback({action:"localvideopublishing",body:!0}),isFunction(_onLocalVideoPublishing)&&_onLocalVideoPublishing()},onRemoteVideoComing:function(id){!_this.closeLog&&console.info("remotevideocoming",id),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remotevideocoming",body:id}),isFunction(_onRemoteVideoComing)&&_onRemoteVideoComing(id)},onRemoteVideoLoading:function(id){!_this.closeLog&&console.info("remotevideoloading by id: ",id),_this.rtcStateCallback&&_this.rtcStateCallback({action:"remotevideoloading",body:id}),isFunction(_onRemoteVideoLoading)&&_onRemoteVideoLoading(id)},onRemoteVideoOff:function(id){var videoEle=document.getElementById("remotevideo500");videoEle&&videoEle.pause(),!_this.closeLog&&console.info("remotevideooff: ",id),_this.isRtcVideoOn=!1,_this.rtcStateCallback&&_this.rtcStateCallback({action:"remotevideooff",body:id}),onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_CLOSE}),isFunction(_onRemoteVideoOff)&&_onRemoteVideoOff(id),_this.timeoutEvent.start()},onSuccess:function(){!_this.closeLog&&console.info("rtc - success"),_this.rtcStateCallback&&_this.rtcStateCallback({action:"success",body:!0}),isFunction(_onSuccess)&&_onSuccess()},onError:function(e){_this.isRtcVideoOn=!1,!_this.closeLog&&console.error("rtc - error:",e),_this.rtcStateCallback&&_this.rtcStateCallback({action:"error",body:e}),onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_ERROR,content:{type:errorTypeEnum.RTC_ERROR}}),isFunction(_onError)&&_onError(e),(e.includes("Lost connection to the server")||e.includes("Is the server down"))&&_this.restartRtc&&_this.restartRtc(rtcConnection)}}})}})),_defineProperty(this,"closeRtc",(function(){_this.timeoutEvent.clear(),clearTimeout(_this.reopenTimer),_this.client&&_this.client.destroy(),_this.restarting=!1})),_defineProperty(this,"restartRtc",(function(rtcConnection){!_this.restarting&&_this.bizConfig&&_this.bizConfig.autoReConnect&&(_this.rtcStateCallback&&_this.rtcStateCallback({action:"restartRtc",body:rtcConnection}),_this.restarting=!0,_this.closeRtc(),_this.reopenTimer=setTimeout((function(){!_this.closeLog&&console.error("拉流失败,开始重启"),_this.restarting=!1,_this.openRtc(rtcConnection)}),2e3))}));var _rtcConnection=props.rtcConnection,rtcConfig=props.rtcConfig,_props$closeLog=props.closeLog,closeLog=void 0===_props$closeLog||_props$closeLog,rtcCallback=props.rtcCallback;this.rtcConnection=_rtcConnection,this.rtcConfig=rtcConfig,this.rtcCallback=rtcCallback,this.closeLog=closeLog,this.timeoutEvent=new TimeoutController(6e4,(function(){if(!_this.rtcConnection)return!_this.closeLog&&console.warn("remotevideoon事件超时, rtcConnectionRef is null");_this.timeoutEvent.clear(),_this.restartRtc&&_this.restartRtc(_this.rtcConnection)}))}return _createClass(BrtcClient,[{key:"muteMicphone",value:function(mute){this.client&&this.client.muteMicphone&&this.client.muteMicphone(mute)}}]),BrtcClient}();export{BrtcClient as default};
package/es/Client.d.ts ADDED
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @file dh-realtime-human
3
+ * @description 云渲染数字人sdk
4
+ * @author zhangyue49
5
+ */
6
+ import { DhRealtimeHumanType, ConnectDataType, CallbackMsgType } from './interface';
7
+ import { DHServerInterface, RenderCallback } from './Server';
8
+ export default class DhRealtimeHuman {
9
+ private token;
10
+ private appKey;
11
+ private appId;
12
+ private connectParams;
13
+ private readonly onDigitalHumanCallback?;
14
+ private dhServer;
15
+ private rtcServer?;
16
+ private rtcConnectParams;
17
+ private cutoutInstance?;
18
+ private readonly isHuawei;
19
+ private rtcServerUrl;
20
+ private recordController;
21
+ private wsReadyState;
22
+ private readonly isPreOpenSession;
23
+ constructor(props: DhRealtimeHumanType);
24
+ init(props: DhRealtimeHumanType): void;
25
+ onDigitalHumanCallbackPlus: (data: CallbackMsgType) => void;
26
+ onRemoteVideoComing: (id: number) => void;
27
+ onLocalStream: () => void;
28
+ onRemoteVideoOn: () => void;
29
+ connectListener: (data: ConnectDataType) => void;
30
+ createServer: (sessionId?: string) => void;
31
+ checkHumanInstansce: () => boolean;
32
+ sendMessage: (message: DHServerInterface | ArrayBuffer, listener: RenderCallback | null) => Promise<void> | null;
33
+ interrupt: () => Promise<void>;
34
+ textRender: (renderData: {
35
+ requestId: string;
36
+ body: string;
37
+ onCallbackMsg: RenderCallback;
38
+ }) => void;
39
+ textStreamRender: (renderData: {
40
+ requestId?: string;
41
+ body: string;
42
+ onCallbackMsg: RenderCallback;
43
+ }) => void;
44
+ audioRender: (renderData: {
45
+ body: string;
46
+ onCallbackMsg: RenderCallback;
47
+ }) => void;
48
+ audioStreamRender: (renderData: {
49
+ requestId?: string;
50
+ body: string;
51
+ onCallbackMsg: RenderCallback;
52
+ }) => void;
53
+ textQuery: (queryData: {
54
+ requestId: string;
55
+ body: string;
56
+ onCallbackMsg: RenderCallback;
57
+ }) => void;
58
+ destroy: () => Promise<void>;
59
+ pauseHuman: () => void | Promise<void>;
60
+ playHuman: () => void | Promise<void>;
61
+ muteHuman: () => HTMLVideoElement;
62
+ unMuteHuman: () => false | Promise<void> | undefined;
63
+ startRecord(): void;
64
+ stopRecord(): void;
65
+ }
package/es/Client.js ADDED
@@ -0,0 +1 @@
1
+ import{createClass as _createClass,classCallCheck as _classCallCheck,defineProperty as _defineProperty,objectSpread2 as _objectSpread2,asyncToGenerator as _asyncToGenerator,regeneratorRuntime as _regeneratorRuntime}from"./_virtual/_rollupPluginBabelHelpers.js";import CustomJSON from"@bddh/starling-json";import isEmpty from"lodash/isEmpty";import omit from"lodash/omit";import pick from"lodash/pick";import{v4}from"uuid";import{statusEnum,errorTypeEnum,CONNECT_MAIN_KEY}from"./interface.js";import Server from"./Server.js";import BrtcClient from"./BrtcClient.js";import{createHumanDom,appendRtcDomStyle,RTC_VIDEO_ID,checkIsHuawei,playHumanVideo,muteHumanVideo,removeHumanDom,DEFAULT_CANVAS_ID}from"./DomControl.js";import Cutout from"./Cutout.js";import RecordController from"./RecordController.js";var DhRealtimeHuman=function(){function DhRealtimeHuman(props){var _this=this;_classCallCheck(this,DhRealtimeHuman),_defineProperty(this,"token",void 0),_defineProperty(this,"appKey",void 0),_defineProperty(this,"appId",void 0),_defineProperty(this,"connectParams",void 0),_defineProperty(this,"onDigitalHumanCallback",void 0),_defineProperty(this,"dhServer",void 0),_defineProperty(this,"rtcServer",void 0),_defineProperty(this,"rtcConnectParams",void 0),_defineProperty(this,"cutoutInstance",void 0),_defineProperty(this,"isHuawei",checkIsHuawei()),_defineProperty(this,"rtcServerUrl",void 0),_defineProperty(this,"recordController",null),_defineProperty(this,"wsReadyState",-1),_defineProperty(this,"isPreOpenSession",!1),_defineProperty(this,"onDigitalHumanCallbackPlus",(function(data){var _data$content;data.status===statusEnum.DH_LIB_FULL_STATUS&&"wsState"===(null===(_data$content=data.content)||void 0===_data$content?void 0:_data$content.type)&&(_this.wsReadyState=data.content.readyState),_this.onDigitalHumanCallback&&_this.onDigitalHumanCallback(data)})),_defineProperty(this,"onRemoteVideoComing",(function(id){_this.rtcConnectParams&&_this.rtcConnectParams.feedId&&id===+_this.rtcConnectParams.feedId&&_this.rtcServer&&_this.rtcServer.client&&_this.rtcServer.client.subscribeStreaming("remotevideo500",+_this.rtcConnectParams.feedId)})),_defineProperty(this,"onLocalStream",(function(){(_this.connectParams||{}).pullAudioFromRtc&&_this.rtcServer&&_this.rtcServer.muteMicphone(!0)})),_defineProperty(this,"onRemoteVideoOn",(function(){setTimeout(_asyncToGenerator(_regeneratorRuntime().mark((function _callee(){var video,videoWidth,videoHeight,canvas,ctx;return _regeneratorRuntime().wrap((function(_context){for(;;)switch(_context.prev=_context.next){case 0:return video=document.getElementById(RTC_VIDEO_ID),_context.next=3,_this.cutoutInstance&&_this.cutoutInstance.updateSource(RTC_VIDEO_ID);case 3:video&&(video.setAttribute("data-playing","true"),videoWidth=video.videoWidth,videoHeight=video.videoHeight,canvas=document.getElementById(DEFAULT_CANVAS_ID),ctx=canvas.getContext("2d"),canvas&&ctx&&(canvas.width=videoWidth,canvas.height=videoHeight,ctx.drawImage(video,0,0,videoWidth,videoHeight)));case 4:case"end":return _context.stop()}}),_callee)}))),100)})),_defineProperty(this,"connectListener",(function(data){var code=data.code,body=data.body,message=data.message;if(0===code&&_this.onDigitalHumanCallback&&_this.onDigitalHumanCallback({status:statusEnum.DH_LIB_WS_SUCCESS,content:data}),0===code){var _data=CustomJSON.parse(body),server=_data.rtcServerUrl,appId=_data.appId,token=_data.clientToken,roomName=_data.roomName,userId=_data.clientId,feedId=_data.feedId,chargeId=_data.chargeId;appId&&roomName&&(_this.rtcConnectParams={server:_this.rtcServerUrl||server,appId:appId,token:token,roomName:roomName,userId:userId,feedId:feedId,chargeId:chargeId},_this.rtcServer&&_this.rtcServer.openRtc(_this.rtcConnectParams))}else _this.onDigitalHumanCallback&&_this.onDigitalHumanCallback({status:statusEnum.DH_LIB_ERROR,content:{type:errorTypeEnum.CONNECT_ERROR,msg:{code:code,errMsg:message}}})})),_defineProperty(this,"createServer",(function(sessionId){var _this$dhServer,params=_objectSpread2(_objectSpread2(_objectSpread2({token:_this.token,appKey:_this.appKey,appId:_this.appId},sessionId?{sessionId:sessionId}:{}),pick(_this.connectParams,CONNECT_MAIN_KEY)),{},{parameters:omit(_this.connectParams,CONNECT_MAIN_KEY),onConnect:_this.connectListener,onDigitalHumanCallback:_this.onDigitalHumanCallbackPlus});null!==(_this$dhServer=_this.dhServer)&&void 0!==_this$dhServer&&_this$dhServer.preConnectSuccess?(_this.dhServer.connectParams=params,_this.dhServer&&_this.dhServer.sendPreConnect(params)):_this.dhServer&&_this.dhServer.createSocket(params)})),_defineProperty(this,"checkHumanInstansce",(function(){return _this.dhServer?!(_this.isPreOpenSession&&!_this.dhServer.preConnectSuccess)||(_this.onDigitalHumanCallback&&_this.onDigitalHumanCallback({status:statusEnum.DH_LIB_ERROR,content:{type:errorTypeEnum.CONNECT_ERROR,msg:{code:4001,errMsg:"webSocket连接中"}}}),!1):(_this.onDigitalHumanCallback&&_this.onDigitalHumanCallback({status:statusEnum.DH_LIB_ERROR,content:{type:errorTypeEnum.CONNECT_ERROR,msg:{code:4002,errMsg:"请先初始化数字人实例"}}}),!1)})),_defineProperty(this,"sendMessage",(function(message,listener){return _this.dhServer&&_this.dhServer.sendMessage(message,listener)})),_defineProperty(this,"interrupt",_asyncToGenerator(_regeneratorRuntime().mark((function _callee2(){return _regeneratorRuntime().wrap((function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:return _context2.abrupt("return",new Promise((function(resolve){_this.dhServer&&_this.dhServer.sendMessage({requestId:v4(),action:"TEXT_RENDER",body:"<interrupt></interrupt>",clientTs:(new Date).getTime()},(function(){return resolve()}))})));case 1:case"end":return _context2.stop()}}),_callee2)})))),_defineProperty(this,"textRender",(function(renderData){var requestId=renderData.requestId,body=renderData.body,onCallbackMsg=renderData.onCallbackMsg;_this.checkHumanInstansce()&&_this.dhServer&&_this.dhServer.sendMessage({requestId:requestId||v4(),action:"TEXT_RENDER",body:body,clientTs:(new Date).getTime()},onCallbackMsg)})),_defineProperty(this,"textStreamRender",(function(renderData){var requestId=renderData.requestId,body=renderData.body,onCallbackMsg=renderData.onCallbackMsg,first=CustomJSON.parse(body).first;_this.checkHumanInstansce()&&_this.dhServer&&_this.dhServer.sendMessage({requestId:requestId||v4(),action:"TEXT_STREAM_RENDER",body:body,clientTs:(new Date).getTime()},first?onCallbackMsg:null)})),_defineProperty(this,"audioRender",(function(renderData){var body=renderData.body,onCallbackMsg=renderData.onCallbackMsg;_this.checkHumanInstansce()&&_this.dhServer&&_this.dhServer.sendMessage({requestId:v4(),action:"AUDIO_RENDER",body:body,clientTs:(new Date).getTime()},onCallbackMsg)})),_defineProperty(this,"audioStreamRender",(function(renderData){var requestId=renderData.requestId,body=renderData.body,onCallbackMsg=renderData.onCallbackMsg;_this.checkHumanInstansce()&&_this.dhServer&&_this.dhServer.sendMessage({requestId:requestId||v4(),action:"AUDIO_STREAM_RENDER",body:body,clientTs:(new Date).getTime()},onCallbackMsg)})),_defineProperty(this,"textQuery",(function(queryData){var requestId=queryData.requestId,body=queryData.body,onCallbackMsg=queryData.onCallbackMsg;_this.dhServer&&_this.dhServer.sendMessage({requestId:requestId||v4(),action:"TEXT_QUERY",body:body,clientTs:(new Date).getTime()},onCallbackMsg)})),_defineProperty(this,"destroy",_asyncToGenerator(_regeneratorRuntime().mark((function _callee3(){return _regeneratorRuntime().wrap((function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:return _context3.next=2,_this.dhServer&&_this.dhServer.closeSocket();case 2:_this.rtcServer&&_this.rtcServer.closeRtc(),removeHumanDom(),_this.dhServer=null,_this.rtcServer=null;case 6:case"end":return _context3.stop()}}),_callee3)})))),_defineProperty(this,"pauseHuman",(function(){return playHumanVideo(!1)})),_defineProperty(this,"playHuman",(function(){return playHumanVideo(!0)})),_defineProperty(this,"muteHuman",(function(){return muteHumanVideo(!0)})),_defineProperty(this,"unMuteHuman",(function(){var humanVideo=muteHumanVideo(!1);try{return _this.isHuawei?(humanVideo.pause(),humanVideo.play()):humanVideo.paused&&humanVideo.play()}catch(e){console.info(e)}}));var _props$isPreOpenSessi=props.isPreOpenSession,isPreOpenSession=void 0!==_props$isPreOpenSessi&&_props$isPreOpenSessi,wsUrl=props.wsUrl,renderParams=props.renderParams,onDigitalHumanCallback=props.onDigitalHumanCallback;this.isPreOpenSession=isPreOpenSession,this.dhServer=new Server(wsUrl||"wss://open.xiling.baidu.com/cloud/digital-human-demonstration"),this.onDigitalHumanCallback=function(data){data.status!==statusEnum.DH_LIB_FULL_STATUS?onDigitalHumanCallback&&onDigitalHumanCallback(data):(null==renderParams?void 0:renderParams.fullStatus)&&onDigitalHumanCallback&&onDigitalHumanCallback(data)},isPreOpenSession?this.dhServer.createSocket({onDigitalHumanCallback:this.onDigitalHumanCallbackPlus},isPreOpenSession):this.init(props)}return _createClass(DhRealtimeHuman,[{key:"init",value:function(props){var _this2=this,wrapperId=props.wrapperId,token=props.token,appKey=props.appKey,appId=props.appId,connectParams=props.connectParams,_props$renderParams=props.renderParams,renderParams=void 0===_props$renderParams?{}:_props$renderParams,rtcConnectParams=props.rtcConnectParams,brtcParams=props.brtcParams,rtcServerUrl=props.rtcServerUrl;if(this.token=token,this.appKey=appKey,this.appId=appId,this.rtcServerUrl=rtcServerUrl,this.connectParams=_objectSpread2({autoChromaKey:(null==renderParams?void 0:renderParams.autoChromaKey)||!1},connectParams),!appId&&!appKey&&!token)throw new Error("appId and appKey or token is required");var closeLog=renderParams.closeLog,playerWrapper=(createHumanDom({autoChromaKey:!!renderParams.autoChromaKey||!isEmpty(renderParams.chromaEffects)})||{}).playerWrapper,wrapperEle=document.getElementById(wrapperId||"");wrapperEle||console.error("dom with wrapperId is not find"),playerWrapper&&wrapperEle&&wrapperEle.appendChild(playerWrapper),appendRtcDomStyle();var rtcConfig=_objectSpread2({remotevideoviewid:wrapperId,showvideobps:!1,usingvideo:!1,usingaudio:!(null==connectParams||!connectParams.pullAudioFromRtc),aspublisher:!(null==connectParams||!connectParams.pullAudioFromRtc),autopublish:!(null==connectParams||!connectParams.pullAudioFromRtc),usingdatachannel:!0,autoplaymuted:!1},brtcParams);this.rtcServer=new BrtcClient({closeLog:closeLog,videoWrapperId:RTC_VIDEO_ID,rtcConfig:rtcConfig,bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback,onLocalStream:this.onLocalStream}}),this.cutoutInstance=new Cutout({sourceId:RTC_VIDEO_ID,effects:null==renderParams?void 0:renderParams.chromaEffects}),isEmpty(rtcConnectParams)||(this.rtcConnectParams=rtcConnectParams,this.rtcServer.openRtc(rtcConnectParams)),null!=connectParams&&connectParams.pullAudioFromRtc?this.recordController=null:this.recordController=new RecordController({pickAudioMode:null==connectParams?void 0:connectParams.pickAudioMode,onAudioData:function(pcmData){1===_this2.wsReadyState&&_this2.sendMessage(pcmData,null)},onError:function(err){_this2.onDigitalHumanCallback&&_this2.onDigitalHumanCallback({status:statusEnum.DH_LIB_ERROR,content:{type:errorTypeEnum.CONNECT_ERROR,msg:{code:-1,errMsg:err.message}}})}})}},{key:"startRecord",value:function(){var _this$connectParams;null!==(_this$connectParams=this.connectParams)&&void 0!==_this$connectParams&&_this$connectParams.pullAudioFromRtc?this.rtcServer&&this.rtcServer.muteMicphone(!1):this.recordController&&this.recordController.start()}},{key:"stopRecord",value:function(){var _this$connectParams2;if(null!==(_this$connectParams2=this.connectParams)&&void 0!==_this$connectParams2&&_this$connectParams2.pullAudioFromRtc)return this.rtcServer&&this.rtcServer.muteMicphone(!0),void this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:v4(),clientTs:Date.now(),body:""},null);this.recordController&&this.recordController.stop(),this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:v4(),clientTs:Date.now(),body:""},null)}}]),DhRealtimeHuman}();export{DhRealtimeHuman as default};
package/es/Cutout.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @file lib/Cutout
3
+ * @description 视频抠绿相关
4
+ * @author zhangyue49
5
+ */
6
+ import { ChromaEffectOptions } from '@bddh/starling-cutout/es/interface';
7
+ import Seriously from '@bddh/starling-cutout/es/Seriously';
8
+ interface CutoutType {
9
+ effects?: {
10
+ version: number;
11
+ chromaKey: Partial<ChromaEffectOptions>;
12
+ };
13
+ sourceId: string;
14
+ }
15
+ export declare const DEFAULT_EFFECTS: {
16
+ version: number;
17
+ chromaKey: {
18
+ screen: number[];
19
+ similarity: number;
20
+ edgeShrink: number;
21
+ smoothness: number;
22
+ spill: number;
23
+ opacity: number;
24
+ contrast: number;
25
+ brightness: number;
26
+ gamma: number;
27
+ };
28
+ };
29
+ declare class Cutout {
30
+ cutoutInstance: Seriously;
31
+ constructor(props: CutoutType);
32
+ setOptions(options: Partial<ChromaEffectOptions>): void;
33
+ destroy(): void;
34
+ updateSource(video: HTMLVideoElement | string): Promise<void>;
35
+ }
36
+ export default Cutout;
package/es/Cutout.js ADDED
@@ -0,0 +1 @@
1
+ import{createClass as _createClass,classCallCheck as _classCallCheck,defineProperty as _defineProperty}from"./_virtual/_rollupPluginBabelHelpers.js";import Seriously from"@bddh/starling-cutout/es/Seriously";var DEFAULT_EFFECTS={version:2,chromaKey:{screen:[53,186,123],similarity:90,edgeShrink:3,smoothness:80,spill:100,opacity:1,contrast:0,brightness:0,gamma:0}},Cutout=function(){function Cutout(props){_classCallCheck(this,Cutout),_defineProperty(this,"cutoutInstance",void 0);var effects=props.effects,sourceId=props.sourceId;this.cutoutInstance=new Seriously("starling-cutout-canvas",sourceId||"starling-cutout-video",effects&&effects.chromaKey||DEFAULT_EFFECTS.chromaKey,effects&&effects.version||DEFAULT_EFFECTS.version)}return _createClass(Cutout,[{key:"setOptions",value:function(options){options&&this.cutoutInstance&&this.cutoutInstance.setOptions(options)}},{key:"destroy",value:function(){return this.cutoutInstance.destroy()}},{key:"updateSource",value:function(video){return this.cutoutInstance.updateSource(video)}}]),Cutout}();export{DEFAULT_EFFECTS,Cutout as default};
@@ -0,0 +1,23 @@
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 declare function appendRtcDomStyle(): void;
23
+ export {};
@@ -0,0 +1 @@
1
+ var DEFAULT_CANVAS_ID="starling-cutout-canvas",HUMAN_WRAPPER_ID="cloud-brtc-player-wrapper",RTC_VIDEO_ID="remotevideo500";function createHumanDom(options){var _options$autoChromaKe=options.autoChromaKey,autoChromaKey=void 0!==_options$autoChromaKe&&_options$autoChromaKe,_options$autoplay=options.autoplay,autoplay=void 0===_options$autoplay||_options$autoplay;if(document.getElementById("remotevideo500"))return console.error('DOM with id "'.concat("remotevideo500",'" already exists.')),null;var playerWrapper=document.createElement("div");playerWrapper.className="cloud-brtc-player-wrapper",playerWrapper.id="cloud-brtc-player-wrapper",playerWrapper.style.position="relative";var video=document.createElement("video");video.id="remotevideo500",video.width=0,video.height=0,video.setAttribute("width","100%"),video.setAttribute("height","100%"),video.setAttribute("data-playing","true"),video.setAttribute("playsinline","");var canvas=document.createElement("canvas");return canvas.id="starling-cutout-canvas",Object.assign(canvas.style,{position:"absolute",top:"0",left:"0",height:"100%",width:"100%",zIndex:"1",display:autoChromaKey?"block":"none"}),autoplay&&video.setAttribute("autoplay",""),autoChromaKey&&(video.style.visibility="hidden"),playerWrapper.appendChild(video),playerWrapper.appendChild(canvas),{playerWrapper:playerWrapper,video:video}}function removeHumanDom(){var humanDom=document.getElementById("cloud-brtc-player-wrapper");humanDom&&humanDom.parentNode&&humanDom.parentNode.removeChild(humanDom)}function muteHumanVideo(muted){var humanVideoDom=document.getElementById("remotevideo500");return humanVideoDom&&(humanVideoDom.muted=muted),humanVideoDom}function playHumanVideo(play){var humanVideoDom=document.getElementById("remotevideo500");try{if(humanVideoDom)return play?humanVideoDom.play():humanVideoDom.pause()}catch(e){console.info(e)}}function checkIsHuawei(){var ua=navigator.userAgent||navigator.vendor;return/huawei/i.test(ua)||/honor/i.test(ua)}function appendRtcDomStyle(){var style=document.createElement("style");style.appendChild(document.createTextNode('\n [id^="videoremote1-"] {\n display: none !important;\n }\n \n video#remotevideo500 {\n width: auto !important;\n height: auto !important;\n max-width: 100% !important;\n max-height: 100% !important;\n width: 100% !important;\n height: 100% !important;\n object-fit: cover;\n }\n\n .cloud-brtc-player-wrapper {\n width: inherit !important;\n background-size: cover;\n height: inherit;\n display: flex;\n justify-content: center;\n }\n\n #starling-cutout-canvas {\n object-fit: cover;\n }\n ')),document.head.appendChild(style)}export{DEFAULT_CANVAS_ID,HUMAN_WRAPPER_ID,RTC_VIDEO_ID,appendRtcDomStyle,checkIsHuawei,createHumanDom,muteHumanVideo,playHumanVideo,removeHumanDom};
@@ -0,0 +1,20 @@
1
+ export type PickAudioMode = 'pressButton' | 'free';
2
+ interface RecordControllerOptions {
3
+ pickAudioMode?: PickAudioMode;
4
+ onAudioData: (pcmData: ArrayBuffer) => void;
5
+ onError?: (err: Error) => void;
6
+ pullAudioFromRtc?: boolean;
7
+ brtcClient?: any;
8
+ }
9
+ declare class RecordController {
10
+ private readonly onAudioData;
11
+ private readonly onError?;
12
+ private audioContext;
13
+ private processor;
14
+ private mediaStream;
15
+ private recording;
16
+ constructor(options: RecordControllerOptions);
17
+ start(): Promise<void>;
18
+ stop(): void;
19
+ }
20
+ export default RecordController;
@@ -0,0 +1 @@
1
+ import{createClass as _createClass,asyncToGenerator as _asyncToGenerator,classCallCheck as _classCallCheck,defineProperty as _defineProperty,regeneratorRuntime as _regeneratorRuntime}from"./_virtual/_rollupPluginBabelHelpers.js";function sampleData(pcmDatas,pcmSampleRate,newSampleRate,prevChunkInfo){try{for(var index=(prevChunkInfo=prevChunkInfo||{})&&"number"==typeof prevChunkInfo.index?prevChunkInfo.index:0,offset=prevChunkInfo&&"number"==typeof prevChunkInfo.offset?prevChunkInfo.offset:0,size=0,i=index;i<pcmDatas.length;i++)size+=pcmDatas[i].length;size=Math.max(0,size-Math.floor(offset));var step=pcmSampleRate/newSampleRate;step>1?size=Math.floor(size/step):(step=1,newSampleRate=pcmSampleRate);for(var res=new Int16Array(size),idx=0,nl=pcmDatas.length;index<nl;index++){for(var o=pcmDatas[index],_i=offset,il=o.length;_i<il;){var before=Math.floor(_i),after=Math.ceil(_i),atPoint=_i-before;res[idx]=o[before]+(o[after]-o[before])*atPoint,idx++,_i+=step}offset=_i-il}return{index:index,offset:offset,sampleRate:newSampleRate,data:res}}catch(error){console.log("转音错误了",error)}}function convertBuffer(arrayBuffer){for(var data=arrayBuffer,out=new Int16Array(arrayBuffer.length),i=0;i<data.length;i++){var s=Math.max(-1,Math.min(1,data[i]));out[i]=s<0?32768*s:32767*s}return out}var RecordController=function(_start){function RecordController(options){_classCallCheck(this,RecordController),_defineProperty(this,"onAudioData",void 0),_defineProperty(this,"onError",void 0),_defineProperty(this,"audioContext",null),_defineProperty(this,"processor",null),_defineProperty(this,"mediaStream",null),_defineProperty(this,"recording",!1),this.onAudioData=options.onAudioData,this.onError=options.onError}return _createClass(RecordController,[{key:"start",value:function(){return(_start=_start||_asyncToGenerator(_regeneratorRuntime().mark((function _callee(){var source,_this=this;return _regeneratorRuntime().wrap((function(_context){for(;;)switch(_context.prev=_context.next){case 0:if(!this.recording){_context.next=2;break}return _context.abrupt("return");case 2:return _context.prev=2,this.audioContext=new(window.AudioContext||window.webkitAudioContext)({sampleRate:16e3}),_context.next=6,navigator.mediaDevices.getUserMedia({audio:!0,video:!1});case 6:this.mediaStream=_context.sent,source=this.audioContext.createMediaStreamSource(this.mediaStream),this.processor=this.audioContext.createScriptProcessor(1024,1,1),source.connect(this.processor),this.processor.connect(this.audioContext.destination),this.processor.onaudioprocess=function(e){if(_this.recording){var resampled=sampleData([convertBuffer(e.inputBuffer.getChannelData(0))],_this.audioContext.sampleRate,16e3,null);resampled&&resampled.data&&_this.onAudioData(resampled.data.buffer)}},this.recording=!0,_context.next=18;break;case 15:_context.prev=15,_context.t0=_context.catch(2),this.onError&&this.onError(_context.t0);case 18:case"end":return _context.stop()}}),_callee,this,[[2,15]])})))).apply(this,arguments)}},{key:"stop",value:function(){this.recording=!1,this.processor&&(this.processor.disconnect(),this.processor.onaudioprocess=null),this.audioContext&&this.audioContext.close(),this.mediaStream&&this.mediaStream.getTracks().forEach((function(track){track.stop()}))}}]),RecordController}();export{RecordController as default};
package/es/Server.d.ts ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @file lib/Server
3
+ * @description ws服务逻辑处理
4
+ * @author zhangyue49
5
+ */
6
+ import { type 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 | ArrayBuffer;
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
+ reConnect?: boolean;
35
+ onConnect?: ConnectListener;
36
+ onDigitalHumanCallback?: (data: CallbackMsgType) => void;
37
+ }
38
+ export interface RenderCallbackRes {
39
+ requestId: string;
40
+ action: string;
41
+ code: number;
42
+ body: string | null;
43
+ message: string;
44
+ }
45
+ export type RenderCallback = (res: RenderCallbackRes) => void;
46
+ declare class Server {
47
+ dhServerSocket: WebSocketClient | null;
48
+ preConnectSuccess: boolean;
49
+ connectParams: createSocketParamsType | null;
50
+ private socketClosingPromise;
51
+ private readonly url;
52
+ private firstConnect;
53
+ constructor(url: string);
54
+ sendPreConnect: (props: createSocketParamsType, resolve?: ((value: any) => void) | undefined) => void;
55
+ createSocket: (props: createSocketParamsType, isPreOpenSession?: boolean) => Promise<WebSocketClient | null>;
56
+ closeSocket: () => Promise<void>;
57
+ handleConnect: () => Promise<void>;
58
+ sendMessage(message: DHServerInterface | ArrayBuffer, listener: RenderCallback | null): Promise<void>;
59
+ }
60
+ export default Server;
package/es/Server.js ADDED
@@ -0,0 +1 @@
1
+ import{createClass as _createClass,asyncToGenerator as _asyncToGenerator,classCallCheck as _classCallCheck,defineProperty as _defineProperty,regeneratorRuntime as _regeneratorRuntime,objectSpread2 as _objectSpread2,typeof as _typeof}from"./_virtual/_rollupPluginBabelHelpers.js";import{v4}from"uuid";import{createWebSocket}from"@bddh/starling-web-socket/es/create-web-socket";import pick from"lodash/pick";import omit from"lodash/omit";import get from"lodash/get";import{errorTypeEnum,CONNECT_MAIN_KEY,statusEnum,WS_STATUS_MAP}from"./interface.js";var Server=function(_sendMessage){function Server(url){var _ref,_this=this;_classCallCheck(this,Server),_defineProperty(this,"dhServerSocket",null),_defineProperty(this,"preConnectSuccess",!1),_defineProperty(this,"connectParams",null),_defineProperty(this,"socketClosingPromise",null),_defineProperty(this,"url",void 0),_defineProperty(this,"firstConnect",!0),_defineProperty(this,"sendPreConnect",(function(props,resolve){var _this$dhServerSocket,token=props.token,sessionId=props.sessionId,onConnect=props.onConnect;null===(_this$dhServerSocket=_this.dhServerSocket)||void 0===_this$dhServerSocket||_this$dhServerSocket.request({action:"RE_CONNECT",requestId:v4(),body:JSON.stringify({token:token,sessionId:sessionId})}).on((function(res,off){null==onConnect||onConnect(res),0!==res.code&&(off(),_this.handleConnect()),null==resolve||resolve(_this.dhServerSocket)})),_this.firstConnect=!1})),_defineProperty(this,"createSocket",(function(_x,_x2){return(_ref=_ref||_asyncToGenerator(_regeneratorRuntime().mark((function _callee(props,isPreOpenSession){var sessionId,onDigitalHumanCallback;return _regeneratorRuntime().wrap((function(_context){for(;;)switch(_context.prev=_context.next){case 0:if(_this.connectParams=props,sessionId=props.sessionId,onDigitalHumanCallback=props.onDigitalHumanCallback,!_this.socketClosingPromise){_context.next=5;break}return _context.next=5,_this.socketClosingPromise;case 5:if(!_this.dhServerSocket){_context.next=8;break}return _context.next=8,_this.closeSocket();case 8:return _context.abrupt("return",new Promise((function(resolve,reject){var createPamrams={keepAlive:"heartbeat",reopen:get(_this.connectParams,"reConnect",!0),combine:function(req,res){return req&&res&&req.requestId===res.requestId},onOpen:function(){_this.dhServerSocket=socket,sessionId&&_this.firstConnect?_this.sendPreConnect(props,resolve):isPreOpenSession&&_this.firstConnect||_this.handleConnect()},onClose:function(){_this.dhServerSocket=null,_this.socketClosingPromise=null},onError:function(error){reject(error)}},socket=createWebSocket(_this.url,createPamrams);socket.onReadyStateChange((function(readyState){0===readyState&&onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_INIT}),1===readyState&&isPreOpenSession&&(_this.preConnectSuccess=!0),3===readyState&&(_this.preConnectSuccess=!1),onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_FULL_STATUS,content:{type:"wsState",readyState:readyState,body:WS_STATUS_MAP[readyState]}})})),socket.onMessage((function(msgData){var action=msgData.action;[errorTypeEnum.TIMEOUT_EXIT,errorTypeEnum.DISCONNECT_ALERT].includes(action)&&onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_STATUS,content:{type:action}}),[errorTypeEnum.TIMEOUT_EXIT,errorTypeEnum.DISCONNECT_ALERT,"HEART_BEAT","CONNECT"].includes(action)||onDigitalHumanCallback&&onDigitalHumanCallback({status:statusEnum.DH_LIB_MESSAGE,content:msgData})})),isPreOpenSession&&(_this.dhServerSocket=socket),socket.open()})));case 9:case"end":return _context.stop()}}),_callee)})))).apply(this,arguments)})),_defineProperty(this,"closeSocket",_asyncToGenerator(_regeneratorRuntime().mark((function _callee2(){return _regeneratorRuntime().wrap((function(_context2){for(;;)switch(_context2.prev=_context2.next){case 0:if(_this.dhServerSocket){_context2.next=2;break}return _context2.abrupt("return");case 2:return _this.socketClosingPromise=_this.dhServerSocket.close(),_context2.next=5,_this.socketClosingPromise;case 5:_this.dhServerSocket=null;case 6:case"end":return _context2.stop()}}),_callee2)})))),_defineProperty(this,"handleConnect",_asyncToGenerator(_regeneratorRuntime().mark((function _callee3(){var _this$connectParams,appId,appKey,token,parameters,onConnect;return _regeneratorRuntime().wrap((function(_context3){for(;;)switch(_context3.prev=_context3.next){case 0:if(_this.connectParams){_context3.next=2;break}return _context3.abrupt("return");case 2:if(_this$connectParams=_this.connectParams,appId=_this$connectParams.appId,appKey=_this$connectParams.appKey,token=_this$connectParams.token,parameters=_this$connectParams.parameters,onConnect=_this$connectParams.onConnect,appId||appKey||token){_context3.next=5;break}return _context3.abrupt("return");case 5:_this.dhServerSocket&&_this.dhServerSocket.request({action:"CONNECT",clientTs:(new Date).getTime(),requestId:v4(),body:JSON.stringify(_objectSpread2(_objectSpread2({token:token,appId:appId,appKey:appKey},pick(_this.connectParams,CONNECT_MAIN_KEY)),{},{parameters:_objectSpread2(_objectSpread2({},omit(parameters,["parameters"])),parameters&&parameters.parameters?parameters.parameters:{})}))}).on((function(res,off){null==onConnect||onConnect(res),0!==res.code&&off()}));case 6:case"end":return _context3.stop()}}),_callee3)})))),this.url=url}return _createClass(Server,[{key:"sendMessage",value:function(_x3,_x4){return(_sendMessage=_sendMessage||_asyncToGenerator(_regeneratorRuntime().mark((function _callee4(message,listener){var requestId;return _regeneratorRuntime().wrap((function(_context4){for(;;)switch(_context4.prev=_context4.next){case 0:try{this.dhServerSocket&&this.dhServerSocket&&this.dhServerSocket.request(message).on((function(response){listener&&listener(response)}))}catch(e){requestId="object"===_typeof(message)&&"requestId"in message?message.requestId:v4(),listener&&listener({code:-11,body:null,action:"RENDER_ERROR",requestId:requestId,message:JSON.stringify(e)})}case 1:case"end":return _context4.stop()}}),_callee4,this)})))).apply(this,arguments)}}]),Server}();export{Server as default};
@@ -0,0 +1 @@
1
+ function ownKeys(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);r&&(o=o.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),t.push.apply(t,o)}return t}function _objectSpread2(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ownKeys(Object(t),!0).forEach((function(r){_defineProperty(e,r,t[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ownKeys(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}))}return e}function _regeneratorRuntime(){_regeneratorRuntime=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",c=i.asyncIterator||"@@asyncIterator",u=i.toStringTag||"@@toStringTag";function define(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{define({},"")}catch(t){define=function(t,e,r){return t[e]=r}}function wrap(t,e,r,n){var i=e&&e.prototype instanceof Generator?e:Generator,a=Object.create(i.prototype),c=new Context(n||[]);return o(a,"_invoke",{value:makeInvokeMethod(t,r,c)}),a}function tryCatch(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=wrap;var h="suspendedStart",l="suspendedYield",f="executing",s="completed",y={};function Generator(){}function GeneratorFunction(){}function GeneratorFunctionPrototype(){}var p={};define(p,a,(function(){return this}));var d=Object.getPrototypeOf,v=d&&d(d(values([])));v&&v!==r&&n.call(v,a)&&(p=v);var g=GeneratorFunctionPrototype.prototype=Generator.prototype=Object.create(p);function defineIteratorMethods(t){["next","throw","return"].forEach((function(e){define(t,e,(function(t){return this._invoke(e,t)}))}))}function AsyncIterator(t,e){function invoke(r,o,i,a){var c=tryCatch(t[r],t,o);if("throw"!==c.type){var u=c.arg,h=u.value;return h&&"object"==typeof h&&n.call(h,"__await")?e.resolve(h.__await).then((function(t){invoke("next",t,i,a)}),(function(t){invoke("throw",t,i,a)})):e.resolve(h).then((function(t){u.value=t,i(u)}),(function(t){return invoke("throw",t,i,a)}))}a(c.arg)}var r;o(this,"_invoke",{value:function(t,n){function callInvokeWithMethodAndArg(){return new e((function(e,r){invoke(t,n,e,r)}))}return r=r?r.then(callInvokeWithMethodAndArg,callInvokeWithMethodAndArg):callInvokeWithMethodAndArg()}})}function makeInvokeMethod(e,r,n){var o=h;return function(i,a){if(o===f)throw new Error("Generator is already running");if(o===s){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var c=n.delegate;if(c){var u=maybeInvokeDelegate(c,n);if(u){if(u===y)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=s,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=f;var p=tryCatch(e,r,n);if("normal"===p.type){if(o=n.done?s:l,p.arg===y)continue;return{value:p.arg,done:n.done}}"throw"===p.type&&(o=s,n.method="throw",n.arg=p.arg)}}}function maybeInvokeDelegate(e,r){var n=r.method,o=e.iterator[n];if(o===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,maybeInvokeDelegate(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var i=tryCatch(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,y;var a=i.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,y):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function pushTryEntry(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function resetTryEntry(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function Context(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(pushTryEntry,this),this.reset(!0)}function values(e){if(e||""===e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function next(){for(;++o<e.length;)if(n.call(e,o))return next.value=e[o],next.done=!1,next;return next.value=t,next.done=!0,next};return i.next=i}}throw new TypeError(typeof e+" is not iterable")}return GeneratorFunction.prototype=GeneratorFunctionPrototype,o(g,"constructor",{value:GeneratorFunctionPrototype,configurable:!0}),o(GeneratorFunctionPrototype,"constructor",{value:GeneratorFunction,configurable:!0}),GeneratorFunction.displayName=define(GeneratorFunctionPrototype,u,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===GeneratorFunction||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,GeneratorFunctionPrototype):(t.__proto__=GeneratorFunctionPrototype,define(t,u,"GeneratorFunction")),t.prototype=Object.create(g),t},e.awrap=function(t){return{__await:t}},defineIteratorMethods(AsyncIterator.prototype),define(AsyncIterator.prototype,c,(function(){return this})),e.AsyncIterator=AsyncIterator,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new AsyncIterator(wrap(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},defineIteratorMethods(g),define(g,u,"Generator"),define(g,a,(function(){return this})),define(g,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function next(){for(;r.length;){var t=r.pop();if(t in e)return next.value=t,next.done=!1,next}return next.done=!0,next}},e.values=values,Context.prototype={constructor:Context,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(resetTryEntry),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function handle(n,o){return a.type="throw",a.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return handle("end");if(i.tryLoc<=this.prev){var c=n.call(i,"catchLoc"),u=n.call(i,"finallyLoc");if(c&&u){if(this.prev<i.catchLoc)return handle(i.catchLoc,!0);if(this.prev<i.finallyLoc)return handle(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return handle(i.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return handle(i.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),resetTryEntry(r),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;resetTryEntry(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:values(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},e}function _typeof(o){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}function asyncGeneratorStep(gen,resolve,reject,_next,_throw,key,arg){try{var info=gen[key](arg),value=info.value}catch(error){return void reject(error)}info.done?resolve(value):Promise.resolve(value).then(_next,_throw)}function _asyncToGenerator(fn){return function(){var self=this,args=arguments;return new Promise((function(resolve,reject){var gen=fn.apply(self,args);function _next(value){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"next",value)}function _throw(err){asyncGeneratorStep(gen,resolve,reject,_next,_throw,"throw",err)}_next(void 0)}))}}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,_toPropertyKey(descriptor.key),descriptor)}}function _createClass(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Object.defineProperty(Constructor,"prototype",{writable:!1}),Constructor}function _defineProperty(obj,key,value){return(key=_toPropertyKey(key))in obj?Object.defineProperty(obj,key,{value:value,enumerable:!0,configurable:!0,writable:!0}):obj[key]=value,obj}function _toPrimitive(input,hint){if("object"!=typeof input||null===input)return input;var prim=input[Symbol.toPrimitive];if(void 0!==prim){var res=prim.call(input,hint||"default");if("object"!=typeof res)return res;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===hint?String:Number)(input)}function _toPropertyKey(arg){var key=_toPrimitive(arg,"string");return"symbol"==typeof key?key:String(key)}export{_asyncToGenerator as asyncToGenerator,_classCallCheck as classCallCheck,_createClass as createClass,_defineProperty as defineProperty,_objectSpread2 as objectSpread2,_regeneratorRuntime as regeneratorRuntime,_toPrimitive as toPrimitive,_toPropertyKey as toPropertyKey,_typeof as typeof};
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 DhRealtimeHuman from"./Client.js";export{CONNECT_MAIN_KEY,WS_STATUS_MAP,errorTypeEnum,statusEnum}from"./interface.js";export{DhRealtimeHuman as default};
@@ -0,0 +1,108 @@
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
+ x264BitRate?: number;
13
+ cameraId?: string | number;
14
+ characterOffset?: string;
15
+ pickAudioMode?: 'pressButton' | 'free';
16
+ autoChromaKey?: boolean;
17
+ usingVideo?: boolean;
18
+ ttsPer?: string | number;
19
+ ttsPitch?: number;
20
+ ttsSpeed?: number;
21
+ ttsVolume?: number;
22
+ ttsLan?: string;
23
+ pullAudioFromRtc?: boolean;
24
+ inactiveDisconnectSec?: number;
25
+ preAlertSec?: number;
26
+ backgroundImageUrl?: string;
27
+ projectId?: string;
28
+ characterConfigId?: string;
29
+ selectors?: any;
30
+ figureName?: string;
31
+ configVersion?: string;
32
+ extraInfo?: any;
33
+ roomName?: string;
34
+ paintSubtitleOnPicture?: boolean;
35
+ h5Embedded?: boolean;
36
+ paintWidgetOnPicture?: boolean;
37
+ extraParameters?: any;
38
+ parameters?: any;
39
+ reConnect?: boolean;
40
+ }
41
+ export declare const CONNECT_MAIN_KEY: string[];
42
+ export interface RenderParamsType {
43
+ chromaEffects?: object;
44
+ autoChromaKey?: boolean;
45
+ closeLog?: boolean;
46
+ fullStatus?: boolean;
47
+ }
48
+ export declare enum statusEnum {
49
+ DH_LIB_INIT = "DH_LIB_INIT",
50
+ DH_LIB_WS_SUCCESS = "DH_LIB_WS_SUCCESS",
51
+ DH_LIB_OPEN = "DH_LIB_OPEN",
52
+ DH_LIB_STATUS = "DH_LIB_STATUS",
53
+ DH_LIB_CLOSE = "DH_LIB_CLOSE",
54
+ DH_LIB_ERROR = "DH_LIB_ERROR",
55
+ DH_LIB_WARNING = "DH_LIB_WARNING",
56
+ DH_LIB_FULL_STATUS = "DH_LIB_FULL_STATUS",
57
+ DH_LIB_MESSAGE = "DH_LIB_MESSAGE"
58
+ }
59
+ export declare enum errorTypeEnum {
60
+ CONNECT_ERROR = "CONNECT_ERROR",
61
+ DISCONNECT_ALERT = "DISCONNECT_ALERT",
62
+ TIMEOUT_EXIT = "TIMEOUT_EXIT",
63
+ LOCAL_VIDEO_MUTED = "LOCAL_VIDEO_MUTED",
64
+ LOCAL_VIDEO_ERROR = "LOCAL_VIDEO_ERROR",
65
+ RTC_ERROR = "RTC_ERROR"
66
+ }
67
+ export interface ErrorType {
68
+ type: errorTypeEnum;
69
+ msg?: {
70
+ code: string | number;
71
+ errMsg: string;
72
+ };
73
+ }
74
+ export interface RtcStateType {
75
+ action: string;
76
+ type: string;
77
+ body: any;
78
+ }
79
+ export declare const WS_STATUS_MAP: {
80
+ '-1': string;
81
+ '0': string;
82
+ '1': string;
83
+ '2': string;
84
+ '3': string;
85
+ };
86
+ export interface WsStateType {
87
+ readyState: number;
88
+ type: string;
89
+ body: string;
90
+ }
91
+ export interface CallbackMsgType {
92
+ status: statusEnum;
93
+ content?: ErrorType | WsStateType | RtcStateType | ConnectDataType;
94
+ }
95
+ export interface DhRealtimeHumanType {
96
+ token?: string;
97
+ wsUrl?: string;
98
+ rtcServerUrl?: string;
99
+ appKey?: string;
100
+ appId?: string;
101
+ wrapperId?: string;
102
+ connectParams?: ConnectParamsType;
103
+ renderParams?: RenderParamsType;
104
+ rtcConnectParams?: RtcConnectionType;
105
+ brtcParams?: any;
106
+ isPreOpenSession?: boolean;
107
+ onDigitalHumanCallback: (data: CallbackMsgType) => void;
108
+ }
@@ -0,0 +1 @@
1
+ var statusEnum,errorTypeEnum,CONNECT_MAIN_KEY=["projectId","characterConfigId","configVersion","selectors","figureName","reConnect"];!function(statusEnum){statusEnum.DH_LIB_INIT="DH_LIB_INIT",statusEnum.DH_LIB_WS_SUCCESS="DH_LIB_WS_SUCCESS",statusEnum.DH_LIB_OPEN="DH_LIB_OPEN",statusEnum.DH_LIB_STATUS="DH_LIB_STATUS",statusEnum.DH_LIB_CLOSE="DH_LIB_CLOSE",statusEnum.DH_LIB_ERROR="DH_LIB_ERROR",statusEnum.DH_LIB_WARNING="DH_LIB_WARNING",statusEnum.DH_LIB_FULL_STATUS="DH_LIB_FULL_STATUS",statusEnum.DH_LIB_MESSAGE="DH_LIB_MESSAGE"}(statusEnum||(statusEnum={})),function(errorTypeEnum){errorTypeEnum.CONNECT_ERROR="CONNECT_ERROR",errorTypeEnum.DISCONNECT_ALERT="DISCONNECT_ALERT",errorTypeEnum.TIMEOUT_EXIT="TIMEOUT_EXIT",errorTypeEnum.LOCAL_VIDEO_MUTED="LOCAL_VIDEO_MUTED",errorTypeEnum.LOCAL_VIDEO_ERROR="LOCAL_VIDEO_ERROR",errorTypeEnum.RTC_ERROR="RTC_ERROR"}(errorTypeEnum||(errorTypeEnum={}));var WS_STATUS_MAP={"-1":"UNINSTANTIATED",0:"CONNECTING",1:"OPEN",2:"CLOSING",3:"CLOSED"};export{CONNECT_MAIN_KEY,WS_STATUS_MAP,errorTypeEnum,statusEnum};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bddh/starling-realtime-client",
3
- "version": "2.0.5",
3
+ "version": "2.0.6",
4
4
  "keywords": [
5
5
  "react",
6
6
  "indexDB"
@@ -24,7 +24,7 @@
24
24
  "scripts": {
25
25
  "build": "rm -rf cjs es && rollup -c --bundleConfigAsCjs",
26
26
  "test": "npx jest --passWithNoTests",
27
- "deploy": "npm run build & npm publish --registry=https://registry.npmjs.org --access public"
27
+ "deploy": "npm publish --registry=https://registry.npmjs.org --access public"
28
28
  },
29
29
  "peerDependencies": {
30
30
  "@bddh/starling-brtc": "^1.0.2",