1 /* 2 * Copyright (c) 2012 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_MODULES_VIDEO_CODING_MAIN_SOURCE_MEDIA_OPTIMIZATION_H_ 12 #define WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_MEDIA_OPTIMIZATION_H_ 13 14 #include <list> 15 16 #include "webrtc/modules/interface/module_common_types.h" 17 #include "webrtc/modules/video_coding/main/interface/video_coding.h" 18 #include "webrtc/modules/video_coding/main/source/media_opt_util.h" 19 #include "webrtc/modules/video_coding/main/source/qm_select.h" 20 #include "webrtc/system_wrappers/interface/scoped_ptr.h" 21 22 namespace webrtc { 23 24 // Forward declarations. 25 class Clock; 26 class FrameDropper; 27 class VCMContentMetricsProcessing; 28 29 namespace media_optimization { 30 31 // TODO(andresp): Make thread safe. 32 class MediaOptimization { 33 public: 34 explicit MediaOptimization(Clock* clock); 35 ~MediaOptimization(); 36 37 // TODO(andresp): Can Reset and SetEncodingData be done at construction time 38 // only? 39 void Reset(); 40 41 // Informs media optimization of initial encoding state. 42 void SetEncodingData(VideoCodecType send_codec_type, 43 int32_t max_bit_rate, 44 uint32_t frame_rate, 45 uint32_t bit_rate, 46 uint16_t width, 47 uint16_t height, 48 int num_temporal_layers, 49 int32_t mtu); 50 51 // Sets target rates for the encoder given the channel parameters. 52 // Inputs: target bitrate - the encoder target bitrate in bits/s. 53 // fraction_lost - packet loss rate in % in the network. 54 // round_trip_time_ms - round trip time in milliseconds. 55 // min_bit_rate - the bit rate of the end-point with lowest rate. 56 // max_bit_rate - the bit rate of the end-point with highest rate. 57 // TODO(andresp): Find if the callbacks can be triggered only after releasing 58 // an internal critical section. 59 uint32_t SetTargetRates(uint32_t target_bitrate, 60 uint8_t fraction_lost, 61 uint32_t round_trip_time_ms, 62 VCMProtectionCallback* protection_callback, 63 VCMQMSettingsCallback* qmsettings_callback); 64 65 void EnableProtectionMethod(bool enable, VCMProtectionMethodEnum method); 66 void EnableQM(bool enable); 67 void EnableFrameDropper(bool enable); 68 69 // Lets the sender suspend video when the rate drops below 70 // |threshold_bps|, and turns back on when the rate goes back up above 71 // |threshold_bps| + |window_bps|. 72 void SuspendBelowMinBitrate(int threshold_bps, int window_bps); 73 bool IsVideoSuspended() const; 74 75 bool DropFrame(); 76 77 void UpdateContentData(const VideoContentMetrics* content_metrics); 78 79 // Informs Media Optimization of encoding output: Length and frame type. 80 int32_t UpdateWithEncodedData(int encoded_length, 81 uint32_t timestamp, 82 FrameType encoded_frame_type); 83 84 uint32_t InputFrameRate(); 85 uint32_t SentFrameRate(); 86 uint32_t SentBitRate(); 87 VCMFrameCount SentFrameCount(); 88 89 private: 90 enum { 91 kFrameCountHistorySize = 90 92 }; 93 enum { 94 kFrameHistoryWinMs = 2000 95 }; 96 enum { 97 kBitrateAverageWinMs = 1000 98 }; 99 100 struct EncodedFrameSample; 101 typedef std::list<EncodedFrameSample> FrameSampleList; 102 103 void UpdateIncomingFrameRate(); 104 void PurgeOldFrameSamples(int64_t now_ms); 105 void UpdateSentBitrate(int64_t now_ms); 106 void UpdateSentFramerate(); 107 108 // Computes new Quality Mode. 109 int32_t SelectQuality(VCMQMSettingsCallback* qmsettings_callback); 110 111 // Verifies if QM settings differ from default, i.e. if an update is required. 112 // Computes actual values, as will be sent to the encoder. 113 bool QMUpdate(VCMResolutionScale* qm, 114 VCMQMSettingsCallback* qmsettings_callback); 115 116 // Checks if we should make a QM change. Return true if yes, false otherwise. 117 bool CheckStatusForQMchange(); 118 119 void ProcessIncomingFrameRate(int64_t now); 120 121 // Checks conditions for suspending the video. The method compares 122 // |target_bit_rate_| with the threshold values for suspension, and changes 123 // the state of |video_suspended_| accordingly. 124 void CheckSuspendConditions(); 125 126 Clock* clock_; 127 int32_t max_bit_rate_; 128 VideoCodecType send_codec_type_; 129 uint16_t codec_width_; 130 uint16_t codec_height_; 131 float user_frame_rate_; 132 scoped_ptr<FrameDropper> frame_dropper_; 133 scoped_ptr<VCMLossProtectionLogic> loss_prot_logic_; 134 uint8_t fraction_lost_; 135 uint32_t send_statistics_[4]; 136 uint32_t send_statistics_zero_encode_; 137 int32_t max_payload_size_; 138 int target_bit_rate_; 139 float incoming_frame_rate_; 140 int64_t incoming_frame_times_[kFrameCountHistorySize]; 141 bool enable_qm_; 142 std::list<EncodedFrameSample> encoded_frame_samples_; 143 uint32_t avg_sent_bit_rate_bps_; 144 uint32_t avg_sent_framerate_; 145 uint32_t key_frame_cnt_; 146 uint32_t delta_frame_cnt_; 147 scoped_ptr<VCMContentMetricsProcessing> content_; 148 scoped_ptr<VCMQmResolution> qm_resolution_; 149 int64_t last_qm_update_time_; 150 int64_t last_change_time_; // Content/user triggered. 151 int num_layers_; 152 bool suspension_enabled_; 153 bool video_suspended_; 154 int suspension_threshold_bps_; 155 int suspension_window_bps_; 156 }; 157 } // namespace media_optimization 158 } // namespace webrtc 159 160 #endif // WEBRTC_MODULES_VIDEO_CODING_MAIN_SOURCE_MEDIA_OPTIMIZATION_H_ 161