image.png
通过found_frames可以知道那些帧是完整的帧。

找到潜在的新帧

image.png
1、获取当前包,和前面一个包
2、判断当前包是否为空,是否不存在,和是否是该帧的第一个包
3、判断前一个包是否存在(是否是连续包),前一个包跟当前包有没有关系seq_num,判断前一个包和当前包是否是在同一个帧。如果前面包是连续的,则可能存在新包。

找到帧的起始包

image.png

将属于同一帧的包都找出来

image.png

源码注释

  1. std::vector<std::unique_ptr<PacketBuffer::Packet>> PacketBuffer::FindFrames(
  2. uint16_t seq_num) {
  3. std::vector<std::unique_ptr<PacketBuffer::Packet>> found_frames;
  4. for (size_t i = 0; i < buffer_.size() && PotentialNewFrame(seq_num); ++i) {
  5. // 获取该包在buffer_中的位置
  6. size_t index = seq_num % buffer_.size();
  7. buffer_[index]->continuous = true;
  8. // If all packets of the frame is continuous, find the first packet of the
  9. // frame and add all packets of the frame to the returned packets.
  10. // 判断这个包,是否是这个帧的最后一个包。
  11. if (buffer_[index]->is_last_packet_in_frame()) {
  12. uint16_t start_seq_num = seq_num;
  13. // Find the start index by searching backward until the packet with
  14. // the |frame_begin| flag is set.
  15. int start_index = index;
  16. size_t tested_packets = 0;
  17. int64_t frame_timestamp = buffer_[start_index]->timestamp;
  18. // Identify H.264 keyframes by means of SPS, PPS, and IDR.
  19. bool is_h264 = buffer_[start_index]->codec() == kVideoCodecH264;
  20. bool has_h264_sps = false;
  21. bool has_h264_pps = false;
  22. bool has_h264_idr = false;
  23. bool is_h264_keyframe = false;
  24. int idr_width = -1;
  25. int idr_height = -1;
  26. while (true) {
  27. ++tested_packets;
  28. if (!is_h264 && buffer_[start_index]->is_first_packet_in_frame())
  29. break;
  30. if (is_h264) {
  31. const auto* h264_header = absl::get_if<RTPVideoHeaderH264>(
  32. &buffer_[start_index]->video_header.video_type_header);
  33. if (!h264_header || h264_header->nalus_length >= kMaxNalusPerPacket)
  34. return found_frames;
  35. for (size_t j = 0; j < h264_header->nalus_length; ++j) {
  36. if (h264_header->nalus[j].type == H264::NaluType::kSps) {
  37. has_h264_sps = true;
  38. } else if (h264_header->nalus[j].type == H264::NaluType::kPps) {
  39. has_h264_pps = true;
  40. } else if (h264_header->nalus[j].type == H264::NaluType::kIdr) {
  41. has_h264_idr = true;
  42. }
  43. }
  44. if ((sps_pps_idr_is_h264_keyframe_ && has_h264_idr && has_h264_sps &&
  45. has_h264_pps) ||
  46. (!sps_pps_idr_is_h264_keyframe_ && has_h264_idr)) {
  47. is_h264_keyframe = true;
  48. // Store the resolution of key frame which is the packet with
  49. // smallest index and valid resolution; typically its IDR or SPS
  50. // packet; there may be packet preceeding this packet, IDR's
  51. // resolution will be applied to them.
  52. if (buffer_[start_index]->width() > 0 &&
  53. buffer_[start_index]->height() > 0) {
  54. idr_width = buffer_[start_index]->width();
  55. idr_height = buffer_[start_index]->height();
  56. }
  57. }
  58. }
  59. if (tested_packets == buffer_.size())
  60. break;
  61. start_index = start_index > 0 ? start_index - 1 : buffer_.size() - 1;
  62. // In the case of H264 we don't have a frame_begin bit (yes,
  63. // |frame_begin| might be set to true but that is a lie). So instead
  64. // we traverese backwards as long as we have a previous packet and
  65. // the timestamp of that packet is the same as this one. This may cause
  66. // the PacketBuffer to hand out incomplete frames.
  67. // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=7106
  68. if (is_h264 && (buffer_[start_index] == nullptr ||
  69. buffer_[start_index]->timestamp != frame_timestamp)) {
  70. break;
  71. }
  72. --start_seq_num;
  73. }
  74. if (is_h264) {
  75. // Warn if this is an unsafe frame.
  76. if (has_h264_idr && (!has_h264_sps || !has_h264_pps)) {
  77. RTC_LOG(LS_WARNING)
  78. << "Received H.264-IDR frame "
  79. "(SPS: "
  80. << has_h264_sps << ", PPS: " << has_h264_pps << "). Treating as "
  81. << (sps_pps_idr_is_h264_keyframe_ ? "delta" : "key")
  82. << " frame since WebRTC-SpsPpsIdrIsH264Keyframe is "
  83. << (sps_pps_idr_is_h264_keyframe_ ? "enabled." : "disabled");
  84. }
  85. // Now that we have decided whether to treat this frame as a key frame
  86. // or delta frame in the frame buffer, we update the field that
  87. // determines if the RtpFrameObject is a key frame or delta frame.
  88. const size_t first_packet_index = start_seq_num % buffer_.size();
  89. if (is_h264_keyframe) {
  90. buffer_[first_packet_index]->video_header.frame_type =
  91. VideoFrameType::kVideoFrameKey;
  92. if (idr_width > 0 && idr_height > 0) {
  93. // IDR frame was finalized and we have the correct resolution for
  94. // IDR; update first packet to have same resolution as IDR.
  95. buffer_[first_packet_index]->video_header.width = idr_width;
  96. buffer_[first_packet_index]->video_header.height = idr_height;
  97. }
  98. } else {
  99. buffer_[first_packet_index]->video_header.frame_type =
  100. VideoFrameType::kVideoFrameDelta;
  101. }
  102. // If this is not a keyframe, make sure there are no gaps in the packet
  103. // sequence numbers up until this point.
  104. if (!is_h264_keyframe && missing_packets_.upper_bound(start_seq_num) !=
  105. missing_packets_.begin()) {
  106. return found_frames;
  107. }
  108. }
  109. const uint16_t end_seq_num = seq_num + 1;
  110. // Use uint16_t type to handle sequence number wrap around case.
  111. uint16_t num_packets = end_seq_num - start_seq_num;
  112. found_frames.reserve(found_frames.size() + num_packets);
  113. for (uint16_t i = start_seq_num; i != end_seq_num; ++i) {
  114. std::unique_ptr<Packet>& packet = buffer_[i % buffer_.size()];
  115. RTC_DCHECK(packet);
  116. RTC_DCHECK_EQ(i, packet->seq_num);
  117. // Ensure frame boundary flags are properly set.
  118. packet->video_header.is_first_packet_in_frame = (i == start_seq_num);
  119. packet->video_header.is_last_packet_in_frame = (i == seq_num);
  120. found_frames.push_back(std::move(packet));
  121. }
  122. missing_packets_.erase(missing_packets_.begin(),
  123. missing_packets_.upper_bound(seq_num));
  124. }
  125. ++seq_num;
  126. }
  127. return found_frames;
  128. }