Shaka Packager SDK
ts_section_pat.cc
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.
4 
5 #include <packager/media/formats/mp2t/ts_section_pat.h>
6 
7 #include <vector>
8 
9 #include <absl/log/log.h>
10 
11 #include <packager/macros/logging.h>
12 #include <packager/media/base/bit_reader.h>
13 #include <packager/media/formats/mp2t/mp2t_common.h>
14 
15 namespace shaka {
16 namespace media {
17 namespace mp2t {
18 
19 TsSectionPat::TsSectionPat(const RegisterPmtCb& register_pmt_cb)
20  : register_pmt_cb_(register_pmt_cb),
21  version_number_(-1) {
22 }
23 
24 TsSectionPat::~TsSectionPat() {
25 }
26 
27 bool TsSectionPat::ParsePsiSection(BitReader* bit_reader) {
28  // Read the fixed section length.
29  int table_id;
30  int section_syntax_indicator;
31  int dummy_zero;
32  int reserved;
33  int section_length;
34  int transport_stream_id;
35  int version_number;
36  int current_next_indicator;
37  int section_number;
38  int last_section_number;
39  RCHECK(bit_reader->ReadBits(8, &table_id));
40  RCHECK(bit_reader->ReadBits(1, &section_syntax_indicator));
41  RCHECK(bit_reader->ReadBits(1, &dummy_zero));
42  RCHECK(bit_reader->ReadBits(2, &reserved));
43  RCHECK(bit_reader->ReadBits(12, &section_length));
44  RCHECK(section_length >= 5);
45  RCHECK(section_length <= 1021);
46  RCHECK(bit_reader->ReadBits(16, &transport_stream_id));
47  RCHECK(bit_reader->ReadBits(2, &reserved));
48  RCHECK(bit_reader->ReadBits(5, &version_number));
49  RCHECK(bit_reader->ReadBits(1, &current_next_indicator));
50  RCHECK(bit_reader->ReadBits(8, &section_number));
51  RCHECK(bit_reader->ReadBits(8, &last_section_number));
52  section_length -= 5;
53 
54  // Perform a few verifications:
55  // - Table ID should be 0 for a PAT.
56  // - section_syntax_indicator should be one.
57  // - section length should not exceed 1021
58  RCHECK(table_id == 0x0);
59  RCHECK(section_syntax_indicator);
60  RCHECK(!dummy_zero);
61 
62  // Both the program table and the CRC have a size multiple of 4.
63  // Note for pmt_pid_count: minus 4 to account for the CRC.
64  RCHECK((section_length % 4) == 0);
65  int pmt_pid_count = (section_length - 4) / 4;
66 
67  // Read the variable length section: program table & crc.
68  std::vector<int> program_number_array(pmt_pid_count);
69  std::vector<int> pmt_pid_array(pmt_pid_count);
70  for (int k = 0; k < pmt_pid_count; k++) {
71  RCHECK(bit_reader->ReadBits(16, &program_number_array[k]));
72  RCHECK(bit_reader->ReadBits(3, &reserved));
73  RCHECK(bit_reader->ReadBits(13, &pmt_pid_array[k]));
74  }
75  int crc32;
76  RCHECK(bit_reader->ReadBits(32, &crc32));
77 
78  // Just ignore the PAT if not applicable yet.
79  if (!current_next_indicator) {
80  DVLOG(1) << "Not supported: received a PAT not applicable yet";
81  return true;
82  }
83 
84  // Ignore the program table if it hasn't changed.
85  if (version_number == version_number_)
86  return true;
87 
88  // Both the MSE and the HLS spec specifies that TS streams should convey
89  // exactly one program.
90  if (pmt_pid_count > 1) {
91  LOG(ERROR) << "Multiple programs detected in the Mpeg2 TS stream";
92  return false;
93  }
94 
95  // Can now register the PMT.
96 #if !defined(NDEBUG)
97  int expected_version_number = version_number;
98  if (version_number_ >= 0)
99  expected_version_number = (version_number_ + 1) % 32;
100  if (version_number != expected_version_number) {
101  VLOG(1) << "Unexpected version number: " << version_number << " vs "
102  << version_number_;
103  }
104 #endif
105  for (int k = 0; k < pmt_pid_count; k++) {
106  if (program_number_array[k] != 0) {
107  // Program numbers different from 0 correspond to PMT.
108  register_pmt_cb_(program_number_array[k], pmt_pid_array[k]);
109  // Even if there are multiple programs, only one can be supported now.
110  // HLS: "Transport Stream segments MUST contain a single MPEG-2 Program."
111  break;
112  }
113  }
114  version_number_ = version_number;
115 
116  return true;
117 }
118 
119 void TsSectionPat::ResetPsiSection() {
120  version_number_ = -1;
121 }
122 
123 } // namespace mp2t
124 } // namespace media
125 } // namespace shaka
126 
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66