OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "media/blink/webmediaplayer_impl.h" | 5 #include "media/blink/webmediaplayer_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
23 #include "media/base/media_log.h" | 23 #include "media/base/media_log.h" |
24 #include "media/base/media_switches.h" | 24 #include "media/base/media_switches.h" |
25 #include "media/base/test_helpers.h" | 25 #include "media/base/test_helpers.h" |
26 #include "media/blink/webmediaplayer_delegate.h" | 26 #include "media/blink/webmediaplayer_delegate.h" |
27 #include "media/blink/webmediaplayer_params.h" | 27 #include "media/blink/webmediaplayer_params.h" |
28 #include "media/renderers/default_renderer_factory.h" | 28 #include "media/renderers/default_renderer_factory.h" |
29 #include "testing/gmock/include/gmock/gmock.h" | 29 #include "testing/gmock/include/gmock/gmock.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 #include "third_party/WebKit/public/platform/WebMediaPlayer.h" |
31 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" | 32 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" |
32 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" | 33 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
33 #include "third_party/WebKit/public/platform/WebSize.h" | 34 #include "third_party/WebKit/public/platform/WebSize.h" |
34 #include "third_party/WebKit/public/web/WebFrameClient.h" | 35 #include "third_party/WebKit/public/web/WebFrameClient.h" |
35 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 36 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
36 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 37 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
37 #include "third_party/WebKit/public/web/WebView.h" | 38 #include "third_party/WebKit/public/web/WebView.h" |
38 #include "url/gurl.h" | 39 #include "url/gurl.h" |
39 | 40 |
40 using ::testing::AnyNumber; | 41 using ::testing::AnyNumber; |
41 using ::testing::InSequence; | 42 using ::testing::InSequence; |
42 using ::testing::Return; | 43 using ::testing::Return; |
43 using ::testing::_; | 44 using ::testing::_; |
44 | 45 |
45 namespace media { | 46 namespace media { |
46 | 47 |
| 48 // Specify different values for testing. |
| 49 const base::TimeDelta kMaxKeyframeDistanceToDisableBackgroundVideo = |
| 50 base::TimeDelta::FromSeconds(5); |
| 51 const base::TimeDelta kMaxKeyframeDistanceToDisableBackgroundVideoMSE = |
| 52 base::TimeDelta::FromSeconds(10); |
| 53 |
47 int64_t OnAdjustAllocatedMemory(int64_t delta) { | 54 int64_t OnAdjustAllocatedMemory(int64_t delta) { |
48 return 0; | 55 return 0; |
49 } | 56 } |
50 | 57 |
51 class DummyWebMediaPlayerClient : public blink::WebMediaPlayerClient { | 58 class DummyWebMediaPlayerClient : public blink::WebMediaPlayerClient { |
52 public: | 59 public: |
53 DummyWebMediaPlayerClient() {} | 60 DummyWebMediaPlayerClient() {} |
54 | 61 |
55 // blink::WebMediaPlayerClient implementation. | 62 // blink::WebMediaPlayerClient implementation. |
56 void NetworkStateChanged() override {} | 63 void NetworkStateChanged() override {} |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 web_local_frame_, &client_, nullptr, &delegate_, | 215 web_local_frame_, &client_, nullptr, &delegate_, |
209 base::MakeUnique<DefaultRendererFactory>( | 216 base::MakeUnique<DefaultRendererFactory>( |
210 media_log_, nullptr, DefaultRendererFactory::GetGpuFactoriesCB()), | 217 media_log_, nullptr, DefaultRendererFactory::GetGpuFactoriesCB()), |
211 url_index_, | 218 url_index_, |
212 WebMediaPlayerParams( | 219 WebMediaPlayerParams( |
213 WebMediaPlayerParams::DeferLoadCB(), | 220 WebMediaPlayerParams::DeferLoadCB(), |
214 scoped_refptr<SwitchableAudioRendererSink>(), media_log_, | 221 scoped_refptr<SwitchableAudioRendererSink>(), media_log_, |
215 media_thread_.task_runner(), message_loop_.task_runner(), | 222 media_thread_.task_runner(), message_loop_.task_runner(), |
216 message_loop_.task_runner(), WebMediaPlayerParams::Context3DCB(), | 223 message_loop_.task_runner(), WebMediaPlayerParams::Context3DCB(), |
217 base::Bind(&OnAdjustAllocatedMemory), nullptr, nullptr, nullptr, | 224 base::Bind(&OnAdjustAllocatedMemory), nullptr, nullptr, nullptr, |
218 base::TimeDelta::FromSeconds(10), false, allow_suspend, false)); | 225 kMaxKeyframeDistanceToDisableBackgroundVideo, |
| 226 kMaxKeyframeDistanceToDisableBackgroundVideoMSE, false, |
| 227 allow_suspend, false)); |
219 } | 228 } |
220 | 229 |
221 ~WebMediaPlayerImplTest() override { | 230 ~WebMediaPlayerImplTest() override { |
222 // Destruct WebMediaPlayerImpl and pump the message loop to ensure that | 231 // Destruct WebMediaPlayerImpl and pump the message loop to ensure that |
223 // objects passed to the message loop for destruction are released. | 232 // objects passed to the message loop for destruction are released. |
224 // | 233 // |
225 // NOTE: This should be done before any other member variables are | 234 // NOTE: This should be done before any other member variables are |
226 // destructed since WMPI may reference them during destruction. | 235 // destructed since WMPI may reference them during destruction. |
227 wmpi_.reset(); | 236 wmpi_.reset(); |
228 base::RunLoop().RunUntilIdle(); | 237 base::RunLoop().RunUntilIdle(); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 void ScheduleIdlePauseTimer() { wmpi_->ScheduleIdlePauseTimer(); } | 333 void ScheduleIdlePauseTimer() { wmpi_->ScheduleIdlePauseTimer(); } |
325 | 334 |
326 bool IsIdlePauseTimerRunning() { | 335 bool IsIdlePauseTimerRunning() { |
327 return wmpi_->background_pause_timer_.IsRunning(); | 336 return wmpi_->background_pause_timer_.IsRunning(); |
328 } | 337 } |
329 | 338 |
330 void SetSuspendState(bool is_suspended) { | 339 void SetSuspendState(bool is_suspended) { |
331 wmpi_->SetSuspendState(is_suspended); | 340 wmpi_->SetSuspendState(is_suspended); |
332 } | 341 } |
333 | 342 |
| 343 void SetLoadType(blink::WebMediaPlayer::LoadType load_type) { |
| 344 wmpi_->load_type_ = load_type; |
| 345 } |
| 346 |
334 // "Renderer" thread. | 347 // "Renderer" thread. |
335 base::MessageLoop message_loop_; | 348 base::MessageLoop message_loop_; |
336 | 349 |
337 // "Media" thread. This is necessary because WMPI destruction waits on a | 350 // "Media" thread. This is necessary because WMPI destruction waits on a |
338 // WaitableEvent. | 351 // WaitableEvent. |
339 base::Thread media_thread_; | 352 base::Thread media_thread_; |
340 | 353 |
341 // Blink state. | 354 // Blink state. |
342 blink::WebFrameClient web_frame_client_; | 355 blink::WebFrameClient web_frame_client_; |
343 blink::WebView* web_view_; | 356 blink::WebView* web_view_; |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 EXPECT_FALSE(IsIdlePauseTimerRunning()); | 802 EXPECT_FALSE(IsIdlePauseTimerRunning()); |
790 | 803 |
791 SetMetadata(true, true); | 804 SetMetadata(true, true); |
792 ScheduleIdlePauseTimer(); | 805 ScheduleIdlePauseTimer(); |
793 EXPECT_TRUE(IsIdlePauseTimerRunning()); | 806 EXPECT_TRUE(IsIdlePauseTimerRunning()); |
794 } | 807 } |
795 | 808 |
796 class WebMediaPlayerImplBackgroundBehaviorTest | 809 class WebMediaPlayerImplBackgroundBehaviorTest |
797 : public WebMediaPlayerImplTest, | 810 : public WebMediaPlayerImplTest, |
798 public ::testing::WithParamInterface< | 811 public ::testing::WithParamInterface< |
799 std::tuple<bool, bool, int, int, bool>> { | 812 std::tuple<bool, bool, int, int, bool, bool>> { |
800 public: | 813 public: |
801 // Indices of the tuple parameters. | 814 // Indices of the tuple parameters. |
802 static const int kIsMediaSuspendEnabled = 0; | 815 static const int kIsMediaSuspendEnabled = 0; |
803 static const int kIsBackgroundOptimizationEnabled = 1; | 816 static const int kIsBackgroundOptimizationEnabled = 1; |
804 static const int kDurationSec = 2; | 817 static const int kDurationSec = 2; |
805 static const int kAverageKeyframeDistanceSec = 3; | 818 static const int kAverageKeyframeDistanceSec = 3; |
806 static const int kIsResumeBackgroundVideoEnabled = 4; | 819 static const int kIsResumeBackgroundVideoEnabled = 4; |
| 820 static const int kIsMediaSource = 5; |
807 | 821 |
808 void SetUp() override { | 822 void SetUp() override { |
809 WebMediaPlayerImplTest::SetUp(); | 823 WebMediaPlayerImplTest::SetUp(); |
810 | 824 |
811 SetUpMediaSuspend(IsMediaSuspendOn()); | 825 SetUpMediaSuspend(IsMediaSuspendOn()); |
812 | 826 |
813 std::string enabled_features; | 827 std::string enabled_features; |
814 std::string disabled_features; | 828 std::string disabled_features; |
815 if (IsBackgroundOptimizationOn()) { | 829 if (IsBackgroundOptimizationOn()) { |
816 enabled_features += kBackgroundVideoTrackOptimization.name; | 830 enabled_features += kBackgroundVideoTrackOptimization.name; |
817 } else { | 831 } else { |
818 disabled_features += kBackgroundVideoTrackOptimization.name; | 832 disabled_features += kBackgroundVideoTrackOptimization.name; |
819 } | 833 } |
820 | 834 |
821 if (IsResumeBackgroundVideoEnabled()) { | 835 if (IsResumeBackgroundVideoEnabled()) { |
822 if (!enabled_features.empty()) | 836 if (!enabled_features.empty()) |
823 enabled_features += ","; | 837 enabled_features += ","; |
824 enabled_features += kResumeBackgroundVideo.name; | 838 enabled_features += kResumeBackgroundVideo.name; |
825 } else { | 839 } else { |
826 if (!disabled_features.empty()) | 840 if (!disabled_features.empty()) |
827 disabled_features += ","; | 841 disabled_features += ","; |
828 disabled_features += kResumeBackgroundVideo.name; | 842 disabled_features += kResumeBackgroundVideo.name; |
829 } | 843 } |
830 | 844 |
831 feature_list_.InitFromCommandLine(enabled_features, disabled_features); | 845 feature_list_.InitFromCommandLine(enabled_features, disabled_features); |
832 | 846 |
833 InitializeWebMediaPlayerImpl(true); | 847 InitializeWebMediaPlayerImpl(true); |
| 848 bool is_media_source = std::get<kIsMediaSource>(GetParam()); |
| 849 SetLoadType(is_media_source ? blink::WebMediaPlayer::kLoadTypeMediaSource |
| 850 : blink::WebMediaPlayer::kLoadTypeURL); |
834 SetVideoKeyframeDistanceAverage( | 851 SetVideoKeyframeDistanceAverage( |
835 base::TimeDelta::FromSeconds(GetAverageKeyframeDistanceSec())); | 852 base::TimeDelta::FromSeconds(GetAverageKeyframeDistanceSec())); |
836 SetDuration(base::TimeDelta::FromSeconds(GetDurationSec())); | 853 SetDuration(base::TimeDelta::FromSeconds(GetDurationSec())); |
837 BackgroundPlayer(); | 854 BackgroundPlayer(); |
838 } | 855 } |
839 | 856 |
840 void SetDuration(base::TimeDelta value) { | 857 void SetDuration(base::TimeDelta value) { |
841 wmpi_->SetPipelineMediaDurationForTest(value); | 858 wmpi_->SetPipelineMediaDurationForTest(value); |
842 } | 859 } |
843 | 860 |
(...skipping 14 matching lines...) Expand all Loading... |
858 bool IsResumeBackgroundVideoEnabled() { | 875 bool IsResumeBackgroundVideoEnabled() { |
859 return std::get<kIsResumeBackgroundVideoEnabled>(GetParam()); | 876 return std::get<kIsResumeBackgroundVideoEnabled>(GetParam()); |
860 } | 877 } |
861 | 878 |
862 int GetDurationSec() const { return std::get<kDurationSec>(GetParam()); } | 879 int GetDurationSec() const { return std::get<kDurationSec>(GetParam()); } |
863 | 880 |
864 int GetAverageKeyframeDistanceSec() const { | 881 int GetAverageKeyframeDistanceSec() const { |
865 return std::get<kAverageKeyframeDistanceSec>(GetParam()); | 882 return std::get<kAverageKeyframeDistanceSec>(GetParam()); |
866 } | 883 } |
867 | 884 |
| 885 int GetMaxKeyframeDistanceSec() const { |
| 886 base::TimeDelta max_keyframe_distance = |
| 887 std::get<kIsMediaSource>(GetParam()) |
| 888 ? kMaxKeyframeDistanceToDisableBackgroundVideoMSE |
| 889 : kMaxKeyframeDistanceToDisableBackgroundVideo; |
| 890 return max_keyframe_distance.InSeconds(); |
| 891 } |
| 892 |
868 bool IsAndroid() { | 893 bool IsAndroid() { |
869 #if defined(OS_ANDROID) | 894 #if defined(OS_ANDROID) |
870 return true; | 895 return true; |
871 #else | 896 #else |
872 return false; | 897 return false; |
873 #endif | 898 #endif |
874 } | 899 } |
875 | 900 |
876 bool ShouldDisableVideoWhenHidden() const { | 901 bool ShouldDisableVideoWhenHidden() const { |
877 return wmpi_->ShouldDisableVideoWhenHidden(); | 902 return wmpi_->ShouldDisableVideoWhenHidden(); |
(...skipping 22 matching lines...) Expand all Loading... |
900 TEST_P(WebMediaPlayerImplBackgroundBehaviorTest, VideoOnly) { | 925 TEST_P(WebMediaPlayerImplBackgroundBehaviorTest, VideoOnly) { |
901 // Video only. | 926 // Video only. |
902 SetMetadata(false, true); | 927 SetMetadata(false, true); |
903 | 928 |
904 // Never disable video track for a video only stream. | 929 // Never disable video track for a video only stream. |
905 EXPECT_FALSE(ShouldDisableVideoWhenHidden()); | 930 EXPECT_FALSE(ShouldDisableVideoWhenHidden()); |
906 | 931 |
907 // There's no optimization criteria for video only on Android. | 932 // There's no optimization criteria for video only on Android. |
908 bool matches_requirements = | 933 bool matches_requirements = |
909 IsAndroid() || | 934 IsAndroid() || |
910 ((GetDurationSec() < GetAverageKeyframeDistanceSec()) || | 935 ((GetDurationSec() < GetMaxKeyframeDistanceSec()) || |
911 (GetAverageKeyframeDistanceSec() < 10)); | 936 (GetAverageKeyframeDistanceSec() < GetMaxKeyframeDistanceSec())); |
912 EXPECT_EQ(matches_requirements, IsBackgroundOptimizationCandidate()); | 937 EXPECT_EQ(matches_requirements, IsBackgroundOptimizationCandidate()); |
913 | 938 |
914 // Video is always paused when suspension is on and only if matches the | 939 // Video is always paused when suspension is on and only if matches the |
915 // optimization criteria if the optimization is on. | 940 // optimization criteria if the optimization is on. |
916 bool should_pause = IsMediaSuspendOn() || | 941 bool should_pause = IsMediaSuspendOn() || |
917 (IsBackgroundOptimizationOn() && matches_requirements); | 942 (IsBackgroundOptimizationOn() && matches_requirements); |
918 EXPECT_EQ(should_pause, ShouldPauseVideoWhenHidden()); | 943 EXPECT_EQ(should_pause, ShouldPauseVideoWhenHidden()); |
919 } | 944 } |
920 | 945 |
921 TEST_P(WebMediaPlayerImplBackgroundBehaviorTest, AudioVideo) { | 946 TEST_P(WebMediaPlayerImplBackgroundBehaviorTest, AudioVideo) { |
922 SetMetadata(true, true); | 947 SetMetadata(true, true); |
923 | 948 |
924 // Optimization requirements are the same for all platforms. | 949 // Optimization requirements are the same for all platforms. |
925 bool matches_requirements = | 950 bool matches_requirements = |
926 (GetDurationSec() < GetAverageKeyframeDistanceSec()) || | 951 (GetDurationSec() < GetMaxKeyframeDistanceSec()) || |
927 (GetAverageKeyframeDistanceSec() < 10); | 952 (GetAverageKeyframeDistanceSec() < GetMaxKeyframeDistanceSec()); |
928 | 953 |
929 EXPECT_EQ(matches_requirements, IsBackgroundOptimizationCandidate()); | 954 EXPECT_EQ(matches_requirements, IsBackgroundOptimizationCandidate()); |
930 EXPECT_EQ(IsBackgroundOptimizationOn() && matches_requirements, | 955 EXPECT_EQ(IsBackgroundOptimizationOn() && matches_requirements, |
931 ShouldDisableVideoWhenHidden()); | 956 ShouldDisableVideoWhenHidden()); |
932 | 957 |
933 // Only pause audible videos on Android if both media suspend and resume | 958 // Only pause audible videos on Android if both media suspend and resume |
934 // background videos is on. On Desktop | 959 // background videos is on. On Desktop |
935 EXPECT_EQ(IsMediaSuspendOn() && IsResumeBackgroundVideoEnabled(), | 960 EXPECT_EQ(IsMediaSuspendOn() && IsResumeBackgroundVideoEnabled(), |
936 ShouldPauseVideoWhenHidden()); | 961 ShouldPauseVideoWhenHidden()); |
937 } | 962 } |
938 | 963 |
939 INSTANTIATE_TEST_CASE_P(BackgroundBehaviorTestInstances, | 964 INSTANTIATE_TEST_CASE_P(BackgroundBehaviorTestInstances, |
940 WebMediaPlayerImplBackgroundBehaviorTest, | 965 WebMediaPlayerImplBackgroundBehaviorTest, |
941 ::testing::Combine(::testing::Bool(), | 966 ::testing::Combine(::testing::Bool(), |
942 ::testing::Bool(), | 967 ::testing::Bool(), |
943 ::testing::Values(5, 300), | 968 ::testing::Values(5, 300), |
944 ::testing::Values(5, 100), | 969 ::testing::Values(5, 100), |
| 970 ::testing::Bool(), |
945 ::testing::Bool())); | 971 ::testing::Bool())); |
946 | 972 |
947 } // namespace media | 973 } // namespace media |
OLD | NEW |