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