-
Notifications
You must be signed in to change notification settings - Fork 6
/
tsparser.hpp
147 lines (132 loc) · 3.83 KB
/
tsparser.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*++
* tssi - A library for parsing MPEG-2 and DVB Transport Streams
*
* Copyright (C) 2017 Martin Hoernig (goforcode.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
--*/
#pragma once
#include <vector>
#include <list>
#include "processnode.hpp"
// convenience
#include "psiheap.hpp"
#include "pesassembler.hpp"
/****h* /tssi
* NAME
* tssi -- A library for parsing MPEG-2 and DVB Transport Streams.
* To start from here, have a look at the modules, or the classes
* TSParser
* PSIHeap (PSISection)
* PESAssembler
*****/
namespace tssi
{
/****c* tssi/TSParser
* NAME
* TSParser -- Transport stream buffer parser, your main entry into tssi.
* Feed this class with a transport stream and feed the result forward to
* PSIHeap and PESAssembler to gather multimedia- or meta-data.
* METHODS
* pid_reset
* pid_parser
*****/
template <class _Alloc = std::allocator< char > >
class TSParser : public ProcessNode {
public:
/****m* TSParser/pid_reset
* NAME
* pid_reset -- Clears all pid to function associations that has been stored. The
* parser is resetted to its initial state, but it is still able to pick up the
* Transport Stream were it left off.
* SYNOPSIS
*/
void pid_reset()
/*******/
{
pid_list.clear();
}
/****m* TSParser/pid_parser
* NAME
* pid_parser -- Link a Pid list with a callback (ProcessNode, functor or lambda...)
* Everytime a pid from the given list is found in the stream, the function is
* called.
* DATA SCOPE
* iso138181::transport_packet
* SYNOPSIS
*/
void pid_parser(const std::vector<uint_fast16_t>& pids, callback_t&& function)
/*******/
{
pid_list.push_back(std::make_pair(pids, function));
}
private:
void process(gsl::span<const char> data) {
Expects(data.size() >= 752);
const size_t in_len = static_cast<size_t>(data.size());
size_t i = 0;
if (packet_buffer.size() > 0) {
if (data[0] == 0x47 &&
data[188] == 0x47 &&
data[376] == 0x47 &&
data[564] == 0x47) {
// discard packet buffer
}
else {
if (in_len == 188 - packet_buffer.size() ||
(in_len > 188 - packet_buffer.size() &&
data[188 - packet_buffer.size()] == 0x47)) {
i += 188 - packet_buffer.size();
copy(std::begin(data), std::begin(data) + (188 - packet_buffer.size()), std::back_inserter(packet_buffer));
filter(packet_buffer);
}
}
packet_buffer.clear();
}
while (i + 188 < in_len) {
if (data[i] == 0x47 &&
data[i + 188] == 0x47) {
filter(data.subspan(i, 188));
i += 188;
}
else {
++i;
}
}
if (i < in_len &&
i + 188 > in_len &&
data[i] == 0x47) {
std::copy(std::begin(data) + i, std::end(data), std::back_inserter(packet_buffer));
}
else {
if (i + 188 == in_len &&
data[i] == 0x47 &&
data[i - 188] == 0x47) {
filter(data.subspan(i, 188));
}
}
}
void filter(gsl::span<const char> data) {
Expects(data.size() == 188);
const auto pid = iso138181::transport_packet::PID(data);
for (const auto& pair : pid_list) {
if (find(pair.first.begin(), pair.first.end(), pid) != pair.first.end())
pair.second(data);
}
}
std::vector<char, _Alloc> packet_buffer;
std::list < std::pair<std::vector<uint_fast16_t>, callback_t>> pid_list;
};
}