| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/renderer_host/media/video_capture_device_impl.h" | 5 #include "content/browser/renderer_host/media/video_capture_device_impl.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" | 9 #include "base/callback_forward.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/message_loop/message_loop_proxy.h" | 15 #include "base/message_loop/message_loop_proxy.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/sequenced_task_runner.h" | 17 #include "base/sequenced_task_runner.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 20 #include "base/threading/thread.h" | 20 #include "base/threading/thread.h" |
| 21 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
| 22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "media/base/bind_to_loop.h" | 24 #include "media/base/bind_to_loop.h" |
| 25 #include "media/base/video_frame.h" | 25 #include "media/base/video_frame.h" |
| 26 #include "media/base/video_util.h" |
| 26 #include "media/video/capture/video_capture_types.h" | 27 #include "media/video/capture/video_capture_types.h" |
| 27 #include "ui/gfx/rect.h" | 28 #include "ui/gfx/rect.h" |
| 28 | 29 |
| 29 namespace content { | 30 namespace content { |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 void DeleteCaptureMachineOnUIThread( | 34 void DeleteCaptureMachineOnUIThread( |
| 34 scoped_ptr<VideoCaptureMachine> capture_machine) { | 35 scoped_ptr<VideoCaptureMachine> capture_machine) { |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 36 if (capture_machine) { | 37 if (capture_machine) { |
| 37 capture_machine->Stop(); | 38 capture_machine->Stop(); |
| 38 capture_machine.reset(); | 39 capture_machine.reset(); |
| 39 } | 40 } |
| 40 } | 41 } |
| 41 | 42 |
| 42 } // namespace | 43 } // namespace |
| 43 | 44 |
| 44 ThreadSafeCaptureOracle::ThreadSafeCaptureOracle( | 45 ThreadSafeCaptureOracle::ThreadSafeCaptureOracle( |
| 45 scoped_ptr<media::VideoCaptureDevice::Client> client, | 46 scoped_ptr<media::VideoCaptureDevice::Client> client, |
| 46 scoped_ptr<VideoCaptureOracle> oracle, | 47 scoped_ptr<VideoCaptureOracle> oracle, |
| 47 const gfx::Size& capture_size, | 48 const media::VideoCaptureParams& params) |
| 48 int frame_rate) | |
| 49 : client_(client.Pass()), | 49 : client_(client.Pass()), |
| 50 oracle_(oracle.Pass()), | 50 oracle_(oracle.Pass()), |
| 51 capture_size_(capture_size), | 51 params_(params), |
| 52 frame_rate_(frame_rate) {} | 52 capture_size_updated_(false) { |
| 53 // Frame dimensions must each be an even integer since the client wants (or |
| 54 // will convert to) YUV420. |
| 55 capture_size_ = gfx::Size( |
| 56 MakeEven(params.requested_format.frame_size.width()), |
| 57 MakeEven(params.requested_format.frame_size.height())); |
| 58 frame_rate_ = params.requested_format.frame_rate; |
| 59 } |
| 53 | 60 |
| 54 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {} | 61 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {} |
| 55 | 62 |
| 56 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( | 63 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( |
| 57 VideoCaptureOracle::Event event, | 64 VideoCaptureOracle::Event event, |
| 58 base::Time event_time, | 65 base::Time event_time, |
| 59 scoped_refptr<media::VideoFrame>* storage, | 66 scoped_refptr<media::VideoFrame>* storage, |
| 60 CaptureFrameCallback* callback) { | 67 CaptureFrameCallback* callback) { |
| 61 base::AutoLock guard(lock_); | 68 base::AutoLock guard(lock_); |
| 62 | 69 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 gfx::Rect(capture_size_), | 122 gfx::Rect(capture_size_), |
| 116 capture_size_, | 123 capture_size_, |
| 117 static_cast<uint8*>(output_buffer->data()), | 124 static_cast<uint8*>(output_buffer->data()), |
| 118 output_buffer->size(), | 125 output_buffer->size(), |
| 119 base::SharedMemory::NULLHandle(), | 126 base::SharedMemory::NULLHandle(), |
| 120 base::TimeDelta(), | 127 base::TimeDelta(), |
| 121 base::Closure()); | 128 base::Closure()); |
| 122 return true; | 129 return true; |
| 123 } | 130 } |
| 124 | 131 |
| 132 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) { |
| 133 base::AutoLock guard(lock_); |
| 134 |
| 135 // If this is the first call to UpdateCaptureSize(), or the receiver supports |
| 136 // variable resolution, then determine the capture size by treating the |
| 137 // requested width and height as maxima. |
| 138 if (!capture_size_updated_ || params_.allow_resolution_change) { |
| 139 // The capture resolution should not exceed the source frame size. |
| 140 // In other words it should downscale the image but not upscale it. |
| 141 gfx::Rect capture_rect = media::ComputeLetterboxRegion( |
| 142 gfx::Rect(params_.requested_format.frame_size), source_size); |
| 143 capture_size_ = gfx::Size(MakeEven(capture_rect.width()), |
| 144 MakeEven(capture_rect.height())); |
| 145 capture_size_updated_ = true; |
| 146 } |
| 147 } |
| 148 |
| 125 void ThreadSafeCaptureOracle::Stop() { | 149 void ThreadSafeCaptureOracle::Stop() { |
| 126 base::AutoLock guard(lock_); | 150 base::AutoLock guard(lock_); |
| 127 client_.reset(); | 151 client_.reset(); |
| 128 } | 152 } |
| 129 | 153 |
| 130 void ThreadSafeCaptureOracle::ReportError() { | 154 void ThreadSafeCaptureOracle::ReportError() { |
| 131 base::AutoLock guard(lock_); | 155 base::AutoLock guard(lock_); |
| 132 if (client_) | 156 if (client_) |
| 133 client_->OnError(); | 157 client_->OnError(); |
| 134 } | 158 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 DVLOG(1) << "Allocate() invoked when not in state Idle."; | 190 DVLOG(1) << "Allocate() invoked when not in state Idle."; |
| 167 return; | 191 return; |
| 168 } | 192 } |
| 169 | 193 |
| 170 if (params.requested_format.frame_rate <= 0) { | 194 if (params.requested_format.frame_rate <= 0) { |
| 171 DVLOG(1) << "invalid frame_rate: " << params.requested_format.frame_rate; | 195 DVLOG(1) << "invalid frame_rate: " << params.requested_format.frame_rate; |
| 172 client->OnError(); | 196 client->OnError(); |
| 173 return; | 197 return; |
| 174 } | 198 } |
| 175 | 199 |
| 176 // Frame dimensions must each be a positive, even integer, since the client | 200 if (params.requested_format.frame_size.width() < kMinFrameWidth || |
| 177 // wants (or will convert to) YUV420. | 201 params.requested_format.frame_size.height() < kMinFrameHeight) { |
| 178 gfx::Size frame_size(MakeEven(params.requested_format.frame_size.width()), | 202 DVLOG(1) << "invalid frame size: " |
| 179 MakeEven(params.requested_format.frame_size.height())); | 203 << params.requested_format.frame_size.ToString(); |
| 180 if (frame_size.width() < kMinFrameWidth || | |
| 181 frame_size.height() < kMinFrameHeight) { | |
| 182 DVLOG(1) << "invalid frame size: " << frame_size.ToString(); | |
| 183 client->OnError(); | 204 client->OnError(); |
| 184 return; | 205 return; |
| 185 } | 206 } |
| 186 | 207 |
| 187 base::TimeDelta capture_period = base::TimeDelta::FromMicroseconds( | 208 base::TimeDelta capture_period = base::TimeDelta::FromMicroseconds( |
| 188 1000000.0 / params.requested_format.frame_rate + 0.5); | 209 1000000.0 / params.requested_format.frame_rate + 0.5); |
| 189 | 210 |
| 190 scoped_ptr<VideoCaptureOracle> oracle( | 211 scoped_ptr<VideoCaptureOracle> oracle( |
| 191 new VideoCaptureOracle(capture_period, | 212 new VideoCaptureOracle(capture_period, |
| 192 kAcceleratedSubscriberIsSupported)); | 213 kAcceleratedSubscriberIsSupported)); |
| 193 oracle_proxy_ = | 214 oracle_proxy_ = |
| 194 new ThreadSafeCaptureOracle(client.Pass(), | 215 new ThreadSafeCaptureOracle(client.Pass(), oracle.Pass(), params); |
| 195 oracle.Pass(), | |
| 196 frame_size, | |
| 197 params.requested_format.frame_rate); | |
| 198 | 216 |
| 199 // Starts the capture machine asynchronously. | 217 // Starts the capture machine asynchronously. |
| 200 BrowserThread::PostTaskAndReplyWithResult( | 218 BrowserThread::PostTaskAndReplyWithResult( |
| 201 BrowserThread::UI, FROM_HERE, | 219 BrowserThread::UI, FROM_HERE, |
| 202 base::Bind(&VideoCaptureMachine::Start, | 220 base::Bind(&VideoCaptureMachine::Start, |
| 203 base::Unretained(capture_machine_.get()), | 221 base::Unretained(capture_machine_.get()), |
| 204 oracle_proxy_), | 222 oracle_proxy_), |
| 205 base::Bind(&VideoCaptureDeviceImpl::CaptureStarted, | 223 base::Bind(&VideoCaptureDeviceImpl::CaptureStarted, |
| 206 AsWeakPtr())); | 224 AsWeakPtr())); |
| 207 | 225 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 return; | 289 return; |
| 272 | 290 |
| 273 if (oracle_proxy_) | 291 if (oracle_proxy_) |
| 274 oracle_proxy_->ReportError(); | 292 oracle_proxy_->ReportError(); |
| 275 | 293 |
| 276 StopAndDeAllocate(); | 294 StopAndDeAllocate(); |
| 277 TransitionStateTo(kError); | 295 TransitionStateTo(kError); |
| 278 } | 296 } |
| 279 | 297 |
| 280 } // namespace content | 298 } // namespace content |
| OLD | NEW |