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