Handle multiple calls to set initial delay

BUG= 1419

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3562 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/video_engine/stream_synchronization.cc b/webrtc/video_engine/stream_synchronization.cc
index a7e3b25..48eb36a 100644
--- a/webrtc/video_engine/stream_synchronization.cc
+++ b/webrtc/video_engine/stream_synchronization.cc
@@ -249,13 +249,15 @@
 }
 
 void StreamSynchronization::SetTargetBufferingDelay(int target_delay_ms) {
+  // Initial extra delay for audio (accounting for existing extra delay).
+  channel_delay_->extra_audio_delay_ms +=
+      target_delay_ms - base_target_delay_ms_;
+  // The video delay is compared to the last value (and how much we can update
+  // is limited by that as well).
+  channel_delay_->last_video_delay_ms +=
+      target_delay_ms - base_target_delay_ms_;
   // Video is already delayed by the desired amount.
   base_target_delay_ms_ = target_delay_ms;
-  // Setting initial extra delay for audio.
-  channel_delay_->extra_audio_delay_ms += target_delay_ms;
-  // The video delay is compared to the last value (and how much we can updated
-  // is limited by that as well).
-  channel_delay_->last_video_delay_ms += target_delay_ms;
 }
 
 }  // namespace webrtc
diff --git a/webrtc/video_engine/stream_synchronization_unittest.cc b/webrtc/video_engine/stream_synchronization_unittest.cc
index 49629f5..44cb146 100644
--- a/webrtc/video_engine/stream_synchronization_unittest.cc
+++ b/webrtc/video_engine/stream_synchronization_unittest.cc
@@ -490,6 +490,27 @@
                              &extra_audio_delay_ms, &total_video_delay_ms));
   EXPECT_EQ(base_target_delay_ms, extra_audio_delay_ms);
   EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
+  // Triggering another call with the same values. Delay should not be modified.
+  base_target_delay_ms = 2000;
+  current_audio_delay_ms = base_target_delay_ms;
+  total_video_delay_ms = base_target_delay_ms;
+  sync_->SetTargetBufferingDelay(base_target_delay_ms);
+  EXPECT_TRUE(DelayedStreams(base_target_delay_ms, base_target_delay_ms,
+                             current_audio_delay_ms,
+                             &extra_audio_delay_ms, &total_video_delay_ms));
+  EXPECT_EQ(base_target_delay_ms, extra_audio_delay_ms);
+  EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
+  // Changing delay value - intended to test this module only. In practice it
+  // would take VoE time to adapt.
+  base_target_delay_ms = 5000;
+  current_audio_delay_ms = base_target_delay_ms;
+  total_video_delay_ms = base_target_delay_ms;
+  sync_->SetTargetBufferingDelay(base_target_delay_ms);
+  EXPECT_TRUE(DelayedStreams(base_target_delay_ms, base_target_delay_ms,
+                             current_audio_delay_ms,
+                             &extra_audio_delay_ms, &total_video_delay_ms));
+  EXPECT_EQ(base_target_delay_ms, extra_audio_delay_ms);
+  EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
 }
 
 TEST_F(StreamSynchronizationTest, BothDelayedAudioLaterWithBaseDelay) {