OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback_helpers.h" | 6 #include "base/callback_helpers.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
11 #include "content/public/renderer/media_stream_video_sink.h" | 11 #include "content/public/renderer/media_stream_video_sink.h" |
12 #include "content/renderer/media/media_stream_video_capturer_source.h" | 12 #include "content/renderer/media/media_stream_video_capturer_source.h" |
13 #include "content/renderer/media/media_stream_video_track.h" | 13 #include "content/renderer/media/media_stream_video_track.h" |
14 #include "content/renderer/media/mock_media_constraint_factory.h" | 14 #include "content/renderer/media/mock_media_constraint_factory.h" |
15 #include "media/base/bind_to_current_loop.h" | 15 #include "media/base/bind_to_current_loop.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "third_party/WebKit/public/web/WebHeap.h" | 18 #include "third_party/WebKit/public/web/WebHeap.h" |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 class MockVideoCapturerDelegate : public VideoCapturerDelegate { | 22 class MockVideoCapturerDelegate : public VideoCapturerDelegate { |
23 public: | 23 public: |
24 explicit MockVideoCapturerDelegate(const StreamDeviceInfo& device_info) | 24 explicit MockVideoCapturerDelegate(const StreamDeviceInfo& device_info) |
25 : VideoCapturerDelegate(device_info) {} | 25 : VideoCapturerDelegate(device_info) {} |
26 | 26 |
27 MOCK_METHOD3(StartCapture, | 27 MOCK_METHOD4( |
28 void(const media::VideoCaptureParams& params, | 28 StartCapture, |
29 const VideoCaptureDeliverFrameCB& new_frame_callback, | 29 void(const media::VideoCaptureParams& params, |
30 const RunningCallback& running_callback)); | 30 const VideoCaptureDeliverFrameCB& new_frame_callback, |
| 31 scoped_refptr<base::SingleThreadTaskRunner> |
| 32 frame_callback_task_runner, |
| 33 const RunningCallback& running_callback)); |
31 MOCK_METHOD0(StopCapture, void()); | 34 MOCK_METHOD0(StopCapture, void()); |
32 | 35 |
| 36 protected: |
33 virtual ~MockVideoCapturerDelegate() {} | 37 virtual ~MockVideoCapturerDelegate() {} |
34 }; | 38 }; |
35 | 39 |
36 class MediaStreamVideoCapturerSourceTest : public testing::Test { | 40 class MediaStreamVideoCapturerSourceTest : public testing::Test { |
37 public: | 41 public: |
38 MediaStreamVideoCapturerSourceTest() | 42 MediaStreamVideoCapturerSourceTest() |
39 : child_process_(new ChildProcess()), | 43 : child_process_(new ChildProcess()), |
40 source_(NULL), | 44 source_(NULL), |
41 delegate_(NULL), | 45 delegate_(NULL), |
42 source_stopped_(false) { | 46 source_stopped_(false) { |
43 } | 47 } |
44 | 48 |
45 void TearDown() override { | 49 void TearDown() override { |
46 webkit_source_.reset(); | 50 webkit_source_.reset(); |
47 blink::WebHeap::collectAllGarbageForTesting(); | 51 blink::WebHeap::collectAllGarbageForTesting(); |
48 } | 52 } |
49 | 53 |
50 void InitWithDeviceInfo(const StreamDeviceInfo& device_info) { | 54 void InitWithDeviceInfo(const StreamDeviceInfo& device_info) { |
51 scoped_ptr<MockVideoCapturerDelegate> delegate( | 55 scoped_refptr<MockVideoCapturerDelegate> delegate( |
52 new MockVideoCapturerDelegate(device_info)); | 56 new MockVideoCapturerDelegate(device_info)); |
53 delegate_ = delegate.get(); | 57 delegate_ = delegate.get(); |
54 source_ = new MediaStreamVideoCapturerSource( | 58 source_ = new MediaStreamVideoCapturerSource( |
55 device_info, | 59 device_info, |
56 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, | 60 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, |
57 base::Unretained(this)), | 61 base::Unretained(this)), |
58 delegate.Pass()); | 62 delegate); |
59 | 63 |
60 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 64 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
61 blink::WebMediaStreamSource::TypeVideo, | 65 blink::WebMediaStreamSource::TypeVideo, |
62 base::UTF8ToUTF16("dummy_source_name"), | 66 base::UTF8ToUTF16("dummy_source_name"), |
63 false /* remote */ , true /* readonly */); | 67 false /* remote */ , true /* readonly */); |
64 webkit_source_.setExtraData(source_); | 68 webkit_source_.setExtraData(source_); |
65 webkit_source_id_ = webkit_source_.id(); | 69 webkit_source_id_ = webkit_source_.id(); |
66 } | 70 } |
67 | 71 |
68 blink::WebMediaStreamTrack StartSource() { | 72 blink::WebMediaStreamTrack StartSource() { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 107 |
104 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureAllowResolutionChange) { | 108 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureAllowResolutionChange) { |
105 StreamDeviceInfo device_info; | 109 StreamDeviceInfo device_info; |
106 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; | 110 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; |
107 InitWithDeviceInfo(device_info); | 111 InitWithDeviceInfo(device_info); |
108 | 112 |
109 EXPECT_CALL(mock_delegate(), StartCapture( | 113 EXPECT_CALL(mock_delegate(), StartCapture( |
110 testing::Field(&media::VideoCaptureParams::resolution_change_policy, | 114 testing::Field(&media::VideoCaptureParams::resolution_change_policy, |
111 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT), | 115 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT), |
112 testing::_, | 116 testing::_, |
| 117 testing::_, |
113 testing::_)).Times(1); | 118 testing::_)).Times(1); |
114 blink::WebMediaStreamTrack track = StartSource(); | 119 blink::WebMediaStreamTrack track = StartSource(); |
115 // When the track goes out of scope, the source will be stopped. | 120 // When the track goes out of scope, the source will be stopped. |
116 EXPECT_CALL(mock_delegate(), StopCapture()); | 121 EXPECT_CALL(mock_delegate(), StopCapture()); |
117 } | 122 } |
118 | 123 |
119 TEST_F(MediaStreamVideoCapturerSourceTest, | 124 TEST_F(MediaStreamVideoCapturerSourceTest, |
120 DesktopCaptureAllowResolutionChange) { | 125 DesktopCaptureAllowResolutionChange) { |
121 StreamDeviceInfo device_info; | 126 StreamDeviceInfo device_info; |
122 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; | 127 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; |
123 InitWithDeviceInfo(device_info); | 128 InitWithDeviceInfo(device_info); |
124 | 129 |
125 EXPECT_CALL(mock_delegate(), StartCapture( | 130 EXPECT_CALL(mock_delegate(), StartCapture( |
126 testing::Field(&media::VideoCaptureParams::resolution_change_policy, | 131 testing::Field(&media::VideoCaptureParams::resolution_change_policy, |
127 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT), | 132 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT), |
128 testing::_, | 133 testing::_, |
| 134 testing::_, |
129 testing::_)).Times(1); | 135 testing::_)).Times(1); |
130 blink::WebMediaStreamTrack track = StartSource(); | 136 blink::WebMediaStreamTrack track = StartSource(); |
131 // When the track goes out of scope, the source will be stopped. | 137 // When the track goes out of scope, the source will be stopped. |
132 EXPECT_CALL(mock_delegate(), StopCapture()); | 138 EXPECT_CALL(mock_delegate(), StopCapture()); |
133 } | 139 } |
134 | 140 |
135 TEST_F(MediaStreamVideoCapturerSourceTest, Ended) { | 141 TEST_F(MediaStreamVideoCapturerSourceTest, Ended) { |
136 StreamDeviceInfo device_info; | 142 StreamDeviceInfo device_info; |
137 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; | 143 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; |
138 scoped_ptr<VideoCapturerDelegate> delegate( | 144 scoped_refptr<VideoCapturerDelegate> delegate( |
139 new VideoCapturerDelegate(device_info)); | 145 new VideoCapturerDelegate(device_info)); |
140 VideoCapturerDelegate* delegate_ptr = delegate.get(); | 146 VideoCapturerDelegate* delegate_ptr = delegate.get(); |
141 source_ = new MediaStreamVideoCapturerSource( | 147 source_ = new MediaStreamVideoCapturerSource( |
142 device_info, | 148 device_info, |
143 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, | 149 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, |
144 base::Unretained(this)), | 150 base::Unretained(this)), |
145 delegate.Pass()); | 151 delegate); |
146 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 152 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
147 blink::WebMediaStreamSource::TypeVideo, | 153 blink::WebMediaStreamSource::TypeVideo, |
148 base::UTF8ToUTF16("dummy_source_name"), | 154 base::UTF8ToUTF16("dummy_source_name"), |
149 false /* remote */ , true /* readonly */); | 155 false /* remote */ , true /* readonly */); |
150 webkit_source_.setExtraData(source_); | 156 webkit_source_.setExtraData(source_); |
151 webkit_source_id_ = webkit_source_.id(); | 157 webkit_source_id_ = webkit_source_.id(); |
152 blink::WebMediaStreamTrack track = StartSource(); | 158 blink::WebMediaStreamTrack track = StartSource(); |
153 message_loop_.RunUntilIdle(); | 159 message_loop_.RunUntilIdle(); |
154 | 160 |
155 delegate_ptr->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_STARTED); | 161 delegate_ptr->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_STARTED); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 StreamDeviceInfo device_info; | 196 StreamDeviceInfo device_info; |
191 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; | 197 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; |
192 InitWithDeviceInfo(device_info); | 198 InitWithDeviceInfo(device_info); |
193 | 199 |
194 VideoCaptureDeliverFrameCB deliver_frame_cb; | 200 VideoCaptureDeliverFrameCB deliver_frame_cb; |
195 VideoCapturerDelegate::RunningCallback running_cb; | 201 VideoCapturerDelegate::RunningCallback running_cb; |
196 | 202 |
197 EXPECT_CALL(mock_delegate(), StartCapture( | 203 EXPECT_CALL(mock_delegate(), StartCapture( |
198 testing::_, | 204 testing::_, |
199 testing::_, | 205 testing::_, |
| 206 testing::_, |
200 testing::_)) | 207 testing::_)) |
201 .Times(1) | 208 .Times(1) |
202 .WillOnce(testing::DoAll(testing::SaveArg<1>(&deliver_frame_cb), | 209 .WillOnce(testing::DoAll(testing::SaveArg<1>(&deliver_frame_cb), |
203 testing::SaveArg<2>(&running_cb))); | 210 testing::SaveArg<3>(&running_cb))); |
204 EXPECT_CALL(mock_delegate(), StopCapture()); | 211 EXPECT_CALL(mock_delegate(), StopCapture()); |
205 blink::WebMediaStreamTrack track = StartSource(); | 212 blink::WebMediaStreamTrack track = StartSource(); |
206 running_cb.Run(MEDIA_DEVICE_OK); | 213 running_cb.Run(true); |
207 | 214 |
208 base::RunLoop run_loop; | 215 base::RunLoop run_loop; |
209 base::TimeTicks reference_capture_time = | 216 base::TimeTicks reference_capture_time = |
210 base::TimeTicks::FromInternalValue(60013); | 217 base::TimeTicks::FromInternalValue(60013); |
211 base::TimeTicks capture_time; | 218 base::TimeTicks capture_time; |
212 FakeMediaStreamVideoSink fake_sink( | 219 FakeMediaStreamVideoSink fake_sink( |
213 &capture_time, | 220 &capture_time, |
214 media::BindToCurrentLoop(run_loop.QuitClosure())); | 221 media::BindToCurrentLoop(run_loop.QuitClosure())); |
215 FakeMediaStreamVideoSink::AddToVideoTrack( | 222 FakeMediaStreamVideoSink::AddToVideoTrack( |
216 &fake_sink, | 223 &fake_sink, |
217 base::Bind(&FakeMediaStreamVideoSink::OnVideoFrame, | 224 base::Bind(&FakeMediaStreamVideoSink::OnVideoFrame, |
218 base::Unretained(&fake_sink)), | 225 base::Unretained(&fake_sink)), |
219 track); | 226 track); |
220 child_process_->io_message_loop()->PostTask( | 227 child_process_->io_message_loop()->PostTask( |
221 FROM_HERE, | 228 FROM_HERE, |
222 base::Bind(deliver_frame_cb, | 229 base::Bind(deliver_frame_cb, |
223 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)), | 230 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)), |
224 media::VideoCaptureFormat(), | 231 media::VideoCaptureFormat(), |
225 reference_capture_time)); | 232 reference_capture_time)); |
226 run_loop.Run(); | 233 run_loop.Run(); |
227 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); | 234 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); |
228 EXPECT_EQ(reference_capture_time, capture_time); | 235 EXPECT_EQ(reference_capture_time, capture_time); |
229 } | 236 } |
230 | 237 |
231 } // namespace content | 238 } // namespace content |
OLD | NEW |