| 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 "content/renderer/media/media_stream_video_capturer_source.h" | 5 #include "content/renderer/media/media_stream_video_capturer_source.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "content/child/child_process.h" | 15 #include "content/child/child_process.h" |
| 16 #include "content/public/renderer/media_stream_video_sink.h" | 16 #include "content/public/renderer/media_stream_video_sink.h" |
| 17 #include "content/renderer/media/media_stream_video_track.h" | 17 #include "content/renderer/media/media_stream_video_track.h" |
| 18 #include "content/renderer/media/mock_media_constraint_factory.h" | 18 #include "content/renderer/media/mock_constraint_factory.h" |
| 19 #include "media/base/bind_to_current_loop.h" | 19 #include "media/base/bind_to_current_loop.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/WebKit/public/web/WebHeap.h" | 22 #include "third_party/WebKit/public/web/WebHeap.h" |
| 23 | 23 |
| 24 using ::testing::_; | 24 using ::testing::_; |
| 25 using ::testing::InSequence; | 25 using ::testing::InSequence; |
| 26 using ::testing::Invoke; | 26 using ::testing::Invoke; |
| 27 using ::testing::WithArgs; | 27 using ::testing::WithArgs; |
| 28 | 28 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 83 |
| 84 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 84 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
| 85 blink::WebMediaStreamSource::TypeVideo, | 85 blink::WebMediaStreamSource::TypeVideo, |
| 86 base::UTF8ToUTF16("dummy_source_name"), | 86 base::UTF8ToUTF16("dummy_source_name"), |
| 87 false /* remote */, | 87 false /* remote */, |
| 88 true /* readonly */); | 88 true /* readonly */); |
| 89 webkit_source_.setExtraData(source_); | 89 webkit_source_.setExtraData(source_); |
| 90 webkit_source_id_ = webkit_source_.id(); | 90 webkit_source_id_ = webkit_source_.id(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 MockMediaConstraintFactory* constraint_factory() { | 93 MockConstraintFactory* constraint_factory() { return &constraint_factory_; } |
| 94 return &constraint_factory_; | |
| 95 } | |
| 96 | 94 |
| 97 blink::WebMediaStreamTrack StartSource() { | 95 blink::WebMediaStreamTrack StartSource() { |
| 98 bool enabled = true; | 96 bool enabled = true; |
| 99 // CreateVideoTrack will trigger OnConstraintsApplied. | 97 // CreateVideoTrack will trigger OnConstraintsApplied. |
| 100 return MediaStreamVideoTrack::CreateVideoTrack( | 98 return MediaStreamVideoTrack::CreateVideoTrack( |
| 101 source_, constraint_factory_.CreateWebMediaConstraints(), | 99 source_, constraint_factory_.CreateWebMediaConstraints(), |
| 102 base::Bind(&MediaStreamVideoCapturerSourceTest::OnConstraintsApplied, | 100 base::Bind(&MediaStreamVideoCapturerSourceTest::OnConstraintsApplied, |
| 103 base::Unretained(this)), | 101 base::Unretained(this)), |
| 104 enabled); | 102 enabled); |
| 105 } | 103 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 124 // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks | 122 // A ChildProcess and a MessageLoopForUI are both needed to fool the Tracks |
| 125 // and Sources below into believing they are on the right threads. | 123 // and Sources below into believing they are on the right threads. |
| 126 base::MessageLoopForUI message_loop_; | 124 base::MessageLoopForUI message_loop_; |
| 127 scoped_ptr<ChildProcess> child_process_; | 125 scoped_ptr<ChildProcess> child_process_; |
| 128 | 126 |
| 129 blink::WebMediaStreamSource webkit_source_; | 127 blink::WebMediaStreamSource webkit_source_; |
| 130 MediaStreamVideoCapturerSource* source_; // owned by |webkit_source_|. | 128 MediaStreamVideoCapturerSource* source_; // owned by |webkit_source_|. |
| 131 MockVideoCapturerSource* delegate_; // owned by |source|. | 129 MockVideoCapturerSource* delegate_; // owned by |source|. |
| 132 blink::WebString webkit_source_id_; | 130 blink::WebString webkit_source_id_; |
| 133 bool source_stopped_; | 131 bool source_stopped_; |
| 134 MockMediaConstraintFactory constraint_factory_; | 132 MockConstraintFactory constraint_factory_; |
| 135 }; | 133 }; |
| 136 | 134 |
| 137 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureFixedResolutionByDefault) { | 135 TEST_F(MediaStreamVideoCapturerSourceTest, TabCaptureFixedResolutionByDefault) { |
| 138 StreamDeviceInfo device_info; | 136 StreamDeviceInfo device_info; |
| 139 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; | 137 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; |
| 140 InitWithDeviceInfo(device_info); | 138 InitWithDeviceInfo(device_info); |
| 141 | 139 |
| 142 // No constraints are being provided to the implementation, so expect only | 140 // No constraints are being provided to the implementation, so expect only |
| 143 // default values. | 141 // default values. |
| 144 media::VideoCaptureParams expected_params; | 142 media::VideoCaptureParams expected_params; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 EXPECT_CALL(mock_delegate(), StopCapture()); | 183 EXPECT_CALL(mock_delegate(), StopCapture()); |
| 186 } | 184 } |
| 187 | 185 |
| 188 TEST_F(MediaStreamVideoCapturerSourceTest, | 186 TEST_F(MediaStreamVideoCapturerSourceTest, |
| 189 TabCaptureConstraintsImplyFixedAspectRatio) { | 187 TabCaptureConstraintsImplyFixedAspectRatio) { |
| 190 StreamDeviceInfo device_info; | 188 StreamDeviceInfo device_info; |
| 191 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; | 189 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; |
| 192 InitWithDeviceInfo(device_info); | 190 InitWithDeviceInfo(device_info); |
| 193 | 191 |
| 194 // Specify max and min size constraints that have the same ~16:9 aspect ratio. | 192 // Specify max and min size constraints that have the same ~16:9 aspect ratio. |
| 195 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMaxWidth, 1920); | 193 constraint_factory()->basic().width.setMax(1920); |
| 196 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMaxHeight, 1080); | 194 constraint_factory()->basic().height.setMax(1080); |
| 197 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMinWidth, 854); | 195 constraint_factory()->basic().width.setMin(854); |
| 198 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMinHeight, 480); | 196 constraint_factory()->basic().height.setMin(480); |
| 199 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMaxFrameRate, | 197 constraint_factory()->basic().frameRate.setMax(60.0); |
| 200 60.0); | |
| 201 | 198 |
| 202 media::VideoCaptureParams expected_params; | 199 media::VideoCaptureParams expected_params; |
| 203 expected_params.requested_format.frame_size.SetSize(1920, 1080); | 200 expected_params.requested_format.frame_size.SetSize(1920, 1080); |
| 204 expected_params.requested_format.frame_rate = 60.0; | 201 expected_params.requested_format.frame_rate = 60.0; |
| 205 expected_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420; | 202 expected_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420; |
| 206 expected_params.resolution_change_policy = | 203 expected_params.resolution_change_policy = |
| 207 media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO; | 204 media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO; |
| 208 | 205 |
| 209 InSequence s; | 206 InSequence s; |
| 210 EXPECT_CALL(mock_delegate(), GetCurrentSupportedFormats(_, _, _, _)); | 207 EXPECT_CALL(mock_delegate(), GetCurrentSupportedFormats(_, _, _, _)); |
| 211 EXPECT_CALL( | 208 EXPECT_CALL( |
| 212 mock_delegate(), | 209 mock_delegate(), |
| 213 StartCapture( | 210 StartCapture( |
| 214 testing::Field(&media::VideoCaptureParams::resolution_change_policy, | 211 testing::Field(&media::VideoCaptureParams::resolution_change_policy, |
| 215 media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO), | 212 media::RESOLUTION_POLICY_FIXED_ASPECT_RATIO), |
| 216 _, _)); | 213 _, _)); |
| 217 blink::WebMediaStreamTrack track = StartSource(); | 214 blink::WebMediaStreamTrack track = StartSource(); |
| 218 // When the track goes out of scope, the source will be stopped. | 215 // When the track goes out of scope, the source will be stopped. |
| 219 EXPECT_CALL(mock_delegate(), StopCapture()); | 216 EXPECT_CALL(mock_delegate(), StopCapture()); |
| 220 } | 217 } |
| 221 | 218 |
| 222 TEST_F(MediaStreamVideoCapturerSourceTest, | 219 TEST_F(MediaStreamVideoCapturerSourceTest, |
| 223 TabCaptureConstraintsImplyAllowingAnyResolutionChange) { | 220 TabCaptureConstraintsImplyAllowingAnyResolutionChange) { |
| 224 StreamDeviceInfo device_info; | 221 StreamDeviceInfo device_info; |
| 225 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; | 222 device_info.device.type = MEDIA_TAB_VIDEO_CAPTURE; |
| 226 InitWithDeviceInfo(device_info); | 223 InitWithDeviceInfo(device_info); |
| 227 | 224 |
| 228 // Specify max and min size constraints with different aspect ratios. | 225 // Specify max and min size constraints with different aspect ratios. |
| 229 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMaxWidth, 1920); | 226 constraint_factory()->basic().width.setMax(1920); |
| 230 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMaxHeight, 1080); | 227 constraint_factory()->basic().height.setMax(1080); |
| 231 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMinWidth, 0); | 228 constraint_factory()->basic().width.setMin(0); |
| 232 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMinHeight, 0); | 229 constraint_factory()->basic().height.setMin(0); |
| 233 constraint_factory()->AddMandatory(MediaStreamVideoSource::kMaxFrameRate, | 230 constraint_factory()->basic().frameRate.setMax(60.0); |
| 234 60.0); | |
| 235 | 231 |
| 236 media::VideoCaptureParams expected_params; | 232 media::VideoCaptureParams expected_params; |
| 237 expected_params.requested_format.frame_size.SetSize(1920, 1080); | 233 expected_params.requested_format.frame_size.SetSize(1920, 1080); |
| 238 expected_params.requested_format.frame_rate = 60.0; | 234 expected_params.requested_format.frame_rate = 60.0; |
| 239 expected_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420; | 235 expected_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420; |
| 240 expected_params.resolution_change_policy = | 236 expected_params.resolution_change_policy = |
| 241 media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT; | 237 media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT; |
| 242 | 238 |
| 243 InSequence s; | 239 InSequence s; |
| 244 EXPECT_CALL(mock_delegate(), GetCurrentSupportedFormats(_, _, _, _)); | 240 EXPECT_CALL(mock_delegate(), GetCurrentSupportedFormats(_, _, _, _)); |
| 245 EXPECT_CALL( | 241 EXPECT_CALL( |
| 246 mock_delegate(), | 242 mock_delegate(), |
| 247 StartCapture( | 243 StartCapture( |
| 248 testing::Field(&media::VideoCaptureParams::resolution_change_policy, | 244 testing::Field(&media::VideoCaptureParams::resolution_change_policy, |
| 249 media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT), | 245 media::RESOLUTION_POLICY_ANY_WITHIN_LIMIT), |
| 250 _, _)); | 246 _, _)); |
| 251 blink::WebMediaStreamTrack track = StartSource(); | 247 blink::WebMediaStreamTrack track = StartSource(); |
| 252 // When the track goes out of scope, the source will be stopped. | 248 // When the track goes out of scope, the source will be stopped. |
| 253 EXPECT_CALL(mock_delegate(), StopCapture()); | 249 EXPECT_CALL(mock_delegate(), StopCapture()); |
| 254 } | 250 } |
| 255 | 251 |
| 256 TEST_F(MediaStreamVideoCapturerSourceTest, | 252 TEST_F(MediaStreamVideoCapturerSourceTest, |
| 257 DeviceCaptureConstraintsSupportPowerLineFrequency) { | 253 DeviceCaptureConstraintsSupportPowerLineFrequency) { |
| 258 for (int frequency = -100; frequency < 100; ++frequency) { | 254 for (int frequency = -100; frequency < 100; ++frequency) { |
| 259 StreamDeviceInfo device_info; | 255 StreamDeviceInfo device_info; |
| 260 device_info.device.type = MEDIA_DEVICE_VIDEO_CAPTURE; | 256 device_info.device.type = MEDIA_DEVICE_VIDEO_CAPTURE; |
| 261 InitWithDeviceInfo(device_info); | 257 InitWithDeviceInfo(device_info); |
| 262 constraint_factory_ = MockMediaConstraintFactory(); | 258 constraint_factory_.Reset(); |
| 263 | 259 |
| 264 constraint_factory()->AddOptional(GetPowerLineFrequencyForTesting(), | 260 constraint_factory()->AddAdvanced().googPowerLineFrequency.setExact( |
| 265 frequency); | 261 frequency); |
| 266 | 262 |
| 267 media::VideoCaptureParams expected_params; | 263 media::VideoCaptureParams expected_params; |
| 268 expected_params.requested_format.frame_size.SetSize( | 264 expected_params.requested_format.frame_size.SetSize( |
| 269 MediaStreamVideoSource::kDefaultWidth, | 265 MediaStreamVideoSource::kDefaultWidth, |
| 270 MediaStreamVideoSource::kDefaultHeight); | 266 MediaStreamVideoSource::kDefaultHeight); |
| 271 expected_params.requested_format.frame_rate = | 267 expected_params.requested_format.frame_rate = |
| 272 MediaStreamVideoSource::kDefaultFrameRate; | 268 MediaStreamVideoSource::kDefaultFrameRate; |
| 273 expected_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420; | 269 expected_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420; |
| 274 expected_params.resolution_change_policy = | 270 expected_params.resolution_change_policy = |
| 275 media::RESOLUTION_POLICY_FIXED_RESOLUTION; | 271 media::RESOLUTION_POLICY_FIXED_RESOLUTION; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 286 | 282 |
| 287 InSequence s; | 283 InSequence s; |
| 288 EXPECT_CALL(mock_delegate(), GetCurrentSupportedFormats(_, _, _, _)); | 284 EXPECT_CALL(mock_delegate(), GetCurrentSupportedFormats(_, _, _, _)); |
| 289 EXPECT_CALL(mock_delegate(), StartCapture(expected_params, _, _)); | 285 EXPECT_CALL(mock_delegate(), StartCapture(expected_params, _, _)); |
| 290 blink::WebMediaStreamTrack track = StartSource(); | 286 blink::WebMediaStreamTrack track = StartSource(); |
| 291 // When the track goes out of scope, the source will be stopped. | 287 // When the track goes out of scope, the source will be stopped. |
| 292 EXPECT_CALL(mock_delegate(), StopCapture()); | 288 EXPECT_CALL(mock_delegate(), StopCapture()); |
| 293 } | 289 } |
| 294 } | 290 } |
| 295 | 291 |
| 296 TEST_F(MediaStreamVideoCapturerSourceTest, | |
| 297 InvalidPowerLineFrequencyHandledProperly) { | |
| 298 // Test out other varieties of invalid input, like non-numeric strings. | |
| 299 StreamDeviceInfo device_info; | |
| 300 device_info.device.type = MEDIA_DEVICE_VIDEO_CAPTURE; | |
| 301 InitWithDeviceInfo(device_info); | |
| 302 constraint_factory_ = MockMediaConstraintFactory(); | |
| 303 | |
| 304 constraint_factory()->AddOptional(GetPowerLineFrequencyForTesting(), | |
| 305 std::string("this is not a frequency")); | |
| 306 | |
| 307 media::VideoCaptureParams expected_params; | |
| 308 expected_params.requested_format.frame_size.SetSize( | |
| 309 MediaStreamVideoSource::kDefaultWidth, | |
| 310 MediaStreamVideoSource::kDefaultHeight); | |
| 311 expected_params.requested_format.frame_rate = | |
| 312 MediaStreamVideoSource::kDefaultFrameRate; | |
| 313 expected_params.requested_format.pixel_format = media::PIXEL_FORMAT_I420; | |
| 314 expected_params.resolution_change_policy = | |
| 315 media::RESOLUTION_POLICY_FIXED_RESOLUTION; | |
| 316 // Invalid frequencies should result in default setting. | |
| 317 expected_params.power_line_frequency = | |
| 318 media::PowerLineFrequency::FREQUENCY_DEFAULT; | |
| 319 | |
| 320 InSequence s; | |
| 321 EXPECT_CALL(mock_delegate(), GetCurrentSupportedFormats(_, _, _, _)); | |
| 322 EXPECT_CALL(mock_delegate(), StartCapture(expected_params, _, _)); | |
| 323 blink::WebMediaStreamTrack track = StartSource(); | |
| 324 // When the track goes out of scope, the source will be stopped. | |
| 325 EXPECT_CALL(mock_delegate(), StopCapture()); | |
| 326 } | |
| 327 | |
| 328 TEST_F(MediaStreamVideoCapturerSourceTest, Ended) { | 292 TEST_F(MediaStreamVideoCapturerSourceTest, Ended) { |
| 329 scoped_ptr<MockVideoCapturerSource> delegate(new MockVideoCapturerSource()); | 293 scoped_ptr<MockVideoCapturerSource> delegate(new MockVideoCapturerSource()); |
| 330 delegate_ = delegate.get(); | 294 delegate_ = delegate.get(); |
| 331 source_ = new MediaStreamVideoCapturerSource( | 295 source_ = new MediaStreamVideoCapturerSource( |
| 332 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, | 296 base::Bind(&MediaStreamVideoCapturerSourceTest::OnSourceStopped, |
| 333 base::Unretained(this)), | 297 base::Unretained(this)), |
| 334 std::move(delegate)); | 298 std::move(delegate)); |
| 335 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), | 299 webkit_source_.initialize(base::UTF8ToUTF16("dummy_source_id"), |
| 336 blink::WebMediaStreamSource::TypeVideo, | 300 blink::WebMediaStreamSource::TypeVideo, |
| 337 base::UTF8ToUTF16("dummy_source_name"), | 301 base::UTF8ToUTF16("dummy_source_name"), |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 run_loop.Run(); | 387 run_loop.Run(); |
| 424 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); | 388 FakeMediaStreamVideoSink::RemoveFromVideoTrack(&fake_sink, track); |
| 425 EXPECT_EQ(reference_capture_time, capture_time); | 389 EXPECT_EQ(reference_capture_time, capture_time); |
| 426 double metadata_value; | 390 double metadata_value; |
| 427 EXPECT_TRUE(metadata.GetDouble(media::VideoFrameMetadata::FRAME_RATE, | 391 EXPECT_TRUE(metadata.GetDouble(media::VideoFrameMetadata::FRAME_RATE, |
| 428 &metadata_value)); | 392 &metadata_value)); |
| 429 EXPECT_EQ(30.0, metadata_value); | 393 EXPECT_EQ(30.0, metadata_value); |
| 430 } | 394 } |
| 431 | 395 |
| 432 } // namespace content | 396 } // namespace content |
| OLD | NEW |