| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/video_frame_compositor.h" | 5 #include "content/renderer/media/video_frame_compositor.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "cc/layers/video_frame_provider.h" | 10 #include "cc/layers/video_frame_provider.h" |
| 11 #include "content/renderer/render_thread_impl.h" | 11 #include "content/renderer/render_thread_impl.h" |
| 12 #include "media/base/video_frame.h" | 12 #include "media/base/video_frame.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 class VideoFrameCompositor::Internal : public cc::VideoFrameProvider { | 16 class VideoFrameCompositor::Internal : public cc::VideoFrameProvider { |
| 17 public: | 17 public: |
| 18 Internal( | 18 Internal( |
| 19 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner, | 19 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner, |
| 20 const base::Callback<void(gfx::Size)>& natural_size_changed_cb) | 20 const base::Callback<void(gfx::Size)>& natural_size_changed_cb) |
| 21 : compositor_task_runner_(compositor_task_runner), | 21 : compositor_task_runner_(compositor_task_runner), |
| 22 natural_size_changed_cb_(natural_size_changed_cb), | 22 natural_size_changed_cb_(natural_size_changed_cb), |
| 23 client_(NULL), | 23 client_(NULL), |
| 24 compositor_notify_finished_(true), | 24 compositor_notification_pending_(false), |
| 25 current_frame_composited_(false), | 25 frames_dropped_before_compositor_was_notified_(0) {} |
| 26 frames_dropped_before_composite_(0) {} | |
| 27 | 26 |
| 28 virtual ~Internal() { | 27 virtual ~Internal() { |
| 29 if (client_) | 28 if (client_) |
| 30 client_->StopUsingProvider(); | 29 client_->StopUsingProvider(); |
| 31 } | 30 } |
| 32 | 31 |
| 33 void DeleteSoon() { | 32 void DeleteSoon() { |
| 34 compositor_task_runner_->DeleteSoon(FROM_HERE, this); | 33 compositor_task_runner_->DeleteSoon(FROM_HERE, this); |
| 35 } | 34 } |
| 36 | 35 |
| 37 void UpdateCurrentFrame(const scoped_refptr<media::VideoFrame>& frame) { | 36 void UpdateCurrentFrame(const scoped_refptr<media::VideoFrame>& frame) { |
| 38 base::AutoLock auto_lock(lock_); | 37 base::AutoLock auto_lock(lock_); |
| 39 | 38 |
| 40 // Count frames as dropped if and only if we updated the frame but didn't | |
| 41 // finish notifying the compositor nor managed to composite the current | |
| 42 // frame. | |
| 43 if (!current_frame_composited_ && !compositor_notify_finished_ && | |
| 44 frames_dropped_before_composite_ < kuint32max) { | |
| 45 ++frames_dropped_before_composite_; | |
| 46 } | |
| 47 | |
| 48 if (current_frame_ && | 39 if (current_frame_ && |
| 49 current_frame_->natural_size() != frame->natural_size()) { | 40 current_frame_->natural_size() != frame->natural_size()) { |
| 50 natural_size_changed_cb_.Run(frame->natural_size()); | 41 natural_size_changed_cb_.Run(frame->natural_size()); |
| 51 } | 42 } |
| 52 | 43 |
| 53 current_frame_ = frame; | 44 current_frame_ = frame; |
| 54 current_frame_composited_ = false; | |
| 55 | 45 |
| 56 compositor_notify_finished_ = false; | 46 // Count frames as dropped if and only if we updated the frame but didn't |
| 47 // finish notifying the compositor for the previous frame. |
| 48 if (compositor_notification_pending_) { |
| 49 if (frames_dropped_before_compositor_was_notified_ < kuint32max) |
| 50 ++frames_dropped_before_compositor_was_notified_; |
| 51 return; |
| 52 } |
| 53 |
| 54 compositor_notification_pending_ = true; |
| 57 compositor_task_runner_->PostTask( | 55 compositor_task_runner_->PostTask( |
| 58 FROM_HERE, | 56 FROM_HERE, |
| 59 base::Bind(&Internal::NotifyCompositorOfNewFrame, | 57 base::Bind(&Internal::NotifyCompositorOfNewFrame, |
| 60 base::Unretained(this))); | 58 base::Unretained(this))); |
| 61 } | 59 } |
| 62 | 60 |
| 63 // If |frame_being_composited| is true the current frame will not be counted | 61 uint32 GetFramesDroppedBeforeCompositorWasNotified() { |
| 64 // as being dropped the next time UpdateCurrentFrame() is called. | |
| 65 scoped_refptr<media::VideoFrame> GetCurrentFrame( | |
| 66 bool frame_being_composited) { | |
| 67 base::AutoLock auto_lock(lock_); | 62 base::AutoLock auto_lock(lock_); |
| 68 if (frame_being_composited) | 63 return frames_dropped_before_compositor_was_notified_; |
| 69 current_frame_composited_ = false; | |
| 70 return current_frame_; | |
| 71 } | 64 } |
| 72 | 65 |
| 73 uint32 GetFramesDroppedBeforeComposite() { | 66 void SetFramesDroppedBeforeCompositorWasNotifiedForTesting( |
| 67 uint32 dropped_frames) { |
| 74 base::AutoLock auto_lock(lock_); | 68 base::AutoLock auto_lock(lock_); |
| 75 return frames_dropped_before_composite_; | 69 frames_dropped_before_compositor_was_notified_ = dropped_frames; |
| 76 } | |
| 77 | |
| 78 void SetFramesDroppedBeforeCompositeForTesting(uint32 dropped_frames) { | |
| 79 base::AutoLock auto_lock(lock_); | |
| 80 frames_dropped_before_composite_ = dropped_frames; | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 void NotifyCompositorOfNewFrame() { | |
| 85 base::AutoLock auto_lock(lock_); | |
| 86 compositor_notify_finished_ = true; | |
| 87 if (client_) | |
| 88 client_->DidReceiveFrame(); | |
| 89 } | 70 } |
| 90 | 71 |
| 91 // cc::VideoFrameProvider implementation. | 72 // cc::VideoFrameProvider implementation. |
| 92 virtual void SetVideoFrameProviderClient( | 73 virtual void SetVideoFrameProviderClient( |
| 93 cc::VideoFrameProvider::Client* client) OVERRIDE { | 74 cc::VideoFrameProvider::Client* client) OVERRIDE { |
| 94 if (client_) | 75 if (client_) |
| 95 client_->StopUsingProvider(); | 76 client_->StopUsingProvider(); |
| 96 client_ = client; | 77 client_ = client; |
| 97 } | 78 } |
| 98 | 79 |
| 99 virtual scoped_refptr<media::VideoFrame> GetCurrentFrame() OVERRIDE { | 80 virtual scoped_refptr<media::VideoFrame> GetCurrentFrame() OVERRIDE { |
| 100 return GetCurrentFrame(true); | 81 base::AutoLock auto_lock(lock_); |
| 82 return current_frame_; |
| 101 } | 83 } |
| 102 | 84 |
| 103 virtual void PutCurrentFrame(const scoped_refptr<media::VideoFrame>& frame) | 85 virtual void PutCurrentFrame(const scoped_refptr<media::VideoFrame>& frame) |
| 104 OVERRIDE {} | 86 OVERRIDE {} |
| 105 | 87 |
| 88 private: |
| 89 void NotifyCompositorOfNewFrame() { |
| 90 base::AutoLock auto_lock(lock_); |
| 91 compositor_notification_pending_ = false; |
| 92 if (client_) |
| 93 client_->DidReceiveFrame(); |
| 94 } |
| 95 |
| 106 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; | 96 scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_; |
| 107 base::Callback<void(gfx::Size)>natural_size_changed_cb_; | 97 base::Callback<void(gfx::Size)>natural_size_changed_cb_; |
| 108 | 98 |
| 109 cc::VideoFrameProvider::Client* client_; | 99 cc::VideoFrameProvider::Client* client_; |
| 110 | 100 |
| 111 base::Lock lock_; | 101 base::Lock lock_; |
| 112 scoped_refptr<media::VideoFrame> current_frame_; | 102 scoped_refptr<media::VideoFrame> current_frame_; |
| 113 bool compositor_notify_finished_; | 103 bool compositor_notification_pending_; |
| 114 bool current_frame_composited_; | 104 uint32 frames_dropped_before_compositor_was_notified_; |
| 115 uint32 frames_dropped_before_composite_; | |
| 116 | 105 |
| 117 DISALLOW_COPY_AND_ASSIGN(Internal); | 106 DISALLOW_COPY_AND_ASSIGN(Internal); |
| 118 }; | 107 }; |
| 119 | 108 |
| 120 VideoFrameCompositor::VideoFrameCompositor( | 109 VideoFrameCompositor::VideoFrameCompositor( |
| 121 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner, | 110 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner, |
| 122 const base::Callback<void(gfx::Size)>& natural_size_changed_cb) | 111 const base::Callback<void(gfx::Size)>& natural_size_changed_cb) |
| 123 : internal_(new Internal(compositor_task_runner, natural_size_changed_cb)) { | 112 : internal_(new Internal(compositor_task_runner, natural_size_changed_cb)) { |
| 124 } | 113 } |
| 125 | 114 |
| 126 VideoFrameCompositor::~VideoFrameCompositor() { | 115 VideoFrameCompositor::~VideoFrameCompositor() { |
| 127 internal_->DeleteSoon(); | 116 internal_->DeleteSoon(); |
| 128 } | 117 } |
| 129 | 118 |
| 130 cc::VideoFrameProvider* VideoFrameCompositor::GetVideoFrameProvider() { | 119 cc::VideoFrameProvider* VideoFrameCompositor::GetVideoFrameProvider() { |
| 131 return internal_; | 120 return internal_; |
| 132 } | 121 } |
| 133 | 122 |
| 134 void VideoFrameCompositor::UpdateCurrentFrame( | 123 void VideoFrameCompositor::UpdateCurrentFrame( |
| 135 const scoped_refptr<media::VideoFrame>& frame) { | 124 const scoped_refptr<media::VideoFrame>& frame) { |
| 136 internal_->UpdateCurrentFrame(frame); | 125 internal_->UpdateCurrentFrame(frame); |
| 137 } | 126 } |
| 138 | 127 |
| 139 scoped_refptr<media::VideoFrame> VideoFrameCompositor::GetCurrentFrame() { | 128 scoped_refptr<media::VideoFrame> VideoFrameCompositor::GetCurrentFrame() { |
| 140 return internal_->GetCurrentFrame(false); | 129 return internal_->GetCurrentFrame(); |
| 141 } | 130 } |
| 142 | 131 |
| 143 uint32 VideoFrameCompositor::GetFramesDroppedBeforeComposite() { | 132 uint32 VideoFrameCompositor::GetFramesDroppedBeforeCompositorWasNotified() { |
| 144 return internal_->GetFramesDroppedBeforeComposite(); | 133 return internal_->GetFramesDroppedBeforeCompositorWasNotified(); |
| 145 } | 134 } |
| 146 | 135 |
| 147 void VideoFrameCompositor::SetFramesDroppedBeforeCompositeForTesting( | 136 void |
| 137 VideoFrameCompositor::SetFramesDroppedBeforeCompositorWasNotifiedForTesting( |
| 148 uint32 dropped_frames) { | 138 uint32 dropped_frames) { |
| 149 internal_->SetFramesDroppedBeforeCompositeForTesting(dropped_frames); | 139 internal_->SetFramesDroppedBeforeCompositorWasNotifiedForTesting( |
| 140 dropped_frames); |
| 150 } | 141 } |
| 151 | 142 |
| 152 } // namespace content | 143 } // namespace content |
| OLD | NEW |