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 "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" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 }; | 100 }; |
101 | 101 |
102 VideoFrameRecorder::VideoFrameRecorder() | 102 VideoFrameRecorder::VideoFrameRecorder() |
103 : content_bytes_(0), | 103 : content_bytes_(0), |
104 max_content_bytes_(0), | 104 max_content_bytes_(0), |
105 enable_recording_(false), | 105 enable_recording_(false), |
106 weak_factory_(this) { | 106 weak_factory_(this) { |
107 } | 107 } |
108 | 108 |
109 VideoFrameRecorder::~VideoFrameRecorder() { | 109 VideoFrameRecorder::~VideoFrameRecorder() { |
110 SetEnableRecording(false); | 110 DetachVideoEncoderWrapper(); |
111 STLDeleteElements(&recorded_frames_); | |
112 } | 111 } |
113 | 112 |
114 scoped_ptr<VideoEncoder> VideoFrameRecorder::WrapVideoEncoder( | 113 scoped_ptr<VideoEncoder> VideoFrameRecorder::WrapVideoEncoder( |
115 scoped_ptr<VideoEncoder> encoder) { | 114 scoped_ptr<VideoEncoder> encoder) { |
| 115 DCHECK(!encoder_task_runner_); |
116 DCHECK(!caller_task_runner_); | 116 DCHECK(!caller_task_runner_); |
117 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 117 caller_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
118 | 118 |
119 scoped_ptr<RecordingVideoEncoder> recording_encoder( | 119 scoped_ptr<RecordingVideoEncoder> recording_encoder( |
120 new RecordingVideoEncoder(encoder.Pass(), | 120 new RecordingVideoEncoder(encoder.Pass(), |
121 caller_task_runner_, | 121 caller_task_runner_, |
122 weak_factory_.GetWeakPtr())); | 122 weak_factory_.GetWeakPtr())); |
123 recording_encoder_ = recording_encoder->AsWeakPtr(); | 123 recording_encoder_ = recording_encoder->AsWeakPtr(); |
124 | 124 |
125 return recording_encoder.PassAs<VideoEncoder>(); | 125 return recording_encoder.PassAs<VideoEncoder>(); |
126 } | 126 } |
127 | 127 |
| 128 void VideoFrameRecorder::DetachVideoEncoderWrapper() { |
| 129 DCHECK(!caller_task_runner_ || caller_task_runner_->BelongsToCurrentThread()); |
| 130 |
| 131 // Immediately detach the wrapper from this recorder. |
| 132 weak_factory_.InvalidateWeakPtrs(); |
| 133 |
| 134 // Clean up any pending recorded frames. |
| 135 STLDeleteElements(&recorded_frames_); |
| 136 content_bytes_ = 0; |
| 137 |
| 138 // Tell the wrapper to stop recording and posting frames to us. |
| 139 if (encoder_task_runner_) { |
| 140 encoder_task_runner_->PostTask(FROM_HERE, |
| 141 base::Bind(&RecordingVideoEncoder::SetEnableRecording, |
| 142 recording_encoder_, false)); |
| 143 } |
| 144 |
| 145 // Detach this recorder from the calling and encode threads. |
| 146 caller_task_runner_ = NULL; |
| 147 encoder_task_runner_ = NULL; |
| 148 } |
| 149 |
128 void VideoFrameRecorder::SetEnableRecording(bool enable_recording) { | 150 void VideoFrameRecorder::SetEnableRecording(bool enable_recording) { |
129 DCHECK(!caller_task_runner_ || caller_task_runner_->BelongsToCurrentThread()); | 151 DCHECK(!caller_task_runner_ || caller_task_runner_->BelongsToCurrentThread()); |
130 | 152 |
131 if (enable_recording_ == enable_recording) { | 153 if (enable_recording_ == enable_recording) { |
132 return; | 154 return; |
133 } | 155 } |
134 enable_recording_ = enable_recording; | 156 enable_recording_ = enable_recording; |
135 | 157 |
136 if (encoder_task_runner_) { | 158 if (encoder_task_runner_) { |
137 encoder_task_runner_->PostTask(FROM_HERE, | 159 encoder_task_runner_->PostTask(FROM_HERE, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 if (content_bytes_ + frame_bytes > max_content_bytes_) { | 220 if (content_bytes_ + frame_bytes > max_content_bytes_) { |
199 return; | 221 return; |
200 } | 222 } |
201 | 223 |
202 // Store the frame and update the content byte count. | 224 // Store the frame and update the content byte count. |
203 recorded_frames_.push_back(frame.release()); | 225 recorded_frames_.push_back(frame.release()); |
204 content_bytes_ += frame_bytes; | 226 content_bytes_ += frame_bytes; |
205 } | 227 } |
206 | 228 |
207 } // namespace remoting | 229 } // namespace remoting |
OLD | NEW |