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