OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "remoting/host/video_frame_recorder.h" |
| 6 |
| 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" |
| 17 |
| 18 namespace remoting { |
| 19 |
| 20 static int64_t FrameContentSize(const webrtc::DesktopFrame* frame) { |
| 21 DCHECK_GT(frame->stride(), 0); |
| 22 return frame->stride() * frame->size().height(); |
| 23 } |
| 24 |
| 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) |
| 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_); |
| 38 } |
| 39 |
| 40 base::WeakPtr<RecordingVideoEncoder> AsWeakPtr() { |
| 41 return weak_factory_.GetWeakPtr(); |
| 42 } |
| 43 |
| 44 void SetEnableRecording(bool enable_recording) { |
| 45 DCHECK(!encoder_task_runner_ || |
| 46 encoder_task_runner_->BelongsToCurrentThread()); |
| 47 enable_recording_ = enable_recording; |
| 48 } |
| 49 |
| 50 // remoting::VideoEncoder interface. |
| 51 virtual void SetLosslessEncode(bool want_lossless) OVERRIDE { |
| 52 encoder_->SetLosslessEncode(want_lossless); |
| 53 } |
| 54 virtual void SetLosslessColor(bool want_lossless) OVERRIDE { |
| 55 encoder_->SetLosslessColor(want_lossless); |
| 56 } |
| 57 virtual scoped_ptr<VideoPacket> Encode( |
| 58 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_) { |
| 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 |
| 89 private: |
| 90 scoped_ptr<VideoEncoder> encoder_; |
| 91 scoped_refptr<base::TaskRunner> recorder_task_runner_; |
| 92 base::WeakPtr<VideoFrameRecorder> recorder_; |
| 93 |
| 94 bool enable_recording_; |
| 95 scoped_refptr<base::SingleThreadTaskRunner> encoder_task_runner_; |
| 96 |
| 97 base::WeakPtrFactory<RecordingVideoEncoder> weak_factory_; |
| 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(RecordingVideoEncoder); |
| 100 }; |
| 101 |
| 102 VideoFrameRecorder::VideoFrameRecorder() |
| 103 : content_bytes_(0), |
| 104 max_content_bytes_(0), |
| 105 enable_recording_(false), |
| 106 weak_factory_(this) { |
| 107 } |
| 108 |
| 109 VideoFrameRecorder::~VideoFrameRecorder() { |
| 110 SetEnableRecording(false); |
| 111 STLDeleteElements(&recorded_frames_); |
| 112 } |
| 113 |
| 114 scoped_ptr<VideoEncoder> VideoFrameRecorder::WrapVideoEncoder( |
| 115 scoped_ptr<VideoEncoder> encoder) { |
| 116 DCHECK(!caller_task_runner_); |
| 117 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 118 |
| 119 scoped_ptr<RecordingVideoEncoder> recording_encoder( |
| 120 new RecordingVideoEncoder(encoder.Pass(), |
| 121 caller_task_runner_, |
| 122 weak_factory_.GetWeakPtr())); |
| 123 recording_encoder_ = recording_encoder->AsWeakPtr(); |
| 124 |
| 125 return recording_encoder.PassAs<VideoEncoder>(); |
| 126 } |
| 127 |
| 128 void VideoFrameRecorder::SetEnableRecording(bool enable_recording) { |
| 129 DCHECK(!caller_task_runner_ || caller_task_runner_->BelongsToCurrentThread()); |
| 130 |
| 131 if (enable_recording_ == enable_recording) { |
| 132 return; |
| 133 } |
| 134 enable_recording_ = enable_recording; |
| 135 |
| 136 if (encoder_task_runner_) { |
| 137 encoder_task_runner_->PostTask(FROM_HERE, |
| 138 base::Bind(&RecordingVideoEncoder::SetEnableRecording, |
| 139 recording_encoder_, |
| 140 enable_recording_)); |
| 141 } |
| 142 } |
| 143 |
| 144 void VideoFrameRecorder::SetMaxContentBytes(int64_t max_content_bytes) { |
| 145 DCHECK(!caller_task_runner_ || caller_task_runner_->BelongsToCurrentThread()); |
| 146 DCHECK_GE(max_content_bytes, 0); |
| 147 |
| 148 max_content_bytes_ = max_content_bytes; |
| 149 } |
| 150 |
| 151 scoped_ptr<webrtc::DesktopFrame> VideoFrameRecorder::NextFrame() { |
| 152 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 153 |
| 154 scoped_ptr<webrtc::DesktopFrame> frame; |
| 155 if (!recorded_frames_.empty()) { |
| 156 frame.reset(recorded_frames_.front()); |
| 157 recorded_frames_.pop_front(); |
| 158 content_bytes_ -= FrameContentSize(frame.get()); |
| 159 DCHECK_GE(content_bytes_, 0); |
| 160 } |
| 161 |
| 162 return frame.Pass(); |
| 163 } |
| 164 |
| 165 void VideoFrameRecorder::SetEncoderTaskRunner( |
| 166 scoped_refptr<base::TaskRunner> task_runner) { |
| 167 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 168 DCHECK(!encoder_task_runner_); |
| 169 DCHECK(task_runner); |
| 170 |
| 171 encoder_task_runner_ = task_runner; |
| 172 |
| 173 // If the caller already enabled recording, inform the recording encoder. |
| 174 if (enable_recording_ && encoder_task_runner_) { |
| 175 encoder_task_runner_->PostTask(FROM_HERE, |
| 176 base::Bind(&RecordingVideoEncoder::SetEnableRecording, |
| 177 recording_encoder_, |
| 178 enable_recording_)); |
| 179 } |
| 180 } |
| 181 |
| 182 void VideoFrameRecorder::RecordFrame(scoped_ptr<webrtc::DesktopFrame> frame) { |
| 183 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 184 |
| 185 int64_t frame_bytes = FrameContentSize(frame.get()); |
| 186 DCHECK_GE(frame_bytes, 0); |
| 187 |
| 188 // Purge existing frames until there is space for the new one. |
| 189 while (content_bytes_ + frame_bytes > max_content_bytes_ && |
| 190 !recorded_frames_.empty()) { |
| 191 scoped_ptr<webrtc::DesktopFrame> drop_frame(recorded_frames_.front()); |
| 192 recorded_frames_.pop_front(); |
| 193 content_bytes_ -= FrameContentSize(drop_frame.get()); |
| 194 DCHECK_GE(content_bytes_, 0); |
| 195 } |
| 196 |
| 197 // If the frame is still too big, ignore it. |
| 198 if (content_bytes_ + frame_bytes > max_content_bytes_) { |
| 199 return; |
| 200 } |
| 201 |
| 202 // Store the frame and update the content byte count. |
| 203 recorded_frames_.push_back(frame.release()); |
| 204 content_bytes_ += frame_bytes; |
| 205 } |
| 206 |
| 207 } // namespace remoting |
OLD | NEW |