Skip to content

Commit 848b108

Browse files
committed
NIT aggregation support
1 parent 76d9893 commit 848b108

File tree

3 files changed

+127
-10
lines changed

3 files changed

+127
-10
lines changed

examples/print-with-context.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
$dvbPsiParser->on('eit', function ($eit) use ($globalContext) {
6868
$globalContext->addEit($eit);
6969
});
70+
$dvbPsiParser->on('nit', function ($nit) use ($globalContext) {
71+
$globalContext->addNit($nit);
72+
});
7073
$dvbPsiParser->on('pmt', function ($pmt) use ($streamContext) {
7174
$streamContext->addPmt($pmt);
7275
});

src/Context/GlobalContext.php

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,42 @@
2626

2727
namespace PhpBg\DvbPsi\Context;
2828

29-
3029
use Evenement\EventEmitter;
3130
use PhpBg\DvbPsi\Tables\Eit;
31+
use PhpBg\DvbPsi\Tables\Nit;
3232

3333
class GlobalContext extends EventEmitter
3434
{
3535
protected $eitByNetworks = [];
3636

37+
/**
38+
* @var NitAggregator[]
39+
*/
40+
protected $nitByNetworks = [];
41+
42+
/**
43+
* @param Nit $nit
44+
* @throws \PhpBg\DvbPsi\Exception
45+
*/
46+
public function addNit(Nit $nit)
47+
{
48+
if ($nit->currentNextIndicator !== 1) {
49+
return;
50+
}
51+
if (!isset($this->nitByNetworks[$nit->networkId])) {
52+
$this->nitByNetworks[$nit->networkId] = new NitAggregator();
53+
}
54+
$nitComplete = $this->nitByNetworks[$nit->networkId]->add($nit);
55+
if ($nitComplete) {
56+
$this->emit('nit', [$this->nitByNetworks[$nit->networkId]]);
57+
}
58+
}
59+
3760
public function addEit(Eit $eit)
3861
{
62+
if ($eit->currentNextIndicator !== 1) {
63+
return;
64+
}
3965
if (!isset($this->eitByNetworks[$eit->originalNetworkId])) {
4066
$this->eitByNetworks[$eit->originalNetworkId] = [];
4167
}
@@ -75,18 +101,19 @@ public function getAllEvents()
75101
public function __toString()
76102
{
77103
$str = '';
78-
if (!empty($this->eitByNetworks)) {
79-
foreach ($this->eitByNetworks as $networkId => $transportStreams) {
80-
$str .= sprintf("Network: %d (0x%x)\n", $networkId, $networkId);
81-
foreach ($transportStreams as $transportStream => $services) {
82-
$str .= sprintf("\tTransport Stream: %d (0x%x)\n", $transportStream, $transportStream);
83-
foreach ($services as $service => $eitAggregator) {
84-
$str .= sprintf("\t\tService: %d (0x%x)\n", $service, $service);
85-
$str .= (string)$eitAggregator;
86-
}
104+
foreach ($this->eitByNetworks as $networkId => $transportStreams) {
105+
$str .= sprintf("Network: %d (0x%x)\n", $networkId, $networkId);
106+
foreach ($transportStreams as $transportStream => $services) {
107+
$str .= sprintf("\tTransport Stream: %d (0x%x)\n", $transportStream, $transportStream);
108+
foreach ($services as $service => $eitAggregator) {
109+
$str .= sprintf("\t\tService: %d (0x%x)\n", $service, $service);
110+
$str .= (string)$eitAggregator;
87111
}
88112
}
89113
}
114+
foreach ($this->nitByNetworks as $networkId => $nitAggregator) {
115+
echo "$nitAggregator\n";
116+
}
90117
return $str;
91118
}
92119
}

src/Context/NitAggregator.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
/**
4+
* MIT License
5+
*
6+
* Copyright (c) 2018 Samuel CHEMLA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
namespace PhpBg\DvbPsi\Context;
28+
29+
use PhpBg\DvbPsi\Exception;
30+
use PhpBg\DvbPsi\Tables\Nit;
31+
32+
/**
33+
* Class NitAggregator
34+
* Aggregates all NIT segments
35+
*/
36+
class NitAggregator
37+
{
38+
public $networkId;
39+
40+
/**
41+
* @var Nit[]
42+
*/
43+
public $segments = [];
44+
45+
protected $version;
46+
47+
/**
48+
* Aggregate a new EIT
49+
*
50+
* @param Nit $nit
51+
* @throws Exception
52+
* @return bool Return true if the EIT was unknown and has been aggregated, false otherwise
53+
*/
54+
public function add(Nit $nit): bool
55+
{
56+
if (!isset($this->networkId)) {
57+
$this->networkId = $nit->networkId;
58+
} else {
59+
if ($this->networkId !== $nit->networkId) {
60+
throw new Exception("NIT and aggregator mismatch");
61+
}
62+
}
63+
if (!isset($this->version) || $nit->versionNumber > $this->version || ($nit->versionNumber === 0 && $this->version === 31)) {
64+
// Version update (or initial collection of nit)
65+
$this->version = $nit->versionNumber;
66+
$this->segments = [];
67+
}
68+
if (!isset($this->segments[$nit->sectionNumber])) {
69+
$this->segments[$nit->sectionNumber] = $nit;
70+
}
71+
if (count($this->segments) >= $nit->lastSectionNumber + 1) {
72+
// NIT aggregation is complete
73+
return true;
74+
}
75+
return false;
76+
}
77+
78+
79+
public function __toString()
80+
{
81+
$str = '';
82+
foreach ($this->segments as $segment) {
83+
echo "{$segment}\n";
84+
}
85+
return $str;
86+
}
87+
}

0 commit comments

Comments
 (0)