@bddh/starling-realtime-client 2.0.7 → 2.0.8-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/Client.d.ts +1 -0
- package/cjs/Client.js +1 -1
- package/cjs/RecordController.d.ts +5 -0
- package/cjs/RecordController.js +1 -1
- package/cjs/interface.d.ts +22 -0
- package/es/Client.d.ts +1 -0
- package/es/Client.js +1 -1
- package/es/RecordController.d.ts +5 -0
- package/es/RecordController.js +1 -1
- package/es/interface.d.ts +22 -0
- package/package.json +2 -2
package/cjs/Client.d.ts
CHANGED
package/cjs/Client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_virtual/_rollupPluginBabelHelpers.js"),t=require("@bddh/starling-json"),r=require("lodash/isEmpty"),n=require("lodash/omit"),o=require("lodash/pick"),a=require("uuid"),i=require("./interface.js"),s=require("./Server.js"),c=require("./BrtcClient.js"),u=require("./DomControl.js"),d=require("./Cutout.js"),l=require("./RecordController.js"),m=function(){function m(r){var c=this;e.classCallCheck(this,m),e.defineProperty(this,"token",void 0),e.defineProperty(this,"appKey",void 0),e.defineProperty(this,"appId",void 0),e.defineProperty(this,"connectParams",void 0),e.defineProperty(this,"onDigitalHumanCallback",void 0),e.defineProperty(this,"dhServer",void 0),e.defineProperty(this,"rtcServer",void 0),e.defineProperty(this,"rtcConnectParams",void 0),e.defineProperty(this,"cutoutInstance",void 0),e.defineProperty(this,"isHuawei",u.checkIsHuawei()),e.defineProperty(this,"rtcServerUrl",void 0),e.defineProperty(this,"recordController",null),e.defineProperty(this,"wsReadyState",-1),e.defineProperty(this,"isPreOpenSession",!1),e.defineProperty(this,"onDigitalHumanCallbackPlus",(function(e){var t;e.status===i.statusEnum.DH_LIB_FULL_STATUS&&"wsState"===(null===(t=e.content)||void 0===t?void 0:t.type)&&(c.wsReadyState=e.content.readyState),c.onDigitalHumanCallback&&c.onDigitalHumanCallback(e)})),e.defineProperty(this,"onRemoteVideoComing",(function(e){c.rtcConnectParams&&c.rtcConnectParams.feedId&&e===+c.rtcConnectParams.feedId&&c.rtcServer&&c.rtcServer.client&&c.rtcServer.client.subscribeStreaming("remotevideo500",+c.rtcConnectParams.feedId)})),e.defineProperty(this,"onLocalStream",(function(){(c.connectParams||{}).pullAudioFromRtc&&c.rtcServer&&c.rtcServer.muteMicphone(!
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_virtual/_rollupPluginBabelHelpers.js"),t=require("@bddh/starling-json"),r=require("lodash/isEmpty"),n=require("lodash/omit"),o=require("lodash/pick"),a=require("uuid"),i=require("./interface.js"),s=require("./Server.js"),c=require("./BrtcClient.js"),u=require("./DomControl.js"),d=require("./Cutout.js"),l=require("./RecordController.js"),m=function(){function m(r){var c=this;e.classCallCheck(this,m),e.defineProperty(this,"token",void 0),e.defineProperty(this,"appKey",void 0),e.defineProperty(this,"appId",void 0),e.defineProperty(this,"connectParams",void 0),e.defineProperty(this,"onDigitalHumanCallback",void 0),e.defineProperty(this,"dhServer",void 0),e.defineProperty(this,"rtcServer",void 0),e.defineProperty(this,"rtcConnectParams",void 0),e.defineProperty(this,"cutoutInstance",void 0),e.defineProperty(this,"isHuawei",u.checkIsHuawei()),e.defineProperty(this,"rtcServerUrl",void 0),e.defineProperty(this,"recordController",null),e.defineProperty(this,"wsReadyState",-1),e.defineProperty(this,"isPreOpenSession",!1),e.defineProperty(this,"onDigitalHumanCallbackPlus",(function(e){var t;e.status===i.statusEnum.DH_LIB_FULL_STATUS&&"wsState"===(null===(t=e.content)||void 0===t?void 0:t.type)&&(c.wsReadyState=e.content.readyState),c.onDigitalHumanCallback&&c.onDigitalHumanCallback(e)})),e.defineProperty(this,"onRemoteVideoComing",(function(e){c.rtcConnectParams&&c.rtcConnectParams.feedId&&e===+c.rtcConnectParams.feedId&&c.rtcServer&&c.rtcServer.client&&c.rtcServer.client.subscribeStreaming("remotevideo500",+c.rtcConnectParams.feedId)})),e.defineProperty(this,"onLocalStream",(function(){(c.connectParams||{}).pullAudioFromRtc&&c.rtcServer&&c.rtcServer.muteMicphone(!1)})),e.defineProperty(this,"onRemoteVideoOn",(function(){setTimeout(e.asyncToGenerator(e.regeneratorRuntime().mark((function t(){var r,n,o,a,i;return e.regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=document.getElementById(u.RTC_VIDEO_ID),e.next=3,c.cutoutInstance&&c.cutoutInstance.updateSource(u.RTC_VIDEO_ID);case 3:r&&(r.setAttribute("data-playing","true"),n=r.videoWidth,o=r.videoHeight,a=document.getElementById(u.DEFAULT_CANVAS_ID),i=a.getContext("2d"),a&&i&&(a.width=n,a.height=o,i.drawImage(r,0,0,n,o)));case 4:case"end":return e.stop()}}),t)}))),100)})),e.defineProperty(this,"connectListener",(function(e){var r=e.code,n=e.body,o=e.message;if(0===r&&c.onDigitalHumanCallback&&c.onDigitalHumanCallback({status:i.statusEnum.DH_LIB_WS_SUCCESS,content:e}),0===r){var a=t.parse(n),s=a.rtcServerUrl,u=a.appId,d=a.clientToken,l=a.roomName,m=a.clientId,p=a.feedId,h=a.chargeId;u&&l&&(c.rtcConnectParams={server:c.rtcServerUrl||s,appId:u,token:d,roomName:l,userId:m,feedId:p,chargeId:h},c.rtcServer&&c.rtcServer.openRtc(c.rtcConnectParams))}else c.onDigitalHumanCallback&&c.onDigitalHumanCallback({status:i.statusEnum.DH_LIB_ERROR,content:{type:i.errorTypeEnum.CONNECT_ERROR,msg:{code:r,errMsg:o}}})})),e.defineProperty(this,"createServer",(function(t){var r,a=e.objectSpread2(e.objectSpread2(e.objectSpread2({token:c.token,appKey:c.appKey,appId:c.appId},t?{sessionId:t}:{}),o(c.connectParams,i.CONNECT_MAIN_KEY)),{},{parameters:n(c.connectParams,i.CONNECT_MAIN_KEY),onConnect:c.connectListener,onDigitalHumanCallback:c.onDigitalHumanCallbackPlus});null!==(r=c.dhServer)&&void 0!==r&&r.preConnectSuccess?(c.dhServer.connectParams=a,c.dhServer&&c.dhServer.sendPreConnect(a)):c.dhServer&&c.dhServer.createSocket(a)})),e.defineProperty(this,"checkHumanInstansce",(function(){return c.dhServer?!(c.isPreOpenSession&&!c.dhServer.preConnectSuccess)||(c.onDigitalHumanCallback&&c.onDigitalHumanCallback({status:i.statusEnum.DH_LIB_ERROR,content:{type:i.errorTypeEnum.CONNECT_ERROR,msg:{code:4001,errMsg:"webSocket连接中"}}}),!1):(c.onDigitalHumanCallback&&c.onDigitalHumanCallback({status:i.statusEnum.DH_LIB_ERROR,content:{type:i.errorTypeEnum.CONNECT_ERROR,msg:{code:4002,errMsg:"请先初始化数字人实例"}}}),!1)})),e.defineProperty(this,"sendMessage",(function(e,t){return c.dhServer&&c.dhServer.sendMessage(e,t)})),e.defineProperty(this,"interrupt",e.asyncToGenerator(e.regeneratorRuntime().mark((function t(){return e.regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",new Promise((function(e){c.dhServer&&c.dhServer.sendMessage({requestId:a.v4(),action:"TEXT_RENDER",body:"<interrupt></interrupt>",clientTs:(new Date).getTime()},(function(){return e()}))})));case 1:case"end":return e.stop()}}),t)})))),e.defineProperty(this,"textRender",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg;c.checkHumanInstansce()&&c.dhServer&&c.dhServer.sendMessage({requestId:t||a.v4(),action:"TEXT_RENDER",body:r,clientTs:(new Date).getTime()},n)})),e.defineProperty(this,"textStreamRender",(function(e){var r=e.requestId,n=e.body,o=e.onCallbackMsg,i=t.parse(n).first;c.checkHumanInstansce()&&c.dhServer&&c.dhServer.sendMessage({requestId:r||a.v4(),action:"TEXT_STREAM_RENDER",body:n,clientTs:(new Date).getTime()},i?o:null)})),e.defineProperty(this,"audioRender",(function(e){var t=e.body,r=e.onCallbackMsg;c.checkHumanInstansce()&&c.dhServer&&c.dhServer.sendMessage({requestId:a.v4(),action:"AUDIO_RENDER",body:t,clientTs:(new Date).getTime()},r)})),e.defineProperty(this,"audioStreamRender",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg;c.checkHumanInstansce()&&c.dhServer&&c.dhServer.sendMessage({requestId:t||a.v4(),action:"AUDIO_STREAM_RENDER",body:r,clientTs:(new Date).getTime()},n)})),e.defineProperty(this,"textQuery",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg;c.dhServer&&c.dhServer.sendMessage({requestId:t||a.v4(),action:"TEXT_QUERY",body:r,clientTs:(new Date).getTime()},n)})),e.defineProperty(this,"destroy",e.asyncToGenerator(e.regeneratorRuntime().mark((function t(){var r;return e.regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,c.dhServer&&c.dhServer.closeSocket();case 2:c.rtcServer&&c.rtcServer.closeRtc(),u.removeHumanDom(),null===(r=c.recordController)||void 0===r||r.destroy(),c.dhServer=null,c.rtcServer=null;case 7:case"end":return e.stop()}}),t)})))),e.defineProperty(this,"pauseHuman",(function(){return u.playHumanVideo(!1)})),e.defineProperty(this,"playHuman",(function(){return u.playHumanVideo(!0)})),e.defineProperty(this,"muteHuman",(function(){return u.muteHumanVideo(!0)})),e.defineProperty(this,"unMuteHuman",(function(){var e=u.muteHumanVideo(!1);try{return c.isHuawei?(e.pause(),e.play()):e.paused&&e.play()}catch(e){console.info(e)}}));var d=r.isPreOpenSession,l=void 0!==d&&d,p=r.wsUrl,h=r.renderParams,v=r.onDigitalHumanCallback;this.isPreOpenSession=l,this.dhServer=new s.default(p||"wss://open.xiling.baidu.com/cloud/digital-human-demonstration"),this.onDigitalHumanCallback=function(e){e.status!==i.statusEnum.DH_LIB_FULL_STATUS?v&&v(e):(null==h?void 0:h.fullStatus)&&v&&v(e)},l?this.dhServer.createSocket({onDigitalHumanCallback:this.onDigitalHumanCallbackPlus},l):this.init(r)}return e.createClass(m,[{key:"init",value:function(t){var n=this,o=t.wrapperId,a=t.token,s=t.appKey,m=t.appId,p=t.connectParams,h=t.renderParams,v=void 0===h?{}:h,f=t.rtcConnectParams,y=t.brtcParams,S=t.rtcServerUrl;if(this.token=a,this.appKey=s,this.appId=m,this.rtcServerUrl=S,this.connectParams=e.objectSpread2({autoChromaKey:(null==v?void 0:v.autoChromaKey)||!1},p),!m&&!s&&!a)throw new Error("appId and appKey or token is required");var C=v.closeLog,g=v.audio3AConstraints,R=(u.createHumanDom({autoChromaKey:!!v.autoChromaKey||!r(v.chromaEffects)})||{}).playerWrapper,I=document.getElementById(o||"");I||console.error("dom with wrapperId is not find"),R&&I&&I.appendChild(R),u.appendRtcDomStyle();var P=e.objectSpread2({remotevideoviewid:o,showvideobps:!1,usingvideo:!1,usingaudio:!(null==p||!p.pullAudioFromRtc),aspublisher:!(null==p||!p.pullAudioFromRtc),autopublish:!(null==p||!p.pullAudioFromRtc),usingdatachannel:!0,autoplaymuted:!1},y);this.rtcServer=new c.default({closeLog:C,videoWrapperId:u.RTC_VIDEO_ID,rtcConfig:P,bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback,onLocalStream:this.onLocalStream}}),(!r(null==v?void 0:v.chromaEffects)||null!=v&&v.autoChromaKey)&&(this.cutoutInstance=new d.default({sourceId:u.RTC_VIDEO_ID,effects:null==v?void 0:v.chromaEffects})),r(f)||(this.rtcConnectParams=f,this.rtcServer.openRtc(f)),null!=p&&p.pullAudioFromRtc?this.recordController=null:this.recordController=new l.default({pickAudioMode:null==p?void 0:p.pickAudioMode,audio3AConstraints:g,onAudioData:function(e){1===n.wsReadyState&&n.sendMessage(e,null)},onError:function(e){n.onDigitalHumanCallback&&n.onDigitalHumanCallback({status:i.statusEnum.DH_LIB_ERROR,content:{type:i.errorTypeEnum.CONNECT_ERROR,msg:{code:-1,errMsg:e.message}}})}})}},{key:"startRecord",value:function(){var e;null!==(e=this.connectParams)&&void 0!==e&&e.pullAudioFromRtc?this.rtcServer&&this.rtcServer.muteMicphone(!1):this.recordController&&this.recordController.start()}},{key:"stopRecord",value:function(){var e;if(null!==(e=this.connectParams)&&void 0!==e&&e.pullAudioFromRtc)return this.rtcServer&&this.rtcServer.muteMicphone(!0),void this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:a.v4(),clientTs:Date.now(),body:""},null);this.recordController&&this.recordController.stop(),this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:a.v4(),clientTs:Date.now(),body:""},null)}},{key:"muteMicrophone",value:function(e){var t;null===(t=this.rtcServer)||void 0===t||t.muteMicphone(e)}}]),m}();exports.default=m;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { Mic3AType } from "./interface";
|
|
1
2
|
export type PickAudioMode = 'pressButton' | 'free';
|
|
2
3
|
interface RecordControllerOptions {
|
|
3
4
|
pickAudioMode?: PickAudioMode;
|
|
5
|
+
audio3AConstraints?: Mic3AType;
|
|
4
6
|
onAudioData: (pcmData: ArrayBuffer) => void;
|
|
5
7
|
onError?: (err: Error) => void;
|
|
6
8
|
pullAudioFromRtc?: boolean;
|
|
@@ -13,8 +15,11 @@ declare class RecordController {
|
|
|
13
15
|
private processor;
|
|
14
16
|
private mediaStream;
|
|
15
17
|
private recording;
|
|
18
|
+
private audio3AConstraints?;
|
|
16
19
|
constructor(options: RecordControllerOptions);
|
|
20
|
+
preWarmStream(): Promise<void>;
|
|
17
21
|
start(): Promise<void>;
|
|
18
22
|
stop(): void;
|
|
23
|
+
destroy(): void;
|
|
19
24
|
}
|
|
20
25
|
export default RecordController;
|
package/cjs/RecordController.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_virtual/_rollupPluginBabelHelpers.js");function t(e,t,r,o){try{for(var n=(o=o||{})&&"number"==typeof o.index?o.index:0,i=o&&"number"==typeof o.offset?o.offset:0,a=0,s=n;s<e.length;s++)a+=e[s].length;a=Math.max(0,a-Math.floor(i));var u=t/r;u>1?a=Math.floor(a/u):(u=1,r=t);for(var c=new Int16Array(a),
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_virtual/_rollupPluginBabelHelpers.js");function t(e,t,r,o){try{for(var n=(o=o||{})&&"number"==typeof o.index?o.index:0,i=o&&"number"==typeof o.offset?o.offset:0,a=0,s=n;s<e.length;s++)a+=e[s].length;a=Math.max(0,a-Math.floor(i));var u=t/r;u>1?a=Math.floor(a/u):(u=1,r=t);for(var c=new Int16Array(a),h=0,d=e.length;n<d;n++){for(var p=e[n],f=i,l=p.length;f<l;){var m=Math.floor(f),v=Math.ceil(f),x=f-m;c[h]=p[m]+(p[v]-p[m])*x,h++,f+=u}i=f-l}return{index:n,offset:i,sampleRate:r,data:c}}catch(e){console.log("转音错误了",e)}}function r(e){for(var t=e,r=new Int16Array(e.length),o=0;o<t.length;o++){var n=Math.max(-1,Math.min(1,t[o]));r[o]=n<0?32768*n:32767*n}return r}var o=function(o,n){function i(t){e.classCallCheck(this,i),e.defineProperty(this,"onAudioData",void 0),e.defineProperty(this,"onError",void 0),e.defineProperty(this,"audioContext",null),e.defineProperty(this,"processor",null),e.defineProperty(this,"mediaStream",null),e.defineProperty(this,"recording",!1),e.defineProperty(this,"audio3AConstraints",void 0),this.onAudioData=t.onAudioData,this.onError=t.onError,this.audio3AConstraints=t.audio3AConstraints,this.preWarmStream()}return e.createClass(i,[{key:"preWarmStream",value:function(){return(o=o||e.asyncToGenerator(e.regeneratorRuntime().mark((function t(){var r;return e.regeneratorRuntime().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,this.audioContext=new(window.AudioContext||window.webkitAudioContext)({sampleRate:16e3}),t.next=4,navigator.mediaDevices.getUserMedia({audio:e.objectSpread2({echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0},this.audio3AConstraints),video:!1});case 4:this.mediaStream=t.sent,r=this.audioContext.createMediaStreamSource(this.mediaStream),this.processor=this.audioContext.createScriptProcessor(1024,1,1),r.connect(this.processor),this.processor.connect(this.audioContext.destination),this.processor.onaudioprocess=function(){},t.next=15;break;case 12:t.prev=12,t.t0=t.catch(0),console.error("preWarmStream error",t.t0);case 15:case"end":return t.stop()}}),t,this,[[0,12]])})))).apply(this,arguments)}},{key:"start",value:function(){return(n=n||e.asyncToGenerator(e.regeneratorRuntime().mark((function o(){var n=this;return e.regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!this.recording){e.next=2;break}return e.abrupt("return");case 2:if(this.audioContext&&this.processor){e.next=4;break}return e.abrupt("return");case 4:try{this.processor.onaudioprocess=function(e){if(n.recording){var o=t([r(e.inputBuffer.getChannelData(0))],n.audioContext.sampleRate,16e3,null);o&&o.data&&n.onAudioData(o.data.buffer)}},this.recording=!0}catch(e){this.onError&&this.onError(e)}case 5:case"end":return e.stop()}}),o,this)})))).apply(this,arguments)}},{key:"stop",value:function(){this.recording=!1,this.processor.onaudioprocess=function(){}}},{key:"destroy",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(e){e.stop()}))}}]),i}();exports.default=o;
|
package/cjs/interface.d.ts
CHANGED
|
@@ -5,6 +5,12 @@ export interface ConnectDataType {
|
|
|
5
5
|
code: number;
|
|
6
6
|
message: string;
|
|
7
7
|
}
|
|
8
|
+
type AsrLanType = 'en-US' | 'ja-JP' | 'zh-CN' | 'pt-BR' | 'de-DE' | 'fr-FR' | 'ko-KR' | 'fil-PH' | 'ms-MY' | 'th-TH' | 'ar-SA';
|
|
9
|
+
export interface Mic3AType {
|
|
10
|
+
echoCancellation?: boolean;
|
|
11
|
+
noiseSuppression?: boolean;
|
|
12
|
+
autoGainControl?: boolean;
|
|
13
|
+
}
|
|
8
14
|
export interface ConnectParamsType {
|
|
9
15
|
figureId?: string | number;
|
|
10
16
|
resolutionHeight: number;
|
|
@@ -37,6 +43,20 @@ export interface ConnectParamsType {
|
|
|
37
43
|
extraParameters?: any;
|
|
38
44
|
parameters?: any;
|
|
39
45
|
reConnect?: boolean;
|
|
46
|
+
asrFormat?: 'pcm' | 'opus';
|
|
47
|
+
asrSample?: number;
|
|
48
|
+
asrLan?: AsrLanType;
|
|
49
|
+
asrDdc?: boolean;
|
|
50
|
+
asrPunc?: boolean;
|
|
51
|
+
asrItn?: boolean;
|
|
52
|
+
asrNonStream?: boolean;
|
|
53
|
+
asrSensitiveWord?: boolean;
|
|
54
|
+
asrBoostingTable?: string;
|
|
55
|
+
botParams?: {
|
|
56
|
+
enabled: boolean;
|
|
57
|
+
};
|
|
58
|
+
enableInterrupt?: boolean;
|
|
59
|
+
asrVadPauseTime?: Number;
|
|
40
60
|
}
|
|
41
61
|
export declare const CONNECT_MAIN_KEY: string[];
|
|
42
62
|
export interface RenderParamsType {
|
|
@@ -44,6 +64,7 @@ export interface RenderParamsType {
|
|
|
44
64
|
autoChromaKey?: boolean;
|
|
45
65
|
closeLog?: boolean;
|
|
46
66
|
fullStatus?: boolean;
|
|
67
|
+
audio3AConstraints?: Mic3AType;
|
|
47
68
|
}
|
|
48
69
|
export declare enum statusEnum {
|
|
49
70
|
DH_LIB_INIT = "DH_LIB_INIT",
|
|
@@ -106,3 +127,4 @@ export interface DhRealtimeHumanType {
|
|
|
106
127
|
isPreOpenSession?: boolean;
|
|
107
128
|
onDigitalHumanCallback: (data: CallbackMsgType) => void;
|
|
108
129
|
}
|
|
130
|
+
export {};
|
package/es/Client.d.ts
CHANGED
package/es/Client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createClass as e,classCallCheck as t,defineProperty as r,objectSpread2 as n,asyncToGenerator as
|
|
1
|
+
import{createClass as e,classCallCheck as t,defineProperty as r,objectSpread2 as n,asyncToGenerator as o,regeneratorRuntime as a}from"./_virtual/_rollupPluginBabelHelpers.js";import i from"@bddh/starling-json";import s from"lodash/isEmpty";import c from"lodash/omit";import l from"lodash/pick";import{v4 as u}from"uuid";import{statusEnum as d,errorTypeEnum as m,CONNECT_MAIN_KEY as h}from"./interface.js";import p from"./Server.js";import v from"./BrtcClient.js";import{createHumanDom as S,appendRtcDomStyle as g,RTC_VIDEO_ID as C,checkIsHuawei as f,playHumanVideo as R,muteHumanVideo as I,removeHumanDom as b,DEFAULT_CANVAS_ID as k}from"./DomControl.js";import y from"./Cutout.js";import D from"./RecordController.js";var E=function(){function E(e){var s=this;t(this,E),r(this,"token",void 0),r(this,"appKey",void 0),r(this,"appId",void 0),r(this,"connectParams",void 0),r(this,"onDigitalHumanCallback",void 0),r(this,"dhServer",void 0),r(this,"rtcServer",void 0),r(this,"rtcConnectParams",void 0),r(this,"cutoutInstance",void 0),r(this,"isHuawei",f()),r(this,"rtcServerUrl",void 0),r(this,"recordController",null),r(this,"wsReadyState",-1),r(this,"isPreOpenSession",!1),r(this,"onDigitalHumanCallbackPlus",(function(e){var t;e.status===d.DH_LIB_FULL_STATUS&&"wsState"===(null===(t=e.content)||void 0===t?void 0:t.type)&&(s.wsReadyState=e.content.readyState),s.onDigitalHumanCallback&&s.onDigitalHumanCallback(e)})),r(this,"onRemoteVideoComing",(function(e){s.rtcConnectParams&&s.rtcConnectParams.feedId&&e===+s.rtcConnectParams.feedId&&s.rtcServer&&s.rtcServer.client&&s.rtcServer.client.subscribeStreaming("remotevideo500",+s.rtcConnectParams.feedId)})),r(this,"onLocalStream",(function(){(s.connectParams||{}).pullAudioFromRtc&&s.rtcServer&&s.rtcServer.muteMicphone(!1)})),r(this,"onRemoteVideoOn",(function(){setTimeout(o(a().mark((function e(){var t,r,n,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=document.getElementById(C),e.next=3,s.cutoutInstance&&s.cutoutInstance.updateSource(C);case 3:t&&(t.setAttribute("data-playing","true"),r=t.videoWidth,n=t.videoHeight,o=document.getElementById(k),i=o.getContext("2d"),o&&i&&(o.width=r,o.height=n,i.drawImage(t,0,0,r,n)));case 4:case"end":return e.stop()}}),e)}))),100)})),r(this,"connectListener",(function(e){var t=e.code,r=e.body,n=e.message;if(0===t&&s.onDigitalHumanCallback&&s.onDigitalHumanCallback({status:d.DH_LIB_WS_SUCCESS,content:e}),0===t){var o=i.parse(r),a=o.rtcServerUrl,c=o.appId,l=o.clientToken,u=o.roomName,h=o.clientId,p=o.feedId,v=o.chargeId;c&&u&&(s.rtcConnectParams={server:s.rtcServerUrl||a,appId:c,token:l,roomName:u,userId:h,feedId:p,chargeId:v},s.rtcServer&&s.rtcServer.openRtc(s.rtcConnectParams))}else s.onDigitalHumanCallback&&s.onDigitalHumanCallback({status:d.DH_LIB_ERROR,content:{type:m.CONNECT_ERROR,msg:{code:t,errMsg:n}}})})),r(this,"createServer",(function(e){var t,r=n(n(n({token:s.token,appKey:s.appKey,appId:s.appId},e?{sessionId:e}:{}),l(s.connectParams,h)),{},{parameters:c(s.connectParams,h),onConnect:s.connectListener,onDigitalHumanCallback:s.onDigitalHumanCallbackPlus});null!==(t=s.dhServer)&&void 0!==t&&t.preConnectSuccess?(s.dhServer.connectParams=r,s.dhServer&&s.dhServer.sendPreConnect(r)):s.dhServer&&s.dhServer.createSocket(r)})),r(this,"checkHumanInstansce",(function(){return s.dhServer?!(s.isPreOpenSession&&!s.dhServer.preConnectSuccess)||(s.onDigitalHumanCallback&&s.onDigitalHumanCallback({status:d.DH_LIB_ERROR,content:{type:m.CONNECT_ERROR,msg:{code:4001,errMsg:"webSocket连接中"}}}),!1):(s.onDigitalHumanCallback&&s.onDigitalHumanCallback({status:d.DH_LIB_ERROR,content:{type:m.CONNECT_ERROR,msg:{code:4002,errMsg:"请先初始化数字人实例"}}}),!1)})),r(this,"sendMessage",(function(e,t){return s.dhServer&&s.dhServer.sendMessage(e,t)})),r(this,"interrupt",o(a().mark((function e(){return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.abrupt("return",new Promise((function(e){s.dhServer&&s.dhServer.sendMessage({requestId:u(),action:"TEXT_RENDER",body:"<interrupt></interrupt>",clientTs:(new Date).getTime()},(function(){return e()}))})));case 1:case"end":return e.stop()}}),e)})))),r(this,"textRender",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg;s.checkHumanInstansce()&&s.dhServer&&s.dhServer.sendMessage({requestId:t||u(),action:"TEXT_RENDER",body:r,clientTs:(new Date).getTime()},n)})),r(this,"textStreamRender",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg,o=i.parse(r).first;s.checkHumanInstansce()&&s.dhServer&&s.dhServer.sendMessage({requestId:t||u(),action:"TEXT_STREAM_RENDER",body:r,clientTs:(new Date).getTime()},o?n:null)})),r(this,"audioRender",(function(e){var t=e.body,r=e.onCallbackMsg;s.checkHumanInstansce()&&s.dhServer&&s.dhServer.sendMessage({requestId:u(),action:"AUDIO_RENDER",body:t,clientTs:(new Date).getTime()},r)})),r(this,"audioStreamRender",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg;s.checkHumanInstansce()&&s.dhServer&&s.dhServer.sendMessage({requestId:t||u(),action:"AUDIO_STREAM_RENDER",body:r,clientTs:(new Date).getTime()},n)})),r(this,"textQuery",(function(e){var t=e.requestId,r=e.body,n=e.onCallbackMsg;s.dhServer&&s.dhServer.sendMessage({requestId:t||u(),action:"TEXT_QUERY",body:r,clientTs:(new Date).getTime()},n)})),r(this,"destroy",o(a().mark((function e(){var t;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,s.dhServer&&s.dhServer.closeSocket();case 2:s.rtcServer&&s.rtcServer.closeRtc(),b(),null===(t=s.recordController)||void 0===t||t.destroy(),s.dhServer=null,s.rtcServer=null;case 7:case"end":return e.stop()}}),e)})))),r(this,"pauseHuman",(function(){return R(!1)})),r(this,"playHuman",(function(){return R(!0)})),r(this,"muteHuman",(function(){return I(!0)})),r(this,"unMuteHuman",(function(){var e=I(!1);try{return s.isHuawei?(e.pause(),e.play()):e.paused&&e.play()}catch(e){console.info(e)}}));var v=e.isPreOpenSession,S=void 0!==v&&v,g=e.wsUrl,y=e.renderParams,D=e.onDigitalHumanCallback;this.isPreOpenSession=S,this.dhServer=new p(g||"wss://open.xiling.baidu.com/cloud/digital-human-demonstration"),this.onDigitalHumanCallback=function(e){e.status!==d.DH_LIB_FULL_STATUS?D&&D(e):(null==y?void 0:y.fullStatus)&&D&&D(e)},S?this.dhServer.createSocket({onDigitalHumanCallback:this.onDigitalHumanCallbackPlus},S):this.init(e)}return e(E,[{key:"init",value:function(e){var t=this,r=e.wrapperId,o=e.token,a=e.appKey,i=e.appId,c=e.connectParams,l=e.renderParams,u=void 0===l?{}:l,h=e.rtcConnectParams,p=e.brtcParams,f=e.rtcServerUrl;if(this.token=o,this.appKey=a,this.appId=i,this.rtcServerUrl=f,this.connectParams=n({autoChromaKey:(null==u?void 0:u.autoChromaKey)||!1},c),!i&&!a&&!o)throw new Error("appId and appKey or token is required");var R=u.closeLog,I=u.audio3AConstraints,b=(S({autoChromaKey:!!u.autoChromaKey||!s(u.chromaEffects)})||{}).playerWrapper,k=document.getElementById(r||"");k||console.error("dom with wrapperId is not find"),b&&k&&k.appendChild(b),g();var E=n({remotevideoviewid:r,showvideobps:!1,usingvideo:!1,usingaudio:!(null==c||!c.pullAudioFromRtc),aspublisher:!(null==c||!c.pullAudioFromRtc),autopublish:!(null==c||!c.pullAudioFromRtc),usingdatachannel:!0,autoplaymuted:!1},p);this.rtcServer=new v({closeLog:R,videoWrapperId:C,rtcConfig:E,bizConfig:{},rtcCallback:{onRemoteVideoComing:this.onRemoteVideoComing,onDigitalHumanCallback:this.onDigitalHumanCallback,onLocalStream:this.onLocalStream}}),(!s(null==u?void 0:u.chromaEffects)||null!=u&&u.autoChromaKey)&&(this.cutoutInstance=new y({sourceId:C,effects:null==u?void 0:u.chromaEffects})),s(h)||(this.rtcConnectParams=h,this.rtcServer.openRtc(h)),null!=c&&c.pullAudioFromRtc?this.recordController=null:this.recordController=new D({pickAudioMode:null==c?void 0:c.pickAudioMode,audio3AConstraints:I,onAudioData:function(e){1===t.wsReadyState&&t.sendMessage(e,null)},onError:function(e){t.onDigitalHumanCallback&&t.onDigitalHumanCallback({status:d.DH_LIB_ERROR,content:{type:m.CONNECT_ERROR,msg:{code:-1,errMsg:e.message}}})}})}},{key:"startRecord",value:function(){var e;null!==(e=this.connectParams)&&void 0!==e&&e.pullAudioFromRtc?this.rtcServer&&this.rtcServer.muteMicphone(!1):this.recordController&&this.recordController.start()}},{key:"stopRecord",value:function(){var e;if(null!==(e=this.connectParams)&&void 0!==e&&e.pullAudioFromRtc)return this.rtcServer&&this.rtcServer.muteMicphone(!0),void this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:u(),clientTs:Date.now(),body:""},null);this.recordController&&this.recordController.stop(),this.sendMessage({action:"AUDIO_QUERY_INTERRUPT",requestId:u(),clientTs:Date.now(),body:""},null)}},{key:"muteMicrophone",value:function(e){var t;null===(t=this.rtcServer)||void 0===t||t.muteMicphone(e)}}]),E}();export{E as default};
|
package/es/RecordController.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { Mic3AType } from "./interface";
|
|
1
2
|
export type PickAudioMode = 'pressButton' | 'free';
|
|
2
3
|
interface RecordControllerOptions {
|
|
3
4
|
pickAudioMode?: PickAudioMode;
|
|
5
|
+
audio3AConstraints?: Mic3AType;
|
|
4
6
|
onAudioData: (pcmData: ArrayBuffer) => void;
|
|
5
7
|
onError?: (err: Error) => void;
|
|
6
8
|
pullAudioFromRtc?: boolean;
|
|
@@ -13,8 +15,11 @@ declare class RecordController {
|
|
|
13
15
|
private processor;
|
|
14
16
|
private mediaStream;
|
|
15
17
|
private recording;
|
|
18
|
+
private audio3AConstraints?;
|
|
16
19
|
constructor(options: RecordControllerOptions);
|
|
20
|
+
preWarmStream(): Promise<void>;
|
|
17
21
|
start(): Promise<void>;
|
|
18
22
|
stop(): void;
|
|
23
|
+
destroy(): void;
|
|
19
24
|
}
|
|
20
25
|
export default RecordController;
|
package/es/RecordController.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createClass as t,asyncToGenerator as e,classCallCheck as r,defineProperty as o,regeneratorRuntime as a}from"./_virtual/_rollupPluginBabelHelpers.js";function
|
|
1
|
+
import{createClass as t,asyncToGenerator as e,classCallCheck as r,defineProperty as o,regeneratorRuntime as a,objectSpread2 as n}from"./_virtual/_rollupPluginBabelHelpers.js";function i(t,e,r,o){try{for(var a=(o=o||{})&&"number"==typeof o.index?o.index:0,n=o&&"number"==typeof o.offset?o.offset:0,i=0,s=a;s<t.length;s++)i+=t[s].length;i=Math.max(0,i-Math.floor(n));var c=e/r;c>1?i=Math.floor(i/c):(c=1,r=e);for(var u=new Int16Array(i),h=0,d=t.length;a<d;a++){for(var p=t[a],l=n,f=p.length;l<f;){var m=Math.floor(l),v=Math.ceil(l),x=l-m;u[h]=p[m]+(p[v]-p[m])*x,h++,l+=c}n=l-f}return{index:a,offset:n,sampleRate:r,data:u}}catch(t){console.log("转音错误了",t)}}function s(t){for(var e=t,r=new Int16Array(t.length),o=0;o<e.length;o++){var a=Math.max(-1,Math.min(1,e[o]));r[o]=a<0?32768*a:32767*a}return r}var c=function(c,u){function h(t){r(this,h),o(this,"onAudioData",void 0),o(this,"onError",void 0),o(this,"audioContext",null),o(this,"processor",null),o(this,"mediaStream",null),o(this,"recording",!1),o(this,"audio3AConstraints",void 0),this.onAudioData=t.onAudioData,this.onError=t.onError,this.audio3AConstraints=t.audio3AConstraints,this.preWarmStream()}return t(h,[{key:"preWarmStream",value:function(){return(c=c||e(a().mark((function t(){var e;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,this.audioContext=new(window.AudioContext||window.webkitAudioContext)({sampleRate:16e3}),t.next=4,navigator.mediaDevices.getUserMedia({audio:n({echoCancellation:!0,noiseSuppression:!0,autoGainControl:!0},this.audio3AConstraints),video:!1});case 4:this.mediaStream=t.sent,e=this.audioContext.createMediaStreamSource(this.mediaStream),this.processor=this.audioContext.createScriptProcessor(1024,1,1),e.connect(this.processor),this.processor.connect(this.audioContext.destination),this.processor.onaudioprocess=function(){},t.next=15;break;case 12:t.prev=12,t.t0=t.catch(0),console.error("preWarmStream error",t.t0);case 15:case"end":return t.stop()}}),t,this,[[0,12]])})))).apply(this,arguments)}},{key:"start",value:function(){return(u=u||e(a().mark((function t(){var e=this;return a().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.recording){t.next=2;break}return t.abrupt("return");case 2:if(this.audioContext&&this.processor){t.next=4;break}return t.abrupt("return");case 4:try{this.processor.onaudioprocess=function(t){if(e.recording){var r=i([s(t.inputBuffer.getChannelData(0))],e.audioContext.sampleRate,16e3,null);r&&r.data&&e.onAudioData(r.data.buffer)}},this.recording=!0}catch(t){this.onError&&this.onError(t)}case 5:case"end":return t.stop()}}),t,this)})))).apply(this,arguments)}},{key:"stop",value:function(){this.recording=!1,this.processor.onaudioprocess=function(){}}},{key:"destroy",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(t){t.stop()}))}}]),h}();export{c as default};
|
package/es/interface.d.ts
CHANGED
|
@@ -5,6 +5,12 @@ export interface ConnectDataType {
|
|
|
5
5
|
code: number;
|
|
6
6
|
message: string;
|
|
7
7
|
}
|
|
8
|
+
type AsrLanType = 'en-US' | 'ja-JP' | 'zh-CN' | 'pt-BR' | 'de-DE' | 'fr-FR' | 'ko-KR' | 'fil-PH' | 'ms-MY' | 'th-TH' | 'ar-SA';
|
|
9
|
+
export interface Mic3AType {
|
|
10
|
+
echoCancellation?: boolean;
|
|
11
|
+
noiseSuppression?: boolean;
|
|
12
|
+
autoGainControl?: boolean;
|
|
13
|
+
}
|
|
8
14
|
export interface ConnectParamsType {
|
|
9
15
|
figureId?: string | number;
|
|
10
16
|
resolutionHeight: number;
|
|
@@ -37,6 +43,20 @@ export interface ConnectParamsType {
|
|
|
37
43
|
extraParameters?: any;
|
|
38
44
|
parameters?: any;
|
|
39
45
|
reConnect?: boolean;
|
|
46
|
+
asrFormat?: 'pcm' | 'opus';
|
|
47
|
+
asrSample?: number;
|
|
48
|
+
asrLan?: AsrLanType;
|
|
49
|
+
asrDdc?: boolean;
|
|
50
|
+
asrPunc?: boolean;
|
|
51
|
+
asrItn?: boolean;
|
|
52
|
+
asrNonStream?: boolean;
|
|
53
|
+
asrSensitiveWord?: boolean;
|
|
54
|
+
asrBoostingTable?: string;
|
|
55
|
+
botParams?: {
|
|
56
|
+
enabled: boolean;
|
|
57
|
+
};
|
|
58
|
+
enableInterrupt?: boolean;
|
|
59
|
+
asrVadPauseTime?: Number;
|
|
40
60
|
}
|
|
41
61
|
export declare const CONNECT_MAIN_KEY: string[];
|
|
42
62
|
export interface RenderParamsType {
|
|
@@ -44,6 +64,7 @@ export interface RenderParamsType {
|
|
|
44
64
|
autoChromaKey?: boolean;
|
|
45
65
|
closeLog?: boolean;
|
|
46
66
|
fullStatus?: boolean;
|
|
67
|
+
audio3AConstraints?: Mic3AType;
|
|
47
68
|
}
|
|
48
69
|
export declare enum statusEnum {
|
|
49
70
|
DH_LIB_INIT = "DH_LIB_INIT",
|
|
@@ -106,3 +127,4 @@ export interface DhRealtimeHumanType {
|
|
|
106
127
|
isPreOpenSession?: boolean;
|
|
107
128
|
onDigitalHumanCallback: (data: CallbackMsgType) => void;
|
|
108
129
|
}
|
|
130
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bddh/starling-realtime-client",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.8-beta.1",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"react",
|
|
6
6
|
"indexDB"
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"lodash": "^4.17.21",
|
|
45
45
|
"uuid": "^9.0.0"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "d6614b38cab03873b04dc027cfd13c7e6b430a57"
|
|
48
48
|
}
|