blob: 72feffdcd4389a5681faacab0beaaece65060fe0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_JITTER_BUFFER_H_
12#define 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
Jonas Orelande62c2f22022-03-29 11:04:48 +020020#include "api/field_trials_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/include/module_common_types.h"
Danil Chapovalov7c067772019-10-07 12:56:24 +020022#include "modules/include/module_common_types_public.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/video_coding/decoding_state.h"
Niels Möllerd3da6b02020-03-05 15:31:10 +010024#include "modules/video_coding/event_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/video_coding/include/video_coding.h"
26#include "modules/video_coding/include/video_coding_defines.h"
27#include "modules/video_coding/inter_frame_delay.h"
28#include "modules/video_coding/jitter_buffer_common.h"
29#include "modules/video_coding/jitter_estimator.h"
Markus Handell6deec382020-07-07 12:17:12 +020030#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/thread_annotations.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000032
stefan@webrtc.org912981f2012-10-12 07:04:52 +000033namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000034
niklase@google.com470e71d2011-07-07 08:21:25 +000035// forward declarations
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +000036class Clock;
niklase@google.com470e71d2011-07-07 08:21:25 +000037class VCMFrameBuffer;
38class VCMPacket;
39class VCMEncodedFrame;
40
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +000041typedef std::list<VCMFrameBuffer*> UnorderedFrameList;
42
stefan@webrtc.org912981f2012-10-12 07:04:52 +000043struct VCMJitterSample {
44 VCMJitterSample() : timestamp(0), frame_size(0), latest_packet_time(-1) {}
45 uint32_t timestamp;
46 uint32_t frame_size;
47 int64_t latest_packet_time;
niklase@google.com470e71d2011-07-07 08:21:25 +000048};
49
stefan@webrtc.org50fb4af2013-06-17 07:33:58 +000050class TimestampLessThan {
51 public:
philipel9d3ab612015-12-21 04:12:39 -080052 bool operator()(uint32_t timestamp1, uint32_t timestamp2) const {
stefan@webrtc.org50fb4af2013-06-17 07:33:58 +000053 return IsNewerTimestamp(timestamp2, timestamp1);
54 }
55};
56
agalusza@google.comd818dcb2013-07-29 21:48:11 +000057class FrameList
58 : public std::map<uint32_t, VCMFrameBuffer*, TimestampLessThan> {
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +000059 public:
60 void InsertFrame(VCMFrameBuffer* frame);
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +000061 VCMFrameBuffer* PopFrame(uint32_t timestamp);
stefan@webrtc.org50fb4af2013-06-17 07:33:58 +000062 VCMFrameBuffer* Front() const;
63 VCMFrameBuffer* Back() const;
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +000064 int RecycleFramesUntilKeyFrame(FrameList::iterator* key_frame_it,
philipel9d3ab612015-12-21 04:12:39 -080065 UnorderedFrameList* free_frames);
pbos@webrtc.org4f16c872014-11-24 09:06:48 +000066 void CleanUpOldOrEmptyFrames(VCMDecodingState* decoding_state,
67 UnorderedFrameList* free_frames);
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +000068 void Reset(UnorderedFrameList* free_frames);
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +000069};
70
stefan@webrtc.org912981f2012-10-12 07:04:52 +000071class VCMJitterBuffer {
72 public:
Jonas Orelande02f9ee2022-03-25 12:43:14 +010073 VCMJitterBuffer(Clock* clock,
74 std::unique_ptr<EventWrapper> event,
Jonas Orelande62c2f22022-03-29 11:04:48 +020075 const FieldTrialsView& field_trials);
Qiang Chend4cec152015-06-19 09:17:00 -070076
Wan-Teh Chang6a1ba8c2015-05-26 14:11:41 -070077 ~VCMJitterBuffer();
niklase@google.com470e71d2011-07-07 08:21:25 +000078
Byoungchan Lee604fd2f2022-01-21 09:49:39 +090079 VCMJitterBuffer(const VCMJitterBuffer&) = delete;
80 VCMJitterBuffer& operator=(const VCMJitterBuffer&) = delete;
81
stefan@webrtc.org912981f2012-10-12 07:04:52 +000082 // Initializes and starts jitter buffer.
83 void Start();
niklase@google.com470e71d2011-07-07 08:21:25 +000084
stefan@webrtc.org912981f2012-10-12 07:04:52 +000085 // Signals all internal events and stops the jitter buffer.
86 void Stop();
niklase@google.com470e71d2011-07-07 08:21:25 +000087
stefan@webrtc.org912981f2012-10-12 07:04:52 +000088 // Returns true if the jitter buffer is running.
89 bool Running() const;
niklase@google.com470e71d2011-07-07 08:21:25 +000090
stefan@webrtc.org912981f2012-10-12 07:04:52 +000091 // Empty the jitter buffer of all its data.
92 void Flush();
stefan@webrtc.org791eec72011-10-11 07:53:43 +000093
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +000094 // Gets number of packets received.
95 int num_packets() const;
96
97 // Gets number of duplicated packets received.
98 int num_duplicated_packets() const;
99
Artem Titovdcd7fc72021-08-09 13:02:57 +0200100 // Wait `max_wait_time_ms` for a complete frame to arrive.
isheriff6b4b5f32016-06-08 00:24:21 -0700101 // If found, a pointer to the frame is returned. Returns nullptr otherwise.
102 VCMEncodedFrame* NextCompleteFrame(uint32_t max_wait_time_ms);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
mikhal@webrtc.org759b0412013-05-07 16:36:00 +0000104 // Extract frame corresponding to input timestamp.
105 // Frame will be set to a decoding state.
106 VCMEncodedFrame* ExtractAndSetDecode(uint32_t timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000108 // Releases a frame returned from the jitter buffer, should be called when
109 // done with decoding.
110 void ReleaseFrame(VCMEncodedFrame* frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000112 // Returns the time in ms when the latest packet was inserted into the frame.
113 // Retransmitted is set to true if any of the packets belonging to the frame
114 // has been retransmitted.
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000115 int64_t LastPacketTime(const VCMEncodedFrame* frame,
116 bool* retransmitted) const;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000118 // Inserts a packet into a frame returned from GetFrame().
Artem Titovdcd7fc72021-08-09 13:02:57 +0200119 // If the return value is <= 0, `frame` is invalidated and the pointer must
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000120 // be dropped after this function returns.
philipel9d3ab612015-12-21 04:12:39 -0800121 VCMFrameBufferEnum InsertPacket(const VCMPacket& packet, bool* retransmitted);
niklase@google.com470e71d2011-07-07 08:21:25 +0000122
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000123 // Returns the estimated jitter in milliseconds.
124 uint32_t EstimatedJitterMs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000125
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000126 void SetNackSettings(size_t max_nack_list_size,
stefan@webrtc.orgef144882013-05-07 19:16:33 +0000127 int max_packet_age_to_nack,
128 int max_incomplete_time_ms);
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000129
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000130 // Returns a list of the sequence numbers currently missing.
Wan-Teh Changb1825a42015-06-03 15:03:35 -0700131 std::vector<uint16_t> GetNackList(bool* request_key_frame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000133 private:
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000134 class SequenceNumberLessThan {
135 public:
philipel9d3ab612015-12-21 04:12:39 -0800136 bool operator()(const uint16_t& sequence_number1,
137 const uint16_t& sequence_number2) const {
stefan@webrtc.org7bc465b2013-04-11 17:48:02 +0000138 return IsNewerSequenceNumber(sequence_number2, sequence_number1);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000139 }
140 };
141 typedef std::set<uint16_t, SequenceNumberLessThan> SequenceNumberSet;
142
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000143 // Gets the frame assigned to the timestamp of the packet. May recycle
144 // existing frames if no free frames are available. Returns an error code if
Artem Titovdcd7fc72021-08-09 13:02:57 +0200145 // failing, or kNoError on success. `frame_list` contains which list the
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000146 // packet was in, or NULL if it was not in a FrameList (a new frame).
147 VCMFrameBufferEnum GetFrame(const VCMPacket& packet,
148 VCMFrameBuffer** frame,
149 FrameList** frame_list)
Markus Handell6deec382020-07-07 12:17:12 +0200150 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
asapersson@webrtc.org83b52002014-11-28 10:17:13 +0000151
Artem Titovdcd7fc72021-08-09 13:02:57 +0200152 // Returns true if `frame` is continuous in `decoding_state`, not taking
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000153 // decodable frames into account.
154 bool IsContinuousInState(const VCMFrameBuffer& frame,
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000155 const VCMDecodingState& decoding_state) const
Markus Handell6deec382020-07-07 12:17:12 +0200156 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Artem Titovdcd7fc72021-08-09 13:02:57 +0200157 // Returns true if `frame` is continuous in the `last_decoded_state_`, taking
stefan@webrtc.orgc8b29a22013-06-17 07:13:16 +0000158 // all decodable frames into account.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000159 bool IsContinuous(const VCMFrameBuffer& frame) const
Markus Handell6deec382020-07-07 12:17:12 +0200160 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Artem Titovdcd7fc72021-08-09 13:02:57 +0200161 // Looks for frames in `incomplete_frames_` which are continuous in the
162 // provided `decoded_state`. Starts the search from the timestamp of
163 // `decoded_state`.
Noah Richardse4cb4e92015-05-22 14:03:00 -0700164 void FindAndInsertContinuousFramesWithState(
165 const VCMDecodingState& decoded_state)
Markus Handell6deec382020-07-07 12:17:12 +0200166 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Artem Titovdcd7fc72021-08-09 13:02:57 +0200167 // Looks for frames in `incomplete_frames_` which are continuous in
168 // `last_decoded_state_` taking all decodable frames into account. Starts
169 // the search from `new_frame`.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000170 void FindAndInsertContinuousFrames(const VCMFrameBuffer& new_frame)
Markus Handell6deec382020-07-07 12:17:12 +0200171 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
172 VCMFrameBuffer* NextFrame() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000173 // Returns true if the NACK list was updated to cover sequence numbers up to
Artem Titovdcd7fc72021-08-09 13:02:57 +0200174 // `sequence_number`. If false a key frame is needed to get into a state where
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000175 // we can continue decoding.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000176 bool UpdateNackList(uint16_t sequence_number)
Markus Handell6deec382020-07-07 12:17:12 +0200177 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000178 bool TooLargeNackList() const;
179 // Returns true if the NACK list was reduced without problem. If false a key
180 // frame is needed to get into a state where we can continue decoding.
Markus Handell6deec382020-07-07 12:17:12 +0200181 bool HandleTooLargeNackList() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000182 bool MissingTooOldPacket(uint16_t latest_sequence_number) const
Markus Handell6deec382020-07-07 12:17:12 +0200183 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000184 // Returns true if the too old packets was successfully removed from the NACK
185 // list. If false, a key frame is needed to get into a state where we can
186 // continue decoding.
pbos@webrtc.org4f16c872014-11-24 09:06:48 +0000187 bool HandleTooOldPackets(uint16_t latest_sequence_number)
Markus Handell6deec382020-07-07 12:17:12 +0200188 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
Artem Titovdcd7fc72021-08-09 13:02:57 +0200189 // Drops all packets in the NACK list up until `last_decoded_sequence_number`.
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000190 void DropPacketsFromNackList(uint16_t last_decoded_sequence_number);
191
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000192 // Gets an empty frame, creating a new frame if necessary (i.e. increases
193 // jitter buffer size).
Markus Handell6deec382020-07-07 12:17:12 +0200194 VCMFrameBuffer* GetEmptyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000195
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +0000196 // Attempts to increase the size of the jitter buffer. Returns true on
197 // success, false otherwise.
Markus Handell6deec382020-07-07 12:17:12 +0200198 bool TryToIncreaseJitterBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
stefan@webrtc.org4cf1a8a2013-06-27 15:20:14 +0000199
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000200 // Recycles oldest frames until a key frame is found. Used if jitter buffer is
201 // completely full. Returns true if a key frame was found.
Markus Handell6deec382020-07-07 12:17:12 +0200202 bool RecycleFramesUntilKeyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000203
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000204 // Update rolling average of packets per frame.
205 void UpdateAveragePacketsPerFrame(int current_number_packets_);
206
mikhal@webrtc.org381da4b2013-04-25 21:45:29 +0000207 // Cleans the frame list in the JB from old/empty frames.
208 // Should only be called prior to actual use.
Markus Handell6deec382020-07-07 12:17:12 +0200209 void CleanUpOldOrEmptyFrames() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
Artem Titovdcd7fc72021-08-09 13:02:57 +0200211 // Returns true if `packet` is likely to have been retransmitted.
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000212 bool IsPacketRetransmitted(const VCMPacket& packet) const;
henrik.lundin@webrtc.orgbaf6db52011-11-02 18:58:39 +0000213
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000214 // The following three functions update the jitter estimate with the
215 // payload size, receive time and RTP timestamp of a frame.
216 void UpdateJitterEstimate(const VCMJitterSample& sample,
217 bool incomplete_frame);
218 void UpdateJitterEstimate(const VCMFrameBuffer& frame, bool incomplete_frame);
219 void UpdateJitterEstimate(int64_t latest_packet_time_ms,
220 uint32_t timestamp,
221 unsigned int frame_size,
222 bool incomplete_frame);
223
Markus Handell6deec382020-07-07 12:17:12 +0200224 int NonContinuousOrIncompleteDuration() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
stefan@webrtc.orgef144882013-05-07 19:16:33 +0000225
226 uint16_t EstimatedLowSequenceNumber(const VCMFrameBuffer& frame) const;
227
sprang22691e02016-07-13 10:57:07 -0700228 // Reset frame buffer and return it to free_frames_.
229 void RecycleFrameBuffer(VCMFrameBuffer* frame)
Markus Handell6deec382020-07-07 12:17:12 +0200230 RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
sprang22691e02016-07-13 10:57:07 -0700231
stefan@webrtc.orga678a3b2013-01-21 07:42:11 +0000232 Clock* clock_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000233 // If we are running (have started) or not.
234 bool running_;
Markus Handell6deec382020-07-07 12:17:12 +0200235 mutable Mutex mutex_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000236 // Event to signal when we have a frame ready for decoder.
kwiberg3f55dea2016-02-29 05:51:59 -0800237 std::unique_ptr<EventWrapper> frame_event_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000238 // Number of allocated frames.
239 int max_number_of_frames_;
Markus Handell6deec382020-07-07 12:17:12 +0200240 UnorderedFrameList free_frames_ RTC_GUARDED_BY(mutex_);
241 FrameList decodable_frames_ RTC_GUARDED_BY(mutex_);
242 FrameList incomplete_frames_ RTC_GUARDED_BY(mutex_);
243 VCMDecodingState last_decoded_state_ RTC_GUARDED_BY(mutex_);
stefan@webrtc.org3417eb42013-05-21 15:25:53 +0000244 bool first_packet_since_reset_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000245
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000246 // Number of packets in a row that have been too old.
247 int num_consecutive_old_packets_;
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000248 // Number of packets received.
Markus Handell6deec382020-07-07 12:17:12 +0200249 int num_packets_ RTC_GUARDED_BY(mutex_);
asapersson@webrtc.org96dc6852014-11-03 14:40:38 +0000250 // Number of duplicated packets received.
Markus Handell6deec382020-07-07 12:17:12 +0200251 int num_duplicated_packets_ RTC_GUARDED_BY(mutex_);
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000252
253 // Jitter estimation.
254 // Filter for estimating jitter.
255 VCMJitterEstimator jitter_estimate_;
256 // Calculates network delays used for jitter calculations.
257 VCMInterFrameDelay inter_frame_delay_;
258 VCMJitterSample waiting_for_completion_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000259
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000260 // Holds the internal NACK list (the missing sequence numbers).
stefan@webrtc.orga64300a2013-03-04 15:24:40 +0000261 SequenceNumberSet missing_sequence_numbers_;
262 uint16_t latest_received_sequence_number_;
stefan@webrtc.orgbecf9c82013-02-01 15:09:57 +0000263 size_t max_nack_list_size_;
264 int max_packet_age_to_nack_; // Measured in sequence numbers.
stefan@webrtc.orgef144882013-05-07 19:16:33 +0000265 int max_incomplete_time_ms_;
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000266
agalusza@google.comd818dcb2013-07-29 21:48:11 +0000267 // Estimated rolling average of packets per frame
268 float average_packets_per_frame_;
269 // average_packets_per_frame converges fast if we have fewer than this many
270 // frames.
271 int frame_counter_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000272};
stefan@webrtc.org912981f2012-10-12 07:04:52 +0000273} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000274
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200275#endif // MODULES_VIDEO_CODING_JITTER_BUFFER_H_