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 "content/renderer/media/android/webmediaplayer_android.h" | 5 #include "content/renderer/media/android/webmediaplayer_android.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <limits> | 9 #include <limits> |
10 | 10 |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 enable_texture_copy_(enable_texture_copy), | 175 enable_texture_copy_(enable_texture_copy), |
176 suppress_deleting_texture_(false), | 176 suppress_deleting_texture_(false), |
177 playback_completed_(false), | 177 playback_completed_(false), |
178 volume_(1.0), | 178 volume_(1.0), |
179 volume_multiplier_(1.0), | 179 volume_multiplier_(1.0), |
180 weak_factory_(this) { | 180 weak_factory_(this) { |
181 DCHECK(player_manager_); | 181 DCHECK(player_manager_); |
182 | 182 |
183 DCHECK(main_thread_checker_.CalledOnValidThread()); | 183 DCHECK(main_thread_checker_.CalledOnValidThread()); |
184 | 184 |
185 if (delegate_) | 185 if (delegate_) { |
186 delegate_id_ = delegate_->AddObserver(this); | 186 delegate_id_ = delegate_->AddObserver(this); |
| 187 delegate_->SetIdle(delegate_id_, true); |
| 188 } |
187 | 189 |
188 player_id_ = player_manager_->RegisterMediaPlayer(this); | 190 player_id_ = player_manager_->RegisterMediaPlayer(this); |
189 | 191 |
190 TryCreateStreamTextureProxyIfNeeded(); | 192 TryCreateStreamTextureProxyIfNeeded(); |
191 interpolator_.SetUpperBound(base::TimeDelta()); | 193 interpolator_.SetUpperBound(base::TimeDelta()); |
192 } | 194 } |
193 | 195 |
194 WebMediaPlayerAndroid::~WebMediaPlayerAndroid() { | 196 WebMediaPlayerAndroid::~WebMediaPlayerAndroid() { |
195 DCHECK(main_thread_checker_.CalledOnValidThread()); | 197 DCHECK(main_thread_checker_.CalledOnValidThread()); |
196 SetVideoFrameProviderClient(NULL); | 198 SetVideoFrameProviderClient(NULL); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 return false; | 290 return false; |
289 } | 291 } |
290 | 292 |
291 void WebMediaPlayerAndroid::play() { | 293 void WebMediaPlayerAndroid::play() { |
292 DCHECK(main_thread_checker_.CalledOnValidThread()); | 294 DCHECK(main_thread_checker_.CalledOnValidThread()); |
293 | 295 |
294 if (hasVideo() && player_manager_->render_frame()->IsHidden()) { | 296 if (hasVideo() && player_manager_->render_frame()->IsHidden()) { |
295 bool can_video_play_in_background = | 297 bool can_video_play_in_background = |
296 base::CommandLine::ForCurrentProcess()->HasSwitch( | 298 base::CommandLine::ForCurrentProcess()->HasSwitch( |
297 switches::kDisableMediaSuspend) || | 299 switches::kDisableMediaSuspend) || |
298 (IsBackgroundVideoCandidate() && | 300 (IsBackgroundVideoCandidate() && delegate_ && |
299 delegate_ && delegate_->IsPlayingBackgroundVideo()); | 301 delegate_->IsBackgroundVideoPlaybackUnlocked()); |
300 if (!can_video_play_in_background) { | 302 if (!can_video_play_in_background) { |
301 is_play_pending_ = true; | 303 is_play_pending_ = true; |
302 return; | 304 return; |
303 } | 305 } |
304 } | 306 } |
305 is_play_pending_ = false; | 307 is_play_pending_ = false; |
306 | 308 |
307 TryCreateStreamTextureProxyIfNeeded(); | 309 TryCreateStreamTextureProxyIfNeeded(); |
308 // There is no need to establish the surface texture peer for fullscreen | 310 // There is no need to establish the surface texture peer for fullscreen |
309 // video. | 311 // video. |
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 | 818 |
817 // Lazily allocate compositing layer. | 819 // Lazily allocate compositing layer. |
818 if (!video_weblayer_) { | 820 if (!video_weblayer_) { |
819 video_weblayer_.reset(new cc_blink::WebLayerImpl( | 821 video_weblayer_.reset(new cc_blink::WebLayerImpl( |
820 cc::VideoLayer::Create(this, media::VIDEO_ROTATION_0))); | 822 cc::VideoLayer::Create(this, media::VIDEO_ROTATION_0))); |
821 client_->setWebLayer(video_weblayer_.get()); | 823 client_->setWebLayer(video_weblayer_.get()); |
822 | 824 |
823 // If we're paused after we receive metadata for the first time, tell the | 825 // If we're paused after we receive metadata for the first time, tell the |
824 // delegate we can now be safely suspended due to inactivity if a subsequent | 826 // delegate we can now be safely suspended due to inactivity if a subsequent |
825 // play event does not occur. | 827 // play event does not occur. |
826 if (paused() && delegate_) | 828 if (paused() && delegate_) { |
827 delegate_->DidPause(delegate_id_, false); | 829 delegate_->DidPause(delegate_id_); |
| 830 delegate_->SetIdle(delegate_id_, true); |
| 831 } |
828 } | 832 } |
829 } | 833 } |
830 | 834 |
831 void WebMediaPlayerAndroid::OnTimeUpdate(base::TimeDelta current_timestamp, | 835 void WebMediaPlayerAndroid::OnTimeUpdate(base::TimeDelta current_timestamp, |
832 base::TimeTicks current_time_ticks) { | 836 base::TimeTicks current_time_ticks) { |
833 DCHECK(main_thread_checker_.CalledOnValidThread()); | 837 DCHECK(main_thread_checker_.CalledOnValidThread()); |
834 | 838 |
835 if (seeking()) | 839 if (seeking()) |
836 return; | 840 return; |
837 | 841 |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1192 interpolator_.StartInterpolating(); | 1196 interpolator_.StartInterpolating(); |
1193 else | 1197 else |
1194 interpolator_.StopInterpolating(); | 1198 interpolator_.StopInterpolating(); |
1195 | 1199 |
1196 if (delegate_) { | 1200 if (delegate_) { |
1197 if (is_playing) { | 1201 if (is_playing) { |
1198 // We must specify either video or audio to the delegate, but neither may | 1202 // We must specify either video or audio to the delegate, but neither may |
1199 // be known at this point -- there are no video only containers, so only | 1203 // be known at this point -- there are no video only containers, so only |
1200 // send audio if we know for sure its audio. The browser side player will | 1204 // send audio if we know for sure its audio. The browser side player will |
1201 // fill in the correct value later for media sessions. | 1205 // fill in the correct value later for media sessions. |
1202 delegate_->DidPlay(delegate_id_, hasVideo(), !hasVideo(), isRemote(), | 1206 if (isRemote()) { |
1203 media::DurationToMediaContentType(duration_)); | 1207 delegate_->PlayerGone(delegate_id_); |
| 1208 } else { |
| 1209 delegate_->DidPlay(delegate_id_, !hasVideo(), hasVideo(), |
| 1210 media::DurationToMediaContentType(duration_)); |
| 1211 } |
| 1212 delegate_->SetIdle(delegate_id_, false); |
1204 } else { | 1213 } else { |
1205 // Even if OnPlaybackComplete() has not been called yet, Blink may have | 1214 // Even if OnPlaybackComplete() has not been called yet, Blink may have |
1206 // already fired the ended event based on current time relative to | 1215 // already fired the ended event based on current time relative to |
1207 // duration -- so we need to check both possibilities here. | 1216 // duration -- so we need to check both possibilities here. |
1208 delegate_->DidPause(delegate_id_, | 1217 if (playback_completed_ || currentTime() >= duration()) { |
1209 playback_completed_ || currentTime() >= duration()); | 1218 delegate_->PlayerGone(delegate_id_); |
| 1219 } else { |
| 1220 delegate_->DidPause(delegate_id_); |
| 1221 } |
| 1222 delegate_->SetIdle(delegate_id_, true); |
1210 } | 1223 } |
1211 } | 1224 } |
1212 } | 1225 } |
1213 | 1226 |
1214 void WebMediaPlayerAndroid::OnHidden() { | 1227 void WebMediaPlayerAndroid::OnFrameHidden() { |
1215 // Pause audible video preserving its session. | 1228 // Pause audible video preserving its session. |
1216 if (hasVideo() && IsBackgroundVideoCandidate() && !paused()) { | 1229 if (hasVideo() && IsBackgroundVideoCandidate() && !paused()) { |
1217 Pause(false); | 1230 Pause(false); |
1218 is_play_pending_ = true; | 1231 is_play_pending_ = true; |
1219 return; | 1232 return; |
1220 } | 1233 } |
1221 | 1234 |
1222 OnSuspendRequested(false); | 1235 OnIdleTimeout(); |
1223 } | 1236 } |
1224 | 1237 |
1225 void WebMediaPlayerAndroid::OnShown() { | 1238 void WebMediaPlayerAndroid::OnFrameClosed() { |
| 1239 SuspendAndReleaseResources(); |
| 1240 } |
| 1241 |
| 1242 void WebMediaPlayerAndroid::OnFrameShown() { |
1226 if (is_play_pending_) | 1243 if (is_play_pending_) |
1227 play(); | 1244 play(); |
1228 } | 1245 } |
1229 | 1246 |
1230 bool WebMediaPlayerAndroid::OnSuspendRequested(bool must_suspend) { | 1247 void WebMediaPlayerAndroid::OnIdleTimeout() { |
1231 if (!must_suspend && | 1248 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
1232 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
1233 switches::kDisableMediaSuspend)) { | 1249 switches::kDisableMediaSuspend)) { |
1234 return true; | 1250 return; |
1235 } | 1251 } |
1236 | 1252 |
1237 // If we're idle or playing video, pause and release resources; audio only | 1253 // If we're playing video or ended, pause and release resources; audio only |
1238 // players are allowed to continue unless indicated otherwise by the call. | 1254 // players are allowed to continue. |
1239 if (must_suspend || (paused() && playback_completed_) || | 1255 if ((hasVideo() && !IsBackgroundVideoCandidate()) || |
1240 (hasVideo() && !IsBackgroundVideoCandidate())) { | 1256 (paused() && playback_completed_)) { |
1241 SuspendAndReleaseResources(); | 1257 SuspendAndReleaseResources(); |
1242 } | 1258 } |
1243 | |
1244 return true; | |
1245 } | 1259 } |
1246 | 1260 |
1247 void WebMediaPlayerAndroid::OnPlay() { | 1261 void WebMediaPlayerAndroid::OnPlay() { |
1248 play(); | 1262 play(); |
1249 client_->playbackStateChanged(); | 1263 client_->playbackStateChanged(); |
1250 } | 1264 } |
1251 | 1265 |
1252 void WebMediaPlayerAndroid::OnPause() { | 1266 void WebMediaPlayerAndroid::OnPause() { |
1253 pause(); | 1267 pause(); |
1254 client_->playbackStateChanged(); | 1268 client_->playbackStateChanged(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1315 | 1329 |
1316 bool WebMediaPlayerAndroid::IsBackgroundVideoCandidate() const { | 1330 bool WebMediaPlayerAndroid::IsBackgroundVideoCandidate() const { |
1317 DCHECK(hasVideo()); | 1331 DCHECK(hasVideo()); |
1318 | 1332 |
1319 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 1333 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
1320 switches::kDisableMediaSuspend)) { | 1334 switches::kDisableMediaSuspend)) { |
1321 return false; | 1335 return false; |
1322 } | 1336 } |
1323 | 1337 |
1324 return base::FeatureList::IsEnabled(media::kResumeBackgroundVideo) && | 1338 return base::FeatureList::IsEnabled(media::kResumeBackgroundVideo) && |
1325 hasAudio() && !isRemote() && delegate_ && delegate_->IsHidden(); | 1339 hasAudio() && !isRemote() && delegate_ && delegate_->IsFrameHidden() && |
| 1340 !delegate_->IsFrameClosed(); |
1326 } | 1341 } |
1327 | 1342 |
1328 } // namespace content | 1343 } // namespace content |
OLD | NEW |