Add implementations of the VideoRtpDepacketizer interface

while suboptimal, these implementions are complete and allow to
swap code from using RtpDepacketizer interface to VideoRtpDepacketizer

Bug: webrtc:11152
Change-Id: Ie7823feeb5b0563b74754255aaedfada9d446ac5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/161380
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30031}
diff --git a/modules/rtp_rtcp/BUILD.gn b/modules/rtp_rtcp/BUILD.gn
index d0f4ce8..39b9180 100644
--- a/modules/rtp_rtcp/BUILD.gn
+++ b/modules/rtp_rtcp/BUILD.gn
@@ -138,6 +138,8 @@
     "source/absolute_capture_time_receiver.h",
     "source/absolute_capture_time_sender.cc",
     "source/absolute_capture_time_sender.h",
+    "source/create_video_rtp_depacketizer.cc",
+    "source/create_video_rtp_depacketizer.h",
     "source/dtmf_queue.cc",
     "source/dtmf_queue.h",
     "source/fec_private_tables_bursty.cc",
@@ -211,6 +213,8 @@
     "source/ulpfec_receiver_impl.cc",
     "source/ulpfec_receiver_impl.h",
     "source/video_rtp_depacketizer.h",
+    "source/video_rtp_depacketizer_raw.cc",
+    "source/video_rtp_depacketizer_raw.h",
   ]
 
   if (rtc_enable_bwe_test_logging) {
@@ -273,6 +277,7 @@
     "../video_coding:codec_globals_headers",
     "//third_party/abseil-cpp/absl/algorithm:container",
     "//third_party/abseil-cpp/absl/container:inlined_vector",
+    "//third_party/abseil-cpp/absl/memory",
     "//third_party/abseil-cpp/absl/strings",
     "//third_party/abseil-cpp/absl/types:optional",
     "//third_party/abseil-cpp/absl/types:variant",
@@ -474,6 +479,7 @@
       "source/ulpfec_generator_unittest.cc",
       "source/ulpfec_header_reader_writer_unittest.cc",
       "source/ulpfec_receiver_unittest.cc",
+      "source/video_rtp_depacketizer_raw_unittest.cc",
     ]
     deps = [
       ":fec_test_helper",
diff --git a/modules/rtp_rtcp/source/create_video_rtp_depacketizer.cc b/modules/rtp_rtcp/source/create_video_rtp_depacketizer.cc
new file mode 100644
index 0000000..8946ec0
--- /dev/null
+++ b/modules/rtp_rtcp/source/create_video_rtp_depacketizer.cc
@@ -0,0 +1,62 @@
+/*
+ *  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.
+ */
+
+#include "modules/rtp_rtcp/source/create_video_rtp_depacketizer.h"
+
+#include <memory>
+
+#include "absl/memory/memory.h"
+#include "absl/types/optional.h"
+#include "modules/rtp_rtcp/source/rtp_format.h"
+#include "modules/rtp_rtcp/source/video_rtp_depacketizer.h"
+#include "rtc_base/checks.h"
+#include "rtc_base/copy_on_write_buffer.h"
+
+namespace webrtc {
+namespace {
+
+// Wrapper over legacy RtpDepacketizer interface.
+// TODO(bugs.webrtc.org/11152): Delete when all RtpDepacketizers updated to
+// the VideoRtpDepacketizer interface.
+class LegacyRtpDepacketizer : public VideoRtpDepacketizer {
+ public:
+  explicit LegacyRtpDepacketizer(VideoCodecType codec) : codec_(codec) {}
+  ~LegacyRtpDepacketizer() override = default;
+
+  absl::optional<ParsedRtpPayload> Parse(
+      rtc::CopyOnWriteBuffer rtp_payload) override {
+    auto depacketizer = absl::WrapUnique(RtpDepacketizer::Create(codec_));
+    RTC_CHECK(depacketizer);
+    RtpDepacketizer::ParsedPayload parsed_payload;
+    if (!depacketizer->Parse(&parsed_payload, rtp_payload.cdata(),
+                             rtp_payload.size())) {
+      return absl::nullopt;
+    }
+    absl::optional<ParsedRtpPayload> result(absl::in_place);
+    result->video_header = parsed_payload.video;
+    result->video_payload.SetData(parsed_payload.payload,
+                                  parsed_payload.payload_length);
+    return result;
+  }
+
+ private:
+  const VideoCodecType codec_;
+};
+
+}  // namespace
+
+std::unique_ptr<VideoRtpDepacketizer> CreateVideoRtpDepacketizer(
+    VideoCodecType codec) {
+  // TODO(bugs.webrtc.org/11152): switch on codec and create specialized
+  // VideoRtpDepacketizers when they are migrated to new interface.
+  return std::make_unique<LegacyRtpDepacketizer>(codec);
+}
+
+}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/create_video_rtp_depacketizer.h b/modules/rtp_rtcp/source/create_video_rtp_depacketizer.h
new file mode 100644
index 0000000..102cacf
--- /dev/null
+++ b/modules/rtp_rtcp/source/create_video_rtp_depacketizer.h
@@ -0,0 +1,26 @@
+/*
+ *  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 MODULES_RTP_RTCP_SOURCE_CREATE_VIDEO_RTP_DEPACKETIZER_H_
+#define MODULES_RTP_RTCP_SOURCE_CREATE_VIDEO_RTP_DEPACKETIZER_H_
+
+#include <memory>
+
+#include "api/video/video_codec_type.h"
+#include "modules/rtp_rtcp/source/video_rtp_depacketizer.h"
+
+namespace webrtc {
+
+std::unique_ptr<VideoRtpDepacketizer> CreateVideoRtpDepacketizer(
+    VideoCodecType codec);
+
+}  // namespace webrtc
+
+#endif  // MODULES_RTP_RTCP_SOURCE_CREATE_VIDEO_RTP_DEPACKETIZER_H_
diff --git a/modules/rtp_rtcp/source/video_rtp_depacketizer_raw.cc b/modules/rtp_rtcp/source/video_rtp_depacketizer_raw.cc
new file mode 100644
index 0000000..81b4e4a
--- /dev/null
+++ b/modules/rtp_rtcp/source/video_rtp_depacketizer_raw.cc
@@ -0,0 +1,28 @@
+/*
+ *  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.
+ */
+
+#include "modules/rtp_rtcp/source/video_rtp_depacketizer_raw.h"
+
+#include <utility>
+
+#include "absl/types/optional.h"
+#include "modules/rtp_rtcp/source/video_rtp_depacketizer.h"
+#include "rtc_base/copy_on_write_buffer.h"
+
+namespace webrtc {
+
+absl::optional<VideoRtpDepacketizer::ParsedRtpPayload>
+VideoRtpDepacketizerRaw::Parse(rtc::CopyOnWriteBuffer rtp_payload) {
+  absl::optional<ParsedRtpPayload> parsed(absl::in_place);
+  parsed->video_payload = std::move(rtp_payload);
+  return parsed;
+}
+
+}  // namespace webrtc
diff --git a/modules/rtp_rtcp/source/video_rtp_depacketizer_raw.h b/modules/rtp_rtcp/source/video_rtp_depacketizer_raw.h
new file mode 100644
index 0000000..59c8695
--- /dev/null
+++ b/modules/rtp_rtcp/source/video_rtp_depacketizer_raw.h
@@ -0,0 +1,30 @@
+/*
+ *  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 MODULES_RTP_RTCP_SOURCE_VIDEO_RTP_DEPACKETIZER_RAW_H_
+#define MODULES_RTP_RTCP_SOURCE_VIDEO_RTP_DEPACKETIZER_RAW_H_
+
+#include "absl/types/optional.h"
+#include "modules/rtp_rtcp/source/video_rtp_depacketizer.h"
+#include "rtc_base/copy_on_write_buffer.h"
+
+namespace webrtc {
+
+class VideoRtpDepacketizerRaw : public VideoRtpDepacketizer {
+ public:
+  ~VideoRtpDepacketizerRaw() override = default;
+
+  absl::optional<ParsedRtpPayload> Parse(
+      rtc::CopyOnWriteBuffer rtp_payload) override;
+};
+
+}  // namespace webrtc
+
+#endif  // MODULES_RTP_RTCP_SOURCE_VIDEO_RTP_DEPACKETIZER_RAW_H_
diff --git a/modules/rtp_rtcp/source/video_rtp_depacketizer_raw_unittest.cc b/modules/rtp_rtcp/source/video_rtp_depacketizer_raw_unittest.cc
new file mode 100644
index 0000000..36c826a
--- /dev/null
+++ b/modules/rtp_rtcp/source/video_rtp_depacketizer_raw_unittest.cc
@@ -0,0 +1,51 @@
+/*
+ *  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.
+ */
+
+#include "modules/rtp_rtcp/source/video_rtp_depacketizer_raw.h"
+
+#include <cstdint>
+
+#include "absl/types/optional.h"
+#include "rtc_base/copy_on_write_buffer.h"
+#include "test/gtest.h"
+
+namespace webrtc {
+namespace {
+
+TEST(VideoRtpDepacketizerRaw, PassRtpPayloadAsVideoPayload) {
+  const uint8_t kPayload[] = {0x05, 0x25, 0x52};
+  rtc::CopyOnWriteBuffer rtp_payload(kPayload);
+
+  VideoRtpDepacketizerRaw depacketizer;
+  absl::optional<VideoRtpDepacketizer::ParsedRtpPayload> parsed =
+      depacketizer.Parse(rtp_payload);
+
+  ASSERT_TRUE(parsed);
+  EXPECT_EQ(parsed->video_payload.size(), rtp_payload.size());
+  // Check there was no memcpy involved by verifying return and original buffers
+  // point to the same buffer.
+  EXPECT_EQ(parsed->video_payload.cdata(), rtp_payload.cdata());
+}
+
+TEST(VideoRtpDepacketizerRaw, UsesDefaultValuesForVideoHeader) {
+  const uint8_t kPayload[] = {0x05, 0x25, 0x52};
+  rtc::CopyOnWriteBuffer rtp_payload(kPayload);
+
+  VideoRtpDepacketizerRaw depacketizer;
+  absl::optional<VideoRtpDepacketizer::ParsedRtpPayload> parsed =
+      depacketizer.Parse(rtp_payload);
+
+  ASSERT_TRUE(parsed);
+  EXPECT_FALSE(parsed->video_header.generic);
+  EXPECT_EQ(parsed->video_header.codec, kVideoCodecGeneric);
+}
+
+}  // namespace
+}  // namespace webrtc