gemercheung 3 years ago
parent
commit
e2ab2c6e4d
2 changed files with 111 additions and 168 deletions
  1. 12 8
      src/move/move.service.ts
  2. 99 160
      src/scene/scene.service.ts

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

@@ -290,9 +290,9 @@ export class MoveService implements OnModuleInit {
         }
         }
         const moveFrames = JSON.parse(moveFramesRes);
         const moveFrames = JSON.parse(moveFramesRes);
         */
         */
-       if(index<0){
-        debugger;
-       }
+        if (index < 0) {
+          debugger;
+        }
         const moveFrames = await this.getMoveFrames(
         const moveFrames = await this.getMoveFrames(
           appId,
           appId,
           start_break_point_id,
           start_break_point_id,
@@ -1333,7 +1333,9 @@ export class MoveService implements OnModuleInit {
   }
   }
 
 
   complementFrame(userId) {
   complementFrame(userId) {
-    console.log('handlejoysticktesttest:complementFrame:'+this.cameraInfos.length);
+    console.log(
+      'handlejoysticktesttest:complementFrame:' + this.cameraInfos.length,
+    );
     if (this.cameraInfos.length > 0) {
     if (this.cameraInfos.length > 0) {
       const user = this.users[userId];
       const user = this.users[userId];
       const cameraInfo = this.cameraInfos.shift();
       const cameraInfo = this.cameraInfos.shift();
@@ -1372,11 +1374,13 @@ export class MoveService implements OnModuleInit {
     }
     }
   }
   }
 
 
-  complementFrames(userId){
-    console.log('handlejoysticktesttest:complementFrames:'+this.cameraInfos.length);
+  complementFrames(userId) {
+    console.log(
+      'handlejoysticktesttest:complementFrames:' + this.cameraInfos.length,
+    );
     const user = this.users[userId];
     const user = this.users[userId];
-    let replys = [];
-    while(this.cameraInfos.length>0){
+    const replys = [];
+    while (this.cameraInfos.length > 0) {
       const cameraInfo = this.cameraInfos.shift();
       const cameraInfo = this.cameraInfos.shift();
       this.reply.traceIds = [];
       this.reply.traceIds = [];
       this.reply['newUserStates'][0].userId = userId;
       this.reply['newUserStates'][0].userId = userId;

+ 99 - 160
src/scene/scene.service.ts

@@ -33,7 +33,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   public _frameTimeout: NodeJS.Timeout;
   public _frameTimeout: NodeJS.Timeout;
   public _rotateTimeout: NodeJS.Timeout;
   public _rotateTimeout: NodeJS.Timeout;
   public _moveTimeout: NodeJS.Timeout;
   public _moveTimeout: NodeJS.Timeout;
-  public _JoyStickingTimeout: NodeJS.Timeout;
+  // public _JoyStickingTimeout: NodeJS.Timeout;
   public _JoyStickingSteamTimeout: NodeJS.Timeout;
   public _JoyStickingSteamTimeout: NodeJS.Timeout;
   public startSteaming = new BehaviorSubject<boolean>(false);
   public startSteaming = new BehaviorSubject<boolean>(false);
   public onRotating = new BehaviorSubject<boolean>(false);
   public onRotating = new BehaviorSubject<boolean>(false);
@@ -563,99 +563,115 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     }
     }
   }
   }
 
 
-  /***
-   * joystick
+  /**
+   * joystick 二合一推流
+   * @param joystickRes StreamMetaType | StreamFrameType;
    */
    */
+  async handlePushJoyStickSteam(joystickRes: StreamReplyType) {
+    this.holdSteam();
+    console.log('joystickRes有mediaSrc', joystickRes.mediaSrc);
+    let streamData: StreamFrameType | StreamMetaType;
 
 
-  async handlejoystick(request: JoystickRequest) {
-    // 二级方法start,为了 drain干complementFrame的pool
-    const pushJoyStickSteam = async (
-      joystickRes: StreamReplyType,
-      _noNeedSleep = false,
-    ) => {
-      this.holdSteam();
-      console.log('joystickRes有mediaSrc', joystickRes.mediaSrc);
+    // if (this.joystickFrameCnt === -1) {
+    this.joystickFrameCnt = this.frameCnt.getValue();
+    // }
+    this.joystickFrameCnt += 1;
+    const hasMedia = joystickRes?.mediaSrc && joystickRes?.mediaSrc.length > 0;
+
+    if (hasMedia) {
       const mediaSrc = joystickRes.mediaSrc.split('?')[0];
       const mediaSrc = joystickRes.mediaSrc.split('?')[0];
       // IDR flag设置为I帧
       // IDR flag设置为I帧
       const setDIR = joystickRes.moveOver || joystickRes.moveStart ? 1 : 3;
       const setDIR = joystickRes.moveOver || joystickRes.moveStart ? 1 : 3;
-      // 过滤新东西, 推完给回false
-      this.moveService.sendingFrameForJoystick = true;
-
-      // if (this.joystickFrameCnt === -1) {
-      this.joystickFrameCnt = this.frameCnt.getValue();
-      // }
-      this.joystickFrameCnt += 1;
-      const streamData: StreamFrameType = {
+      streamData = {
         frame: this.joystickFrameCnt,
         frame: this.joystickFrameCnt,
         clipPath: mediaSrc,
         clipPath: mediaSrc,
         metaData: JSON.stringify(joystickRes),
         metaData: JSON.stringify(joystickRes),
         serverTime: this.mockserverTime,
         serverTime: this.mockserverTime,
         DIR: setDIR,
         DIR: setDIR,
       };
       };
+    } else {
+      streamData = {
+        frame: this.joystickFrameCnt,
+        metaData: JSON.stringify(joystickRes),
+      };
+    }
 
 
-      const hasPush = await this.streamService.pushFrameToSteam(streamData);
-      if (hasPush.done) {
-        this.isJoystickHasStream = true;
-        console.log('joystick-hasPush', hasPush);
-        if (this.isJoystickHasStream && !_noNeedSleep) {
-          await this.sleep(20);
-        }
-        this.moveService.sendingFrameForJoystick = false;
-        const data = joystickRes as StreamReplyType;
-        console.log('handlejoystick-moveOver:' + data.moveOver);
-        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);
-        }
-        // 共用_JoyStickingTimeout
-        this.frameCnt.next(hasPush.frame);
-        clearTimeout(this._JoyStickingSteamTimeout);
-        this._JoyStickingSteamTimeout = setTimeout(async () => {
-          const complementFrame = this.moveService.complementFrame(
-            this.user_id,
-          ) as StreamReplyType;
-          console.log('has-complementFrame', complementFrame);
-          if (complementFrame) {
-            const start = performance.now();
-            pushJoyStickSteam(complementFrame, true);
-            const stop = performance.now();
-            const inMillSeconds = stop - start;
-            const rounded = Number(inMillSeconds).toFixed(3);
-            console.log(`complementFrame调用时间---->${rounded}`);
-          } else {
-            this.logger.log('joystick opt done');
-            this.logger.log('joystick 交权给空流,当前pts', hasPush.frame);
-            const stopFrameData = this.moveService.stopJoystick(this.user_id);
-            const stopFrame: StreamMetaType = {
-              frame: hasPush.frame + 1,
-              metaData: JSON.stringify(stopFrameData),
-            };
-            const stopRes = await this.streamService.pushMetaDataToSteam(
-              stopFrame,
-            );
-            console.log('stopFrame', stopFrame);
-            if (stopRes.done) {
-              this.onJoysticking.next(false);
-              this.resumeStream();
-              this.joystickFrameCnt = -1;
-              this.isJoystickHasStream = false;
-            }
-            // this.frameCnt.next(res.frame);
-          }
-        }, 200);
-      } else {
-        console.error('joystick-流地址有误::', joystickRes.mediaSrc);
-        this.logger.error('joystick-流地址有误::', joystickRes.mediaSrc);
-        this.resumeStream();
+    // 过滤新东西, 推完给回false
+    this.moveService.sendingFrameForJoystick = true;
+
+    const hasPush = hasMedia
+      ? await this.streamService.pushFrameToSteam(streamData as StreamFrameType)
+      : await this.streamService.pushMetaDataToSteam(
+        streamData as StreamMetaType,
+      );
+
+    if (hasPush.done) {
+      this.isJoystickHasStream = true;
+      console.log('joystick-hasPush', hasPush);
+      if (this.isJoystickHasStream) {
+        await this.sleep(20);
       }
       }
-    };
-    // 二级方法end
+      this.moveService.sendingFrameForJoystick = false;
+      const data = joystickRes as StreamReplyType;
+      console.log('handlejoystick-moveOver:' + data.moveOver);
+      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.frameCnt.next(hasPush.frame);
+
+      // 最后一帧200ms
+      clearTimeout(this._JoyStickingSteamTimeout);
+      this._JoyStickingSteamTimeout = setTimeout(async () => {
+        const complementFrame = this.moveService.complementFrame(
+          this.user_id,
+        ) as StreamReplyType;
+        console.log('has-complementFrame', complementFrame);
+        if (complementFrame) {
+          const start = performance.now();
+          this.handlePushJoyStickSteam(complementFrame);
+          const stop = performance.now();
+          const inMillSeconds = stop - start;
+          const rounded = Number(inMillSeconds).toFixed(3);
+          console.log(`complementFrame调用时间---->${rounded}`);
+        } else {
+          this.logger.log('joystick opt done');
+          this.logger.log('joystick 交权给空流,当前pts', hasPush.frame);
+          // const stopFrameData = this.moveService.stopJoystick(this.user_id);
+          // const stopFrame: StreamMetaType = {
+          //   frame: hasPush.frame + 1,
+          //   metaData: JSON.stringify(stopFrameData),
+          // };
+          // const stopRes = await this.streamService.pushMetaDataToSteam(
+          //   stopFrame,
+          // );
+          // console.log('stopFrame', stopFrame);
+          // if (stopRes.done) {
+          // }
+          this.onJoysticking.next(false);
+          this.resumeStream();
+          this.joystickFrameCnt = -1;
+          this.isJoystickHasStream = false;
+          // this.frameCnt.next(res.frame);
+        }
+      }, 200);
+    } else {
+      console.error('joystick-流地址有误::', joystickRes.mediaSrc);
+      this.logger.error('joystick-流地址有误::', joystickRes.mediaSrc);
+      this.resumeStream();
+    }
+  }
+
+  /***
+   * joystick
+   */
 
 
+  async handlejoystick(request: JoystickRequest) {
     try {
     try {
       //const joystickRes = await this.moveService.joystick(request);
       //const joystickRes = await this.moveService.joystick(request);
       this._rotateCount = 0;
       this._rotateCount = 0;
@@ -669,13 +685,6 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
 
 
       if (joystickRes) {
       if (joystickRes) {
         this.onJoysticking.next(true);
         this.onJoysticking.next(true);
-        // console.log(
-        //   'handlejoystick:' +
-        //     joystickRes.mediaSrc +
-        //     ',相机坐标:' +
-        //     JSON.stringify(
-        //       joystickRes.newUserStates[0].playerState.camera.position,
-        //     ),
         console.log(
         console.log(
           'handlejoysticktesttest:' +
           'handlejoysticktesttest:' +
           joystickRes.mediaSrc +
           joystickRes.mediaSrc +
@@ -684,82 +693,12 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
             joystickRes.newUserStates[0].playerState.player.position,
             joystickRes.newUserStates[0].playerState.player.position,
           ),
           ),
         );
         );
-        if (joystickRes.mediaSrc) {
-          pushJoyStickSteam(joystickRes);
-        } else {
-          if (!this.onMoving.getValue()) {
-            // 在非行走时接受
-            this.holdSteam();
-            // if (this.joystickFrameCnt === -1) {
-            this.joystickFrameCnt = this.frameCnt.getValue();
-            // }
-            // 人物数据去掉mediaSrc以免前误会
-            joystickRes?.mediaSrc && delete joystickRes.mediaSrc;
-
-            this.joystickFrameCnt += 1;
-            const stream: StreamMetaType = {
-              frame: this.joystickFrameCnt,
-              metaData: JSON.stringify(joystickRes),
-            };
-            //this.logger.log('rotate', stream, Date.now());
-            const res = await this.streamService.pushMetaDataToSteam(stream);
-            if (res.done) {
-              this.logger.log('joystick-位置流', res.frame);
-              this.frameCnt.next(res.frame);
-              clearTimeout(this._JoyStickingTimeout);
-              this._JoyStickingTimeout = setTimeout(async () => {
-                this.logger.log('joystick-位置流 opt done');
-                this.logger.log(
-                  'joystick-位置流 交权给空流,当前pts',
-                  res.frame,
-                );
-                // const complementFrame = this.moveService.complementFrame(
-                //   this.user_id,
-                // ) as StreamReplyType;
-                // console.log('has-complementFrame', complementFrame);
-                // if (complementFrame) {
-                //   pushJoyStickSteam(complementFrame);
-                // } else {
-                // this.moveService.stopJoystick(this.user_id);
-                const stopFrameData = this.moveService.stopJoystick(
-                  this.user_id,
-                );
-                const stopFrame: StreamMetaType = {
-                  frame: res.frame + 1,
-                  metaData: JSON.stringify(stopFrameData),
-                };
-                const stopRes = await this.streamService.pushMetaDataToSteam(
-                  stopFrame,
-                );
-                if (stopRes.done) {
-                  this.resumeStream();
-                  this.joystickFrameCnt = -1;
-                  this.isJoystickHasStream = false;
-                  this.onJoysticking.next(false);
-                }
-
-                // }
-              }, 200);
-            } else {
-              console.error(
-                'joystick-位置流有误::',
-                res.frame,
-                joystickRes.mediaSrc,
-              );
-              this.logger.error(
-                'joystick-位置流有误::',
-                res.frame,
-                joystickRes.mediaSrc,
-              );
-              this.onJoysticking.next(false);
-              this.joystickFrameCnt = -1;
-              this.resumeStream();
-            }
-          }
+        if (!this.onMoving.getValue()) {
+          this.handlePushJoyStickSteam(joystickRes);
         }
         }
       } else {
       } else {
         console.log('handlejoystick:null');
         console.log('handlejoystick:null');
-        this.onJoysticking.next(false);
+        // this.onJoysticking.next(false);
       }
       }
     } catch (error) {
     } catch (error) {
       console.error('joystick错误', error);
       console.error('joystick错误', error);