From 848b1083daa7ccea414dadd94a995f3d2ab66fcb Mon Sep 17 00:00:00 2001 From: Samuel CHEMLA Date: Sat, 9 Mar 2019 22:24:24 +0100 Subject: [PATCH] NIT aggregation support --- examples/print-with-context.php | 3 ++ src/Context/GlobalContext.php | 47 ++++++++++++++---- src/Context/NitAggregator.php | 87 +++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 10 deletions(-) create mode 100644 src/Context/NitAggregator.php diff --git a/examples/print-with-context.php b/examples/print-with-context.php index 41d38c2..29bbf4d 100644 --- a/examples/print-with-context.php +++ b/examples/print-with-context.php @@ -67,6 +67,9 @@ $dvbPsiParser->on('eit', function ($eit) use ($globalContext) { $globalContext->addEit($eit); }); +$dvbPsiParser->on('nit', function ($nit) use ($globalContext) { + $globalContext->addNit($nit); +}); $dvbPsiParser->on('pmt', function ($pmt) use ($streamContext) { $streamContext->addPmt($pmt); }); diff --git a/src/Context/GlobalContext.php b/src/Context/GlobalContext.php index 56b8a2a..7f52846 100644 --- a/src/Context/GlobalContext.php +++ b/src/Context/GlobalContext.php @@ -26,16 +26,42 @@ namespace PhpBg\DvbPsi\Context; - use Evenement\EventEmitter; use PhpBg\DvbPsi\Tables\Eit; +use PhpBg\DvbPsi\Tables\Nit; class GlobalContext extends EventEmitter { protected $eitByNetworks = []; + /** + * @var NitAggregator[] + */ + protected $nitByNetworks = []; + + /** + * @param Nit $nit + * @throws \PhpBg\DvbPsi\Exception + */ + public function addNit(Nit $nit) + { + if ($nit->currentNextIndicator !== 1) { + return; + } + if (!isset($this->nitByNetworks[$nit->networkId])) { + $this->nitByNetworks[$nit->networkId] = new NitAggregator(); + } + $nitComplete = $this->nitByNetworks[$nit->networkId]->add($nit); + if ($nitComplete) { + $this->emit('nit', [$this->nitByNetworks[$nit->networkId]]); + } + } + public function addEit(Eit $eit) { + if ($eit->currentNextIndicator !== 1) { + return; + } if (!isset($this->eitByNetworks[$eit->originalNetworkId])) { $this->eitByNetworks[$eit->originalNetworkId] = []; } @@ -75,18 +101,19 @@ public function getAllEvents() public function __toString() { $str = ''; - if (!empty($this->eitByNetworks)) { - foreach ($this->eitByNetworks as $networkId => $transportStreams) { - $str .= sprintf("Network: %d (0x%x)\n", $networkId, $networkId); - foreach ($transportStreams as $transportStream => $services) { - $str .= sprintf("\tTransport Stream: %d (0x%x)\n", $transportStream, $transportStream); - foreach ($services as $service => $eitAggregator) { - $str .= sprintf("\t\tService: %d (0x%x)\n", $service, $service); - $str .= (string)$eitAggregator; - } + foreach ($this->eitByNetworks as $networkId => $transportStreams) { + $str .= sprintf("Network: %d (0x%x)\n", $networkId, $networkId); + foreach ($transportStreams as $transportStream => $services) { + $str .= sprintf("\tTransport Stream: %d (0x%x)\n", $transportStream, $transportStream); + foreach ($services as $service => $eitAggregator) { + $str .= sprintf("\t\tService: %d (0x%x)\n", $service, $service); + $str .= (string)$eitAggregator; } } } + foreach ($this->nitByNetworks as $networkId => $nitAggregator) { + echo "$nitAggregator\n"; + } return $str; } } \ No newline at end of file diff --git a/src/Context/NitAggregator.php b/src/Context/NitAggregator.php new file mode 100644 index 0000000..412e3f9 --- /dev/null +++ b/src/Context/NitAggregator.php @@ -0,0 +1,87 @@ +networkId)) { + $this->networkId = $nit->networkId; + } else { + if ($this->networkId !== $nit->networkId) { + throw new Exception("NIT and aggregator mismatch"); + } + } + if (!isset($this->version) || $nit->versionNumber > $this->version || ($nit->versionNumber === 0 && $this->version === 31)) { + // Version update (or initial collection of nit) + $this->version = $nit->versionNumber; + $this->segments = []; + } + if (!isset($this->segments[$nit->sectionNumber])) { + $this->segments[$nit->sectionNumber] = $nit; + } + if (count($this->segments) >= $nit->lastSectionNumber + 1) { + // NIT aggregation is complete + return true; + } + return false; + } + + + public function __toString() + { + $str = ''; + foreach ($this->segments as $segment) { + echo "{$segment}\n"; + } + return $str; + } +} \ No newline at end of file