Index: webrtc/api/rtcstatscollector.cc |
diff --git a/webrtc/api/rtcstatscollector.cc b/webrtc/api/rtcstatscollector.cc |
index b14b39670e61ce8d08bd71118c7b296ca3c5d7e7..ee49759ddfc66a6c956b67c37d69fd64f1d186b4 100644 |
--- a/webrtc/api/rtcstatscollector.cc |
+++ b/webrtc/api/rtcstatscollector.cc |
@@ -231,7 +231,9 @@ RTCStatsCollector::RTCStatsCollector(PeerConnection* pc, |
num_pending_partial_reports_(0), |
partial_report_timestamp_us_(0), |
cache_timestamp_us_(0), |
- cache_lifetime_us_(cache_lifetime_us) { |
+ cache_lifetime_us_(cache_lifetime_us), |
+ data_channels_opened_(0), |
+ data_channels_closed_(0) { |
RTC_DCHECK(pc_); |
RTC_DCHECK(signaling_thread_); |
RTC_DCHECK(worker_thread_); |
@@ -470,23 +472,10 @@ void RTCStatsCollector::ProduceIceCandidateAndPairStats_s( |
void RTCStatsCollector::ProducePeerConnectionStats_s( |
int64_t timestamp_us, RTCStatsReport* report) const { |
RTC_DCHECK(signaling_thread_->IsCurrent()); |
- // TODO(hbos): If data channels are removed from the peer connection this will |
- // yield incorrect counts. Address before closing crbug.com/636818. See |
- // https://w3c.github.io/webrtc-stats/webrtc-stats.html#pcstats-dict*. |
- uint32_t data_channels_opened = 0; |
- const std::vector<rtc::scoped_refptr<DataChannel>>& data_channels = |
- pc_->sctp_data_channels(); |
- for (const rtc::scoped_refptr<DataChannel>& data_channel : data_channels) { |
- if (data_channel->state() == DataChannelInterface::kOpen) |
- ++data_channels_opened; |
- } |
- // There is always just one |RTCPeerConnectionStats| so its |id| can be a |
- // constant. |
std::unique_ptr<RTCPeerConnectionStats> stats( |
new RTCPeerConnectionStats("RTCPeerConnection", timestamp_us)); |
- stats->data_channels_opened = data_channels_opened; |
- stats->data_channels_closed = static_cast<uint32_t>(data_channels.size()) - |
- data_channels_opened; |
+ stats->data_channels_opened = data_channels_opened_; |
+ stats->data_channels_closed = data_channels_closed_; |
report->AddStats(std::move(stats)); |
} |
@@ -675,6 +664,32 @@ RTCStatsCollector::PrepareTransportCertificateStats_s( |
return transport_cert_stats; |
} |
+void RTCStatsCollector::OnDataChannelOpened(DataChannel* channel) { |
+ RTC_DCHECK(signaling_thread_->IsCurrent()); |
+ if (opened_data_channels_.insert(channel).second) { |
+ ++data_channels_opened_; |
hta-webrtc
2016/11/03 13:49:58
Isn't this equvalent to
bool result = opened_data
hbos
2016/11/03 15:02:02
Done.
|
+ } else { |
+ RTC_NOTREACHED(); |
+ } |
+} |
+ |
+void RTCStatsCollector::OnDataChannelClosed(DataChannel* channel) { |
+ RTC_DCHECK(signaling_thread_->IsCurrent()); |
+ // Only channels that have been fully opened (and have increased the |
+ // |data_channels_opened_| counter) increase the closed counter. |
+ if (opened_data_channels_.find(channel) != opened_data_channels_.end()) { |
+ ++data_channels_closed_; |
hta-webrtc
2016/11/03 13:49:58
Is it possible to get into the state where a chann
hbos
2016/11/03 15:02:02
From looking at the code I think this would happen
Taylor Brandstetter
2016/11/09 00:32:48
Yes, this is definitely legal. Any kind of underly
|
+ } |
+} |
+ |
+void RTCStatsCollector::OnDataChannelOpenedForTesting(DataChannel* channel) { |
+ OnDataChannelOpened(channel); |
+} |
+ |
+void RTCStatsCollector::OnDataChannelClosedForTesting(DataChannel* channel) { |
+ OnDataChannelClosed(channel); |
+} |
+ |
const char* CandidateTypeToRTCIceCandidateTypeForTesting( |
const std::string& type) { |
return CandidateTypeToRTCIceCandidateType(type); |