Cleanup modules_common_types

Uninline RTPFragmentaion functions
fix RTPFragmentation move constructor and assign operators (was recursive for win)
replace assert with rtc::dchecked_cast
Remove unused includes and dependencies.
Fix other targets that used those includes transitively instead of directly

Bug: None
Change-Id: I647cb1eda107dc7d87d25234095545bc2842fa40
Reviewed-on: https://webrtc-review.googlesource.com/100500
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24759}
diff --git a/audio/transport_feedback_packet_loss_tracker_unittest.cc b/audio/transport_feedback_packet_loss_tracker_unittest.cc
index 2f9bf68..f522635 100644
--- a/audio/transport_feedback_packet_loss_tracker_unittest.cc
+++ b/audio/transport_feedback_packet_loss_tracker_unittest.cc
@@ -116,8 +116,6 @@
 
  private:
   int64_t time_ms_{0};
-
-  RTC_DISALLOW_COPY_AND_ASSIGN(TransportFeedbackPacketLossTrackerTest);
 };
 
 }  // namespace
diff --git a/modules/BUILD.gn b/modules/BUILD.gn
index e1c1f3a..8127244 100644
--- a/modules/BUILD.gn
+++ b/modules/BUILD.gn
@@ -44,6 +44,7 @@
   visibility = [ "*" ]
   sources = [
     "include/module.h",
+    "include/module_common_types.cc",
     "include/module_common_types.h",
   ]
   deps = [
@@ -51,14 +52,8 @@
     ":module_fec_api",
     "..:webrtc_common",
     "../api:libjingle_peerconnection_api",
-    "../api/transport:network_control",
-    "../api/video:video_frame",
-    "../api/video:video_frame_i420",
     "../modules/rtp_rtcp:rtp_video_header",
-    "../rtc_base:deprecation",
-    "../rtc_base:rtc_base_approved",
-    "video_coding:codec_globals_headers",
-    "//third_party/abseil-cpp/absl/types:optional",
+    "../rtc_base:safe_conversions",
   ]
 }
 
diff --git a/modules/audio_coding/test/iSACTest.cc b/modules/audio_coding/test/iSACTest.cc
index 6a52042..95af4d4 100644
--- a/modules/audio_coding/test/iSACTest.cc
+++ b/modules/audio_coding/test/iSACTest.cc
@@ -27,6 +27,7 @@
 #include "modules/audio_coding/codecs/audio_format_conversion.h"
 #include "modules/audio_coding/test/utility.h"
 #include "rtc_base/strings/string_builder.h"
+#include "rtc_base/timeutils.h"
 #include "system_wrappers/include/sleep.h"
 #include "test/testsupport/fileutils.h"
 
diff --git a/modules/congestion_controller/BUILD.gn b/modules/congestion_controller/BUILD.gn
index de57e5b..5381a1d 100644
--- a/modules/congestion_controller/BUILD.gn
+++ b/modules/congestion_controller/BUILD.gn
@@ -70,6 +70,7 @@
   ]
 
   deps = [
+    "../../api/transport:network_control",
     "../../modules:module_api",
     "../../rtc_base:checks",
     "../../rtc_base:rtc_base_approved",
diff --git a/modules/congestion_controller/rtp/BUILD.gn b/modules/congestion_controller/rtp/BUILD.gn
index ddfe813..0f2a64e 100644
--- a/modules/congestion_controller/rtp/BUILD.gn
+++ b/modules/congestion_controller/rtp/BUILD.gn
@@ -70,6 +70,7 @@
 
   deps = [
     "../..:module_api",
+    "../../../api/transport:network_control",
     "../../../rtc_base:checks",
     "../../../rtc_base:rtc_base_approved",
     "../../../system_wrappers",
diff --git a/modules/congestion_controller/rtp/transport_feedback_adapter.h b/modules/congestion_controller/rtp/transport_feedback_adapter.h
index ae38b84..e2477ca 100644
--- a/modules/congestion_controller/rtp/transport_feedback_adapter.h
+++ b/modules/congestion_controller/rtp/transport_feedback_adapter.h
@@ -14,6 +14,7 @@
 #include <deque>
 #include <vector>
 
+#include "api/transport/network_types.h"
 #include "modules/congestion_controller/rtp/send_time_history.h"
 #include "rtc_base/criticalsection.h"
 #include "rtc_base/thread_annotations.h"
diff --git a/modules/congestion_controller/transport_feedback_adapter.h b/modules/congestion_controller/transport_feedback_adapter.h
index b411c26..206236c 100644
--- a/modules/congestion_controller/transport_feedback_adapter.h
+++ b/modules/congestion_controller/transport_feedback_adapter.h
@@ -14,6 +14,7 @@
 #include <deque>
 #include <vector>
 
+#include "api/transport/network_types.h"
 #include "modules/congestion_controller/rtp/send_time_history.h"
 #include "rtc_base/criticalsection.h"
 #include "rtc_base/thread_annotations.h"
diff --git a/modules/include/module_common_types.cc b/modules/include/module_common_types.cc
new file mode 100644
index 0000000..4ad5d14
--- /dev/null
+++ b/modules/include/module_common_types.cc
@@ -0,0 +1,159 @@
+/*
+ *  Copyright (c) 2018 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/include/module_common_types.h"
+
+#include <string.h>
+#include <utility>
+
+#include "rtc_base/numerics/safe_conversions.h"
+
+namespace webrtc {
+
+RTPFragmentationHeader::RTPFragmentationHeader()
+    : fragmentationVectorSize(0),
+      fragmentationOffset(nullptr),
+      fragmentationLength(nullptr),
+      fragmentationTimeDiff(nullptr),
+      fragmentationPlType(nullptr) {}
+
+RTPFragmentationHeader::RTPFragmentationHeader(RTPFragmentationHeader&& other)
+    : RTPFragmentationHeader() {
+  swap(*this, other);
+}
+
+RTPFragmentationHeader& RTPFragmentationHeader::operator=(
+    RTPFragmentationHeader&& other) {
+  swap(*this, other);
+  return *this;
+}
+
+RTPFragmentationHeader::~RTPFragmentationHeader() {
+  delete[] fragmentationOffset;
+  delete[] fragmentationLength;
+  delete[] fragmentationTimeDiff;
+  delete[] fragmentationPlType;
+}
+
+void swap(RTPFragmentationHeader& a, RTPFragmentationHeader& b) {
+  using std::swap;
+  swap(a.fragmentationVectorSize, b.fragmentationVectorSize);
+  swap(a.fragmentationOffset, b.fragmentationOffset);
+  swap(a.fragmentationLength, b.fragmentationLength);
+  swap(a.fragmentationTimeDiff, b.fragmentationTimeDiff);
+  swap(a.fragmentationPlType, b.fragmentationPlType);
+}
+
+void RTPFragmentationHeader::CopyFrom(const RTPFragmentationHeader& src) {
+  if (this == &src) {
+    return;
+  }
+
+  if (src.fragmentationVectorSize != fragmentationVectorSize) {
+    // new size of vectors
+
+    // delete old
+    delete[] fragmentationOffset;
+    fragmentationOffset = nullptr;
+    delete[] fragmentationLength;
+    fragmentationLength = nullptr;
+    delete[] fragmentationTimeDiff;
+    fragmentationTimeDiff = nullptr;
+    delete[] fragmentationPlType;
+    fragmentationPlType = nullptr;
+
+    if (src.fragmentationVectorSize > 0) {
+      // allocate new
+      if (src.fragmentationOffset) {
+        fragmentationOffset = new size_t[src.fragmentationVectorSize];
+      }
+      if (src.fragmentationLength) {
+        fragmentationLength = new size_t[src.fragmentationVectorSize];
+      }
+      if (src.fragmentationTimeDiff) {
+        fragmentationTimeDiff = new uint16_t[src.fragmentationVectorSize];
+      }
+      if (src.fragmentationPlType) {
+        fragmentationPlType = new uint8_t[src.fragmentationVectorSize];
+      }
+    }
+    // set new size
+    fragmentationVectorSize = src.fragmentationVectorSize;
+  }
+
+  if (src.fragmentationVectorSize > 0) {
+    // copy values
+    if (src.fragmentationOffset) {
+      memcpy(fragmentationOffset, src.fragmentationOffset,
+             src.fragmentationVectorSize * sizeof(size_t));
+    }
+    if (src.fragmentationLength) {
+      memcpy(fragmentationLength, src.fragmentationLength,
+             src.fragmentationVectorSize * sizeof(size_t));
+    }
+    if (src.fragmentationTimeDiff) {
+      memcpy(fragmentationTimeDiff, src.fragmentationTimeDiff,
+             src.fragmentationVectorSize * sizeof(uint16_t));
+    }
+    if (src.fragmentationPlType) {
+      memcpy(fragmentationPlType, src.fragmentationPlType,
+             src.fragmentationVectorSize * sizeof(uint8_t));
+    }
+  }
+}
+
+void RTPFragmentationHeader::Resize(size_t size) {
+  const uint16_t size16 = rtc::dchecked_cast<uint16_t>(size);
+  if (fragmentationVectorSize < size16) {
+    uint16_t oldVectorSize = fragmentationVectorSize;
+    {
+      // offset
+      size_t* oldOffsets = fragmentationOffset;
+      fragmentationOffset = new size_t[size16];
+      memset(fragmentationOffset + oldVectorSize, 0,
+             sizeof(size_t) * (size16 - oldVectorSize));
+      // copy old values
+      memcpy(fragmentationOffset, oldOffsets, sizeof(size_t) * oldVectorSize);
+      delete[] oldOffsets;
+    }
+    // length
+    {
+      size_t* oldLengths = fragmentationLength;
+      fragmentationLength = new size_t[size16];
+      memset(fragmentationLength + oldVectorSize, 0,
+             sizeof(size_t) * (size16 - oldVectorSize));
+      memcpy(fragmentationLength, oldLengths, sizeof(size_t) * oldVectorSize);
+      delete[] oldLengths;
+    }
+    // time diff
+    {
+      uint16_t* oldTimeDiffs = fragmentationTimeDiff;
+      fragmentationTimeDiff = new uint16_t[size16];
+      memset(fragmentationTimeDiff + oldVectorSize, 0,
+             sizeof(uint16_t) * (size16 - oldVectorSize));
+      memcpy(fragmentationTimeDiff, oldTimeDiffs,
+             sizeof(uint16_t) * oldVectorSize);
+      delete[] oldTimeDiffs;
+    }
+    // payload type
+    {
+      uint8_t* oldTimePlTypes = fragmentationPlType;
+      fragmentationPlType = new uint8_t[size16];
+      memset(fragmentationPlType + oldVectorSize, 0,
+             sizeof(uint8_t) * (size16 - oldVectorSize));
+      memcpy(fragmentationPlType, oldTimePlTypes,
+             sizeof(uint8_t) * oldVectorSize);
+      delete[] oldTimePlTypes;
+    }
+    fragmentationVectorSize = size16;
+  }
+}
+
+}  // namespace webrtc
diff --git a/modules/include/module_common_types.h b/modules/include/module_common_types.h
index 8ee2369..4b4fd10 100644
--- a/modules/include/module_common_types.h
+++ b/modules/include/module_common_types.h
@@ -11,24 +11,14 @@
 #ifndef MODULES_INCLUDE_MODULE_COMMON_TYPES_H_
 #define MODULES_INCLUDE_MODULE_COMMON_TYPES_H_
 
-#include <assert.h>
-#include <string.h>  // memcpy
+#include <stddef.h>
+#include <stdint.h>
 
-#include <algorithm>
-#include <limits>
-
-#include "absl/types/optional.h"
 #include "api/rtp_headers.h"
-#include "api/transport/network_types.h"
-#include "api/video/video_rotation.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "modules/include/module_common_types_public.h"
 #include "modules/include/module_fec_types.h"
 #include "modules/rtp_rtcp/source/rtp_video_header.h"
-#include "rtc_base/constructormagic.h"
-#include "rtc_base/deprecation.h"
-#include "rtc_base/numerics/safe_conversions.h"
-#include "rtc_base/timeutils.h"
 
 namespace webrtc {
 
@@ -45,142 +35,29 @@
 
 class RTPFragmentationHeader {
  public:
-  RTPFragmentationHeader()
-      : fragmentationVectorSize(0),
-        fragmentationOffset(NULL),
-        fragmentationLength(NULL),
-        fragmentationTimeDiff(NULL),
-        fragmentationPlType(NULL) {}
+  RTPFragmentationHeader();
+  RTPFragmentationHeader(const RTPFragmentationHeader&) = delete;
+  RTPFragmentationHeader(RTPFragmentationHeader&& other);
+  RTPFragmentationHeader& operator=(const RTPFragmentationHeader& other) =
+      delete;
+  RTPFragmentationHeader& operator=(RTPFragmentationHeader&& other);
+  ~RTPFragmentationHeader();
 
-  RTPFragmentationHeader(RTPFragmentationHeader&& other)
-      : RTPFragmentationHeader() {
-    std::swap(*this, other);
-  }
+  friend void swap(RTPFragmentationHeader& a, RTPFragmentationHeader& b);
 
-  ~RTPFragmentationHeader() {
-    delete[] fragmentationOffset;
-    delete[] fragmentationLength;
-    delete[] fragmentationTimeDiff;
-    delete[] fragmentationPlType;
-  }
+  void CopyFrom(const RTPFragmentationHeader& src);
+  void VerifyAndAllocateFragmentationHeader(size_t size) { Resize(size); }
 
-  void operator=(RTPFragmentationHeader&& other) { std::swap(*this, other); }
+  void Resize(size_t size);
+  size_t Size() const { return fragmentationVectorSize; }
 
-  friend void swap(RTPFragmentationHeader& a, RTPFragmentationHeader& b) {
-    using std::swap;
-    swap(a.fragmentationVectorSize, b.fragmentationVectorSize);
-    swap(a.fragmentationOffset, b.fragmentationOffset);
-    swap(a.fragmentationLength, b.fragmentationLength);
-    swap(a.fragmentationTimeDiff, b.fragmentationTimeDiff);
-    swap(a.fragmentationPlType, b.fragmentationPlType);
-  }
+  size_t Offset(size_t index) const { return fragmentationOffset[index]; }
+  size_t Length(size_t index) const { return fragmentationLength[index]; }
+  uint16_t TimeDiff(size_t index) const { return fragmentationTimeDiff[index]; }
+  int PayloadType(size_t index) const { return fragmentationPlType[index]; }
 
-  void CopyFrom(const RTPFragmentationHeader& src) {
-    if (this == &src) {
-      return;
-    }
-
-    if (src.fragmentationVectorSize != fragmentationVectorSize) {
-      // new size of vectors
-
-      // delete old
-      delete[] fragmentationOffset;
-      fragmentationOffset = NULL;
-      delete[] fragmentationLength;
-      fragmentationLength = NULL;
-      delete[] fragmentationTimeDiff;
-      fragmentationTimeDiff = NULL;
-      delete[] fragmentationPlType;
-      fragmentationPlType = NULL;
-
-      if (src.fragmentationVectorSize > 0) {
-        // allocate new
-        if (src.fragmentationOffset) {
-          fragmentationOffset = new size_t[src.fragmentationVectorSize];
-        }
-        if (src.fragmentationLength) {
-          fragmentationLength = new size_t[src.fragmentationVectorSize];
-        }
-        if (src.fragmentationTimeDiff) {
-          fragmentationTimeDiff = new uint16_t[src.fragmentationVectorSize];
-        }
-        if (src.fragmentationPlType) {
-          fragmentationPlType = new uint8_t[src.fragmentationVectorSize];
-        }
-      }
-      // set new size
-      fragmentationVectorSize = src.fragmentationVectorSize;
-    }
-
-    if (src.fragmentationVectorSize > 0) {
-      // copy values
-      if (src.fragmentationOffset) {
-        memcpy(fragmentationOffset, src.fragmentationOffset,
-               src.fragmentationVectorSize * sizeof(size_t));
-      }
-      if (src.fragmentationLength) {
-        memcpy(fragmentationLength, src.fragmentationLength,
-               src.fragmentationVectorSize * sizeof(size_t));
-      }
-      if (src.fragmentationTimeDiff) {
-        memcpy(fragmentationTimeDiff, src.fragmentationTimeDiff,
-               src.fragmentationVectorSize * sizeof(uint16_t));
-      }
-      if (src.fragmentationPlType) {
-        memcpy(fragmentationPlType, src.fragmentationPlType,
-               src.fragmentationVectorSize * sizeof(uint8_t));
-      }
-    }
-  }
-
-  void VerifyAndAllocateFragmentationHeader(const size_t size) {
-    assert(size <= std::numeric_limits<uint16_t>::max());
-    const uint16_t size16 = static_cast<uint16_t>(size);
-    if (fragmentationVectorSize < size16) {
-      uint16_t oldVectorSize = fragmentationVectorSize;
-      {
-        // offset
-        size_t* oldOffsets = fragmentationOffset;
-        fragmentationOffset = new size_t[size16];
-        memset(fragmentationOffset + oldVectorSize, 0,
-               sizeof(size_t) * (size16 - oldVectorSize));
-        // copy old values
-        memcpy(fragmentationOffset, oldOffsets, sizeof(size_t) * oldVectorSize);
-        delete[] oldOffsets;
-      }
-      // length
-      {
-        size_t* oldLengths = fragmentationLength;
-        fragmentationLength = new size_t[size16];
-        memset(fragmentationLength + oldVectorSize, 0,
-               sizeof(size_t) * (size16 - oldVectorSize));
-        memcpy(fragmentationLength, oldLengths, sizeof(size_t) * oldVectorSize);
-        delete[] oldLengths;
-      }
-      // time diff
-      {
-        uint16_t* oldTimeDiffs = fragmentationTimeDiff;
-        fragmentationTimeDiff = new uint16_t[size16];
-        memset(fragmentationTimeDiff + oldVectorSize, 0,
-               sizeof(uint16_t) * (size16 - oldVectorSize));
-        memcpy(fragmentationTimeDiff, oldTimeDiffs,
-               sizeof(uint16_t) * oldVectorSize);
-        delete[] oldTimeDiffs;
-      }
-      // payload type
-      {
-        uint8_t* oldTimePlTypes = fragmentationPlType;
-        fragmentationPlType = new uint8_t[size16];
-        memset(fragmentationPlType + oldVectorSize, 0,
-               sizeof(uint8_t) * (size16 - oldVectorSize));
-        memcpy(fragmentationPlType, oldTimePlTypes,
-               sizeof(uint8_t) * oldVectorSize);
-        delete[] oldTimePlTypes;
-      }
-      fragmentationVectorSize = size16;
-    }
-  }
-
+  // TODO(danilchap): Move all members to private section,
+  // simplify by replacing 4 raw arrays with single std::vector<Fragment>
   uint16_t fragmentationVectorSize;  // Number of fragmentations
   size_t* fragmentationOffset;       // Offset of pointer to data for each
                                      // fragmentation
@@ -188,9 +65,6 @@
   uint16_t* fragmentationTimeDiff;   // Timestamp difference relative "now" for
                                      // each fragmentation
   uint8_t* fragmentationPlType;      // Payload type of each fragmentation
-
- private:
-  RTC_DISALLOW_COPY_AND_ASSIGN(RTPFragmentationHeader);
 };
 
 struct RTCPVoIPMetric {
diff --git a/modules/pacing/BUILD.gn b/modules/pacing/BUILD.gn
index c9689d9..602f396 100644
--- a/modules/pacing/BUILD.gn
+++ b/modules/pacing/BUILD.gn
@@ -25,6 +25,7 @@
     ":interval_budget",
     "..:module_api",
     "../../:webrtc_common",
+    "../../api/transport:network_control",
     "../../logging:rtc_event_bwe",
     "../../logging:rtc_event_log_api",
     "../../logging:rtc_event_pacing",
diff --git a/modules/pacing/bitrate_prober.h b/modules/pacing/bitrate_prober.h
index f0ceab2..b2548c0 100644
--- a/modules/pacing/bitrate_prober.h
+++ b/modules/pacing/bitrate_prober.h
@@ -13,6 +13,7 @@
 
 #include <queue>
 
+#include "api/transport/network_types.h"
 #include "modules/include/module_common_types.h"
 
 namespace webrtc {
diff --git a/modules/rtp_rtcp/BUILD.gn b/modules/rtp_rtcp/BUILD.gn
index 50204d3..e470869 100644
--- a/modules/rtp_rtcp/BUILD.gn
+++ b/modules/rtp_rtcp/BUILD.gn
@@ -90,6 +90,7 @@
     "../../api:array_view",
     "../../api:libjingle_peerconnection_api",
     "../../api/audio_codecs:audio_codecs_api",
+    "../../api/transport:network_control",
     "../../api/video:video_frame",
     "../../common_video",
     "../../rtc_base:checks",
diff --git a/modules/rtp_rtcp/include/rtp_rtcp_defines.h b/modules/rtp_rtcp/include/rtp_rtcp_defines.h
index 51e6337..0b2a238 100644
--- a/modules/rtp_rtcp/include/rtp_rtcp_defines.h
+++ b/modules/rtp_rtcp/include/rtp_rtcp_defines.h
@@ -18,6 +18,7 @@
 #include "absl/types/variant.h"
 #include "api/audio_codecs/audio_format.h"
 #include "api/rtp_headers.h"
+#include "api/transport/network_types.h"
 #include "common_types.h"  // NOLINT(build/include)
 #include "modules/include/module_common_types.h"
 #include "system_wrappers/include/clock.h"
diff --git a/modules/video_coding/codecs/test/videocodec_test_fixture_impl.cc b/modules/video_coding/codecs/test/videocodec_test_fixture_impl.cc
index c94b2f4..2251df7 100644
--- a/modules/video_coding/codecs/test/videocodec_test_fixture_impl.cc
+++ b/modules/video_coding/codecs/test/videocodec_test_fixture_impl.cc
@@ -11,6 +11,7 @@
 #include "modules/video_coding/codecs/test/videocodec_test_fixture_impl.h"
 
 #include <algorithm>
+#include <cmath>
 #include <memory>
 #include <utility>
 
diff --git a/sdk/android/src/jni/videodecoderwrapper.cc b/sdk/android/src/jni/videodecoderwrapper.cc
index d5812cd..6c1423c 100644
--- a/sdk/android/src/jni/videodecoderwrapper.cc
+++ b/sdk/android/src/jni/videodecoderwrapper.cc
@@ -15,6 +15,7 @@
 #include "modules/video_coding/utility/vp8_header_parser.h"
 #include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
 #include "rtc_base/logging.h"
+#include "rtc_base/timeutils.h"
 #include "sdk/android/generated_video_jni/jni/VideoDecoderWrapper_jni.h"
 #include "sdk/android/generated_video_jni/jni/VideoDecoder_jni.h"
 #include "sdk/android/native_api/jni/java_types.h"
diff --git a/sdk/android/src/jni/videoencoderwrapper.cc b/sdk/android/src/jni/videoencoderwrapper.cc
index c50e9f6..1d0f0bb 100644
--- a/sdk/android/src/jni/videoencoderwrapper.cc
+++ b/sdk/android/src/jni/videoencoderwrapper.cc
@@ -19,6 +19,7 @@
 #include "modules/video_coding/utility/vp8_header_parser.h"
 #include "modules/video_coding/utility/vp9_uncompressed_header_parser.h"
 #include "rtc_base/logging.h"
+#include "rtc_base/timeutils.h"
 #include "sdk/android/generated_video_jni/jni/VideoEncoderWrapper_jni.h"
 #include "sdk/android/generated_video_jni/jni/VideoEncoder_jni.h"
 #include "sdk/android/native_api/jni/class_loader.h"
diff --git a/video/video_stream_decoder_impl.cc b/video/video_stream_decoder_impl.cc
index 3e67e5c..2e8fc1a 100644
--- a/video/video_stream_decoder_impl.cc
+++ b/video/video_stream_decoder_impl.cc
@@ -13,6 +13,7 @@
 #include "absl/memory/memory.h"
 #include "rtc_base/logging.h"
 #include "rtc_base/numerics/mod_ops.h"
+#include "rtc_base/timeutils.h"
 
 namespace webrtc {