OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_H_ |
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_H_ | 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/lock.h" | 12 #include "base/lock.h" |
13 #include "base/ref_counted.h" | 13 #include "base/ref_counted.h" |
14 #include "base/thread.h" | 14 #include "base/thread.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
16 | 16 |
17 class Task; | 17 class Task; |
18 namespace base { | 18 namespace base { |
19 class TimeDelta; | 19 class Thread; |
20 } // namespace base | 20 } // namespace base |
21 | 21 |
22 namespace chromeos { | 22 namespace chromeos { |
23 | 23 |
24 // Class that wraps interaction with video capturing device. Returns | 24 // Class that wraps interaction with video capturing device. Returns |
25 // frames captured with specified intervals of time via delegate interface. | 25 // frames captured with specified intervals of time via delegate interface. |
26 // All communication with camera driver is performed on a separate camera | 26 // All communication with camera driver is performed on a separate camera |
27 // thread. Delegate's callback are called on UI thread. | 27 // thread. Delegate's callback are called on UI thread. |
28 class Camera : public base::RefCountedThreadSafe<Camera> { | 28 class Camera : public base::RefCountedThreadSafe<Camera> { |
29 public: | 29 public: |
(...skipping 12 matching lines...) Expand all Loading... |
42 | 42 |
43 // Notifies the delegate that new frame was captured. | 43 // Notifies the delegate that new frame was captured. |
44 // The frame can be obtained via GetFrame() method. | 44 // The frame can be obtained via GetFrame() method. |
45 virtual void OnCaptureSuccess() = 0; | 45 virtual void OnCaptureSuccess() = 0; |
46 | 46 |
47 // Notifies the delegate that we failed to capture the next frame. | 47 // Notifies the delegate that we failed to capture the next frame. |
48 virtual void OnCaptureFailure() = 0; | 48 virtual void OnCaptureFailure() = 0; |
49 }; | 49 }; |
50 | 50 |
51 // Initializes object members. |delegate| is object that will receive | 51 // Initializes object members. |delegate| is object that will receive |
52 // notifications about success of async method calls. |mirrored| | 52 // notifications about success of async method calls. |thread| is a thread |
53 // determines if the returned video image is mirrored horizontally. | 53 // to post blocking tasks to. |mirrored| determines if the returned video |
54 Camera(Delegate* delegate, bool mirrored); | 54 // image is mirrored horizontally. |
| 55 Camera(Delegate* delegate, base::Thread* thread, bool mirrored); |
55 | 56 |
56 // Initializes camera device on camera thread. Corresponding delegate's | 57 // Initializes camera device on camera thread. Corresponding delegate's |
57 // callback is called on UI thread to notify about success or failure. Does | 58 // callback is called on UI thread to notify about success or failure. Does |
58 // nothing if camera is successfully initialized already. Sets the desired | 59 // nothing if camera is successfully initialized already. Sets the desired |
59 // width and height of the frame to receive from camera. | 60 // width and height of the frame to receive from camera. |
60 void Initialize(int desired_width, int desired_height); | 61 void Initialize(int desired_width, int desired_height); |
61 | 62 |
62 // Uninitializes the camera on camera thread. Can be called anytime, any | 63 // Uninitializes the camera on camera thread. Can be called anytime, any |
63 // number of times. | 64 // number of times. |
64 void Uninitialize(); | 65 void Uninitialize(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 bool ReadFrame(); | 105 bool ReadFrame(); |
105 | 106 |
106 // Transforms raw data received from camera into SkBitmap with desired | 107 // Transforms raw data received from camera into SkBitmap with desired |
107 // size and notifies the delegate that the image is ready. | 108 // size and notifies the delegate that the image is ready. |
108 void ProcessImage(void* data); | 109 void ProcessImage(void* data); |
109 | 110 |
110 // Actual routines that run on camera thread and call delegate's callbacks. | 111 // Actual routines that run on camera thread and call delegate's callbacks. |
111 // See the corresponding methods without Do prefix for details. | 112 // See the corresponding methods without Do prefix for details. |
112 void DoInitialize(int desired_width, int desired_height); | 113 void DoInitialize(int desired_width, int desired_height); |
113 void DoStartCapturing(); | 114 void DoStartCapturing(); |
| 115 void DoUninitialize(); |
| 116 void DoStopCapturing(); |
114 | 117 |
115 // Helper method that reports failure to the delegate via method | 118 // Helper method that reports failure to the delegate via method |
116 // corresponding to the current state of the object. | 119 // corresponding to the current state of the object. |
117 void ReportFailure(); | 120 void ReportFailure(); |
118 | 121 |
119 // Methods called on UI thread to call delegate. | 122 // Methods called on UI thread to call delegate. |
120 void OnInitializeSuccess(); | 123 void OnInitializeSuccess(); |
121 void OnInitializeFailure(); | 124 void OnInitializeFailure(); |
122 void OnStartCapturingSuccess(); | 125 void OnStartCapturingSuccess(); |
123 void OnStartCapturingFailure(); | 126 void OnStartCapturingFailure(); |
124 void OnCaptureSuccess(); | 127 void OnCaptureSuccess(); |
125 void OnCaptureFailure(); | 128 void OnCaptureFailure(); |
126 | 129 |
127 // Camera thread routines that implement the corresponding public methods. | |
128 void DoUninitialize(); | |
129 void DoStopCapturing(); | |
130 | |
131 // All methods on the camera thread need to call this to post back to the UI | |
132 // thread. Otherwise the camera object could be deleted on the camera thread | |
133 // which is not allowed. | |
134 void PostCameraThreadAck(); | |
135 void CameraThreadAck(); | |
136 | |
137 // Returns true if the code is executed on camera thread right now, false | 130 // Returns true if the code is executed on camera thread right now, false |
138 // otherwise. | 131 // otherwise. |
139 bool IsOnCameraThread() const; | 132 bool IsOnCameraThread() const; |
140 | 133 |
141 // Posts task to camera thread. | 134 // Posts task to camera thread. |
142 void PostCameraTask(Task* task); | 135 void PostCameraTask(const tracked_objects::Location& from_here, |
| 136 Task* task); |
143 | 137 |
144 // Defines a buffer in memory where one frame from the camera is stored. | 138 // Defines a buffer in memory where one frame from the camera is stored. |
145 struct VideoBuffer { | 139 struct VideoBuffer { |
146 void* start; | 140 void* start; |
147 size_t length; | 141 size_t length; |
148 }; | 142 }; |
149 | 143 |
150 // Delegate that receives the frames from the camera. | 144 // Delegate that receives the frames from the camera. |
151 // Delegate is accessed only on UI thread. | 145 // Delegate is accessed only on UI thread. |
152 Delegate* delegate_; | 146 Delegate* delegate_; |
153 | 147 |
154 // Thread where all work with the device is going on. | 148 // Thread where all work with the device is going on. |
155 base::Thread camera_thread_; | 149 base::Thread* thread_; |
156 | 150 |
157 // All the members below are accessed only on camera thread. | 151 // All the members below are accessed only on camera thread. |
158 // Name of the device file, i.e. "/dev/video0". | 152 // Name of the device file, i.e. "/dev/video0". |
159 std::string device_name_; | 153 std::string device_name_; |
160 | 154 |
161 // File descriptor of the opened device. | 155 // File descriptor of the opened device. |
162 int device_descriptor_; | 156 int device_descriptor_; |
163 | 157 |
164 // Vector of buffers where to store video frames from camera. | 158 // Vector of buffers where to store video frames from camera. |
165 std::vector<VideoBuffer> buffers_; | 159 std::vector<VideoBuffer> buffers_; |
(...skipping 14 matching lines...) Expand all Loading... |
180 int frame_height_; | 174 int frame_height_; |
181 | 175 |
182 // If set to true, the returned image will be reflected from the Y axis to | 176 // If set to true, the returned image will be reflected from the Y axis to |
183 // mimic mirror behavior. | 177 // mimic mirror behavior. |
184 bool mirrored_; | 178 bool mirrored_; |
185 | 179 |
186 // Image where camera frames are stored for UI thread to pick up. | 180 // Image where camera frames are stored for UI thread to pick up. |
187 SkBitmap frame_image_; | 181 SkBitmap frame_image_; |
188 | 182 |
189 // Lock that guards references to |frame_image_|. | 183 // Lock that guards references to |frame_image_|. |
190 static Lock image_lock_; | 184 mutable Lock image_lock_; |
191 | 185 |
192 // Lock that guards references to |camera_thread_|. | 186 // Lock that guards references to |camera_thread_|. |
193 static Lock thread_lock_; | 187 mutable Lock thread_lock_; |
194 | 188 |
195 DISALLOW_COPY_AND_ASSIGN(Camera); | 189 DISALLOW_COPY_AND_ASSIGN(Camera); |
196 }; | 190 }; |
197 | 191 |
198 } // namespace chromeos | 192 } // namespace chromeos |
199 | 193 |
200 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_H_ | 194 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_CAMERA_H_ |
OLD | NEW |