| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 } else { | 45 } else { |
| 46 probing_state_ = kDisabled; | 46 probing_state_ = kDisabled; |
| 47 LOG(LS_INFO) << "Initial bandwidth probing disabled"; | 47 LOG(LS_INFO) << "Initial bandwidth probing disabled"; |
| 48 } | 48 } |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool BitrateProber::IsProbing() const { | 51 bool BitrateProber::IsProbing() const { |
| 52 return probing_state_ == kProbing; | 52 return probing_state_ == kProbing; |
| 53 } | 53 } |
| 54 | 54 |
| 55 void BitrateProber::MaybeInitializeProbe(int bitrate_bps) { | 55 void BitrateProber::OnIncomingPacket(int bitrate_bps, |
| 56 size_t packet_size, |
| 57 int64_t now_ms) { |
| 58 // Don't initialize probing unless we have something large enough to start |
| 59 // probing. |
| 60 if (packet_size < PacedSender::kMinProbePacketSize) |
| 61 return; |
| 56 if (probing_state_ != kAllowedToProbe) | 62 if (probing_state_ != kAllowedToProbe) |
| 57 return; | 63 return; |
| 58 probe_bitrates_.clear(); | 64 probe_bitrates_.clear(); |
| 59 // Max number of packets used for probing. | 65 // Max number of packets used for probing. |
| 60 const int kMaxNumProbes = 2; | 66 const int kMaxNumProbes = 2; |
| 61 const int kPacketsPerProbe = 5; | 67 const int kPacketsPerProbe = 5; |
| 62 const float kProbeBitrateMultipliers[kMaxNumProbes] = {3, 6}; | 68 const float kProbeBitrateMultipliers[kMaxNumProbes] = {3, 6}; |
| 63 int bitrates_bps[kMaxNumProbes]; | 69 int bitrates_bps[kMaxNumProbes]; |
| 64 std::stringstream bitrate_log; | 70 std::stringstream bitrate_log; |
| 65 bitrate_log << "Start probing for bandwidth, bitrates:"; | 71 bitrate_log << "Start probing for bandwidth, bitrates:"; |
| 66 for (int i = 0; i < kMaxNumProbes; ++i) { | 72 for (int i = 0; i < kMaxNumProbes; ++i) { |
| 67 bitrates_bps[i] = kProbeBitrateMultipliers[i] * bitrate_bps; | 73 bitrates_bps[i] = kProbeBitrateMultipliers[i] * bitrate_bps; |
| 68 bitrate_log << " " << bitrates_bps[i]; | 74 bitrate_log << " " << bitrates_bps[i]; |
| 69 // We need one extra to get 5 deltas for the first probe. | 75 // We need one extra to get 5 deltas for the first probe. |
| 70 if (i == 0) | 76 if (i == 0) |
| 71 probe_bitrates_.push_back(bitrates_bps[i]); | 77 probe_bitrates_.push_back(bitrates_bps[i]); |
| 72 for (int j = 0; j < kPacketsPerProbe; ++j) | 78 for (int j = 0; j < kPacketsPerProbe; ++j) |
| 73 probe_bitrates_.push_back(bitrates_bps[i]); | 79 probe_bitrates_.push_back(bitrates_bps[i]); |
| 74 } | 80 } |
| 75 bitrate_log << ", num packets: " << probe_bitrates_.size(); | 81 bitrate_log << ", num packets: " << probe_bitrates_.size(); |
| 76 LOG(LS_INFO) << bitrate_log.str().c_str(); | 82 LOG(LS_INFO) << bitrate_log.str().c_str(); |
| 83 // Set last send time to current time so TimeUntilNextProbe doesn't short |
| 84 // circuit due to inactivity. |
| 85 time_last_send_ms_ = now_ms; |
| 77 probing_state_ = kProbing; | 86 probing_state_ = kProbing; |
| 78 } | 87 } |
| 79 | 88 |
| 80 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { | 89 int BitrateProber::TimeUntilNextProbe(int64_t now_ms) { |
| 81 if (probing_state_ != kDisabled && probe_bitrates_.empty()) { | 90 if (probing_state_ != kDisabled && probe_bitrates_.empty()) { |
| 82 probing_state_ = kWait; | 91 probing_state_ = kWait; |
| 83 } | 92 } |
| 84 if (probe_bitrates_.empty()) { | 93 if (probe_bitrates_.empty() || time_last_send_ms_ == -1) { |
| 85 // No probe started, or waiting for next probe. | 94 // No probe started, probe finished, or too long since last probe packet. |
| 86 return -1; | 95 return -1; |
| 87 } | 96 } |
| 88 int64_t elapsed_time_ms = now_ms - time_last_send_ms_; | 97 int64_t elapsed_time_ms = now_ms - time_last_send_ms_; |
| 98 // If no packets have been sent for n milliseconds, temporarily deactivate to |
| 99 // not keep spinning. |
| 100 static const int kInactiveSendDeltaMs = 5000; |
| 101 if (elapsed_time_ms > kInactiveSendDeltaMs) { |
| 102 time_last_send_ms_ = -1; |
| 103 probing_state_ = kAllowedToProbe; |
| 104 return -1; |
| 105 } |
| 89 // We will send the first probe packet immediately if no packet has been | 106 // We will send the first probe packet immediately if no packet has been |
| 90 // sent before. | 107 // sent before. |
| 91 int time_until_probe_ms = 0; | 108 int time_until_probe_ms = 0; |
| 92 if (packet_size_last_send_ > PacedSender::kMinProbePacketSize && | 109 if (packet_size_last_send_ != 0 && probing_state_ == kProbing) { |
| 93 probing_state_ == kProbing) { | |
| 94 int next_delta_ms = ComputeDeltaFromBitrate(packet_size_last_send_, | 110 int next_delta_ms = ComputeDeltaFromBitrate(packet_size_last_send_, |
| 95 probe_bitrates_.front()); | 111 probe_bitrates_.front()); |
| 96 time_until_probe_ms = next_delta_ms - elapsed_time_ms; | 112 time_until_probe_ms = next_delta_ms - elapsed_time_ms; |
| 97 // There is no point in trying to probe with less than 1 ms between packets | 113 // There is no point in trying to probe with less than 1 ms between packets |
| 98 // as it essentially means trying to probe at infinite bandwidth. | 114 // as it essentially means trying to probe at infinite bandwidth. |
| 99 const int kMinProbeDeltaMs = 1; | 115 const int kMinProbeDeltaMs = 1; |
| 100 // If we have waited more than 3 ms for a new packet to probe with we will | 116 // If we have waited more than 3 ms for a new packet to probe with we will |
| 101 // consider this probing session over. | 117 // consider this probing session over. |
| 102 const int kMaxProbeDelayMs = 3; | 118 const int kMaxProbeDelayMs = 3; |
| 103 if (next_delta_ms < kMinProbeDeltaMs || | 119 if (next_delta_ms < kMinProbeDeltaMs || |
| 104 time_until_probe_ms < -kMaxProbeDelayMs) { | 120 time_until_probe_ms < -kMaxProbeDelayMs) { |
| 105 // We currently disable probing after the first probe, as we only want | 121 // We currently disable probing after the first probe, as we only want |
| 106 // to probe at the beginning of a connection. We should set this to | 122 // to probe at the beginning of a connection. We should set this to |
| 107 // kWait if we later want to probe periodically. | 123 // kWait if we later want to probe periodically. |
| 108 probing_state_ = kWait; | 124 probing_state_ = kWait; |
| 109 LOG(LS_INFO) << "Next delta too small, stop probing."; | 125 LOG(LS_INFO) << "Next delta too small, stop probing."; |
| 110 time_until_probe_ms = 0; | 126 time_until_probe_ms = 0; |
| 111 } | 127 } |
| 112 } | 128 } |
| 113 return std::max(time_until_probe_ms, 0); | 129 return std::max(time_until_probe_ms, 0); |
| 114 } | 130 } |
| 115 | 131 |
| 116 size_t BitrateProber::RecommendedPacketSize() const { | 132 size_t BitrateProber::RecommendedPacketSize() const { |
| 117 return packet_size_last_send_; | 133 return packet_size_last_send_; |
| 118 } | 134 } |
| 119 | 135 |
| 120 void BitrateProber::PacketSent(int64_t now_ms, size_t packet_size) { | 136 void BitrateProber::PacketSent(int64_t now_ms, size_t packet_size) { |
| 121 assert(packet_size > 0); | 137 assert(packet_size > 0); |
| 138 if (packet_size < PacedSender::kMinProbePacketSize) |
| 139 return; |
| 122 packet_size_last_send_ = packet_size; | 140 packet_size_last_send_ = packet_size; |
| 123 time_last_send_ms_ = now_ms; | 141 time_last_send_ms_ = now_ms; |
| 124 if (probing_state_ != kProbing) | 142 if (probing_state_ != kProbing) |
| 125 return; | 143 return; |
| 126 if (!probe_bitrates_.empty()) | 144 if (!probe_bitrates_.empty()) |
| 127 probe_bitrates_.pop_front(); | 145 probe_bitrates_.pop_front(); |
| 128 } | 146 } |
| 129 } // namespace webrtc | 147 } // namespace webrtc |
| OLD | NEW |