yjz-web-sdk 1.0.8-beta.3 → 1.0.8-beta.4
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.
|
@@ -120,7 +120,8 @@ export declare class ClarityData {
|
|
|
120
120
|
export declare class ChannelData {
|
|
121
121
|
type: ChannelDataType;
|
|
122
122
|
data: string | null;
|
|
123
|
-
|
|
123
|
+
myId: string | null;
|
|
124
|
+
constructor(type: ChannelDataType, data?: string | null, myId?: string | null);
|
|
124
125
|
toString(): string;
|
|
125
126
|
/**
|
|
126
127
|
* 格式化数据
|
|
@@ -144,7 +145,7 @@ export declare class ChannelData {
|
|
|
144
145
|
* 生成输入数据
|
|
145
146
|
* @param data 输入数据对象
|
|
146
147
|
*/
|
|
147
|
-
static input(data: any): ChannelData;
|
|
148
|
+
static input(data: any, myId: string): ChannelData;
|
|
148
149
|
/**
|
|
149
150
|
* 生成鼠标滚动数据
|
|
150
151
|
* @param data 输入数据对象
|
|
@@ -20,9 +20,10 @@ export interface WebRTCConfigOptions {
|
|
|
20
20
|
token: string;
|
|
21
21
|
isGroup: boolean;
|
|
22
22
|
turnKey?: Array<string>;
|
|
23
|
-
hostTurn
|
|
24
|
-
spareTurn
|
|
23
|
+
hostTurn?: Array<string>;
|
|
24
|
+
spareTurn?: Array<string>;
|
|
25
25
|
maxRecount?: number;
|
|
26
|
+
isCalculate?: boolean;
|
|
26
27
|
}
|
|
27
28
|
export declare class WebRTCConfig {
|
|
28
29
|
signalServerUrl: string;
|
package/lib/yjz-web-sdk.js
CHANGED
|
@@ -418,6 +418,7 @@ class SignalingClient extends EventEmitter {
|
|
|
418
418
|
turnKey: this.config.turnKey
|
|
419
419
|
};
|
|
420
420
|
const jsonMessage = JSON.stringify(message);
|
|
421
|
+
console.log("sendSignInMessage===>", jsonMessage);
|
|
421
422
|
this.sendMessage(jsonMessage);
|
|
422
423
|
}
|
|
423
424
|
sendSwitchControlMessage(isCtrl) {
|
|
@@ -3263,14 +3264,16 @@ class ClarityData {
|
|
|
3263
3264
|
}
|
|
3264
3265
|
}
|
|
3265
3266
|
class ChannelData {
|
|
3266
|
-
constructor(type, data = null) {
|
|
3267
|
+
constructor(type, data = null, myId = null) {
|
|
3267
3268
|
__publicField(this, "type");
|
|
3268
3269
|
__publicField(this, "data");
|
|
3270
|
+
__publicField(this, "myId");
|
|
3269
3271
|
this.type = type;
|
|
3270
3272
|
this.data = data;
|
|
3273
|
+
this.myId = myId;
|
|
3271
3274
|
}
|
|
3272
3275
|
toString() {
|
|
3273
|
-
return JSON.stringify({ type: this.type, data: this.data });
|
|
3276
|
+
return JSON.stringify({ type: this.type, data: this.data, myId: this.myId });
|
|
3274
3277
|
}
|
|
3275
3278
|
/**
|
|
3276
3279
|
* 格式化数据
|
|
@@ -3286,24 +3289,25 @@ class ChannelData {
|
|
|
3286
3289
|
* @param touchData 触摸数据,可以是任意类型
|
|
3287
3290
|
*/
|
|
3288
3291
|
static click(touchData) {
|
|
3289
|
-
return new ChannelData("ClickData", this.formatData(touchData));
|
|
3292
|
+
return new ChannelData("ClickData", this.formatData(touchData), "");
|
|
3290
3293
|
}
|
|
3291
3294
|
static action(actionData) {
|
|
3292
|
-
return new ChannelData("ActionCommand", this.formatData(actionData));
|
|
3295
|
+
return new ChannelData("ActionCommand", this.formatData(actionData), "");
|
|
3293
3296
|
}
|
|
3294
3297
|
/**
|
|
3295
3298
|
* 生成剪贴板数据
|
|
3296
3299
|
* @param data 剪贴板数据,可以是字符串或其他类型
|
|
3297
3300
|
*/
|
|
3298
3301
|
static clipboard(data) {
|
|
3299
|
-
|
|
3302
|
+
console.log(`剪贴板数据,可以是字符串或其他类型 ${data}`);
|
|
3303
|
+
return new ChannelData("ClipboardData", this.formatData(data), "");
|
|
3300
3304
|
}
|
|
3301
3305
|
/**
|
|
3302
3306
|
* 生成输入数据
|
|
3303
3307
|
* @param data 输入数据对象
|
|
3304
3308
|
*/
|
|
3305
|
-
static input(data) {
|
|
3306
|
-
return new ChannelData("ActionInput", this.formatData(data));
|
|
3309
|
+
static input(data, myId) {
|
|
3310
|
+
return new ChannelData("ActionInput", this.formatData(data), myId);
|
|
3307
3311
|
}
|
|
3308
3312
|
/**
|
|
3309
3313
|
* 生成鼠标滚动数据
|
|
@@ -3397,7 +3401,7 @@ class WebRTCClient extends EventEmitter {
|
|
|
3397
3401
|
channelData = ChannelData.clipboard(data);
|
|
3398
3402
|
break;
|
|
3399
3403
|
case ChannelDataType.ActionInput:
|
|
3400
|
-
channelData = ChannelData.input(data);
|
|
3404
|
+
channelData = ChannelData.input(data, this.config.myId);
|
|
3401
3405
|
break;
|
|
3402
3406
|
case ChannelDataType.ActionChinese:
|
|
3403
3407
|
channelData = ChannelData.chinese(data);
|
|
@@ -3730,6 +3734,7 @@ const testMultipleTurnServers = async (servers, timeoutMs = 600) => {
|
|
|
3730
3734
|
})
|
|
3731
3735
|
);
|
|
3732
3736
|
const results = await Promise.all(testPromises);
|
|
3737
|
+
console.log("results===>", results);
|
|
3733
3738
|
const available = results.filter((r) => r.rtt !== Infinity);
|
|
3734
3739
|
if (available.length === 0) {
|
|
3735
3740
|
throw new Error("All TURN servers are unreachable or slow (RTT = Infinity).");
|
|
@@ -3775,6 +3780,7 @@ class WebRTCSdk extends EventEmitter {
|
|
|
3775
3780
|
*/
|
|
3776
3781
|
__publicField(this, "handleSignaling", (message) => {
|
|
3777
3782
|
var _a, _b, _c, _d, _e;
|
|
3783
|
+
console.log("handleSignaling===>", message);
|
|
3778
3784
|
if (!this.config) return;
|
|
3779
3785
|
switch (message.type) {
|
|
3780
3786
|
case MessageType.Peers:
|
|
@@ -3893,6 +3899,7 @@ class WebRTCSdk extends EventEmitter {
|
|
|
3893
3899
|
throw createWebRtcError(FailCode.OPTION_ERR, "中继配置为空");
|
|
3894
3900
|
}
|
|
3895
3901
|
const tryTurnList = async (list, isHost) => {
|
|
3902
|
+
if (list.length === 0) return false;
|
|
3896
3903
|
try {
|
|
3897
3904
|
const result = await selectBestTurnServer(list);
|
|
3898
3905
|
if (result.url && typeof result.rtt === "number") {
|
|
@@ -3911,14 +3918,23 @@ class WebRTCSdk extends EventEmitter {
|
|
|
3911
3918
|
};
|
|
3912
3919
|
const triedHost = ((_a = this.options.hostTurn) == null ? void 0 : _a.length) ? await tryTurnList(this.options.hostTurn, true) : false;
|
|
3913
3920
|
if (!triedHost && ((_b = this.options.spareTurn) == null ? void 0 : _b.length)) {
|
|
3914
|
-
const
|
|
3921
|
+
const allTurns = (this.options.hostTurn ?? []).concat(this.options.spareTurn ?? []);
|
|
3922
|
+
const triedSpare = await tryTurnList(allTurns, false);
|
|
3915
3923
|
if (!triedSpare) {
|
|
3916
|
-
|
|
3924
|
+
if (this.options.turnServerUri) {
|
|
3925
|
+
this.options.turnKey = [this.options.turnServerUri];
|
|
3926
|
+
} else {
|
|
3927
|
+
throw createWebRtcError(FailCode.OPTION_ERR, "暂无可用TURN服务器");
|
|
3928
|
+
}
|
|
3917
3929
|
}
|
|
3918
3930
|
} else if (!triedHost) {
|
|
3919
|
-
const triedSpare = await tryTurnList(this.options.hostTurn, false);
|
|
3931
|
+
const triedSpare = await tryTurnList(this.options.hostTurn ?? [], false);
|
|
3920
3932
|
if (!triedSpare) {
|
|
3921
|
-
|
|
3933
|
+
if (this.options.turnServerUri) {
|
|
3934
|
+
this.options.turnKey = [this.options.turnServerUri];
|
|
3935
|
+
} else {
|
|
3936
|
+
throw createWebRtcError(FailCode.OPTION_ERR, "暂无可用TURN服务器");
|
|
3937
|
+
}
|
|
3922
3938
|
}
|
|
3923
3939
|
}
|
|
3924
3940
|
}
|
|
@@ -3933,7 +3949,14 @@ class WebRTCSdk extends EventEmitter {
|
|
|
3933
3949
|
if (this.isConnecting) return;
|
|
3934
3950
|
this.isConnecting = true;
|
|
3935
3951
|
try {
|
|
3936
|
-
|
|
3952
|
+
if (areTurnListsEmpty(this.options)) {
|
|
3953
|
+
if (!this.options.turnServerUri) {
|
|
3954
|
+
this.emit(EmitType.webrtcError, createWebRtcError(FailCode.OPTION_ERR, "暂无可用TURN服务器"));
|
|
3955
|
+
return;
|
|
3956
|
+
}
|
|
3957
|
+
} else {
|
|
3958
|
+
await this.initConfig();
|
|
3959
|
+
}
|
|
3937
3960
|
this.initConnectConfig();
|
|
3938
3961
|
(_a = this.signalingClient) == null ? void 0 : _a.start();
|
|
3939
3962
|
} catch (e) {
|
package/lib/yjz-web-sdk.umd.cjs
CHANGED
|
@@ -31,7 +31,7 @@ type:t[7]};for(let r=8;r<t.length;r+=2)switch(t[r]){case"raddr":n.relatedAddress
|
|
|
31
31
|
// algorithm is case-sensitive in Edge.
|
|
32
32
|
value:t[1].toUpperCase()}},t.getDtlsParameters=function(e,n){return{role:"auto",fingerprints:t.matchPrefix(e+n,"a=fingerprint:").map(t.parseFingerprint)}},t.writeDtlsParameters=function(e,t){let n="a=setup:"+t+"\r\n";return e.fingerprints.forEach((e=>{n+="a=fingerprint:"+e.algorithm+" "+e.value+"\r\n"})),n},t.parseCryptoLine=function(e){const t=e.substring(9).split(" ");return{tag:parseInt(t[0],10),cryptoSuite:t[1],keyParams:t[2],sessionParams:t.slice(3)}},t.writeCryptoLine=function(e){return"a=crypto:"+e.tag+" "+e.cryptoSuite+" "+("object"==typeof e.keyParams?t.writeCryptoKeyParams(e.keyParams):e.keyParams)+(e.sessionParams?" "+e.sessionParams.join(" "):"")+"\r\n"},t.parseCryptoKeyParams=function(e){if(0!==e.indexOf("inline:"))return null;const t=e.substring(7).split("|");return{keyMethod:"inline",keySalt:t[0],lifeTime:t[1],mkiValue:t[2]?t[2].split(":")[0]:void 0,mkiLength:t[2]?t[2].split(":")[1]:void 0}},t.writeCryptoKeyParams=function(e){return e.keyMethod+":"+e.keySalt+(e.lifeTime?"|"+e.lifeTime:"")+(e.mkiValue&&e.mkiLength?"|"+e.mkiValue+":"+e.mkiLength:"")},t.getCryptoParameters=function(e,n){return t.matchPrefix(e+n,"a=crypto:").map(t.parseCryptoLine)},t.getIceParameters=function(e,n){const r=t.matchPrefix(e+n,"a=ice-ufrag:")[0],i=t.matchPrefix(e+n,"a=ice-pwd:")[0];return r&&i?{usernameFragment:r.substring(12),password:i.substring(10)}:null},t.writeIceParameters=function(e){let t="a=ice-ufrag:"+e.usernameFragment+"\r\na=ice-pwd:"+e.password+"\r\n";return e.iceLite&&(t+="a=ice-lite\r\n"),t},t.parseRtpParameters=function(e){const n={codecs:[],headerExtensions:[],fecMechanisms:[],rtcp:[]},r=t.splitLines(e)[0].split(" ");n.profile=r[2];for(let o=3;o<r.length;o++){const i=r[o],s=t.matchPrefix(e,"a=rtpmap:"+i+" ")[0];if(s){const r=t.parseRtpMap(s),o=t.matchPrefix(e,"a=fmtp:"+i+" ");switch(r.parameters=o.length?t.parseFmtp(o[0]):{},r.rtcpFeedback=t.matchPrefix(e,"a=rtcp-fb:"+i+" ").map(t.parseRtcpFb),n.codecs.push(r),r.name.toUpperCase()){case"RED":case"ULPFEC":n.fecMechanisms.push(r.name.toUpperCase())}}}t.matchPrefix(e,"a=extmap:").forEach((e=>{n.headerExtensions.push(t.parseExtmap(e))}));const i=t.matchPrefix(e,"a=rtcp-fb:* ").map(t.parseRtcpFb);return n.codecs.forEach((e=>{i.forEach((t=>{e.rtcpFeedback.find((e=>e.type===t.type&&e.parameter===t.parameter))||e.rtcpFeedback.push(t)}))})),n},t.writeRtpDescription=function(e,n){let r="";r+="m="+e+" ",r+=n.codecs.length>0?"9":"0",r+=" "+(n.profile||"UDP/TLS/RTP/SAVPF")+" ",r+=n.codecs.map((e=>void 0!==e.preferredPayloadType?e.preferredPayloadType:e.payloadType)).join(" ")+"\r\n",r+="c=IN IP4 0.0.0.0\r\n",r+="a=rtcp:9 IN IP4 0.0.0.0\r\n",n.codecs.forEach((e=>{r+=t.writeRtpMap(e),r+=t.writeFmtp(e),r+=t.writeRtcpFb(e)}));let i=0;return n.codecs.forEach((e=>{e.maxptime>i&&(i=e.maxptime)})),i>0&&(r+="a=maxptime:"+i+"\r\n"),n.headerExtensions&&n.headerExtensions.forEach((e=>{r+=t.writeExtmap(e)})),r},t.parseRtpEncodingParameters=function(e){const n=[],r=t.parseRtpParameters(e),i=-1!==r.fecMechanisms.indexOf("RED"),o=-1!==r.fecMechanisms.indexOf("ULPFEC"),s=t.matchPrefix(e,"a=ssrc:").map((e=>t.parseSsrcMedia(e))).filter((e=>"cname"===e.attribute)),a=s.length>0&&s[0].ssrc;let c;const d=t.matchPrefix(e,"a=ssrc-group:FID").map((e=>e.substring(17).split(" ").map((e=>parseInt(e,10)))));d.length>0&&d[0].length>1&&d[0][0]===a&&(c=d[0][1]),r.codecs.forEach((e=>{if("RTX"===e.name.toUpperCase()&&e.parameters.apt){let t={ssrc:a,codecPayloadType:parseInt(e.parameters.apt,10)};a&&c&&(t.rtx={ssrc:c}),n.push(t),i&&(t=JSON.parse(JSON.stringify(t)),t.fec={ssrc:a,mechanism:o?"red+ulpfec":"red"},n.push(t))}})),0===n.length&&a&&n.push({ssrc:a});let l=t.matchPrefix(e,"b=");return l.length&&(l=0===l[0].indexOf("b=TIAS:")?parseInt(l[0].substring(7),10):0===l[0].indexOf("b=AS:")?1e3*parseInt(l[0].substring(5),10)*.95-16e3:void 0,n.forEach((e=>{e.maxBitrate=l}))),n},t.parseRtcpParameters=function(e){const n={},r=t.matchPrefix(e,"a=ssrc:").map((e=>t.parseSsrcMedia(e))).filter((e=>"cname"===e.attribute))[0];r&&(n.cname=r.value,n.ssrc=r.ssrc);const i=t.matchPrefix(e,"a=rtcp-rsize");n.reducedSize=i.length>0,n.compound=0===i.length;const o=t.matchPrefix(e,"a=rtcp-mux");return n.mux=o.length>0,n},t.writeRtcpParameters=function(e){let t="";return e.reducedSize&&(t+="a=rtcp-rsize\r\n"),e.mux&&(t+="a=rtcp-mux\r\n"),void 0!==e.ssrc&&e.cname&&(t+="a=ssrc:"+e.ssrc+" cname:"+e.cname+"\r\n"),t},t.parseMsid=function(e){let n;const r=t.matchPrefix(e,"a=msid:");if(1===r.length)return n=r[0].substring(7).split(" "),{stream:n[0],track:n[1]};const i=t.matchPrefix(e,"a=ssrc:").map((e=>t.parseSsrcMedia(e))).filter((e=>"msid"===e.attribute));return i.length>0?(n=i[0].value.split(" "),{stream:n[0],track:n[1]}):void 0},t.parseSctpDescription=function(e){const n=t.parseMLine(e),r=t.matchPrefix(e,"a=max-message-size:");let i;r.length>0&&(i=parseInt(r[0].substring(19),10)),isNaN(i)&&(i=65536);const o=t.matchPrefix(e,"a=sctp-port:");if(o.length>0)return{port:parseInt(o[0].substring(12),10),protocol:n.fmt,maxMessageSize:i};const s=t.matchPrefix(e,"a=sctpmap:");if(s.length>0){const e=s[0].substring(10).split(" ");return{port:parseInt(e[0],10),protocol:e[1],maxMessageSize:i}}},t.writeSctpDescription=function(e,t){let n=[];return n="DTLS/SCTP"!==e.protocol?["m="+e.kind+" 9 "+e.protocol+" "+t.protocol+"\r\n","c=IN IP4 0.0.0.0\r\n","a=sctp-port:"+t.port+"\r\n"]:["m="+e.kind+" 9 "+e.protocol+" "+t.port+"\r\n","c=IN IP4 0.0.0.0\r\n","a=sctpmap:"+t.port+" "+t.protocol+" 65535\r\n"],void 0!==t.maxMessageSize&&n.push("a=max-message-size:"+t.maxMessageSize+"\r\n"),n.join("")},t.generateSessionId=function(){return Math.random().toString().substr(2,22)},t.writeSessionBoilerplate=function(e,n,r){let i;const o=void 0!==n?n:2;return i=e||t.generateSessionId(),"v=0\r\no="+(r||"thisisadapterortc")+" "+i+" "+o+" IN IP4 127.0.0.1\r\ns=-\r\nt=0 0\r\n"},t.getDirection=function(e,n){const r=t.splitLines(e);for(let t=0;t<r.length;t++)switch(r[t]){case"a=sendrecv":case"a=sendonly":case"a=recvonly":case"a=inactive":return r[t].substring(2)}return n?t.getDirection(n):"sendrecv"},t.getKind=function(e){return t.splitLines(e)[0].split(" ")[0].substring(2)},t.isRejected=function(e){return"0"===e.split(" ",2)[1]},t.parseMLine=function(e){const n=t.splitLines(e)[0].substring(2).split(" ");return{kind:n[0],port:parseInt(n[1],10),protocol:n[2],fmt:n.slice(3).join(" ")}},t.parseOLine=function(e){const n=t.matchPrefix(e,"o=")[0].substring(2).split(" ");return{username:n[0],sessionId:n[1],sessionVersion:parseInt(n[2],10),netType:n[3],addressType:n[4],address:n[5]}},t.isValidSDP=function(e){if("string"!=typeof e||0===e.length)return!1;const n=t.splitLines(e);for(let t=0;t<n.length;t++)if(n[t].length<2||"="!==n[t].charAt(1))return!1;return!0},e.exports=t}(ce)),ce.exports);const le=o(de),he=i({__proto__:null,default:le},[de]);function pe(e){if(!e.RTCIceCandidate||e.RTCIceCandidate&&"foundation"in e.RTCIceCandidate.prototype)return;const t=e.RTCIceCandidate;e.RTCIceCandidate=function(e){if("object"==typeof e&&e.candidate&&0===e.candidate.indexOf("a=")&&((e=JSON.parse(JSON.stringify(e))).candidate=e.candidate.substring(2)),e.candidate&&e.candidate.length){const n=new t(e),r=le.parseCandidate(e.candidate);for(const e in r)e in n||Object.defineProperty(n,e,{value:r[e]});return n.toJSON=function(){return{candidate:n.candidate,sdpMid:n.sdpMid,sdpMLineIndex:n.sdpMLineIndex,usernameFragment:n.usernameFragment}},n}return new t(e)},e.RTCIceCandidate.prototype=t.prototype,T(e,"icecandidate",(t=>(t.candidate&&Object.defineProperty(t,"candidate",{value:new e.RTCIceCandidate(t.candidate),writable:"false"}),t)))}function ue(e){!e.RTCIceCandidate||e.RTCIceCandidate&&"relayProtocol"in e.RTCIceCandidate.prototype||T(e,"icecandidate",(e=>{if(e.candidate){const t=le.parseCandidate(e.candidate.candidate);"relay"===t.type&&(e.candidate.relayProtocol={0:"tls",1:"tcp",2:"udp"}[t.priority>>24])}return e}))}function fe(e,t){if(!e.RTCPeerConnection)return;"sctp"in e.RTCPeerConnection.prototype||Object.defineProperty(e.RTCPeerConnection.prototype,"sctp",{get(){return void 0===this._sctp?null:this._sctp}});const n=e.RTCPeerConnection.prototype.setRemoteDescription;e.RTCPeerConnection.prototype.setRemoteDescription=function(){if(this._sctp=null,"chrome"===t.browser&&t.version>=76){const{sdpSemantics:e}=this.getConfiguration();"plan-b"===e&&Object.defineProperty(this,"sctp",{get(){return void 0===this._sctp?null:this._sctp},enumerable:!0,configurable:!0})}if(function(e){if(!e||!e.sdp)return!1;const t=le.splitSections(e.sdp);return t.shift(),t.some((e=>{const t=le.parseMLine(e);return t&&"application"===t.kind&&-1!==t.protocol.indexOf("SCTP")}))}(arguments[0])){const e=function(e){const t=e.sdp.match(/mozilla...THIS_IS_SDPARTA-(\d+)/);if(null===t||t.length<2)return-1;const n=parseInt(t[1],10);return n!=n?-1:n}(arguments[0]),n=function(e){let n=65536;return"firefox"===t.browser&&(n=t.version<57?-1===e?16384:2147483637:t.version<60?57===t.version?65535:65536:2147483637),n}(e),r=function(e,n){let r=65536;"firefox"===t.browser&&57===t.version&&(r=65535);const i=le.matchPrefix(e.sdp,"a=max-message-size:");return i.length>0?r=parseInt(i[0].substring(19),10):"firefox"===t.browser&&-1!==n&&(r=2147483637),r}(arguments[0],e);let i;i=0===n&&0===r?Number.POSITIVE_INFINITY:0===n||0===r?Math.max(n,r):Math.min(n,r);const o={};Object.defineProperty(o,"maxMessageSize",{get:()=>i}),this._sctp=o}return n.apply(this,arguments)}}function me(e){if(!e.RTCPeerConnection||!("createDataChannel"in e.RTCPeerConnection.prototype))return;function t(e,t){const n=e.send;e.send=function(){const r=arguments[0],i=r.length||r.size||r.byteLength;if("open"===e.readyState&&t.sctp&&i>t.sctp.maxMessageSize)throw new TypeError("Message too large (can send a maximum of "+t.sctp.maxMessageSize+" bytes)");return n.apply(e,arguments)}}const n=e.RTCPeerConnection.prototype.createDataChannel;e.RTCPeerConnection.prototype.createDataChannel=function(){const e=n.apply(this,arguments);return t(e,this),e},T(e,"datachannel",(e=>(t(e.channel,e.target),e)))}function Ce(e){if(!e.RTCPeerConnection||"connectionState"in e.RTCPeerConnection.prototype)return;const t=e.RTCPeerConnection.prototype;Object.defineProperty(t,"connectionState",{get(){return{completed:"connected",checking:"connecting"}[this.iceConnectionState]||this.iceConnectionState},enumerable:!0,configurable:!0}),Object.defineProperty(t,"onconnectionstatechange",{get(){return this._onconnectionstatechange||null},set(e){this._onconnectionstatechange&&(this.removeEventListener("connectionstatechange",this._onconnectionstatechange),delete this._onconnectionstatechange),e&&this.addEventListener("connectionstatechange",this._onconnectionstatechange=e)},enumerable:!0,configurable:!0}),["setLocalDescription","setRemoteDescription"].forEach((e=>{const n=t[e];t[e]=function(){return this._connectionstatechangepoly||(this._connectionstatechangepoly=e=>{const t=e.target;if(t._lastConnectionState!==t.connectionState){t._lastConnectionState=t.connectionState;const n=new Event("connectionstatechange",e);t.dispatchEvent(n)}return e},this.addEventListener("iceconnectionstatechange",this._connectionstatechangepoly)),n.apply(this,arguments)}}))}function ge(e,t){if(!e.RTCPeerConnection)return;if("chrome"===t.browser&&t.version>=71)return;if("safari"===t.browser&&t._safariVersion>=13.1)return;const n=e.RTCPeerConnection.prototype.setRemoteDescription;e.RTCPeerConnection.prototype.setRemoteDescription=function(t){if(t&&t.sdp&&-1!==t.sdp.indexOf("\na=extmap-allow-mixed")){const n=t.sdp.split("\n").filter((e=>"a=extmap-allow-mixed"!==e.trim())).join("\n");e.RTCSessionDescription&&t instanceof e.RTCSessionDescription?arguments[0]=new e.RTCSessionDescription({type:t.type,sdp:n}):t.sdp=n}return n.apply(this,arguments)}}function ye(e,t){if(!e.RTCPeerConnection||!e.RTCPeerConnection.prototype)return;const n=e.RTCPeerConnection.prototype.addIceCandidate;n&&0!==n.length&&(e.RTCPeerConnection.prototype.addIceCandidate=function(){return arguments[0]?("chrome"===t.browser&&t.version<78||"firefox"===t.browser&&t.version<68||"safari"===t.browser)&&arguments[0]&&""===arguments[0].candidate?Promise.resolve():n.apply(this,arguments):(arguments[1]&&arguments[1].apply(null),Promise.resolve())})}function ve(e,t){if(!e.RTCPeerConnection||!e.RTCPeerConnection.prototype)return;const n=e.RTCPeerConnection.prototype.setLocalDescription;n&&0!==n.length&&(e.RTCPeerConnection.prototype.setLocalDescription=function(){let e=arguments[0]||{};if("object"!=typeof e||e.type&&e.sdp)return n.apply(this,arguments);if(e={type:e.type,sdp:e.sdp},!e.type)switch(this.signalingState){case"stable":case"have-local-offer":case"have-remote-pranswer":e.type="offer";break;default:e.type="answer"}if(e.sdp||"offer"!==e.type&&"answer"!==e.type)return n.apply(this,[e]);return("offer"===e.type?this.createOffer:this.createAnswer).apply(this).then((e=>n.apply(this,[e])))})}const Te=Object.freeze(Object.defineProperty({__proto__:null,removeExtmapAllowMixed:ge,shimAddIceCandidateNullOrEmpty:ye,shimConnectionState:Ce,shimMaxMessageSize:fe,shimParameterlessSetLocalDescription:ve,shimRTCIceCandidate:pe,shimRTCIceCandidateRelayProtocol:ue,shimSendThrowTypeError:me},Symbol.toStringTag,{value:"Module"}));!function({window:e}={},t={shimChrome:!0,shimFirefox:!0,shimSafari:!0}){const n=b,r=function(e){const t={browser:null,version:null};if(void 0===e||!e.navigator||!e.navigator.userAgent)return t.browser="Not a browser.",t;const{navigator:n}=e;if(n.userAgentData&&n.userAgentData.brands){const e=n.userAgentData.brands.find((e=>"Chromium"===e.brand));if(e)return{browser:"chrome",version:parseInt(e.version,10)}}if(n.mozGetUserMedia)t.browser="firefox",t.version=parseInt(v(n.userAgent,/Firefox\/(\d+)\./,1));else if(n.webkitGetUserMedia||!1===e.isSecureContext&&e.webkitRTCPeerConnection)t.browser="chrome",t.version=parseInt(v(n.userAgent,/Chrom(e|ium)\/(\d+)\./,2));else{if(!e.RTCPeerConnection||!n.userAgent.match(/AppleWebKit\/(\d+)\./))return t.browser="Not a supported browser.",t;t.browser="safari",t.version=parseInt(v(n.userAgent,/AppleWebKit\/(\d+)\./,1)),t.supportsUnifiedPlan=e.RTCRtpTransceiver&&"currentDirection"in e.RTCRtpTransceiver.prototype,t._safariVersion=v(n.userAgent,/Version\/(\d+(\.?\d+))/,1)}return t}(e),i={browserDetails:r,commonShim:Te,extractVersion:v,disableLog:S,disableWarnings:R,
|
|
33
33
|
// Expose sdp as a convenience. For production apps include directly.
|
|
34
|
-
sdp:he};switch(r.browser){case"chrome":if(!U||!N||!t.shimChrome)return n("Chrome shim is not included in this adapter release."),i;if(null===r.version)return n("Chrome shim can not determine version, not shimming."),i;n("adapter.js shimming chrome."),i.browserShim=U,ye(e,r),ve(e),A(e,r),O(e),N(e,r),I(e),L(e,r),D(e),M(e),j(e,r),pe(e),ue(e),Ce(e),fe(e,r),me(e),ge(e,r);break;case"firefox":if(!$||!z||!t.shimFirefox)return n("Firefox shim is not included in this adapter release."),i;n("adapter.js shimming firefox."),i.browserShim=$,ye(e,r),ve(e),F(e,r),z(e,r),K(e),B(e),V(e),W(e),G(e),H(e),J(e),q(e),Q(e),pe(e),Ce(e),fe(e,r),me(e);break;case"safari":if(!se||!t.shimSafari)return n("Safari shim is not included in this adapter release."),i;n("adapter.js shimming safari."),i.browserShim=se,ye(e,r),ve(e),ne(e),ie(e),Z(e),X(e),Y(e),re(e),ee(e),oe(e),pe(e),ue(e),fe(e,r),me(e),ge(e,r);break;default:n("Unsupported browser!")}}({window:"undefined"==typeof window?void 0:window});class Se{constructor(e){r(this,"signalServerUrl"),r(this,"myId"),r(this,"roomId"),r(this,"targetId"),r(this,"stunServerUri"),r(this,"stunServerUriAli"),r(this,"stunServerUriTel"),r(this,"turnServerUri"),r(this,"turnServerUserName"),r(this,"turnServerPassword"),r(this,"isControl"),r(this,"screenshotWidth"),r(this,"screenshotHeight"),r(this,"screenshotQuality"),r(this,"token"),r(this,"isGroup"),r(this,"turnKey"),this.signalServerUrl=e.signalServerUrl||"",this.myId=e.myId||"",this.roomId=e.roomId||"",this.targetId=e.targetId||"",this.turnKey=e.turnKey||[],this.stunServerUri=e.stunServerUri||"stun:stun.l.google.com:19302",this.stunServerUriAli=e.stunServerUriAli||"stun:stun.middle.aliyun.com:3478",this.stunServerUriTel=e.stunServerUriTel||"stun:stun.qq.com:3478",this.turnServerUri=e.turnServerUri||"turn:121.37.25.106:3478",this.turnServerUserName=e.turnServerUserName||"yangyj",this.turnServerPassword=e.turnServerPassword||"hb@2025@168",this.isControl=e.isControl??!0,this.screenshotWidth=e.screenshotWidth||144,this.screenshotHeight=e.screenshotHeight||256,this.screenshotQuality=e.screenshotQuality||20,this.token=e.token,this.isGroup=e.isGroup}}const Re=(e,t,n)=>{e.createAnswer().then((r=>be(e,r,t,n))).catch((e=>null==n?void 0:n(f(p.CREATE_ANSWER,e))))},be=(e,t,n,r)=>{e.setLocalDescription(t).then((()=>{null==n||n(t.sdp??"")})).catch((e=>{null==r||r(f(p.LOCAL_DES,e))}))},we=(e,t,n)=>{e.createOffer().then((r=>Ee(e,r,t,n))).catch((e=>null==n?void 0:n(f(p.CREATE_OFFER,e))))},Ee=(e,t,n,r)=>{e.setLocalDescription(t).then((()=>n(t.sdp??""))).catch((e=>null==r?void 0:r(f(p.LOCAL_DES,e))))};var Pe=(e=>(e.ClickData="ClickData",e.ClipboardData="ClipboardData",e.ActionCommand="ActionCommand",e.ActionInput="ActionInput",e.ActionChinese="ActionChinese",e.ActionRequestCloudDeviceInfo="ActionRequestCloudDeviceInfo",e.ActionClarity="ActionClarity",e.ActionWheel="ActionWheel",e.CloudClipData="CloudClipData",e.ActionCommandEvent="ActionCommandEvent",e.ActionUpdateCloudStatus="ActionUpdateCloudStatus",e.ActionGesture="ActionGesture",e.ActionTrack="ActionTrack",e))(Pe||{}),ke=(e=>(e.ENABLE="ENABLE",e.DISABLE="DISABLE",e))(ke||{}),_e=(e=>(e.ActionBack="ActionBack",e.ActionHome="ActionHome",e.ActionRecent="ActionRecent",e.ActionSwitchNavButtons="ActionSwitchNavButtons",e.ActionSwitchNavGestural="ActionSwitchNavGestural",e))(_e||{}),Ae=(e=>(e.ACTION_CONTROL_VIDEO_AND_AUDIO="ACTION_CONTROL_VIDEO_AND_AUDIO",e.ACTION_CONTROL_VIDEO="ACTION_CONTROL_VIDEO",e.ACTION_CONTROL_AUDIO="ACTION_CONTROL_AUDIO",e))(Ae||{});const Oe={ACTION_DOWN:0,ACTION_MOVE:2,ACTION_UP:1},Ie={ROTATION_0:0,ROTATION_180:2},De={Vertical:0,Horizontal:1};class Me{constructor(e,t,n,i,o,s="adb"){r(this,"action"),r(this,"p"),r(this,"x"),r(this,"y"),r(this,"offsetTime"),r(this,"type"),this.action=e,this.p=t,this.x=n,this.y=i,this.offsetTime=o,this.type=s}}class xe{constructor(e){r(this,"axis"),this.axis=e}}class Le{constructor(e,t){r(this,"keyCode"),r(this,"meta"),this.keyCode=e,this.meta=t}}class Ne{constructor(e,t=null){r(this,"type"),r(this,"data"),this.type=e,this.data=t}toString(){return JSON.stringify({type:this.type,data:this.data})}
|
|
34
|
+
sdp:he};switch(r.browser){case"chrome":if(!U||!N||!t.shimChrome)return n("Chrome shim is not included in this adapter release."),i;if(null===r.version)return n("Chrome shim can not determine version, not shimming."),i;n("adapter.js shimming chrome."),i.browserShim=U,ye(e,r),ve(e),A(e,r),O(e),N(e,r),I(e),L(e,r),D(e),M(e),j(e,r),pe(e),ue(e),Ce(e),fe(e,r),me(e),ge(e,r);break;case"firefox":if(!$||!z||!t.shimFirefox)return n("Firefox shim is not included in this adapter release."),i;n("adapter.js shimming firefox."),i.browserShim=$,ye(e,r),ve(e),F(e,r),z(e,r),K(e),B(e),V(e),W(e),G(e),H(e),J(e),q(e),Q(e),pe(e),Ce(e),fe(e,r),me(e);break;case"safari":if(!se||!t.shimSafari)return n("Safari shim is not included in this adapter release."),i;n("adapter.js shimming safari."),i.browserShim=se,ye(e,r),ve(e),ne(e),ie(e),Z(e),X(e),Y(e),re(e),ee(e),oe(e),pe(e),ue(e),fe(e,r),me(e),ge(e,r);break;default:n("Unsupported browser!")}}({window:"undefined"==typeof window?void 0:window});class Se{constructor(e){r(this,"signalServerUrl"),r(this,"myId"),r(this,"roomId"),r(this,"targetId"),r(this,"stunServerUri"),r(this,"stunServerUriAli"),r(this,"stunServerUriTel"),r(this,"turnServerUri"),r(this,"turnServerUserName"),r(this,"turnServerPassword"),r(this,"isControl"),r(this,"screenshotWidth"),r(this,"screenshotHeight"),r(this,"screenshotQuality"),r(this,"token"),r(this,"isGroup"),r(this,"turnKey"),this.signalServerUrl=e.signalServerUrl||"",this.myId=e.myId||"",this.roomId=e.roomId||"",this.targetId=e.targetId||"",this.turnKey=e.turnKey||[],this.stunServerUri=e.stunServerUri||"stun:stun.l.google.com:19302",this.stunServerUriAli=e.stunServerUriAli||"stun:stun.middle.aliyun.com:3478",this.stunServerUriTel=e.stunServerUriTel||"stun:stun.qq.com:3478",this.turnServerUri=e.turnServerUri||"turn:121.37.25.106:3478",this.turnServerUserName=e.turnServerUserName||"yangyj",this.turnServerPassword=e.turnServerPassword||"hb@2025@168",this.isControl=e.isControl??!0,this.screenshotWidth=e.screenshotWidth||144,this.screenshotHeight=e.screenshotHeight||256,this.screenshotQuality=e.screenshotQuality||20,this.token=e.token,this.isGroup=e.isGroup}}const Re=(e,t,n)=>{e.createAnswer().then((r=>be(e,r,t,n))).catch((e=>null==n?void 0:n(f(p.CREATE_ANSWER,e))))},be=(e,t,n,r)=>{e.setLocalDescription(t).then((()=>{null==n||n(t.sdp??"")})).catch((e=>{null==r||r(f(p.LOCAL_DES,e))}))},we=(e,t,n)=>{e.createOffer().then((r=>Ee(e,r,t,n))).catch((e=>null==n?void 0:n(f(p.CREATE_OFFER,e))))},Ee=(e,t,n,r)=>{e.setLocalDescription(t).then((()=>n(t.sdp??""))).catch((e=>null==r?void 0:r(f(p.LOCAL_DES,e))))};var Pe=(e=>(e.ClickData="ClickData",e.ClipboardData="ClipboardData",e.ActionCommand="ActionCommand",e.ActionInput="ActionInput",e.ActionChinese="ActionChinese",e.ActionRequestCloudDeviceInfo="ActionRequestCloudDeviceInfo",e.ActionClarity="ActionClarity",e.ActionWheel="ActionWheel",e.CloudClipData="CloudClipData",e.ActionCommandEvent="ActionCommandEvent",e.ActionUpdateCloudStatus="ActionUpdateCloudStatus",e.ActionGesture="ActionGesture",e.ActionTrack="ActionTrack",e))(Pe||{}),ke=(e=>(e.ENABLE="ENABLE",e.DISABLE="DISABLE",e))(ke||{}),_e=(e=>(e.ActionBack="ActionBack",e.ActionHome="ActionHome",e.ActionRecent="ActionRecent",e.ActionSwitchNavButtons="ActionSwitchNavButtons",e.ActionSwitchNavGestural="ActionSwitchNavGestural",e))(_e||{}),Ae=(e=>(e.ACTION_CONTROL_VIDEO_AND_AUDIO="ACTION_CONTROL_VIDEO_AND_AUDIO",e.ACTION_CONTROL_VIDEO="ACTION_CONTROL_VIDEO",e.ACTION_CONTROL_AUDIO="ACTION_CONTROL_AUDIO",e))(Ae||{});const Oe={ACTION_DOWN:0,ACTION_MOVE:2,ACTION_UP:1},Ie={ROTATION_0:0,ROTATION_180:2},De={Vertical:0,Horizontal:1};class Me{constructor(e,t,n,i,o,s="adb"){r(this,"action"),r(this,"p"),r(this,"x"),r(this,"y"),r(this,"offsetTime"),r(this,"type"),this.action=e,this.p=t,this.x=n,this.y=i,this.offsetTime=o,this.type=s}}class xe{constructor(e){r(this,"axis"),this.axis=e}}class Le{constructor(e,t){r(this,"keyCode"),r(this,"meta"),this.keyCode=e,this.meta=t}}class Ne{constructor(e,t=null,n=null){r(this,"type"),r(this,"data"),r(this,"myId"),this.type=e,this.data=t,this.myId=n}toString(){return JSON.stringify({type:this.type,data:this.data,myId:this.myId})}
|
|
35
35
|
/**
|
|
36
36
|
* 格式化数据
|
|
37
37
|
* 如果数据已经是字符串,则直接返回;否则使用 JSON.stringify() 转换为字符串
|
|
@@ -41,15 +41,15 @@ sdp:he};switch(r.browser){case"chrome":if(!U||!N||!t.shimChrome)return n("Chrome
|
|
|
41
41
|
/**
|
|
42
42
|
* 生成点击数据
|
|
43
43
|
* @param touchData 触摸数据,可以是任意类型
|
|
44
|
-
*/static click(e){return new Ne("ClickData",this.formatData(e))}static action(e){return new Ne("ActionCommand",this.formatData(e))}
|
|
44
|
+
*/static click(e){return new Ne("ClickData",this.formatData(e),"")}static action(e){return new Ne("ActionCommand",this.formatData(e),"")}
|
|
45
45
|
/**
|
|
46
46
|
* 生成剪贴板数据
|
|
47
47
|
* @param data 剪贴板数据,可以是字符串或其他类型
|
|
48
|
-
*/static clipboard(e){return new Ne("ClipboardData",this.formatData(e))}
|
|
48
|
+
*/static clipboard(e){return new Ne("ClipboardData",this.formatData(e),"")}
|
|
49
49
|
/**
|
|
50
50
|
* 生成输入数据
|
|
51
51
|
* @param data 输入数据对象
|
|
52
|
-
*/static input(e){return new Ne("ActionInput",this.formatData(e))}
|
|
52
|
+
*/static input(e,t){return new Ne("ActionInput",this.formatData(e),t)}
|
|
53
53
|
/**
|
|
54
54
|
* 生成鼠标滚动数据
|
|
55
55
|
* @param data 输入数据对象
|
|
@@ -68,9 +68,9 @@ sdp:he};switch(r.browser){case"chrome":if(!U||!N||!t.shimChrome)return n("Chrome
|
|
|
68
68
|
/**
|
|
69
69
|
* 生成清晰度数据(clarity)
|
|
70
70
|
* @param data 清晰度数据
|
|
71
|
-
*/static clarity(e){return new Ne("ActionClarity",this.formatData(e))}static switchAudio(e){return new Ne("ActionCommandEvent",this.formatData(e))}static changeSender(e){return new Ne("ActionTrack",this.formatData(e))}}class je extends d{constructor(e){super(),r(this,"config"),r(this,"peerConnection",null),r(this,"localStream",null),r(this,"isPushingStream",!1),r(this,"dataChannel",null),r(this,"statsTimer"),r(this,"lastReportTime",0),r(this,"lastBytesReceived",0),r(this,"lastPacketsLost",0),r(this,"lastPacketsReceived",0),r(this,"lostPacketCount",0),r(this,"maxLostRate",0),r(this,"lastSecondDecodedCount",0),this.config=e}async startPush(){if(!this.isPushingStream){this.isPushingStream=!0;try{await this.readyCapture()}catch(e){this.emit(m.webrtcError,f(p.CAMERA,e))}}}handleOffer(e){this.resetPeerConnection(),((e,t,n,r)=>{const i=new RTCSessionDescription({type:"offer",sdp:t});e.setRemoteDescription(i).then((()=>Re(e,n,r))).catch((e=>null==r?void 0:r(f(p.HANDLE_OFFER,e))))})(this.peerConnection,e,(e=>{this.emit(m.sendAnswer,e)}),(e=>this.emit(m.webrtcError,e)))}handleAnswer(e){((e,t,n)=>{const r=new RTCSessionDescription({type:"answer",sdp:t});e.setRemoteDescription(r).catch((e=>null==n?void 0:n(f(p.REMOTE_DES,e))))})(this.peerConnection,e??"",(e=>this.emit(m.webrtcError,e)))}handleIceCandidate(e){((e,t,n)=>{e.addIceCandidate(t).catch((e=>null==n?void 0:n(f(p.HANDLE_ICE,e))))})(this.peerConnection,e,(e=>this.emit(m.webrtcError,e)))}sendChannelData(e,t){var n;try{let r=null;switch(e){case Pe.ClickData:r=Ne.click(t);break;case Pe.ClipboardData:r=Ne.clipboard(t);break;case Pe.ActionInput:r=Ne.input(t);break;case Pe.ActionChinese:r=Ne.chinese(t);break;case Pe.ActionRequestCloudDeviceInfo:r=Ne.requestCloudDeviceInfo();break;case Pe.ActionClarity:r=Ne.clarity(t);break;case Pe.ActionWheel:r=Ne.wheel(t);break;case Pe.ActionGesture:r=Ne.gesture(t);break;case Pe.ActionCommand:r=Ne.action(t);break;case Pe.ActionCommandEvent:r=Ne.switchAudio(t);break;case Pe.ActionTrack:r=Ne.changeSender(t)}if(r){const e=JSON.stringify(r),t=(new TextEncoder).encode(e).buffer;null==(n=this.dataChannel)||n.send(t),r=null}}catch(r){this.emit(m.webrtcError,f(p.DATACHANNEL_ERR,r))}}closeConnection(){this.statsTimer&&(clearInterval(this.statsTimer),this.statsTimer=void 0),this.peerConnection&&("function"==typeof this.peerConnection.getSenders&&this.peerConnection.getSenders&&this.peerConnection.getSenders().forEach((e=>{var t,n;e.track&&(null==(n=(t=e.track).stop)||n.call(t))})),"function"==typeof this.peerConnection.getReceivers&&this.peerConnection.getReceivers&&this.peerConnection.getReceivers().forEach((e=>{var t,n;e.track&&(null==(n=(t=e.track).stop)||n.call(t))})),this.peerConnection.getTransceivers&&this.peerConnection.getTransceivers().forEach((e=>{var t;null==(t=e.stop)||t.call(e)})),this.removeAllListeners(),this.dataChannel=null,this.peerConnection.onicecandidate=null,this.peerConnection.ontrack=null,this.peerConnection.ondatachannel=null,this.peerConnection.onconnectionstatechange=null,this.peerConnection.oniceconnectionstatechange=null,this.peerConnection.onsignalingstatechange=null,this.peerConnection.onnegotiationneeded=null,this.peerConnection.onicegatheringstatechange=null,this.peerConnection.close(),this.peerConnection=null)}async readyCapture(){this.resetPeerConnection();try{this.localStream=await navigator.mediaDevices.getUserMedia({video:{width:640,height:480},audio:!0});const e=await this.getRotatedStream(this.localStream);e.getTracks().forEach((t=>{t.contentHint="detail",this.peerConnection.addTrack(t,e)})),this.peerConnection.getSenders().forEach((e=>this.setVideoParams(e))),we(this.peerConnection,(e=>{this.emit(m.sendOffer,e)}),(e=>this.emit(m.webrtcError,e)))}catch(e){throw new Error(`mediaDevices error: ${e}`)}}async getRotatedStream(e){const t=document.createElement("canvas"),n=t.getContext("2d"),r=e.getVideoTracks()[0],{width:i,height:o}=await new Promise((e=>{const t=document.createElement("video");t.srcObject=new MediaStream([r]),t.onloadedmetadata=()=>e({width:t.videoWidth,height:t.videoHeight})}));t.width=640,t.height=480;const s=document.createElement("video");s.srcObject=e,await s.play();const a=()=>{n.clearRect(0,0,t.width,t.height),n.save(),n.translate(t.width/2,t.height/2),n.rotate(Math.PI/2),n.drawImage(s,-i/2,-o/2,i,o),n.restore(),requestAnimationFrame(a)};return a(),t.captureStream()}async setVideoParams(e){const t=e.getParameters();t.degradationPreference="maintain-resolution",t.encodings.forEach((e=>{e.maxBitrate=1e6,e.priority="high",e.scaleResolutionDownBy=1})),await e.setParameters(t)}stopPush(){var e;this.isPushingStream&&(this.isPushingStream=!1,null==(e=this.localStream)||e.getTracks().forEach((e=>{var t;null==(t=this.peerConnection)||t.getSenders().forEach((e=>{var t;null==(t=this.peerConnection)||t.removeTrack(e)})),e.stop()})),this.localStream=null,we(this.peerConnection,(e=>{this.emit(m.sendOffer,e)}),(e=>this.emit(m.webrtcError,e))))}resetPeerConnection(){this.peerConnection||(this.peerConnection=(e=>{const t=[{urls:e.stunServerUri},{urls:e.turnServerUri,username:e.turnServerUserName,credential:e.turnServerPassword}];return new RTCPeerConnection({iceServers:t,iceTransportPolicy:"relay",bundlePolicy:"max-bundle"})})(this.config),((e,t,n,r,i)=>{let o=null;e.onicecandidate=e=>{if(!e.candidate)return;const n={sdp:e.candidate.candidate,sdpMid:e.candidate.sdpMid,sdpMLineIndex:e.candidate.sdpMLineIndex};t(JSON.stringify(n))},e.onconnectionstatechange=()=>{const t=e.iceConnectionState;t&&("failed"!==t&&"disconnected"!==t&&"closed"!==t||null==i||i(f(p.ICE_STATE,"failed")),null==r||r(t))},e.onicegatheringstatechange=()=>{"new"===e.iceConnectionState?o=setTimeout((()=>{null==i||i(f(p.ICE_STATE,"请检查相关配置")),o=null}),6e3):"checking"===e.iceConnectionState&&o&&clearTimeout(o)},e.ontrack=e=>{const t=e.track;t.contentHint="motion",null==n||n(t)}})(this.peerConnection,(e=>{this.emit(m.sendICEMessage,e)}),(e=>{this.emit(m.streamTrack,e)}),(e=>{this.emit(m.iceConnectionState,e)}),(e=>this.emit(m.webrtcError,e))),this.checkStats(),this.configDataChannel())}configDataChannel(){this.peerConnection.ondatachannel=e=>{this.dataChannel=e.channel,this.dataChannel.onmessage=e=>this.handleDataChannelMessage(e),this.dataChannel.onerror=e=>this.emit(m.webrtcError,f(p.DATACHANNEL_ERR,e)),this.sendChannelData(Pe.ActionRequestCloudDeviceInfo,"")}}handleDataChannelMessage(e){const t=JSON.parse(e.data);if(t.type===Pe.ActionCommandEvent){const{action:e,value:n}=JSON.parse(t.data);"ACTION_CONTROL_VIDEO"===e&&("ENABLE"===n?this.startPush():this.stopPush())}else if(t.type===Pe.ActionUpdateCloudStatus){const{rotation:e,screenWidth:n,screenHeight:r,gestureMode:i,level:o,isClarity:s}=JSON.parse(t.data),a={direction:[Ie.ROTATION_0,Ie.ROTATION_180].includes(e)?De.Vertical:De.Horizontal,screenWidth:n,screenHeight:r,gestureMode:i,clarityLevel:o,isClarity:s};this.emit(m.cloudStatusChanged,a)}else t.type===Pe.CloudClipData&&this.emit(m.cloudClipData,t.data)}checkStats(){this.statsTimer=setInterval((()=>this.processStats()),1e3)}processStats(){this.peerConnection&&this.peerConnection.getStats(null).then((e=>{let t=0,n=0,r=0,i=0,o=0,s=0,a=0,c=0,d=0,l="";const h=Date.now();e.forEach((h=>{if("inbound-rtp"===h.type&&(s+=h.bytesReceived||0,i+=h.packetsLost||0,o+=h.packetsReceived||0,"video"===h.kind&&(t=h.framesPerSecond||0,n=h.totalDecodeTime||0,a=h.framesDecoded||0,d=h.framesReceived||0,c=(h.pliCount||0)+(h.firCount||0))),"candidate-pair"===h.type&&"succeeded"===h.state){r=void 0!==h.currentRoundTripTime?h.currentRoundTripTime:h.responsesReceived>0?h.totalRoundTripTime/h.responsesReceived:0;const t=e.get(h.localCandidateId),n=e.get(h.remoteCandidateId);t&&n&&(l=this.getConnectionType(t,n))}})),r=Math.floor(1e3*(r||0));const p=h-this.lastReportTime,u=s-this.lastBytesReceived,f=(p>0?1e3*u/p:0)/1024,C=f/1024;this.lastBytesReceived=s,this.lastReportTime=h;let g=0;const y=i-this.lastPacketsLost,v=o-this.lastPacketsReceived;y>0&&v>0&&(g=y/(y+v),this.maxLostRate=Math.max(this.maxLostRate||0,g),this.lostPacketCount++),this.lastPacketsLost=i,this.lastPacketsReceived=o;const T=void 0!==t?t:a-this.lastSecondDecodedCount;this.lastSecondDecodedCount=a;const S=f<1024?`${Math.floor(f)} KB/s`:`${Math.floor(C)} MB/s`,R=a>0?Math.floor(1e4*n/a):0,b={connectionType:l,framesPerSecond:T,currentRoundTripTime:r,lostRate:Math.floor(100*g),
|
|
71
|
+
*/static clarity(e){return new Ne("ActionClarity",this.formatData(e))}static switchAudio(e){return new Ne("ActionCommandEvent",this.formatData(e))}static changeSender(e){return new Ne("ActionTrack",this.formatData(e))}}class je extends d{constructor(e){super(),r(this,"config"),r(this,"peerConnection",null),r(this,"localStream",null),r(this,"isPushingStream",!1),r(this,"dataChannel",null),r(this,"statsTimer"),r(this,"lastReportTime",0),r(this,"lastBytesReceived",0),r(this,"lastPacketsLost",0),r(this,"lastPacketsReceived",0),r(this,"lostPacketCount",0),r(this,"maxLostRate",0),r(this,"lastSecondDecodedCount",0),this.config=e}async startPush(){if(!this.isPushingStream){this.isPushingStream=!0;try{await this.readyCapture()}catch(e){this.emit(m.webrtcError,f(p.CAMERA,e))}}}handleOffer(e){this.resetPeerConnection(),((e,t,n,r)=>{const i=new RTCSessionDescription({type:"offer",sdp:t});e.setRemoteDescription(i).then((()=>Re(e,n,r))).catch((e=>null==r?void 0:r(f(p.HANDLE_OFFER,e))))})(this.peerConnection,e,(e=>{this.emit(m.sendAnswer,e)}),(e=>this.emit(m.webrtcError,e)))}handleAnswer(e){((e,t,n)=>{const r=new RTCSessionDescription({type:"answer",sdp:t});e.setRemoteDescription(r).catch((e=>null==n?void 0:n(f(p.REMOTE_DES,e))))})(this.peerConnection,e??"",(e=>this.emit(m.webrtcError,e)))}handleIceCandidate(e){((e,t,n)=>{e.addIceCandidate(t).catch((e=>null==n?void 0:n(f(p.HANDLE_ICE,e))))})(this.peerConnection,e,(e=>this.emit(m.webrtcError,e)))}sendChannelData(e,t){var n;try{let r=null;switch(e){case Pe.ClickData:r=Ne.click(t);break;case Pe.ClipboardData:r=Ne.clipboard(t);break;case Pe.ActionInput:r=Ne.input(t,this.config.myId);break;case Pe.ActionChinese:r=Ne.chinese(t);break;case Pe.ActionRequestCloudDeviceInfo:r=Ne.requestCloudDeviceInfo();break;case Pe.ActionClarity:r=Ne.clarity(t);break;case Pe.ActionWheel:r=Ne.wheel(t);break;case Pe.ActionGesture:r=Ne.gesture(t);break;case Pe.ActionCommand:r=Ne.action(t);break;case Pe.ActionCommandEvent:r=Ne.switchAudio(t);break;case Pe.ActionTrack:r=Ne.changeSender(t)}if(r){const e=JSON.stringify(r),t=(new TextEncoder).encode(e).buffer;null==(n=this.dataChannel)||n.send(t),r=null}}catch(r){this.emit(m.webrtcError,f(p.DATACHANNEL_ERR,r))}}closeConnection(){this.statsTimer&&(clearInterval(this.statsTimer),this.statsTimer=void 0),this.peerConnection&&("function"==typeof this.peerConnection.getSenders&&this.peerConnection.getSenders&&this.peerConnection.getSenders().forEach((e=>{var t,n;e.track&&(null==(n=(t=e.track).stop)||n.call(t))})),"function"==typeof this.peerConnection.getReceivers&&this.peerConnection.getReceivers&&this.peerConnection.getReceivers().forEach((e=>{var t,n;e.track&&(null==(n=(t=e.track).stop)||n.call(t))})),this.peerConnection.getTransceivers&&this.peerConnection.getTransceivers().forEach((e=>{var t;null==(t=e.stop)||t.call(e)})),this.removeAllListeners(),this.dataChannel=null,this.peerConnection.onicecandidate=null,this.peerConnection.ontrack=null,this.peerConnection.ondatachannel=null,this.peerConnection.onconnectionstatechange=null,this.peerConnection.oniceconnectionstatechange=null,this.peerConnection.onsignalingstatechange=null,this.peerConnection.onnegotiationneeded=null,this.peerConnection.onicegatheringstatechange=null,this.peerConnection.close(),this.peerConnection=null)}async readyCapture(){this.resetPeerConnection();try{this.localStream=await navigator.mediaDevices.getUserMedia({video:{width:640,height:480},audio:!0});const e=await this.getRotatedStream(this.localStream);e.getTracks().forEach((t=>{t.contentHint="detail",this.peerConnection.addTrack(t,e)})),this.peerConnection.getSenders().forEach((e=>this.setVideoParams(e))),we(this.peerConnection,(e=>{this.emit(m.sendOffer,e)}),(e=>this.emit(m.webrtcError,e)))}catch(e){throw new Error(`mediaDevices error: ${e}`)}}async getRotatedStream(e){const t=document.createElement("canvas"),n=t.getContext("2d"),r=e.getVideoTracks()[0],{width:i,height:o}=await new Promise((e=>{const t=document.createElement("video");t.srcObject=new MediaStream([r]),t.onloadedmetadata=()=>e({width:t.videoWidth,height:t.videoHeight})}));t.width=640,t.height=480;const s=document.createElement("video");s.srcObject=e,await s.play();const a=()=>{n.clearRect(0,0,t.width,t.height),n.save(),n.translate(t.width/2,t.height/2),n.rotate(Math.PI/2),n.drawImage(s,-i/2,-o/2,i,o),n.restore(),requestAnimationFrame(a)};return a(),t.captureStream()}async setVideoParams(e){const t=e.getParameters();t.degradationPreference="maintain-resolution",t.encodings.forEach((e=>{e.maxBitrate=1e6,e.priority="high",e.scaleResolutionDownBy=1})),await e.setParameters(t)}stopPush(){var e;this.isPushingStream&&(this.isPushingStream=!1,null==(e=this.localStream)||e.getTracks().forEach((e=>{var t;null==(t=this.peerConnection)||t.getSenders().forEach((e=>{var t;null==(t=this.peerConnection)||t.removeTrack(e)})),e.stop()})),this.localStream=null,we(this.peerConnection,(e=>{this.emit(m.sendOffer,e)}),(e=>this.emit(m.webrtcError,e))))}resetPeerConnection(){this.peerConnection||(this.peerConnection=(e=>{const t=[{urls:e.stunServerUri},{urls:e.turnServerUri,username:e.turnServerUserName,credential:e.turnServerPassword}];return new RTCPeerConnection({iceServers:t,iceTransportPolicy:"relay",bundlePolicy:"max-bundle"})})(this.config),((e,t,n,r,i)=>{let o=null;e.onicecandidate=e=>{if(!e.candidate)return;const n={sdp:e.candidate.candidate,sdpMid:e.candidate.sdpMid,sdpMLineIndex:e.candidate.sdpMLineIndex};t(JSON.stringify(n))},e.onconnectionstatechange=()=>{const t=e.iceConnectionState;t&&("failed"!==t&&"disconnected"!==t&&"closed"!==t||null==i||i(f(p.ICE_STATE,"failed")),null==r||r(t))},e.onicegatheringstatechange=()=>{"new"===e.iceConnectionState?o=setTimeout((()=>{null==i||i(f(p.ICE_STATE,"请检查相关配置")),o=null}),6e3):"checking"===e.iceConnectionState&&o&&clearTimeout(o)},e.ontrack=e=>{const t=e.track;t.contentHint="motion",null==n||n(t)}})(this.peerConnection,(e=>{this.emit(m.sendICEMessage,e)}),(e=>{this.emit(m.streamTrack,e)}),(e=>{this.emit(m.iceConnectionState,e)}),(e=>this.emit(m.webrtcError,e))),this.checkStats(),this.configDataChannel())}configDataChannel(){this.peerConnection.ondatachannel=e=>{this.dataChannel=e.channel,this.dataChannel.onmessage=e=>this.handleDataChannelMessage(e),this.dataChannel.onerror=e=>this.emit(m.webrtcError,f(p.DATACHANNEL_ERR,e)),this.sendChannelData(Pe.ActionRequestCloudDeviceInfo,"")}}handleDataChannelMessage(e){const t=JSON.parse(e.data);if(t.type===Pe.ActionCommandEvent){const{action:e,value:n}=JSON.parse(t.data);"ACTION_CONTROL_VIDEO"===e&&("ENABLE"===n?this.startPush():this.stopPush())}else if(t.type===Pe.ActionUpdateCloudStatus){const{rotation:e,screenWidth:n,screenHeight:r,gestureMode:i,level:o,isClarity:s}=JSON.parse(t.data),a={direction:[Ie.ROTATION_0,Ie.ROTATION_180].includes(e)?De.Vertical:De.Horizontal,screenWidth:n,screenHeight:r,gestureMode:i,clarityLevel:o,isClarity:s};this.emit(m.cloudStatusChanged,a)}else t.type===Pe.CloudClipData&&this.emit(m.cloudClipData,t.data)}checkStats(){this.statsTimer=setInterval((()=>this.processStats()),1e3)}processStats(){this.peerConnection&&this.peerConnection.getStats(null).then((e=>{let t=0,n=0,r=0,i=0,o=0,s=0,a=0,c=0,d=0,l="";const h=Date.now();e.forEach((h=>{if("inbound-rtp"===h.type&&(s+=h.bytesReceived||0,i+=h.packetsLost||0,o+=h.packetsReceived||0,"video"===h.kind&&(t=h.framesPerSecond||0,n=h.totalDecodeTime||0,a=h.framesDecoded||0,d=h.framesReceived||0,c=(h.pliCount||0)+(h.firCount||0))),"candidate-pair"===h.type&&"succeeded"===h.state){r=void 0!==h.currentRoundTripTime?h.currentRoundTripTime:h.responsesReceived>0?h.totalRoundTripTime/h.responsesReceived:0;const t=e.get(h.localCandidateId),n=e.get(h.remoteCandidateId);t&&n&&(l=this.getConnectionType(t,n))}})),r=Math.floor(1e3*(r||0));const p=h-this.lastReportTime,u=s-this.lastBytesReceived,f=(p>0?1e3*u/p:0)/1024,C=f/1024;this.lastBytesReceived=s,this.lastReportTime=h;let g=0;const y=i-this.lastPacketsLost,v=o-this.lastPacketsReceived;y>0&&v>0&&(g=y/(y+v),this.maxLostRate=Math.max(this.maxLostRate||0,g),this.lostPacketCount++),this.lastPacketsLost=i,this.lastPacketsReceived=o;const T=void 0!==t?t:a-this.lastSecondDecodedCount;this.lastSecondDecodedCount=a;const S=f<1024?`${Math.floor(f)} KB/s`:`${Math.floor(C)} MB/s`,R=a>0?Math.floor(1e4*n/a):0,b={connectionType:l,framesPerSecond:T,currentRoundTripTime:r,lostRate:Math.floor(100*g),
|
|
72
72
|
// percent
|
|
73
|
-
bitrate:S,pliCount:c,averageDecodeTime:R,framesDecoded:a,framesReceived:d};this.emit(m.statisticInfo,b)})).catch((e=>{this.emit(m.webrtcError,f(p.STREAM_STATE,e))}))}getConnectionType(e,t){return"host"===e.candidateType&&"host"===t.candidateType?"直连":"relay"===e.candidateType||"relay"===t.candidateType?"中继":"NAT"}}const Ue=async(e,t=600)=>{const n=e.map((e=>({urls:e,username:"yangyj",credential:"hb@2025@168"}))).map((e=>((e,t=600)=>new Promise((n=>{const r=performance.now();let i=!1;const o=new RTCPeerConnection({iceServers:[e],iceTransportPolicy:"relay"});o.createDataChannel("test"),o.createOffer().then((e=>o.setLocalDescription(e))).catch((()=>{i||(i=!0,o.close(),n({...e,rtt:1/0}))})),o.onicecandidate=t=>{if(!i){if(t.candidate&&t.candidate.candidate.includes("relay")){const t=Math.trunc(performance.now()-r);i=!0,o.close(),n({...e,rtt:t})}null===t.candidate&&(i=!0,o.close(),n({...e,rtt:1/0}))}},setTimeout((()=>{i||(i=!0,o.close(),n({...e,rtt:1/0}))}),t)})))(e,t).then((e=>e)).catch((t=>({...e,rtt:1/0}))))),r=await Promise.all(n),i=r.filter((e=>e.rtt!==1/0));if(0===i.length)throw new Error("All TURN servers are unreachable or slow (RTT = Infinity).");const o=i.sort(((e,t)=>e.rtt-t.rtt))[0];return{best:o?Fe(o):void 0,all:r.map(Fe)}},Fe=e=>({urls:e.urls,rtt:e.rtt});class
|
|
73
|
+
bitrate:S,pliCount:c,averageDecodeTime:R,framesDecoded:a,framesReceived:d};this.emit(m.statisticInfo,b)})).catch((e=>{this.emit(m.webrtcError,f(p.STREAM_STATE,e))}))}getConnectionType(e,t){return"host"===e.candidateType&&"host"===t.candidateType?"直连":"relay"===e.candidateType||"relay"===t.candidateType?"中继":"NAT"}}const Ue=async(e,t=600)=>{const n=e.map((e=>({urls:e,username:"yangyj",credential:"hb@2025@168"}))).map((e=>((e,t=600)=>new Promise((n=>{const r=performance.now();let i=!1;const o=new RTCPeerConnection({iceServers:[e],iceTransportPolicy:"relay"});o.createDataChannel("test"),o.createOffer().then((e=>o.setLocalDescription(e))).catch((()=>{i||(i=!0,o.close(),n({...e,rtt:1/0}))})),o.onicecandidate=t=>{if(!i){if(t.candidate&&t.candidate.candidate.includes("relay")){const t=Math.trunc(performance.now()-r);i=!0,o.close(),n({...e,rtt:t})}null===t.candidate&&(i=!0,o.close(),n({...e,rtt:1/0}))}},setTimeout((()=>{i||(i=!0,o.close(),n({...e,rtt:1/0}))}),t)})))(e,t).then((e=>e)).catch((t=>({...e,rtt:1/0}))))),r=await Promise.all(n),i=r.filter((e=>e.rtt!==1/0));if(0===i.length)throw new Error("All TURN servers are unreachable or slow (RTT = Infinity).");const o=i.sort(((e,t)=>e.rtt-t.rtt))[0];return{best:o?Fe(o):void 0,all:r.map(Fe)}},Fe=e=>({urls:e.urls,rtt:e.rtt}),Ke=e=>!(e.hostTurn&&0!==e.hostTurn.length||e.spareTurn&&0!==e.spareTurn.length);class ze extends d{constructor(e){super(),r(this,"config",null),r(this,"signalingClient",null),r(this,"webRTCClient",null),r(this,"options"),r(this,"isConnected",!1),r(this,"isConnecting",!1),r(this,"connectCount",0),r(this,"MAX_COUNT",1),
|
|
74
74
|
/**
|
|
75
75
|
* 处理 signal 消息,根据不同消息类型分发到 webRTCClient 或直接触发 SDK 事件
|
|
76
76
|
* @param message 信令消息
|
|
@@ -81,11 +81,11 @@ r(this,"sendOffer",(e=>{var t;if(this.config&&e&&e.length>0){const n={type:h.Off
|
|
|
81
81
|
/** 发送 Answer 信令 */
|
|
82
82
|
r(this,"sendAnswer",(e=>{var t;if(this.config&&e&&e.length>0){const n={type:h.Answer,identity:h.Identity,roomId:this.config.roomId,senderId:this.config.myId,targetId:this.config.targetId,sdp:e};null==(t=this.signalingClient)||t.sendMessage(JSON.stringify(n))}})),
|
|
83
83
|
/** 发送 ICE 候选信息 */
|
|
84
|
-
r(this,"sendICEMessage",(e=>{var t;if(this.config&&e&&e.length>0){const n={type:h.IceCandidates,identity:h.Identity,roomId:this.config.roomId,senderId:this.config.myId,targetId:this.config.targetId,ice:e};null==(t=this.signalingClient)||t.sendMessage(JSON.stringify(n))}})),this.options=e,this.options.maxRecount&&(this.MAX_COUNT=this.options.maxRecount)}async initConfig(){var e,t
|
|
85
|
-
/** 开始连接 signal 服务 */startConnection(){this.prepareConnection().catch((e=>{this.emit(m.webrtcError,e)}))}async prepareConnection(){var e;if(!this.isConnecting){this.isConnecting=!0;try{await this.initConfig()
|
|
84
|
+
r(this,"sendICEMessage",(e=>{var t;if(this.config&&e&&e.length>0){const n={type:h.IceCandidates,identity:h.Identity,roomId:this.config.roomId,senderId:this.config.myId,targetId:this.config.targetId,ice:e};null==(t=this.signalingClient)||t.sendMessage(JSON.stringify(n))}})),this.options=e,this.options.maxRecount&&(this.MAX_COUNT=this.options.maxRecount)}async initConfig(){var e,t;if(!this.options||0===Object.keys(this.options).length)throw f(p.OPTION_ERR,"option null");if(Ke(this.options))throw f(p.OPTION_ERR,"中继配置为空");const n=async(e,t)=>{if(0===e.length)return!1;try{const n=await(async e=>{var t,n;try{const r=await Ue(e);return{url:null==(t=r.best)?void 0:t.urls,rtt:null==(n=r.best)?void 0:n.rtt}}catch(r){return{error:r.message||"未知错误"}}})(e);return!(!n.url||"number"!=typeof n.rtt)&&(!(t&&n.rtt>140)&&(this.options.turnServerUri=n.url,this.options.turnKey=[n.url],!0))}catch(n){return!1}},r=!!(null==(e=this.options.hostTurn)?void 0:e.length)&&await n(this.options.hostTurn,!0);if(!r&&(null==(t=this.options.spareTurn)?void 0:t.length)){const e=(this.options.hostTurn??[]).concat(this.options.spareTurn??[]);if(!(await n(e,!1))){if(!this.options.turnServerUri)throw f(p.OPTION_ERR,"暂无可用TURN服务器");this.options.turnKey=[this.options.turnServerUri]}}else if(!r){if(!(await n(this.options.hostTurn??[],!1))){if(!this.options.turnServerUri)throw f(p.OPTION_ERR,"暂无可用TURN服务器");this.options.turnKey=[this.options.turnServerUri]}}}
|
|
85
|
+
/** 开始连接 signal 服务 */startConnection(){this.prepareConnection().catch((e=>{this.emit(m.webrtcError,e)}))}async prepareConnection(){var e;if(!this.isConnecting){this.isConnecting=!0;try{if(Ke(this.options)){if(!this.options.turnServerUri)return void this.emit(m.webrtcError,f(p.OPTION_ERR,"暂无可用TURN服务器"))}else await this.initConfig();this.initConnectConfig(),null==(e=this.signalingClient)||e.start()}catch(t){throw this.isConnected=!1,t}finally{this.isConnecting=!1}}}async reconnect(){this.resetConfig();try{await this.prepareConnection()}catch(e){this.emit(m.webrtcError,e)}}initConnectConfig(){this.config=new Se(this.options),this.signalingClient=new C(this.config),this.webRTCClient=new je(this.config),this.signalingClient.on(m.signalMessage,(e=>this.handleSignaling(e))),this.signalingClient.on(m.webrtcError,(e=>{this.emit(m.webrtcError,e)})),this.webRTCClient.on(m.sendOffer,(e=>this.sendOffer(e))),this.webRTCClient.on(m.sendAnswer,(e=>this.sendAnswer(e))),this.webRTCClient.on(m.sendICEMessage,(e=>this.sendICEMessage(e))),this.webRTCClient.on(m.streamTrack,(e=>{this.emit(m.streamTrack,e)})),this.webRTCClient.on(m.iceConnectionState,(e=>{this.emit(m.iceConnectionState,e),"connected"===e&&(this.isConnected=!0)})),this.webRTCClient.on(m.cloudStatusChanged,(e=>this.emit(m.cloudStatusChanged,e))),this.webRTCClient.on(m.cloudClipData,(e=>this.emit(m.cloudClipData,e))),this.webRTCClient.on(m.webrtcError,(e=>{e.code===p.ICE_STATE&&this.connectCount<this.MAX_COUNT&&this.isConnected?(this.connectCount++,this.emit(m.reconnect),this.reconnect()):this.emit(m.webrtcError,e)})),this.webRTCClient.on(m.statisticInfo,(e=>{this.emit(m.statisticInfo,e)}))}switchControl(e){var t;null==(t=this.signalingClient)||t.sendSwitchControlMessage(e)}resetConfig(){var e,t;this.sendSignOut(),null==(e=this.signalingClient)||e.close(),null==(t=this.webRTCClient)||t.closeConnection(),this.signalingClient=null,this.webRTCClient=null,this.config=null,this.isConnected=!1,this.isConnecting=!1}
|
|
86
86
|
/** 停止连接,并发送退出信令 */stop(){var e,t;this.sendSignOut(),null==(e=this.signalingClient)||e.close(),null==(t=this.webRTCClient)||t.closeConnection(),this.removeAllListeners(),this.signalingClient=null,this.webRTCClient=null,this.config=null,this.connectCount=0,this.isConnected=!1,this.isConnecting=!1}
|
|
87
87
|
/** 开始推流:触发媒体采集与轨道添加 */async startPush(){}
|
|
88
|
-
/** 发送信道数据 */sendChannelData(e,t){var n;null==(n=this.webRTCClient)||n.sendChannelData(e,t)}sendControlEvent(e){var t;const n=new Le(e,0);null==(t=this.webRTCClient)||t.sendChannelData(Pe.ActionInput,n)}sendRequestScreenshot(e){var t;null==(t=this.signalingClient)||t.sendRequestScreenshot(e)}sendPong(){var e;const t={type:h.Pong},n=JSON.stringify(t);null==(e=this.signalingClient)||e.sendMessage(n)}sendSignOut(){var e;if(!this.config)return;const t={type:h.SignOut,identity:h.Identity,roomId:this.config.roomId,myId:this.config.myId};null==(e=this.signalingClient)||e.sendMessage(JSON.stringify(t))}}const
|
|
88
|
+
/** 发送信道数据 */sendChannelData(e,t){var n;null==(n=this.webRTCClient)||n.sendChannelData(e,t)}sendControlEvent(e){var t;const n=new Le(e,0);null==(t=this.webRTCClient)||t.sendChannelData(Pe.ActionInput,n)}sendRequestScreenshot(e){var t;null==(t=this.signalingClient)||t.sendRequestScreenshot(e)}sendPong(){var e;const t={type:h.Pong},n=JSON.stringify(t);null==(e=this.signalingClient)||e.sendMessage(n)}sendSignOut(){var e;if(!this.config)return;const t={type:h.SignOut,identity:h.Identity,roomId:this.config.roomId,myId:this.config.myId};null==(e=this.signalingClient)||e.sendMessage(JSON.stringify(t))}}const Ve={
|
|
89
89
|
// 数字键
|
|
90
90
|
Digit1:8,Digit2:9,Digit3:10,Digit4:11,Digit5:12,Digit6:13,Digit7:14,Digit8:15,Digit9:16,Digit0:7,
|
|
91
91
|
// 符号键(主键盘区)
|
|
@@ -103,9 +103,9 @@ F1:131,F2:132,F3:133,F4:134,F5:135,F6:136,F7:137,F8:138,F9:139,F10:140,F11:141,F
|
|
|
103
103
|
// 数字小键盘
|
|
104
104
|
Numpad0:7,Numpad1:8,Numpad2:9,Numpad3:10,Numpad4:11,Numpad5:12,Numpad6:13,Numpad7:14,Numpad8:15,Numpad9:16,NumpadAdd:157,NumpadSubtract:156,NumpadMultiply:155,NumpadDivide:154,NumpadDecimal:158,NumpadEnter:66,
|
|
105
105
|
// 其他特殊键
|
|
106
|
-
Insert:124,Delete:112,Home:122,End:123,PageUp:92,PageDown:93,ScrollLock:116,Pause:121,ContextMenu:117},
|
|
106
|
+
Insert:124,Delete:112,Home:122,End:123,PageUp:92,PageDown:93,ScrollLock:116,Pause:121,ContextMenu:117},We=e=>{const t=Ve[e.code]??-1,n=(e=>{let t=0;return e.shiftKey&&(t|=1),e.altKey&&(t|=2),e.ctrlKey&&(t|=4096),e.metaKey&&(t|=65536),t})(e);return{keyCode:t,meta:n}};function Be(e,t,n,r,i,o,s,a){let c,d;-90===i?(d=t,c=n*e/r):(d=e,c=n*t/r);const l=(d-c)/2,h=d/2,p=l,u=d-l,f=h-l;if(-90===i){const e=He(l,h,p,u,f,a);return-90===o?[s,e]:[t-e,s]}{const n=He(l,h,p,u,f,s);if(-90===o){const[r,i]=function(e,t,n,r){const i=t/2,o=e/2,s=n-o,a=r-i;return[a+i,-s+o]}(e,t,n,a);return[r,i]}return[n,a]}}function Ge(e,t,n,r,i,o,s,a){let c,d;return 0===i&&0===o?(c=s/e,d=a/t):-90===i&&0===o?(c=s/t,d=a/e):-90===i&&-90===o?(c=s/e*(r/n),d=a/t*(n/r)):(c=s/t*(r/n),d=a/e*(n/r)),[c,d]}function He(e,t,n,r,i,o){return o>=n&&o<=t-.1?(o-e)*t/i:o===t?t:o>=t+.1&&o<=r?t+(o-t)*t/i:o}class Je extends d{constructor(e){super(),r(this,"sdk"),r(this,"config"),this.config=e,this.sdk=new ze(this.config),this.initSdk()}initSdk(){const e=this.config.roomId;this.sdk.removeAllListeners(),this.config.isControl?(this.sdk.on(m.streamTrack,(e=>{const t="video"===e.kind;this.emit(m.streamTrack,t,e)})),this.sdk.on(m.cloudStatusChanged,(e=>{this.emit(m.cloudStatusChanged,e)}))):this.sdk.on(m.screenshot,(t=>this.emit(m.screenshot,e,t))),this.sdk.on(m.iceConnectionState,(t=>this.emit(m.iceConnectionState,e,t))),this.sdk.on(m.webrtcError,(t=>{this.emit(m.webrtcError,e,t),this.stop()}))}
|
|
107
107
|
// 动态更新 isControl 值
|
|
108
|
-
setIsControl(e){this.config.isControl!==e&&(this.config.isControl=e,this.initSdk())}start(){this.sdk.startConnection()}stop(){this.sdk.stop(),this.sdk.removeAllListeners()}switchControl(e){this.sdk.switchControl(e)}sendData(e,t){(this.config.canOperate||this.config.isControl)&&1===this.config.connectStatus&&this.sdk.sendChannelData(e,t)}sendControlData(e){(this.config.canOperate||this.config.isControl)&&1===this.config.connectStatus&&this.sdk.sendControlEvent(e)}requestScreenshot(e){this.sdk.sendRequestScreenshot(e)}}function
|
|
108
|
+
setIsControl(e){this.config.isControl!==e&&(this.config.isControl=e,this.initSdk())}start(){this.sdk.startConnection()}stop(){this.sdk.stop(),this.sdk.removeAllListeners()}switchControl(e){this.sdk.switchControl(e)}sendData(e,t){(this.config.canOperate||this.config.isControl)&&1===this.config.connectStatus&&this.sdk.sendChannelData(e,t)}sendControlData(e){(this.config.canOperate||this.config.isControl)&&1===this.config.connectStatus&&this.sdk.sendControlEvent(e)}requestScreenshot(e){this.sdk.sendRequestScreenshot(e)}}function qe(e,n,r,i){const o=t.ref(!1),s=t.ref({}),a=t.ref({width:0,height:0}),c=t.ref(0),d=t.ref(0),l=t.ref(null),h=t.ref(0),p=t.computed((()=>r.value%180!=0)),u=t.computed((()=>{let e=0,t=0;const n=p.value?a.value.height:a.value.width,r=p.value?a.value.width:a.value.height,i=s.value.width??720,o=s.value.height??1280,l=i/o,h=n/l;return h>r?(t=r,e=t*l):(e=n,t=h),c.value=i/e,d.value=o/t,{width:e,height:t}})),f=(e,t)=>{e.srcObject||(e.srcObject=new MediaStream);const n=e.srcObject;"video"===t.kind?(n.getTracks().forEach((e=>{e.id!==t.id&&(n.removeTrack(e),e.stop())})),n.getVideoTracks().some((e=>e.id===t.id))||n.addTrack(t)):n.addTrack(t),e.playsInline=!0,e.setAttribute("webkit-playsinline","true")},m=()=>{o.value=!1;const e=n.value;e&&e.srcObject&&(e.srcObject.getTracks().forEach((e=>e.stop())),e.srcObject=null,e.cancelVideoFrameCallback(h.value))},C=()=>{if("visible"===document.visibilityState){const e=n.value;e&&e.srcObject&&e.srcObject.getTracks().forEach((e=>{"audio"!==e.kind||e.enabled||(e.enabled=!0)}))}else{const e=n.value;e&&e.srcObject&&e.srcObject.getTracks().forEach((e=>{"audio"===e.kind&&e.enabled&&(e.enabled=!1)}))}};return t.onMounted((()=>{document.addEventListener("visibilitychange",C)})),t.onBeforeUnmount((()=>{document.removeEventListener("visibilitychange",C),l.value&&e.value&&l.value.unobserve(e.value),m()})),{videoSize:u,remoteVideo:s,dimensions:a,widthRadio:c,heightRadio:d,screenStatus:o,initVideoContainer:()=>{e.value&&(l.value=new ResizeObserver((([e])=>{const{width:t,height:n}=e.contentRect;a.value={width:t,height:n}})),l.value.observe(e.value)),(()=>{const e=n.value;if(!e)return;const r=()=>{s.value={width:e.videoWidth,height:e.videoHeight},o.value=!0,i("loadedSuccess")};e.addEventListener("loadedmetadata",r),t.onBeforeUnmount((()=>{e.removeEventListener("loadedmetadata",r)}))})()},startPlay:e=>{const t=n.value;t&&f(t,e)},stopPlay:m}}const Qe=((e,t)=>{const n=e.__vccOpts||e;for(const[r,i]of t)n[r]=i;return n})(t.defineComponent({__name:"index",props:{streamAngle:{default:0},videoAngle:{default:0},cursorType:{default:0},cloudDeviceSize:{default:()=>({width:0,height:0})},disabled:{type:Boolean,default:!0},bgColor:{default:"transparent"}},emits:["channelEvent","groupControlEvent","loadedSuccess"],setup(e,{expose:n,emit:r}){const i=r,o=e,{streamAngle:s,videoAngle:a,cursorType:c,cloudDeviceSize:d,disabled:l,bgColor:h}=t.toRefs(o),p=t.ref(null),u=t.ref(null),f=function(e){return t.computed((()=>{switch(e.value){case 1:return"circle-cursor";case 2:return"triangle-cursor";default:return"default-cursor"}}))}(c),{videoSize:m,dimensions:C,widthRadio:g,initVideoContainer:y,startPlay:v,stopPlay:T}=qe(p,u,a,i),{handleMouseDown:S,handleMouseMove:R,handleMouseEnter:b,handleMouseUp:w,handleMouseLeave:E,handleWheel:P}=function(e){const{remoteVideoElement:n,cloudDeviceSize:r,streamAngle:i,videoAngle:o,widthRadio:s,emit:a}=e,c=t.ref(!1),d=t.ref(0),l=t.ref(new Array(20).fill(0)),h=t.ref(new Array(10).fill(0)),p=(e,t)=>{if(!n.value)return;const s=Math.trunc(e.timeStamp-h.value[0]),c=n.value.getBoundingClientRect();let p=e.clientX-c.left,u=e.clientY-c.top;const f=r.value.width,m=r.value.height,C=Be(c.width,c.height,f,m,o.value,i.value,p,u);if(!C||C.length<2)return;if(p=C[0],u=C[1],t===Oe.ACTION_MOVE){const e=l.value[10]-u,t=l.value[0]-p;if(Math.abs(e)<d.value&&Math.abs(t)<d.value)return}l.value[0]=p,l.value[10]=u;const g=Ge(c.width,c.height,f,m,o.value,i.value,p,u),y=new Me(t,0,g[0],g[1],s);a("channelEvent",Pe.ClickData,y)},u=e=>{c.value&&(c.value=!1,p(e,Oe.ACTION_UP),n.value&&n.value.releasePointerCapture(e.pointerId))};return{isPointerDown:c,handleMouseDown:e=>{c.value=!0,n.value&&n.value.setPointerCapture(e.pointerId),h.value[0]=e.timeStamp,d.value=Math.trunc(6/s.value),p(e,Oe.ACTION_DOWN)},handleMouseMove:e=>{if(!c.value)return;if(p(e,Oe.ACTION_MOVE),!n.value)return;const t=n.value.getBoundingClientRect(),{clientX:r,clientY:i}=e;(r<t.left||r>t.right||i<t.top||i>t.bottom)&&u(e)},handleMouseEnter:e=>{1!==e.buttons||c.value||(c.value=!0,n.value&&n.value.setPointerCapture(e.pointerId),h.value[0]=e.timeStamp,d.value=Math.trunc(6/s.value),p(e,Oe.ACTION_DOWN))},handleMouseUp:u,handleMouseLeave:e=>{c.value&&(p(e,Oe.ACTION_UP),c.value=!1,n.value&&n.value.releasePointerCapture(e.pointerId))},handleWheel:e=>{const t=-1*Math.sign(e.deltaY),n=new xe(t);a("channelEvent",Pe.ActionWheel,n)}}}({remoteVideoElement:u,cloudDeviceSize:d,streamAngle:s,videoAngle:a,widthRadio:g,emit:i}),{startListening:k,stopListening:_}=function(e,n){const r=t.ref(!1),i=e=>{const t=We(e);n("channelEvent",Pe.ActionInput,t)};return{startListening:()=>{e&&(r.value=!0,document.addEventListener("keydown",i))},stopListening:()=>{e&&(r.value=!1,document.removeEventListener("keydown",i))}}}(l,i);!function(e,n){let r=null;t.onMounted((()=>{e.value&&(r=new ResizeObserver((e=>{for(const t of e){const{width:e,height:r}=t.contentRect;n.value={width:e,height:r}}})),r.observe(e.value))})),t.onUnmounted((()=>{null==r||r.disconnect()}))}(p,C);const A=()=>{var e;(null==(e=u.value)?void 0:e.srcObject)&&(u.value.muted=!1)};return t.onMounted((()=>{document.addEventListener("click",A),y()})),t.onBeforeUnmount((()=>{document.removeEventListener("click",A)})),n({startPlay:v,stopPlay:T,remoteVideoElement:u}),(e,n)=>(t.openBlock(),t.createElementBlock("div",{ref_key:"videoContainer",ref:p,class:"flex flex-1 items-center justify-center",style:{width:"100%",height:"100%",position:"relative",overflow:"hidden"}},[t.createElementVNode("div",{ref:"keyboardArea",onPointerenter:n[6]||(n[6]=//@ts-ignore
|
|
109
109
|
(...e)=>t.unref(k)&&t.unref(k)(...e)),onPointerleave:n[7]||(n[7]=//@ts-ignore
|
|
110
110
|
(...e)=>t.unref(_)&&t.unref(_)(...e)),class:"vContainer",style:t.normalizeStyle([{height:t.unref(m).height+"px",width:t.unref(m).width+"px",transform:`rotate(${t.unref(a)}deg)`},{position:"relative",overflow:"hidden"}])},[t.createElementVNode("video",{ref_key:"remoteVideoElement",ref:u,class:t.normalizeClass(["video-control",[t.unref(f),{"no-events":t.unref(l)}]]),onPointerenter:n[0]||(n[0]=//@ts-ignore
|
|
111
111
|
(...e)=>t.unref(b)&&t.unref(b)(...e)),onPointerdown:n[1]||(n[1]=//@ts-ignore
|
|
@@ -120,6 +120,6 @@ setIsControl(e){this.config.isControl!==e&&(this.config.isControl=e,this.initSdk
|
|
|
120
120
|
/**
|
|
121
121
|
* 找到目标config, 设置为主控
|
|
122
122
|
* @param roomId
|
|
123
|
-
*/setAsControl(e){for(const[t,n]of this.configs.entries())if(t===e)return this.configs.set(t,{...n,isControl:!0}),t}stopControl(){this.controllers.forEach((e=>{e.removeAllListeners(),e.stop()})),this.controllers.clear(),this.configs.clear(),this.removeAllListeners()}addController(e){const t=new
|
|
123
|
+
*/setAsControl(e){for(const[t,n]of this.configs.entries())if(t===e)return this.configs.set(t,{...n,isControl:!0}),t}stopControl(){this.controllers.forEach((e=>{e.removeAllListeners(),e.stop()})),this.controllers.clear(),this.configs.clear(),this.removeAllListeners()}addController(e){const t=new Je(e);t.on(m.streamTrack,((e,t)=>{this.emit(m.streamTrack,e,t)})),t.on(m.iceConnectionState,((e,t)=>{this.emit(m.iceConnectionState,e,t)})),t.on(m.cloudStatusChanged,(e=>{this.emit(m.cloudStatusChanged,e)})),t.on(m.webrtcError,((e,t)=>{this.emit(m.webrtcError,e,t)})),t.on(m.screenshot,((e,t)=>{this.emit(m.screenshot,e,t)})),t.start(),this.controllers.set(e.roomId,t)}getConfigs(){return new Map(this.configs)}
|
|
124
124
|
// 🔥 新增:暴露拿单个 controller
|
|
125
|
-
getController(e){return this.controllers.get(e)}refreshController(e){const t=this.configs.get(e);if(!t)throw new Error(`未找到配置项: ${e}`);const n=this.controllers.get(e);n&&(n.removeAllListeners(),n.stop(),this.controllers.delete(e)),this.addController(t)}updateProperty(e,t,n){const r=this.configs.get(e);if(!r)throw new Error(`未找到 ${e}`);r[t]=n}sendData(e,t){this.controllers.forEach((n=>n.sendData(e,t)))}sendControlEvent(e){this.controllers.forEach((t=>t.sendControlData(e)))}},e.InputData=class{constructor(e){r(this,"text"),this.text=e}},e.KeyEventData=Le,e.RemotePlayer=
|
|
125
|
+
getController(e){return this.controllers.get(e)}refreshController(e){const t=this.configs.get(e);if(!t)throw new Error(`未找到配置项: ${e}`);const n=this.controllers.get(e);n&&(n.removeAllListeners(),n.stop(),this.controllers.delete(e)),this.addController(t)}updateProperty(e,t,n){const r=this.configs.get(e);if(!r)throw new Error(`未找到 ${e}`);r[t]=n}sendData(e,t){this.controllers.forEach((n=>n.sendData(e,t)))}sendControlEvent(e){this.controllers.forEach((t=>t.sendControlData(e)))}},e.InputData=class{constructor(e){r(this,"text"),this.text=e}},e.KeyEventData=Le,e.RemotePlayer=Qe,e.TouchData=Me,e.TrackEventData=class{constructor(e){r(this,"visible"),this.visible=e}},e.WebRTCSdk=ze,e.WheelData=xe,e.getKeyEventData=We,e.testMultipleTurnServers=Ue,e.transformCoordinate=Be,e.valueToPercentage=Ge,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}));
|