| 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 #include "webrtc/modules/video_coding/jitter_buffer.h" | 10 #include "webrtc/modules/video_coding/jitter_buffer.h" |
| 11 | 11 |
| 12 #include <assert.h> | 12 #include <assert.h> |
| 13 | 13 |
| 14 #include <algorithm> | 14 #include <algorithm> |
| 15 #include <limits> | 15 #include <limits> |
| 16 #include <utility> | 16 #include <utility> |
| 17 | 17 |
| 18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
| 20 #include "webrtc/base/trace_event.h" | 20 #include "webrtc/base/trace_event.h" |
| 21 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | 21 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 22 #include "webrtc/modules/video_coding/include/video_coding.h" | 22 #include "webrtc/modules/video_coding/include/video_coding.h" |
| 23 #include "webrtc/modules/video_coding/frame_buffer.h" | 23 #include "webrtc/modules/video_coding/frame_buffer.h" |
| 24 #include "webrtc/modules/video_coding/inter_frame_delay.h" | 24 #include "webrtc/modules/video_coding/inter_frame_delay.h" |
| 25 #include "webrtc/modules/video_coding/internal_defines.h" | 25 #include "webrtc/modules/video_coding/internal_defines.h" |
| 26 #include "webrtc/modules/video_coding/jitter_buffer_common.h" | 26 #include "webrtc/modules/video_coding/jitter_buffer_common.h" |
| 27 #include "webrtc/modules/video_coding/jitter_estimator.h" | 27 #include "webrtc/modules/video_coding/jitter_estimator.h" |
| 28 #include "webrtc/modules/video_coding/packet.h" | 28 #include "webrtc/modules/video_coding/packet.h" |
| 29 #include "webrtc/system_wrappers/include/clock.h" | 29 #include "webrtc/system_wrappers/include/clock.h" |
| 30 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | |
| 31 #include "webrtc/system_wrappers/include/event_wrapper.h" | 30 #include "webrtc/system_wrappers/include/event_wrapper.h" |
| 32 #include "webrtc/system_wrappers/include/field_trial.h" | 31 #include "webrtc/system_wrappers/include/field_trial.h" |
| 33 #include "webrtc/system_wrappers/include/metrics.h" | 32 #include "webrtc/system_wrappers/include/metrics.h" |
| 34 | 33 |
| 35 namespace webrtc { | 34 namespace webrtc { |
| 36 // Interval for updating SS data. | 35 // Interval for updating SS data. |
| 37 static const uint32_t kSsCleanupIntervalSec = 60; | 36 static const uint32_t kSsCleanupIntervalSec = 60; |
| 38 | 37 |
| 39 // Use this rtt if no value has been reported. | 38 // Use this rtt if no value has been reported. |
| 40 static const int64_t kDefaultRtt = 200; | 39 static const int64_t kDefaultRtt = 200; |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 } | 213 } |
| 215 } | 214 } |
| 216 } | 215 } |
| 217 | 216 |
| 218 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, | 217 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, |
| 219 std::unique_ptr<EventWrapper> event, | 218 std::unique_ptr<EventWrapper> event, |
| 220 NackSender* nack_sender, | 219 NackSender* nack_sender, |
| 221 KeyFrameRequestSender* keyframe_request_sender) | 220 KeyFrameRequestSender* keyframe_request_sender) |
| 222 : clock_(clock), | 221 : clock_(clock), |
| 223 running_(false), | 222 running_(false), |
| 224 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | |
| 225 frame_event_(std::move(event)), | 223 frame_event_(std::move(event)), |
| 226 max_number_of_frames_(kStartNumberOfFrames), | 224 max_number_of_frames_(kStartNumberOfFrames), |
| 227 free_frames_(), | 225 free_frames_(), |
| 228 decodable_frames_(), | 226 decodable_frames_(), |
| 229 incomplete_frames_(), | 227 incomplete_frames_(), |
| 230 last_decoded_state_(), | 228 last_decoded_state_(), |
| 231 first_packet_since_reset_(true), | 229 first_packet_since_reset_(true), |
| 232 stats_callback_(nullptr), | 230 stats_callback_(nullptr), |
| 233 incoming_frame_rate_(0), | 231 incoming_frame_rate_(0), |
| 234 incoming_frame_count_(0), | 232 incoming_frame_count_(0), |
| (...skipping 30 matching lines...) Expand all Loading... |
| 265 delete *it; | 263 delete *it; |
| 266 } | 264 } |
| 267 for (FrameList::iterator it = incomplete_frames_.begin(); | 265 for (FrameList::iterator it = incomplete_frames_.begin(); |
| 268 it != incomplete_frames_.end(); ++it) { | 266 it != incomplete_frames_.end(); ++it) { |
| 269 delete it->second; | 267 delete it->second; |
| 270 } | 268 } |
| 271 for (FrameList::iterator it = decodable_frames_.begin(); | 269 for (FrameList::iterator it = decodable_frames_.begin(); |
| 272 it != decodable_frames_.end(); ++it) { | 270 it != decodable_frames_.end(); ++it) { |
| 273 delete it->second; | 271 delete it->second; |
| 274 } | 272 } |
| 275 delete crit_sect_; | |
| 276 } | 273 } |
| 277 | 274 |
| 278 void VCMJitterBuffer::UpdateHistograms() { | 275 void VCMJitterBuffer::UpdateHistograms() { |
| 279 if (num_packets_ <= 0 || !running_) { | 276 if (num_packets_ <= 0 || !running_) { |
| 280 return; | 277 return; |
| 281 } | 278 } |
| 282 int64_t elapsed_sec = | 279 int64_t elapsed_sec = |
| 283 (clock_->TimeInMilliseconds() - time_first_packet_ms_) / 1000; | 280 (clock_->TimeInMilliseconds() - time_first_packet_ms_) / 1000; |
| 284 if (elapsed_sec < metrics::kMinRunTimeInSeconds) { | 281 if (elapsed_sec < metrics::kMinRunTimeInSeconds) { |
| 285 return; | 282 return; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 297 "WebRTC.Video.CompleteFramesReceivedPerSecond", | 294 "WebRTC.Video.CompleteFramesReceivedPerSecond", |
| 298 static_cast<int>((total_frames / elapsed_sec) + 0.5f)); | 295 static_cast<int>((total_frames / elapsed_sec) + 0.5f)); |
| 299 RTC_HISTOGRAM_COUNTS_1000( | 296 RTC_HISTOGRAM_COUNTS_1000( |
| 300 "WebRTC.Video.KeyFramesReceivedInPermille", | 297 "WebRTC.Video.KeyFramesReceivedInPermille", |
| 301 static_cast<int>( | 298 static_cast<int>( |
| 302 (receive_statistics_.key_frames * 1000.0f / total_frames) + 0.5f)); | 299 (receive_statistics_.key_frames * 1000.0f / total_frames) + 0.5f)); |
| 303 } | 300 } |
| 304 } | 301 } |
| 305 | 302 |
| 306 void VCMJitterBuffer::Start() { | 303 void VCMJitterBuffer::Start() { |
| 307 CriticalSectionScoped cs(crit_sect_); | 304 rtc::CritScope cs(&crit_sect_); |
| 308 running_ = true; | 305 running_ = true; |
| 309 incoming_frame_count_ = 0; | 306 incoming_frame_count_ = 0; |
| 310 incoming_frame_rate_ = 0; | 307 incoming_frame_rate_ = 0; |
| 311 incoming_bit_count_ = 0; | 308 incoming_bit_count_ = 0; |
| 312 incoming_bit_rate_ = 0; | 309 incoming_bit_rate_ = 0; |
| 313 time_last_incoming_frame_count_ = clock_->TimeInMilliseconds(); | 310 time_last_incoming_frame_count_ = clock_->TimeInMilliseconds(); |
| 314 receive_statistics_ = FrameCounts(); | 311 receive_statistics_ = FrameCounts(); |
| 315 | 312 |
| 316 num_consecutive_old_packets_ = 0; | 313 num_consecutive_old_packets_ = 0; |
| 317 num_packets_ = 0; | 314 num_packets_ = 0; |
| 318 num_duplicated_packets_ = 0; | 315 num_duplicated_packets_ = 0; |
| 319 num_discarded_packets_ = 0; | 316 num_discarded_packets_ = 0; |
| 320 time_first_packet_ms_ = 0; | 317 time_first_packet_ms_ = 0; |
| 321 | 318 |
| 322 // Start in a non-signaled state. | 319 // Start in a non-signaled state. |
| 323 waiting_for_completion_.frame_size = 0; | 320 waiting_for_completion_.frame_size = 0; |
| 324 waiting_for_completion_.timestamp = 0; | 321 waiting_for_completion_.timestamp = 0; |
| 325 waiting_for_completion_.latest_packet_time = -1; | 322 waiting_for_completion_.latest_packet_time = -1; |
| 326 first_packet_since_reset_ = true; | 323 first_packet_since_reset_ = true; |
| 327 rtt_ms_ = kDefaultRtt; | 324 rtt_ms_ = kDefaultRtt; |
| 328 last_decoded_state_.Reset(); | 325 last_decoded_state_.Reset(); |
| 329 | 326 |
| 330 decodable_frames_.Reset(&free_frames_); | 327 decodable_frames_.Reset(&free_frames_); |
| 331 incomplete_frames_.Reset(&free_frames_); | 328 incomplete_frames_.Reset(&free_frames_); |
| 332 } | 329 } |
| 333 | 330 |
| 334 void VCMJitterBuffer::Stop() { | 331 void VCMJitterBuffer::Stop() { |
| 335 CriticalSectionScoped cs(crit_sect_); | 332 rtc::CritScope cs(&crit_sect_); |
| 336 UpdateHistograms(); | 333 UpdateHistograms(); |
| 337 running_ = false; | 334 running_ = false; |
| 338 last_decoded_state_.Reset(); | 335 last_decoded_state_.Reset(); |
| 339 | 336 |
| 340 // Make sure we wake up any threads waiting on these events. | 337 // Make sure we wake up any threads waiting on these events. |
| 341 frame_event_->Set(); | 338 frame_event_->Set(); |
| 342 } | 339 } |
| 343 | 340 |
| 344 bool VCMJitterBuffer::Running() const { | 341 bool VCMJitterBuffer::Running() const { |
| 345 CriticalSectionScoped cs(crit_sect_); | 342 rtc::CritScope cs(&crit_sect_); |
| 346 return running_; | 343 return running_; |
| 347 } | 344 } |
| 348 | 345 |
| 349 void VCMJitterBuffer::Flush() { | 346 void VCMJitterBuffer::Flush() { |
| 350 CriticalSectionScoped cs(crit_sect_); | 347 rtc::CritScope cs(&crit_sect_); |
| 351 decodable_frames_.Reset(&free_frames_); | 348 decodable_frames_.Reset(&free_frames_); |
| 352 incomplete_frames_.Reset(&free_frames_); | 349 incomplete_frames_.Reset(&free_frames_); |
| 353 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. | 350 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. |
| 354 num_consecutive_old_packets_ = 0; | 351 num_consecutive_old_packets_ = 0; |
| 355 // Also reset the jitter and delay estimates | 352 // Also reset the jitter and delay estimates |
| 356 jitter_estimate_.Reset(); | 353 jitter_estimate_.Reset(); |
| 357 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); | 354 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); |
| 358 waiting_for_completion_.frame_size = 0; | 355 waiting_for_completion_.frame_size = 0; |
| 359 waiting_for_completion_.timestamp = 0; | 356 waiting_for_completion_.timestamp = 0; |
| 360 waiting_for_completion_.latest_packet_time = -1; | 357 waiting_for_completion_.latest_packet_time = -1; |
| 361 first_packet_since_reset_ = true; | 358 first_packet_since_reset_ = true; |
| 362 missing_sequence_numbers_.clear(); | 359 missing_sequence_numbers_.clear(); |
| 363 } | 360 } |
| 364 | 361 |
| 365 // Get received key and delta frames | 362 // Get received key and delta frames |
| 366 FrameCounts VCMJitterBuffer::FrameStatistics() const { | 363 FrameCounts VCMJitterBuffer::FrameStatistics() const { |
| 367 CriticalSectionScoped cs(crit_sect_); | 364 rtc::CritScope cs(&crit_sect_); |
| 368 return receive_statistics_; | 365 return receive_statistics_; |
| 369 } | 366 } |
| 370 | 367 |
| 371 int VCMJitterBuffer::num_packets() const { | 368 int VCMJitterBuffer::num_packets() const { |
| 372 CriticalSectionScoped cs(crit_sect_); | 369 rtc::CritScope cs(&crit_sect_); |
| 373 return num_packets_; | 370 return num_packets_; |
| 374 } | 371 } |
| 375 | 372 |
| 376 int VCMJitterBuffer::num_duplicated_packets() const { | 373 int VCMJitterBuffer::num_duplicated_packets() const { |
| 377 CriticalSectionScoped cs(crit_sect_); | 374 rtc::CritScope cs(&crit_sect_); |
| 378 return num_duplicated_packets_; | 375 return num_duplicated_packets_; |
| 379 } | 376 } |
| 380 | 377 |
| 381 int VCMJitterBuffer::num_discarded_packets() const { | 378 int VCMJitterBuffer::num_discarded_packets() const { |
| 382 CriticalSectionScoped cs(crit_sect_); | 379 rtc::CritScope cs(&crit_sect_); |
| 383 return num_discarded_packets_; | 380 return num_discarded_packets_; |
| 384 } | 381 } |
| 385 | 382 |
| 386 // Calculate framerate and bitrate. | 383 // Calculate framerate and bitrate. |
| 387 void VCMJitterBuffer::IncomingRateStatistics(unsigned int* framerate, | 384 void VCMJitterBuffer::IncomingRateStatistics(unsigned int* framerate, |
| 388 unsigned int* bitrate) { | 385 unsigned int* bitrate) { |
| 389 assert(framerate); | 386 assert(framerate); |
| 390 assert(bitrate); | 387 assert(bitrate); |
| 391 CriticalSectionScoped cs(crit_sect_); | 388 rtc::CritScope cs(&crit_sect_); |
| 392 const int64_t now = clock_->TimeInMilliseconds(); | 389 const int64_t now = clock_->TimeInMilliseconds(); |
| 393 int64_t diff = now - time_last_incoming_frame_count_; | 390 int64_t diff = now - time_last_incoming_frame_count_; |
| 394 if (diff < 1000 && incoming_frame_rate_ > 0 && incoming_bit_rate_ > 0) { | 391 if (diff < 1000 && incoming_frame_rate_ > 0 && incoming_bit_rate_ > 0) { |
| 395 // Make sure we report something even though less than | 392 // Make sure we report something even though less than |
| 396 // 1 second has passed since last update. | 393 // 1 second has passed since last update. |
| 397 *framerate = incoming_frame_rate_; | 394 *framerate = incoming_frame_rate_; |
| 398 *bitrate = incoming_bit_rate_; | 395 *bitrate = incoming_bit_rate_; |
| 399 } else if (incoming_frame_count_ != 0) { | 396 } else if (incoming_frame_count_ != 0) { |
| 400 // We have received frame(s) since last call to this function | 397 // We have received frame(s) since last call to this function |
| 401 | 398 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 *framerate = 0; | 435 *framerate = 0; |
| 439 *bitrate = 0; | 436 *bitrate = 0; |
| 440 incoming_frame_rate_ = 0; | 437 incoming_frame_rate_ = 0; |
| 441 incoming_bit_rate_ = 0; | 438 incoming_bit_rate_ = 0; |
| 442 } | 439 } |
| 443 } | 440 } |
| 444 | 441 |
| 445 // Returns immediately or a |max_wait_time_ms| ms event hang waiting for a | 442 // Returns immediately or a |max_wait_time_ms| ms event hang waiting for a |
| 446 // complete frame, |max_wait_time_ms| decided by caller. | 443 // complete frame, |max_wait_time_ms| decided by caller. |
| 447 VCMEncodedFrame* VCMJitterBuffer::NextCompleteFrame(uint32_t max_wait_time_ms) { | 444 VCMEncodedFrame* VCMJitterBuffer::NextCompleteFrame(uint32_t max_wait_time_ms) { |
| 448 crit_sect_->Enter(); | 445 crit_sect_.Enter(); |
| 449 if (!running_) { | 446 if (!running_) { |
| 450 crit_sect_->Leave(); | 447 crit_sect_.Leave(); |
| 451 return nullptr; | 448 return nullptr; |
| 452 } | 449 } |
| 453 CleanUpOldOrEmptyFrames(); | 450 CleanUpOldOrEmptyFrames(); |
| 454 | 451 |
| 455 if (decodable_frames_.empty() || | 452 if (decodable_frames_.empty() || |
| 456 decodable_frames_.Front()->GetState() != kStateComplete) { | 453 decodable_frames_.Front()->GetState() != kStateComplete) { |
| 457 const int64_t end_wait_time_ms = | 454 const int64_t end_wait_time_ms = |
| 458 clock_->TimeInMilliseconds() + max_wait_time_ms; | 455 clock_->TimeInMilliseconds() + max_wait_time_ms; |
| 459 int64_t wait_time_ms = max_wait_time_ms; | 456 int64_t wait_time_ms = max_wait_time_ms; |
| 460 while (wait_time_ms > 0) { | 457 while (wait_time_ms > 0) { |
| 461 crit_sect_->Leave(); | 458 crit_sect_.Leave(); |
| 462 const EventTypeWrapper ret = | 459 const EventTypeWrapper ret = |
| 463 frame_event_->Wait(static_cast<uint32_t>(wait_time_ms)); | 460 frame_event_->Wait(static_cast<uint32_t>(wait_time_ms)); |
| 464 crit_sect_->Enter(); | 461 crit_sect_.Enter(); |
| 465 if (ret == kEventSignaled) { | 462 if (ret == kEventSignaled) { |
| 466 // Are we shutting down the jitter buffer? | 463 // Are we shutting down the jitter buffer? |
| 467 if (!running_) { | 464 if (!running_) { |
| 468 crit_sect_->Leave(); | 465 crit_sect_.Leave(); |
| 469 return nullptr; | 466 return nullptr; |
| 470 } | 467 } |
| 471 // Finding oldest frame ready for decoder. | 468 // Finding oldest frame ready for decoder. |
| 472 CleanUpOldOrEmptyFrames(); | 469 CleanUpOldOrEmptyFrames(); |
| 473 if (decodable_frames_.empty() || | 470 if (decodable_frames_.empty() || |
| 474 decodable_frames_.Front()->GetState() != kStateComplete) { | 471 decodable_frames_.Front()->GetState() != kStateComplete) { |
| 475 wait_time_ms = end_wait_time_ms - clock_->TimeInMilliseconds(); | 472 wait_time_ms = end_wait_time_ms - clock_->TimeInMilliseconds(); |
| 476 } else { | 473 } else { |
| 477 break; | 474 break; |
| 478 } | 475 } |
| 479 } else { | 476 } else { |
| 480 break; | 477 break; |
| 481 } | 478 } |
| 482 } | 479 } |
| 483 } | 480 } |
| 484 if (decodable_frames_.empty() || | 481 if (decodable_frames_.empty() || |
| 485 decodable_frames_.Front()->GetState() != kStateComplete) { | 482 decodable_frames_.Front()->GetState() != kStateComplete) { |
| 486 crit_sect_->Leave(); | 483 crit_sect_.Leave(); |
| 487 return nullptr; | 484 return nullptr; |
| 488 } | 485 } |
| 489 VCMEncodedFrame* encoded_frame = decodable_frames_.Front(); | 486 VCMEncodedFrame* encoded_frame = decodable_frames_.Front(); |
| 490 crit_sect_->Leave(); | 487 crit_sect_.Leave(); |
| 491 return encoded_frame; | 488 return encoded_frame; |
| 492 } | 489 } |
| 493 | 490 |
| 494 bool VCMJitterBuffer::NextMaybeIncompleteTimestamp(uint32_t* timestamp) { | 491 bool VCMJitterBuffer::NextMaybeIncompleteTimestamp(uint32_t* timestamp) { |
| 495 CriticalSectionScoped cs(crit_sect_); | 492 rtc::CritScope cs(&crit_sect_); |
| 496 if (!running_) { | 493 if (!running_) { |
| 497 return false; | 494 return false; |
| 498 } | 495 } |
| 499 if (decode_error_mode_ == kNoErrors) { | 496 if (decode_error_mode_ == kNoErrors) { |
| 500 // No point to continue, as we are not decoding with errors. | 497 // No point to continue, as we are not decoding with errors. |
| 501 return false; | 498 return false; |
| 502 } | 499 } |
| 503 | 500 |
| 504 CleanUpOldOrEmptyFrames(); | 501 CleanUpOldOrEmptyFrames(); |
| 505 | 502 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 522 oldest_frame->GetState() != kStateComplete) { | 519 oldest_frame->GetState() != kStateComplete) { |
| 523 return false; | 520 return false; |
| 524 } | 521 } |
| 525 } | 522 } |
| 526 | 523 |
| 527 *timestamp = oldest_frame->TimeStamp(); | 524 *timestamp = oldest_frame->TimeStamp(); |
| 528 return true; | 525 return true; |
| 529 } | 526 } |
| 530 | 527 |
| 531 VCMEncodedFrame* VCMJitterBuffer::ExtractAndSetDecode(uint32_t timestamp) { | 528 VCMEncodedFrame* VCMJitterBuffer::ExtractAndSetDecode(uint32_t timestamp) { |
| 532 CriticalSectionScoped cs(crit_sect_); | 529 rtc::CritScope cs(&crit_sect_); |
| 533 if (!running_) { | 530 if (!running_) { |
| 534 return NULL; | 531 return NULL; |
| 535 } | 532 } |
| 536 // Extract the frame with the desired timestamp. | 533 // Extract the frame with the desired timestamp. |
| 537 VCMFrameBuffer* frame = decodable_frames_.PopFrame(timestamp); | 534 VCMFrameBuffer* frame = decodable_frames_.PopFrame(timestamp); |
| 538 bool continuous = true; | 535 bool continuous = true; |
| 539 if (!frame) { | 536 if (!frame) { |
| 540 frame = incomplete_frames_.PopFrame(timestamp); | 537 frame = incomplete_frames_.PopFrame(timestamp); |
| 541 if (frame) | 538 if (frame) |
| 542 continuous = last_decoded_state_.ContinuousFrame(frame); | 539 continuous = last_decoded_state_.ContinuousFrame(frame); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 if ((*frame).IsSessionComplete()) | 573 if ((*frame).IsSessionComplete()) |
| 577 UpdateAveragePacketsPerFrame(frame->NumPackets()); | 574 UpdateAveragePacketsPerFrame(frame->NumPackets()); |
| 578 | 575 |
| 579 return frame; | 576 return frame; |
| 580 } | 577 } |
| 581 | 578 |
| 582 // Release frame when done with decoding. Should never be used to release | 579 // Release frame when done with decoding. Should never be used to release |
| 583 // frames from within the jitter buffer. | 580 // frames from within the jitter buffer. |
| 584 void VCMJitterBuffer::ReleaseFrame(VCMEncodedFrame* frame) { | 581 void VCMJitterBuffer::ReleaseFrame(VCMEncodedFrame* frame) { |
| 585 RTC_CHECK(frame != nullptr); | 582 RTC_CHECK(frame != nullptr); |
| 586 CriticalSectionScoped cs(crit_sect_); | 583 rtc::CritScope cs(&crit_sect_); |
| 587 VCMFrameBuffer* frame_buffer = static_cast<VCMFrameBuffer*>(frame); | 584 VCMFrameBuffer* frame_buffer = static_cast<VCMFrameBuffer*>(frame); |
| 588 RecycleFrameBuffer(frame_buffer); | 585 RecycleFrameBuffer(frame_buffer); |
| 589 } | 586 } |
| 590 | 587 |
| 591 // Gets frame to use for this timestamp. If no match, get empty frame. | 588 // Gets frame to use for this timestamp. If no match, get empty frame. |
| 592 VCMFrameBufferEnum VCMJitterBuffer::GetFrame(const VCMPacket& packet, | 589 VCMFrameBufferEnum VCMJitterBuffer::GetFrame(const VCMPacket& packet, |
| 593 VCMFrameBuffer** frame, | 590 VCMFrameBuffer** frame, |
| 594 FrameList** frame_list) { | 591 FrameList** frame_list) { |
| 595 *frame = incomplete_frames_.PopFrame(packet.timestamp); | 592 *frame = incomplete_frames_.PopFrame(packet.timestamp); |
| 596 if (*frame != NULL) { | 593 if (*frame != NULL) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 617 return kFlushIndicator; | 614 return kFlushIndicator; |
| 618 } | 615 } |
| 619 } | 616 } |
| 620 (*frame)->Reset(); | 617 (*frame)->Reset(); |
| 621 return kNoError; | 618 return kNoError; |
| 622 } | 619 } |
| 623 | 620 |
| 624 int64_t VCMJitterBuffer::LastPacketTime(const VCMEncodedFrame* frame, | 621 int64_t VCMJitterBuffer::LastPacketTime(const VCMEncodedFrame* frame, |
| 625 bool* retransmitted) const { | 622 bool* retransmitted) const { |
| 626 assert(retransmitted); | 623 assert(retransmitted); |
| 627 CriticalSectionScoped cs(crit_sect_); | 624 rtc::CritScope cs(&crit_sect_); |
| 628 const VCMFrameBuffer* frame_buffer = | 625 const VCMFrameBuffer* frame_buffer = |
| 629 static_cast<const VCMFrameBuffer*>(frame); | 626 static_cast<const VCMFrameBuffer*>(frame); |
| 630 *retransmitted = (frame_buffer->GetNackCount() > 0); | 627 *retransmitted = (frame_buffer->GetNackCount() > 0); |
| 631 return frame_buffer->LatestPacketTimeMs(); | 628 return frame_buffer->LatestPacketTimeMs(); |
| 632 } | 629 } |
| 633 | 630 |
| 634 VCMFrameBufferEnum VCMJitterBuffer::InsertPacket(const VCMPacket& packet, | 631 VCMFrameBufferEnum VCMJitterBuffer::InsertPacket(const VCMPacket& packet, |
| 635 bool* retransmitted) { | 632 bool* retransmitted) { |
| 636 CriticalSectionScoped cs(crit_sect_); | 633 rtc::CritScope cs(&crit_sect_); |
| 637 | 634 |
| 638 ++num_packets_; | 635 ++num_packets_; |
| 639 if (num_packets_ == 1) { | 636 if (num_packets_ == 1) { |
| 640 time_first_packet_ms_ = clock_->TimeInMilliseconds(); | 637 time_first_packet_ms_ = clock_->TimeInMilliseconds(); |
| 641 } | 638 } |
| 642 // Does this packet belong to an old frame? | 639 // Does this packet belong to an old frame? |
| 643 if (last_decoded_state_.IsOldPacket(&packet)) { | 640 if (last_decoded_state_.IsOldPacket(&packet)) { |
| 644 // Account only for media packets. | 641 // Account only for media packets. |
| 645 if (packet.sizeBytes > 0) { | 642 if (packet.sizeBytes > 0) { |
| 646 num_discarded_packets_++; | 643 num_discarded_packets_++; |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 873 decoding_state.SetState(frame); | 870 decoding_state.SetState(frame); |
| 874 } else if (frame->TemporalId() <= 0) { | 871 } else if (frame->TemporalId() <= 0) { |
| 875 break; | 872 break; |
| 876 } else { | 873 } else { |
| 877 ++it; | 874 ++it; |
| 878 } | 875 } |
| 879 } | 876 } |
| 880 } | 877 } |
| 881 | 878 |
| 882 uint32_t VCMJitterBuffer::EstimatedJitterMs() { | 879 uint32_t VCMJitterBuffer::EstimatedJitterMs() { |
| 883 CriticalSectionScoped cs(crit_sect_); | 880 rtc::CritScope cs(&crit_sect_); |
| 884 // Compute RTT multiplier for estimation. | 881 // Compute RTT multiplier for estimation. |
| 885 // low_rtt_nackThresholdMs_ == -1 means no FEC. | 882 // low_rtt_nackThresholdMs_ == -1 means no FEC. |
| 886 double rtt_mult = 1.0f; | 883 double rtt_mult = 1.0f; |
| 887 if (low_rtt_nack_threshold_ms_ >= 0 && | 884 if (low_rtt_nack_threshold_ms_ >= 0 && |
| 888 rtt_ms_ >= low_rtt_nack_threshold_ms_) { | 885 rtt_ms_ >= low_rtt_nack_threshold_ms_) { |
| 889 // For RTTs above low_rtt_nack_threshold_ms_ we don't apply extra delay | 886 // For RTTs above low_rtt_nack_threshold_ms_ we don't apply extra delay |
| 890 // when waiting for retransmissions. | 887 // when waiting for retransmissions. |
| 891 rtt_mult = 0.0f; | 888 rtt_mult = 0.0f; |
| 892 } | 889 } |
| 893 return jitter_estimate_.GetJitterEstimate(rtt_mult); | 890 return jitter_estimate_.GetJitterEstimate(rtt_mult); |
| 894 } | 891 } |
| 895 | 892 |
| 896 void VCMJitterBuffer::UpdateRtt(int64_t rtt_ms) { | 893 void VCMJitterBuffer::UpdateRtt(int64_t rtt_ms) { |
| 897 CriticalSectionScoped cs(crit_sect_); | 894 rtc::CritScope cs(&crit_sect_); |
| 898 rtt_ms_ = rtt_ms; | 895 rtt_ms_ = rtt_ms; |
| 899 jitter_estimate_.UpdateRtt(rtt_ms); | 896 jitter_estimate_.UpdateRtt(rtt_ms); |
| 900 if (!WaitForRetransmissions()) | 897 if (!WaitForRetransmissions()) |
| 901 jitter_estimate_.ResetNackCount(); | 898 jitter_estimate_.ResetNackCount(); |
| 902 } | 899 } |
| 903 | 900 |
| 904 void VCMJitterBuffer::SetNackMode(VCMNackMode mode, | 901 void VCMJitterBuffer::SetNackMode(VCMNackMode mode, |
| 905 int64_t low_rtt_nack_threshold_ms, | 902 int64_t low_rtt_nack_threshold_ms, |
| 906 int64_t high_rtt_nack_threshold_ms) { | 903 int64_t high_rtt_nack_threshold_ms) { |
| 907 CriticalSectionScoped cs(crit_sect_); | 904 rtc::CritScope cs(&crit_sect_); |
| 908 nack_mode_ = mode; | 905 nack_mode_ = mode; |
| 909 if (mode == kNoNack) { | 906 if (mode == kNoNack) { |
| 910 missing_sequence_numbers_.clear(); | 907 missing_sequence_numbers_.clear(); |
| 911 } | 908 } |
| 912 assert(low_rtt_nack_threshold_ms >= -1 && high_rtt_nack_threshold_ms >= -1); | 909 assert(low_rtt_nack_threshold_ms >= -1 && high_rtt_nack_threshold_ms >= -1); |
| 913 assert(high_rtt_nack_threshold_ms == -1 || | 910 assert(high_rtt_nack_threshold_ms == -1 || |
| 914 low_rtt_nack_threshold_ms <= high_rtt_nack_threshold_ms); | 911 low_rtt_nack_threshold_ms <= high_rtt_nack_threshold_ms); |
| 915 assert(low_rtt_nack_threshold_ms > -1 || high_rtt_nack_threshold_ms == -1); | 912 assert(low_rtt_nack_threshold_ms > -1 || high_rtt_nack_threshold_ms == -1); |
| 916 low_rtt_nack_threshold_ms_ = low_rtt_nack_threshold_ms; | 913 low_rtt_nack_threshold_ms_ = low_rtt_nack_threshold_ms; |
| 917 high_rtt_nack_threshold_ms_ = high_rtt_nack_threshold_ms; | 914 high_rtt_nack_threshold_ms_ = high_rtt_nack_threshold_ms; |
| 918 // Don't set a high start rtt if high_rtt_nack_threshold_ms_ is used, to not | 915 // Don't set a high start rtt if high_rtt_nack_threshold_ms_ is used, to not |
| 919 // disable NACK in |kNack| mode. | 916 // disable NACK in |kNack| mode. |
| 920 if (rtt_ms_ == kDefaultRtt && high_rtt_nack_threshold_ms_ != -1) { | 917 if (rtt_ms_ == kDefaultRtt && high_rtt_nack_threshold_ms_ != -1) { |
| 921 rtt_ms_ = 0; | 918 rtt_ms_ = 0; |
| 922 } | 919 } |
| 923 if (!WaitForRetransmissions()) { | 920 if (!WaitForRetransmissions()) { |
| 924 jitter_estimate_.ResetNackCount(); | 921 jitter_estimate_.ResetNackCount(); |
| 925 } | 922 } |
| 926 } | 923 } |
| 927 | 924 |
| 928 void VCMJitterBuffer::SetNackSettings(size_t max_nack_list_size, | 925 void VCMJitterBuffer::SetNackSettings(size_t max_nack_list_size, |
| 929 int max_packet_age_to_nack, | 926 int max_packet_age_to_nack, |
| 930 int max_incomplete_time_ms) { | 927 int max_incomplete_time_ms) { |
| 931 CriticalSectionScoped cs(crit_sect_); | 928 rtc::CritScope cs(&crit_sect_); |
| 932 assert(max_packet_age_to_nack >= 0); | 929 assert(max_packet_age_to_nack >= 0); |
| 933 assert(max_incomplete_time_ms_ >= 0); | 930 assert(max_incomplete_time_ms_ >= 0); |
| 934 max_nack_list_size_ = max_nack_list_size; | 931 max_nack_list_size_ = max_nack_list_size; |
| 935 max_packet_age_to_nack_ = max_packet_age_to_nack; | 932 max_packet_age_to_nack_ = max_packet_age_to_nack; |
| 936 max_incomplete_time_ms_ = max_incomplete_time_ms; | 933 max_incomplete_time_ms_ = max_incomplete_time_ms; |
| 937 } | 934 } |
| 938 | 935 |
| 939 VCMNackMode VCMJitterBuffer::nack_mode() const { | 936 VCMNackMode VCMJitterBuffer::nack_mode() const { |
| 940 CriticalSectionScoped cs(crit_sect_); | 937 rtc::CritScope cs(&crit_sect_); |
| 941 return nack_mode_; | 938 return nack_mode_; |
| 942 } | 939 } |
| 943 | 940 |
| 944 int VCMJitterBuffer::NonContinuousOrIncompleteDuration() { | 941 int VCMJitterBuffer::NonContinuousOrIncompleteDuration() { |
| 945 if (incomplete_frames_.empty()) { | 942 if (incomplete_frames_.empty()) { |
| 946 return 0; | 943 return 0; |
| 947 } | 944 } |
| 948 uint32_t start_timestamp = incomplete_frames_.Front()->TimeStamp(); | 945 uint32_t start_timestamp = incomplete_frames_.Front()->TimeStamp(); |
| 949 if (!decodable_frames_.empty()) { | 946 if (!decodable_frames_.empty()) { |
| 950 start_timestamp = decodable_frames_.Back()->TimeStamp(); | 947 start_timestamp = decodable_frames_.Back()->TimeStamp(); |
| 951 } | 948 } |
| 952 return incomplete_frames_.Back()->TimeStamp() - start_timestamp; | 949 return incomplete_frames_.Back()->TimeStamp() - start_timestamp; |
| 953 } | 950 } |
| 954 | 951 |
| 955 uint16_t VCMJitterBuffer::EstimatedLowSequenceNumber( | 952 uint16_t VCMJitterBuffer::EstimatedLowSequenceNumber( |
| 956 const VCMFrameBuffer& frame) const { | 953 const VCMFrameBuffer& frame) const { |
| 957 assert(frame.GetLowSeqNum() >= 0); | 954 assert(frame.GetLowSeqNum() >= 0); |
| 958 if (frame.HaveFirstPacket()) | 955 if (frame.HaveFirstPacket()) |
| 959 return frame.GetLowSeqNum(); | 956 return frame.GetLowSeqNum(); |
| 960 | 957 |
| 961 // This estimate is not accurate if more than one packet with lower sequence | 958 // This estimate is not accurate if more than one packet with lower sequence |
| 962 // number is lost. | 959 // number is lost. |
| 963 return frame.GetLowSeqNum() - 1; | 960 return frame.GetLowSeqNum() - 1; |
| 964 } | 961 } |
| 965 | 962 |
| 966 std::vector<uint16_t> VCMJitterBuffer::GetNackList(bool* request_key_frame) { | 963 std::vector<uint16_t> VCMJitterBuffer::GetNackList(bool* request_key_frame) { |
| 967 CriticalSectionScoped cs(crit_sect_); | 964 rtc::CritScope cs(&crit_sect_); |
| 968 *request_key_frame = false; | 965 *request_key_frame = false; |
| 969 if (nack_mode_ == kNoNack) { | 966 if (nack_mode_ == kNoNack) { |
| 970 return std::vector<uint16_t>(); | 967 return std::vector<uint16_t>(); |
| 971 } | 968 } |
| 972 if (last_decoded_state_.in_initial_state()) { | 969 if (last_decoded_state_.in_initial_state()) { |
| 973 VCMFrameBuffer* next_frame = NextFrame(); | 970 VCMFrameBuffer* next_frame = NextFrame(); |
| 974 const bool first_frame_is_key = next_frame && | 971 const bool first_frame_is_key = next_frame && |
| 975 next_frame->FrameType() == kVideoFrameKey && | 972 next_frame->FrameType() == kVideoFrameKey && |
| 976 next_frame->HaveFirstPacket(); | 973 next_frame->HaveFirstPacket(); |
| 977 if (!first_frame_is_key) { | 974 if (!first_frame_is_key) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1017 DropPacketsFromNackList(EstimatedLowSequenceNumber(*rit->second)); | 1014 DropPacketsFromNackList(EstimatedLowSequenceNumber(*rit->second)); |
| 1018 } | 1015 } |
| 1019 } | 1016 } |
| 1020 } | 1017 } |
| 1021 std::vector<uint16_t> nack_list(missing_sequence_numbers_.begin(), | 1018 std::vector<uint16_t> nack_list(missing_sequence_numbers_.begin(), |
| 1022 missing_sequence_numbers_.end()); | 1019 missing_sequence_numbers_.end()); |
| 1023 return nack_list; | 1020 return nack_list; |
| 1024 } | 1021 } |
| 1025 | 1022 |
| 1026 void VCMJitterBuffer::SetDecodeErrorMode(VCMDecodeErrorMode error_mode) { | 1023 void VCMJitterBuffer::SetDecodeErrorMode(VCMDecodeErrorMode error_mode) { |
| 1027 CriticalSectionScoped cs(crit_sect_); | 1024 rtc::CritScope cs(&crit_sect_); |
| 1028 decode_error_mode_ = error_mode; | 1025 decode_error_mode_ = error_mode; |
| 1029 } | 1026 } |
| 1030 | 1027 |
| 1031 VCMFrameBuffer* VCMJitterBuffer::NextFrame() const { | 1028 VCMFrameBuffer* VCMJitterBuffer::NextFrame() const { |
| 1032 if (!decodable_frames_.empty()) | 1029 if (!decodable_frames_.empty()) |
| 1033 return decodable_frames_.Front(); | 1030 return decodable_frames_.Front(); |
| 1034 if (!incomplete_frames_.empty()) | 1031 if (!incomplete_frames_.empty()) |
| 1035 return incomplete_frames_.Front(); | 1032 return incomplete_frames_.Front(); |
| 1036 return NULL; | 1033 return NULL; |
| 1037 } | 1034 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1117 uint16_t last_decoded_sequence_number) { | 1114 uint16_t last_decoded_sequence_number) { |
| 1118 // Erase all sequence numbers from the NACK list which we won't need any | 1115 // Erase all sequence numbers from the NACK list which we won't need any |
| 1119 // longer. | 1116 // longer. |
| 1120 missing_sequence_numbers_.erase( | 1117 missing_sequence_numbers_.erase( |
| 1121 missing_sequence_numbers_.begin(), | 1118 missing_sequence_numbers_.begin(), |
| 1122 missing_sequence_numbers_.upper_bound(last_decoded_sequence_number)); | 1119 missing_sequence_numbers_.upper_bound(last_decoded_sequence_number)); |
| 1123 } | 1120 } |
| 1124 | 1121 |
| 1125 void VCMJitterBuffer::RegisterStatsCallback( | 1122 void VCMJitterBuffer::RegisterStatsCallback( |
| 1126 VCMReceiveStatisticsCallback* callback) { | 1123 VCMReceiveStatisticsCallback* callback) { |
| 1127 CriticalSectionScoped cs(crit_sect_); | 1124 rtc::CritScope cs(&crit_sect_); |
| 1128 stats_callback_ = callback; | 1125 stats_callback_ = callback; |
| 1129 } | 1126 } |
| 1130 | 1127 |
| 1131 VCMFrameBuffer* VCMJitterBuffer::GetEmptyFrame() { | 1128 VCMFrameBuffer* VCMJitterBuffer::GetEmptyFrame() { |
| 1132 if (free_frames_.empty()) { | 1129 if (free_frames_.empty()) { |
| 1133 if (!TryToIncreaseJitterBufferSize()) { | 1130 if (!TryToIncreaseJitterBufferSize()) { |
| 1134 return NULL; | 1131 return NULL; |
| 1135 } | 1132 } |
| 1136 } | 1133 } |
| 1137 VCMFrameBuffer* frame = free_frames_.front(); | 1134 VCMFrameBuffer* frame = free_frames_.front(); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1301 } | 1298 } |
| 1302 return true; | 1299 return true; |
| 1303 } | 1300 } |
| 1304 | 1301 |
| 1305 void VCMJitterBuffer::RecycleFrameBuffer(VCMFrameBuffer* frame) { | 1302 void VCMJitterBuffer::RecycleFrameBuffer(VCMFrameBuffer* frame) { |
| 1306 frame->Reset(); | 1303 frame->Reset(); |
| 1307 free_frames_.push_back(frame); | 1304 free_frames_.push_back(frame); |
| 1308 } | 1305 } |
| 1309 | 1306 |
| 1310 } // namespace webrtc | 1307 } // namespace webrtc |
| OLD | NEW |