trtc-sdk-v5 5.11.0-wasm.2 → 5.11.1-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/index.d.ts +34 -13
- package/package.json +6 -4
- package/plugins/cdn-streaming/package.json +1 -1
- package/plugins/cross-room/package.json +1 -1
- package/plugins/custom-encryption/package.json +1 -1
- package/plugins/device-detector/package.json +1 -1
- package/plugins/small-stream-auto-switcher/package.json +1 -1
- package/plugins/small-stream-auto-switcher/small-stream-auto-switcher.esm.js +1 -1
- package/plugins/small-stream-auto-switcher/small-stream-auto-switcher.umd.js +1 -1
- package/plugins/video-decoder/package.json +1 -1
- package/plugins/video-effect/basic-beauty/package.json +1 -1
- package/plugins/video-effect/beauty/package.json +1 -1
- package/plugins/video-effect/video-mixer/package.json +1 -1
- package/plugins/video-effect/virtual-background/package.json +1 -1
- package/plugins/video-effect/watermark/package.json +1 -1
- package/plugins/voice-changer/package.json +1 -1
- package/trtc.esm.js +43 -64
- package/trtc.js +1 -1
- package/assets/av_processing.wasm +0 -0
- package/assets/worker.js +0 -17
package/index.d.ts
CHANGED
|
@@ -12,8 +12,8 @@ import { InitAudioProcessorOptions, UpdateAudioProcessorOptions } from 'trtc-js-
|
|
|
12
12
|
|
|
13
13
|
export { CDNStreamingOptions, DeviceDetectorOptions, VirtualBackgroundOptions, UpdateVirtualBackgroundOptions, WatermarkOptions, BeautyOptions, UpdateBeautyOptions, BasicBeautyOptions, StartCrossRoomOption, UpdateCrossRoomOption, StopCrossRoomOption, SmallStreamAutoSwitcherOptions, VideoMixerOptions };
|
|
14
14
|
type TRTCPlugin = typeof CrossRoom | typeof CDNStreaming | typeof DeviceDetector | typeof VirtualBackground | typeof Watermark | typeof Beauty | typeof BasicBeauty | typeof CustomEncryption | typeof SmallStreamAutoSwitcher | typeof VideoMixer;
|
|
15
|
-
export declare
|
|
16
|
-
|
|
15
|
+
export declare type ExperimentalAPIFunctionMap = {
|
|
16
|
+
'enableAudioFrameEvent': EnableAudioFrameEventOptions;
|
|
17
17
|
}
|
|
18
18
|
export declare type PluginStartOptionsMap = {
|
|
19
19
|
'AudioMixer': AudioMixerOptions;
|
|
@@ -688,6 +688,13 @@ export declare interface VideoFrameConfig {
|
|
|
688
688
|
export declare const enum AutoStartPluginName {
|
|
689
689
|
Debug = 'Debug'
|
|
690
690
|
}
|
|
691
|
+
export declare interface EnableAudioFrameEventOptions {
|
|
692
|
+
enable: boolean
|
|
693
|
+
userId: string
|
|
694
|
+
sampleRate?: number
|
|
695
|
+
channelCount?: number
|
|
696
|
+
port?: MessagePort
|
|
697
|
+
}
|
|
691
698
|
|
|
692
699
|
/**
|
|
693
700
|
* @typedef TRTCStatistics TRTC statistics
|
|
@@ -726,6 +733,14 @@ export declare const enum AutoStartPluginName {
|
|
|
726
733
|
* @property {number} frameRate Video frameRate
|
|
727
734
|
* @property {'big'|'small'|'sub'} videoType Video type: big, small, sub.
|
|
728
735
|
*/
|
|
736
|
+
/** enableAudioFrameEvent Options
|
|
737
|
+
* @typedef EnableAudioFrameEventOptions
|
|
738
|
+
* @property {boolean} enable Whether to enable callback of audio frame
|
|
739
|
+
* @property {string} userId The userId of the monitored user. '' indicates local microphone data, '*' indicates monitoring the audio data of all remote users.
|
|
740
|
+
* @property {number=} [sampleRate=48000] Set the sampling rate of pcm data, the default is 48000. Support 8000, 16000, 32000, 44100, 48000
|
|
741
|
+
* @property {number=} [channelCount=1] Set the number of channels of pcm data, the default is 1. Support 1, 2
|
|
742
|
+
* @property {MessagePort=} port Set the MessagePort for the pcm callback
|
|
743
|
+
*/
|
|
729
744
|
/**
|
|
730
745
|
* **TRTC Event List**<br>
|
|
731
746
|
* <br>
|
|
@@ -1860,7 +1875,7 @@ export declare class TRTC {
|
|
|
1860
1875
|
/**
|
|
1861
1876
|
* Used to control the playback volume of remote audio.<br>
|
|
1862
1877
|
*
|
|
1863
|
-
* -
|
|
1878
|
+
* - Not supported by iOS Safari
|
|
1864
1879
|
* @param {string} userId - Remote user ID。'*' represents all remote users.
|
|
1865
1880
|
* @param {number} volume - Volume, ranging from 0 to 100. The default value is 100.<br>
|
|
1866
1881
|
* Since `v5.1.3+`, the volume can be set higher than 100.
|
|
@@ -2095,18 +2110,24 @@ export declare class TRTC {
|
|
|
2095
2110
|
/**
|
|
2096
2111
|
* call experimental API
|
|
2097
2112
|
*
|
|
2098
|
-
* |
|
|
2099
|
-
* |
|
|
2100
|
-
* | enableAudioFrameEvent |
|
|
2113
|
+
* | APIName | name | param |
|
|
2114
|
+
* | --- | --- | --- |
|
|
2115
|
+
* | 'enableAudioFrameEvent' | Config the pcm data of Audio Frame Event | [EnableAudioFrameEventOptions](https://web.sdk.qcloud.com/trtc/webrtc/v5/doc/en/global.html#EnableAudioFrameEventOptions) |
|
|
2101
2116
|
* @private
|
|
2102
2117
|
* @param {string} name
|
|
2103
|
-
* @param {
|
|
2104
|
-
* @example
|
|
2105
|
-
*
|
|
2106
|
-
* await trtc.callExperimentalAPI('enableAudioFrameEvent', { enable: true, userId: '
|
|
2107
|
-
*
|
|
2108
|
-
|
|
2109
|
-
|
|
2118
|
+
* @param {EnableAudioFrameEventOptions} options
|
|
2119
|
+
* @example
|
|
2120
|
+
* // Call back the pcm data of the remote user 'user_A'. The default pcm data is 48kHZ, mono
|
|
2121
|
+
* await trtc.callExperimentalAPI('enableAudioFrameEvent', { enable: true, userId: 'user_A'})
|
|
2122
|
+
* // Call back all remote pcm data and set the pcm data to 16kHZ, stereo
|
|
2123
|
+
* await trtc.callExperimentalAPI('enableAudioFrameEvent', { enable: true, userId: '*', sampleRate: 16000, channelCount: 2 })
|
|
2124
|
+
* // Set the MessagePort for the local microphone pcm data callback
|
|
2125
|
+
* await trtc.callExperimentalAPI('enableAudioFrameEvent', { enable: true, userId: '', port })
|
|
2126
|
+
* // Cancel callback of local microphone pcm data
|
|
2127
|
+
* await trtc.callExperimentalAPI('enableAudioFrameEvent', { enable: false, userId: '' })
|
|
2128
|
+
*/
|
|
2129
|
+
callExperimentalAPI<T extends keyof ExperimentalAPIFunctionMap, O extends ExperimentalAPIFunctionMap[T]>(name: O extends undefined ? never : T, options: O): Promise<void>;
|
|
2130
|
+
callExperimentalAPI<T extends keyof ExperimentalAPIFunctionMap, O extends ExperimentalAPIFunctionMap[T]>(name: O extends undefined ? T : never): Promise<void>;
|
|
2110
2131
|
static EVENT: typeof TRTCEvent;
|
|
2111
2132
|
static ERROR_CODE: {
|
|
2112
2133
|
INVALID_PARAMETER: number;
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "trtc-sdk-v5",
|
|
3
|
-
"version": "5.11.
|
|
3
|
+
"version": "5.11.1-beta.1",
|
|
4
4
|
"description": "Tencent Cloud RTC SDK for Web",
|
|
5
|
-
"
|
|
6
|
-
"type": "module",
|
|
5
|
+
"main": "trtc.js",
|
|
7
6
|
"types": "index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"webrtc-adapter": "^8.2.3"
|
|
9
|
+
},
|
|
8
10
|
"keywords": [
|
|
9
11
|
"webrtc",
|
|
10
12
|
"TRTC",
|
|
@@ -27,4 +29,4 @@
|
|
|
27
29
|
},
|
|
28
30
|
"author": "Tencent Cloud Client R&D Center",
|
|
29
31
|
"license": "ISC"
|
|
30
|
-
}
|
|
32
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var __defProp=Object.defineProperty,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(t,e,s)=>e in t?__defProp(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s,__spreadValues=(t,e)=>{for(var s in e||(e={}))__hasOwnProp.call(e,s)&&__defNormalProp(t,s,e[s]);if(__getOwnPropSymbols)for(var s of __getOwnPropSymbols(e))__propIsEnum.call(e,s)&&__defNormalProp(t,s,e[s]);return t},__publicField=(t,e,s)=>__defNormalProp(t,"symbol"!=typeof e?e+"":e,s),
|
|
1
|
+
var __defProp=Object.defineProperty,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(t,e,s)=>e in t?__defProp(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s,__spreadValues=(t,e)=>{for(var s in e||(e={}))__hasOwnProp.call(e,s)&&__defNormalProp(t,s,e[s]);if(__getOwnPropSymbols)for(var s of __getOwnPropSymbols(e))__propIsEnum.call(e,s)&&__defNormalProp(t,s,e[s]);return t},__publicField=(t,e,s)=>__defNormalProp(t,"symbol"!=typeof e?e+"":e,s),DEFAULT_OPTIONS={rttPoorLimit:200,lossPoorLimit:20,rttGoodLimit:100,lossGoodLimit:10,sleepTime:3e4,poorDuration:1e4,goodDuration:2e4,maxAutoSwitchToSmallCount:2},ssasSeq=0,_SmallStreamAutoSwitcher=class t{constructor(t){this.core=t,__publicField(this,"log"),__publicField(this,"options",DEFAULT_OPTIONS),__publicField(this,"rttQueue",[]),__publicField(this,"lossQueue",[]),__publicField(this,"autoSwitchToSmallCountMap",new Map),__publicField(this,"sleepMap",new Map),__publicField(this,"fpsSmallQueueMap",new Map),__publicField(this,"fpsBigQueueMap",new Map),__publicField(this,"checkIntervalTime",2e3),__publicField(this,"rttOverCount",0),__publicField(this,"lossOverCount",0),__publicField(this,"rttUnderCount",0),__publicField(this,"lossUnderCount",0),__publicField(this,"checkInterval",null),__publicField(this,"beforeSwitchFpsTrackingMap",new Map),__publicField(this,"isAutoSwitching",!1),__publicField(this,"testCount"),__publicField(this,"poorCount"),__publicField(this,"goodCount"),this.log=t.log.createChild({id:`${this.getAlias()}${ssasSeq}`}),this.log.info(`created id=${this.getAlias()}${ssasSeq}`),ssasSeq+=1}getName(){return t.Name}getAlias(){return"ssas"}getGroup(){return"ssas"}destroy(){this.log.debug("destroy"),this.stop()}getValidateRule(){return{type:this.core.enums.BASIC_TYPE.Object,required:!1}}start(t){const e=this.core.room.scheduleResult.autoSwitchSmallConfig||{};this.options=__spreadValues(__spreadValues(__spreadValues({},DEFAULT_OPTIONS),t),e),this.log.info("start with options:",this.options),this.poorCount=Math.floor(this.options.poorDuration/this.checkIntervalTime),this.goodCount=Math.floor(this.options.goodDuration/this.checkIntervalTime),this.testCount=Math.max(1,this.poorCount,this.goodCount),this.core.trtc.on(this.core.TRTC.EVENT.STATISTICS,this.onStatistics,this),this.core.trtc.on(this.core.TRTC.EVENT.NETWORK_QUALITY,this.onNetworkQuality,this),this.core.room.on(this.core.enums.RoomEvent.SUBSCRIBE_SMALL_VIDEO_CHANGED,this.onSmallVideoChanged,this),this.core.room.on(this.core.enums.RoomEvent.PEER_LEAVE,this.onRemoteUserLeave,this),this.checkInterval=setInterval((()=>{const{room:t}=this.core;if(0===t.remotePublishedUserMap.size)return;for(const[t,e]of this.sleepMap)if(e>0){const s=e-this.checkIntervalTime;s<=0?this.sleepMap.delete(t):this.sleepMap.set(t,s)}const e=this.getShouldSwitchToSmallUserList(),s=this.getShouldSwitchToBigUserList();for(const s of e){if((this.sleepMap.get(s)||0)>0)continue;const e=t.remotePublishedUserMap.get(s),i=this.isBigVideoSubscribed(t,s),o=null==e?void 0:e.muteState.hasSmall;if(!e||!i||!o)continue;const r=this.autoSwitchToSmallCountMap.get(s)||0;r>=this.options.maxAutoSwitchToSmallCount||(this.toggleSmall(e,!0),this.fpsBigQueueMap.set(s,[]),this.autoSwitchToSmallCountMap.set(s,r+1),this.sleepMap.set(s,this.options.sleepTime))}for(const e of s){if((this.sleepMap.get(e)||0)>0)continue;const s=t.remotePublishedUserMap.get(e),i=this.isSmallVideoSubscribed(t,e);if(!s||!i)continue;(this.autoSwitchToSmallCountMap.get(e)||0)===this.options.maxAutoSwitchToSmallCount-1&&(this.toggleSmall(s,!1),this.fpsSmallQueueMap.set(e,[]),this.sleepMap.set(e,this.options.sleepTime))}}),this.checkIntervalTime),this.log.info("started")}update(){this.log.info("update")}stop(){this.core.trtc.off(this.core.TRTC.EVENT.NETWORK_QUALITY,this.onNetworkQuality,this),this.core.trtc.off(this.core.TRTC.EVENT.STATISTICS,this.onStatistics,this),this.core.room.off(this.core.enums.RoomEvent.SUBSCRIBE_SMALL_VIDEO_CHANGED,this.onSmallVideoChanged,this),this.core.room.off(this.core.enums.RoomEvent.PEER_LEAVE,this.onRemoteUserLeave,this),this.fpsBigQueueMap.clear(),this.fpsSmallQueueMap.clear(),this.rttQueue=[],this.lossQueue=[],this.rttOverCount=0,this.lossOverCount=0,this.rttUnderCount=0,this.lossUnderCount=0,this.sleepMap.clear(),this.autoSwitchToSmallCountMap.clear(),this.beforeSwitchFpsTrackingMap.clear(),this.checkInterval&&(clearInterval(this.checkInterval),this.checkInterval=null),this.log.info("stopped")}toggleSmall(t,e){const{userId:s}=t;this.log.info(`autoswitch ${s} to ${e?"small":"big"} stream`),this.isAutoSwitching=!0,this.core.room.changeType(e,t),setTimeout((()=>this.isAutoSwitching=!1),0),this.takeSnapshot(s,e);const i=e?593e3:593001;if(this.core.kvStatManager.addCount({key:i}),e){const t=this.fpsBigQueueMap.get(s);if(!t)return;const e=t.reduce(((t,e)=>t+e),0)/t.length;if(this.fpsSmallQueueMap.set(s,[]),0===e)return;this.beforeSwitchFpsTrackingMap.set(s,e)}}onSmallVideoChanged(t){if(this.isAutoSwitching)return;const{userId:e,isSmall:s}=t;this.log.info(`small video changed: userId=${e}, isSmall=${s}`),s?this.autoSwitchToSmallCountMap.set(e,this.options.maxAutoSwitchToSmallCount):(this.autoSwitchToSmallCountMap.set(e,0),this.sleepMap.set(e,this.options.sleepTime))}onRemoteUserLeave(t){this.fpsBigQueueMap.delete(t),this.fpsSmallQueueMap.delete(t),this.autoSwitchToSmallCountMap.delete(t),this.sleepMap.delete(t),this.beforeSwitchFpsTrackingMap.delete(t)}onNetworkQuality(t){const{downlinkRTT:e,downlinkLoss:s}=t;this.rttQueue.push(e),this.lossQueue.push(s),this.rttQueue.length>this.testCount&&this.rttQueue.shift(),this.lossQueue.length>this.testCount&&this.lossQueue.shift(),this.rttOverCount=e>this.options.rttPoorLimit?this.rttOverCount+1:0,this.lossOverCount=s>this.options.lossPoorLimit?this.lossOverCount+1:0,this.rttUnderCount=e<this.options.rttGoodLimit?this.rttUnderCount+1:0,this.lossUnderCount=s<this.options.lossGoodLimit?this.lossUnderCount+1:0}onStatistics(t){var e;if(null==(e=null==t?void 0:t.remoteStatistics)?void 0:e.length)try{t.remoteStatistics.forEach((t=>{var e,s,i;const{userId:o}=t;if(!o||!(null==(e=t.video)?void 0:e.length))return;const r=(null==(s=t.video.find((t=>"big"===t.videoType)))?void 0:s.frameRate)||0,h=(null==(i=t.video.find((t=>"small"===t.videoType)))?void 0:i.frameRate)||0,l=(t,e)=>{let s=t.get(o);s||(s=[],t.set(o,s)),s.push(e),s.length>this.testCount&&s.shift()};l(this.fpsBigQueueMap,r),l(this.fpsSmallQueueMap,h),this.checkAndReportFpsRatio(o)}))}catch(t){this.log.warn("onStatistics error",t)}}checkAndReportFpsRatio(t){const e=this.beforeSwitchFpsTrackingMap.get(t);if(!e)return;const s=this.fpsSmallQueueMap.get(t);if(!s||s.length<this.testCount)return;const i=s.reduce(((t,e)=>t+e),0)/s.length,o=i/e*100,r=Math.round(Math.max(1,Math.min(1e3,o)));this.core.kvStatManager.addNumber({key:593800,value:r,split:[0,100,200,300,400,500,800,1e3],max:1e3}),this.log.debug(`beforeSwitchFps: ${e}, afterSwitchFps: ${i}, afterSwitchQueue: ${s.join(",")}`),this.log.info(`FPS ratio reported for user ${t}: ${r.toFixed(2)}% (before: ${e.toFixed(1)}, after: ${i.toFixed(1)})`),this.beforeSwitchFpsTrackingMap.delete(t)}getBadFrameRateUserList(){const t=[];for(const[e,s]of this.fpsBigQueueMap){if(!s||0===s.length)continue;const i=Math.max(...s),o=Math.min(...s);s.length!==this.testCount||0!==o||0!==i?0!==i&&i-o>i/3&&t.push(e):t.push(e)}return t}getGoodFrameRateUserList(){const t=[];for(const[e,s]of this.fpsSmallQueueMap){if(!s||0===s.length)continue;const i=Math.max(...s),o=Math.min(...s);0!==i&&(i-o<=i/3&&t.push(e))}return t}getShouldSwitchToSmallUserList(){if(this.rttQueue.length<this.testCount||this.lossQueue.length<this.testCount)return[];if(!(this.rttOverCount>=this.poorCount||this.lossOverCount>=this.poorCount))return[];const t=this.getBadFrameRateUserList();return t.length>0&&this.log.debug(`badFrameRateUserList: ${t.join(",")}`),t}getShouldSwitchToBigUserList(){if(!(this.rttUnderCount>=this.goodCount&&this.lossUnderCount>=this.goodCount))return[];const t=this.getGoodFrameRateUserList();return t.length>0&&this.log.debug(`goodFrameRateUserList: ${t.join(",")}`),t}takeSnapshot(t,e){this.log.info(`Network stats at snapshot: RTTQueue=${this.rttQueue.join(",")}, LossQueue=${this.lossQueue.join(",")}`);const s=e?this.fpsBigQueueMap.get(t):this.fpsSmallQueueMap.get(t);this.log.info(`${e?"big":"small"} frame rate stats at snapshot: ${(null==s?void 0:s.join(","))||"N/A"}`)}isBigVideoSubscribed(t,e){const s=t.remotePublishedUserMap.get(e);return!!s&&s.remoteVideoTrack.mediaType===this.core.enums.MediaType.BIG_VIDEO}isSmallVideoSubscribed(t,e){const s=t.remotePublishedUserMap.get(e);return!!s&&s.remoteVideoTrack.mediaType===this.core.enums.MediaType.SMALL_VIDEO}};__publicField(_SmallStreamAutoSwitcher,"Name","SmallStreamAutoSwitcher");var SmallStreamAutoSwitcher=_SmallStreamAutoSwitcher,index_default=SmallStreamAutoSwitcher;export{index_default as default};export{SmallStreamAutoSwitcher};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).SmallStreamAutoSwitcher={})}(this,(function(t){"use strict";function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var o=0,i=Array(e);o<e;o++)i[o]=t[o];return i}function o(t,e,o){return e&&function(t,e){for(var o=0;o<e.length;o++){var i=e[o];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,s(i.key),i)}}(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t}function i(t,e){var o="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!o){if(Array.isArray(t)||(o=u(t))||e){o&&(t=o);var i=0,n=function(){};return{s:n,n:function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}},e:function(t){throw t},f:n}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r,s=!0,a=!1;return{s:function(){o=o.call(t)},n:function(){var t=o.next();return s=t.done,t},e:function(t){a=!0,r=t},f:function(){try{s||null==o.return||o.return()}finally{if(a)throw r}}}}function n(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var o=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=o){var i,n,r,s,a=[],u=!0,l=!1;try{if(r=(o=o.call(t)).next,0===e);else for(;!(u=(i=r.call(o)).done)&&(a.push(i.value),a.length!==e);u=!0);}catch(t){l=!0,n=t}finally{try{if(!u&&null!=o.return&&(s=o.return(),Object(s)!==s))return}finally{if(l)throw n}}return a}}(t,e)||u(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(t){return function(t){if(Array.isArray(t))return e(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||u(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var o=t[Symbol.toPrimitive];if(void 0!==o){var i=o.call(t,e);if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t,"string");return"symbol"==typeof e?e:e+""}function a(t){return a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},a(t)}function u(t,o){if(t){if("string"==typeof t)return e(t,o);var i={}.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?e(t,o):void 0}}var l=Object.defineProperty,h=Object.getOwnPropertySymbols,c=Object.prototype.hasOwnProperty,f=Object.prototype.propertyIsEnumerable,p=function(t,e,o){return e in t?l(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o},m=function(t,e){for(var o in e||(e={}))c.call(e,o)&&p(t,o,e[o]);if(h){var n,r=i(h(e));try{for(r.s();!(n=r.n()).done;){o=n.value;f.call(e,o)&&p(t,o,e[o])}}catch(t){r.e(t)}finally{r.f()}}return t},d=function(t,e,o){return p(t,"symbol"!==a(e)?e+"":e,o)},v=function(t){return t[t.AFTER_SWITCH_TO_SMALL=593800]="AFTER_SWITCH_TO_SMALL",t}(v||{}),S=function(t){return t[t.SWITCH_TO_SMALL=593e3]="SWITCH_TO_SMALL",t[t.SWITCH_TO_BIG=593001]="SWITCH_TO_BIG",t}(S||{}),g={rttPoorLimit:200,lossPoorLimit:20,rttGoodLimit:100,lossGoodLimit:10,sleepTime:3e4,poorDuration:1e4,goodDuration:2e4,maxAutoSwitchToSmallCount:2},y=0,T=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.core=e,d(this,"log"),d(this,"options",g),d(this,"rttQueue",[]),d(this,"lossQueue",[]),d(this,"autoSwitchToSmallCountMap",new Map),d(this,"sleepMap",new Map),d(this,"fpsSmallQueueMap",new Map),d(this,"fpsBigQueueMap",new Map),d(this,"checkIntervalTime",2e3),d(this,"rttOverCount",0),d(this,"lossOverCount",0),d(this,"rttUnderCount",0),d(this,"lossUnderCount",0),d(this,"checkInterval",null),d(this,"beforeSwitchFpsTrackingMap",new Map),d(this,"isAutoSwitching",!1),d(this,"testCount"),d(this,"poorCount"),d(this,"goodCount"),this.log=e.log.createChild({id:"".concat(this.getAlias()).concat(y)}),this.log.info("created id=".concat(this.getAlias()).concat(y)),y+=1}return o(t,[{key:"getName",value:function(){return t.Name}},{key:"getAlias",value:function(){return"ssas"}},{key:"getGroup",value:function(){return"ssas"}},{key:"destroy",value:function(){this.log.debug("destroy"),this.stop()}},{key:"getValidateRule",value:function(){return{type:this.core.enums.BASIC_TYPE.Object,required:!1}}},{key:"start",value:function(t){var e=this,o=this.core.room.scheduleResult.autoSwitchSmallConfig||{};this.options=m(m(m({},g),t),o),this.log.info("start with options:",this.options),this.poorCount=Math.floor(this.options.poorDuration/this.checkIntervalTime),this.goodCount=Math.floor(this.options.goodDuration/this.checkIntervalTime),this.testCount=Math.max(1,this.poorCount,this.goodCount),this.core.trtc.on(this.core.TRTC.EVENT.STATISTICS,this.onStatistics,this),this.core.trtc.on(this.core.TRTC.EVENT.NETWORK_QUALITY,this.onNetworkQuality,this),this.core.room.on(this.core.enums.RoomEvent.SUBSCRIBE_SMALL_VIDEO_CHANGED,this.onSmallVideoChanged,this),this.core.room.on(this.core.enums.RoomEvent.PEER_LEAVE,this.onRemoteUserLeave,this),this.checkInterval=setInterval((function(){var t=e.core.room;if(0!==t.remotePublishedUserMap.size){var o,r=i(e.sleepMap);try{for(r.s();!(o=r.n()).done;){var s=n(o.value,2),a=s[0],u=s[1];if(u>0){var l=u-e.checkIntervalTime;l<=0?e.sleepMap.delete(a):e.sleepMap.set(a,l)}}}catch(t){r.e(t)}finally{r.f()}var h,c=e.getShouldSwitchToSmallUserList(),f=e.getShouldSwitchToBigUserList(),p=i(c);try{for(p.s();!(h=p.n()).done;){var m=h.value;if(!((e.sleepMap.get(m)||0)>0)){var d=t.remotePublishedUserMap.get(m),v=e.isBigVideoSubscribed(t,m);if(d&&v){var S=e.autoSwitchToSmallCountMap.get(m)||0;S>=e.options.maxAutoSwitchToSmallCount||(e.toggleSmall(d,!0),e.fpsBigQueueMap.set(m,[]),e.autoSwitchToSmallCountMap.set(m,S+1),e.sleepMap.set(m,e.options.sleepTime))}}}}catch(t){p.e(t)}finally{p.f()}var g,y=i(f);try{for(y.s();!(g=y.n()).done;){var T=g.value;if(!((e.sleepMap.get(T)||0)>0)){var b=t.remotePublishedUserMap.get(T),M=e.isSmallVideoSubscribed(t,T);if(b&&M)(e.autoSwitchToSmallCountMap.get(T)||0)===e.options.maxAutoSwitchToSmallCount-1&&(e.toggleSmall(b,!1),e.fpsSmallQueueMap.set(T,[]),e.sleepMap.set(T,e.options.sleepTime))}}}catch(t){y.e(t)}finally{y.f()}}}),this.checkIntervalTime),this.log.info("started")}},{key:"update",value:function(){this.log.info("update")}},{key:"stop",value:function(){this.core.trtc.off(this.core.TRTC.EVENT.NETWORK_QUALITY,this.onNetworkQuality,this),this.core.trtc.off(this.core.TRTC.EVENT.STATISTICS,this.onStatistics,this),this.core.room.off(this.core.enums.RoomEvent.SUBSCRIBE_SMALL_VIDEO_CHANGED,this.onSmallVideoChanged,this),this.core.room.off(this.core.enums.RoomEvent.PEER_LEAVE,this.onRemoteUserLeave,this),this.fpsBigQueueMap.clear(),this.fpsSmallQueueMap.clear(),this.rttQueue=[],this.lossQueue=[],this.rttOverCount=0,this.lossOverCount=0,this.rttUnderCount=0,this.lossUnderCount=0,this.sleepMap.clear(),this.autoSwitchToSmallCountMap.clear(),this.beforeSwitchFpsTrackingMap.clear(),this.checkInterval&&(clearInterval(this.checkInterval),this.checkInterval=null),this.log.info("stopped")}},{key:"toggleSmall",value:function(t,e){var o=this,i=t.userId;this.log.info("autoswitch ".concat(i," to ").concat(e?"small":"big"," stream")),this.isAutoSwitching=!0,this.core.room.changeType(e,t),setTimeout((function(){return o.isAutoSwitching=!1}),0),this.takeSnapshot(i,e);var n=e?593e3:593001;if(this.core.kvStatManager.addCount({key:n}),e){var r=this.fpsBigQueueMap.get(i);if(!r)return;var s=r.reduce((function(t,e){return t+e}),0)/r.length;if(this.fpsSmallQueueMap.set(i,[]),0===s)return;this.beforeSwitchFpsTrackingMap.set(i,s)}}},{key:"onSmallVideoChanged",value:function(t){if(!this.isAutoSwitching){var e=t.userId,o=t.isSmall;this.log.info("small video changed: userId=".concat(e,", isSmall=").concat(o)),o?this.autoSwitchToSmallCountMap.set(e,this.options.maxAutoSwitchToSmallCount):(this.autoSwitchToSmallCountMap.set(e,0),this.sleepMap.set(e,this.options.sleepTime))}}},{key:"onRemoteUserLeave",value:function(t){this.fpsBigQueueMap.delete(t),this.fpsSmallQueueMap.delete(t),this.autoSwitchToSmallCountMap.delete(t),this.sleepMap.delete(t),this.beforeSwitchFpsTrackingMap.delete(t)}},{key:"onNetworkQuality",value:function(t){var e=t.downlinkRTT,o=t.downlinkLoss;this.rttQueue.push(e),this.lossQueue.push(o),this.rttQueue.length>this.testCount&&this.rttQueue.shift(),this.lossQueue.length>this.testCount&&this.lossQueue.shift(),this.rttOverCount=e>this.options.rttPoorLimit?this.rttOverCount+1:0,this.lossOverCount=o>this.options.lossPoorLimit?this.lossOverCount+1:0,this.rttUnderCount=e<this.options.rttGoodLimit?this.rttUnderCount+1:0,this.lossUnderCount=o<this.options.lossGoodLimit?this.lossUnderCount+1:0}},{key:"onStatistics",value:function(t){var e,o=this;if(null==(e=null==t?void 0:t.remoteStatistics)?void 0:e.length)try{t.remoteStatistics.forEach((function(t){var e,i,n,r=t.userId;if(r&&(null==(e=t.video)?void 0:e.length)){var s=(null==(i=t.video.find((function(t){return"big"===t.videoType})))?void 0:i.frameRate)||0,a=(null==(n=t.video.find((function(t){return"small"===t.videoType})))?void 0:n.frameRate)||0,u=function(t,e){var i=t.get(r);i||(i=[],t.set(r,i)),i.push(e),i.length>o.testCount&&i.shift()};u(o.fpsBigQueueMap,s),u(o.fpsSmallQueueMap,a),o.checkAndReportFpsRatio(r)}}))}catch(t){this.log.warn("onStatistics error",t)}}},{key:"checkAndReportFpsRatio",value:function(t){var e=this.beforeSwitchFpsTrackingMap.get(t);if(e){var o=this.fpsSmallQueueMap.get(t);if(o&&!(o.length<this.testCount)){var i=o.reduce((function(t,e){return t+e}),0)/o.length,n=i/e*100,r=Math.round(Math.max(1,Math.min(1e3,n)));this.core.kvStatManager.addNumber({key:593800,value:r,split:[0,100,200,300,400,500,800,1e3],max:1e3}),this.log.debug("beforeSwitchFps: ".concat(e,", afterSwitchFps: ").concat(i,", afterSwitchQueue: ").concat(o.join(","))),this.log.info("FPS ratio reported for user ".concat(t,": ").concat(r.toFixed(2),"% (before: ").concat(e.toFixed(1),", after: ").concat(i.toFixed(1),")")),this.beforeSwitchFpsTrackingMap.delete(t)}}}},{key:"getBadFrameRateUserList",value:function(){var t,e=[],o=i(this.fpsBigQueueMap);try{for(o.s();!(t=o.n()).done;){var s=n(t.value,2),a=s[0],u=s[1];if(u&&0!==u.length){var l=Math.max.apply(Math,r(u)),h=Math.min.apply(Math,r(u));u.length!==this.testCount||0!==h||0!==l?0!==l&&l-h>l/3&&e.push(a):e.push(a)}}}catch(t){o.e(t)}finally{o.f()}return e}},{key:"getGoodFrameRateUserList",value:function(){var t,e=[],o=i(this.fpsSmallQueueMap);try{for(o.s();!(t=o.n()).done;){var s=n(t.value,2),a=s[0],u=s[1];if(u&&0!==u.length){var l=Math.max.apply(Math,r(u)),h=Math.min.apply(Math,r(u));0!==l&&l-h<=l/3&&e.push(a)}}}catch(t){o.e(t)}finally{o.f()}return e}},{key:"getShouldSwitchToSmallUserList",value:function(){if(this.rttQueue.length<this.testCount||this.lossQueue.length<this.testCount)return[];if(!(this.rttOverCount>=this.poorCount||this.lossOverCount>=this.poorCount))return[];var t=this.getBadFrameRateUserList();return t.length>0&&this.log.debug("badFrameRateUserList: ".concat(t.join(","))),t}},{key:"getShouldSwitchToBigUserList",value:function(){if(!(this.rttUnderCount>=this.goodCount&&this.lossUnderCount>=this.goodCount))return[];var t=this.getGoodFrameRateUserList();return t.length>0&&this.log.debug("goodFrameRateUserList: ".concat(t.join(","))),t}},{key:"takeSnapshot",value:function(t,e){this.log.info("Network stats at snapshot: RTTQueue=".concat(this.rttQueue.join(","),", LossQueue=").concat(this.lossQueue.join(",")));var o=e?this.fpsBigQueueMap.get(t):this.fpsSmallQueueMap.get(t);this.log.info("".concat(e?"big":"small"," frame rate stats at snapshot: ").concat((null==o?void 0:o.join(","))||"N/A"))}},{key:"isBigVideoSubscribed",value:function(t,e){var o=t.remotePublishedUserMap.get(e);return!!o&&o.remoteVideoTrack.mediaType===this.core.enums.MediaType.BIG_VIDEO}},{key:"isSmallVideoSubscribed",value:function(t,e){var o=t.remotePublishedUserMap.get(e);return!!o&&o.remoteVideoTrack.mediaType===this.core.enums.MediaType.SMALL_VIDEO}}])}();d(T,"Name","SmallStreamAutoSwitcher");var b=T;t.KV_REPORT_KEY_SMALL_STREAM_AUTO_SWITCHER=S,t.KV_REPORT_KEY_SMALL_STREAM_AUTO_SWITCHER_FRAME_RATE_RATIO=v,t.default=b,Object.defineProperty(t,"__esModule",{value:!0})}));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).SmallStreamAutoSwitcher=e()}(this,(function(){"use strict";function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var o=0,i=Array(e);o<e;o++)i[o]=t[o];return i}function e(t,e,o){return e&&function(t,e){for(var o=0;o<e.length;o++){var i=e[o];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,r(i.key),i)}}(t.prototype,e),Object.defineProperty(t,"prototype",{writable:!1}),t}function o(t,e){var o="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!o){if(Array.isArray(t)||(o=a(t))||e){o&&(t=o);var i=0,n=function(){};return{s:n,n:function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}},e:function(t){throw t},f:n}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r,s=!0,u=!1;return{s:function(){o=o.call(t)},n:function(){var t=o.next();return s=t.done,t},e:function(t){u=!0,r=t},f:function(){try{s||null==o.return||o.return()}finally{if(u)throw r}}}}function i(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var o=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=o){var i,n,r,s,a=[],u=!0,l=!1;try{if(r=(o=o.call(t)).next,0===e);else for(;!(u=(i=r.call(o)).done)&&(a.push(i.value),a.length!==e);u=!0);}catch(t){l=!0,n=t}finally{try{if(!u&&null!=o.return&&(s=o.return(),Object(s)!==s))return}finally{if(l)throw n}}return a}}(t,e)||a(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function n(e){return function(e){if(Array.isArray(e))return t(e)}(e)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(e)||a(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var o=t[Symbol.toPrimitive];if(void 0!==o){var i=o.call(t,e);if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t,"string");return"symbol"==typeof e?e:e+""}function s(t){return s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},s(t)}function a(e,o){if(e){if("string"==typeof e)return t(e,o);var i={}.toString.call(e).slice(8,-1);return"Object"===i&&e.constructor&&(i=e.constructor.name),"Map"===i||"Set"===i?Array.from(e):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?t(e,o):void 0}}var u=Object.defineProperty,l=Object.getOwnPropertySymbols,h=Object.prototype.hasOwnProperty,c=Object.prototype.propertyIsEnumerable,f=function(t,e,o){return e in t?u(t,e,{enumerable:!0,configurable:!0,writable:!0,value:o}):t[e]=o},p=function(t,e){for(var i in e||(e={}))h.call(e,i)&&f(t,i,e[i]);if(l){var n,r=o(l(e));try{for(r.s();!(n=r.n()).done;){i=n.value;c.call(e,i)&&f(t,i,e[i])}}catch(t){r.e(t)}finally{r.f()}}return t},m=function(t,e,o){return f(t,"symbol"!==s(e)?e+"":e,o)},d={rttPoorLimit:200,lossPoorLimit:20,rttGoodLimit:100,lossGoodLimit:10,sleepTime:3e4,poorDuration:1e4,goodDuration:2e4,maxAutoSwitchToSmallCount:2},v=0,g=function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.core=e,m(this,"log"),m(this,"options",d),m(this,"rttQueue",[]),m(this,"lossQueue",[]),m(this,"autoSwitchToSmallCountMap",new Map),m(this,"sleepMap",new Map),m(this,"fpsSmallQueueMap",new Map),m(this,"fpsBigQueueMap",new Map),m(this,"checkIntervalTime",2e3),m(this,"rttOverCount",0),m(this,"lossOverCount",0),m(this,"rttUnderCount",0),m(this,"lossUnderCount",0),m(this,"checkInterval",null),m(this,"beforeSwitchFpsTrackingMap",new Map),m(this,"isAutoSwitching",!1),m(this,"testCount"),m(this,"poorCount"),m(this,"goodCount"),this.log=e.log.createChild({id:"".concat(this.getAlias()).concat(v)}),this.log.info("created id=".concat(this.getAlias()).concat(v)),v+=1}return e(t,[{key:"getName",value:function(){return t.Name}},{key:"getAlias",value:function(){return"ssas"}},{key:"getGroup",value:function(){return"ssas"}},{key:"destroy",value:function(){this.log.debug("destroy"),this.stop()}},{key:"getValidateRule",value:function(){return{type:this.core.enums.BASIC_TYPE.Object,required:!1}}},{key:"start",value:function(t){var e=this,n=this.core.room.scheduleResult.autoSwitchSmallConfig||{};this.options=p(p(p({},d),t),n),this.log.info("start with options:",this.options),this.poorCount=Math.floor(this.options.poorDuration/this.checkIntervalTime),this.goodCount=Math.floor(this.options.goodDuration/this.checkIntervalTime),this.testCount=Math.max(1,this.poorCount,this.goodCount),this.core.trtc.on(this.core.TRTC.EVENT.STATISTICS,this.onStatistics,this),this.core.trtc.on(this.core.TRTC.EVENT.NETWORK_QUALITY,this.onNetworkQuality,this),this.core.room.on(this.core.enums.RoomEvent.SUBSCRIBE_SMALL_VIDEO_CHANGED,this.onSmallVideoChanged,this),this.core.room.on(this.core.enums.RoomEvent.PEER_LEAVE,this.onRemoteUserLeave,this),this.checkInterval=setInterval((function(){var t=e.core.room;if(0!==t.remotePublishedUserMap.size){var n,r=o(e.sleepMap);try{for(r.s();!(n=r.n()).done;){var s=i(n.value,2),a=s[0],u=s[1];if(u>0){var l=u-e.checkIntervalTime;l<=0?e.sleepMap.delete(a):e.sleepMap.set(a,l)}}}catch(t){r.e(t)}finally{r.f()}var h,c=e.getShouldSwitchToSmallUserList(),f=e.getShouldSwitchToBigUserList(),p=o(c);try{for(p.s();!(h=p.n()).done;){var m=h.value;if(!((e.sleepMap.get(m)||0)>0)){var d=t.remotePublishedUserMap.get(m),v=e.isBigVideoSubscribed(t,m),g=null==d?void 0:d.muteState.hasSmall;if(d&&v&&g){var y=e.autoSwitchToSmallCountMap.get(m)||0;y>=e.options.maxAutoSwitchToSmallCount||(e.toggleSmall(d,!0),e.fpsBigQueueMap.set(m,[]),e.autoSwitchToSmallCountMap.set(m,y+1),e.sleepMap.set(m,e.options.sleepTime))}}}}catch(t){p.e(t)}finally{p.f()}var S,b=o(f);try{for(b.s();!(S=b.n()).done;){var T=S.value;if(!((e.sleepMap.get(T)||0)>0)){var M=t.remotePublishedUserMap.get(T),C=e.isSmallVideoSubscribed(t,T);if(M&&C)(e.autoSwitchToSmallCountMap.get(T)||0)===e.options.maxAutoSwitchToSmallCount-1&&(e.toggleSmall(M,!1),e.fpsSmallQueueMap.set(T,[]),e.sleepMap.set(T,e.options.sleepTime))}}}catch(t){b.e(t)}finally{b.f()}}}),this.checkIntervalTime),this.log.info("started")}},{key:"update",value:function(){this.log.info("update")}},{key:"stop",value:function(){this.core.trtc.off(this.core.TRTC.EVENT.NETWORK_QUALITY,this.onNetworkQuality,this),this.core.trtc.off(this.core.TRTC.EVENT.STATISTICS,this.onStatistics,this),this.core.room.off(this.core.enums.RoomEvent.SUBSCRIBE_SMALL_VIDEO_CHANGED,this.onSmallVideoChanged,this),this.core.room.off(this.core.enums.RoomEvent.PEER_LEAVE,this.onRemoteUserLeave,this),this.fpsBigQueueMap.clear(),this.fpsSmallQueueMap.clear(),this.rttQueue=[],this.lossQueue=[],this.rttOverCount=0,this.lossOverCount=0,this.rttUnderCount=0,this.lossUnderCount=0,this.sleepMap.clear(),this.autoSwitchToSmallCountMap.clear(),this.beforeSwitchFpsTrackingMap.clear(),this.checkInterval&&(clearInterval(this.checkInterval),this.checkInterval=null),this.log.info("stopped")}},{key:"toggleSmall",value:function(t,e){var o=this,i=t.userId;this.log.info("autoswitch ".concat(i," to ").concat(e?"small":"big"," stream")),this.isAutoSwitching=!0,this.core.room.changeType(e,t),setTimeout((function(){return o.isAutoSwitching=!1}),0),this.takeSnapshot(i,e);var n=e?593e3:593001;if(this.core.kvStatManager.addCount({key:n}),e){var r=this.fpsBigQueueMap.get(i);if(!r)return;var s=r.reduce((function(t,e){return t+e}),0)/r.length;if(this.fpsSmallQueueMap.set(i,[]),0===s)return;this.beforeSwitchFpsTrackingMap.set(i,s)}}},{key:"onSmallVideoChanged",value:function(t){if(!this.isAutoSwitching){var e=t.userId,o=t.isSmall;this.log.info("small video changed: userId=".concat(e,", isSmall=").concat(o)),o?this.autoSwitchToSmallCountMap.set(e,this.options.maxAutoSwitchToSmallCount):(this.autoSwitchToSmallCountMap.set(e,0),this.sleepMap.set(e,this.options.sleepTime))}}},{key:"onRemoteUserLeave",value:function(t){this.fpsBigQueueMap.delete(t),this.fpsSmallQueueMap.delete(t),this.autoSwitchToSmallCountMap.delete(t),this.sleepMap.delete(t),this.beforeSwitchFpsTrackingMap.delete(t)}},{key:"onNetworkQuality",value:function(t){var e=t.downlinkRTT,o=t.downlinkLoss;this.rttQueue.push(e),this.lossQueue.push(o),this.rttQueue.length>this.testCount&&this.rttQueue.shift(),this.lossQueue.length>this.testCount&&this.lossQueue.shift(),this.rttOverCount=e>this.options.rttPoorLimit?this.rttOverCount+1:0,this.lossOverCount=o>this.options.lossPoorLimit?this.lossOverCount+1:0,this.rttUnderCount=e<this.options.rttGoodLimit?this.rttUnderCount+1:0,this.lossUnderCount=o<this.options.lossGoodLimit?this.lossUnderCount+1:0}},{key:"onStatistics",value:function(t){var e,o=this;if(null==(e=null==t?void 0:t.remoteStatistics)?void 0:e.length)try{t.remoteStatistics.forEach((function(t){var e,i,n,r=t.userId;if(r&&(null==(e=t.video)?void 0:e.length)){var s=(null==(i=t.video.find((function(t){return"big"===t.videoType})))?void 0:i.frameRate)||0,a=(null==(n=t.video.find((function(t){return"small"===t.videoType})))?void 0:n.frameRate)||0,u=function(t,e){var i=t.get(r);i||(i=[],t.set(r,i)),i.push(e),i.length>o.testCount&&i.shift()};u(o.fpsBigQueueMap,s),u(o.fpsSmallQueueMap,a),o.checkAndReportFpsRatio(r)}}))}catch(t){this.log.warn("onStatistics error",t)}}},{key:"checkAndReportFpsRatio",value:function(t){var e=this.beforeSwitchFpsTrackingMap.get(t);if(e){var o=this.fpsSmallQueueMap.get(t);if(o&&!(o.length<this.testCount)){var i=o.reduce((function(t,e){return t+e}),0)/o.length,n=i/e*100,r=Math.round(Math.max(1,Math.min(1e3,n)));this.core.kvStatManager.addNumber({key:593800,value:r,split:[0,100,200,300,400,500,800,1e3],max:1e3}),this.log.debug("beforeSwitchFps: ".concat(e,", afterSwitchFps: ").concat(i,", afterSwitchQueue: ").concat(o.join(","))),this.log.info("FPS ratio reported for user ".concat(t,": ").concat(r.toFixed(2),"% (before: ").concat(e.toFixed(1),", after: ").concat(i.toFixed(1),")")),this.beforeSwitchFpsTrackingMap.delete(t)}}}},{key:"getBadFrameRateUserList",value:function(){var t,e=[],r=o(this.fpsBigQueueMap);try{for(r.s();!(t=r.n()).done;){var s=i(t.value,2),a=s[0],u=s[1];if(u&&0!==u.length){var l=Math.max.apply(Math,n(u)),h=Math.min.apply(Math,n(u));u.length!==this.testCount||0!==h||0!==l?0!==l&&l-h>l/3&&e.push(a):e.push(a)}}}catch(t){r.e(t)}finally{r.f()}return e}},{key:"getGoodFrameRateUserList",value:function(){var t,e=[],r=o(this.fpsSmallQueueMap);try{for(r.s();!(t=r.n()).done;){var s=i(t.value,2),a=s[0],u=s[1];if(u&&0!==u.length){var l=Math.max.apply(Math,n(u)),h=Math.min.apply(Math,n(u));0!==l&&l-h<=l/3&&e.push(a)}}}catch(t){r.e(t)}finally{r.f()}return e}},{key:"getShouldSwitchToSmallUserList",value:function(){if(this.rttQueue.length<this.testCount||this.lossQueue.length<this.testCount)return[];if(!(this.rttOverCount>=this.poorCount||this.lossOverCount>=this.poorCount))return[];var t=this.getBadFrameRateUserList();return t.length>0&&this.log.debug("badFrameRateUserList: ".concat(t.join(","))),t}},{key:"getShouldSwitchToBigUserList",value:function(){if(!(this.rttUnderCount>=this.goodCount&&this.lossUnderCount>=this.goodCount))return[];var t=this.getGoodFrameRateUserList();return t.length>0&&this.log.debug("goodFrameRateUserList: ".concat(t.join(","))),t}},{key:"takeSnapshot",value:function(t,e){this.log.info("Network stats at snapshot: RTTQueue=".concat(this.rttQueue.join(","),", LossQueue=").concat(this.lossQueue.join(",")));var o=e?this.fpsBigQueueMap.get(t):this.fpsSmallQueueMap.get(t);this.log.info("".concat(e?"big":"small"," frame rate stats at snapshot: ").concat((null==o?void 0:o.join(","))||"N/A"))}},{key:"isBigVideoSubscribed",value:function(t,e){var o=t.remotePublishedUserMap.get(e);return!!o&&o.remoteVideoTrack.mediaType===this.core.enums.MediaType.BIG_VIDEO}},{key:"isSmallVideoSubscribed",value:function(t,e){var o=t.remotePublishedUserMap.get(e);return!!o&&o.remoteVideoTrack.mediaType===this.core.enums.MediaType.SMALL_VIDEO}}])}();return m(g,"Name","SmallStreamAutoSwitcher"),g}));
|