Fix segfault when PeerConnection is destroyed during stats collection.

RTCStatsCollector relies on PeerConnection and its WebRtcSession. If the
PeerConnection is destroyed, reference counting keeps the
RTCStatsCollector alive until the request has completed. But the request
is using PeerConnection/WebRtcSession resources that are destroyed in
~PeerConnection().

To get around this problem, RTCStatsCollector::WaitForPendingRequest()
is added, which is invoked at ~PeerConnection().

Integration test added, it caused a segmentation fault before this
change / EXPECT failure.

BUG=chromium:627816

Review-Url: https://codereview.webrtc.org/2583613003
Cr-Commit-Position: refs/heads/master@{#15674}
diff --git a/webrtc/api/rtcstats_integrationtest.cc b/webrtc/api/rtcstats_integrationtest.cc
index db54b17..c28130f 100644
--- a/webrtc/api/rtcstats_integrationtest.cc
+++ b/webrtc/api/rtcstats_integrationtest.cc
@@ -537,6 +537,19 @@
   RTCStatsReportVerifier(report.get()).VerifyReport();
 }
 
+TEST_F(RTCStatsIntegrationTest, GetsStatsWhileDestroyingPeerConnections) {
+  StartCall();
+
+  rtc::scoped_refptr<RTCStatsObtainer> stats_obtainer =
+      RTCStatsObtainer::Create();
+  caller_->pc()->GetStats(stats_obtainer);
+  // This will destroy the peer connection.
+  caller_ = nullptr;
+  // Any pending stats requests should have completed in the act of destroying
+  // the peer connection.
+  EXPECT_TRUE(stats_obtainer->report());
+}
+
 }  // namespace
 
 }  // namespace webrtc