Allow the FEC to protect up to maximum #packets (48) if the 
media packet list is above this max.
Review URL: http://webrtc-codereview.appspot.com/45005

git-svn-id: http://webrtc.googlecode.com/svn/trunk@138 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/modules/rtp_rtcp/source/forward_error_correction.cc b/modules/rtp_rtcp/source/forward_error_correction.cc
index 9156da6..4cef2ed 100644
--- a/modules/rtp_rtcp/source/forward_error_correction.cc
+++ b/modules/rtp_rtcp/source/forward_error_correction.cc
@@ -32,7 +32,7 @@
 // ULP header size in bytes (L bit is cleared).
 const WebRtc_UWord8 kUlpHeaderSizeLBitClear = (2 + kMaskSizeLBitClear);
 
-//Transport header size in bytes. Assume UDP/IPv4 as a reasonable minimum.
+// Transport header size in bytes. Assume UDP/IPv4 as a reasonable minimum.
 const WebRtc_UWord8 kTransportOverhead = 28;
 
 //
@@ -118,13 +118,12 @@
         (lBit == 1)? kUlpHeaderSizeLBitSet : kUlpHeaderSizeLBitClear;
     const WebRtc_UWord16 fecRtpOffset =
         kFecHeaderSize + ulpHeaderSize - kRtpHeaderSize;
-    const WebRtc_UWord16 maxMediaPackets = numMaskBytes * 8;
 
-    if (numMediaPackets > maxMediaPackets)
+    if (numMediaPackets > kMaxMediaPackets)
     {
         WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id,
             "%s can only protect %d media packets per frame; %d requested",
-            __FUNCTION__, maxMediaPackets, numMediaPackets);
+            __FUNCTION__, kMaxMediaPackets, numMediaPackets);
         return -1;
     }
 
@@ -174,7 +173,8 @@
     }
 
     // Result in Q0 with an unsigned round.
-    WebRtc_UWord32 numFecPackets = (numMediaPackets * protectionFactor + (1 << 7)) >> 8;
+    WebRtc_UWord32 numFecPackets = (numMediaPackets * protectionFactor +
+                                   (1 << 7)) >> 8;
     if (numFecPackets == 0)
     {
         return 0;
@@ -283,8 +283,8 @@
         {
             //Note: This shouldn't happen: means packet mask is wrong or poorly designed
             WEBRTC_TRACE(kTraceError, kTraceRtpRtcp, _id,
-                "Packet mask has row of zeros %d %d",
-                numMediaPackets, numFecPackets);
+                "Packet mask has row of zeros %d %d %d ",
+                numMediaPackets, numImportantPackets, numFecPackets);
             delete packetMask;
             return -1;
 
diff --git a/modules/rtp_rtcp/source/forward_error_correction.h b/modules/rtp_rtcp/source/forward_error_correction.h
index 2c9ff3f..275643b 100644
--- a/modules/rtp_rtcp/source/forward_error_correction.h
+++ b/modules/rtp_rtcp/source/forward_error_correction.h
@@ -23,6 +23,10 @@
 class ForwardErrorCorrection
 {
 public:
+
+    // Maximum number of media packets we can protect
+    static const int kMaxMediaPackets = 48;
+
     /**
      * The ListWrapper parameters of #GenerateFEC() should reference structs of this type.
      */
diff --git a/modules/rtp_rtcp/source/rtp_sender_video.cc b/modules/rtp_rtcp/source/rtp_sender_video.cc
index f0d3168..a02c08e 100644
--- a/modules/rtp_rtcp/source/rtp_sender_video.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_video.cc
@@ -163,7 +163,12 @@
 
         // Add packet to FEC list
         _rtpPacketListFec.PushBack(ptrGenericFEC);
-        _mediaPacketListFec.PushBack(ptrGenericFEC->pkt);
+        // FEC can only protect up to kMaxMediaPackets packets
+        if (_mediaPacketListFec.GetSize() <
+            ForwardErrorCorrection::kMaxMediaPackets)
+        {
+            _mediaPacketListFec.PushBack(ptrGenericFEC->pkt);
+        }
 
         // Last packet in frame
         if (markerBit)
@@ -182,6 +187,14 @@
             // Replace payload and clear marker bit.
             lastMediaRtpHeader.data[1] = _payloadTypeRED;
 
+            // Number of first partition packets cannot exceed kMaxMediaPackets
+            if (_numberFirstPartition >
+                    ForwardErrorCorrection::kMaxMediaPackets)
+            {
+                _numberFirstPartition =
+                    ForwardErrorCorrection::kMaxMediaPackets;
+            }
+
             retVal = _fec.GenerateFEC(_mediaPacketListFec, _fecProtectionFactor,
                                       _numberFirstPartition, fecPacketList);
             while(!_rtpPacketListFec.Empty())