-
Notifications
You must be signed in to change notification settings - Fork 3
/
ts_trimmer.hpp
210 lines (195 loc) · 4.1 KB
/
ts_trimmer.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef _TSD_TS_TRIMMER_HPP_
#define _TSD_TS_TRIMMER_HPP_
#include <ostream>
#include <sstream>
#include "splitter_context.hpp"
#include "transport_packet.hpp"
namespace tsd
{
class ts_trimmer
{
enum class state
{
buffering,
writing,
buffering2,
eof
};
public:
ts_trimmer(
std::ostream& ost,
std::iostream& buffer,
int64_t trim_threshold_sec,
bool enable_pmt_separator,
bool enable_eit_separator,
int overlap_front,
int overlap_back) :
output_(ost),
buffer_(buffer),
input_pos_(0),
context_(trim_threshold_sec),
state_(state::buffering),
enable_pmt_separator_(enable_pmt_separator),
enable_eit_separator_(enable_eit_separator),
overlap_front_(overlap_front),
overlap_back_(overlap_back),
written_overlap_back_(0)
{}
void add_packet(const transport_packet& packet) {
switch(state_) {
case state::buffering:
{
packet.pack(buffer_);
}
break;
case state::writing:
{
packet.pack(output_);
}
break;
case state::buffering2:
{
if(written_overlap_back_ < overlap_back_){
packet.pack(output_);
written_overlap_back_ += 1;
}
else {
packet.pack(buffer_);
}
}
break;
case state::eof:
{
}
break;
}
}
void signal_pmt() {
if(enable_pmt_separator_)
signal_split();
}
void signal_eit() {
if(enable_eit_separator_)
signal_split();
}
void signal_pcr(uint64_t pcr) {
switch(state_) {
case state::buffering:
{
context_.signal_pcr(pcr);
if(context_.get_state() ==
splitter_context::state::writing) {
flush_buffer();
state_ = state::writing;
}
}
break;
case state::writing:
{
context_.signal_pcr(pcr);
}
break;
case state::buffering2:
{
context_.signal_pcr(pcr);
if(context_.get_state() ==
splitter_context::state::writing){
flush_buffer();
written_overlap_back_ = 0;
state_ = state::writing;
}
}
break;
case state::eof:
{
}
break;
}
}
void signal_finish_stream() {
switch(state_) {
case state::buffering:
{
drop_buffer(0);
state_ = state::eof;
}
break;
case state::writing:
{
state_ = state::eof;
}
break;
case state::buffering2:
{
drop_buffer(0);
state_ = state::eof;
}
break;
case state::eof:
{
}
break;
}
}
private:
void signal_split() {
switch(state_) {
case state::buffering:
{
context_.signal_split();
drop_buffer(overlap_front_*transport_packet::size);
}
break;
case state::writing:
{
context_.signal_split();
state_ = state::buffering2;
}
break;
case state::buffering2:
{
}
break;
case state::eof:
{
}
break;
}
}
void flush_buffer() {
buffer_.exceptions(
std::ios_base::failbit |
std::ios_base::badbit);
// because a joint file position is maintained by std::basic_filebuf
buffer_.seekg(input_pos_);
std::copy(
istreambuf_iterator<char>(buffer_),
istreambuf_iterator<char>(),
ostreambuf_iterator<char>(output_));
buffer_.clear(); // clear eofbit
input_pos_ = buffer_.tellg();
}
void drop_buffer(size_t remain_bytes) {
buffer_.exceptions(
std::ios_base::failbit |
std::ios_base::badbit);
auto end_of_buffer_pos = buffer_.tellp();
input_pos_ =
std::max<decltype(input_pos_)>(
end_of_buffer_pos - static_cast<std::iostream::off_type>(remain_bytes),
input_pos_);
}
private:
std::ostream& output_;
std::iostream& buffer_;
std::iostream::pos_type input_pos_;
splitter_context context_;
state state_;
bool enable_pmt_separator_;
bool enable_eit_separator_;
int overlap_front_;
int overlap_back_;
int written_overlap_back_;
};
}
#endif