NACK(Negative Acknowledgement)机制是WebRTC中用于处理数据包丢失的重要机制。用于在数据包丢失时通知发送方重新发送丢失的数据包。当接收方检测到数据包丢失时,它会发送一个NACK消息给发送方,请求重新发送丢失的数据包。这种机制可以帮助提高通信质量和可靠性,尤其在不可靠的网络环境下。
本文将从webrtc源码分析nack的实现,版本m98

一、NackRequester::OnReceivedPacket

int NackRequester::OnReceivedPacket(uint16_t seq_num,
                                    bool is_keyframe,
                                    bool is_recovered) {
  RTC_DCHECK_RUN_ON(worker_thread_);
  // TODO(philipel): When the packet includes information whether it is
  //                 retransmitted or not, use that value instead. For
  //                 now set it to true, which will cause the reordering
  //                 statistics to never be updated.
  bool is_retransmitted = true;

  if (!initialized_) {  //如果没有初始化,就初始化,设置newest_seq_num_
    newest_seq_num_ = seq_num;
    if (is_keyframe) //如果是关键帧,则插入关键帧队列
      keyframe_list_.insert(seq_num);
    initialized_ = true;
    return 0;
  }

  // Since the `newest_seq_num_` is a packet we have actually received we know
  // that packet has never been Nacked.
  if (seq_num == newest_seq_num_) //当前帧等于最新帧,说明无需更新nack模块
    return 0;

  if (AheadOf(newest_seq_num_, seq_num)) {   //当前帧比最新帧序列号要老,说明是乱序帧,或者是重传帧
    // An out of order packet has been received.
    auto nack_list_it = nack_list_.find(seq_num);
    int nacks_sent_for_packet = 0;
    if (nack_list_it != nack_list_.end()) {   //在丢包队列里找到了,说明当前帧是重传帧,更新信息,并在丢包队列里清除掉当前帧
      nacks_sent_for_packet = nack_list_it->second.retries;
      nack_list_.erase(nack_list_it);
    }
    if (!is_retransmitted)  //永远不会触发
      UpdateReorderingStatistics(seq_num);
    return nacks_sent_for_packet;
  }

  // Keep track of new keyframes.
  if (is_keyframe) //如果是关键帧,则插入关键帧队列
    keyframe_list_.insert(seq_num);

  // And remove old ones so we don't accumulate keyframes.
  //清除掉太老的关键帧,最多10000包
  auto it = keyframe_list_.lower_bound(seq_num - kMaxPacketAge);
  if (it != keyframe_list_.begin())
    keyframe_list_.erase(keyframe_list_.begin(), it);

  if (is_recovered) {  //recovered 说明是fec包恢复的,则更新recovered_list_
    recovered_list_.insert(seq_num);

    // Remove old ones so we don't accumulate recovered packets.
    auto it = recovered_list_.lower_bound(seq_num - kMaxPacketAge);
    if (it != recovered_list_.begin())
      recovered_list_.erase(recovered_list_.begin(), it);

    // Do not send nack for packets recovered by FEC or RTX.
    return 0;
  }

  //将newest_seq_num_ + 1 至 seq_num的所有包添加至Nack队列
  AddPacketsToNack(newest_seq_num_ + 1, seq_num);
  newest_seq_num_ = seq_num;

  // Are there any nacks that are waiting for this seq_num.
  //获取nack包组
  std::vector<uint16_t> nack_batch = GetNackBatch(kSeqNumOnly);
  if (!nack_batch.empty()) {
    // This batch of NACKs is triggered externally; the initiator can
    // batch them with other feedback messages.
    //打包并发送nack信息
    nack_sender_->SendNack(nack_batch, /*buffering_allowed=*/true);
  }

  return 0;
}

webrtc收到rtp数据后,会调用NackRequester::OnReceivedPacket,来更新和处理nack相关的信息。在NackRequester::OnReceivedPacket中,先判断是否是乱序或重传包,并更新丢包队列,否则通过NackRequester::AddPacketsToNack将新丢失包更新到丢包队列,最后再通过NackRequester::GetNackBatch获取到丢失包组以后,通过NackSender发送出去。

二、NackRequester::AddPacketsToNack

void NackRequester::AddPacketsToNack(uint16_t seq_num_start,
                                     uint16_t seq_num_end) {
  // Called on worker_thread_.
  // Remove old packets.
  //清除太旧的Nack包
  auto it = nack_list_.lower_bound(seq_num_end - kMaxPacketAge);
  nack_list_.erase(nack_list_.begin(), it);

  // If the nack list is too large, remove packets from the nack list until
  // the latest first packet of a keyframe. If the list is still too large,
  // clear it and request a keyframe.
  uint16_t num_new_nacks = ForwardDiff(seq_num_start, seq_num_end);
  //如果要插入的nack包数目+当前数目超过最大包数目,就按关键帧队列,清除关键帧之前的nac包
  if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
    while (RemovePacketsUntilKeyFrame() &&
           nack_list_.size() + num_new_nacks > kMaxNackPackets) {
    }

    //清除以后还是空间还是不够,就清空nack队列,请求关键帧
    if (nack_list_.size() + num_new_nacks > kMaxNackPackets) {
      nack_list_.clear();
      RTC_LOG(LS_WARNING) << "NACK list full, clearing NACK"
                             " list and requesting keyframe.";
      keyframe_request_sender_->RequestKeyFrame();
      return;
    }
  }

  //依次把待插入的nack包插入nack队列
  for (uint16_t seq_num = seq_num_start; seq_num != seq_num_end; ++seq_num) {
    // Do not send nack for packets that are already recovered by FEC or RTX
    if (recovered_list_.find(seq_num) != recovered_list_.end())
      continue;
    NackInfo nack_info(seq_num, seq_num + WaitNumberOfPackets(0.5),
                       clock_->TimeInMilliseconds());
    RTC_DCHECK(nack_list_.find(seq_num) == nack_list_.end());
    nack_list_[seq_num] = nack_info;
  }
}

插入nack包的逻辑比较简答,就是做了一个最大包数的判断,如果超过的话就按关键帧序号清理nack队列,如果清理后还是超限,就清空nack队列并向发送端请求关键帧。

三、NackRequester::GetNackBatch

std::vector<uint16_t> NackRequester::GetNackBatch(NackFilterOptions options) {
  // Called on worker_thread_.
  //两种模式
  bool consider_seq_num = options != kTimeOnly;
  bool consider_timestamp = options != kSeqNumOnly;
  Timestamp now = clock_->CurrentTime();
  std::vector<uint16_t> nack_batch;
  auto it = nack_list_.begin();
  while (it != nack_list_.end()) {
    TimeDelta resend_delay = TimeDelta::Millis(rtt_ms_);
    //根据配置选项,计算重传补偿延迟值,不配置的话就是一个rtt
    if (backoff_settings_) {
      resend_delay =
          std::max(resend_delay, backoff_settings_->min_retry_interval);
      if (it->second.retries > 1) {
        TimeDelta exponential_backoff =
            std::min(TimeDelta::Millis(rtt_ms_), backoff_settings_->max_rtt) *
            std::pow(backoff_settings_->base, it->second.retries - 1);
        resend_delay = std::max(resend_delay, exponential_backoff);
      }
    }

    bool delay_timed_out =
        now.ms() - it->second.created_at_time >= send_nack_delay_ms_;  //可以通过配置nack发送延迟来设置,0-20ms,默认是0;
    bool nack_on_rtt_passed =
        now.ms() - it->second.sent_at_time >= resend_delay.ms(); //是否超过了nack重传延迟
    bool nack_on_seq_num_passed =
        it->second.sent_at_time == -1 &&
        AheadOrAt(newest_seq_num_, it->second.send_at_seq_num);  //是否是第一次发送nack,且序列号小于最新包
    if (delay_timed_out && ((consider_seq_num && nack_on_seq_num_passed) ||
                            (consider_timestamp && nack_on_rtt_passed))) { //满足条件,将nack序列放入nack_batch
      nack_batch.emplace_back(it->second.seq_num);
      ++it->second.retries;
      it->second.sent_at_time = now.ms();
      if (it->second.retries >= kMaxNackRetries) {
        RTC_LOG(LS_WARNING) << "Sequence number " << it->second.seq_num
                            << " removed from NACK list due to max retries.";
        it = nack_list_.erase(it);
      } else {
        ++it;
      }
      continue;
    }
    ++it;
  }
  return nack_batch;
}

GetNackBatch有两种模式,分别是依据序列号模式和依据时间戳模式。依据序列号模式是接收到rtp后,首次确认丢包,则依据序列号模式发送一次丢包请求;依据时间戳模式是按一定的时间间隔,周期性的发送丢包请求(WebRTC默认是20ms)。
重要参数:

  • resend_delay:丢包重传时间间隔,默认为一个rtt。可以通过配置backoff_settings_来修改,比如可以配置min_retry_interval来设置最小重传间隔,防止rtt小的时候,频繁发送重传请求。
  • send_nack_delay_ms_:首次丢包重传延迟。收到正常顺序外的包,原生机制默认是直接就返送nack的,通过控制NACK延时发送的时间间隔,避免固定延时网络下无必要的重传请求。比如,如果kDefaultSendNackDelayMs=20ms,如果因为网络的固有延时,造成某些数据包迟到了10ms,而此时没有NACK延时发送机制的话,这些包都会被认为丢了,从而对这些包请求重传。但是如果有20ms的NACK延时发送,这些包就不会被计算为丢失,从而避免了没有必要的重传请求,避免了资源浪费。

NACK发送的逻辑分析完毕,具体打包发送和接收处理本文不再分析。

Logo

音视频技术社区,一个全球开发者共同探讨、分享、学习音视频技术的平台,加入我们,与全球开发者一起创造更加优秀的音视频产品!

更多推荐