blob: 0cc03dd810761591227f77ea1b70ce08140c5314 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org29794612012-02-08 08:58:55 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Kjellander2557b862015-11-18 22:00:21 +010011#ifndef WEBRTC_MODULES_VIDEO_CODING_JITTER_BUFFER_H_
12#define WEBRTC_MODULES_VIDEO_CODING_JITTER_BUFFER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +000014#include <list>
stefan@webrtc.org50fb4af2013-06-17 07:33:58 +000015#include <map>
kwiberg3f55dea2016-02-29 05:51:59 -080016#include <memory>
stefan@webrtc.orga64300a2013-03-04 15:24:40 +000017#include <set>
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +000018#include <vector>
stefan@webrtc.org29794612012-02-08 08:58:55 +000019
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000020#include "webrtc/base/constructormagic.h"
asapersson@webrtc.org83b52002014-11-28 10:17:13 +000021#include "webrtc/base/thread_annotations.h"
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010022#include "webrtc/modules/include/module_common_types.h"
philipel83f831a2016-03-12 03:30:23 -080023#include "webrtc/modules/utility/include/process_thread.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010024#include "webrtc/modules/video_coding/include/video_coding.h"
25#include "webrtc/modules/video_coding/include/video_coding_defines.h"
26#include "webrtc/modules/video_coding/decoding_state.h"
27#include "webrtc/modules/video_coding/inter_frame_delay.h"
28#include "webrtc/modules/video_coding/jitter_buffer_common.h"
29#include "webrtc/modules/video_coding/jitter_estimator.h"
philipel83f831a2016-03-12 03:30:23 -080030#include "webrtc/modules/video_coding/nack_module.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010031#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
mikhal@webrtc.org119c67d2013-01-31 17:18:02 +000032#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000033
stefan@webrtc.org912981f2012-10-12 07:04:52 +000034namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000035
philipel9d3ab612015-12-21 04:12:39 -080036enum VCMNackMode { kNack, kNoNack };
niklase@google.com470e71d2011-07-07 08:21:25 +000037
38// forward declarations
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000039class Clock;
stefan@webrtc.org2baf5f52013-03-13 08:46:25 +000040class EventFactory;
41class EventWrapper;
niklase@google.com470e71d2011-07-07 08:21:25 +000042class VCMFrameBuffer;
43class VCMPacket;
44class VCMEncodedFrame;
45
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +000046typedef std::list<VCMFrameBuffer*> UnorderedFrameList;
47
stefan@webrtc.org912981f2012-10-12 07:04:52 +000048struct VCMJitterSample {
49 VCMJitterSample() : timestamp(0), frame_size(0), latest_packet_time(-1) {}
50 uint32_t timestamp;
51 uint32_t frame_size;
52 int64_t latest_packet_time;
niklase@google.com470e71d2011-07-07 08:21:25 +000053};
54
stefan@webrtc.org50fb4af2013-06-17 07:33:58 +000055class TimestampLessThan {
56 public:
philipel9d3ab612015-12-21 04:12:39 -080057 bool operator()(uint32_t timestamp1, uint32_t timestamp2) const {
stefan@webrtc.org50fb4af2013-06-17 07:33:58 +000058 return IsNewerTimestamp(timestamp2, timestamp1);
59 }
60};
61
agalusza@google.comd818dcb2013-07-29 21:48:11 +000062class FrameList
63 : public std::map<uint32_t, VCMFrameBuffer*, TimestampLessThan> {
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +000064 public:
65 void InsertFrame(VCMFrameBuffer* frame);
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +000066 VCMFrameBuffer* PopFrame(uint32_t timestamp);
stefan@webrtc.org50fb4af2013-06-17 07:33:58 +000067 VCMFrameBuffer* Front() const;
68 VCMFrameBuffer* Back() const;
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +000069 int RecycleFramesUntilKeyFrame(FrameList::iterator* key_frame_it,
philipel9d3ab612015-12-21 04:12:39 -080070 UnorderedFrameList* free_frames);
pbos@webrtc.org4f16c872014-11-24 09:06:48 +000071 void CleanUpOldOrEmptyFrames(VCMDecodingState* decoding_state,
72 UnorderedFrameList* free_frames);
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +000073 void Reset(UnorderedFrameList* free_frames);
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +000074};
75
asapersson9a4cd872015-10-23 00:27:14 -070076class Vp9SsMap {
77 public:
78 typedef std::map<uint32_t, GofInfoVP9, TimestampLessThan> SsMap;
79 bool Insert(const VCMPacket& packet);
80 void Reset();
81
82 // Removes SS data that are older than |timestamp|.
83 // The |timestamp| should be an old timestamp, i.e. packets with older
84 // timestamps should no longer be inserted.
85 void RemoveOld(uint32_t timestamp);
86
87 bool UpdatePacket(VCMPacket* packet);
88 void UpdateFrames(FrameList* frames);
89
90 // Public for testing.
91 // Returns an iterator to the corresponding SS data for the input |timestamp|.
92 bool Find(uint32_t timestamp, SsMap::iterator* it);
93
94 private:
95 // These two functions are called by RemoveOld.
96 // Checks if it is time to do a clean up (done each kSsCleanupIntervalSec).
97 bool TimeForCleanup(uint32_t timestamp) const;
98
99 // Advances the oldest SS data to handle timestamp wrap in cases where SS data
100 // are received very seldom (e.g. only once in beginning, second when
101 // IsNewerTimestamp is not true).
102 void AdvanceFront(uint32_t timestamp);
103
104 SsMap ss_map_;
105};
106
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000107class VCMJitterBuffer {
108 public:
philipel83f831a2016-03-12 03:30:23 -0800109 VCMJitterBuffer(Clock* clock,
110 std::unique_ptr<EventWrapper> event,
111 NackSender* nack_sender = nullptr,
112 KeyFrameRequestSender* keyframe_request_sender = nullptr);
Qiang Chend4cec152015-06-19 09:17:00 -0700113
Wan-Teh Chang6a1ba8c2015-05-26 14:11:41 -0700114 ~VCMJitterBuffer();
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000116 // Initializes and starts jitter buffer.
117 void Start();
niklase@google.com470e71d2011-07-07 08:21:25 +0000118
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000119 // Signals all internal events and stops the jitter buffer.
120 void Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000122 // Returns true if the jitter buffer is running.
123 bool Running() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000124
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000125 // Empty the jitter buffer of all its data.
126 void Flush();
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000127
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000128 // Get the number of received frames, by type, since the jitter buffer
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000129 // was started.
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000130 FrameCounts FrameStatistics() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000132 // The number of packets discarded by the jitter buffer because the decoder
133 // won't be able to decode them.
134 int num_not_decodable_packets() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000136 // Gets number of packets received.
137 int num_packets() const;
138
139 // Gets number of duplicated packets received.
140 int num_duplicated_packets() const;
141
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000142 // Gets number of packets discarded by the jitter buffer.
143 int num_discarded_packets() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000144
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000145 // Statistics, Calculate frame and bit rates.
philipel9d3ab612015-12-21 04:12:39 -0800146 void IncomingRateStatistics(unsigned int* framerate, unsigned int* bitrate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000147
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000148 // Checks if the packet sequence will be complete if the next frame would be
149 // grabbed for decoding. That is, if a frame has been lost between the
150 // last decoded frame and the next, or if the next frame is missing one
151 // or more packets.
152 bool CompleteSequenceWithNextFrame();
niklase@google.com470e71d2011-07-07 08:21:25 +0000153
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000154 // Wait |max_wait_time_ms| for a complete frame to arrive.
155 // The function returns true once such a frame is found, its corresponding
156 // timestamp is returned. Otherwise, returns false.
157 bool NextCompleteTimestamp(uint32_t max_wait_time_ms, uint32_t* timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000158
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000159 // Locates a frame for decoding (even an incomplete) without delay.
160 // The function returns true once such a frame is found, its corresponding
161 // timestamp is returned. Otherwise, returns false.
162 bool NextMaybeIncompleteTimestamp(uint32_t* timestamp);
163
164 // Extract frame corresponding to input timestamp.
165 // Frame will be set to a decoding state.
166 VCMEncodedFrame* ExtractAndSetDecode(uint32_t timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000168 // Releases a frame returned from the jitter buffer, should be called when
169 // done with decoding.
170 void ReleaseFrame(VCMEncodedFrame* frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000171
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000172 // Returns the time in ms when the latest packet was inserted into the frame.
173 // Retransmitted is set to true if any of the packets belonging to the frame
174 // has been retransmitted.
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000175 int64_t LastPacketTime(const VCMEncodedFrame* frame,
176 bool* retransmitted) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000178 // Inserts a packet into a frame returned from GetFrame().
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000179 // If the return value is <= 0, |frame| is invalidated and the pointer must
180 // be dropped after this function returns.
philipel9d3ab612015-12-21 04:12:39 -0800181 VCMFrameBufferEnum InsertPacket(const VCMPacket& packet, bool* retransmitted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000183 // Returns the estimated jitter in milliseconds.
184 uint32_t EstimatedJitterMs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000185
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000186 // Updates the round-trip time estimate.
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000187 void UpdateRtt(int64_t rtt_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
Wan-Teh Chang603175a2015-05-28 14:10:14 -0700189 // Set the NACK mode. |high_rtt_nack_threshold_ms| is an RTT threshold in ms
Wan-Teh Changf2912872015-06-05 13:16:45 -0700190 // above which NACK will be disabled if the NACK mode is |kNack|, -1 meaning
191 // that NACK is always enabled in the |kNack| mode.
Wan-Teh Chang603175a2015-05-28 14:10:14 -0700192 // |low_rtt_nack_threshold_ms| is an RTT threshold in ms below which we expect
193 // to rely on NACK only, and therefore are using larger buffers to have time
194 // to wait for retransmissions.
philipel9d3ab612015-12-21 04:12:39 -0800195 void SetNackMode(VCMNackMode mode,
196 int64_t low_rtt_nack_threshold_ms,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000197 int64_t high_rtt_nack_threshold_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000199 void SetNackSettings(size_t max_nack_list_size,
stefan@webrtc.orgef144882013-05-07 19:16:33 +0000200 int max_packet_age_to_nack,
201 int max_incomplete_time_ms);
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000202
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000203 // Returns the current NACK mode.
204 VCMNackMode nack_mode() const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000206 // Returns a list of the sequence numbers currently missing.
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700207 std::vector<uint16_t> GetNackList(bool* request_key_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000208
mikhal@webrtc.org3c5a9242013-09-03 20:45:36 +0000209 // Set decode error mode - Should not be changed in the middle of the
210 // session. Changes will not influence frames already in the buffer.
mikhal@webrtc.orgdbf6a812013-08-21 20:40:47 +0000211 void SetDecodeErrorMode(VCMDecodeErrorMode error_mode);
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000212 int64_t LastDecodedTimestamp() const;
philipel9d3ab612015-12-21 04:12:39 -0800213 VCMDecodeErrorMode decode_error_mode() const { return decode_error_mode_; }
stefan@webrtc.org4c059d82011-10-13 07:35:37 +0000214
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000215 // Used to compute time of complete continuous frames. Returns the timestamps
216 // corresponding to the start and end of the continuous complete buffer.
217 void RenderBufferSize(uint32_t* timestamp_start, uint32_t* timestamp_end);
mikhal@webrtc.org381da4b2013-04-25 21:45:29 +0000218
pbos@webrtc.org55707692014-12-19 15:45:03 +0000219 void RegisterStatsCallback(VCMReceiveStatisticsCallback* callback);
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000220
philipel83f831a2016-03-12 03:30:23 -0800221 int64_t TimeUntilNextProcess();
222 void Process();
223
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000224 private:
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000225 class SequenceNumberLessThan {
226 public:
philipel9d3ab612015-12-21 04:12:39 -0800227 bool operator()(const uint16_t& sequence_number1,
228 const uint16_t& sequence_number2) const {
stefan@webrtc.org7bc465b2013-04-11 17:48:02 +0000229 return IsNewerSequenceNumber(sequence_number2, sequence_number1);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000230 }
231 };
232 typedef std::set<uint16_t, SequenceNumberLessThan> SequenceNumberSet;
233
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000234 // Gets the frame assigned to the timestamp of the packet. May recycle
235 // existing frames if no free frames are available. Returns an error code if
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000236 // failing, or kNoError on success. |frame_list| contains which list the
237 // packet was in, or NULL if it was not in a FrameList (a new frame).
238 VCMFrameBufferEnum GetFrame(const VCMPacket& packet,
239 VCMFrameBuffer** frame,
240 FrameList** frame_list)
241 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000242
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000243 // Returns true if |frame| is continuous in |decoding_state|, not taking
244 // decodable frames into account.
245 bool IsContinuousInState(const VCMFrameBuffer& frame,
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000246 const VCMDecodingState& decoding_state) const
247 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000248 // Returns true if |frame| is continuous in the |last_decoded_state_|, taking
249 // all decodable frames into account.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000250 bool IsContinuous(const VCMFrameBuffer& frame) const
251 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
Noah Richardse4cb4e92015-05-22 14:03:00 -0700252 // Looks for frames in |incomplete_frames_| which are continuous in the
253 // provided |decoded_state|. Starts the search from the timestamp of
254 // |decoded_state|.
255 void FindAndInsertContinuousFramesWithState(
256 const VCMDecodingState& decoded_state)
257 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000258 // Looks for frames in |incomplete_frames_| which are continuous in
259 // |last_decoded_state_| taking all decodable frames into account. Starts
260 // the search from |new_frame|.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000261 void FindAndInsertContinuousFrames(const VCMFrameBuffer& new_frame)
262 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
263 VCMFrameBuffer* NextFrame() const EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000264 // Returns true if the NACK list was updated to cover sequence numbers up to
265 // |sequence_number|. If false a key frame is needed to get into a state where
266 // we can continue decoding.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000267 bool UpdateNackList(uint16_t sequence_number)
268 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000269 bool TooLargeNackList() const;
270 // Returns true if the NACK list was reduced without problem. If false a key
271 // frame is needed to get into a state where we can continue decoding.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000272 bool HandleTooLargeNackList() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
273 bool MissingTooOldPacket(uint16_t latest_sequence_number) const
274 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000275 // Returns true if the too old packets was successfully removed from the NACK
276 // list. If false, a key frame is needed to get into a state where we can
277 // continue decoding.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000278 bool HandleTooOldPackets(uint16_t latest_sequence_number)
279 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000280 // Drops all packets in the NACK list up until |last_decoded_sequence_number|.
281 void DropPacketsFromNackList(uint16_t last_decoded_sequence_number);
282
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000283 void ReleaseFrameIfNotDecoding(VCMFrameBuffer* frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000284
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000285 // Gets an empty frame, creating a new frame if necessary (i.e. increases
286 // jitter buffer size).
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000287 VCMFrameBuffer* GetEmptyFrame() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000288
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +0000289 // Attempts to increase the size of the jitter buffer. Returns true on
290 // success, false otherwise.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000291 bool TryToIncreaseJitterBufferSize() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +0000292
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000293 // Recycles oldest frames until a key frame is found. Used if jitter buffer is
294 // completely full. Returns true if a key frame was found.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000295 bool RecycleFramesUntilKeyFrame() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000296
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +0000297 // Updates the frame statistics.
agalusza@google.comd177c102013-08-08 01:12:33 +0000298 // Counts only complete frames, so decodable incomplete frames will not be
299 // counted.
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000300 void CountFrame(const VCMFrameBuffer& frame)
301 EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.org791eec72011-10-11 07:53:43 +0000302
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000303 // Update rolling average of packets per frame.
304 void UpdateAveragePacketsPerFrame(int current_number_packets_);
305
mikhal@webrtc.org381da4b2013-04-25 21:45:29 +0000306 // Cleans the frame list in the JB from old/empty frames.
307 // Should only be called prior to actual use.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000308 void CleanUpOldOrEmptyFrames() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000309
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000310 // Returns true if |packet| is likely to have been retransmitted.
311 bool IsPacketRetransmitted(const VCMPacket& packet) const;
henrik.lundin@webrtc.orgbaf6db52011-11-02 18:58:39 +0000312
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000313 // The following three functions update the jitter estimate with the
314 // payload size, receive time and RTP timestamp of a frame.
315 void UpdateJitterEstimate(const VCMJitterSample& sample,
316 bool incomplete_frame);
317 void UpdateJitterEstimate(const VCMFrameBuffer& frame, bool incomplete_frame);
318 void UpdateJitterEstimate(int64_t latest_packet_time_ms,
319 uint32_t timestamp,
320 unsigned int frame_size,
321 bool incomplete_frame);
322
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000323 // Returns true if we should wait for retransmissions, false otherwise.
324 bool WaitForRetransmissions();
325
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000326 int NonContinuousOrIncompleteDuration() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
stefan@webrtc.orgef144882013-05-07 19:16:33 +0000327
328 uint16_t EstimatedLowSequenceNumber(const VCMFrameBuffer& frame) const;
329
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000330 void UpdateHistograms() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000331
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000332 Clock* clock_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000333 // If we are running (have started) or not.
334 bool running_;
335 CriticalSectionWrapper* crit_sect_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000336 // Event to signal when we have a frame ready for decoder.
kwiberg3f55dea2016-02-29 05:51:59 -0800337 std::unique_ptr<EventWrapper> frame_event_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000338 // Number of allocated frames.
339 int max_number_of_frames_;
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000340 UnorderedFrameList free_frames_ GUARDED_BY(crit_sect_);
341 FrameList decodable_frames_ GUARDED_BY(crit_sect_);
342 FrameList incomplete_frames_ GUARDED_BY(crit_sect_);
343 VCMDecodingState last_decoded_state_ GUARDED_BY(crit_sect_);
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000344 bool first_packet_since_reset_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000345
346 // Statistics.
pbos@webrtc.org55707692014-12-19 15:45:03 +0000347 VCMReceiveStatisticsCallback* stats_callback_ GUARDED_BY(crit_sect_);
sprang@webrtc.org71f055f2013-12-04 15:09:27 +0000348 // Frame counts for each type (key, delta, ...)
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +0000349 FrameCounts receive_statistics_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000350 // Latest calculated frame rates of incoming stream.
351 unsigned int incoming_frame_rate_;
352 unsigned int incoming_frame_count_;
353 int64_t time_last_incoming_frame_count_;
354 unsigned int incoming_bit_count_;
355 unsigned int incoming_bit_rate_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000356 // Number of frames in a row that have been too old.
357 int num_consecutive_old_frames_;
358 // Number of packets in a row that have been too old.
359 int num_consecutive_old_packets_;
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000360 // Number of packets received.
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000361 int num_packets_ GUARDED_BY(crit_sect_);
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000362 // Number of duplicated packets received.
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000363 int num_duplicated_packets_ GUARDED_BY(crit_sect_);
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000364 // Number of packets discarded by the jitter buffer.
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000365 int num_discarded_packets_ GUARDED_BY(crit_sect_);
366 // Time when first packet is received.
367 int64_t time_first_packet_ms_ GUARDED_BY(crit_sect_);
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000368
369 // Jitter estimation.
370 // Filter for estimating jitter.
371 VCMJitterEstimator jitter_estimate_;
372 // Calculates network delays used for jitter calculations.
373 VCMInterFrameDelay inter_frame_delay_;
374 VCMJitterSample waiting_for_completion_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000375 int64_t rtt_ms_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000376
377 // NACK and retransmissions.
378 VCMNackMode nack_mode_;
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000379 int64_t low_rtt_nack_threshold_ms_;
380 int64_t high_rtt_nack_threshold_ms_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000381 // Holds the internal NACK list (the missing sequence numbers).
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000382 SequenceNumberSet missing_sequence_numbers_;
383 uint16_t latest_received_sequence_number_;
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000384 size_t max_nack_list_size_;
385 int max_packet_age_to_nack_; // Measured in sequence numbers.
stefan@webrtc.orgef144882013-05-07 19:16:33 +0000386 int max_incomplete_time_ms_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000387
agalusza@google.coma7e360e2013-08-01 03:15:08 +0000388 VCMDecodeErrorMode decode_error_mode_;
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000389 // Estimated rolling average of packets per frame
390 float average_packets_per_frame_;
391 // average_packets_per_frame converges fast if we have fewer than this many
392 // frames.
393 int frame_counter_;
philipel83f831a2016-03-12 03:30:23 -0800394
395 std::unique_ptr<NackModule> nack_module_;
396
henrikg3c089d72015-09-16 05:37:44 -0700397 RTC_DISALLOW_COPY_AND_ASSIGN(VCMJitterBuffer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000398};
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000399} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000400
Henrik Kjellander2557b862015-11-18 22:00:21 +0100401#endif // WEBRTC_MODULES_VIDEO_CODING_JITTER_BUFFER_H_