Przeglądaj źródła

feat: 增加steamService处理所有流的操作

gemercheung 3 lat temu
rodzic
commit
8ad24853d5

+ 11 - 0
config.yaml

@@ -11,6 +11,17 @@ redis:
   password: '' # 非远程不需要密码
   decode_responses: true
   db: 9
+
+stun:
+  server: ['stun:172.18.156.41:3478', 'stun:120.24.252.95:3478']
+  portRangeBegin: 52000
+  portRangeEnd: 53000
+
+server:
+  private_ip: 172.18.197.114
+  public_ip: 120.24.252.95
+
+  
 # PRIVATE_IP=172.18.197.114
 # PUBLIC_IP=120.24.252.95
 # STUNS_SEVER="stun:172.18.156.41:3478,stun:120.24.252.95:3478"

+ 1 - 7
src/app.controller.ts

@@ -1,15 +1,9 @@
 import { Controller, Get, OnModuleInit } from '@nestjs/common';
 import { AppService } from './app.service';
-// import { UtilsModule } from '@app/utils';
 import { grpcClientOptions } from './scene/grpc-scene.options';
-
 import { ClientGrpc, Client } from '@nestjs/microservices';
 import { SceneService } from './scene/scene.service';
-interface Point {
-  x: string;
-  y: string;
-  z: string;
-}
+
 @Controller()
 export class AppController implements OnModuleInit {
   @Client(grpcClientOptions) private readonly client: ClientGrpc;

+ 2 - 2
src/app.module.ts

@@ -5,7 +5,7 @@ import { AppService } from './app.service';
 import { MetaGateway } from './meta.gateway';
 import { RoomModule } from './room/room.module';
 import { SceneModule } from './scene/scene.module';
-import { CacheModule } from './cache/cache.module';
+// import { CacheModule } from './cache/cache.module';
 import configuration from './config/configuration';
 
 @Module({
@@ -13,7 +13,7 @@ import configuration from './config/configuration';
     ConfigModule.forRoot({ isGlobal: true, load: [configuration] }),
     RoomModule,
     SceneModule,
-    CacheModule,
+    // CacheModule,
   ],
   controllers: [AppController],
   providers: [AppService, MetaGateway],

+ 1 - 1
src/cache/cache.module.ts

@@ -8,4 +8,4 @@ import configuration from 'src/config/configuration';
   providers: [CacheService],
   exports: [CacheService],
 })
-export class CacheModule { }
+export class CacheModule {}

+ 4 - 2
src/cache/cache.service.ts

@@ -1,14 +1,16 @@
-import { Injectable, OnModuleInit } from '@nestjs/common';
+import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
 import { RedisService } from 'nestjs-redis';
 
 @Injectable()
 export class CacheService implements OnModuleInit {
   public client;
+  private logger: Logger = new Logger('CacheService');
   constructor(private redisService: RedisService) {}
+
   async onModuleInit() {
-    console.log('redis init');
     try {
       this.getClient();
+      this.logger.log('redis init');
     } catch (error) {
       console.error('error', error);
     }

+ 24 - 16
src/meta.gateway.ts

@@ -22,6 +22,7 @@ import { Logger } from '@nestjs/common';
 import * as path from 'path';
 import { createReadStream } from 'fs';
 import { SceneService } from './scene/scene.service';
+import { ConfigService } from '@nestjs/config';
 // 'Verbose' | 'Debug' | 'Info' | 'Warning' | 'Error' | 'Fatal';
 initLogger('Debug');
 
@@ -34,7 +35,10 @@ initLogger('Debug');
 export class MetaGateway
   implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
 {
-  constructor(private readonly sceneService: SceneService) {}
+  constructor(
+    private readonly sceneService: SceneService,
+    private readonly configService: ConfigService,
+  ) {}
   private logger: Logger = new Logger('MetaGateway');
   private peer: PeerConnection = null;
   private timer: NodeJS.Timeout;
@@ -79,40 +83,44 @@ export class MetaGateway
 
   @SubscribeMessage('init_webrtc')
   handleInitWebRtc(client: any, payload: any): void {
-    console.log('handleInitWebRtc');
-    console.log('this.sceneService', this.sceneService);
-    const stun_server: string[] = Array.from(
-      String(process.env.STUNS_SEVER).split(','),
-    );
-    this.logger.log('stun_server', stun_server);
+    this.logger.log('action::handleInitWebRtc', JSON.stringify(payload));
+
+    const stun_server = this.configService.get('stun.server');
+    const portRangeBegin = this.configService.get('stun.portRangeBegin');
+    const portRangeEnd = this.configService.get('stun.portRangeEnd');
+
     this.peer = new PeerConnection('roomTest', {
-      portRangeBegin: 52000,
-      portRangeEnd: 53000,
+      portRangeBegin: portRangeBegin,
+      portRangeEnd: portRangeEnd,
       iceServers: stun_server,
     });
 
     this.peer.onLocalDescription((sdp, type) => {
-      console.warn('peer SDP:', sdp, ' Type:', type);
+      // console.warn('peer SDP:', sdp, ' Type:', type);
       const offer = { sdp, type };
       const offerFormat = {
         id: 'offer',
         data: Buffer.from(JSON.stringify(offer)).toString('base64'),
       };
-      console.log('send', offerFormat);
+      this.logger.log('peer::onLocalDescription', JSON.stringify(offerFormat));
       client.send(JSON.stringify(offerFormat));
     });
 
     const replaceToPublic = (candidate) => {
-      console.warn('PRIVATE_IP', process.env.PRIVATE_IP);
-      return candidate.replace(process.env.PRIVATE_IP, process.env.PUBLIC_IP);
+      const PRIVATE_IP = this.configService.get('server.private_ip');
+      const PUBLIC_IP = this.configService.get('server.public_ip');
+      this.logger.log(
+        'peer::replaceToPublic',
+        `private_ip:${PRIVATE_IP} to public_ip:${PUBLIC_IP}`,
+      );
+      return candidate.replace(PRIVATE_IP, PUBLIC_IP);
     };
 
     this.peer.onLocalCandidate((candidate, mid) => {
       if (/172\./.test(candidate)) {
         console.error('private Ip process', candidate);
-
-        if (candidate.includes(process.env.PRIVATE_IP)) {
-          console.error('PRIVATE_IP', process.env.PRIVATE_IP);
+        const PRIVATE_IP = this.configService.get('server.private_ip');
+        if (candidate.includes(PRIVATE_IP)) {
           candidate = replaceToPublic(candidate);
         } else {
           return;

+ 3 - 2
src/scene/scene.module.ts

@@ -3,11 +3,12 @@ import { SceneService } from './scene.service';
 import { CacheModule } from '../cache/cache.module';
 import { CacheService } from '../cache/cache.service';
 // import { RedisService } from '../redis/redis.service';
+import { StreamService } from './stream/stream.service';
 
 @Module({
   imports: [CacheModule],
   controllers: [],
-  providers: [SceneService, CacheService],
+  providers: [SceneService, CacheService, StreamService],
   exports: [SceneService],
 })
-export class SceneModule { }
+export class SceneModule {}

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

@@ -11,10 +11,9 @@ import * as streamBuffers from 'stream-buffers';
 import { ActionType } from './actionType';
 import { CacheService } from 'src/cache/cache.service';
 
-
 @Injectable()
 export class SceneService implements OnModuleInit {
-  constructor(private cacheService: CacheService) { }
+  constructor(private cacheService: CacheService) {}
   @Client(grpcClientOptions) private readonly client: ClientGrpc;
   private sceneGrpcService: SceneGrpcService;
 

+ 18 - 0
src/scene/stream/stream.service.spec.ts

@@ -0,0 +1,18 @@
+import { Test, TestingModule } from '@nestjs/testing';
+import { StreamService } from './stream.service';
+
+describe('StreamService', () => {
+  let service: StreamService;
+
+  beforeEach(async () => {
+    const module: TestingModule = await Test.createTestingModule({
+      providers: [StreamService],
+    }).compile();
+
+    service = module.get<StreamService>(StreamService);
+  });
+
+  it('should be defined', () => {
+    expect(service).toBeDefined();
+  });
+});

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

@@ -0,0 +1,4 @@
+import { Injectable } from '@nestjs/common';
+
+@Injectable()
+export class StreamService {}