| 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 "content/browser/media/capture/content_video_capture_device_core.h" | 5 #include "media/capture/screen_capture_device_core.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/bind.h" | 7 #include "base/bind.h" |
| 9 #include "base/callback_forward.h" | |
| 10 #include "base/callback_helpers.h" | |
| 11 #include "base/logging.h" | 8 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 14 #include "base/metrics/histogram.h" | |
| 15 #include "base/sequenced_task_runner.h" | |
| 16 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 18 #include "base/synchronization/lock.h" | |
| 19 #include "base/threading/thread.h" | |
| 20 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 21 #include "base/time/time.h" | |
| 22 #include "base/trace_event/trace_event.h" | |
| 23 #include "content/public/browser/browser_thread.h" | |
| 24 #include "media/base/video_capture_types.h" | |
| 25 #include "media/base/video_frame.h" | |
| 26 #include "media/base/video_frame_metadata.h" | |
| 27 #include "media/base/video_util.h" | |
| 28 #include "ui/gfx/geometry/rect.h" | |
| 29 | 14 |
| 30 namespace content { | 15 namespace media { |
| 31 | 16 |
| 32 namespace { | 17 namespace { |
| 33 | 18 |
| 34 void DeleteCaptureMachineOnUIThread( | 19 void DeleteCaptureMachine( |
| 35 scoped_ptr<VideoCaptureMachine> capture_machine) { | 20 scoped_ptr<VideoCaptureMachine> capture_machine) { |
| 36 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 37 | |
| 38 capture_machine.reset(); | 21 capture_machine.reset(); |
| 39 } | 22 } |
| 40 | 23 |
| 41 } // namespace | 24 } // namespace |
| 42 | 25 |
| 43 ThreadSafeCaptureOracle::ThreadSafeCaptureOracle( | 26 void ScreenCaptureDeviceCore::AllocateAndStart( |
| 44 scoped_ptr<media::VideoCaptureDevice::Client> client, | 27 const VideoCaptureParams& params, |
| 45 const media::VideoCaptureParams& params) | 28 scoped_ptr<VideoCaptureDevice::Client> client) { |
| 46 : client_(client.Pass()), | |
| 47 oracle_(base::TimeDelta::FromMicroseconds( | |
| 48 static_cast<int64>(1000000.0 / params.requested_format.frame_rate + | |
| 49 0.5 /* to round to nearest int */))), | |
| 50 params_(params), | |
| 51 resolution_chooser_(params.requested_format.frame_size, | |
| 52 params.resolution_change_policy) {} | |
| 53 | |
| 54 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {} | |
| 55 | |
| 56 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( | |
| 57 VideoCaptureOracle::Event event, | |
| 58 const gfx::Rect& damage_rect, | |
| 59 base::TimeTicks event_time, | |
| 60 scoped_refptr<media::VideoFrame>* storage, | |
| 61 CaptureFrameCallback* callback) { | |
| 62 // Grab the current time before waiting to acquire the |lock_|. | |
| 63 const base::TimeTicks capture_begin_time = base::TimeTicks::Now(); | |
| 64 | |
| 65 base::AutoLock guard(lock_); | |
| 66 | |
| 67 if (!client_) | |
| 68 return false; // Capture is stopped. | |
| 69 | |
| 70 const gfx::Size visible_size = resolution_chooser_.capture_size(); | |
| 71 // Always round up the coded size to multiple of 16 pixels. | |
| 72 // See http://crbug.com/402151. | |
| 73 const gfx::Size coded_size((visible_size.width() + 15) & ~15, | |
| 74 (visible_size.height() + 15) & ~15); | |
| 75 | |
| 76 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> output_buffer( | |
| 77 client_->ReserveOutputBuffer(params_.requested_format.pixel_format, | |
| 78 coded_size)); | |
| 79 // TODO(miu): Use current buffer pool utilization to drive automatic video | |
| 80 // resolution changes. http://crbug.com/156767. | |
| 81 VLOG(2) << "Current buffer pool utilization is " | |
| 82 << (client_->GetBufferPoolUtilization() * 100.0) << '%'; | |
| 83 | |
| 84 const bool should_capture = | |
| 85 oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time); | |
| 86 | |
| 87 const char* event_name = | |
| 88 (event == VideoCaptureOracle::kTimerPoll ? "poll" : | |
| 89 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" : | |
| 90 "unknown")); | |
| 91 | |
| 92 // Consider the various reasons not to initiate a capture. | |
| 93 if (should_capture && !output_buffer.get()) { | |
| 94 TRACE_EVENT_INSTANT1("gpu.capture", | |
| 95 "PipelineLimited", | |
| 96 TRACE_EVENT_SCOPE_THREAD, | |
| 97 "trigger", | |
| 98 event_name); | |
| 99 return false; | |
| 100 } else if (!should_capture && output_buffer.get()) { | |
| 101 if (event == VideoCaptureOracle::kCompositorUpdate) { | |
| 102 // This is a normal and acceptable way to drop a frame. We've hit our | |
| 103 // capture rate limit: for example, the content is animating at 60fps but | |
| 104 // we're capturing at 30fps. | |
| 105 TRACE_EVENT_INSTANT1("gpu.capture", "FpsRateLimited", | |
| 106 TRACE_EVENT_SCOPE_THREAD, | |
| 107 "trigger", event_name); | |
| 108 } | |
| 109 return false; | |
| 110 } else if (!should_capture && !output_buffer.get()) { | |
| 111 // We decided not to capture, but we wouldn't have been able to if we wanted | |
| 112 // to because no output buffer was available. | |
| 113 TRACE_EVENT_INSTANT1("gpu.capture", "NearlyPipelineLimited", | |
| 114 TRACE_EVENT_SCOPE_THREAD, | |
| 115 "trigger", event_name); | |
| 116 return false; | |
| 117 } | |
| 118 int frame_number = oracle_.RecordCapture(); | |
| 119 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(), | |
| 120 "frame_number", frame_number, | |
| 121 "trigger", event_name); | |
| 122 // NATIVE_TEXTURE frames wrap a texture mailbox, which we don't have at the | |
| 123 // moment. We do not construct those frames. | |
| 124 if (params_.requested_format.pixel_format != media::PIXEL_FORMAT_TEXTURE) { | |
| 125 *storage = media::VideoFrame::WrapExternalData( | |
| 126 media::VideoFrame::I420, | |
| 127 coded_size, | |
| 128 gfx::Rect(visible_size), | |
| 129 visible_size, | |
| 130 static_cast<uint8*>(output_buffer->data()), | |
| 131 output_buffer->size(), | |
| 132 base::TimeDelta()); | |
| 133 DCHECK(*storage); | |
| 134 } | |
| 135 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, | |
| 136 this, | |
| 137 frame_number, | |
| 138 base::Passed(&output_buffer), | |
| 139 capture_begin_time, | |
| 140 oracle_.estimated_frame_duration()); | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 gfx::Size ThreadSafeCaptureOracle::GetCaptureSize() const { | |
| 145 base::AutoLock guard(lock_); | |
| 146 return resolution_chooser_.capture_size(); | |
| 147 } | |
| 148 | |
| 149 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) { | |
| 150 base::AutoLock guard(lock_); | |
| 151 resolution_chooser_.SetSourceSize(source_size); | |
| 152 VLOG(1) << "Source size changed to " << source_size.ToString() | |
| 153 << " --> Capture size is now " | |
| 154 << resolution_chooser_.capture_size().ToString(); | |
| 155 } | |
| 156 | |
| 157 void ThreadSafeCaptureOracle::Stop() { | |
| 158 base::AutoLock guard(lock_); | |
| 159 client_.reset(); | |
| 160 } | |
| 161 | |
| 162 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) { | |
| 163 base::AutoLock guard(lock_); | |
| 164 if (client_) | |
| 165 client_->OnError(reason); | |
| 166 } | |
| 167 | |
| 168 void ThreadSafeCaptureOracle::DidCaptureFrame( | |
| 169 int frame_number, | |
| 170 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | |
| 171 base::TimeTicks capture_begin_time, | |
| 172 base::TimeDelta estimated_frame_duration, | |
| 173 const scoped_refptr<media::VideoFrame>& frame, | |
| 174 base::TimeTicks timestamp, | |
| 175 bool success) { | |
| 176 base::AutoLock guard(lock_); | |
| 177 TRACE_EVENT_ASYNC_END2("gpu.capture", "Capture", buffer.get(), | |
| 178 "success", success, | |
| 179 "timestamp", timestamp.ToInternalValue()); | |
| 180 | |
| 181 if (oracle_.CompleteCapture(frame_number, success, ×tamp)) { | |
| 182 TRACE_EVENT_INSTANT0("gpu.capture", "CaptureSucceeded", | |
| 183 TRACE_EVENT_SCOPE_THREAD); | |
| 184 | |
| 185 if (!client_) | |
| 186 return; // Capture is stopped. | |
| 187 | |
| 188 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | |
| 189 params_.requested_format.frame_rate); | |
| 190 frame->metadata()->SetTimeTicks( | |
| 191 media::VideoFrameMetadata::CAPTURE_BEGIN_TIME, capture_begin_time); | |
| 192 frame->metadata()->SetTimeTicks( | |
| 193 media::VideoFrameMetadata::CAPTURE_END_TIME, base::TimeTicks::Now()); | |
| 194 frame->metadata()->SetTimeDelta(media::VideoFrameMetadata::FRAME_DURATION, | |
| 195 estimated_frame_duration); | |
| 196 | |
| 197 frame->AddDestructionObserver(base::Bind( | |
| 198 &ThreadSafeCaptureOracle::DidConsumeFrame, | |
| 199 this, | |
| 200 frame_number, | |
| 201 frame->metadata())); | |
| 202 | |
| 203 client_->OnIncomingCapturedVideoFrame(buffer.Pass(), frame, timestamp); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 void ThreadSafeCaptureOracle::DidConsumeFrame( | |
| 208 int frame_number, | |
| 209 const media::VideoFrameMetadata* metadata) { | |
| 210 // Note: This function may be called on any thread by the VideoFrame | |
| 211 // destructor. |metadata| is still valid for read-access at this point. | |
| 212 double utilization = -1.0; | |
| 213 if (metadata->GetDouble(media::VideoFrameMetadata::RESOURCE_UTILIZATION, | |
| 214 &utilization) && | |
| 215 utilization >= 0.0) { | |
| 216 VLOG(2) << "Consumer resource utilization for frame " << frame_number | |
| 217 << ": " << utilization; | |
| 218 } | |
| 219 | |
| 220 // TODO(miu): Use |utilization| to drive automatic video resolution changes. | |
| 221 // http://crbug.com/156767. | |
| 222 } | |
| 223 | |
| 224 void ContentVideoCaptureDeviceCore::AllocateAndStart( | |
| 225 const media::VideoCaptureParams& params, | |
| 226 scoped_ptr<media::VideoCaptureDevice::Client> client) { | |
| 227 DCHECK(thread_checker_.CalledOnValidThread()); | 29 DCHECK(thread_checker_.CalledOnValidThread()); |
| 228 | 30 |
| 229 if (state_ != kIdle) { | 31 if (state_ != kIdle) { |
| 230 DVLOG(1) << "Allocate() invoked when not in state Idle."; | 32 DVLOG(1) << "Allocate() invoked when not in state Idle."; |
| 231 return; | 33 return; |
| 232 } | 34 } |
| 233 | 35 |
| 234 if (params.requested_format.frame_rate <= 0) { | 36 if (params.requested_format.frame_rate <= 0) { |
| 235 std::string error_msg("Invalid frame_rate: "); | 37 std::string error_msg("Invalid frame_rate: "); |
| 236 error_msg += base::DoubleToString(params.requested_format.frame_rate); | 38 error_msg += base::DoubleToString(params.requested_format.frame_rate); |
| 237 DVLOG(1) << error_msg; | 39 DVLOG(1) << error_msg; |
| 238 client->OnError(error_msg); | 40 client->OnError(error_msg); |
| 239 return; | 41 return; |
| 240 } | 42 } |
| 241 | 43 |
| 242 if (params.requested_format.pixel_format != media::PIXEL_FORMAT_I420 && | 44 if (params.requested_format.pixel_format != PIXEL_FORMAT_I420 && |
| 243 params.requested_format.pixel_format != media::PIXEL_FORMAT_TEXTURE) { | 45 params.requested_format.pixel_format != PIXEL_FORMAT_TEXTURE) { |
| 244 std::string error_msg = base::StringPrintf( | 46 std::string error_msg = base::StringPrintf( |
| 245 "unsupported format: %d", params.requested_format.pixel_format); | 47 "unsupported format: %d", params.requested_format.pixel_format); |
| 246 DVLOG(1) << error_msg; | 48 DVLOG(1) << error_msg; |
| 247 client->OnError(error_msg); | 49 client->OnError(error_msg); |
| 248 return; | 50 return; |
| 249 } | 51 } |
| 250 | 52 |
| 251 if (params.requested_format.frame_size.IsEmpty()) { | 53 if (params.requested_format.frame_size.IsEmpty()) { |
| 252 std::string error_msg = | 54 std::string error_msg = |
| 253 "invalid frame size: " + params.requested_format.frame_size.ToString(); | 55 "invalid frame size: " + params.requested_format.frame_size.ToString(); |
| 254 DVLOG(1) << error_msg; | 56 DVLOG(1) << error_msg; |
| 255 client->OnError(error_msg); | 57 client->OnError(error_msg); |
| 256 return; | 58 return; |
| 257 } | 59 } |
| 258 | 60 |
| 259 oracle_proxy_ = new ThreadSafeCaptureOracle(client.Pass(), params); | 61 oracle_proxy_ = new ThreadSafeCaptureOracle(client.Pass(), params); |
| 260 | 62 |
| 261 // Starts the capture machine asynchronously. | 63 capture_machine_->Start( |
| 262 BrowserThread::PostTaskAndReplyWithResult( | 64 oracle_proxy_, |
| 263 BrowserThread::UI, | 65 params, |
| 264 FROM_HERE, | 66 base::Bind(&ScreenCaptureDeviceCore::CaptureStarted, AsWeakPtr())); |
| 265 base::Bind(&VideoCaptureMachine::Start, | |
| 266 base::Unretained(capture_machine_.get()), | |
| 267 oracle_proxy_, | |
| 268 params), | |
| 269 base::Bind(&ContentVideoCaptureDeviceCore::CaptureStarted, AsWeakPtr())); | |
| 270 | 67 |
| 271 TransitionStateTo(kCapturing); | 68 TransitionStateTo(kCapturing); |
| 272 } | 69 } |
| 273 | 70 |
| 274 void ContentVideoCaptureDeviceCore::StopAndDeAllocate() { | 71 void ScreenCaptureDeviceCore::StopAndDeAllocate() { |
| 275 DCHECK(thread_checker_.CalledOnValidThread()); | 72 DCHECK(thread_checker_.CalledOnValidThread()); |
| 276 | 73 |
| 277 if (state_ != kCapturing) | 74 if (state_ != kCapturing) |
| 278 return; | 75 return; |
| 279 | 76 |
| 280 oracle_proxy_->Stop(); | 77 oracle_proxy_->Stop(); |
| 281 oracle_proxy_ = NULL; | 78 oracle_proxy_ = NULL; |
| 282 | 79 |
| 283 TransitionStateTo(kIdle); | 80 TransitionStateTo(kIdle); |
| 284 | 81 |
| 285 // Stops the capture machine asynchronously. | 82 capture_machine_->Stop(base::Bind(&base::DoNothing)); |
| 286 BrowserThread::PostTask( | |
| 287 BrowserThread::UI, FROM_HERE, base::Bind( | |
| 288 &VideoCaptureMachine::Stop, | |
| 289 base::Unretained(capture_machine_.get()), | |
| 290 base::Bind(&base::DoNothing))); | |
| 291 } | 83 } |
| 292 | 84 |
| 293 void ContentVideoCaptureDeviceCore::CaptureStarted(bool success) { | 85 void ScreenCaptureDeviceCore::CaptureStarted(bool success) { |
| 294 DCHECK(thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
| 295 if (!success) { | 87 if (!success) { |
| 296 std::string reason("Failed to start capture machine."); | 88 std::string reason("Failed to start capture machine."); |
| 297 DVLOG(1) << reason; | 89 DVLOG(1) << reason; |
| 298 Error(reason); | 90 Error(reason); |
| 299 } | 91 } |
| 300 } | 92 } |
| 301 | 93 |
| 302 ContentVideoCaptureDeviceCore::ContentVideoCaptureDeviceCore( | 94 ScreenCaptureDeviceCore::ScreenCaptureDeviceCore( |
| 303 scoped_ptr<VideoCaptureMachine> capture_machine) | 95 scoped_ptr<VideoCaptureMachine> capture_machine) |
| 304 : state_(kIdle), | 96 : state_(kIdle), |
| 305 capture_machine_(capture_machine.Pass()) { | 97 capture_machine_(capture_machine.Pass()) { |
| 306 DCHECK(capture_machine_.get()); | 98 DCHECK(capture_machine_.get()); |
| 307 } | 99 } |
| 308 | 100 |
| 309 ContentVideoCaptureDeviceCore::~ContentVideoCaptureDeviceCore() { | 101 ScreenCaptureDeviceCore::~ScreenCaptureDeviceCore() { |
| 310 DCHECK(thread_checker_.CalledOnValidThread()); | 102 DCHECK(thread_checker_.CalledOnValidThread()); |
| 311 DCHECK_NE(state_, kCapturing); | 103 DCHECK_NE(state_, kCapturing); |
| 312 // If capture_machine is not NULL, then we need to return to the UI thread to | |
| 313 // safely stop the capture machine. | |
| 314 if (capture_machine_) { | 104 if (capture_machine_) { |
| 315 VideoCaptureMachine* capture_machine_ptr = capture_machine_.get(); | 105 capture_machine_->Stop(base::Bind(&DeleteCaptureMachine, |
| 316 BrowserThread::PostTask( | 106 base::Passed(&capture_machine_))); |
| 317 BrowserThread::UI, FROM_HERE, | |
| 318 base::Bind(&VideoCaptureMachine::Stop, | |
| 319 base::Unretained(capture_machine_ptr), | |
| 320 base::Bind(&DeleteCaptureMachineOnUIThread, | |
| 321 base::Passed(&capture_machine_)))); | |
| 322 } | 107 } |
| 323 DVLOG(1) << "ContentVideoCaptureDeviceCore@" << this << " destroying."; | 108 DVLOG(1) << "ScreenCaptureDeviceCore@" << this << " destroying."; |
| 324 } | 109 } |
| 325 | 110 |
| 326 void ContentVideoCaptureDeviceCore::TransitionStateTo(State next_state) { | 111 void ScreenCaptureDeviceCore::TransitionStateTo(State next_state) { |
| 327 DCHECK(thread_checker_.CalledOnValidThread()); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 328 | 113 |
| 329 #ifndef NDEBUG | 114 #ifndef NDEBUG |
| 330 static const char* kStateNames[] = { | 115 static const char* kStateNames[] = { |
| 331 "Idle", "Allocated", "Capturing", "Error" | 116 "Idle", "Allocated", "Capturing", "Error" |
| 332 }; | 117 }; |
| 333 DVLOG(1) << "State change: " << kStateNames[state_] | 118 DVLOG(1) << "State change: " << kStateNames[state_] |
| 334 << " --> " << kStateNames[next_state]; | 119 << " --> " << kStateNames[next_state]; |
| 335 #endif | 120 #endif |
| 336 | 121 |
| 337 state_ = next_state; | 122 state_ = next_state; |
| 338 } | 123 } |
| 339 | 124 |
| 340 void ContentVideoCaptureDeviceCore::Error(const std::string& reason) { | 125 void ScreenCaptureDeviceCore::Error(const std::string& reason) { |
| 341 DCHECK(thread_checker_.CalledOnValidThread()); | 126 DCHECK(thread_checker_.CalledOnValidThread()); |
| 342 | 127 |
| 343 if (state_ == kIdle) | 128 if (state_ == kIdle) |
| 344 return; | 129 return; |
| 345 | 130 |
| 346 if (oracle_proxy_.get()) | 131 if (oracle_proxy_.get()) |
| 347 oracle_proxy_->ReportError(reason); | 132 oracle_proxy_->ReportError(reason); |
| 348 | 133 |
| 349 StopAndDeAllocate(); | 134 StopAndDeAllocate(); |
| 350 TransitionStateTo(kError); | 135 TransitionStateTo(kError); |
| 351 } | 136 } |
| 352 | 137 |
| 353 } // namespace content | 138 } // namespace media |
| OLD | NEW |