ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_VIDEO_VIDEO_TIMING_H_ |
| 12 | #define API_VIDEO_VIDEO_TIMING_H_ |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 13 | |
| 14 | #include <stdint.h> |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 15 | |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 16 | #include <limits> |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 17 | #include <string> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/checks.h" |
| 20 | #include "rtc_base/safe_conversions.h" |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 24 | enum TimingFrameFlags : uint8_t { |
Niels Möller | c241af9 | 2017-11-10 08:51:29 +0100 | [diff] [blame] | 25 | kNotTriggered = 0, // Timing info valid, but not to be transmitted. |
| 26 | // Used on send-side only. |
| 27 | // TODO(ilnik): Delete compatibility alias. |
| 28 | // Used to be sent over the wire, for the old protocol. |
| 29 | kDefault = 0, // Old name, for API compatibility. |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 30 | kTriggeredByTimer = 1 << 0, // Frame marked for tracing by periodic timer. |
| 31 | kTriggeredBySize = 1 << 1, // Frame marked for tracing due to size. |
| 32 | kInvalid = std::numeric_limits<uint8_t>::max() // Invalid, ignore! |
| 33 | }; |
| 34 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 35 | // Video timing timestamps in ms counted from capture_time_ms of a frame. |
| 36 | // This structure represents data sent in video-timing RTP header extension. |
| 37 | struct VideoSendTiming { |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 38 | // Offsets of the fields in the RTP header extension, counting from the first |
| 39 | // byte after the one-byte header. |
| 40 | static constexpr uint8_t kFlagsOffset = 0; |
| 41 | static constexpr uint8_t kEncodeStartDeltaOffset = 1; |
| 42 | static constexpr uint8_t kEncodeFinishDeltaOffset = 3; |
| 43 | static constexpr uint8_t kPacketizationFinishDeltaOffset = 5; |
| 44 | static constexpr uint8_t kPacerExitDeltaOffset = 7; |
| 45 | static constexpr uint8_t kNetworkTimestampDeltaOffset = 9; |
| 46 | static constexpr uint8_t kNetwork2TimestampDeltaOffset = 11; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 47 | |
| 48 | // Returns |time_ms - base_ms| capped at max 16-bit value. |
| 49 | // Used to fill this data structure as per |
| 50 | // https://webrtc.org/experiments/rtp-hdrext/video-timing/ extension stores |
| 51 | // 16-bit deltas of timestamps from packet capture time. |
| 52 | static uint16_t GetDeltaCappedMs(int64_t base_ms, int64_t time_ms) { |
| 53 | RTC_DCHECK_GE(time_ms, base_ms); |
| 54 | return rtc::saturated_cast<uint16_t>(time_ms - base_ms); |
| 55 | } |
| 56 | |
| 57 | uint16_t encode_start_delta_ms; |
| 58 | uint16_t encode_finish_delta_ms; |
| 59 | uint16_t packetization_finish_delta_ms; |
| 60 | uint16_t pacer_exit_delta_ms; |
Danil Chapovalov | 996eb9e | 2017-10-30 17:14:41 +0100 | [diff] [blame] | 61 | uint16_t network_timestamp_delta_ms; |
| 62 | uint16_t network2_timestamp_delta_ms; |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 63 | uint8_t flags; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 66 | // Used to report precise timings of a 'timing frames'. Contains all important |
| 67 | // timestamps for a lifetime of that specific frame. Reported as a string via |
| 68 | // GetStats(). Only frame which took the longest between two GetStats calls is |
| 69 | // reported. |
| 70 | struct TimingFrameInfo { |
| 71 | TimingFrameInfo(); |
| 72 | |
| 73 | // Returns end-to-end delay of a frame, if sender and receiver timestamps are |
| 74 | // synchronized, -1 otherwise. |
| 75 | int64_t EndToEndDelay() const; |
| 76 | |
| 77 | // Returns true if current frame took longer to process than |other| frame. |
| 78 | // If other frame's clocks are not synchronized, current frame is always |
| 79 | // preferred. |
| 80 | bool IsLongerThan(const TimingFrameInfo& other) const; |
| 81 | |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 82 | // Returns true if flags are set to indicate this frame was marked for tracing |
| 83 | // due to the size being outside some limit. |
| 84 | bool IsOutlier() const; |
| 85 | |
| 86 | // Returns true if flags are set to indicate this frame was marked fro tracing |
| 87 | // due to cyclic timer. |
| 88 | bool IsTimerTriggered() const; |
| 89 | |
| 90 | // Returns true if the timing data is marked as invalid, in which case it |
| 91 | // should be ignored. |
| 92 | bool IsInvalid() const; |
| 93 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 94 | std::string ToString() const; |
| 95 | |
ilnik | 75204c5 | 2017-09-04 03:35:40 -0700 | [diff] [blame] | 96 | bool operator<(const TimingFrameInfo& other) const; |
| 97 | |
| 98 | bool operator<=(const TimingFrameInfo& other) const; |
| 99 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 100 | uint32_t rtp_timestamp; // Identifier of a frame. |
| 101 | // All timestamps below are in local monotonous clock of a receiver. |
| 102 | // If sender clock is not yet estimated, sender timestamps |
| 103 | // (capture_time_ms ... pacer_exit_ms) are negative values, still |
| 104 | // relatively correct. |
| 105 | int64_t capture_time_ms; // Captrue time of a frame. |
| 106 | int64_t encode_start_ms; // Encode start time. |
| 107 | int64_t encode_finish_ms; // Encode completion time. |
| 108 | int64_t packetization_finish_ms; // Time when frame was passed to pacer. |
| 109 | int64_t pacer_exit_ms; // Time when last packet was pushed out of pacer. |
| 110 | // Two in-network RTP processor timestamps: meaning is application specific. |
| 111 | int64_t network_timestamp_ms; |
| 112 | int64_t network2_timestamp_ms; |
| 113 | int64_t receive_start_ms; // First received packet time. |
| 114 | int64_t receive_finish_ms; // Last received packet time. |
| 115 | int64_t decode_start_ms; // Decode start time. |
| 116 | int64_t decode_finish_ms; // Decode completion time. |
| 117 | int64_t render_time_ms; // Proposed render time to insure smooth playback. |
sprang | ba050a6 | 2017-08-18 02:51:12 -0700 | [diff] [blame] | 118 | |
| 119 | uint8_t flags; // Flags indicating validity and/or why tracing was triggered. |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 122 | } // namespace webrtc |
| 123 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 124 | #endif // API_VIDEO_VIDEO_TIMING_H_ |