OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/video_scheduler.h" | 5 #include "remoting/host/video_scheduler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "remoting/base/auto_thread_task_runner.h" |
10 #include "remoting/capturer/capture_data.h" | 11 #include "remoting/capturer/capture_data.h" |
11 #include "remoting/capturer/video_capturer_mock_objects.h" | 12 #include "remoting/capturer/video_capturer_mock_objects.h" |
12 #include "remoting/codec/video_encoder.h" | 13 #include "remoting/codec/video_encoder.h" |
13 #include "remoting/proto/video.pb.h" | 14 #include "remoting/proto/video.pb.h" |
14 #include "remoting/protocol/protocol_mock_objects.h" | 15 #include "remoting/protocol/protocol_mock_objects.h" |
15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 using ::remoting::protocol::MockClientStub; | 19 using ::remoting::protocol::MockClientStub; |
19 using ::remoting::protocol::MockVideoStub; | 20 using ::remoting::protocol::MockVideoStub; |
(...skipping 17 matching lines...) Expand all Loading... |
37 ACTION(FinishEncode) { | 38 ACTION(FinishEncode) { |
38 scoped_ptr<VideoPacket> packet(new VideoPacket()); | 39 scoped_ptr<VideoPacket> packet(new VideoPacket()); |
39 packet->set_flags(VideoPacket::LAST_PACKET | VideoPacket::LAST_PARTITION); | 40 packet->set_flags(VideoPacket::LAST_PACKET | VideoPacket::LAST_PARTITION); |
40 arg2.Run(packet.Pass()); | 41 arg2.Run(packet.Pass()); |
41 } | 42 } |
42 | 43 |
43 ACTION(FinishSend) { | 44 ACTION(FinishSend) { |
44 arg1.Run(); | 45 arg1.Run(); |
45 } | 46 } |
46 | 47 |
47 ACTION_P2(StopVideoScheduler, scheduler, task) { | |
48 scheduler->get()->Stop(task); | |
49 } | |
50 | |
51 } // namespace | 48 } // namespace |
52 | 49 |
53 static const int kWidth = 640; | 50 static const int kWidth = 640; |
54 static const int kHeight = 480; | 51 static const int kHeight = 480; |
55 | 52 |
56 class MockVideoEncoder : public VideoEncoder { | 53 class MockVideoEncoder : public VideoEncoder { |
57 public: | 54 public: |
58 MockVideoEncoder(); | 55 MockVideoEncoder(); |
59 virtual ~MockVideoEncoder(); | 56 virtual ~MockVideoEncoder(); |
60 | 57 |
61 MOCK_METHOD3(Encode, void( | 58 MOCK_METHOD3(Encode, void( |
62 scoped_refptr<CaptureData> capture_data, | 59 scoped_refptr<CaptureData> capture_data, |
63 bool key_frame, | 60 bool key_frame, |
64 const DataAvailableCallback& data_available_callback)); | 61 const DataAvailableCallback& data_available_callback)); |
65 | 62 |
66 private: | 63 private: |
67 DISALLOW_COPY_AND_ASSIGN(MockVideoEncoder); | 64 DISALLOW_COPY_AND_ASSIGN(MockVideoEncoder); |
68 }; | 65 }; |
69 | 66 |
70 MockVideoEncoder::MockVideoEncoder() {} | 67 MockVideoEncoder::MockVideoEncoder() {} |
71 | 68 |
72 MockVideoEncoder::~MockVideoEncoder() {} | 69 MockVideoEncoder::~MockVideoEncoder() {} |
73 | 70 |
74 class VideoSchedulerTest : public testing::Test { | 71 class VideoSchedulerTest : public testing::Test { |
75 public: | 72 public: |
76 VideoSchedulerTest() : size_(SkISize::Make(0, 0)) { | 73 VideoSchedulerTest() : size_(SkISize::Make(0, 0)) { |
77 } | 74 } |
78 | 75 |
79 virtual void SetUp() OVERRIDE { | 76 virtual void SetUp() OVERRIDE { |
| 77 task_runner_ = new AutoThreadTaskRunner( |
| 78 message_loop_.message_loop_proxy(), |
| 79 base::Bind(&VideoSchedulerTest::QuitMainMessageLoop, |
| 80 base::Unretained(this))); |
| 81 |
80 encoder_ = new MockVideoEncoder(); | 82 encoder_ = new MockVideoEncoder(); |
81 } | 83 } |
82 | 84 |
83 void StartVideoScheduler() { | 85 void StartVideoScheduler(scoped_ptr<VideoFrameCapturer> capturer) { |
84 scheduler_ = VideoScheduler::Create( | 86 scheduler_ = VideoScheduler::Create( |
85 message_loop_.message_loop_proxy(), | 87 task_runner_, // Capture |
86 message_loop_.message_loop_proxy(), | 88 task_runner_, // Encode |
87 message_loop_.message_loop_proxy(), | 89 task_runner_, // Network |
88 &capturer_, | 90 capturer.Pass(), |
89 scoped_ptr<VideoEncoder>(encoder_), | 91 scoped_ptr<VideoEncoder>(encoder_), |
90 &client_stub_, | 92 &client_stub_, |
91 &video_stub_); | 93 &video_stub_); |
92 } | 94 } |
93 | 95 |
94 void GenerateOnCaptureCompleted(); | 96 void GenerateOnCaptureCompleted(); |
95 | 97 |
| 98 void QuitMainMessageLoop(); |
| 99 |
| 100 void StopVideoScheduler(); |
| 101 |
96 protected: | 102 protected: |
97 MessageLoop message_loop_; | 103 MessageLoop message_loop_; |
| 104 base::RunLoop run_loop_; |
| 105 scoped_refptr<AutoThreadTaskRunner> task_runner_; |
98 scoped_refptr<VideoScheduler> scheduler_; | 106 scoped_refptr<VideoScheduler> scheduler_; |
99 | 107 |
100 MockClientStub client_stub_; | 108 MockClientStub client_stub_; |
101 MockVideoStub video_stub_; | 109 MockVideoStub video_stub_; |
102 MockVideoFrameCapturer capturer_; | |
103 | 110 |
104 // The following mock objects are owned by VideoScheduler. | 111 // The following mock objects are owned by VideoScheduler. |
105 MockVideoEncoder* encoder_; | 112 MockVideoEncoder* encoder_; |
106 | 113 |
107 SkISize size_; | 114 SkISize size_; |
108 scoped_refptr<CaptureData> data_; | 115 scoped_refptr<CaptureData> data_; |
109 | 116 |
110 private: | 117 private: |
111 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); | 118 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); |
112 }; | 119 }; |
113 | 120 |
114 void VideoSchedulerTest::GenerateOnCaptureCompleted() { | 121 void VideoSchedulerTest::GenerateOnCaptureCompleted() { |
115 SkRegion update_region(SkIRect::MakeXYWH(0, 0, 10, 10)); | 122 SkRegion update_region(SkIRect::MakeXYWH(0, 0, 10, 10)); |
116 data_->mutable_dirty_region().op(update_region, SkRegion::kUnion_Op); | 123 data_->mutable_dirty_region().op(update_region, SkRegion::kUnion_Op); |
117 | 124 |
118 scheduler_->OnCaptureCompleted(data_); | 125 scheduler_->OnCaptureCompleted(data_); |
119 } | 126 } |
120 | 127 |
| 128 void VideoSchedulerTest::QuitMainMessageLoop() { |
| 129 message_loop_.PostTask(FROM_HERE, run_loop_.QuitClosure()); |
| 130 } |
| 131 |
| 132 void VideoSchedulerTest::StopVideoScheduler() { |
| 133 scheduler_->Stop(); |
| 134 scheduler_ = NULL; |
| 135 } |
| 136 |
121 // This test mocks capturer, encoder and network layer to simulate one capture | 137 // This test mocks capturer, encoder and network layer to simulate one capture |
122 // cycle. When the first encoded packet is submitted to the network | 138 // cycle. When the first encoded packet is submitted to the network |
123 // VideoScheduler is instructed to come to a complete stop. We expect the stop | 139 // VideoScheduler is instructed to come to a complete stop. We expect the stop |
124 // sequence to be executed successfully. | 140 // sequence to be executed successfully. |
125 TEST_F(VideoSchedulerTest, StartAndStop) { | 141 TEST_F(VideoSchedulerTest, StartAndStop) { |
126 Expectation capturer_start = EXPECT_CALL(capturer_, Start(_)); | 142 scoped_ptr<MockVideoFrameCapturer> capturer_(new MockVideoFrameCapturer()); |
| 143 Expectation capturer_start = EXPECT_CALL(*capturer_, Start(_)); |
127 | 144 |
128 size_.set(kWidth, kHeight); | 145 size_.set(kWidth, kHeight); |
129 data_ = new CaptureData(NULL, kWidth * CaptureData::kBytesPerPixel, size_); | 146 data_ = new CaptureData(NULL, kWidth * CaptureData::kBytesPerPixel, size_); |
130 | 147 |
131 // Create a RunLoop through which to drive |message_loop_|. | 148 EXPECT_CALL(*capturer_, size_most_recent()) |
132 base::RunLoop run_loop; | |
133 | |
134 EXPECT_CALL(capturer_, size_most_recent()) | |
135 .WillRepeatedly(ReturnRef(size_)); | 149 .WillRepeatedly(ReturnRef(size_)); |
136 | 150 |
137 // First the capturer is called. | 151 // First the capturer is called. |
138 Expectation capturer_capture = EXPECT_CALL(capturer_, CaptureFrame()) | 152 Expectation capturer_capture = EXPECT_CALL(*capturer_, CaptureFrame()) |
139 .After(capturer_start) | 153 .After(capturer_start) |
140 .WillRepeatedly(InvokeWithoutArgs( | 154 .WillRepeatedly(InvokeWithoutArgs( |
141 this, &VideoSchedulerTest::GenerateOnCaptureCompleted)); | 155 this, &VideoSchedulerTest::GenerateOnCaptureCompleted)); |
142 | 156 |
143 // Expect the encoder be called. | 157 // Expect the encoder be called. |
144 EXPECT_CALL(*encoder_, Encode(data_, false, _)) | 158 EXPECT_CALL(*encoder_, Encode(data_, false, _)) |
145 .WillRepeatedly(FinishEncode()); | 159 .WillRepeatedly(FinishEncode()); |
146 | 160 |
147 // By default delete the arguments when ProcessVideoPacket is received. | 161 // By default delete the arguments when ProcessVideoPacket is received. |
148 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 162 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
149 .WillRepeatedly(FinishSend()); | 163 .WillRepeatedly(FinishSend()); |
150 | 164 |
151 // For the first time when ProcessVideoPacket is received we stop the | 165 // For the first time when ProcessVideoPacket is received we stop the |
152 // VideoScheduler. | 166 // VideoScheduler. |
153 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 167 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
154 .WillOnce(DoAll( | 168 .WillOnce(DoAll( |
155 FinishSend(), | 169 FinishSend(), |
156 StopVideoScheduler(&scheduler_, run_loop.QuitClosure()))) | 170 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) |
157 .RetiresOnSaturation(); | 171 .RetiresOnSaturation(); |
158 | 172 |
159 EXPECT_CALL(capturer_, Stop()) | 173 EXPECT_CALL(*capturer_, Stop()) |
160 .After(capturer_capture); | 174 .After(capturer_capture); |
161 | 175 |
162 // Start video frame capture. | 176 // Start video frame capture. |
163 StartVideoScheduler(); | 177 StartVideoScheduler(capturer_.PassAs<VideoFrameCapturer>()); |
164 run_loop.Run(); | 178 |
| 179 task_runner_ = NULL; |
| 180 run_loop_.Run(); |
165 } | 181 } |
166 | 182 |
167 } // namespace remoting | 183 } // namespace remoting |
OLD | NEW |