• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/chromeos/login/camera_controller.h"
6 
7 #include "base/compiler_specific.h"
8 #include "base/threading/thread_restrictions.h"
9 
10 namespace chromeos {
11 
12 namespace {
13 
14 // Maximum number of capture failures we ignore before we try to initialize
15 // the camera again.
16 const int kMaxCaptureFailureCounter = 5;
17 
18 // Maximum number of camera initialization retries.
19 const int kMaxCameraInitFailureCounter = 3;
20 
21 // Name for camera thread.
22 const char kCameraThreadName[] = "Chrome_CameraThread";
23 
24 }  // namespace
25 
CameraController(Delegate * delegate)26 CameraController::CameraController(Delegate* delegate)
27     : frame_width_(0),
28       frame_height_(0),
29       capture_failure_counter_(0),
30       camera_init_failure_counter_(0),
31       camera_thread_(new base::Thread(kCameraThreadName)),
32       delegate_(delegate) {
33   camera_thread_->Start();
34 }
35 
~CameraController()36 CameraController::~CameraController() {
37   if (camera_.get())
38     camera_->set_delegate(NULL);
39   {
40     // A ScopedAllowIO object is required to join the thread when calling Stop.
41     // See http://crosbug.com/11392.
42     base::ThreadRestrictions::ScopedAllowIO allow_io_for_thread_join;
43     camera_thread_.reset();
44   }
45 }
46 
Start()47 void CameraController::Start() {
48   camera_ = new Camera(this, camera_thread_.get(), true);
49   camera_->Initialize(frame_width_, frame_height_);
50 }
51 
Stop()52 void CameraController::Stop() {
53   if (!camera_.get())
54     return;
55   camera_->StopCapturing();
56   camera_->Uninitialize();
57 }
58 
GetFrame(SkBitmap * frame) const59 void CameraController::GetFrame(SkBitmap* frame) const {
60   if (camera_.get())
61     camera_->GetFrame(frame);
62 }
63 
OnInitializeSuccess()64 void CameraController::OnInitializeSuccess() {
65   DCHECK(camera_.get());
66   camera_->StartCapturing();
67 }
68 
OnInitializeFailure()69 void CameraController::OnInitializeFailure() {
70   ++camera_init_failure_counter_;
71   if (camera_init_failure_counter_ > kMaxCameraInitFailureCounter) {
72     if (delegate_)
73       delegate_->OnCaptureFailure();
74   } else {
75     // Retry initializing the camera.
76     Start();
77   }
78 }
79 
OnStartCapturingSuccess()80 void CameraController::OnStartCapturingSuccess() {
81 }
82 
OnStartCapturingFailure()83 void CameraController::OnStartCapturingFailure() {
84   // Try to reinitialize camera.
85   OnInitializeFailure();
86 }
87 
OnCaptureSuccess()88 void CameraController::OnCaptureSuccess() {
89   DCHECK(camera_.get());
90   capture_failure_counter_ = 0;
91   camera_init_failure_counter_ = 0;
92   if (delegate_)
93     delegate_->OnCaptureSuccess();
94 }
95 
OnCaptureFailure()96 void CameraController::OnCaptureFailure() {
97   ++capture_failure_counter_;
98   if (capture_failure_counter_ < kMaxCaptureFailureCounter)
99     return;
100 
101   capture_failure_counter_ = 0;
102   OnInitializeFailure();
103 }
104 
105 }  // namespace chromeos
106 
107