packet_buffer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "absl/base/attributes.h"
  17. #include "api/rtp_packet_info.h"
  18. #include "api/video/encoded_image.h"
  19. #include "modules/rtp_rtcp/source/rtp_packet_received.h"
  20. #include "modules/rtp_rtcp/source/rtp_video_header.h"
  21. #include "rtc_base/copy_on_write_buffer.h"
  22. #include "rtc_base/numerics/sequence_number_util.h"
  23. #include "rtc_base/synchronization/mutex.h"
  24. #include "rtc_base/thread_annotations.h"
  25. #include "system_wrappers/include/clock.h"
  26. namespace webrtc {
  27. namespace video_coding {
  28. class PacketBuffer {
  29. public:
  30. struct Packet {
  31. Packet() = default;
  32. Packet(const RtpPacketReceived& rtp_packet,
  33. const RTPVideoHeader& video_header,
  34. int64_t ntp_time_ms,
  35. int64_t receive_time_ms);
  36. Packet(const Packet&) = delete;
  37. Packet(Packet&&) = delete;
  38. Packet& operator=(const Packet&) = delete;
  39. Packet& operator=(Packet&&) = delete;
  40. ~Packet() = default;
  41. VideoCodecType codec() const { return video_header.codec; }
  42. int width() const { return video_header.width; }
  43. int height() const { return video_header.height; }
  44. bool is_first_packet_in_frame() const {
  45. return video_header.is_first_packet_in_frame;
  46. }
  47. bool is_last_packet_in_frame() const {
  48. return video_header.is_last_packet_in_frame;
  49. }
  50. // If all its previous packets have been inserted into the packet buffer.
  51. // Set and used internally by the PacketBuffer.
  52. bool continuous = false;
  53. bool marker_bit = false;
  54. uint8_t payload_type = 0;
  55. uint16_t seq_num = 0;
  56. uint32_t timestamp = 0;
  57. // NTP time of the capture time in local timebase in milliseconds.
  58. int64_t ntp_time_ms = -1;
  59. int times_nacked = -1;
  60. rtc::CopyOnWriteBuffer video_payload;
  61. RTPVideoHeader video_header;
  62. RtpPacketInfo packet_info;
  63. };
  64. struct InsertResult {
  65. std::vector<std::unique_ptr<Packet>> packets;
  66. // Indicates if the packet buffer was cleared, which means that a key
  67. // frame request should be sent.
  68. bool buffer_cleared = false;
  69. };
  70. // Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
  71. PacketBuffer(Clock* clock, size_t start_buffer_size, size_t max_buffer_size);
  72. ~PacketBuffer();
  73. ABSL_MUST_USE_RESULT InsertResult InsertPacket(std::unique_ptr<Packet> packet)
  74. RTC_LOCKS_EXCLUDED(mutex_);
  75. ABSL_MUST_USE_RESULT InsertResult InsertPadding(uint16_t seq_num)
  76. RTC_LOCKS_EXCLUDED(mutex_);
  77. void ClearTo(uint16_t seq_num) RTC_LOCKS_EXCLUDED(mutex_);
  78. void Clear() RTC_LOCKS_EXCLUDED(mutex_);
  79. // Timestamp (not RTP timestamp) of the last received packet/keyframe packet.
  80. absl::optional<int64_t> LastReceivedPacketMs() const
  81. RTC_LOCKS_EXCLUDED(mutex_);
  82. absl::optional<int64_t> LastReceivedKeyframePacketMs() const
  83. RTC_LOCKS_EXCLUDED(mutex_);
  84. void ForceSpsPpsIdrIsH264Keyframe();
  85. private:
  86. Clock* const clock_;
  87. // Clears with |mutex_| taken.
  88. void ClearInternal() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  89. // Tries to expand the buffer.
  90. bool ExpandBufferSize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  91. // Test if all previous packets has arrived for the given sequence number.
  92. bool PotentialNewFrame(uint16_t seq_num) const
  93. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  94. // Test if all packets of a frame has arrived, and if so, returns packets to
  95. // create frames.
  96. std::vector<std::unique_ptr<Packet>> FindFrames(uint16_t seq_num)
  97. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  98. void UpdateMissingPackets(uint16_t seq_num)
  99. RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
  100. mutable Mutex mutex_;
  101. // buffer_.size() and max_size_ must always be a power of two.
  102. const size_t max_size_;
  103. // The fist sequence number currently in the buffer.
  104. uint16_t first_seq_num_ RTC_GUARDED_BY(mutex_);
  105. // If the packet buffer has received its first packet.
  106. bool first_packet_received_ RTC_GUARDED_BY(mutex_);
  107. // If the buffer is cleared to |first_seq_num_|.
  108. bool is_cleared_to_first_seq_num_ RTC_GUARDED_BY(mutex_);
  109. // Buffer that holds the the inserted packets and information needed to
  110. // determine continuity between them.
  111. std::vector<std::unique_ptr<Packet>> buffer_ RTC_GUARDED_BY(mutex_);
  112. // Timestamp of the last received packet/keyframe packet.
  113. absl::optional<int64_t> last_received_packet_ms_ RTC_GUARDED_BY(mutex_);
  114. absl::optional<int64_t> last_received_keyframe_packet_ms_
  115. RTC_GUARDED_BY(mutex_);
  116. absl::optional<uint32_t> last_received_keyframe_rtp_timestamp_
  117. RTC_GUARDED_BY(mutex_);
  118. absl::optional<uint16_t> newest_inserted_seq_num_ RTC_GUARDED_BY(mutex_);
  119. std::set<uint16_t, DescendingSeqNumComp<uint16_t>> missing_packets_
  120. RTC_GUARDED_BY(mutex_);
  121. // Indicates if we should require SPS, PPS, and IDR for a particular
  122. // RTP timestamp to treat the corresponding frame as a keyframe.
  123. bool sps_pps_idr_is_h264_keyframe_;
  124. };
  125. } // namespace video_coding
  126. } // namespace webrtc
  127. #endif // MODULES_VIDEO_CODING_PACKET_BUFFER_H_