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_source.h" | 5 #include "content/renderer/media/media_stream_video_source.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "content/child/child_process.h" | 14 #include "content/child/child_process.h" |
15 #include "content/renderer/media/media_stream_constraints_util.h" | 15 #include "content/renderer/media/media_stream_constraints_util.h" |
16 #include "content/renderer/media/media_stream_video_track.h" | 16 #include "content/renderer/media/media_stream_video_track.h" |
17 #include "content/renderer/media/video_track_adapter.h" | 17 #include "content/renderer/media/video_track_adapter.h" |
18 #include "media/base/bind_to_current_loop.h" | |
19 | 18 |
20 namespace content { | 19 namespace content { |
21 | 20 |
22 // Constraint keys. Specified by draft-alvestrand-constraints-resolution-00b | 21 // Constraint keys. Specified by draft-alvestrand-constraints-resolution-00b |
23 const char MediaStreamVideoSource::kMinAspectRatio[] = "minAspectRatio"; | 22 const char MediaStreamVideoSource::kMinAspectRatio[] = "minAspectRatio"; |
24 const char MediaStreamVideoSource::kMaxAspectRatio[] = "maxAspectRatio"; | 23 const char MediaStreamVideoSource::kMaxAspectRatio[] = "maxAspectRatio"; |
25 const char MediaStreamVideoSource::kMaxWidth[] = "maxWidth"; | 24 const char MediaStreamVideoSource::kMaxWidth[] = "maxWidth"; |
26 const char MediaStreamVideoSource::kMinWidth[] = "minWidth"; | 25 const char MediaStreamVideoSource::kMinWidth[] = "minWidth"; |
27 const char MediaStreamVideoSource::kMaxHeight[] = "maxHeight"; | 26 const char MediaStreamVideoSource::kMaxHeight[] = "maxHeight"; |
28 const char MediaStreamVideoSource::kMinHeight[] = "minHeight"; | 27 const char MediaStreamVideoSource::kMinHeight[] = "minHeight"; |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 bool MediaStreamVideoSource::IsConstraintSupported(const std::string& name) { | 350 bool MediaStreamVideoSource::IsConstraintSupported(const std::string& name) { |
352 for (size_t i = 0; i < arraysize(kSupportedConstraints); ++i) { | 351 for (size_t i = 0; i < arraysize(kSupportedConstraints); ++i) { |
353 if (kSupportedConstraints[i] == name) | 352 if (kSupportedConstraints[i] == name) |
354 return true; | 353 return true; |
355 } | 354 } |
356 return false; | 355 return false; |
357 } | 356 } |
358 | 357 |
359 MediaStreamVideoSource::MediaStreamVideoSource() | 358 MediaStreamVideoSource::MediaStreamVideoSource() |
360 : state_(NEW), | 359 : state_(NEW), |
361 muted_state_(false), | |
362 track_adapter_(new VideoTrackAdapter( | 360 track_adapter_(new VideoTrackAdapter( |
363 ChildProcess::current()->io_message_loop_proxy())), | 361 ChildProcess::current()->io_message_loop_proxy())), |
364 weak_factory_(this) { | 362 weak_factory_(this) { |
365 } | 363 } |
366 | 364 |
367 MediaStreamVideoSource::~MediaStreamVideoSource() { | 365 MediaStreamVideoSource::~MediaStreamVideoSource() { |
368 DCHECK(CalledOnValidThread()); | 366 DCHECK(CalledOnValidThread()); |
369 } | 367 } |
370 | 368 |
371 void MediaStreamVideoSource::AddTrack( | 369 void MediaStreamVideoSource::AddTrack( |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 MediaStreamVideoSource::io_message_loop() const { | 451 MediaStreamVideoSource::io_message_loop() const { |
454 DCHECK(CalledOnValidThread()); | 452 DCHECK(CalledOnValidThread()); |
455 return track_adapter_->io_message_loop(); | 453 return track_adapter_->io_message_loop(); |
456 } | 454 } |
457 | 455 |
458 void MediaStreamVideoSource::DoStopSource() { | 456 void MediaStreamVideoSource::DoStopSource() { |
459 DCHECK(CalledOnValidThread()); | 457 DCHECK(CalledOnValidThread()); |
460 DVLOG(3) << "DoStopSource()"; | 458 DVLOG(3) << "DoStopSource()"; |
461 if (state_ == ENDED) | 459 if (state_ == ENDED) |
462 return; | 460 return; |
| 461 track_adapter_->StopFrameMonitoring(); |
463 StopSourceImpl(); | 462 StopSourceImpl(); |
464 state_ = ENDED; | 463 state_ = ENDED; |
465 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded); | 464 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded); |
466 } | 465 } |
467 | 466 |
468 void MediaStreamVideoSource::OnSupportedFormats( | 467 void MediaStreamVideoSource::OnSupportedFormats( |
469 const media::VideoCaptureFormats& formats) { | 468 const media::VideoCaptureFormats& formats) { |
470 DCHECK(CalledOnValidThread()); | 469 DCHECK(CalledOnValidThread()); |
471 DCHECK_EQ(RETRIEVING_CAPABILITIES, state_); | 470 DCHECK_EQ(RETRIEVING_CAPABILITIES, state_); |
472 | 471 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 return false; | 527 return false; |
529 } | 528 } |
530 | 529 |
531 void MediaStreamVideoSource::OnStartDone(MediaStreamRequestResult result) { | 530 void MediaStreamVideoSource::OnStartDone(MediaStreamRequestResult result) { |
532 DCHECK(CalledOnValidThread()); | 531 DCHECK(CalledOnValidThread()); |
533 DVLOG(3) << "OnStartDone({result =" << result << "})"; | 532 DVLOG(3) << "OnStartDone({result =" << result << "})"; |
534 if (result == MEDIA_DEVICE_OK) { | 533 if (result == MEDIA_DEVICE_OK) { |
535 DCHECK_EQ(STARTING, state_); | 534 DCHECK_EQ(STARTING, state_); |
536 state_ = STARTED; | 535 state_ = STARTED; |
537 SetReadyState(blink::WebMediaStreamSource::ReadyStateLive); | 536 SetReadyState(blink::WebMediaStreamSource::ReadyStateLive); |
| 537 |
| 538 track_adapter_->StartFrameMonitoring( |
| 539 current_format_.frame_rate, |
| 540 base::Bind(&MediaStreamVideoSource::SetMutedState, |
| 541 weak_factory_.GetWeakPtr())); |
| 542 |
538 } else { | 543 } else { |
539 StopSource(); | 544 StopSource(); |
540 } | 545 } |
541 | 546 |
542 // This object can be deleted after calling FinalizeAddTrack. See comment in | 547 // This object can be deleted after calling FinalizeAddTrack. See comment in |
543 // the header file. | 548 // the header file. |
544 FinalizeAddTrack(); | 549 FinalizeAddTrack(); |
545 } | 550 } |
546 | 551 |
547 void MediaStreamVideoSource::FinalizeAddTrack() { | 552 void MediaStreamVideoSource::FinalizeAddTrack() { |
(...skipping 22 matching lines...) Expand all Loading... |
570 GetDesiredMaxWidthAndHeight(it->constraints, &max_width, &max_height); | 575 GetDesiredMaxWidthAndHeight(it->constraints, &max_width, &max_height); |
571 double max_aspect_ratio; | 576 double max_aspect_ratio; |
572 double min_aspect_ratio; | 577 double min_aspect_ratio; |
573 GetDesiredMinAndMaxAspectRatio(it->constraints, | 578 GetDesiredMinAndMaxAspectRatio(it->constraints, |
574 &min_aspect_ratio, | 579 &min_aspect_ratio, |
575 &max_aspect_ratio); | 580 &max_aspect_ratio); |
576 double max_frame_rate = 0.0f; | 581 double max_frame_rate = 0.0f; |
577 GetConstraintValueAsDouble(it->constraints, | 582 GetConstraintValueAsDouble(it->constraints, |
578 kMaxFrameRate, &max_frame_rate); | 583 kMaxFrameRate, &max_frame_rate); |
579 | 584 |
580 VideoTrackAdapter::OnMutedCallback on_mute_callback = | |
581 media::BindToCurrentLoop(base::Bind( | |
582 &MediaStreamVideoSource::SetMutedState, | |
583 weak_factory_.GetWeakPtr())); | |
584 track_adapter_->AddTrack(it->track, it->frame_callback, | 585 track_adapter_->AddTrack(it->track, it->frame_callback, |
585 max_width, max_height, | 586 max_width, max_height, |
586 min_aspect_ratio, max_aspect_ratio, | 587 min_aspect_ratio, max_aspect_ratio, |
587 max_frame_rate, current_format_.frame_rate, | 588 max_frame_rate); |
588 on_mute_callback); | |
589 } | 589 } |
590 | 590 |
591 DVLOG(3) << "FinalizeAddTrack() result " << result; | 591 DVLOG(3) << "FinalizeAddTrack() result " << result; |
592 | 592 |
593 if (!it->callback.is_null()) { | 593 if (!it->callback.is_null()) { |
594 it->callback.Run(this, result, unsatisfied_constraint); | 594 it->callback.Run(this, result, unsatisfied_constraint); |
595 } | 595 } |
596 } | 596 } |
597 } | 597 } |
598 | 598 |
599 void MediaStreamVideoSource::SetReadyState( | 599 void MediaStreamVideoSource::SetReadyState( |
600 blink::WebMediaStreamSource::ReadyState state) { | 600 blink::WebMediaStreamSource::ReadyState state) { |
601 DVLOG(3) << "MediaStreamVideoSource::SetReadyState state " << state; | 601 DVLOG(3) << "MediaStreamVideoSource::SetReadyState state " << state; |
602 DCHECK(CalledOnValidThread()); | 602 DCHECK(CalledOnValidThread()); |
603 if (!owner().isNull()) | 603 if (!owner().isNull()) |
604 owner().setReadyState(state); | 604 owner().setReadyState(state); |
605 for (std::vector<MediaStreamVideoTrack*>::iterator it = tracks_.begin(); | 605 for (std::vector<MediaStreamVideoTrack*>::iterator it = tracks_.begin(); |
606 it != tracks_.end(); ++it) { | 606 it != tracks_.end(); ++it) { |
607 (*it)->OnReadyStateChanged(state); | 607 (*it)->OnReadyStateChanged(state); |
608 } | 608 } |
609 } | 609 } |
610 | 610 |
611 void MediaStreamVideoSource::SetMutedState(bool muted_state) { | 611 void MediaStreamVideoSource::SetMutedState(bool muted_state) { |
612 DVLOG(3) << "MediaStreamVideoSource::SetMutedState state=" << muted_state; | 612 DVLOG(3) << "MediaStreamVideoSource::SetMutedState state=" << muted_state; |
613 DCHECK(CalledOnValidThread()); | 613 DCHECK(CalledOnValidThread()); |
614 if (muted_state != muted_state_) { | 614 if (!owner().isNull()) { |
615 muted_state_ = muted_state; | 615 owner().setReadyState(muted_state |
616 if (!owner().isNull()) { | |
617 owner().setReadyState(muted_state_ | |
618 ? blink::WebMediaStreamSource::ReadyStateMuted | 616 ? blink::WebMediaStreamSource::ReadyStateMuted |
619 : blink::WebMediaStreamSource::ReadyStateLive); | 617 : blink::WebMediaStreamSource::ReadyStateLive); |
620 } | |
621 } | |
622 // WebMediaStreamSource doesn't have a muted state, the tracks do. | |
623 for (std::vector<MediaStreamVideoTrack*>::iterator it = tracks_.begin(); | |
624 it != tracks_.end(); ++it) { | |
625 (*it)->SetMutedState(muted_state); | |
626 } | 618 } |
627 } | 619 } |
628 | 620 |
629 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints( | 621 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints( |
630 MediaStreamVideoTrack* track, | 622 MediaStreamVideoTrack* track, |
631 const VideoCaptureDeliverFrameCB& frame_callback, | 623 const VideoCaptureDeliverFrameCB& frame_callback, |
632 const blink::WebMediaConstraints& constraints, | 624 const blink::WebMediaConstraints& constraints, |
633 const ConstraintsCallback& callback) | 625 const ConstraintsCallback& callback) |
634 : track(track), | 626 : track(track), |
635 frame_callback(frame_callback), | 627 frame_callback(frame_callback), |
636 constraints(constraints), | 628 constraints(constraints), |
637 callback(callback) { | 629 callback(callback) { |
638 } | 630 } |
639 | 631 |
640 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() { | 632 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() { |
641 } | 633 } |
642 | 634 |
643 } // namespace content | 635 } // namespace content |
OLD | NEW |