Browse Source

Merge branch 'master' of http://192.168.0.115:3000/zhangyupeng/meta-server

gemercheung 3 years ago
parent
commit
8775dffabf
4 changed files with 363 additions and 13 deletions
  1. 107 0
      src/get-router/get-router.service.ts
  2. 235 12
      src/move/move.service.ts
  3. 2 0
      src/rotate/rotate.service.ts
  4. 19 1
      src/scene/scene.service.ts

+ 107 - 0
src/get-router/get-router.service.ts

@@ -214,6 +214,113 @@ export class GetRouterService implements OnModuleInit {
     }
   }
 
+
+  searchRoad2(startPointId, endPointId) {
+    try {
+      if (startPointId == endPointId) {
+        console.log('点击的点和当前点是同一个点');
+        return null;
+      }
+
+      const startPoint = this.breakPointInfo[startPointId];
+      if (startPoint.contact.indexOf(endPointId) > -1) {
+        return [startPointId, endPointId];
+      }
+
+      const endPoint = this.breakPointInfo[endPointId];
+      const openList = [], //开启列表
+        closeList = []; //关闭列表
+      let result = []; //结果数组
+      let result_index: number; //结果数组在开启列表中的序号
+
+      //把当前点加入到开启列表中,并且G是0
+      openList.push({
+        breakPointId: startPointId,
+        G: 0,
+        position: startPoint.position,
+        contact: startPoint.contact,
+      }); //把当前点加入到开启列表中,并且G是0
+
+      do {
+        const currentPoint = openList.pop();
+        closeList.push(currentPoint);
+        if (closeList.length > 10000) {
+          console.log('错误过渡路径:', closeList.length);
+          return null;
+        }
+        let surroundPoint = [];
+        const _breakPoint = this.breakPointInfo[currentPoint.breakPointId];
+        surroundPoint = _breakPoint.contact;
+        for (let i = 0; i < surroundPoint.length; ++i) {
+          const neighPointId = surroundPoint[i];
+          const item = this.breakPointInfo[neighPointId];
+          if (this.existList(item, closeList) != -1) {
+            continue;
+          }
+          item.breakPointId = neighPointId;
+          //g 到父节点的位置
+          const g =
+            currentPoint.G +
+            this.getDistance(currentPoint.position, item.position);
+          if (this.existList(item, openList) == -1) {
+            //如果不在开启列表中
+            item['H'] = this.getDistance(endPoint.position, item.position);
+            item['G'] = g;
+            item['F'] = item.H + item.G;
+            item['parent'] = currentPoint;
+            openList.push(item);
+          } else {
+            //存在在开启列表中,比较目前的g值和之前的g的大小
+            const index = this.existList(item, openList);
+            //如果当前点的g更小
+            if (g < openList[index].G) {
+              openList[index].parent = currentPoint;
+              openList[index].G = g;
+              openList[index].F = g + openList[index].H;
+            }
+          }
+        }
+        //如果开启列表空了,没有通路,结果为空
+        if (openList.length == 0) {
+          break;
+        }
+
+        openList.sort(this.sortF); //这一步是为了循环回去的时候,找出 F 值最小的, 将它从 "开启列表" 中移掉
+      } while ((result_index = this.existList(endPoint, openList)) == -1);
+
+      //判断结果列表是否为空
+      let currentObj;
+      if (result_index == -1) {
+        result = [];
+      } else {
+        currentObj = openList[result_index];
+        do {
+          //把路劲节点添加到result当中
+          result.unshift(currentObj.breakPointId);
+          currentObj = currentObj.parent;
+          if (!currentObj) {
+            break;
+          }
+        } while (
+          result.length < 50 &&
+          currentObj.contact.indexOf(startPointId) < 0
+        );
+      }
+      if (result.length > 50) {
+        console.log('错误过渡路径:' + result);
+        // debugger;
+        return null;
+      }
+      result.unshift(currentObj.breakPointId);
+      result.unshift(startPointId);
+      console.log('path-end' + result);
+      return result;
+    } catch (error) {
+      console.error('searchRoad', error);
+      return [];
+    }
+  }
+
   //用F值对数组排序
   sortF(a, b) {
     return b.F - a.F;

+ 235 - 12
src/move/move.service.ts

@@ -9,6 +9,10 @@ import configuration from 'src/config/configuration';
 // import * as  BreakPointIds from '../../ws/points-BreakPointId.json';
 // import { SceneService } from 'src/scene/scene.service';
 
+const MaxDelayTime = 80;   //超过这个值就需要干预(操作杆)
+const MaxBufferCameraInfo = 3;   //超过这个值就需要干预(操作杆)
+const MaxNet = 3;
+
 const seqExeAsyncFn = (asyncFn) => {
   let runPromise = null;
   return function seq(...args) {
@@ -27,8 +31,6 @@ const seqExeAsyncFn = (asyncFn) => {
   };
 };
 
-let moveInterval = 3;
-
 @Injectable()
 export class MoveService implements OnModuleInit {
   constructor(
@@ -357,7 +359,7 @@ export class MoveService implements OnModuleInit {
     );
     const user = this.users[userId];
     let i;
-    for (i = 1; i < moveFrames.length; i += moveInterval) {
+    for (i = 1; i < moveFrames.length; i+=3) {
       const moveFrame = moveFrames[i];
       const reply = JSON.parse(JSON.stringify(this.reply));
       if (reply.traceIds.indexOf(traceId) == -1) {
@@ -660,12 +662,13 @@ export class MoveService implements OnModuleInit {
       const traceId = actionRequest['trace_id'];
       const dir_action = actionRequest['dir_action'];
       const actionType = actionRequest['action_type'];
+      const time_delay = actionRequest['time_delay'];
       const user = this.users[userId];
       const breakPointId = user.breakPointId;
       const appId = user.appId;
-      moveInterval = 1;
       const replys = [];
-      const step = 0.5;
+      const step = 0.4;
+      //const step = 1;
       const closestDis = 50; //小于这个距离就跳到邻居呼吸点
       const distance = step * dir_action.speed_level;
       let angle = null;
@@ -794,6 +797,21 @@ export class MoveService implements OnModuleInit {
         );
         this.reply.actionResponses[0].actionType = actionType;
 
+        console.log('20220708_延时->'+time_delay);
+        // //网络延迟的情况下,就暂时不推流
+        if(time_delay && time_delay>MaxDelayTime){
+          user.netQua = 0;
+        }
+        else if(time_delay && time_delay<MaxDelayTime && user.netQua<MaxNet){
+          ++user.netQua
+        }
+
+        if(user.netQua<MaxNet){
+          if(user.lastJoyStickMediaSrc){
+            user.lastJoyStickMediaSrc = false;
+            return this.reply;
+          }
+        }
         const cameraInfo = this.getCameraInfo();
         if (cameraInfo != null) {
           console.log(
@@ -807,6 +825,8 @@ export class MoveService implements OnModuleInit {
           if (cameraInfo.mediaSrc) {
             this.reply.mediaSrc = cameraInfo.mediaSrc;
             this.reply.isIDR = cameraInfo.isIDR;
+            user.lastJoyStickMediaSrc = true;
+            console.log('20220708_mediaSrc1:'+cameraInfo.mediaSrc);
           }
 
           user.camera.position = JSON.parse(
@@ -1060,9 +1080,9 @@ export class MoveService implements OnModuleInit {
         // } else {
         //   chooseBreakPointId = neighPoints[0].breakPointId;
         // }
-        if (closestNeighorId == null) {
-          debugger;
-        }
+        // if (closestNeighorId == null) {
+        //   debugger;
+        // }
         chooseBreakPointId = closestNeighorId;
         return await this.moveCamera(
           breakPointId,
@@ -1255,7 +1275,7 @@ export class MoveService implements OnModuleInit {
         chooseBreakPointId,
         index,
       );
-      this.setCameraInfo(appId, moveFrames, breakPointId, chooseBreakPointId);
+      this.setCameraInfo(appId, userId, moveFrames, breakPointId, chooseBreakPointId);
 
       user.breakPointId = chooseBreakPointId;
 
@@ -1294,8 +1314,23 @@ export class MoveService implements OnModuleInit {
     }
   }
 
-  setCameraInfo(appId, moveFrames, startBreakPointId, endBreakPointId) {
-    for (let i = 0; i < moveFrames.length; i += moveInterval) {
+  setCameraInfo(appId, userId,moveFrames, startBreakPointId, endBreakPointId) {
+    // let moveInterval = 1;
+    // console.log('20220708_cameraInfos设置->'+this.cameraInfos.length);
+    // if(this.cameraInfos.length>MaxBufferCameraInfo){
+    //   moveInterval = 1;
+    // }
+    const user = this.users[userId];
+    let moveInterval = 2;
+    let flag = false;
+    for (let i = 0; i < moveFrames.length; ++i) {
+      if(user.netQua<MaxNet){
+        if(i!= 0 && i!= moveFrames.length-1 && i%moveInterval == 0){
+          continue;
+        }
+      }
+
+      moveFrames[i].startBreakPointId = startBreakPointId;
       moveFrames[i].endBreakPointId = endBreakPointId;
       moveFrames[i].mediaSrc =
         '/' +
@@ -1328,13 +1363,55 @@ export class MoveService implements OnModuleInit {
           '?m=' +
           new Date().getTime();
       }
+      console.log('20220708_mediaSrc3:'+moveFrames[i].mediaSrc);
       if (i == 0 || i == moveFrames.length - 1) {
         moveFrames[i].isIDR = true;
+        if(i == moveFrames.length - 1){
+          flag = true;
+        }
       } else {
         moveFrames[i].isIDR = false;
       }
       this.cameraInfos.push(moveFrames[i]);
     }
+    if(!flag){
+      const cameraInfo = moveFrames[moveFrames.length-1];
+      cameraInfo.endBreakPointId = endBreakPointId;
+      cameraInfo.mediaSrc =
+        '/' +
+        appId +
+        '/' +
+        startBreakPointId +
+        '/' +
+        cameraInfo.file_name.substring(
+          0,
+          cameraInfo.file_name.indexOf('.'),
+        ) +
+        '/' +
+        cameraInfo.file_name +
+        '?m=' +
+        new Date().getTime();
+
+      if (startBreakPointId > endBreakPointId) {
+        cameraInfo.mediaSrc =
+          '/' +
+          appId +
+          '/' +
+          endBreakPointId +
+          '/' +
+          cameraInfo.file_name.substring(
+            0,
+            cameraInfo.file_name.indexOf('.'),
+          ) +
+          '/' +
+          cameraInfo.file_name +
+          '?m=' +
+          new Date().getTime();
+      }
+      cameraInfo.isIDR = true;
+      this.cameraInfos.push(cameraInfo);
+      console.log('20220708_mediaSrc4:'+cameraInfo.mediaSrc);
+    }
   }
 
   addCameraInfo(cameraPosition, cameraAngle, mediaSrc, isIDR) {
@@ -1349,6 +1426,7 @@ export class MoveService implements OnModuleInit {
   getCameraInfo() {
     if (this.cameraInfos.length > 0) {
       const item = this.cameraInfos.shift();
+      console.log('20220708_cameraInfos取值->'+this.cameraInfos.length);
       return item;
     } else {
       return null;
@@ -1389,7 +1467,6 @@ export class MoveService implements OnModuleInit {
       user.camera.position = JSON.parse(
         JSON.stringify(cameraInfo.camera_position),
       );
-
       user.camera.angle = JSON.parse(JSON.stringify(cameraInfo.camera_angle));
 
       this.sendingFrameForJoystick = true;
@@ -1405,6 +1482,152 @@ export class MoveService implements OnModuleInit {
     }
   }
 
+  // createDefaultReply(userId){
+  //   const user = this.users[userId];
+  //   this.reply.traceIds = [];
+  //   this.reply['newUserStates'][0].userId = userId;
+  //   this.reply['actionResponses'][0].traceId = null;
+  //   this.reply['newUserStates'][0].playerState.player.angle.yaw =
+  //     user.player.angle.yaw;
+  //   this.reply['newUserStates'][0].playerState.player.position = JSON.parse(
+  //     JSON.stringify(user.player.position),
+  //   );
+  //   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(user.player.position),
+  //   );
+  //   this.reply['newUserStates'][0].renderInfo.isMoving = 0;
+  //   this.reply['actionResponses'][0].traceId = null;
+  //   return JSON.parse(JSON.stringify(this.reply));
+  // }
+
+  async updateCameraInfoForDely(appId,userId,path){
+
+    if(path == null){
+      this.cameraInfos = [];
+      return;
+    }
+    const user = this.users[userId];
+    const index = this.getMoveIndex(user.camera.angle.yaw);
+    let frames = [];
+
+    let pointIds = [];
+
+    for (let i = 0; i < path.length - 1; ++i) {
+      const start_break_point_id = path[i];
+      const end_break_point_id = path[i + 1];
+      const moveFrames = await this.getMoveFrames(
+        appId,
+        start_break_point_id,
+        end_break_point_id,
+        index,
+      );
+      if (!moveFrames) {
+        return null;
+      }
+      frames = frames.concat(moveFrames)
+      pointIds.push({
+        index:frames.length,
+        startBreakPointId:start_break_point_id,
+        endBreakPointId:end_break_point_id
+      });
+    }
+    this.cameraInfos = [];
+    const interval = Math.floor(frames.length/12);
+    for (let i = 0; i < frames.length-1; i+=interval) {
+      const frame = frames[i];
+      let isIDR = false;
+      if(i == 0){
+        isIDR = true;
+      }
+
+      let startBreakPointId,endBreakPointId;
+      for(let j=0;j<pointIds.length;++j){
+        if(i<pointIds[j].index+1){
+          startBreakPointId = pointIds[j].startBreakPointId;
+          endBreakPointId = pointIds[j].endBreakPointId;
+          break;
+        }
+      }
+
+        let mediaSrc =
+        '/' +
+        appId +
+        '/' +
+        startBreakPointId +
+        '/' +
+        frame.file_name.substring(0, frame.file_name.indexOf('.')) +
+        '/' +
+        frame.file_name +
+        '?m=' +
+        new Date().getTime();
+      if (startBreakPointId > endBreakPointId) {
+        mediaSrc =
+          '/' +
+          appId +
+          '/' +
+          endBreakPointId +
+          '/' +
+          frame.file_name.substring(0, frame.file_name.indexOf('.')) +
+          '/' +
+          frame.file_name +
+          '?m=' +
+          new Date().getTime();
+      }
+      console.log('20220708_mediaSrc2:'+mediaSrc);
+      this.cameraInfos.push({
+        startBreakPointId:startBreakPointId,
+        endBreakPointId:endBreakPointId,
+        camera_position: frame.camera_position,
+        camera_angle: frame.camera_angle,
+        mediaSrc: mediaSrc,
+        isIDR: isIDR,
+      });
+    }
+
+    const startBreakPointId = pointIds[pointIds.length-1].startBreakPointId;
+    const endBreakPointId = pointIds[pointIds.length-1].endBreakPointId;
+    const frame = frames[frames.length-1];
+    let mediaSrc =
+        '/' +
+        appId +
+        '/' +
+        startBreakPointId +
+        '/' +
+        frame.file_name.substring(0, frame.file_name.indexOf('.')) +
+        '/' +
+        frame.file_name +
+        '?m=' +
+        new Date().getTime();
+      if (startBreakPointId > endBreakPointId) {
+        frame.mediaSrc =
+          '/' +
+          appId +
+          '/' +
+          endBreakPointId +
+          '/' +
+          frame.file_name.substring(0, frame.file_name.indexOf('.')) +
+          '/' +
+          frame.file_name +
+          '?m=' +
+          new Date().getTime();
+      }
+      this.cameraInfos.push({
+        startBreakPointId:startBreakPointId,
+        endBreakPointId:endBreakPointId,
+        camera_position: frame.camera_position,
+        camera_angle: frame.camera_angle,
+        mediaSrc: mediaSrc,
+        isIDR: true,
+      });
+  }
+
   stopJoystick(userId) {
     console.log('20220627test:stopJoystick');
     this.reply.traceIds = [];

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

@@ -90,6 +90,7 @@ export class RotateService {
         horizontal_move: 0,
       },
       moveInfo: {},
+      netQua:3,
       // traceIds: [],
       // actionResponses:[]
     };
@@ -107,6 +108,7 @@ export class RotateService {
     user.camera.position = JSON.parse(
       JSON.stringify(this.firstPoint.camera.position),
     );
+    
     console.log('user-init', user);
     this.users[userId] = user;
 

+ 19 - 1
src/scene/scene.service.ts

@@ -547,6 +547,22 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     }
   }
 
+  async getSimplestCameraInfo(appId,userId){
+    if(this.moveService.cameraInfos.length>20){
+      const startBreakPointId = this.moveService.cameraInfos[0].startBreakPointId;
+      const endBreakPointId = this.moveService.cameraInfos[this.moveService.cameraInfos.length-1].endBreakPointId;
+
+      const path = this.getRouterService.searchRoad2(startBreakPointId,endBreakPointId);
+      if(path == null){
+        //可以清空cameraInfos
+        this.moveService.cameraInfos = [];
+      }
+      else{
+        await this.moveService.updateCameraInfoForDely(appId,userId,path);
+      }
+    }
+  }
+
   /**
    * 行走动作
    *
@@ -913,11 +929,13 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   /**
    * Joystick Stop function
    */
-  handleJoystickStop(hasPush: StreamPushResponse) {
+  async handleJoystickStop(hasPush: StreamPushResponse) {
     // 最后一帧200ms
     clearTimeout(this._JoyStickingSteamTimeout);
     this._JoyStickingSteamTimeout = setTimeout(async () => {
       console.log('20220627test-complementFrame handleJoystickStop 200ms之后');
+      const user = this.moveService.users[this.user_id];
+      await this.getSimplestCameraInfo(user.appId,this.user_id);
       const complementFrame = this.moveService.complementFrame(
         this.user_id,
       ) as StreamReplyType;