gemercheung 3 年之前
父节点
当前提交
c7876dd5ed
共有 4 个文件被更改,包括 180 次插入108 次删除
  1. 87 99
      src/move/move.service.ts
  2. 67 2
      src/rotate/rotate.service.ts
  3. 25 7
      src/scene/scene.service.ts
  4. 1 0
      src/scene/stream/stream.d.ts

+ 87 - 99
src/move/move.service.ts

@@ -48,8 +48,7 @@ export class MoveService implements OnModuleInit {
     traceIds: [],
     vehicle: null,
     mediaSrc: null,
-    //moveOver: false,
-    moveStart: false,
+    joystickIDR: false,
     newUserStates: [
       {
         userId: 'dcff36ae4fc1d',
@@ -214,6 +213,10 @@ export class MoveService implements OnModuleInit {
     end_break_point_id,
     angleIndex,
   ) {
+    console.log(
+      'handlejoystick-angle->相机过渡angleIndex-------------------------:' +
+        angleIndex,
+    );
     let moveFramesRes, moveFrames;
     let key =
       'moveframe:app_id:' +
@@ -521,7 +524,6 @@ export class MoveService implements OnModuleInit {
   updateUser(userId, breakPointId, lastReply) {
     const user = this.users[userId];
     user.breakPointId = breakPointId;
-    console.log('handlejoystick:updateUser');
     if (lastReply.actionResponses[0].actionType != 15) {
       user.player.position =
         lastReply['newUserStates'][0].playerState.player.position;
@@ -880,12 +882,17 @@ export class MoveService implements OnModuleInit {
       const closestDis = 80; //小于这个距离就跳到邻居呼吸点
       const distance = step * dir_action.speed_level;
       let angle = null;
+      console.log(
+        'handlejoystick-angle->:dir_action.move_angle' + dir_action.move_angle,
+      );
       if (user.camera.angle.yaw < 0) {
         user.camera.angle.yaw = 360 + user.camera.angle.yaw;
       }
       let move_angle = dir_action.move_angle + user.camera.angle.yaw;
       move_angle = move_angle % 360;
-
+      console.log(
+        'handlejoystick-angle->:叠加后move_angle(也是user.angle)' + move_angle,
+      );
       //TODO 临时增加断言
       const playerPosition: Point = { x: 0, y: 0, z: 0 };
       playerPosition.x =
@@ -895,6 +902,9 @@ export class MoveService implements OnModuleInit {
         user.player.position.y +
         distance * Math.sin((move_angle / 360) * 2 * Math.PI);
 
+      const offsetX = playerPosition.x - user.player.position.x;
+      const offsetY = playerPosition.y - user.player.position.y;
+
       //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
       let chooseBreakPointId = null;
       const breakPoint = this.breakPointInfo[breakPointId];
@@ -924,12 +934,7 @@ export class MoveService implements OnModuleInit {
       console.log('joystickjoystick:' + this.cameraInfos.length);
 
       if (surroundPointIds.length == 1) {
-        // console.log(
-        //   'joystick校验--->' + breakPointId + '-' + surroundPointIds[0],
-        // );
-        console.log(
-          'handlejoysticktesttest:actionRequest-笔直' + new Date().getTime(),
-        );
+        //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
         return await this.moveDirect(
           playerPosition,
           closestDis,
@@ -951,9 +956,26 @@ export class MoveService implements OnModuleInit {
         playerPosition,
       );
 
+      const offsetBreakPosition = {
+        x: breakPoint.position.x + offsetX,
+        y: breakPoint.position.y + offsetY,
+      };
+      let closestNeighorId = null;
+      let neighDis = null;
+
       let singleInfo = null;
       for (let i = 0; i < surroundPointIds.length; ++i) {
         const neighPoint = this.breakPointInfo[surroundPointIds[i]];
+        if (closestNeighorId == null) {
+          neighDis = this.getDistance(offsetBreakPosition, neighPoint.position);
+          closestNeighorId = surroundPointIds[i];
+        } else if (
+          neighDis > this.getDistance(offsetBreakPosition, neighPoint.position)
+        ) {
+          neighDis = this.getDistance(offsetBreakPosition, neighPoint.position);
+          closestNeighorId = surroundPointIds[i];
+        }
+
         neighPoint.breakPointId = surroundPointIds[i];
         angle = this.getAngle(
           breakPoint.position,
@@ -975,23 +997,7 @@ export class MoveService implements OnModuleInit {
           neighPoint.angle = angle;
           neighPoints.push(neighPoint);
           ++count;
-          //console.log('handlejoysticktesttest:45°的倍数');
-          //debugger;
           break;
-
-          // 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) {
@@ -1026,9 +1032,7 @@ export class MoveService implements OnModuleInit {
           //   'joystick校验--->' + breakPointId + '-' + singleInfo.breakPointId,
           // );
           //console.log('handlejoysticktesttest校验0角度:'+_angle+','+move_angle)       //这个没办法,得换方案
-          console.log(
-            'handlejoysticktesttest:actionRequest-笔直' + new Date().getTime(),
-          );
+          //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
           return await this.moveDirect(
             playerPosition,
             closestDis,
@@ -1040,12 +1044,7 @@ export class MoveService implements OnModuleInit {
             actionType,
           );
         } else if (count == 1) {
-          // console.log(
-          //   'joystick校验--->' + breakPointId + '-' + neighPoints[0].breakPointId,
-          // );
-          console.log(
-            'handlejoysticktesttest:actionRequest-笔直' + new Date().getTime(),
-          );
+          //console.log('handlejoysticktesttest:actionRequest-笔直'+new Date().getTime());
           return await this.moveDirect(
             playerPosition,
             closestDis,
@@ -1060,9 +1059,7 @@ export class MoveService implements OnModuleInit {
           this.reply['newUserStates'][0].playerState.player.position =
             JSON.parse(JSON.stringify(user.player.position));
           this.reply.actionResponses[0].actionType = actionType;
-          console.log(
-            'handlejoysticktesttest:actionRequest-停止' + new Date().getTime(),
-          );
+          //console.log('handlejoysticktesttest:actionRequest-停止'+new Date().getTime());
           return this.reply;
         }
       }
@@ -1070,25 +1067,25 @@ export class MoveService implements OnModuleInit {
       //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 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));
-        }
+        // 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;
+        // if (offsetAngle1 > offsetAngle2) {
+        //   chooseBreakPointId = neighPoints[1].breakPointId;
+        // } else {
+        //   chooseBreakPointId = neighPoints[0].breakPointId;
+        // }
+        if (closestNeighorId == null) {
+          debugger;
         }
-        // console.log(
-        //   'joystick镜头过渡--->' + breakPointId + '-' + chooseBreakPointId,
-        // );
-        //this.reply.moveOver = true;
+        chooseBreakPointId = closestNeighorId;
         return await this.moveCamera(
           breakPointId,
           chooseBreakPointId,
@@ -1113,28 +1110,23 @@ export class MoveService implements OnModuleInit {
 
           if (cameraInfo.mediaSrc) {
             this.reply.mediaSrc = cameraInfo.mediaSrc;
-            if (this.cameraInfos.length == 0) {
-              this.reply.moveStart = true;
-            } else {
-              this.reply.moveStart = false;
-            }
+            this.reply.joystickIDR = cameraInfo.joystickIDR;
           }
-          // if (this.cameraInfos.length == 0) {
-          //   this.reply.moveOver = true;
-          // }
 
           user.camera.position = JSON.parse(
             JSON.stringify(cameraInfo.camera_position),
           );
+          console.log(
+            'handlejoystick-angle->:自由并过渡更新user.angle' +
+              cameraInfo.camera_angle.yaw,
+          );
           user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
 
           this.sendingFrameForJoystick = true;
         } else {
           console.log('joystick自由--->不合并');
         }
-        console.log(
-          'handlejoysticktesttest:actionRequest-自由' + new Date().getTime(),
-        );
+        //console.log('handlejoysticktesttest:actionRequest-自由'+new Date().getTime());
         return this.reply;
       }
     } catch (error) {
@@ -1195,19 +1187,16 @@ export class MoveService implements OnModuleInit {
 
         if (cameraInfo.mediaSrc) {
           this.reply.mediaSrc = cameraInfo.mediaSrc;
-          if (this.cameraInfos.length == 0) {
-            this.reply.moveStart = true;
-          } else {
-            this.reply.moveStart = false;
-          }
+          this.reply.joystickIDR = cameraInfo.joystickIDR;
         }
-        // if (this.cameraInfos.length == 0) {
-        //   this.reply.moveOver = true;
-        // }
 
         user.camera.position = JSON.parse(
           JSON.stringify(cameraInfo.camera_position),
         );
+        console.log(
+          'handlejoystick-angle->:moveDirect更新user.angle' +
+            cameraInfo.camera_angle.yaw,
+        );
         user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
 
         this.sendingFrameForJoystick = true;
@@ -1252,11 +1241,15 @@ export class MoveService implements OnModuleInit {
       );
 
       for (let i = 0; i < checkReplys.length; ++i) {
-        //checkReplys[i].actionResponses[0].actionType = actionType;
+        let joystickIDR = false;
+        if (i == 0 || i == checkReplys.length - 1) {
+          joystickIDR = true;
+        }
         this.addCameraInfo(
           checkReplys[i]['newUserStates'][0].playerState.camera.position,
           checkReplys[i]['newUserStates'][0].playerState.camera.angle,
           checkReplys[i].mediaSrc,
+          joystickIDR,
         );
       }
 
@@ -1296,17 +1289,19 @@ export class MoveService implements OnModuleInit {
         user.camera.position = JSON.parse(
           JSON.stringify(cameraInfo.camera_position),
         );
+        console.log(
+          'handlejoystick-angle->:刚刚过渡更新user.angle' +
+            cameraInfo.camera_angle.yaw,
+        );
         user.camera.angle.yaw = cameraInfo.camera_angle.yaw;
 
         if (cameraInfo.mediaSrc) {
           this.reply.mediaSrc = cameraInfo.mediaSrc;
-          this.reply.moveStart = true;
+          this.reply.joystickIDR = cameraInfo.joystickIDR;
         }
         this.sendingFrameForJoystick = true;
       }
-      console.log(
-        'handlejoysticktesttest:actionRequest-镜头过渡' + new Date().getTime(),
-      );
+      //console.log('handlejoysticktesttest:actionRequest-镜头过渡'+new Date().getTime());
       return this.reply;
     }
   }
@@ -1345,15 +1340,23 @@ export class MoveService implements OnModuleInit {
           '?m=' +
           new Date().getTime();
       }
+      if (i == 0) {
+        moveFrames[i].joystickIDR = true;
+      } else if (i == moveFrames.length - 1) {
+        moveFrames[i].joystickIDR = true;
+      } else {
+        moveFrames[i].joystickIDR = false;
+      }
       this.cameraInfos.push(moveFrames[i]);
     }
   }
 
-  addCameraInfo(cameraPosition, cameraAngle, mediaSrc) {
+  addCameraInfo(cameraPosition, cameraAngle, mediaSrc, joystickIDR) {
     this.cameraInfos.push({
       camera_position: cameraPosition,
       camera_angle: cameraAngle,
       mediaSrc: mediaSrc,
+      joystickIDR: joystickIDR,
     });
   }
 
@@ -1368,12 +1371,7 @@ export class MoveService implements OnModuleInit {
 
   complementFrame(userId) {
     if (this.cameraInfos.length > 0) {
-      console.log(
-        'handlejoysticktesttest:complementFrame-1 继续' +
-          userId +
-          ',' +
-          new Date().getTime(),
-      );
+      //console.log('handlejoysticktesttest:complementFrame-1 继续'+userId+','+new Date().getTime())
       const user = this.users[userId];
       const cameraInfo = this.cameraInfos.shift();
       this.reply.traceIds = [];
@@ -1399,11 +1397,7 @@ export class MoveService implements OnModuleInit {
 
       if (cameraInfo.mediaSrc) {
         this.reply.mediaSrc = cameraInfo.mediaSrc;
-        if (this.cameraInfos.length == 0) {
-          this.reply.moveStart = true;
-        } else {
-          this.reply.moveStart = false;
-        }
+        this.reply.joystickIDR = cameraInfo.joystickIDR;
       }
 
       user.camera.position = JSON.parse(
@@ -1416,23 +1410,17 @@ export class MoveService implements OnModuleInit {
       return this.reply;
     } else if (this.cameraInfos.length == 0) {
       if (this.reply['newUserStates'][0].renderInfo.isMoving == 1) {
-        console.log(
-          'handlejoysticktesttest:complementFrame-2 停止1' +
-            new Date().getTime(),
-        );
+        //console.log('handlejoysticktesttest:complementFrame-2 停止1'+new Date().getTime())
         return this.stopJoystick(userId);
       } else {
-        console.log(
-          'handlejoysticktesttest:complementFrame-2 停止2' +
-            new Date().getTime(),
-        );
+        //console.log('handlejoysticktesttest:complementFrame-2 停止2'+new Date().getTime())
         return null;
       }
     }
   }
 
   stopJoystick(userId) {
-    console.log('handlejoysticktesttest:stopJoystick');
+    //console.log('handlejoysticktesttest:stopJoystick');
     this.reply.traceIds = [];
     const user = this.users[userId];
     this.reply['newUserStates'][0].userId = userId;

+ 67 - 2
src/rotate/rotate.service.ts

@@ -36,6 +36,24 @@ export class RotateService {
   };
   public users = {};
   private replies = {};
+  private firstPoint = {
+    player: {
+      position: { x: -560.0, y: 320.0, z: 0.0 }, //{ x: -800.0, y: 100.0, z: 0.0 },
+      angle: {
+        pitch: 0,
+        yaw: 0,
+        roll: 0,
+      },
+    },
+    camera: {
+      position: { x: -998.0, y: 319.9999694824219, z: 120.0 }, //{ x: -1141.6719970703125, y: 94.03607940673828, z: 120.0 },
+      angle: {
+        pitch: 0,
+        yaw: 0,
+        roll: 0,
+      },
+    },
+  };
 
   init(app_id: string, userId: string) {
     const user = {
@@ -71,8 +89,15 @@ export class RotateService {
     user.appId = app_id;
     user.userId = userId;
     user.breakPointId = Number(this.configService.get('app.startPoint')) || 0;
+    user.player.position = JSON.parse(
+      JSON.stringify(this.firstPoint.player.position),
+    );
+    user.camera.position = JSON.parse(
+      JSON.stringify(this.firstPoint.camera.position),
+    );
     console.log('user-init', user);
     this.users[userId] = user;
+
     const reply = {
       traceIds: [],
       vehicle: null,
@@ -160,15 +185,55 @@ export class RotateService {
     };
 
     reply['newUserStates'][0].playerState.player.position = JSON.parse(
-      JSON.stringify(user.player.position),
+      JSON.stringify(this.firstPoint.player.position),
     );
     reply['newUserStates'][0].playerState.camera.position = JSON.parse(
-      JSON.stringify(user.camera.position),
+      JSON.stringify(this.firstPoint.camera.position),
     );
     this.replies[userId] = reply;
     return reply;
   }
 
+  //首帧
+  getFirstStreamData(appId, userId) {
+    const user = this.users[userId];
+    const reply = this.replies[userId];
+    reply.traceIds = [];
+    reply['newUserStates'][0].userId = userId;
+    reply['newUserStates'][0].playerState.player.position = JSON.parse(
+      JSON.stringify(this.firstPoint.player.position),
+    );
+    reply['newUserStates'][0].playerState.player.angle = JSON.parse(
+      JSON.stringify(this.firstPoint.player.angle),
+    );
+    reply['newUserStates'][0].playerState.camera.position = JSON.parse(
+      JSON.stringify(this.firstPoint.camera.position),
+    );
+    reply['newUserStates'][0].playerState.camera.angle = JSON.parse(
+      JSON.stringify(this.firstPoint.camera.angle),
+    );
+    reply['newUserStates'][0].playerState.cameraCenter = JSON.parse(
+      JSON.stringify(this.firstPoint.player.position),
+    );
+    reply['actionResponses'][0].traceId = null;
+
+    reply.mediaSrc =
+      '/' +
+      appId +
+      '/' +
+      user.breakPointId +
+      '/' +
+      user.breakPointId +
+      '/' +
+      user.breakPointId +
+      '.0000.h264' +
+      '?m=' +
+      new Date().getTime();
+    reply['newUserStates'][0].renderInfo.isMoving = 0;
+    this.replies[userId] = reply;
+    return reply;
+  }
+
   /**
    * delete User object
    * @param userId

+ 25 - 7
src/scene/scene.service.ts

@@ -100,6 +100,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
   private isJoystickHasStream = false;
   private hasJoystickFocusRepeat = false;
+  private startSub: any
 
   public users = {};
 
@@ -195,6 +196,14 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     try {
       this.rotateService.init(request.app_id, request.user_id);
       this.cacheService.getClient();
+      console.log('rotateService::init');
+      this.startSub = this.startSteaming.subscribe((flag) => {
+        if (flag) {
+          console.log('初始推流::');
+          this.startStream();
+          this.handleStream();
+        }
+      });
       // 加载
     } catch (error) {
       this.logger.error('error', error);
@@ -204,6 +213,10 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   exit() {
     this.frameCnt.next(-1);
     this.rotateService.deleteUser(this.user_id);
+    if (this.startSub) {
+      this.startSub.unsubscribe();
+      this.startSub = null;
+    }
   }
 
   async rotate(request: RotateRequest) {
@@ -464,7 +477,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     // if (this.isHasWalkingJoints()) {
     //   console.log('lock-锁-walking', this.latestWalkingRequest);
     //   this.globalOptLock = true;
-    // } 
+    // }
 
     // 进入正常walking流程
     if (!this.onMoving.getValue()) {
@@ -744,7 +757,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
       const src = joystickRes.mediaSrc.split('?')[0];
       const mediaSrc = this.configService.get('app.prefix') + src;
       // IDR flag设置为I帧
-      const setDIR = joystickRes.moveOver || joystickRes.moveStart ? 1 : 3;
+      const setDIR = joystickRes.joystickIDR ? 1 : 3;
       streamData = {
         frame: this.joystickFrameCnt,
         clipPath: mediaSrc,
@@ -929,7 +942,6 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     });
   }
 
-
   handleMoveSteam() {
     this.moveQueueSubscription = this.moveQueue.subscribe(
       async (stream: StreamFrameType) => {
@@ -1044,8 +1056,8 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     this.peer = peer;
     this.streamService.setChannel(channel);
     this.startSteaming.next(true);
-    this.startStream();
-    this.handleStream();
+    // this.startStream();
+    // this.handleStream();
   }
 
   handleDataChanelClose(): void {
@@ -1193,7 +1205,13 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
             this.peer.bytesSent(),
           );
           if (frame === 1) {
-            redisData = await this.rotateService.echo(this.user_id, true);
+            // redisData = await this.rotateService.echo(this.user_id, true);
+            const app_id = this.configService.get('app.appId');
+            console.log('首页数据', app_id, this.user_id);
+            redisData = this.rotateService.getFirstStreamData(
+              app_id,
+              this.user_id,
+            );
             this.logger.log('获取-首屏', redisData);
             this.onSteaming = true;
             this.holdSteam();
@@ -1271,7 +1289,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           }
         } catch (error) {
           if (this.frameCnt.getValue() === 1) {
-            this.logger.error('首屏读取redis有误:', redisData, error.message);
+            this.logger.error('首屏读取有误:', redisData, error.message);
           }
           this.stopStream();
           this.logger.error('handleStream', error.message);

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

@@ -85,6 +85,7 @@ interface StreamReplyType {
   DIR: ?number;
   moveStart?: boolean;
   moveOver?: boolean;
+  joystickIDR?: boolean;
 }
 
 // interface NewUserStatesType{