1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/formats/mp2t/ts_section_pat.h"
9 #include "base/logging.h"
10 #include "media/base/bit_reader.h"
11 #include "media/formats/mp2t/mp2t_common.h"
16 TsSectionPat::TsSectionPat(const RegisterPmtCb
& register_pmt_cb
)
17 : register_pmt_cb_(register_pmt_cb
),
21 TsSectionPat::~TsSectionPat() {
24 bool TsSectionPat::ParsePsiSection(BitReader
* bit_reader
) {
25 // Read the fixed section length.
27 int section_syntax_indicator
;
31 int transport_stream_id
;
33 int current_next_indicator
;
35 int last_section_number
;
36 RCHECK(bit_reader
->ReadBits(8, &table_id
));
37 RCHECK(bit_reader
->ReadBits(1, §ion_syntax_indicator
));
38 RCHECK(bit_reader
->ReadBits(1, &dummy_zero
));
39 RCHECK(bit_reader
->ReadBits(2, &reserved
));
40 RCHECK(bit_reader
->ReadBits(12, §ion_length
));
41 RCHECK(section_length
>= 5);
42 RCHECK(section_length
<= 1021);
43 RCHECK(bit_reader
->ReadBits(16, &transport_stream_id
));
44 RCHECK(bit_reader
->ReadBits(2, &reserved
));
45 RCHECK(bit_reader
->ReadBits(5, &version_number
));
46 RCHECK(bit_reader
->ReadBits(1, ¤t_next_indicator
));
47 RCHECK(bit_reader
->ReadBits(8, §ion_number
));
48 RCHECK(bit_reader
->ReadBits(8, &last_section_number
));
51 // Perform a few verifications:
52 // - Table ID should be 0 for a PAT.
53 // - section_syntax_indicator should be one.
54 // - section length should not exceed 1021
55 RCHECK(table_id
== 0x0);
56 RCHECK(section_syntax_indicator
);
59 // Both the program table and the CRC have a size multiple of 4.
60 // Note for pmt_pid_count: minus 4 to account for the CRC.
61 RCHECK((section_length
% 4) == 0);
62 int pmt_pid_count
= (section_length
- 4) / 4;
64 // Read the variable length section: program table & crc.
65 std::vector
<int> program_number_array(pmt_pid_count
);
66 std::vector
<int> pmt_pid_array(pmt_pid_count
);
67 for (int k
= 0; k
< pmt_pid_count
; k
++) {
69 RCHECK(bit_reader
->ReadBits(16, &program_number_array
[k
]));
70 RCHECK(bit_reader
->ReadBits(3, &reserved
));
71 RCHECK(bit_reader
->ReadBits(13, &pmt_pid_array
[k
]));
74 RCHECK(bit_reader
->ReadBits(32, &crc32
));
76 // Just ignore the PAT if not applicable yet.
77 if (!current_next_indicator
) {
78 DVLOG(1) << "Not supported: received a PAT not applicable yet";
82 // Ignore the program table if it hasn't changed.
83 if (version_number
== version_number_
)
86 // Both the MSE and the HLS spec specifies that TS streams should convey
87 // exactly one program.
88 if (pmt_pid_count
> 1) {
89 DVLOG(1) << "Multiple programs detected in the Mpeg2 TS stream";
93 // Can now register the PMT.
95 int expected_version_number
= version_number
;
96 if (version_number_
>= 0)
97 expected_version_number
= (version_number_
+ 1) % 32;
98 DVLOG_IF(1, version_number
!= expected_version_number
)
99 << "Unexpected version number: "
100 << version_number
<< " vs " << version_number_
;
102 for (int k
= 0; k
< pmt_pid_count
; k
++) {
103 if (program_number_array
[k
] != 0) {
104 // Program numbers different from 0 correspond to PMT.
105 register_pmt_cb_
.Run(program_number_array
[k
], pmt_pid_array
[k
]);
106 // Even if there are multiple programs, only one can be supported now.
107 // HLS: "Transport Stream segments MUST contain a single MPEG-2 Program."
111 version_number_
= version_number
;
116 void TsSectionPat::ResetPsiSection() {
117 version_number_
= -1;