| Index: media/mpeg2/mpeg2ts_pat.cc
|
| diff --git a/media/mpeg2/mpeg2ts_pat.cc b/media/mpeg2/mpeg2ts_pat.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..d6cacb8d3b2926ae1a55a526bbe25a8baa0cd6d6
|
| --- /dev/null
|
| +++ b/media/mpeg2/mpeg2ts_pat.cc
|
| @@ -0,0 +1,136 @@
|
| +// 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/mpeg2/mpeg2ts_pat.h"
|
| +
|
| +#include "base/logging.h"
|
| +#include "media/base/bit_reader.h"
|
| +
|
| +namespace media {
|
| +namespace mpeg2ts {
|
| +
|
| +Mpeg2TsPatParser::Mpeg2TsPatParser(RegisterPmtCb register_pmt_cb)
|
| + : register_pmt_cb_(register_pmt_cb),
|
| + version_number_(-1) {
|
| +}
|
| +
|
| +Mpeg2TsPatParser::~Mpeg2TsPatParser() {
|
| +}
|
| +
|
| +bool Mpeg2TsPatParser::ParsePsiSection(BitReader* bit_reader) {
|
| + int remaining_size = bit_reader->bits_available() / 8;
|
| +
|
| + // Read the fixed section length.
|
| + if (remaining_size < 8) {
|
| + // This should not happen if the PAT size
|
| + // was correctly encoded into the stream.
|
| + LOG(WARNING) << "remaining_size=" << remaining_size;
|
| + return false;
|
| + }
|
| + int table_id;
|
| + DCHECK(bit_reader->ReadBits(8, &table_id));
|
| + if (table_id != 0x0) {
|
| + // Table ID should be 0 for a PAT.
|
| + LOG(WARNING) << "Unexpected table id: " << table_id;
|
| + return false;
|
| + }
|
| + bool section_syntax_indicator;
|
| + DCHECK(bit_reader->ReadBits(1, §ion_syntax_indicator));
|
| + if (!section_syntax_indicator) {
|
| + LOG(WARNING) << "Unexpected section_syntax_indicator";
|
| + return false;
|
| + }
|
| + bool dummy_zero;
|
| + DCHECK(bit_reader->ReadBits(1, &dummy_zero));
|
| + if (dummy_zero) {
|
| + LOG(WARNING) << "Unexpected bit";
|
| + return false;
|
| + }
|
| + int reserved;
|
| + DCHECK(bit_reader->ReadBits(2, &reserved));
|
| + int section_length;
|
| + DCHECK(bit_reader->ReadBits(12, §ion_length));
|
| + if (section_length >= 1021) {
|
| + return false;
|
| + }
|
| + int transport_stream_id;
|
| + DCHECK(bit_reader->ReadBits(16, &transport_stream_id));
|
| + DCHECK(bit_reader->ReadBits(2, &reserved));
|
| + int version_number;
|
| + DCHECK(bit_reader->ReadBits(5, &version_number));
|
| + bool current_next_indicator;
|
| + DCHECK(bit_reader->ReadBits(1, ¤t_next_indicator));
|
| + int section_number;
|
| + DCHECK(bit_reader->ReadBits(8, §ion_number));
|
| + int last_section_number;
|
| + DCHECK(bit_reader->ReadBits(8, &last_section_number));
|
| + remaining_size -= 8;
|
| + section_length -= 5;
|
| +
|
| + if (remaining_size < section_length) {
|
| + // This should not happen if the PAT size
|
| + // was correctly encoded into the stream.
|
| + LOG(WARNING) << "remaining_size=" << remaining_size;
|
| + return false;
|
| + }
|
| +
|
| + // Minus 4 to account for the following CRC.
|
| + if ((section_length & 0x3) != 0) {
|
| + LOG(WARNING) << "PAT table size should be a multiple of 4";
|
| + return false;
|
| + }
|
| + int pmt_pid_count = (section_length - 4) / 4;
|
| + 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++) {
|
| + if (remaining_size < 4) {
|
| + // This should not happen if the PAT size
|
| + // was correctly encoded into the stream.
|
| + LOG(WARNING) << "remaining_size=" << remaining_size;
|
| + return false;
|
| + }
|
| + DCHECK(bit_reader->ReadBits(16, &program_number_array[k]));
|
| + int reserved;
|
| + DCHECK(bit_reader->ReadBits(3, &reserved));
|
| + DCHECK(bit_reader->ReadBits(13, &pmt_pid_array[k]));
|
| + remaining_size -= 4;
|
| + }
|
| +
|
| + // Read the CRC.
|
| + if (remaining_size < 4) {
|
| + // This should not happen if the PAT size
|
| + // was correctly encoded into the stream.
|
| + LOG(WARNING) << "remaining_size=" << remaining_size;
|
| + return false;
|
| + }
|
| + int crc32;
|
| + DCHECK(bit_reader->ReadBits(32, &crc32));
|
| + remaining_size -= 4;
|
| +
|
| + // TODO(damienv): current_next_indicator flag is not handled at all.
|
| +
|
| + // Register the PMT only once the PAT is known to be well formed
|
| + // and the version number has changed.
|
| + if (version_number_ != 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, we can support only one.
|
| + break;
|
| + }
|
| + }
|
| + version_number_ = version_number;
|
| + }
|
| +
|
| + // This should not happen if the PAT size
|
| + // was correctly encoded into the stream.
|
| + LOG_IF(WARNING, remaining_size != 0)
|
| + << "remaining_size=" << remaining_size;
|
| +
|
| + return true;
|
| +}
|
| +
|
| +} // namespace mpeg2ts
|
| +} // namespace media
|
|
|