Shaka Packager SDK
ts_section_psi.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_psi.h>
6 
7 #include <algorithm>
8 #include <cstdint>
9 
10 #include <absl/log/check.h>
11 #include <absl/log/log.h>
12 
13 #include <packager/macros/logging.h>
14 #include <packager/media/base/bit_reader.h>
15 #include <packager/media/formats/mp2t/mp2t_common.h>
16 
17 static bool IsCrcValid(const uint8_t* buf, int size) {
18  uint32_t crc = 0xffffffffu;
19  const uint32_t kCrcPoly = 0x4c11db7;
20 
21  for (int k = 0; k < size; k++) {
22  int nbits = 8;
23  uint32_t data_msb_aligned = buf[k];
24  data_msb_aligned <<= (32 - nbits);
25 
26  while (nbits > 0) {
27  if ((data_msb_aligned ^ crc) & 0x80000000) {
28  crc <<= 1;
29  crc ^= kCrcPoly;
30  } else {
31  crc <<= 1;
32  }
33 
34  data_msb_aligned <<= 1;
35  nbits--;
36  }
37  }
38 
39  return (crc == 0);
40 }
41 
42 namespace shaka {
43 namespace media {
44 namespace mp2t {
45 
46 TsSectionPsi::TsSectionPsi()
47  : wait_for_pusi_(true),
48  leading_bytes_to_discard_(0) {
49 }
50 
51 TsSectionPsi::~TsSectionPsi() {
52 }
53 
54 bool TsSectionPsi::Parse(bool payload_unit_start_indicator,
55  const uint8_t* buf,
56  int size) {
57  // Ignore partial PSI.
58  if (wait_for_pusi_ && !payload_unit_start_indicator)
59  return true;
60 
61  if (payload_unit_start_indicator) {
62  // Reset the state of the PSI section.
63  ResetPsiState();
64 
65  // Update the state.
66  wait_for_pusi_ = false;
67  DCHECK_GE(size, 1);
68  int pointer_field = buf[0];
69  leading_bytes_to_discard_ = pointer_field;
70  buf++;
71  size--;
72  }
73 
74  // Discard some leading bytes if needed.
75  if (leading_bytes_to_discard_ > 0) {
76  int nbytes_to_discard = std::min(leading_bytes_to_discard_, size);
77  buf += nbytes_to_discard;
78  size -= nbytes_to_discard;
79  leading_bytes_to_discard_ -= nbytes_to_discard;
80  }
81  if (size == 0)
82  return true;
83 
84  // Add the data to the parser state.
85  psi_byte_queue_.Push(buf, size);
86  int raw_psi_size;
87  const uint8_t* raw_psi;
88  psi_byte_queue_.Peek(&raw_psi, &raw_psi_size);
89 
90  // Check whether we have enough data to start parsing.
91  if (raw_psi_size < 3)
92  return true;
93  int section_length =
94  ((static_cast<int>(raw_psi[1]) << 8) |
95  (static_cast<int>(raw_psi[2]))) & 0xfff;
96  if (section_length >= 1021)
97  return false;
98  int psi_length = section_length + 3;
99  if (raw_psi_size < psi_length) {
100  // Don't throw an error when there is not enough data,
101  // just wait for more data to come.
102  return true;
103  }
104 
105  // There should not be any trailing bytes after a PMT.
106  // Instead, the pointer field should be used to stuff bytes.
107  if (raw_psi_size > psi_length) {
108  DVLOG(1) << "Trailing bytes after a PSI section: " << psi_length << " vs "
109  << raw_psi_size;
110  }
111 
112  // Verify the CRC.
113  RCHECK(IsCrcValid(raw_psi, psi_length));
114 
115  // Parse the PSI section.
116  BitReader bit_reader(raw_psi, raw_psi_size);
117  bool status = ParsePsiSection(&bit_reader);
118  if (status)
119  ResetPsiState();
120 
121  return status;
122 }
123 
124 bool TsSectionPsi::Flush() {
125  return true;
126 }
127 
128 void TsSectionPsi::Reset() {
129  ResetPsiSection();
130  ResetPsiState();
131 }
132 
133 void TsSectionPsi::ResetPsiState() {
134  wait_for_pusi_ = true;
135  psi_byte_queue_.Reset();
136  leading_bytes_to_discard_ = 0;
137 }
138 
139 } // namespace mp2t
140 } // namespace media
141 } // namespace shaka
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66