gemercheung 3 years ago
parent
commit
9dde1a6cbd
2 changed files with 49 additions and 44 deletions
  1. 10 8
      src/meta.gateway.ts
  2. 39 36
      src/scene/scene.service.ts

+ 10 - 8
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;
@@ -187,14 +188,15 @@ export class MetaGateway
     });
 
     this.gameChanel.onOpen(() => {
-      console.log('channel is open');
-      this.sceneService.handleDataChanelOpen(this.gameChanel, this.peer);
-      const peers = this.peer.getSelectedCandidatePair();
-      this.logger.log('配对成功', JSON.stringify(peers));
-
+      this.logger.log('channel is open');
       if (this.gameChanel.isOpen()) {
+        const peers = this.peer.getSelectedCandidatePair();
+        this.logger.log('配对成功', JSON.stringify(peers));
         console.log('gameChanel', this.gameChanel.isOpen());
         this.sendWertcHeartPack(this.gameChanel);
+        this.sceneService.handleDataChanelOpen(this.gameChanel, this.peer);
+      } else {
+        console.log('gameChanel has problem');
       }
 
       // Number.prototype.padLeft = function (n, str) {
@@ -286,7 +288,7 @@ export class MetaGateway
           }
         },
       );
-    } catch (error) { }
+    } catch (error) {}
   }
 
   handleConnection(client: WebSocket, ...args: any[]) {

+ 39 - 36
src/scene/scene.service.ts

@@ -26,7 +26,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;
@@ -324,16 +324,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;
         redisMeta = await this.moveService.stop(
@@ -588,7 +588,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-1', joystickRes);
@@ -754,14 +754,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(
@@ -951,8 +951,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
           const IDRflag = this._rotateCurrentFame % 5 === 0 ? 1 : 3;
           this.logger.log(
-            `当前rotate ,mainframeCnt:${this.frameCnt.getValue()}, _rotateCurrentFame:${
-              this._rotateCurrentFame
+            `当前rotate ,mainframeCnt:${this.frameCnt.getValue()}, _rotateCurrentFame:${this._rotateCurrentFame
             } IDRflag:${IDRflag}`,
           );
           stream.DIR = this.rotateFirstIDR ? 1 : IDRflag;
@@ -963,12 +962,12 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
           this.logger.log(
             '[media-rotate]: ' +
-              ', frame: ' +
-              stream.frame +
-              ', rotateframeCnt: ' +
-              this.rotateframeCnt +
-              ', clipPath: ' +
-              stream.clipPath,
+            ', frame: ' +
+            stream.frame +
+            ', rotateframeCnt: ' +
+            this.rotateframeCnt +
+            ', clipPath: ' +
+            stream.clipPath,
             // stream.metaData,
           );
           // this.logger.log(
@@ -1019,21 +1018,22 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   }
   handleStream() {
     this.logger.log('this.frameCntSubscription', this.frameCntSubscription);
+    let redisData;
     if (!this.frameCntSubscription) {
       this.frameCntSubscription = this.frameCnt.subscribe(async (frame) => {
         try {
           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(),
           );
           if (frame === 1) {
-            const redisData = await this.rotateService.echo(this.user_id, true);
+            redisData = await this.rotateService.echo(this.user_id, true);
             this.logger.log('获取-首屏', redisData);
             this.onSteaming = true;
             this.holdSteam();
@@ -1045,7 +1045,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,
@@ -1089,6 +1089,9 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
             }
           }
         } catch (error) {
+          if (this.frameCnt.getValue() === 1) {
+            this.logger.error('首屏读取redis有误:', redisData, error.message);
+          }
           this.stopStream();
           this.logger.error('handleStream', error.message);
         }