-
Notifications
You must be signed in to change notification settings - Fork 3
/
view.hpp
479 lines (435 loc) · 14.1 KB
/
view.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#ifndef _TSD_VIEW_HPP_
#define _TSD_VIEW_HPP_
#include <iostream>
#include <sstream>
#include <chrono>
#include <ctime>
#include "picojson.h"
#include "aribstr.h"
#include "util.hpp"
#include "section_header.hpp"
#include "pat.hpp"
#include "pmt.hpp"
#include "sdt.hpp"
#include "tot.hpp"
#include "eit.hpp"
#include "descriptor.hpp"
namespace tsd
{
class view
{
public:
view() :
print_section_header_(false),
print_pat_(false),
print_pmt_(false),
print_sdt_(false),
print_tot_(false),
print_eit_(false),
print_if_changed_(false),
print_packet_num_(false)
{}
virtual ~view() {}
void set_print_section_header(bool p) {
print_section_header_ = p;
}
void set_print_pat(bool p) {
print_pat_ = p;
}
void set_print_pmt(bool p) {
print_pmt_ = p;
}
void set_print_sdt(bool p) {
print_sdt_ = p;
}
void set_print_tot(bool p) {
print_tot_ = p;
}
void set_print_eit(bool p) {
print_eit_ = p;
}
void set_print_if_changed(bool p) {
print_if_changed_ = p;
}
void set_print_packet_num(bool p) {
print_packet_num_ = p;
}
void print(
uint64_t packet_num,
const section_header& header,
const program_association_table& pat,
bool changed = true) const {
if(print_if_changed_ && !changed)
return;
if(print_pat_) {
print_packet_num(packet_num);
print_section_header(header);
on_print(pat);
}
}
void print(
uint64_t packet_num,
const section_header& header,
const program_map_table& pmt,
bool changed = true) const {
if(print_if_changed_ && !changed)
return;
if(print_pmt_) {
print_packet_num(packet_num);
print_section_header(header);
on_print(pmt);
}
}
void print(
uint64_t packet_num,
const section_header& header,
const service_description_table& sdt,
bool changed = true) const {
if(print_if_changed_ && !changed)
return;
if(print_sdt_) {
print_packet_num(packet_num);
print_section_header(header);
on_print(sdt);
}
}
void print(
uint64_t packet_num,
const section_header& header,
const time_offset_table& tot,
bool changed = true) const {
if(print_if_changed_ && !changed)
return;
if(print_tot_) {
print_packet_num(packet_num);
print_section_header(header);
on_print(tot);
}
}
void print(
uint64_t packet_num,
const section_header& header,
const event_information_table& eit,
bool changed = true) const {
if(print_if_changed_ && !changed)
return;
if(print_eit_) {
print_packet_num(packet_num);
print_section_header(header);
on_print(eit);
}
}
private:
void print_packet_num(uint64_t n) const {
if(print_packet_num_)
on_print_packet_num(n);
}
void print_section_header(const section_header& h) const {
if(print_section_header_)
on_print_section_header(h);
}
protected:
virtual void on_print_packet_num(uint64_t n) const {
cout << std::dec << n << "\t";
}
virtual void on_print_section_header(const section_header& header) const {}
virtual void on_print(const program_association_table& pat) const {}
virtual void on_print(const program_map_table& pmt) const {}
virtual void on_print(const service_description_table& sdt) const {}
virtual void on_print(const time_offset_table& tot) const {}
virtual void on_print(const event_information_table& eit) const {}
protected:
bool print_section_header_;
bool print_pat_;
bool print_pmt_;
bool print_sdt_;
bool print_tot_;
bool print_eit_;
bool print_if_changed_;
bool print_packet_num_;
};
class json_view : public view
{
public:
json_view() :
prettify_(false) {}
explicit json_view(bool prettify) :
prettify_(prettify) {}
virtual ~json_view() {}
protected:
virtual void on_print_section_header(const section_header& h) const {
cout << serialize_section_header(h).serialize(prettify_);
}
virtual void on_print(const program_association_table& pat) const {
picojson::object o;
picojson::array association;
for(auto& i : pat.association) {
picojson::object program_to_pid_obj;
program_to_pid_obj.emplace(
"program_number", picojson::value(d(i.program_number)));
program_to_pid_obj.emplace(
"pmt_pid", picojson::value(d(i.pmt_pid)));
association.emplace_back(
picojson::value(program_to_pid_obj));
}
o.emplace(
"association",
picojson::value(association));
cout << picojson::value(o).serialize(prettify_) << endl;
}
virtual void on_print(const program_map_table& pmt) const {
picojson::object o;
o.emplace(
"pcr_pid",
picojson::value(d(pmt.pcr_pid)));
picojson::array program_info;
for(auto& pi : pmt.program_info) {
picojson::object pio;
pio.emplace("tag", picojson::value(d(pi.tag)));
program_info.emplace_back(picojson::value(pio));
}
o.emplace("program_info", picojson::value(program_info));
picojson::array program_elements;
for(auto& pe : pmt.program_elements) {
picojson::object peo;
peo.emplace("stream_type", picojson::value(d(pe.stream_type)));
peo.emplace("elementary_pid", picojson::value(d(pe.elementary_pid)));
picojson::array es_info;
for(auto& desc : pe.es_info) {
picojson::object es_info_o;
es_info_o.emplace("tag", picojson::value(d(desc.tag)));
if(desc.tag == stream_identifier_descriptor::TAG) {
auto sid = desc.as<stream_identifier_descriptor>();
es_info_o.emplace("component_tag", picojson::value(d(sid.component_tag)));
}
es_info.emplace_back(picojson::value(es_info_o));
}
peo.emplace("es_info", picojson::value(es_info));
program_elements.emplace_back(picojson::value(peo));
}
o.emplace("program_elements", picojson::value(program_elements));
cout << picojson::value(o).serialize(prettify_) << endl;
}
virtual void on_print(const service_description_table& sdt) const {
char tmpbuf[4096];
picojson::object o;
o.emplace(
"original_network_id",
picojson::value(d(sdt.original_network_id)));
picojson::array services;
for(auto& s : sdt.services) {
picojson::object sobj;
sobj.emplace("service_id", picojson::value(d(s.service_id)));
picojson::array descriptors;
for(auto& desc : s.descriptors) {
if(desc.tag == service_descriptor::TAG) {
auto sd = desc.as<service_descriptor>();
picojson::object dobj;
dobj.emplace("service_type", picojson::value(d(sd.service_type)));
AribToString(
tmpbuf,
sd.service_provider_name.data(),
sd.service_provider_name.size());
dobj.emplace("provider_name", picojson::value(tmpbuf));
AribToString(
tmpbuf,
sd.service_name.data(),
sd.service_name.size());
dobj.emplace("service_name", picojson::value(tmpbuf));
descriptors.emplace_back(picojson::value(dobj));
}
}
sobj.emplace("descriptors", picojson::value(descriptors));
services.emplace_back(picojson::value(sobj));
}
o.emplace("services", picojson::value(services));
cout << picojson::value(o).serialize(prettify_) << endl;
}
virtual void on_print(const time_offset_table& tot) const {
picojson::object o;
time_t t = tot.time.to_time_t();
char tmpbuf[100];
std::strftime(tmpbuf, sizeof(tmpbuf), "%c %Z", std::localtime(&t));
o.emplace("jst_time", picojson::value(tmpbuf));
cout << picojson::value(o).serialize(prettify_) << endl;
}
virtual void on_print(const event_information_table& eit) const {
picojson::object rooto;
rooto.emplace(
"transport_stream_id", picojson::value(d(eit.transport_stream_id)));
rooto.emplace(
"original_network_id", picojson::value(d(eit.original_network_id)));
rooto.emplace(
"segment_last_section_number", picojson::value(d(eit.segment_last_section_number)));
rooto.emplace(
"last_table_id", picojson::value(d(eit.last_table_id)));
std::ostringstream oss;
char tmpbuf[4096];
time_t t;
picojson::array events;
for(auto& e : eit.events) {
picojson::object eo;
eo.emplace("event_id", picojson::value(d(e.event_id)));
t = e.start_time.to_time_t();
std::strftime(tmpbuf, sizeof(tmpbuf), "%c %Z", std::localtime(&t));
eo.emplace("start_time", picojson::value(tmpbuf));
oss.str("");
oss
<< static_cast<int>(e.duration.hour) << ':'
<< static_cast<int>(e.duration.min) << ':'
<< static_cast<int>(e.duration.sec);
eo.emplace(
"duration", picojson::value(oss.str()));
eo.emplace(
"running_status", picojson::value(d(e.running_status)));
eo.emplace(
"free_ca_mode", picojson::value(static_cast<bool>(e.free_ca_mode)));
picojson::array descriptors;
for(auto& desc : e.descriptors) {
picojson::object dobj;
dobj.emplace("tag", picojson::value(d(desc.tag)));
if(desc.tag == short_event_descriptor::TAG) {
auto sed = desc.as<short_event_descriptor>();
dobj.emplace(
"iso_639_language_code",
picojson::value(sed.iso_639_language_code));
AribToString(
tmpbuf,
sed.event_name.data(),
sed.event_name.size());
dobj.emplace(
"event_name",
picojson::value(tmpbuf));
AribToString(
tmpbuf,
sed.text.data(),
sed.text.size());
dobj.emplace(
"text",
picojson::value(tmpbuf));
}
descriptors.emplace_back(picojson::value(dobj));
}
eo.emplace("descriptors", picojson::value(descriptors));
events.emplace_back(picojson::value(eo));
}
rooto.emplace("events", picojson::value(events));
cout << picojson::value(rooto).serialize(prettify_) << endl;
}
private:
picojson::value serialize_section_header(
const section_header& sh) const {
picojson::object o;
o.emplace(
"table_id", picojson::value(d(sh.table_id)));
if(sh.section_syntax_indicator) {
o.emplace(
"table_id_extension", picojson::value(d(sh.table_id_extension)));
o.emplace(
"version", picojson::value(d(sh.version)));
o.emplace(
"current_next_indicator", picojson::value(d(sh.current_next_indicator)));
o.emplace(
"section_number", picojson::value(d(sh.section_number)));
o.emplace(
"last_section_number", picojson::value(d(sh.section_number)));
}
return picojson::value(o);
}
template<typename D>
double d(const D& d) const {
return static_cast<double>(d);
}
private:
bool prettify_;
};
class debug_view : public view
{
public:
virtual ~debug_view() {}
protected:
virtual void on_print_section_header(const section_header& h) const {
cout << "-----section header-----" << endl;
dump_section_header(h);
}
virtual void on_print(const program_association_table& pat) const {
cout << "----- pat -----" << endl;
for(auto& i : pat.association) {
cout << "[program : " << i.program_number;
cout << ", pid : " << i.pmt_pid << "]" << endl;
}
}
virtual void on_print(const program_map_table& pmt) const {
cout << "----- pmt -----" << endl;
cout << "pcr_pid : " << (int)pmt.pcr_pid << endl;
cout << "program_info(descriptor)" << endl;
{
for(auto& i : pmt.program_info) {
cout << "\t" << "tag : " << (int)i.tag << endl;
cout << "\t" << "length : " << (int)i.length << endl;
}
}
cout << "program_elements" << endl;
{
for(auto& i : pmt.program_elements) {
cout << "\t" << "stream_type : " << (int)i.stream_type << endl;
cout << "\t" << "elementary_pid : " << (int)i.elementary_pid << endl;
cout << "\t" << "es_info(descriptor)" << endl;
for(auto& j : i.es_info) {
cout << "\t\t" << "tag : " << (int)j.tag << endl;
cout << "\t\t" << "length : " << (int)j.length << endl;
}
}
}
}
virtual void on_print(const service_description_table& sdt) const {
cout << "----SDT sectoin----" << endl;
char tmpbuf[4096];
cout << "services : " << endl;
for(auto& s : sdt.services) {
cout << "\t" << "service_id : " << (int)s.service_id << endl;
for(auto& d : s.descriptors) {
cout << "\t\t" << "tag : " << (int)d.tag << endl;
cout << "\t\t" << "length : " << (int)d.length << endl;
if(d.tag == service_descriptor::TAG) {
auto sd = d.as<service_descriptor>();
cout << "\t\t" << "service_type: " << (int)sd.service_type << endl;
AribToString(
tmpbuf,
sd.service_provider_name.data(),
sd.service_provider_name.size());
cout << "\t\t" << "service_provider_name: " << tmpbuf << endl;
AribToString(
tmpbuf,
sd.service_name.data(),
sd.service_name.size());
cout << "\t\t" << "service_name: " << tmpbuf << endl;
}
}
}
}
virtual void on_print(const time_offset_table& tot) const {
cout << "----- tot section -----" << endl;
cout << dec;
time_t t = tot.time.to_time_t();
char tmpbuf[100];
std::strftime(tmpbuf, sizeof(tmpbuf), "%c %Z", std::localtime(&t));
cout << "time : " << tmpbuf << endl;
}
virtual void on_print(const event_information_table& eit) const {
cout << "----- EIT section -----" << endl;
}
private:
void dump_section_header(const section_header& sh) const {
cout << "table id : " << (int)sh.table_id << endl;
if(sh.section_syntax_indicator) {
cout << "table_id_extension : " << (int)sh.table_id_extension << endl;
cout << "version : " << (int)sh.version << endl;
cout << "current_next_indicator : " << (int)sh.current_next_indicator << endl;
cout << "section number : " << (int)sh.section_number << endl;
cout << "last section number : " << (int)sh.last_section_number << endl;
}
}
};
}
#endif