Improve AV-sync when initial delay is set and NetEq has long buffer.

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3883 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/modules/audio_coding/neteq/webrtc_neteq.c b/webrtc/modules/audio_coding/neteq/webrtc_neteq.c
index 1be0133..31940c8 100644
--- a/webrtc/modules/audio_coding/neteq/webrtc_neteq.c
+++ b/webrtc/modules/audio_coding/neteq/webrtc_neteq.c
@@ -440,6 +440,9 @@
     NetEqMainInst->MCUinst.NoOfExpandCalls = 0;
     NetEqMainInst->MCUinst.fs = fs;
 
+    /* Not in AV-sync by default. */
+    NetEqMainInst->MCUinst.av_sync = 0;
+
 #ifdef NETEQ_ATEVENT_DECODE
     /* init DTMF decoder */
     ok = WebRtcNetEQ_DtmfDecoderInit(&(NetEqMainInst->MCUinst.DTMF_inst),fs,560);
@@ -806,7 +809,7 @@
 #endif
 
     ok = WebRtcNetEQ_RecOutInternal(&NetEqMainInst->DSPinst, pw16_outData,
-        pw16_len, 0 /* not BGN only */);
+        pw16_len, 0 /* not BGN only */, NetEqMainInst->MCUinst.av_sync);
     if (ok != 0)
     {
         NetEqMainInst->ErrorCode = -ok;
@@ -887,7 +890,7 @@
     }
 
     ok  = WebRtcNetEQ_RecOutInternal(&NetEqMainInst->DSPinst, pw16_outData,
-        pw16_len, 0 /* not BGN only */);
+        pw16_len, 0 /* not BGN only */, NetEqMainInst->MCUinst.av_sync);
     if (ok != 0)
     {
         NetEqMainInst->ErrorCode = -ok;
@@ -958,7 +961,7 @@
 #endif
 
     ok = WebRtcNetEQ_RecOutInternal(&NetEqMainInst->DSPinst, pw16_outData,
-        pw16_len, 1 /* BGN only */);
+        pw16_len, 1 /* BGN only */, NetEqMainInst->MCUinst.av_sync);
     if (ok != 0)
     {
         NetEqMainInst->ErrorCode = -ok;
@@ -1186,7 +1189,8 @@
         /* Query packet buffer for number of samples. */
         temp32 = WebRtcNetEQ_PacketBufferGetSize(
             &NetEqMainInst->MCUinst.PacketBuffer_inst,
-            &NetEqMainInst->MCUinst.codec_DB_inst);
+            &NetEqMainInst->MCUinst.codec_DB_inst,
+            NetEqMainInst->MCUinst.av_sync);
 
         /* Divide by sample rate.
          * Calculate temp32 * 1000 / fs to get result in ms. */
@@ -1671,3 +1675,21 @@
 
   WebRtcNetEQ_ClearActivityStats(&NetEqMainInst->DSPinst);
 }
+
+void WebRtcNetEQ_EnableAVSync(void* inst, int enable) {
+  MainInst_t *NetEqMainInst = (MainInst_t*) inst;
+  NetEqMainInst->MCUinst.av_sync = (enable != 0) ? 1 : 0;
+}
+
+int WebRtcNetEQ_RecInSyncRTP(void* inst, WebRtcNetEQ_RTPInfo* rtp_info,
+                             uint32_t receive_timestamp) {
+  MainInst_t *NetEqMainInst = (MainInst_t*) inst;
+  if (NetEqMainInst->MCUinst.av_sync == 0)
+    return -1;
+  if (WebRtcNetEQ_RecInRTPStruct(inst, rtp_info, kSyncPayload,
+                                 SYNC_PAYLOAD_LEN_BYTES,
+                                 receive_timestamp) < 0) {
+    return -1;
+  }
+  return SYNC_PAYLOAD_LEN_BYTES;
+}