gemercheung 3 年之前
父節點
當前提交
25a2aeb940
共有 1 個文件被更改,包括 60 次插入56 次删除
  1. 60 56
      src/scene/scene.service.ts

+ 60 - 56
src/scene/scene.service.ts

@@ -660,67 +660,71 @@ export class SceneService implements OnModuleInit, OnModuleDestroy {
   }
 
   handleStream() {
-    this.frameCntSubscription = this.frameCnt.subscribe(async (frame) => {
-      try {
-        console.log('frame', frame);
-        if (frame === 1) {
-          const redisData = await this.rotateService.echo(this.user_id);
-          this.onSteaming = true;
-          this.holdSteam();
-
-          if (redisData && 'mediaSrc' in redisData) {
-            const mediaSrc: string = redisData.mediaSrc || '';
-            if (mediaSrc.length > 0) {
-              const src = mediaSrc.split('?')[0];
-              // 临时本地替换路经
-              // src = src.replace('/10086/', '');
-              // const clipPath = join(__dirname, `../ws/${src}`);
-              const clipPath = this.configService.get('app.prefix') + src;
-              delete redisData.mediaSrc;
-              this.logger.log(
-                `user:${this.user_id}:first render stream` +
-                JSON.stringify({ path: clipPath, meta: redisData }),
-              );
-              const status = await this.pushFirstRender(
-                clipPath,
-                JSON.stringify(redisData),
-              );
-              if (status) {
-                this.firstRender = true;
-                this.frameCnt.next(2);
-                this.resumeStream();
-              } else {
-                this.logger.error('first render problem', status);
+    this.logger.log('this.frameCntSubscription', this.frameCntSubscription);
+    if (!this.frameCntSubscription) {
+      this.frameCntSubscription = this.frameCnt.subscribe(async (frame) => {
+        try {
+          console.log('frame', frame);
+          if (frame === 1) {
+            const redisData = await this.rotateService.echo(this.user_id);
+            this.onSteaming = true;
+            this.holdSteam();
+
+            if (redisData && 'mediaSrc' in redisData) {
+              const mediaSrc: string = redisData.mediaSrc || '';
+              if (mediaSrc.length > 0) {
+                const src = mediaSrc.split('?')[0];
+                // 临时本地替换路经
+                // src = src.replace('/10086/', '');
+                // const clipPath = join(__dirname, `../ws/${src}`);
+                const clipPath = this.configService.get('app.prefix') + src;
+                delete redisData.mediaSrc;
+                this.logger.log(
+                  `user:${this.user_id}:first render stream` +
+                  JSON.stringify({ path: clipPath, meta: redisData }),
+                );
+                const status = await this.pushFirstRender(
+                  clipPath,
+                  JSON.stringify(redisData),
+                );
+                if (status) {
+                  this.firstRender = true;
+                  this.frameCnt.next(2);
+                  this.resumeStream();
+                } else {
+                  this.logger.error('first render problem', status);
+                }
               }
             }
           }
-        }
-        if (
-          frame > 1 &&
-          !this.onMoving.value &&
-          !this.onRotating.value &&
-          this.firstRender
-        ) {
-          const redisDataAuto = await this.rotateService.echo(this.user_id);
-          if (redisDataAuto) {
-            console.log(`空白流::有数据:${frame}`);
-            'mediaSrc' in redisDataAuto && delete redisDataAuto.mediaSrc;
-            const streamMeta: StreamMetaType = {
-              frame: frame,
-              metaData: JSON.stringify(redisDataAuto),
-            };
-            this.streamService.pushMetaDataToSteam(streamMeta);
-          } else {
-            this.stopStream();
-            console.log('空流无Redis数据');
-            throw new Error('空流无Redis数据');
+          if (
+            frame > 1 &&
+            !this.onMoving.value &&
+            !this.onRotating.value &&
+            this.firstRender
+          ) {
+            const redisDataAuto = await this.rotateService.echo(this.user_id);
+            if (redisDataAuto) {
+              console.log(`空白流::有数据:${frame}`);
+              'mediaSrc' in redisDataAuto && delete redisDataAuto.mediaSrc;
+              const streamMeta: StreamMetaType = {
+                frame: frame,
+                metaData: JSON.stringify(redisDataAuto),
+              };
+              this.streamService.pushMetaDataToSteam(streamMeta);
+            } else {
+              this.stopStream();
+              console.log('空流无Redis数据');
+              throw new Error('空流无Redis数据');
+            }
           }
+        } catch (error) {
+          this.stopStream();
+          this.logger.error('handleStream', error);
         }
-      } catch (error) {
-        this.stopStream();
-        this.logger.error('handleStream', error);
-      }
-    });
+      });
+    }
+
   }
   handleRotateStream() {
     this.roQueueSubscription = this.roQueue.subscribe(