1 /*
2 * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "test/pc/e2e/stats_poller.h"
12
13 #include <utility>
14
15 #include "rtc_base/logging.h"
16 #include "rtc_base/synchronization/mutex.h"
17
18 namespace webrtc {
19 namespace webrtc_pc_e2e {
20
PollStats()21 void InternalStatsObserver::PollStats() {
22 peer_->GetStats(this);
23 }
24
OnStatsDelivered(const rtc::scoped_refptr<const RTCStatsReport> & report)25 void InternalStatsObserver::OnStatsDelivered(
26 const rtc::scoped_refptr<const RTCStatsReport>& report) {
27 for (auto* observer : observers_) {
28 observer->OnStatsReports(pc_label_, report);
29 }
30 }
31
StatsPoller(std::vector<StatsObserverInterface * > observers,std::map<std::string,StatsProvider * > peers)32 StatsPoller::StatsPoller(std::vector<StatsObserverInterface*> observers,
33 std::map<std::string, StatsProvider*> peers)
34 : observers_(std::move(observers)) {
35 webrtc::MutexLock lock(&mutex_);
36 for (auto& peer : peers) {
37 pollers_.push_back(rtc::make_ref_counted<InternalStatsObserver>(
38 peer.first, peer.second, observers_));
39 }
40 }
41
StatsPoller(std::vector<StatsObserverInterface * > observers,std::map<std::string,TestPeer * > peers)42 StatsPoller::StatsPoller(std::vector<StatsObserverInterface*> observers,
43 std::map<std::string, TestPeer*> peers)
44 : observers_(std::move(observers)) {
45 webrtc::MutexLock lock(&mutex_);
46 for (auto& peer : peers) {
47 pollers_.push_back(rtc::make_ref_counted<InternalStatsObserver>(
48 peer.first, peer.second, observers_));
49 }
50 }
51
PollStatsAndNotifyObservers()52 void StatsPoller::PollStatsAndNotifyObservers() {
53 webrtc::MutexLock lock(&mutex_);
54 for (auto& poller : pollers_) {
55 poller->PollStats();
56 }
57 }
58
RegisterParticipantInCall(absl::string_view peer_name,StatsProvider * peer)59 void StatsPoller::RegisterParticipantInCall(absl::string_view peer_name,
60 StatsProvider* peer) {
61 webrtc::MutexLock lock(&mutex_);
62 pollers_.push_back(rtc::make_ref_counted<InternalStatsObserver>(
63 peer_name, peer, observers_));
64 }
65
UnregisterParticipantInCall(absl::string_view peer_name)66 bool StatsPoller::UnregisterParticipantInCall(absl::string_view peer_name) {
67 webrtc::MutexLock lock(&mutex_);
68 for (auto it = pollers_.begin(); it != pollers_.end(); ++it) {
69 if ((*it)->pc_label() == peer_name) {
70 pollers_.erase(it);
71 return true;
72 }
73 }
74 return false;
75 }
76
77 } // namespace webrtc_pc_e2e
78 } // namespace webrtc
79