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