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