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