• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // libjingle
2 // Copyright 2010 Google Inc.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 //  1. Redistributions of source code must retain the above copyright notice,
8 //     this list of conditions and the following disclaimer.
9 //  2. Redistributions in binary form must reproduce the above copyright notice,
10 //     this list of conditions and the following disclaimer in the documentation
11 //     and/or other materials provided with the distribution.
12 //  3. The name of the author may not be used to endorse or promote products
13 //     derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef TALK_MEDIA_BASE_VIDEOADAPTER_H_  // NOLINT
27 #define TALK_MEDIA_BASE_VIDEOADAPTER_H_
28 
29 #include "talk/base/common.h"  // For ASSERT
30 #include "talk/base/criticalsection.h"
31 #include "talk/base/scoped_ptr.h"
32 #include "talk/base/sigslot.h"
33 #include "talk/media/base/videocommon.h"
34 
35 namespace cricket {
36 
37 class VideoFrame;
38 
39 // VideoAdapter adapts an input video frame to an output frame based on the
40 // specified input and output formats. The adaptation includes dropping frames
41 // to reduce frame rate and scaling frames. VideoAdapter is thread safe.
42 class VideoAdapter {
43  public:
44   VideoAdapter();
45   virtual ~VideoAdapter();
46 
47   virtual void SetInputFormat(const VideoFormat& format);
48   void SetOutputFormat(const VideoFormat& format);
49   // Constrain output resolution to this many pixels overall
50   void SetOutputNumPixels(int num_pixels);
51   int GetOutputNumPixels() const;
52 
53   const VideoFormat& input_format();
54   // Returns true if the adapter is dropping frames in calls to AdaptFrame.
55   bool drops_all_frames() const;
56   const VideoFormat& output_format();
57   // If the parameter black is true, the adapted frames will be black.
58   void SetBlackOutput(bool black);
59 
60   // Adapt the input frame from the input format to the output format. Return
61   // true and set the output frame to NULL if the input frame is dropped. Return
62   // true and set the out frame to output_frame_ if the input frame is adapted
63   // successfully. Return false otherwise.
64   // Note that, if no adaptation is required, |out_frame| will refer directly
65   // in_frame. If a copy is always required, the caller must do an explicit
66   // copy.
67   // If a copy has taken place, |output_frame_| is owned by the VideoAdapter
68   // and will remain usable until the adapter is destroyed or AdaptFrame is
69   // called again.
70   bool AdaptFrame(VideoFrame* in_frame, VideoFrame** out_frame);
71 
72   void set_scale_third(bool enable);
scale_third()73   bool scale_third() const { return scale_third_; }
74 
75  protected:
76   float FindClosestScale(int width, int height, int target_num_pixels);
77   float FindClosestViewScale(int width, int height, int target_num_pixels);
78   float FindLowerScale(int width, int height, int target_num_pixels);
79 
80  private:
81   const float* GetViewScaleFactors() const;
82   float FindScale(const float* scale_factors,
83                   const float upbias, int width, int height,
84                   int target_num_pixels);
85   bool StretchToOutputFrame(const VideoFrame* in_frame);
86 
87   VideoFormat input_format_;
88   VideoFormat output_format_;
89   int output_num_pixels_;
90   bool scale_third_;  // True if adapter allows scaling to 1/3 and 2/3.
91   int frames_in_;  // Number of input frames.
92   int frames_out_;  // Number of output frames.
93   int frames_scaled_;  // Number of frames scaled.
94   int adaption_changes_;  // Number of changes in scale factor.
95   size_t previous_width_;  // Previous adapter output width.
96   size_t previous_height_;  // Previous adapter output height.
97   bool black_output_;  // Flag to tell if we need to black output_frame_.
98   bool is_black_;  // Flag to tell if output_frame_ is currently black.
99   int64 interval_next_frame_;
100   talk_base::scoped_ptr<VideoFrame> output_frame_;
101   // The critical section to protect the above variables.
102   talk_base::CriticalSection critical_section_;
103 
104   DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
105 };
106 
107 // CoordinatedVideoAdapter adapts the video input to the encoder by coordinating
108 // the format request from the server, the resolution request from the encoder,
109 // and the CPU load.
110 class CoordinatedVideoAdapter
111     : public VideoAdapter, public sigslot::has_slots<>  {
112  public:
113   enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE };
114   enum AdaptReasonEnum {
115     ADAPTREASON_NONE = 0,
116     ADAPTREASON_CPU = 1,
117     ADAPTREASON_BANDWIDTH = 2,
118     ADAPTREASON_VIEW = 4
119   };
120   typedef int AdaptReason;
121 
122   CoordinatedVideoAdapter();
~CoordinatedVideoAdapter()123   virtual ~CoordinatedVideoAdapter() {}
124 
125   virtual void SetInputFormat(const VideoFormat& format);
126 
127   // Enable or disable video adaptation due to the change of the CPU load.
set_cpu_adaptation(bool enable)128   void set_cpu_adaptation(bool enable) { cpu_adaptation_ = enable; }
cpu_adaptation()129   bool cpu_adaptation() const { return cpu_adaptation_; }
130   // Enable or disable smoothing when doing CPU adaptation. When smoothing is
131   // enabled, system CPU load is tracked using an exponential weighted
132   // average.
133   void set_cpu_smoothing(bool enable);
cpu_smoothing()134   bool cpu_smoothing() const { return cpu_smoothing_; }
135   // Enable or disable video adaptation due to the change of the GD
set_gd_adaptation(bool enable)136   void set_gd_adaptation(bool enable) { gd_adaptation_ = enable; }
gd_adaptation()137   bool gd_adaptation() const { return gd_adaptation_; }
138   // Enable or disable video adaptation due to the change of the View
set_view_adaptation(bool enable)139   void set_view_adaptation(bool enable) { view_adaptation_ = enable; }
view_adaptation()140   bool view_adaptation() const { return view_adaptation_; }
141   // Enable or disable video adaptation to fast switch View
set_view_switch(bool enable)142   void set_view_switch(bool enable) { view_switch_ = enable; }
view_switch()143   bool view_switch() const { return view_switch_; }
144 
adapt_reason()145   CoordinatedVideoAdapter::AdaptReason adapt_reason() const {
146     return adapt_reason_;
147   }
148 
149   // When the video is decreased, set the waiting time for CPU adaptation to
150   // decrease video again.
151   void set_cpu_load_min_samples(int cpu_load_min_samples);
cpu_load_min_samples()152   int cpu_load_min_samples() const { return cpu_load_min_samples_; }
153   // CPU system load high threshold for reducing resolution.  e.g. 0.85f
154   void set_high_system_threshold(float high_system_threshold);
high_system_threshold()155   float high_system_threshold() const { return high_system_threshold_; }
156   // CPU system load low threshold for increasing resolution.  e.g. 0.70f
157   void set_low_system_threshold(float low_system_threshold);
low_system_threshold()158   float low_system_threshold() const { return low_system_threshold_; }
159   // CPU process load threshold for reducing resolution.  e.g. 0.10f
160   void set_process_threshold(float process_threshold);
process_threshold()161   float process_threshold() const { return process_threshold_; }
162 
163   // Handle the format request from the server via Jingle update message.
164   void OnOutputFormatRequest(const VideoFormat& format);
165   // Handle the resolution request from the encoder due to bandwidth changes.
166   void OnEncoderResolutionRequest(int width, int height, AdaptRequest request);
167   // Handle the resolution request for CPU overuse.
168   void OnCpuResolutionRequest(AdaptRequest request);
169   // Handle the CPU load provided by a CPU monitor.
170   void OnCpuLoadUpdated(int current_cpus, int max_cpus,
171                         float process_load, float system_load);
172 
173   sigslot::signal0<> SignalCpuAdaptationUnable;
174 
175  private:
176   // Adapt to the minimum of the formats the server requests, the CPU wants, and
177   // the encoder wants. Returns true if resolution changed.
178   bool AdaptToMinimumFormat(int* new_width, int* new_height);
179   bool IsMinimumFormat(int pixels);
180   void StepPixelCount(CoordinatedVideoAdapter::AdaptRequest request,
181                       int* num_pixels);
182   CoordinatedVideoAdapter::AdaptRequest FindCpuRequest(
183     int current_cpus, int max_cpus,
184     float process_load, float system_load);
185 
186   bool cpu_adaptation_;  // True if cpu adaptation is enabled.
187   bool cpu_smoothing_;  // True if cpu smoothing is enabled (with adaptation).
188   bool gd_adaptation_;  // True if gd adaptation is enabled.
189   bool view_adaptation_;  // True if view adaptation is enabled.
190   bool view_switch_;  // True if view switch is enabled.
191   int cpu_downgrade_count_;
192   int cpu_load_min_samples_;
193   int cpu_load_num_samples_;
194   // cpu system load thresholds relative to max cpus.
195   float high_system_threshold_;
196   float low_system_threshold_;
197   // cpu process load thresholds relative to current cpus.
198   float process_threshold_;
199   // Video formats that the server view requests, the CPU wants, and the encoder
200   // wants respectively. The adapted output format is the minimum of these.
201   int view_desired_num_pixels_;
202   int64 view_desired_interval_;
203   int encoder_desired_num_pixels_;
204   int cpu_desired_num_pixels_;
205   CoordinatedVideoAdapter::AdaptReason adapt_reason_;
206   // The critical section to protect handling requests.
207   talk_base::CriticalSection request_critical_section_;
208 
209   // The weighted average of cpu load over time. It's always updated (if cpu
210   // adaptation is on), but only used if cpu_smoothing_ is set.
211   float system_load_average_;
212 
213   DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter);
214 };
215 
216 }  // namespace cricket
217 
218 #endif  // TALK_MEDIA_BASE_VIDEOADAPTER_H_  // NOLINT
219