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 <utility> | 16 #include <utility> |
16 | 17 |
17 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
18 #include "webrtc/base/logging.h" | 19 #include "webrtc/base/logging.h" |
19 #include "webrtc/base/trace_event.h" | 20 #include "webrtc/base/trace_event.h" |
20 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" | 21 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
21 #include "webrtc/modules/video_coding/include/video_coding.h" | 22 #include "webrtc/modules/video_coding/include/video_coding.h" |
22 #include "webrtc/modules/video_coding/frame_buffer.h" | 23 #include "webrtc/modules/video_coding/frame_buffer.h" |
23 #include "webrtc/modules/video_coding/inter_frame_delay.h" | 24 #include "webrtc/modules/video_coding/inter_frame_delay.h" |
24 #include "webrtc/modules/video_coding/internal_defines.h" | 25 #include "webrtc/modules/video_coding/internal_defines.h" |
25 #include "webrtc/modules/video_coding/jitter_buffer_common.h" | 26 #include "webrtc/modules/video_coding/jitter_buffer_common.h" |
26 #include "webrtc/modules/video_coding/jitter_estimator.h" | 27 #include "webrtc/modules/video_coding/jitter_estimator.h" |
27 #include "webrtc/modules/video_coding/packet.h" | 28 #include "webrtc/modules/video_coding/packet.h" |
28 #include "webrtc/system_wrappers/include/clock.h" | 29 #include "webrtc/system_wrappers/include/clock.h" |
29 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" | 30 #include "webrtc/system_wrappers/include/critical_section_wrapper.h" |
30 #include "webrtc/system_wrappers/include/event_wrapper.h" | 31 #include "webrtc/system_wrappers/include/event_wrapper.h" |
| 32 #include "webrtc/system_wrappers/include/field_trial.h" |
31 #include "webrtc/system_wrappers/include/metrics.h" | 33 #include "webrtc/system_wrappers/include/metrics.h" |
32 | 34 |
33 namespace webrtc { | 35 namespace webrtc { |
34 | |
35 // Interval for updating SS data. | 36 // Interval for updating SS data. |
36 static const uint32_t kSsCleanupIntervalSec = 60; | 37 static const uint32_t kSsCleanupIntervalSec = 60; |
37 | 38 |
38 // Use this rtt if no value has been reported. | 39 // Use this rtt if no value has been reported. |
39 static const int64_t kDefaultRtt = 200; | 40 static const int64_t kDefaultRtt = 200; |
40 | 41 |
41 // Request a keyframe if no continuous frame has been received for this | 42 // Request a keyframe if no continuous frame has been received for this |
42 // number of milliseconds and NACKs are disabled. | 43 // number of milliseconds and NACKs are disabled. |
43 static const int64_t kMaxDiscontinuousFramesTime = 1000; | 44 static const int64_t kMaxDiscontinuousFramesTime = 1000; |
44 | 45 |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 SsMap::iterator ss_it; | 208 SsMap::iterator ss_it; |
208 if (Find(frame_it.second->TimeStamp(), &ss_it)) { | 209 if (Find(frame_it.second->TimeStamp(), &ss_it)) { |
209 if (gof_idx >= ss_it->second.num_frames_in_gof) { | 210 if (gof_idx >= ss_it->second.num_frames_in_gof) { |
210 continue; // Assume corresponding SS not yet received. | 211 continue; // Assume corresponding SS not yet received. |
211 } | 212 } |
212 frame_it.second->SetGofInfo(ss_it->second, gof_idx); | 213 frame_it.second->SetGofInfo(ss_it->second, gof_idx); |
213 } | 214 } |
214 } | 215 } |
215 } | 216 } |
216 | 217 |
| 218 static NackModule* |
| 219 MaybeCreateNackModule(Clock* clock, |
| 220 NackSender* nack_sender, |
| 221 KeyFrameRequestSender* keyframe_request_sender) { |
| 222 if (field_trial::FindFullName("WebRTC-NewVideoJitterBuffer") == "Enabled") { |
| 223 // Only create a nack module if a nack sender is available. |
| 224 if (nack_sender) { |
| 225 RTC_DCHECK(keyframe_request_sender); |
| 226 return new NackModule(clock, nack_sender, keyframe_request_sender); |
| 227 } |
| 228 } |
| 229 return nullptr; |
| 230 } |
| 231 |
217 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, | 232 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, |
218 std::unique_ptr<EventWrapper> event) | 233 std::unique_ptr<EventWrapper> event, |
| 234 NackSender* nack_sender, |
| 235 KeyFrameRequestSender* keyframe_request_sender) |
219 : clock_(clock), | 236 : clock_(clock), |
220 running_(false), | 237 running_(false), |
221 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), | 238 crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), |
222 frame_event_(std::move(event)), | 239 frame_event_(std::move(event)), |
223 max_number_of_frames_(kStartNumberOfFrames), | 240 max_number_of_frames_(kStartNumberOfFrames), |
224 free_frames_(), | 241 free_frames_(), |
225 decodable_frames_(), | 242 decodable_frames_(), |
226 incomplete_frames_(), | 243 incomplete_frames_(), |
227 last_decoded_state_(), | 244 last_decoded_state_(), |
228 first_packet_since_reset_(true), | 245 first_packet_since_reset_(true), |
(...skipping 13 matching lines...) Expand all Loading... |
242 rtt_ms_(kDefaultRtt), | 259 rtt_ms_(kDefaultRtt), |
243 nack_mode_(kNoNack), | 260 nack_mode_(kNoNack), |
244 low_rtt_nack_threshold_ms_(-1), | 261 low_rtt_nack_threshold_ms_(-1), |
245 high_rtt_nack_threshold_ms_(-1), | 262 high_rtt_nack_threshold_ms_(-1), |
246 missing_sequence_numbers_(SequenceNumberLessThan()), | 263 missing_sequence_numbers_(SequenceNumberLessThan()), |
247 max_nack_list_size_(0), | 264 max_nack_list_size_(0), |
248 max_packet_age_to_nack_(0), | 265 max_packet_age_to_nack_(0), |
249 max_incomplete_time_ms_(0), | 266 max_incomplete_time_ms_(0), |
250 decode_error_mode_(kNoErrors), | 267 decode_error_mode_(kNoErrors), |
251 average_packets_per_frame_(0.0f), | 268 average_packets_per_frame_(0.0f), |
252 frame_counter_(0) { | 269 frame_counter_(0), |
| 270 nack_module_(MaybeCreateNackModule(clock, |
| 271 nack_sender, |
| 272 keyframe_request_sender)) { |
253 for (int i = 0; i < kStartNumberOfFrames; i++) | 273 for (int i = 0; i < kStartNumberOfFrames; i++) |
254 free_frames_.push_back(new VCMFrameBuffer()); | 274 free_frames_.push_back(new VCMFrameBuffer()); |
255 } | 275 } |
256 | 276 |
257 VCMJitterBuffer::~VCMJitterBuffer() { | 277 VCMJitterBuffer::~VCMJitterBuffer() { |
258 Stop(); | 278 Stop(); |
259 for (UnorderedFrameList::iterator it = free_frames_.begin(); | 279 for (UnorderedFrameList::iterator it = free_frames_.begin(); |
260 it != free_frames_.end(); ++it) { | 280 it != free_frames_.end(); ++it) { |
261 delete *it; | 281 delete *it; |
262 } | 282 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 first_packet_since_reset_ = true; | 342 first_packet_since_reset_ = true; |
323 rtt_ms_ = kDefaultRtt; | 343 rtt_ms_ = kDefaultRtt; |
324 last_decoded_state_.Reset(); | 344 last_decoded_state_.Reset(); |
325 } | 345 } |
326 | 346 |
327 void VCMJitterBuffer::Stop() { | 347 void VCMJitterBuffer::Stop() { |
328 crit_sect_->Enter(); | 348 crit_sect_->Enter(); |
329 UpdateHistograms(); | 349 UpdateHistograms(); |
330 running_ = false; | 350 running_ = false; |
331 last_decoded_state_.Reset(); | 351 last_decoded_state_.Reset(); |
| 352 if (nack_module_) |
| 353 nack_module_->Stop(); |
332 | 354 |
333 // Make sure all frames are free and reset. | 355 // Make sure all frames are free and reset. |
334 for (FrameList::iterator it = decodable_frames_.begin(); | 356 for (FrameList::iterator it = decodable_frames_.begin(); |
335 it != decodable_frames_.end(); ++it) { | 357 it != decodable_frames_.end(); ++it) { |
336 free_frames_.push_back(it->second); | 358 free_frames_.push_back(it->second); |
337 } | 359 } |
338 for (FrameList::iterator it = incomplete_frames_.begin(); | 360 for (FrameList::iterator it = incomplete_frames_.begin(); |
339 it != incomplete_frames_.end(); ++it) { | 361 it != incomplete_frames_.end(); ++it) { |
340 free_frames_.push_back(it->second); | 362 free_frames_.push_back(it->second); |
341 } | 363 } |
(...skipping 20 matching lines...) Expand all Loading... |
362 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. | 384 last_decoded_state_.Reset(); // TODO(mikhal): sync reset. |
363 num_consecutive_old_packets_ = 0; | 385 num_consecutive_old_packets_ = 0; |
364 // Also reset the jitter and delay estimates | 386 // Also reset the jitter and delay estimates |
365 jitter_estimate_.Reset(); | 387 jitter_estimate_.Reset(); |
366 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); | 388 inter_frame_delay_.Reset(clock_->TimeInMilliseconds()); |
367 waiting_for_completion_.frame_size = 0; | 389 waiting_for_completion_.frame_size = 0; |
368 waiting_for_completion_.timestamp = 0; | 390 waiting_for_completion_.timestamp = 0; |
369 waiting_for_completion_.latest_packet_time = -1; | 391 waiting_for_completion_.latest_packet_time = -1; |
370 first_packet_since_reset_ = true; | 392 first_packet_since_reset_ = true; |
371 missing_sequence_numbers_.clear(); | 393 missing_sequence_numbers_.clear(); |
| 394 if (nack_module_) |
| 395 nack_module_->Clear(); |
372 } | 396 } |
373 | 397 |
374 // Get received key and delta frames | 398 // Get received key and delta frames |
375 FrameCounts VCMJitterBuffer::FrameStatistics() const { | 399 FrameCounts VCMJitterBuffer::FrameStatistics() const { |
376 CriticalSectionScoped cs(crit_sect_); | 400 CriticalSectionScoped cs(crit_sect_); |
377 return receive_statistics_; | 401 return receive_statistics_; |
378 } | 402 } |
379 | 403 |
380 int VCMJitterBuffer::num_packets() const { | 404 int VCMJitterBuffer::num_packets() const { |
381 CriticalSectionScoped cs(crit_sect_); | 405 CriticalSectionScoped cs(crit_sect_); |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 // decoder. Propagates the missing_frame bit. | 622 // decoder. Propagates the missing_frame bit. |
599 frame->PrepareForDecode(continuous); | 623 frame->PrepareForDecode(continuous); |
600 | 624 |
601 // We have a frame - update the last decoded state and nack list. | 625 // We have a frame - update the last decoded state and nack list. |
602 last_decoded_state_.SetState(frame); | 626 last_decoded_state_.SetState(frame); |
603 DropPacketsFromNackList(last_decoded_state_.sequence_num()); | 627 DropPacketsFromNackList(last_decoded_state_.sequence_num()); |
604 | 628 |
605 if ((*frame).IsSessionComplete()) | 629 if ((*frame).IsSessionComplete()) |
606 UpdateAveragePacketsPerFrame(frame->NumPackets()); | 630 UpdateAveragePacketsPerFrame(frame->NumPackets()); |
607 | 631 |
| 632 if (nack_module_) |
| 633 nack_module_->ClearUpTo(frame->GetHighSeqNum()); |
608 return frame; | 634 return frame; |
609 } | 635 } |
610 | 636 |
611 // Release frame when done with decoding. Should never be used to release | 637 // Release frame when done with decoding. Should never be used to release |
612 // frames from within the jitter buffer. | 638 // frames from within the jitter buffer. |
613 void VCMJitterBuffer::ReleaseFrame(VCMEncodedFrame* frame) { | 639 void VCMJitterBuffer::ReleaseFrame(VCMEncodedFrame* frame) { |
614 CriticalSectionScoped cs(crit_sect_); | 640 CriticalSectionScoped cs(crit_sect_); |
615 VCMFrameBuffer* frame_buffer = static_cast<VCMFrameBuffer*>(frame); | 641 VCMFrameBuffer* frame_buffer = static_cast<VCMFrameBuffer*>(frame); |
616 if (frame_buffer) { | 642 if (frame_buffer) { |
617 free_frames_.push_back(frame_buffer); | 643 free_frames_.push_back(frame_buffer); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
658 const VCMFrameBuffer* frame_buffer = | 684 const VCMFrameBuffer* frame_buffer = |
659 static_cast<const VCMFrameBuffer*>(frame); | 685 static_cast<const VCMFrameBuffer*>(frame); |
660 *retransmitted = (frame_buffer->GetNackCount() > 0); | 686 *retransmitted = (frame_buffer->GetNackCount() > 0); |
661 return frame_buffer->LatestPacketTimeMs(); | 687 return frame_buffer->LatestPacketTimeMs(); |
662 } | 688 } |
663 | 689 |
664 VCMFrameBufferEnum VCMJitterBuffer::InsertPacket(const VCMPacket& packet, | 690 VCMFrameBufferEnum VCMJitterBuffer::InsertPacket(const VCMPacket& packet, |
665 bool* retransmitted) { | 691 bool* retransmitted) { |
666 CriticalSectionScoped cs(crit_sect_); | 692 CriticalSectionScoped cs(crit_sect_); |
667 | 693 |
| 694 if (nack_module_) |
| 695 nack_module_->OnReceivedPacket(packet); |
| 696 |
668 ++num_packets_; | 697 ++num_packets_; |
669 if (num_packets_ == 1) { | 698 if (num_packets_ == 1) { |
670 time_first_packet_ms_ = clock_->TimeInMilliseconds(); | 699 time_first_packet_ms_ = clock_->TimeInMilliseconds(); |
671 } | 700 } |
672 // Does this packet belong to an old frame? | 701 // Does this packet belong to an old frame? |
673 if (last_decoded_state_.IsOldPacket(&packet)) { | 702 if (last_decoded_state_.IsOldPacket(&packet)) { |
674 // Account only for media packets. | 703 // Account only for media packets. |
675 if (packet.sizeBytes > 0) { | 704 if (packet.sizeBytes > 0) { |
676 num_discarded_packets_++; | 705 num_discarded_packets_++; |
677 num_consecutive_old_packets_++; | 706 num_consecutive_old_packets_++; |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 // when waiting for retransmissions. | 949 // when waiting for retransmissions. |
921 rtt_mult = 0.0f; | 950 rtt_mult = 0.0f; |
922 } | 951 } |
923 return jitter_estimate_.GetJitterEstimate(rtt_mult); | 952 return jitter_estimate_.GetJitterEstimate(rtt_mult); |
924 } | 953 } |
925 | 954 |
926 void VCMJitterBuffer::UpdateRtt(int64_t rtt_ms) { | 955 void VCMJitterBuffer::UpdateRtt(int64_t rtt_ms) { |
927 CriticalSectionScoped cs(crit_sect_); | 956 CriticalSectionScoped cs(crit_sect_); |
928 rtt_ms_ = rtt_ms; | 957 rtt_ms_ = rtt_ms; |
929 jitter_estimate_.UpdateRtt(rtt_ms); | 958 jitter_estimate_.UpdateRtt(rtt_ms); |
| 959 if (nack_module_) |
| 960 nack_module_->UpdateRtt(rtt_ms); |
930 } | 961 } |
931 | 962 |
932 void VCMJitterBuffer::SetNackMode(VCMNackMode mode, | 963 void VCMJitterBuffer::SetNackMode(VCMNackMode mode, |
933 int64_t low_rtt_nack_threshold_ms, | 964 int64_t low_rtt_nack_threshold_ms, |
934 int64_t high_rtt_nack_threshold_ms) { | 965 int64_t high_rtt_nack_threshold_ms) { |
935 CriticalSectionScoped cs(crit_sect_); | 966 CriticalSectionScoped cs(crit_sect_); |
936 nack_mode_ = mode; | 967 nack_mode_ = mode; |
937 if (mode == kNoNack) { | 968 if (mode == kNoNack) { |
938 missing_sequence_numbers_.clear(); | 969 missing_sequence_numbers_.clear(); |
939 } | 970 } |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1039 } else { | 1070 } else { |
1040 // Skip to the last key frame. If it's incomplete we will start | 1071 // Skip to the last key frame. If it's incomplete we will start |
1041 // NACKing it. | 1072 // NACKing it. |
1042 // Note that the estimated low sequence number is correct for VP8 | 1073 // Note that the estimated low sequence number is correct for VP8 |
1043 // streams because only the first packet of a key frame is marked. | 1074 // streams because only the first packet of a key frame is marked. |
1044 last_decoded_state_.Reset(); | 1075 last_decoded_state_.Reset(); |
1045 DropPacketsFromNackList(EstimatedLowSequenceNumber(*rit->second)); | 1076 DropPacketsFromNackList(EstimatedLowSequenceNumber(*rit->second)); |
1046 } | 1077 } |
1047 } | 1078 } |
1048 } | 1079 } |
| 1080 // The experiment is running, the nack module will send Nacks. |
| 1081 // The reason we allow the major part of the GetNackList function to |
| 1082 // run even if we are running the experiment is because this |
| 1083 // function is also used to signal that the jitter buffer wants to |
| 1084 // request a keyframe. |
| 1085 if (nack_module_) |
| 1086 return std::vector<uint16_t>(); |
| 1087 |
1049 std::vector<uint16_t> nack_list(missing_sequence_numbers_.begin(), | 1088 std::vector<uint16_t> nack_list(missing_sequence_numbers_.begin(), |
1050 missing_sequence_numbers_.end()); | 1089 missing_sequence_numbers_.end()); |
1051 return nack_list; | 1090 return nack_list; |
1052 } | 1091 } |
1053 | 1092 |
1054 void VCMJitterBuffer::SetDecodeErrorMode(VCMDecodeErrorMode error_mode) { | 1093 void VCMJitterBuffer::SetDecodeErrorMode(VCMDecodeErrorMode error_mode) { |
1055 CriticalSectionScoped cs(crit_sect_); | 1094 CriticalSectionScoped cs(crit_sect_); |
1056 decode_error_mode_ = error_mode; | 1095 decode_error_mode_ = error_mode; |
1057 } | 1096 } |
1058 | 1097 |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1336 return false; | 1375 return false; |
1337 } | 1376 } |
1338 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in | 1377 // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in |
1339 // that case we don't wait for retransmissions. | 1378 // that case we don't wait for retransmissions. |
1340 if (high_rtt_nack_threshold_ms_ >= 0 && | 1379 if (high_rtt_nack_threshold_ms_ >= 0 && |
1341 rtt_ms_ >= high_rtt_nack_threshold_ms_) { | 1380 rtt_ms_ >= high_rtt_nack_threshold_ms_) { |
1342 return false; | 1381 return false; |
1343 } | 1382 } |
1344 return true; | 1383 return true; |
1345 } | 1384 } |
| 1385 |
| 1386 int64_t VCMJitterBuffer::TimeUntilNextProcess() { |
| 1387 if (nack_module_) |
| 1388 return nack_module_->TimeUntilNextProcess(); |
| 1389 return std::numeric_limits<int64_t>::max(); |
| 1390 } |
| 1391 |
| 1392 void VCMJitterBuffer::Process() { |
| 1393 if (nack_module_) |
| 1394 nack_module_->Process(); |
| 1395 } |
1346 } // namespace webrtc | 1396 } // namespace webrtc |
OLD | NEW |