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 "content/browser/media/capture/content_video_capture_device_core.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/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.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 "base/trace_event/trace_event.h" | 23 #include "base/trace_event/trace_event.h" |
24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
25 #include "media/base/bind_to_current_loop.h" | 25 #include "media/base/bind_to_current_loop.h" |
26 #include "media/base/video_capture_types.h" | 26 #include "media/base/video_capture_types.h" |
27 #include "media/base/video_frame.h" | 27 #include "media/base/video_frame.h" |
| 28 #include "media/base/video_frame_metadata.h" |
28 #include "media/base/video_util.h" | 29 #include "media/base/video_util.h" |
29 #include "ui/gfx/geometry/rect.h" | 30 #include "ui/gfx/geometry/rect.h" |
30 | 31 |
31 namespace content { | 32 namespace content { |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 void DeleteCaptureMachineOnUIThread( | 36 void DeleteCaptureMachineOnUIThread( |
36 scoped_ptr<VideoCaptureMachine> capture_machine) { | 37 scoped_ptr<VideoCaptureMachine> capture_machine) { |
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 26 matching lines...) Expand all Loading... |
64 } | 65 } |
65 | 66 |
66 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {} | 67 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {} |
67 | 68 |
68 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( | 69 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( |
69 VideoCaptureOracle::Event event, | 70 VideoCaptureOracle::Event event, |
70 const gfx::Rect& damage_rect, | 71 const gfx::Rect& damage_rect, |
71 base::TimeTicks event_time, | 72 base::TimeTicks event_time, |
72 scoped_refptr<media::VideoFrame>* storage, | 73 scoped_refptr<media::VideoFrame>* storage, |
73 CaptureFrameCallback* callback) { | 74 CaptureFrameCallback* callback) { |
| 75 // Grab the current time before waiting to acquire the |lock_|. |
| 76 const base::TimeTicks capture_begin_time = base::TimeTicks::Now(); |
| 77 |
74 base::AutoLock guard(lock_); | 78 base::AutoLock guard(lock_); |
75 | 79 |
76 if (!client_) | 80 if (!client_) |
77 return false; // Capture is stopped. | 81 return false; // Capture is stopped. |
78 | 82 |
79 // Always round up the coded size to multiple of 16 pixels. | 83 // Always round up the coded size to multiple of 16 pixels. |
80 // See http://crbug.com/402151. | 84 // See http://crbug.com/402151. |
81 const gfx::Size visible_size = params_.requested_format.frame_size; | 85 const gfx::Size visible_size = params_.requested_format.frame_size; |
82 const gfx::Size coded_size((visible_size.width() + 15) & ~15, | 86 const gfx::Size coded_size((visible_size.width() + 15) & ~15, |
83 (visible_size.height() + 15) & ~15); | 87 (visible_size.height() + 15) & ~15); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 static_cast<uint8*>(output_buffer->data()), | 139 static_cast<uint8*>(output_buffer->data()), |
136 output_buffer->size(), | 140 output_buffer->size(), |
137 base::SharedMemory::NULLHandle(), | 141 base::SharedMemory::NULLHandle(), |
138 0, | 142 0, |
139 base::TimeDelta(), | 143 base::TimeDelta(), |
140 base::Closure()); | 144 base::Closure()); |
141 } | 145 } |
142 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, | 146 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, |
143 this, | 147 this, |
144 frame_number, | 148 frame_number, |
145 output_buffer); | 149 output_buffer, |
| 150 capture_begin_time); |
146 return true; | 151 return true; |
147 } | 152 } |
148 | 153 |
149 gfx::Size ThreadSafeCaptureOracle::GetCaptureSize() const { | 154 gfx::Size ThreadSafeCaptureOracle::GetCaptureSize() const { |
150 base::AutoLock guard(lock_); | 155 base::AutoLock guard(lock_); |
151 return params_.requested_format.frame_size; | 156 return params_.requested_format.frame_size; |
152 } | 157 } |
153 | 158 |
154 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) { | 159 void ThreadSafeCaptureOracle::UpdateCaptureSize(const gfx::Size& source_size) { |
155 base::AutoLock guard(lock_); | 160 base::AutoLock guard(lock_); |
(...skipping 26 matching lines...) Expand all Loading... |
182 | 187 |
183 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) { | 188 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) { |
184 base::AutoLock guard(lock_); | 189 base::AutoLock guard(lock_); |
185 if (client_) | 190 if (client_) |
186 client_->OnError(reason); | 191 client_->OnError(reason); |
187 } | 192 } |
188 | 193 |
189 void ThreadSafeCaptureOracle::DidCaptureFrame( | 194 void ThreadSafeCaptureOracle::DidCaptureFrame( |
190 int frame_number, | 195 int frame_number, |
191 const scoped_refptr<media::VideoCaptureDevice::Client::Buffer>& buffer, | 196 const scoped_refptr<media::VideoCaptureDevice::Client::Buffer>& buffer, |
| 197 base::TimeTicks capture_begin_time, |
192 const scoped_refptr<media::VideoFrame>& frame, | 198 const scoped_refptr<media::VideoFrame>& frame, |
193 base::TimeTicks timestamp, | 199 base::TimeTicks timestamp, |
194 bool success) { | 200 bool success) { |
195 base::AutoLock guard(lock_); | 201 base::AutoLock guard(lock_); |
196 TRACE_EVENT_ASYNC_END2("mirroring", "Capture", buffer.get(), | 202 TRACE_EVENT_ASYNC_END2("mirroring", "Capture", buffer.get(), |
197 "success", success, | 203 "success", success, |
198 "timestamp", timestamp.ToInternalValue()); | 204 "timestamp", timestamp.ToInternalValue()); |
199 | 205 |
200 if (!client_) | 206 if (!client_) |
201 return; // Capture is stopped. | 207 return; // Capture is stopped. |
202 | 208 |
203 if (success) { | 209 if (success) { |
204 if (oracle_.CompleteCapture(frame_number, ×tamp)) { | 210 if (oracle_.CompleteCapture(frame_number, ×tamp)) { |
205 media::VideoCaptureFormat format = params_.requested_format; | 211 // TODO(miu): Use the locked-in frame rate from AnimatedContentSampler. |
206 // TODO(miu): Passing VideoCaptureFormat here introduces ambiguities. The | 212 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, |
207 // following is a hack where frame_size takes on a different meaning than | 213 params_.requested_format.frame_rate); |
208 // everywhere else (i.e., coded size, not visible size). Will fix in | 214 frame->metadata()->SetTimeTicks( |
209 // soon-upcoming code change. | 215 media::VideoFrameMetadata::CAPTURE_BEGIN_TIME, capture_begin_time); |
210 format.frame_size = frame->coded_size(); | 216 frame->metadata()->SetTimeTicks( |
211 client_->OnIncomingCapturedVideoFrame(buffer, format, frame, timestamp); | 217 media::VideoFrameMetadata::CAPTURE_END_TIME, base::TimeTicks::Now()); |
| 218 client_->OnIncomingCapturedVideoFrame(buffer, frame, timestamp); |
212 } | 219 } |
213 } | 220 } |
214 } | 221 } |
215 | 222 |
216 void ContentVideoCaptureDeviceCore::AllocateAndStart( | 223 void ContentVideoCaptureDeviceCore::AllocateAndStart( |
217 const media::VideoCaptureParams& params, | 224 const media::VideoCaptureParams& params, |
218 scoped_ptr<media::VideoCaptureDevice::Client> client) { | 225 scoped_ptr<media::VideoCaptureDevice::Client> client) { |
219 DCHECK(thread_checker_.CalledOnValidThread()); | 226 DCHECK(thread_checker_.CalledOnValidThread()); |
220 | 227 |
221 if (state_ != kIdle) { | 228 if (state_ != kIdle) { |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 return; | 351 return; |
345 | 352 |
346 if (oracle_proxy_.get()) | 353 if (oracle_proxy_.get()) |
347 oracle_proxy_->ReportError(reason); | 354 oracle_proxy_->ReportError(reason); |
348 | 355 |
349 StopAndDeAllocate(); | 356 StopAndDeAllocate(); |
350 TransitionStateTo(kError); | 357 TransitionStateTo(kError); |
351 } | 358 } |
352 | 359 |
353 } // namespace content | 360 } // namespace content |
OLD | NEW |