Rewrote external media test.

BUG=
TEST=

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2007 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/src/voice_engine/main/test/auto_test/fakes/fake_media_process.h b/src/voice_engine/main/test/auto_test/fakes/fake_media_process.h
new file mode 100644
index 0000000..ee0ebd6
--- /dev/null
+++ b/src/voice_engine/main/test/auto_test/fakes/fake_media_process.h
@@ -0,0 +1,44 @@
+/*
+ *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+#ifndef FAKE_MEDIA_PROCESS_H_
+#define FAKE_MEDIA_PROCESS_H_
+
+#include <cmath>
+
+class FakeMediaProcess : public webrtc::VoEMediaProcess {
+ public:
+  virtual void Process(const int channel,
+                       const webrtc::ProcessingTypes type,
+                       WebRtc_Word16 audio_10ms[],
+                       const int length,
+                       const int sampling_freq_hz,
+                       const bool stereo) {
+    for (int i = 0; i < length; i++) {
+      if (!stereo) {
+        audio_10ms[i] = static_cast<WebRtc_Word16>(audio_10ms[i] *
+            sin(2.0 * 3.14 * frequency * 400.0 / sampling_freq_hz));
+      } else {
+        // Interleaved stereo.
+        audio_10ms[2 * i] = static_cast<WebRtc_Word16> (
+            audio_10ms[2 * i] * sin(2.0 * 3.14 *
+                frequency * 400.0 / sampling_freq_hz));
+        audio_10ms[2 * i + 1] = static_cast<WebRtc_Word16> (
+            audio_10ms[2 * i + 1] * sin(2.0 * 3.14 *
+                frequency * 400.0 / sampling_freq_hz));
+      }
+      frequency++;
+    }
+  }
+
+ private:
+  int frequency;
+};
+
+#endif  // FAKE_MEDIA_PROCESS_H_
diff --git a/src/voice_engine/main/test/auto_test/fixtures/after_streaming_fixture.cc b/src/voice_engine/main/test/auto_test/fixtures/after_streaming_fixture.cc
index e1a4820..c12c806 100644
--- a/src/voice_engine/main/test/auto_test/fixtures/after_streaming_fixture.cc
+++ b/src/voice_engine/main/test/auto_test/fixtures/after_streaming_fixture.cc
@@ -39,7 +39,7 @@
 
   TEST_LOG("You need to speak manually into the microphone for this test.\n");
   TEST_LOG("Please start speaking now.\n");
-  Sleep(2000);
+  Sleep(1000);
 }
 
 void AfterStreamingFixture::RestartFakeMicrophone() {
diff --git a/src/voice_engine/main/test/auto_test/standard/external_media_test.cc b/src/voice_engine/main/test/auto_test/standard/external_media_test.cc
new file mode 100644
index 0000000..559fada
--- /dev/null
+++ b/src/voice_engine/main/test/auto_test/standard/external_media_test.cc
@@ -0,0 +1,84 @@
+/*
+ *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "voice_engine/main/interface/voe_external_media.h"
+#include "voice_engine/main/test/auto_test/fakes/fake_media_process.h"
+#include "voice_engine/main/test/auto_test/fixtures/after_streaming_fixture.h"
+
+class ExternalMediaTest : public AfterStreamingFixture {
+ protected:
+  void TestRegisterExternalMedia(int channel, webrtc::ProcessingTypes type) {
+    FakeMediaProcess fake_media_process;
+    EXPECT_EQ(0, voe_xmedia_->RegisterExternalMediaProcessing(
+        channel, type, fake_media_process));
+    Sleep(2000);
+
+    TEST_LOG("Back to normal.\n");
+    EXPECT_EQ(0, voe_xmedia_->DeRegisterExternalMediaProcessing(
+        channel, type));
+    Sleep(2000);
+  }
+};
+
+TEST_F(ExternalMediaTest, ManualCanRecordAndPlaybackUsingExternalPlayout) {
+  SwitchToManualMicrophone();
+
+  EXPECT_EQ(0, voe_base_->StopSend(channel_));
+  EXPECT_EQ(0, voe_base_->StopPlayout(channel_));
+  EXPECT_EQ(0, voe_xmedia_->SetExternalPlayoutStatus(true));
+  EXPECT_EQ(0, voe_base_->StartPlayout(channel_));
+  EXPECT_EQ(0, voe_base_->StartSend(channel_));
+
+  TEST_LOG("Recording data for 2 seconds starting now: please speak.\n");
+  int16_t recording[32000];
+  for (int i = 0; i < 200; i++) {
+    int sample_length = 0;
+    EXPECT_EQ(0, voe_xmedia_->ExternalPlayoutGetData(
+        &(recording[i * 160]), 16000, 100, sample_length));
+    EXPECT_EQ(160, sample_length);
+    Sleep(10);
+  }
+
+  EXPECT_EQ(0, voe_base_->StopSend(channel_));
+  EXPECT_EQ(0, voe_base_->StopPlayout(channel_));
+  EXPECT_EQ(0, voe_xmedia_->SetExternalPlayoutStatus(false));
+  EXPECT_EQ(0, voe_base_->StartPlayout(channel_));
+  EXPECT_EQ(0, voe_xmedia_->SetExternalRecordingStatus(true));
+  EXPECT_EQ(0, voe_base_->StartSend(channel_));
+
+  TEST_LOG("Playing back recording, you should hear what you said earlier.\n");
+  for (int i = 0; i < 200; i++) {
+    EXPECT_EQ(0, voe_xmedia_->ExternalRecordingInsertData(
+        &(recording[i * 160]), 160, 16000, 20));
+    Sleep(10);
+  }
+
+  EXPECT_EQ(0, voe_base_->StopSend(channel_));
+  EXPECT_EQ(0, voe_xmedia_->SetExternalRecordingStatus(false));
+}
+
+TEST_F(ExternalMediaTest,
+    ManualRegisterExternalMediaProcessingOnAllChannelsAffectsPlayout) {
+  TEST_LOG("Enabling external media processing: audio should be affected.\n");
+  TestRegisterExternalMedia(-1, webrtc::kPlaybackAllChannelsMixed);
+}
+
+TEST_F(ExternalMediaTest,
+    ManualRegisterExternalMediaOnSingleChannelAffectsPlayout) {
+  TEST_LOG("Enabling external media processing: audio should be affected.\n");
+  TestRegisterExternalMedia(channel_, webrtc::kRecordingPerChannel);
+}
+
+TEST_F(ExternalMediaTest,
+    ManualRegisterExternalMediaOnAllChannelsMixedAffectsRecording) {
+  SwitchToManualMicrophone();
+  TEST_LOG("Speak and verify your voice is distorted.\n");
+  TestRegisterExternalMedia(-1, webrtc::kRecordingAllChannelsMixed);
+}
diff --git a/src/voice_engine/main/test/auto_test/voe_standard_test.cc b/src/voice_engine/main/test/auto_test/voe_standard_test.cc
index ed05425..bcbafb2 100644
--- a/src/voice_engine/main/test/auto_test/voe_standard_test.cc
+++ b/src/voice_engine/main/test/auto_test/voe_standard_test.cc
@@ -188,31 +188,6 @@
   fflush(NULL);
 }
 
-#ifdef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
-void MyMedia::Process(const int channel,
-                      const ProcessingTypes type,
-                      WebRtc_Word16 audio_10ms[],
-                      const int length,
-                      const int samplingFreqHz,
-                      const bool stereo) {
-  for (int i = 0; i < length; i++) {
-    if (!stereo) {
-      audio_10ms[i] = (WebRtc_Word16) (audio_10ms[i] *
-          sin(2.0 * 3.14 * f * 400.0 / samplingFreqHz));
-    } else {
-      // interleaved stereo
-      audio_10ms[2 * i] = (WebRtc_Word16) (audio_10ms[2 * i] *
-          sin(2.0 * 3.14 * f * 400.0 / samplingFreqHz));
-      audio_10ms[2 * i + 1] = (WebRtc_Word16) (audio_10ms[2 * i + 1] *
-          sin(2.0 * 3.14 * f * 400.0 / samplingFreqHz));
-    }
-    f++;
-  }
-}
-#endif
-
-MyMedia mobj;
-
 FakeExternalTransport::FakeExternalTransport(VoENetwork* ptr)
     : my_network_(ptr),
       thread_(NULL),
@@ -943,100 +918,6 @@
   if (TestStartPlaying() != 0) return -1;
 
   //////////////////
-  // External media
-
-#ifdef _TEST_XMEDIA_
-  TEST_LOG("\n\n+++ External media tests +++\n\n");
-
-#ifdef WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
-  TEST_LOG("Stop playing file as microphone \n");
-  TEST_LOG("==> Talk into the microphone \n");
-  TEST_MUSTPASS(voe_file_->StopPlayingFileAsMicrophone(0));
-
-  TEST_LOG("Enabling external playout\n");
-  TEST_MUSTPASS(voe_base_->StopSend(0));
-  TEST_MUSTPASS(voe_base_->StopPlayout(0));
-  TEST_MUSTPASS(voe_xmedia_->SetExternalPlayoutStatus(true));
-  TEST_MUSTPASS(voe_base_->StartPlayout(0));
-  TEST_MUSTPASS(voe_base_->StartSend(0));
-
-  TEST_LOG("Writing 2 secs of play data to vector\n");
-  int getLen;
-  WebRtc_Word16 speechData[32000];
-  for (int i = 0; i < 200; i++) {
-    TEST_MUSTPASS(voe_xmedia_->ExternalPlayoutGetData(speechData+i*160,
-            16000,
-            100,
-            getLen));
-    TEST_MUSTPASS(160 != getLen);
-    SLEEP(10);
-  }
-
-  TEST_LOG("Disabling external playout\n");
-  TEST_MUSTPASS(voe_base_->StopSend(0));
-  TEST_MUSTPASS(voe_base_->StopPlayout(0));
-  TEST_MUSTPASS(voe_xmedia_->SetExternalPlayoutStatus(false));
-  TEST_MUSTPASS(voe_base_->StartPlayout(0));
-
-  TEST_LOG("Enabling external recording\n");
-  TEST_MUSTPASS(voe_xmedia_->SetExternalRecordingStatus(true));
-  TEST_MUSTPASS(voe_base_->StartSend(0));
-
-  TEST_LOG("Inserting record data from vector\n");
-  for (int i = 0; i < 200; i++) {
-    TEST_MUSTPASS(voe_xmedia_->ExternalRecordingInsertData(speechData+i*160,
-            160,
-            16000,
-            20));
-    SLEEP(10);
-  }
-
-  TEST_LOG("Disabling external recording\n");
-  TEST_MUSTPASS(voe_base_->StopSend(0));
-  TEST_MUSTPASS(voe_xmedia_->SetExternalRecordingStatus(false));
-  TEST_MUSTPASS(voe_base_->StartSend(0));
-
-  TEST_LOG("==> Start playing a file as microphone again \n");
-  TEST_MUSTPASS(voe_file_->StartPlayingFileAsMicrophone(0, AudioFilename(),
-          true, true));
-#else
-  TEST_LOG("Skipping external rec and playout tests - \
-             WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT not defined \n");
-#endif // WEBRTC_VOE_EXTERNAL_REC_AND_PLAYOUT
-  TEST_LOG("Enabling playout external media processing => "
-    "played audio should now be affected \n");
-  TEST_MUSTPASS(voe_xmedia_->RegisterExternalMediaProcessing(
-          -1, kPlaybackAllChannelsMixed, mobj));
-  SLEEP(2000);
-  TEST_LOG("Back to normal again \n");
-  TEST_MUSTPASS(voe_xmedia_->DeRegisterExternalMediaProcessing(
-          -1, kPlaybackAllChannelsMixed));
-  SLEEP(2000);
-  // Note that we must do per channel here because PlayFileAsMicrophone
-  // is only done on ch 0.
-  TEST_LOG("Enabling recording external media processing => "
-    "played audio should now be affected \n");
-  TEST_MUSTPASS(voe_xmedia_->RegisterExternalMediaProcessing(
-          0, kRecordingPerChannel, mobj));
-  SLEEP(2000);
-  TEST_LOG("Back to normal again \n");
-  TEST_MUSTPASS(voe_xmedia_->DeRegisterExternalMediaProcessing(
-          0, kRecordingPerChannel));
-  SLEEP(2000);
-  TEST_LOG("Enabling recording external media processing => "
-    "speak and make sure that voice is affected \n");
-  TEST_MUSTPASS(voe_xmedia_->RegisterExternalMediaProcessing(
-          -1, kRecordingAllChannelsMixed, mobj));
-  SLEEP(2000);
-  TEST_LOG("Back to normal again \n");
-  TEST_MUSTPASS(voe_xmedia_->DeRegisterExternalMediaProcessing(
-          -1, kRecordingAllChannelsMixed));
-  SLEEP(2000);
-#else
-  TEST_LOG("\n\n+++ External media tests NOT ENABLED +++\n");
-#endif // #ifdef _TEST_XMEDIA_
-
-  //////////////////
   // Stop streaming
   TEST_LOG("\n\n+++ Stop streaming +++\n\n");
 
diff --git a/src/voice_engine/main/test/auto_test/voe_standard_test.h b/src/voice_engine/main/test/auto_test/voe_standard_test.h
index 4512e5d..ce4135f 100644
--- a/src/voice_engine/main/test/auto_test/voe_standard_test.h
+++ b/src/voice_engine/main/test/auto_test/voe_standard_test.h
@@ -166,20 +166,6 @@
   int _vadDecision;
 };
 
-#ifdef WEBRTC_VOICE_ENGINE_EXTERNAL_MEDIA_API
-class MyMedia : public VoEMediaProcess {
- public:
-  virtual void Process(const int channel,
-                       const ProcessingTypes type,
-                       WebRtc_Word16 audio_10ms[],
-                       const int length,
-                       const int samplingFreqHz,
-                       const bool stereo);
- private:
-  int f;
-};
-#endif
-
 class SubAPIManager {
  public:
   SubAPIManager()
diff --git a/src/voice_engine/main/test/auto_test/voe_unit_test.cc b/src/voice_engine/main/test/auto_test/voe_unit_test.cc
index 2c1e5c5..d3a6e72 100644
--- a/src/voice_engine/main/test/auto_test/voe_unit_test.cc
+++ b/src/voice_engine/main/test/auto_test/voe_unit_test.cc
@@ -8,6 +8,8 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
+#include "voe_unit_test.h"
+
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
@@ -16,10 +18,9 @@
 #include <conio.h>
 #endif
 
-#include "voe_unit_test.h"
-
-#include "../../source/voice_engine_defines.h"
-#include "thread_wrapper.h"
+#include "system_wrappers/interface/thread_wrapper.h"
+#include "voice_engine/main/source/voice_engine_defines.h"
+#include "voice_engine/main/test/auto_test/fakes/fake_media_process.h"
 
 using namespace webrtc;
 
@@ -180,7 +181,7 @@
 }
 
 // VoEVEMediaProcess
-MyMedia mpobj;
+FakeMediaProcess mpobj;
 
 // ----------------------------------------------------------------------------
 //                               VoEUnitTest
diff --git a/src/voice_engine/main/test/voice_engine_tests.gypi b/src/voice_engine/main/test/voice_engine_tests.gypi
index 94f940a..554e68a 100644
--- a/src/voice_engine/main/test/voice_engine_tests.gypi
+++ b/src/voice_engine/main/test/voice_engine_tests.gypi
@@ -42,6 +42,7 @@
         'auto_test/standard/codec_test.cc',
         'auto_test/standard/dtmf_test.cc',
         'auto_test/standard/encryption_test.cc',
+        'auto_test/standard/external_media_test.cc',
         'auto_test/standard/file_test.cc',
         'auto_test/standard/hardware_before_initializing_test.cc',
         'auto_test/standard/hardware_before_streaming_test.cc',