OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
miu
2015/06/05 01:13:36
nit: Please don't update the year here, since the
jiajia.qin
2015/06/08 04:58:23
Done.
| |
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/media_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" | |
25 #include "media/base/bind_to_current_loop.h" | 24 #include "media/base/bind_to_current_loop.h" |
26 #include "media/base/video_capture_types.h" | 25 #include "media/base/video_capture_types.h" |
27 #include "media/base/video_frame.h" | 26 #include "media/base/video_frame.h" |
28 #include "media/base/video_frame_metadata.h" | 27 #include "media/base/video_frame_metadata.h" |
29 #include "media/base/video_util.h" | 28 #include "media/base/video_util.h" |
30 #include "ui/gfx/geometry/rect.h" | 29 #include "ui/gfx/geometry/rect.h" |
31 | 30 |
32 namespace content { | 31 namespace media { |
33 | 32 |
34 namespace { | 33 namespace { |
35 | 34 |
36 void DeleteCaptureMachineOnUIThread( | 35 void DeleteCaptureMachine( |
37 scoped_ptr<VideoCaptureMachine> capture_machine) { | 36 scoped_ptr<VideoCaptureMachine> capture_machine) { |
38 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
39 | |
40 capture_machine.reset(); | 37 capture_machine.reset(); |
41 } | 38 } |
42 | 39 |
43 } // namespace | 40 } // namespace |
44 | 41 |
45 ThreadSafeCaptureOracle::ThreadSafeCaptureOracle( | 42 ThreadSafeCaptureOracle::ThreadSafeCaptureOracle( |
46 scoped_ptr<media::VideoCaptureDevice::Client> client, | 43 scoped_ptr<VideoCaptureDevice::Client> client, |
47 const media::VideoCaptureParams& params) | 44 const VideoCaptureParams& params) |
48 : client_(client.Pass()), | 45 : client_(client.Pass()), |
49 oracle_(base::TimeDelta::FromMicroseconds( | 46 oracle_(base::TimeDelta::FromMicroseconds( |
50 static_cast<int64>(1000000.0 / params.requested_format.frame_rate + | 47 static_cast<int64>(1000000.0 / params.requested_format.frame_rate + |
51 0.5 /* to round to nearest int */))), | 48 0.5 /* to round to nearest int */))), |
52 params_(params), | 49 params_(params), |
53 resolution_chooser_(params.requested_format.frame_size, | 50 resolution_chooser_(params.requested_format.frame_size, |
54 params.resolution_change_policy) {} | 51 params.resolution_change_policy) {} |
55 | 52 |
56 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {} | 53 ThreadSafeCaptureOracle::~ThreadSafeCaptureOracle() {} |
57 | 54 |
58 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( | 55 bool ThreadSafeCaptureOracle::ObserveEventAndDecideCapture( |
59 VideoCaptureOracle::Event event, | 56 VideoCaptureOracle::Event event, |
60 const gfx::Rect& damage_rect, | 57 const gfx::Rect& damage_rect, |
61 base::TimeTicks event_time, | 58 base::TimeTicks event_time, |
62 scoped_refptr<media::VideoFrame>* storage, | 59 scoped_refptr<VideoFrame>* storage, |
63 CaptureFrameCallback* callback) { | 60 CaptureFrameCallback* callback) { |
64 // Grab the current time before waiting to acquire the |lock_|. | 61 // Grab the current time before waiting to acquire the |lock_|. |
65 const base::TimeTicks capture_begin_time = base::TimeTicks::Now(); | 62 const base::TimeTicks capture_begin_time = base::TimeTicks::Now(); |
66 | 63 |
67 base::AutoLock guard(lock_); | 64 base::AutoLock guard(lock_); |
68 | 65 |
69 if (!client_) | 66 if (!client_) |
70 return false; // Capture is stopped. | 67 return false; // Capture is stopped. |
71 | 68 |
72 const gfx::Size visible_size = resolution_chooser_.capture_size(); | 69 const gfx::Size visible_size = resolution_chooser_.capture_size(); |
73 // Always round up the coded size to multiple of 16 pixels. | 70 // Always round up the coded size to multiple of 16 pixels. |
74 // See http://crbug.com/402151. | 71 // See http://crbug.com/402151. |
75 const gfx::Size coded_size((visible_size.width() + 15) & ~15, | 72 const gfx::Size coded_size((visible_size.width() + 15) & ~15, |
76 (visible_size.height() + 15) & ~15); | 73 (visible_size.height() + 15) & ~15); |
77 | 74 |
78 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> output_buffer( | 75 scoped_ptr<VideoCaptureDevice::Client::Buffer> output_buffer( |
79 client_->ReserveOutputBuffer(params_.requested_format.pixel_format, | 76 client_->ReserveOutputBuffer(params_.requested_format.pixel_format, |
80 coded_size)); | 77 coded_size)); |
81 const bool should_capture = | 78 const bool should_capture = |
82 oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time); | 79 oracle_.ObserveEventAndDecideCapture(event, damage_rect, event_time); |
83 const char* event_name = | 80 const char* event_name = |
84 (event == VideoCaptureOracle::kTimerPoll ? "poll" : | 81 (event == VideoCaptureOracle::kTimerPoll ? "poll" : |
85 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" : | 82 (event == VideoCaptureOracle::kCompositorUpdate ? "gpu" : |
86 "paint")); | 83 "paint")); |
87 | 84 |
88 // Consider the various reasons not to initiate a capture. | 85 // Consider the various reasons not to initiate a capture. |
(...skipping 21 matching lines...) Expand all Loading... | |
110 TRACE_EVENT_SCOPE_THREAD, | 107 TRACE_EVENT_SCOPE_THREAD, |
111 "trigger", event_name); | 108 "trigger", event_name); |
112 return false; | 109 return false; |
113 } | 110 } |
114 int frame_number = oracle_.RecordCapture(); | 111 int frame_number = oracle_.RecordCapture(); |
115 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(), | 112 TRACE_EVENT_ASYNC_BEGIN2("gpu.capture", "Capture", output_buffer.get(), |
116 "frame_number", frame_number, | 113 "frame_number", frame_number, |
117 "trigger", event_name); | 114 "trigger", event_name); |
118 // NATIVE_TEXTURE frames wrap a texture mailbox, which we don't have at the | 115 // NATIVE_TEXTURE frames wrap a texture mailbox, which we don't have at the |
119 // moment. We do not construct those frames. | 116 // moment. We do not construct those frames. |
120 if (params_.requested_format.pixel_format != media::PIXEL_FORMAT_TEXTURE) { | 117 if (params_.requested_format.pixel_format != PIXEL_FORMAT_TEXTURE) { |
121 *storage = media::VideoFrame::WrapExternalData( | 118 *storage = VideoFrame::WrapExternalData( |
122 media::VideoFrame::I420, | 119 VideoFrame::I420, |
123 coded_size, | 120 coded_size, |
124 gfx::Rect(visible_size), | 121 gfx::Rect(visible_size), |
125 visible_size, | 122 visible_size, |
126 static_cast<uint8*>(output_buffer->data()), | 123 static_cast<uint8*>(output_buffer->data()), |
127 output_buffer->size(), | 124 output_buffer->size(), |
128 base::TimeDelta()); | 125 base::TimeDelta()); |
129 DCHECK(*storage); | 126 DCHECK(*storage); |
130 } | 127 } |
131 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, | 128 *callback = base::Bind(&ThreadSafeCaptureOracle::DidCaptureFrame, |
132 this, | 129 this, |
(...skipping 23 matching lines...) Expand all Loading... | |
156 } | 153 } |
157 | 154 |
158 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) { | 155 void ThreadSafeCaptureOracle::ReportError(const std::string& reason) { |
159 base::AutoLock guard(lock_); | 156 base::AutoLock guard(lock_); |
160 if (client_) | 157 if (client_) |
161 client_->OnError(reason); | 158 client_->OnError(reason); |
162 } | 159 } |
163 | 160 |
164 void ThreadSafeCaptureOracle::DidCaptureFrame( | 161 void ThreadSafeCaptureOracle::DidCaptureFrame( |
165 int frame_number, | 162 int frame_number, |
166 scoped_ptr<media::VideoCaptureDevice::Client::Buffer> buffer, | 163 scoped_ptr<VideoCaptureDevice::Client::Buffer> buffer, |
167 base::TimeTicks capture_begin_time, | 164 base::TimeTicks capture_begin_time, |
168 base::TimeDelta estimated_frame_duration, | 165 base::TimeDelta estimated_frame_duration, |
169 const scoped_refptr<media::VideoFrame>& frame, | 166 const scoped_refptr<VideoFrame>& frame, |
170 base::TimeTicks timestamp, | 167 base::TimeTicks timestamp, |
171 bool success) { | 168 bool success) { |
172 base::AutoLock guard(lock_); | 169 base::AutoLock guard(lock_); |
173 TRACE_EVENT_ASYNC_END2("gpu.capture", "Capture", buffer.get(), | 170 TRACE_EVENT_ASYNC_END2("gpu.capture", "Capture", buffer.get(), |
174 "success", success, | 171 "success", success, |
175 "timestamp", timestamp.ToInternalValue()); | 172 "timestamp", timestamp.ToInternalValue()); |
176 | 173 |
177 if (oracle_.CompleteCapture(frame_number, success, ×tamp)) { | 174 if (oracle_.CompleteCapture(frame_number, success, ×tamp)) { |
178 TRACE_EVENT_INSTANT0("gpu.capture", "CaptureSucceeded", | 175 TRACE_EVENT_INSTANT0("gpu.capture", "CaptureSucceeded", |
179 TRACE_EVENT_SCOPE_THREAD); | 176 TRACE_EVENT_SCOPE_THREAD); |
180 | 177 |
181 if (!client_) | 178 if (!client_) |
182 return; // Capture is stopped. | 179 return; // Capture is stopped. |
183 | 180 |
184 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | 181 frame->metadata()->SetDouble(VideoFrameMetadata::FRAME_RATE, |
185 params_.requested_format.frame_rate); | 182 params_.requested_format.frame_rate); |
186 frame->metadata()->SetTimeTicks( | 183 frame->metadata()->SetTimeTicks( |
187 media::VideoFrameMetadata::CAPTURE_BEGIN_TIME, capture_begin_time); | 184 VideoFrameMetadata::CAPTURE_BEGIN_TIME, capture_begin_time); |
188 frame->metadata()->SetTimeTicks( | 185 frame->metadata()->SetTimeTicks( |
189 media::VideoFrameMetadata::CAPTURE_END_TIME, base::TimeTicks::Now()); | 186 VideoFrameMetadata::CAPTURE_END_TIME, base::TimeTicks::Now()); |
190 frame->metadata()->SetTimeDelta(media::VideoFrameMetadata::FRAME_DURATION, | 187 frame->metadata()->SetTimeDelta(VideoFrameMetadata::FRAME_DURATION, |
191 estimated_frame_duration); | 188 estimated_frame_duration); |
192 client_->OnIncomingCapturedVideoFrame(buffer.Pass(), frame, timestamp); | 189 client_->OnIncomingCapturedVideoFrame(buffer.Pass(), frame, timestamp); |
193 } | 190 } |
194 } | 191 } |
195 | 192 |
196 void ContentVideoCaptureDeviceCore::AllocateAndStart( | 193 void MediaVideoCaptureDeviceCore::AllocateAndStart( |
197 const media::VideoCaptureParams& params, | 194 const VideoCaptureParams& params, |
198 scoped_ptr<media::VideoCaptureDevice::Client> client) { | 195 scoped_ptr<VideoCaptureDevice::Client> client) { |
199 DCHECK(thread_checker_.CalledOnValidThread()); | 196 DCHECK(thread_checker_.CalledOnValidThread()); |
200 | 197 |
201 if (state_ != kIdle) { | 198 if (state_ != kIdle) { |
202 DVLOG(1) << "Allocate() invoked when not in state Idle."; | 199 DVLOG(1) << "Allocate() invoked when not in state Idle."; |
203 return; | 200 return; |
204 } | 201 } |
205 | 202 |
206 if (params.requested_format.frame_rate <= 0) { | 203 if (params.requested_format.frame_rate <= 0) { |
207 std::string error_msg("Invalid frame_rate: "); | 204 std::string error_msg("Invalid frame_rate: "); |
208 error_msg += base::DoubleToString(params.requested_format.frame_rate); | 205 error_msg += base::DoubleToString(params.requested_format.frame_rate); |
209 DVLOG(1) << error_msg; | 206 DVLOG(1) << error_msg; |
210 client->OnError(error_msg); | 207 client->OnError(error_msg); |
211 return; | 208 return; |
212 } | 209 } |
213 | 210 |
214 if (params.requested_format.pixel_format != media::PIXEL_FORMAT_I420 && | 211 if (params.requested_format.pixel_format != PIXEL_FORMAT_I420 && |
215 params.requested_format.pixel_format != media::PIXEL_FORMAT_TEXTURE) { | 212 params.requested_format.pixel_format != PIXEL_FORMAT_TEXTURE) { |
216 std::string error_msg = base::StringPrintf( | 213 std::string error_msg = base::StringPrintf( |
217 "unsupported format: %d", params.requested_format.pixel_format); | 214 "unsupported format: %d", params.requested_format.pixel_format); |
218 DVLOG(1) << error_msg; | 215 DVLOG(1) << error_msg; |
219 client->OnError(error_msg); | 216 client->OnError(error_msg); |
220 return; | 217 return; |
221 } | 218 } |
222 | 219 |
223 if (params.requested_format.frame_size.IsEmpty()) { | 220 if (params.requested_format.frame_size.IsEmpty()) { |
224 std::string error_msg = | 221 std::string error_msg = |
225 "invalid frame size: " + params.requested_format.frame_size.ToString(); | 222 "invalid frame size: " + params.requested_format.frame_size.ToString(); |
226 DVLOG(1) << error_msg; | 223 DVLOG(1) << error_msg; |
227 client->OnError(error_msg); | 224 client->OnError(error_msg); |
228 return; | 225 return; |
229 } | 226 } |
230 | 227 |
231 oracle_proxy_ = new ThreadSafeCaptureOracle(client.Pass(), params); | 228 oracle_proxy_ = new ThreadSafeCaptureOracle(client.Pass(), params); |
232 | 229 |
233 // Starts the capture machine asynchronously. | 230 capture_machine_->Start( |
234 BrowserThread::PostTaskAndReplyWithResult( | 231 oracle_proxy_, |
235 BrowserThread::UI, | 232 params, |
236 FROM_HERE, | 233 base::Bind(&MediaVideoCaptureDeviceCore::CaptureStarted, AsWeakPtr())); |
237 base::Bind(&VideoCaptureMachine::Start, | |
238 base::Unretained(capture_machine_.get()), | |
239 oracle_proxy_, | |
240 params), | |
241 base::Bind(&ContentVideoCaptureDeviceCore::CaptureStarted, AsWeakPtr())); | |
242 | 234 |
243 TransitionStateTo(kCapturing); | 235 TransitionStateTo(kCapturing); |
244 } | 236 } |
245 | 237 |
246 void ContentVideoCaptureDeviceCore::StopAndDeAllocate() { | 238 void MediaVideoCaptureDeviceCore::StopAndDeAllocate() { |
247 DCHECK(thread_checker_.CalledOnValidThread()); | 239 DCHECK(thread_checker_.CalledOnValidThread()); |
248 | 240 |
249 if (state_ != kCapturing) | 241 if (state_ != kCapturing) |
250 return; | 242 return; |
251 | 243 |
252 oracle_proxy_->Stop(); | 244 oracle_proxy_->Stop(); |
253 oracle_proxy_ = NULL; | 245 oracle_proxy_ = NULL; |
254 | 246 |
255 TransitionStateTo(kIdle); | 247 TransitionStateTo(kIdle); |
256 | 248 |
257 // Stops the capture machine asynchronously. | 249 capture_machine_->Stop(base::Bind(&base::DoNothing)); |
258 BrowserThread::PostTask( | |
259 BrowserThread::UI, FROM_HERE, base::Bind( | |
260 &VideoCaptureMachine::Stop, | |
261 base::Unretained(capture_machine_.get()), | |
262 base::Bind(&base::DoNothing))); | |
263 } | 250 } |
264 | 251 |
265 void ContentVideoCaptureDeviceCore::CaptureStarted(bool success) { | 252 void MediaVideoCaptureDeviceCore::CaptureStarted(bool success) { |
266 DCHECK(thread_checker_.CalledOnValidThread()); | 253 DCHECK(thread_checker_.CalledOnValidThread()); |
267 if (!success) { | 254 if (!success) { |
268 std::string reason("Failed to start capture machine."); | 255 std::string reason("Failed to start capture machine."); |
269 DVLOG(1) << reason; | 256 DVLOG(1) << reason; |
270 Error(reason); | 257 Error(reason); |
271 } | 258 } |
272 } | 259 } |
273 | 260 |
274 ContentVideoCaptureDeviceCore::ContentVideoCaptureDeviceCore( | 261 MediaVideoCaptureDeviceCore::MediaVideoCaptureDeviceCore( |
275 scoped_ptr<VideoCaptureMachine> capture_machine) | 262 scoped_ptr<VideoCaptureMachine> capture_machine) |
276 : state_(kIdle), | 263 : state_(kIdle), |
277 capture_machine_(capture_machine.Pass()) { | 264 capture_machine_(capture_machine.Pass()) { |
278 DCHECK(capture_machine_.get()); | 265 DCHECK(capture_machine_.get()); |
279 } | 266 } |
280 | 267 |
281 ContentVideoCaptureDeviceCore::~ContentVideoCaptureDeviceCore() { | 268 MediaVideoCaptureDeviceCore::~MediaVideoCaptureDeviceCore() { |
282 DCHECK(thread_checker_.CalledOnValidThread()); | 269 DCHECK(thread_checker_.CalledOnValidThread()); |
283 DCHECK_NE(state_, kCapturing); | 270 DCHECK_NE(state_, kCapturing); |
284 // If capture_machine is not NULL, then we need to return to the UI thread to | 271 // If capture_machine is not NULL, then we need to return to the UI thread to |
miu
2015/06/05 01:13:36
Please remove this comment, since the posting of t
jiajia.qin
2015/06/08 04:58:24
Done.
| |
285 // safely stop the capture machine. | 272 // safely stop the capture machine. |
286 if (capture_machine_) { | 273 if (capture_machine_) { |
287 VideoCaptureMachine* capture_machine_ptr = capture_machine_.get(); | 274 capture_machine_->Stop(base::Bind(&DeleteCaptureMachine, |
288 BrowserThread::PostTask( | 275 base::Passed(&capture_machine_))); |
289 BrowserThread::UI, FROM_HERE, | |
290 base::Bind(&VideoCaptureMachine::Stop, | |
291 base::Unretained(capture_machine_ptr), | |
292 base::Bind(&DeleteCaptureMachineOnUIThread, | |
293 base::Passed(&capture_machine_)))); | |
294 } | 276 } |
295 DVLOG(1) << "ContentVideoCaptureDeviceCore@" << this << " destroying."; | 277 DVLOG(1) << "MediaVideoCaptureDeviceCore@" << this << " destroying."; |
296 } | 278 } |
297 | 279 |
298 void ContentVideoCaptureDeviceCore::TransitionStateTo(State next_state) { | 280 void MediaVideoCaptureDeviceCore::TransitionStateTo(State next_state) { |
299 DCHECK(thread_checker_.CalledOnValidThread()); | 281 DCHECK(thread_checker_.CalledOnValidThread()); |
300 | 282 |
301 #ifndef NDEBUG | 283 #ifndef NDEBUG |
302 static const char* kStateNames[] = { | 284 static const char* kStateNames[] = { |
303 "Idle", "Allocated", "Capturing", "Error" | 285 "Idle", "Allocated", "Capturing", "Error" |
304 }; | 286 }; |
305 DVLOG(1) << "State change: " << kStateNames[state_] | 287 DVLOG(1) << "State change: " << kStateNames[state_] |
306 << " --> " << kStateNames[next_state]; | 288 << " --> " << kStateNames[next_state]; |
307 #endif | 289 #endif |
308 | 290 |
309 state_ = next_state; | 291 state_ = next_state; |
310 } | 292 } |
311 | 293 |
312 void ContentVideoCaptureDeviceCore::Error(const std::string& reason) { | 294 void MediaVideoCaptureDeviceCore::Error(const std::string& reason) { |
313 DCHECK(thread_checker_.CalledOnValidThread()); | 295 DCHECK(thread_checker_.CalledOnValidThread()); |
314 | 296 |
315 if (state_ == kIdle) | 297 if (state_ == kIdle) |
316 return; | 298 return; |
317 | 299 |
318 if (oracle_proxy_.get()) | 300 if (oracle_proxy_.get()) |
319 oracle_proxy_->ReportError(reason); | 301 oracle_proxy_->ReportError(reason); |
320 | 302 |
321 StopAndDeAllocate(); | 303 StopAndDeAllocate(); |
322 TransitionStateTo(kError); | 304 TransitionStateTo(kError); |
323 } | 305 } |
324 | 306 |
325 } // namespace content | 307 } // namespace media |
OLD | NEW |