Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(476)

Side by Side Diff: remoting/host/video_frame_recorder.cc

Issue 468613002: Readability review. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "remoting/host/video_frame_recorder.h" 5 #include "remoting/host/video_frame_recorder.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 "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/thread_task_runner_handle.h" 11 #include "base/thread_task_runner_handle.h"
12 #include "remoting/codec/video_encoder.h" 12 #include "remoting/codec/video_encoder.h"
13 #include "remoting/proto/video.pb.h" 13 #include "remoting/proto/video.pb.h"
14 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" 14 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
15 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" 15 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
16 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h" 16 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
17 17
18 namespace remoting { 18 namespace remoting {
19 19
20 static int64_t FrameContentSize(const webrtc::DesktopFrame* frame) { 20 static int64_t FrameContentSize(const webrtc::DesktopFrame* frame) {
21 DCHECK_GT(frame->stride(), 0); 21 DCHECK_GT(frame->stride(), 0);
22 return frame->stride() * frame->size().height(); 22 return frame->stride() * frame->size().height();
23 } 23 }
24 24
25 // VideoEncoder wrapper used to intercept frames passed to a real VideoEncoder. 25 // VideoEncoder wrapper used to intercept frames passed to a real VideoEncoder.
26 class VideoFrameRecorder::RecordingVideoEncoder : public VideoEncoder { 26 class VideoFrameRecorder::RecordingVideoEncoder : public VideoEncoder {
27 public: 27 public:
28 RecordingVideoEncoder(scoped_ptr<VideoEncoder> encoder, 28 RecordingVideoEncoder(scoped_ptr<VideoEncoder> encoder,
29 scoped_refptr<base::TaskRunner> recorder_task_runner, 29 scoped_refptr<base::TaskRunner> recorder_task_runner,
30 base::WeakPtr<VideoFrameRecorder> recorder) 30 base::WeakPtr<VideoFrameRecorder> recorder);
31 : encoder_(encoder.Pass()),
32 recorder_task_runner_(recorder_task_runner),
33 recorder_(recorder),
34 enable_recording_(false),
35 weak_factory_(this) {
36 DCHECK(encoder_);
37 DCHECK(recorder_task_runner_.get());
38 }
39 31
40 base::WeakPtr<RecordingVideoEncoder> AsWeakPtr() { 32 base::WeakPtr<RecordingVideoEncoder> AsWeakPtr();
41 return weak_factory_.GetWeakPtr();
42 }
43 33
44 void SetEnableRecording(bool enable_recording) { 34 void set_enable_recording(bool enable_recording) {
45 DCHECK(!encoder_task_runner_.get() || 35 DCHECK(!encoder_task_runner_.get() ||
46 encoder_task_runner_->BelongsToCurrentThread()); 36 encoder_task_runner_->BelongsToCurrentThread());
47 enable_recording_ = enable_recording; 37 enable_recording_ = enable_recording;
48 } 38 }
49 39
50 // remoting::VideoEncoder interface. 40 // remoting::VideoEncoder interface.
51 virtual void SetLosslessEncode(bool want_lossless) OVERRIDE { 41 virtual void SetLosslessEncode(bool want_lossless) OVERRIDE;
52 encoder_->SetLosslessEncode(want_lossless); 42 virtual void SetLosslessColor(bool want_lossless) OVERRIDE;
53 }
54 virtual void SetLosslessColor(bool want_lossless) OVERRIDE {
55 encoder_->SetLosslessColor(want_lossless);
56 }
57 virtual scoped_ptr<VideoPacket> Encode( 43 virtual scoped_ptr<VideoPacket> Encode(
58 const webrtc::DesktopFrame& frame) OVERRIDE { 44 const webrtc::DesktopFrame& frame) OVERRIDE;
59 // If this is the first Encode() then store the TaskRunner and inform the
60 // VideoFrameRecorder so it can post SetEnableRecording() on it.
61 if (!encoder_task_runner_.get()) {
62 encoder_task_runner_ = base::ThreadTaskRunnerHandle::Get();
63 recorder_task_runner_->PostTask(FROM_HERE,
64 base::Bind(&VideoFrameRecorder::SetEncoderTaskRunner,
65 recorder_,
66 encoder_task_runner_));
67 }
68
69 DCHECK(encoder_task_runner_->BelongsToCurrentThread());
70
71 if (enable_recording_) {
72 // Copy the frame and post it to the VideoFrameRecorder to store.
73 scoped_ptr<webrtc::DesktopFrame> frame_copy(
74 new webrtc::BasicDesktopFrame(frame.size()));
75 *frame_copy->mutable_updated_region() = frame.updated_region();
76 frame_copy->set_dpi(frame.dpi());
77 frame_copy->CopyPixelsFrom(frame.data(),
78 frame.stride(),
79 webrtc::DesktopRect::MakeSize(frame.size()));
80 recorder_task_runner_->PostTask(FROM_HERE,
81 base::Bind(&VideoFrameRecorder::RecordFrame,
82 recorder_,
83 base::Passed(&frame_copy)));
84 }
85
86 return encoder_->Encode(frame);
87 }
88 45
89 private: 46 private:
90 scoped_ptr<VideoEncoder> encoder_; 47 scoped_ptr<VideoEncoder> encoder_;
91 scoped_refptr<base::TaskRunner> recorder_task_runner_; 48 scoped_refptr<base::TaskRunner> recorder_task_runner_;
92 base::WeakPtr<VideoFrameRecorder> recorder_; 49 base::WeakPtr<VideoFrameRecorder> recorder_;
93 50
94 bool enable_recording_; 51 bool enable_recording_;
95 scoped_refptr<base::SingleThreadTaskRunner> encoder_task_runner_; 52 scoped_refptr<base::SingleThreadTaskRunner> encoder_task_runner_;
96 53
97 base::WeakPtrFactory<RecordingVideoEncoder> weak_factory_; 54 base::WeakPtrFactory<RecordingVideoEncoder> weak_factory_;
98 55
99 DISALLOW_COPY_AND_ASSIGN(RecordingVideoEncoder); 56 DISALLOW_COPY_AND_ASSIGN(RecordingVideoEncoder);
100 }; 57 };
101 58
59 VideoFrameRecorder::RecordingVideoEncoder::RecordingVideoEncoder(
60 scoped_ptr<VideoEncoder> encoder,
61 scoped_refptr<base::TaskRunner> recorder_task_runner,
62 base::WeakPtr<VideoFrameRecorder> recorder)
63 : encoder_(encoder.Pass()),
64 recorder_task_runner_(recorder_task_runner),
65 recorder_(recorder),
66 enable_recording_(false),
67 weak_factory_(this) {
68 DCHECK(encoder_);
69 DCHECK(recorder_task_runner_.get());
70 }
71
72 base::WeakPtr<VideoFrameRecorder::RecordingVideoEncoder>
73 VideoFrameRecorder::RecordingVideoEncoder::AsWeakPtr() {
74 return weak_factory_.GetWeakPtr();
75 }
76
77 void VideoFrameRecorder::RecordingVideoEncoder::SetLosslessEncode(
78 bool want_lossless) {
79 encoder_->SetLosslessEncode(want_lossless);
80 }
81
82 void VideoFrameRecorder::RecordingVideoEncoder::SetLosslessColor(
83 bool want_lossless) {
84 encoder_->SetLosslessColor(want_lossless);
85 }
86
87 scoped_ptr<VideoPacket> VideoFrameRecorder::RecordingVideoEncoder::Encode(
88 const webrtc::DesktopFrame& frame) {
89 // If this is the first Encode() then store the TaskRunner and inform the
90 // VideoFrameRecorder so it can post set_enable_recording() on it.
91 if (!encoder_task_runner_.get()) {
92 encoder_task_runner_ = base::ThreadTaskRunnerHandle::Get();
93 recorder_task_runner_->PostTask(FROM_HERE,
94 base::Bind(&VideoFrameRecorder::SetEncoderTaskRunner,
95 recorder_,
96 encoder_task_runner_));
97 }
98
99 DCHECK(encoder_task_runner_->BelongsToCurrentThread());
100
101 if (enable_recording_) {
102 // Copy the frame and post it to the VideoFrameRecorder to store.
103 scoped_ptr<webrtc::DesktopFrame> frame_copy(
104 new webrtc::BasicDesktopFrame(frame.size()));
105 *frame_copy->mutable_updated_region() = frame.updated_region();
106 frame_copy->set_dpi(frame.dpi());
107 frame_copy->CopyPixelsFrom(frame.data(),
108 frame.stride(),
109 webrtc::DesktopRect::MakeSize(frame.size()));
110 recorder_task_runner_->PostTask(FROM_HERE,
111 base::Bind(&VideoFrameRecorder::RecordFrame,
112 recorder_,
113 base::Passed(&frame_copy)));
114 }
115
116 return encoder_->Encode(frame);
117 }
118
102 VideoFrameRecorder::VideoFrameRecorder() 119 VideoFrameRecorder::VideoFrameRecorder()
103 : content_bytes_(0), 120 : content_bytes_(0),
104 max_content_bytes_(0), 121 max_content_bytes_(0),
105 enable_recording_(false), 122 enable_recording_(false),
106 weak_factory_(this) { 123 weak_factory_(this) {
107 } 124 }
108 125
109 VideoFrameRecorder::~VideoFrameRecorder() { 126 VideoFrameRecorder::~VideoFrameRecorder() {
110 DetachVideoEncoderWrapper(); 127 DetachVideoEncoderWrapper();
111 } 128 }
(...skipping 20 matching lines...) Expand all
132 // Immediately detach the wrapper from this recorder. 149 // Immediately detach the wrapper from this recorder.
133 weak_factory_.InvalidateWeakPtrs(); 150 weak_factory_.InvalidateWeakPtrs();
134 151
135 // Clean up any pending recorded frames. 152 // Clean up any pending recorded frames.
136 STLDeleteElements(&recorded_frames_); 153 STLDeleteElements(&recorded_frames_);
137 content_bytes_ = 0; 154 content_bytes_ = 0;
138 155
139 // Tell the wrapper to stop recording and posting frames to us. 156 // Tell the wrapper to stop recording and posting frames to us.
140 if (encoder_task_runner_.get()) { 157 if (encoder_task_runner_.get()) {
141 encoder_task_runner_->PostTask(FROM_HERE, 158 encoder_task_runner_->PostTask(FROM_HERE,
142 base::Bind(&RecordingVideoEncoder::SetEnableRecording, 159 base::Bind(&RecordingVideoEncoder::set_enable_recording,
143 recording_encoder_, false)); 160 recording_encoder_, false));
144 } 161 }
145 162
146 // Detach this recorder from the calling and encode threads. 163 // Detach this recorder from the calling and encode threads.
147 caller_task_runner_ = NULL; 164 caller_task_runner_ = NULL;
148 encoder_task_runner_ = NULL; 165 encoder_task_runner_ = NULL;
149 } 166 }
150 167
151 void VideoFrameRecorder::SetEnableRecording(bool enable_recording) { 168 void VideoFrameRecorder::SetEnableRecording(bool enable_recording) {
152 DCHECK(!caller_task_runner_.get() || 169 DCHECK(!caller_task_runner_.get() ||
153 caller_task_runner_->BelongsToCurrentThread()); 170 caller_task_runner_->BelongsToCurrentThread());
154 171
155 if (enable_recording_ == enable_recording) { 172 if (enable_recording_ == enable_recording) {
156 return; 173 return;
157 } 174 }
158 enable_recording_ = enable_recording; 175 enable_recording_ = enable_recording;
159 176
160 if (encoder_task_runner_.get()) { 177 if (encoder_task_runner_.get()) {
161 encoder_task_runner_->PostTask(FROM_HERE, 178 encoder_task_runner_->PostTask(FROM_HERE,
162 base::Bind(&RecordingVideoEncoder::SetEnableRecording, 179 base::Bind(&RecordingVideoEncoder::set_enable_recording,
163 recording_encoder_, 180 recording_encoder_,
164 enable_recording_)); 181 enable_recording_));
165 } 182 }
166 } 183 }
167 184
168 void VideoFrameRecorder::SetMaxContentBytes(int64_t max_content_bytes) { 185 void VideoFrameRecorder::SetMaxContentBytes(int64_t max_content_bytes) {
169 DCHECK(!caller_task_runner_.get() || 186 DCHECK(!caller_task_runner_.get() ||
170 caller_task_runner_->BelongsToCurrentThread()); 187 caller_task_runner_->BelongsToCurrentThread());
171 DCHECK_GE(max_content_bytes, 0); 188 DCHECK_GE(max_content_bytes, 0);
172 189
(...skipping 18 matching lines...) Expand all
191 scoped_refptr<base::TaskRunner> task_runner) { 208 scoped_refptr<base::TaskRunner> task_runner) {
192 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 209 DCHECK(caller_task_runner_->BelongsToCurrentThread());
193 DCHECK(!encoder_task_runner_.get()); 210 DCHECK(!encoder_task_runner_.get());
194 DCHECK(task_runner.get()); 211 DCHECK(task_runner.get());
195 212
196 encoder_task_runner_ = task_runner; 213 encoder_task_runner_ = task_runner;
197 214
198 // If the caller already enabled recording, inform the recording encoder. 215 // If the caller already enabled recording, inform the recording encoder.
199 if (enable_recording_ && encoder_task_runner_.get()) { 216 if (enable_recording_ && encoder_task_runner_.get()) {
200 encoder_task_runner_->PostTask(FROM_HERE, 217 encoder_task_runner_->PostTask(FROM_HERE,
201 base::Bind(&RecordingVideoEncoder::SetEnableRecording, 218 base::Bind(&RecordingVideoEncoder::set_enable_recording,
202 recording_encoder_, 219 recording_encoder_,
203 enable_recording_)); 220 enable_recording_));
204 } 221 }
205 } 222 }
206 223
207 void VideoFrameRecorder::RecordFrame(scoped_ptr<webrtc::DesktopFrame> frame) { 224 void VideoFrameRecorder::RecordFrame(scoped_ptr<webrtc::DesktopFrame> frame) {
208 DCHECK(caller_task_runner_->BelongsToCurrentThread()); 225 DCHECK(caller_task_runner_->BelongsToCurrentThread());
209 226
210 int64_t frame_bytes = FrameContentSize(frame.get()); 227 int64_t frame_bytes = FrameContentSize(frame.get());
211 DCHECK_GE(frame_bytes, 0); 228 DCHECK_GE(frame_bytes, 0);
(...skipping 11 matching lines...) Expand all
223 if (content_bytes_ + frame_bytes > max_content_bytes_) { 240 if (content_bytes_ + frame_bytes > max_content_bytes_) {
224 return; 241 return;
225 } 242 }
226 243
227 // Store the frame and update the content byte count. 244 // Store the frame and update the content byte count.
228 recorded_frames_.push_back(frame.release()); 245 recorded_frames_.push_back(frame.release());
229 content_bytes_ += frame_bytes; 246 content_bytes_ += frame_bytes;
230 } 247 }
231 248
232 } // namespace remoting 249 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/video_frame_recorder.h ('k') | remoting/host/video_frame_recorder_host_extension.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698