Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Unified Diff: media/mpeg2/mpeg2ts_pat.cc

Issue 23566013: Mpeg2 TS stream parser for media source. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..0e0196ab393ff3a4fc606bf79a0fd80218f3db05
--- /dev/null
+++ b/media/mpeg2/mpeg2ts_pat.cc
@@ -0,0 +1,125 @@
+// 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"
+#include "media/mpeg2/mpeg2ts_common.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) {
+ // Read the fixed section length.
+ int remaining_size = bit_reader->bits_available() / 8;
+ if (remaining_size < 8) {
+ LOG(WARNING) << "remaining_size=" << remaining_size;
acolwell GONE FROM CHROMIUM 2013/09/05 18:29:10 nit: DVLOG
damienv1 2013/09/09 23:29:45 Done.
+ return false;
+ }
+ int table_id;
+ DCHECK(bit_reader->ReadBits(8, &table_id));
+ bool section_syntax_indicator;
acolwell GONE FROM CHROMIUM 2013/09/05 18:29:10 nit: put all declarations together to make this ea
damienv1 2013/09/09 23:29:45 Done.
+ DCHECK(bit_reader->ReadBits(1, &section_syntax_indicator));
acolwell GONE FROM CHROMIUM 2013/09/05 18:29:10 These should be RCHECK otherwise these won't run i
damienv1 2013/09/09 23:29:45 Running this is safe in release builds: I just che
acolwell GONE FROM CHROMIUM 2013/09/12 19:59:50 But none of the code in a DCHECK runs in a release
damienv1 2013/09/12 20:53:51 OK, my mistake. If we had to use DCHECK, it would
+ bool dummy_zero;
+ DCHECK(bit_reader->ReadBits(1, &dummy_zero));
+ int reserved;
+ DCHECK(bit_reader->ReadBits(2, &reserved));
+ int section_length;
+ DCHECK(bit_reader->ReadBits(12, &section_length));
+ 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, &current_next_indicator));
+ int section_number;
+ DCHECK(bit_reader->ReadBits(8, &section_number));
+ int last_section_number;
+ DCHECK(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);
+
+ // Make sure there are enough bits for the variable section.
+ remaining_size = bit_reader->bits_available() / 8;
+ if (remaining_size < section_length) {
+ LOG(WARNING) << "remaining_size=" << remaining_size;
acolwell GONE FROM CHROMIUM 2013/09/05 18:29:10 DVLOG here and all cases below.
damienv1 2013/09/09 23:29:45 Done.
+ return false;
+ }
+ LOG_IF(WARNING, remaining_size > section_length)
+ << "Trailing bytes after the PAT: nbytes="
+ << (remaining_size - section_length);
+
+ // 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.
+ 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;
+
+ // 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++) {
+ DCHECK(bit_reader->ReadBits(16, &program_number_array[k]));
acolwell GONE FROM CHROMIUM 2013/09/05 18:29:10 RCHECK here and below
damienv1 2013/09/09 23:29:45 By design, we should not run out of bits in this s
+ int reserved;
+ DCHECK(bit_reader->ReadBits(3, &reserved));
acolwell GONE FROM CHROMIUM 2013/09/05 18:29:10 RCHECK reserved value
damienv1 2013/09/09 23:29:45 Ditto.
+ DCHECK(bit_reader->ReadBits(13, &pmt_pid_array[k]));
+ }
+ int crc32;
+ DCHECK(bit_reader->ReadBits(32, &crc32));
+
+ // Just ignore the PAT if not applicable yet.
+ if (!current_next_indicator) {
+ LOG(WARNING) << "Not supported: received a PAT not applicable yet";
+ return true;
+ }
+
+ // Ignore the program table if it hasn't changed.
+ if (version_number == version_number_) {
acolwell GONE FROM CHROMIUM 2013/09/05 18:29:10 nit: remove {}
damienv1 2013/09/09 23:29:45 Done.
+ return true;
+ }
+
+ // Can now register the PMT.
+ int expected_version_number = version_number;
+ if (version_number_ >= 0) {
+ expected_version_number = (version_number_ + 1) % 32;
+ }
+ LOG_IF(WARNING, 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."
acolwell GONE FROM CHROMIUM 2013/09/05 18:29:10 If this code is detecting multiple programs here,
damienv1 2013/09/09 23:29:45 Done.
+ break;
+ }
+ }
+ version_number_ = version_number;
+
+ return true;
+}
+
+} // namespace mpeg2ts
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698