gemercheung 3 years ago
parent
commit
582ee36270
1 changed files with 86 additions and 80 deletions
  1. 86 80
      src/scene/scene.service.ts

+ 86 - 80
src/scene/scene.service.ts

@@ -26,14 +26,13 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     private rotateService: RotateService,
     private moveService: MoveService,
     private getRouterService: GetRouterService, // @InjectQueue('rotate') private rotateQueue: Queue, // @InjectQueue('walking') private walkingQueue: Queue,
-  ) {}
+  ) { }
   @Client(grpcClientOptions) private readonly client: ClientGrpc;
 
   public _frameInteval: NodeJS.Timeout;
   public _frameTimeout: NodeJS.Timeout;
   public _rotateTimeout: NodeJS.Timeout;
   public _moveTimeout: NodeJS.Timeout;
-  // public _JoyStickingTimeout: NodeJS.Timeout;
   public _JoyStickingSteamTimeout: NodeJS.Timeout;
   public startSteaming = new BehaviorSubject<boolean>(false);
   public onRotating = new BehaviorSubject<boolean>(false);
@@ -46,7 +45,6 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   private rotateFirstIDR = true;
   private rotateStopThrottle = false; //防止多次瞬间解触发
 
-  private sceneGrpcService: SceneGrpcService;
   private channel: DataChannel;
   private peer: PeerConnection;
   private logger: Logger = new Logger('SceneService');
@@ -62,8 +60,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   private roQueueSubscription: any;
   private moveQueueSubscription: any;
   private walkingSub: any;
-  private joystickSub: any;
-  private clickQueueSub: any;
+
   private _rotateCurrentFame = -1;
   private _rotateCount = -1;
 
@@ -75,9 +72,6 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   private moveQueue: RxQueue = new DelayQueue(
     Number(this.configService.get('queueConfig.move')) || 20,
   );
-  // private joystickQueue: RxQueue = new DelayQueue(
-  //   Number(this.configService.get('queueConfig.joystick')) || 10,
-  // );
   private requestIFrameQueue: RxQueue = new DebounceQueue(2000);
 
   private requestIFrameQueueSub: any;
@@ -110,10 +104,10 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
   public sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
 
+  private globalOptLock = false;
+
+
   onModuleInit(): void {
-    this.sceneGrpcService =
-      this.client.getService<SceneGrpcService>('SceneGrpcService');
-    this.logger.log('init SceneGrpcService');
     this.streamServiceSub = this.streamService.onSteaming.subscribe((val) => {
       this.onSteaming = val;
     });
@@ -227,7 +221,9 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
   async handleRotate(request) {
     // try {
-    if (this.firstRender) {
+    const rotateKey = this.firstRender && !this.globalOptLock;
+    console.log('handleRotate条件--->' + rotateKey);
+    if (rotateKey) {
       if (!this.roQueueSubscription) {
         this.handleRotateStream();
       }
@@ -272,16 +268,16 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
         const playerAngle = newUserStates.playerState.player.angle;
         this.logger.log(
           'stop-data-0' +
-            'trace_id: ' +
-            trace_id +
-            'userId:' +
-            userId +
-            'breakPointId :' +
-            breakPointId +
-            'cameraAngle :' +
-            JSON.stringify(cameraAngle) +
-            'playerAngle: ' +
-            JSON.stringify(playerAngle),
+          'trace_id: ' +
+          trace_id +
+          'userId:' +
+          userId +
+          'breakPointId :' +
+          breakPointId +
+          'cameraAngle :' +
+          JSON.stringify(cameraAngle) +
+          'playerAngle: ' +
+          JSON.stringify(playerAngle),
         );
         //debugger;
         console.log('moveService.stop-1:' + breakPointId);
@@ -330,7 +326,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
             const rounded = Number(inMillSeconds).toFixed(3);
             this.logger.log(
               `[timer]-rotate-入队列前: ${rounded}ms -->` +
-                JSON.stringify(stream),
+              JSON.stringify(stream),
             );
 
             this.roQueue.next(stream);
@@ -339,6 +335,11 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           }
         }
       }
+    } else {
+      if (this.roQueueSubscription) {
+        this.roQueueSubscription.unsubscribe();
+        this.roQueueSubscription = null;
+      }
     }
     // } catch (error) {
     //   this.logger.error('rotate', error.message);
@@ -434,15 +435,15 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
       console.log('进入1 - searchRoad');
       this.logger.log(
         'handleWalking-users' +
-          JSON.stringify(this.moveService.users) +
-          ' this.user_id: ' +
-          this.user_id,
+        JSON.stringify(this.moveService.users) +
+        ' this.user_id: ' +
+        this.user_id,
       );
       this.logger.log(
         'handleWalking-currentUser' +
-          JSON.stringify(user) +
-          ' this.user_id: ' +
-          this.user_id,
+        JSON.stringify(user) +
+        ' this.user_id: ' +
+        this.user_id,
       );
       console.log('path-start' + user.breakPointId);
 
@@ -474,7 +475,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           walkingRes[0].forEach((item: StreamReplyType, index: number) => {
             item.mType = 'rotate';
             // item.DIR = index === 0 ? 1 : 3;
-            const IDRflag = index % 5 === 0 ? 1 : 3;
+            const IDRflag = (index + 1) % 5 === 0 ? 1 : 3;
             const dir = this.isHeaderOrLast(index, walkingRes[0].length - 1);
             item.DIR = dir ? 1 : IDRflag;
           });
@@ -512,16 +513,16 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
         if (seqs?.length) {
           this.logger.log(
             'walking --队列总览:' +
-              ' 总段数: ' +
-              walkingRes.length +
-              ' 镜头帧数:' +
-              walkingRes[0].length +
-              ' 行走段数:' +
-              (walkingRes[0]?.length
-                ? walkingRes.length - 1
-                : walkingRes.length) +
-              ' 队列总帧数:' +
-              seqs.length,
+            ' 总段数: ' +
+            walkingRes.length +
+            ' 镜头帧数:' +
+            walkingRes[0].length +
+            ' 行走段数:' +
+            (walkingRes[0]?.length
+              ? walkingRes.length - 1
+              : walkingRes.length) +
+            ' 队列总帧数:' +
+            seqs.length,
           );
           const stop = performance.now();
           const inMillSeconds = stop - start;
@@ -607,8 +608,8 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     const hasPush = hasMedia
       ? await this.streamService.pushFrameToSteam(streamData as StreamFrameType)
       : await this.streamService.pushMetaDataToSteam(
-          streamData as StreamMetaType,
-        );
+        streamData as StreamMetaType,
+      );
 
     if (hasPush.done) {
       this.isJoystickHasStream = true;
@@ -643,11 +644,13 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
         if (complementFrame) {
           // 第二次或N次进入时如果有值直接重新进入流主程
           this.handlePushJoyStickSteam(complementFrame);
+          this.globalOptLock = true;
         } else {
           // 第二次或N次无pool数据再次trigger handleJoystickStop
           console.log('gemer-test-complementFrame-空2');
           this.hasJoystickFocusRepeat = false;
           this.testTimer = 0;
+          this.globalOptLock = false;
           this.handleJoystickStop(hasPush);
         }
       } else {
@@ -673,6 +676,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
       console.log('gemer-test-complementFrame', complementFrame);
       if (complementFrame) {
         this.hasJoystickFocusRepeat = true;
+        this.globalOptLock = true;
         this.testTimer += 1;
         console.log('gemer-test-complementFrame-有值');
         const start = performance.now();
@@ -706,7 +710,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
       const joystickRes = await this.moveService.seqExeJoystick(request);
       this.logger.log(
         'joystick-breakPointId:' +
-          this.moveService.users[this.user_id].breakPointId,
+        this.moveService.users[this.user_id].breakPointId,
       );
       // 有数据 [0]是rotate数据,[1-infinity]是walking数据
       this.logger.log('joystickRes', JSON.stringify(joystickRes));
@@ -715,11 +719,11 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
         this.onJoysticking.next(true);
         console.log(
           'handlejoysticktesttest:' +
-            joystickRes.mediaSrc +
-            ',相机坐标:' +
-            JSON.stringify(
-              joystickRes.newUserStates[0].playerState.player.position,
-            ),
+          joystickRes.mediaSrc +
+          ',相机坐标:' +
+          JSON.stringify(
+            joystickRes.newUserStates[0].playerState.player.position,
+          ),
         );
         if (!this.onMoving.getValue()) {
           this.handlePushJoyStickSteam(joystickRes);
@@ -808,14 +812,14 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           };
           this.logger.log(
             '[media-move]: ' +
-              ', moveframeCnt: ' +
-              this.moveframeCnt +
-              ', clipPath: ' +
-              stream.clipPath +
-              ', mType: ' +
-              stream.mType +
-              ', DIR: ' +
-              stream.DIR,
+            ', moveframeCnt: ' +
+            this.moveframeCnt +
+            ', clipPath: ' +
+            stream.clipPath +
+            ', mType: ' +
+            stream.mType +
+            ', DIR: ' +
+            stream.DIR,
             // stream.metaData,
           );
           this.logger.log(
@@ -825,6 +829,8 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           // 记录lastMoveStreamFrame给打断逻辑使用
           this.lastMoveStreamFrame.next(streamData);
           // this.lastMoveStreamFrameBk = streamData;
+          this.holdSteam();
+          this.globalOptLock = true;
           const res = await this.streamService.pushFrameToSteam(streamData);
 
           const isLastFrameIndex = this.lastMovingPointArray.findIndex(
@@ -857,6 +863,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
               this.lastMovingPointArray = [];
               this.hasJoystickMoveRequest = false;
               this.cleanMoveSteam();
+              this.globalOptLock = false;
               this.resumeStream();
               this.logger.log('move end');
             }, 200);
@@ -1011,8 +1018,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
           const IDRflag = this._rotateCurrentFame % 5 === 0 ? 1 : 3;
           this.logger.log(
-            `当前rotate ,mainframeCnt:${this.frameCnt.getValue()}, _rotateCurrentFame:${
-              this._rotateCurrentFame
+            `当前rotate ,mainframeCnt:${this.frameCnt.getValue()}, _rotateCurrentFame:${this._rotateCurrentFame
             } IDRflag:${IDRflag}`,
           );
           stream.DIR = this.rotateFirstIDR ? 1 : IDRflag;
@@ -1023,12 +1029,12 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
           this.logger.log(
             '[media-rotate]: ' +
-              ', frame: ' +
-              stream.frame +
-              ', rotateframeCnt: ' +
-              this.rotateframeCnt +
-              ', clipPath: ' +
-              stream.clipPath,
+            ', frame: ' +
+            stream.frame +
+            ', rotateframeCnt: ' +
+            this.rotateframeCnt +
+            ', clipPath: ' +
+            stream.clipPath,
             // stream.metaData,
           );
           // this.logger.log(
@@ -1090,12 +1096,12 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           this.logger.log('frame', frame);
           console.log(
             'mock' +
-              ' maxMessageSize: ' +
-              this.channel.maxMessageSize() +
-              ' bytesReceived: ' +
-              this.peer.bytesReceived() +
-              ' bytesSent: ' +
-              this.peer.bytesSent(),
+            ' maxMessageSize: ' +
+            this.channel.maxMessageSize() +
+            ' bytesReceived: ' +
+            this.peer.bytesReceived() +
+            ' bytesSent: ' +
+            this.peer.bytesSent(),
           );
           if (frame === 1) {
             redisData = await this.rotateService.echo(this.user_id, true);
@@ -1110,7 +1116,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
                 delete redisData.mediaSrc;
                 this.logger.log(
                   `user:${this.user_id}:first render stream` +
-                    JSON.stringify({ path: clipPath, meta: redisData }),
+                  JSON.stringify({ path: clipPath, meta: redisData }),
                 );
                 const status = await this.pushFirstRender(
                   clipPath,
@@ -1138,15 +1144,15 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
             console.log(
               '空白流条件-->:' +
-                isOk +
-                ' onMoving: ' +
-                this.onMoving.value +
-                ' onRotating: ' +
-                this.onRotating.value +
-                ' onJoysticking: ' +
-                this.onJoysticking.value +
-                ' firstRender: ' +
-                this.firstRender,
+              isOk +
+              ' onMoving: ' +
+              this.onMoving.value +
+              ' onRotating: ' +
+              this.onRotating.value +
+              ' onJoysticking: ' +
+              this.onJoysticking.value +
+              ' firstRender: ' +
+              this.firstRender,
             );
           }