henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame^] | 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/logging.h" |
| 32 | #include "talk/base/scoped_ptr.h" |
| 33 | #include "talk/base/sigslot.h" |
| 34 | #include "talk/media/base/videocommon.h" |
| 35 | |
| 36 | namespace cricket { |
| 37 | |
| 38 | class VideoFrame; |
| 39 | |
| 40 | // VideoAdapter adapts an input video frame to an output frame based on the |
| 41 | // specified input and output formats. The adaptation includes dropping frames |
| 42 | // to reduce frame rate and scaling frames. VideoAdapter is thread safe. |
| 43 | class VideoAdapter { |
| 44 | public: |
| 45 | VideoAdapter(); |
| 46 | virtual ~VideoAdapter(); |
| 47 | |
| 48 | void SetInputFormat(const VideoFrame& in_frame); |
| 49 | void SetInputFormat(const VideoFormat& format); |
| 50 | void SetOutputFormat(const VideoFormat& format); |
| 51 | // Constrain output resolution to this many pixels overall |
| 52 | void SetOutputNumPixels(int num_pixels); |
| 53 | int GetOutputNumPixels() const; |
| 54 | |
| 55 | const VideoFormat& input_format(); |
| 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 | // output_frame_ is owned by the VideoAdapter that has the best knowledge on |
| 65 | // the output frame. |
| 66 | bool AdaptFrame(const VideoFrame* in_frame, const VideoFrame** out_frame); |
| 67 | |
| 68 | protected: |
| 69 | float FindClosestScale(int width, int height, int target_num_pixels); |
| 70 | float FindLowerScale(int width, int height, int target_num_pixels); |
| 71 | |
| 72 | private: |
| 73 | bool StretchToOutputFrame(const VideoFrame* in_frame); |
| 74 | |
| 75 | VideoFormat input_format_; |
| 76 | VideoFormat output_format_; |
| 77 | int output_num_pixels_; |
| 78 | bool black_output_; // Flag to tell if we need to black output_frame_. |
| 79 | bool is_black_; // Flag to tell if output_frame_ is currently black. |
| 80 | int64 interval_next_frame_; |
| 81 | talk_base::scoped_ptr<VideoFrame> output_frame_; |
| 82 | // The critical section to protect the above variables. |
| 83 | talk_base::CriticalSection critical_section_; |
| 84 | |
| 85 | DISALLOW_COPY_AND_ASSIGN(VideoAdapter); |
| 86 | }; |
| 87 | |
| 88 | // CoordinatedVideoAdapter adapts the video input to the encoder by coordinating |
| 89 | // the format request from the server, the resolution request from the encoder, |
| 90 | // and the CPU load. |
| 91 | class CoordinatedVideoAdapter |
| 92 | : public VideoAdapter, public sigslot::has_slots<> { |
| 93 | public: |
| 94 | enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE }; |
| 95 | enum { |
| 96 | ADAPTREASON_CPU = 1, |
| 97 | ADAPTREASON_BANDWIDTH = 2, |
| 98 | ADAPTREASON_VIEW = 4 |
| 99 | }; |
| 100 | typedef int AdaptReason; |
| 101 | |
| 102 | CoordinatedVideoAdapter(); |
| 103 | virtual ~CoordinatedVideoAdapter() {} |
| 104 | |
| 105 | // Enable or disable video adaptation due to the change of the CPU load. |
| 106 | void set_cpu_adaptation(bool enable) { cpu_adaptation_ = enable; } |
| 107 | bool cpu_adaptation() const { return cpu_adaptation_; } |
| 108 | // Enable or disable video adaptation due to the change of the GD |
| 109 | void set_gd_adaptation(bool enable) { gd_adaptation_ = enable; } |
| 110 | bool gd_adaptation() const { return gd_adaptation_; } |
| 111 | // Enable or disable video adaptation due to the change of the View |
| 112 | void set_view_adaptation(bool enable) { view_adaptation_ = enable; } |
| 113 | bool view_adaptation() const { return view_adaptation_; } |
| 114 | // Enable or disable video adaptation to fast switch View |
| 115 | void set_view_switch(bool enable) { view_switch_ = enable; } |
| 116 | bool view_switch() const { return view_switch_; } |
| 117 | |
| 118 | CoordinatedVideoAdapter::AdaptReason adapt_reason() const { |
| 119 | return adapt_reason_; |
| 120 | } |
| 121 | |
| 122 | // When the video is decreased, set the waiting time for CPU adaptation to |
| 123 | // decrease video again. |
| 124 | void set_cpu_downgrade_wait_time(uint32 cpu_downgrade_wait_time) { |
| 125 | if (cpu_downgrade_wait_time_ != static_cast<int>(cpu_downgrade_wait_time)) { |
| 126 | LOG(LS_INFO) << "VAdapt Change Cpu Downgrade Wait Time from: " |
| 127 | << cpu_downgrade_wait_time_ << " to " |
| 128 | << cpu_downgrade_wait_time; |
| 129 | cpu_downgrade_wait_time_ = static_cast<int>(cpu_downgrade_wait_time); |
| 130 | } |
| 131 | } |
| 132 | // CPU system load high threshold for reducing resolution. e.g. 0.85f |
| 133 | void set_high_system_threshold(float high_system_threshold) { |
| 134 | ASSERT(high_system_threshold <= 1.0f); |
| 135 | ASSERT(high_system_threshold >= 0.0f); |
| 136 | if (high_system_threshold_ != high_system_threshold) { |
| 137 | LOG(LS_INFO) << "VAdapt Change High System Threshold from: " |
| 138 | << high_system_threshold_ << " to " << high_system_threshold; |
| 139 | high_system_threshold_ = high_system_threshold; |
| 140 | } |
| 141 | } |
| 142 | float high_system_threshold() const { return high_system_threshold_; } |
| 143 | // CPU system load low threshold for increasing resolution. e.g. 0.70f |
| 144 | void set_low_system_threshold(float low_system_threshold) { |
| 145 | ASSERT(low_system_threshold <= 1.0f); |
| 146 | ASSERT(low_system_threshold >= 0.0f); |
| 147 | if (low_system_threshold_ != low_system_threshold) { |
| 148 | LOG(LS_INFO) << "VAdapt Change Low System Threshold from: " |
| 149 | << low_system_threshold_ << " to " << low_system_threshold; |
| 150 | low_system_threshold_ = low_system_threshold; |
| 151 | } |
| 152 | } |
| 153 | float low_system_threshold() const { return low_system_threshold_; } |
| 154 | // CPU process load threshold for reducing resolution. e.g. 0.10f |
| 155 | void set_process_threshold(float process_threshold) { |
| 156 | ASSERT(process_threshold <= 1.0f); |
| 157 | ASSERT(process_threshold >= 0.0f); |
| 158 | if (process_threshold_ != process_threshold) { |
| 159 | LOG(LS_INFO) << "VAdapt Change High Process Threshold from: " |
| 160 | << process_threshold_ << " to " << process_threshold; |
| 161 | process_threshold_ = process_threshold; |
| 162 | } |
| 163 | } |
| 164 | float process_threshold() const { return process_threshold_; } |
| 165 | |
| 166 | // Handle the format request from the server via Jingle update message. |
| 167 | void OnOutputFormatRequest(const VideoFormat& format); |
| 168 | // Handle the resolution request from the encoder due to bandwidth changes. |
| 169 | void OnEncoderResolutionRequest(int width, int height, AdaptRequest request); |
| 170 | // Handle the CPU load provided by a CPU monitor. |
| 171 | void OnCpuLoadUpdated(int current_cpus, int max_cpus, |
| 172 | float process_load, float system_load); |
| 173 | |
| 174 | sigslot::signal0<> SignalCpuAdaptationUnable; |
| 175 | |
| 176 | private: |
| 177 | // Adapt to the minimum of the formats the server requests, the CPU wants, and |
| 178 | // the encoder wants. Returns true if resolution changed. |
| 179 | bool AdaptToMinimumFormat(int* new_width, int* new_height); |
| 180 | bool IsMinimumFormat(int pixels); |
| 181 | void StepPixelCount(CoordinatedVideoAdapter::AdaptRequest request, |
| 182 | int* num_pixels); |
| 183 | CoordinatedVideoAdapter::AdaptRequest FindCpuRequest( |
| 184 | int current_cpus, int max_cpus, |
| 185 | float process_load, float system_load); |
| 186 | |
| 187 | bool cpu_adaptation_; // True if cpu adaptation is enabled. |
| 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_downgrade_wait_time_; |
| 193 | // cpu system load thresholds relative to max cpus. |
| 194 | float high_system_threshold_; |
| 195 | float low_system_threshold_; |
| 196 | // cpu process load thresholds relative to current cpus. |
| 197 | float process_threshold_; |
| 198 | // Video formats that the server view requests, the CPU wants, and the encoder |
| 199 | // wants respectively. The adapted output format is the minimum of these. |
| 200 | int view_desired_num_pixels_; |
| 201 | int64 view_desired_interval_; |
| 202 | int encoder_desired_num_pixels_; |
| 203 | int cpu_desired_num_pixels_; |
| 204 | CoordinatedVideoAdapter::AdaptReason adapt_reason_; |
| 205 | // The critical section to protect handling requests. |
| 206 | talk_base::CriticalSection request_critical_section_; |
| 207 | |
| 208 | DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter); |
| 209 | }; |
| 210 | |
| 211 | } // namespace cricket |
| 212 | |
| 213 | #endif // TALK_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT |