1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/video_frame_recorder.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/thread_task_runner_handle.h"
#include "remoting/codec/video_encoder.h"
#include "remoting/proto/video.pb.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
namespace remoting {
static int64_t FrameContentSize(const webrtc::DesktopFrame* frame) {
DCHECK_GT(frame->stride(), 0);
return frame->stride() * frame->size().height();
}
// VideoEncoder wrapper used to intercept frames passed to a real VideoEncoder.
class VideoFrameRecorder::RecordingVideoEncoder : public VideoEncoder {
public:
RecordingVideoEncoder(scoped_ptr<VideoEncoder> encoder,
scoped_refptr<base::TaskRunner> recorder_task_runner,
base::WeakPtr<VideoFrameRecorder> recorder);
base::WeakPtr<RecordingVideoEncoder> AsWeakPtr();
void set_enable_recording(bool enable_recording) {
DCHECK(!encoder_task_runner_.get() ||
encoder_task_runner_->BelongsToCurrentThread());
enable_recording_ = enable_recording;
}
// remoting::VideoEncoder interface.
virtual void SetLosslessEncode(bool want_lossless) override;
virtual void SetLosslessColor(bool want_lossless) override;
virtual scoped_ptr<VideoPacket> Encode(
const webrtc::DesktopFrame& frame) override;
private:
scoped_ptr<VideoEncoder> encoder_;
scoped_refptr<base::TaskRunner> recorder_task_runner_;
base::WeakPtr<VideoFrameRecorder> recorder_;
bool enable_recording_;
scoped_refptr<base::SingleThreadTaskRunner> encoder_task_runner_;
base::WeakPtrFactory<RecordingVideoEncoder> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(RecordingVideoEncoder);
};
VideoFrameRecorder::RecordingVideoEncoder::RecordingVideoEncoder(
scoped_ptr<VideoEncoder> encoder,
scoped_refptr<base::TaskRunner> recorder_task_runner,
base::WeakPtr<VideoFrameRecorder> recorder)
: encoder_(encoder.Pass()),
recorder_task_runner_(recorder_task_runner),
recorder_(recorder),
enable_recording_(false),
weak_factory_(this) {
DCHECK(encoder_);
DCHECK(recorder_task_runner_.get());
}
base::WeakPtr<VideoFrameRecorder::RecordingVideoEncoder>
VideoFrameRecorder::RecordingVideoEncoder::AsWeakPtr() {
return weak_factory_.GetWeakPtr();
}
void VideoFrameRecorder::RecordingVideoEncoder::SetLosslessEncode(
bool want_lossless) {
encoder_->SetLosslessEncode(want_lossless);
}
void VideoFrameRecorder::RecordingVideoEncoder::SetLosslessColor(
bool want_lossless) {
encoder_->SetLosslessColor(want_lossless);
}
scoped_ptr<VideoPacket> VideoFrameRecorder::RecordingVideoEncoder::Encode(
const webrtc::DesktopFrame& frame) {
// If this is the first Encode() then store the TaskRunner and inform the
// VideoFrameRecorder so it can post set_enable_recording() on it.
if (!encoder_task_runner_.get()) {
encoder_task_runner_ = base::ThreadTaskRunnerHandle::Get();
recorder_task_runner_->PostTask(FROM_HERE,
base::Bind(&VideoFrameRecorder::SetEncoderTaskRunner,
recorder_,
encoder_task_runner_));
}
DCHECK(encoder_task_runner_->BelongsToCurrentThread());
if (enable_recording_) {
// Copy the frame and post it to the VideoFrameRecorder to store.
scoped_ptr<webrtc::DesktopFrame> frame_copy(
new webrtc::BasicDesktopFrame(frame.size()));
*frame_copy->mutable_updated_region() = frame.updated_region();
frame_copy->set_dpi(frame.dpi());
frame_copy->CopyPixelsFrom(frame.data(),
frame.stride(),
webrtc::DesktopRect::MakeSize(frame.size()));
recorder_task_runner_->PostTask(FROM_HERE,
base::Bind(&VideoFrameRecorder::RecordFrame,
recorder_,
base::Passed(&frame_copy)));
}
return encoder_->Encode(frame);
}
VideoFrameRecorder::VideoFrameRecorder()
: content_bytes_(0),
max_content_bytes_(0),
enable_recording_(false),
weak_factory_(this) {
}
VideoFrameRecorder::~VideoFrameRecorder() {
DetachVideoEncoderWrapper();
}
scoped_ptr<VideoEncoder> VideoFrameRecorder::WrapVideoEncoder(
scoped_ptr<VideoEncoder> encoder) {
DCHECK(!encoder_task_runner_.get());
DCHECK(!caller_task_runner_.get());
caller_task_runner_ = base::ThreadTaskRunnerHandle::Get();
scoped_ptr<RecordingVideoEncoder> recording_encoder(
new RecordingVideoEncoder(encoder.Pass(),
caller_task_runner_,
weak_factory_.GetWeakPtr()));
recording_encoder_ = recording_encoder->AsWeakPtr();
return recording_encoder.Pass();
}
void VideoFrameRecorder::DetachVideoEncoderWrapper() {
DCHECK(!caller_task_runner_.get() ||
caller_task_runner_->BelongsToCurrentThread());
// Immediately detach the wrapper from this recorder.
weak_factory_.InvalidateWeakPtrs();
// Clean up any pending recorded frames.
STLDeleteElements(&recorded_frames_);
content_bytes_ = 0;
// Tell the wrapper to stop recording and posting frames to us.
if (encoder_task_runner_.get()) {
encoder_task_runner_->PostTask(FROM_HERE,
base::Bind(&RecordingVideoEncoder::set_enable_recording,
recording_encoder_, false));
}
// Detach this recorder from the calling and encode threads.
caller_task_runner_ = NULL;
encoder_task_runner_ = NULL;
}
void VideoFrameRecorder::SetEnableRecording(bool enable_recording) {
DCHECK(!caller_task_runner_.get() ||
caller_task_runner_->BelongsToCurrentThread());
if (enable_recording_ == enable_recording) {
return;
}
enable_recording_ = enable_recording;
if (encoder_task_runner_.get()) {
encoder_task_runner_->PostTask(FROM_HERE,
base::Bind(&RecordingVideoEncoder::set_enable_recording,
recording_encoder_,
enable_recording_));
}
}
void VideoFrameRecorder::SetMaxContentBytes(int64_t max_content_bytes) {
DCHECK(!caller_task_runner_.get() ||
caller_task_runner_->BelongsToCurrentThread());
DCHECK_GE(max_content_bytes, 0);
max_content_bytes_ = max_content_bytes;
}
scoped_ptr<webrtc::DesktopFrame> VideoFrameRecorder::NextFrame() {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
scoped_ptr<webrtc::DesktopFrame> frame;
if (!recorded_frames_.empty()) {
frame.reset(recorded_frames_.front());
recorded_frames_.pop_front();
content_bytes_ -= FrameContentSize(frame.get());
DCHECK_GE(content_bytes_, 0);
}
return frame.Pass();
}
void VideoFrameRecorder::SetEncoderTaskRunner(
scoped_refptr<base::TaskRunner> task_runner) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
DCHECK(!encoder_task_runner_.get());
DCHECK(task_runner.get());
encoder_task_runner_ = task_runner;
// If the caller already enabled recording, inform the recording encoder.
if (enable_recording_ && encoder_task_runner_.get()) {
encoder_task_runner_->PostTask(FROM_HERE,
base::Bind(&RecordingVideoEncoder::set_enable_recording,
recording_encoder_,
enable_recording_));
}
}
void VideoFrameRecorder::RecordFrame(scoped_ptr<webrtc::DesktopFrame> frame) {
DCHECK(caller_task_runner_->BelongsToCurrentThread());
int64_t frame_bytes = FrameContentSize(frame.get());
DCHECK_GE(frame_bytes, 0);
// Purge existing frames until there is space for the new one.
while (content_bytes_ + frame_bytes > max_content_bytes_ &&
!recorded_frames_.empty()) {
scoped_ptr<webrtc::DesktopFrame> drop_frame(recorded_frames_.front());
recorded_frames_.pop_front();
content_bytes_ -= FrameContentSize(drop_frame.get());
DCHECK_GE(content_bytes_, 0);
}
// If the frame is still too big, ignore it.
if (content_bytes_ + frame_bytes > max_content_bytes_) {
return;
}
// Store the frame and update the content byte count.
recorded_frames_.push_back(frame.release());
content_bytes_ += frame_bytes;
}
} // namespace remoting
|