19a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org/*
29a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
39a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *
49a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
59a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
69a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
79a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
89a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
99a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org */
109a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
11e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_
12e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#define WEBRTC_MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_
139a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
149a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include <vector>
159a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
16774b3d38a4a0f1a8ec08972a3c543cb5d607ce13henrike@webrtc.org#include "webrtc/base/constructormagic.h"
17e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/interface/neteq.h"
189a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org#include "webrtc/typedefs.h"
199a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
209a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgnamespace webrtc {
219a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
229a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// Forward declarations.
239a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgclass DecisionLogic;
249a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgclass DelayManager;
259a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
269a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org// This class handles various network statistics in NetEq.
279a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.orgclass StatisticsCalculator {
289a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org public:
299a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  StatisticsCalculator();
309a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
319a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  virtual ~StatisticsCalculator() {}
329a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
339a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Resets most of the counters.
349a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void Reset();
359a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
369a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Resets the counters that are not handled by Reset().
379a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void ResetMcu();
389a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
399a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Resets the waiting time statistics.
409a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void ResetWaitingTimeStatistics();
419a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
429a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Reports that |num_samples| samples were produced through expansion, and
439a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // that the expansion produced other than just noise samples.
449a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void ExpandedVoiceSamples(int num_samples);
459a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
469a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Reports that |num_samples| samples were produced through expansion, and
479a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // that the expansion produced only noise samples.
489a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void ExpandedNoiseSamples(int num_samples);
499a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
509a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Reports that |num_samples| samples were produced through preemptive
519a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // expansion.
529a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void PreemptiveExpandedSamples(int num_samples);
539a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
549a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Reports that |num_samples| samples were removed through accelerate.
559a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void AcceleratedSamples(int num_samples);
569a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
579a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Reports that |num_samples| zeros were inserted into the output.
589a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void AddZeros(int num_samples);
599a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
609a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Reports that |num_packets| packets were discarded.
619a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void PacketsDiscarded(int num_packets);
629a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
639a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Reports that |num_samples| were lost.
649a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void LostSamples(int num_samples);
659a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
669a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Increases the report interval counter with |num_samples| at a sample rate
679a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // of |fs_hz|.
689a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void IncreaseCounter(int num_samples, int fs_hz);
699a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
709a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Stores new packet waiting time in waiting time statistics.
719a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void StoreWaitingTime(int waiting_time_ms);
729a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
739a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Returns the current network statistics in |stats|. The current sample rate
749a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // is |fs_hz|, the total number of samples in packet buffer and sync buffer
759a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // yet to play out is |num_samples_in_buffers|, and the number of samples per
769a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // packet is |samples_per_packet|.
779a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void GetNetworkStatistics(int fs_hz,
789a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                            int num_samples_in_buffers,
799a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                            int samples_per_packet,
809a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                            const DelayManager& delay_manager,
819a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                            const DecisionLogic& decision_logic,
829a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org                            NetEqNetworkStatistics *stats);
839a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
849a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  void WaitingTimes(std::vector<int>* waiting_times);
859a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
869a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org private:
879a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static const int kMaxReportPeriod = 60;  // Seconds before auto-reset.
889a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static const int kLenWaitingTimes = 100;
899a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
909a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  // Calculates numerator / denominator, and returns the value in Q14.
919a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  static int CalculateQ14Ratio(uint32_t numerator, uint32_t denominator);
929a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
939a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t preemptive_samples_;
949a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t accelerate_samples_;
959a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int added_zero_samples_;
969a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t expanded_voice_samples_;
979a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t expanded_noise_samples_;
989a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int discarded_packets_;
999a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t lost_timestamps_;
1009a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  uint32_t last_report_timestamp_;
1019a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int waiting_times_[kLenWaitingTimes];  // Used as a circular buffer.
1029a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int len_waiting_times_;
1039a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  int next_waiting_time_index_;
1049a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1059a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org  DISALLOW_COPY_AND_ASSIGN(StatisticsCalculator);
1069a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org};
1079a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org
1089a400812ca0006d12e538d465ab6728a8ecd07aahenrik.lundin@webrtc.org}  // namespace webrtc
109e5abc854f3dc47de16067c2a41476c39b7626722henrik.lundin@webrtc.org#endif  // WEBRTC_MODULES_AUDIO_CODING_NETEQ_STATISTICS_CALCULATOR_H_
110