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