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 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_VIDEO_CODING_TIMING_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_TIMING_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
pbos@webrtc.org | 38344ed | 2014-09-24 06:05:00 +0000 | [diff] [blame] | 14 | #include "webrtc/base/thread_annotations.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 15 | #include "webrtc/modules/video_coding/codec_timer.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 16 | #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 17 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 19 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 21 | class Clock; |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 22 | class TimestampExtrapolator; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 24 | class VCMTiming { |
| 25 | public: |
| 26 | // The primary timing component should be passed |
| 27 | // if this is the dual timing component. |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame^] | 28 | explicit VCMTiming(Clock* clock, VCMTiming* master_timing = NULL); |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 29 | ~VCMTiming(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 31 | // Resets the timing to the initial state. |
| 32 | void Reset(); |
| 33 | void ResetDecodeTime(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 35 | // Set the amount of time needed to render an image. Defaults to 10 ms. |
| 36 | void set_render_delay(uint32_t render_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 38 | // 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] | 39 | // get the desired jitter buffer level. |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 40 | void SetJitterDelay(uint32_t required_delay_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
mikhal@webrtc.org | adc64a7 | 2013-05-30 16:20:18 +0000 | [diff] [blame] | 42 | // Set the minimum playout delay required to sync video with audio. |
| 43 | void set_min_playout_delay(uint32_t min_playout_delay); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 45 | // Increases or decreases the current delay to get closer to the target delay. |
| 46 | // Calculates how long it has been since the previous call to this function, |
| 47 | // and increases/decreases the delay in proportion to the time difference. |
| 48 | void UpdateCurrentDelay(uint32_t frame_timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 49 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 50 | // Increases or decreases the current delay to get closer to the target delay. |
| 51 | // Given the actual decode time in ms and the render time in ms for a frame, |
| 52 | // this function calculates how late the frame is and increases the delay |
| 53 | // accordingly. |
| 54 | void UpdateCurrentDelay(int64_t render_time_ms, |
| 55 | int64_t actual_decode_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 57 | // Stops the decoder timer, should be called when the decoder returns a frame |
| 58 | // or when the decoded frame callback is called. |
| 59 | int32_t StopDecodeTimer(uint32_t time_stamp, |
Per | 327d8ba | 2015-11-10 14:00:27 +0100 | [diff] [blame] | 60 | int32_t decode_time_ms, |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 61 | int64_t now_ms, |
| 62 | int64_t render_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 64 | // Used to report that a frame is passed to decoding. Updates the timestamp |
| 65 | // filter which is used to map between timestamps and receiver system time. |
| 66 | void IncomingTimestamp(uint32_t time_stamp, int64_t last_packet_time_ms); |
| 67 | // Returns the receiver system time when the frame with timestamp |
| 68 | // frame_timestamp should be rendered, assuming that the system time currently |
| 69 | // is now_ms. |
| 70 | 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] | 71 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 72 | // Returns the maximum time in ms that we can wait for a frame to become |
| 73 | // complete before we must pass it to the decoder. |
| 74 | 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] | 75 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 76 | // Returns the current target delay which is required delay + decode time + |
| 77 | // render delay. |
| 78 | uint32_t TargetVideoDelay() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 80 | // Calculates whether or not there is enough time to decode a frame given a |
| 81 | // certain amount of processing time. |
| 82 | bool EnoughTimeToDecode(uint32_t available_processing_time_ms) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | |
fischman@webrtc.org | 37bb497 | 2013-10-23 23:59:45 +0000 | [diff] [blame] | 84 | // Return current timing information. |
| 85 | void GetTimings(int* decode_ms, |
| 86 | int* max_decode_ms, |
| 87 | int* current_delay_ms, |
| 88 | int* target_delay_ms, |
| 89 | int* jitter_buffer_ms, |
| 90 | int* min_playout_delay_ms, |
| 91 | int* render_delay_ms) const; |
| 92 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 93 | enum { kDefaultRenderDelayMs = 10 }; |
| 94 | enum { kDelayMaxChangeMsPerS = 100 }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 96 | protected: |
pbos@webrtc.org | 0422100 | 2014-07-10 15:25:37 +0000 | [diff] [blame] | 97 | int32_t MaxDecodeTimeMs(FrameType frame_type = kVideoFrameDelta) const |
| 98 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 99 | int64_t RenderTimeMsInternal(uint32_t frame_timestamp, int64_t now_ms) const |
| 100 | EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 101 | uint32_t TargetDelayInternal() const EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
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 | private: |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 104 | void UpdateHistograms() const; |
| 105 | |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 106 | CriticalSectionWrapper* crit_sect_; |
pbos@webrtc.org | 0422100 | 2014-07-10 15:25:37 +0000 | [diff] [blame] | 107 | Clock* const clock_; |
| 108 | bool master_ GUARDED_BY(crit_sect_); |
| 109 | TimestampExtrapolator* ts_extrapolator_ GUARDED_BY(crit_sect_); |
| 110 | VCMCodecTimer codec_timer_ GUARDED_BY(crit_sect_); |
| 111 | uint32_t render_delay_ms_ GUARDED_BY(crit_sect_); |
| 112 | uint32_t min_playout_delay_ms_ GUARDED_BY(crit_sect_); |
| 113 | uint32_t jitter_delay_ms_ GUARDED_BY(crit_sect_); |
| 114 | uint32_t current_delay_ms_ GUARDED_BY(crit_sect_); |
| 115 | int last_decode_ms_ GUARDED_BY(crit_sect_); |
| 116 | uint32_t prev_frame_timestamp_ GUARDED_BY(crit_sect_); |
asapersson@webrtc.org | f244760 | 2014-12-09 14:13:26 +0000 | [diff] [blame] | 117 | |
| 118 | // Statistics. |
| 119 | size_t num_decoded_frames_ GUARDED_BY(crit_sect_); |
| 120 | size_t num_delayed_decoded_frames_ GUARDED_BY(crit_sect_); |
| 121 | int64_t first_decoded_frame_ms_ GUARDED_BY(crit_sect_); |
| 122 | uint64_t sum_missed_render_deadline_ms_ GUARDED_BY(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | }; |
mikhal@webrtc.org | 2eaf98b | 2013-05-21 17:58:43 +0000 | [diff] [blame] | 124 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 126 | #endif // WEBRTC_MODULES_VIDEO_CODING_TIMING_H_ |