Refactor frame generation code so it can be used by multiple modules.

R=pbos@webrtc.org, stefan@webrtc.org, pbos, stefan
BUG=

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

git-svn-id: http://webrtc.googlecode.com/svn/trunk@4791 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/video_engine/test/common/frame_generator_capturer.cc b/webrtc/video_engine/test/common/frame_generator_capturer.cc
index 0c2a15c..96a7af8 100644
--- a/webrtc/video_engine/test/common/frame_generator_capturer.cc
+++ b/webrtc/video_engine/test/common/frame_generator_capturer.cc
@@ -10,22 +10,58 @@
 
 #include "webrtc/video_engine/test/common/frame_generator_capturer.h"
 
+#include <math.h>
+#include <string.h>
+
+#include "webrtc/common_video/test/frame_generator.h"
+#include "webrtc/system_wrappers/interface/clock.h"
 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
 #include "webrtc/system_wrappers/interface/event_wrapper.h"
 #include "webrtc/system_wrappers/interface/sleep.h"
 #include "webrtc/system_wrappers/interface/thread_wrapper.h"
-#include "webrtc/video_engine/test/common/frame_generator.h"
+#include "webrtc/video_engine/new_include/video_send_stream.h"
 
 namespace webrtc {
 namespace test {
+namespace {
+class ChromaGenerator : public FrameGenerator {
+ public:
+  ChromaGenerator(size_t width, size_t height, Clock* clock) : clock_(clock) {
+    assert(width > 0);
+    assert(height > 0);
+    frame_.CreateEmptyFrame(static_cast<int>(width),
+                            static_cast<int>(height),
+                            static_cast<int>(width),
+                            static_cast<int>((width + 1) / 2),
+                            static_cast<int>((width + 1) / 2));
+    memset(frame_.buffer(kYPlane), 0x80, frame_.allocated_size(kYPlane));
+  }
+
+  virtual I420VideoFrame& NextFrame() OVERRIDE {
+    double angle =
+        static_cast<double>(clock_->CurrentNtpInMilliseconds()) / 1000.0;
+    uint8_t u = fabs(sin(angle)) * 0xFF;
+    uint8_t v = fabs(cos(angle)) * 0xFF;
+
+    memset(frame_.buffer(kUPlane), u, frame_.allocated_size(kUPlane));
+    memset(frame_.buffer(kVPlane), v, frame_.allocated_size(kVPlane));
+    return frame_;
+  }
+
+ private:
+  Clock* clock_;
+  I420VideoFrame frame_;
+};
+}  // namespace
 
 FrameGeneratorCapturer* FrameGeneratorCapturer::Create(
     VideoSendStreamInput* input,
-    FrameGenerator* frame_generator,
-    int target_fps) {
-  FrameGeneratorCapturer* capturer =
-      new FrameGeneratorCapturer(input, frame_generator, target_fps);
-
+    size_t width,
+    size_t height,
+    int target_fps,
+    Clock* clock) {
+  FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
+      clock, input, new ChromaGenerator(width, height, clock), target_fps);
   if (!capturer->Init()) {
     delete capturer;
     return NULL;
@@ -34,11 +70,32 @@
   return capturer;
 }
 
-FrameGeneratorCapturer::FrameGeneratorCapturer(
+FrameGeneratorCapturer* FrameGeneratorCapturer::CreateFromYuvFile(
     VideoSendStreamInput* input,
-    FrameGenerator* frame_generator,
-    int target_fps)
+    const char* file_name,
+    size_t width,
+    size_t height,
+    int target_fps,
+    Clock* clock) {
+  FrameGeneratorCapturer* capturer = new FrameGeneratorCapturer(
+      clock,
+      input,
+      FrameGenerator::CreateFromYuvFile(file_name, width, height),
+      target_fps);
+  if (!capturer->Init()) {
+    delete capturer;
+    return NULL;
+  }
+
+  return capturer;
+}
+
+FrameGeneratorCapturer::FrameGeneratorCapturer(Clock* clock,
+                                               VideoSendStreamInput* input,
+                                               FrameGenerator* frame_generator,
+                                               int target_fps)
     : VideoCapturer(input),
+      clock_(clock),
       sending_(false),
       tick_(EventWrapper::Create()),
       lock_(CriticalSectionWrapper::CreateCriticalSection()),
@@ -82,8 +139,13 @@
 void FrameGeneratorCapturer::InsertFrame() {
   {
     CriticalSectionScoped cs(lock_.get());
-    if (sending_)
-      frame_generator_->InsertFrame(input_);
+    if (sending_) {
+      int64_t time_before = clock_->CurrentNtpInMilliseconds();
+      I420VideoFrame& frame = frame_generator_->NextFrame();
+      frame.set_render_time_ms(time_before);
+      int64_t time_after = clock_->CurrentNtpInMilliseconds();
+      input_->PutFrame(frame, static_cast<uint32_t>(time_after - time_before));
+    }
   }
   tick_->Wait(WEBRTC_EVENT_INFINITE);
 }