OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 }; | 103 }; |
104 | 104 |
105 /////////////////////////////////////////////////////////////////////////// | 105 /////////////////////////////////////////////////////////////////////////// |
106 // Implementation of class WebRtcVideoCapturer | 106 // Implementation of class WebRtcVideoCapturer |
107 /////////////////////////////////////////////////////////////////////////// | 107 /////////////////////////////////////////////////////////////////////////// |
108 | 108 |
109 WebRtcVideoCapturer::WebRtcVideoCapturer() | 109 WebRtcVideoCapturer::WebRtcVideoCapturer() |
110 : factory_(new WebRtcVcmFactory), | 110 : factory_(new WebRtcVcmFactory), |
111 module_(nullptr), | 111 module_(nullptr), |
112 captured_frames_(0), | 112 captured_frames_(0), |
113 start_thread_(nullptr), | 113 start_thread_(nullptr) {} |
114 async_invoker_(nullptr) {} | |
115 | 114 |
116 WebRtcVideoCapturer::WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory) | 115 WebRtcVideoCapturer::WebRtcVideoCapturer(WebRtcVcmFactoryInterface* factory) |
117 : factory_(factory), | 116 : factory_(factory), |
118 module_(nullptr), | 117 module_(nullptr), |
119 captured_frames_(0), | 118 captured_frames_(0), |
120 start_thread_(nullptr), | 119 start_thread_(nullptr) {} |
121 async_invoker_(nullptr) {} | |
122 | 120 |
123 WebRtcVideoCapturer::~WebRtcVideoCapturer() {} | 121 WebRtcVideoCapturer::~WebRtcVideoCapturer() {} |
124 | 122 |
125 bool WebRtcVideoCapturer::Init(const Device& device) { | 123 bool WebRtcVideoCapturer::Init(const Device& device) { |
126 RTC_DCHECK(!start_thread_); | 124 RTC_DCHECK(!start_thread_); |
127 if (module_) { | 125 if (module_) { |
128 LOG(LS_ERROR) << "The capturer is already initialized"; | 126 LOG(LS_ERROR) << "The capturer is already initialized"; |
129 return false; | 127 return false; |
130 } | 128 } |
131 | 129 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 return CS_FAILED; | 247 return CS_FAILED; |
250 } | 248 } |
251 if (start_thread_) { | 249 if (start_thread_) { |
252 LOG(LS_ERROR) << "The capturer is already running"; | 250 LOG(LS_ERROR) << "The capturer is already running"; |
253 RTC_DCHECK(start_thread_->IsCurrent()) | 251 RTC_DCHECK(start_thread_->IsCurrent()) |
254 << "Trying to start capturer on different threads"; | 252 << "Trying to start capturer on different threads"; |
255 return CS_FAILED; | 253 return CS_FAILED; |
256 } | 254 } |
257 | 255 |
258 start_thread_ = rtc::Thread::Current(); | 256 start_thread_ = rtc::Thread::Current(); |
259 RTC_DCHECK(!async_invoker_); | |
260 async_invoker_.reset(new rtc::AsyncInvoker()); | |
261 captured_frames_ = 0; | 257 captured_frames_ = 0; |
262 | 258 |
263 SetCaptureFormat(&capture_format); | 259 SetCaptureFormat(&capture_format); |
264 | 260 |
265 webrtc::VideoCaptureCapability cap; | 261 webrtc::VideoCaptureCapability cap; |
266 if (!FormatToCapability(capture_format, &cap)) { | 262 if (!FormatToCapability(capture_format, &cap)) { |
267 LOG(LS_ERROR) << "Invalid capture format specified"; | 263 LOG(LS_ERROR) << "Invalid capture format specified"; |
268 return CS_FAILED; | 264 return CS_FAILED; |
269 } | 265 } |
270 | 266 |
271 int64_t start = rtc::TimeMillis(); | 267 int64_t start = rtc::TimeMillis(); |
272 module_->RegisterCaptureDataCallback(this); | 268 module_->RegisterCaptureDataCallback(this); |
273 if (module_->StartCapture(cap) != 0) { | 269 if (module_->StartCapture(cap) != 0) { |
274 LOG(LS_ERROR) << "Camera '" << GetId() << "' failed to start"; | 270 LOG(LS_ERROR) << "Camera '" << GetId() << "' failed to start"; |
275 module_->DeRegisterCaptureDataCallback(); | 271 module_->DeRegisterCaptureDataCallback(); |
276 async_invoker_.reset(); | |
277 SetCaptureFormat(nullptr); | 272 SetCaptureFormat(nullptr); |
278 start_thread_ = nullptr; | 273 start_thread_ = nullptr; |
279 return CS_FAILED; | 274 return CS_FAILED; |
280 } | 275 } |
281 | 276 |
282 LOG(LS_INFO) << "Camera '" << GetId() << "' started with format " | 277 LOG(LS_INFO) << "Camera '" << GetId() << "' started with format " |
283 << capture_format.ToString() << ", elapsed time " | 278 << capture_format.ToString() << ", elapsed time " |
284 << rtc::TimeSince(start) << " ms"; | 279 << rtc::TimeSince(start) << " ms"; |
285 | 280 |
286 SetCaptureState(CS_RUNNING); | 281 SetCaptureState(CS_RUNNING); |
287 return CS_STARTING; | 282 return CS_STARTING; |
288 } | 283 } |
289 | 284 |
290 void WebRtcVideoCapturer::Stop() { | 285 void WebRtcVideoCapturer::Stop() { |
291 if (!start_thread_) { | 286 if (!start_thread_) { |
292 LOG(LS_ERROR) << "The capturer is already stopped"; | 287 LOG(LS_ERROR) << "The capturer is already stopped"; |
293 return; | 288 return; |
294 } | 289 } |
295 RTC_DCHECK(start_thread_); | 290 RTC_DCHECK(start_thread_); |
296 RTC_DCHECK(start_thread_->IsCurrent()); | 291 RTC_DCHECK(start_thread_->IsCurrent()); |
297 RTC_DCHECK(async_invoker_); | |
298 if (IsRunning()) { | 292 if (IsRunning()) { |
299 // The module is responsible for OnIncomingCapturedFrame being called, if | 293 // The module is responsible for OnIncomingCapturedFrame being called, if |
300 // we stop it we will get no further callbacks. | 294 // we stop it we will get no further callbacks. |
301 module_->StopCapture(); | 295 module_->StopCapture(); |
302 } | 296 } |
303 module_->DeRegisterCaptureDataCallback(); | 297 module_->DeRegisterCaptureDataCallback(); |
304 | 298 |
305 // TODO(juberti): Determine if the VCM exposes any drop stats we can use. | 299 // TODO(juberti): Determine if the VCM exposes any drop stats we can use. |
306 double drop_ratio = 0.0; | 300 double drop_ratio = 0.0; |
307 LOG(LS_INFO) << "Camera '" << GetId() << "' stopped after capturing " | 301 LOG(LS_INFO) << "Camera '" << GetId() << "' stopped after capturing " |
308 << captured_frames_ << " frames and dropping " | 302 << captured_frames_ << " frames and dropping " |
309 << drop_ratio << "%"; | 303 << drop_ratio << "%"; |
310 | 304 |
311 // Clear any pending async invokes (that OnIncomingCapturedFrame may have | |
312 // caused). | |
313 async_invoker_.reset(); | |
314 | |
315 SetCaptureFormat(NULL); | 305 SetCaptureFormat(NULL); |
316 start_thread_ = nullptr; | 306 start_thread_ = nullptr; |
317 SetCaptureState(CS_STOPPED); | 307 SetCaptureState(CS_STOPPED); |
318 } | 308 } |
319 | 309 |
320 bool WebRtcVideoCapturer::IsRunning() { | 310 bool WebRtcVideoCapturer::IsRunning() { |
321 return (module_ != NULL && module_->CaptureStarted()); | 311 return (module_ != NULL && module_->CaptureStarted()); |
322 } | 312 } |
323 | 313 |
324 bool WebRtcVideoCapturer::GetPreferredFourccs(std::vector<uint32_t>* fourccs) { | 314 bool WebRtcVideoCapturer::GetPreferredFourccs(std::vector<uint32_t>* fourccs) { |
325 if (!fourccs) { | 315 if (!fourccs) { |
326 return false; | 316 return false; |
327 } | 317 } |
328 | 318 |
329 fourccs->clear(); | 319 fourccs->clear(); |
330 for (size_t i = 0; i < arraysize(kSupportedFourCCs); ++i) { | 320 for (size_t i = 0; i < arraysize(kSupportedFourCCs); ++i) { |
331 fourccs->push_back(kSupportedFourCCs[i].fourcc); | 321 fourccs->push_back(kSupportedFourCCs[i].fourcc); |
332 } | 322 } |
333 return true; | 323 return true; |
334 } | 324 } |
335 | 325 |
336 void WebRtcVideoCapturer::OnFrame( | 326 void WebRtcVideoCapturer::OnFrame( |
337 const webrtc::VideoFrame& sample) { | 327 const webrtc::VideoFrame& sample) { |
338 // This can only happen between Start() and Stop(). | 328 // This can only happen between Start() and Stop(). |
339 RTC_DCHECK(start_thread_); | 329 RTC_DCHECK(start_thread_); |
340 RTC_DCHECK(async_invoker_); | |
341 | 330 |
342 ++captured_frames_; | 331 ++captured_frames_; |
343 // Log the size and pixel aspect ratio of the first captured frame. | 332 // Log the size and pixel aspect ratio of the first captured frame. |
344 if (1 == captured_frames_) { | 333 if (1 == captured_frames_) { |
345 LOG(LS_INFO) << "Captured frame size " | 334 LOG(LS_INFO) << "Captured frame size " |
346 << sample.width() << "x" << sample.height() | 335 << sample.width() << "x" << sample.height() |
347 << ". Expected format " << GetCaptureFormat()->ToString(); | 336 << ". Expected format " << GetCaptureFormat()->ToString(); |
348 } | 337 } |
349 | 338 |
350 VideoCapturer::OnFrame(sample, sample.width(), sample.height()); | 339 VideoCapturer::OnFrame(sample, sample.width(), sample.height()); |
351 } | 340 } |
352 | 341 |
353 } // namespace cricket | 342 } // namespace cricket |
OLD | NEW |