1 /*
2  *  Copyright (c) 2018 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 MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_STATS_GETTER_H_
12 #define MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_STATS_GETTER_H_
13 
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
19 #include "modules/audio_coding/neteq/tools/neteq_test.h"
20 
21 namespace webrtc {
22 namespace test {
23 
24 class NetEqStatsGetter : public NetEqGetAudioCallback {
25  public:
26   // This struct is a replica of webrtc::NetEqNetworkStatistics, but with all
27   // values stored in double precision.
28   struct Stats {
29     double current_buffer_size_ms = 0.0;
30     double preferred_buffer_size_ms = 0.0;
31     double jitter_peaks_found = 0.0;
32     double packet_loss_rate = 0.0;
33     double expand_rate = 0.0;
34     double speech_expand_rate = 0.0;
35     double preemptive_rate = 0.0;
36     double accelerate_rate = 0.0;
37     double secondary_decoded_rate = 0.0;
38     double secondary_discarded_rate = 0.0;
39     double clockdrift_ppm = 0.0;
40     double added_zero_samples = 0.0;
41     double mean_waiting_time_ms = 0.0;
42     double median_waiting_time_ms = 0.0;
43     double min_waiting_time_ms = 0.0;
44     double max_waiting_time_ms = 0.0;
45   };
46 
47   struct ConcealmentEvent {
48     uint64_t duration_ms;
49     size_t concealment_event_number;
50     int64_t time_from_previous_event_end_ms;
51     std::string ToString() const;
52   };
53 
54   // Takes a pointer to another callback object, which will be invoked after
55   // this object finishes. This does not transfer ownership, and null is a
56   // valid value.
57   explicit NetEqStatsGetter(std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer);
58 
set_stats_query_interval_ms(int64_t stats_query_interval_ms)59   void set_stats_query_interval_ms(int64_t stats_query_interval_ms) {
60     stats_query_interval_ms_ = stats_query_interval_ms;
61   }
62 
63   void BeforeGetAudio(NetEq* neteq) override;
64 
65   void AfterGetAudio(int64_t time_now_ms,
66                      const AudioFrame& audio_frame,
67                      bool muted,
68                      NetEq* neteq) override;
69 
70   double AverageSpeechExpandRate() const;
71 
delay_analyzer()72   NetEqDelayAnalyzer* delay_analyzer() const { return delay_analyzer_.get(); }
73 
concealment_events()74   const std::vector<ConcealmentEvent>& concealment_events() const {
75     // Do not account for the last concealment event to avoid potential end
76     // call skewing.
77     return concealment_events_;
78   }
79 
stats()80   const std::vector<std::pair<int64_t, NetEqNetworkStatistics>>* stats() const {
81     return &stats_;
82   }
83 
84   const std::vector<std::pair<int64_t, NetEqLifetimeStatistics>>*
lifetime_stats()85   lifetime_stats() const {
86     return &lifetime_stats_;
87   }
88 
89   Stats AverageStats() const;
90 
91  private:
92   std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer_;
93   int64_t stats_query_interval_ms_ = 1000;
94   int64_t last_stats_query_time_ms_ = 0;
95   std::vector<std::pair<int64_t, NetEqNetworkStatistics>> stats_;
96   std::vector<std::pair<int64_t, NetEqLifetimeStatistics>> lifetime_stats_;
97   size_t current_concealment_event_ = 1;
98   uint64_t voice_concealed_samples_until_last_event_ = 0;
99   std::vector<ConcealmentEvent> concealment_events_;
100   int64_t last_event_end_time_ms_ = 0;
101 };
102 
103 }  // namespace test
104 }  // namespace webrtc
105 
106 #endif  // MODULES_AUDIO_CODING_NETEQ_TOOLS_NETEQ_STATS_GETTER_H_
107