test pc 3 年之前
父節點
當前提交
bedd53f60e
共有 1 個文件被更改,包括 131 次插入118 次删除
  1. 131 118
      src/scene/scene.service.ts

+ 131 - 118
src/scene/scene.service.ts

@@ -38,7 +38,7 @@ 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;
@@ -121,6 +121,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   public sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
 
   private globalOptLock = false;
+  private isStopJointing = false;
 
   onModuleInit(): void {
     this.streamServiceSub = this.streamService.onSteaming.subscribe((val) => {
@@ -261,6 +262,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     console.log('rotateUnlock条件--->' + rotateUnlock, this.globalOptLock);
 
     if (rotateUnlock) {
+      //console.log('20220627test:handleRotate')
       const start = performance.now();
       // 当move时处理 _rotateCount是移动端同时触发的问题,rotateStopThrottle是减少重复抖动stop的处理。
       this.holdSteam();
@@ -287,7 +289,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),
             );
             if (!this.stopRotated) {
               await this.seqExehandleRotateStream(stream);
@@ -337,16 +339,16 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     stream.DIR = this.rotateFirstIDR ? 1 : IDRflag;
 
     console.log(
-      '[旋转信息:::info--->]:clipPath: %s, main-frameCnt: %s, stream.frame %s ,_rotateStartFame: %s, _rotateCountFame: %s, IDRflag: %s',
+      '[旋转信息:::info--->]:clipPath: %s, main-frameCnt: %s, stream.frame %s ,_rotateStartFame: %s, _rotateCountFame: %s, IDRflag: %s,time: %s',
       stream.clipPath,
       // this._rotateStartFame.value,
       this.frameCnt.value,
       stream.frame,
       this._rotateStartFame.value,
       this._rotateCountFame,
-
       // this.rotateframeCnt,
       IDRflag,
+      new Date().getTime(),
     );
     const res = await this.streamService.pushFrameToSteam(stream);
     if (res.done) {
@@ -452,6 +454,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
    */
   async handleRotateOrWalkingStop(request): Promise<boolean> {
     this.rotateStopThrottle = true;
+    this.isStopJointing = true
     const lastStreamFrame = this.lastMoveStreamFrame.getValue();
     this.logger.log(
       'handleRotateOrWalkingStop-frame',
@@ -482,19 +485,20 @@ 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);
+    //console.log('20220627test:handleRotateOrWalkingStop-stop');
     const redisMeta = await this.moveService.stop(
       trace_id,
       userId,
@@ -516,7 +520,9 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
       //推最后一个 STOP Frame
       const hasPush = await this.streamService.pushFrameToSteam(streamData);
       if (hasPush.done) {
+        //console.log('20220627test:handleRotateOrWalkingStop-stop:'+streamData.clipPath+'**'+streamData.frame);
         this.frameCnt.next(hasPush.frame);
+        this.isStopJointing = false
         // this.onMoving.next(false);
         // this.cleanMoveSteam();
         return Promise.resolve(true);
@@ -694,15 +700,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);
 
@@ -757,16 +763,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;
@@ -821,7 +827,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     //console.log('joystickRes有mediaSrc', joystickRes.mediaSrc);
     console.log(
       'handlejoystick-angle->相机角度-------------------------:' +
-        joystickRes['newUserStates'][0].playerState.camera.angle.yaw,
+      joystickRes['newUserStates'][0].playerState.camera.angle.yaw,
     );
     let streamData: StreamFrameType | StreamMetaType;
 
@@ -856,8 +862,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;
@@ -958,7 +964,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));
@@ -1024,22 +1030,23 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     this.moveQueueSubscription = this.moveQueue.subscribe(
       async (stream: StreamFrameType) => {
         try {
-          const metaData: StreamReplyType = JSON.parse(stream.metaData);
-          if (this.moveframeCnt === -1) {
-            this.moveframeCnt = this.frameCnt.getValue();
-          }
-          this.moveframeCnt += 1;
-          // this.latestBreakPointId = metaData.endBreakPointId;
-
-          const streamData: StreamFrameType = {
-            frame: this.moveframeCnt,
-            clipPath: stream.clipPath,
-            metaData: stream.metaData,
-            serverTime: this.mockserverTime,
-            DIR: stream.DIR,
-          };
-          this.logger.log(
-            '[media-move]: ' +
+          if (!this.isStopJointing) {
+            const metaData: StreamReplyType = JSON.parse(stream.metaData);
+            if (this.moveframeCnt === -1) {
+              this.moveframeCnt = this.frameCnt.getValue();
+            }
+            this.moveframeCnt += 1;
+            // this.latestBreakPointId = metaData.endBreakPointId;
+
+            const streamData: StreamFrameType = {
+              frame: this.moveframeCnt,
+              clipPath: stream.clipPath,
+              metaData: stream.metaData,
+              serverTime: this.mockserverTime,
+              DIR: stream.DIR,
+            };
+            this.logger.log(
+              '[media-move]: ' +
               ', moveframeCnt: ' +
               this.moveframeCnt +
               ', clipPath: ' +
@@ -1048,57 +1055,61 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
               stream.mType +
               ', DIR: ' +
               stream.DIR,
-            // stream.metaData,
-          );
-          this.logger.log(
-            '[media-move-lastMovingPointArray]',
-            this.lastMovingPointArray?.length,
-          );
-          // 记录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(
-            (item) => item.mediaSrc === metaData.mediaSrc,
-          );
-          // this.logger.log('path-update-index', isLastFrameIndex);
-          //关节点入库
-          if (isLastFrameIndex > -1) {
-            //this.logger.log('path-update-array', this.lastMovingPointArray);
-            const currentMeta = this.lastMovingPointArray[isLastFrameIndex];
-            const userId = this.user_id;
-            const breakPointId = currentMeta.metaData.endBreakPointId;
-            const lastReply = currentMeta.metaData;
-            this.moveService.updateUser(userId, breakPointId, lastReply);
-            this.lastMovingPointArray.splice(isLastFrameIndex, 1);
-            this.moveSliceLastFrame.next(currentMeta);
-          }
+              // stream.metaData,
+            );
+            this.logger.log(
+              '[media-move-lastMovingPointArray]',
+              this.lastMovingPointArray?.length,
+            );
+            // 记录lastMoveStreamFrame给打断逻辑使用
+            this.lastMoveStreamFrame.next(streamData);
+            // this.lastMoveStreamFrameBk = streamData;
+            this.holdSteam();
+            // this.globalOptLock = true;
+            //console.log('20220627test:handleMoveSteam:' + stream.clipPath)
+            const res = await this.streamService.pushFrameToSteam(streamData);
 
-          if (res.done) {
-            clearTimeout(this._moveTimeout);
-            this._moveTimeout = setTimeout(() => {
-              this.logger.log('move 交权给空流,当前pts', res.frame);
-              this.rewalking = false;
-              this.frameCnt.next(res.frame);
-              this.rotateframeCnt = -1;
-              this.onMoving.next(false);
-              this.onJoysticking.next(false);
-              this.lastMovingPointArray = [];
-              this.hasJoystickMoveRequest = false;
-              this.cleanMoveSteam();
-              this.globalOptLock = false;
-              this.resumeStream();
-              this.logger.log('move end');
-            }, 200);
-          } else {
-            console.error('流地址有误::', res.frame, JSON.stringify(res));
-            this.logger.error(
-              `movesteam::当前帧:${res.frame}` + JSON.stringify(res),
+            const isLastFrameIndex = this.lastMovingPointArray.findIndex(
+              (item) => item.mediaSrc === metaData.mediaSrc,
             );
-            this.resumeStream();
+            // this.logger.log('path-update-index', isLastFrameIndex);
+
+            if (res.done) {
+              this.frameCnt.next(res.frame)
+              //关节点入库
+              if (isLastFrameIndex > -1) {
+                //this.logger.log('path-update-array', this.lastMovingPointArray);
+                const currentMeta = this.lastMovingPointArray[isLastFrameIndex];
+                const userId = this.user_id;
+                const breakPointId = currentMeta.metaData.endBreakPointId;
+                const lastReply = currentMeta.metaData;
+                this.moveService.updateUser(userId, breakPointId, lastReply);
+                this.lastMovingPointArray.splice(isLastFrameIndex, 1);
+                this.moveSliceLastFrame.next(currentMeta);
+              }
+
+              clearTimeout(this._moveTimeout);
+              this._moveTimeout = setTimeout(() => {
+                this.logger.log('move 交权给空流,当前pts', res.frame);
+                this.rewalking = false;
+                this.frameCnt.next(res.frame);
+                this.rotateframeCnt = -1;
+                this.onMoving.next(false);
+                this.onJoysticking.next(false);
+                this.lastMovingPointArray = [];
+                this.hasJoystickMoveRequest = false;
+                this.cleanMoveSteam();
+                this.globalOptLock = false;
+                this.resumeStream();
+                this.logger.log('move end');
+              }, 200);
+            } else {
+              console.error('流地址有误::', res.frame, JSON.stringify(res));
+              this.logger.error(
+                `movesteam::当前帧:${res.frame}` + JSON.stringify(res),
+              );
+              this.resumeStream();
+            }
           }
         } catch (error) {
           this.logger.error('handleMoveSteam::error', error);
@@ -1295,12 +1306,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(),
             ' state: ' + this.peer.state(),
           );
           if (frame === 1) {
@@ -1322,7 +1333,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,
@@ -1346,21 +1357,22 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
               !this.onMoving.value &&
               !this.onRotating.value &&
               !this.onJoysticking.value &&
+              !this.onSteaming &&
               this.firstRender;
 
             console.log(
               '空白流条件-->:' +
-                isOk +
-                ' onMoving: ' +
-                this.onMoving.value +
-                ' onRotating: ' +
-                this.onRotating.value +
-                ' onJoysticking: ' +
-                this.onJoysticking.value +
-                ' onSteaming: ' +
-                this.onSteaming +
-                ' firstRender: ' +
-                this.firstRender,
+              isOk +
+              ' onMoving: ' +
+              this.onMoving.value +
+              ' onRotating: ' +
+              this.onRotating.value +
+              ' onJoysticking: ' +
+              this.onJoysticking.value +
+              ' onSteaming: ' +
+              this.onSteaming +
+              ' firstRender: ' +
+              this.firstRender,
             );
           }
 
@@ -1369,9 +1381,10 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
             !this.onMoving.value &&
             !this.onRotating.value &&
             !this.onJoysticking.value &&
-            this.onSteaming &&
+            !this.onSteaming &&
             this.firstRender
           ) {
+            // debugger
             const redisDataAuto = await this.rotateService.echo(
               this.user_id,
               false,