• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
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
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #include "webrtc/modules/video_coding/main/source/jitter_buffer.h"
11 
12 #include <assert.h>
13 
14 #include <algorithm>
15 #include <utility>
16 
17 #include "webrtc/modules/video_coding/main/interface/video_coding.h"
18 #include "webrtc/modules/video_coding/main/source/frame_buffer.h"
19 #include "webrtc/modules/video_coding/main/source/inter_frame_delay.h"
20 #include "webrtc/modules/video_coding/main/source/internal_defines.h"
21 #include "webrtc/modules/video_coding/main/source/jitter_buffer_common.h"
22 #include "webrtc/modules/video_coding/main/source/jitter_estimator.h"
23 #include "webrtc/modules/video_coding/main/source/packet.h"
24 #include "webrtc/system_wrappers/interface/clock.h"
25 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
26 #include "webrtc/system_wrappers/interface/event_wrapper.h"
27 #include "webrtc/system_wrappers/interface/logging.h"
28 #include "webrtc/system_wrappers/interface/trace_event.h"
29 
30 namespace webrtc {
31 
32 // Use this rtt if no value has been reported.
33 static const uint32_t kDefaultRtt = 200;
34 
35 typedef std::pair<uint32_t, VCMFrameBuffer*> FrameListPair;
36 
IsKeyFrame(FrameListPair pair)37 bool IsKeyFrame(FrameListPair pair) {
38   return pair.second->FrameType() == kVideoFrameKey;
39 }
40 
HasNonEmptyState(FrameListPair pair)41 bool HasNonEmptyState(FrameListPair pair) {
42   return pair.second->GetState() != kStateEmpty;
43 }
44 
InsertFrame(VCMFrameBuffer * frame)45 void FrameList::InsertFrame(VCMFrameBuffer* frame) {
46   insert(rbegin().base(), FrameListPair(frame->TimeStamp(), frame));
47 }
48 
FindFrame(uint32_t timestamp) const49 VCMFrameBuffer* FrameList::FindFrame(uint32_t timestamp) const {
50   FrameList::const_iterator it = find(timestamp);
51   if (it == end())
52     return NULL;
53   return it->second;
54 }
55 
PopFrame(uint32_t timestamp)56 VCMFrameBuffer* FrameList::PopFrame(uint32_t timestamp) {
57   FrameList::iterator it = find(timestamp);
58   if (it == end())
59     return NULL;
60   VCMFrameBuffer* frame = it->second;
61   erase(it);
62   return frame;
63 }
64 
Front() const65 VCMFrameBuffer* FrameList::Front() const {
66   return begin()->second;
67 }
68 
Back() const69 VCMFrameBuffer* FrameList::Back() const {
70   return rbegin()->second;
71 }
72 
RecycleFramesUntilKeyFrame(FrameList::iterator * key_frame_it,UnorderedFrameList * free_frames)73 int FrameList::RecycleFramesUntilKeyFrame(FrameList::iterator* key_frame_it,
74                                           UnorderedFrameList* free_frames) {
75   int drop_count = 0;
76   FrameList::iterator it = begin();
77   while (!empty()) {
78     // Throw at least one frame.
79     it->second->Reset();
80     free_frames->push_back(it->second);
81     erase(it++);
82     ++drop_count;
83     if (it != end() && it->second->FrameType() == kVideoFrameKey) {
84       *key_frame_it = it;
85       return drop_count;
86     }
87   }
88   *key_frame_it = end();
89   return drop_count;
90 }
91 
CleanUpOldOrEmptyFrames(VCMDecodingState * decoding_state,UnorderedFrameList * free_frames)92 int FrameList::CleanUpOldOrEmptyFrames(VCMDecodingState* decoding_state,
93                                        UnorderedFrameList* free_frames) {
94   int drop_count = 0;
95   while (!empty()) {
96     VCMFrameBuffer* oldest_frame = Front();
97     bool remove_frame = false;
98     if (oldest_frame->GetState() == kStateEmpty && size() > 1) {
99       // This frame is empty, try to update the last decoded state and drop it
100       // if successful.
101       remove_frame = decoding_state->UpdateEmptyFrame(oldest_frame);
102     } else {
103       remove_frame = decoding_state->IsOldFrame(oldest_frame);
104     }
105     if (!remove_frame) {
106       break;
107     }
108     free_frames->push_back(oldest_frame);
109     ++drop_count;
110     TRACE_EVENT_INSTANT1("webrtc", "JB::OldOrEmptyFrameDropped", "timestamp",
111                          oldest_frame->TimeStamp());
112     erase(begin());
113   }
114   return drop_count;
115 }
116 
Reset(UnorderedFrameList * free_frames)117 void FrameList::Reset(UnorderedFrameList* free_frames) {
118   while (!empty()) {
119     begin()->second->Reset();
120     free_frames->push_back(begin()->second);
121     erase(begin());
122   }
123 }
124 
VCMJitterBuffer(Clock * clock,EventFactory * event_factory)125 VCMJitterBuffer::VCMJitterBuffer(Clock* clock, EventFactory* event_factory)
126     : clock_(clock),
127       running_(false),
128       crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
129       frame_event_(event_factory->CreateEvent()),
130       packet_event_(event_factory->CreateEvent()),
131       max_number_of_frames_(kStartNumberOfFrames),
132       frame_buffers_(),
133       free_frames_(),
134       decodable_frames_(),
135       incomplete_frames_(),
136       last_decoded_state_(),
137       first_packet_since_reset_(true),
138       incoming_frame_rate_(0),
139       incoming_frame_count_(0),
140       time_last_incoming_frame_count_(0),
141       incoming_bit_count_(0),
142       incoming_bit_rate_(0),
143       drop_count_(0),
144       num_consecutive_old_frames_(0),
145       num_consecutive_old_packets_(0),
146       num_discarded_packets_(0),
147       jitter_estimate_(clock),
148       inter_frame_delay_(clock_->TimeInMilliseconds()),
149       rtt_ms_(kDefaultRtt),
150       nack_mode_(kNoNack),
151       low_rtt_nack_threshold_ms_(-1),
152       high_rtt_nack_threshold_ms_(-1),
153       missing_sequence_numbers_(SequenceNumberLessThan()),
154       nack_seq_nums_(),
155       max_nack_list_size_(0),
156       max_packet_age_to_nack_(0),
157       max_incomplete_time_ms_(0),
158       decode_error_mode_(kNoErrors),
159       average_packets_per_frame_(0.0f),
160       frame_counter_(0) {
161   memset(frame_buffers_, 0, sizeof(frame_buffers_));
162 
163   for (int i = 0; i < kStartNumberOfFrames; i++) {
164     frame_buffers_[i] = new VCMFrameBuffer();
165     free_frames_.push_back(frame_buffers_[i]);
166   }
167 }
168 
~VCMJitterBuffer()169 VCMJitterBuffer::~VCMJitterBuffer() {
170   Stop();
171   for (int i = 0; i < kMaxNumberOfFrames; i++) {
172     if (frame_buffers_[i]) {
173       delete frame_buffers_[i];
174     }
175   }
176   delete crit_sect_;
177 }
178 
CopyFrom(const VCMJitterBuffer & rhs)179 void VCMJitterBuffer::CopyFrom(const VCMJitterBuffer& rhs) {
180   if (this != &rhs) {
181     crit_sect_->Enter();
182     rhs.crit_sect_->Enter();
183     running_ = rhs.running_;
184     max_number_of_frames_ = rhs.max_number_of_frames_;
185     incoming_frame_rate_ = rhs.incoming_frame_rate_;
186     incoming_frame_count_ = rhs.incoming_frame_count_;
187     time_last_incoming_frame_count_ = rhs.time_last_incoming_frame_count_;
188     incoming_bit_count_ = rhs.incoming_bit_count_;
189     incoming_bit_rate_ = rhs.incoming_bit_rate_;
190     drop_count_ = rhs.drop_count_;
191     num_consecutive_old_frames_ = rhs.num_consecutive_old_frames_;
192     num_consecutive_old_packets_ = rhs.num_consecutive_old_packets_;
193     num_discarded_packets_ = rhs.num_discarded_packets_;
194     jitter_estimate_ = rhs.jitter_estimate_;
195     inter_frame_delay_ = rhs.inter_frame_delay_;
196     waiting_for_completion_ = rhs.waiting_for_completion_;
197     rtt_ms_ = rhs.rtt_ms_;
198     first_packet_since_reset_ = rhs.first_packet_since_reset_;
199     last_decoded_state_ =  rhs.last_decoded_state_;
200     decode_error_mode_ = rhs.decode_error_mode_;
201     assert(max_nack_list_size_ == rhs.max_nack_list_size_);
202     assert(max_packet_age_to_nack_ == rhs.max_packet_age_to_nack_);
203     assert(max_incomplete_time_ms_ == rhs.max_incomplete_time_ms_);
204     receive_statistics_ = rhs.receive_statistics_;
205     nack_seq_nums_.resize(rhs.nack_seq_nums_.size());
206     missing_sequence_numbers_ = rhs.missing_sequence_numbers_;
207     latest_received_sequence_number_ = rhs.latest_received_sequence_number_;
208     average_packets_per_frame_ = rhs.average_packets_per_frame_;
209     for (int i = 0; i < kMaxNumberOfFrames; i++) {
210       if (frame_buffers_[i] != NULL) {
211         delete frame_buffers_[i];
212         frame_buffers_[i] = NULL;
213       }
214     }
215     free_frames_.clear();
216     decodable_frames_.clear();
217     incomplete_frames_.clear();
218     int i = 0;
219     for (UnorderedFrameList::const_iterator it = rhs.free_frames_.begin();
220          it != rhs.free_frames_.end(); ++it, ++i) {
221       frame_buffers_[i] = new VCMFrameBuffer;
222       free_frames_.push_back(frame_buffers_[i]);
223     }
224     CopyFrames(&decodable_frames_, rhs.decodable_frames_, &i);
225     CopyFrames(&incomplete_frames_, rhs.incomplete_frames_, &i);
226     rhs.crit_sect_->Leave();
227     crit_sect_->Leave();
228   }
229 }
230 
CopyFrames(FrameList * to_list,const FrameList & from_list,int * index)231 void VCMJitterBuffer::CopyFrames(FrameList* to_list,
232     const FrameList& from_list, int* index) {
233   to_list->clear();
234   for (FrameList::const_iterator it = from_list.begin();
235        it != from_list.end(); ++it, ++*index) {
236     frame_buffers_[*index] = new VCMFrameBuffer(*it->second);
237     to_list->InsertFrame(frame_buffers_[*index]);
238   }
239 }
240 
Start()241 void VCMJitterBuffer::Start() {
242   CriticalSectionScoped cs(crit_sect_);
243   running_ = true;
244   incoming_frame_count_ = 0;
245   incoming_frame_rate_ = 0;
246   incoming_bit_count_ = 0;
247   incoming_bit_rate_ = 0;
248   time_last_incoming_frame_count_ = clock_->TimeInMilliseconds();
249   receive_statistics_.clear();
250 
251   num_consecutive_old_frames_ = 0;
252   num_consecutive_old_packets_ = 0;
253   num_discarded_packets_ = 0;
254 
255   // Start in a non-signaled state.
256   frame_event_->Reset();
257   packet_event_->Reset();
258   waiting_for_completion_.frame_size = 0;
259   waiting_for_completion_.timestamp = 0;
260   waiting_for_completion_.latest_packet_time = -1;
261   first_packet_since_reset_ = true;
262   rtt_ms_ = kDefaultRtt;
263   last_decoded_state_.Reset();
264 }
265 
Stop()266 void VCMJitterBuffer::Stop() {
267   crit_sect_->Enter();
268   running_ = false;
269   last_decoded_state_.Reset();
270   free_frames_.clear();
271   decodable_frames_.clear();
272   incomplete_frames_.clear();
273   // Make sure all frames are reset and free.
274   for (int i = 0; i < kMaxNumberOfFrames; i++) {
275     if (frame_buffers_[i] != NULL) {
276       static_cast<VCMFrameBuffer*>(frame_buffers_[i])->Reset();
277       free_frames_.push_back(frame_buffers_[i]);
278     }
279   }
280   crit_sect_->Leave();
281   // Make sure we wake up any threads waiting on these events.
282   frame_event_->Set();
283   packet_event_->Set();
284 }
285 
Running() const286 bool VCMJitterBuffer::Running() const {
287   CriticalSectionScoped cs(crit_sect_);
288   return running_;
289 }
290 
Flush()291 void VCMJitterBuffer::Flush() {
292   CriticalSectionScoped cs(crit_sect_);
293   decodable_frames_.Reset(&free_frames_);
294   incomplete_frames_.Reset(&free_frames_);
295   last_decoded_state_.Reset();  // TODO(mikhal): sync reset.
296   frame_event_->Reset();
297   packet_event_->Reset();
298   num_consecutive_old_frames_ = 0;
299   num_consecutive_old_packets_ = 0;
300   // Also reset the jitter and delay estimates
301   jitter_estimate_.Reset();
302   inter_frame_delay_.Reset(clock_->TimeInMilliseconds());
303   waiting_for_completion_.frame_size = 0;
304   waiting_for_completion_.timestamp = 0;
305   waiting_for_completion_.latest_packet_time = -1;
306   first_packet_since_reset_ = true;
307   missing_sequence_numbers_.clear();
308 }
309 
310 // Get received key and delta frames
FrameStatistics() const311 std::map<FrameType, uint32_t> VCMJitterBuffer::FrameStatistics() const {
312   CriticalSectionScoped cs(crit_sect_);
313   return receive_statistics_;
314 }
315 
num_discarded_packets() const316 int VCMJitterBuffer::num_discarded_packets() const {
317   CriticalSectionScoped cs(crit_sect_);
318   return num_discarded_packets_;
319 }
320 
321 // Calculate framerate and bitrate.
IncomingRateStatistics(unsigned int * framerate,unsigned int * bitrate)322 void VCMJitterBuffer::IncomingRateStatistics(unsigned int* framerate,
323                                              unsigned int* bitrate) {
324   assert(framerate);
325   assert(bitrate);
326   CriticalSectionScoped cs(crit_sect_);
327   const int64_t now = clock_->TimeInMilliseconds();
328   int64_t diff = now - time_last_incoming_frame_count_;
329   if (diff < 1000 && incoming_frame_rate_ > 0 && incoming_bit_rate_ > 0) {
330     // Make sure we report something even though less than
331     // 1 second has passed since last update.
332     *framerate = incoming_frame_rate_;
333     *bitrate = incoming_bit_rate_;
334   } else if (incoming_frame_count_ != 0) {
335     // We have received frame(s) since last call to this function
336 
337     // Prepare calculations
338     if (diff <= 0) {
339       diff = 1;
340     }
341     // we add 0.5f for rounding
342     float rate = 0.5f + ((incoming_frame_count_ * 1000.0f) / diff);
343     if (rate < 1.0f) {
344       rate = 1.0f;
345     }
346 
347     // Calculate frame rate
348     // Let r be rate.
349     // r(0) = 1000*framecount/delta_time.
350     // (I.e. frames per second since last calculation.)
351     // frame_rate = r(0)/2 + r(-1)/2
352     // (I.e. fr/s average this and the previous calculation.)
353     *framerate = (incoming_frame_rate_ + static_cast<unsigned int>(rate)) / 2;
354     incoming_frame_rate_ = static_cast<unsigned int>(rate);
355 
356     // Calculate bit rate
357     if (incoming_bit_count_ == 0) {
358       *bitrate = 0;
359     } else {
360       *bitrate = 10 * ((100 * incoming_bit_count_) /
361                        static_cast<unsigned int>(diff));
362     }
363     incoming_bit_rate_ = *bitrate;
364 
365     // Reset count
366     incoming_frame_count_ = 0;
367     incoming_bit_count_ = 0;
368     time_last_incoming_frame_count_ = now;
369 
370   } else {
371     // No frames since last call
372     time_last_incoming_frame_count_ = clock_->TimeInMilliseconds();
373     *framerate = 0;
374     *bitrate = 0;
375     incoming_frame_rate_ = 0;
376     incoming_bit_rate_ = 0;
377   }
378 }
379 
380 // Answers the question:
381 // Will the packet sequence be complete if the next frame is grabbed for
382 // decoding right now? That is, have we lost a frame between the last decoded
383 // frame and the next, or is the next
384 // frame missing one or more packets?
CompleteSequenceWithNextFrame()385 bool VCMJitterBuffer::CompleteSequenceWithNextFrame() {
386   CriticalSectionScoped cs(crit_sect_);
387   // Finding oldest frame ready for decoder, check sequence number and size
388   CleanUpOldOrEmptyFrames();
389   if (!decodable_frames_.empty()) {
390     if (decodable_frames_.Front()->GetState() == kStateComplete) {
391       return true;
392     }
393   } else if (incomplete_frames_.size() <= 1) {
394     // Frame not ready to be decoded.
395     return true;
396   }
397   return false;
398 }
399 
400 // Returns immediately or a |max_wait_time_ms| ms event hang waiting for a
401 // complete frame, |max_wait_time_ms| decided by caller.
NextCompleteTimestamp(uint32_t max_wait_time_ms,uint32_t * timestamp)402 bool VCMJitterBuffer::NextCompleteTimestamp(
403     uint32_t max_wait_time_ms, uint32_t* timestamp) {
404   crit_sect_->Enter();
405   if (!running_) {
406     crit_sect_->Leave();
407     return false;
408   }
409   CleanUpOldOrEmptyFrames();
410 
411   if (decodable_frames_.empty() ||
412       decodable_frames_.Front()->GetState() != kStateComplete) {
413     const int64_t end_wait_time_ms = clock_->TimeInMilliseconds() +
414         max_wait_time_ms;
415     int64_t wait_time_ms = max_wait_time_ms;
416     while (wait_time_ms > 0) {
417       crit_sect_->Leave();
418       const EventTypeWrapper ret =
419         frame_event_->Wait(static_cast<uint32_t>(wait_time_ms));
420       crit_sect_->Enter();
421       if (ret == kEventSignaled) {
422         // Are we shutting down the jitter buffer?
423         if (!running_) {
424           crit_sect_->Leave();
425           return false;
426         }
427         // Finding oldest frame ready for decoder.
428         CleanUpOldOrEmptyFrames();
429         if (decodable_frames_.empty() ||
430             decodable_frames_.Front()->GetState() != kStateComplete) {
431           wait_time_ms = end_wait_time_ms - clock_->TimeInMilliseconds();
432         } else {
433           break;
434         }
435       } else {
436         break;
437       }
438     }
439     // Inside |crit_sect_|.
440   } else {
441     // We already have a frame, reset the event.
442     frame_event_->Reset();
443   }
444   if (decodable_frames_.empty() ||
445       decodable_frames_.Front()->GetState() != kStateComplete) {
446     crit_sect_->Leave();
447     return false;
448   }
449   *timestamp = decodable_frames_.Front()->TimeStamp();
450   crit_sect_->Leave();
451   return true;
452 }
453 
NextMaybeIncompleteTimestamp(uint32_t * timestamp)454 bool VCMJitterBuffer::NextMaybeIncompleteTimestamp(uint32_t* timestamp) {
455   CriticalSectionScoped cs(crit_sect_);
456   if (!running_) {
457     return false;
458   }
459   if (decode_error_mode_ == kNoErrors) {
460     // No point to continue, as we are not decoding with errors.
461     return false;
462   }
463 
464   CleanUpOldOrEmptyFrames();
465 
466   if (decodable_frames_.empty()) {
467     return false;
468   }
469   VCMFrameBuffer* oldest_frame = decodable_frames_.Front();
470   // If we have exactly one frame in the buffer, release it only if it is
471   // complete. We know decodable_frames_ is  not empty due to the previous
472   // check.
473   if (decodable_frames_.size() == 1 && incomplete_frames_.empty()
474       && oldest_frame->GetState() != kStateComplete) {
475     return false;
476   }
477 
478   *timestamp = oldest_frame->TimeStamp();
479   return true;
480 }
481 
ExtractAndSetDecode(uint32_t timestamp)482 VCMEncodedFrame* VCMJitterBuffer::ExtractAndSetDecode(uint32_t timestamp) {
483   CriticalSectionScoped cs(crit_sect_);
484   if (!running_) {
485     return NULL;
486   }
487   // Extract the frame with the desired timestamp.
488   VCMFrameBuffer* frame = decodable_frames_.PopFrame(timestamp);
489   bool continuous = true;
490   if (!frame) {
491     frame = incomplete_frames_.PopFrame(timestamp);
492     if (frame)
493       continuous = last_decoded_state_.ContinuousFrame(frame);
494     else
495       return NULL;
496   }
497   TRACE_EVENT_ASYNC_STEP0("webrtc", "Video", timestamp, "Extract");
498   // Frame pulled out from jitter buffer, update the jitter estimate.
499   const bool retransmitted = (frame->GetNackCount() > 0);
500   if (retransmitted) {
501     jitter_estimate_.FrameNacked();
502   } else if (frame->Length() > 0) {
503     // Ignore retransmitted and empty frames.
504     if (waiting_for_completion_.latest_packet_time >= 0) {
505       UpdateJitterEstimate(waiting_for_completion_, true);
506     }
507     if (frame->GetState() == kStateComplete) {
508       UpdateJitterEstimate(*frame, false);
509     } else {
510       // Wait for this one to get complete.
511       waiting_for_completion_.frame_size = frame->Length();
512       waiting_for_completion_.latest_packet_time =
513           frame->LatestPacketTimeMs();
514       waiting_for_completion_.timestamp = frame->TimeStamp();
515     }
516   }
517 
518   // The state must be changed to decoding before cleaning up zero sized
519   // frames to avoid empty frames being cleaned up and then given to the
520   // decoder. Propagates the missing_frame bit.
521   frame->PrepareForDecode(continuous);
522 
523   // We have a frame - update the last decoded state and nack list.
524   last_decoded_state_.SetState(frame);
525   DropPacketsFromNackList(last_decoded_state_.sequence_num());
526 
527   if ((*frame).IsSessionComplete())
528     UpdateAveragePacketsPerFrame(frame->NumPackets());
529 
530   return frame;
531 }
532 
533 // Release frame when done with decoding. Should never be used to release
534 // frames from within the jitter buffer.
ReleaseFrame(VCMEncodedFrame * frame)535 void VCMJitterBuffer::ReleaseFrame(VCMEncodedFrame* frame) {
536   CriticalSectionScoped cs(crit_sect_);
537   VCMFrameBuffer* frame_buffer = static_cast<VCMFrameBuffer*>(frame);
538   if (frame_buffer) {
539     free_frames_.push_back(frame_buffer);
540   }
541 }
542 
543 // Gets frame to use for this timestamp. If no match, get empty frame.
GetFrame(const VCMPacket & packet,VCMFrameBuffer ** frame)544 VCMFrameBufferEnum VCMJitterBuffer::GetFrame(const VCMPacket& packet,
545                                              VCMFrameBuffer** frame) {
546   // Does this packet belong to an old frame?
547   if (last_decoded_state_.IsOldPacket(&packet)) {
548     // Account only for media packets.
549     if (packet.sizeBytes > 0) {
550       num_discarded_packets_++;
551       num_consecutive_old_packets_++;
552     }
553     // Update last decoded sequence number if the packet arrived late and
554     // belongs to a frame with a timestamp equal to the last decoded
555     // timestamp.
556     last_decoded_state_.UpdateOldPacket(&packet);
557     DropPacketsFromNackList(last_decoded_state_.sequence_num());
558 
559     if (num_consecutive_old_packets_ > kMaxConsecutiveOldPackets) {
560       LOG(LS_WARNING) << num_consecutive_old_packets_ << " consecutive old "
561                          "packets received. Flushing the jitter buffer.";
562       Flush();
563       return kFlushIndicator;
564     }
565     return kOldPacket;
566   }
567   num_consecutive_old_packets_ = 0;
568 
569   *frame = incomplete_frames_.FindFrame(packet.timestamp);
570   if (*frame)
571     return kNoError;
572   *frame = decodable_frames_.FindFrame(packet.timestamp);
573   if (*frame)
574     return kNoError;
575 
576   // No match, return empty frame.
577   *frame = GetEmptyFrame();
578   VCMFrameBufferEnum ret = kNoError;
579   if (!*frame) {
580     // No free frame! Try to reclaim some...
581     LOG(LS_WARNING) << "Unable to get empty frame; Recycling.";
582     bool found_key_frame = RecycleFramesUntilKeyFrame();
583     *frame = GetEmptyFrame();
584     assert(*frame);
585     if (!found_key_frame) {
586       ret = kFlushIndicator;
587     }
588   }
589   (*frame)->Reset();
590   return ret;
591 }
592 
LastPacketTime(const VCMEncodedFrame * frame,bool * retransmitted) const593 int64_t VCMJitterBuffer::LastPacketTime(const VCMEncodedFrame* frame,
594                                         bool* retransmitted) const {
595   assert(retransmitted);
596   CriticalSectionScoped cs(crit_sect_);
597   const VCMFrameBuffer* frame_buffer =
598       static_cast<const VCMFrameBuffer*>(frame);
599   *retransmitted = (frame_buffer->GetNackCount() > 0);
600   return frame_buffer->LatestPacketTimeMs();
601 }
602 
InsertPacket(const VCMPacket & packet,bool * retransmitted)603 VCMFrameBufferEnum VCMJitterBuffer::InsertPacket(const VCMPacket& packet,
604                                                  bool* retransmitted) {
605   CriticalSectionScoped cs(crit_sect_);
606 
607   VCMFrameBuffer* frame = NULL;
608   const VCMFrameBufferEnum error = GetFrame(packet, &frame);
609   if (error != kNoError && frame == NULL) {
610     return error;
611   }
612   int64_t now_ms = clock_->TimeInMilliseconds();
613   // We are keeping track of the first and latest seq numbers, and
614   // the number of wraps to be able to calculate how many packets we expect.
615   if (first_packet_since_reset_) {
616     // Now it's time to start estimating jitter
617     // reset the delay estimate.
618     inter_frame_delay_.Reset(now_ms);
619   }
620   if (last_decoded_state_.IsOldPacket(&packet)) {
621     // This packet belongs to an old, already decoded frame, we want to update
622     // the last decoded sequence number.
623     last_decoded_state_.UpdateOldPacket(&packet);
624     drop_count_++;
625     // Flush if this happens consistently.
626     num_consecutive_old_frames_++;
627     if (num_consecutive_old_frames_ > kMaxConsecutiveOldFrames) {
628       LOG(LS_WARNING) << num_consecutive_old_packets_ << " consecutive old "
629                          "frames received. Flushing the jitter buffer.";
630       Flush();
631       return kFlushIndicator;
632     }
633     return kNoError;
634   }
635 
636   num_consecutive_old_frames_ = 0;
637 
638   // Empty packets may bias the jitter estimate (lacking size component),
639   // therefore don't let empty packet trigger the following updates:
640   if (packet.frameType != kFrameEmpty) {
641     if (waiting_for_completion_.timestamp == packet.timestamp) {
642       // This can get bad if we have a lot of duplicate packets,
643       // we will then count some packet multiple times.
644       waiting_for_completion_.frame_size += packet.sizeBytes;
645       waiting_for_completion_.latest_packet_time = now_ms;
646     } else if (waiting_for_completion_.latest_packet_time >= 0 &&
647                waiting_for_completion_.latest_packet_time + 2000 <= now_ms) {
648       // A packet should never be more than two seconds late
649       UpdateJitterEstimate(waiting_for_completion_, true);
650       waiting_for_completion_.latest_packet_time = -1;
651       waiting_for_completion_.frame_size = 0;
652       waiting_for_completion_.timestamp = 0;
653     }
654   }
655 
656   VCMFrameBufferStateEnum previous_state = frame->GetState();
657   // Insert packet.
658   // Check for first packet. High sequence number will be -1 if neither an empty
659   // packet nor a media packet has been inserted.
660   bool first = (frame->GetHighSeqNum() == -1);
661   FrameData frame_data;
662   frame_data.rtt_ms = rtt_ms_;
663   frame_data.rolling_average_packets_per_frame = average_packets_per_frame_;
664   VCMFrameBufferEnum buffer_return = frame->InsertPacket(packet,
665                                                          now_ms,
666                                                          decode_error_mode_,
667                                                          frame_data);
668   if (!frame->GetCountedFrame()) {
669     TRACE_EVENT_ASYNC_BEGIN1("webrtc", "Video", frame->TimeStamp(),
670                              "timestamp", frame->TimeStamp());
671   }
672 
673   if (buffer_return > 0) {
674     incoming_bit_count_ += packet.sizeBytes << 3;
675     if (first_packet_since_reset_) {
676       latest_received_sequence_number_ = packet.seqNum;
677       first_packet_since_reset_ = false;
678     } else {
679       if (IsPacketRetransmitted(packet)) {
680         frame->IncrementNackCount();
681       }
682       if (!UpdateNackList(packet.seqNum) &&
683           packet.frameType != kVideoFrameKey) {
684         buffer_return = kFlushIndicator;
685       }
686       latest_received_sequence_number_ = LatestSequenceNumber(
687           latest_received_sequence_number_, packet.seqNum);
688     }
689   }
690 
691   // Is the frame already in the decodable list?
692   bool update_decodable_list = (previous_state != kStateDecodable &&
693       previous_state != kStateComplete);
694   bool continuous = IsContinuous(*frame);
695   switch (buffer_return) {
696     case kGeneralError:
697     case kTimeStampError:
698     case kSizeError: {
699       // This frame will be cleaned up later from the frame list.
700       frame->Reset();
701       break;
702     }
703     case kCompleteSession: {
704       if (update_decodable_list) {
705         CountFrame(*frame);
706         frame->SetCountedFrame(true);
707         if (continuous) {
708           // Signal that we have a complete session.
709           frame_event_->Set();
710         }
711       }
712     }
713     // Note: There is no break here - continuing to kDecodableSession.
714     case kDecodableSession: {
715       *retransmitted = (frame->GetNackCount() > 0);
716       // Signal that we have a received packet.
717       packet_event_->Set();
718       if (!update_decodable_list) {
719         break;
720       }
721       if (continuous) {
722         if (!first) {
723           incomplete_frames_.PopFrame(packet.timestamp);
724         }
725         decodable_frames_.InsertFrame(frame);
726         FindAndInsertContinuousFrames(*frame);
727       } else if (first) {
728         incomplete_frames_.InsertFrame(frame);
729       }
730       break;
731     }
732     case kIncomplete: {
733       // No point in storing empty continuous frames.
734       if (frame->GetState() == kStateEmpty &&
735           last_decoded_state_.UpdateEmptyFrame(frame)) {
736         free_frames_.push_back(frame);
737         frame->Reset();
738         frame = NULL;
739         return kNoError;
740       } else if (first) {
741         incomplete_frames_.InsertFrame(frame);
742       }
743       // Signal that we have received a packet.
744       packet_event_->Set();
745       break;
746     }
747     case kNoError:
748     case kOutOfBoundsPacket:
749     case kDuplicatePacket: {
750       break;
751     }
752     case kFlushIndicator:
753       return kFlushIndicator;
754     default: {
755       assert(false && "JitterBuffer::InsertPacket: Undefined value");
756     }
757   }
758   return buffer_return;
759 }
760 
IsContinuousInState(const VCMFrameBuffer & frame,const VCMDecodingState & decoding_state) const761 bool VCMJitterBuffer::IsContinuousInState(const VCMFrameBuffer& frame,
762     const VCMDecodingState& decoding_state) const {
763   if (decode_error_mode_ == kWithErrors)
764     return true;
765   // Is this frame (complete or decodable) and continuous?
766   // kStateDecodable will never be set when decode_error_mode_ is false
767   // as SessionInfo determines this state based on the error mode (and frame
768   // completeness).
769   if ((frame.GetState() == kStateComplete ||
770        frame.GetState() == kStateDecodable) &&
771        decoding_state.ContinuousFrame(&frame)) {
772     return true;
773   } else {
774     return false;
775   }
776 }
777 
IsContinuous(const VCMFrameBuffer & frame) const778 bool VCMJitterBuffer::IsContinuous(const VCMFrameBuffer& frame) const {
779   if (IsContinuousInState(frame, last_decoded_state_)) {
780     return true;
781   }
782   VCMDecodingState decoding_state;
783   decoding_state.CopyFrom(last_decoded_state_);
784   for (FrameList::const_iterator it = decodable_frames_.begin();
785        it != decodable_frames_.end(); ++it)  {
786     VCMFrameBuffer* decodable_frame = it->second;
787     if (IsNewerTimestamp(decodable_frame->TimeStamp(), frame.TimeStamp())) {
788       break;
789     }
790     decoding_state.SetState(decodable_frame);
791     if (IsContinuousInState(frame, decoding_state)) {
792       return true;
793     }
794   }
795   return false;
796 }
797 
FindAndInsertContinuousFrames(const VCMFrameBuffer & new_frame)798 void VCMJitterBuffer::FindAndInsertContinuousFrames(
799     const VCMFrameBuffer& new_frame) {
800   VCMDecodingState decoding_state;
801   decoding_state.CopyFrom(last_decoded_state_);
802   decoding_state.SetState(&new_frame);
803   // When temporal layers are available, we search for a complete or decodable
804   // frame until we hit one of the following:
805   // 1. Continuous base or sync layer.
806   // 2. The end of the list was reached.
807   for (FrameList::iterator it = incomplete_frames_.begin();
808        it != incomplete_frames_.end();)  {
809     VCMFrameBuffer* frame = it->second;
810     if (IsNewerTimestamp(new_frame.TimeStamp(), frame->TimeStamp())) {
811       ++it;
812       continue;
813     }
814     if (IsContinuousInState(*frame, decoding_state)) {
815       decodable_frames_.InsertFrame(frame);
816       incomplete_frames_.erase(it++);
817       decoding_state.SetState(frame);
818     } else if (frame->TemporalId() <= 0) {
819       break;
820     } else {
821       ++it;
822     }
823   }
824 }
825 
EstimatedJitterMs()826 uint32_t VCMJitterBuffer::EstimatedJitterMs() {
827   CriticalSectionScoped cs(crit_sect_);
828   // Compute RTT multiplier for estimation.
829   // low_rtt_nackThresholdMs_ == -1 means no FEC.
830   double rtt_mult = 1.0f;
831   if (low_rtt_nack_threshold_ms_ >= 0 &&
832       static_cast<int>(rtt_ms_) >= low_rtt_nack_threshold_ms_) {
833     // For RTTs above low_rtt_nack_threshold_ms_ we don't apply extra delay
834     // when waiting for retransmissions.
835     rtt_mult = 0.0f;
836   }
837   return jitter_estimate_.GetJitterEstimate(rtt_mult);
838 }
839 
UpdateRtt(uint32_t rtt_ms)840 void VCMJitterBuffer::UpdateRtt(uint32_t rtt_ms) {
841   CriticalSectionScoped cs(crit_sect_);
842   rtt_ms_ = rtt_ms;
843   jitter_estimate_.UpdateRtt(rtt_ms);
844 }
845 
SetNackMode(VCMNackMode mode,int low_rtt_nack_threshold_ms,int high_rtt_nack_threshold_ms)846 void VCMJitterBuffer::SetNackMode(VCMNackMode mode,
847                                   int low_rtt_nack_threshold_ms,
848                                   int high_rtt_nack_threshold_ms) {
849   CriticalSectionScoped cs(crit_sect_);
850   nack_mode_ = mode;
851   if (mode == kNoNack) {
852     missing_sequence_numbers_.clear();
853   }
854   assert(low_rtt_nack_threshold_ms >= -1 && high_rtt_nack_threshold_ms >= -1);
855   assert(high_rtt_nack_threshold_ms == -1 ||
856          low_rtt_nack_threshold_ms <= high_rtt_nack_threshold_ms);
857   assert(low_rtt_nack_threshold_ms > -1 || high_rtt_nack_threshold_ms == -1);
858   low_rtt_nack_threshold_ms_ = low_rtt_nack_threshold_ms;
859   high_rtt_nack_threshold_ms_ = high_rtt_nack_threshold_ms;
860   // Don't set a high start rtt if high_rtt_nack_threshold_ms_ is used, to not
861   // disable NACK in hybrid mode.
862   if (rtt_ms_ == kDefaultRtt && high_rtt_nack_threshold_ms_ != -1) {
863     rtt_ms_ = 0;
864   }
865   if (!WaitForRetransmissions()) {
866     jitter_estimate_.ResetNackCount();
867   }
868 }
869 
SetNackSettings(size_t max_nack_list_size,int max_packet_age_to_nack,int max_incomplete_time_ms)870 void VCMJitterBuffer::SetNackSettings(size_t max_nack_list_size,
871                                       int max_packet_age_to_nack,
872                                       int max_incomplete_time_ms) {
873   CriticalSectionScoped cs(crit_sect_);
874   assert(max_packet_age_to_nack >= 0);
875   assert(max_incomplete_time_ms_ >= 0);
876   max_nack_list_size_ = max_nack_list_size;
877   max_packet_age_to_nack_ = max_packet_age_to_nack;
878   max_incomplete_time_ms_ = max_incomplete_time_ms;
879   nack_seq_nums_.resize(max_nack_list_size_);
880 }
881 
nack_mode() const882 VCMNackMode VCMJitterBuffer::nack_mode() const {
883   CriticalSectionScoped cs(crit_sect_);
884   return nack_mode_;
885 }
886 
NonContinuousOrIncompleteDuration()887 int VCMJitterBuffer::NonContinuousOrIncompleteDuration() {
888   if (incomplete_frames_.empty()) {
889     return 0;
890   }
891   uint32_t start_timestamp = incomplete_frames_.Front()->TimeStamp();
892   if (!decodable_frames_.empty()) {
893     start_timestamp = decodable_frames_.Back()->TimeStamp();
894   }
895   return incomplete_frames_.Back()->TimeStamp() - start_timestamp;
896 }
897 
EstimatedLowSequenceNumber(const VCMFrameBuffer & frame) const898 uint16_t VCMJitterBuffer::EstimatedLowSequenceNumber(
899     const VCMFrameBuffer& frame) const {
900   assert(frame.GetLowSeqNum() >= 0);
901   if (frame.HaveFirstPacket())
902     return frame.GetLowSeqNum();
903 
904   // This estimate is not accurate if more than one packet with lower sequence
905   // number is lost.
906   return frame.GetLowSeqNum() - 1;
907 }
908 
GetNackList(uint16_t * nack_list_size,bool * request_key_frame)909 uint16_t* VCMJitterBuffer::GetNackList(uint16_t* nack_list_size,
910                                        bool* request_key_frame) {
911   CriticalSectionScoped cs(crit_sect_);
912   *request_key_frame = false;
913   if (nack_mode_ == kNoNack) {
914     *nack_list_size = 0;
915     return NULL;
916   }
917   if (last_decoded_state_.in_initial_state()) {
918     VCMFrameBuffer* next_frame =  NextFrame();
919     const bool first_frame_is_key = next_frame &&
920         next_frame->FrameType() == kVideoFrameKey &&
921         next_frame->HaveFirstPacket();
922     if (!first_frame_is_key) {
923       bool have_non_empty_frame = decodable_frames_.end() != find_if(
924           decodable_frames_.begin(), decodable_frames_.end(),
925           HasNonEmptyState);
926       if (!have_non_empty_frame) {
927         have_non_empty_frame = incomplete_frames_.end() != find_if(
928             incomplete_frames_.begin(), incomplete_frames_.end(),
929             HasNonEmptyState);
930       }
931       bool found_key_frame = RecycleFramesUntilKeyFrame();
932       if (!found_key_frame) {
933         *request_key_frame = have_non_empty_frame;
934         *nack_list_size = 0;
935         return NULL;
936       }
937     }
938   }
939   if (TooLargeNackList()) {
940     *request_key_frame = !HandleTooLargeNackList();
941   }
942   if (max_incomplete_time_ms_ > 0) {
943     int non_continuous_incomplete_duration =
944         NonContinuousOrIncompleteDuration();
945     if (non_continuous_incomplete_duration > 90 * max_incomplete_time_ms_) {
946       LOG_F(LS_WARNING) << "Too long non-decodable duration: "
947                         << non_continuous_incomplete_duration << " > "
948                         << 90 * max_incomplete_time_ms_;
949       FrameList::reverse_iterator rit = find_if(incomplete_frames_.rbegin(),
950           incomplete_frames_.rend(), IsKeyFrame);
951       if (rit == incomplete_frames_.rend()) {
952         // Request a key frame if we don't have one already.
953         *request_key_frame = true;
954         *nack_list_size = 0;
955         return NULL;
956       } else {
957         // Skip to the last key frame. If it's incomplete we will start
958         // NACKing it.
959         // Note that the estimated low sequence number is correct for VP8
960         // streams because only the first packet of a key frame is marked.
961         last_decoded_state_.Reset();
962         DropPacketsFromNackList(EstimatedLowSequenceNumber(*rit->second));
963       }
964     }
965   }
966   unsigned int i = 0;
967   SequenceNumberSet::iterator it = missing_sequence_numbers_.begin();
968   for (; it != missing_sequence_numbers_.end(); ++it, ++i) {
969     nack_seq_nums_[i] = *it;
970   }
971   *nack_list_size = i;
972   return &nack_seq_nums_[0];
973 }
974 
SetDecodeErrorMode(VCMDecodeErrorMode error_mode)975 void VCMJitterBuffer::SetDecodeErrorMode(VCMDecodeErrorMode error_mode) {
976   CriticalSectionScoped cs(crit_sect_);
977   decode_error_mode_ = error_mode;
978 }
979 
NextFrame() const980 VCMFrameBuffer* VCMJitterBuffer::NextFrame() const {
981   if (!decodable_frames_.empty())
982     return decodable_frames_.Front();
983   if (!incomplete_frames_.empty())
984     return incomplete_frames_.Front();
985   return NULL;
986 }
987 
UpdateNackList(uint16_t sequence_number)988 bool VCMJitterBuffer::UpdateNackList(uint16_t sequence_number) {
989   if (nack_mode_ == kNoNack) {
990     return true;
991   }
992   // Make sure we don't add packets which are already too old to be decoded.
993   if (!last_decoded_state_.in_initial_state()) {
994     latest_received_sequence_number_ = LatestSequenceNumber(
995         latest_received_sequence_number_,
996         last_decoded_state_.sequence_num());
997   }
998   if (IsNewerSequenceNumber(sequence_number,
999                             latest_received_sequence_number_)) {
1000     // Push any missing sequence numbers to the NACK list.
1001     for (uint16_t i = latest_received_sequence_number_ + 1;
1002          IsNewerSequenceNumber(sequence_number, i); ++i) {
1003       missing_sequence_numbers_.insert(missing_sequence_numbers_.end(), i);
1004       TRACE_EVENT_INSTANT1("webrtc", "AddNack", "seqnum", i);
1005     }
1006     if (TooLargeNackList() && !HandleTooLargeNackList()) {
1007       LOG(LS_WARNING) << "Requesting key frame due to too large NACK list.";
1008       return false;
1009     }
1010     if (MissingTooOldPacket(sequence_number) &&
1011         !HandleTooOldPackets(sequence_number)) {
1012       LOG(LS_WARNING) << "Requesting key frame due to missing too old packets";
1013       return false;
1014     }
1015   } else {
1016     missing_sequence_numbers_.erase(sequence_number);
1017     TRACE_EVENT_INSTANT1("webrtc", "RemoveNack", "seqnum", sequence_number);
1018   }
1019   return true;
1020 }
1021 
TooLargeNackList() const1022 bool VCMJitterBuffer::TooLargeNackList() const {
1023   return missing_sequence_numbers_.size() > max_nack_list_size_;
1024 }
1025 
HandleTooLargeNackList()1026 bool VCMJitterBuffer::HandleTooLargeNackList() {
1027   // Recycle frames until the NACK list is small enough. It is likely cheaper to
1028   // request a key frame than to retransmit this many missing packets.
1029   LOG_F(LS_WARNING) << "NACK list has grown too large: "
1030                     << missing_sequence_numbers_.size() << " > "
1031                     << max_nack_list_size_;
1032   bool key_frame_found = false;
1033   while (TooLargeNackList()) {
1034     key_frame_found = RecycleFramesUntilKeyFrame();
1035   }
1036   return key_frame_found;
1037 }
1038 
MissingTooOldPacket(uint16_t latest_sequence_number) const1039 bool VCMJitterBuffer::MissingTooOldPacket(
1040     uint16_t latest_sequence_number) const {
1041   if (missing_sequence_numbers_.empty()) {
1042     return false;
1043   }
1044   const uint16_t age_of_oldest_missing_packet = latest_sequence_number -
1045       *missing_sequence_numbers_.begin();
1046   // Recycle frames if the NACK list contains too old sequence numbers as
1047   // the packets may have already been dropped by the sender.
1048   return age_of_oldest_missing_packet > max_packet_age_to_nack_;
1049 }
1050 
HandleTooOldPackets(uint16_t latest_sequence_number)1051 bool VCMJitterBuffer::HandleTooOldPackets(uint16_t latest_sequence_number) {
1052   bool key_frame_found = false;
1053   const uint16_t age_of_oldest_missing_packet = latest_sequence_number -
1054       *missing_sequence_numbers_.begin();
1055   LOG_F(LS_WARNING) << "NACK list contains too old sequence numbers: "
1056                     << age_of_oldest_missing_packet << " > "
1057                     << max_packet_age_to_nack_;
1058   while (MissingTooOldPacket(latest_sequence_number)) {
1059     key_frame_found = RecycleFramesUntilKeyFrame();
1060   }
1061   return key_frame_found;
1062 }
1063 
DropPacketsFromNackList(uint16_t last_decoded_sequence_number)1064 void VCMJitterBuffer::DropPacketsFromNackList(
1065     uint16_t last_decoded_sequence_number) {
1066   // Erase all sequence numbers from the NACK list which we won't need any
1067   // longer.
1068   missing_sequence_numbers_.erase(missing_sequence_numbers_.begin(),
1069                                   missing_sequence_numbers_.upper_bound(
1070                                       last_decoded_sequence_number));
1071 }
1072 
LastDecodedTimestamp() const1073 int64_t VCMJitterBuffer::LastDecodedTimestamp() const {
1074   CriticalSectionScoped cs(crit_sect_);
1075   return last_decoded_state_.time_stamp();
1076 }
1077 
RenderBufferSize(uint32_t * timestamp_start,uint32_t * timestamp_end)1078 void VCMJitterBuffer::RenderBufferSize(uint32_t* timestamp_start,
1079                                        uint32_t* timestamp_end) {
1080   CriticalSectionScoped cs(crit_sect_);
1081   CleanUpOldOrEmptyFrames();
1082   *timestamp_start = 0;
1083   *timestamp_end = 0;
1084   if (decodable_frames_.empty()) {
1085     return;
1086   }
1087   *timestamp_start = decodable_frames_.Front()->TimeStamp();
1088   *timestamp_end = decodable_frames_.Back()->TimeStamp();
1089 }
1090 
GetEmptyFrame()1091 VCMFrameBuffer* VCMJitterBuffer::GetEmptyFrame() {
1092   if (free_frames_.empty()) {
1093     if (!TryToIncreaseJitterBufferSize()) {
1094       return NULL;
1095     }
1096   }
1097   VCMFrameBuffer* frame = free_frames_.front();
1098   free_frames_.pop_front();
1099   return frame;
1100 }
1101 
TryToIncreaseJitterBufferSize()1102 bool VCMJitterBuffer::TryToIncreaseJitterBufferSize() {
1103   if (max_number_of_frames_ >= kMaxNumberOfFrames)
1104     return false;
1105   VCMFrameBuffer* new_frame = new VCMFrameBuffer();
1106   frame_buffers_[max_number_of_frames_] = new_frame;
1107   free_frames_.push_back(new_frame);
1108   ++max_number_of_frames_;
1109   TRACE_COUNTER1("webrtc", "JBMaxFrames", max_number_of_frames_);
1110   return true;
1111 }
1112 
1113 // Recycle oldest frames up to a key frame, used if jitter buffer is completely
1114 // full.
RecycleFramesUntilKeyFrame()1115 bool VCMJitterBuffer::RecycleFramesUntilKeyFrame() {
1116   // First release incomplete frames, and only release decodable frames if there
1117   // are no incomplete ones.
1118   FrameList::iterator key_frame_it;
1119   bool key_frame_found = false;
1120   int dropped_frames = 0;
1121   dropped_frames += incomplete_frames_.RecycleFramesUntilKeyFrame(
1122       &key_frame_it, &free_frames_);
1123   key_frame_found = key_frame_it != incomplete_frames_.end();
1124   if (dropped_frames == 0) {
1125     dropped_frames += decodable_frames_.RecycleFramesUntilKeyFrame(
1126         &key_frame_it, &free_frames_);
1127     key_frame_found = key_frame_it != decodable_frames_.end();
1128   }
1129   drop_count_ += dropped_frames;
1130   TRACE_EVENT_INSTANT0("webrtc", "JB::RecycleFramesUntilKeyFrame");
1131   if (key_frame_found) {
1132     LOG(LS_INFO) << "Found key frame while dropping frames.";
1133     // Reset last decoded state to make sure the next frame decoded is a key
1134     // frame, and start NACKing from here.
1135     last_decoded_state_.Reset();
1136     DropPacketsFromNackList(EstimatedLowSequenceNumber(*key_frame_it->second));
1137   } else if (decodable_frames_.empty()) {
1138     // All frames dropped. Reset the decoding state and clear missing sequence
1139     // numbers as we're starting fresh.
1140     last_decoded_state_.Reset();
1141     missing_sequence_numbers_.clear();
1142   }
1143   return key_frame_found;
1144 }
1145 
1146 // Must be called under the critical section |crit_sect_|.
CountFrame(const VCMFrameBuffer & frame)1147 void VCMJitterBuffer::CountFrame(const VCMFrameBuffer& frame) {
1148   if (!frame.GetCountedFrame()) {
1149     // Ignore ACK frames.
1150     incoming_frame_count_++;
1151   }
1152 
1153   if (frame.FrameType() == kVideoFrameKey) {
1154     TRACE_EVENT_ASYNC_STEP0("webrtc", "Video",
1155                             frame.TimeStamp(), "KeyComplete");
1156   } else {
1157     TRACE_EVENT_ASYNC_STEP0("webrtc", "Video",
1158                             frame.TimeStamp(), "DeltaComplete");
1159   }
1160 
1161   // Update receive statistics. We count all layers, thus when you use layers
1162   // adding all key and delta frames might differ from frame count.
1163   if (frame.IsSessionComplete()) {
1164     ++receive_statistics_[frame.FrameType()];
1165   }
1166 }
1167 
UpdateAveragePacketsPerFrame(int current_number_packets)1168 void VCMJitterBuffer::UpdateAveragePacketsPerFrame(int current_number_packets) {
1169   if (frame_counter_ > kFastConvergeThreshold) {
1170     average_packets_per_frame_ = average_packets_per_frame_
1171               * (1 - kNormalConvergeMultiplier)
1172             + current_number_packets * kNormalConvergeMultiplier;
1173   } else if (frame_counter_ > 0) {
1174     average_packets_per_frame_ = average_packets_per_frame_
1175               * (1 - kFastConvergeMultiplier)
1176             + current_number_packets * kFastConvergeMultiplier;
1177     frame_counter_++;
1178   } else {
1179     average_packets_per_frame_ = current_number_packets;
1180     frame_counter_++;
1181   }
1182 }
1183 
1184 // Must be called under the critical section |crit_sect_|.
CleanUpOldOrEmptyFrames()1185 void VCMJitterBuffer::CleanUpOldOrEmptyFrames() {
1186   drop_count_ +=
1187       decodable_frames_.CleanUpOldOrEmptyFrames(&last_decoded_state_,
1188           &free_frames_);
1189   drop_count_ +=
1190       incomplete_frames_.CleanUpOldOrEmptyFrames(&last_decoded_state_,
1191           &free_frames_);
1192   if (!last_decoded_state_.in_initial_state()) {
1193     DropPacketsFromNackList(last_decoded_state_.sequence_num());
1194   }
1195 }
1196 
1197 // Must be called from within |crit_sect_|.
IsPacketRetransmitted(const VCMPacket & packet) const1198 bool VCMJitterBuffer::IsPacketRetransmitted(const VCMPacket& packet) const {
1199   return missing_sequence_numbers_.find(packet.seqNum) !=
1200       missing_sequence_numbers_.end();
1201 }
1202 
1203 // Must be called under the critical section |crit_sect_|. Should never be
1204 // called with retransmitted frames, they must be filtered out before this
1205 // function is called.
UpdateJitterEstimate(const VCMJitterSample & sample,bool incomplete_frame)1206 void VCMJitterBuffer::UpdateJitterEstimate(const VCMJitterSample& sample,
1207                                            bool incomplete_frame) {
1208   if (sample.latest_packet_time == -1) {
1209     return;
1210   }
1211   UpdateJitterEstimate(sample.latest_packet_time, sample.timestamp,
1212                        sample.frame_size, incomplete_frame);
1213 }
1214 
1215 // Must be called under the critical section crit_sect_. Should never be
1216 // called with retransmitted frames, they must be filtered out before this
1217 // function is called.
UpdateJitterEstimate(const VCMFrameBuffer & frame,bool incomplete_frame)1218 void VCMJitterBuffer::UpdateJitterEstimate(const VCMFrameBuffer& frame,
1219                                            bool incomplete_frame) {
1220   if (frame.LatestPacketTimeMs() == -1) {
1221     return;
1222   }
1223   // No retransmitted frames should be a part of the jitter
1224   // estimate.
1225   UpdateJitterEstimate(frame.LatestPacketTimeMs(), frame.TimeStamp(),
1226                        frame.Length(), incomplete_frame);
1227 }
1228 
1229 // Must be called under the critical section |crit_sect_|. Should never be
1230 // called with retransmitted frames, they must be filtered out before this
1231 // function is called.
UpdateJitterEstimate(int64_t latest_packet_time_ms,uint32_t timestamp,unsigned int frame_size,bool incomplete_frame)1232 void VCMJitterBuffer::UpdateJitterEstimate(
1233     int64_t latest_packet_time_ms,
1234     uint32_t timestamp,
1235     unsigned int frame_size,
1236     bool incomplete_frame) {
1237   if (latest_packet_time_ms == -1) {
1238     return;
1239   }
1240   int64_t frame_delay;
1241   bool not_reordered = inter_frame_delay_.CalculateDelay(timestamp,
1242                                                       &frame_delay,
1243                                                       latest_packet_time_ms);
1244   // Filter out frames which have been reordered in time by the network
1245   if (not_reordered) {
1246     // Update the jitter estimate with the new samples
1247     jitter_estimate_.UpdateEstimate(frame_delay, frame_size, incomplete_frame);
1248   }
1249 }
1250 
WaitForRetransmissions()1251 bool VCMJitterBuffer::WaitForRetransmissions() {
1252   if (nack_mode_ == kNoNack) {
1253     // NACK disabled -> don't wait for retransmissions.
1254     return false;
1255   }
1256   // Evaluate if the RTT is higher than |high_rtt_nack_threshold_ms_|, and in
1257   // that case we don't wait for retransmissions.
1258   if (high_rtt_nack_threshold_ms_ >= 0 &&
1259       rtt_ms_ >= static_cast<unsigned int>(high_rtt_nack_threshold_ms_)) {
1260     return false;
1261   }
1262   return true;
1263 }
1264 }  // namespace webrtc
1265