Revert 3892 "VCM/JB: Using last decoded state for waiting for key"

> VCM/JB: Using last decoded state for waiting for key
> 
> Review URL: https://webrtc-codereview.appspot.com/1323006

Although I have no idea why, it appears this might be causing failures in ViEStandardIntegrationTest.RunsFileTestWithoutErrors. I was unable to reproduce locally. This is a trial revert to verify. If the errors continue to happen, I will restore this change.

TBR=mikhal@webrtc.org

Review URL: https://webrtc-codereview.appspot.com/1321010

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3896 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/video_coding/main/source/decoding_state.cc b/webrtc/modules/video_coding/main/source/decoding_state.cc
index eb03705..8147897 100644
--- a/webrtc/modules/video_coding/main/source/decoding_state.cc
+++ b/webrtc/modules/video_coding/main/source/decoding_state.cc
@@ -149,11 +149,8 @@
   // Return true when in initial state.
   // Note that when a method is not applicable it will return false.
   assert(frame != NULL);
-  if (in_initial_state_) {
-    // Always start with a key frame.
-    if (frame->FrameType() == kVideoFrameKey) return true;
-    return false;
-  }
+  if (in_initial_state_)
+    return true;
 
   if (!ContinuousLayer(frame->TemporalId(), frame->Tl0PicId())) {
     // Base layers are not continuous or temporal layers are inactive.
diff --git a/webrtc/modules/video_coding/main/source/decoding_state_unittest.cc b/webrtc/modules/video_coding/main/source/decoding_state_unittest.cc
index c5ad5e6..53a26ad 100644
--- a/webrtc/modules/video_coding/main/source/decoding_state_unittest.cc
+++ b/webrtc/modules/video_coding/main/source/decoding_state_unittest.cc
@@ -30,9 +30,8 @@
 TEST(TestDecodingState, FrameContinuity) {
   VCMDecodingState dec_state;
   // Check that makes decision based on correct method.
-  VCMFrameBuffer frame, frame_key;
+  VCMFrameBuffer frame;
   frame.SetState(kStateEmpty);
-  frame_key.SetState(kStateEmpty);
   VCMPacket* packet = new VCMPacket();
   packet->isFirstPacket = 1;
   packet->timestamp = 1;
@@ -41,15 +40,11 @@
   packet->codecSpecificHeader.codec = kRTPVideoVP8;
   packet->codecSpecificHeader.codecHeader.VP8.pictureId = 0x007F;
   frame.InsertPacket(*packet, 0, false, 0);
-  // Always start with a key frame.
+  // Should return true on init.
   dec_state.Reset();
-  EXPECT_FALSE(dec_state.ContinuousFrame(&frame));
-  packet->frameType = kVideoFrameKey;
-  frame_key.InsertPacket(*packet, 0, false, 0);
-  EXPECT_TRUE(dec_state.ContinuousFrame(&frame_key));
+  EXPECT_TRUE(dec_state.ContinuousFrame(&frame));
   dec_state.SetState(&frame);
   frame.Reset();
-  packet->frameType = kVideoFrameDelta;
   // Use pictureId
   packet->codecSpecificHeader.codecHeader.VP8.pictureId = 0x0002;
   frame.InsertPacket(*packet, 0, false, 0);
@@ -463,4 +458,5 @@
 
   delete packet;
 }
+
 }  // namespace webrtc
diff --git a/webrtc/modules/video_coding/main/source/jitter_buffer.cc b/webrtc/modules/video_coding/main/source/jitter_buffer.cc
index ecb015a..abcda82 100644
--- a/webrtc/modules/video_coding/main/source/jitter_buffer.cc
+++ b/webrtc/modules/video_coding/main/source/jitter_buffer.cc
@@ -105,6 +105,7 @@
       nack_seq_nums_(),
       max_nack_list_size_(0),
       max_packet_age_to_nack_(0),
+      waiting_for_key_frame_(false),
       decode_with_errors_(false) {
   memset(frame_buffers_, 0, sizeof(frame_buffers_));
   memset(receive_statistics_, 0, sizeof(receive_statistics_));
@@ -146,6 +147,7 @@
     inter_frame_delay_ = rhs.inter_frame_delay_;
     waiting_for_completion_ = rhs.waiting_for_completion_;
     rtt_ms_ = rhs.rtt_ms_;
+    waiting_for_key_frame_ = rhs.waiting_for_key_frame_;
     first_packet_ = rhs.first_packet_;
     last_decoded_state_ =  rhs.last_decoded_state_;
     num_not_decodable_packets_ = rhs.num_not_decodable_packets_;
@@ -199,9 +201,9 @@
   waiting_for_completion_.timestamp = 0;
   waiting_for_completion_.latest_packet_time = -1;
   first_packet_ = true;
+  waiting_for_key_frame_ = false;
   rtt_ms_ = kDefaultRtt;
   num_not_decodable_packets_ = 0;
-  last_decoded_state_.Reset();
 
   WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideoCoding,
                VCMId(vcm_id_, receiver_id_), "JB(0x%x): Jitter buffer: start",
@@ -439,6 +441,10 @@
 
   CleanUpOldOrEmptyFrames();
 
+  if (last_decoded_state_.in_initial_state()) {
+    waiting_for_key_frame_ = true;
+  }
+
   FrameList::iterator it = FindOldestCompleteContinuousFrame();
   if (it == frame_list_.end()) {
     const int64_t end_wait_time_ms = clock_->TimeInMilliseconds() +
@@ -489,6 +495,12 @@
 
   VCMFrameBuffer* oldest_frame = *it;
 
+  // Are we waiting for a key frame?
+  if (waiting_for_key_frame_ && oldest_frame->FrameType() != kVideoFrameKey) {
+    crit_sect_->Leave();
+    return NULL;
+  }
+
   it = frame_list_.erase(it);
   if (frame_list_.empty()) {
     TRACE_EVENT_INSTANT1("webrtc", "JB::FrameListEmptied",
@@ -506,6 +518,10 @@
 
   oldest_frame->SetState(kStateDecoding);
 
+  if (oldest_frame->FrameType() == kVideoFrameKey) {
+    waiting_for_key_frame_ = false;
+  }
+
   // We have a frame - update decoded state with frame info.
   last_decoded_state_.SetState(oldest_frame);
   DropPacketsFromNackList(last_decoded_state_.sequence_num());
@@ -537,12 +553,6 @@
     return NULL;
   }
 
-  // Always start with a key frame.
-  if (last_decoded_state_.in_initial_state() &&
-      oldest_frame->FrameType() != kVideoFrameKey) {
-    return NULL;
-  }
-
   // Incomplete frame pulled out from jitter buffer,
   // update the jitter estimate with what we currently know.
   // This frame shouldn't have been retransmitted, but if we recently
@@ -577,6 +587,10 @@
   // Set as decoding. Propagates the missing_frame bit.
   oldest_frame->SetState(kStateDecoding);
 
+  if (oldest_frame->FrameType() == kVideoFrameKey) {
+    waiting_for_key_frame_ = false;
+  }
+
   num_not_decodable_packets_ += oldest_frame->NotDecodablePackets();
 
   // We have a frame - update decoded state with frame info.
@@ -990,6 +1004,9 @@
   // First look for a complete continuous frame.
   // When waiting for nack, wait for a key frame, if a continuous frame cannot
   // be determined (i.e. initial decoding state).
+  if (last_decoded_state_.in_initial_state()) {
+    waiting_for_key_frame_ = true;
+  }
   FrameList::iterator it = FindOldestCompleteContinuousFrame();
   if (it == frame_list_.end()) {
     // If we didn't find one we're good with a complete key frame.
@@ -1000,7 +1017,6 @@
     }
   }
   VCMFrameBuffer* oldest_frame = *it;
-
   // Update jitter estimate
   const bool retransmitted = (oldest_frame->GetNackCount() > 0);
   if (retransmitted) {
@@ -1023,6 +1039,10 @@
   // decoder.
   oldest_frame->SetState(kStateDecoding);
 
+  if (oldest_frame->FrameType() == kVideoFrameKey) {
+    waiting_for_key_frame_ = false;
+  }
+
   // We have a frame - update decoded state with frame info.
   last_decoded_state_.SetState(oldest_frame);
   DropPacketsFromNackList(last_decoded_state_.sequence_num());
@@ -1100,6 +1120,7 @@
     TRACE_EVENT_INSTANT1("webrtc", "JB::FrameListEmptied",
                          "type", "RecycleFramesUntilKeyFrame");
   }
+  waiting_for_key_frame_ = true;
   last_decoded_state_.Reset();  // TODO(mikhal): No sync.
   missing_sequence_numbers_.clear();
   return false;
@@ -1241,8 +1262,11 @@
   if (oldest_frame == NULL) {
     // No complete frame no point to continue.
     return frame_list_.end();
+  } else if (waiting_for_key_frame_ &&
+      oldest_frame->FrameType() != kVideoFrameKey) {
+    // We are waiting for a key frame.
+    return frame_list_.end();
   }
-
   // We have a complete continuous frame.
   return it;
 }
diff --git a/webrtc/modules/video_coding/main/source/jitter_buffer.h b/webrtc/modules/video_coding/main/source/jitter_buffer.h
index 13dcf83..669089f 100644
--- a/webrtc/modules/video_coding/main/source/jitter_buffer.h
+++ b/webrtc/modules/video_coding/main/source/jitter_buffer.h
@@ -294,6 +294,7 @@
   std::vector<uint16_t> nack_seq_nums_;
   size_t max_nack_list_size_;
   int max_packet_age_to_nack_;  // Measured in sequence numbers.
+  bool waiting_for_key_frame_;
 
   bool decode_with_errors_;
   DISALLOW_COPY_AND_ASSIGN(VCMJitterBuffer);