OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/callback_helpers.h" | 16 #include "base/callback_helpers.h" |
17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
18 #include "base/debug/alias.h" | 18 #include "base/debug/alias.h" |
19 #include "base/debug/crash_logging.h" | 19 #include "base/debug/crash_logging.h" |
20 #include "base/location.h" | 20 #include "base/location.h" |
21 #include "base/memory/ptr_util.h" | |
21 #include "base/metrics/histogram_macros.h" | 22 #include "base/metrics/histogram_macros.h" |
22 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
23 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
24 #include "base/task_runner_util.h" | 25 #include "base/task_runner_util.h" |
25 #include "base/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
26 #include "base/trace_event/trace_event.h" | 27 #include "base/trace_event/trace_event.h" |
27 #include "build/build_config.h" | 28 #include "build/build_config.h" |
28 #include "cc/blink/web_layer_impl.h" | 29 #include "cc/blink/web_layer_impl.h" |
29 #include "cc/layers/video_layer.h" | 30 #include "cc/layers/video_layer.h" |
30 #include "media/audio/null_audio_sink.h" | 31 #include "media/audio/null_audio_sink.h" |
(...skipping 1042 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1073 #endif | 1074 #endif |
1074 | 1075 |
1075 // If we're not in an aggressive buffering state, tell the data source we have | 1076 // If we're not in an aggressive buffering state, tell the data source we have |
1076 // enough data so that it may release the connection. | 1077 // enough data so that it may release the connection. |
1077 if (buffering_strategy_ != | 1078 if (buffering_strategy_ != |
1078 MultibufferDataSource::BUFFERING_STRATEGY_AGGRESSIVE) { | 1079 MultibufferDataSource::BUFFERING_STRATEGY_AGGRESSIVE) { |
1079 if (data_source_) | 1080 if (data_source_) |
1080 data_source_->OnBufferingHaveEnough(true); | 1081 data_source_->OnBufferingHaveEnough(true); |
1081 } | 1082 } |
1082 | 1083 |
1083 ReportMemoryUsage(); | 1084 ReportMemoryUsage(OptionalMemoryPressureLevel()); |
1084 | 1085 |
1085 if (pending_suspend_resume_cycle_) { | 1086 if (pending_suspend_resume_cycle_) { |
1086 pending_suspend_resume_cycle_ = false; | 1087 pending_suspend_resume_cycle_ = false; |
1087 UpdatePlayState(); | 1088 UpdatePlayState(); |
1088 } | 1089 } |
1089 } | 1090 } |
1090 | 1091 |
1091 void WebMediaPlayerImpl::OnDemuxerOpened() { | 1092 void WebMediaPlayerImpl::OnDemuxerOpened() { |
1092 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1093 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1093 client_->mediaSourceOpened( | 1094 client_->mediaSourceOpened( |
1094 new WebMediaSourceImpl(chunk_demuxer_, media_log_)); | 1095 new WebMediaSourceImpl(chunk_demuxer_, media_log_)); |
1096 // base::Unretained is safe because |this| outlives memory_pressure_listener_. | |
1097 memory_pressure_listener_ = | |
1098 base::MakeUnique<base::MemoryPressureListener>(base::Bind( | |
1099 &WebMediaPlayerImpl::OnMemoryPressure, base::Unretained(this))); | |
1100 } | |
1101 | |
1102 void WebMediaPlayerImpl::OnMemoryPressure( | |
1103 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | |
1104 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
1105 ReportMemoryUsage(memory_pressure_level); | |
1095 } | 1106 } |
1096 | 1107 |
1097 void WebMediaPlayerImpl::OnError(PipelineStatus status) { | 1108 void WebMediaPlayerImpl::OnError(PipelineStatus status) { |
1098 DVLOG(1) << __func__; | 1109 DVLOG(1) << __func__; |
1099 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1110 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1100 DCHECK_NE(status, PIPELINE_OK); | 1111 DCHECK_NE(status, PIPELINE_OK); |
1101 | 1112 |
1102 if (suppress_destruction_errors_) | 1113 if (suppress_destruction_errors_) |
1103 return; | 1114 return; |
1104 | 1115 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1192 // to release unused network connections. | 1203 // to release unused network connections. |
1193 if (data_source_) | 1204 if (data_source_) |
1194 data_source_->OnBufferingHaveEnough(false); | 1205 data_source_->OnBufferingHaveEnough(false); |
1195 | 1206 |
1196 // Blink expects a timeChanged() in response to a seek(). | 1207 // Blink expects a timeChanged() in response to a seek(). |
1197 if (should_notify_time_changed_) | 1208 if (should_notify_time_changed_) |
1198 client_->timeChanged(); | 1209 client_->timeChanged(); |
1199 | 1210 |
1200 // Once we have enough, start reporting the total memory usage. We'll also | 1211 // Once we have enough, start reporting the total memory usage. We'll also |
1201 // report once playback starts. | 1212 // report once playback starts. |
1202 ReportMemoryUsage(); | 1213 ReportMemoryUsage(OptionalMemoryPressureLevel()); |
1203 | 1214 |
1204 // Report the amount of time it took to leave the underflow state. Don't | 1215 // Report the amount of time it took to leave the underflow state. Don't |
1205 // bother to report this for MSE playbacks since it's out of our control. | 1216 // bother to report this for MSE playbacks since it's out of our control. |
1206 if (underflow_timer_ && data_source_) { | 1217 if (underflow_timer_ && data_source_) { |
1207 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration", | 1218 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration", |
1208 underflow_timer_->Elapsed()); | 1219 underflow_timer_->Elapsed()); |
1209 underflow_timer_.reset(); | 1220 underflow_timer_.reset(); |
1210 } | 1221 } |
1211 } else { | 1222 } else { |
1212 // Buffering has underflowed. | 1223 // Buffering has underflowed. |
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1757 } | 1768 } |
1758 | 1769 |
1759 void WebMediaPlayerImpl::SetMemoryReportingState( | 1770 void WebMediaPlayerImpl::SetMemoryReportingState( |
1760 bool is_memory_reporting_enabled) { | 1771 bool is_memory_reporting_enabled) { |
1761 if (memory_usage_reporting_timer_.IsRunning() == | 1772 if (memory_usage_reporting_timer_.IsRunning() == |
1762 is_memory_reporting_enabled) { | 1773 is_memory_reporting_enabled) { |
1763 return; | 1774 return; |
1764 } | 1775 } |
1765 | 1776 |
1766 if (is_memory_reporting_enabled) { | 1777 if (is_memory_reporting_enabled) { |
1767 memory_usage_reporting_timer_.Start(FROM_HERE, | 1778 // base::Unretained is safe because |this| outlives |
1768 base::TimeDelta::FromSeconds(2), this, | 1779 // memory_usage_reporting_timer_. |
1769 &WebMediaPlayerImpl::ReportMemoryUsage); | 1780 memory_usage_reporting_timer_.Start( |
1781 FROM_HERE, base::TimeDelta::FromSeconds(2), | |
1782 base::Bind(&WebMediaPlayerImpl::ReportMemoryUsage, | |
1783 base::Unretained(this), OptionalMemoryPressureLevel())); | |
1770 } else { | 1784 } else { |
1771 memory_usage_reporting_timer_.Stop(); | 1785 memory_usage_reporting_timer_.Stop(); |
1772 ReportMemoryUsage(); | 1786 ReportMemoryUsage(OptionalMemoryPressureLevel()); |
1773 } | 1787 } |
1774 } | 1788 } |
1775 | 1789 |
1776 void WebMediaPlayerImpl::SetSuspendState(bool is_suspended) { | 1790 void WebMediaPlayerImpl::SetSuspendState(bool is_suspended) { |
1777 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1791 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1778 | 1792 |
1779 // Do not change the state after an error has occurred. | 1793 // Do not change the state after an error has occurred. |
1780 // TODO(sandersd): Update PipelineController to remove the need for this. | 1794 // TODO(sandersd): Update PipelineController to remove the need for this. |
1781 if (IsNetworkStateError(network_state_)) | 1795 if (IsNetworkStateError(network_state_)) |
1782 return; | 1796 return; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1903 } | 1917 } |
1904 | 1918 |
1905 // It's not critical if some cases where memory usage can change are missed, | 1919 // It's not critical if some cases where memory usage can change are missed, |
1906 // since media memory changes are usually gradual. | 1920 // since media memory changes are usually gradual. |
1907 result.is_memory_reporting_enabled = | 1921 result.is_memory_reporting_enabled = |
1908 can_play && !result.is_suspended && !paused_; | 1922 can_play && !result.is_suspended && !paused_; |
1909 | 1923 |
1910 return result; | 1924 return result; |
1911 } | 1925 } |
1912 | 1926 |
1913 void WebMediaPlayerImpl::ReportMemoryUsage() { | 1927 void WebMediaPlayerImpl::ReportMemoryUsage( |
1928 OptionalMemoryPressureLevel memory_pressure_level) { | |
1914 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1929 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1915 | 1930 |
1916 // About base::Unretained() usage below: We destroy |demuxer_| on the main | 1931 // About base::Unretained() usage below: We destroy |demuxer_| on the main |
1917 // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the | 1932 // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the |
1918 // media thread and waits for it to finish. Hence, the GetMemoryUsage() task | 1933 // media thread and waits for it to finish. Hence, the GetMemoryUsage() task |
1919 // posted here must finish earlier. | 1934 // posted here must finish earlier. |
1920 | 1935 |
1921 if (demuxer_) { | 1936 if (demuxer_) { |
1922 base::PostTaskAndReplyWithResult( | 1937 base::PostTaskAndReplyWithResult( |
1923 media_task_runner_.get(), FROM_HERE, | 1938 media_task_runner_.get(), FROM_HERE, |
1924 base::Bind(&Demuxer::GetMemoryUsage, base::Unretained(demuxer_.get())), | 1939 base::Bind(&Demuxer::GetMemoryUsage, base::Unretained(demuxer_.get())), |
1925 base::Bind(&WebMediaPlayerImpl::FinishMemoryUsageReport, AsWeakPtr())); | 1940 base::Bind(&WebMediaPlayerImpl::FinishMemoryUsageReport, AsWeakPtr(), |
1941 memory_pressure_level)); | |
1926 } else { | 1942 } else { |
1927 FinishMemoryUsageReport(0); | 1943 FinishMemoryUsageReport(memory_pressure_level, 0); |
1928 } | 1944 } |
1929 } | 1945 } |
1930 | 1946 |
1931 void WebMediaPlayerImpl::FinishMemoryUsageReport(int64_t demuxer_memory_usage) { | 1947 std::string MemoryPressureLevelToString( |
1948 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | |
1949 switch (memory_pressure_level) { | |
1950 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | |
1951 return "none"; | |
1952 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | |
1953 return "moderate"; | |
1954 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | |
1955 return "critical"; | |
1956 } | |
1957 NOTREACHED(); | |
1958 return ""; | |
1959 } | |
1960 | |
1961 void WebMediaPlayerImpl::FinishMemoryUsageReport( | |
1962 OptionalMemoryPressureLevel memory_pressure_level, | |
1963 int64_t demuxer_memory_usage) { | |
1932 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1964 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1933 | 1965 |
1934 const PipelineStatistics stats = pipeline_.GetStatistics(); | 1966 const PipelineStatistics stats = pipeline_.GetStatistics(); |
1967 const int64_t data_source_memory_usage = | |
1968 (data_source_ ? data_source_->GetMemoryUsage() : 0); | |
1935 const int64_t current_memory_usage = | 1969 const int64_t current_memory_usage = |
1936 stats.audio_memory_usage + stats.video_memory_usage + | 1970 stats.audio_memory_usage + stats.video_memory_usage + |
1937 (data_source_ ? data_source_->GetMemoryUsage() : 0) + | 1971 data_source_memory_usage + demuxer_memory_usage; |
1938 demuxer_memory_usage; | 1972 |
1973 std::string memory_pressure_log = ""; | |
1974 if (memory_pressure_level) { | |
1975 memory_pressure_log = " (Memory pressure: " + | |
1976 MemoryPressureLevelToString(*memory_pressure_level) + | |
1977 ")"; | |
1978 } | |
1939 | 1979 |
1940 // Note, this isn't entirely accurate, there may be VideoFrames held by the | 1980 // Note, this isn't entirely accurate, there may be VideoFrames held by the |
1941 // compositor or other resources that we're unaware of. | 1981 // compositor or other resources that we're unaware of. |
1942 | 1982 DVLOG(2) << "Memory Usage " << memory_pressure_log |
1943 DVLOG(2) << "Memory Usage -- Audio: " << stats.audio_memory_usage | 1983 << " -- Audio: " << stats.audio_memory_usage |
1944 << ", Video: " << stats.video_memory_usage << ", DataSource: " | 1984 << ", Video: " << stats.video_memory_usage |
1945 << (data_source_ ? data_source_->GetMemoryUsage() : 0) | 1985 << ", DataSource: " << data_source_memory_usage |
1946 << ", Demuxer: " << demuxer_memory_usage; | 1986 << ", Demuxer: " << demuxer_memory_usage; |
1947 | 1987 |
1948 const int64_t delta = current_memory_usage - last_reported_memory_usage_; | 1988 const int64_t delta = current_memory_usage - last_reported_memory_usage_; |
1949 last_reported_memory_usage_ = current_memory_usage; | 1989 last_reported_memory_usage_ = current_memory_usage; |
1950 adjust_allocated_memory_cb_.Run(delta); | 1990 adjust_allocated_memory_cb_.Run(delta); |
1991 | |
1992 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Audio", | |
1993 stats.audio_memory_usage / 1024); | |
1994 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Video", | |
1995 stats.video_memory_usage / 1024); | |
1996 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource", | |
1997 stats.video_memory_usage / 1024); | |
DaleCurtis
2016/12/14 19:33:23
Typo?
servolk
2016/12/14 19:47:11
Oops, yes, fixed.
| |
1998 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer", | |
1999 demuxer_memory_usage / 1024); | |
1951 } | 2000 } |
1952 | 2001 |
1953 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { | 2002 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { |
1954 // Only schedule the pause timer if we're playing and are suspended. | 2003 // Only schedule the pause timer if we're playing and are suspended. |
1955 if (paused_ || !pipeline_controller_.IsSuspended()) | 2004 if (paused_ || !pipeline_controller_.IsSuspended()) |
1956 return; | 2005 return; |
1957 | 2006 |
1958 #if defined(OS_ANDROID) | 2007 #if defined(OS_ANDROID) |
1959 // Remote players will be suspended and locally paused. | 2008 // Remote players will be suspended and locally paused. |
1960 if (isRemote()) | 2009 if (isRemote()) |
(...skipping 18 matching lines...) Expand all Loading... | |
1979 watch_time_reporter_->OnShown(); | 2028 watch_time_reporter_->OnShown(); |
1980 } | 2029 } |
1981 | 2030 |
1982 bool WebMediaPlayerImpl::IsHidden() const { | 2031 bool WebMediaPlayerImpl::IsHidden() const { |
1983 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 2032 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1984 | 2033 |
1985 return delegate_ && delegate_->IsHidden(); | 2034 return delegate_ && delegate_->IsHidden(); |
1986 } | 2035 } |
1987 | 2036 |
1988 } // namespace media | 2037 } // namespace media |
OLD | NEW |