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