gemercheung 3 年之前
父節點
當前提交
310dfe333a
共有 3 個文件被更改,包括 83 次插入128 次删除
  1. 79 128
      src/scene/scene.service.ts
  2. 2 0
      src/scene/stream/stream.d.ts
  3. 2 0
      src/scene/stream/stream.service.ts

+ 79 - 128
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;
@@ -267,16 +267,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);
@@ -325,7 +325,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),
             );
 
             this.roQueue.next(stream);
@@ -429,15 +429,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);
 
@@ -507,16 +507,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;
@@ -572,7 +572,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);
@@ -581,11 +581,11 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
         this.onJoysticking.next(true);
         console.log(
           'handlejoystick:' +
-          joystickRes.mediaSrc +
-          ',相机坐标:' +
-          JSON.stringify(
-            joystickRes.newUserStates[0].playerState.camera.position,
-          ),
+            joystickRes.mediaSrc +
+            ',相机坐标:' +
+            JSON.stringify(
+              joystickRes.newUserStates[0].playerState.camera.position,
+            ),
         );
         if (joystickRes.mediaSrc) {
           this.holdSteam();
@@ -611,16 +611,18 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           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);
-            // debugger;
-            // this.moveService.updateUser(userId, breakPointId, lastReply);
+            if (data?.moveOver && data.moveOver) {
+              // moveOver
+              console.log('回传updateUser', data);
+              const userId = this.user_id;
+              // 回传点暂时有问题,待修复
+              const breakPointId = data.endBreakPointId || data.breakPointId;
+              const lastReply = JSON.stringify(joystickRes);
+              this.moveService.updateUser(userId, breakPointId, lastReply);
+              this.resumeStream();
+            }
           } else {
             console.error(
               'joystick-流地址有误::',
@@ -634,61 +636,6 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
             );
             this.resumeStream();
           }
-
-          // // 处理第一个镜头数据
-          // 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()) {
@@ -697,6 +644,9 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
             if (this.joystickFrameCnt === -1) {
               this.joystickFrameCnt = this.frameCnt.getValue();
             }
+            // 人物数据去掉mediaSrc以免前误会
+            joystickRes?.mediaSrc && delete joystickRes.mediaSrc;
+
             this.joystickFrameCnt += 1;
             const stream: StreamMetaType = {
               frame: this.joystickFrameCnt,
@@ -814,14 +764,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(
@@ -1017,7 +967,8 @@ 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;
@@ -1028,12 +979,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(
@@ -1095,12 +1046,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(),
           );
           if (frame === 1) {
             redisData = await this.rotateService.echo(this.user_id, true);
@@ -1115,7 +1066,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,
@@ -1143,15 +1094,15 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
             console.log(
               '空白流条件-->:' +
-              isOk +
-              ' onMoving: ' +
-              this.onMoving.value +
-              ' onRotating: ' +
-              this.onRotating.value +
-              ' onJoysticking: ' +
-              this.onJoysticking.value +
-              ' firstRender: ' +
-              this.firstRender,
+                isOk +
+                ' onMoving: ' +
+                this.onMoving.value +
+                ' onRotating: ' +
+                this.onRotating.value +
+                ' onJoysticking: ' +
+                this.onJoysticking.value +
+                ' firstRender: ' +
+                this.firstRender,
             );
           }
 

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

@@ -83,6 +83,8 @@ interface StreamReplyType {
   breakPointId?: number; //临时记录存在的点()
   mType?: string; //类型
   DIR: ?number;
+  moveStart?: boolean;
+  moveOver?: boolean;
 }
 
 // interface NewUserStatesType{

+ 2 - 0
src/scene/stream/stream.service.ts

@@ -6,12 +6,14 @@ import * as streamBuffers from 'stream-buffers';
 import { BehaviorSubject } from 'rxjs';
 import { CacheService } from 'src/cache/cache.service';
 import { join } from 'path';
+import { SceneService } from '../scene.service';
 
 @Injectable()
 export class StreamService {
   private channel: DataChannel;
   private readonly chunk_size = 16000;
   private readonly block = 36;
+  private SceneService: SceneService;
   private logger: Logger = new Logger('StreamService');
   public onSteaming = new BehaviorSubject<boolean>(false);
   public lastStreamFrame = new BehaviorSubject<StreamFrameType>({