Add interface class for bitstream parser.

Bug: webrtc:10439
Change-Id: I0decbbf4aa21a96db50f340f200ccf8adc9e8b53
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/128760
Commit-Queue: Kári Helgason <kthelgason@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#27237}
diff --git a/api/video_codecs/BUILD.gn b/api/video_codecs/BUILD.gn
index 24b1298..e219353 100644
--- a/api/video_codecs/BUILD.gn
+++ b/api/video_codecs/BUILD.gn
@@ -52,6 +52,16 @@
   ]
 }
 
+rtc_source_set("bitstream_parser_api") {
+  visibility = [ "*" ]
+  sources = [
+    "bitstream_parser.h",
+  ]
+  deps = [
+    "..:array_view",
+  ]
+}
+
 rtc_static_library("builtin_video_decoder_factory") {
   visibility = [ "*" ]
   allow_poison = [
diff --git a/api/video_codecs/bitstream_parser.h b/api/video_codecs/bitstream_parser.h
new file mode 100644
index 0000000..0d8d014
--- /dev/null
+++ b/api/video_codecs/bitstream_parser.h
@@ -0,0 +1,35 @@
+/*
+ *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef API_VIDEO_CODECS_BITSTREAM_PARSER_H_
+#define API_VIDEO_CODECS_BITSTREAM_PARSER_H_
+#include <stddef.h>
+#include <stdint.h>
+
+#include "api/array_view.h"
+
+namespace webrtc {
+
+// This class is an interface for bitstream parsers.
+class BitstreamParser {
+ public:
+  virtual ~BitstreamParser() = default;
+
+  // Parse an additional chunk of the bitstream.
+  virtual void ParseBitstream(rtc::ArrayView<const uint8_t> bitstream) = 0;
+
+  // Get the last extracted QP value from the parsed bitstream. If no QP
+  // value could be parsed, returns absl::nullopt.
+  virtual absl::optional<int> GetLastSliceQp() const = 0;
+};
+
+}  // namespace webrtc
+
+#endif  // API_VIDEO_CODECS_BITSTREAM_PARSER_H_
diff --git a/common_video/BUILD.gn b/common_video/BUILD.gn
index 2b0712c..fdc586f 100644
--- a/common_video/BUILD.gn
+++ b/common_video/BUILD.gn
@@ -47,6 +47,7 @@
     "../api/video:video_bitrate_allocator",
     "../api/video:video_frame",
     "../api/video:video_frame_i420",
+    "../api/video_codecs:bitstream_parser_api",
     "../media:rtc_h264_profile_id",
     "../rtc_base",
     "../rtc_base:checks",
diff --git a/common_video/h264/h264_bitstream_parser.cc b/common_video/h264/h264_bitstream_parser.cc
index e71d1ca..f1ad84f 100644
--- a/common_video/h264/h264_bitstream_parser.cc
+++ b/common_video/h264/h264_bitstream_parser.cc
@@ -18,9 +18,11 @@
 #include "rtc_base/logging.h"
 
 namespace {
+
 const int kMaxAbsQpDeltaValue = 51;
 const int kMinQpValue = 0;
 const int kMaxQpValue = 51;
+
 }  // namespace
 
 namespace webrtc {
@@ -313,4 +315,15 @@
   return true;
 }
 
+void H264BitstreamParser::ParseBitstream(
+    rtc::ArrayView<const uint8_t> bitstream) {
+  ParseBitstream(bitstream.data(), bitstream.size());
+}
+
+absl::optional<int> H264BitstreamParser::GetLastSliceQp() const {
+  int qp;
+  bool success = GetLastSliceQp(&qp);
+  return success ? absl::optional<int>(qp) : absl::nullopt;
+}
+
 }  // namespace webrtc
diff --git a/common_video/h264/h264_bitstream_parser.h b/common_video/h264/h264_bitstream_parser.h
index 962c9c1..4819066 100644
--- a/common_video/h264/h264_bitstream_parser.h
+++ b/common_video/h264/h264_bitstream_parser.h
@@ -14,6 +14,7 @@
 #include <stdint.h>
 
 #include "absl/types/optional.h"
+#include "api/video_codecs/bitstream_parser.h"
 #include "common_video/h264/pps_parser.h"
 #include "common_video/h264/sps_parser.h"
 
@@ -25,24 +26,25 @@
 // TODO(pbos): If/when this gets used on the receiver side CHECKs must be
 // removed and gracefully abort as we have no control over receive-side
 // bitstreams.
-class H264BitstreamParser {
+class H264BitstreamParser : public BitstreamParser {
  public:
+  H264BitstreamParser();
+  ~H264BitstreamParser() override;
+
+  // These are here for backwards-compatability for the time being.
+  void ParseBitstream(const uint8_t* bitstream, size_t length);
+  bool GetLastSliceQp(int* qp) const;
+
+  // New interface.
+  void ParseBitstream(rtc::ArrayView<const uint8_t> bitstream) override;
+  absl::optional<int> GetLastSliceQp() const override;
+
+ protected:
   enum Result {
     kOk,
     kInvalidStream,
     kUnsupportedStream,
   };
-
-  H264BitstreamParser();
-  virtual ~H264BitstreamParser();
-
-  // Parse an additional chunk of H264 bitstream.
-  void ParseBitstream(const uint8_t* bitstream, size_t length);
-
-  // Get the last extracted QP value from the parsed bitstream.
-  bool GetLastSliceQp(int* qp) const;
-
- protected:
   void ParseSlice(const uint8_t* slice, size_t length);
   Result ParseNonParameterSetNalu(const uint8_t* source,
                                   size_t source_length,