zhouenguang 3 年之前
父節點
當前提交
0a0ff5471a
共有 2 個文件被更改,包括 224 次插入234 次删除
  1. 147 136
      src/EngineProxy.js
  2. 77 98
      src/开发笔记.txt

+ 147 - 136
src/EngineProxy.js

@@ -49,141 +49,36 @@ function getSceneManager(i, e) {
 
 export default class EngineProxy{
     constructor(xverseRoom) {
-        E(this, "_tvs", []);
-        E(this, "isRenderFirstFrame", !1);
-        E(this, "_idleTime", 0);
-        E(this, "renderTimer");
-        E(this, "lightManager");
-        E(this, "_checkSceneNotReadyCount", 0);
-        E(this, "_checkSceneDurationFrameNum", 0);
-        E(this, "_checkSceneFrameCount", 0);
-        E(this, "timeoutCircularArray", new CircularArray(120,!1,[]));
-        E(this, "frameCircularArray", new CircularArray(120,!1,[]));
-        E(this, "interFrameCircularArray", new CircularArray(120,!1,[]));
-        E(this, "drawCallCntCircularArray", new CircularArray(120,!1,[]));
-        E(this, "activeFacesCircularArray", new CircularArray(120,!1,[]));
-        E(this, "renderTimeCircularArray", new CircularArray(120,!1,[]));
-        E(this, "drawCallTimeCircularArray", new CircularArray(120,!1,[]));
-        E(this, "animationCircularArray", new CircularArray(120,!1,[]));
-        E(this, "meshSelectCircularArray", new CircularArray(120,!1,[]));
-        E(this, "renderTargetCircularArray", new CircularArray(120,!1,[]));
-        E(this, "regBeforeRenderCircularArray", new CircularArray(120,!1,[]));
-        E(this, "regAfterRenderCircularArray", new CircularArray(120,!1,[]));
-        E(this, "renderCnt", 0);
-        E(this, "renderErrorCount", 0);
-        E(this, "engineSloppyCnt", 0);
-        E(this, "systemStuckCnt", 0);
-        E(this, "frameRenderNumber", 0);
-        E(this, "_setFPS", (sceneManager,t=25)=>{
-            logger.info("Set fps to", t);
-            const r = t > 60 ? 60 : t < 24 ? 24 : t;
-            sceneManager.Engine.stopRenderLoop();
-            const n = 1e3 / r;
-            let o = Date.now()
-              , a = Date.now()
-              , s = n
-              , l = 1;
-            const u = ()=>{
-                var T;
-                const c = Date.now()
-                  , h = c - o
-                  , f = c - a;
-                a = c,
-                this.frameCircularArray.add(f),
-                h - s > n && (this.systemStuckCnt += 1);
-                const d = h / s;
-                l = .9 * l + .1 * d;
-                const _ = Date.now();
-                let g = 0
-                  , m = 0;
-                if (this.room.isUpdatedRawYUVData || this.room.isPano) {
-                    this.isRenderFirstFrame = !0
-                    if (this._checkSceneDurationFrameNum > 0)
-                    {
-                        this._checkSceneFrameCount++,
-                        this.room.sceneManager.isReadyToRender({}) && this._checkSceneDurationFrameNum--,
-                        this._checkSceneFrameCount > EngineProxy._CHECK_DURATION && (this._checkSceneDurationFrameNum = EngineProxy._CHECK_DURATION,
-                        this._checkSceneFrameCount = 0,
-                        this._checkSceneNotReadyCount++,
-                        (this._checkSceneNotReadyCount == 1 || this._checkSceneNotReadyCount % 100 == 0) && logger.error(`[SDK] Scene not ready, skip render. loop: ${this._checkSceneNotReadyCount}`),
-                        this._checkSceneNotReadyCount > 10 && (logger.error("[SDK] Scene not ready, reload later"),
-                        this.room.proxyEvents("renderError", {
-                            error: new Error("[SDK] Scene not ready, skip render and reload.")
-                        })),
-                        this.room.stats.assign({
-                            renderErrorCount: this._checkSceneNotReadyCount
-                        }),
-                        logger.infoAndReportMeasurement({
-                            value: 0,
-                            startTime: Date.now(),
-                            metric: "renderError",
-                            error: new Error("[SDK] Scene not ready, skip render and reload."),
-                            reportOptions: {
-                                sampleRate: .1
-                            }
-                        }));
-                    }
-                    else
-                    {
-                        try {
-                            sceneManager.render()
-                        } 
-                        catch (C) {
-                            this.renderErrorCount++,
-                            this.renderErrorCount > 10 && this.room.proxyEvents("renderError", {
-                                error: C
-                            }),
-                            this.room.stats.assign({
-                                renderErrorCount: this.renderErrorCount
-                            }),
-                            logger.infoAndReportMeasurement({
-                                value: 0,
-                                startTime: Date.now(),
-                                metric: "renderError",
-                                error: C,
-                                reportOptions: {
-                                    sampleRate: .1
-                                }
-                            })
-                        }
-                    }
-                    g = Date.now() - _,
-                    this.frameRenderNumber < 1e3 && this.frameRenderNumber++,
-                    this.room.networkController.rtcp.workers.UpdateYUV(),
-                    m = Date.now() - _ - g
-                }
-                this.isRenderFirstFrame || this.room.networkController.rtcp.workers.UpdateYUV();
-                const y = Date.now() - _;
-                o = c + y,
-                s = Math.min(Math.max((n - y) / l, 5), 200),
-                y > n && (s = 10,
-                this.engineSloppyCnt += 1),
-                this._idleTime = s;
-                const b = s;
-                if (s > 150 && console.log("lastGap is ", s, ", ratio is ", l, ", usedTimeMs is ", y, ", cpuRenderTime is ", g, ", cpuUpdateYUVTime is ", m),
-                this.timeoutCircularArray.add(b),
-                this.renderCnt % 25 == 0) {
-                    const C = this.frameCircularArray.getAvg()
-                      , A = this.timeoutCircularArray.getAvg()
-                      , S = this.frameCircularArray.getMax()
-                      , P = this.timeoutCircularArray.getMax();
-                    (T = this.room.stats) == null || T.assign({
-                        avgFrameTime: C,
-                        avgTimeoutTime: A,
-                        maxFrameTime: S,
-                        maxTimeoutTime: P,
-                        systemStuckCnt: this.systemStuckCnt
-                    })
-                }
-                this.renderTimer = window.setTimeout(u, s)
-            }
-            ;
-            this.renderTimer = window.setTimeout(u, n / l)
-        });
+        this._tvs = []
+        this.isRenderFirstFrame = !1
+        this._idleTime = 0
+        this.renderTimer
+        this.lightManager
+        this._checkSceneNotReadyCount = 0
+        this._checkSceneDurationFrameNum = 0
+        this._checkSceneFrameCount = 0
+        this.timeoutCircularArray = new CircularArray(120, !1, [])
+        this.frameCircularArray = new CircularArray(120, !1, [])
+        this.interFrameCircularArray = new CircularArray(120, !1, [])
+        this.drawCallCntCircularArray = new CircularArray(120, !1, [])
+        this.activeFacesCircularArray = new CircularArray(120, !1, [])
+        this.renderTimeCircularArray = new CircularArray(120, !1, [])
+        this.drawCallTimeCircularArray = new CircularArray(120, !1, [])
+        this.animationCircularArray = new CircularArray(120, !1, [])
+        this.meshSelectCircularArray = new CircularArray(120, !1, [])
+        this.renderTargetCircularArray = new CircularArray(120, !1, [])
+        this.regBeforeRenderCircularArray = new CircularArray(120, !1, [])
+        this.regAfterRenderCircularArray = new CircularArray(120, !1, [])
+        this.renderCnt = 0
+        this.renderErrorCount = 0
+        this.engineSloppyCnt = 0
+        this.systemStuckCnt = 0
+        this.frameRenderNumber = 0
 
-        E(this, "updateStats", ()=>{
-            var e;
-            (e = this.room.stats) == null || e.assign({
+        this.room = xverseRoom
+
+        this.updateStats = ()=>{
+            this.room.stats && this.room.stats.assign({
                 renderFrameTime: this.renderTimeCircularArray.getAvg(),
                 maxRenderFrameTime: this.renderTimeCircularArray.getMax(),
                 interFrameTime: this.interFrameCircularArray.getAvg(),
@@ -210,9 +105,125 @@ export default class EngineProxy{
                 maxTimeoutTime: this.timeoutCircularArray.getMax()
             })
         }
-        );
-        this.room = xverseRoom
     }
+
+    _setFPS(sceneManager, fps=25) {
+        logger.info("Set fps to", fps);
+        const protectFps = fps > 60 ? 60 : fps < 24 ? 24 : fps;
+        sceneManager.Engine.stopRenderLoop();
+        const mspf = 1e3 / protectFps; //毫秒每帧
+        let o = Date.now()
+            , a = Date.now()
+            , lastGap = mspf
+            , ratio = 1;
+
+        const updatePerGap = ()=>{
+            const c = Date.now()
+                , h = c - o
+                , f = c - a;
+
+            a = c
+            this.frameCircularArray.add(f)
+            h - lastGap > mspf && (this.systemStuckCnt += 1)
+
+            const d = h / lastGap;
+            ratio = .9 * ratio + .1 * d;
+            const _ = Date.now();
+            let cpuRenderTime = 0, cpuUpdateYUVTime = 0;
+            
+            if (this.room.isUpdatedRawYUVData || this.room.isPano) {
+                this.isRenderFirstFrame = !0
+                if (this._checkSceneDurationFrameNum > 0)
+                {
+                    this._checkSceneFrameCount++,
+                    this.room.sceneManager.isReadyToRender({}) && this._checkSceneDurationFrameNum--,
+                    this._checkSceneFrameCount > EngineProxy._CHECK_DURATION && (this._checkSceneDurationFrameNum = EngineProxy._CHECK_DURATION,
+                    this._checkSceneFrameCount = 0,
+                    this._checkSceneNotReadyCount++,
+
+                    (
+                        this._checkSceneNotReadyCount == 1 || 
+                        this._checkSceneNotReadyCount % 100 == 0
+                    ) && logger.error(`[SDK] Scene not ready, skip render. loop: ${this._checkSceneNotReadyCount}`),
+
+                    this._checkSceneNotReadyCount > 10 && (
+                        logger.error("[SDK] Scene not ready, reload later"),
+                        this.room.proxyEvents("renderError", {
+                            error: new Error("[SDK] Scene not ready, skip render and reload.")
+                        })
+                    ),
+                    this.room.stats.assign({
+                        renderErrorCount: this._checkSceneNotReadyCount
+                    }),
+                    logger.infoAndReportMeasurement({
+                        value: 0,
+                        startTime: Date.now(),
+                        metric: "renderError",
+                        error: new Error("[SDK] Scene not ready, skip render and reload."),
+                        reportOptions: {
+                            sampleRate: .1
+                        }
+                    }));
+                } else {
+                    try {
+                        sceneManager.render()
+                    } 
+                    catch (e) {
+                        this.renderErrorCount++,
+                        this.renderErrorCount > 10 && this.room.proxyEvents("renderError", {
+                            error: e
+                        }),
+                        this.room.stats.assign({
+                            renderErrorCount: this.renderErrorCount
+                        }),
+                        logger.infoAndReportMeasurement({
+                            value: 0,
+                            startTime: Date.now(),
+                            metric: "renderError",
+                            error: e,
+                            reportOptions: {
+                                sampleRate: .1
+                            }
+                        })
+                    }
+                }
+                cpuRenderTime = Date.now() - _,
+                this.frameRenderNumber < 1e3 && this.frameRenderNumber++,
+                this.room.networkController.rtcp.workers.UpdateYUV(),
+                cpuUpdateYUVTime = Date.now() - _ - cpuRenderTime
+            }
+            
+            this.isRenderFirstFrame || this.room.networkController.rtcp.workers.UpdateYUV();
+
+            const usedTimeMs = Date.now() - _;
+            o = c + usedTimeMs,
+            lastGap = Math.min(Math.max((mspf - usedTimeMs) / ratio, 5), 200),
+            usedTimeMs > mspf && (
+                lastGap = 10,
+                this.engineSloppyCnt += 1
+            ),
+            this._idleTime = lastGap;
+
+            lastGap > 150 && console.log("lastGap is ", lastGap, ", ratio is ", ratio, ", usedTimeMs is ", usedTimeMs, 
+                ", cpuRenderTime is ", cpuRenderTime, ", cpuUpdateYUVTime is ", cpuUpdateYUVTime)
+
+            const b = lastGap;
+            this.timeoutCircularArray.add(b)
+            
+            if (this.renderCnt % 25 == 0) {
+                this.room.stats && this.room.stats.assign({
+                    avgFrameTime: this.frameCircularArray.getAvg(),
+                    avgTimeoutTime: this.frameCircularArray.getMax(),
+                    maxFrameTime: this.frameCircularArray.getMax(),
+                    maxTimeoutTime: this.timeoutCircularArray.getMax(),
+                    systemStuckCnt: this.systemStuckCnt
+                })
+            }
+            this.renderTimer = window.setTimeout(updatePerGap, lastGap)
+        }
+        this.renderTimer = window.setTimeout(updatePerGap, mspf / ratio)
+    }
+
     async initEngine(e) {
         await this.updateBillboard();
         logger.info("engine version:", VERSION$1);

+ 77 - 98
src/开发笔记.txt

@@ -1,110 +1,89 @@
 1. Logger不能用单例模式,里面的module对应的是类的名称
 2. BABYLON源码修改:
 
-          this._onBeforeRegisterBeforeRenderObserver = null,
-                this._onAfterRegisterBeforeRenderObserver = null,
-                this._RTT1Time = new Rr.a,
-                this._onBeforeRTT1Observer = null,
-                this._onAfterRTT1Observer = null,
-                this._registerAfterRenderTime = new Rr.a,
-                this._onBeforeRegisterAfterRenderObserver = null,
-                this._onAfterRegisterAfterRenderObserver = null,
-				
-				
-				
-				
-				
-				  this._onBeforeRegisterBeforeRenderObserver = e.onBeforeRunRegisterBeforeRenderObservable.add(function() {
-                    t._registerBeforeRenderTime.beginMonitoring()
-                }),
-                this._onAfterRegisterBeforeRenderObserver = e.onAfterRunRegisterBeforeRenderObservable.add(function() {
-                    t._registerBeforeRenderTime.endMonitoring()
-                }),
-                this._onBeforeRegisterAfterRenderObserver = e.onBeforeRunRegisterAfterRenderObservable.add(function() {
-                    t._registerAfterRenderTime.beginMonitoring()
-                }),
-                this._onAfterRegisterAfterRenderObserver = e.onAfterRunRegisterAfterRenderObservable.add(function() {
-                    t._registerAfterRenderTime.endMonitoring()
-                }),
-                this._onBeforeRTT1Observer = e.onBeforeRTT1Observable.add(function() {
-                    t._RTT1Time.beginMonitoring()
-                }),
-                this._onAfterRTT1Observer = e.onAfterRTT1Observable.add(function() {
-                    t._RTT1Time.endMonitoring()
-                })
-				
-				
-				
-				
-				
-				
-				
-				
-				 Object.defineProperty(e.prototype, "registerBeforeTimeCounter", {
-                get: function() {
-                    return this._registerBeforeRenderTime
-                },
-                enumerable: !1,
-                configurable: !0
-            }),
-            Object.defineProperty(e.prototype, "getRTT1TimeCounter", {
-                get: function() {
-                    return this._RTT1Time
-                },
-                enumerable: !1,
-                configurable: !0
-            }),
-            Object.defineProperty(e.prototype, "registerAfterTimeCounter", {
-                get: function() {
-                    return this._registerAfterRenderTime
-                },
-                enumerable: !1,
-                configurable: !0
-            }),
-			
-			
-			
-			
-			
-			
-			
-			        r.onBeforeRunRegisterBeforeRenderObservable = new Observable,
+        this._onBeforeRegisterBeforeRenderObserver = null,
+        this._onAfterRegisterBeforeRenderObserver = null,
+        this._RTT1Time = new Rr.a,
+        this._onBeforeRTT1Observer = null,
+        this._onAfterRTT1Observer = null,
+        this._registerAfterRenderTime = new Rr.a,
+        this._onBeforeRegisterAfterRenderObserver = null,
+        this._onAfterRegisterAfterRenderObserver = null,
+        
+
+        Object.defineProperty(e.prototype, "registerBeforeTimeCounter", {
+            get: function() {
+                return this._registerBeforeRenderTime
+            },
+            enumerable: !1,
+            configurable: !0
+        }),
+        Object.defineProperty(e.prototype, "getRTT1TimeCounter", {
+            get: function() {
+                return this._RTT1Time
+            },
+            enumerable: !1,
+            configurable: !0
+        }),
+        Object.defineProperty(e.prototype, "registerAfterTimeCounter", {
+            get: function() {
+                return this._registerAfterRenderTime
+            },
+            enumerable: !1,
+            configurable: !0
+        }),
+
+
+		r.onBeforeRunRegisterBeforeRenderObservable = new Observable,
         r.onAfterRunRegisterBeforeRenderObservable = new Observable,
         r.onBeforeRunRegisterAfterRenderObservable = new Observable,
         r.onAfterRunRegisterAfterRenderObservable = new Observable,
         r.onBeforeRTT1Observable = new Observable,
         r.onAfterRTT1Observable = new Observable,
 		
-		
-		
-		
-		
-		                    this.onBeforeRunRegisterBeforeRenderObservable.notifyObservers(this),
-                    this.onBeforeRenderObservable.notifyObservers(this),
-                    this.onAfterRunRegisterBeforeRenderObservable.notifyObservers(this),
-					
-					
-					
-					
-					
-					
-					
-					
-					                    this.onBeforeRunRegisterBeforeRenderObservable.clear(),
-                    this.onAfterRunRegisterBeforeRenderObservable.clear(),
-                    this.onBeforeRTT1Observable.clear(),
-                    this.onAfterRTT1Observable.clear(),
-                    this.onBeforeRunRegisterAfterRenderObservable.clear(),
-                    this.onAfterRunRegisterAfterRenderObservable.clear()
-					
-					
-					
-					
-					
-					
-					
-					
-					        this.scene.onBeforeRunRegisterBeforeRenderObservable.remove(this._onBeforeRegisterBeforeRenderObserver),
+
+
+
+
+
+        this._onBeforeRegisterBeforeRenderObserver = e.onBeforeRunRegisterBeforeRenderObservable.add(function() {
+            t._registerBeforeRenderTime.beginMonitoring()
+        }),
+        this._onAfterRegisterBeforeRenderObserver = e.onAfterRunRegisterBeforeRenderObservable.add(function() {
+            t._registerBeforeRenderTime.endMonitoring()
+        }),
+        this._onBeforeRegisterAfterRenderObserver = e.onBeforeRunRegisterAfterRenderObservable.add(function() {
+            t._registerAfterRenderTime.beginMonitoring()
+        }),
+        this._onAfterRegisterAfterRenderObserver = e.onAfterRunRegisterAfterRenderObservable.add(function() {
+            t._registerAfterRenderTime.endMonitoring()
+        }),
+        this._onBeforeRTT1Observer = e.onBeforeRTT1Observable.add(function() {
+            t._RTT1Time.beginMonitoring()
+        }),
+        this._onAfterRTT1Observer = e.onAfterRTT1Observable.add(function() {
+            t._RTT1Time.endMonitoring()
+        })
+
+
+        this.onBeforeRunRegisterBeforeRenderObservable.notifyObservers(this),
+        this.onBeforeRenderObservable.notifyObservers(this),
+        this.onAfterRunRegisterBeforeRenderObservable.notifyObservers(this),
+
+
+
+
+
+
+
+        this.onBeforeRunRegisterBeforeRenderObservable.clear(),
+        this.onAfterRunRegisterBeforeRenderObservable.clear(),
+        this.onBeforeRTT1Observable.clear(),
+        this.onAfterRTT1Observable.clear(),
+        this.onBeforeRunRegisterAfterRenderObservable.clear(),
+        this.onAfterRunRegisterAfterRenderObservable.clear()
+
+		this.scene.onBeforeRunRegisterBeforeRenderObservable.remove(this._onBeforeRegisterBeforeRenderObserver),
         this._onBeforeRegisterBeforeRenderObserver = null,
         this.scene.onAfterRunRegisterBeforeRenderObservable.remove(this._onAfterRegisterBeforeRenderObserver),
         this._onAfterRegisterBeforeRenderObserver = null,