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 | |
33 virtual ~MockVideoCapturerDelegate() {} | |
34 }; | 35 }; |
35 | 36 |
36 class MediaStreamVideoCapturerSourceTest : public testing::Test { | 37 class MediaStreamVideoCapturerSourceTest : public testing::Test { |
37 public: | 38 public: |
38 MediaStreamVideoCapturerSourceTest() | 39 MediaStreamVideoCapturerSourceTest() |
39 : child_process_(new ChildProcess()), | 40 : child_process_(new ChildProcess()), |
40 source_(NULL), | 41 source_(NULL), |
41 delegate_(NULL), | 42 delegate_(NULL), |
42 source_stopped_(false) { | 43 source_stopped_(false) { |
43 } | 44 } |
44 | 45 |
45 void TearDown() override { | 46 void TearDown() override { |
46 webkit_source_.reset(); | 47 webkit_source_.reset(); |
47 blink::WebHeap::collectAllGarbageForTesting(); | 48 blink::WebHeap::collectAllGarbageForTesting(); |
48 } | 49 } |
49 | 50 |
50 void InitWithDeviceInfo(const StreamDeviceInfo& device_info) { | 51 void InitWithDeviceInfo(const StreamDeviceInfo& device_info) { |
51 scoped_ptr<MockVideoCapturerDelegate> delegate( | 52 scoped_ptr<MockVideoCapturerDelegate> delegate( |
52 new MockVideoCapturerDelegate(device_info)); | 53 new MockVideoCapturerDelegate(device_info)); |
53 delegate_ = delegate.get(); | 54 delegate_ = delegate.get(); |
54 source_ = new MediaStreamVideoCapturerSource( | 55 source_ = new MediaStreamVideoCapturerSource( |
55 device_info, | |
56 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, | 56 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, |
57 base::Unretained(this)), | 57 base::Unretained(this)), |
58 delegate.Pass()); | 58 delegate.Pass()); |
| 59 source_->SetDeviceInfo(device_info); |
59 | 60 |
60 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 61 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
61 blink::WebMediaStreamSource::TypeVideo, | 62 blink::WebMediaStreamSource::TypeVideo, |
62 base::UTF8ToUTF16("dummy_source_name"), | 63 base::UTF8ToUTF16("dummy_source_name"), |
63 false /* remote */ , true /* readonly */); | 64 false /* remote */ , true /* readonly */); |
64 webkit_source_.setExtraData(source_); | 65 webkit_source_.setExtraData(source_); |
65 webkit_source_id_ = webkit_source_.id(); | 66 webkit_source_id_ = webkit_source_.id(); |
66 } | 67 } |
67 | 68 |
68 blink::WebMediaStreamTrack StartSource() { | 69 blink::WebMediaStreamTrack StartSource() { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 104 |
104 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureAllowResolutionChange) { | 105 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureAllowResolutionChange) { |
105 StreamDeviceInfo device_info; | 106 StreamDeviceInfo device_info; |
106 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; | 107 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; |
107 InitWithDeviceInfo(device_info); | 108 InitWithDeviceInfo(device_info); |
108 | 109 |
109 EXPECT_CALL(mock_delegate(), StartCapture( | 110 EXPECT_CALL(mock_delegate(), StartCapture( |
110 testing::Field(&media::VideoCaptureParams::resolution_change_policy, | 111 testing::Field(&media::VideoCaptureParams::resolution_change_policy, |
111 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT), | 112 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT), |
112 testing::_, | 113 testing::_, |
| 114 testing::_, |
113 testing::_)).Times(1); | 115 testing::_)).Times(1); |
114 blink::WebMediaStreamTrack track = StartSource(); | 116 blink::WebMediaStreamTrack track = StartSource(); |
115 // When the track goes out of scope, the source will be stopped. | 117 // When the track goes out of scope, the source will be stopped. |
116 EXPECT_CALL(mock_delegate(), StopCapture()); | 118 EXPECT_CALL(mock_delegate(), StopCapture()); |
117 } | 119 } |
118 | 120 |
119 TEST_F(MediaStreamVideoCapturerSourceTest, | 121 TEST_F(MediaStreamVideoCapturerSourceTest, |
120 DesktopCaptureAllowResolutionChange) { | 122 DesktopCaptureAllowResolutionChange) { |
121 StreamDeviceInfo device_info; | 123 StreamDeviceInfo device_info; |
122 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; | 124 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; |
123 InitWithDeviceInfo(device_info); | 125 InitWithDeviceInfo(device_info); |
124 | 126 |
125 EXPECT_CALL(mock_delegate(), StartCapture( | 127 EXPECT_CALL(mock_delegate(), StartCapture( |
126 testing::Field(&media::VideoCaptureParams::resolution_change_policy, | 128 testing::Field(&media::VideoCaptureParams::resolution_change_policy, |
127 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT), | 129 media::RESOLUTION_POLICY_DYNAMIC_WITHIN_LIMIT), |
128 testing::_, | 130 testing::_, |
| 131 testing::_, |
129 testing::_)).Times(1); | 132 testing::_)).Times(1); |
130 blink::WebMediaStreamTrack track = StartSource(); | 133 blink::WebMediaStreamTrack track = StartSource(); |
131 // When the track goes out of scope, the source will be stopped. | 134 // When the track goes out of scope, the source will be stopped. |
132 EXPECT_CALL(mock_delegate(), StopCapture()); | 135 EXPECT_CALL(mock_delegate(), StopCapture()); |
133 } | 136 } |
134 | 137 |
135 TEST_F(MediaStreamVideoCapturerSourceTest, Ended) { | 138 TEST_F(MediaStreamVideoCapturerSourceTest, Ended) { |
136 StreamDeviceInfo device_info; | 139 StreamDeviceInfo device_info; |
137 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; | 140 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; |
138 scoped_ptr<VideoCapturerDelegate> delegate( | 141 scoped_ptr<VideoCapturerDelegate> delegate( |
139 new VideoCapturerDelegate(device_info)); | 142 new VideoCapturerDelegate(device_info)); |
140 VideoCapturerDelegate* delegate_ptr = delegate.get(); | 143 VideoCapturerDelegate* delegate_ptr = delegate.get(); |
141 source_ = new MediaStreamVideoCapturerSource( | 144 source_ = new MediaStreamVideoCapturerSource( |
142 device_info, | |
143 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, | 145 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, |
144 base::Unretained(this)), | 146 base::Unretained(this)), |
145 delegate.Pass()); | 147 delegate.Pass()); |
| 148 source_->SetDeviceInfo(device_info); |
146 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 149 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
147 blink::WebMediaStreamSource::TypeVideo, | 150 blink::WebMediaStreamSource::TypeVideo, |
148 base::UTF8ToUTF16("dummy_source_name"), | 151 base::UTF8ToUTF16("dummy_source_name"), |
149 false /* remote */ , true /* readonly */); | 152 false /* remote */ , true /* readonly */); |
150 webkit_source_.setExtraData(source_); | 153 webkit_source_.setExtraData(source_); |
151 webkit_source_id_ = webkit_source_.id(); | 154 webkit_source_id_ = webkit_source_.id(); |
152 blink::WebMediaStreamTrack track = StartSource(); | 155 blink::WebMediaStreamTrack track = StartSource(); |
153 message_loop_.RunUntilIdle(); | 156 message_loop_.RunUntilIdle(); |
154 | 157 |
155 delegate_ptr->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_STARTED); | 158 delegate_ptr->OnStateUpdateOnRenderThread(VIDEO_CAPTURE_STATE_STARTED); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 StreamDeviceInfo device_info; | 193 StreamDeviceInfo device_info; |
191 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; | 194 device_info.device.type = MEDIA_DESKTOP_VIDEO_CAPTURE; |
192 InitWithDeviceInfo(device_info); | 195 InitWithDeviceInfo(device_info); |
193 | 196 |
194 VideoCaptureDeliverFrameCB deliver_frame_cb; | 197 VideoCaptureDeliverFrameCB deliver_frame_cb; |
195 VideoCapturerDelegate::RunningCallback running_cb; | 198 VideoCapturerDelegate::RunningCallback running_cb; |
196 | 199 |
197 EXPECT_CALL(mock_delegate(), StartCapture( | 200 EXPECT_CALL(mock_delegate(), StartCapture( |
198 testing::_, | 201 testing::_, |
199 testing::_, | 202 testing::_, |
| 203 testing::_, |
200 testing::_)) | 204 testing::_)) |
201 .Times(1) | 205 .Times(1) |
202 .WillOnce(testing::DoAll(testing::SaveArg<1>(&deliver_frame_cb), | 206 .WillOnce(testing::DoAll(testing::SaveArg<1>(&deliver_frame_cb), |
203 testing::SaveArg<2>(&running_cb))); | 207 testing::SaveArg<3>(&running_cb))); |
204 EXPECT_CALL(mock_delegate(), StopCapture()); | 208 EXPECT_CALL(mock_delegate(), StopCapture()); |
205 blink::WebMediaStreamTrack track = StartSource(); | 209 blink::WebMediaStreamTrack track = StartSource(); |
206 running_cb.Run(MEDIA_DEVICE_OK); | 210 running_cb.Run(true); |
207 | 211 |
208 base::RunLoop run_loop; | 212 base::RunLoop run_loop; |
209 base::TimeTicks reference_capture_time = | 213 base::TimeTicks reference_capture_time = |
210 base::TimeTicks::FromInternalValue(60013); | 214 base::TimeTicks::FromInternalValue(60013); |
211 base::TimeTicks capture_time; | 215 base::TimeTicks capture_time; |
212 FakeMediaStreamVideoSink fake_sink( | 216 FakeMediaStreamVideoSink fake_sink( |
213 &capture_time, | 217 &capture_time, |
214 media::BindToCurrentLoop(run_loop.QuitClosure())); | 218 media::BindToCurrentLoop(run_loop.QuitClosure())); |
215 FakeMediaStreamVideoSink::AddToVideoTrack( | 219 FakeMediaStreamVideoSink::AddToVideoTrack( |
216 &fake_sink, | 220 &fake_sink, |
217 base::Bind(&FakeMediaStreamVideoSink::OnVideoFrame, | 221 base::Bind(&FakeMediaStreamVideoSink::OnVideoFrame, |
218 base::Unretained(&fake_sink)), | 222 base::Unretained(&fake_sink)), |
219 track); | 223 track); |
220 child_process_->io_message_loop()->PostTask( | 224 child_process_->io_message_loop()->PostTask( |
221 FROM_HERE, | 225 FROM_HERE, |
222 base::Bind(deliver_frame_cb, | 226 base::Bind(deliver_frame_cb, |
223 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)), | 227 media::VideoFrame::CreateBlackFrame(gfx::Size(2, 2)), |
224 media::VideoCaptureFormat(), | 228 media::VideoCaptureFormat(), |
225 reference_capture_time)); | 229 reference_capture_time)); |
226 run_loop.Run(); | 230 run_loop.Run(); |
227 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); | 231 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); |
228 EXPECT_EQ(reference_capture_time, capture_time); | 232 EXPECT_EQ(reference_capture_time, capture_time); |
229 } | 233 } |
230 | 234 |
231 } // namespace content | 235 } // namespace content |
OLD | NEW |