You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
7.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef TALK_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT
  26. #define TALK_MEDIA_BASE_VIDEOADAPTER_H_
  27. #include "talk/base/criticalsection.h"
  28. #include "talk/base/scoped_ptr.h"
  29. #include "talk/base/sigslot.h"
  30. #include "talk/media/base/videocommon.h"
  31. namespace cricket {
  32. class VideoFrame;
  33. // VideoAdapter adapts an input video frame to an output frame based on the
  34. // specified input and output formats. The adaptation includes dropping frames
  35. // to reduce frame rate and scaling frames. VideoAdapter is thread safe.
  36. class VideoAdapter {
  37. public:
  38. VideoAdapter();
  39. virtual ~VideoAdapter();
  40. void SetInputFormat(const VideoFormat& format);
  41. void SetOutputFormat(const VideoFormat& format);
  42. // Constrain output resolution to this many pixels overall
  43. void SetOutputNumPixels(int num_pixels);
  44. int GetOutputNumPixels() const;
  45. const VideoFormat& input_format();
  46. const VideoFormat& output_format();
  47. // If the parameter black is true, the adapted frames will be black.
  48. void SetBlackOutput(bool black);
  49. // Adapt the input frame from the input format to the output format. Return
  50. // true and set the output frame to NULL if the input frame is dropped. Return
  51. // true and set the out frame to output_frame_ if the input frame is adapted
  52. // successfully. Return false otherwise.
  53. // output_frame_ is owned by the VideoAdapter that has the best knowledge on
  54. // the output frame.
  55. bool AdaptFrame(const VideoFrame* in_frame, const VideoFrame** out_frame);
  56. protected:
  57. float FindClosestScale(int width, int height, int target_num_pixels);
  58. float FindLowerScale(int width, int height, int target_num_pixels);
  59. private:
  60. bool StretchToOutputFrame(const VideoFrame* in_frame);
  61. VideoFormat input_format_;
  62. VideoFormat output_format_;
  63. int output_num_pixels_;
  64. bool black_output_; // Flag to tell if we need to black output_frame_.
  65. bool is_black_; // Flag to tell if output_frame_ is currently black.
  66. int64 interval_next_frame_;
  67. talk_base::scoped_ptr<VideoFrame> output_frame_;
  68. // The critical section to protect the above variables.
  69. talk_base::CriticalSection critical_section_;
  70. DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
  71. };
  72. // CoordinatedVideoAdapter adapts the video input to the encoder by coordinating
  73. // the format request from the server, the resolution request from the encoder,
  74. // and the CPU load.
  75. class CoordinatedVideoAdapter
  76. : public VideoAdapter, public sigslot::has_slots<> {
  77. public:
  78. enum AdaptRequest { UPGRADE, KEEP, DOWNGRADE };
  79. CoordinatedVideoAdapter();
  80. virtual ~CoordinatedVideoAdapter() {}
  81. // Enable or disable video adaptation due to the change of the CPU load.
  82. void set_cpu_adaptation(bool enable) { cpu_adaptation_ = enable; }
  83. bool cpu_adaptation() const { return cpu_adaptation_; }
  84. // Enable or disable video adaptation due to the change of the GD
  85. void set_gd_adaptation(bool enable) { gd_adaptation_ = enable; }
  86. bool gd_adaptation() const { return gd_adaptation_; }
  87. // Enable or disable video adaptation due to the change of the View
  88. void set_view_adaptation(bool enable) { view_adaptation_ = enable; }
  89. bool view_adaptation() const { return view_adaptation_; }
  90. // Enable or disable video adaptation to fast switch View
  91. void set_view_switch(bool enable) { view_switch_ = enable; }
  92. bool view_switch() const { return view_switch_; }
  93. // When the video is decreased, set the waiting time for CPU adaptation to
  94. // decrease video again.
  95. void set_cpu_downgrade_wait_time(uint32 ms) { cpu_downgrade_wait_time_ = ms; }
  96. // CPU system load high threshold for reducing resolution. e.g. 0.90f
  97. void set_high_system_threshold(float high_system_threshold) {
  98. high_system_threshold_ = high_system_threshold;
  99. }
  100. float high_system_threshold() const { return high_system_threshold_; }
  101. // CPU system load low threshold for increasing resolution. e.g. 0.70f
  102. void set_low_system_threshold(float low_system_threshold) {
  103. low_system_threshold_ = low_system_threshold;
  104. }
  105. float low_system_threshold() const { return low_system_threshold_; }
  106. // CPU process load threshold for reducing resolution. e.g. 0.40f
  107. void set_process_threshold(float process_threshold) {
  108. process_threshold_ = process_threshold;
  109. }
  110. float process_threshold() const { return process_threshold_; }
  111. // Handle the format request from the server via Jingle update message.
  112. void OnOutputFormatRequest(const VideoFormat& format);
  113. // Handle the resolution request from the encoder due to bandwidth changes.
  114. void OnEncoderResolutionRequest(int width, int height, AdaptRequest request);
  115. // Handle the CPU load provided by a CPU monitor.
  116. void OnCpuLoadUpdated(int current_cpus, int max_cpus,
  117. float process_load, float system_load);
  118. sigslot::signal0<> SignalCpuAdaptationUnable;
  119. private:
  120. // Adapt to the minimum of the formats the server requests, the CPU wants, and
  121. // the encoder wants. Returns true if resolution changed.
  122. bool AdaptToMinimumFormat(int* new_width, int* new_height);
  123. bool IsMinimumFormat(int pixels);
  124. void StepPixelCount(CoordinatedVideoAdapter::AdaptRequest request,
  125. int* num_pixels);
  126. CoordinatedVideoAdapter::AdaptRequest FindCpuRequest(
  127. int current_cpus, int max_cpus,
  128. float process_load, float system_load);
  129. bool cpu_adaptation_; // True if cpu adaptation is enabled.
  130. bool gd_adaptation_; // True if gd adaptation is enabled.
  131. bool view_adaptation_; // True if view adaptation is enabled.
  132. bool view_switch_; // True if view switch is enabled.
  133. int cpu_downgrade_count_;
  134. int cpu_downgrade_wait_time_;
  135. // cpu system load thresholds relative to max cpus.
  136. float high_system_threshold_;
  137. float low_system_threshold_;
  138. // cpu process load thresholds relative to current cpus.
  139. float process_threshold_;
  140. // Video formats that the server view requests, the CPU wants, and the encoder
  141. // wants respectively. The adapted output format is the minimum of these.
  142. int view_desired_num_pixels_;
  143. int64 view_desired_interval_;
  144. int encoder_desired_num_pixels_;
  145. int cpu_desired_num_pixels_;
  146. // The critical section to protect handling requests.
  147. talk_base::CriticalSection request_critical_section_;
  148. DISALLOW_COPY_AND_ASSIGN(CoordinatedVideoAdapter);
  149. };
  150. } // namespace cricket
  151. #endif // TALK_MEDIA_BASE_VIDEOADAPTER_H_ // NOLINT