1 // Copyright 2013 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 "media/mp2t/ts_section_pat.h" 6 7 #include <vector> 8 9 #include "base/logging.h" 10 #include "media/base/bit_reader.h" 11 #include "media/mp2t/mp2t_common.h" 12 13 namespace media { 14 namespace mp2t { 15 16 TsSectionPat::TsSectionPat(const RegisterPmtCb& register_pmt_cb) 17 : register_pmt_cb_(register_pmt_cb), 18 version_number_(-1) { 19 } 20 21 TsSectionPat::~TsSectionPat() { 22 } 23 24 bool TsSectionPat::ParsePsiSection(BitReader* bit_reader) { 25 // Read the fixed section length. 26 int table_id; 27 int section_syntax_indicator; 28 int dummy_zero; 29 int reserved; 30 int section_length; 31 int transport_stream_id; 32 int version_number; 33 int current_next_indicator; 34 int section_number; 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)); 49 section_length -= 5; 50 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); 57 RCHECK(!dummy_zero); 58 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; 63 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++) { 68 int reserved; 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])); 72 } 73 int crc32; 74 RCHECK(bit_reader->ReadBits(32, &crc32)); 75 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"; 79 return true; 80 } 81 82 // Ignore the program table if it hasn't changed. 83 if (version_number == version_number_) 84 return true; 85 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"; 90 return false; 91 } 92 93 // Can now register the PMT. 94 #if !defined(NDEBUG) 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_; 101 #endif 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." 108 break; 109 } 110 } 111 version_number_ = version_number; 112 113 return true; 114 } 115 116 void TsSectionPat::ResetPsiSection() { 117 version_number_ = -1; 118 } 119 120 } // namespace mp2t 121 } // namespace media 122 123