Request key frames to battle error propagation.

The VP8 decoder wrapper will request key frames 30 frames after seeing
a packet loss, if it hasn't received a state refresh (only possible
through key frames in this version).

For this to be possible the jitter buffer has been made aware of
picture ids to be able to detect frame losses. Legacy JB code to
handle streams without marker bits was also removed since it
conflicts with streams with FEC.

BUG=
TEST=

Review URL: http://webrtc-codereview.appspot.com/239002

git-svn-id: http://webrtc.googlecode.com/svn/trunk@774 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/src/modules/video_coding/main/source/frame_buffer.cc b/src/modules/video_coding/main/source/frame_buffer.cc
index bb4ec87..f143044 100644
--- a/src/modules/video_coding/main/source/frame_buffer.cc
+++ b/src/modules/video_coding/main/source/frame_buffer.cc
@@ -69,6 +69,10 @@
     return _sessionInfo.GetHighSeqNum();
 }
 
+int VCMFrameBuffer::PictureId() const {
+  return _sessionInfo.PictureId();
+}
+
 bool
 VCMFrameBuffer::IsSessionComplete() const
 {
@@ -234,13 +238,6 @@
     return _sessionInfo.HaveLastPacket();
 }
 
-bool
-VCMFrameBuffer::ForceSetHaveLastPacket()
-{
-    _sessionInfo.ForceSetHaveLastPacket();
-    return _sessionInfo.IsSessionComplete();
-}
-
 void
 VCMFrameBuffer::Reset()
 {
@@ -346,10 +343,7 @@
     PrepareForDecode();
     _frameType = ConvertFrameType(_sessionInfo.FrameType());
     _completeFrame = _sessionInfo.IsSessionComplete();
-    // TODO(holmer): This bit is disabled for now since we can't tell whether
-    // we have had a full frame loss or if we've just lost an FEC/empty packet.
-    // _missingFrame = _sessionInfo.PreviousFrameLoss();
-    _missingFrame = false;
+    _missingFrame = _sessionInfo.PreviousFrameLoss();
 }
 
 WebRtc_Word32
diff --git a/src/modules/video_coding/main/source/frame_buffer.h b/src/modules/video_coding/main/source/frame_buffer.h
index 619834f..b554408 100644
--- a/src/modules/video_coding/main/source/frame_buffer.h
+++ b/src/modules/video_coding/main/source/frame_buffer.h
@@ -45,7 +45,6 @@
     bool IsRetransmitted() const;
     bool IsSessionComplete() const;
     bool HaveLastPacket() const;
-    bool ForceSetHaveLastPacket();
     // Makes sure the session contain a decodable stream.
     void MakeSessionDecodable();
 
@@ -55,6 +54,8 @@
     // Get highest packet sequence number in frame
     WebRtc_Word32 GetHighSeqNum() const;
 
+    int PictureId() const;
+
     // Set counted status (as counted by JB or not)
     void SetCountedFrame(bool frameCounted);
     bool GetCountedFrame() const;
diff --git a/src/modules/video_coding/main/source/jitter_buffer.cc b/src/modules/video_coding/main/source/jitter_buffer.cc
index 1dd5745..5557dd6 100644
--- a/src/modules/video_coding/main/source/jitter_buffer.cc
+++ b/src/modules/video_coding/main/source/jitter_buffer.cc
@@ -73,6 +73,7 @@
     _frameBuffersTSOrder(),
     _lastDecodedSeqNum(-1),
     _lastDecodedTimeStamp(-1),
+    _lastDecodedPictureId(-1),
     _packetsNotDecodable(0),
     _receiveStatistics(),
     _incomingFrameRate(0),
@@ -88,7 +89,6 @@
     _nackMode(kNoNack),
     _NACKSeqNum(),
     _NACKSeqNumLength(0),
-    _missingMarkerBits(false),
     _firstPacket(true)
 {
     memset(_frameBuffers, 0, sizeof(_frameBuffers));
@@ -141,10 +141,10 @@
         _nackMode = rhs._nackMode;
         _rttMs = rhs._rttMs;
         _NACKSeqNumLength = rhs._NACKSeqNumLength;
-        _missingMarkerBits = rhs._missingMarkerBits;
         _firstPacket = rhs._firstPacket;
         _lastDecodedSeqNum =  rhs._lastDecodedSeqNum;
         _lastDecodedTimeStamp = rhs._lastDecodedTimeStamp;
+        _lastDecodedPictureId = rhs._lastDecodedPictureId;
         _packetsNotDecodable = rhs._packetsNotDecodable;
         memcpy(_receiveStatistics, rhs._receiveStatistics,
                sizeof(_receiveStatistics));
@@ -196,7 +196,6 @@
     _waitingForCompletion.frameSize = 0;
     _waitingForCompletion.timestamp = 0;
     _waitingForCompletion.latestPacketTime = -1;
-    _missingMarkerBits = false;
     _firstPacket = true;
     _NACKSeqNumLength = 0;
     _rttMs = 0;
@@ -215,6 +214,7 @@
     _running = false;
     _lastDecodedTimeStamp = -1;
     _lastDecodedSeqNum = -1;
+    _lastDecodedPictureId = -1;
     _frameBuffersTSOrder.Flush();
     for (int i = 0; i < kMaxNumberOfFrames; i++)
     {
@@ -258,6 +258,7 @@
     }
     _lastDecodedSeqNum = -1;
     _lastDecodedTimeStamp = -1;
+    _lastDecodedPictureId = -1;
     _packetsNotDecodable = 0;
 
     _frameEvent.Reset();
@@ -274,7 +275,6 @@
     _waitingForCompletion.timestamp = 0;
     _waitingForCompletion.latestPacketTime = -1;
 
-    _missingMarkerBits = false;
     _firstPacket = true;
 
     _NACKSeqNumLength = 0;
@@ -602,22 +602,9 @@
         oldestFrame = oldestFrameItem->GetItem();
     }
     // is the frame complete?
-    if (oldestFrame != NULL)
+    if (oldestFrame != NULL && kStateComplete != oldestFrame->GetState())
     {
-        if (kStateComplete != oldestFrame->GetState())
-        {
-            // Try to see if the frame is complete even though the state is not
-            // complete. Can happen if markerbit is not set.
-            if (!CheckForCompleteFrame(oldestFrameItem))
-            {
-                oldestFrame = NULL;
-            }
-        }
-        else
-        {
-            // we have a complete frame
-            currentLow = oldestFrame->GetLowSeqNum();
-        }
+        oldestFrame = NULL;
     }
     if (oldestFrame == NULL)
     {
@@ -647,52 +634,6 @@
     return oldestFrameItem;
 }
 
-// Check if the oldest frame is complete even though it isn't complete.
-// This can happen when makerbit is not set
-// Must be called under the critical section _critSect.
-// Return false for lost packets
-bool
-VCMJitterBuffer::CheckForCompleteFrame(VCMFrameListItem* oldestFrameItem)
-{
-    const VCMFrameListItem*
-          nextFrameItem = _frameBuffersTSOrder.Next(oldestFrameItem);
-    VCMFrameBuffer* oldestFrame = NULL;
-    if (oldestFrameItem != NULL)
-    {
-        oldestFrame = oldestFrameItem->GetItem();
-    }
-    if (nextFrameItem != NULL)
-    {
-       // We have received at least one packet from a later frame.
-       if(!oldestFrame->HaveLastPacket()) // If we don't have the markerbit
-        {
-            VCMFrameBuffer* nextFrame = nextFrameItem->GetItem();
-            // Verify that we have received the first packet of the next frame,
-            // so we're not missing the last packet.
-            if (nextFrame != NULL && nextFrame->GetLowSeqNum() ==
-                static_cast<WebRtc_UWord16>(oldestFrame->GetHighSeqNum() + 1))
-            {
-                _missingMarkerBits = true;
-                bool completeSession = oldestFrame->ForceSetHaveLastPacket();
-                if (completeSession)
-                {
-                    UpdateFrameState(oldestFrame);
-                }
-                const VCMFrameBufferStateEnum state = oldestFrame->GetState();
-                if (state == kStateComplete)
-                {
-                    if (oldestFrame->Length() > 0)
-                    {
-                        UpdateJitterAndDelayEstimates(*oldestFrame, false);
-                    }
-                    return true;
-                }
-            }
-        }
-    }
-    return false;
-}
-
 // Call from inside the critical section _critSect
 void
 VCMJitterBuffer::RecycleFrame(VCMFrameBuffer* frame)
@@ -710,7 +651,6 @@
     ReleaseFrameInternal(frame);
 }
 
-
 // Calculate frame and bit rates
 WebRtc_Word32
 VCMJitterBuffer::GetUpdate(WebRtc_UWord32& frameRate, WebRtc_UWord32& bitRate)
@@ -878,13 +818,11 @@
         // Ignore retransmitted and empty frames.
         UpdateJitterAndDelayEstimates(*oldestFrame, false);
     }
-
-    // This needs to be done before we clean up old frames,
-    // otherwise we'll remove ourselves...
-    oldestFrame->SetState(kStateDecoding);
     _frameBuffersTSOrder.Erase(oldestFrameListItem);
     oldestFrameListItem = NULL;
 
+    oldestFrame->SetState(kStateDecoding);
+
     CleanUpOldFrames();
     CleanUpSizeZeroFrames();
 
@@ -893,6 +831,7 @@
     // We have a frame - store seqnum & timestamp
     _lastDecodedSeqNum = oldestFrame->GetHighSeqNum();
     _lastDecodedTimeStamp = oldestFrame->TimeStamp();
+    _lastDecodedPictureId = oldestFrame->PictureId();
 
     return oldestFrame;
 }
@@ -918,17 +857,6 @@
     }
     estimate += static_cast<WebRtc_UWord32>
                 (_jitterEstimate.GetJitterEstimate(rttMult) + 0.5);
-    if (_missingMarkerBits)
-    {
-        // Since the incoming packets are all missing marker bits we have to
-        // wait until the first packet of the next frame arrives, before we can
-        // safely say that the frame is complete. Therefore we have to
-        // compensate the jitter buffer level with one frame period.
-        // TODO(holmer): The timestamp diff should probably be filtered
-        // (max filter) since the diff can alternate between e.g. 3000 and 6000
-        // if we have a frame rate between 15 and 30 frames per seconds.
-        estimate += _delayEstimate.CurrentTimeStampDiffMs();
-    }
     return estimate;
 }
 
@@ -1045,8 +973,18 @@
     {
         return false;
     }
-    else if (oldestFrame->GetLowSeqNum() != (_lastDecodedSeqNum + 1)
-                                             % 0x00010000)
+    // We can't use sequence numbers to detect frame loss when FEC is enabled.
+    // Assume FEC is only enabled for VP8 with picture ids, and use picture ids
+    // to detect frame loss in that situation.
+    else if (oldestFrame->PictureId() == kNoPictureId)
+    {
+        if (oldestFrame->GetLowSeqNum() !=
+            (_lastDecodedSeqNum + 1) % 0x00010000)
+        {
+            return false;
+        }
+    }
+    else if (!ContinuousPictureId(oldestFrame->PictureId()))
     {
         return false;
     }
@@ -1111,19 +1049,24 @@
     _frameBuffersTSOrder.Erase(oldestFrameListItem);
     oldestFrameListItem = NULL;
 
-    CleanUpOldFrames();
-    CleanUpSizeZeroFrames();
-
     // Look for previous frame loss
     VerifyAndSetPreviousFrameLost(*oldestFrame);
+
+    // The state must be changed to decoding before cleaning up zero sized
+    // frames to avoid empty frames being cleaned up and then given to the
+    // decoder.
     // Set as decoding. Propagates the missingFrame bit.
     oldestFrame->SetState(kStateDecoding);
 
+    CleanUpOldFrames();
+    CleanUpSizeZeroFrames();
+
     _packetsNotDecodable += oldestFrame->NotDecodablePackets();
 
     // Store current seqnum & time
     _lastDecodedSeqNum = oldestFrame->GetHighSeqNum();
     _lastDecodedTimeStamp = oldestFrame->TimeStamp();
+    _lastDecodedPictureId = oldestFrame->PictureId();
 
     return oldestFrame;
 }
@@ -1171,11 +1114,16 @@
         // Ignore retransmitted and empty frames.
         UpdateJitterAndDelayEstimates(*oldestFrame, false);
     }
-
-    // This needs to be done before we clean up old frames,
-    // otherwise we might release the frame we want to decode right now.
-    oldestFrame->SetState(kStateDecoding);
     _frameBuffersTSOrder.Erase(oldestFrameListItem);
+    oldestFrameListItem = NULL;
+
+    // Look for previous frame loss
+    VerifyAndSetPreviousFrameLost(*oldestFrame);
+
+    // The state must be changed to decoding before cleaning up zero sized
+    // frames to avoid empty frames being cleaned up and then given to the
+    // decoder.
+    oldestFrame->SetState(kStateDecoding);
 
     // Clean up old frames and empty frames
     CleanUpOldFrames();
@@ -1185,6 +1133,7 @@
     // Store seqnum & timestamp
     _lastDecodedSeqNum = oldestFrame->GetHighSeqNum();
     _lastDecodedTimeStamp = oldestFrame->TimeStamp();
+    _lastDecodedPictureId = oldestFrame->PictureId();
 
     return oldestFrame;
 }
@@ -1903,7 +1852,8 @@
         VCMFrameBuffer* ptrTempBuffer = frameListItem->GetItem();
 
         // pop frame if its size zero but store seqnum
-        if (ptrTempBuffer->Length() == 0)
+        if (ptrTempBuffer->Length() == 0 &&
+            ptrTempBuffer->GetState() != kStateDecoding)
         {
             WebRtc_Word32 frameHighSeqNum = ptrTempBuffer->GetHighSeqNum();
             if (frameHighSeqNum == -1)
@@ -1983,21 +1933,47 @@
     frame.MakeSessionDecodable(); // make sure the session can be decoded.
     if (frame.FrameType() == kVideoFrameKey)
         return;
-    WebRtc_UWord16 nextExpectedSeqNum =
-        static_cast<WebRtc_UWord16>(_lastDecodedSeqNum + 1);
+
     if (_lastDecodedSeqNum == -1)
     {
-        // First frame
+        // First frame is not a key frame
         frame.SetPreviousFrameLoss();
+        return;
     }
-    else if (static_cast<WebRtc_UWord16>(frame.GetLowSeqNum()) !=
-        nextExpectedSeqNum)
+    // We can't use sequence numbers to detect frame loss when FEC is enabled.
+    // Assume FEC is only enabled for VP8 with picture ids, and use picture ids
+    // to detect frame loss in that situation.
+    if (frame.PictureId() == kNoPictureId)
     {
-        // Frame loss
+        WebRtc_UWord16 nextExpectedSeqNum =
+            static_cast<WebRtc_UWord16>(_lastDecodedSeqNum + 1);
+        if (static_cast<WebRtc_UWord16>(frame.GetLowSeqNum()) !=
+            nextExpectedSeqNum)
+        {
+            frame.SetPreviousFrameLoss();
+        }
+    }
+    else if (!ContinuousPictureId(frame.PictureId()))
+    {
         frame.SetPreviousFrameLoss();
     }
 }
 
+bool VCMJitterBuffer::ContinuousPictureId(int pictureId) const {
+  if (pictureId < _lastDecodedPictureId) {
+    // Wrap
+    if (_lastDecodedPictureId >= (1<<8)) {
+      // 15 bits used for picture id
+      return (((_lastDecodedPictureId + 1) % 0x7FFF) == pictureId);
+    } else {
+      // 7 bits used for picture id
+      return (((_lastDecodedPictureId + 1) % 0x7F) == pictureId);
+    }
+  }
+  // No wrap
+  return (_lastDecodedPictureId + 1 == pictureId);
+}
+
 bool
 VCMJitterBuffer::WaitForNack()
 {
diff --git a/src/modules/video_coding/main/source/jitter_buffer.h b/src/modules/video_coding/main/source/jitter_buffer.h
index fc027e1..8ac8cd6 100644
--- a/src/modules/video_coding/main/source/jitter_buffer.h
+++ b/src/modules/video_coding/main/source/jitter_buffer.h
@@ -150,10 +150,6 @@
     // Find oldest complete frame, used for getting next frame to decode
     VCMFrameListItem* FindOldestCompleteContinuousFrame();
 
-    // Check if a frame is missing the markerbit but is complete
-    bool CheckForCompleteFrame(VCMFrameListItem* oldestFrameItem);
-
-
     void CleanUpOldFrames();
     void CleanUpSizeZeroFrames();
 
@@ -181,6 +177,7 @@
                                     const void* timestamp);
     static bool CompleteDecodableKeyFrameCriteria(VCMFrameBuffer* frame,
                                                   const void* notUsed);
+    bool ContinuousPictureId(int pictureId) const;
     // Decide whether should wait for NACK (mainly relevant for hybrid mode)
     bool WaitForNack();
 
@@ -205,6 +202,7 @@
     WebRtc_Word32           _lastDecodedSeqNum;
     // Timestamp of last frame that was given to decoder
     WebRtc_Word64           _lastDecodedTimeStamp;
+    int                     _lastDecodedPictureId;
     WebRtc_UWord32          _packetsNotDecodable;
 
     // Statistics
@@ -241,7 +239,6 @@
     WebRtc_UWord16          _NACKSeqNum[kNackHistoryLength];
     WebRtc_UWord32          _NACKSeqNumLength;
 
-    bool                    _missingMarkerBits;
     bool                    _firstPacket;
 };
 
diff --git a/src/modules/video_coding/main/source/session_info.cc b/src/modules/video_coding/main/source/session_info.cc
index 58d95dc..cbfcf22 100644
--- a/src/modules/video_coding/main/source/session_info.cc
+++ b/src/modules/video_coding/main/source/session_info.cc
@@ -26,7 +26,8 @@
     _emptySeqNumLow(-1),
     _emptySeqNumHigh(-1),
     _markerSeqNum(-1),
-    _packetsNotDecodable(0)
+    _packetsNotDecodable(0),
+    _pictureId(-1)
 {
 }
 
@@ -55,6 +56,10 @@
     return LatestSequenceNumber(_emptySeqNumHigh, _highSeqNum, NULL);
 }
 
+int VCMSessionInfo::PictureId() const {
+  return _pictureId;
+}
+
 void
 VCMSessionInfo::Reset() {
   for (int i = 0; i <= _highestPacketIndex; ++i)
@@ -71,6 +76,7 @@
   _highestPacketIndex = 0;
   _markerSeqNum = -1;
   _packetsNotDecodable = 0;
+  _pictureId = -1;
 }
 
 WebRtc_UWord32
@@ -169,6 +175,11 @@
     }
     returnLength = packetSize;
 
+    if (packet.codecSpecificHeader.codec == kRTPVideoVP8)
+    {
+        _pictureId = packet.codecSpecificHeader.codecHeader.VP8.pictureId;
+    }
+
     if (packet.markerBit)
     {
         _markerBit = true;
@@ -645,13 +656,6 @@
     return _markerBit;
 }
 
-void
-VCMSessionInfo::ForceSetHaveLastPacket()
-{
-    _markerBit = true;
-    UpdateCompleteSession();
-}
-
 bool
 VCMSessionInfo::IsRetransmitted() const
 {
diff --git a/src/modules/video_coding/main/source/session_info.h b/src/modules/video_coding/main/source/session_info.h
index 64878a5..942c85d 100644
--- a/src/modules/video_coding/main/source/session_info.h
+++ b/src/modules/video_coding/main/source/session_info.h
@@ -61,7 +61,6 @@
 
     WebRtc_UWord32 GetSessionLength();
     bool HaveLastPacket() const;
-    void ForceSetHaveLastPacket();
     bool IsRetransmitted() const;
     webrtc::FrameType FrameType() const { return _frameType; }
 
@@ -74,6 +73,7 @@
     WebRtc_Word32 GetLowSeqNum() const;
     // returns highest seqNum, media or empty
     WebRtc_Word32 GetHighSeqNum() const;
+    int PictureId() const;
 
     WebRtc_UWord32 PrepareForDecode(WebRtc_UWord8* ptrStartOfLayer,
                                     VideoCodecType codec);
@@ -130,6 +130,7 @@
     WebRtc_Word32      _markerSeqNum;
     // Number of packets discarded because the decoder can't use them.
     int                _packetsNotDecodable;
+    int                _pictureId;
 };
 
 } // namespace webrtc