Disable PasswordManagerWebUITest.testOpenPasswordManager
[chromium-blink-merge.git] / remoting / host / video_frame_recorder.cc
blobc286e39bb47cb229885ce0aa84aa915b5cf0735b
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
3 // found in the LICENSE file.
5 #include "remoting/host/video_frame_recorder.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/stl_util.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "remoting/codec/video_encoder.h"
13 #include "remoting/proto/video.pb.h"
14 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
15 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
16 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
18 namespace remoting {
20 static int64_t FrameContentSize(const webrtc::DesktopFrame* frame) {
21 DCHECK_GT(frame->stride(), 0);
22 return frame->stride() * frame->size().height();
25 // VideoEncoder wrapper used to intercept frames passed to a real VideoEncoder.
26 class VideoFrameRecorder::RecordingVideoEncoder : public VideoEncoder {
27 public:
28 RecordingVideoEncoder(scoped_ptr<VideoEncoder> encoder,
29 scoped_refptr<base::TaskRunner> recorder_task_runner,
30 base::WeakPtr<VideoFrameRecorder> recorder);
32 base::WeakPtr<RecordingVideoEncoder> AsWeakPtr();
34 void set_enable_recording(bool enable_recording) {
35 DCHECK(!encoder_task_runner_.get() ||
36 encoder_task_runner_->BelongsToCurrentThread());
37 enable_recording_ = enable_recording;
40 // remoting::VideoEncoder interface.
41 void SetLosslessEncode(bool want_lossless) override;
42 void SetLosslessColor(bool want_lossless) override;
43 scoped_ptr<VideoPacket> Encode(const webrtc::DesktopFrame& frame) override;
45 private:
46 scoped_ptr<VideoEncoder> encoder_;
47 scoped_refptr<base::TaskRunner> recorder_task_runner_;
48 base::WeakPtr<VideoFrameRecorder> recorder_;
50 bool enable_recording_;
51 scoped_refptr<base::SingleThreadTaskRunner> encoder_task_runner_;
53 base::WeakPtrFactory<RecordingVideoEncoder> weak_factory_;
55 DISALLOW_COPY_AND_ASSIGN(RecordingVideoEncoder);
58 VideoFrameRecorder::RecordingVideoEncoder::RecordingVideoEncoder(
59 scoped_ptr<VideoEncoder> encoder,
60 scoped_refptr<base::TaskRunner> recorder_task_runner,
61 base::WeakPtr<VideoFrameRecorder> recorder)
62 : encoder_(encoder.Pass()),
63 recorder_task_runner_(recorder_task_runner),
64 recorder_(recorder),
65 enable_recording_(false),
66 weak_factory_(this) {
67 DCHECK(encoder_);
68 DCHECK(recorder_task_runner_.get());
71 base::WeakPtr<VideoFrameRecorder::RecordingVideoEncoder>
72 VideoFrameRecorder::RecordingVideoEncoder::AsWeakPtr() {
73 return weak_factory_.GetWeakPtr();
76 void VideoFrameRecorder::RecordingVideoEncoder::SetLosslessEncode(
77 bool want_lossless) {
78 encoder_->SetLosslessEncode(want_lossless);
81 void VideoFrameRecorder::RecordingVideoEncoder::SetLosslessColor(
82 bool want_lossless) {
83 encoder_->SetLosslessColor(want_lossless);
86 scoped_ptr<VideoPacket> VideoFrameRecorder::RecordingVideoEncoder::Encode(
87 const webrtc::DesktopFrame& frame) {
88 // If this is the first Encode() then store the TaskRunner and inform the
89 // VideoFrameRecorder so it can post set_enable_recording() on it.
90 if (!encoder_task_runner_.get()) {
91 encoder_task_runner_ = base::ThreadTaskRunnerHandle::Get();
92 recorder_task_runner_->PostTask(FROM_HERE,
93 base::Bind(&VideoFrameRecorder::SetEncoderTaskRunner,
94 recorder_,
95 encoder_task_runner_));
98 DCHECK(encoder_task_runner_->BelongsToCurrentThread());
100 if (enable_recording_) {
101 // Copy the frame and post it to the VideoFrameRecorder to store.
102 scoped_ptr<webrtc::DesktopFrame> frame_copy(
103 new webrtc::BasicDesktopFrame(frame.size()));
104 *frame_copy->mutable_updated_region() = frame.updated_region();
105 frame_copy->set_dpi(frame.dpi());
106 frame_copy->CopyPixelsFrom(frame.data(),
107 frame.stride(),
108 webrtc::DesktopRect::MakeSize(frame.size()));
109 recorder_task_runner_->PostTask(FROM_HERE,
110 base::Bind(&VideoFrameRecorder::RecordFrame,
111 recorder_,
112 base::Passed(&frame_copy)));
115 return encoder_->Encode(frame);
118 VideoFrameRecorder::VideoFrameRecorder()
119 : content_bytes_(0),
120 max_content_bytes_(0),
121 enable_recording_(false),
122 weak_factory_(this) {
125 VideoFrameRecorder::~VideoFrameRecorder() {
126 DetachVideoEncoderWrapper();
129 scoped_ptr<VideoEncoder> VideoFrameRecorder::WrapVideoEncoder(
130 scoped_ptr<VideoEncoder> encoder) {
131 DCHECK(!encoder_task_runner_.get());
132 DCHECK(!caller_task_runner_.get());
133 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get();
135 scoped_ptr<RecordingVideoEncoder> recording_encoder(
136 new RecordingVideoEncoder(encoder.Pass(),
137 caller_task_runner_,
138 weak_factory_.GetWeakPtr()));
139 recording_encoder_ = recording_encoder->AsWeakPtr();
141 return recording_encoder.Pass();
144 void VideoFrameRecorder::DetachVideoEncoderWrapper() {
145 DCHECK(!caller_task_runner_.get() ||
146 caller_task_runner_->BelongsToCurrentThread());
148 // Immediately detach the wrapper from this recorder.
149 weak_factory_.InvalidateWeakPtrs();
151 // Clean up any pending recorded frames.
152 STLDeleteElements(&recorded_frames_);
153 content_bytes_ = 0;
155 // Tell the wrapper to stop recording and posting frames to us.
156 if (encoder_task_runner_.get()) {
157 encoder_task_runner_->PostTask(FROM_HERE,
158 base::Bind(&RecordingVideoEncoder::set_enable_recording,
159 recording_encoder_, false));
162 // Detach this recorder from the calling and encode threads.
163 caller_task_runner_ = nullptr;
164 encoder_task_runner_ = nullptr;
167 void VideoFrameRecorder::SetEnableRecording(bool enable_recording) {
168 DCHECK(!caller_task_runner_.get() ||
169 caller_task_runner_->BelongsToCurrentThread());
171 if (enable_recording_ == enable_recording) {
172 return;
174 enable_recording_ = enable_recording;
176 if (encoder_task_runner_.get()) {
177 encoder_task_runner_->PostTask(FROM_HERE,
178 base::Bind(&RecordingVideoEncoder::set_enable_recording,
179 recording_encoder_,
180 enable_recording_));
184 void VideoFrameRecorder::SetMaxContentBytes(int64_t max_content_bytes) {
185 DCHECK(!caller_task_runner_.get() ||
186 caller_task_runner_->BelongsToCurrentThread());
187 DCHECK_GE(max_content_bytes, 0);
189 max_content_bytes_ = max_content_bytes;
192 scoped_ptr<webrtc::DesktopFrame> VideoFrameRecorder::NextFrame() {
193 DCHECK(caller_task_runner_->BelongsToCurrentThread());
195 scoped_ptr<webrtc::DesktopFrame> frame;
196 if (!recorded_frames_.empty()) {
197 frame.reset(recorded_frames_.front());
198 recorded_frames_.pop_front();
199 content_bytes_ -= FrameContentSize(frame.get());
200 DCHECK_GE(content_bytes_, 0);
203 return frame.Pass();
206 void VideoFrameRecorder::SetEncoderTaskRunner(
207 scoped_refptr<base::TaskRunner> task_runner) {
208 DCHECK(caller_task_runner_->BelongsToCurrentThread());
209 DCHECK(!encoder_task_runner_.get());
210 DCHECK(task_runner.get());
212 encoder_task_runner_ = task_runner;
214 // If the caller already enabled recording, inform the recording encoder.
215 if (enable_recording_ && encoder_task_runner_.get()) {
216 encoder_task_runner_->PostTask(FROM_HERE,
217 base::Bind(&RecordingVideoEncoder::set_enable_recording,
218 recording_encoder_,
219 enable_recording_));
223 void VideoFrameRecorder::RecordFrame(scoped_ptr<webrtc::DesktopFrame> frame) {
224 DCHECK(caller_task_runner_->BelongsToCurrentThread());
226 int64_t frame_bytes = FrameContentSize(frame.get());
227 DCHECK_GE(frame_bytes, 0);
229 // Purge existing frames until there is space for the new one.
230 while (content_bytes_ + frame_bytes > max_content_bytes_ &&
231 !recorded_frames_.empty()) {
232 scoped_ptr<webrtc::DesktopFrame> drop_frame(recorded_frames_.front());
233 recorded_frames_.pop_front();
234 content_bytes_ -= FrameContentSize(drop_frame.get());
235 DCHECK_GE(content_bytes_, 0);
238 // If the frame is still too big, ignore it.
239 if (content_bytes_ + frame_bytes > max_content_bytes_) {
240 return;
243 // Store the frame and update the content byte count.
244 recorded_frames_.push_back(frame.release());
245 content_bytes_ += frame_bytes;
248 } // namespace remoting