Rename api/stats_types.h to api/legacy_stats_types.h.

As to not break downstream projects, the old name api/stats_types.h is
kept around to help include api/legacy_stats_types.h. We can delete this
in a follow-up.

NOTRY=True

Bug: webrtc:14180
Change-Id: I270ca5e366ae36e324cbc9f982bbb066ab92d203
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/283081
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38610}
diff --git a/api/BUILD.gn b/api/BUILD.gn
index 56e9237..b3c5180 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -206,6 +206,8 @@
     "jsep_ice_candidate.cc",
     "jsep_ice_candidate.h",
     "jsep_session_description.h",
+    "legacy_stats_types.cc",
+    "legacy_stats_types.h",
     "peer_connection_interface.cc",
     "peer_connection_interface.h",
     "rtp_receiver_interface.cc",
@@ -217,7 +219,6 @@
     "sctp_transport_interface.h",
     "set_local_description_observer_interface.h",
     "set_remote_description_observer_interface.h",
-    "stats_types.cc",
     "stats_types.h",
     "uma_metrics.h",
     "video_track_source_proxy_factory.h",
diff --git a/api/DEPS b/api/DEPS
index 5f01204..bcfd705 100644
--- a/api/DEPS
+++ b/api/DEPS
@@ -182,7 +182,7 @@
     "+rtc_base/ref_count.h",
   ],
 
-  "stats_types\.h": [
+  "legacy_stats_types\.h": [
     "+rtc_base/ref_count.h",
     "+rtc_base/thread_checker.h",
   ],
diff --git a/api/stats_types.cc b/api/legacy_stats_types.cc
similarity index 99%
rename from api/stats_types.cc
rename to api/legacy_stats_types.cc
index 61a0b84..e3b2144 100644
--- a/api/stats_types.cc
+++ b/api/legacy_stats_types.cc
@@ -8,7 +8,7 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "api/stats_types.h"
+#include "api/legacy_stats_types.h"
 
 #include <string.h>
 
diff --git a/api/legacy_stats_types.h b/api/legacy_stats_types.h
new file mode 100644
index 0000000..a62e014
--- /dev/null
+++ b/api/legacy_stats_types.h
@@ -0,0 +1,455 @@
+/*
+ *  Copyright 2012 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.
+ */
+
+// This file contains structures used for retrieving statistics from an ongoing
+// libjingle session.
+
+#ifndef API_LEGACY_STATS_TYPES_H_
+#define API_LEGACY_STATS_TYPES_H_
+
+#include <algorithm>
+#include <list>
+#include <map>
+#include <string>
+#include <vector>
+
+#include "api/scoped_refptr.h"
+#include "api/sequence_checker.h"
+#include "rtc_base/ref_count.h"
+#include "rtc_base/system/rtc_export.h"
+
+namespace webrtc {
+
+class RTC_EXPORT StatsReport {
+ public:
+  // Indicates whether a track is for sending or receiving.
+  // Used in reports for audio/video tracks.
+  enum Direction {
+    kSend = 0,
+    kReceive,
+  };
+
+  enum StatsType {
+    // StatsReport types.
+    // A StatsReport of `type` = "googSession" contains overall information
+    // about the thing libjingle calls a session (which may contain one
+    // or more RTP sessions.
+    kStatsReportTypeSession,
+
+    // A StatsReport of `type` = "googTransport" contains information
+    // about a libjingle "transport".
+    kStatsReportTypeTransport,
+
+    // A StatsReport of `type` = "googComponent" contains information
+    // about a libjingle "channel" (typically, RTP or RTCP for a transport).
+    // This is intended to be the same thing as an ICE "Component".
+    kStatsReportTypeComponent,
+
+    // A StatsReport of `type` = "googCandidatePair" contains information
+    // about a libjingle "connection" - a single source/destination port pair.
+    // This is intended to be the same thing as an ICE "candidate pair".
+    kStatsReportTypeCandidatePair,
+
+    // A StatsReport of `type` = "VideoBWE" is statistics for video Bandwidth
+    // Estimation, which is global per-session.  The `id` field is "bweforvideo"
+    // (will probably change in the future).
+    kStatsReportTypeBwe,
+
+    // A StatsReport of `type` = "ssrc" is statistics for a specific rtp stream.
+    // The `id` field is the SSRC in decimal form of the rtp stream.
+    kStatsReportTypeSsrc,
+
+    // A StatsReport of `type` = "remoteSsrc" is statistics for a specific
+    // rtp stream, generated by the remote end of the connection.
+    kStatsReportTypeRemoteSsrc,
+
+    // A StatsReport of `type` = "googTrack" is statistics for a specific media
+    // track. The `id` field is the track id.
+    kStatsReportTypeTrack,
+
+    // A StatsReport of `type` = "localcandidate" or "remotecandidate" is
+    // attributes on a specific ICE Candidate. It links to its connection pair
+    // by candidate id. The string value is taken from
+    // http://w3c.github.io/webrtc-stats/#rtcstatstype-enum*.
+    kStatsReportTypeIceLocalCandidate,
+    kStatsReportTypeIceRemoteCandidate,
+
+    // A StatsReport of `type` = "googCertificate" contains an SSL certificate
+    // transmitted by one of the endpoints of this connection.  The `id` is
+    // controlled by the fingerprint, and is used to identify the certificate in
+    // the Channel stats (as "googLocalCertificateId" or
+    // "googRemoteCertificateId") and in any child certificates (as
+    // "googIssuerId").
+    kStatsReportTypeCertificate,
+
+    // A StatsReport of `type` = "datachannel" with statistics for a
+    // particular DataChannel.
+    kStatsReportTypeDataChannel,
+  };
+
+  enum StatsValueName {
+    kStatsValueNameActiveConnection,
+    kStatsValueNameAecDivergentFilterFraction,
+    kStatsValueNameAudioInputLevel,
+    kStatsValueNameAudioOutputLevel,
+    kStatsValueNameBytesReceived,
+    kStatsValueNameBytesSent,
+    kStatsValueNameCodecImplementationName,
+    kStatsValueNameConcealedSamples,
+    kStatsValueNameConcealmentEvents,
+    kStatsValueNameDataChannelId,
+    kStatsValueNameFramesDecoded,
+    kStatsValueNameFramesEncoded,
+    kStatsValueNameJitterBufferDelay,
+    kStatsValueNameMediaType,
+    kStatsValueNamePacketsLost,
+    kStatsValueNamePacketsReceived,
+    kStatsValueNamePacketsSent,
+    kStatsValueNameProtocol,
+    kStatsValueNameQpSum,
+    kStatsValueNameReceiving,
+    kStatsValueNameSelectedCandidatePairId,
+    kStatsValueNameSsrc,
+    kStatsValueNameState,
+    kStatsValueNameTotalAudioEnergy,
+    kStatsValueNameTotalSamplesDuration,
+    kStatsValueNameTotalSamplesReceived,
+    kStatsValueNameTransportId,
+    kStatsValueNameSentPingRequestsTotal,
+    kStatsValueNameSentPingRequestsBeforeFirstResponse,
+    kStatsValueNameSentPingResponses,
+    kStatsValueNameRecvPingRequests,
+    kStatsValueNameRecvPingResponses,
+    kStatsValueNameSentStunKeepaliveRequests,
+    kStatsValueNameRecvStunKeepaliveResponses,
+    kStatsValueNameStunKeepaliveRttTotal,
+    kStatsValueNameStunKeepaliveRttSquaredTotal,
+
+    // Internal StatsValue names.
+    kStatsValueNameAccelerateRate,
+    kStatsValueNameActualEncBitrate,
+    kStatsValueNameAdaptationChanges,
+    kStatsValueNameAvailableReceiveBandwidth,
+    kStatsValueNameAvailableSendBandwidth,
+    kStatsValueNameAvgEncodeMs,
+    kStatsValueNameBandwidthLimitedResolution,
+    kStatsValueNameBucketDelay,
+    kStatsValueNameCaptureStartNtpTimeMs,
+    kStatsValueNameCandidateIPAddress,
+    kStatsValueNameCandidateNetworkType,
+    kStatsValueNameCandidatePortNumber,
+    kStatsValueNameCandidatePriority,
+    kStatsValueNameCandidateTransportType,
+    kStatsValueNameCandidateType,
+    kStatsValueNameChannelId,
+    kStatsValueNameCodecName,
+    kStatsValueNameComponent,
+    kStatsValueNameContentName,
+    kStatsValueNameContentType,
+    kStatsValueNameCpuLimitedResolution,
+    kStatsValueNameCurrentDelayMs,
+    kStatsValueNameDecodeMs,
+    kStatsValueNameDecodingCNG,
+    kStatsValueNameDecodingCTN,
+    kStatsValueNameDecodingCTSG,
+    kStatsValueNameDecodingMutedOutput,
+    kStatsValueNameDecodingNormal,
+    kStatsValueNameDecodingPLC,
+    kStatsValueNameDecodingCodecPLC,
+    kStatsValueNameDecodingPLCCNG,
+    kStatsValueNameDer,
+    kStatsValueNameDtlsCipher,
+    kStatsValueNameEchoDelayMedian,
+    kStatsValueNameEchoDelayStdDev,
+    kStatsValueNameEchoReturnLoss,
+    kStatsValueNameEchoReturnLossEnhancement,
+    kStatsValueNameEncodeUsagePercent,
+    kStatsValueNameExpandRate,
+    kStatsValueNameFingerprint,
+    kStatsValueNameFingerprintAlgorithm,
+    kStatsValueNameFirsReceived,
+    kStatsValueNameFirsSent,
+    kStatsValueNameFirstFrameReceivedToDecodedMs,
+    kStatsValueNameFrameHeightInput,
+    kStatsValueNameFrameHeightReceived,
+    kStatsValueNameFrameHeightSent,
+    kStatsValueNameFrameRateDecoded,
+    kStatsValueNameFrameRateInput,
+    kStatsValueNameFrameRateOutput,
+    kStatsValueNameFrameRateReceived,
+    kStatsValueNameFrameRateSent,
+    kStatsValueNameFrameWidthInput,
+    kStatsValueNameFrameWidthReceived,
+    kStatsValueNameFrameWidthSent,
+    kStatsValueNameHasEnteredLowResolution,
+    kStatsValueNameHugeFramesSent,
+    kStatsValueNameInitiator,
+    kStatsValueNameInterframeDelayMaxMs,  // Max over last 10 seconds.
+    kStatsValueNameIssuerId,
+    kStatsValueNameJitterBufferMs,
+    kStatsValueNameJitterReceived,
+    kStatsValueNameLabel,
+    kStatsValueNameLocalAddress,
+    kStatsValueNameLocalCandidateId,
+    kStatsValueNameLocalCandidateType,
+    kStatsValueNameLocalCertificateId,
+    kStatsValueNameMaxDecodeMs,
+    kStatsValueNameMinPlayoutDelayMs,
+    kStatsValueNameNacksReceived,
+    kStatsValueNameNacksSent,
+    kStatsValueNamePlisReceived,
+    kStatsValueNamePlisSent,
+    kStatsValueNamePreemptiveExpandRate,
+    kStatsValueNamePreferredJitterBufferMs,
+    kStatsValueNameRemoteAddress,
+    kStatsValueNameRemoteCandidateId,
+    kStatsValueNameRemoteCandidateType,
+    kStatsValueNameRemoteCertificateId,
+    kStatsValueNameRenderDelayMs,
+    kStatsValueNameResidualEchoLikelihood,
+    kStatsValueNameResidualEchoLikelihoodRecentMax,
+    kStatsValueNameAnaBitrateActionCounter,
+    kStatsValueNameAnaChannelActionCounter,
+    kStatsValueNameAnaDtxActionCounter,
+    kStatsValueNameAnaFecActionCounter,
+    kStatsValueNameAnaFrameLengthIncreaseCounter,
+    kStatsValueNameAnaFrameLengthDecreaseCounter,
+    kStatsValueNameAnaUplinkPacketLossFraction,
+    kStatsValueNameRetransmitBitrate,
+    kStatsValueNameRtt,
+    kStatsValueNameSecondaryDecodedRate,
+    kStatsValueNameSecondaryDiscardedRate,
+    kStatsValueNameSendPacketsDiscarded,
+    kStatsValueNameSpeechExpandRate,
+    kStatsValueNameSrtpCipher,
+    kStatsValueNameTargetDelayMs,
+    kStatsValueNameTargetEncBitrate,
+    kStatsValueNameTimingFrameInfo,  // Result of `TimingFrameInfo::ToString`
+    kStatsValueNameTrackId,
+    kStatsValueNameTransmitBitrate,
+    kStatsValueNameTransportType,
+    kStatsValueNameWritable,
+    kStatsValueNameAudioDeviceUnderrunCounter,
+    kStatsValueNameLocalCandidateRelayProtocol,
+  };
+
+  class RTC_EXPORT IdBase : public rtc::RefCountInterface {
+   public:
+    ~IdBase() override;
+    StatsType type() const;
+
+    // Users of IdBase will be using the Id typedef, which is compatible with
+    // this Equals() function.  It simply calls the protected (and overridden)
+    // Equals() method.
+    bool Equals(const rtc::scoped_refptr<IdBase>& other) const {
+      return Equals(*other.get());
+    }
+
+    virtual std::string ToString() const = 0;
+
+   protected:
+    // Protected since users of the IdBase type will be using the Id typedef.
+    virtual bool Equals(const IdBase& other) const;
+
+    explicit IdBase(StatsType type);  // Only meant for derived classes.
+    const StatsType type_;
+
+    static const char kSeparator = '_';
+  };
+
+  typedef rtc::scoped_refptr<IdBase> Id;
+
+  struct RTC_EXPORT Value {
+    enum Type {
+      kInt,           // int.
+      kInt64,         // int64_t.
+      kFloat,         // float.
+      kString,        // std::string
+      kStaticString,  // const char*.
+      kBool,          // bool.
+      kId,            // Id.
+    };
+
+    Value(StatsValueName name, int64_t value, Type int_type);
+    Value(StatsValueName name, float f);
+    Value(StatsValueName name, const std::string& value);
+    Value(StatsValueName name, const char* value);
+    Value(StatsValueName name, bool b);
+    Value(StatsValueName name, const Id& value);
+
+    ~Value();
+
+    Value(const Value&) = delete;
+    Value& operator=(const Value&) = delete;
+
+    // Support ref counting. Note that for performance reasons, we
+    // don't use thread safe operations. Therefore, all operations
+    // affecting the ref count (in practice, creation and copying of
+    // the Values mapping) must occur on webrtc's signalling thread.
+    int AddRef() const {
+      RTC_DCHECK_RUN_ON(&thread_checker_);
+      return ++ref_count_;
+    }
+    int Release() const {
+      RTC_DCHECK_RUN_ON(&thread_checker_);
+      int count = --ref_count_;
+      if (!count)
+        delete this;
+      return count;
+    }
+
+    // TODO(tommi): This compares name as well as value...
+    // I think we should only need to compare the value part and
+    // move the name part into a hash map.
+    bool Equals(const Value& other) const;
+
+    // Comparison operators. Return true iff the current instance is of the
+    // correct type and holds the same value.  No conversion is performed so
+    // a string value of "123" is not equal to an int value of 123 and an int
+    // value of 123 is not equal to a float value of 123.0f.
+    // One exception to this is that types kInt and kInt64 can be compared and
+    // kString and kStaticString too.
+    bool operator==(const std::string& value) const;
+    bool operator==(const char* value) const;
+    bool operator==(int64_t value) const;
+    bool operator==(bool value) const;
+    bool operator==(float value) const;
+    bool operator==(const Id& value) const;
+
+    // Getters that allow getting the native value directly.
+    // The caller must know the type beforehand or else hit a check.
+    int int_val() const;
+    int64_t int64_val() const;
+    float float_val() const;
+    const char* static_string_val() const;
+    const std::string& string_val() const;
+    bool bool_val() const;
+    const Id& id_val() const;
+
+    // Returns the string representation of `name`.
+    const char* display_name() const;
+
+    // Converts the native value to a string representation of the value.
+    std::string ToString() const;
+
+    Type type() const { return type_; }
+
+    // TODO(tommi): Move `name` and `display_name` out of the Value struct.
+    const StatsValueName name;
+
+   private:
+    webrtc::SequenceChecker thread_checker_;
+    mutable int ref_count_ RTC_GUARDED_BY(thread_checker_) = 0;
+
+    const Type type_;
+    // TODO(tommi): Use C++ 11 union and make value_ const.
+    union InternalType {
+      int int_;
+      int64_t int64_;
+      float float_;
+      bool bool_;
+      std::string* string_;
+      const char* static_string_;
+      Id* id_;
+    } value_;
+  };
+
+  typedef rtc::scoped_refptr<Value> ValuePtr;
+  typedef std::map<StatsValueName, ValuePtr> Values;
+
+  // Ownership of `id` is passed to `this`.
+  explicit StatsReport(const Id& id);
+  ~StatsReport();
+
+  StatsReport(const StatsReport&) = delete;
+  StatsReport& operator=(const StatsReport&) = delete;
+
+  // Factory functions for various types of stats IDs.
+  static Id NewBandwidthEstimationId();
+  static Id NewTypedId(StatsType type, const std::string& id);
+  static Id NewTypedIntId(StatsType type, int id);
+  static Id NewIdWithDirection(StatsType type,
+                               const std::string& id,
+                               Direction direction);
+  static Id NewCandidateId(bool local, const std::string& id);
+  static Id NewComponentId(const std::string& content_name, int component);
+  static Id NewCandidatePairId(const std::string& content_name,
+                               int component,
+                               int index);
+
+  const Id& id() const { return id_; }
+  StatsType type() const { return id_->type(); }
+  double timestamp() const { return timestamp_; }
+  void set_timestamp(double t) { timestamp_ = t; }
+  bool empty() const { return values_.empty(); }
+  const Values& values() const { return values_; }
+
+  const char* TypeToString() const;
+
+  void AddString(StatsValueName name, const std::string& value);
+  void AddString(StatsValueName name, const char* value);
+  void AddInt64(StatsValueName name, int64_t value);
+  void AddInt(StatsValueName name, int value);
+  void AddFloat(StatsValueName name, float value);
+  void AddBoolean(StatsValueName name, bool value);
+  void AddId(StatsValueName name, const Id& value);
+
+  const Value* FindValue(StatsValueName name) const;
+
+ private:
+  // The unique identifier for this object.
+  // This is used as a key for this report in ordered containers,
+  // so it must never be changed.
+  const Id id_;
+  double timestamp_;  // Time since 1970-01-01T00:00:00Z in milliseconds.
+  Values values_;
+};
+
+// Typedef for an array of const StatsReport pointers.
+// Ownership of the pointers held by this implementation is assumed to lie
+// elsewhere and lifetime guarantees are made by the implementation that uses
+// this type.  In the StatsCollector, object ownership lies with the
+// StatsCollection class.
+typedef std::vector<const StatsReport*> StatsReports;
+
+// A map from the report id to the report.
+// This class wraps an STL container and provides a limited set of
+// functionality in order to keep things simple.
+class StatsCollection {
+ public:
+  StatsCollection();
+  ~StatsCollection();
+
+  typedef std::list<StatsReport*> Container;
+  typedef Container::iterator iterator;
+  typedef Container::const_iterator const_iterator;
+
+  const_iterator begin() const;
+  const_iterator end() const;
+  size_t size() const;
+
+  // Creates a new report object with `id` that does not already
+  // exist in the list of reports.
+  StatsReport* InsertNew(const StatsReport::Id& id);
+  StatsReport* FindOrAddNew(const StatsReport::Id& id);
+  StatsReport* ReplaceOrAddNew(const StatsReport::Id& id);
+
+  // Looks for a report with the given `id`.  If one is not found, null
+  // will be returned.
+  StatsReport* Find(const StatsReport::Id& id);
+
+ private:
+  Container list_;
+  webrtc::SequenceChecker thread_checker_;
+};
+
+}  // namespace webrtc
+
+#endif  // API_LEGACY_STATS_TYPES_H_
diff --git a/api/peer_connection_interface.h b/api/peer_connection_interface.h
index f8689eb..1037afa 100644
--- a/api/peer_connection_interface.h
+++ b/api/peer_connection_interface.h
@@ -94,6 +94,7 @@
 #include "api/field_trials_view.h"
 #include "api/ice_transport_interface.h"
 #include "api/jsep.h"
+#include "api/legacy_stats_types.h"
 #include "api/media_stream_interface.h"
 #include "api/media_types.h"
 #include "api/metronome/metronome.h"
@@ -112,7 +113,6 @@
 #include "api/set_local_description_observer_interface.h"
 #include "api/set_remote_description_observer_interface.h"
 #include "api/stats/rtc_stats_collector_callback.h"
-#include "api/stats_types.h"
 #include "api/task_queue/task_queue_factory.h"
 #include "api/transport/bitrate_settings.h"
 #include "api/transport/enums.h"
diff --git a/api/stats_types.h b/api/stats_types.h
index d75da46..cc94ecb 100644
--- a/api/stats_types.h
+++ b/api/stats_types.h
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2012 The WebRTC project authors. All Rights Reserved.
+ *  Copyright 2022 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
@@ -8,448 +8,12 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-// This file contains structures used for retrieving statistics from an ongoing
-// libjingle session.
-
 #ifndef API_STATS_TYPES_H_
 #define API_STATS_TYPES_H_
 
-#include <algorithm>
-#include <list>
-#include <map>
-#include <string>
-#include <vector>
-
-#include "api/scoped_refptr.h"
-#include "api/sequence_checker.h"
-#include "rtc_base/ref_count.h"
-#include "rtc_base/system/rtc_export.h"
-
-namespace webrtc {
-
-class RTC_EXPORT StatsReport {
- public:
-  // Indicates whether a track is for sending or receiving.
-  // Used in reports for audio/video tracks.
-  enum Direction {
-    kSend = 0,
-    kReceive,
-  };
-
-  enum StatsType {
-    // StatsReport types.
-    // A StatsReport of `type` = "googSession" contains overall information
-    // about the thing libjingle calls a session (which may contain one
-    // or more RTP sessions.
-    kStatsReportTypeSession,
-
-    // A StatsReport of `type` = "googTransport" contains information
-    // about a libjingle "transport".
-    kStatsReportTypeTransport,
-
-    // A StatsReport of `type` = "googComponent" contains information
-    // about a libjingle "channel" (typically, RTP or RTCP for a transport).
-    // This is intended to be the same thing as an ICE "Component".
-    kStatsReportTypeComponent,
-
-    // A StatsReport of `type` = "googCandidatePair" contains information
-    // about a libjingle "connection" - a single source/destination port pair.
-    // This is intended to be the same thing as an ICE "candidate pair".
-    kStatsReportTypeCandidatePair,
-
-    // A StatsReport of `type` = "VideoBWE" is statistics for video Bandwidth
-    // Estimation, which is global per-session.  The `id` field is "bweforvideo"
-    // (will probably change in the future).
-    kStatsReportTypeBwe,
-
-    // A StatsReport of `type` = "ssrc" is statistics for a specific rtp stream.
-    // The `id` field is the SSRC in decimal form of the rtp stream.
-    kStatsReportTypeSsrc,
-
-    // A StatsReport of `type` = "remoteSsrc" is statistics for a specific
-    // rtp stream, generated by the remote end of the connection.
-    kStatsReportTypeRemoteSsrc,
-
-    // A StatsReport of `type` = "googTrack" is statistics for a specific media
-    // track. The `id` field is the track id.
-    kStatsReportTypeTrack,
-
-    // A StatsReport of `type` = "localcandidate" or "remotecandidate" is
-    // attributes on a specific ICE Candidate. It links to its connection pair
-    // by candidate id. The string value is taken from
-    // http://w3c.github.io/webrtc-stats/#rtcstatstype-enum*.
-    kStatsReportTypeIceLocalCandidate,
-    kStatsReportTypeIceRemoteCandidate,
-
-    // A StatsReport of `type` = "googCertificate" contains an SSL certificate
-    // transmitted by one of the endpoints of this connection.  The `id` is
-    // controlled by the fingerprint, and is used to identify the certificate in
-    // the Channel stats (as "googLocalCertificateId" or
-    // "googRemoteCertificateId") and in any child certificates (as
-    // "googIssuerId").
-    kStatsReportTypeCertificate,
-
-    // A StatsReport of `type` = "datachannel" with statistics for a
-    // particular DataChannel.
-    kStatsReportTypeDataChannel,
-  };
-
-  enum StatsValueName {
-    kStatsValueNameActiveConnection,
-    kStatsValueNameAecDivergentFilterFraction,
-    kStatsValueNameAudioInputLevel,
-    kStatsValueNameAudioOutputLevel,
-    kStatsValueNameBytesReceived,
-    kStatsValueNameBytesSent,
-    kStatsValueNameCodecImplementationName,
-    kStatsValueNameConcealedSamples,
-    kStatsValueNameConcealmentEvents,
-    kStatsValueNameDataChannelId,
-    kStatsValueNameFramesDecoded,
-    kStatsValueNameFramesEncoded,
-    kStatsValueNameJitterBufferDelay,
-    kStatsValueNameMediaType,
-    kStatsValueNamePacketsLost,
-    kStatsValueNamePacketsReceived,
-    kStatsValueNamePacketsSent,
-    kStatsValueNameProtocol,
-    kStatsValueNameQpSum,
-    kStatsValueNameReceiving,
-    kStatsValueNameSelectedCandidatePairId,
-    kStatsValueNameSsrc,
-    kStatsValueNameState,
-    kStatsValueNameTotalAudioEnergy,
-    kStatsValueNameTotalSamplesDuration,
-    kStatsValueNameTotalSamplesReceived,
-    kStatsValueNameTransportId,
-    kStatsValueNameSentPingRequestsTotal,
-    kStatsValueNameSentPingRequestsBeforeFirstResponse,
-    kStatsValueNameSentPingResponses,
-    kStatsValueNameRecvPingRequests,
-    kStatsValueNameRecvPingResponses,
-    kStatsValueNameSentStunKeepaliveRequests,
-    kStatsValueNameRecvStunKeepaliveResponses,
-    kStatsValueNameStunKeepaliveRttTotal,
-    kStatsValueNameStunKeepaliveRttSquaredTotal,
-
-    // Internal StatsValue names.
-    kStatsValueNameAccelerateRate,
-    kStatsValueNameActualEncBitrate,
-    kStatsValueNameAdaptationChanges,
-    kStatsValueNameAvailableReceiveBandwidth,
-    kStatsValueNameAvailableSendBandwidth,
-    kStatsValueNameAvgEncodeMs,
-    kStatsValueNameBandwidthLimitedResolution,
-    kStatsValueNameBucketDelay,
-    kStatsValueNameCaptureStartNtpTimeMs,
-    kStatsValueNameCandidateIPAddress,
-    kStatsValueNameCandidateNetworkType,
-    kStatsValueNameCandidatePortNumber,
-    kStatsValueNameCandidatePriority,
-    kStatsValueNameCandidateTransportType,
-    kStatsValueNameCandidateType,
-    kStatsValueNameChannelId,
-    kStatsValueNameCodecName,
-    kStatsValueNameComponent,
-    kStatsValueNameContentName,
-    kStatsValueNameContentType,
-    kStatsValueNameCpuLimitedResolution,
-    kStatsValueNameCurrentDelayMs,
-    kStatsValueNameDecodeMs,
-    kStatsValueNameDecodingCNG,
-    kStatsValueNameDecodingCTN,
-    kStatsValueNameDecodingCTSG,
-    kStatsValueNameDecodingMutedOutput,
-    kStatsValueNameDecodingNormal,
-    kStatsValueNameDecodingPLC,
-    kStatsValueNameDecodingCodecPLC,
-    kStatsValueNameDecodingPLCCNG,
-    kStatsValueNameDer,
-    kStatsValueNameDtlsCipher,
-    kStatsValueNameEchoDelayMedian,
-    kStatsValueNameEchoDelayStdDev,
-    kStatsValueNameEchoReturnLoss,
-    kStatsValueNameEchoReturnLossEnhancement,
-    kStatsValueNameEncodeUsagePercent,
-    kStatsValueNameExpandRate,
-    kStatsValueNameFingerprint,
-    kStatsValueNameFingerprintAlgorithm,
-    kStatsValueNameFirsReceived,
-    kStatsValueNameFirsSent,
-    kStatsValueNameFirstFrameReceivedToDecodedMs,
-    kStatsValueNameFrameHeightInput,
-    kStatsValueNameFrameHeightReceived,
-    kStatsValueNameFrameHeightSent,
-    kStatsValueNameFrameRateDecoded,
-    kStatsValueNameFrameRateInput,
-    kStatsValueNameFrameRateOutput,
-    kStatsValueNameFrameRateReceived,
-    kStatsValueNameFrameRateSent,
-    kStatsValueNameFrameWidthInput,
-    kStatsValueNameFrameWidthReceived,
-    kStatsValueNameFrameWidthSent,
-    kStatsValueNameHasEnteredLowResolution,
-    kStatsValueNameHugeFramesSent,
-    kStatsValueNameInitiator,
-    kStatsValueNameInterframeDelayMaxMs,  // Max over last 10 seconds.
-    kStatsValueNameIssuerId,
-    kStatsValueNameJitterBufferMs,
-    kStatsValueNameJitterReceived,
-    kStatsValueNameLabel,
-    kStatsValueNameLocalAddress,
-    kStatsValueNameLocalCandidateId,
-    kStatsValueNameLocalCandidateType,
-    kStatsValueNameLocalCertificateId,
-    kStatsValueNameMaxDecodeMs,
-    kStatsValueNameMinPlayoutDelayMs,
-    kStatsValueNameNacksReceived,
-    kStatsValueNameNacksSent,
-    kStatsValueNamePlisReceived,
-    kStatsValueNamePlisSent,
-    kStatsValueNamePreemptiveExpandRate,
-    kStatsValueNamePreferredJitterBufferMs,
-    kStatsValueNameRemoteAddress,
-    kStatsValueNameRemoteCandidateId,
-    kStatsValueNameRemoteCandidateType,
-    kStatsValueNameRemoteCertificateId,
-    kStatsValueNameRenderDelayMs,
-    kStatsValueNameResidualEchoLikelihood,
-    kStatsValueNameResidualEchoLikelihoodRecentMax,
-    kStatsValueNameAnaBitrateActionCounter,
-    kStatsValueNameAnaChannelActionCounter,
-    kStatsValueNameAnaDtxActionCounter,
-    kStatsValueNameAnaFecActionCounter,
-    kStatsValueNameAnaFrameLengthIncreaseCounter,
-    kStatsValueNameAnaFrameLengthDecreaseCounter,
-    kStatsValueNameAnaUplinkPacketLossFraction,
-    kStatsValueNameRetransmitBitrate,
-    kStatsValueNameRtt,
-    kStatsValueNameSecondaryDecodedRate,
-    kStatsValueNameSecondaryDiscardedRate,
-    kStatsValueNameSendPacketsDiscarded,
-    kStatsValueNameSpeechExpandRate,
-    kStatsValueNameSrtpCipher,
-    kStatsValueNameTargetDelayMs,
-    kStatsValueNameTargetEncBitrate,
-    kStatsValueNameTimingFrameInfo,  // Result of `TimingFrameInfo::ToString`
-    kStatsValueNameTrackId,
-    kStatsValueNameTransmitBitrate,
-    kStatsValueNameTransportType,
-    kStatsValueNameWritable,
-    kStatsValueNameAudioDeviceUnderrunCounter,
-    kStatsValueNameLocalCandidateRelayProtocol,
-  };
-
-  class RTC_EXPORT IdBase : public rtc::RefCountInterface {
-   public:
-    ~IdBase() override;
-    StatsType type() const;
-
-    // Users of IdBase will be using the Id typedef, which is compatible with
-    // this Equals() function.  It simply calls the protected (and overridden)
-    // Equals() method.
-    bool Equals(const rtc::scoped_refptr<IdBase>& other) const {
-      return Equals(*other.get());
-    }
-
-    virtual std::string ToString() const = 0;
-
-   protected:
-    // Protected since users of the IdBase type will be using the Id typedef.
-    virtual bool Equals(const IdBase& other) const;
-
-    explicit IdBase(StatsType type);  // Only meant for derived classes.
-    const StatsType type_;
-
-    static const char kSeparator = '_';
-  };
-
-  typedef rtc::scoped_refptr<IdBase> Id;
-
-  struct RTC_EXPORT Value {
-    enum Type {
-      kInt,           // int.
-      kInt64,         // int64_t.
-      kFloat,         // float.
-      kString,        // std::string
-      kStaticString,  // const char*.
-      kBool,          // bool.
-      kId,            // Id.
-    };
-
-    Value(StatsValueName name, int64_t value, Type int_type);
-    Value(StatsValueName name, float f);
-    Value(StatsValueName name, const std::string& value);
-    Value(StatsValueName name, const char* value);
-    Value(StatsValueName name, bool b);
-    Value(StatsValueName name, const Id& value);
-
-    ~Value();
-
-    Value(const Value&) = delete;
-    Value& operator=(const Value&) = delete;
-
-    // Support ref counting. Note that for performance reasons, we
-    // don't use thread safe operations. Therefore, all operations
-    // affecting the ref count (in practice, creation and copying of
-    // the Values mapping) must occur on webrtc's signalling thread.
-    int AddRef() const {
-      RTC_DCHECK_RUN_ON(&thread_checker_);
-      return ++ref_count_;
-    }
-    int Release() const {
-      RTC_DCHECK_RUN_ON(&thread_checker_);
-      int count = --ref_count_;
-      if (!count)
-        delete this;
-      return count;
-    }
-
-    // TODO(tommi): This compares name as well as value...
-    // I think we should only need to compare the value part and
-    // move the name part into a hash map.
-    bool Equals(const Value& other) const;
-
-    // Comparison operators. Return true iff the current instance is of the
-    // correct type and holds the same value.  No conversion is performed so
-    // a string value of "123" is not equal to an int value of 123 and an int
-    // value of 123 is not equal to a float value of 123.0f.
-    // One exception to this is that types kInt and kInt64 can be compared and
-    // kString and kStaticString too.
-    bool operator==(const std::string& value) const;
-    bool operator==(const char* value) const;
-    bool operator==(int64_t value) const;
-    bool operator==(bool value) const;
-    bool operator==(float value) const;
-    bool operator==(const Id& value) const;
-
-    // Getters that allow getting the native value directly.
-    // The caller must know the type beforehand or else hit a check.
-    int int_val() const;
-    int64_t int64_val() const;
-    float float_val() const;
-    const char* static_string_val() const;
-    const std::string& string_val() const;
-    bool bool_val() const;
-    const Id& id_val() const;
-
-    // Returns the string representation of `name`.
-    const char* display_name() const;
-
-    // Converts the native value to a string representation of the value.
-    std::string ToString() const;
-
-    Type type() const { return type_; }
-
-    // TODO(tommi): Move `name` and `display_name` out of the Value struct.
-    const StatsValueName name;
-
-   private:
-    webrtc::SequenceChecker thread_checker_;
-    mutable int ref_count_ RTC_GUARDED_BY(thread_checker_) = 0;
-
-    const Type type_;
-    // TODO(tommi): Use C++ 11 union and make value_ const.
-    union InternalType {
-      int int_;
-      int64_t int64_;
-      float float_;
-      bool bool_;
-      std::string* string_;
-      const char* static_string_;
-      Id* id_;
-    } value_;
-  };
-
-  typedef rtc::scoped_refptr<Value> ValuePtr;
-  typedef std::map<StatsValueName, ValuePtr> Values;
-
-  // Ownership of `id` is passed to `this`.
-  explicit StatsReport(const Id& id);
-  ~StatsReport();
-
-  StatsReport(const StatsReport&) = delete;
-  StatsReport& operator=(const StatsReport&) = delete;
-
-  // Factory functions for various types of stats IDs.
-  static Id NewBandwidthEstimationId();
-  static Id NewTypedId(StatsType type, const std::string& id);
-  static Id NewTypedIntId(StatsType type, int id);
-  static Id NewIdWithDirection(StatsType type,
-                               const std::string& id,
-                               Direction direction);
-  static Id NewCandidateId(bool local, const std::string& id);
-  static Id NewComponentId(const std::string& content_name, int component);
-  static Id NewCandidatePairId(const std::string& content_name,
-                               int component,
-                               int index);
-
-  const Id& id() const { return id_; }
-  StatsType type() const { return id_->type(); }
-  double timestamp() const { return timestamp_; }
-  void set_timestamp(double t) { timestamp_ = t; }
-  bool empty() const { return values_.empty(); }
-  const Values& values() const { return values_; }
-
-  const char* TypeToString() const;
-
-  void AddString(StatsValueName name, const std::string& value);
-  void AddString(StatsValueName name, const char* value);
-  void AddInt64(StatsValueName name, int64_t value);
-  void AddInt(StatsValueName name, int value);
-  void AddFloat(StatsValueName name, float value);
-  void AddBoolean(StatsValueName name, bool value);
-  void AddId(StatsValueName name, const Id& value);
-
-  const Value* FindValue(StatsValueName name) const;
-
- private:
-  // The unique identifier for this object.
-  // This is used as a key for this report in ordered containers,
-  // so it must never be changed.
-  const Id id_;
-  double timestamp_;  // Time since 1970-01-01T00:00:00Z in milliseconds.
-  Values values_;
-};
-
-// Typedef for an array of const StatsReport pointers.
-// Ownership of the pointers held by this implementation is assumed to lie
-// elsewhere and lifetime guarantees are made by the implementation that uses
-// this type.  In the StatsCollector, object ownership lies with the
-// StatsCollection class.
-typedef std::vector<const StatsReport*> StatsReports;
-
-// A map from the report id to the report.
-// This class wraps an STL container and provides a limited set of
-// functionality in order to keep things simple.
-class StatsCollection {
- public:
-  StatsCollection();
-  ~StatsCollection();
-
-  typedef std::list<StatsReport*> Container;
-  typedef Container::iterator iterator;
-  typedef Container::const_iterator const_iterator;
-
-  const_iterator begin() const;
-  const_iterator end() const;
-  size_t size() const;
-
-  // Creates a new report object with `id` that does not already
-  // exist in the list of reports.
-  StatsReport* InsertNew(const StatsReport::Id& id);
-  StatsReport* FindOrAddNew(const StatsReport::Id& id);
-  StatsReport* ReplaceOrAddNew(const StatsReport::Id& id);
-
-  // Looks for a report with the given `id`.  If one is not found, null
-  // will be returned.
-  StatsReport* Find(const StatsReport::Id& id);
-
- private:
-  Container list_;
-  webrtc::SequenceChecker thread_checker_;
-};
-
-}  // namespace webrtc
+// TODO(https://crbug.com/webrtc/14180): When downstream dependencies are
+// including legacy_stats_types.h directly (or use spec-compliant stats) instead
+// of this file, delete it.
+#include "api/legacy_stats_types.h"
 
 #endif  // API_STATS_TYPES_H_
diff --git a/pc/legacy_stats_collector.h b/pc/legacy_stats_collector.h
index 21f51c5..cedd36c 100644
--- a/pc/legacy_stats_collector.h
+++ b/pc/legacy_stats_collector.h
@@ -27,10 +27,10 @@
 
 #include "absl/types/optional.h"
 #include "api/field_trials_view.h"
+#include "api/legacy_stats_types.h"
 #include "api/media_stream_interface.h"
 #include "api/peer_connection_interface.h"
 #include "api/scoped_refptr.h"
-#include "api/stats_types.h"
 #include "p2p/base/connection_info.h"
 #include "p2p/base/port.h"
 #include "pc/legacy_stats_collector_interface.h"
diff --git a/pc/peer_connection_message_handler.cc b/pc/peer_connection_message_handler.cc
index 2d674aa..8ddedde 100644
--- a/pc/peer_connection_message_handler.cc
+++ b/pc/peer_connection_message_handler.cc
@@ -13,11 +13,11 @@
 #include <utility>
 
 #include "api/jsep.h"
+#include "api/legacy_stats_types.h"
 #include "api/media_stream_interface.h"
 #include "api/peer_connection_interface.h"
 #include "api/scoped_refptr.h"
 #include "api/sequence_checker.h"
-#include "api/stats_types.h"
 #include "api/task_queue/pending_task_safety_flag.h"
 #include "pc/legacy_stats_collector_interface.h"
 #include "rtc_base/checks.h"
diff --git a/pc/peer_connection_message_handler.h b/pc/peer_connection_message_handler.h
index 1351a27..8bd0e5e 100644
--- a/pc/peer_connection_message_handler.h
+++ b/pc/peer_connection_message_handler.h
@@ -14,10 +14,10 @@
 #include <functional>
 
 #include "api/jsep.h"
+#include "api/legacy_stats_types.h"
 #include "api/media_stream_interface.h"
 #include "api/peer_connection_interface.h"
 #include "api/rtc_error.h"
-#include "api/stats_types.h"
 #include "api/task_queue/pending_task_safety_flag.h"
 #include "api/task_queue/task_queue_base.h"
 #include "pc/legacy_stats_collector_interface.h"