Add UMA counters for type of SDP semantic in use.

We count a) what semantics are asked for explicitly (if any),
and b) what semantics are reflected in the successfully
processed answer, as indicated by presence of msid lines
of type Unified Plan vs Plan B.

This gives an indication of usage in sessions initiated by
the browser. It does not indicate usage in sessions where the
browser is the answerer.

Bug: chromium:811683
Change-Id: I2e28a6a83df1664e1aa1e17cd4ff2921de1fba7e
Reviewed-on: https://webrtc-review.googlesource.com/52101
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22008}
diff --git a/api/umametrics.h b/api/umametrics.h
index 2347ac2..71a3d5e 100644
--- a/api/umametrics.h
+++ b/api/umametrics.h
@@ -38,6 +38,8 @@
   kEnumCounterIceRegathering,
   kEnumCounterIceRestart,
   kEnumCounterKeyProtocol,
+  kEnumCounterSdpSemanticRequested,
+  kEnumCounterSdpSemanticNegotiated,
   kPeerConnectionEnumCounterMax
 };
 
@@ -118,6 +120,21 @@
   kEnumCounterKeyProtocolMax
 };
 
+enum SdpSemanticRequested {
+  kSdpSemanticRequestDefault,
+  kSdpSemanticRequestPlanB,
+  kSdpSemanticRequestUnifiedPlan,
+  kSdpSemanticRequestMax
+};
+
+enum SdpSemanticNegotiated {
+  kSdpSemanticNegotiatedNone,
+  kSdpSemanticNegotiatedPlanB,
+  kSdpSemanticNegotiatedUnifiedPlan,
+  kSdpSemanticNegotiatedMixed,
+  kSdpSemanticNegotiatedMax
+};
+
 class MetricsObserverInterface : public rtc::RefCountInterface {
  public:
   // |type| is the type of the enum counter to be incremented. |counter|
diff --git a/pc/peerconnection.cc b/pc/peerconnection.cc
index 14c2a88..1bdd640 100644
--- a/pc/peerconnection.cc
+++ b/pc/peerconnection.cc
@@ -1976,6 +1976,34 @@
     network_thread()->Invoke<void>(
         RTC_FROM_HERE, rtc::Bind(&cricket::PortAllocator::DiscardCandidatePool,
                                  port_allocator_.get()));
+    // Make UMA notes about what was agreed to.
+    if (uma_observer_) {
+      switch (remote_description()->description()->msid_signaling()) {
+        case 0:
+          uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated,
+                                              kSdpSemanticNegotiatedNone,
+                                              kSdpSemanticNegotiatedMax);
+          break;
+        case cricket::kMsidSignalingMediaSection:
+          uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated,
+                                              kSdpSemanticNegotiatedUnifiedPlan,
+                                              kSdpSemanticNegotiatedMax);
+          break;
+        case cricket::kMsidSignalingSsrcAttribute:
+          uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated,
+                                              kSdpSemanticNegotiatedPlanB,
+                                              kSdpSemanticNegotiatedMax);
+          break;
+        case cricket::kMsidSignalingMediaSection |
+            cricket::kMsidSignalingSsrcAttribute:
+          uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticNegotiated,
+                                              kSdpSemanticNegotiatedMixed,
+                                              kSdpSemanticNegotiatedMax);
+          break;
+        default:
+          RTC_NOTREACHED();
+      }
+    }
   }
 
   observer->OnSetRemoteDescriptionComplete(RTCError::OK());
@@ -2675,6 +2703,27 @@
           kEnumCounterAddressFamily, kPeerConnection_IPv4,
           kPeerConnectionAddressFamilyCounter_Max);
     }
+    // Send information about the requested SDP semantics.
+    switch (configuration_.sdp_semantics) {
+      case SdpSemantics::kDefault:
+        uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested,
+                                            kSdpSemanticRequestDefault,
+                                            kSdpSemanticRequestMax);
+
+        break;
+      case SdpSemantics::kPlanB:
+        uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested,
+                                            kSdpSemanticRequestPlanB,
+                                            kSdpSemanticRequestMax);
+        break;
+      case SdpSemantics::kUnifiedPlan:
+        uma_observer_->IncrementEnumCounter(kEnumCounterSdpSemanticRequested,
+                                            kSdpSemanticRequestUnifiedPlan,
+                                            kSdpSemanticRequestMax);
+        break;
+      default:
+        RTC_NOTREACHED();
+    }
   }
 }
 
diff --git a/pc/peerconnection_rtp_unittest.cc b/pc/peerconnection_rtp_unittest.cc
index 023ce92..2f20261 100644
--- a/pc/peerconnection_rtp_unittest.cc
+++ b/pc/peerconnection_rtp_unittest.cc
@@ -13,6 +13,7 @@
 
 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
+#include "api/fakemetricsobserver.h"
 #include "api/jsep.h"
 #include "api/mediastreaminterface.h"
 #include "api/peerconnectioninterface.h"
@@ -889,6 +890,9 @@
   caller->AddAudioTrack("caller_audio");
   auto callee = CreatePeerConnectionWithUnifiedPlan();
   callee->AddAudioTrack("callee_audio");
+  auto caller_observer =
+      new rtc::RefCountedObject<webrtc::FakeMetricsObserver>();
+  caller->pc()->RegisterUMAObserver(caller_observer);
 
   ASSERT_TRUE(caller->ExchangeOfferAnswerWith(callee.get()));
 
@@ -902,6 +906,19 @@
   auto* answer = caller->pc()->remote_description();
   EXPECT_EQ(cricket::kMsidSignalingMediaSection,
             answer->description()->msid_signaling());
+  // Check that this is counted correctly
+  EXPECT_EQ(1, caller_observer->GetEnumCounter(
+                   webrtc::kEnumCounterSdpSemanticNegotiated,
+                   webrtc::kSdpSemanticNegotiatedUnifiedPlan));
+  EXPECT_EQ(0, caller_observer->GetEnumCounter(
+                   webrtc::kEnumCounterSdpSemanticNegotiated,
+                   webrtc::kSdpSemanticNegotiatedNone));
+  EXPECT_EQ(0, caller_observer->GetEnumCounter(
+                   webrtc::kEnumCounterSdpSemanticNegotiated,
+                   webrtc::kSdpSemanticNegotiatedPlanB));
+  EXPECT_EQ(0, caller_observer->GetEnumCounter(
+                   webrtc::kEnumCounterSdpSemanticNegotiated,
+                   webrtc::kSdpSemanticNegotiatedMixed));
 }
 
 TEST_F(PeerConnectionMsidSignalingTest, PlanBOfferToUnifiedPlanAnswer) {