xref: /aosp_15_r20/external/webrtc/test/pc/e2e/stats_poller.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #ifndef TEST_PC_E2E_STATS_POLLER_H_
12 #define TEST_PC_E2E_STATS_POLLER_H_
13 
14 #include <map>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 #include "api/peer_connection_interface.h"
20 #include "api/stats/rtc_stats_collector_callback.h"
21 #include "api/test/stats_observer_interface.h"
22 #include "rtc_base/synchronization/mutex.h"
23 #include "rtc_base/thread_annotations.h"
24 #include "test/pc/e2e/stats_provider.h"
25 #include "test/pc/e2e/test_peer.h"
26 
27 namespace webrtc {
28 namespace webrtc_pc_e2e {
29 
30 // Helper class that will notify all the webrtc::test::StatsObserverInterface
31 // objects subscribed.
32 class InternalStatsObserver : public RTCStatsCollectorCallback {
33  public:
InternalStatsObserver(absl::string_view pc_label,StatsProvider * peer,std::vector<StatsObserverInterface * > observers)34   InternalStatsObserver(absl::string_view pc_label,
35                         StatsProvider* peer,
36                         std::vector<StatsObserverInterface*> observers)
37       : pc_label_(pc_label), peer_(peer), observers_(std::move(observers)) {}
38 
pc_label()39   std::string pc_label() const { return pc_label_; }
40 
41   void PollStats();
42 
43   void OnStatsDelivered(
44       const rtc::scoped_refptr<const RTCStatsReport>& report) override;
45 
46  private:
47   std::string pc_label_;
48   StatsProvider* peer_;
49   std::vector<StatsObserverInterface*> observers_;
50 };
51 
52 // Helper class to invoke GetStats on a PeerConnection by passing a
53 // webrtc::StatsObserver that will notify all the
54 // webrtc::test::StatsObserverInterface subscribed.
55 class StatsPoller {
56  public:
57   StatsPoller(std::vector<StatsObserverInterface*> observers,
58               std::map<std::string, StatsProvider*> peers_to_observe);
59   StatsPoller(std::vector<StatsObserverInterface*> observers,
60               std::map<std::string, TestPeer*> peers_to_observe);
61 
62   void PollStatsAndNotifyObservers();
63 
64   void RegisterParticipantInCall(absl::string_view peer_name,
65                                  StatsProvider* peer);
66   // Unregister participant from stats poller. Returns true if participant was
67   // removed and false if participant wasn't found.
68   bool UnregisterParticipantInCall(absl::string_view peer_name);
69 
70  private:
71   const std::vector<StatsObserverInterface*> observers_;
72   webrtc::Mutex mutex_;
73   std::vector<rtc::scoped_refptr<InternalStatsObserver>> pollers_
74       RTC_GUARDED_BY(mutex_);
75 };
76 
77 }  // namespace webrtc_pc_e2e
78 }  // namespace webrtc
79 
80 #endif  // TEST_PC_E2E_STATS_POLLER_H_
81