| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/video/vie_encoder.h" | 11 #include "webrtc/video/video_stream_encoder.h" |
| 12 | 12 |
| 13 #include <algorithm> | 13 #include <algorithm> |
| 14 #include <limits> | 14 #include <limits> |
| 15 #include <numeric> | 15 #include <numeric> |
| 16 #include <utility> | 16 #include <utility> |
| 17 | 17 |
| 18 #include "webrtc/api/video/i420_buffer.h" | 18 #include "webrtc/api/video/i420_buffer.h" |
| 19 #include "webrtc/common_video/include/video_bitrate_allocator.h" | 19 #include "webrtc/common_video/include/video_bitrate_allocator.h" |
| 20 #include "webrtc/common_video/include/video_frame.h" | 20 #include "webrtc/common_video/include/video_frame.h" |
| 21 #include "webrtc/modules/pacing/paced_sender.h" | 21 #include "webrtc/modules/pacing/paced_sender.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 bool IsFramerateScalingEnabled( | 96 bool IsFramerateScalingEnabled( |
| 97 VideoSendStream::DegradationPreference degradation_preference) { | 97 VideoSendStream::DegradationPreference degradation_preference) { |
| 98 return degradation_preference == | 98 return degradation_preference == |
| 99 VideoSendStream::DegradationPreference::kMaintainResolution || | 99 VideoSendStream::DegradationPreference::kMaintainResolution || |
| 100 degradation_preference == | 100 degradation_preference == |
| 101 VideoSendStream::DegradationPreference::kBalanced; | 101 VideoSendStream::DegradationPreference::kBalanced; |
| 102 } | 102 } |
| 103 | 103 |
| 104 } // namespace | 104 } // namespace |
| 105 | 105 |
| 106 class ViEEncoder::ConfigureEncoderTask : public rtc::QueuedTask { | 106 class VideoStreamEncoder::ConfigureEncoderTask : public rtc::QueuedTask { |
| 107 public: | 107 public: |
| 108 ConfigureEncoderTask(ViEEncoder* vie_encoder, | 108 ConfigureEncoderTask(VideoStreamEncoder* video_stream_encoder, |
| 109 VideoEncoderConfig config, | 109 VideoEncoderConfig config, |
| 110 size_t max_data_payload_length, | 110 size_t max_data_payload_length, |
| 111 bool nack_enabled) | 111 bool nack_enabled) |
| 112 : vie_encoder_(vie_encoder), | 112 : video_stream_encoder_(video_stream_encoder), |
| 113 config_(std::move(config)), | 113 config_(std::move(config)), |
| 114 max_data_payload_length_(max_data_payload_length), | 114 max_data_payload_length_(max_data_payload_length), |
| 115 nack_enabled_(nack_enabled) {} | 115 nack_enabled_(nack_enabled) {} |
| 116 | 116 |
| 117 private: | 117 private: |
| 118 bool Run() override { | 118 bool Run() override { |
| 119 vie_encoder_->ConfigureEncoderOnTaskQueue( | 119 video_stream_encoder_->ConfigureEncoderOnTaskQueue( |
| 120 std::move(config_), max_data_payload_length_, nack_enabled_); | 120 std::move(config_), max_data_payload_length_, nack_enabled_); |
| 121 return true; | 121 return true; |
| 122 } | 122 } |
| 123 | 123 |
| 124 ViEEncoder* const vie_encoder_; | 124 VideoStreamEncoder* const video_stream_encoder_; |
| 125 VideoEncoderConfig config_; | 125 VideoEncoderConfig config_; |
| 126 size_t max_data_payload_length_; | 126 size_t max_data_payload_length_; |
| 127 bool nack_enabled_; | 127 bool nack_enabled_; |
| 128 }; | 128 }; |
| 129 | 129 |
| 130 class ViEEncoder::EncodeTask : public rtc::QueuedTask { | 130 class VideoStreamEncoder::EncodeTask : public rtc::QueuedTask { |
| 131 public: | 131 public: |
| 132 EncodeTask(const VideoFrame& frame, | 132 EncodeTask(const VideoFrame& frame, |
| 133 ViEEncoder* vie_encoder, | 133 VideoStreamEncoder* video_stream_encoder, |
| 134 int64_t time_when_posted_us, | 134 int64_t time_when_posted_us, |
| 135 bool log_stats) | 135 bool log_stats) |
| 136 : frame_(frame), | 136 : frame_(frame), |
| 137 vie_encoder_(vie_encoder), | 137 video_stream_encoder_(video_stream_encoder), |
| 138 time_when_posted_us_(time_when_posted_us), | 138 time_when_posted_us_(time_when_posted_us), |
| 139 log_stats_(log_stats) { | 139 log_stats_(log_stats) { |
| 140 ++vie_encoder_->posted_frames_waiting_for_encode_; | 140 ++video_stream_encoder_->posted_frames_waiting_for_encode_; |
| 141 } | 141 } |
| 142 | 142 |
| 143 private: | 143 private: |
| 144 bool Run() override { | 144 bool Run() override { |
| 145 RTC_DCHECK_RUN_ON(&vie_encoder_->encoder_queue_); | 145 RTC_DCHECK_RUN_ON(&video_stream_encoder_->encoder_queue_); |
| 146 RTC_DCHECK_GT(vie_encoder_->posted_frames_waiting_for_encode_.Value(), 0); | 146 RTC_DCHECK_GT( |
| 147 vie_encoder_->stats_proxy_->OnIncomingFrame(frame_.width(), | 147 video_stream_encoder_->posted_frames_waiting_for_encode_.Value(), 0); |
| 148 frame_.height()); | 148 video_stream_encoder_->stats_proxy_->OnIncomingFrame(frame_.width(), |
| 149 ++vie_encoder_->captured_frame_count_; | 149 frame_.height()); |
| 150 if (--vie_encoder_->posted_frames_waiting_for_encode_ == 0) { | 150 ++video_stream_encoder_->captured_frame_count_; |
| 151 vie_encoder_->EncodeVideoFrame(frame_, time_when_posted_us_); | 151 if (--video_stream_encoder_->posted_frames_waiting_for_encode_ == 0) { |
| 152 video_stream_encoder_->EncodeVideoFrame(frame_, time_when_posted_us_); |
| 152 } else { | 153 } else { |
| 153 // There is a newer frame in flight. Do not encode this frame. | 154 // There is a newer frame in flight. Do not encode this frame. |
| 154 LOG(LS_VERBOSE) | 155 LOG(LS_VERBOSE) |
| 155 << "Incoming frame dropped due to that the encoder is blocked."; | 156 << "Incoming frame dropped due to that the encoder is blocked."; |
| 156 ++vie_encoder_->dropped_frame_count_; | 157 ++video_stream_encoder_->dropped_frame_count_; |
| 157 } | 158 } |
| 158 if (log_stats_) { | 159 if (log_stats_) { |
| 159 LOG(LS_INFO) << "Number of frames: captured " | 160 LOG(LS_INFO) << "Number of frames: captured " |
| 160 << vie_encoder_->captured_frame_count_ | 161 << video_stream_encoder_->captured_frame_count_ |
| 161 << ", dropped (due to encoder blocked) " | 162 << ", dropped (due to encoder blocked) " |
| 162 << vie_encoder_->dropped_frame_count_ << ", interval_ms " | 163 << video_stream_encoder_->dropped_frame_count_ |
| 164 << ", interval_ms " |
| 163 << kFrameLogIntervalMs; | 165 << kFrameLogIntervalMs; |
| 164 vie_encoder_->captured_frame_count_ = 0; | 166 video_stream_encoder_->captured_frame_count_ = 0; |
| 165 vie_encoder_->dropped_frame_count_ = 0; | 167 video_stream_encoder_->dropped_frame_count_ = 0; |
| 166 } | 168 } |
| 167 return true; | 169 return true; |
| 168 } | 170 } |
| 169 VideoFrame frame_; | 171 VideoFrame frame_; |
| 170 ViEEncoder* const vie_encoder_; | 172 VideoStreamEncoder* const video_stream_encoder_; |
| 171 const int64_t time_when_posted_us_; | 173 const int64_t time_when_posted_us_; |
| 172 const bool log_stats_; | 174 const bool log_stats_; |
| 173 }; | 175 }; |
| 174 | 176 |
| 175 // VideoSourceProxy is responsible ensuring thread safety between calls to | 177 // VideoSourceProxy is responsible ensuring thread safety between calls to |
| 176 // ViEEncoder::SetSource that will happen on libjingle's worker thread when a | 178 // VideoStreamEncoder::SetSource that will happen on libjingle's worker thread |
| 177 // video capturer is connected to the encoder and the encoder task queue | 179 // when a video capturer is connected to the encoder and the encoder task queue |
| 178 // (encoder_queue_) where the encoder reports its VideoSinkWants. | 180 // (encoder_queue_) where the encoder reports its VideoSinkWants. |
| 179 class ViEEncoder::VideoSourceProxy { | 181 class VideoStreamEncoder::VideoSourceProxy { |
| 180 public: | 182 public: |
| 181 explicit VideoSourceProxy(ViEEncoder* vie_encoder) | 183 explicit VideoSourceProxy(VideoStreamEncoder* video_stream_encoder) |
| 182 : vie_encoder_(vie_encoder), | 184 : video_stream_encoder_(video_stream_encoder), |
| 183 degradation_preference_( | 185 degradation_preference_( |
| 184 VideoSendStream::DegradationPreference::kDegradationDisabled), | 186 VideoSendStream::DegradationPreference::kDegradationDisabled), |
| 185 source_(nullptr) {} | 187 source_(nullptr) {} |
| 186 | 188 |
| 187 void SetSource( | 189 void SetSource( |
| 188 rtc::VideoSourceInterface<VideoFrame>* source, | 190 rtc::VideoSourceInterface<VideoFrame>* source, |
| 189 const VideoSendStream::DegradationPreference& degradation_preference) { | 191 const VideoSendStream::DegradationPreference& degradation_preference) { |
| 190 // Called on libjingle's worker thread. | 192 // Called on libjingle's worker thread. |
| 191 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); | 193 RTC_DCHECK_CALLED_SEQUENTIALLY(&main_checker_); |
| 192 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; | 194 rtc::VideoSourceInterface<VideoFrame>* old_source = nullptr; |
| 193 rtc::VideoSinkWants wants; | 195 rtc::VideoSinkWants wants; |
| 194 { | 196 { |
| 195 rtc::CritScope lock(&crit_); | 197 rtc::CritScope lock(&crit_); |
| 196 degradation_preference_ = degradation_preference; | 198 degradation_preference_ = degradation_preference; |
| 197 old_source = source_; | 199 old_source = source_; |
| 198 source_ = source; | 200 source_ = source; |
| 199 wants = GetActiveSinkWantsInternal(); | 201 wants = GetActiveSinkWantsInternal(); |
| 200 } | 202 } |
| 201 | 203 |
| 202 if (old_source != source && old_source != nullptr) { | 204 if (old_source != source && old_source != nullptr) { |
| 203 old_source->RemoveSink(vie_encoder_); | 205 old_source->RemoveSink(video_stream_encoder_); |
| 204 } | 206 } |
| 205 | 207 |
| 206 if (!source) { | 208 if (!source) { |
| 207 return; | 209 return; |
| 208 } | 210 } |
| 209 | 211 |
| 210 source->AddOrUpdateSink(vie_encoder_, wants); | 212 source->AddOrUpdateSink(video_stream_encoder_, wants); |
| 211 } | 213 } |
| 212 | 214 |
| 213 void SetWantsRotationApplied(bool rotation_applied) { | 215 void SetWantsRotationApplied(bool rotation_applied) { |
| 214 rtc::CritScope lock(&crit_); | 216 rtc::CritScope lock(&crit_); |
| 215 sink_wants_.rotation_applied = rotation_applied; | 217 sink_wants_.rotation_applied = rotation_applied; |
| 216 if (source_) | 218 if (source_) |
| 217 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 219 source_->AddOrUpdateSink(video_stream_encoder_, sink_wants_); |
| 218 } | 220 } |
| 219 | 221 |
| 220 rtc::VideoSinkWants GetActiveSinkWants() { | 222 rtc::VideoSinkWants GetActiveSinkWants() { |
| 221 rtc::CritScope lock(&crit_); | 223 rtc::CritScope lock(&crit_); |
| 222 return GetActiveSinkWantsInternal(); | 224 return GetActiveSinkWantsInternal(); |
| 223 } | 225 } |
| 224 | 226 |
| 225 void ResetPixelFpsCount() { | 227 void ResetPixelFpsCount() { |
| 226 rtc::CritScope lock(&crit_); | 228 rtc::CritScope lock(&crit_); |
| 227 sink_wants_.max_pixel_count = std::numeric_limits<int>::max(); | 229 sink_wants_.max_pixel_count = std::numeric_limits<int>::max(); |
| 228 sink_wants_.target_pixel_count.reset(); | 230 sink_wants_.target_pixel_count.reset(); |
| 229 sink_wants_.max_framerate_fps = std::numeric_limits<int>::max(); | 231 sink_wants_.max_framerate_fps = std::numeric_limits<int>::max(); |
| 230 if (source_) | 232 if (source_) |
| 231 source_->AddOrUpdateSink(vie_encoder_, sink_wants_); | 233 source_->AddOrUpdateSink(video_stream_encoder_, sink_wants_); |
| 232 } | 234 } |
| 233 | 235 |
| 234 bool RequestResolutionLowerThan(int pixel_count) { | 236 bool RequestResolutionLowerThan(int pixel_count) { |
| 235 // Called on the encoder task queue. | 237 // Called on the encoder task queue. |
| 236 rtc::CritScope lock(&crit_); | 238 rtc::CritScope lock(&crit_); |
| 237 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { | 239 if (!source_ || !IsResolutionScalingEnabled(degradation_preference_)) { |
| 238 // This can happen since |degradation_preference_| is set on libjingle's | 240 // This can happen since |degradation_preference_| is set on libjingle's |
| 239 // worker thread but the adaptation is done on the encoder task queue. | 241 // worker thread but the adaptation is done on the encoder task queue. |
| 240 return false; | 242 return false; |
| 241 } | 243 } |
| 242 // The input video frame size will have a resolution less than or equal to | 244 // The input video frame size will have a resolution less than or equal to |
| 243 // |max_pixel_count| depending on how the source can scale the frame size. | 245 // |max_pixel_count| depending on how the source can scale the frame size. |
| 244 const int pixels_wanted = (pixel_count * 3) / 5; | 246 const int pixels_wanted = (pixel_count * 3) / 5; |
| 245 if (pixels_wanted < kMinPixelsPerFrame || | 247 if (pixels_wanted < kMinPixelsPerFrame || |
| 246 pixels_wanted >= sink_wants_.max_pixel_count) { | 248 pixels_wanted >= sink_wants_.max_pixel_count) { |
| 247 return false; | 249 return false; |
| 248 } | 250 } |
| 249 LOG(LS_INFO) << "Scaling down resolution, max pixels: " << pixels_wanted; | 251 LOG(LS_INFO) << "Scaling down resolution, max pixels: " << pixels_wanted; |
| 250 sink_wants_.max_pixel_count = pixels_wanted; | 252 sink_wants_.max_pixel_count = pixels_wanted; |
| 251 sink_wants_.target_pixel_count = rtc::Optional<int>(); | 253 sink_wants_.target_pixel_count = rtc::Optional<int>(); |
| 252 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWantsInternal()); | 254 source_->AddOrUpdateSink(video_stream_encoder_, |
| 255 GetActiveSinkWantsInternal()); |
| 253 return true; | 256 return true; |
| 254 } | 257 } |
| 255 | 258 |
| 256 int RequestFramerateLowerThan(int fps) { | 259 int RequestFramerateLowerThan(int fps) { |
| 257 // Called on the encoder task queue. | 260 // Called on the encoder task queue. |
| 258 // The input video frame rate will be scaled down to 2/3, rounding down. | 261 // The input video frame rate will be scaled down to 2/3, rounding down. |
| 259 int framerate_wanted = (fps * 2) / 3; | 262 int framerate_wanted = (fps * 2) / 3; |
| 260 return RestrictFramerate(framerate_wanted) ? framerate_wanted : -1; | 263 return RestrictFramerate(framerate_wanted) ? framerate_wanted : -1; |
| 261 } | 264 } |
| 262 | 265 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 283 // On step down we request at most 3/5 the pixel count of the previous | 286 // On step down we request at most 3/5 the pixel count of the previous |
| 284 // resolution, so in order to take "one step up" we request a resolution | 287 // resolution, so in order to take "one step up" we request a resolution |
| 285 // as close as possible to 5/3 of the current resolution. The actual pixel | 288 // as close as possible to 5/3 of the current resolution. The actual pixel |
| 286 // count selected depends on the capabilities of the source. In order to | 289 // count selected depends on the capabilities of the source. In order to |
| 287 // not take a too large step up, we cap the requested pixel count to be at | 290 // not take a too large step up, we cap the requested pixel count to be at |
| 288 // most four time the current number of pixels. | 291 // most four time the current number of pixels. |
| 289 sink_wants_.target_pixel_count = | 292 sink_wants_.target_pixel_count = |
| 290 rtc::Optional<int>((pixel_count * 5) / 3); | 293 rtc::Optional<int>((pixel_count * 5) / 3); |
| 291 } | 294 } |
| 292 LOG(LS_INFO) << "Scaling up resolution, max pixels: " << max_pixels_wanted; | 295 LOG(LS_INFO) << "Scaling up resolution, max pixels: " << max_pixels_wanted; |
| 293 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWantsInternal()); | 296 source_->AddOrUpdateSink(video_stream_encoder_, |
| 297 GetActiveSinkWantsInternal()); |
| 294 return true; | 298 return true; |
| 295 } | 299 } |
| 296 | 300 |
| 297 // Request upgrade in framerate. Returns the new requested frame, or -1 if | 301 // Request upgrade in framerate. Returns the new requested frame, or -1 if |
| 298 // no change requested. Note that maxint may be returned if limits due to | 302 // no change requested. Note that maxint may be returned if limits due to |
| 299 // adaptation requests are removed completely. In that case, consider | 303 // adaptation requests are removed completely. In that case, consider |
| 300 // |max_framerate_| to be the current limit (assuming the capturer complies). | 304 // |max_framerate_| to be the current limit (assuming the capturer complies). |
| 301 int RequestHigherFramerateThan(int fps) { | 305 int RequestHigherFramerateThan(int fps) { |
| 302 // Called on the encoder task queue. | 306 // Called on the encoder task queue. |
| 303 // The input frame rate will be scaled up to the last step, with rounding. | 307 // The input frame rate will be scaled up to the last step, with rounding. |
| 304 int framerate_wanted = fps; | 308 int framerate_wanted = fps; |
| 305 if (fps != std::numeric_limits<int>::max()) | 309 if (fps != std::numeric_limits<int>::max()) |
| 306 framerate_wanted = (fps * 3) / 2; | 310 framerate_wanted = (fps * 3) / 2; |
| 307 | 311 |
| 308 return IncreaseFramerate(framerate_wanted) ? framerate_wanted : -1; | 312 return IncreaseFramerate(framerate_wanted) ? framerate_wanted : -1; |
| 309 } | 313 } |
| 310 | 314 |
| 311 bool RestrictFramerate(int fps) { | 315 bool RestrictFramerate(int fps) { |
| 312 // Called on the encoder task queue. | 316 // Called on the encoder task queue. |
| 313 rtc::CritScope lock(&crit_); | 317 rtc::CritScope lock(&crit_); |
| 314 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) | 318 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) |
| 315 return false; | 319 return false; |
| 316 | 320 |
| 317 const int fps_wanted = std::max(kMinFramerateFps, fps); | 321 const int fps_wanted = std::max(kMinFramerateFps, fps); |
| 318 if (fps_wanted >= sink_wants_.max_framerate_fps) | 322 if (fps_wanted >= sink_wants_.max_framerate_fps) |
| 319 return false; | 323 return false; |
| 320 | 324 |
| 321 LOG(LS_INFO) << "Scaling down framerate: " << fps_wanted; | 325 LOG(LS_INFO) << "Scaling down framerate: " << fps_wanted; |
| 322 sink_wants_.max_framerate_fps = fps_wanted; | 326 sink_wants_.max_framerate_fps = fps_wanted; |
| 323 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWantsInternal()); | 327 source_->AddOrUpdateSink(video_stream_encoder_, |
| 328 GetActiveSinkWantsInternal()); |
| 324 return true; | 329 return true; |
| 325 } | 330 } |
| 326 | 331 |
| 327 bool IncreaseFramerate(int fps) { | 332 bool IncreaseFramerate(int fps) { |
| 328 // Called on the encoder task queue. | 333 // Called on the encoder task queue. |
| 329 rtc::CritScope lock(&crit_); | 334 rtc::CritScope lock(&crit_); |
| 330 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) | 335 if (!source_ || !IsFramerateScalingEnabled(degradation_preference_)) |
| 331 return false; | 336 return false; |
| 332 | 337 |
| 333 const int fps_wanted = std::max(kMinFramerateFps, fps); | 338 const int fps_wanted = std::max(kMinFramerateFps, fps); |
| 334 if (fps_wanted <= sink_wants_.max_framerate_fps) | 339 if (fps_wanted <= sink_wants_.max_framerate_fps) |
| 335 return false; | 340 return false; |
| 336 | 341 |
| 337 LOG(LS_INFO) << "Scaling up framerate: " << fps_wanted; | 342 LOG(LS_INFO) << "Scaling up framerate: " << fps_wanted; |
| 338 sink_wants_.max_framerate_fps = fps_wanted; | 343 sink_wants_.max_framerate_fps = fps_wanted; |
| 339 source_->AddOrUpdateSink(vie_encoder_, GetActiveSinkWantsInternal()); | 344 source_->AddOrUpdateSink(video_stream_encoder_, |
| 345 GetActiveSinkWantsInternal()); |
| 340 return true; | 346 return true; |
| 341 } | 347 } |
| 342 | 348 |
| 343 private: | 349 private: |
| 344 rtc::VideoSinkWants GetActiveSinkWantsInternal() | 350 rtc::VideoSinkWants GetActiveSinkWantsInternal() |
| 345 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { | 351 EXCLUSIVE_LOCKS_REQUIRED(&crit_) { |
| 346 rtc::VideoSinkWants wants = sink_wants_; | 352 rtc::VideoSinkWants wants = sink_wants_; |
| 347 // Clear any constraints from the current sink wants that don't apply to | 353 // Clear any constraints from the current sink wants that don't apply to |
| 348 // the used degradation_preference. | 354 // the used degradation_preference. |
| 349 switch (degradation_preference_) { | 355 switch (degradation_preference_) { |
| 350 case VideoSendStream::DegradationPreference::kBalanced: | 356 case VideoSendStream::DegradationPreference::kBalanced: |
| 351 break; | 357 break; |
| 352 case VideoSendStream::DegradationPreference::kMaintainFramerate: | 358 case VideoSendStream::DegradationPreference::kMaintainFramerate: |
| 353 wants.max_framerate_fps = std::numeric_limits<int>::max(); | 359 wants.max_framerate_fps = std::numeric_limits<int>::max(); |
| 354 break; | 360 break; |
| 355 case VideoSendStream::DegradationPreference::kMaintainResolution: | 361 case VideoSendStream::DegradationPreference::kMaintainResolution: |
| 356 wants.max_pixel_count = std::numeric_limits<int>::max(); | 362 wants.max_pixel_count = std::numeric_limits<int>::max(); |
| 357 wants.target_pixel_count.reset(); | 363 wants.target_pixel_count.reset(); |
| 358 break; | 364 break; |
| 359 case VideoSendStream::DegradationPreference::kDegradationDisabled: | 365 case VideoSendStream::DegradationPreference::kDegradationDisabled: |
| 360 wants.max_pixel_count = std::numeric_limits<int>::max(); | 366 wants.max_pixel_count = std::numeric_limits<int>::max(); |
| 361 wants.target_pixel_count.reset(); | 367 wants.target_pixel_count.reset(); |
| 362 wants.max_framerate_fps = std::numeric_limits<int>::max(); | 368 wants.max_framerate_fps = std::numeric_limits<int>::max(); |
| 363 } | 369 } |
| 364 return wants; | 370 return wants; |
| 365 } | 371 } |
| 366 | 372 |
| 367 rtc::CriticalSection crit_; | 373 rtc::CriticalSection crit_; |
| 368 rtc::SequencedTaskChecker main_checker_; | 374 rtc::SequencedTaskChecker main_checker_; |
| 369 ViEEncoder* const vie_encoder_; | 375 VideoStreamEncoder* const video_stream_encoder_; |
| 370 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); | 376 rtc::VideoSinkWants sink_wants_ GUARDED_BY(&crit_); |
| 371 VideoSendStream::DegradationPreference degradation_preference_ | 377 VideoSendStream::DegradationPreference degradation_preference_ |
| 372 GUARDED_BY(&crit_); | 378 GUARDED_BY(&crit_); |
| 373 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); | 379 rtc::VideoSourceInterface<VideoFrame>* source_ GUARDED_BY(&crit_); |
| 374 | 380 |
| 375 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); | 381 RTC_DISALLOW_COPY_AND_ASSIGN(VideoSourceProxy); |
| 376 }; | 382 }; |
| 377 | 383 |
| 378 ViEEncoder::ViEEncoder(uint32_t number_of_cores, | 384 VideoStreamEncoder::VideoStreamEncoder(uint32_t number_of_cores, |
| 379 SendStatisticsProxy* stats_proxy, | 385 SendStatisticsProxy* stats_proxy, |
| 380 const VideoSendStream::Config::EncoderSettings& settings, | 386 const VideoSendStream::Config::EncoderSettings& settings, |
| 381 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 387 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
| 382 EncodedFrameObserver* encoder_timing, | 388 EncodedFrameObserver* encoder_timing, |
| 383 std::unique_ptr<OveruseFrameDetector> overuse_detector) | 389 std::unique_ptr<OveruseFrameDetector> overuse_detector) |
| 384 : shutdown_event_(true /* manual_reset */, false), | 390 : shutdown_event_(true /* manual_reset */, false), |
| 385 number_of_cores_(number_of_cores), | 391 number_of_cores_(number_of_cores), |
| 386 initial_rampup_(0), | 392 initial_rampup_(0), |
| 387 source_proxy_(new VideoSourceProxy(this)), | 393 source_proxy_(new VideoSourceProxy(this)), |
| 388 sink_(nullptr), | 394 sink_(nullptr), |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 encoder_queue_("EncoderQueue") { | 427 encoder_queue_("EncoderQueue") { |
| 422 RTC_DCHECK(stats_proxy); | 428 RTC_DCHECK(stats_proxy); |
| 423 encoder_queue_.PostTask([this] { | 429 encoder_queue_.PostTask([this] { |
| 424 RTC_DCHECK_RUN_ON(&encoder_queue_); | 430 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 425 overuse_detector_->StartCheckForOveruse(); | 431 overuse_detector_->StartCheckForOveruse(); |
| 426 video_sender_.RegisterExternalEncoder( | 432 video_sender_.RegisterExternalEncoder( |
| 427 settings_.encoder, settings_.payload_type, settings_.internal_source); | 433 settings_.encoder, settings_.payload_type, settings_.internal_source); |
| 428 }); | 434 }); |
| 429 } | 435 } |
| 430 | 436 |
| 431 ViEEncoder::~ViEEncoder() { | 437 VideoStreamEncoder::~VideoStreamEncoder() { |
| 432 RTC_DCHECK_RUN_ON(&thread_checker_); | 438 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 433 RTC_DCHECK(shutdown_event_.Wait(0)) | 439 RTC_DCHECK(shutdown_event_.Wait(0)) |
| 434 << "Must call ::Stop() before destruction."; | 440 << "Must call ::Stop() before destruction."; |
| 435 } | 441 } |
| 436 | 442 |
| 437 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle | 443 // TODO(pbos): Lower these thresholds (to closer to 100%) when we handle |
| 438 // pipelining encoders better (multiple input frames before something comes | 444 // pipelining encoders better (multiple input frames before something comes |
| 439 // out). This should effectively turn off CPU adaptations for systems that | 445 // out). This should effectively turn off CPU adaptations for systems that |
| 440 // remotely cope with the load right now. | 446 // remotely cope with the load right now. |
| 441 CpuOveruseOptions ViEEncoder::GetCpuOveruseOptions(bool full_overuse_time) { | 447 CpuOveruseOptions VideoStreamEncoder::GetCpuOveruseOptions( |
| 448 bool full_overuse_time) { |
| 442 CpuOveruseOptions options; | 449 CpuOveruseOptions options; |
| 443 if (full_overuse_time) { | 450 if (full_overuse_time) { |
| 444 options.low_encode_usage_threshold_percent = 150; | 451 options.low_encode_usage_threshold_percent = 150; |
| 445 options.high_encode_usage_threshold_percent = 200; | 452 options.high_encode_usage_threshold_percent = 200; |
| 446 } | 453 } |
| 447 return options; | 454 return options; |
| 448 } | 455 } |
| 449 | 456 |
| 450 void ViEEncoder::Stop() { | 457 void VideoStreamEncoder::Stop() { |
| 451 RTC_DCHECK_RUN_ON(&thread_checker_); | 458 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 452 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); | 459 source_proxy_->SetSource(nullptr, VideoSendStream::DegradationPreference()); |
| 453 encoder_queue_.PostTask([this] { | 460 encoder_queue_.PostTask([this] { |
| 454 RTC_DCHECK_RUN_ON(&encoder_queue_); | 461 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 455 overuse_detector_->StopCheckForOveruse(); | 462 overuse_detector_->StopCheckForOveruse(); |
| 456 rate_allocator_.reset(); | 463 rate_allocator_.reset(); |
| 457 bitrate_observer_ = nullptr; | 464 bitrate_observer_ = nullptr; |
| 458 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, | 465 video_sender_.RegisterExternalEncoder(nullptr, settings_.payload_type, |
| 459 false); | 466 false); |
| 460 quality_scaler_ = nullptr; | 467 quality_scaler_ = nullptr; |
| 461 shutdown_event_.Set(); | 468 shutdown_event_.Set(); |
| 462 }); | 469 }); |
| 463 | 470 |
| 464 shutdown_event_.Wait(rtc::Event::kForever); | 471 shutdown_event_.Wait(rtc::Event::kForever); |
| 465 } | 472 } |
| 466 | 473 |
| 467 void ViEEncoder::RegisterProcessThread(ProcessThread* module_process_thread) { | 474 void VideoStreamEncoder::RegisterProcessThread( |
| 475 ProcessThread* module_process_thread) { |
| 468 RTC_DCHECK_RUN_ON(&thread_checker_); | 476 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 469 RTC_DCHECK(!module_process_thread_); | 477 RTC_DCHECK(!module_process_thread_); |
| 470 module_process_thread_ = module_process_thread; | 478 module_process_thread_ = module_process_thread; |
| 471 module_process_thread_->RegisterModule(&video_sender_, RTC_FROM_HERE); | 479 module_process_thread_->RegisterModule(&video_sender_, RTC_FROM_HERE); |
| 472 module_process_thread_checker_.DetachFromThread(); | 480 module_process_thread_checker_.DetachFromThread(); |
| 473 } | 481 } |
| 474 | 482 |
| 475 void ViEEncoder::DeRegisterProcessThread() { | 483 void VideoStreamEncoder::DeRegisterProcessThread() { |
| 476 RTC_DCHECK_RUN_ON(&thread_checker_); | 484 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 477 module_process_thread_->DeRegisterModule(&video_sender_); | 485 module_process_thread_->DeRegisterModule(&video_sender_); |
| 478 } | 486 } |
| 479 | 487 |
| 480 void ViEEncoder::SetBitrateObserver( | 488 void VideoStreamEncoder::SetBitrateObserver( |
| 481 VideoBitrateAllocationObserver* bitrate_observer) { | 489 VideoBitrateAllocationObserver* bitrate_observer) { |
| 482 RTC_DCHECK_RUN_ON(&thread_checker_); | 490 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 483 encoder_queue_.PostTask([this, bitrate_observer] { | 491 encoder_queue_.PostTask([this, bitrate_observer] { |
| 484 RTC_DCHECK_RUN_ON(&encoder_queue_); | 492 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 485 RTC_DCHECK(!bitrate_observer_); | 493 RTC_DCHECK(!bitrate_observer_); |
| 486 bitrate_observer_ = bitrate_observer; | 494 bitrate_observer_ = bitrate_observer; |
| 487 }); | 495 }); |
| 488 } | 496 } |
| 489 | 497 |
| 490 void ViEEncoder::SetSource( | 498 void VideoStreamEncoder::SetSource( |
| 491 rtc::VideoSourceInterface<VideoFrame>* source, | 499 rtc::VideoSourceInterface<VideoFrame>* source, |
| 492 const VideoSendStream::DegradationPreference& degradation_preference) { | 500 const VideoSendStream::DegradationPreference& degradation_preference) { |
| 493 RTC_DCHECK_RUN_ON(&thread_checker_); | 501 RTC_DCHECK_RUN_ON(&thread_checker_); |
| 494 source_proxy_->SetSource(source, degradation_preference); | 502 source_proxy_->SetSource(source, degradation_preference); |
| 495 encoder_queue_.PostTask([this, degradation_preference] { | 503 encoder_queue_.PostTask([this, degradation_preference] { |
| 496 RTC_DCHECK_RUN_ON(&encoder_queue_); | 504 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 497 if (degradation_preference_ != degradation_preference) { | 505 if (degradation_preference_ != degradation_preference) { |
| 498 // Reset adaptation state, so that we're not tricked into thinking there's | 506 // Reset adaptation state, so that we're not tricked into thinking there's |
| 499 // an already pending request of the same type. | 507 // an already pending request of the same type. |
| 500 last_adaptation_request_.reset(); | 508 last_adaptation_request_.reset(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 514 ConfigureQualityScaler(); | 522 ConfigureQualityScaler(); |
| 515 if (!IsFramerateScalingEnabled(degradation_preference) && | 523 if (!IsFramerateScalingEnabled(degradation_preference) && |
| 516 max_framerate_ != -1) { | 524 max_framerate_ != -1) { |
| 517 // If frame rate scaling is no longer allowed, remove any potential | 525 // If frame rate scaling is no longer allowed, remove any potential |
| 518 // allowance for longer frame intervals. | 526 // allowance for longer frame intervals. |
| 519 overuse_detector_->OnTargetFramerateUpdated(max_framerate_); | 527 overuse_detector_->OnTargetFramerateUpdated(max_framerate_); |
| 520 } | 528 } |
| 521 }); | 529 }); |
| 522 } | 530 } |
| 523 | 531 |
| 524 void ViEEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { | 532 void VideoStreamEncoder::SetSink(EncoderSink* sink, bool rotation_applied) { |
| 525 source_proxy_->SetWantsRotationApplied(rotation_applied); | 533 source_proxy_->SetWantsRotationApplied(rotation_applied); |
| 526 encoder_queue_.PostTask([this, sink] { | 534 encoder_queue_.PostTask([this, sink] { |
| 527 RTC_DCHECK_RUN_ON(&encoder_queue_); | 535 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 528 sink_ = sink; | 536 sink_ = sink; |
| 529 }); | 537 }); |
| 530 } | 538 } |
| 531 | 539 |
| 532 void ViEEncoder::SetStartBitrate(int start_bitrate_bps) { | 540 void VideoStreamEncoder::SetStartBitrate(int start_bitrate_bps) { |
| 533 encoder_queue_.PostTask([this, start_bitrate_bps] { | 541 encoder_queue_.PostTask([this, start_bitrate_bps] { |
| 534 RTC_DCHECK_RUN_ON(&encoder_queue_); | 542 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 535 encoder_start_bitrate_bps_ = start_bitrate_bps; | 543 encoder_start_bitrate_bps_ = start_bitrate_bps; |
| 536 }); | 544 }); |
| 537 } | 545 } |
| 538 | 546 |
| 539 void ViEEncoder::ConfigureEncoder(VideoEncoderConfig config, | 547 void VideoStreamEncoder::ConfigureEncoder(VideoEncoderConfig config, |
| 540 size_t max_data_payload_length, | 548 size_t max_data_payload_length, |
| 541 bool nack_enabled) { | 549 bool nack_enabled) { |
| 542 encoder_queue_.PostTask( | 550 encoder_queue_.PostTask( |
| 543 std::unique_ptr<rtc::QueuedTask>(new ConfigureEncoderTask( | 551 std::unique_ptr<rtc::QueuedTask>(new ConfigureEncoderTask( |
| 544 this, std::move(config), max_data_payload_length, nack_enabled))); | 552 this, std::move(config), max_data_payload_length, nack_enabled))); |
| 545 } | 553 } |
| 546 | 554 |
| 547 void ViEEncoder::ConfigureEncoderOnTaskQueue(VideoEncoderConfig config, | 555 void VideoStreamEncoder::ConfigureEncoderOnTaskQueue( |
| 548 size_t max_data_payload_length, | 556 VideoEncoderConfig config, |
| 549 bool nack_enabled) { | 557 size_t max_data_payload_length, |
| 558 bool nack_enabled) { |
| 550 RTC_DCHECK_RUN_ON(&encoder_queue_); | 559 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 551 RTC_DCHECK(sink_); | 560 RTC_DCHECK(sink_); |
| 552 LOG(LS_INFO) << "ConfigureEncoder requested."; | 561 LOG(LS_INFO) << "ConfigureEncoder requested."; |
| 553 | 562 |
| 554 max_data_payload_length_ = max_data_payload_length; | 563 max_data_payload_length_ = max_data_payload_length; |
| 555 nack_enabled_ = nack_enabled; | 564 nack_enabled_ = nack_enabled; |
| 556 encoder_config_ = std::move(config); | 565 encoder_config_ = std::move(config); |
| 557 pending_encoder_reconfiguration_ = true; | 566 pending_encoder_reconfiguration_ = true; |
| 558 | 567 |
| 559 // Reconfigure the encoder now if the encoder has an internal source or | 568 // Reconfigure the encoder now if the encoder has an internal source or |
| 560 // if the frame resolution is known. Otherwise, the reconfiguration is | 569 // if the frame resolution is known. Otherwise, the reconfiguration is |
| 561 // deferred until the next frame to minimize the number of reconfigurations. | 570 // deferred until the next frame to minimize the number of reconfigurations. |
| 562 // The codec configuration depends on incoming video frame size. | 571 // The codec configuration depends on incoming video frame size. |
| 563 if (last_frame_info_) { | 572 if (last_frame_info_) { |
| 564 ReconfigureEncoder(); | 573 ReconfigureEncoder(); |
| 565 } else if (settings_.internal_source) { | 574 } else if (settings_.internal_source) { |
| 566 last_frame_info_ = | 575 last_frame_info_ = |
| 567 rtc::Optional<VideoFrameInfo>(VideoFrameInfo(176, 144, false)); | 576 rtc::Optional<VideoFrameInfo>(VideoFrameInfo(176, 144, false)); |
| 568 ReconfigureEncoder(); | 577 ReconfigureEncoder(); |
| 569 } | 578 } |
| 570 } | 579 } |
| 571 | 580 |
| 572 void ViEEncoder::ReconfigureEncoder() { | 581 void VideoStreamEncoder::ReconfigureEncoder() { |
| 573 RTC_DCHECK_RUN_ON(&encoder_queue_); | 582 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 574 RTC_DCHECK(pending_encoder_reconfiguration_); | 583 RTC_DCHECK(pending_encoder_reconfiguration_); |
| 575 std::vector<VideoStream> streams = | 584 std::vector<VideoStream> streams = |
| 576 encoder_config_.video_stream_factory->CreateEncoderStreams( | 585 encoder_config_.video_stream_factory->CreateEncoderStreams( |
| 577 last_frame_info_->width, last_frame_info_->height, encoder_config_); | 586 last_frame_info_->width, last_frame_info_->height, encoder_config_); |
| 578 | 587 |
| 579 // TODO(ilnik): If configured resolution is significantly less than provided, | 588 // TODO(ilnik): If configured resolution is significantly less than provided, |
| 580 // e.g. because there are not enough SSRCs for all simulcast streams, | 589 // e.g. because there are not enough SSRCs for all simulcast streams, |
| 581 // signal new resolutions via SinkWants to video source. | 590 // signal new resolutions via SinkWants to video source. |
| 582 | 591 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 // maintain-resolution or balanced mode. This is used to make sure overuse | 645 // maintain-resolution or balanced mode. This is used to make sure overuse |
| 637 // detection doesn't needlessly trigger in low and/or variable framerate | 646 // detection doesn't needlessly trigger in low and/or variable framerate |
| 638 // scenarios. | 647 // scenarios. |
| 639 int target_framerate = std::min( | 648 int target_framerate = std::min( |
| 640 max_framerate_, source_proxy_->GetActiveSinkWants().max_framerate_fps); | 649 max_framerate_, source_proxy_->GetActiveSinkWants().max_framerate_fps); |
| 641 overuse_detector_->OnTargetFramerateUpdated(target_framerate); | 650 overuse_detector_->OnTargetFramerateUpdated(target_framerate); |
| 642 | 651 |
| 643 ConfigureQualityScaler(); | 652 ConfigureQualityScaler(); |
| 644 } | 653 } |
| 645 | 654 |
| 646 void ViEEncoder::ConfigureQualityScaler() { | 655 void VideoStreamEncoder::ConfigureQualityScaler() { |
| 647 RTC_DCHECK_RUN_ON(&encoder_queue_); | 656 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 648 const auto scaling_settings = settings_.encoder->GetScalingSettings(); | 657 const auto scaling_settings = settings_.encoder->GetScalingSettings(); |
| 649 const bool quality_scaling_allowed = | 658 const bool quality_scaling_allowed = |
| 650 IsResolutionScalingEnabled(degradation_preference_) && | 659 IsResolutionScalingEnabled(degradation_preference_) && |
| 651 scaling_settings.enabled; | 660 scaling_settings.enabled; |
| 652 | 661 |
| 653 if (quality_scaling_allowed) { | 662 if (quality_scaling_allowed) { |
| 654 if (quality_scaler_.get() == nullptr) { | 663 if (quality_scaler_.get() == nullptr) { |
| 655 // Quality scaler has not already been configured. | 664 // Quality scaler has not already been configured. |
| 656 // Drop frames and scale down until desired quality is achieved. | 665 // Drop frames and scale down until desired quality is achieved. |
| 657 if (scaling_settings.thresholds) { | 666 if (scaling_settings.thresholds) { |
| 658 quality_scaler_.reset( | 667 quality_scaler_.reset( |
| 659 new QualityScaler(this, *(scaling_settings.thresholds))); | 668 new QualityScaler(this, *(scaling_settings.thresholds))); |
| 660 } else { | 669 } else { |
| 661 quality_scaler_.reset(new QualityScaler(this, codec_type_)); | 670 quality_scaler_.reset(new QualityScaler(this, codec_type_)); |
| 662 } | 671 } |
| 663 } | 672 } |
| 664 } else { | 673 } else { |
| 665 quality_scaler_.reset(nullptr); | 674 quality_scaler_.reset(nullptr); |
| 666 initial_rampup_ = kMaxInitialFramedrop; | 675 initial_rampup_ = kMaxInitialFramedrop; |
| 667 } | 676 } |
| 668 | 677 |
| 669 stats_proxy_->SetAdaptationStats(GetActiveCounts(kCpu), | 678 stats_proxy_->SetAdaptationStats(GetActiveCounts(kCpu), |
| 670 GetActiveCounts(kQuality)); | 679 GetActiveCounts(kQuality)); |
| 671 } | 680 } |
| 672 | 681 |
| 673 void ViEEncoder::OnFrame(const VideoFrame& video_frame) { | 682 void VideoStreamEncoder::OnFrame(const VideoFrame& video_frame) { |
| 674 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); | 683 RTC_DCHECK_RUNS_SERIALIZED(&incoming_frame_race_checker_); |
| 675 VideoFrame incoming_frame = video_frame; | 684 VideoFrame incoming_frame = video_frame; |
| 676 | 685 |
| 677 // Local time in webrtc time base. | 686 // Local time in webrtc time base. |
| 678 int64_t current_time_us = clock_->TimeInMicroseconds(); | 687 int64_t current_time_us = clock_->TimeInMicroseconds(); |
| 679 int64_t current_time_ms = current_time_us / rtc::kNumMicrosecsPerMillisec; | 688 int64_t current_time_ms = current_time_us / rtc::kNumMicrosecsPerMillisec; |
| 680 // In some cases, e.g., when the frame from decoder is fed to encoder, | 689 // In some cases, e.g., when the frame from decoder is fed to encoder, |
| 681 // the timestamp may be set to the future. As the encoding pipeline assumes | 690 // the timestamp may be set to the future. As the encoding pipeline assumes |
| 682 // capture time to be less than present time, we should reset the capture | 691 // capture time to be less than present time, we should reset the capture |
| 683 // timestamps here. Otherwise there may be issues with RTP send stream. | 692 // timestamps here. Otherwise there may be issues with RTP send stream. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 713 if (current_time_ms - last_frame_log_ms_ > kFrameLogIntervalMs) { | 722 if (current_time_ms - last_frame_log_ms_ > kFrameLogIntervalMs) { |
| 714 last_frame_log_ms_ = current_time_ms; | 723 last_frame_log_ms_ = current_time_ms; |
| 715 log_stats = true; | 724 log_stats = true; |
| 716 } | 725 } |
| 717 | 726 |
| 718 last_captured_timestamp_ = incoming_frame.ntp_time_ms(); | 727 last_captured_timestamp_ = incoming_frame.ntp_time_ms(); |
| 719 encoder_queue_.PostTask(std::unique_ptr<rtc::QueuedTask>(new EncodeTask( | 728 encoder_queue_.PostTask(std::unique_ptr<rtc::QueuedTask>(new EncodeTask( |
| 720 incoming_frame, this, rtc::TimeMicros(), log_stats))); | 729 incoming_frame, this, rtc::TimeMicros(), log_stats))); |
| 721 } | 730 } |
| 722 | 731 |
| 723 bool ViEEncoder::EncoderPaused() const { | 732 bool VideoStreamEncoder::EncoderPaused() const { |
| 724 RTC_DCHECK_RUN_ON(&encoder_queue_); | 733 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 725 // Pause video if paused by caller or as long as the network is down or the | 734 // Pause video if paused by caller or as long as the network is down or the |
| 726 // pacer queue has grown too large in buffered mode. | 735 // pacer queue has grown too large in buffered mode. |
| 727 // If the pacer queue has grown too large or the network is down, | 736 // If the pacer queue has grown too large or the network is down, |
| 728 // last_observed_bitrate_bps_ will be 0. | 737 // last_observed_bitrate_bps_ will be 0. |
| 729 return last_observed_bitrate_bps_ == 0; | 738 return last_observed_bitrate_bps_ == 0; |
| 730 } | 739 } |
| 731 | 740 |
| 732 void ViEEncoder::TraceFrameDropStart() { | 741 void VideoStreamEncoder::TraceFrameDropStart() { |
| 733 RTC_DCHECK_RUN_ON(&encoder_queue_); | 742 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 734 // Start trace event only on the first frame after encoder is paused. | 743 // Start trace event only on the first frame after encoder is paused. |
| 735 if (!encoder_paused_and_dropped_frame_) { | 744 if (!encoder_paused_and_dropped_frame_) { |
| 736 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this); | 745 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "EncoderPaused", this); |
| 737 } | 746 } |
| 738 encoder_paused_and_dropped_frame_ = true; | 747 encoder_paused_and_dropped_frame_ = true; |
| 739 } | 748 } |
| 740 | 749 |
| 741 void ViEEncoder::TraceFrameDropEnd() { | 750 void VideoStreamEncoder::TraceFrameDropEnd() { |
| 742 RTC_DCHECK_RUN_ON(&encoder_queue_); | 751 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 743 // End trace event on first frame after encoder resumes, if frame was dropped. | 752 // End trace event on first frame after encoder resumes, if frame was dropped. |
| 744 if (encoder_paused_and_dropped_frame_) { | 753 if (encoder_paused_and_dropped_frame_) { |
| 745 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this); | 754 TRACE_EVENT_ASYNC_END0("webrtc", "EncoderPaused", this); |
| 746 } | 755 } |
| 747 encoder_paused_and_dropped_frame_ = false; | 756 encoder_paused_and_dropped_frame_ = false; |
| 748 } | 757 } |
| 749 | 758 |
| 750 void ViEEncoder::EncodeVideoFrame(const VideoFrame& video_frame, | 759 void VideoStreamEncoder::EncodeVideoFrame(const VideoFrame& video_frame, |
| 751 int64_t time_when_posted_us) { | 760 int64_t time_when_posted_us) { |
| 752 RTC_DCHECK_RUN_ON(&encoder_queue_); | 761 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 753 | 762 |
| 754 if (pre_encode_callback_) | 763 if (pre_encode_callback_) |
| 755 pre_encode_callback_->OnFrame(video_frame); | 764 pre_encode_callback_->OnFrame(video_frame); |
| 756 | 765 |
| 757 if (!last_frame_info_ || video_frame.width() != last_frame_info_->width || | 766 if (!last_frame_info_ || video_frame.width() != last_frame_info_->width || |
| 758 video_frame.height() != last_frame_info_->height || | 767 video_frame.height() != last_frame_info_->height || |
| 759 video_frame.is_texture() != last_frame_info_->is_texture) { | 768 video_frame.is_texture() != last_frame_info_->is_texture) { |
| 760 pending_encoder_reconfiguration_ = true; | 769 pending_encoder_reconfiguration_ = true; |
| 761 last_frame_info_ = rtc::Optional<VideoFrameInfo>(VideoFrameInfo( | 770 last_frame_info_ = rtc::Optional<VideoFrameInfo>(VideoFrameInfo( |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 817 } | 826 } |
| 818 | 827 |
| 819 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), | 828 TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", video_frame.render_time_ms(), |
| 820 "Encode"); | 829 "Encode"); |
| 821 | 830 |
| 822 overuse_detector_->FrameCaptured(out_frame, time_when_posted_us); | 831 overuse_detector_->FrameCaptured(out_frame, time_when_posted_us); |
| 823 | 832 |
| 824 video_sender_.AddVideoFrame(out_frame, nullptr); | 833 video_sender_.AddVideoFrame(out_frame, nullptr); |
| 825 } | 834 } |
| 826 | 835 |
| 827 void ViEEncoder::SendKeyFrame() { | 836 void VideoStreamEncoder::SendKeyFrame() { |
| 828 if (!encoder_queue_.IsCurrent()) { | 837 if (!encoder_queue_.IsCurrent()) { |
| 829 encoder_queue_.PostTask([this] { SendKeyFrame(); }); | 838 encoder_queue_.PostTask([this] { SendKeyFrame(); }); |
| 830 return; | 839 return; |
| 831 } | 840 } |
| 832 RTC_DCHECK_RUN_ON(&encoder_queue_); | 841 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 833 video_sender_.IntraFrameRequest(0); | 842 video_sender_.IntraFrameRequest(0); |
| 834 } | 843 } |
| 835 | 844 |
| 836 EncodedImageCallback::Result ViEEncoder::OnEncodedImage( | 845 EncodedImageCallback::Result VideoStreamEncoder::OnEncodedImage( |
| 837 const EncodedImage& encoded_image, | 846 const EncodedImage& encoded_image, |
| 838 const CodecSpecificInfo* codec_specific_info, | 847 const CodecSpecificInfo* codec_specific_info, |
| 839 const RTPFragmentationHeader* fragmentation) { | 848 const RTPFragmentationHeader* fragmentation) { |
| 840 // Encoded is called on whatever thread the real encoder implementation run | 849 // Encoded is called on whatever thread the real encoder implementation run |
| 841 // on. In the case of hardware encoders, there might be several encoders | 850 // on. In the case of hardware encoders, there might be several encoders |
| 842 // running in parallel on different threads. | 851 // running in parallel on different threads. |
| 843 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); | 852 stats_proxy_->OnSendEncodedImage(encoded_image, codec_specific_info); |
| 844 | 853 |
| 845 EncodedImageCallback::Result result = | 854 EncodedImageCallback::Result result = |
| 846 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); | 855 sink_->OnEncodedImage(encoded_image, codec_specific_info, fragmentation); |
| 847 | 856 |
| 848 int64_t time_sent_us = rtc::TimeMicros(); | 857 int64_t time_sent_us = rtc::TimeMicros(); |
| 849 uint32_t timestamp = encoded_image._timeStamp; | 858 uint32_t timestamp = encoded_image._timeStamp; |
| 850 const int qp = encoded_image.qp_; | 859 const int qp = encoded_image.qp_; |
| 851 encoder_queue_.PostTask([this, timestamp, time_sent_us, qp] { | 860 encoder_queue_.PostTask([this, timestamp, time_sent_us, qp] { |
| 852 RTC_DCHECK_RUN_ON(&encoder_queue_); | 861 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 853 overuse_detector_->FrameSent(timestamp, time_sent_us); | 862 overuse_detector_->FrameSent(timestamp, time_sent_us); |
| 854 if (quality_scaler_ && qp >= 0) | 863 if (quality_scaler_ && qp >= 0) |
| 855 quality_scaler_->ReportQP(qp); | 864 quality_scaler_->ReportQP(qp); |
| 856 }); | 865 }); |
| 857 | 866 |
| 858 return result; | 867 return result; |
| 859 } | 868 } |
| 860 | 869 |
| 861 void ViEEncoder::OnDroppedFrame() { | 870 void VideoStreamEncoder::OnDroppedFrame() { |
| 862 encoder_queue_.PostTask([this] { | 871 encoder_queue_.PostTask([this] { |
| 863 RTC_DCHECK_RUN_ON(&encoder_queue_); | 872 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 864 if (quality_scaler_) | 873 if (quality_scaler_) |
| 865 quality_scaler_->ReportDroppedFrame(); | 874 quality_scaler_->ReportDroppedFrame(); |
| 866 }); | 875 }); |
| 867 } | 876 } |
| 868 | 877 |
| 869 void ViEEncoder::SendStatistics(uint32_t bit_rate, uint32_t frame_rate) { | 878 void VideoStreamEncoder::SendStatistics(uint32_t bit_rate, |
| 879 uint32_t frame_rate) { |
| 870 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); | 880 RTC_DCHECK(module_process_thread_checker_.CalledOnValidThread()); |
| 871 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); | 881 stats_proxy_->OnEncoderStatsUpdate(frame_rate, bit_rate); |
| 872 } | 882 } |
| 873 | 883 |
| 874 void ViEEncoder::OnReceivedIntraFrameRequest(size_t stream_index) { | 884 void VideoStreamEncoder::OnReceivedIntraFrameRequest(size_t stream_index) { |
| 875 if (!encoder_queue_.IsCurrent()) { | 885 if (!encoder_queue_.IsCurrent()) { |
| 876 encoder_queue_.PostTask( | 886 encoder_queue_.PostTask( |
| 877 [this, stream_index] { OnReceivedIntraFrameRequest(stream_index); }); | 887 [this, stream_index] { OnReceivedIntraFrameRequest(stream_index); }); |
| 878 return; | 888 return; |
| 879 } | 889 } |
| 880 RTC_DCHECK_RUN_ON(&encoder_queue_); | 890 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 881 // Key frame request from remote side, signal to VCM. | 891 // Key frame request from remote side, signal to VCM. |
| 882 TRACE_EVENT0("webrtc", "OnKeyFrameRequest"); | 892 TRACE_EVENT0("webrtc", "OnKeyFrameRequest"); |
| 883 video_sender_.IntraFrameRequest(stream_index); | 893 video_sender_.IntraFrameRequest(stream_index); |
| 884 } | 894 } |
| 885 | 895 |
| 886 void ViEEncoder::OnBitrateUpdated(uint32_t bitrate_bps, | 896 void VideoStreamEncoder::OnBitrateUpdated(uint32_t bitrate_bps, |
| 887 uint8_t fraction_lost, | 897 uint8_t fraction_lost, |
| 888 int64_t round_trip_time_ms) { | 898 int64_t round_trip_time_ms) { |
| 889 if (!encoder_queue_.IsCurrent()) { | 899 if (!encoder_queue_.IsCurrent()) { |
| 890 encoder_queue_.PostTask( | 900 encoder_queue_.PostTask( |
| 891 [this, bitrate_bps, fraction_lost, round_trip_time_ms] { | 901 [this, bitrate_bps, fraction_lost, round_trip_time_ms] { |
| 892 OnBitrateUpdated(bitrate_bps, fraction_lost, round_trip_time_ms); | 902 OnBitrateUpdated(bitrate_bps, fraction_lost, round_trip_time_ms); |
| 893 }); | 903 }); |
| 894 return; | 904 return; |
| 895 } | 905 } |
| 896 RTC_DCHECK_RUN_ON(&encoder_queue_); | 906 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 897 RTC_DCHECK(sink_) << "sink_ must be set before the encoder is active."; | 907 RTC_DCHECK(sink_) << "sink_ must be set before the encoder is active."; |
| 898 | 908 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 910 bool video_suspension_changed = video_is_suspended != EncoderPaused(); | 920 bool video_suspension_changed = video_is_suspended != EncoderPaused(); |
| 911 last_observed_bitrate_bps_ = bitrate_bps; | 921 last_observed_bitrate_bps_ = bitrate_bps; |
| 912 | 922 |
| 913 if (video_suspension_changed) { | 923 if (video_suspension_changed) { |
| 914 LOG(LS_INFO) << "Video suspend state changed to: " | 924 LOG(LS_INFO) << "Video suspend state changed to: " |
| 915 << (video_is_suspended ? "suspended" : "not suspended"); | 925 << (video_is_suspended ? "suspended" : "not suspended"); |
| 916 stats_proxy_->OnSuspendChange(video_is_suspended); | 926 stats_proxy_->OnSuspendChange(video_is_suspended); |
| 917 } | 927 } |
| 918 } | 928 } |
| 919 | 929 |
| 920 void ViEEncoder::AdaptDown(AdaptReason reason) { | 930 void VideoStreamEncoder::AdaptDown(AdaptReason reason) { |
| 921 RTC_DCHECK_RUN_ON(&encoder_queue_); | 931 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 922 AdaptationRequest adaptation_request = { | 932 AdaptationRequest adaptation_request = { |
| 923 last_frame_info_->pixel_count(), | 933 last_frame_info_->pixel_count(), |
| 924 stats_proxy_->GetStats().input_frame_rate, | 934 stats_proxy_->GetStats().input_frame_rate, |
| 925 AdaptationRequest::Mode::kAdaptDown}; | 935 AdaptationRequest::Mode::kAdaptDown}; |
| 926 | 936 |
| 927 bool downgrade_requested = | 937 bool downgrade_requested = |
| 928 last_adaptation_request_ && | 938 last_adaptation_request_ && |
| 929 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; | 939 last_adaptation_request_->mode_ == AdaptationRequest::Mode::kAdaptDown; |
| 930 | 940 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1001 RTC_NOTREACHED(); | 1011 RTC_NOTREACHED(); |
| 1002 } | 1012 } |
| 1003 | 1013 |
| 1004 last_adaptation_request_.emplace(adaptation_request); | 1014 last_adaptation_request_.emplace(adaptation_request); |
| 1005 | 1015 |
| 1006 UpdateAdaptationStats(reason); | 1016 UpdateAdaptationStats(reason); |
| 1007 | 1017 |
| 1008 LOG(LS_INFO) << GetConstAdaptCounter().ToString(); | 1018 LOG(LS_INFO) << GetConstAdaptCounter().ToString(); |
| 1009 } | 1019 } |
| 1010 | 1020 |
| 1011 void ViEEncoder::AdaptUp(AdaptReason reason) { | 1021 void VideoStreamEncoder::AdaptUp(AdaptReason reason) { |
| 1012 RTC_DCHECK_RUN_ON(&encoder_queue_); | 1022 RTC_DCHECK_RUN_ON(&encoder_queue_); |
| 1013 | 1023 |
| 1014 const AdaptCounter& adapt_counter = GetConstAdaptCounter(); | 1024 const AdaptCounter& adapt_counter = GetConstAdaptCounter(); |
| 1015 int num_downgrades = adapt_counter.TotalCount(reason); | 1025 int num_downgrades = adapt_counter.TotalCount(reason); |
| 1016 if (num_downgrades == 0) | 1026 if (num_downgrades == 0) |
| 1017 return; | 1027 return; |
| 1018 RTC_DCHECK_GT(num_downgrades, 0); | 1028 RTC_DCHECK_GT(num_downgrades, 0); |
| 1019 | 1029 |
| 1020 AdaptationRequest adaptation_request = { | 1030 AdaptationRequest adaptation_request = { |
| 1021 last_frame_info_->pixel_count(), | 1031 last_frame_info_->pixel_count(), |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1089 return; | 1099 return; |
| 1090 } | 1100 } |
| 1091 | 1101 |
| 1092 last_adaptation_request_.emplace(adaptation_request); | 1102 last_adaptation_request_.emplace(adaptation_request); |
| 1093 | 1103 |
| 1094 UpdateAdaptationStats(reason); | 1104 UpdateAdaptationStats(reason); |
| 1095 | 1105 |
| 1096 LOG(LS_INFO) << adapt_counter.ToString(); | 1106 LOG(LS_INFO) << adapt_counter.ToString(); |
| 1097 } | 1107 } |
| 1098 | 1108 |
| 1099 void ViEEncoder::UpdateAdaptationStats(AdaptReason reason) { | 1109 void VideoStreamEncoder::UpdateAdaptationStats(AdaptReason reason) { |
| 1100 switch (reason) { | 1110 switch (reason) { |
| 1101 case kCpu: | 1111 case kCpu: |
| 1102 stats_proxy_->OnCpuAdaptationChanged(GetActiveCounts(kCpu), | 1112 stats_proxy_->OnCpuAdaptationChanged(GetActiveCounts(kCpu), |
| 1103 GetActiveCounts(kQuality)); | 1113 GetActiveCounts(kQuality)); |
| 1104 break; | 1114 break; |
| 1105 case kQuality: | 1115 case kQuality: |
| 1106 stats_proxy_->OnQualityAdaptationChanged(GetActiveCounts(kCpu), | 1116 stats_proxy_->OnQualityAdaptationChanged(GetActiveCounts(kCpu), |
| 1107 GetActiveCounts(kQuality)); | 1117 GetActiveCounts(kQuality)); |
| 1108 break; | 1118 break; |
| 1109 } | 1119 } |
| 1110 } | 1120 } |
| 1111 | 1121 |
| 1112 ViEEncoder::AdaptCounts ViEEncoder::GetActiveCounts(AdaptReason reason) { | 1122 VideoStreamEncoder::AdaptCounts VideoStreamEncoder::GetActiveCounts( |
| 1113 ViEEncoder::AdaptCounts counts = GetConstAdaptCounter().Counts(reason); | 1123 AdaptReason reason) { |
| 1124 VideoStreamEncoder::AdaptCounts counts = |
| 1125 GetConstAdaptCounter().Counts(reason); |
| 1114 switch (reason) { | 1126 switch (reason) { |
| 1115 case kCpu: | 1127 case kCpu: |
| 1116 if (!IsFramerateScalingEnabled(degradation_preference_)) | 1128 if (!IsFramerateScalingEnabled(degradation_preference_)) |
| 1117 counts.fps = -1; | 1129 counts.fps = -1; |
| 1118 if (!IsResolutionScalingEnabled(degradation_preference_)) | 1130 if (!IsResolutionScalingEnabled(degradation_preference_)) |
| 1119 counts.resolution = -1; | 1131 counts.resolution = -1; |
| 1120 break; | 1132 break; |
| 1121 case kQuality: | 1133 case kQuality: |
| 1122 if (!IsFramerateScalingEnabled(degradation_preference_) || | 1134 if (!IsFramerateScalingEnabled(degradation_preference_) || |
| 1123 !quality_scaler_) { | 1135 !quality_scaler_) { |
| 1124 counts.fps = -1; | 1136 counts.fps = -1; |
| 1125 } | 1137 } |
| 1126 if (!IsResolutionScalingEnabled(degradation_preference_) || | 1138 if (!IsResolutionScalingEnabled(degradation_preference_) || |
| 1127 !quality_scaler_) { | 1139 !quality_scaler_) { |
| 1128 counts.resolution = -1; | 1140 counts.resolution = -1; |
| 1129 } | 1141 } |
| 1130 break; | 1142 break; |
| 1131 } | 1143 } |
| 1132 return counts; | 1144 return counts; |
| 1133 } | 1145 } |
| 1134 | 1146 |
| 1135 ViEEncoder::AdaptCounter& ViEEncoder::GetAdaptCounter() { | 1147 VideoStreamEncoder::AdaptCounter& VideoStreamEncoder::GetAdaptCounter() { |
| 1136 return adapt_counters_[degradation_preference_]; | 1148 return adapt_counters_[degradation_preference_]; |
| 1137 } | 1149 } |
| 1138 | 1150 |
| 1139 const ViEEncoder::AdaptCounter& ViEEncoder::GetConstAdaptCounter() { | 1151 const VideoStreamEncoder::AdaptCounter& |
| 1152 VideoStreamEncoder::GetConstAdaptCounter() { |
| 1140 return adapt_counters_[degradation_preference_]; | 1153 return adapt_counters_[degradation_preference_]; |
| 1141 } | 1154 } |
| 1142 | 1155 |
| 1143 // Class holding adaptation information. | 1156 // Class holding adaptation information. |
| 1144 ViEEncoder::AdaptCounter::AdaptCounter() { | 1157 VideoStreamEncoder::AdaptCounter::AdaptCounter() { |
| 1145 fps_counters_.resize(kScaleReasonSize); | 1158 fps_counters_.resize(kScaleReasonSize); |
| 1146 resolution_counters_.resize(kScaleReasonSize); | 1159 resolution_counters_.resize(kScaleReasonSize); |
| 1147 static_assert(kScaleReasonSize == 2, "Update MoveCount."); | 1160 static_assert(kScaleReasonSize == 2, "Update MoveCount."); |
| 1148 } | 1161 } |
| 1149 | 1162 |
| 1150 ViEEncoder::AdaptCounter::~AdaptCounter() {} | 1163 VideoStreamEncoder::AdaptCounter::~AdaptCounter() {} |
| 1151 | 1164 |
| 1152 std::string ViEEncoder::AdaptCounter::ToString() const { | 1165 std::string VideoStreamEncoder::AdaptCounter::ToString() const { |
| 1153 std::stringstream ss; | 1166 std::stringstream ss; |
| 1154 ss << "Downgrade counts: fps: {" << ToString(fps_counters_); | 1167 ss << "Downgrade counts: fps: {" << ToString(fps_counters_); |
| 1155 ss << "}, resolution: {" << ToString(resolution_counters_) << "}"; | 1168 ss << "}, resolution: {" << ToString(resolution_counters_) << "}"; |
| 1156 return ss.str(); | 1169 return ss.str(); |
| 1157 } | 1170 } |
| 1158 | 1171 |
| 1159 ViEEncoder::AdaptCounts ViEEncoder::AdaptCounter::Counts(int reason) const { | 1172 VideoStreamEncoder::AdaptCounts VideoStreamEncoder::AdaptCounter::Counts( |
| 1173 int reason) const { |
| 1160 AdaptCounts counts; | 1174 AdaptCounts counts; |
| 1161 counts.fps = fps_counters_[reason]; | 1175 counts.fps = fps_counters_[reason]; |
| 1162 counts.resolution = resolution_counters_[reason]; | 1176 counts.resolution = resolution_counters_[reason]; |
| 1163 return counts; | 1177 return counts; |
| 1164 } | 1178 } |
| 1165 | 1179 |
| 1166 void ViEEncoder::AdaptCounter::IncrementFramerate(int reason) { | 1180 void VideoStreamEncoder::AdaptCounter::IncrementFramerate(int reason) { |
| 1167 ++(fps_counters_[reason]); | 1181 ++(fps_counters_[reason]); |
| 1168 } | 1182 } |
| 1169 | 1183 |
| 1170 void ViEEncoder::AdaptCounter::IncrementResolution(int reason) { | 1184 void VideoStreamEncoder::AdaptCounter::IncrementResolution(int reason) { |
| 1171 ++(resolution_counters_[reason]); | 1185 ++(resolution_counters_[reason]); |
| 1172 } | 1186 } |
| 1173 | 1187 |
| 1174 void ViEEncoder::AdaptCounter::DecrementFramerate(int reason) { | 1188 void VideoStreamEncoder::AdaptCounter::DecrementFramerate(int reason) { |
| 1175 if (fps_counters_[reason] == 0) { | 1189 if (fps_counters_[reason] == 0) { |
| 1176 // Balanced mode: Adapt up is in a different order, switch reason. | 1190 // Balanced mode: Adapt up is in a different order, switch reason. |
| 1177 // E.g. framerate adapt down: quality (2), framerate adapt up: cpu (3). | 1191 // E.g. framerate adapt down: quality (2), framerate adapt up: cpu (3). |
| 1178 // 1. Down resolution (cpu): res={quality:0,cpu:1}, fps={quality:0,cpu:0} | 1192 // 1. Down resolution (cpu): res={quality:0,cpu:1}, fps={quality:0,cpu:0} |
| 1179 // 2. Down fps (quality): res={quality:0,cpu:1}, fps={quality:1,cpu:0} | 1193 // 2. Down fps (quality): res={quality:0,cpu:1}, fps={quality:1,cpu:0} |
| 1180 // 3. Up fps (cpu): res={quality:1,cpu:0}, fps={quality:0,cpu:0} | 1194 // 3. Up fps (cpu): res={quality:1,cpu:0}, fps={quality:0,cpu:0} |
| 1181 // 4. Up resolution (quality): res={quality:0,cpu:0}, fps={quality:0,cpu:0} | 1195 // 4. Up resolution (quality): res={quality:0,cpu:0}, fps={quality:0,cpu:0} |
| 1182 RTC_DCHECK_GT(TotalCount(reason), 0) << "No downgrade for reason."; | 1196 RTC_DCHECK_GT(TotalCount(reason), 0) << "No downgrade for reason."; |
| 1183 RTC_DCHECK_GT(FramerateCount(), 0) << "Framerate not downgraded."; | 1197 RTC_DCHECK_GT(FramerateCount(), 0) << "Framerate not downgraded."; |
| 1184 MoveCount(&resolution_counters_, reason); | 1198 MoveCount(&resolution_counters_, reason); |
| 1185 MoveCount(&fps_counters_, (reason + 1) % kScaleReasonSize); | 1199 MoveCount(&fps_counters_, (reason + 1) % kScaleReasonSize); |
| 1186 } | 1200 } |
| 1187 --(fps_counters_[reason]); | 1201 --(fps_counters_[reason]); |
| 1188 RTC_DCHECK_GE(fps_counters_[reason], 0); | 1202 RTC_DCHECK_GE(fps_counters_[reason], 0); |
| 1189 } | 1203 } |
| 1190 | 1204 |
| 1191 void ViEEncoder::AdaptCounter::DecrementResolution(int reason) { | 1205 void VideoStreamEncoder::AdaptCounter::DecrementResolution(int reason) { |
| 1192 if (resolution_counters_[reason] == 0) { | 1206 if (resolution_counters_[reason] == 0) { |
| 1193 // Balanced mode: Adapt up is in a different order, switch reason. | 1207 // Balanced mode: Adapt up is in a different order, switch reason. |
| 1194 RTC_DCHECK_GT(TotalCount(reason), 0) << "No downgrade for reason."; | 1208 RTC_DCHECK_GT(TotalCount(reason), 0) << "No downgrade for reason."; |
| 1195 RTC_DCHECK_GT(ResolutionCount(), 0) << "Resolution not downgraded."; | 1209 RTC_DCHECK_GT(ResolutionCount(), 0) << "Resolution not downgraded."; |
| 1196 MoveCount(&fps_counters_, reason); | 1210 MoveCount(&fps_counters_, reason); |
| 1197 MoveCount(&resolution_counters_, (reason + 1) % kScaleReasonSize); | 1211 MoveCount(&resolution_counters_, (reason + 1) % kScaleReasonSize); |
| 1198 } | 1212 } |
| 1199 --(resolution_counters_[reason]); | 1213 --(resolution_counters_[reason]); |
| 1200 RTC_DCHECK_GE(resolution_counters_[reason], 0); | 1214 RTC_DCHECK_GE(resolution_counters_[reason], 0); |
| 1201 } | 1215 } |
| 1202 | 1216 |
| 1203 void ViEEncoder::AdaptCounter::DecrementFramerate(int reason, int cur_fps) { | 1217 void VideoStreamEncoder::AdaptCounter::DecrementFramerate(int reason, |
| 1218 int cur_fps) { |
| 1204 DecrementFramerate(reason); | 1219 DecrementFramerate(reason); |
| 1205 // Reset if at max fps (i.e. in case of fewer steps up than down). | 1220 // Reset if at max fps (i.e. in case of fewer steps up than down). |
| 1206 if (cur_fps == std::numeric_limits<int>::max()) | 1221 if (cur_fps == std::numeric_limits<int>::max()) |
| 1207 std::fill(fps_counters_.begin(), fps_counters_.end(), 0); | 1222 std::fill(fps_counters_.begin(), fps_counters_.end(), 0); |
| 1208 } | 1223 } |
| 1209 | 1224 |
| 1210 int ViEEncoder::AdaptCounter::FramerateCount() const { | 1225 int VideoStreamEncoder::AdaptCounter::FramerateCount() const { |
| 1211 return Count(fps_counters_); | 1226 return Count(fps_counters_); |
| 1212 } | 1227 } |
| 1213 | 1228 |
| 1214 int ViEEncoder::AdaptCounter::ResolutionCount() const { | 1229 int VideoStreamEncoder::AdaptCounter::ResolutionCount() const { |
| 1215 return Count(resolution_counters_); | 1230 return Count(resolution_counters_); |
| 1216 } | 1231 } |
| 1217 | 1232 |
| 1218 int ViEEncoder::AdaptCounter::FramerateCount(int reason) const { | 1233 int VideoStreamEncoder::AdaptCounter::FramerateCount(int reason) const { |
| 1219 return fps_counters_[reason]; | 1234 return fps_counters_[reason]; |
| 1220 } | 1235 } |
| 1221 | 1236 |
| 1222 int ViEEncoder::AdaptCounter::ResolutionCount(int reason) const { | 1237 int VideoStreamEncoder::AdaptCounter::ResolutionCount(int reason) const { |
| 1223 return resolution_counters_[reason]; | 1238 return resolution_counters_[reason]; |
| 1224 } | 1239 } |
| 1225 | 1240 |
| 1226 int ViEEncoder::AdaptCounter::TotalCount(int reason) const { | 1241 int VideoStreamEncoder::AdaptCounter::TotalCount(int reason) const { |
| 1227 return FramerateCount(reason) + ResolutionCount(reason); | 1242 return FramerateCount(reason) + ResolutionCount(reason); |
| 1228 } | 1243 } |
| 1229 | 1244 |
| 1230 int ViEEncoder::AdaptCounter::Count(const std::vector<int>& counters) const { | 1245 int VideoStreamEncoder::AdaptCounter::Count( |
| 1246 const std::vector<int>& counters) const { |
| 1231 return std::accumulate(counters.begin(), counters.end(), 0); | 1247 return std::accumulate(counters.begin(), counters.end(), 0); |
| 1232 } | 1248 } |
| 1233 | 1249 |
| 1234 void ViEEncoder::AdaptCounter::MoveCount(std::vector<int>* counters, | 1250 void VideoStreamEncoder::AdaptCounter::MoveCount(std::vector<int>* counters, |
| 1235 int from_reason) { | 1251 int from_reason) { |
| 1236 int to_reason = (from_reason + 1) % kScaleReasonSize; | 1252 int to_reason = (from_reason + 1) % kScaleReasonSize; |
| 1237 ++((*counters)[to_reason]); | 1253 ++((*counters)[to_reason]); |
| 1238 --((*counters)[from_reason]); | 1254 --((*counters)[from_reason]); |
| 1239 } | 1255 } |
| 1240 | 1256 |
| 1241 std::string ViEEncoder::AdaptCounter::ToString( | 1257 std::string VideoStreamEncoder::AdaptCounter::ToString( |
| 1242 const std::vector<int>& counters) const { | 1258 const std::vector<int>& counters) const { |
| 1243 std::stringstream ss; | 1259 std::stringstream ss; |
| 1244 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { | 1260 for (size_t reason = 0; reason < kScaleReasonSize; ++reason) { |
| 1245 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; | 1261 ss << (reason ? " cpu" : "quality") << ":" << counters[reason]; |
| 1246 } | 1262 } |
| 1247 return ss.str(); | 1263 return ss.str(); |
| 1248 } | 1264 } |
| 1249 | 1265 |
| 1250 } // namespace webrtc | 1266 } // namespace webrtc |
| OLD | NEW |