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