Chromium Code Reviews| Index: media/mp2t/ts_section_pat.cc |
| diff --git a/media/mp2t/ts_section_pat.cc b/media/mp2t/ts_section_pat.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e4673fc21172eb88b9e927bae137ca0a597ab1c8 |
| --- /dev/null |
| +++ b/media/mp2t/ts_section_pat.cc |
| @@ -0,0 +1,118 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/mp2t/ts_section_pat.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "media/base/bit_reader.h" |
| +#include "media/mp2t/mp2t_common.h" |
| + |
| +namespace media { |
| +namespace mp2t { |
| + |
| +TsSectionPat::TsSectionPat(RegisterPmtCb register_pmt_cb) |
| + : register_pmt_cb_(register_pmt_cb), |
| + version_number_(-1) { |
| +} |
| + |
| +TsSectionPat::~TsSectionPat() { |
| +} |
| + |
| +bool TsSectionPat::ParsePsiSection(BitReader* bit_reader) { |
| + // Read the fixed section length. |
| + int table_id; |
| + bool section_syntax_indicator; |
| + bool dummy_zero; |
| + int reserved; |
| + int section_length; |
| + int transport_stream_id; |
| + int version_number; |
| + bool current_next_indicator; |
| + int section_number; |
| + int last_section_number; |
| + RCHECK(bit_reader->ReadBits(8, &table_id)); |
| + RCHECK(bit_reader->ReadBits(1, §ion_syntax_indicator)); |
| + RCHECK(bit_reader->ReadBits(1, &dummy_zero)); |
| + RCHECK(bit_reader->ReadBits(2, &reserved)); |
| + RCHECK(bit_reader->ReadBits(12, §ion_length)); |
| + RCHECK(section_length >= 5); |
| + RCHECK(bit_reader->ReadBits(16, &transport_stream_id)); |
| + RCHECK(bit_reader->ReadBits(2, &reserved)); |
| + RCHECK(bit_reader->ReadBits(5, &version_number)); |
| + RCHECK(bit_reader->ReadBits(1, ¤t_next_indicator)); |
| + RCHECK(bit_reader->ReadBits(8, §ion_number)); |
| + RCHECK(bit_reader->ReadBits(8, &last_section_number)); |
| + section_length -= 5; |
| + |
| + // Perform a few verifications: |
| + // - Table ID should be 0 for a PAT. |
| + // - section_syntax_indicator should be one. |
| + // - section length should not exceed 1021 |
| + RCHECK(table_id == 0x0); |
| + RCHECK(section_syntax_indicator); |
| + RCHECK(!dummy_zero); |
| + RCHECK(section_length <= 1021); |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
s/1021/1016 to compensate for the -= 5 above?
damienv1
2013/09/17 02:58:22
Good catch. I moved all the RCHECK together in a p
|
| + |
| + // Both the program table and the CRC have a size multiple of 4. |
| + // Note for pmt_pid_count: minus 4 to account for the CRC. |
| + RCHECK((section_length & 0x3) == 0); |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: s/& 0x3/% 4/ just to make it easier to see th
damienv1
2013/09/17 02:58:22
Done.
|
| + int pmt_pid_count = (section_length - 4) / 4; |
| + |
| + // Read the variable length section: program table & crc. |
| + std::vector<int> program_number_array(pmt_pid_count); |
| + std::vector<int> pmt_pid_array(pmt_pid_count); |
| + for (int k = 0; k < pmt_pid_count; k++) { |
| + int reserved; |
| + RCHECK(bit_reader->ReadBits(16, &program_number_array[k])); |
| + RCHECK(bit_reader->ReadBits(3, &reserved)); |
| + RCHECK(bit_reader->ReadBits(13, &pmt_pid_array[k])); |
| + } |
| + int crc32; |
| + RCHECK(bit_reader->ReadBits(32, &crc32)); |
| + |
| + // Just ignore the PAT if not applicable yet. |
| + if (!current_next_indicator) { |
| + DVLOG(1) << "Not supported: received a PAT not applicable yet"; |
| + return true; |
| + } |
| + |
| + // Ignore the program table if it hasn't changed. |
| + if (version_number == version_number_) |
| + return true; |
| + |
| + // Both the MSE and the HLS spec specifies that TS streams should convey |
| + // exactly one program. |
| + if (pmt_pid_count > 1) |
| + return false; |
|
acolwell GONE FROM CHROMIUM
2013/09/16 06:19:30
nit: A DVLOG saying multiple programs were detecte
damienv1
2013/09/17 02:58:22
Done.
|
| + |
| + // Can now register the PMT. |
| + int expected_version_number = version_number; |
| + if (version_number_ >= 0) |
| + expected_version_number = (version_number_ + 1) % 32; |
| + DVLOG_IF(1, version_number != expected_version_number) |
| + << "Unexpected version number: " |
| + << version_number << " vs " << version_number_; |
| + for (int k = 0; k < pmt_pid_count; k++) { |
| + if (program_number_array[k] != 0) { |
| + // Program numbers different from 0 correspond to PMT. |
| + register_pmt_cb_.Run(program_number_array[k], pmt_pid_array[k]); |
| + // Even if there are multiple programs, only one can be supported now. |
| + // HLS: "Transport Stream segments MUST contain a single MPEG-2 Program." |
| + break; |
| + } |
| + } |
| + version_number_ = version_number; |
| + |
| + return true; |
| +} |
| + |
| +void TsSectionPat::ResetPsiSection() { |
| + version_number_ = -1; |
| +} |
| + |
| +} // namespace mp2t |
| +} // namespace media |
| + |