Android-x86
Fork
Faire un don

  • R/O
  • HTTP
  • SSH
  • HTTPS

hardware-libcamera: Commit

hardware/libcamera


Commit MetaInfo

Révision937775cb914e2357047ed2ecee79d349a470927f (tree)
l'heure2012-04-30 03:35:19
AuteurAndres Rodriguez <andresx7@gmai...>
CommiterChih-Wei Huang

Message de Log

Add support for multiple cameras.

The camera layout can be specified in camera.cfg using the following format
$orientation $camerapath

Where orientation can be "front" or "back" and $camerapath is the path to the
V4L device to use as input.

Change Summary

Modification

--- a/CameraFactory.cpp
+++ b/CameraFactory.cpp
@@ -20,7 +20,11 @@
2020 */
2121
2222 #define LOG_NDEBUG 0
23+#define DEFAULT_DEVICE_FRONT "/dev/video1"
24+#define DEFAULT_DEVICE_BACK "/dev/video0"
25+#define CONFIG_FILE "/etc/camera.cfg"
2326 #define LOG_TAG "Camera_Factory"
27+
2428 #include <cutils/log.h>
2529 #include <cutils/properties.h>
2630 #include "CameraFactory.h"
@@ -34,18 +38,22 @@ android::CameraFactory gCameraFactory;
3438
3539 namespace android {
3640
37-CameraFactory::CameraFactory() : mCamera(NULL)
41+CameraFactory::CameraFactory()
3842 {
3943 LOGD("CameraFactory::CameraFactory");
44+ mCamera = NULL;
45+ mCameraDevices = NULL;
46+ mCameraOrientation = NULL;
47+ parseConfig(CONFIG_FILE);
4048 }
4149
4250 CameraFactory::~CameraFactory()
4351 {
4452 LOGD("CameraFactory::~CameraFactory");
45- if (mCamera != NULL) {
46- delete mCamera;
47- mCamera = NULL;
53+ for (int i=0; i < getCameraNum(); i++) {
54+ delete mCamera[i];
4855 }
56+ free(mCamera);
4957 }
5058
5159 /****************************************************************************
@@ -62,29 +70,28 @@ int CameraFactory::cameraDeviceOpen(const hw_module_t* module,int camera_id, hw_
6270
6371 *device = NULL;
6472
65- if (camera_id < 0 || camera_id >= getCameraNum()) {
73+ if (!mCamera || camera_id < 0 || camera_id >= getCameraNum()) {
6674 LOGE("%s: Camera id %d is out of bounds (%d)",
6775 __FUNCTION__, camera_id, getCameraNum());
6876 return -EINVAL;
6977 }
7078
71- if (!mCamera)
72- mCamera = new CameraHardware(module);
73-
74- return mCamera->connectCamera(device);
79+ if (!mCamera[camera_id]) {
80+ mCamera[camera_id] = new CameraHardware(module, mCameraDevices[camera_id]);
81+ }
82+ return mCamera[camera_id]->connectCamera(device);
7583 }
7684
7785 /* Returns the number of available cameras */
7886 int CameraFactory::getCameraNum()
7987 {
80- LOGD("CameraFactory::getCameraNum");
81- return 1;
88+ LOGD("CameraFactory::getCameraNum: %d", mCameraNum);
89+ return mCameraNum;
8290 }
8391
84-
8592 int CameraFactory::getCameraInfo(int camera_id, struct camera_info* info)
8693 {
87- LOGD("CameraFactory::getCameraInfo: id = %d,info = %p", camera_id,info);
94+ LOGD("CameraFactory::getCameraInfo: id = %d, info = %p", camera_id, info);
8895
8996 if (camera_id < 0 || camera_id >= getCameraNum()) {
9097 LOGE("%s: Camera id %d is out of bounds (%d)",
@@ -92,9 +99,65 @@ int CameraFactory::getCameraInfo(int camera_id, struct camera_info* info)
9299 return -EINVAL;
93100 }
94101
102+ return CameraHardware::getCameraInfo(info, mCameraOrientation[camera_id]);
103+}
95104
96- LOGD("CameraFactory::getCameraInfo: about to fetch info");
97- return CameraHardware::getCameraInfo(info);
105+// Parse a simple configuration file
106+void CameraFactory::parseConfig(const char* configFile)
107+{
108+ LOGD("CameraFactory::parseConfig: configFile = %s", configFile);
109+
110+ FILE* config = fopen(configFile, "r");
111+ if (config != NULL) {
112+ char line[128];
113+ char arg1[128];
114+ char arg2[128];
115+
116+ while (fgets(line, sizeof line, config) != NULL) {
117+ int lineStart = strspn(line, " \t\n\v" );
118+
119+ if (line[lineStart] == '#')
120+ continue;
121+
122+ sscanf(line, "%s %s", arg1, arg2);
123+
124+ if (strcmp(arg1, "front")) {
125+ newCameraConfig(CAMERA_FACING_FRONT, arg2);
126+ } else if (strcmp(arg1, "back")) {
127+ newCameraConfig(CAMERA_FACING_BACK, arg2);
128+ } else {
129+ LOGD("CameraFactory::parseConfig: Unrecognized config line '%s'", line);
130+ }
131+ }
132+ } else {
133+ LOGD("%s not found, using camera configuration defaults", CONFIG_FILE);
134+ if (access(DEFAULT_DEVICE_BACK, F_OK) != -1){
135+ LOGD("Found device %s", DEFAULT_DEVICE_BACK);
136+ newCameraConfig(CAMERA_FACING_BACK, DEFAULT_DEVICE_BACK);
137+ }
138+ if (access(DEFAULT_DEVICE_FRONT, F_OK) != -1){
139+ LOGD("Found device %s", DEFAULT_DEVICE_FRONT);
140+ newCameraConfig(CAMERA_FACING_FRONT, DEFAULT_DEVICE_FRONT);
141+ }
142+ }
143+}
144+
145+// Although realloc could be a costly operation, we only execute this function usually 2 times
146+void CameraFactory::newCameraConfig(int facing, const char* location)
147+{
148+ // Keep track of cameras
149+ mCameraNum++;
150+
151+ // Grow the information arrays
152+ mCamera = (CameraHardware**) realloc(mCamera, mCameraNum * sizeof(CameraHardware*));
153+ mCameraDevices = (char**) realloc(mCameraDevices, mCameraNum * sizeof(char*));
154+ mCameraOrientation = (int*) realloc(mCameraOrientation, mCameraNum * sizeof(int));
155+
156+ // Store the values for each camera_id
157+ mCamera[mCameraNum - 1] = NULL;
158+ mCameraDevices[mCameraNum - 1] = strdup(location);
159+ mCameraOrientation[mCameraNum - 1] = facing;
160+ LOGD("CameraFactory::newCameraConfig: %d -> %s", mCameraOrientation[mCameraNum - 1], mCameraDevices[mCameraNum - 1]);
98161 }
99162
100163 /****************************************************************************
@@ -122,7 +185,8 @@ int CameraFactory::device_open(const hw_module_t* module,
122185 return -EINVAL;
123186 }
124187
125- return gCameraFactory.cameraDeviceOpen(module,atoi(name), device);
188+ int camera_id = atoi(name);
189+ return gCameraFactory.cameraDeviceOpen(module, camera_id, device);
126190 }
127191
128192 int CameraFactory::get_number_of_cameras(void)
--- a/CameraFactory.h
+++ b/CameraFactory.h
@@ -72,11 +72,10 @@ public:
7272 /* Gets emulated camera information.
7373 * This method is called in response to camera_module_t::get_camera_info callback.
7474 */
75- static int getCameraInfo(int camera_id, struct camera_info *info);
76-
75+ int getCameraInfo(int camera_id, struct camera_info *info);
7776
7877 /* Returns the number of available cameras */
79- static int getCameraNum();
78+ int getCameraNum();
8079
8180 /****************************************************************************
8281 * Camera HAL API callbacks.
@@ -95,10 +94,16 @@ private:
9594 const char* name,
9695 hw_device_t** device);
9796
97+ void parseConfig(const char* configFile);
98+ void newCameraConfig(int facing, const char* location);
99+
98100 private:
99101
100102 /* Camera hardware */
101- CameraHardware* mCamera;
103+ CameraHardware** mCamera;
104+ char** mCameraDevices;
105+ int* mCameraOrientation;
106+ int mCameraNum;
102107
103108 public:
104109 /* Contains device open entry point, as required by HAL API. */
--- a/CameraHardware.cpp
+++ b/CameraHardware.cpp
@@ -28,7 +28,6 @@
2828 #include "CameraHardware.h"
2929 #include "Converter.h"
3030
31-#define VIDEO_DEVICE "/dev/video0"
3231 #define MIN_WIDTH 320
3332 #define MIN_HEIGHT 240
3433
@@ -132,7 +131,7 @@ bool CameraHardware::PowerOn()
132131 int timeOut = 500;
133132 do {
134133 // Try to open the video capture device
135- handle = ::open(VIDEO_DEVICE,O_RDWR);
134+ handle = ::open(mVideoDevice,O_RDWR);
136135 if (handle >= 0)
137136 break;
138137 // Wait a bit
@@ -170,7 +169,7 @@ bool CameraHardware::PowerOff()
170169 return true;
171170 }
172171
173-CameraHardware::CameraHardware(const hw_module_t* module) :
172+CameraHardware::CameraHardware(const hw_module_t* module, char* devLocation) :
174173 mWin(0),
175174 mPreviewWinFmt(PIXEL_FORMAT_UNKNOWN),
176175 mPreviewWinWidth(0),
@@ -211,6 +210,9 @@ CameraHardware::CameraHardware(const hw_module_t* module) :
211210 mCurrentRecordingFrame(0),
212211 mCameraPowerFile(0)
213212 {
213+ //Store the video device location
214+ mVideoDevice = devLocation;
215+
214216 /*
215217 * Initialize camera_device descriptor for this object.
216218 */
@@ -319,11 +321,11 @@ status_t CameraHardware::closeCamera()
319321 return NO_ERROR;
320322 }
321323
322-status_t CameraHardware::getCameraInfo(struct camera_info* info)
324+status_t CameraHardware::getCameraInfo(struct camera_info* info, int facing)
323325 {
324326 LOGD("CameraHardware::getCameraInfo");
325327
326- info->facing = CAMERA_FACING_FRONT;
328+ info->facing = facing;
327329 info->orientation = 0;
328330
329331 return NO_ERROR;
@@ -476,7 +478,7 @@ status_t CameraHardware::startPreviewLocked()
476478
477479 LOGD("CameraHardware::startPreviewLocked: Open, %dx%d", width, height);
478480
479- status_t ret = camera.Open(VIDEO_DEVICE);
481+ status_t ret = camera.Open(mVideoDevice);
480482 if (ret != NO_ERROR) {
481483 LOGE("Failed to initialize Camera");
482484 return ret;
@@ -810,7 +812,7 @@ void CameraHardware::initDefaultParameters()
810812 SortedVector<SurfaceSize> avSizes;
811813 SortedVector<int> avFps;
812814
813- if (camera.Open(VIDEO_DEVICE) != NO_ERROR) {
815+ if (camera.Open(mVideoDevice) != NO_ERROR) {
814816 LOGE("cannot open device.");
815817 } else {
816818
@@ -1644,7 +1646,7 @@ int CameraHardware::pictureThread()
16441646
16451647 LOGD("CameraHardware::pictureThread: taking picture (%d x %d)", w, h);
16461648
1647- if (camera.Open(VIDEO_DEVICE) == NO_ERROR) {
1649+ if (camera.Open(mVideoDevice) == NO_ERROR) {
16481650 camera.Init(w, h, 1);
16491651
16501652 /* Retrieve the real size being used */
--- a/CameraHardware.h
+++ b/CameraHardware.h
@@ -182,7 +182,7 @@ public:
182182 * instance in camera factory's array.
183183 * module - Emulated camera HAL module descriptor.
184184 */
185- CameraHardware(const hw_module_t* module);
185+ CameraHardware(const hw_module_t* module, char* devLocation);
186186
187187 /* Destructs EmulatedCamera instance. */
188188 virtual ~CameraHardware();
@@ -212,7 +212,7 @@ public:
212212 * NOTE: When this method is called the object is locked.
213213 * Note that failures in this method are reported as negave EXXX statuses.
214214 */
215- static status_t getCameraInfo(struct camera_info* info);
215+ static status_t getCameraInfo(struct camera_info* info, int facing);
216216
217217 private:
218218
@@ -251,7 +251,7 @@ private:
251251 int mPreviewWinHeight;
252252
253253 CameraParameters mParameters;
254-
254+ char* mVideoDevice;
255255
256256 camera_memory_t* mRawPreviewHeap;
257257 int mRawPreviewFrameSize;
Afficher sur ancien navigateur de dépôt.