Skip to content

Commit

Permalink
NIT aggregation support
Browse files Browse the repository at this point in the history
  • Loading branch information
phpbg committed Mar 9, 2019
1 parent 76d9893 commit 848b108
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 10 deletions.
3 changes: 3 additions & 0 deletions examples/print-with-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
47 changes: 37 additions & 10 deletions src/Context/GlobalContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [];
}
Expand Down Expand Up @@ -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;
}
}
87 changes: 87 additions & 0 deletions src/Context/NitAggregator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* MIT License
*
* Copyright (c) 2018 Samuel CHEMLA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace PhpBg\DvbPsi\Context;

use PhpBg\DvbPsi\Exception;
use PhpBg\DvbPsi\Tables\Nit;

/**
* Class NitAggregator
* Aggregates all NIT segments
*/
class NitAggregator
{
public $networkId;

/**
* @var Nit[]
*/
public $segments = [];

protected $version;

/**
* Aggregate a new EIT
*
* @param Nit $nit
* @throws Exception
* @return bool Return true if the EIT was unknown and has been aggregated, false otherwise
*/
public function add(Nit $nit): bool
{
if (!isset($this->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;
}
}

0 comments on commit 848b108

Please sign in to comment.