Fix audio/video sync when FEC is enabled.

Also improves the tests by adding a test case for FEC, and running the a/v sync
tests with NACK and simulated packet loss.

BUG=crbug/374104
R=mflodman@webrtc.org, pbos@webrtc.org

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@7053 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/test/call_test.cc b/webrtc/test/call_test.cc
index e29b8de..ccad755 100644
--- a/webrtc/test/call_test.cc
+++ b/webrtc/test/call_test.cc
@@ -159,6 +159,8 @@
 const uint8_t CallTest::kSendPayloadType = 100;
 const uint8_t CallTest::kFakeSendPayloadType = 125;
 const uint8_t CallTest::kSendRtxPayloadType = 98;
+const uint8_t CallTest::kRedPayloadType = 118;
+const uint8_t CallTest::kUlpfecPayloadType = 119;
 const uint32_t CallTest::kSendRtxSsrcs[kNumSsrcs] = {0xBADCAFD, 0xBADCAFE,
                                                      0xBADCAFF};
 const uint32_t CallTest::kSendSsrcs[kNumSsrcs] = {0xC0FFED, 0xC0FFEE, 0xC0FFEF};
diff --git a/webrtc/test/call_test.h b/webrtc/test/call_test.h
index b390329..a03c3bb 100644
--- a/webrtc/test/call_test.h
+++ b/webrtc/test/call_test.h
@@ -36,6 +36,8 @@
   static const uint8_t kSendPayloadType;
   static const uint8_t kSendRtxPayloadType;
   static const uint8_t kFakeSendPayloadType;
+  static const uint8_t kRedPayloadType;
+  static const uint8_t kUlpfecPayloadType;
   static const uint32_t kSendRtxSsrcs[kNumSsrcs];
   static const uint32_t kSendSsrcs[kNumSsrcs];
   static const uint32_t kReceiverLocalSsrc;
diff --git a/webrtc/video/call_perf_tests.cc b/webrtc/video/call_perf_tests.cc
index 0eaf310..3d1bf7f 100644
--- a/webrtc/video/call_perf_tests.cc
+++ b/webrtc/video/call_perf_tests.cc
@@ -48,6 +48,8 @@
 
 class CallPerfTest : public test::CallTest {
  protected:
+  void TestAudioVideoSync(bool fec);
+
   void TestMinTransmitBitrate(bool pad_to_min_bitrate);
 
   void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
@@ -188,7 +190,7 @@
   int64_t first_time_in_sync_;
 };
 
-TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
+void CallPerfTest::TestAudioVideoSync(bool fec) {
   class AudioPacketReceiver : public PacketReceiver {
    public:
     AudioPacketReceiver(int channel, VoENetwork* voe_network)
@@ -229,6 +231,7 @@
 
   FakeNetworkPipe::Config net_config;
   net_config.queue_delay_ms = 500;
+  net_config.loss_percent = 5;
   SyncRtcpObserver audio_observer(net_config);
   VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(),
                                     channel,
@@ -257,6 +260,14 @@
   CreateSendConfig(1);
   CreateMatchingReceiveConfigs();
 
+  send_config_.rtp.nack.rtp_history_ms = kNackRtpHistoryMs;
+  if (fec) {
+    send_config_.rtp.fec.red_payload_type = kRedPayloadType;
+    send_config_.rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
+    receive_configs_[0].rtp.fec.red_payload_type = kRedPayloadType;
+    receive_configs_[0].rtp.fec.ulpfec_payload_type = kUlpfecPayloadType;
+  }
+  receive_configs_[0].rtp.nack.rtp_history_ms = 1000;
   receive_configs_[0].renderer = &observer;
   receive_configs_[0].audio_channel_id = channel;
 
@@ -294,6 +305,14 @@
   VoiceEngine::Delete(voice_engine);
 }
 
+TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) {
+  TestAudioVideoSync(false);
+}
+
+TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSyncWithFec) {
+  TestAudioVideoSync(true);
+}
+
 void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config,
                                       int threshold_ms,
                                       int start_time_ms,
diff --git a/webrtc/video/end_to_end_tests.cc b/webrtc/video/end_to_end_tests.cc
index 11ce642..114ab65 100644
--- a/webrtc/video/end_to_end_tests.cc
+++ b/webrtc/video/end_to_end_tests.cc
@@ -44,8 +44,6 @@
 namespace webrtc {
 
 static const unsigned long kSilenceTimeoutMs = 2000;
-static const int kRedPayloadType = 118;
-static const int kUlpfecPayloadType = 119;
 
 class EndToEndTest : public test::CallTest {
  public:
diff --git a/webrtc/video/video_send_stream_tests.cc b/webrtc/video/video_send_stream_tests.cc
index 76e9f0f..b628312 100644
--- a/webrtc/video/video_send_stream_tests.cc
+++ b/webrtc/video/video_send_stream_tests.cc
@@ -311,8 +311,6 @@
 }
 
 TEST_F(VideoSendStreamTest, SupportsFec) {
-  static const int kRedPayloadType = 118;
-  static const int kUlpfecPayloadType = 119;
   class FecObserver : public test::SendTest {
    public:
     FecObserver()
@@ -486,8 +484,6 @@
   static const uint32_t start = 90;
   static const uint32_t stop = 290;
 
-  static const int kRedPayloadType = 118;
-  static const int kUlpfecPayloadType = 119;
   // Observer that verifies that the expected number of packets and bytes
   // arrive for each frame size, from start_size to stop_size.
   class FrameFragmentationTest : public test::SendTest,
diff --git a/webrtc/video_engine/vie_receiver.cc b/webrtc/video_engine/vie_receiver.cc
index 5d90ac6..a19c4b0 100644
--- a/webrtc/video_engine/vie_receiver.cc
+++ b/webrtc/video_engine/vie_receiver.cc
@@ -193,7 +193,8 @@
     return false;
   }
   header.payload_type_frequency = kVideoPayloadTypeFrequency;
-  return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
+  bool in_order = IsPacketInOrder(header);
+  return ReceivePacket(rtp_packet, rtp_packet_length, header, in_order);
 }
 
 void ViEReceiver::ReceivedBWEPacket(