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 "rtc_base/logging.h"
15 #include "rtc_base/numerics/safe_conversions.h"
16 #include "rtc_base/strings/string_builder.h"
17 
18 namespace webrtc {
19 
GetDeltaCappedMs(int64_t base_ms,int64_t time_ms)20 uint16_t VideoSendTiming::GetDeltaCappedMs(int64_t base_ms, int64_t time_ms) {
21   if (time_ms < base_ms) {
22     RTC_DLOG(LS_ERROR) << "Delta " << (time_ms - base_ms)
23                        << "ms expected to be positive";
24   }
25   return rtc::saturated_cast<uint16_t>(time_ms - base_ms);
26 }
27 
TimingFrameInfo()28 TimingFrameInfo::TimingFrameInfo()
29     : rtp_timestamp(0),
30       capture_time_ms(-1),
31       encode_start_ms(-1),
32       encode_finish_ms(-1),
33       packetization_finish_ms(-1),
34       pacer_exit_ms(-1),
35       network_timestamp_ms(-1),
36       network2_timestamp_ms(-1),
37       receive_start_ms(-1),
38       receive_finish_ms(-1),
39       decode_start_ms(-1),
40       decode_finish_ms(-1),
41       render_time_ms(-1),
42       flags(VideoSendTiming::kNotTriggered) {}
43 
EndToEndDelay() const44 int64_t TimingFrameInfo::EndToEndDelay() const {
45   return capture_time_ms >= 0 ? decode_finish_ms - capture_time_ms : -1;
46 }
47 
IsLongerThan(const TimingFrameInfo & other) const48 bool TimingFrameInfo::IsLongerThan(const TimingFrameInfo& other) const {
49   int64_t other_delay = other.EndToEndDelay();
50   return other_delay == -1 || EndToEndDelay() > other_delay;
51 }
52 
operator <(const TimingFrameInfo & other) const53 bool TimingFrameInfo::operator<(const TimingFrameInfo& other) const {
54   return other.IsLongerThan(*this);
55 }
56 
operator <=(const TimingFrameInfo & other) const57 bool TimingFrameInfo::operator<=(const TimingFrameInfo& other) const {
58   return !IsLongerThan(other);
59 }
60 
IsOutlier() const61 bool TimingFrameInfo::IsOutlier() const {
62   return !IsInvalid() && (flags & VideoSendTiming::kTriggeredBySize);
63 }
64 
IsTimerTriggered() const65 bool TimingFrameInfo::IsTimerTriggered() const {
66   return !IsInvalid() && (flags & VideoSendTiming::kTriggeredByTimer);
67 }
68 
IsInvalid() const69 bool TimingFrameInfo::IsInvalid() const {
70   return flags == VideoSendTiming::kInvalid;
71 }
72 
ToString() const73 std::string TimingFrameInfo::ToString() const {
74   if (IsInvalid()) {
75     return "";
76   }
77 
78   char buf[1024];
79   rtc::SimpleStringBuilder sb(buf);
80 
81   sb << rtp_timestamp << ',' << capture_time_ms << ',' << encode_start_ms << ','
82      << encode_finish_ms << ',' << packetization_finish_ms << ','
83      << pacer_exit_ms << ',' << network_timestamp_ms << ','
84      << network2_timestamp_ms << ',' << receive_start_ms << ','
85      << receive_finish_ms << ',' << decode_start_ms << ',' << decode_finish_ms
86      << ',' << render_time_ms << ',' << IsOutlier() << ','
87      << IsTimerTriggered();
88 
89   return sb.str();
90 }
91 
92 }  // namespace webrtc
93