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(MaybeMemPressureLvl()); |
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 // Using base::Unretained(this) is safe here, because MemoryPressureListener | |
1097 // object is owned by this (WMPI::mem_pressure_listener_). | |
watk
2016/12/13 23:34:31
"because the".
Or if you want to reword it: "base
servolk
2016/12/14 00:07:23
Done.
| |
1098 mem_pressure_listener_ = | |
1099 base::WrapUnique(new base::MemoryPressureListener(base::Bind( | |
watk
2016/12/13 23:34:31
Prefer MakeUnique(). (Unless there's a reason you
servolk
2016/12/14 00:07:23
Done.
| |
1100 &WebMediaPlayerImpl::OnMemoryPressure, base::Unretained(this)))); | |
1101 } | |
1102 | |
1103 void WebMediaPlayerImpl::OnMemoryPressure( | |
1104 base::MemoryPressureListener::MemoryPressureLevel lvl) { | |
1105 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
1106 ReportMemoryUsage(lvl); | |
1095 } | 1107 } |
1096 | 1108 |
1097 void WebMediaPlayerImpl::OnError(PipelineStatus status) { | 1109 void WebMediaPlayerImpl::OnError(PipelineStatus status) { |
1098 DVLOG(1) << __func__; | 1110 DVLOG(1) << __func__; |
1099 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1111 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1100 DCHECK_NE(status, PIPELINE_OK); | 1112 DCHECK_NE(status, PIPELINE_OK); |
1101 | 1113 |
1102 if (suppress_destruction_errors_) | 1114 if (suppress_destruction_errors_) |
1103 return; | 1115 return; |
1104 | 1116 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1192 // to release unused network connections. | 1204 // to release unused network connections. |
1193 if (data_source_) | 1205 if (data_source_) |
1194 data_source_->OnBufferingHaveEnough(false); | 1206 data_source_->OnBufferingHaveEnough(false); |
1195 | 1207 |
1196 // Blink expects a timeChanged() in response to a seek(). | 1208 // Blink expects a timeChanged() in response to a seek(). |
1197 if (should_notify_time_changed_) | 1209 if (should_notify_time_changed_) |
1198 client_->timeChanged(); | 1210 client_->timeChanged(); |
1199 | 1211 |
1200 // Once we have enough, start reporting the total memory usage. We'll also | 1212 // Once we have enough, start reporting the total memory usage. We'll also |
1201 // report once playback starts. | 1213 // report once playback starts. |
1202 ReportMemoryUsage(); | 1214 ReportMemoryUsage(MaybeMemPressureLvl()); |
1203 | 1215 |
1204 // Report the amount of time it took to leave the underflow state. Don't | 1216 // 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. | 1217 // bother to report this for MSE playbacks since it's out of our control. |
1206 if (underflow_timer_ && data_source_) { | 1218 if (underflow_timer_ && data_source_) { |
1207 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration", | 1219 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration", |
1208 underflow_timer_->Elapsed()); | 1220 underflow_timer_->Elapsed()); |
1209 underflow_timer_.reset(); | 1221 underflow_timer_.reset(); |
1210 } | 1222 } |
1211 } else { | 1223 } else { |
1212 // Buffering has underflowed. | 1224 // Buffering has underflowed. |
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1757 } | 1769 } |
1758 | 1770 |
1759 void WebMediaPlayerImpl::SetMemoryReportingState( | 1771 void WebMediaPlayerImpl::SetMemoryReportingState( |
1760 bool is_memory_reporting_enabled) { | 1772 bool is_memory_reporting_enabled) { |
1761 if (memory_usage_reporting_timer_.IsRunning() == | 1773 if (memory_usage_reporting_timer_.IsRunning() == |
1762 is_memory_reporting_enabled) { | 1774 is_memory_reporting_enabled) { |
1763 return; | 1775 return; |
1764 } | 1776 } |
1765 | 1777 |
1766 if (is_memory_reporting_enabled) { | 1778 if (is_memory_reporting_enabled) { |
1767 memory_usage_reporting_timer_.Start(FROM_HERE, | 1779 memory_usage_reporting_timer_.Start( |
1768 base::TimeDelta::FromSeconds(2), this, | 1780 FROM_HERE, base::TimeDelta::FromSeconds(2), this, |
1769 &WebMediaPlayerImpl::ReportMemoryUsage); | 1781 &WebMediaPlayerImpl::ReportMemoryUsageOnTimer); |
1770 } else { | 1782 } else { |
1771 memory_usage_reporting_timer_.Stop(); | 1783 memory_usage_reporting_timer_.Stop(); |
1772 ReportMemoryUsage(); | 1784 ReportMemoryUsage(MaybeMemPressureLvl()); |
1773 } | 1785 } |
1774 } | 1786 } |
1775 | 1787 |
1776 void WebMediaPlayerImpl::SetSuspendState(bool is_suspended) { | 1788 void WebMediaPlayerImpl::SetSuspendState(bool is_suspended) { |
1777 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1789 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1778 | 1790 |
1779 // Do not change the state after an error has occurred. | 1791 // Do not change the state after an error has occurred. |
1780 // TODO(sandersd): Update PipelineController to remove the need for this. | 1792 // TODO(sandersd): Update PipelineController to remove the need for this. |
1781 if (IsNetworkStateError(network_state_)) | 1793 if (IsNetworkStateError(network_state_)) |
1782 return; | 1794 return; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1903 } | 1915 } |
1904 | 1916 |
1905 // It's not critical if some cases where memory usage can change are missed, | 1917 // It's not critical if some cases where memory usage can change are missed, |
1906 // since media memory changes are usually gradual. | 1918 // since media memory changes are usually gradual. |
1907 result.is_memory_reporting_enabled = | 1919 result.is_memory_reporting_enabled = |
1908 can_play && !result.is_suspended && !paused_; | 1920 can_play && !result.is_suspended && !paused_; |
1909 | 1921 |
1910 return result; | 1922 return result; |
1911 } | 1923 } |
1912 | 1924 |
1913 void WebMediaPlayerImpl::ReportMemoryUsage() { | 1925 void WebMediaPlayerImpl::ReportMemoryUsageOnTimer() { |
watk
2016/12/13 23:34:31
Can you bind the nullopt into the callback and del
servolk
2016/12/14 00:07:23
This is passed into timer.Start (see https://cs.ch
servolk
2016/12/14 00:11:23
Ah, wait, despite the weird Start method at https:
servolk
2016/12/14 00:15:37
Done.
| |
1926 ReportMemoryUsage(MaybeMemPressureLvl()); | |
1927 } | |
1928 | |
1929 void WebMediaPlayerImpl::ReportMemoryUsage( | |
1930 MaybeMemPressureLvl maybe_mem_pressure_lvl) { | |
1914 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1931 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1915 | 1932 |
1916 // About base::Unretained() usage below: We destroy |demuxer_| on the main | 1933 // About base::Unretained() usage below: We destroy |demuxer_| on the main |
1917 // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the | 1934 // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the |
1918 // media thread and waits for it to finish. Hence, the GetMemoryUsage() task | 1935 // media thread and waits for it to finish. Hence, the GetMemoryUsage() task |
1919 // posted here must finish earlier. | 1936 // posted here must finish earlier. |
1920 | 1937 |
1921 if (demuxer_) { | 1938 if (demuxer_) { |
1922 base::PostTaskAndReplyWithResult( | 1939 base::PostTaskAndReplyWithResult( |
1923 media_task_runner_.get(), FROM_HERE, | 1940 media_task_runner_.get(), FROM_HERE, |
1924 base::Bind(&Demuxer::GetMemoryUsage, base::Unretained(demuxer_.get())), | 1941 base::Bind(&Demuxer::GetMemoryUsage, base::Unretained(demuxer_.get())), |
1925 base::Bind(&WebMediaPlayerImpl::FinishMemoryUsageReport, AsWeakPtr())); | 1942 base::Bind(&WebMediaPlayerImpl::FinishMemoryUsageReport, AsWeakPtr(), |
1943 maybe_mem_pressure_lvl)); | |
1926 } else { | 1944 } else { |
1927 FinishMemoryUsageReport(0); | 1945 FinishMemoryUsageReport(maybe_mem_pressure_lvl, 0); |
1928 } | 1946 } |
1929 } | 1947 } |
1930 | 1948 |
1931 void WebMediaPlayerImpl::FinishMemoryUsageReport(int64_t demuxer_memory_usage) { | 1949 std::string MaybeMemPressureToLogString( |
1950 const base::Optional<base::MemoryPressureListener::MemoryPressureLevel>& | |
watk
2016/12/13 23:34:31
Use the type alias?
servolk
2016/12/14 00:07:23
I could, but the type alias is currently private i
watk
2016/12/14 00:34:05
Ah, didn't notice this was a non-member function.
servolk
2016/12/14 02:34:33
Acknowledged.
| |
1951 maybe_mem_pressure_lvl) { | |
1952 if (maybe_mem_pressure_lvl) { | |
1953 switch (*maybe_mem_pressure_lvl) { | |
1954 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | |
1955 return "none"; | |
1956 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | |
1957 return "moderate"; | |
1958 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | |
1959 return "critical"; | |
1960 } | |
1961 } | |
1962 return ""; | |
1963 } | |
1964 | |
1965 void WebMediaPlayerImpl::FinishMemoryUsageReport( | |
1966 MaybeMemPressureLvl maybe_mem_pressure_lvl, | |
1967 int64_t demuxer_memory_usage) { | |
1932 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1968 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1933 | 1969 |
1934 const PipelineStatistics stats = pipeline_.GetStatistics(); | 1970 const PipelineStatistics stats = pipeline_.GetStatistics(); |
1971 const int64_t data_source_mem_usage = | |
watk
2016/12/13 23:34:31
"memory_usage" for consistency with other variable
servolk
2016/12/14 00:07:23
Done.
| |
1972 (data_source_ ? data_source_->GetMemoryUsage() : 0); | |
1935 const int64_t current_memory_usage = | 1973 const int64_t current_memory_usage = |
1936 stats.audio_memory_usage + stats.video_memory_usage + | 1974 stats.audio_memory_usage + stats.video_memory_usage + |
1937 (data_source_ ? data_source_->GetMemoryUsage() : 0) + | 1975 data_source_mem_usage + demuxer_memory_usage; |
1938 demuxer_memory_usage; | 1976 |
1977 std::string mem_pressure_log = | |
1978 MaybeMemPressureToLogString(maybe_mem_pressure_lvl); | |
1979 if (mem_pressure_log != "") | |
1980 mem_pressure_log = " (Mem pressure: " + mem_pressure_log + ")"; | |
watk
2016/12/13 23:34:31
Do this inside MaybeMemPressureToLogString and inl
servolk
2016/12/14 00:07:23
This would make MaybeMemPressureToLogString less r
| |
1939 | 1981 |
1940 // Note, this isn't entirely accurate, there may be VideoFrames held by the | 1982 // Note, this isn't entirely accurate, there may be VideoFrames held by the |
1941 // compositor or other resources that we're unaware of. | 1983 // compositor or other resources that we're unaware of. |
1942 | 1984 DVLOG(2) << "Memory Usage " << mem_pressure_log |
1943 DVLOG(2) << "Memory Usage -- Audio: " << stats.audio_memory_usage | 1985 << " -- Audio: " << stats.audio_memory_usage |
1944 << ", Video: " << stats.video_memory_usage << ", DataSource: " | 1986 << ", Video: " << stats.video_memory_usage |
1945 << (data_source_ ? data_source_->GetMemoryUsage() : 0) | 1987 << ", DataSource: " << data_source_mem_usage |
1946 << ", Demuxer: " << demuxer_memory_usage; | 1988 << ", Demuxer: " << demuxer_memory_usage; |
1947 | 1989 |
1948 const int64_t delta = current_memory_usage - last_reported_memory_usage_; | 1990 const int64_t delta = current_memory_usage - last_reported_memory_usage_; |
1949 last_reported_memory_usage_ = current_memory_usage; | 1991 last_reported_memory_usage_ = current_memory_usage; |
1950 adjust_allocated_memory_cb_.Run(delta); | 1992 adjust_allocated_memory_cb_.Run(delta); |
1993 | |
1994 if (maybe_mem_pressure_lvl && | |
DaleCurtis
2016/12/12 23:49:48
Hmm, why? Why not just always report the average m
servolk
2016/12/12 23:56:12
ReportMemoryUsage and FinishMemoryUsageReport are
DaleCurtis
2016/12/13 00:04:07
Reporting UMA is mostly just updating a static val
| |
1995 *maybe_mem_pressure_lvl >= | |
1996 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE) { | |
1997 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.AudioKb", | |
1998 stats.audio_memory_usage / 1024); | |
1999 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.VideoKb", | |
2000 stats.video_memory_usage / 1024); | |
2001 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSourceKb", | |
2002 stats.video_memory_usage / 1024); | |
2003 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DemuxerKb", | |
2004 demuxer_memory_usage / 1024); | |
2005 } | |
1951 } | 2006 } |
1952 | 2007 |
1953 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { | 2008 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { |
1954 // Only schedule the pause timer if we're playing and are suspended. | 2009 // Only schedule the pause timer if we're playing and are suspended. |
1955 if (paused_ || !pipeline_controller_.IsSuspended()) | 2010 if (paused_ || !pipeline_controller_.IsSuspended()) |
1956 return; | 2011 return; |
1957 | 2012 |
1958 #if defined(OS_ANDROID) | 2013 #if defined(OS_ANDROID) |
1959 // Remote players will be suspended and locally paused. | 2014 // Remote players will be suspended and locally paused. |
1960 if (isRemote()) | 2015 if (isRemote()) |
(...skipping 18 matching lines...) Expand all Loading... | |
1979 watch_time_reporter_->OnShown(); | 2034 watch_time_reporter_->OnShown(); |
1980 } | 2035 } |
1981 | 2036 |
1982 bool WebMediaPlayerImpl::IsHidden() const { | 2037 bool WebMediaPlayerImpl::IsHidden() const { |
1983 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 2038 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1984 | 2039 |
1985 return delegate_ && delegate_->IsHidden(); | 2040 return delegate_ && delegate_->IsHidden(); |
1986 } | 2041 } |
1987 | 2042 |
1988 } // namespace media | 2043 } // namespace media |
OLD | NEW |