jitter_buffer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #ifndef MODULES_VIDEO_CODING_JITTER_BUFFER_H_
  11. #define MODULES_VIDEO_CODING_JITTER_BUFFER_H_
  12. #include <list>
  13. #include <map>
  14. #include <memory>
  15. #include <set>
  16. #include <vector>
  17. #include "modules/include/module_common_types.h"
  18. #include "modules/include/module_common_types_public.h"
  19. #include "modules/utility/include/process_thread.h"
  20. #include "modules/video_coding/decoding_state.h"
  21. #include "modules/video_coding/event_wrapper.h"
  22. #include "modules/video_coding/include/video_coding.h"
  23. #include "modules/video_coding/include/video_coding_defines.h"
  24. #include "modules/video_coding/inter_frame_delay.h"
  25. #include "modules/video_coding/jitter_buffer_common.h"
  26. #include "modules/video_coding/jitter_estimator.h"
  27. #include "rtc_base/constructor_magic.h"
  28. #include "rtc_base/synchronization/mutex.h"
  29. #include "rtc_base/thread_annotations.h"
  30. namespace webrtc {
  31. // forward declarations
  32. class Clock;
  33. class VCMFrameBuffer;
  34. class VCMPacket;
  35. class VCMEncodedFrame;
  36. typedef std::list<VCMFrameBuffer*> UnorderedFrameList;
  37. struct VCMJitterSample {
  38. VCMJitterSample() : timestamp(0), frame_size(0), latest_packet_time(-1) {}
  39. uint32_t timestamp;
  40. uint32_t frame_size;
  41. int64_t latest_packet_time;
  42. };
  43. class TimestampLessThan {
  44. public:
  45. bool operator()(uint32_t timestamp1, uint32_t timestamp2) const {
  46. return IsNewerTimestamp(timestamp2, timestamp1);
  47. }
  48. };
  49. class FrameList
  50. : public std::map<uint32_t, VCMFrameBuffer*, TimestampLessThan> {
  51. public:
  52. void InsertFrame(VCMFrameBuffer* frame);
  53. VCMFrameBuffer* PopFrame(uint32_t timestamp);
  54. VCMFrameBuffer* Front() const;
  55. VCMFrameBuffer* Back() const;
  56. int RecycleFramesUntilKeyFrame(FrameList::iterator* key_frame_it,
  57. UnorderedFrameList* free_frames);
  58. void CleanUpOldOrEmptyFrames(VCMDecodingState* decoding_state,
  59. UnorderedFrameList* free_frames);
  60. void Reset(UnorderedFrameList* free_frames);
  61. };
  62. class VCMJitterBuffer {
  63. public:
  64. VCMJitterBuffer(Clock* clock, std::unique_ptr<EventWrapper> event);
  65. ~VCMJitterBuffer();
  66. // Initializes and starts jitter buffer.
  67. void Start();
  68. // Signals all internal events and stops the jitter buffer.
  69. void Stop();
  70. // Returns true if the jitter buffer is running.
  71. bool Running() const;
  72. // Empty the jitter buffer of all its data.
  73. void Flush();
  74. // Gets number of packets received.
  75. int num_packets() const;
  76. // Gets number of duplicated packets received.
  77. int num_duplicated_packets() const;
  78. // Wait |max_wait_time_ms| for a complete frame to arrive.
  79. // If found, a pointer to the frame is returned. Returns nullptr otherwise.
  80. VCMEncodedFrame* NextCompleteFrame(uint32_t max_wait_time_ms);
  81. // Extract frame corresponding to input timestamp.
  82. // Frame will be set to a decoding state.
  83. VCMEncodedFrame* ExtractAndSetDecode(uint32_t timestamp);
  84. // Releases a frame returned from the jitter buffer, should be called when
  85. // done with decoding.
  86. void ReleaseFrame(VCMEncodedFrame* frame);
  87. // Returns the time in ms when the latest packet was inserted into the frame.
  88. // Retransmitted is set to true if any of the packets belonging to the frame
  89. // has been retransmitted.
  90. int64_t LastPacketTime(const VCMEncodedFrame* frame,
  91. bool* retransmitted) const;
  92. // Inserts a packet into a frame returned from GetFrame().
  93. // If the return value is <= 0, |frame| is invalidated and the pointer must
  94. // be dropped after this function returns.
  95. VCMFrameBufferEnum InsertPacket(const VCMPacket& packet, bool* retransmitted);
  96. // Returns the estimated jitter in milliseconds.
  97. uint32_t EstimatedJitterMs();
  98. void SetNackSettings(size_t max_nack_list_size,
  99. int max_packet_age_to_nack,
  100. int max_incomplete_time_ms);
  101. // Returns a list of the sequence numbers currently missing.
  102. std::vector<uint16_t> GetNackList(bool* request_key_frame);
  103. private:
  104. class SequenceNumberLessThan {
  105. public:
  106. bool operator()(const uint16_t& sequence_number1,
  107. const uint16_t& sequence_number2) const {
  108. return IsNewerSequenceNumber(sequence_number2, sequence_number1);
  109. }
  110. };
  111. typedef std::set<uint16_t, SequenceNumberLessThan> SequenceNumberSet;
  112. // Gets the frame assigned to the timestamp of the packet. May recycle
  113. // existing frames if no free frames are available. Returns an error code if
  114. // failing, or kNoError on success. |frame_list| contains which list the
  115. // packet was in, or NULL if it was not in a FrameList (a new frame).
  116. VCMFrameBufferEnum GetFrame(const VCMPacket& packet,
  117. VCMFrameBuffer** frame,
  118. FrameList** frame_list)
  119. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  120. // Returns true if |frame| is continuous in |decoding_state|, not taking
  121. // decodable frames into account.
  122. bool IsContinuousInState(const VCMFrameBuffer& frame,
  123. const VCMDecodingState& decoding_state) const
  124. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  125. // Returns true if |frame| is continuous in the |last_decoded_state_|, taking
  126. // all decodable frames into account.
  127. bool IsContinuous(const VCMFrameBuffer& frame) const
  128. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  129. // Looks for frames in |incomplete_frames_| which are continuous in the
  130. // provided |decoded_state|. Starts the search from the timestamp of
  131. // |decoded_state|.
  132. void FindAndInsertContinuousFramesWithState(
  133. const VCMDecodingState& decoded_state)
  134. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  135. // Looks for frames in |incomplete_frames_| which are continuous in
  136. // |last_decoded_state_| taking all decodable frames into account. Starts
  137. // the search from |new_frame|.
  138. void FindAndInsertContinuousFrames(const VCMFrameBuffer& new_frame)
  139. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  140. VCMFrameBuffer* NextFrame() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  141. // Returns true if the NACK list was updated to cover sequence numbers up to
  142. // |sequence_number|. If false a key frame is needed to get into a state where
  143. // we can continue decoding.
  144. bool UpdateNackList(uint16_t sequence_number)
  145. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  146. bool TooLargeNackList() const;
  147. // Returns true if the NACK list was reduced without problem. If false a key
  148. // frame is needed to get into a state where we can continue decoding.
  149. bool HandleTooLargeNackList() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  150. bool MissingTooOldPacket(uint16_t latest_sequence_number) const
  151. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  152. // Returns true if the too old packets was successfully removed from the NACK
  153. // list. If false, a key frame is needed to get into a state where we can
  154. // continue decoding.
  155. bool HandleTooOldPackets(uint16_t latest_sequence_number)
  156. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  157. // Drops all packets in the NACK list up until |last_decoded_sequence_number|.
  158. void DropPacketsFromNackList(uint16_t last_decoded_sequence_number);
  159. // Gets an empty frame, creating a new frame if necessary (i.e. increases
  160. // jitter buffer size).
  161. VCMFrameBuffer* GetEmptyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  162. // Attempts to increase the size of the jitter buffer. Returns true on
  163. // success, false otherwise.
  164. bool TryToIncreaseJitterBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  165. // Recycles oldest frames until a key frame is found. Used if jitter buffer is
  166. // completely full. Returns true if a key frame was found.
  167. bool RecycleFramesUntilKeyFrame() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  168. // Update rolling average of packets per frame.
  169. void UpdateAveragePacketsPerFrame(int current_number_packets_);
  170. // Cleans the frame list in the JB from old/empty frames.
  171. // Should only be called prior to actual use.
  172. void CleanUpOldOrEmptyFrames() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  173. // Returns true if |packet| is likely to have been retransmitted.
  174. bool IsPacketRetransmitted(const VCMPacket& packet) const;
  175. // The following three functions update the jitter estimate with the
  176. // payload size, receive time and RTP timestamp of a frame.
  177. void UpdateJitterEstimate(const VCMJitterSample& sample,
  178. bool incomplete_frame);
  179. void UpdateJitterEstimate(const VCMFrameBuffer& frame, bool incomplete_frame);
  180. void UpdateJitterEstimate(int64_t latest_packet_time_ms,
  181. uint32_t timestamp,
  182. unsigned int frame_size,
  183. bool incomplete_frame);
  184. int NonContinuousOrIncompleteDuration() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  185. uint16_t EstimatedLowSequenceNumber(const VCMFrameBuffer& frame) const;
  186. // Reset frame buffer and return it to free_frames_.
  187. void RecycleFrameBuffer(VCMFrameBuffer* frame)
  188. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  189. Clock* clock_;
  190. // If we are running (have started) or not.
  191. bool running_;
  192. mutable Mutex mutex_;
  193. // Event to signal when we have a frame ready for decoder.
  194. std::unique_ptr<EventWrapper> frame_event_;
  195. // Number of allocated frames.
  196. int max_number_of_frames_;
  197. UnorderedFrameList free_frames_ RTC_GUARDED_BY(mutex_);
  198. FrameList decodable_frames_ RTC_GUARDED_BY(mutex_);
  199. FrameList incomplete_frames_ RTC_GUARDED_BY(mutex_);
  200. VCMDecodingState last_decoded_state_ RTC_GUARDED_BY(mutex_);
  201. bool first_packet_since_reset_;
  202. // Number of packets in a row that have been too old.
  203. int num_consecutive_old_packets_;
  204. // Number of packets received.
  205. int num_packets_ RTC_GUARDED_BY(mutex_);
  206. // Number of duplicated packets received.
  207. int num_duplicated_packets_ RTC_GUARDED_BY(mutex_);
  208. // Jitter estimation.
  209. // Filter for estimating jitter.
  210. VCMJitterEstimator jitter_estimate_;
  211. // Calculates network delays used for jitter calculations.
  212. VCMInterFrameDelay inter_frame_delay_;
  213. VCMJitterSample waiting_for_completion_;
  214. // Holds the internal NACK list (the missing sequence numbers).
  215. SequenceNumberSet missing_sequence_numbers_;
  216. uint16_t latest_received_sequence_number_;
  217. size_t max_nack_list_size_;
  218. int max_packet_age_to_nack_; // Measured in sequence numbers.
  219. int max_incomplete_time_ms_;
  220. // Estimated rolling average of packets per frame
  221. float average_packets_per_frame_;
  222. // average_packets_per_frame converges fast if we have fewer than this many
  223. // frames.
  224. int frame_counter_;
  225. RTC_DISALLOW_COPY_AND_ASSIGN(VCMJitterBuffer);
  226. };
  227. } // namespace webrtc
  228. #endif // MODULES_VIDEO_CODING_JITTER_BUFFER_H_