Browse Source

优化更多记录

gemercheung 3 years ago
parent
commit
11f287ab6e

+ 5 - 4
src/meta.gateway.ts

@@ -35,11 +35,12 @@ initLogger('Debug');
   path: '/ws',
 })
 export class MetaGateway
-  implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
+  implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
+{
   constructor(
     private readonly sceneService: SceneService,
     private readonly configService: ConfigService,
-  ) { }
+  ) {}
   private logger: Logger = new Logger('MetaGateway');
   private peer: PeerConnection = null;
   private timer: NodeJS.Timeout;
@@ -181,7 +182,7 @@ export class MetaGateway
     });
 
     this.gameChanel = this.peer.createDataChannel('game-input', {
-      ordered: true,
+      // ordered: true,
       // negotiated: false
     });
 
@@ -290,7 +291,7 @@ export class MetaGateway
           }
         },
       );
-    } catch (error) { }
+    } catch (error) {}
   }
 
   handleConnection(client: WebSocket, ...args: any[]) {

+ 35 - 13
src/move/move.service.ts

@@ -270,10 +270,9 @@ export class MoveService implements OnModuleInit {
       );
       for (let i = 0; i < checkReplys.length; ++i) {
         checkReplys[i].actionResponses[0].actionType = actionType;
-        if(i == 0|| i == checkReplys.length-1){
+        if (i == 0 || i == checkReplys.length - 1) {
           checkReplys[i].isIDR = true;
-        }
-        else {
+        } else {
           checkReplys[i].isIDR = false;
         }
       }
@@ -300,7 +299,7 @@ export class MoveService implements OnModuleInit {
           return replys;
         }
 
-        let pathReplys = this.createCacheReplys(
+        const pathReplys = this.createCacheReplys(
           appId,
           moveFrames,
           traceId,
@@ -311,14 +310,16 @@ export class MoveService implements OnModuleInit {
         );
 
         //第一段
-        if(i == 0){   
+        if (i == 0) {
           //第一帧
           pathReplys[0].isIDR = true;
         }
         //最后一段
         else if (i == path.length - 2) {
           //最后一帧
-          pathReplys[pathReplys.length - 1]['newUserStates'][0].renderInfo.isMoving = 0;
+          pathReplys[pathReplys.length - 1][
+            'newUserStates'
+          ][0].renderInfo.isMoving = 0;
         }
         for (let j = 0; j < pathReplys.length; ++j) {
           pathReplys[j].actionResponses[0].actionType = actionType;
@@ -433,10 +434,9 @@ export class MoveService implements OnModuleInit {
 
       reply.startBreakPointId = startBreakPointId;
       reply.endBreakPointId = endBreakPointId;
-      if (i == moveFrames.length - 1){
+      if (i == moveFrames.length - 1) {
         reply.isIDR = true;
-      }
-      else{
+      } else {
         reply.isIDR = false;
       }
       replys.push(reply);
@@ -905,7 +905,10 @@ export class MoveService implements OnModuleInit {
 
       // const offsetX = playerPosition.x - user.player.position.x;
       // const offsetY = playerPosition.y - user.player.position.y;
-      console.log('handlejoysticktesttest:actionRequest-playerPosition'+JSON.stringify(playerPosition));
+      console.log(
+        'handlejoysticktesttest:actionRequest-playerPosition' +
+          JSON.stringify(playerPosition),
+      );
       //找到邻居点,判断user.player.position与邻居点的距离,如果距离小于closestDis,就要更新camera的position
       let chooseBreakPointId = null;
       const breakPoint = this.breakPointInfo[breakPointId];
@@ -1067,7 +1070,14 @@ export class MoveService implements OnModuleInit {
 
       //count == 2
       //超出范围了
-      console.log('handlejoysticktesttest:距离:'+JSON.stringify(playerPosition)+'->'+JSON.stringify(breakPoint.position)+':'+this.getDistance(playerPosition, breakPoint.position));
+      console.log(
+        'handlejoysticktesttest:距离:' +
+          JSON.stringify(playerPosition) +
+          '->' +
+          JSON.stringify(breakPoint.position) +
+          ':' +
+          this.getDistance(playerPosition, breakPoint.position),
+      );
       if (this.getDistance(playerPosition, breakPoint.position) > closestDis) {
         // let offsetAngle1 = Math.abs(_angle - neighPoints[0].angle);
         // if (neighPoints[0].angle == 0) {
@@ -1128,7 +1138,12 @@ export class MoveService implements OnModuleInit {
           //console.log('joystick自由--->不合并');
         }
         //console.log('handlejoysticktesttest:actionRequest-自由'+new Date().getTime());
-        console.log('handlejoysticktesttest:自由:'+this.reply.mediaSrc+'->'+JSON.stringify(user.player.position));
+        console.log(
+          'handlejoysticktesttest:自由:' +
+            this.reply.mediaSrc +
+            '->' +
+            JSON.stringify(user.player.position),
+        );
         return this.reply;
       }
     } catch (error) {
@@ -1301,7 +1316,14 @@ export class MoveService implements OnModuleInit {
         this.sendingFrameForJoystick = true;
       }
       //console.log('handlejoysticktesttest:actionRequest-镜头过渡'+new Date().getTime());
-      console.log('handlejoysticktesttest:actionRequest-镜头过渡:从'+breakPointId+'到'+chooseBreakPointId+','+JSON.stringify(user.player.position));
+      console.log(
+        'handlejoysticktesttest:actionRequest-镜头过渡:从' +
+          breakPointId +
+          '到' +
+          chooseBreakPointId +
+          ',' +
+          JSON.stringify(user.player.position),
+      );
       return this.reply;
     }
   }

+ 39 - 28
src/rotate/rotate.service.ts

@@ -183,7 +183,7 @@ export class RotateService {
       code: 0,
       msg: 'OK',
     };
-    
+
     reply['newUserStates'][0].playerState.player.position = JSON.parse(
       JSON.stringify(this.firstPoint.player.position),
     );
@@ -194,34 +194,45 @@ export class RotateService {
     return reply;
   }
 
-//首帧
-getFirstStreamData(appId,userId){
-  const user = this.users[userId];
-  let 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;
+  //首帧
+  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;
-}
+    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

+ 63 - 60
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;
@@ -287,7 +287,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);
@@ -326,6 +326,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     // 在未开始前开始
     if (!this.onRotating.value) {
       this._rotateStartFame.next(this.frameCnt.value);
+      console.log('旋转开始记录帧:::--->', this._rotateStartFame.value);
     }
 
     this.onRotating.next(true);
@@ -336,12 +337,14 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     stream.DIR = this.rotateFirstIDR ? 1 : IDRflag;
 
     console.log(
-      '[旋转信息:::info--->]:clipPath: %s, frameCnt: %s,_rotateCountFame: %s, stream.frame %s , IDRflag: %s',
+      '[旋转信息:::info--->]:clipPath: %s, main-frameCnt: %s, stream.frame %s ,_rotateStartFame: %s, _rotateCountFame: %s, IDRflag: %s',
       stream.clipPath,
       // this._rotateStartFame.value,
       this.frameCnt.value,
-      this._rotateCountFame,
       stream.frame,
+      this._rotateStartFame.value,
+      this._rotateCountFame,
+
       // this.rotateframeCnt,
       IDRflag,
     );
@@ -479,16 +482,16 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     const playerAngle = newUserStates.playerState.player.angle;
     this.logger.log(
       'stop-data-0' +
-      'trace_id: ' +
-      trace_id +
-      'userId:' +
-      userId +
-      'breakPointId :' +
-      breakPointId +
-      'cameraAngle :' +
-      JSON.stringify(cameraAngle) +
-      'playerAngle: ' +
-      JSON.stringify(playerAngle),
+        'trace_id: ' +
+        trace_id +
+        'userId:' +
+        userId +
+        'breakPointId :' +
+        breakPointId +
+        'cameraAngle :' +
+        JSON.stringify(cameraAngle) +
+        'playerAngle: ' +
+        JSON.stringify(playerAngle),
     );
     //debugger;
     console.log('moveService.stop-1:' + breakPointId);
@@ -691,15 +694,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);
 
@@ -754,16 +757,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;
@@ -818,7 +821,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;
 
@@ -853,8 +856,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;
@@ -955,7 +958,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));
@@ -1037,14 +1040,14 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           };
           this.logger.log(
             '[media-move]: ' +
-            ', moveframeCnt: ' +
-            this.moveframeCnt +
-            ', clipPath: ' +
-            stream.clipPath +
-            ', mType: ' +
-            stream.mType +
-            ', DIR: ' +
-            stream.DIR,
+              ', moveframeCnt: ' +
+              this.moveframeCnt +
+              ', clipPath: ' +
+              stream.clipPath +
+              ', mType: ' +
+              stream.mType +
+              ', DIR: ' +
+              stream.DIR,
             // stream.metaData,
           );
           this.logger.log(
@@ -1292,12 +1295,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) {
@@ -1319,7 +1322,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,
@@ -1347,17 +1350,17 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
             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,
             );
           }
 

+ 13 - 1
src/scene/stream/stream.service.ts

@@ -53,6 +53,12 @@ export class StreamService {
         const metaDataBin = metaData.replace(/\s/g, '');
         const buff = Buffer.from(metaDataBin, 'utf-8');
 
+        if (stream.frame < 0) {
+          this.logger.error('不存在的帧位::' + stream.frame);
+          console.error('不存在的帧位::' + stream.frame);
+          return resolve({ frame: stream.frame, done: false });
+        }
+
         // const block = 36;
         const blockBuff = Buffer.alloc(this.block);
         const packBuffer = Buffer.concat([blockBuff, buff]);
@@ -118,6 +124,12 @@ export class StreamService {
         // 其次其他地方会拿这里的最后一帧数据会出错,由于上流数据很多不稳定问题尽可保流的稳定性。
         if (!existsSync(clipPath)) {
           this.logger.error('不存在的推流路径::' + clipPath);
+          console.error('不存在的推流路径::' + clipPath);
+          return resolve({ frame: stream.frame, done: false });
+        }
+        if (stream.frame < 0) {
+          this.logger.error('不存在的帧位::' + stream.frame);
+          console.error('不存在的帧位::' + stream.frame);
           return resolve({ frame: stream.frame, done: false });
         }
         // const clipPath = stream.clipPath;
@@ -153,7 +165,7 @@ export class StreamService {
         console.log(
           'stream-size %s : frame: %s, IDR: %s',
           ((coordBuff.byteLength + clipBuffer.byteLength) / 1024).toFixed(2) +
-          ' KB',
+            ' KB',
           frame,
           dir,
         );