You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2016 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_PACKET_BUFFER_H_
  11. #define MODULES_VIDEO_CODING_PACKET_BUFFER_H_
  12. #include <memory>
  13. #include <queue>
  14. #include <set>
  15. #include <vector>
  16. #include "api/video/encoded_image.h"
  17. #include "modules/video_coding/packet.h"
  18. #include "rtc_base/critical_section.h"
  19. #include "rtc_base/numerics/sequence_number_util.h"
  20. #include "rtc_base/thread_annotations.h"
  21. namespace webrtc {
  22. class Clock;
  23. namespace video_coding {
  24. class RtpFrameObject;
  25. // A frame is assembled when all of its packets have been received.
  26. class OnAssembledFrameCallback {
  27. public:
  28. virtual ~OnAssembledFrameCallback() {}
  29. virtual void OnAssembledFrame(std::unique_ptr<RtpFrameObject> frame) = 0;
  30. };
  31. class PacketBuffer {
  32. public:
  33. // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
  34. PacketBuffer(Clock* clock,
  35. size_t start_buffer_size,
  36. size_t max_buffer_size,
  37. OnAssembledFrameCallback* frame_callback);
  38. ~PacketBuffer();
  39. // Returns true unless the packet buffer is cleared, which means that a key
  40. // frame request should be sent. The PacketBuffer will always take ownership
  41. // of the |packet.dataPtr| when this function is called.
  42. bool InsertPacket(VCMPacket* packet);
  43. void ClearTo(uint16_t seq_num);
  44. void Clear();
  45. void PaddingReceived(uint16_t seq_num);
  46. // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
  47. absl::optional<int64_t> LastReceivedPacketMs() const;
  48. absl::optional<int64_t> LastReceivedKeyframePacketMs() const;
  49. // Returns number of different frames seen in the packet buffer
  50. int GetUniqueFramesSeen() const;
  51. private:
  52. struct StoredPacket {
  53. uint16_t seq_num() const { return data.seqNum; }
  54. // If this is the first packet of the frame.
  55. bool frame_begin() const { return data.is_first_packet_in_frame(); }
  56. // If this is the last packet of the frame.
  57. bool frame_end() const { return data.is_last_packet_in_frame(); }
  58. // If this slot is currently used.
  59. bool used = false;
  60. // If all its previous packets have been inserted into the packet buffer.
  61. bool continuous = false;
  62. VCMPacket data;
  63. };
  64. Clock* const clock_;
  65. // Tries to expand the buffer.
  66. bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  67. // Test if all previous packets has arrived for the given sequence number.
  68. bool PotentialNewFrame(uint16_t seq_num) const
  69. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  70. // Test if all packets of a frame has arrived, and if so, creates a frame.
  71. // Returns a vector of received frames.
  72. std::vector<std::unique_ptr<RtpFrameObject>> FindFrames(uint16_t seq_num)
  73. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  74. rtc::scoped_refptr<EncodedImageBuffer> GetEncodedImageBuffer(
  75. size_t frame_size,
  76. uint16_t first_seq_num,
  77. uint16_t last_seq_num) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  78. // Get the packet with sequence number |seq_num|.
  79. VCMPacket* GetPacket(uint16_t seq_num) RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  80. // Clears the packet buffer from |start_seq_num| to |stop_seq_num| where the
  81. // endpoints are inclusive.
  82. void ClearInterval(uint16_t start_seq_num, uint16_t stop_seq_num)
  83. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  84. void UpdateMissingPackets(uint16_t seq_num)
  85. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  86. // Counts unique received timestamps and updates |unique_frames_seen_|.
  87. void OnTimestampReceived(uint32_t rtp_timestamp)
  88. RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
  89. rtc::CriticalSection crit_;
  90. // buffer_.size() and max_size_ must always be a power of two.
  91. const size_t max_size_;
  92. // The fist sequence number currently in the buffer.
  93. uint16_t first_seq_num_ RTC_GUARDED_BY(crit_);
  94. // If the packet buffer has received its first packet.
  95. bool first_packet_received_ RTC_GUARDED_BY(crit_);
  96. // If the buffer is cleared to |first_seq_num_|.
  97. bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(crit_);
  98. // Buffer that holds the the inserted packets and information needed to
  99. // determine continuity between them.
  100. std::vector<StoredPacket> buffer_ RTC_GUARDED_BY(crit_);
  101. // Called when all packets in a frame are received, allowing the frame
  102. // to be assembled.
  103. OnAssembledFrameCallback* const assembled_frame_callback_;
  104. // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
  105. absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(crit_);
  106. absl::optional<int64_t> last_received_keyframe_packet_ms_
  107. RTC_GUARDED_BY(crit_);
  108. int unique_frames_seen_ RTC_GUARDED_BY(crit_);
  109. absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(crit_);
  110. std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
  111. RTC_GUARDED_BY(crit_);
  112. // Indicates if we should require SPS, PPS, and IDR for a particular
  113. // RTP timestamp to treat the corresponding frame as a keyframe.
  114. const bool sps_pps_idr_is_h264_keyframe_;
  115. // Stores several last seen unique timestamps for quick search.
  116. std::set<uint32_t> rtp_timestamps_history_set_ RTC_GUARDED_BY(crit_);
  117. // Stores the same unique timestamps in the order of insertion.
  118. std::queue<uint32_t> rtp_timestamps_history_queue_ RTC_GUARDED_BY(crit_);
  119. };
  120. } // namespace video_coding
  121. } // namespace webrtc
  122. #endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_