summaryrefslogtreecommitdiff
path: root/guest/hals/camera/EmulatedCameraFactory.cpp
blob: 857f2ed1188ed058c3a07ad3df9df8d6cd73391b (plain)
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
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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.
 */

/*
 * Contains implementation of a class EmulatedCameraFactory that manages cameras
 * available for emulation.
 */

#define LOG_NDEBUG 0
#define LOG_TAG "EmulatedCamera_Factory"
#include <log/log.h>
#include <cutils/properties.h>
#include "EmulatedFakeCamera.h"

#include "EmulatedCameraHotplugThread.h"
#include "EmulatedFakeCamera2.h"

#include "EmulatedFakeCamera3.h"

#include "EmulatedCameraFactory.h"

extern camera_module_t HAL_MODULE_INFO_SYM;

namespace android {
EmulatedCameraFactory& EmulatedCameraFactory::Instance() {
  static EmulatedCameraFactory* factory = new EmulatedCameraFactory;
  return *factory;
}

EmulatedCameraFactory::EmulatedCameraFactory()
    : mCallbacks(NULL)
{
  mCameraConfiguration.Init();
  const std::vector<cvd::CameraDefinition>& cameras =
      mCameraConfiguration.cameras();
  for (size_t camera_index = 0; camera_index < cameras.size(); ++camera_index) {
    mCameraDefinitions.push(cameras[camera_index]);
    /* Reserve a spot for camera, but don't create just yet. */
    mEmulatedCameras.push(NULL);
  }

  ALOGV("%zu cameras are being emulated.", getEmulatedCameraNum());

  /* Create hotplug thread */
  {
    mHotplugThread = new EmulatedCameraHotplugThread(getEmulatedCameraNum());
    mHotplugThread->run("EmulatedCameraHotplugThread");
  }
}

EmulatedBaseCamera* EmulatedCameraFactory::getOrCreateFakeCamera(
    size_t cameraId) {
  ::cvd::LockGuard< ::cvd::Mutex> lock(mEmulatedCamerasMutex);

  if (cameraId >= getEmulatedCameraNum()) {
    ALOGE("%s: Invalid camera ID: %zu", __FUNCTION__, cameraId);
    return NULL;
  }

  if (mEmulatedCameras[cameraId] != NULL) {
    return mEmulatedCameras[cameraId];
  }

  const cvd::CameraDefinition& definition = mCameraDefinitions[cameraId];
  bool is_back_facing =
      (definition.orientation == cvd::CameraDefinition::kBack);

  EmulatedBaseCamera* camera;
  /* Create, and initialize the fake camera */
  switch (definition.hal_version) {
    case cvd::CameraDefinition::kHalV1:
      camera = new EmulatedFakeCamera(cameraId, is_back_facing,
                                      &HAL_MODULE_INFO_SYM.common);
      break;
    case cvd::CameraDefinition::kHalV2:
      camera = new EmulatedFakeCamera2(cameraId, is_back_facing,
                                       &HAL_MODULE_INFO_SYM.common);
      break;
    case cvd::CameraDefinition::kHalV3:
      camera = new EmulatedFakeCamera3(cameraId, is_back_facing,
                                       &HAL_MODULE_INFO_SYM.common);
      break;
    default:
      ALOGE("%s: Unsupported camera hal version requested: %d", __FUNCTION__,
            definition.hal_version);
      return NULL;
  }

  ALOGI("%s: Camera device %zu hal version is %d", __FUNCTION__, cameraId,
        definition.hal_version);
  int res = camera->Initialize(definition);

  if (res != NO_ERROR) {
    ALOGE("%s: Unable to intialize camera %zu: %s (%d)", __FUNCTION__, cameraId,
          strerror(-res), res);
    delete camera;
    return NULL;
  }

  ALOGI("%s: Inserting camera", __FUNCTION__);
  mEmulatedCameras.replaceAt(camera, cameraId);
  ALOGI("%s: Done", __FUNCTION__);
  return camera;
}

EmulatedCameraFactory::~EmulatedCameraFactory() {
  for (size_t n = 0; n < mEmulatedCameras.size(); n++) {
    if (mEmulatedCameras[n] != NULL) {
      delete mEmulatedCameras[n];
    }
  }

  if (mHotplugThread != NULL) {
    mHotplugThread->requestExit();
    mHotplugThread->join();
  }
}

/****************************************************************************
 * Camera HAL API handlers.
 *
 * Each handler simply verifies existence of an appropriate EmulatedBaseCamera
 * instance, and dispatches the call to that instance.
 *
 ***************************************************************************/

int EmulatedCameraFactory::cameraDeviceOpen(int camera_id,
                                            hw_device_t** device) {
  ALOGV("%s: id = %d", __FUNCTION__, camera_id);

  *device = NULL;

  EmulatedBaseCamera* camera = getOrCreateFakeCamera(camera_id);
  if (camera == NULL) return -EINVAL;

  return camera->connectCamera(device);
}

int EmulatedCameraFactory::getCameraInfo(int camera_id,
                                         struct camera_info* info) {
  ALOGV("%s: id = %d", __FUNCTION__, camera_id);

  EmulatedBaseCamera* camera = getOrCreateFakeCamera(camera_id);
  if (camera == NULL) return -EINVAL;

  return camera->getCameraInfo(info);
}

int EmulatedCameraFactory::setCallbacks(
    const camera_module_callbacks_t* callbacks) {
  ALOGV("%s: callbacks = %p", __FUNCTION__, callbacks);

  mCallbacks = callbacks;

  return OK;
}

void EmulatedCameraFactory::getVendorTagOps(vendor_tag_ops_t* ops) {
  ALOGV("%s: ops = %p", __FUNCTION__, ops);

  // No vendor tags defined for emulator yet, so not touching ops
}

int EmulatedCameraFactory::setTorchMode(const char* camera_id, bool enabled) {
  ALOGV("%s: camera_id = %s, enabled =%d", __FUNCTION__, camera_id, enabled);

  EmulatedBaseCamera* camera = getOrCreateFakeCamera(atoi(camera_id));
  if (camera == NULL) return -EINVAL;

  return camera->setTorchMode(enabled);
}

/****************************************************************************
 * Camera HAL API callbacks.
 ***************************************************************************/

int EmulatedCameraFactory::device_open(const hw_module_t* module,
                                       const char* name, hw_device_t** device) {
  /*
   * Simply verify the parameters, and dispatch the call inside the
   * EmulatedCameraFactory instance.
   */

  if (module != &HAL_MODULE_INFO_SYM.common) {
    ALOGE("%s: Invalid module %p expected %p", __FUNCTION__, module,
          &HAL_MODULE_INFO_SYM.common);
    return -EINVAL;
  }
  if (name == NULL) {
    ALOGE("%s: NULL name is not expected here", __FUNCTION__);
    return -EINVAL;
  }

  return EmulatedCameraFactory::Instance().cameraDeviceOpen(atoi(name), device);
}

int EmulatedCameraFactory::get_number_of_cameras(void) {
  return EmulatedCameraFactory::Instance().getEmulatedCameraNum();
}

int EmulatedCameraFactory::get_camera_info(int camera_id,
                                           struct camera_info* info) {
  return EmulatedCameraFactory::Instance().getCameraInfo(camera_id, info);
}

int EmulatedCameraFactory::set_callbacks(
    const camera_module_callbacks_t* callbacks) {
  return EmulatedCameraFactory::Instance().setCallbacks(callbacks);
}

void EmulatedCameraFactory::get_vendor_tag_ops(vendor_tag_ops_t* ops) {
  EmulatedCameraFactory::Instance().getVendorTagOps(ops);
}

int EmulatedCameraFactory::open_legacy(const struct hw_module_t* /*module*/,
                                       const char* /*id*/,
                                       uint32_t /*halVersion*/,
                                       struct hw_device_t** /*device*/) {
  // Not supporting legacy open
  return -ENOSYS;
}

int EmulatedCameraFactory::set_torch_mode(const char* camera_id, bool enabled) {
  return EmulatedCameraFactory::Instance().setTorchMode(camera_id, enabled);
}

/********************************************************************************
 * Internal API
 *******************************************************************************/

void EmulatedCameraFactory::onStatusChanged(int cameraId, int newStatus) {
  EmulatedBaseCamera* cam = getOrCreateFakeCamera(cameraId);
  if (!cam) {
    ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
    return;
  }

  /**
   * (Order is important)
   * Send the callback first to framework, THEN close the camera.
   */

  if (newStatus == cam->getHotplugStatus()) {
    ALOGW("%s: Ignoring transition to the same status", __FUNCTION__);
    return;
  }

  const camera_module_callbacks_t* cb = mCallbacks;
  if (cb != NULL && cb->camera_device_status_change != NULL) {
    cb->camera_device_status_change(cb, cameraId, newStatus);
  }

  if (newStatus == CAMERA_DEVICE_STATUS_NOT_PRESENT) {
    cam->unplugCamera();
  } else if (newStatus == CAMERA_DEVICE_STATUS_PRESENT) {
    cam->plugCamera();
  }
}

void EmulatedCameraFactory::onTorchModeStatusChanged(int cameraId,
                                                     int newStatus) {
  EmulatedBaseCamera* cam = getOrCreateFakeCamera(cameraId);
  if (!cam) {
    ALOGE("%s: Invalid camera ID %d", __FUNCTION__, cameraId);
    return;
  }

  const camera_module_callbacks_t* cb = mCallbacks;
  if (cb != NULL && cb->torch_mode_status_change != NULL) {
    char id[10];
    sprintf(id, "%d", cameraId);
    cb->torch_mode_status_change(cb, id, newStatus);
  }
}

/********************************************************************************
 * Initializer for the static member structure.
 *******************************************************************************/

/* Entry point for camera HAL API. */
struct hw_module_methods_t EmulatedCameraFactory::mCameraModuleMethods = {
    .open = EmulatedCameraFactory::device_open};

}; /* namespace android */