liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright (c) 2021 by Allwinnertech Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#define LOG_TAG "CameraHALv3_4"
#include "v4l2_camera_hal.h"
 
#include <android-base/parseint.h>
#include <dirent.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
 
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <string>
#include <unordered_set>
#include <iostream>
#include <sstream>
 
#include "camera_config.h"
#include "v4l2_camera.h"
 
/*
 * This file serves as the entry point to the HAL. It is modified from the
 * example default HAL available in hardware/libhardware/modules/camera.
 * It contains the module structure and functions used by the framework
 * to load and interface to this HAL, as well as the handles to the individual
 * camera devices.
 */
 
namespace v4l2_camera_hal {
 
// Default global camera hal.
static V4L2CameraHAL gCameraHAL;
 
V4L2CameraHAL::V4L2CameraHAL() : mCameras(), mCallbacks(NULL) {
  HAL_LOG_ENTER();
 
  // Find /dev/video* nodes.
  // Fix stream is not manager now.
  int ret;
  std::string node;
 
  // Test each for V4L2 support and uniqueness.
  int id = 0;
  int i = 0;
  char dev_node[16];
  sprintf(dev_node, "/dev/video%d", i);
  ret = access(dev_node, F_OK);
  if (ret == 0) {
    HAL_LOGV("Found video node %s.", dev_node);
  } else {
    HAL_LOGE("Cannot found video node %s. %s", dev_node, strerror(errno));
  }
 
  hal_merge_stream_flag = false;
  char * s_value;
  int pic_width = 0;
  int pic_height = 0;
  std::string tmp;
  std::vector<std::string> s_data;
  // id == 0 camera facing back;
  // id == 1 camera facing front;
 
  int MAX_NUM_OF_CAMERAS = CCameraConfig::numberOfCamera();
  for (id = 0; id <MAX_NUM_OF_CAMERAS; id ++) {
    int cameraId = getSupportCameraId(id);
    // camera config information
    std::shared_ptr<CCameraConfig> config =
        std::make_shared<CCameraConfig>(cameraId);
    if (config == 0) {
      HAL_LOGW("create CCameraConfig failed");
    } else {
      config->initParameters();
      config->dumpParameters();
    }
    mCameraConfig.push_back(config);
 
    s_value = config->supportPictureSizeValue();
    std::string st1 = s_value;
    std::stringstream input(st1);
 
    while (getline(input, tmp, ',')) {
        s_data.push_back(tmp);
    }
    for(auto s : s_data) {
        sscanf(s.c_str(), "%dx%d", &pic_width,&pic_height);
        if(pic_width * pic_height > 4000*3000) {
            hal_merge_stream_flag = true;
        }
    }
 
    V4L2Camera* cam(V4L2Camera::NewV4L2Camera(id,
                                              config,
                                              hal_merge_stream_flag));
    if (cam) {
      mCameras.push_back(cam);
    } else {
      HAL_LOGE("Failed to initialize camera at %s.", dev_node);
    }
    mCameraConfig.push_back(config);
    if (getSingleCameraId() == 0)
      break;
  }
}
 
V4L2CameraHAL::~V4L2CameraHAL() {
  HAL_LOG_ENTER();
  int MAX_NUM_OF_CAMERAS = CCameraConfig::numberOfCamera();
  for (int n = 0; n < MAX_NUM_OF_CAMERAS; n++) {
    if (mCameraConfig[n] != NULL) {
      // delete mCameraConfig[n];
      mCameraConfig[n] = NULL;
    }
    if (getSingleCameraId() == 0)
      break;
  }
}
 
int V4L2CameraHAL::getNumberOfCameras() {
  HAL_LOGV("returns %zu", mCameras.size());
  return mCameras.size();
}
 
int V4L2CameraHAL::getCameraInfo(int id, camera_info_t* info) {
  HAL_LOG_ENTER();
  if (id < 0 || id >= static_cast<int>(mCameras.size())) {
    return -EINVAL;
  }
  // TODO(b/29185945): Hotplugging: return -EINVAL if unplugged.
  return mCameras[id]->getInfo(info);
}
 
int V4L2CameraHAL::setCallbacks(const camera_module_callbacks_t* callbacks) {
  HAL_LOG_ENTER();
  mCallbacks = callbacks;
  return 0;
}
 
void V4L2CameraHAL::getVendorTagOps(vendor_tag_ops_t* UNUSED(ops)) {
  HAL_LOG_ENTER();
  // No vendor ops for this HAL. From <hardware/camera_common.h>:
  // "leave ops unchanged if no vendor tags are defined."
}
 
int V4L2CameraHAL::openLegacy(const hw_module_t* /*module*/,
                              const char* /*id*/,
                              uint32_t /*halVersion*/,
                              hw_device_t** /*device*/) {
  HAL_LOG_ENTER();
  // Not supported.
  return -ENOSYS;
}
 
int V4L2CameraHAL::setTorchMode(const char* camera_id, bool enabled) {
  HAL_LOG_ENTER();
  // TODO(b/29158098): HAL is required to respond appropriately if
  // the desired camera actually does support flash.
  int retVal = 0;
  int cam_id = std::atoi(camera_id);
  if (mCameraConfig[cam_id] != NULL) {
    if (!mCameraConfig[cam_id]->supportFlashMode()) {
      return -ENOSYS;
    }
  }
 
  HAL_LOGD("enabled:%d cam_id:%d", enabled, cam_id);
  if (cam_id == 1 && mCallbacks != NULL) {
    HAL_LOGV("no flash!cam_id:%d", cam_id);
    mCallbacks->torch_mode_status_change(mCallbacks,
                                         camera_id,
                                         TORCH_MODE_STATUS_NOT_AVAILABLE);
  } else {
    if (enabled) {
      retVal = mCameras[cam_id]->setFlashTorchMode(enabled);
      if (mCallbacks != NULL && !retVal) {
        mCallbacks->torch_mode_status_change(mCallbacks,
                                             camera_id,
                                             TORCH_MODE_STATUS_AVAILABLE_ON);
      }
    } else {
      retVal = mCameras[cam_id]->setFlashTorchMode(enabled);
      if (mCallbacks != NULL && !retVal) {
        mCallbacks->torch_mode_status_change(mCallbacks,
                                             camera_id,
                                             TORCH_MODE_STATUS_AVAILABLE_OFF);
      }
    }
  }
  return retVal;
}
 
int V4L2CameraHAL::openDevice(const hw_module_t* module,
                              const char* name,
                              hw_device_t** device) {
  HAL_LOG_ENTER();
 
  if (module != &HAL_MODULE_INFO_SYM.common) {
    HAL_LOGE(
        "Invalid module %p expected %p", module, &HAL_MODULE_INFO_SYM.common);
    return -EINVAL;
  }
 
  int id;
  if (!android::base::ParseInt(name, &id, 0, getNumberOfCameras() - 1)) {
    return -EINVAL;
  }
  if (mCameraConfig[id]->supportCameraMulplex()) {
    if (mCameras[id]->isBusy()) {
      return -EUSERS;
    }
  } else {
    uint32_t id_tmp;
    for (id_tmp = 0; id_tmp < mCameras.size(); id_tmp++) {
      if (mCameras[id_tmp]->isBusy()) {
        return -EUSERS;
      }
    }
  }
 
  if (mCallbacks != NULL) {
    const char* camera_id = "0";
    setTorchMode(camera_id, false);
    mCameras[0]->closeFlashTorch();
  }
  // TODO(b/29185945): Hotplugging: return -EINVAL if unplugged.
 
  return mCameras[id]->openDevice(module, device);
}
 
/*
 * The framework calls the following wrappers, which in turn
 * call the corresponding methods of the global HAL object.
 */
 
static int get_number_of_cameras() {
  return gCameraHAL.getNumberOfCameras();
}
 
static int get_camera_info(int id, struct camera_info* info) {
  return gCameraHAL.getCameraInfo(id, info);
}
 
static int set_callbacks(const camera_module_callbacks_t* callbacks) {
  return gCameraHAL.setCallbacks(callbacks);
}
 
static void get_vendor_tag_ops(vendor_tag_ops_t* ops) {
  return gCameraHAL.getVendorTagOps(ops);
}
 
static int open_legacy(const hw_module_t* module,
                       const char* id,
                       uint32_t halVersion,
                       hw_device_t** device) {
  return gCameraHAL.openLegacy(module, id, halVersion, device);
}
 
static int set_torch_mode(const char* camera_id, bool enabled) {
  return gCameraHAL.setTorchMode(camera_id, enabled);
}
 
static int open_dev(const hw_module_t* module,
                    const char* name,
                    hw_device_t** device) {
  return gCameraHAL.openDevice(module, name, device);
}
 
}  // namespace v4l2_camera_hal
 
static hw_module_methods_t v4l2_module_methods = {
  .open = v4l2_camera_hal::open_dev};
 
camera_module_t HAL_MODULE_INFO_SYM __attribute__((visibility("default"))) = {
  .common = {
    .tag = HARDWARE_MODULE_TAG,
    .module_api_version = CAMERA_MODULE_API_VERSION_2_4,
    .hal_api_version = HARDWARE_HAL_API_VERSION,
    .id = CAMERA_HARDWARE_MODULE_ID,
    .name = "V4L2 Camera HAL v3",
    .author = "ZJW",
    .methods = &v4l2_module_methods,
    .dso = nullptr,
    .reserved = {0},
  },
  .get_number_of_cameras = v4l2_camera_hal::get_number_of_cameras,
  .get_camera_info = v4l2_camera_hal::get_camera_info,
  .set_callbacks = v4l2_camera_hal::set_callbacks,
  .get_vendor_tag_ops = v4l2_camera_hal::get_vendor_tag_ops,
  .open_legacy = v4l2_camera_hal::open_legacy,
  .set_torch_mode = v4l2_camera_hal::set_torch_mode,
  .init = nullptr,
  .reserved = {nullptr, nullptr}};