gemercheung 3 years ago
parent
commit
a2db538e88

+ 227 - 229
src/move/move.service.ts

@@ -436,9 +436,9 @@ export class MoveService implements OnModuleInit {
     const user = this.users[userId];
     user.breakPointId = breakPointId;
 
-    if(lastReply.actionResponses[0].actionType != 15)
-    {
-      user.player.position = lastReply['newUserStates'][0].playerState.player.position;
+    if (lastReply.actionResponses[0].actionType != 15) {
+      user.player.position =
+        lastReply['newUserStates'][0].playerState.player.position;
     }
     user.player.angle = lastReply['newUserStates'][0].playerState.player.angle;
     user.camera.position =
@@ -683,7 +683,6 @@ export class MoveService implements OnModuleInit {
   //         neighPoints.push(neighPoint);
   //         ++count;
   //       }
-        
 
   //       if(Math.abs(angle - move_angle)<45||Math.abs(angle+360 - move_angle)<45){
   //         if(singleInfo == null){
@@ -701,7 +700,7 @@ export class MoveService implements OnModuleInit {
   //       this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
   //         JSON.stringify(playerPosition),
   //       );
-  //     } 
+  //     }
   //     else{
   //       if(singleInfo != null){
   //         return await this.moveDirect(
@@ -780,72 +779,119 @@ export class MoveService implements OnModuleInit {
   async joystick(actionRequest) {
     //try {
 
+    if (this.sendingFrameForJoystick) {
+      return null;
+    }
 
-      if(this.sendingFrameForJoystick){
-        return null;
-      }
-
-      const userId = actionRequest['user_id'];
-      const traceId = actionRequest['trace_id'];
-      const dir_action = actionRequest['dir_action'];
-      const actionType = actionRequest['action_type'];
-      const user = this.users[userId];
-      const breakPointId = user.breakPointId;
-      const appId = user.appId;
+    const userId = actionRequest['user_id'];
+    const traceId = actionRequest['trace_id'];
+    const dir_action = actionRequest['dir_action'];
+    const actionType = actionRequest['action_type'];
+    const user = this.users[userId];
+    const breakPointId = user.breakPointId;
+    const appId = user.appId;
 
-      const step = 0.3;
-      const closestDis = 85; //小于这个距离就跳到邻居呼吸点
-      const distance = step * dir_action.speed_level;
-      let angle = null;
-      let move_angle = dir_action.move_angle + user.camera.angle.yaw;
-      move_angle = move_angle % 360;
-
-      //TODO 临时增加断言
-      const playerPosition: Point = { x: 0, y: 0, z: 0 };
-      playerPosition.x =
-        user.player.position.x +
-        distance * Math.cos((move_angle / 360) * 2 * Math.PI);
-      playerPosition.y =
-        user.player.position.y +
-        distance * Math.sin((move_angle / 360) * 2 * Math.PI);
-
-      //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
-      let chooseBreakPointId = null;
-      const breakPoint = this.breakPointInfo[breakPointId];
-      const surroundPointIds = breakPoint.contact;
-      const traceIds = [];
+    const step = 0.3;
+    const closestDis = 85; //小于这个距离就跳到邻居呼吸点
+    const distance = step * dir_action.speed_level;
+    let angle = null;
+    let move_angle = dir_action.move_angle + user.camera.angle.yaw;
+    move_angle = move_angle % 360;
+
+    //TODO 临时增加断言
+    const playerPosition: Point = { x: 0, y: 0, z: 0 };
+    playerPosition.x =
+      user.player.position.x +
+      distance * Math.cos((move_angle / 360) * 2 * Math.PI);
+    playerPosition.y =
+      user.player.position.y +
+      distance * Math.sin((move_angle / 360) * 2 * Math.PI);
+
+    //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
+    let chooseBreakPointId = null;
+    const breakPoint = this.breakPointInfo[breakPointId];
+    const surroundPointIds = breakPoint.contact;
+    const traceIds = [];
+
+    user.player.angle.yaw = move_angle;
+    traceIds.push(traceId);
+    this.reply.traceIds = traceIds;
+    this.reply['newUserStates'][0].userId = userId;
+    this.reply['actionResponses'][0].traceId = traceId;
+    this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
+
+    this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
+      JSON.stringify(user.camera.angle),
+    );
+    this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
+      JSON.stringify(user.camera.position),
+    );
+    this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
+      JSON.stringify(breakPoint.position),
+    );
+    this.reply['newUserStates'][0].renderInfo.isMoving = 1;
+    this.reply['actionResponses'][0].traceId = traceId;
+    this.reply.mediaSrc = null;
 
-      user.player.angle.yaw = move_angle;
-      traceIds.push(traceId);
-      this.reply.traceIds = traceIds;
-      this.reply['newUserStates'][0].userId = userId;
-      this.reply['actionResponses'][0].traceId = traceId;
-      this.reply['newUserStates'][0].playerState.player.angle.yaw = move_angle;
+    console.log('joystickjoystick:' + this.cameraInfos.length);
 
-      this.reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
-        JSON.stringify(user.camera.angle),
-      );
-      this.reply['newUserStates'][0].playerState.camera.position = JSON.parse(
-        JSON.stringify(user.camera.position),
+    if (surroundPointIds.length == 1) {
+      console.log(
+        'joystick校验--->' + breakPointId + '-' + surroundPointIds[0],
       );
-      this.reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
-        JSON.stringify(breakPoint.position),
+      return await this.moveDirect(
+        playerPosition,
+        closestDis,
+        breakPointId,
+        surroundPointIds[0],
+        appId,
+        userId,
+        traceId,
+        actionType,
       );
-      this.reply['newUserStates'][0].renderInfo.isMoving = 1;
-      this.reply['actionResponses'][0].traceId = traceId;
-      this.reply.mediaSrc = null;
+    }
 
-      console.log('joystickjoystick:'+this.cameraInfos.length);
+    let count = 0;
+    const neighPoints = [];
+    //人在哪个角度
+    let _angle = this.getAngle(
+      breakPoint.position,
+      { x: breakPoint.position.x + 1, y: breakPoint.position.y },
+      playerPosition,
+    );
+    if (_angle < 0) {
+      _angle += 360;
+    }
+
+    let singleInfo = null;
+    for (let i = 0; i < surroundPointIds.length; ++i) {
+      const neighPoint = this.breakPointInfo[surroundPointIds[i]];
+      neighPoint.breakPointId = surroundPointIds[i];
+      angle = this.getAngle(
+        breakPoint.position,
+        { x: breakPoint.position.x + 1, y: breakPoint.position.y },
+        neighPoint.position,
+      );
 
-      if (surroundPointIds.length == 1) {
+      //if(angle<45&&angle!=0){
+      if (
+        // Math.abs(angle - move_angle) < 45 &&
+        // Math.abs(angle - move_angle) != 0
+        Math.abs(angle - _angle) < 45 &&
+        Math.abs(angle - _angle) != 0
+      ) {
+        neighPoint.angle = angle;
+        neighPoints.push(neighPoint);
+        ++count;
+      } else if (Math.abs(angle - move_angle) == 0) {
         console.log(
-          'joystick校验--->' + breakPointId + '-' + surroundPointIds[0],
+          'joystick直走--->' + breakPointId + '-' + surroundPointIds[i],
         );
         return await this.moveDirect(
           playerPosition,
           closestDis,
           breakPointId,
-          surroundPointIds[0],
+          surroundPointIds[i],
           appId,
           userId,
           traceId,
@@ -853,190 +899,141 @@ export class MoveService implements OnModuleInit {
         );
       }
 
-      let count = 0;
-      const neighPoints = [];
-      //人在哪个角度
-      let _angle = this.getAngle(
-        breakPoint.position,
-        { x: breakPoint.position.x + 1, y: breakPoint.position.y },
-        playerPosition,
-      );
-      if (_angle < 0) {
-        _angle += 360;
+      //if (angle == 0 && Math.abs(360 - move_angle) < 45) {
+      if (angle == 0 && Math.abs(360 - _angle) < 45) {
+        neighPoint.angle = angle;
+        neighPoints.push(neighPoint);
+        ++count;
       }
 
-      let singleInfo = null;
-      for (let i = 0; i < surroundPointIds.length; ++i) {
-        const neighPoint = this.breakPointInfo[surroundPointIds[i]];
-        neighPoint.breakPointId = surroundPointIds[i];
-        angle = this.getAngle(
-          breakPoint.position,
-          { x: breakPoint.position.x + 1, y: breakPoint.position.y },
-          neighPoint.position,
-        );
-
-        //if(angle<45&&angle!=0){
-        if (
-          // Math.abs(angle - move_angle) < 45 &&
-          // Math.abs(angle - move_angle) != 0
-          Math.abs(angle - _angle) < 45 &&
-          Math.abs(angle - _angle) != 0
-        ) {
-          neighPoint.angle = angle;
-          neighPoints.push(neighPoint);
-          ++count;
-        } else if (Math.abs(angle - move_angle) == 0) {
-          console.log(
-            'joystick直走--->' + breakPointId + '-' + surroundPointIds[i],
-          );
-          return await this.moveDirect(
-            playerPosition,
-            closestDis,
-            breakPointId,
-            surroundPointIds[i],
-            appId,
-            userId,
-            traceId,
-            actionType,
-          );
-        }
-
-        //if (angle == 0 && Math.abs(360 - move_angle) < 45) {
-        if (angle == 0 && Math.abs(360 - _angle) < 45) {
-          neighPoint.angle = angle;
-          neighPoints.push(neighPoint);
-          ++count;
-        }
-
-        if (
-          Math.abs(angle - move_angle) < 45 ||
-          Math.abs(angle + 360 - move_angle) < 45
-        ) {
-          if (singleInfo == null) {
-            singleInfo = {
-              angle: angle,
-              breakPointId: surroundPointIds[i],
-            };
-          }
+      if (
+        Math.abs(angle - move_angle) < 45 ||
+        Math.abs(angle + 360 - move_angle) < 45
+      ) {
+        if (singleInfo == null) {
+          singleInfo = {
+            angle: angle,
+            breakPointId: surroundPointIds[i],
+          };
         }
       }
+    }
 
-      if (count == 2) {
-        //人物移动
-        user.player.position = JSON.parse(JSON.stringify(playerPosition));
-        this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
-          JSON.stringify(playerPosition),
+    if (count == 2) {
+      //人物移动
+      user.player.position = JSON.parse(JSON.stringify(playerPosition));
+      this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
+        JSON.stringify(playerPosition),
+      );
+    } else {
+      if (singleInfo != null) {
+        console.log(
+          'joystick校验--->' + breakPointId + '-' + singleInfo.breakPointId,
         );
-      } else {
-        if (singleInfo != null) {
-          console.log(
-            'joystick校验--->' + breakPointId + '-' + singleInfo.breakPointId,
-          );
-          return await this.moveDirect(
-            playerPosition,
-            closestDis,
-            breakPointId,
-            singleInfo.breakPointId,
-            appId,
-            userId,
-            traceId,
-            actionType,
-          );
-        } else if (count == 1) {
-          console.log(
-            'joystick校验--->' +
-              breakPointId +
-              '-' +
-              neighPoints[0].breakPointId,
-          );
-          return await this.moveDirect(
-            playerPosition,
-            closestDis,
-            breakPointId,
-            neighPoints[0].breakPointId,
-            appId,
-            userId,
-            traceId,
-            actionType,
-          );
-        } else if (count == 0) {
-          this.reply['newUserStates'][0].playerState.player.position =
-            JSON.parse(JSON.stringify(user.player.position));
-          this.reply.actionResponses[0].actionType = actionType;
-          return this.reply;
-        }
-      }
-
-      //count == 2
-      //超出范围了
-      if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
-        let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
-        if (neighPoints[0].angle == 0) {
-          offsetAngle1 = Math.min(offsetAngle1, Math.abs(_angle - 360));
-        }
-
-        let offsetAngle2 = Math.abs(_angle - neighPoints[1].angle);
-        if (neighPoints[1].angle == 0) {
-          offsetAngle2 = Math.min(offsetAngle2, Math.abs(_angle - 360));
-        }
-
-        if (offsetAngle1 > offsetAngle2) {
-          chooseBreakPointId = neighPoints[1].breakPointId;
-        } else {
-          chooseBreakPointId = neighPoints[0].breakPointId;
-        }
+        return await this.moveDirect(
+          playerPosition,
+          closestDis,
+          breakPointId,
+          singleInfo.breakPointId,
+          appId,
+          userId,
+          traceId,
+          actionType,
+        );
+      } else if (count == 1) {
         console.log(
-          'joystick镜头过渡--->' + breakPointId + '-' + chooseBreakPointId,
+          'joystick校验--->' + breakPointId + '-' + neighPoints[0].breakPointId,
         );
-        return await this.moveCamera(
+        return await this.moveDirect(
+          playerPosition,
+          closestDis,
           breakPointId,
-          chooseBreakPointId,
+          neighPoints[0].breakPointId,
           appId,
           userId,
           traceId,
           actionType,
         );
-      } else {
-        user.player.position = JSON.parse(JSON.stringify(playerPosition));
+      } else if (count == 0) {
         this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
           JSON.stringify(user.player.position),
         );
         this.reply.actionResponses[0].actionType = actionType;
-        const cameraInfo = this.getCameraInfo();
-        if (cameraInfo != null) {
-          console.log('joystick自由--->合并');
-          this.reply['newUserStates'][0].playerState.camera.position = cameraInfo.camera_position;
-          this.reply['newUserStates'][0].playerState.camera.angle = cameraInfo.camera_angle;
-
-          if (cameraInfo.mediaSrc) {
-            this.reply.mediaSrc = cameraInfo.mediaSrc;
-          } 
-          else if (cameraInfo.file_name) {
-            this.reply.mediaSrc =
-              '/' +
-              appId +
-              '/' +
-              breakPointId +
-              '/' +
-              cameraInfo.file_name.substring(
-                0,
-                cameraInfo.file_name.indexOf('.'),
-              ) +
-              '/' +
-              cameraInfo.file_name +
-              '?m=' +
-              new Date().getTime();
-          }
-          if(this.cameraInfos.length == 0){
-            this.reply.moveOver = true;
-          }
-          this.sendingFrameForJoystick = true;
-        } else {
-          console.log('joystick自由--->不合并');
-        }
         return this.reply;
       }
-    // } 
+    }
+
+    //count == 2
+    //超出范围了
+    if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
+      let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
+      if (neighPoints[0].angle == 0) {
+        offsetAngle1 = Math.min(offsetAngle1, Math.abs(_angle - 360));
+      }
+
+      let offsetAngle2 = Math.abs(_angle - neighPoints[1].angle);
+      if (neighPoints[1].angle == 0) {
+        offsetAngle2 = Math.min(offsetAngle2, Math.abs(_angle - 360));
+      }
+
+      if (offsetAngle1 > offsetAngle2) {
+        chooseBreakPointId = neighPoints[1].breakPointId;
+      } else {
+        chooseBreakPointId = neighPoints[0].breakPointId;
+      }
+      console.log(
+        'joystick镜头过渡--->' + breakPointId + '-' + chooseBreakPointId,
+      );
+      return await this.moveCamera(
+        breakPointId,
+        chooseBreakPointId,
+        appId,
+        userId,
+        traceId,
+        actionType,
+      );
+    } else {
+      user.player.position = JSON.parse(JSON.stringify(playerPosition));
+      this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
+        JSON.stringify(user.player.position),
+      );
+      this.reply.actionResponses[0].actionType = actionType;
+      const cameraInfo = this.getCameraInfo();
+      if (cameraInfo != null) {
+        console.log('joystick自由--->合并');
+        this.reply['newUserStates'][0].playerState.camera.position =
+          cameraInfo.camera_position;
+        this.reply['newUserStates'][0].playerState.camera.angle =
+          cameraInfo.camera_angle;
+
+        if (cameraInfo.mediaSrc) {
+          this.reply.mediaSrc = cameraInfo.mediaSrc;
+        } else if (cameraInfo.file_name) {
+          this.reply.mediaSrc =
+            '/' +
+            appId +
+            '/' +
+            breakPointId +
+            '/' +
+            cameraInfo.file_name.substring(
+              0,
+              cameraInfo.file_name.indexOf('.'),
+            ) +
+            '/' +
+            cameraInfo.file_name +
+            '?m=' +
+            new Date().getTime();
+        }
+        if (this.cameraInfos.length == 0) {
+          this.reply.moveOver = true;
+        }
+        this.sendingFrameForJoystick = true;
+      } else {
+        console.log('joystick自由--->不合并');
+      }
+      return this.reply;
+    }
+    // }
     // catch (error) {
     //   console.log('MoveService', error);
     //   debugger;
@@ -1057,7 +1054,7 @@ export class MoveService implements OnModuleInit {
   ) {
     const breakPoint = this.breakPointInfo[breakPointId];
     const user = this.users[userId];
-    let player_Position = this.getTarget(
+    const player_Position = this.getTarget(
       playerPosition,
       breakPoint.position,
       this.breakPointInfo[neighBreakPointId].position,
@@ -1085,13 +1082,14 @@ export class MoveService implements OnModuleInit {
       this.reply.actionResponses[0].actionType = actionType;
       const cameraInfo = this.getCameraInfo();
       if (cameraInfo != null) {
-        this.reply['newUserStates'][0].playerState.camera.position = cameraInfo.camera_position;
-        this.reply['newUserStates'][0].playerState.camera.angle = cameraInfo.camera_angle;
-   
+        this.reply['newUserStates'][0].playerState.camera.position =
+          cameraInfo.camera_position;
+        this.reply['newUserStates'][0].playerState.camera.angle =
+          cameraInfo.camera_angle;
+
         if (cameraInfo.mediaSrc) {
           this.reply.mediaSrc = cameraInfo.mediaSrc;
-        } 
-        else if (cameraInfo.file_name) {
+        } else if (cameraInfo.file_name) {
           this.reply.mediaSrc =
             '/' +
             appId +
@@ -1108,7 +1106,7 @@ export class MoveService implements OnModuleInit {
             new Date().getTime();
         }
 
-        if(this.cameraInfos.length == 0){
+        if (this.cameraInfos.length == 0) {
           this.reply.moveOver = true;
         }
         this.sendingFrameForJoystick = true;
@@ -1229,7 +1227,7 @@ export class MoveService implements OnModuleInit {
   }
 
   setCameraInfo(moveFrames) {
-    for(let i=0;i<moveFrames.length;i += 5){
+    for (let i = 0; i < moveFrames.length; i += 5) {
       this.cameraInfos.push(moveFrames[i]);
     }
   }

+ 16 - 9
src/rotate/rotate.service.ts

@@ -276,20 +276,20 @@ export class RotateService {
           user.breakPointId;
         // const value = null;
         console.log('rotateForAngle-3:' + user.breakPointId);
-        console.log('rotateForAngle-3-key:'+ key);
+        console.log('rotateForAngle-3-key:' + key);
         this.logger.log('rotateForAngle-3-key', key);
         let redisData = await this.cacheService.get(key);
         //if (redisData && redisData.length > 0) {
         let value = JSON.parse(redisData); //redisData ? JSON.parse(redisData) : null;
         //console.log('rotateForAngle-4:'+user.breakPointId+','+value.directory+','+value.fileName);
-        if(value.directory != user.breakPointId){
+        if (value.directory != user.breakPointId) {
           key =
-          'rotateframe:app_id:' +
-          user.appId +
-          ':frame_index:' +
-          user.camera.angle.yaw +
-          ':break_point_id:' +
-          user.breakPointId;
+            'rotateframe:app_id:' +
+            user.appId +
+            ':frame_index:' +
+            user.camera.angle.yaw +
+            ':break_point_id:' +
+            user.breakPointId;
           redisData = await this.cacheService.get(key);
           value = JSON.parse(redisData);
         }
@@ -328,7 +328,14 @@ export class RotateService {
           '?m=' +
           new Date().getTime();
 
-          console.log('rotateForAngle-5:'+user.breakPointId+','+value.directory+','+value.fileName);
+        console.log(
+          'rotateForAngle-5:' +
+            user.breakPointId +
+            ',' +
+            value.directory +
+            ',' +
+            value.fileName,
+        );
         reply.breakPointId = user.breakPointId;
         this.replies[userId].traceIds = [];
         this.replies[userId].actionResponses = [];

File diff suppressed because it is too large
+ 1158 - 0
src/scene/scene.service copy.ts


+ 165 - 125
src/scene/scene.service.ts

@@ -105,44 +105,6 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
   public users = {};
 
-  // initUsers(app_id, userId) {
-  //   const user = {
-  //     appId: null,
-  //     userId: null,
-  //     breakPointId: null,
-  //     roomId: null,
-  //     player: {
-  //       position: { x: -700, y: 0, z: 0 },
-  //       angle: {
-  //         pitch: 0,
-  //         yaw: 0,
-  //         roll: 0,
-  //       },
-  //     },
-  //     camera: {
-  //       position: { x: -1145, y: 0, z: 160 },
-  //       angle: {
-  //         pitch: 0,
-  //         yaw: 0,
-  //         roll: 0,
-  //       },
-  //     },
-  //     rotateInfo: {
-  //       frameIndex: 0,
-  //       horizontal_move: 0,
-  //       mediaSrc: null,
-  //     },
-  //     moveInfo: {},
-  //     // traceIds: [],
-  //     // actionResponses:[]
-  //   };
-
-  //   user.appId = app_id;
-  //   user.userId = userId;
-  //   user.breakPointId = 100;
-  //   this.users[userId] = user;
-  // }
-
   onModuleInit(): void {
     this.sceneGrpcService =
       this.client.getService<SceneGrpcService>('SceneGrpcService');
@@ -190,6 +152,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   public resumeStream(): void {
     this.onMoving.next(false);
     this.onRotating.next(false);
+    this.onJoysticking.next(false);
     this.isHoldingStream = false;
     this.moveframeCnt = -1;
     this.rotateframeCnt = -1;
@@ -316,7 +279,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           JSON.stringify(playerAngle),
         );
         //debugger;
-        console.log('moveService.stop-1:' + breakPointId)
+        console.log('moveService.stop-1:' + breakPointId);
         redisMeta = await this.moveService.stop(
           trace_id,
           userId,
@@ -420,7 +383,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
                 cameraAngle,
                 cameraAngle,
               );
-              console.log('moveService.stop-2:' + breakPointId)
+              console.log('moveService.stop-2:' + breakPointId);
               const redisMeta = await this.moveService.stop(
                 trace_id,
                 userId,
@@ -613,100 +576,153 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
       );
       // 有数据 [0]是rotate数据,[1-infinity]是walking数据
       this.logger.log('joystickRes-1', joystickRes);
-      this.onJoysticking.next(true);
-      if(joystickRes != null){
-        console.log('handlejoystick:'+joystickRes.mediaSrc+',相机坐标:'+JSON.stringify(joystickRes.newUserStates[0].playerState.camera.position))
-      }
-      else{
-        console.log('handlejoystick:null');
-      }
-      if (Array.isArray(joystickRes)) {
-        // 处理第一个镜头数据
-        const rotateCamData = joystickRes[0];
-        this.logger.log('rotateCamData', rotateCamData.length);
-        if (rotateCamData?.length) {
-          // 头数组[0] rotate 序列, 头是关键key
-          joystickRes[0].forEach((item: StreamReplyType, index: number) => {
-            const IDRflag = index % 5 === 0 ? 1 : 3;
-            const dir = this.isHeaderOrLast(index, joystickRes[0].length - 1);
-            item.DIR = dir ? 1 : IDRflag;
-            item.mType = 'rotate';
-          });
-        } else {
-          this.logger.log('rotateCamData无数据');
-        }
-        // 二维数组 做move 序列, move类型
-        if (joystickRes?.length >= 1) {
-          for (let i = 1; i < joystickRes.length; i++) {
-            this.logger.log('joystickRes-2', joystickRes[i]);
-            Array.from(joystickRes[i]).forEach(
-              (item: StreamReplyType, index: number) => {
-                const IDRflag = index % 5 === 0 ? 1 : 3;
-                const dir = this.isHeaderOrLast(
-                  index,
-                  joystickRes[i].length - 1,
-                );
-                item.DIR = dir ? 1 : IDRflag;
-                // 将每段最后一个推入lastMovingPointArray
-                if (index === joystickRes[i].length - 1) {
-                  this.lastMovingPointArray.push({
-                    mediaSrc: item.mediaSrc,
-                    metaData: item,
-                  });
-                }
-                //this.logger.log(
-                //   'joystick:' +
-                //     JSON.stringify(
-                //       joystickRes[i][index]['newUserStates'][0].playerState
-                //         .camera.position,
-                //     ),
-                // );
-              },
-            );
-          }
-        }
-        const seqs = Array.from(joystickRes).flat() as any as StreamReplyType[];
 
-        if (seqs?.length > 1) {
-          this.logger.log('joystick:-seqs', seqs.length);
-          //TODO joystick中断逻辑
-          this.hasJoystickMoveRequest = true;
-          this.handleSeqMoving(seqs);
-        } else {
-          console.warn('joystick-move无数据');
-        }
-      } else {
-        this.logger.log('joystick-接收人物数据', this.onMoving.getValue());
-        if (!this.onMoving.getValue()) {
-          // 在非行走时接受
+      if (joystickRes) {
+        this.onJoysticking.next(true);
+        console.log(
+          'handlejoystick:' +
+          joystickRes.mediaSrc +
+          ',相机坐标:' +
+          JSON.stringify(
+            joystickRes.newUserStates[0].playerState.camera.position,
+          ),
+        );
+        if (joystickRes.mediaSrc) {
           this.holdSteam();
+          console.log('joystickRes有mediaSrc', joystickRes.mediaSrc);
+          // IDR flag设置为I帧
+          const setDIR = joystickRes.moveOver || joystickRes.moveStart ? 1 : 3;
+          // 过滤新东西, 推完给回false
+          this.moveService.sendingFrameForJoystick = true;
+
           if (this.joystickFrameCnt === -1) {
             this.joystickFrameCnt = this.frameCnt.getValue();
           }
           this.joystickFrameCnt += 1;
-          const stream: StreamMetaType = {
+          const streamData: StreamFrameType = {
             frame: this.joystickFrameCnt,
+            clipPath: joystickRes.mediaSrc,
             metaData: JSON.stringify(joystickRes),
+            serverTime: this.mockserverTime,
+            DIR: setDIR,
           };
-          //this.logger.log('rotate', stream, Date.now());
-          const res = await this.streamService.pushMetaDataToSteam(stream);
-          if (res.done) {
-            this.logger.log('joystick-frame', res.frame);
-            this.frameCnt.next(res.frame);
-            clearTimeout(this._JoyStickingTimeout);
-            this._JoyStickingTimeout = setTimeout(() => {
-              this.logger.log('joystick opt done');
-              this.logger.log('joystick 交权给空流,当前pts', res.frame);
-              // this.frameCnt.next(res.frame);
-              this.onJoysticking.next(false);
-              this.resumeStream();
-              this.joystickFrameCnt = -1;
-            }, 100);
+
+          const hasPush = await this.streamService.pushFrameToSteam(streamData);
+          if (hasPush.done) {
+            console.log('joystick-hasPush', hasPush);
+            this.resumeStream();
+            this.moveService.sendingFrameForJoystick = false;
+
+            const data = joystickRes as StreamReplyType;
+            //不清楚是否每帧回传
+            const userId = this.user_id;
+            const breakPointId = data.endBreakPointId;
+            const lastReply = JSON.stringify(joystickRes);
+            this.moveService.updateUser(userId, breakPointId, lastReply);
+          } else {
+            console.error(
+              'joystick-流地址有误::',
+              hasPush.frame,
+              joystickRes.mediaSrc,
+            );
+            this.logger.error(
+              'joystick-流地址有误::',
+              hasPush.frame,
+              joystickRes.mediaSrc,
+            );
+          }
+
+          // // 处理第一个镜头数据
+          // const rotateCamData = joystickRes[0];
+          // this.logger.log('rotateCamData', rotateCamData.length);
+          // if (rotateCamData?.length) {
+          //   // 头数组[0] rotate 序列, 头是关键key
+          //   joystickRes[0].forEach((item: StreamReplyType, index: number) => {
+          //     const IDRflag = index % 5 === 0 ? 1 : 3;
+          //     const dir = this.isHeaderOrLast(index, joystickRes[0].length - 1);
+          //     item.DIR = dir ? 1 : IDRflag;
+          //     item.mType = 'rotate';
+          //   });
+          // } else {
+          //   this.logger.log('rotateCamData无数据');
+          // }
+          // // 二维数组 做move 序列, move类型
+          // if (joystickRes?.length >= 1) {
+          //   for (let i = 1; i < joystickRes.length; i++) {
+          //     this.logger.log('joystickRes-2', joystickRes[i]);
+          //     Array.from(joystickRes[i]).forEach(
+          //       (item: StreamReplyType, index: number) => {
+          //         const IDRflag = index % 5 === 0 ? 1 : 3;
+          //         const dir = this.isHeaderOrLast(
+          //           index,
+          //           joystickRes[i].length - 1,
+          //         );
+          //         item.DIR = dir ? 1 : IDRflag;
+          //         // 将每段最后一个推入lastMovingPointArray
+          //         if (index === joystickRes[i].length - 1) {
+          //           this.lastMovingPointArray.push({
+          //             mediaSrc: item.mediaSrc,
+          //             metaData: item,
+          //           });
+          //         }
+          //         //this.logger.log(
+          //         //   'joystick:' +
+          //         //     JSON.stringify(
+          //         //       joystickRes[i][index]['newUserStates'][0].playerState
+          //         //         .camera.position,
+          //         //     ),
+          //         // );
+          //       },
+          //     );
+          //   }
+          // }
+          // const seqs = Array.from(joystickRes).flat() as any as StreamReplyType[];
+
+          // if (seqs?.length > 1) {
+          //   this.logger.log('joystick:-seqs', seqs.length);
+          //   //TODO joystick中断逻辑
+          //   this.hasJoystickMoveRequest = true;
+          //   this.handleSeqMoving(seqs);
+          // } else {
+          //   console.warn('joystick-move无数据');
+          // }
+        } else {
+          this.logger.log('joystick-接收人物数据', this.onMoving.getValue());
+          if (!this.onMoving.getValue()) {
+            // 在非行走时接受
+            this.holdSteam();
+            if (this.joystickFrameCnt === -1) {
+              this.joystickFrameCnt = this.frameCnt.getValue();
+            }
+            this.joystickFrameCnt += 1;
+            const stream: StreamMetaType = {
+              frame: this.joystickFrameCnt,
+              metaData: JSON.stringify(joystickRes),
+            };
+            //this.logger.log('rotate', stream, Date.now());
+            const res = await this.streamService.pushMetaDataToSteam(stream);
+            if (res.done) {
+              this.logger.log('joystick-frame', res.frame);
+              this.frameCnt.next(res.frame);
+              clearTimeout(this._JoyStickingTimeout);
+              this._JoyStickingTimeout = setTimeout(() => {
+                this.logger.log('joystick opt done');
+                this.logger.log('joystick 交权给空流,当前pts', res.frame);
+                // this.frameCnt.next(res.frame);
+                this.onJoysticking.next(false);
+                this.resumeStream();
+                this.joystickFrameCnt = -1;
+              }, 50);
+            }
           }
         }
+      } else {
+        console.log('handlejoystick:null');
+        this.onJoysticking.next(false);
       }
     } catch (error) {
       console.error('joystick错误', error);
+      this.onJoysticking.next(false);
       this.logger.error('joystick', error.message);
     }
   }
@@ -721,8 +737,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     // this.logger.log('moving-seqs', seqs.length);
     this.onMoving.next(true);
     this.holdSteam();
-    //TODO Remove
-    // clearTimeout(this._JoyStickingTimeout);
+    // 保证每一段都是序列动作的前面队列是空的
     this.moveQueue.clean();
 
     seqs.forEach((frame: StreamReplyType) => {
@@ -835,8 +850,9 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
               this.lastMovingPointArray = [];
               this.hasJoystickMoveRequest = false;
               this.logger.log('move end');
-            }, 200);
+            }, 80);
           } else {
+            console.error('流地址有误::', res.frame, JSON.stringify(res));
             this.logger.error(
               `movesteam::当前帧:${res.frame}` + JSON.stringify(res),
             );
@@ -1026,7 +1042,10 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
                 this.roRequestQueueSub.unsubscribe();
                 this.roRequestQueueSub = null;
               }
-            }, 300);
+            }, 50);
+          } else {
+            console.error('流地址有误::', res.frame, JSON.stringify(res));
+            this.logger.error('流地址有误::', res.frame, JSON.stringify(res));
           }
         },
       );
@@ -1097,6 +1116,28 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
               this.logger.error(`first render problem:${frame}`);
             }
           }
+
+          if (frame > 1) {
+            const isOk =
+              !this.onMoving.value &&
+              !this.onRotating.value &&
+              !this.onJoysticking.value &&
+              this.firstRender;
+
+            console.log(
+              '空白流条件-->:' +
+              isOk +
+              ' onMoving: ' +
+              this.onMoving.value +
+              ' onRotating: ' +
+              this.onRotating.value +
+              ' onJoysticking: ' +
+              this.onJoysticking.value +
+              ' firstRender: ' +
+              this.firstRender,
+            );
+          }
+
           if (
             frame > 1 &&
             !this.onMoving.value &&
@@ -1119,7 +1160,6 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
             } else {
               this.stopStream();
               this.logger.log('空流无Redis数据');
-              throw new Error('空流无Redis数据');
             }
           }
         } catch (error) {

+ 3 - 0
src/scene/stream/stream.d.ts

@@ -6,6 +6,9 @@ interface StreamFrameType {
   DIR?: number;
   mediaSrc?: string; // 临时
   mType?: string;
+  newUserStates?: NewUserStatesType[];
+  moveStart?: boolean;
+  moveOver?: boolean;
 }
 interface StreamMetaType {
   frame: number;

+ 6 - 5
src/scene/stream/stream.service.ts

@@ -113,6 +113,12 @@ export class StreamService {
         } else {
           clipPath = stream.clipPath;
         }
+        // 增加不存在帧数据中断数据,原因有太多不准确的路径。
+        // 其次其他地方会拿这里的最后一帧数据会出错,由于上流数据很多不稳定问题尽可保流的稳定性。
+        if (!existsSync(clipPath)) {
+          this.logger.error('不存在的推流路径::' + clipPath);
+          return resolve({ frame: stream.frame, done: false });
+        }
         // const clipPath = stream.clipPath;
         const metaData = stream.metaData || '{}';
         const frame = stream.frame;
@@ -132,11 +138,6 @@ export class StreamService {
         // console.warn('coordBuff', coordBuff.byteLength);
         // const steamStat = statSync(clipPath);
         // const steamTotalSize = metaData.length + steamStat.size;
-        // 增加不存在帧数据中断数据,原因有太多不准确的路径。
-        if (!existsSync(clipPath)) {
-          this.logger.error('不存在的推流路径::' + clipPath);
-          return;
-        }
 
         const clipBuffer = readFileSync(clipPath);