niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
stefan@webrtc.org | 2979461 | 2012-02-08 08:58:55 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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_JITTER_BUFFER_H_ |
| 12 | #define MODULES_VIDEO_CODING_JITTER_BUFFER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
stefan@webrtc.org | 4cf1a8a | 2013-06-27 15:20:14 +0000 | [diff] [blame] | 14 | #include <list> |
stefan@webrtc.org | 50fb4af | 2013-06-17 07:33:58 +0000 | [diff] [blame] | 15 | #include <map> |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 16 | #include <memory> |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 17 | #include <set> |
stefan@webrtc.org | becf9c8 | 2013-02-01 15:09:57 +0000 | [diff] [blame] | 18 | #include <vector> |
stefan@webrtc.org | 2979461 | 2012-02-08 08:58:55 +0000 | [diff] [blame] | 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/include/module_common_types.h" |
| 21 | #include "modules/utility/include/process_thread.h" |
| 22 | #include "modules/video_coding/decoding_state.h" |
| 23 | #include "modules/video_coding/include/video_coding.h" |
| 24 | #include "modules/video_coding/include/video_coding_defines.h" |
| 25 | #include "modules/video_coding/inter_frame_delay.h" |
| 26 | #include "modules/video_coding/jitter_buffer_common.h" |
| 27 | #include "modules/video_coding/jitter_estimator.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/constructormagic.h" |
| 29 | #include "rtc_base/criticalsection.h" |
| 30 | #include "rtc_base/thread_annotations.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 31 | #include "typedefs.h" // NOLINT(build/include) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 33 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 35 | enum VCMNackMode { kNack, kNoNack }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | |
| 37 | // forward declarations |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 38 | class Clock; |
stefan@webrtc.org | 2baf5f5 | 2013-03-13 08:46:25 +0000 | [diff] [blame] | 39 | class EventFactory; |
| 40 | class EventWrapper; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | class VCMFrameBuffer; |
| 42 | class VCMPacket; |
| 43 | class VCMEncodedFrame; |
| 44 | |
stefan@webrtc.org | 4cf1a8a | 2013-06-27 15:20:14 +0000 | [diff] [blame] | 45 | typedef std::list<VCMFrameBuffer*> UnorderedFrameList; |
| 46 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 47 | struct VCMJitterSample { |
| 48 | VCMJitterSample() : timestamp(0), frame_size(0), latest_packet_time(-1) {} |
| 49 | uint32_t timestamp; |
| 50 | uint32_t frame_size; |
| 51 | int64_t latest_packet_time; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
stefan@webrtc.org | 50fb4af | 2013-06-17 07:33:58 +0000 | [diff] [blame] | 54 | class TimestampLessThan { |
| 55 | public: |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 56 | bool operator()(uint32_t timestamp1, uint32_t timestamp2) const { |
stefan@webrtc.org | 50fb4af | 2013-06-17 07:33:58 +0000 | [diff] [blame] | 57 | return IsNewerTimestamp(timestamp2, timestamp1); |
| 58 | } |
| 59 | }; |
| 60 | |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 +0000 | [diff] [blame] | 61 | class FrameList |
| 62 | : public std::map<uint32_t, VCMFrameBuffer*, TimestampLessThan> { |
stefan@webrtc.org | c8b29a2 | 2013-06-17 07:13:16 +0000 | [diff] [blame] | 63 | public: |
| 64 | void InsertFrame(VCMFrameBuffer* frame); |
stefan@webrtc.org | c8b29a2 | 2013-06-17 07:13:16 +0000 | [diff] [blame] | 65 | VCMFrameBuffer* PopFrame(uint32_t timestamp); |
stefan@webrtc.org | 50fb4af | 2013-06-17 07:33:58 +0000 | [diff] [blame] | 66 | VCMFrameBuffer* Front() const; |
| 67 | VCMFrameBuffer* Back() const; |
stefan@webrtc.org | 4cf1a8a | 2013-06-27 15:20:14 +0000 | [diff] [blame] | 68 | int RecycleFramesUntilKeyFrame(FrameList::iterator* key_frame_it, |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 69 | UnorderedFrameList* free_frames); |
pbos@webrtc.org | 4f16c87 | 2014-11-24 09:06:48 +0000 | [diff] [blame] | 70 | void CleanUpOldOrEmptyFrames(VCMDecodingState* decoding_state, |
| 71 | UnorderedFrameList* free_frames); |
stefan@webrtc.org | 4cf1a8a | 2013-06-27 15:20:14 +0000 | [diff] [blame] | 72 | void Reset(UnorderedFrameList* free_frames); |
stefan@webrtc.org | c8b29a2 | 2013-06-17 07:13:16 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
asapersson | 9a4cd87 | 2015-10-23 00:27:14 -0700 | [diff] [blame] | 75 | class Vp9SsMap { |
| 76 | public: |
| 77 | typedef std::map<uint32_t, GofInfoVP9, TimestampLessThan> SsMap; |
| 78 | bool Insert(const VCMPacket& packet); |
| 79 | void Reset(); |
| 80 | |
| 81 | // Removes SS data that are older than |timestamp|. |
| 82 | // The |timestamp| should be an old timestamp, i.e. packets with older |
| 83 | // timestamps should no longer be inserted. |
| 84 | void RemoveOld(uint32_t timestamp); |
| 85 | |
| 86 | bool UpdatePacket(VCMPacket* packet); |
| 87 | void UpdateFrames(FrameList* frames); |
| 88 | |
| 89 | // Public for testing. |
| 90 | // Returns an iterator to the corresponding SS data for the input |timestamp|. |
| 91 | bool Find(uint32_t timestamp, SsMap::iterator* it); |
| 92 | |
| 93 | private: |
| 94 | // These two functions are called by RemoveOld. |
| 95 | // Checks if it is time to do a clean up (done each kSsCleanupIntervalSec). |
| 96 | bool TimeForCleanup(uint32_t timestamp) const; |
| 97 | |
| 98 | // Advances the oldest SS data to handle timestamp wrap in cases where SS data |
| 99 | // are received very seldom (e.g. only once in beginning, second when |
| 100 | // IsNewerTimestamp is not true). |
| 101 | void AdvanceFront(uint32_t timestamp); |
| 102 | |
| 103 | SsMap ss_map_; |
| 104 | }; |
| 105 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 106 | class VCMJitterBuffer { |
| 107 | public: |
philipel | 83f831a | 2016-03-12 03:30:23 -0800 | [diff] [blame] | 108 | VCMJitterBuffer(Clock* clock, |
| 109 | std::unique_ptr<EventWrapper> event, |
| 110 | NackSender* nack_sender = nullptr, |
| 111 | KeyFrameRequestSender* keyframe_request_sender = nullptr); |
Qiang Chen | d4cec15 | 2015-06-19 09:17:00 -0700 | [diff] [blame] | 112 | |
Wan-Teh Chang | 6a1ba8c | 2015-05-26 14:11:41 -0700 | [diff] [blame] | 113 | ~VCMJitterBuffer(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 115 | // Initializes and starts jitter buffer. |
| 116 | void Start(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 118 | // Signals all internal events and stops the jitter buffer. |
| 119 | void Stop(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 121 | // Returns true if the jitter buffer is running. |
| 122 | bool Running() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 124 | // Empty the jitter buffer of all its data. |
| 125 | void Flush(); |
stefan@webrtc.org | 791eec7 | 2011-10-11 07:53:43 +0000 | [diff] [blame] | 126 | |
sprang@webrtc.org | 71f055f | 2013-12-04 15:09:27 +0000 | [diff] [blame] | 127 | // Get the number of received frames, by type, since the jitter buffer |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 128 | // was started. |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 129 | FrameCounts FrameStatistics() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 131 | // Gets number of packets received. |
| 132 | int num_packets() const; |
| 133 | |
| 134 | // Gets number of duplicated packets received. |
| 135 | int num_duplicated_packets() const; |
| 136 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 137 | // Gets number of packets discarded by the jitter buffer. |
| 138 | int num_discarded_packets() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 139 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 140 | // Statistics, Calculate frame and bit rates. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 141 | void IncomingRateStatistics(unsigned int* framerate, unsigned int* bitrate); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | |
mikhal@webrtc.org | 759b041 | 2013-05-07 16:36:00 +0000 | [diff] [blame] | 143 | // Wait |max_wait_time_ms| for a complete frame to arrive. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 144 | // If found, a pointer to the frame is returned. Returns nullptr otherwise. |
| 145 | VCMEncodedFrame* NextCompleteFrame(uint32_t max_wait_time_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | |
mikhal@webrtc.org | 759b041 | 2013-05-07 16:36:00 +0000 | [diff] [blame] | 147 | // Locates a frame for decoding (even an incomplete) without delay. |
| 148 | // The function returns true once such a frame is found, its corresponding |
| 149 | // timestamp is returned. Otherwise, returns false. |
| 150 | bool NextMaybeIncompleteTimestamp(uint32_t* timestamp); |
| 151 | |
| 152 | // Extract frame corresponding to input timestamp. |
| 153 | // Frame will be set to a decoding state. |
| 154 | VCMEncodedFrame* ExtractAndSetDecode(uint32_t timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 156 | // Releases a frame returned from the jitter buffer, should be called when |
| 157 | // done with decoding. |
| 158 | void ReleaseFrame(VCMEncodedFrame* frame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 160 | // Returns the time in ms when the latest packet was inserted into the frame. |
| 161 | // Retransmitted is set to true if any of the packets belonging to the frame |
| 162 | // has been retransmitted. |
stefan@webrtc.org | 3417eb4 | 2013-05-21 15:25:53 +0000 | [diff] [blame] | 163 | int64_t LastPacketTime(const VCMEncodedFrame* frame, |
| 164 | bool* retransmitted) const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 166 | // Inserts a packet into a frame returned from GetFrame(). |
stefan@webrtc.org | c8b29a2 | 2013-06-17 07:13:16 +0000 | [diff] [blame] | 167 | // If the return value is <= 0, |frame| is invalidated and the pointer must |
| 168 | // be dropped after this function returns. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 169 | VCMFrameBufferEnum InsertPacket(const VCMPacket& packet, bool* retransmitted); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 171 | // Returns the estimated jitter in milliseconds. |
| 172 | uint32_t EstimatedJitterMs(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 173 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 174 | // Updates the round-trip time estimate. |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 175 | void UpdateRtt(int64_t rtt_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | |
Wan-Teh Chang | 603175a | 2015-05-28 14:10:14 -0700 | [diff] [blame] | 177 | // Set the NACK mode. |high_rtt_nack_threshold_ms| is an RTT threshold in ms |
Wan-Teh Chang | f291287 | 2015-06-05 13:16:45 -0700 | [diff] [blame] | 178 | // above which NACK will be disabled if the NACK mode is |kNack|, -1 meaning |
| 179 | // that NACK is always enabled in the |kNack| mode. |
Wan-Teh Chang | 603175a | 2015-05-28 14:10:14 -0700 | [diff] [blame] | 180 | // |low_rtt_nack_threshold_ms| is an RTT threshold in ms below which we expect |
| 181 | // to rely on NACK only, and therefore are using larger buffers to have time |
| 182 | // to wait for retransmissions. |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 183 | void SetNackMode(VCMNackMode mode, |
| 184 | int64_t low_rtt_nack_threshold_ms, |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 185 | int64_t high_rtt_nack_threshold_ms); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | |
stefan@webrtc.org | becf9c8 | 2013-02-01 15:09:57 +0000 | [diff] [blame] | 187 | void SetNackSettings(size_t max_nack_list_size, |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 188 | int max_packet_age_to_nack, |
| 189 | int max_incomplete_time_ms); |
stefan@webrtc.org | becf9c8 | 2013-02-01 15:09:57 +0000 | [diff] [blame] | 190 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 191 | // Returns the current NACK mode. |
| 192 | VCMNackMode nack_mode() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 193 | |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 194 | // Returns a list of the sequence numbers currently missing. |
Wan-Teh Chang | b1825a4 | 2015-06-03 15:03:35 -0700 | [diff] [blame] | 195 | std::vector<uint16_t> GetNackList(bool* request_key_frame); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | |
mikhal@webrtc.org | 3c5a924 | 2013-09-03 20:45:36 +0000 | [diff] [blame] | 197 | // Set decode error mode - Should not be changed in the middle of the |
| 198 | // session. Changes will not influence frames already in the buffer. |
mikhal@webrtc.org | dbf6a81 | 2013-08-21 20:40:47 +0000 | [diff] [blame] | 199 | void SetDecodeErrorMode(VCMDecodeErrorMode error_mode); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 200 | VCMDecodeErrorMode decode_error_mode() const { return decode_error_mode_; } |
stefan@webrtc.org | 4c059d8 | 2011-10-13 07:35:37 +0000 | [diff] [blame] | 201 | |
pbos@webrtc.org | 5570769 | 2014-12-19 15:45:03 +0000 | [diff] [blame] | 202 | void RegisterStatsCallback(VCMReceiveStatisticsCallback* callback); |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 203 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 204 | private: |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 205 | class SequenceNumberLessThan { |
| 206 | public: |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 207 | bool operator()(const uint16_t& sequence_number1, |
| 208 | const uint16_t& sequence_number2) const { |
stefan@webrtc.org | 7bc465b | 2013-04-11 17:48:02 +0000 | [diff] [blame] | 209 | return IsNewerSequenceNumber(sequence_number2, sequence_number1); |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 210 | } |
| 211 | }; |
| 212 | typedef std::set<uint16_t, SequenceNumberLessThan> SequenceNumberSet; |
| 213 | |
stefan@webrtc.org | 3417eb4 | 2013-05-21 15:25:53 +0000 | [diff] [blame] | 214 | // Gets the frame assigned to the timestamp of the packet. May recycle |
| 215 | // existing frames if no free frames are available. Returns an error code if |
pbos@webrtc.org | 4f16c87 | 2014-11-24 09:06:48 +0000 | [diff] [blame] | 216 | // failing, or kNoError on success. |frame_list| contains which list the |
| 217 | // packet was in, or NULL if it was not in a FrameList (a new frame). |
| 218 | VCMFrameBufferEnum GetFrame(const VCMPacket& packet, |
| 219 | VCMFrameBuffer** frame, |
| 220 | FrameList** frame_list) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 221 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
asapersson@webrtc.org | 83b5200 | 2014-11-28 10:17:13 +0000 | [diff] [blame] | 222 | |
stefan@webrtc.org | c8b29a2 | 2013-06-17 07:13:16 +0000 | [diff] [blame] | 223 | // Returns true if |frame| is continuous in |decoding_state|, not taking |
| 224 | // decodable frames into account. |
| 225 | bool IsContinuousInState(const VCMFrameBuffer& frame, |
pbos@webrtc.org | 4f16c87 | 2014-11-24 09:06:48 +0000 | [diff] [blame] | 226 | const VCMDecodingState& decoding_state) const |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 227 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | c8b29a2 | 2013-06-17 07:13:16 +0000 | [diff] [blame] | 228 | // Returns true if |frame| is continuous in the |last_decoded_state_|, taking |
| 229 | // all decodable frames into account. |
pbos@webrtc.org | 4f16c87 | 2014-11-24 09:06:48 +0000 | [diff] [blame] | 230 | bool IsContinuous(const VCMFrameBuffer& frame) const |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 231 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
Noah Richards | e4cb4e9 | 2015-05-22 14:03:00 -0700 | [diff] [blame] | 232 | // Looks for frames in |incomplete_frames_| which are continuous in the |
| 233 | // provided |decoded_state|. Starts the search from the timestamp of |
| 234 | // |decoded_state|. |
| 235 | void FindAndInsertContinuousFramesWithState( |
| 236 | const VCMDecodingState& decoded_state) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 237 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | c8b29a2 | 2013-06-17 07:13:16 +0000 | [diff] [blame] | 238 | // Looks for frames in |incomplete_frames_| which are continuous in |
| 239 | // |last_decoded_state_| taking all decodable frames into account. Starts |
| 240 | // the search from |new_frame|. |
pbos@webrtc.org | 4f16c87 | 2014-11-24 09:06:48 +0000 | [diff] [blame] | 241 | void FindAndInsertContinuousFrames(const VCMFrameBuffer& new_frame) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 242 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
| 243 | VCMFrameBuffer* NextFrame() const RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 244 | // Returns true if the NACK list was updated to cover sequence numbers up to |
| 245 | // |sequence_number|. If false a key frame is needed to get into a state where |
| 246 | // we can continue decoding. |
pbos@webrtc.org | 4f16c87 | 2014-11-24 09:06:48 +0000 | [diff] [blame] | 247 | bool UpdateNackList(uint16_t sequence_number) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 248 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 249 | bool TooLargeNackList() const; |
| 250 | // Returns true if the NACK list was reduced without problem. If false a key |
| 251 | // frame is needed to get into a state where we can continue decoding. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 252 | bool HandleTooLargeNackList() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
pbos@webrtc.org | 4f16c87 | 2014-11-24 09:06:48 +0000 | [diff] [blame] | 253 | bool MissingTooOldPacket(uint16_t latest_sequence_number) const |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 254 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 255 | // Returns true if the too old packets was successfully removed from the NACK |
| 256 | // list. If false, a key frame is needed to get into a state where we can |
| 257 | // continue decoding. |
pbos@webrtc.org | 4f16c87 | 2014-11-24 09:06:48 +0000 | [diff] [blame] | 258 | bool HandleTooOldPackets(uint16_t latest_sequence_number) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 259 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 260 | // Drops all packets in the NACK list up until |last_decoded_sequence_number|. |
| 261 | void DropPacketsFromNackList(uint16_t last_decoded_sequence_number); |
| 262 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 263 | // Gets an empty frame, creating a new frame if necessary (i.e. increases |
| 264 | // jitter buffer size). |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 265 | VCMFrameBuffer* GetEmptyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 266 | |
stefan@webrtc.org | 4cf1a8a | 2013-06-27 15:20:14 +0000 | [diff] [blame] | 267 | // Attempts to increase the size of the jitter buffer. Returns true on |
| 268 | // success, false otherwise. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 269 | bool TryToIncreaseJitterBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | 4cf1a8a | 2013-06-27 15:20:14 +0000 | [diff] [blame] | 270 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 271 | // Recycles oldest frames until a key frame is found. Used if jitter buffer is |
| 272 | // completely full. Returns true if a key frame was found. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 273 | bool RecycleFramesUntilKeyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 274 | |
stefan@webrtc.org | 4cf1a8a | 2013-06-27 15:20:14 +0000 | [diff] [blame] | 275 | // Updates the frame statistics. |
agalusza@google.com | d177c10 | 2013-08-08 01:12:33 +0000 | [diff] [blame] | 276 | // Counts only complete frames, so decodable incomplete frames will not be |
| 277 | // counted. |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 278 | void CountFrame(const VCMFrameBuffer& frame) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 279 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | 791eec7 | 2011-10-11 07:53:43 +0000 | [diff] [blame] | 280 | |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 +0000 | [diff] [blame] | 281 | // Update rolling average of packets per frame. |
| 282 | void UpdateAveragePacketsPerFrame(int current_number_packets_); |
| 283 | |
mikhal@webrtc.org | 381da4b | 2013-04-25 21:45:29 +0000 | [diff] [blame] | 284 | // Cleans the frame list in the JB from old/empty frames. |
| 285 | // Should only be called prior to actual use. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 286 | void CleanUpOldOrEmptyFrames() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 288 | // Returns true if |packet| is likely to have been retransmitted. |
| 289 | bool IsPacketRetransmitted(const VCMPacket& packet) const; |
henrik.lundin@webrtc.org | baf6db5 | 2011-11-02 18:58:39 +0000 | [diff] [blame] | 290 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 291 | // The following three functions update the jitter estimate with the |
| 292 | // payload size, receive time and RTP timestamp of a frame. |
| 293 | void UpdateJitterEstimate(const VCMJitterSample& sample, |
| 294 | bool incomplete_frame); |
| 295 | void UpdateJitterEstimate(const VCMFrameBuffer& frame, bool incomplete_frame); |
| 296 | void UpdateJitterEstimate(int64_t latest_packet_time_ms, |
| 297 | uint32_t timestamp, |
| 298 | unsigned int frame_size, |
| 299 | bool incomplete_frame); |
| 300 | |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 301 | // Returns true if we should wait for retransmissions, false otherwise. |
| 302 | bool WaitForRetransmissions(); |
| 303 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 304 | int NonContinuousOrIncompleteDuration() |
| 305 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 306 | |
| 307 | uint16_t EstimatedLowSequenceNumber(const VCMFrameBuffer& frame) const; |
| 308 | |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 309 | void UpdateHistograms() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 310 | |
sprang | 22691e0 | 2016-07-13 10:57:07 -0700 | [diff] [blame] | 311 | // Reset frame buffer and return it to free_frames_. |
| 312 | void RecycleFrameBuffer(VCMFrameBuffer* frame) |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 313 | RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_); |
sprang | 22691e0 | 2016-07-13 10:57:07 -0700 | [diff] [blame] | 314 | |
stefan@webrtc.org | a678a3b | 2013-01-21 07:42:11 +0000 | [diff] [blame] | 315 | Clock* clock_; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 316 | // If we are running (have started) or not. |
| 317 | bool running_; |
kthelgason | ff046c7 | 2017-03-31 02:03:55 -0700 | [diff] [blame] | 318 | rtc::CriticalSection crit_sect_; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 319 | // Event to signal when we have a frame ready for decoder. |
kwiberg | 3f55dea | 2016-02-29 05:51:59 -0800 | [diff] [blame] | 320 | std::unique_ptr<EventWrapper> frame_event_; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 321 | // Number of allocated frames. |
| 322 | int max_number_of_frames_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 323 | UnorderedFrameList free_frames_ RTC_GUARDED_BY(crit_sect_); |
| 324 | FrameList decodable_frames_ RTC_GUARDED_BY(crit_sect_); |
| 325 | FrameList incomplete_frames_ RTC_GUARDED_BY(crit_sect_); |
| 326 | VCMDecodingState last_decoded_state_ RTC_GUARDED_BY(crit_sect_); |
stefan@webrtc.org | 3417eb4 | 2013-05-21 15:25:53 +0000 | [diff] [blame] | 327 | bool first_packet_since_reset_; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 328 | |
| 329 | // Statistics. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 330 | VCMReceiveStatisticsCallback* stats_callback_ RTC_GUARDED_BY(crit_sect_); |
sprang@webrtc.org | 71f055f | 2013-12-04 15:09:27 +0000 | [diff] [blame] | 331 | // Frame counts for each type (key, delta, ...) |
pbos@webrtc.org | ce4e9a3 | 2014-12-18 13:50:16 +0000 | [diff] [blame] | 332 | FrameCounts receive_statistics_; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 333 | // Latest calculated frame rates of incoming stream. |
| 334 | unsigned int incoming_frame_rate_; |
| 335 | unsigned int incoming_frame_count_; |
| 336 | int64_t time_last_incoming_frame_count_; |
| 337 | unsigned int incoming_bit_count_; |
| 338 | unsigned int incoming_bit_rate_; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 339 | // Number of packets in a row that have been too old. |
| 340 | int num_consecutive_old_packets_; |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 341 | // Number of packets received. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 342 | int num_packets_ RTC_GUARDED_BY(crit_sect_); |
asapersson@webrtc.org | 96dc685 | 2014-11-03 14:40:38 +0000 | [diff] [blame] | 343 | // Number of duplicated packets received. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 344 | int num_duplicated_packets_ RTC_GUARDED_BY(crit_sect_); |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 345 | // Number of packets discarded by the jitter buffer. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 346 | int num_discarded_packets_ RTC_GUARDED_BY(crit_sect_); |
asapersson@webrtc.org | 83b5200 | 2014-11-28 10:17:13 +0000 | [diff] [blame] | 347 | // Time when first packet is received. |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 348 | int64_t time_first_packet_ms_ RTC_GUARDED_BY(crit_sect_); |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 349 | |
| 350 | // Jitter estimation. |
| 351 | // Filter for estimating jitter. |
| 352 | VCMJitterEstimator jitter_estimate_; |
| 353 | // Calculates network delays used for jitter calculations. |
| 354 | VCMInterFrameDelay inter_frame_delay_; |
| 355 | VCMJitterSample waiting_for_completion_; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 356 | int64_t rtt_ms_; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 357 | |
| 358 | // NACK and retransmissions. |
| 359 | VCMNackMode nack_mode_; |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 360 | int64_t low_rtt_nack_threshold_ms_; |
| 361 | int64_t high_rtt_nack_threshold_ms_; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 362 | // Holds the internal NACK list (the missing sequence numbers). |
stefan@webrtc.org | a64300a | 2013-03-04 15:24:40 +0000 | [diff] [blame] | 363 | SequenceNumberSet missing_sequence_numbers_; |
| 364 | uint16_t latest_received_sequence_number_; |
stefan@webrtc.org | becf9c8 | 2013-02-01 15:09:57 +0000 | [diff] [blame] | 365 | size_t max_nack_list_size_; |
| 366 | int max_packet_age_to_nack_; // Measured in sequence numbers. |
stefan@webrtc.org | ef14488 | 2013-05-07 19:16:33 +0000 | [diff] [blame] | 367 | int max_incomplete_time_ms_; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 368 | |
agalusza@google.com | a7e360e | 2013-08-01 03:15:08 +0000 | [diff] [blame] | 369 | VCMDecodeErrorMode decode_error_mode_; |
agalusza@google.com | d818dcb | 2013-07-29 21:48:11 +0000 | [diff] [blame] | 370 | // Estimated rolling average of packets per frame |
| 371 | float average_packets_per_frame_; |
| 372 | // average_packets_per_frame converges fast if we have fewer than this many |
| 373 | // frames. |
| 374 | int frame_counter_; |
philipel | 83f831a | 2016-03-12 03:30:23 -0800 | [diff] [blame] | 375 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 376 | RTC_DISALLOW_COPY_AND_ASSIGN(VCMJitterBuffer); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 377 | }; |
stefan@webrtc.org | 912981f | 2012-10-12 07:04:52 +0000 | [diff] [blame] | 378 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 379 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 380 | #endif // MODULES_VIDEO_CODING_JITTER_BUFFER_H_ |