Reland "Added option to specify a maximum file size when recording an AEC dump.", commit ae2c5ad12afc8cc29fe9c59dea432b697b871a87.

The revert of the original CL was commit 36d4c545007129446e551c45c17b25377dce89a4.
Original review: https://codereview.webrtc.org/1413483003/

The original CL changes a function on audio_processing.h that is used by Chrome, this CL adds back the old function.

TBR=glaznev@webrtc.org, henrik.lundin@webrtc.org, solenberg@google.com, henrikg@webrtc.org, perkj@webrtc.org
BUG=webrtc:4741
Committed: https://crrev.com/f4f5cb09277d5ef6aeac8341e5f54a055867803a
Cr-Commit-Position: refs/heads/master@{#11093}

Review URL: https://codereview.webrtc.org/1540103002

Cr-Commit-Position: refs/heads/master@{#11267}
diff --git a/talk/app/webrtc/java/jni/peerconnection_jni.cc b/talk/app/webrtc/java/jni/peerconnection_jni.cc
index eea5fbd..f80f576 100644
--- a/talk/app/webrtc/java/jni/peerconnection_jni.cc
+++ b/talk/app/webrtc/java/jni/peerconnection_jni.cc
@@ -1306,11 +1306,12 @@
 }
 
 JOW(jboolean, PeerConnectionFactory_nativeStartAecDump)(
-    JNIEnv* jni, jclass, jlong native_factory, jint file) {
+    JNIEnv* jni, jclass, jlong native_factory, jint file,
+    jint filesize_limit_bytes) {
 #if defined(ANDROID)
   rtc::scoped_refptr<PeerConnectionFactoryInterface> factory(
       factoryFromJava(native_factory));
-  return factory->StartAecDump(file);
+  return factory->StartAecDump(file, filesize_limit_bytes);
 #else
   return false;
 #endif
diff --git a/talk/app/webrtc/java/src/org/webrtc/PeerConnectionFactory.java b/talk/app/webrtc/java/src/org/webrtc/PeerConnectionFactory.java
index 37f0bee..6ee062b 100644
--- a/talk/app/webrtc/java/src/org/webrtc/PeerConnectionFactory.java
+++ b/talk/app/webrtc/java/src/org/webrtc/PeerConnectionFactory.java
@@ -148,8 +148,8 @@
   // Starts recording an AEC dump. Ownership of the file is transfered to the
   // native code. If an AEC dump is already in progress, it will be stopped and
   // a new one will start using the provided file.
-  public boolean startAecDump(int file_descriptor) {
-    return nativeStartAecDump(nativeFactory, file_descriptor);
+  public boolean startAecDump(int file_descriptor, int filesize_limit_bytes) {
+    return nativeStartAecDump(nativeFactory, file_descriptor, filesize_limit_bytes);
   }
 
   // Stops recording an AEC dump. If no AEC dump is currently being recorded,
@@ -256,7 +256,8 @@
   private static native long nativeCreateAudioTrack(
       long nativeFactory, String id, long nativeSource);
 
-  private static native boolean nativeStartAecDump(long nativeFactory, int file_descriptor);
+  private static native boolean nativeStartAecDump(
+      long nativeFactory, int file_descriptor, int filesize_limit_bytes);
 
   private static native void nativeStopAecDump(long nativeFactory);
 
diff --git a/talk/app/webrtc/peerconnectionfactory.cc b/talk/app/webrtc/peerconnectionfactory.cc
index c58f88c..ab8fdf5 100644
--- a/talk/app/webrtc/peerconnectionfactory.cc
+++ b/talk/app/webrtc/peerconnectionfactory.cc
@@ -225,9 +225,10 @@
   return VideoSourceProxy::Create(signaling_thread_, source);
 }
 
-bool PeerConnectionFactory::StartAecDump(rtc::PlatformFile file) {
+bool PeerConnectionFactory::StartAecDump(rtc::PlatformFile file,
+                                         int64_t max_size_bytes) {
   RTC_DCHECK(signaling_thread_->IsCurrent());
-  return channel_manager_->StartAecDump(file);
+  return channel_manager_->StartAecDump(file, max_size_bytes);
 }
 
 void PeerConnectionFactory::StopAecDump() {
diff --git a/talk/app/webrtc/peerconnectionfactory.h b/talk/app/webrtc/peerconnectionfactory.h
index 8b274e1..a38218a 100644
--- a/talk/app/webrtc/peerconnectionfactory.h
+++ b/talk/app/webrtc/peerconnectionfactory.h
@@ -82,7 +82,7 @@
       CreateAudioTrack(const std::string& id,
                        AudioSourceInterface* audio_source) override;
 
-  bool StartAecDump(rtc::PlatformFile file) override;
+  bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes) override;
   void StopAecDump() override;
   bool StartRtcEventLog(rtc::PlatformFile file) override;
   void StopRtcEventLog() override;
diff --git a/talk/app/webrtc/peerconnectionfactoryproxy.h b/talk/app/webrtc/peerconnectionfactoryproxy.h
index 714ce6b..1d0b6aa 100644
--- a/talk/app/webrtc/peerconnectionfactoryproxy.h
+++ b/talk/app/webrtc/peerconnectionfactoryproxy.h
@@ -62,7 +62,7 @@
                 CreateVideoTrack, const std::string&,  VideoSourceInterface*)
   PROXY_METHOD2(rtc::scoped_refptr<AudioTrackInterface>,
                 CreateAudioTrack, const std::string&,  AudioSourceInterface*)
-  PROXY_METHOD1(bool, StartAecDump, rtc::PlatformFile)
+  PROXY_METHOD2(bool, StartAecDump, rtc::PlatformFile, int64_t)
   PROXY_METHOD0(void, StopAecDump)
   PROXY_METHOD1(bool, StartRtcEventLog, rtc::PlatformFile)
   PROXY_METHOD0(void, StopRtcEventLog)
diff --git a/talk/app/webrtc/peerconnectioninterface.h b/talk/app/webrtc/peerconnectioninterface.h
index da99a7b..e449dc4 100644
--- a/talk/app/webrtc/peerconnectioninterface.h
+++ b/talk/app/webrtc/peerconnectioninterface.h
@@ -578,9 +578,11 @@
   // Starts AEC dump using existing file. Takes ownership of |file| and passes
   // it on to VoiceEngine (via other objects) immediately, which will take
   // the ownerhip. If the operation fails, the file will be closed.
-  // TODO(grunell): Remove when Chromium has started to use AEC in each source.
-  // http://crbug.com/264611.
-  virtual bool StartAecDump(rtc::PlatformFile file) = 0;
+  // A maximum file size in bytes can be specified. When the file size limit is
+  // reached, logging is stopped automatically. If max_size_bytes is set to a
+  // value <= 0, no limit will be used, and logging will continue until the
+  // StopAecDump function is called.
+  virtual bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes) = 0;
 
   // Stops logging the AEC dump.
   virtual void StopAecDump() = 0;
diff --git a/talk/media/base/fakemediaengine.h b/talk/media/base/fakemediaengine.h
index 149704f..c17ab28 100644
--- a/talk/media/base/fakemediaengine.h
+++ b/talk/media/base/fakemediaengine.h
@@ -762,7 +762,9 @@
 
   int GetInputLevel() { return 0; }
 
-  bool StartAecDump(rtc::PlatformFile file) { return false; }
+  bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes) {
+    return false;
+  }
 
   void StopAecDump() {}
 
diff --git a/talk/media/base/mediaengine.h b/talk/media/base/mediaengine.h
index 467614b..3f7eec2 100644
--- a/talk/media/base/mediaengine.h
+++ b/talk/media/base/mediaengine.h
@@ -102,8 +102,10 @@
   virtual const std::vector<VideoCodec>& video_codecs() = 0;
   virtual RtpCapabilities GetVideoCapabilities() = 0;
 
-  // Starts AEC dump using existing file.
-  virtual bool StartAecDump(rtc::PlatformFile file) = 0;
+  // Starts AEC dump using existing file, a maximum file size in bytes can be
+  // specified. Logging is stopped just before the size limit is exceeded.
+  // If max_size_bytes is set to a value <= 0, no limit will be used.
+  virtual bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes) = 0;
 
   // Stops recording AEC dump.
   virtual void StopAecDump() = 0;
@@ -185,8 +187,8 @@
     return video_.GetCapabilities();
   }
 
-  virtual bool StartAecDump(rtc::PlatformFile file) {
-    return voice_.StartAecDump(file);
+  virtual bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes) {
+    return voice_.StartAecDump(file, max_size_bytes);
   }
 
   virtual void StopAecDump() {
diff --git a/talk/media/webrtc/fakewebrtcvoiceengine.h b/talk/media/webrtc/fakewebrtcvoiceengine.h
index 65ba927..4c6c016 100644
--- a/talk/media/webrtc/fakewebrtcvoiceengine.h
+++ b/talk/media/webrtc/fakewebrtcvoiceengine.h
@@ -114,8 +114,9 @@
   WEBRTC_VOID_STUB(set_stream_key_pressed, (bool key_pressed));
   WEBRTC_VOID_STUB(set_delay_offset_ms, (int offset));
   WEBRTC_STUB_CONST(delay_offset_ms, ());
-  WEBRTC_STUB(StartDebugRecording, (const char filename[kMaxFilenameSize]));
-  WEBRTC_STUB(StartDebugRecording, (FILE* handle));
+  WEBRTC_STUB(StartDebugRecording,
+              (const char filename[kMaxFilenameSize], int64_t max_size_bytes));
+  WEBRTC_STUB(StartDebugRecording, (FILE * handle, int64_t max_size_bytes));
   WEBRTC_STUB(StopDebugRecording, ());
   WEBRTC_VOID_STUB(UpdateHistogramsOnCallEnd, ());
   webrtc::EchoCancellation* echo_cancellation() const override { return NULL; }
diff --git a/talk/media/webrtc/webrtcvoiceengine.cc b/talk/media/webrtc/webrtcvoiceengine.cc
index 4285af3..6c07ff4 100644
--- a/talk/media/webrtc/webrtcvoiceengine.cc
+++ b/talk/media/webrtc/webrtcvoiceengine.cc
@@ -1014,7 +1014,8 @@
   return true;
 }
 
-bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
+bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
+                                     int64_t max_size_bytes) {
   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
   if (!aec_dump_file_stream) {
@@ -1024,7 +1025,8 @@
     return false;
   }
   StopAecDump();
-  if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
+  if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
+          aec_dump_file_stream, max_size_bytes) !=
       webrtc::AudioProcessing::kNoError) {
     LOG_RTCERR0(StartDebugRecording);
     fclose(aec_dump_file_stream);
@@ -1038,8 +1040,8 @@
   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   if (!is_dumping_aec_) {
     // Start dumping AEC when we are not dumping.
-    if (voe_wrapper_->processing()->StartDebugRecording(
-        filename.c_str()) != webrtc::AudioProcessing::kNoError) {
+    if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
+            filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
       LOG_RTCERR1(StartDebugRecording, filename.c_str());
     } else {
       is_dumping_aec_ = true;
@@ -1051,7 +1053,7 @@
   RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
   if (is_dumping_aec_) {
     // Stop dumping AEC when we are dumping.
-    if (voe_wrapper_->processing()->StopDebugRecording() !=
+    if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
         webrtc::AudioProcessing::kNoError) {
       LOG_RTCERR0(StopDebugRecording);
     }
diff --git a/talk/media/webrtc/webrtcvoiceengine.h b/talk/media/webrtc/webrtcvoiceengine.h
index 0f2f59e..ce3bdf3 100644
--- a/talk/media/webrtc/webrtcvoiceengine.h
+++ b/talk/media/webrtc/webrtcvoiceengine.h
@@ -94,8 +94,11 @@
   // Set the external ADM. This can only be called before Init.
   bool SetAudioDeviceModule(webrtc::AudioDeviceModule* adm);
 
-  // Starts AEC dump using existing file.
-  bool StartAecDump(rtc::PlatformFile file);
+  // Starts AEC dump using an existing file. A maximum file size in bytes can be
+  // specified. When the maximum file size is reached, logging is stopped and
+  // the file is closed. If max_size_bytes is set to <= 0, no limit will be
+  // used.
+  bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes);
 
   // Stops AEC dump.
   void StopAecDump();
diff --git a/talk/session/media/channelmanager.cc b/talk/session/media/channelmanager.cc
index e7a4b8b..bd89a41 100644
--- a/talk/session/media/channelmanager.cc
+++ b/talk/session/media/channelmanager.cc
@@ -550,9 +550,11 @@
   }
 }
 
-bool ChannelManager::StartAecDump(rtc::PlatformFile file) {
-  return worker_thread_->Invoke<bool>(
-      Bind(&MediaEngineInterface::StartAecDump, media_engine_.get(), file));
+bool ChannelManager::StartAecDump(rtc::PlatformFile file,
+                                  int64_t max_size_bytes) {
+  return worker_thread_->Invoke<bool>(Bind(&MediaEngineInterface::StartAecDump,
+                                           media_engine_.get(), file,
+                                           max_size_bytes));
 }
 
 void ChannelManager::StopAecDump() {
diff --git a/talk/session/media/channelmanager.h b/talk/session/media/channelmanager.h
index 2bc516b..f69bf9a 100644
--- a/talk/session/media/channelmanager.h
+++ b/talk/session/media/channelmanager.h
@@ -162,8 +162,10 @@
 
   // The operations below occur on the main thread.
 
-  // Starts AEC dump using existing file.
-  bool StartAecDump(rtc::PlatformFile file);
+  // Starts AEC dump using existing file, with a specified maximum file size in
+  // bytes. When the limit is reached, logging will stop and the file will be
+  // closed. If max_size_bytes is set to <= 0, no limit will be used.
+  bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes);
 
   // Stops recording AEC dump.
   void StopAecDump();