| 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 | #include "video/adaptation/encode_usage_resource.h" |
| 12 |
| 13 | #include <limits> |
| 14 | #include <utility> |
| 15 |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/ref_counted_object.h" |
| 18 |
| 19 | namespace webrtc { |
| 20 |
| 21 | // static |
| 22 | rtc::scoped_refptr<EncodeUsageResource> EncodeUsageResource::Create( |
| 23 | std::unique_ptr<OveruseFrameDetector> overuse_detector) { |
| 24 | return new rtc::RefCountedObject<EncodeUsageResource>( |
| 25 | std::move(overuse_detector)); |
| 26 | } |
| 27 |
| 28 | EncodeUsageResource::EncodeUsageResource( |
| 29 | std::unique_ptr<OveruseFrameDetector> overuse_detector) |
| 30 | : VideoStreamEncoderResource("EncoderUsageResource"), |
| 31 | overuse_detector_(std::move(overuse_detector)), |
| 32 | is_started_(false), |
| 33 | target_frame_rate_(absl::nullopt) { |
| 34 | RTC_DCHECK(overuse_detector_); |
| 35 | } |
| 36 |
| 37 | EncodeUsageResource::~EncodeUsageResource() {} |
| 38 |
| 39 | bool EncodeUsageResource::is_started() const { |
| 40 | RTC_DCHECK_RUN_ON(encoder_queue()); |
| 41 | return is_started_; |
| 42 | } |
| 43 |
| 44 | void EncodeUsageResource::StartCheckForOveruse(CpuOveruseOptions options) { |
| 45 | RTC_DCHECK_RUN_ON(encoder_queue()); |
| 46 | RTC_DCHECK(!is_started_); |
| 47 | overuse_detector_->StartCheckForOveruse(TaskQueueBase::Current(), |
| 48 | std::move(options), this); |
| 49 | is_started_ = true; |
| 50 | overuse_detector_->OnTargetFramerateUpdated(TargetFrameRateAsInt()); |
| 51 | } |
| 52 |
| 53 | void EncodeUsageResource::StopCheckForOveruse() { |
| 54 | RTC_DCHECK_RUN_ON(encoder_queue()); |
| 55 | overuse_detector_->StopCheckForOveruse(); |
| 56 | is_started_ = false; |
| 57 | } |
| 58 |
| 59 | void EncodeUsageResource::SetTargetFrameRate( |
| 60 | absl::optional<double> target_frame_rate) { |
| 61 | RTC_DCHECK_RUN_ON(encoder_queue()); |
| 62 | if (target_frame_rate == target_frame_rate_) |
| 63 | return; |
| 64 | target_frame_rate_ = target_frame_rate; |
| 65 | if (is_started_) |
| 66 | overuse_detector_->OnTargetFramerateUpdated(TargetFrameRateAsInt()); |
| 67 | } |
| 68 |
| 69 | void EncodeUsageResource::OnEncodeStarted(const VideoFrame& cropped_frame, |
| 70 | int64_t time_when_first_seen_us) { |
| 71 | RTC_DCHECK_RUN_ON(encoder_queue()); |
| 72 | // TODO(hbos): Rename FrameCaptured() to something more appropriate (e.g. |
| 73 | // "OnEncodeStarted"?) or revise usage. |
| 74 | overuse_detector_->FrameCaptured(cropped_frame, time_when_first_seen_us); |
| 75 | } |
| 76 |
| 77 | void EncodeUsageResource::OnEncodeCompleted( |
| 78 | uint32_t timestamp, |
| 79 | int64_t time_sent_in_us, |
| 80 | int64_t capture_time_us, |
| 81 | absl::optional<int> encode_duration_us) { |
| 82 | RTC_DCHECK_RUN_ON(encoder_queue()); |
| 83 | // TODO(hbos): Rename FrameSent() to something more appropriate (e.g. |
| 84 | // "OnEncodeCompleted"?). |
| 85 | overuse_detector_->FrameSent(timestamp, time_sent_in_us, capture_time_us, |
| 86 | encode_duration_us); |
| 87 | } |
| 88 |
| 89 | void EncodeUsageResource::AdaptUp() { |
| 90 | RTC_DCHECK_RUN_ON(encoder_queue()); |
| 91 | OnResourceUsageStateMeasured(ResourceUsageState::kUnderuse); |
| 92 | } |
| 93 |
| 94 | void EncodeUsageResource::AdaptDown() { |
| 95 | RTC_DCHECK_RUN_ON(encoder_queue()); |
| 96 | OnResourceUsageStateMeasured(ResourceUsageState::kOveruse); |
| 97 | } |
| 98 |
| 99 | int EncodeUsageResource::TargetFrameRateAsInt() { |
| 100 | RTC_DCHECK_RUN_ON(encoder_queue()); |
| 101 | return target_frame_rate_.has_value() |
| 102 | ? static_cast<int>(target_frame_rate_.value()) |
| 103 | : std::numeric_limits<int>::max(); |
| 104 | } |
| 105 |
| 106 | } // namespace webrtc |
| 107 |