| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 | 239 |
| 240 int target_rate_kbps_; | 240 int target_rate_kbps_; |
| 241 int bytes_remaining_; | 241 int bytes_remaining_; |
| 242 }; | 242 }; |
| 243 } // namespace paced_sender | 243 } // namespace paced_sender |
| 244 | 244 |
| 245 const int64_t PacedSender::kMaxQueueLengthMs = 2000; | 245 const int64_t PacedSender::kMaxQueueLengthMs = 2000; |
| 246 const float PacedSender::kDefaultPaceMultiplier = 2.5f; | 246 const float PacedSender::kDefaultPaceMultiplier = 2.5f; |
| 247 | 247 |
| 248 PacedSender::PacedSender(Clock* clock, | 248 PacedSender::PacedSender(Clock* clock, |
| 249 Callback* callback, | 249 PacketSender* packet_sender) |
| 250 int bitrate_kbps, | |
| 251 int max_bitrate_kbps, | |
| 252 int min_bitrate_kbps) | |
| 253 : clock_(clock), | 250 : clock_(clock), |
| 254 callback_(callback), | 251 packet_sender_(packet_sender), |
| 255 critsect_(CriticalSectionWrapper::CreateCriticalSection()), | 252 critsect_(CriticalSectionWrapper::CreateCriticalSection()), |
| 256 paused_(false), | 253 paused_(false), |
| 257 probing_enabled_(true), | 254 probing_enabled_(true), |
| 258 media_budget_(new paced_sender::IntervalBudget(max_bitrate_kbps)), | 255 media_budget_(new paced_sender::IntervalBudget(0)), |
| 259 padding_budget_(new paced_sender::IntervalBudget(min_bitrate_kbps)), | 256 padding_budget_(new paced_sender::IntervalBudget(0)), |
| 260 prober_(new BitrateProber()), | 257 prober_(new BitrateProber()), |
| 261 bitrate_bps_(1000 * bitrate_kbps), | 258 estimated_bitrate_bps_(0), |
| 262 max_bitrate_kbps_(max_bitrate_kbps), | 259 min_send_bitrate_kbps_(0u), |
| 260 pacing_bitrate_kbps_(0), |
| 263 time_last_update_us_(clock->TimeInMicroseconds()), | 261 time_last_update_us_(clock->TimeInMicroseconds()), |
| 264 packets_(new paced_sender::PacketQueue(clock)), | 262 packets_(new paced_sender::PacketQueue(clock)), |
| 265 packet_counter_(0) { | 263 packet_counter_(0) { |
| 266 UpdateBytesPerInterval(kMinPacketLimitMs); | 264 UpdateBytesPerInterval(kMinPacketLimitMs); |
| 267 } | 265 } |
| 268 | 266 |
| 269 PacedSender::~PacedSender() {} | 267 PacedSender::~PacedSender() {} |
| 270 | 268 |
| 271 void PacedSender::Pause() { | 269 void PacedSender::Pause() { |
| 272 CriticalSectionScoped cs(critsect_.get()); | 270 CriticalSectionScoped cs(critsect_.get()); |
| 273 paused_ = true; | 271 paused_ = true; |
| 274 } | 272 } |
| 275 | 273 |
| 276 void PacedSender::Resume() { | 274 void PacedSender::Resume() { |
| 277 CriticalSectionScoped cs(critsect_.get()); | 275 CriticalSectionScoped cs(critsect_.get()); |
| 278 paused_ = false; | 276 paused_ = false; |
| 279 } | 277 } |
| 280 | 278 |
| 281 void PacedSender::SetProbingEnabled(bool enabled) { | 279 void PacedSender::SetProbingEnabled(bool enabled) { |
| 282 RTC_CHECK_EQ(0u, packet_counter_); | 280 RTC_CHECK_EQ(0u, packet_counter_); |
| 283 probing_enabled_ = enabled; | 281 probing_enabled_ = enabled; |
| 284 } | 282 } |
| 285 | 283 |
| 286 void PacedSender::UpdateBitrate(int bitrate_kbps, | 284 void PacedSender::SetEstimatedBitrate(uint32_t bitrate_bps) { |
| 287 int max_bitrate_kbps, | 285 LOG(LS_INFO) << "SetNetWorkEstimateTargetBitrate, bitrate " << bitrate_bps; |
| 288 int min_bitrate_kbps) { | 286 |
| 289 CriticalSectionScoped cs(critsect_.get()); | 287 CriticalSectionScoped cs(critsect_.get()); |
| 290 // Don't set media bitrate here as it may be boosted in order to meet max | 288 estimated_bitrate_bps_ = bitrate_bps; |
| 291 // queue time constraint. Just update max_bitrate_kbps_ and let media_budget_ | 289 pacing_bitrate_kbps_ = |
| 292 // be updated in Process(). | 290 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) * |
| 293 padding_budget_->set_target_rate_kbps(min_bitrate_kbps); | 291 kDefaultPaceMultiplier; |
| 294 bitrate_bps_ = 1000 * bitrate_kbps; | 292 } |
| 295 max_bitrate_kbps_ = max_bitrate_kbps; | 293 |
| 294 void PacedSender::SetAllocatedSendBitrate(int allocated_bitrate, |
| 295 int padding_bitrate) { |
| 296 CriticalSectionScoped cs(critsect_.get()); |
| 297 min_send_bitrate_kbps_ = allocated_bitrate / 1000; |
| 298 pacing_bitrate_kbps_ = |
| 299 std::max(min_send_bitrate_kbps_, estimated_bitrate_bps_ / 1000) * |
| 300 kDefaultPaceMultiplier; |
| 301 padding_budget_->set_target_rate_kbps(padding_bitrate / 1000); |
| 296 } | 302 } |
| 297 | 303 |
| 298 void PacedSender::InsertPacket(RtpPacketSender::Priority priority, | 304 void PacedSender::InsertPacket(RtpPacketSender::Priority priority, |
| 299 uint32_t ssrc, | 305 uint32_t ssrc, |
| 300 uint16_t sequence_number, | 306 uint16_t sequence_number, |
| 301 int64_t capture_time_ms, | 307 int64_t capture_time_ms, |
| 302 size_t bytes, | 308 size_t bytes, |
| 303 bool retransmission) { | 309 bool retransmission) { |
| 304 CriticalSectionScoped cs(critsect_.get()); | 310 CriticalSectionScoped cs(critsect_.get()); |
| 311 RTC_DCHECK(estimated_bitrate_bps_ > 0) |
| 312 << "SetEstimatedBitrate must be called before InsertPacket."; |
| 305 | 313 |
| 306 if (probing_enabled_ && !prober_->IsProbing()) | 314 if (probing_enabled_ && !prober_->IsProbing()) |
| 307 prober_->SetEnabled(true); | 315 prober_->SetEnabled(true); |
| 308 int64_t now_ms = clock_->TimeInMilliseconds(); | 316 int64_t now_ms = clock_->TimeInMilliseconds(); |
| 309 prober_->OnIncomingPacket(bitrate_bps_, bytes, now_ms); | 317 prober_->OnIncomingPacket(estimated_bitrate_bps_, bytes, now_ms); |
| 310 | 318 |
| 311 if (capture_time_ms < 0) | 319 if (capture_time_ms < 0) |
| 312 capture_time_ms = now_ms; | 320 capture_time_ms = now_ms; |
| 313 | 321 |
| 314 packets_->Push(paced_sender::Packet(priority, ssrc, sequence_number, | 322 packets_->Push(paced_sender::Packet(priority, ssrc, sequence_number, |
| 315 capture_time_ms, now_ms, bytes, | 323 capture_time_ms, now_ms, bytes, |
| 316 retransmission, packet_counter_++)); | 324 retransmission, packet_counter_++)); |
| 317 } | 325 } |
| 318 | 326 |
| 319 int64_t PacedSender::ExpectedQueueTimeMs() const { | 327 int64_t PacedSender::ExpectedQueueTimeMs() const { |
| 320 CriticalSectionScoped cs(critsect_.get()); | 328 CriticalSectionScoped cs(critsect_.get()); |
| 321 RTC_DCHECK_GT(max_bitrate_kbps_, 0); | 329 RTC_DCHECK_GT(pacing_bitrate_kbps_, 0u); |
| 322 return static_cast<int64_t>(packets_->SizeInBytes() * 8 / max_bitrate_kbps_); | 330 return static_cast<int64_t>(packets_->SizeInBytes() * 8 / |
| 331 pacing_bitrate_kbps_); |
| 323 } | 332 } |
| 324 | 333 |
| 325 size_t PacedSender::QueueSizePackets() const { | 334 size_t PacedSender::QueueSizePackets() const { |
| 326 CriticalSectionScoped cs(critsect_.get()); | 335 CriticalSectionScoped cs(critsect_.get()); |
| 327 return packets_->SizeInPackets(); | 336 return packets_->SizeInPackets(); |
| 328 } | 337 } |
| 329 | 338 |
| 330 int64_t PacedSender::QueueInMs() const { | 339 int64_t PacedSender::QueueInMs() const { |
| 331 CriticalSectionScoped cs(critsect_.get()); | 340 CriticalSectionScoped cs(critsect_.get()); |
| 332 | 341 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 353 int64_t elapsed_time_us = clock_->TimeInMicroseconds() - time_last_update_us_; | 362 int64_t elapsed_time_us = clock_->TimeInMicroseconds() - time_last_update_us_; |
| 354 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000; | 363 int64_t elapsed_time_ms = (elapsed_time_us + 500) / 1000; |
| 355 return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0); | 364 return std::max<int64_t>(kMinPacketLimitMs - elapsed_time_ms, 0); |
| 356 } | 365 } |
| 357 | 366 |
| 358 void PacedSender::Process() { | 367 void PacedSender::Process() { |
| 359 int64_t now_us = clock_->TimeInMicroseconds(); | 368 int64_t now_us = clock_->TimeInMicroseconds(); |
| 360 CriticalSectionScoped cs(critsect_.get()); | 369 CriticalSectionScoped cs(critsect_.get()); |
| 361 int64_t elapsed_time_ms = (now_us - time_last_update_us_ + 500) / 1000; | 370 int64_t elapsed_time_ms = (now_us - time_last_update_us_ + 500) / 1000; |
| 362 time_last_update_us_ = now_us; | 371 time_last_update_us_ = now_us; |
| 363 int target_bitrate_kbps = max_bitrate_kbps_; | 372 int target_bitrate_kbps = pacing_bitrate_kbps_; |
| 364 // TODO(holmer): Remove the !paused_ check when issue 5307 has been fixed. | 373 // TODO(holmer): Remove the !paused_ check when issue 5307 has been fixed. |
| 365 if (!paused_ && elapsed_time_ms > 0) { | 374 if (!paused_ && elapsed_time_ms > 0) { |
| 366 size_t queue_size_bytes = packets_->SizeInBytes(); | 375 size_t queue_size_bytes = packets_->SizeInBytes(); |
| 367 if (queue_size_bytes > 0) { | 376 if (queue_size_bytes > 0) { |
| 368 // Assuming equal size packets and input/output rate, the average packet | 377 // Assuming equal size packets and input/output rate, the average packet |
| 369 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if | 378 // has avg_time_left_ms left to get queue_size_bytes out of the queue, if |
| 370 // time constraint shall be met. Determine bitrate needed for that. | 379 // time constraint shall be met. Determine bitrate needed for that. |
| 371 packets_->UpdateQueueTime(clock_->TimeInMilliseconds()); | 380 packets_->UpdateQueueTime(clock_->TimeInMilliseconds()); |
| 372 int64_t avg_time_left_ms = std::max<int64_t>( | 381 int64_t avg_time_left_ms = std::max<int64_t>( |
| 373 1, kMaxQueueLengthMs - packets_->AverageQueueTimeMs()); | 382 1, kMaxQueueLengthMs - packets_->AverageQueueTimeMs()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 SendPadding(static_cast<size_t>(padding_needed)); | 427 SendPadding(static_cast<size_t>(padding_needed)); |
| 419 } | 428 } |
| 420 | 429 |
| 421 bool PacedSender::SendPacket(const paced_sender::Packet& packet) { | 430 bool PacedSender::SendPacket(const paced_sender::Packet& packet) { |
| 422 // TODO(holmer): Because of this bug issue 5307 we have to send audio | 431 // TODO(holmer): Because of this bug issue 5307 we have to send audio |
| 423 // packets even when the pacer is paused. Here we assume audio packets are | 432 // packets even when the pacer is paused. Here we assume audio packets are |
| 424 // always high priority and that they are the only high priority packets. | 433 // always high priority and that they are the only high priority packets. |
| 425 if (paused_ && packet.priority != kHighPriority) | 434 if (paused_ && packet.priority != kHighPriority) |
| 426 return false; | 435 return false; |
| 427 critsect_->Leave(); | 436 critsect_->Leave(); |
| 428 const bool success = callback_->TimeToSendPacket(packet.ssrc, | 437 const bool success = packet_sender_->TimeToSendPacket( |
| 429 packet.sequence_number, | 438 packet.ssrc, packet.sequence_number, packet.capture_time_ms, |
| 430 packet.capture_time_ms, | 439 packet.retransmission); |
| 431 packet.retransmission); | |
| 432 critsect_->Enter(); | 440 critsect_->Enter(); |
| 433 | 441 |
| 434 if (success) { | 442 if (success) { |
| 435 prober_->PacketSent(clock_->TimeInMilliseconds(), packet.bytes); | 443 prober_->PacketSent(clock_->TimeInMilliseconds(), packet.bytes); |
| 436 // TODO(holmer): High priority packets should only be accounted for if we | 444 // TODO(holmer): High priority packets should only be accounted for if we |
| 437 // are allocating bandwidth for audio. | 445 // are allocating bandwidth for audio. |
| 438 if (packet.priority != kHighPriority) { | 446 if (packet.priority != kHighPriority) { |
| 439 // Update media bytes sent. | 447 // Update media bytes sent. |
| 440 media_budget_->UseBudget(packet.bytes); | 448 media_budget_->UseBudget(packet.bytes); |
| 441 padding_budget_->UseBudget(packet.bytes); | 449 padding_budget_->UseBudget(packet.bytes); |
| 442 } | 450 } |
| 443 } | 451 } |
| 444 | 452 |
| 445 return success; | 453 return success; |
| 446 } | 454 } |
| 447 | 455 |
| 448 void PacedSender::SendPadding(size_t padding_needed) { | 456 void PacedSender::SendPadding(size_t padding_needed) { |
| 449 critsect_->Leave(); | 457 critsect_->Leave(); |
| 450 size_t bytes_sent = callback_->TimeToSendPadding(padding_needed); | 458 size_t bytes_sent = packet_sender_->TimeToSendPadding(padding_needed); |
| 451 critsect_->Enter(); | 459 critsect_->Enter(); |
| 452 | 460 |
| 453 if (bytes_sent > 0) { | 461 if (bytes_sent > 0) { |
| 454 prober_->PacketSent(clock_->TimeInMilliseconds(), bytes_sent); | 462 prober_->PacketSent(clock_->TimeInMilliseconds(), bytes_sent); |
| 455 media_budget_->UseBudget(bytes_sent); | 463 media_budget_->UseBudget(bytes_sent); |
| 456 padding_budget_->UseBudget(bytes_sent); | 464 padding_budget_->UseBudget(bytes_sent); |
| 457 } | 465 } |
| 458 } | 466 } |
| 459 | 467 |
| 460 void PacedSender::UpdateBytesPerInterval(int64_t delta_time_ms) { | 468 void PacedSender::UpdateBytesPerInterval(int64_t delta_time_ms) { |
| 461 media_budget_->IncreaseBudget(delta_time_ms); | 469 media_budget_->IncreaseBudget(delta_time_ms); |
| 462 padding_budget_->IncreaseBudget(delta_time_ms); | 470 padding_budget_->IncreaseBudget(delta_time_ms); |
| 463 } | 471 } |
| 464 } // namespace webrtc | 472 } // namespace webrtc |
| OLD | NEW |