Home | History | Annotate | Download | only in video_engine
      1 /*
      2  *  Copyright (c) 2013 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 WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
     12 #define WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
     13 
     14 #include "webrtc/base/constructormagic.h"
     15 #include "webrtc/base/exp_filter.h"
     16 #include "webrtc/modules/interface/module.h"
     17 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     18 #include "webrtc/video_engine/include/vie_base.h"
     19 
     20 namespace webrtc {
     21 
     22 class Clock;
     23 class CpuOveruseObserver;
     24 class CriticalSectionWrapper;
     25 
     26 // TODO(pbos): Move this somewhere appropriate.
     27 class Statistics {
     28  public:
     29   Statistics();
     30 
     31   void AddSample(float sample_ms);
     32   void Reset();
     33   void SetOptions(const CpuOveruseOptions& options);
     34 
     35   float Mean() const;
     36   float StdDev() const;
     37   uint64_t Count() const;
     38 
     39  private:
     40   float InitialMean() const;
     41   float InitialVariance() const;
     42 
     43   float sum_;
     44   uint64_t count_;
     45   CpuOveruseOptions options_;
     46   scoped_ptr<rtc::ExpFilter> filtered_samples_;
     47   scoped_ptr<rtc::ExpFilter> filtered_variance_;
     48 };
     49 
     50 // Use to detect system overuse based on jitter in incoming frames.
     51 class OveruseFrameDetector : public Module {
     52  public:
     53   explicit OveruseFrameDetector(Clock* clock);
     54   ~OveruseFrameDetector();
     55 
     56   // Registers an observer receiving overuse and underuse callbacks. Set
     57   // 'observer' to NULL to disable callbacks.
     58   void SetObserver(CpuOveruseObserver* observer);
     59 
     60   // Sets options for overuse detection.
     61   void SetOptions(const CpuOveruseOptions& options);
     62 
     63   // Called for each captured frame.
     64   void FrameCaptured(int width, int height);
     65 
     66   // Called when the processing of a captured frame is started.
     67   void FrameProcessingStarted();
     68 
     69   // Called for each encoded frame.
     70   void FrameEncoded(int encode_time_ms);
     71 
     72   // Accessors.
     73 
     74   // Returns CpuOveruseMetrics where
     75   // capture_jitter_ms: The estimated jitter based on incoming captured frames.
     76   // avg_encode_time_ms: Running average of reported encode time
     77   //                     (FrameEncoded()). Only used for stats.
     78   // encode_usage_percent: The average encode time divided by the average time
     79   //                       difference between incoming captured frames.
     80   // capture_queue_delay_ms_per_s: The current time delay between an incoming
     81   //                               captured frame (FrameCaptured()) until the
     82   //                               frame is being processed
     83   //                               (FrameProcessingStarted()). (Note: if a new
     84   //                               frame is received before an old frame has
     85   //                               been processed, the old frame is skipped).
     86   //                               The delay is expressed in ms delay per sec.
     87   //                               Only used for stats.
     88   void GetCpuOveruseMetrics(CpuOveruseMetrics* metrics) const;
     89 
     90   int CaptureQueueDelayMsPerS() const;
     91 
     92   // Implements Module.
     93   virtual int32_t TimeUntilNextProcess() OVERRIDE;
     94   virtual int32_t Process() OVERRIDE;
     95 
     96  private:
     97   class EncodeTimeAvg;
     98   class EncodeTimeRsd;
     99   class EncodeUsage;
    100   class CaptureQueueDelay;
    101 
    102   bool IsOverusing();
    103   bool IsUnderusing(int64_t time_now);
    104 
    105   bool FrameTimeoutDetected(int64_t now) const;
    106   bool FrameSizeChanged(int num_pixels) const;
    107 
    108   void ResetAll(int num_pixels);
    109 
    110   // Protecting all members.
    111   scoped_ptr<CriticalSectionWrapper> crit_;
    112 
    113   // Observer getting overuse reports.
    114   CpuOveruseObserver* observer_;
    115 
    116   CpuOveruseOptions options_;
    117 
    118   Clock* clock_;
    119   int64_t next_process_time_;
    120   int64_t num_process_times_;
    121 
    122   Statistics capture_deltas_;
    123   int64_t last_capture_time_;
    124 
    125   int64_t last_overuse_time_;
    126   int checks_above_threshold_;
    127   int num_overuse_detections_;
    128 
    129   int64_t last_rampup_time_;
    130   bool in_quick_rampup_;
    131   int current_rampup_delay_ms_;
    132 
    133   // Number of pixels of last captured frame.
    134   int num_pixels_;
    135 
    136   int64_t last_encode_sample_ms_;
    137   scoped_ptr<EncodeTimeAvg> encode_time_;
    138   scoped_ptr<EncodeTimeRsd> encode_rsd_;
    139   scoped_ptr<EncodeUsage> encode_usage_;
    140 
    141   scoped_ptr<CaptureQueueDelay> capture_queue_delay_;
    142 
    143   DISALLOW_COPY_AND_ASSIGN(OveruseFrameDetector);
    144 };
    145 
    146 }  // namespace webrtc
    147 
    148 #endif  // WEBRTC_VIDEO_ENGINE_OVERUSE_FRAME_DETECTOR_H_
    149