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 "api/array_view.h"
14 #include "api/units/time_delta.h"
15 #include "rtc_base/logging.h"
16 #include "rtc_base/numerics/safe_conversions.h"
17 #include "rtc_base/strings/string_builder.h"
18
19 namespace webrtc {
20
GetDeltaCappedMs(int64_t base_ms,int64_t time_ms)21 uint16_t VideoSendTiming::GetDeltaCappedMs(int64_t base_ms, int64_t time_ms) {
22 if (time_ms < base_ms) {
23 RTC_DLOG(LS_ERROR) << "Delta " << (time_ms - base_ms)
24 << "ms expected to be positive";
25 }
26 return rtc::saturated_cast<uint16_t>(time_ms - base_ms);
27 }
28
GetDeltaCappedMs(TimeDelta delta)29 uint16_t VideoSendTiming::GetDeltaCappedMs(TimeDelta delta) {
30 if (delta < TimeDelta::Zero()) {
31 RTC_DLOG(LS_ERROR) << "Delta " << delta.ms()
32 << "ms expected to be positive";
33 }
34 return rtc::saturated_cast<uint16_t>(delta.ms());
35 }
36
TimingFrameInfo()37 TimingFrameInfo::TimingFrameInfo()
38 : rtp_timestamp(0),
39 capture_time_ms(-1),
40 encode_start_ms(-1),
41 encode_finish_ms(-1),
42 packetization_finish_ms(-1),
43 pacer_exit_ms(-1),
44 network_timestamp_ms(-1),
45 network2_timestamp_ms(-1),
46 receive_start_ms(-1),
47 receive_finish_ms(-1),
48 decode_start_ms(-1),
49 decode_finish_ms(-1),
50 render_time_ms(-1),
51 flags(VideoSendTiming::kNotTriggered) {}
52
EndToEndDelay() const53 int64_t TimingFrameInfo::EndToEndDelay() const {
54 return capture_time_ms >= 0 ? decode_finish_ms - capture_time_ms : -1;
55 }
56
IsLongerThan(const TimingFrameInfo & other) const57 bool TimingFrameInfo::IsLongerThan(const TimingFrameInfo& other) const {
58 int64_t other_delay = other.EndToEndDelay();
59 return other_delay == -1 || EndToEndDelay() > other_delay;
60 }
61
operator <(const TimingFrameInfo & other) const62 bool TimingFrameInfo::operator<(const TimingFrameInfo& other) const {
63 return other.IsLongerThan(*this);
64 }
65
operator <=(const TimingFrameInfo & other) const66 bool TimingFrameInfo::operator<=(const TimingFrameInfo& other) const {
67 return !IsLongerThan(other);
68 }
69
IsOutlier() const70 bool TimingFrameInfo::IsOutlier() const {
71 return !IsInvalid() && (flags & VideoSendTiming::kTriggeredBySize);
72 }
73
IsTimerTriggered() const74 bool TimingFrameInfo::IsTimerTriggered() const {
75 return !IsInvalid() && (flags & VideoSendTiming::kTriggeredByTimer);
76 }
77
IsInvalid() const78 bool TimingFrameInfo::IsInvalid() const {
79 return flags == VideoSendTiming::kInvalid;
80 }
81
ToString() const82 std::string TimingFrameInfo::ToString() const {
83 if (IsInvalid()) {
84 return "";
85 }
86
87 char buf[1024];
88 rtc::SimpleStringBuilder sb(buf);
89
90 sb << rtp_timestamp << ',' << capture_time_ms << ',' << encode_start_ms << ','
91 << encode_finish_ms << ',' << packetization_finish_ms << ','
92 << pacer_exit_ms << ',' << network_timestamp_ms << ','
93 << network2_timestamp_ms << ',' << receive_start_ms << ','
94 << receive_finish_ms << ',' << decode_start_ms << ',' << decode_finish_ms
95 << ',' << render_time_ms << ',' << IsOutlier() << ','
96 << IsTimerTriggered();
97
98 return sb.str();
99 }
100
101 } // namespace webrtc
102