15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)// found in the LICENSE file.
468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/formats/mp2t/ts_section_pat.h"
668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include <vector>
868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "base/logging.h"
1068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "media/base/bit_reader.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/formats/mp2t/mp2t_common.h"
1268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)namespace media {
1468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)namespace mp2t {
1568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)TsSectionPat::TsSectionPat(const RegisterPmtCb& register_pmt_cb)
1768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    : register_pmt_cb_(register_pmt_cb),
1868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      version_number_(-1) {
1968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
2068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
2168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)TsSectionPat::~TsSectionPat() {
2268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
2368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
2468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)bool TsSectionPat::ParsePsiSection(BitReader* bit_reader) {
2568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Read the fixed section length.
2668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int table_id;
2768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int section_syntax_indicator;
2868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int dummy_zero;
2968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int reserved;
3068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int section_length;
3168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int transport_stream_id;
3268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int version_number;
3368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int current_next_indicator;
3468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int section_number;
3568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int last_section_number;
3668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(8, &table_id));
3768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(1, &section_syntax_indicator));
3868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(1, &dummy_zero));
3968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(2, &reserved));
4068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(12, &section_length));
4168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(section_length >= 5);
4268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(section_length <= 1021);
4368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(16, &transport_stream_id));
4468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(2, &reserved));
4568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(5, &version_number));
4668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(1, &current_next_indicator));
4768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(8, &section_number));
4868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(8, &last_section_number));
4968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  section_length -= 5;
5068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
5168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Perform a few verifications:
5268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // - Table ID should be 0 for a PAT.
5368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // - section_syntax_indicator should be one.
5468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // - section length should not exceed 1021
5568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(table_id == 0x0);
5668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(section_syntax_indicator);
5768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(!dummy_zero);
5868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
5968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Both the program table and the CRC have a size multiple of 4.
6068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Note for pmt_pid_count: minus 4 to account for the CRC.
6168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK((section_length % 4) == 0);
6268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int pmt_pid_count = (section_length - 4) / 4;
6368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
6468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Read the variable length section: program table & crc.
6568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  std::vector<int> program_number_array(pmt_pid_count);
6668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  std::vector<int> pmt_pid_array(pmt_pid_count);
6768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  for (int k = 0; k < pmt_pid_count; k++) {
6868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    int reserved;
6968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    RCHECK(bit_reader->ReadBits(16, &program_number_array[k]));
7068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    RCHECK(bit_reader->ReadBits(3, &reserved));
7168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    RCHECK(bit_reader->ReadBits(13, &pmt_pid_array[k]));
7268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
7368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int crc32;
7468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  RCHECK(bit_reader->ReadBits(32, &crc32));
7568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
7668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Just ignore the PAT if not applicable yet.
7768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (!current_next_indicator) {
7868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    DVLOG(1) << "Not supported: received a PAT not applicable yet";
7968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    return true;
8068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
8168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
8268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Ignore the program table if it hasn't changed.
8368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (version_number == version_number_)
8468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    return true;
8568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
8668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Both the MSE and the HLS spec specifies that TS streams should convey
8768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // exactly one program.
8868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (pmt_pid_count > 1) {
8968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    DVLOG(1) << "Multiple programs detected in the Mpeg2 TS stream";
9068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    return false;
9168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
9268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
9368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Can now register the PMT.
9468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#if !defined(NDEBUG)
9568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int expected_version_number = version_number;
9668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (version_number_ >= 0)
9768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    expected_version_number = (version_number_ + 1) % 32;
9868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  DVLOG_IF(1, version_number != expected_version_number)
9968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      << "Unexpected version number: "
10068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      << version_number << " vs " << version_number_;
10168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#endif
10268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  for (int k = 0; k < pmt_pid_count; k++) {
10368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    if (program_number_array[k] != 0) {
10468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      // Program numbers different from 0 correspond to PMT.
10568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      register_pmt_cb_.Run(program_number_array[k], pmt_pid_array[k]);
10668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      // Even if there are multiple programs, only one can be supported now.
10768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      // HLS: "Transport Stream segments MUST contain a single MPEG-2 Program."
10868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      break;
10968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    }
11068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
11168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  version_number_ = version_number;
11268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
11368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  return true;
11468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
11568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
11668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)void TsSectionPat::ResetPsiSection() {
11768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  version_number_ = -1;
11868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}
11968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
12068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}  // namespace mp2t
12168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)}  // namespace media
12268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
123