niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 MODULES_VIDEO_CODING_TIMING_H_ |
| 12 | #define MODULES_VIDEO_CODING_TIMING_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
magjed | 2943f01 | 2016-03-22 05:12:09 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/video_coding/codec_timer.h" |
| 17 | #include "rtc_base/criticalsection.h" |
| 18 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 19 | #include "typedefs.h" // NOLINT(build/include) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 21 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 23 | class Clock; |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 24 | class TimestampExtrapolator; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 26 | class VCMTiming { |
| 27 | public: |
| 28 | // The primary timing component should be passed |
| 29 | // if this is the dual timing component. |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 30 | explicit VCMTiming(Clock* clock, VCMTiming* master_timing = NULL); |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 31 | virtual ~VCMTiming(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 33 | // Resets the timing to the initial state. |
| 34 | void Reset(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 36 | // Set the amount of time needed to render an image. Defaults to 10 ms. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 37 | void set_render_delay(int render_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 39 | // Set the minimum time the video must be delayed on the receiver to |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 40 | // get the desired jitter buffer level. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 41 | void SetJitterDelay(int required_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame^] | 43 | // Set/get the minimum playout delay from capture to render in ms. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 44 | void set_min_playout_delay(int min_playout_delay_ms); |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 45 | int min_playout_delay(); |
| 46 | |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame^] | 47 | // Set/get the maximum playout delay from capture to render in ms. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 48 | void set_max_playout_delay(int max_playout_delay_ms); |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 49 | int max_playout_delay(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 51 | // Increases or decreases the current delay to get closer to the target delay. |
| 52 | // Calculates how long it has been since the previous call to this function, |
| 53 | // and increases/decreases the delay in proportion to the time difference. |
| 54 | void UpdateCurrentDelay(uint32_t frame_timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 56 | // Increases or decreases the current delay to get closer to the target delay. |
| 57 | // Given the actual decode time in ms and the render time in ms for a frame, |
| 58 | // this function calculates how late the frame is and increases the delay |
| 59 | // accordingly. |
| 60 | void UpdateCurrentDelay(int64_t render_time_ms, |
| 61 | int64_t actual_decode_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 63 | // Stops the decoder timer, should be called when the decoder returns a frame |
| 64 | // or when the decoded frame callback is called. |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame^] | 65 | void StopDecodeTimer(uint32_t time_stamp, |
| 66 | int32_t decode_time_ms, |
| 67 | int64_t now_ms, |
| 68 | int64_t render_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 70 | // Used to report that a frame is passed to decoding. Updates the timestamp |
| 71 | // filter which is used to map between timestamps and receiver system time. |
| 72 | void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms); |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame^] | 73 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 74 | // Returns the receiver system time when the frame with timestamp |
Åsa Persson | 8368d1a | 2018-01-05 12:44:45 +0100 | [diff] [blame^] | 75 | // |frame_timestamp| should be rendered, assuming that the system time |
| 76 | // currently is |now_ms|. |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 77 | virtual int64_t RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 79 | // Returns the maximum time in ms that we can wait for a frame to become |
| 80 | // complete before we must pass it to the decoder. |
philipel | be7a9e5 | 2016-05-19 12:19:35 +0200 | [diff] [blame] | 81 | virtual uint32_t MaxWaitingTime(int64_t render_time_ms, int64_t now_ms) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 83 | // Returns the current target delay which is required delay + decode time + |
| 84 | // render delay. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 85 | int TargetVideoDelay() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
asapersson | 8d56088 | 2016-12-22 01:26:18 -0800 | [diff] [blame] | 87 | // Return current timing information. Returns true if the first frame has been |
| 88 | // decoded, false otherwise. |
philipel | a45102f | 2017-02-22 05:30:39 -0800 | [diff] [blame] | 89 | virtual bool GetTimings(int* decode_ms, |
| 90 | int* max_decode_ms, |
| 91 | int* current_delay_ms, |
| 92 | int* target_delay_ms, |
| 93 | int* jitter_buffer_ms, |
| 94 | int* min_playout_delay_ms, |
| 95 | int* render_delay_ms) const; |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 96 | |
ilnik | 2edc684 | 2017-07-06 03:06:50 -0700 | [diff] [blame] | 97 | void SetTimingFrameInfo(const TimingFrameInfo& info); |
| 98 | rtc::Optional<TimingFrameInfo> GetTimingFrameInfo(); |
| 99 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 100 | enum { kDefaultRenderDelayMs = 10 }; |
| 101 | enum { kDelayMaxChangeMsPerS = 100 }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 102 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 103 | protected: |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 104 | int RequiredDecodeTimeMs() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
pbos@webrtc.org | 0422100 | 2014-07-10 15:25:37 +0000 | [diff] [blame] | 105 | int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 106 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 107 | int TargetDelayInternal() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 109 | private: |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 110 | void UpdateHistograms() const; |
| 111 | |
kthelgason | d701dfd | 2017-03-27 07:24:57 -0700 | [diff] [blame] | 112 | rtc::CriticalSection crit_sect_; |
pbos@webrtc.org | 0422100 | 2014-07-10 15:25:37 +0000 | [diff] [blame] | 113 | Clock* const clock_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 114 | bool master_ RTC_GUARDED_BY(crit_sect_); |
| 115 | TimestampExtrapolator* ts_extrapolator_ RTC_GUARDED_BY(crit_sect_); |
| 116 | std::unique_ptr<VCMCodecTimer> codec_timer_ RTC_GUARDED_BY(crit_sect_); |
| 117 | int render_delay_ms_ RTC_GUARDED_BY(crit_sect_); |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 118 | // Best-effort playout delay range for frames from capture to render. |
| 119 | // The receiver tries to keep the delay between |min_playout_delay_ms_| |
| 120 | // and |max_playout_delay_ms_| taking the network jitter into account. |
| 121 | // A special case is where min_playout_delay_ms_ = max_playout_delay_ms_ = 0, |
| 122 | // in which case the receiver tries to play the frames as they arrive. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 123 | int min_playout_delay_ms_ RTC_GUARDED_BY(crit_sect_); |
| 124 | int max_playout_delay_ms_ RTC_GUARDED_BY(crit_sect_); |
| 125 | int jitter_delay_ms_ RTC_GUARDED_BY(crit_sect_); |
| 126 | int current_delay_ms_ RTC_GUARDED_BY(crit_sect_); |
| 127 | int last_decode_ms_ RTC_GUARDED_BY(crit_sect_); |
| 128 | uint32_t prev_frame_timestamp_ RTC_GUARDED_BY(crit_sect_); |
| 129 | rtc::Optional<TimingFrameInfo> timing_frame_info_ RTC_GUARDED_BY(crit_sect_); |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 130 | |
| 131 | // Statistics. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 132 | size_t num_decoded_frames_ RTC_GUARDED_BY(crit_sect_); |
| 133 | size_t num_delayed_decoded_frames_ RTC_GUARDED_BY(crit_sect_); |
| 134 | int64_t first_decoded_frame_ms_ RTC_GUARDED_BY(crit_sect_); |
| 135 | uint64_t sum_missed_render_deadline_ms_ RTC_GUARDED_BY(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | }; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 137 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 138 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 139 | #endif // MODULES_VIDEO_CODING_TIMING_H_ |