webrtc 代码学习(三十三)pace 模块的发包控制

pace 模块的发包控制
作者:LanPZzzz

文章目录

          • 1. pace 模块的发包控制,在modules\pacing\interval_budget.cc
          • 2. SendPacket 时候获取 media_budget_->bytes_remaining() 当前时间点上可以发送的数据字节数
          • 3. media_budget_ 的码率设置
          • 4. pacing_bitrate_kbps_ 是从哪里获取的,接下来需要分析的
          • 5. 参照 [WebRTC中PacedSender工作原理和代码分析](https://www.jianshu.com/p/3fde9b8d77f6)

1. pace 模块的发包控制,在modules\pacing\interval_budget.cc
2. SendPacket 时候获取 media_budget_->bytes_remaining() 当前时间点上可以发送的数据字节数
bool PacedSender::SendPacket(const PacketQueueInterface::Packet& packet,
                             const PacedPacketInfo& pacing_info) {
  RTC_DCHECK(!paused_);
  bool audio_packet = packet.priority == kHighPriority;
  bool apply_pacing =
      !audio_packet || account_for_audio_ || video_blocks_audio_;
      
      // media_budget_->bytes_remaining() == 0
  if (apply_pacing && (Congested() || (media_budget_->bytes_remaining() == 0 &&
                                       pacing_info.probe_cluster_id ==
                                           PacedPacketInfo::kNotAProbe))) {
    return false;
  }

  critsect_.Leave();
  const bool success = packet_sender_->TimeToSendPacket(
      packet.ssrc, packet.sequence_number, packet.capture_time_ms,
      packet.retransmission, pacing_info);
  critsect_.Enter();

  if (success) {
    if (first_sent_packet_ms_ == -1)
      first_sent_packet_ms_ = clock_->TimeInMilliseconds();
    if (!audio_packet || account_for_audio_) {
      // Update media bytes sent.
      // TODO(eladalon): TimeToSendPacket() can also return |true| in some
      // situations where nothing actually ended up being sent to the network,
      // and we probably don't want to update the budget in such cases.
      // https://bugs.chromium.org/p/webrtc/issues/detail?id=8052
      
      发送成功就更新
      UpdateBudgetWithBytesSent(packet.bytes);
      last_send_time_us_ = clock_->TimeInMicroseconds();
    }
  }

  return success;
}

void PacedSender::UpdateBudgetWithBytesSent(size_t bytes_sent) {
  outstanding_bytes_ += bytes_sent;
  media_budget_->UseBudget(bytes_sent);
  padding_budget_->UseBudget(bytes_sent);
}
3. media_budget_ 的码率设置
void PacedSender::Process() {
  int64_t now_us = clock_->TimeInMicroseconds();
  rtc::CritScope cs(&critsect_);
  int64_t elapsed_time_ms = (now_us - time_last_process_us_ + 500) / 1000;
。。。。
  if (elapsed_time_ms > 0) {
    第一步 pacing_bitrate_kbps_
    int target_bitrate_kbps = pacing_bitrate_kbps_;
    size_t queue_size_bytes = packets_->SizeInBytes();
    if (queue_size_bytes > 0) {
      // Assuming equal size packets and input/output rate, the average packet
      // has avg_time_left_ms left to get queue_size_bytes out of the queue, if
      // time constraint shall be met. Determine bitrate needed for that.
      packets_->UpdateQueueTime(clock_->TimeInMilliseconds());
      
      drain_large_queues_ 具体什么设置还不知道======
      if (drain_large_queues_) {
        int64_t avg_time_left_ms = std::max(
            1, queue_time_limit - packets_->AverageQueueTimeMs());
        int min_bitrate_needed_kbps =
            static_cast(queue_size_bytes * 8 / avg_time_left_ms);
        if (min_bitrate_needed_kbps > target_bitrate_kbps)
          target_bitrate_kbps = min_bitrate_needed_kbps;
      }
    }

    设置可发送的码率,没有设置成最大,只是设置成合适(就是不会占用全部的带宽,只是预测到网络抖动的最大值即可)
    media_budget_->set_target_rate_kbps(target_bitrate_kbps);
    设置消耗的时间
    UpdateBudgetWithElapsedTime(elapsed_time_ms);
  }
  。。。。

这里有公式计算的

4. pacing_bitrate_kbps_ 是从哪里获取的,接下来需要分析的

PacedSender::SetEstimatedBitrate
PacedSender::SetSendBitrateLimits

5. 参照 WebRTC中PacedSender工作原理和代码分析

你可能感兴趣的:(webrtc学习)