| OLD | NEW | 
|    1 // Copyright 2013 The Chromium Authors. All rights reserved. |    1 // Copyright 2013 The Chromium Authors. All rights reserved. | 
|    2 // Use of this source code is governed by a BSD-style license that can be |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #include "remoting/host/capture_scheduler.h" |    5 #include "remoting/host/capture_scheduler.h" | 
 |    6  | 
 |    7 #include "base/message_loop/message_loop.h" | 
 |    8 #include "base/test/simple_test_tick_clock.h" | 
 |    9 #include "base/timer/mock_timer.h" | 
|    6 #include "testing/gtest/include/gtest/gtest.h" |   10 #include "testing/gtest/include/gtest/gtest.h" | 
|    7  |   11  | 
|    8 namespace remoting { |   12 namespace remoting { | 
|    9  |   13  | 
|   10 static const int kTestInputs[] = { 100, 50, 30, 20, 10, 30, 60, 80 }; |   14 static const int kTestInputs[] = { 100, 50, 30, 20, 10, 30, 60, 80 }; | 
|   11 static const int kMinumumFrameIntervalMs = 50; |   15 static const int kMinumumFrameIntervalMs = 50; | 
|   12  |   16  | 
|   13 TEST(CaptureSchedulerTest, SingleSampleSameTimes) { |   17 class CaptureSchedulerTest : public testing::Test { | 
 |   18  public: | 
 |   19   CaptureSchedulerTest() : capture_called_(false) {} | 
 |   20  | 
 |   21   void InitScheduler() { | 
 |   22     scheduler_.reset(new CaptureScheduler( | 
 |   23         base::Bind(&CaptureSchedulerTest::DoCapture, base::Unretained(this)))); | 
 |   24     scheduler_->set_minimum_interval( | 
 |   25         base::TimeDelta::FromMilliseconds(kMinumumFrameIntervalMs)); | 
 |   26     tick_clock_ = new base::SimpleTestTickClock(); | 
 |   27     scheduler_->SetTickClockForTest(make_scoped_ptr(tick_clock_)); | 
 |   28     capture_timer_ = new base::MockTimer(false, false); | 
 |   29     scheduler_->SetTimerForTest(make_scoped_ptr(capture_timer_)); | 
 |   30     scheduler_->Start(); | 
 |   31   } | 
 |   32  | 
 |   33   void DoCapture() { | 
 |   34     capture_called_ = true; | 
 |   35   } | 
 |   36  | 
 |   37   void CheckCaptureCalled() { | 
 |   38     EXPECT_TRUE(capture_called_); | 
 |   39     capture_called_ = false; | 
 |   40   } | 
 |   41  | 
 |   42   void SimulateSingleFrameCapture( | 
 |   43       base::TimeDelta capture_delay, | 
 |   44       base::TimeDelta encode_delay, | 
 |   45       base::TimeDelta expected_delay_between_frames) { | 
 |   46     capture_timer_->Fire(); | 
 |   47     CheckCaptureCalled(); | 
 |   48     tick_clock_->Advance(capture_delay); | 
 |   49     scheduler_->OnCaptureCompleted(); | 
 |   50     scheduler_->OnFrameEncoded(encode_delay); | 
 |   51     scheduler_->OnFrameSent(); | 
 |   52  | 
 |   53     EXPECT_TRUE(capture_timer_->IsRunning()); | 
 |   54     EXPECT_EQ(std::max(base::TimeDelta(), | 
 |   55                        expected_delay_between_frames - capture_delay), | 
 |   56               capture_timer_->GetCurrentDelay()); | 
 |   57   } | 
 |   58  | 
 |   59  protected: | 
 |   60   base::MessageLoop message_loop_; | 
 |   61  | 
 |   62   scoped_ptr<CaptureScheduler> scheduler_; | 
 |   63  | 
 |   64   // Owned by |scheduler_|. | 
 |   65   base::SimpleTestTickClock* tick_clock_; | 
 |   66   base::MockTimer* capture_timer_; | 
 |   67  | 
 |   68   bool capture_called_; | 
 |   69 }; | 
 |   70  | 
 |   71 TEST_F(CaptureSchedulerTest, SingleSampleSameTimes) { | 
|   14   const int kTestResults[][arraysize(kTestInputs)] = { |   72   const int kTestResults[][arraysize(kTestInputs)] = { | 
|   15     { 400, 200, 120, 80, 50, 120, 240, 320 }, // One core. |   73     { 400, 200, 120, 80, 50, 120, 240, 320 }, // One core. | 
|   16     { 200, 100, 60, 50, 50, 60, 120, 160 },   // Two cores. |   74     { 200, 100, 60, 50, 50, 60, 120, 160 },   // Two cores. | 
|   17     { 100, 50, 50, 50, 50, 50, 60, 80 },      // Four cores. |   75     { 100, 50, 50, 50, 50, 50, 60, 80 },      // Four cores. | 
|   18     { 50, 50, 50, 50, 50, 50, 50, 50 }        // Eight cores. |   76     { 50, 50, 50, 50, 50, 50, 50, 50 }        // Eight cores. | 
|   19   }; |   77   }; | 
|   20  |   78  | 
|   21   for (size_t i = 0; i < arraysize(kTestResults); ++i) { |   79   for (size_t i = 0; i < arraysize(kTestResults); ++i) { | 
|   22     for (size_t j = 0; j < arraysize(kTestInputs); ++j) { |   80     for (size_t j = 0; j < arraysize(kTestInputs); ++j) { | 
|   23       CaptureScheduler scheduler; |   81       InitScheduler(); | 
|   24       scheduler.SetNumOfProcessorsForTest(1 << i); |   82       scheduler_->SetNumOfProcessorsForTest(1 << i); | 
|   25       scheduler.set_minimum_interval( |   83  | 
|   26           base::TimeDelta::FromMilliseconds(kMinumumFrameIntervalMs)); |   84       SimulateSingleFrameCapture( | 
|   27       scheduler.RecordCaptureTime( |   85           base::TimeDelta::FromMilliseconds(kTestInputs[j]), | 
|   28           base::TimeDelta::FromMilliseconds(kTestInputs[j])); |   86           base::TimeDelta::FromMilliseconds(kTestInputs[j]), | 
|   29       scheduler.RecordEncodeTime( |   87           base::TimeDelta::FromMilliseconds(kTestResults[i][j])); | 
|   30           base::TimeDelta::FromMilliseconds(kTestInputs[j])); |  | 
|   31       EXPECT_EQ(kTestResults[i][j], |  | 
|   32                 scheduler.NextCaptureDelay().InMilliseconds()) << i  << " "<< j; |  | 
|   33     } |   88     } | 
|   34   } |   89   } | 
|   35 } |   90 } | 
|   36  |   91  | 
|   37 TEST(CaptureSchedulerTest, SingleSampleDifferentTimes) { |   92 TEST_F(CaptureSchedulerTest, SingleSampleDifferentTimes) { | 
|   38   const int kTestResults[][arraysize(kTestInputs)] = { |   93   const int kTestResults[][arraysize(kTestInputs)] = { | 
|   39     { 360, 220, 120, 60, 60, 120, 220, 360 }, // One core. |   94     { 360, 220, 120, 60, 60, 120, 220, 360 }, // One core. | 
|   40     { 180, 110, 60, 50, 50, 60, 110, 180 },   // Two cores. |   95     { 180, 110, 60, 50, 50, 60, 110, 180 },   // Two cores. | 
|   41     { 90, 55, 50, 50, 50, 50, 55, 90 },       // Four cores. |   96     { 90, 55, 50, 50, 50, 50, 55, 90 },       // Four cores. | 
|   42     { 50, 50, 50, 50, 50, 50, 50, 50 }        // Eight cores. |   97     { 50, 50, 50, 50, 50, 50, 50, 50 }        // Eight cores. | 
|   43   }; |   98   }; | 
|   44  |   99  | 
|   45   for (size_t i = 0; i < arraysize(kTestResults); ++i) { |  100   for (size_t i = 0; i < arraysize(kTestResults); ++i) { | 
|   46     for (size_t j = 0; j < arraysize(kTestInputs); ++j) { |  101     for (size_t j = 0; j < arraysize(kTestInputs); ++j) { | 
|   47       CaptureScheduler scheduler; |  102       InitScheduler(); | 
|   48       scheduler.SetNumOfProcessorsForTest(1 << i); |  103       scheduler_->SetNumOfProcessorsForTest(1 << i); | 
|   49       scheduler.set_minimum_interval( |  104  | 
|   50           base::TimeDelta::FromMilliseconds(kMinumumFrameIntervalMs)); |  105       SimulateSingleFrameCapture( | 
|   51       scheduler.RecordCaptureTime( |  106           base::TimeDelta::FromMilliseconds(kTestInputs[j]), | 
|   52           base::TimeDelta::FromMilliseconds(kTestInputs[j])); |  | 
|   53       scheduler.RecordEncodeTime( |  | 
|   54           base::TimeDelta::FromMilliseconds( |  107           base::TimeDelta::FromMilliseconds( | 
|   55               kTestInputs[arraysize(kTestInputs) - 1 - j])); |  108               kTestInputs[arraysize(kTestInputs) - 1 - j]), | 
|   56       EXPECT_EQ(kTestResults[i][j], |  109           base::TimeDelta::FromMilliseconds(kTestResults[i][j])); | 
|   57                 scheduler.NextCaptureDelay().InMilliseconds()); |  | 
|   58     } |  110     } | 
|   59   } |  111   } | 
|   60 } |  112 } | 
|   61  |  113  | 
|   62 TEST(CaptureSchedulerTest, RollingAverageDifferentTimes) { |  114 TEST_F(CaptureSchedulerTest, RollingAverageDifferentTimes) { | 
|   63   const int kTestResults[][arraysize(kTestInputs)] = { |  115   const int kTestResults[][arraysize(kTestInputs)] = { | 
|   64     { 360, 290, 233, 133, 80, 80, 133, 233 }, // One core. |  116     { 360, 290, 233, 133, 80, 80, 133, 233 }, // One core. | 
|   65     { 180, 145, 116, 66, 50, 50, 66, 116 },   // Two cores. |  117     { 180, 145, 116, 66, 50, 50, 66, 116 },   // Two cores. | 
|   66     { 90, 72, 58, 50, 50, 50, 50, 58 },       // Four cores. |  118     { 90, 72, 58, 50, 50, 50, 50, 58 },       // Four cores. | 
|   67     { 50, 50, 50, 50, 50, 50, 50, 50 }        // Eight cores. |  119     { 50, 50, 50, 50, 50, 50, 50, 50 }        // Eight cores. | 
|   68   }; |  120   }; | 
|   69  |  121  | 
|   70   for (size_t i = 0; i < arraysize(kTestResults); ++i) { |  122   for (size_t i = 0; i < arraysize(kTestResults); ++i) { | 
|   71     CaptureScheduler scheduler; |  123     InitScheduler(); | 
|   72     scheduler.SetNumOfProcessorsForTest(1 << i); |  124     scheduler_->SetNumOfProcessorsForTest(1 << i); | 
|   73     scheduler.set_minimum_interval( |  | 
|   74         base::TimeDelta::FromMilliseconds(kMinumumFrameIntervalMs)); |  | 
|   75     for (size_t j = 0; j < arraysize(kTestInputs); ++j) { |  125     for (size_t j = 0; j < arraysize(kTestInputs); ++j) { | 
|   76       scheduler.RecordCaptureTime( |  126       SimulateSingleFrameCapture( | 
|   77           base::TimeDelta::FromMilliseconds(kTestInputs[j])); |  127           base::TimeDelta::FromMilliseconds(kTestInputs[j]), | 
|   78       scheduler.RecordEncodeTime( |  | 
|   79           base::TimeDelta::FromMilliseconds( |  128           base::TimeDelta::FromMilliseconds( | 
|   80               kTestInputs[arraysize(kTestInputs) - 1 - j])); |  129               kTestInputs[arraysize(kTestInputs) - 1 - j]), | 
|   81       EXPECT_EQ(kTestResults[i][j], |  130           base::TimeDelta::FromMilliseconds(kTestResults[i][j])); | 
|   82                 scheduler.NextCaptureDelay().InMilliseconds()); |  | 
|   83     } |  131     } | 
|   84   } |  132   } | 
|   85 } |  133 } | 
|   86  |  134  | 
 |  135 // Verify that we never have more than 2 pending frames. | 
 |  136 TEST_F(CaptureSchedulerTest, MaximumPendingFrames) { | 
 |  137   InitScheduler(); | 
 |  138  | 
 |  139   capture_timer_->Fire(); | 
 |  140   CheckCaptureCalled(); | 
 |  141   scheduler_->OnCaptureCompleted(); | 
 |  142  | 
 |  143   capture_timer_->Fire(); | 
 |  144   CheckCaptureCalled(); | 
 |  145   scheduler_->OnCaptureCompleted(); | 
 |  146  | 
 |  147   EXPECT_FALSE(capture_timer_->IsRunning()); | 
 |  148  | 
 |  149   scheduler_->OnFrameEncoded(base::TimeDelta()); | 
 |  150   scheduler_->OnFrameSent(); | 
 |  151  | 
 |  152   EXPECT_TRUE(capture_timer_->IsRunning()); | 
 |  153 } | 
 |  154  | 
|   87 }  // namespace remoting |  155 }  // namespace remoting | 
| OLD | NEW |