gemercheung 3 years ago
parent
commit
ed17247b6c
8 changed files with 151 additions and 51 deletions
  1. 12 12
      config.yaml
  2. 1 0
      package.json
  3. 18 0
      src/cache/cache.service.ts
  4. 5 3
      src/meta.gateway.ts
  5. 5 3
      src/scene/scene.d.ts
  6. 99 32
      src/scene/scene.service.ts
  7. 1 1
      src/scene/stream/stream.service.ts
  8. 10 0
      yarn.lock

+ 12 - 12
config.yaml

@@ -3,25 +3,25 @@ http:
   port: 6688
 
 
-grpc:
-  url: '221.4.210.172:23000'
 # grpc:
-#   url: '192.168.0.47:3000'
-
-redis:
-  port: 26379
-  host: '221.4.210.172' #远程调试需要设置bindip 为0.0.0.0 并且设置密码
-  password: '' # 非远程不需要密码
-  decode_responses: true
-  db: 9
+#   url: '221.4.210.172:23000'
+grpc:
+  url: '192.168.0.200:3000'
 
 # redis:
-#   port: 6379
-#   host: '192.168.0.47' #远程调试需要设置bindip 为0.0.0.0 并且设置密码
+#   port: 26379
+#   host: '221.4.210.172' #远程调试需要设置bindip 为0.0.0.0 并且设置密码
 #   password: '' # 非远程不需要密码
 #   decode_responses: true
 #   db: 9
 
+redis:
+  port: 6379
+  host: '192.168.0.47' #远程调试需要设置bindip 为0.0.0.0 并且设置密码
+  password: '' # 非远程不需要密码
+  decode_responses: true
+  db: 9
+
 stun:
   server: ['stun:172.18.156.41:3478', 'stun:120.24.252.95:3478']
   portRangeBegin: 52000

+ 1 - 0
package.json

@@ -31,6 +31,7 @@
     "@nestjs/platform-socket.io": "^8.4.4",
     "@nestjs/platform-ws": "^8.4.4",
     "@nestjs/websockets": "^8.4.4",
+    "ajv": "^8.11.0",
     "buffer": "^6.0.3",
     "js-yaml": "^4.1.0",
     "multistream": "^4.1.0",

+ 18 - 0
src/cache/cache.service.ts

@@ -31,6 +31,24 @@ export class CacheService implements OnModuleInit {
       await this.client.set(key, value, 'EX', seconds);
     }
   }
+  // rpop
+  public async rpop(key: string) {
+    if (!this.client) {
+      await this.getClient();
+    }
+    const data = await this.client.rpop(key);
+    if (!data) return;
+    return JSON.parse(data);
+  }
+  //获取Range值的方法
+  public async lpop(key: string) {
+    if (!this.client) {
+      await this.getClient();
+    }
+    const data = await this.client.lpop(key);
+    if (!data) return;
+    return JSON.parse(data);
+  }
 
   //获取值的方法
   public async get(key: string) {

+ 5 - 3
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;
@@ -277,7 +278,7 @@ export class MetaGateway
         }
         // debugger
       });
-    } catch (error) { }
+    } catch (error) {}
   }
 
   handleConnection(client: WebSocket, ...args: any[]) {
@@ -290,6 +291,7 @@ export class MetaGateway
 
     this.user_id = params.get('userId');
     this.roomId = params.get('roomId');
+    this.sceneService.setConfig(this.user_id, this.roomId);
 
     this.logger.log(`Client connected:`);
 

+ 5 - 3
src/scene/scene.d.ts

@@ -6,7 +6,7 @@ interface SceneGrpcService {
   getBreakPoint(request: GetBreakPointRequest): Observable<any>;
   joystick(request: JoystickRequest): Observable<any>;
   echo(request: EchoRequest): Observable<any>;
-  test(request: TestRequest): Observable<any>;
+  exit(request: ExitRequest): Observable<any>;
 }
 
 interface Point {
@@ -24,8 +24,10 @@ interface Player {
   angle: Angle;
 }
 
-interface TestRequest {
-  name: string;
+interface ExitRequest {
+  action_type: number;
+  user_id: string;
+  trace_id: string;
 }
 interface RouteRequest {
   sLocation: Point;

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

@@ -76,7 +76,7 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   constructor(
     private cacheService: CacheService,
     private streamService: StreamService,
-  ) {}
+  ) { }
   @Client(grpcClientOptions) private readonly client: ClientGrpc;
   private sceneGrpcService: SceneGrpcService;
 
@@ -86,10 +86,15 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   public _frameInteval: NodeJS.Timeout;
   private channel: DataChannel;
   public startSteaming = new BehaviorSubject<boolean>(false);
-  public user_id: string;
-  public roomId: string;
+  private user_id: string;
+  private roomId: string;
   private onSteaming = false;
 
+  setConfig(user_id: string, roomId: string) {
+    this.user_id = user_id;
+    this.roomId = roomId;
+  }
+
   onModuleInit() {
     this.sceneGrpcService =
       this.client.getService<SceneGrpcService>('SceneGrpcService');
@@ -97,6 +102,9 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     this.streamService.onSteaming.subscribe((val) => {
       this.onSteaming = val;
     });
+    Number.prototype.padLeft = function (n, str) {
+      return Array(n - String(this).length + 1).join(str || '0') + this;
+    };
   }
 
   onModuleDestroy() {
@@ -121,40 +129,75 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     }
   }
 
+  exit(request: ExitRequest) {
+    const exitReply = this.sceneGrpcService.exit(request);
+    exitReply.subscribe((reply) => {
+      console.log('exitReply', reply);
+    });
+  }
+
   move(request: MoveRequest) {
     return this.sceneGrpcService.move(request);
   }
 
-  rotate(request: RotateRequest) {
-    const reply = this.sceneGrpcService.rotate(request);
-    if (!this.onSteaming) {
-      this.frameCnt += 1;
+  async rotate(request: RotateRequest) {
+    try {
 
-      const demoPath =
-        this.frameCnt > 10
-          ? path.join(__dirname, '../ws/video/53.h264')
-          : path.join(__dirname, '../ws/video/2.h264');
-      const stream: StreamFrameType = {
-        frame: this.frameCnt,
-        clipPath: demoPath,
-        metaData: JSON.stringify(frameMetaReply),
-        serverTime: 754873824,
-        DIR: 1,
-      };
-      this.streamService.pushFrameToSteam(stream);
-    }
-    reply.subscribe((res: NormalReply) => {
-      if (res.code === 200) {
-        // const stream: StreamFrameType = {
-        //   frame: this.frameCnt,
-        //   clipPath: path.join(__dirname, '../ws/video/2.h264'),
-        //   metaData: JSON.stringify(frameMetaReply),
-        //   serverTime: 754873824,
-        //   DIR: 1,
-        // };
-        // this.streamService.pushFrameToSteam(stream);
+      const reply = this.sceneGrpcService.rotate(request);
+      if (!this.onSteaming) {
+        const redisMeta = await this.cacheService.rpop(
+          `updateFrameMetadata:${this.user_id}`,
+        );
+        this.frameCnt += 1;
+        if (redisMeta && redisMeta.length > 0) {
+          console.log('this.user_id', this.user_id);
+          const meta = JSON.parse(redisMeta);
+          if ('mediaSrc' in meta) {
+            console.log('meta', meta);
+            console.log('mediaSrc', meta.mediaSrc);
+
+            if (meta.mediaSrc.length > 0) {
+              const testclipPath = meta.mediaSrc.replace(
+                '/mnt/oss/metaverse/scene/0000000001/100/',
+                '',
+              );
+              console.log('testclipPath', testclipPath);
+              // const demoPath =
+              //   this.frameCnt > 10
+              //     ? path.join(__dirname, '../ws/video/53.h264')
+              //     : path.join(__dirname, '../ws/video/2.h264');
+              const demoPath = path.join(
+                __dirname,
+                `../ws/video/${testclipPath}`,
+              );
+              delete meta.mediaSrc;
+              const stream: StreamFrameType = {
+                frame: this.frameCnt,
+                clipPath: demoPath,
+                metaData: JSON.stringify(meta),
+                serverTime: 754873824,
+                DIR: 1,
+              };
+              this.streamService.pushFrameToSteam(stream);
+            }
+          }
+        }
       }
-    });
+      reply.subscribe((res: NormalReply) => {
+        if (res.code === 200) {
+          // const stream: StreamFrameType = {
+          //   frame: this.frameCnt,
+          //   clipPath: path.join(__dirname, '../ws/video/2.h264'),
+          //   metaData: JSON.stringify(frameMetaReply),
+          //   serverTime: 754873824,
+          //   DIR: 1,
+          // };
+          // this.streamService.pushFrameToSteam(stream);
+        }
+      });
+    } catch (error) {
+      this.logger.error('rotate', error);
+    }
   }
 
   joystick(request: JoystickRequest) {
@@ -172,6 +215,12 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
     this.stopCountingFrame();
     this.startSteaming.next(false);
     this.streamService.closeChannel();
+    const exitRequest: ExitRequest = {
+      action_type: 1002,
+      user_id: this.user_id,
+      trace_id: '',
+    };
+    this.exit(exitRequest);
   }
 
   handleMessage(message: string | Buffer) {
@@ -294,13 +343,31 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
           this.streamService.pushFrameToSteam(stream);
         }
 
+        // console.log('padLeft', );
+        // if (this.frameCnt > 2) {
+        //   setInterval(() => {
+        //     this.frameCnt += 1;
+        //     const stream: StreamFrameType = {
+        //       frame: this.frameCnt,
+        //       clipPath: path.join(
+        //         __dirname,
+        //         `../ws/video/100/100.${this.frameCnt.padLeft(4, '0')}.h264`,
+        //       ),
+        //       metaData: JSON.stringify(frameMetaReply),
+        //       serverTime: 754871824,
+        //     };
+        //     this.streamService.pushFrameToSteam(stream);
+        //   }, 1000 / 30);
+        // };
+
         if (this.frameCnt > 1 && !this.onSteaming) {
           const streamMeta: StreamMetaType = {
             frame: this.frameCnt,
             metaData: JSON.stringify(frameMetaReply),
           };
           this.streamService.pushMetaDataToSteam(streamMeta);
-        }
+        };
+
         // if (this.frameCnt == 4) {
         //   const stream: StreamFrameType = {
         //     frame: 4,

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

@@ -79,7 +79,7 @@ export class StreamService {
       const dir = stream.DIR || 1;
       const metaDataString = metaData.replace(/\s/g, '');
       const coordBuff = Buffer.from(metaDataString, 'utf-8');
-      console.warn('coordBuff', coordBuff.byteLength);
+      // console.warn('coordBuff', coordBuff.byteLength);
       // const steamStat = statSync(clipPath);
       // const steamTotalSize = metaData.length + steamStat.size;
       const clipBuffer = readFileSync(clipPath);

+ 10 - 0
yarn.lock

@@ -1447,6 +1447,16 @@ ajv@^8.0.0:
     require-from-string "^2.0.2"
     uri-js "^4.2.2"
 
+ajv@^8.11.0:
+  version "8.11.0"
+  resolved "https://registry.npmmirror.com/ajv/-/ajv-8.11.0.tgz#977e91dd96ca669f54a11e23e378e33b884a565f"
+  integrity sha512-wGgprdCvMalC0BztXvitD2hC04YffAvtsUn93JbGXYLAtCUO4xd17mCCZQxUOItiBwZvJScWo8NIvQMQ71rdpg==
+  dependencies:
+    fast-deep-equal "^3.1.1"
+    json-schema-traverse "^1.0.0"
+    require-from-string "^2.0.2"
+    uri-js "^4.2.2"
+
 ansi-colors@4.1.1:
   version "4.1.1"
   resolved "https://registry.npmmirror.com/ansi-colors/-/ansi-colors-4.1.1.tgz"