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 #ifndef API_VIDEO_VIDEO_TIMING_H_
12 #define API_VIDEO_VIDEO_TIMING_H_
13 
14 #include <stdint.h>
15 
16 #include <limits>
17 #include <string>
18 
19 namespace webrtc {
20 
21 // Video timing timestamps in ms counted from capture_time_ms of a frame.
22 // This structure represents data sent in video-timing RTP header extension.
23 struct VideoSendTiming {
24   enum TimingFrameFlags : uint8_t {
25     kNotTriggered = 0,  // Timing info valid, but not to be transmitted.
26                         // Used on send-side only.
27     kTriggeredByTimer = 1 << 0,  // Frame marked for tracing by periodic timer.
28     kTriggeredBySize = 1 << 1,   // Frame marked for tracing due to size.
29     kInvalid = std::numeric_limits<uint8_t>::max()  // Invalid, ignore!
30   };
31 
32   // Returns |time_ms - base_ms| capped at max 16-bit value.
33   // Used to fill this data structure as per
34   // https://webrtc.org/experiments/rtp-hdrext/video-timing/ extension stores
35   // 16-bit deltas of timestamps from packet capture time.
36   static uint16_t GetDeltaCappedMs(int64_t base_ms, int64_t time_ms);
37 
38   uint16_t encode_start_delta_ms;
39   uint16_t encode_finish_delta_ms;
40   uint16_t packetization_finish_delta_ms;
41   uint16_t pacer_exit_delta_ms;
42   uint16_t network_timestamp_delta_ms;
43   uint16_t network2_timestamp_delta_ms;
44   uint8_t flags;
45 };
46 
47 // Used to report precise timings of a 'timing frames'. Contains all important
48 // timestamps for a lifetime of that specific frame. Reported as a string via
49 // GetStats(). Only frame which took the longest between two GetStats calls is
50 // reported.
51 struct TimingFrameInfo {
52   TimingFrameInfo();
53 
54   // Returns end-to-end delay of a frame, if sender and receiver timestamps are
55   // synchronized, -1 otherwise.
56   int64_t EndToEndDelay() const;
57 
58   // Returns true if current frame took longer to process than |other| frame.
59   // If other frame's clocks are not synchronized, current frame is always
60   // preferred.
61   bool IsLongerThan(const TimingFrameInfo& other) const;
62 
63   // Returns true if flags are set to indicate this frame was marked for tracing
64   // due to the size being outside some limit.
65   bool IsOutlier() const;
66 
67   // Returns true if flags are set to indicate this frame was marked fro tracing
68   // due to cyclic timer.
69   bool IsTimerTriggered() const;
70 
71   // Returns true if the timing data is marked as invalid, in which case it
72   // should be ignored.
73   bool IsInvalid() const;
74 
75   std::string ToString() const;
76 
77   bool operator<(const TimingFrameInfo& other) const;
78 
79   bool operator<=(const TimingFrameInfo& other) const;
80 
81   uint32_t rtp_timestamp;  // Identifier of a frame.
82   // All timestamps below are in local monotonous clock of a receiver.
83   // If sender clock is not yet estimated, sender timestamps
84   // (capture_time_ms ... pacer_exit_ms) are negative values, still
85   // relatively correct.
86   int64_t capture_time_ms;          // Captrue time of a frame.
87   int64_t encode_start_ms;          // Encode start time.
88   int64_t encode_finish_ms;         // Encode completion time.
89   int64_t packetization_finish_ms;  // Time when frame was passed to pacer.
90   int64_t pacer_exit_ms;  // Time when last packet was pushed out of pacer.
91   // Two in-network RTP processor timestamps: meaning is application specific.
92   int64_t network_timestamp_ms;
93   int64_t network2_timestamp_ms;
94   int64_t receive_start_ms;   // First received packet time.
95   int64_t receive_finish_ms;  // Last received packet time.
96   int64_t decode_start_ms;    // Decode start time.
97   int64_t decode_finish_ms;   // Decode completion time.
98   int64_t render_time_ms;     // Proposed render time to insure smooth playback.
99 
100   uint8_t flags;  // Flags indicating validity and/or why tracing was triggered.
101 };
102 
103 // Minimum and maximum playout delay values from capture to render.
104 // These are best effort values.
105 //
106 // A value < 0 indicates no change from previous valid value.
107 //
108 // min = max = 0 indicates that the receiver should try and render
109 // frame as soon as possible.
110 //
111 // min = x, max = y indicates that the receiver is free to adapt
112 // in the range (x, y) based on network jitter.
113 struct VideoPlayoutDelay {
114   VideoPlayoutDelay() = default;
VideoPlayoutDelayVideoPlayoutDelay115   VideoPlayoutDelay(int min_ms, int max_ms) : min_ms(min_ms), max_ms(max_ms) {}
116   int min_ms = -1;
117   int max_ms = -1;
118 
119   bool operator==(const VideoPlayoutDelay& rhs) const {
120     return min_ms == rhs.min_ms && max_ms == rhs.max_ms;
121   }
122 };
123 
124 // TODO(bugs.webrtc.org/7660): Old name, delete after downstream use is updated.
125 using PlayoutDelay = VideoPlayoutDelay;
126 
127 }  // namespace webrtc
128 
129 #endif  // API_VIDEO_VIDEO_TIMING_H_
130