| 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 "api/video/video_timing.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 |
| 15 | #include "api/array_view.h" |
| 16 | #include "api/units/time_delta.h" |
| 17 | #include "rtc_base/logging.h" |
| 18 | #include "rtc_base/numerics/safe_conversions.h" |
| 19 | #include "rtc_base/strings/string_builder.h" |
| 20 |
| 21 | namespace webrtc { |
| 22 |
| 23 | uint16_t VideoSendTiming::GetDeltaCappedMs(int64_t base_ms, int64_t time_ms) { |
| 24 | if (time_ms < base_ms) { |
| 25 | RTC_DLOG(LS_ERROR) << "Delta " << (time_ms - base_ms) |
| 26 | << "ms expected to be positive"; |
| 27 | } |
| 28 | return rtc::saturated_cast<uint16_t>(time_ms - base_ms); |
| 29 | } |
| 30 |
| 31 | uint16_t VideoSendTiming::GetDeltaCappedMs(TimeDelta delta) { |
| 32 | if (delta < TimeDelta::Zero()) { |
| 33 | RTC_DLOG(LS_ERROR) << "Delta " << delta.ms() |
| 34 | << "ms expected to be positive"; |
| 35 | } |
| 36 | return rtc::saturated_cast<uint16_t>(delta.ms()); |
| 37 | } |
| 38 |
| 39 | TimingFrameInfo::TimingFrameInfo() |
| 40 | : rtp_timestamp(0), |
| 41 | capture_time_ms(-1), |
| 42 | encode_start_ms(-1), |
| 43 | encode_finish_ms(-1), |
| 44 | packetization_finish_ms(-1), |
| 45 | pacer_exit_ms(-1), |
| 46 | network_timestamp_ms(-1), |
| 47 | network2_timestamp_ms(-1), |
| 48 | receive_start_ms(-1), |
| 49 | receive_finish_ms(-1), |
| 50 | decode_start_ms(-1), |
| 51 | decode_finish_ms(-1), |
| 52 | render_time_ms(-1), |
| 53 | flags(VideoSendTiming::kNotTriggered) {} |
| 54 |
| 55 | int64_t TimingFrameInfo::EndToEndDelay() const { |
| 56 | return capture_time_ms >= 0 ? decode_finish_ms - capture_time_ms : -1; |
| 57 | } |
| 58 |
| 59 | bool TimingFrameInfo::IsLongerThan(const TimingFrameInfo& other) const { |
| 60 | int64_t other_delay = other.EndToEndDelay(); |
| 61 | return other_delay == -1 || EndToEndDelay() > other_delay; |
| 62 | } |
| 63 |
| 64 | bool TimingFrameInfo::operator<(const TimingFrameInfo& other) const { |
| 65 | return other.IsLongerThan(*this); |
| 66 | } |
| 67 |
| 68 | bool TimingFrameInfo::operator<=(const TimingFrameInfo& other) const { |
| 69 | return !IsLongerThan(other); |
| 70 | } |
| 71 |
| 72 | bool TimingFrameInfo::IsOutlier() const { |
| 73 | return !IsInvalid() && (flags & VideoSendTiming::kTriggeredBySize); |
| 74 | } |
| 75 |
| 76 | bool TimingFrameInfo::IsTimerTriggered() const { |
| 77 | return !IsInvalid() && (flags & VideoSendTiming::kTriggeredByTimer); |
| 78 | } |
| 79 |
| 80 | bool TimingFrameInfo::IsInvalid() const { |
| 81 | return flags == VideoSendTiming::kInvalid; |
| 82 | } |
| 83 |
| 84 | std::string TimingFrameInfo::ToString() const { |
| 85 | if (IsInvalid()) { |
| 86 | return ""; |
| 87 | } |
| 88 |
| 89 | char buf[1024]; |
| 90 | rtc::SimpleStringBuilder sb(buf); |
| 91 |
| 92 | sb << rtp_timestamp << ',' << capture_time_ms << ',' << encode_start_ms << ',' |
| 93 | << encode_finish_ms << ',' << packetization_finish_ms << ',' |
| 94 | << pacer_exit_ms << ',' << network_timestamp_ms << ',' |
| 95 | << network2_timestamp_ms << ',' << receive_start_ms << ',' |
| 96 | << receive_finish_ms << ',' << decode_start_ms << ',' << decode_finish_ms |
| 97 | << ',' << render_time_ms << ',' << IsOutlier() << ',' |
| 98 | << IsTimerTriggered(); |
| 99 |
| 100 | return sb.str(); |
| 101 | } |
| 102 |
| 103 | VideoPlayoutDelay::VideoPlayoutDelay(TimeDelta min, TimeDelta max) |
| 104 | : min_(std::clamp(min, TimeDelta::Zero(), kMax)), |
| 105 | max_(std::clamp(max, min_, kMax)) { |
| 106 | if (!(TimeDelta::Zero() <= min && min <= max && max <= kMax)) { |
| 107 | RTC_LOG(LS_ERROR) << "Invalid video playout delay: [" << min << "," << max |
| 108 | << "]. Clamped to [" << this->min() << "," << this->max() |
| 109 | << "]"; |
| 110 | } |
| 111 | } |
| 112 |
| 113 | bool VideoPlayoutDelay::Set(TimeDelta min, TimeDelta max) { |
| 114 | if (TimeDelta::Zero() <= min && min <= max && max <= kMax) { |
| 115 | min_ = min; |
| 116 | max_ = max; |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | } |
| 121 |
| 122 | } // namespace webrtc |
| 123 |