| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/glue/webmediaplayer_impl.h" | 5 #include "webkit/glue/webmediaplayer_impl.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 #include "webkit/glue/media/media_stream_client.h" | 33 #include "webkit/glue/media/media_stream_client.h" |
| 34 #include "webkit/glue/media/video_renderer_impl.h" | 34 #include "webkit/glue/media/video_renderer_impl.h" |
| 35 #include "webkit/glue/media/web_video_renderer.h" | 35 #include "webkit/glue/media/web_video_renderer.h" |
| 36 #include "webkit/glue/webmediaplayer_delegate.h" | 36 #include "webkit/glue/webmediaplayer_delegate.h" |
| 37 #include "webkit/glue/webmediaplayer_proxy.h" | 37 #include "webkit/glue/webmediaplayer_proxy.h" |
| 38 #include "webkit/glue/webvideoframe_impl.h" | 38 #include "webkit/glue/webvideoframe_impl.h" |
| 39 | 39 |
| 40 using WebKit::WebCanvas; | 40 using WebKit::WebCanvas; |
| 41 using WebKit::WebRect; | 41 using WebKit::WebRect; |
| 42 using WebKit::WebSize; | 42 using WebKit::WebSize; |
| 43 using media::NetworkEvent; |
| 43 using media::PipelineStatus; | 44 using media::PipelineStatus; |
| 44 | 45 |
| 45 namespace { | 46 namespace { |
| 46 | 47 |
| 47 // Amount of extra memory used by each player instance reported to V8. | 48 // Amount of extra memory used by each player instance reported to V8. |
| 48 // It is not exact number -- first, it differs on different platforms, | 49 // It is not exact number -- first, it differs on different platforms, |
| 49 // and second, it is very hard to calculate. Instead, use some arbitrary | 50 // and second, it is very hard to calculate. Instead, use some arbitrary |
| 50 // value that will cause garbage collection from time to time. We don't want | 51 // value that will cause garbage collection from time to time. We don't want |
| 51 // it to happen on every allocation, but don't want 5k players to sit in memory | 52 // it to happen on every allocation, but don't want 5k players to sit in memory |
| 52 // either. Looks that chosen constant achieves both goals, at least for audio | 53 // either. Looks that chosen constant achieves both goals, at least for audio |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 message_loop_factory_(message_loop_factory), | 112 message_loop_factory_(message_loop_factory), |
| 112 paused_(true), | 113 paused_(true), |
| 113 seeking_(false), | 114 seeking_(false), |
| 114 playback_rate_(0.0f), | 115 playback_rate_(0.0f), |
| 115 pending_seek_(false), | 116 pending_seek_(false), |
| 116 client_(client), | 117 client_(client), |
| 117 proxy_(NULL), | 118 proxy_(NULL), |
| 118 delegate_(delegate), | 119 delegate_(delegate), |
| 119 media_stream_client_(media_stream_client), | 120 media_stream_client_(media_stream_client), |
| 120 media_log_(media_log), | 121 media_log_(media_log), |
| 121 incremented_externally_allocated_memory_(false) { | 122 incremented_externally_allocated_memory_(false), |
| 123 can_play_through_(false) { |
| 122 // Saves the current message loop. | 124 // Saves the current message loop. |
| 123 DCHECK(!main_loop_); | 125 DCHECK(!main_loop_); |
| 124 main_loop_ = MessageLoop::current(); | 126 main_loop_ = MessageLoop::current(); |
| 125 media_log_->AddEvent( | 127 media_log_->AddEvent( |
| 126 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED)); | 128 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED)); |
| 127 } | 129 } |
| 128 | 130 |
| 129 bool WebMediaPlayerImpl::Initialize( | 131 bool WebMediaPlayerImpl::Initialize( |
| 130 WebKit::WebFrame* frame, | 132 WebKit::WebFrame* frame, |
| 131 bool use_simple_data_source, | 133 bool use_simple_data_source, |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 pending_seek_ = true; | 321 pending_seek_ = true; |
| 320 pending_seek_seconds_ = seconds; | 322 pending_seek_seconds_ = seconds; |
| 321 return; | 323 return; |
| 322 } | 324 } |
| 323 | 325 |
| 324 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds)); | 326 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds)); |
| 325 | 327 |
| 326 base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds); | 328 base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds); |
| 327 | 329 |
| 328 // Update our paused time. | 330 // Update our paused time. |
| 329 if (paused_) { | 331 if (paused_) |
| 330 paused_time_ = seek_time; | 332 paused_time_ = seek_time; |
| 331 } | |
| 332 | 333 |
| 333 seeking_ = true; | 334 seeking_ = true; |
| 334 | 335 |
| 335 proxy_->DemuxerFlush(); | 336 proxy_->DemuxerFlush(); |
| 336 | 337 |
| 337 // Kick off the asynchronous seek! | 338 // Kick off the asynchronous seek! |
| 338 pipeline_->Seek( | 339 pipeline_->Seek( |
| 339 seek_time, | 340 seek_time, |
| 340 base::Bind(&WebMediaPlayerProxy::PipelineSeekCallback, | 341 base::Bind(&WebMediaPlayerProxy::PipelineSeekCallback, |
| 341 proxy_.get())); | 342 proxy_.get())); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 DCHECK_EQ(main_loop_, MessageLoop::current()); | 443 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 443 | 444 |
| 444 base::TimeDelta duration = pipeline_->GetMediaDuration(); | 445 base::TimeDelta duration = pipeline_->GetMediaDuration(); |
| 445 if (duration.InMicroseconds() == media::Limits::kMaxTimeInMicroseconds) | 446 if (duration.InMicroseconds() == media::Limits::kMaxTimeInMicroseconds) |
| 446 return std::numeric_limits<float>::infinity(); | 447 return std::numeric_limits<float>::infinity(); |
| 447 return static_cast<float>(duration.InSecondsF()); | 448 return static_cast<float>(duration.InSecondsF()); |
| 448 } | 449 } |
| 449 | 450 |
| 450 float WebMediaPlayerImpl::currentTime() const { | 451 float WebMediaPlayerImpl::currentTime() const { |
| 451 DCHECK_EQ(main_loop_, MessageLoop::current()); | 452 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 452 if (paused_) { | 453 if (paused_) |
| 453 return static_cast<float>(paused_time_.InSecondsF()); | 454 return static_cast<float>(paused_time_.InSecondsF()); |
| 454 } | |
| 455 return static_cast<float>(pipeline_->GetCurrentTime().InSecondsF()); | 455 return static_cast<float>(pipeline_->GetCurrentTime().InSecondsF()); |
| 456 } | 456 } |
| 457 | 457 |
| 458 int WebMediaPlayerImpl::dataRate() const { | 458 int WebMediaPlayerImpl::dataRate() const { |
| 459 DCHECK_EQ(main_loop_, MessageLoop::current()); | 459 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 460 | 460 |
| 461 // TODO(hclam): Add this method call if pipeline has it in the interface. | 461 // TODO(hclam): Add this method call if pipeline has it in the interface. |
| 462 return 0; | 462 return 0; |
| 463 } | 463 } |
| 464 | 464 |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 void WebMediaPlayerImpl::OnPipelineInitialize(PipelineStatus status) { | 689 void WebMediaPlayerImpl::OnPipelineInitialize(PipelineStatus status) { |
| 690 DCHECK_EQ(main_loop_, MessageLoop::current()); | 690 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 691 if (status == media::PIPELINE_OK) { | 691 if (status == media::PIPELINE_OK) { |
| 692 // Only keep one time range starting from 0. | 692 // Only keep one time range starting from 0. |
| 693 WebKit::WebTimeRanges new_buffered(static_cast<size_t>(1)); | 693 WebKit::WebTimeRanges new_buffered(static_cast<size_t>(1)); |
| 694 new_buffered[0].start = 0.0f; | 694 new_buffered[0].start = 0.0f; |
| 695 new_buffered[0].end = | 695 new_buffered[0].end = |
| 696 static_cast<float>(pipeline_->GetMediaDuration().InSecondsF()); | 696 static_cast<float>(pipeline_->GetMediaDuration().InSecondsF()); |
| 697 buffered_.swap(new_buffered); | 697 buffered_.swap(new_buffered); |
| 698 | 698 |
| 699 if (pipeline_->IsLoaded()) { | 699 if (pipeline_->IsLoaded()) |
| 700 SetNetworkState(WebKit::WebMediaPlayer::Loaded); | 700 SetNetworkState(WebKit::WebMediaPlayer::Loaded); |
| 701 } | |
| 702 | 701 |
| 703 // Since we have initialized the pipeline, say we have everything otherwise | |
| 704 // we'll remain either loading/idle. | |
| 705 // TODO(hclam): change this to report the correct status. | |
| 706 SetReadyState(WebKit::WebMediaPlayer::HaveMetadata); | 702 SetReadyState(WebKit::WebMediaPlayer::HaveMetadata); |
| 707 SetReadyState(WebKit::WebMediaPlayer::HaveEnoughData); | 703 SetReadyState(WebKit::WebMediaPlayer::HaveFutureData); |
| 708 } else { | 704 } else { |
| 709 // TODO(hclam): should use |status| to determine the state | 705 // TODO(hclam): should use |status| to determine the state |
| 710 // properly and reports error using MediaError. | 706 // properly and reports error using MediaError. |
| 711 // WebKit uses FormatError to indicate an error for bogus URL or bad file. | 707 // WebKit uses FormatError to indicate an error for bogus URL or bad file. |
| 712 // Since we are at the initialization stage we can safely treat every error | 708 // Since we are at the initialization stage we can safely treat every error |
| 713 // as format error. Should post a task to call to |webmediaplayer_|. | 709 // as format error. Should post a task to call to |webmediaplayer_|. |
| 714 SetNetworkState(WebKit::WebMediaPlayer::FormatError); | 710 SetNetworkState(WebKit::WebMediaPlayer::FormatError); |
| 715 } | 711 } |
| 716 | 712 |
| 717 // Repaint to trigger UI update. | 713 // Repaint to trigger UI update. |
| 718 Repaint(); | 714 Repaint(); |
| 719 } | 715 } |
| 720 | 716 |
| 721 void WebMediaPlayerImpl::OnPipelineSeek(PipelineStatus status) { | 717 void WebMediaPlayerImpl::OnPipelineSeek(PipelineStatus status) { |
| 722 DCHECK_EQ(main_loop_, MessageLoop::current()); | 718 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 723 seeking_ = false; | 719 seeking_ = false; |
| 724 if (pending_seek_) { | 720 if (pending_seek_) { |
| 725 pending_seek_ = false; | 721 pending_seek_ = false; |
| 726 seek(pending_seek_seconds_); | 722 seek(pending_seek_seconds_); |
| 727 return; | 723 return; |
| 728 } | 724 } |
| 729 | 725 |
| 730 if (status == media::PIPELINE_OK) { | 726 if (status == media::PIPELINE_OK) { |
| 731 // Update our paused time. | 727 // Update our paused time. |
| 732 if (paused_) { | 728 if (paused_) |
| 733 paused_time_ = pipeline_->GetCurrentTime(); | 729 paused_time_ = pipeline_->GetCurrentTime(); |
| 734 } | |
| 735 | 730 |
| 736 SetReadyState(WebKit::WebMediaPlayer::HaveEnoughData); | 731 if (can_play_through_) |
| 732 SetReadyState(WebKit::WebMediaPlayer::HaveFutureData); |
| 733 else |
| 734 SetReadyState(WebKit::WebMediaPlayer::HaveEnoughData); |
| 735 |
| 737 GetClient()->timeChanged(); | 736 GetClient()->timeChanged(); |
| 738 } | 737 } |
| 739 } | 738 } |
| 740 | 739 |
| 741 void WebMediaPlayerImpl::OnPipelineEnded(PipelineStatus status) { | 740 void WebMediaPlayerImpl::OnPipelineEnded(PipelineStatus status) { |
| 742 DCHECK_EQ(main_loop_, MessageLoop::current()); | 741 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 743 if (status == media::PIPELINE_OK) { | 742 if (status == media::PIPELINE_OK) |
| 744 GetClient()->timeChanged(); | 743 GetClient()->timeChanged(); |
| 745 } | |
| 746 } | 744 } |
| 747 | 745 |
| 748 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) { | 746 void WebMediaPlayerImpl::OnPipelineError(PipelineStatus error) { |
| 749 DCHECK_EQ(main_loop_, MessageLoop::current()); | 747 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 750 switch (error) { | 748 switch (error) { |
| 751 case media::PIPELINE_OK: | 749 case media::PIPELINE_OK: |
| 752 LOG(DFATAL) << "PIPELINE_OK isn't an error!"; | 750 LOG(DFATAL) << "PIPELINE_OK isn't an error!"; |
| 753 break; | 751 break; |
| 754 | 752 |
| 755 case media::PIPELINE_ERROR_NETWORK: | 753 case media::PIPELINE_ERROR_NETWORK: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 779 case media::PIPELINE_ERROR_INVALID_STATE: | 777 case media::PIPELINE_ERROR_INVALID_STATE: |
| 780 // Decode error. | 778 // Decode error. |
| 781 SetNetworkState(WebMediaPlayer::DecodeError); | 779 SetNetworkState(WebMediaPlayer::DecodeError); |
| 782 break; | 780 break; |
| 783 } | 781 } |
| 784 | 782 |
| 785 // Repaint to trigger UI update. | 783 // Repaint to trigger UI update. |
| 786 Repaint(); | 784 Repaint(); |
| 787 } | 785 } |
| 788 | 786 |
| 789 void WebMediaPlayerImpl::OnNetworkEvent(bool is_downloading_data) { | 787 void WebMediaPlayerImpl::OnNetworkEvent(NetworkEvent type) { |
| 790 DCHECK_EQ(main_loop_, MessageLoop::current()); | 788 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 791 if (is_downloading_data) | 789 switch(type) { |
| 792 SetNetworkState(WebKit::WebMediaPlayer::Loading); | 790 case media::DOWNLOAD_CONTINUED: |
| 793 else | 791 SetNetworkState(WebKit::WebMediaPlayer::Loading); |
| 794 SetNetworkState(WebKit::WebMediaPlayer::Idle); | 792 break; |
| 793 case media::DOWNLOAD_PAUSED: |
| 794 SetNetworkState(WebKit::WebMediaPlayer::Idle); |
| 795 break; |
| 796 case media::CAN_PLAY_THROUGH: |
| 797 if (!can_play_through_) { |
| 798 can_play_through_ = true; |
| 799 SetReadyState(WebKit::WebMediaPlayer::HaveEnoughData); |
| 800 } |
| 801 break; |
| 802 default: |
| 803 NOTREACHED(); |
| 804 } |
| 795 } | 805 } |
| 796 | 806 |
| 797 void WebMediaPlayerImpl::OnDemuxerOpened() { | 807 void WebMediaPlayerImpl::OnDemuxerOpened() { |
| 798 DCHECK_EQ(main_loop_, MessageLoop::current()); | 808 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 799 | 809 |
| 800 // TODO(acolwell): Uncomment once WebKit changes are checked in. | 810 // TODO(acolwell): Uncomment once WebKit changes are checked in. |
| 801 // https://bugs.webkit.org/show_bug.cgi?id=64731 | 811 // https://bugs.webkit.org/show_bug.cgi?id=64731 |
| 802 //GetClient()->sourceOpened(); | 812 //GetClient()->sourceOpened(); |
| 803 } | 813 } |
| 804 | 814 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 858 return client_; | 868 return client_; |
| 859 } | 869 } |
| 860 | 870 |
| 861 void WebMediaPlayerImpl::IncrementExternallyAllocatedMemory() { | 871 void WebMediaPlayerImpl::IncrementExternallyAllocatedMemory() { |
| 862 DCHECK_EQ(main_loop_, MessageLoop::current()); | 872 DCHECK_EQ(main_loop_, MessageLoop::current()); |
| 863 incremented_externally_allocated_memory_ = true; | 873 incremented_externally_allocated_memory_ = true; |
| 864 v8::V8::AdjustAmountOfExternalAllocatedMemory(kPlayerExtraMemory); | 874 v8::V8::AdjustAmountOfExternalAllocatedMemory(kPlayerExtraMemory); |
| 865 } | 875 } |
| 866 | 876 |
| 867 } // namespace webkit_glue | 877 } // namespace webkit_glue |
| OLD | NEW |