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