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