diff --git a/src/Dvb/TSStream.php b/src/Dvb/TSStream.php index a8f1800..f5d1932 100644 --- a/src/Dvb/TSStream.php +++ b/src/Dvb/TSStream.php @@ -90,6 +90,11 @@ class TSStream extends EventEmitter */ private $channels; + /** + * @var Exiting + * Indicating exit process started. This process depends on termination delay. + */ + private $exiting = false; private $exited = false; /** @@ -276,6 +281,7 @@ public function _handleExit() $this->emit('exit'); $this->removeAllListeners(); + $this->exiting = false; } /** @@ -430,6 +436,15 @@ public function removeClient(WritableStreamInterface $client) $this->autoKillIfKillable(); } + /** + * Return the exiting status + * @return bool + */ + public function isExiting() + { + return $this->exiting; + } + /** * Terminate process forcefully */ @@ -438,6 +453,7 @@ public function terminate() if ($this->exited) { return; } + $this->exiting = true; $this->logger->debug("Terminate process " . $this->process->getCommand()); if (!$this->process->terminate(SIGKILL)) { throw new \RuntimeException("Unable to signal process"); @@ -495,4 +511,4 @@ public function releaseEpgGrabbing() $this->epgGrabbing = false; $this->autoKillIfKillable(); } -} \ No newline at end of file +} diff --git a/src/Dvb/TSStreamFactory.php b/src/Dvb/TSStreamFactory.php index 05a0b28..f97a34d 100644 --- a/src/Dvb/TSStreamFactory.php +++ b/src/Dvb/TSStreamFactory.php @@ -99,6 +99,22 @@ public function getTsStream(int $channelServiceId): ExtendedPromiseInterface }); } $this->doCreateTsStream($channelDescriptor); + } else { + $curStream = $this->streamsByChannelFrequency[$channelFrequency]; + // in the case exit process in ongoing, we need to wait it ends before creating a new TsStream + if ($curStream->isExiting()) { + $this->logger->debug("getTsStream() while isExiting() == true"); + $exitPromise = new Promise(function (callable $exitResolver) use ($curStream) { + $curStream->on('exit', function () use ($exitResolver) { + $this->logger->debug("on exit executed"); + return $exitResolver(); + }); + }); + return $exitPromise->then(function () use ($resolver, $channelDescriptor, $channelFrequency) { + $this->logger->debug("doCreateTsStream delayed after on exit"); + return $resolver($this->doCreateTsStream($channelDescriptor)); + }); + } } return $resolver($this->streamsByChannelFrequency[$channelFrequency]); }); @@ -141,4 +157,4 @@ public function terminateTsStream() } return null; } -} \ No newline at end of file +}