Chromium Code Reviews| 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 <algorithm> | |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 11 #include "base/debug/stack_trace.h" | 12 #include "base/debug/stack_trace.h" |
| 12 #include "base/location.h" | 13 #include "base/location.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 15 #include "content/common/media/media_stream_messages.h" | 16 #include "content/common/media/media_stream_messages.h" |
| 16 #include "content/public/common/media_stream_request.h" | 17 #include "content/public/common/media_stream_request.h" |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 constraints, | 188 constraints, |
| 188 &blink::WebMediaTrackConstraintSet::googPowerLineFrequency, &freq)) { | 189 &blink::WebMediaTrackConstraintSet::googPowerLineFrequency, &freq)) { |
| 189 return; | 190 return; |
| 190 } | 191 } |
| 191 if (freq == static_cast<int>(media::PowerLineFrequency::FREQUENCY_50HZ)) | 192 if (freq == static_cast<int>(media::PowerLineFrequency::FREQUENCY_50HZ)) |
| 192 params->power_line_frequency = media::PowerLineFrequency::FREQUENCY_50HZ; | 193 params->power_line_frequency = media::PowerLineFrequency::FREQUENCY_50HZ; |
| 193 else if (freq == static_cast<int>(media::PowerLineFrequency::FREQUENCY_60HZ)) | 194 else if (freq == static_cast<int>(media::PowerLineFrequency::FREQUENCY_60HZ)) |
| 194 params->power_line_frequency = media::PowerLineFrequency::FREQUENCY_60HZ; | 195 params->power_line_frequency = media::PowerLineFrequency::FREQUENCY_60HZ; |
| 195 } | 196 } |
| 196 | 197 |
| 197 // LocalVideoCapturerSource is a delegate used by MediaStreamVideoCapturerSource | 198 // LocalVideoCapturerSource is a delegate used by MediaStreamVideoCapturerSource |
|
hbos_chromium
2017/04/05 12:37:28
nit: LegacyLocalVideoCapturerSource
Guido Urdaneta
2017/04/05 16:17:16
Done.
| |
| 198 // for local video capture. It uses the Render singleton VideoCaptureImplManager | 199 // for local video capture. It uses the Render singleton VideoCaptureImplManager |
| 199 // to start / stop and receive I420 frames from Chrome's video capture | 200 // to start / stop and receive I420 frames from Chrome's video capture |
| 200 // implementation. This is a main Render thread only object. | 201 // implementation. This is a main Render thread only object. |
| 201 class LocalVideoCapturerSource final : public media::VideoCapturerSource { | 202 // TODO(guidou): Remove this class. http://crbug.com/706408 |
| 203 class LegacyLocalVideoCapturerSource final : public media::VideoCapturerSource { | |
| 202 public: | 204 public: |
| 203 explicit LocalVideoCapturerSource(const StreamDeviceInfo& device_info); | 205 explicit LegacyLocalVideoCapturerSource(const StreamDeviceInfo& device_info); |
| 204 ~LocalVideoCapturerSource() override; | 206 ~LegacyLocalVideoCapturerSource() override; |
| 205 | 207 |
| 206 // VideoCaptureDelegate Implementation. | 208 // VideoCaptureDelegate Implementation. |
| 207 void GetCurrentSupportedFormats( | 209 void GetCurrentSupportedFormats( |
| 208 int max_requested_width, | 210 int max_requested_width, |
| 209 int max_requested_height, | 211 int max_requested_height, |
| 210 double max_requested_frame_rate, | 212 double max_requested_frame_rate, |
| 211 const VideoCaptureDeviceFormatsCB& callback) override; | 213 const VideoCaptureDeviceFormatsCB& callback) override; |
| 212 media::VideoCaptureFormats GetPreferredFormats() override; | 214 media::VideoCaptureFormats GetPreferredFormats() override; |
| 213 void StartCapture(const media::VideoCaptureParams& params, | 215 void StartCapture(const media::VideoCaptureParams& params, |
| 214 const VideoCaptureDeliverFrameCB& new_frame_callback, | 216 const VideoCaptureDeliverFrameCB& new_frame_callback, |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 240 RunningCallback running_callback_; | 242 RunningCallback running_callback_; |
| 241 base::Closure stop_capture_cb_; | 243 base::Closure stop_capture_cb_; |
| 242 | 244 |
| 243 // Placeholder keeping the callback between asynchronous device enumeration | 245 // Placeholder keeping the callback between asynchronous device enumeration |
| 244 // calls. | 246 // calls. |
| 245 VideoCaptureDeviceFormatsCB formats_enumerated_callback_; | 247 VideoCaptureDeviceFormatsCB formats_enumerated_callback_; |
| 246 | 248 |
| 247 // Bound to the main render thread. | 249 // Bound to the main render thread. |
| 248 base::ThreadChecker thread_checker_; | 250 base::ThreadChecker thread_checker_; |
| 249 | 251 |
| 250 base::WeakPtrFactory<LocalVideoCapturerSource> weak_factory_; | 252 base::WeakPtrFactory<LegacyLocalVideoCapturerSource> weak_factory_; |
| 251 | 253 |
| 252 DISALLOW_COPY_AND_ASSIGN(LocalVideoCapturerSource); | 254 DISALLOW_COPY_AND_ASSIGN(LegacyLocalVideoCapturerSource); |
| 253 }; | 255 }; |
| 254 | 256 |
| 255 } // namespace | 257 LegacyLocalVideoCapturerSource::LegacyLocalVideoCapturerSource( |
| 256 | |
| 257 LocalVideoCapturerSource::LocalVideoCapturerSource( | |
| 258 const StreamDeviceInfo& device_info) | 258 const StreamDeviceInfo& device_info) |
| 259 : session_id_(device_info.session_id), | 259 : session_id_(device_info.session_id), |
| 260 manager_(RenderThreadImpl::current()->video_capture_impl_manager()), | 260 manager_(RenderThreadImpl::current()->video_capture_impl_manager()), |
| 261 release_device_cb_(manager_->UseDevice(session_id_)), | 261 release_device_cb_(manager_->UseDevice(session_id_)), |
| 262 is_content_capture_(IsContentVideoCaptureDevice(device_info)), | 262 is_content_capture_(IsContentVideoCaptureDevice(device_info)), |
| 263 weak_factory_(this) { | 263 weak_factory_(this) { |
| 264 DCHECK(RenderThreadImpl::current()); | 264 DCHECK(RenderThreadImpl::current()); |
| 265 } | 265 } |
| 266 | 266 |
| 267 LocalVideoCapturerSource::~LocalVideoCapturerSource() { | 267 LegacyLocalVideoCapturerSource::~LegacyLocalVideoCapturerSource() { |
| 268 DCHECK(thread_checker_.CalledOnValidThread()); | 268 DCHECK(thread_checker_.CalledOnValidThread()); |
| 269 release_device_cb_.Run(); | 269 release_device_cb_.Run(); |
| 270 } | 270 } |
| 271 | 271 |
| 272 void LocalVideoCapturerSource::GetCurrentSupportedFormats( | 272 void LegacyLocalVideoCapturerSource::GetCurrentSupportedFormats( |
| 273 int max_requested_width, | 273 int max_requested_width, |
| 274 int max_requested_height, | 274 int max_requested_height, |
| 275 double max_requested_frame_rate, | 275 double max_requested_frame_rate, |
| 276 const VideoCaptureDeviceFormatsCB& callback) { | 276 const VideoCaptureDeviceFormatsCB& callback) { |
| 277 DVLOG(3) << "GetCurrentSupportedFormats({ max_requested_height = " | 277 DVLOG(3) << "GetCurrentSupportedFormats({ max_requested_height = " |
| 278 << max_requested_height << "}) { max_requested_width = " | 278 << max_requested_height << "}) { max_requested_width = " |
| 279 << max_requested_width << "}) { max_requested_frame_rate = " | 279 << max_requested_width << "}) { max_requested_frame_rate = " |
| 280 << max_requested_frame_rate << "})"; | 280 << max_requested_frame_rate << "})"; |
| 281 DCHECK(thread_checker_.CalledOnValidThread()); | 281 DCHECK(thread_checker_.CalledOnValidThread()); |
| 282 | 282 |
| 283 if (is_content_capture_) { | 283 if (is_content_capture_) { |
| 284 const int width = max_requested_width ? | 284 const int width = max_requested_width ? |
| 285 max_requested_width : MediaStreamVideoSource::kDefaultWidth; | 285 max_requested_width : MediaStreamVideoSource::kDefaultWidth; |
| 286 const int height = max_requested_height ? | 286 const int height = max_requested_height ? |
| 287 max_requested_height : MediaStreamVideoSource::kDefaultHeight; | 287 max_requested_height : MediaStreamVideoSource::kDefaultHeight; |
| 288 callback.Run(media::VideoCaptureFormats( | 288 callback.Run(media::VideoCaptureFormats( |
| 289 1, media::VideoCaptureFormat( | 289 1, media::VideoCaptureFormat( |
| 290 gfx::Size(width, height), | 290 gfx::Size(width, height), |
| 291 static_cast<float>( | 291 static_cast<float>( |
| 292 std::min(kMaxScreenCastFrameRate, max_requested_frame_rate)), | 292 std::min(kMaxScreenCastFrameRate, max_requested_frame_rate)), |
| 293 media::PIXEL_FORMAT_I420))); | 293 media::PIXEL_FORMAT_I420))); |
| 294 return; | 294 return; |
| 295 } | 295 } |
| 296 | 296 |
| 297 DCHECK(formats_enumerated_callback_.is_null()); | 297 DCHECK(formats_enumerated_callback_.is_null()); |
| 298 formats_enumerated_callback_ = callback; | 298 formats_enumerated_callback_ = callback; |
| 299 manager_->GetDeviceFormatsInUse( | 299 manager_->GetDeviceFormatsInUse( |
| 300 session_id_, media::BindToCurrentLoop(base::Bind( | 300 session_id_, |
| 301 &LocalVideoCapturerSource::OnDeviceFormatsInUseReceived, | 301 media::BindToCurrentLoop(base::Bind( |
| 302 weak_factory_.GetWeakPtr()))); | 302 &LegacyLocalVideoCapturerSource::OnDeviceFormatsInUseReceived, |
| 303 weak_factory_.GetWeakPtr()))); | |
| 303 } | 304 } |
| 304 | 305 |
| 305 media::VideoCaptureFormats LocalVideoCapturerSource::GetPreferredFormats() { | 306 media::VideoCaptureFormats |
| 307 LegacyLocalVideoCapturerSource::GetPreferredFormats() { | |
| 306 return media::VideoCaptureFormats(); | 308 return media::VideoCaptureFormats(); |
| 307 } | 309 } |
| 308 | 310 |
| 309 void LocalVideoCapturerSource::StartCapture( | 311 void LegacyLocalVideoCapturerSource::StartCapture( |
| 310 const media::VideoCaptureParams& params, | 312 const media::VideoCaptureParams& params, |
| 311 const VideoCaptureDeliverFrameCB& new_frame_callback, | 313 const VideoCaptureDeliverFrameCB& new_frame_callback, |
| 312 const RunningCallback& running_callback) { | 314 const RunningCallback& running_callback) { |
| 313 DCHECK(params.requested_format.IsValid()); | 315 DCHECK(params.requested_format.IsValid()); |
| 314 DCHECK(thread_checker_.CalledOnValidThread()); | 316 DCHECK(thread_checker_.CalledOnValidThread()); |
| 315 running_callback_ = running_callback; | 317 running_callback_ = running_callback; |
| 316 | 318 |
| 317 stop_capture_cb_ = manager_->StartCapture( | 319 stop_capture_cb_ = |
| 318 session_id_, params, media::BindToCurrentLoop(base::Bind( | 320 manager_->StartCapture(session_id_, params, |
| 319 &LocalVideoCapturerSource::OnStateUpdate, | 321 media::BindToCurrentLoop(base::Bind( |
| 320 weak_factory_.GetWeakPtr())), | 322 &LegacyLocalVideoCapturerSource::OnStateUpdate, |
| 321 new_frame_callback); | 323 weak_factory_.GetWeakPtr())), |
| 324 new_frame_callback); | |
| 322 } | 325 } |
| 323 | 326 |
| 324 void LocalVideoCapturerSource::RequestRefreshFrame() { | 327 void LegacyLocalVideoCapturerSource::RequestRefreshFrame() { |
| 325 DVLOG(3) << __func__; | 328 DVLOG(3) << __func__; |
| 326 DCHECK(thread_checker_.CalledOnValidThread()); | 329 DCHECK(thread_checker_.CalledOnValidThread()); |
| 327 if (stop_capture_cb_.is_null()) | 330 if (stop_capture_cb_.is_null()) |
| 328 return; // Do not request frames if the source is stopped. | 331 return; // Do not request frames if the source is stopped. |
| 329 manager_->RequestRefreshFrame(session_id_); | 332 manager_->RequestRefreshFrame(session_id_); |
| 330 } | 333 } |
| 331 | 334 |
| 332 void LocalVideoCapturerSource::MaybeSuspend() { | 335 void LegacyLocalVideoCapturerSource::MaybeSuspend() { |
| 333 DVLOG(3) << __func__; | 336 DVLOG(3) << __func__; |
| 334 DCHECK(thread_checker_.CalledOnValidThread()); | 337 DCHECK(thread_checker_.CalledOnValidThread()); |
| 335 manager_->Suspend(session_id_); | 338 manager_->Suspend(session_id_); |
| 336 } | 339 } |
| 337 | 340 |
| 338 void LocalVideoCapturerSource::Resume() { | 341 void LegacyLocalVideoCapturerSource::Resume() { |
| 339 DVLOG(3) << __func__; | 342 DVLOG(3) << __func__; |
| 340 DCHECK(thread_checker_.CalledOnValidThread()); | 343 DCHECK(thread_checker_.CalledOnValidThread()); |
| 341 manager_->Resume(session_id_); | 344 manager_->Resume(session_id_); |
| 342 } | 345 } |
| 343 | 346 |
| 344 void LocalVideoCapturerSource::StopCapture() { | 347 void LegacyLocalVideoCapturerSource::StopCapture() { |
| 345 DVLOG(3) << __func__; | 348 DVLOG(3) << __func__; |
| 346 DCHECK(thread_checker_.CalledOnValidThread()); | 349 DCHECK(thread_checker_.CalledOnValidThread()); |
| 347 // Immediately make sure we don't provide more frames. | 350 // Immediately make sure we don't provide more frames. |
| 348 if (!stop_capture_cb_.is_null()) | 351 if (!stop_capture_cb_.is_null()) |
| 349 base::ResetAndReturn(&stop_capture_cb_).Run(); | 352 base::ResetAndReturn(&stop_capture_cb_).Run(); |
| 350 running_callback_.Reset(); | 353 running_callback_.Reset(); |
| 351 // Invalidate any potential format enumerations going on. | 354 // Invalidate any potential format enumerations going on. |
| 352 formats_enumerated_callback_.Reset(); | 355 formats_enumerated_callback_.Reset(); |
| 353 } | 356 } |
| 354 | 357 |
| 355 void LocalVideoCapturerSource::OnStateUpdate(VideoCaptureState state) { | 358 void LegacyLocalVideoCapturerSource::OnStateUpdate(VideoCaptureState state) { |
| 356 DVLOG(3) << __func__ << " state = " << state; | 359 DVLOG(3) << __func__ << " state = " << state; |
| 357 DCHECK(thread_checker_.CalledOnValidThread()); | 360 DCHECK(thread_checker_.CalledOnValidThread()); |
| 358 if (running_callback_.is_null()) | 361 if (running_callback_.is_null()) |
| 359 return; | 362 return; |
| 360 switch (state) { | 363 switch (state) { |
| 361 case VIDEO_CAPTURE_STATE_STARTED: | 364 case VIDEO_CAPTURE_STATE_STARTED: |
| 362 running_callback_.Run(true); | 365 running_callback_.Run(true); |
| 363 break; | 366 break; |
| 364 | 367 |
| 365 case VIDEO_CAPTURE_STATE_STOPPING: | 368 case VIDEO_CAPTURE_STATE_STOPPING: |
| 366 case VIDEO_CAPTURE_STATE_STOPPED: | 369 case VIDEO_CAPTURE_STATE_STOPPED: |
| 367 case VIDEO_CAPTURE_STATE_ERROR: | 370 case VIDEO_CAPTURE_STATE_ERROR: |
| 368 case VIDEO_CAPTURE_STATE_ENDED: | 371 case VIDEO_CAPTURE_STATE_ENDED: |
| 369 base::ResetAndReturn(&running_callback_).Run(false); | 372 base::ResetAndReturn(&running_callback_).Run(false); |
| 370 break; | 373 break; |
| 371 | 374 |
| 372 case VIDEO_CAPTURE_STATE_STARTING: | 375 case VIDEO_CAPTURE_STATE_STARTING: |
| 373 case VIDEO_CAPTURE_STATE_PAUSED: | 376 case VIDEO_CAPTURE_STATE_PAUSED: |
| 374 case VIDEO_CAPTURE_STATE_RESUMED: | 377 case VIDEO_CAPTURE_STATE_RESUMED: |
| 375 // Not applicable to reporting on device starts or errors. | 378 // Not applicable to reporting on device starts or errors. |
| 376 break; | 379 break; |
| 377 } | 380 } |
| 378 } | 381 } |
| 379 | 382 |
| 380 void LocalVideoCapturerSource::OnDeviceFormatsInUseReceived( | 383 void LegacyLocalVideoCapturerSource::OnDeviceFormatsInUseReceived( |
| 381 const media::VideoCaptureFormats& formats_in_use) { | 384 const media::VideoCaptureFormats& formats_in_use) { |
| 382 DVLOG(3) << __func__ << ", #formats received: " << formats_in_use.size(); | 385 DVLOG(3) << __func__ << ", #formats received: " << formats_in_use.size(); |
| 383 DCHECK(thread_checker_.CalledOnValidThread()); | 386 DCHECK(thread_checker_.CalledOnValidThread()); |
| 384 // StopCapture() might have destroyed |formats_enumerated_callback_| before | 387 // StopCapture() might have destroyed |formats_enumerated_callback_| before |
| 385 // arriving here. | 388 // arriving here. |
| 386 if (formats_enumerated_callback_.is_null()) | 389 if (formats_enumerated_callback_.is_null()) |
| 387 return; | 390 return; |
| 388 if (formats_in_use.size()) { | 391 if (formats_in_use.size()) { |
| 389 base::ResetAndReturn(&formats_enumerated_callback_).Run(formats_in_use); | 392 base::ResetAndReturn(&formats_enumerated_callback_).Run(formats_in_use); |
| 390 return; | 393 return; |
| 391 } | 394 } |
| 392 | 395 |
| 393 // The device doesn't seem to have formats in use so try and retrieve the | 396 // The device doesn't seem to have formats in use so try and retrieve the |
| 394 // whole list of supported ones. | 397 // whole list of supported ones. |
| 395 manager_->GetDeviceSupportedFormats( | 398 manager_->GetDeviceSupportedFormats( |
| 396 session_id_, | 399 session_id_, |
| 397 media::BindToCurrentLoop( | 400 media::BindToCurrentLoop(base::Bind( |
| 398 base::Bind( | 401 &LegacyLocalVideoCapturerSource::OnDeviceSupportedFormatsEnumerated, |
| 399 &LocalVideoCapturerSource::OnDeviceSupportedFormatsEnumerated, | 402 weak_factory_.GetWeakPtr()))); |
| 400 weak_factory_.GetWeakPtr()))); | |
| 401 } | 403 } |
| 402 | 404 |
| 403 void LocalVideoCapturerSource::OnDeviceSupportedFormatsEnumerated( | 405 void LegacyLocalVideoCapturerSource::OnDeviceSupportedFormatsEnumerated( |
| 404 const media::VideoCaptureFormats& formats) { | 406 const media::VideoCaptureFormats& formats) { |
| 405 DVLOG(3) << __func__ << ", #formats received: " << formats.size(); | 407 DVLOG(3) << __func__ << ", #formats received: " << formats.size(); |
| 406 DCHECK(thread_checker_.CalledOnValidThread()); | 408 DCHECK(thread_checker_.CalledOnValidThread()); |
| 407 // StopCapture() might have destroyed |formats_enumerated_callback_| before | 409 // StopCapture() might have destroyed |formats_enumerated_callback_| before |
| 408 // arriving here. | 410 // arriving here. |
| 409 if (formats_enumerated_callback_.is_null()) | 411 if (formats_enumerated_callback_.is_null()) |
| 410 return; | 412 return; |
| 411 if (formats.size()) { | 413 if (formats.size()) { |
| 412 base::ResetAndReturn(&formats_enumerated_callback_).Run(formats); | 414 base::ResetAndReturn(&formats_enumerated_callback_).Run(formats); |
| 413 return; | 415 return; |
| 414 } | 416 } |
| 415 | 417 |
| 416 // The capture device doesn't seem to support capability enumeration, compose | 418 // The capture device doesn't seem to support capability enumeration, compose |
| 417 // a fallback list of capabilities. | 419 // a fallback list of capabilities. |
| 418 media::VideoCaptureFormats default_formats; | 420 media::VideoCaptureFormats default_formats; |
| 419 for (const auto& resolution : kVideoResolutions) { | 421 for (const auto& resolution : kVideoResolutions) { |
| 420 for (const auto frame_rate : kVideoFrameRates) { | 422 for (const auto frame_rate : kVideoFrameRates) { |
| 421 default_formats.push_back(media::VideoCaptureFormat( | 423 default_formats.push_back(media::VideoCaptureFormat( |
| 422 gfx::Size(resolution.width, resolution.height), frame_rate, | 424 gfx::Size(resolution.width, resolution.height), frame_rate, |
| 423 media::PIXEL_FORMAT_I420)); | 425 media::PIXEL_FORMAT_I420)); |
| 424 } | 426 } |
| 425 } | 427 } |
| 426 base::ResetAndReturn(&formats_enumerated_callback_).Run(default_formats); | 428 base::ResetAndReturn(&formats_enumerated_callback_).Run(default_formats); |
| 427 } | 429 } |
| 428 | 430 |
| 431 // LocalVideoCapturerSource is a delegate used by MediaStreamVideoCapturerSource | |
| 432 // for local video capture. It uses the Render singleton VideoCaptureImplManager | |
| 433 // to start / stop and receive I420 frames from Chrome's video capture | |
| 434 // implementation. This is a main Render thread only object. | |
| 435 class LocalVideoCapturerSource final : public media::VideoCapturerSource { | |
| 436 public: | |
| 437 explicit LocalVideoCapturerSource(const StreamDeviceInfo& device_info); | |
| 438 ~LocalVideoCapturerSource() override; | |
| 439 | |
| 440 // VideoCaptureDelegate Implementation. | |
|
hbos_chromium
2017/04/05 12:37:28
What happened to GetCurrentSupportedFormats?
Guido Urdaneta
2017/04/05 16:17:16
Using the default implementation from the base cla
hbos_chromium
2017/04/05 16:56:31
Acknowledged.
| |
| 441 media::VideoCaptureFormats GetPreferredFormats() override; | |
| 442 void StartCapture(const media::VideoCaptureParams& params, | |
| 443 const VideoCaptureDeliverFrameCB& new_frame_callback, | |
| 444 const RunningCallback& running_callback) override; | |
| 445 void RequestRefreshFrame() override; | |
| 446 void MaybeSuspend() override; | |
| 447 void Resume() override; | |
| 448 void StopCapture() override; | |
| 449 | |
| 450 private: | |
| 451 void OnStateUpdate(VideoCaptureState state); | |
| 452 | |
| 453 // |session_id_| identifies the capture device used for this capture session. | |
| 454 const media::VideoCaptureSessionId session_id_; | |
| 455 | |
| 456 VideoCaptureImplManager* const manager_; | |
| 457 | |
| 458 const base::Closure release_device_cb_; | |
| 459 | |
| 460 // These two are valid between StartCapture() and StopCapture(). | |
| 461 // |running_call_back_| is run when capture is successfully started, and when | |
| 462 // it is stopped or error happens. | |
| 463 RunningCallback running_callback_; | |
| 464 base::Closure stop_capture_cb_; | |
| 465 | |
| 466 // Bound to the main render thread. | |
| 467 base::ThreadChecker thread_checker_; | |
| 468 | |
| 469 base::WeakPtrFactory<LocalVideoCapturerSource> weak_factory_; | |
| 470 | |
| 471 DISALLOW_COPY_AND_ASSIGN(LocalVideoCapturerSource); | |
| 472 }; | |
| 473 | |
| 474 LocalVideoCapturerSource::LocalVideoCapturerSource( | |
| 475 const StreamDeviceInfo& device_info) | |
| 476 : session_id_(device_info.session_id), | |
| 477 manager_(RenderThreadImpl::current()->video_capture_impl_manager()), | |
| 478 release_device_cb_(manager_->UseDevice(session_id_)), | |
| 479 weak_factory_(this) { | |
| 480 DCHECK(RenderThreadImpl::current()); | |
| 481 } | |
| 482 | |
| 483 LocalVideoCapturerSource::~LocalVideoCapturerSource() { | |
| 484 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 485 release_device_cb_.Run(); | |
| 486 } | |
| 487 | |
| 488 media::VideoCaptureFormats LocalVideoCapturerSource::GetPreferredFormats() { | |
| 489 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 490 return media::VideoCaptureFormats(); | |
|
hbos_chromium
2017/04/05 12:37:28
No preferred formats? What does that imply? (How a
Guido Urdaneta
2017/04/05 16:17:16
In this case it doesn't imply anything because the
hbos_chromium
2017/04/05 16:56:31
Acknowledged.
| |
| 491 } | |
| 492 | |
| 493 void LocalVideoCapturerSource::StartCapture( | |
| 494 const media::VideoCaptureParams& params, | |
| 495 const VideoCaptureDeliverFrameCB& new_frame_callback, | |
| 496 const RunningCallback& running_callback) { | |
| 497 DCHECK(params.requested_format.IsValid()); | |
| 498 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 499 running_callback_ = running_callback; | |
| 500 | |
| 501 stop_capture_cb_ = | |
|
hbos_chromium
2017/04/05 12:37:28
What if we've already stopped? Do we DCHECK, do we
Guido Urdaneta
2017/04/05 16:17:16
There is no change here. This is the same as Legac
hbos_chromium
2017/04/05 16:56:31
Acknowledged.
| |
| 502 manager_->StartCapture(session_id_, params, | |
| 503 media::BindToCurrentLoop(base::Bind( | |
| 504 &LocalVideoCapturerSource::OnStateUpdate, | |
| 505 weak_factory_.GetWeakPtr())), | |
| 506 new_frame_callback); | |
| 507 } | |
| 508 | |
| 509 void LocalVideoCapturerSource::RequestRefreshFrame() { | |
| 510 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 511 if (stop_capture_cb_.is_null()) | |
| 512 return; // Do not request frames if the source is stopped. | |
| 513 manager_->RequestRefreshFrame(session_id_); | |
| 514 } | |
| 515 | |
| 516 void LocalVideoCapturerSource::MaybeSuspend() { | |
| 517 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 518 manager_->Suspend(session_id_); | |
| 519 } | |
| 520 | |
| 521 void LocalVideoCapturerSource::Resume() { | |
| 522 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 523 manager_->Resume(session_id_); | |
| 524 } | |
| 525 | |
| 526 void LocalVideoCapturerSource::StopCapture() { | |
| 527 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 528 // Immediately make sure we don't provide more frames. | |
| 529 if (!stop_capture_cb_.is_null()) | |
| 530 base::ResetAndReturn(&stop_capture_cb_).Run(); | |
| 531 running_callback_.Reset(); | |
| 532 } | |
| 533 | |
| 534 void LocalVideoCapturerSource::OnStateUpdate(VideoCaptureState state) { | |
| 535 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 536 if (running_callback_.is_null()) | |
| 537 return; | |
| 538 switch (state) { | |
| 539 case VIDEO_CAPTURE_STATE_STARTED: | |
| 540 running_callback_.Run(true); | |
| 541 break; | |
| 542 | |
| 543 case VIDEO_CAPTURE_STATE_STOPPING: | |
| 544 case VIDEO_CAPTURE_STATE_STOPPED: | |
| 545 case VIDEO_CAPTURE_STATE_ERROR: | |
| 546 case VIDEO_CAPTURE_STATE_ENDED: | |
| 547 base::ResetAndReturn(&running_callback_).Run(false); | |
| 548 break; | |
| 549 | |
| 550 case VIDEO_CAPTURE_STATE_STARTING: | |
| 551 case VIDEO_CAPTURE_STATE_PAUSED: | |
| 552 case VIDEO_CAPTURE_STATE_RESUMED: | |
| 553 // Not applicable to reporting on device starts or errors. | |
| 554 break; | |
| 555 } | |
| 556 } | |
| 557 | |
| 558 } // namespace | |
| 559 | |
| 429 MediaStreamVideoCapturerSource::MediaStreamVideoCapturerSource( | 560 MediaStreamVideoCapturerSource::MediaStreamVideoCapturerSource( |
| 430 const SourceStoppedCallback& stop_callback, | 561 const SourceStoppedCallback& stop_callback, |
| 431 std::unique_ptr<media::VideoCapturerSource> source) | 562 std::unique_ptr<media::VideoCapturerSource> source) |
| 432 : RenderFrameObserver(nullptr), source_(std::move(source)) { | 563 : RenderFrameObserver(nullptr), source_(std::move(source)) { |
| 564 if (!IsOldVideoConstraints()) { | |
| 565 media::VideoCaptureFormats preferred_formats = | |
| 566 source_->GetPreferredFormats(); | |
| 567 if (!preferred_formats.empty()) | |
| 568 capture_params_.requested_format = preferred_formats.front(); | |
| 569 } | |
| 433 SetStopCallback(stop_callback); | 570 SetStopCallback(stop_callback); |
| 434 } | 571 } |
| 435 | 572 |
| 436 MediaStreamVideoCapturerSource::MediaStreamVideoCapturerSource( | 573 MediaStreamVideoCapturerSource::MediaStreamVideoCapturerSource( |
| 437 const SourceStoppedCallback& stop_callback, | 574 const SourceStoppedCallback& stop_callback, |
| 438 const StreamDeviceInfo& device_info, | 575 const StreamDeviceInfo& device_info, |
| 439 RenderFrame* render_frame) | 576 RenderFrame* render_frame) |
| 440 : RenderFrameObserver(render_frame), | 577 : RenderFrameObserver(render_frame), |
| 441 source_(new LocalVideoCapturerSource(device_info)) { | 578 source_(new LegacyLocalVideoCapturerSource(device_info)) { |
| 579 DCHECK(IsOldVideoConstraints()); | |
| 442 SetStopCallback(stop_callback); | 580 SetStopCallback(stop_callback); |
| 443 SetDeviceInfo(device_info); | 581 SetDeviceInfo(device_info); |
| 444 } | 582 } |
| 583 | |
| 584 MediaStreamVideoCapturerSource::MediaStreamVideoCapturerSource( | |
| 585 const SourceStoppedCallback& stop_callback, | |
| 586 const StreamDeviceInfo& device_info, | |
| 587 const media::VideoCaptureParams& capture_params, | |
| 588 RenderFrame* render_frame) | |
| 589 : RenderFrameObserver(render_frame), | |
| 590 source_(new LocalVideoCapturerSource(device_info)), | |
| 591 capture_params_(capture_params) { | |
| 592 DCHECK(!IsOldVideoConstraints()); | |
| 593 SetStopCallback(stop_callback); | |
| 594 SetDeviceInfo(device_info); | |
| 595 } | |
| 445 | 596 |
| 446 MediaStreamVideoCapturerSource::~MediaStreamVideoCapturerSource() { | 597 MediaStreamVideoCapturerSource::~MediaStreamVideoCapturerSource() { |
| 447 } | 598 } |
| 448 | 599 |
| 449 void MediaStreamVideoCapturerSource::RequestRefreshFrame() { | 600 void MediaStreamVideoCapturerSource::RequestRefreshFrame() { |
| 450 source_->RequestRefreshFrame(); | 601 source_->RequestRefreshFrame(); |
| 451 } | 602 } |
| 452 | 603 |
| 453 void MediaStreamVideoCapturerSource::OnHasConsumers(bool has_consumers) { | 604 void MediaStreamVideoCapturerSource::OnHasConsumers(bool has_consumers) { |
| 454 if (has_consumers) | 605 if (has_consumers) |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 471 max_requested_width, | 622 max_requested_width, |
| 472 max_requested_height, | 623 max_requested_height, |
| 473 max_requested_frame_rate, | 624 max_requested_frame_rate, |
| 474 callback); | 625 callback); |
| 475 } | 626 } |
| 476 | 627 |
| 477 void MediaStreamVideoCapturerSource::StartSourceImpl( | 628 void MediaStreamVideoCapturerSource::StartSourceImpl( |
| 478 const media::VideoCaptureFormat& format, | 629 const media::VideoCaptureFormat& format, |
| 479 const blink::WebMediaConstraints& constraints, | 630 const blink::WebMediaConstraints& constraints, |
| 480 const VideoCaptureDeliverFrameCB& frame_callback) { | 631 const VideoCaptureDeliverFrameCB& frame_callback) { |
| 481 media::VideoCaptureParams new_params; | 632 if (IsOldVideoConstraints()) { |
| 482 new_params.requested_format = format; | 633 capture_params_.requested_format = format; |
| 483 if (IsContentVideoCaptureDevice(device_info())) { | 634 if (IsContentVideoCaptureDevice(device_info())) { |
| 484 SetContentCaptureParamsFromConstraints( | 635 SetContentCaptureParamsFromConstraints( |
| 485 constraints, device_info().device.type, &new_params); | 636 constraints, device_info().device.type, &capture_params_); |
| 486 } else if (device_info().device.type == MEDIA_DEVICE_VIDEO_CAPTURE) { | 637 } else if (device_info().device.type == MEDIA_DEVICE_VIDEO_CAPTURE) { |
| 487 SetPowerLineFrequencyParamFromConstraints(constraints, &new_params); | 638 SetPowerLineFrequencyParamFromConstraints(constraints, &capture_params_); |
| 639 } | |
| 488 } | 640 } |
| 489 | 641 |
| 490 is_capture_starting_ = true; | 642 is_capture_starting_ = true; |
| 491 source_->StartCapture( | 643 source_->StartCapture( |
| 492 new_params, frame_callback, | 644 capture_params_, frame_callback, |
| 493 base::Bind(&MediaStreamVideoCapturerSource::OnRunStateChanged, | 645 base::Bind(&MediaStreamVideoCapturerSource::OnRunStateChanged, |
| 494 base::Unretained(this))); | 646 base::Unretained(this))); |
| 495 } | 647 } |
| 496 | 648 |
| 497 void MediaStreamVideoCapturerSource::StopSourceImpl() { | 649 void MediaStreamVideoCapturerSource::StopSourceImpl() { |
| 498 source_->StopCapture(); | 650 source_->StopCapture(); |
| 499 } | 651 } |
| 500 | 652 |
| 653 base::Optional<media::VideoCaptureFormat> | |
| 654 MediaStreamVideoCapturerSource::GetCurrentFormatImpl() const { | |
| 655 DCHECK(!IsOldVideoConstraints()); | |
| 656 return capture_params_.requested_format; | |
| 657 } | |
| 658 | |
| 501 void MediaStreamVideoCapturerSource::OnRunStateChanged(bool is_running) { | 659 void MediaStreamVideoCapturerSource::OnRunStateChanged(bool is_running) { |
| 502 if (is_capture_starting_) { | 660 if (is_capture_starting_) { |
| 503 OnStartDone(is_running ? MEDIA_DEVICE_OK | 661 OnStartDone(is_running ? MEDIA_DEVICE_OK |
| 504 : MEDIA_DEVICE_TRACK_START_FAILURE); | 662 : MEDIA_DEVICE_TRACK_START_FAILURE); |
| 505 is_capture_starting_ = false; | 663 is_capture_starting_ = false; |
| 506 } else if (!is_running) { | 664 } else if (!is_running) { |
| 507 StopSource(); | 665 StopSource(); |
| 508 } | 666 } |
| 509 } | 667 } |
| 510 | 668 |
| 511 const char* | 669 const char* |
| 512 MediaStreamVideoCapturerSource::GetPowerLineFrequencyForTesting() const { | 670 MediaStreamVideoCapturerSource::GetPowerLineFrequencyForTesting() const { |
| 513 return kPowerLineFrequency; | 671 return kPowerLineFrequency; |
| 514 } | 672 } |
| 515 | 673 |
| 516 } // namespace content | 674 } // namespace content |
| OLD | NEW |