summaryrefslogtreecommitdiff
path: root/common/hal/aidl_service/aidl_camera_device.cc
blob: a086897e56b3801ab2ecd4a28304eb780247bf9b (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
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (C) 2022 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.
 */

#define LOG_TAG "GCH_AidlCameraDevice"
//#define LOG_NDEBUG 0
#include "aidl_camera_device.h"

#include <log/log.h>

#include "aidl_camera_device_session.h"
#include "aidl_profiler.h"
#include "aidl_utils.h"

namespace android {
namespace hardware {
namespace camera {
namespace device {
namespace implementation {

namespace aidl_utils = ::android::hardware::camera::implementation::aidl_utils;

using aidl::android::hardware::camera::common::Status;
using ::android::google_camera_hal::HalCameraMetadata;

const std::string AidlCameraDevice::kDeviceVersion = "1.1";

std::shared_ptr<AidlCameraDevice> AidlCameraDevice::Create(
    std::unique_ptr<CameraDevice> google_camera_device) {
  auto device = ndk::SharedRefBase::make<AidlCameraDevice>();
  if (device == nullptr) {
    ALOGE("%s: Cannot create a AidlCameraDevice.", __FUNCTION__);
    return nullptr;
  }

  status_t res = device->Initialize(std::move(google_camera_device));
  if (res != OK) {
    ALOGE("%s: Initializing AidlCameraDevice failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return nullptr;
  }

  return device;
}

status_t AidlCameraDevice::Initialize(
    std::unique_ptr<CameraDevice> google_camera_device) {
  if (google_camera_device == nullptr) {
    ALOGE("%s: google_camera_device is nullptr.", __FUNCTION__);
    return BAD_VALUE;
  }

  camera_id_ = google_camera_device->GetPublicCameraId();
  google_camera_device_ = std::move(google_camera_device);
  aidl_profiler_ = AidlProfiler::Create(camera_id_);
  if (aidl_profiler_ == nullptr) {
    ALOGE("%s: Failed to create AidlProfiler.", __FUNCTION__);
    return UNKNOWN_ERROR;
  }
  return OK;
}

ScopedAStatus AidlCameraDevice::getResourceCost(
    CameraResourceCost* resource_cost) {
  google_camera_hal::CameraResourceCost hal_cost;
  if (resource_cost == nullptr) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
  }
  status_t res = google_camera_device_->GetResourceCost(&hal_cost);
  if (res != OK) {
    ALOGE("%s: Getting resource cost failed for camera %u: %s(%d)",
          __FUNCTION__, camera_id_, strerror(-res), res);
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::INTERNAL_ERROR));
  }

  res = aidl_utils::ConvertToAidlResourceCost(hal_cost, resource_cost);
  if (res != OK) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::INTERNAL_ERROR));
  }

  return ScopedAStatus::ok();
}

ScopedAStatus AidlCameraDevice::getCameraCharacteristics(
    CameraMetadata* characteristics_ret) {
  if (characteristics_ret == nullptr) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
  }
  characteristics_ret->metadata.clear();
  std::unique_ptr<HalCameraMetadata> characteristics;
  status_t res =
      google_camera_device_->GetCameraCharacteristics(&characteristics);
  if (res != OK) {
    ALOGE("%s: Getting camera characteristics for camera %u failed: %s(%d)",
          __FUNCTION__, camera_id_, strerror(-res), res);
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::INTERNAL_ERROR));
  }

  if (characteristics == nullptr) {
    ALOGE("%s: Camera characteristics for camera %u is nullptr.", __FUNCTION__,
          camera_id_);
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::INTERNAL_ERROR));
  }

  uint32_t metadata_size = characteristics->GetCameraMetadataSize();
  uint8_t* chars_p = (uint8_t*)characteristics->GetRawCameraMetadata();
  characteristics_ret->metadata.assign(chars_p, chars_p + metadata_size);

  return ScopedAStatus::ok();
}

ScopedAStatus AidlCameraDevice::setTorchMode(bool on) {
  google_camera_hal::TorchMode hal_torch_mode;
  hal_torch_mode = on ? google_camera_hal::TorchMode::kOn
                      : google_camera_hal::TorchMode::kOff;
  auto res = google_camera_device_->SetTorchMode(hal_torch_mode);
  return aidl_utils::ConvertToAidlReturn(res);
}

ScopedAStatus AidlCameraDevice::turnOnTorchWithStrengthLevel(
    int32_t torch_strength) {
  status_t res =
      google_camera_device_->TurnOnTorchWithStrengthLevel(torch_strength);
  return aidl_utils::ConvertToAidlReturn(res);
}

ScopedAStatus AidlCameraDevice::getTorchStrengthLevel(int32_t* strength_level) {
  if (strength_level == nullptr) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
  }
  *strength_level = 0;
  int32_t torch_strength;
  status_t res = google_camera_device_->GetTorchStrengthLevel(torch_strength);
  if (res != OK) {
    ALOGE(
        "%s: Getting camera flash unit torch strength level for camera %u "
        "failed: %s(%d)",
        __FUNCTION__, camera_id_, strerror(-res), res);
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::INTERNAL_ERROR));
  }
  *strength_level = torch_strength;
  return ScopedAStatus::ok();
}

ScopedAStatus AidlCameraDevice::constructDefaultRequestSettings(
    RequestTemplate /*requestTemplate*/, CameraMetadata* requestSettings) {
  if (requestSettings == nullptr) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
  }
  return ScopedAStatus::fromServiceSpecificError(
      static_cast<int32_t>(Status::OPERATION_NOT_SUPPORTED));
}

ScopedAStatus AidlCameraDevice::isStreamCombinationWithSettingsSupported(
    const StreamConfiguration& /*streamConfiguration*/, bool* supported) {
  if (supported == nullptr) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
  }
  return ScopedAStatus::fromServiceSpecificError(
      static_cast<int32_t>(Status::OPERATION_NOT_SUPPORTED));
}

ScopedAStatus AidlCameraDevice::getSessionCharacteristics(
    const StreamConfiguration& session_config,
    CameraMetadata* characteristics_ret) {
  // Temporary check to make sure session configuration is valid.
  // As a mock implementation, we are just returning the camera characteristics
  // for now.
  google_camera_hal::StreamConfiguration stream_config;
  status_t res =
      aidl_utils::ConvertToHalStreamConfig(session_config, &stream_config);
  if (res != OK) {
    ALOGE("%s: ConvertToHalStreamConfig fail", __FUNCTION__);
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::INTERNAL_ERROR));
  }
  return getCameraCharacteristics(characteristics_ret);
}

ScopedAStatus AidlCameraDevice::getPhysicalCameraCharacteristics(
    const std::string& physicalCameraId, CameraMetadata* characteristics_ret) {
  if (characteristics_ret == nullptr) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
  }
  characteristics_ret->metadata.clear();
  std::unique_ptr<HalCameraMetadata> physical_characteristics;
  uint32_t physical_camera_id = atoi(physicalCameraId.c_str());
  status_t res = google_camera_device_->GetPhysicalCameraCharacteristics(
      physical_camera_id, &physical_characteristics);
  if (res != OK) {
    ALOGE("%s: Getting physical characteristics for camera %u failed: %s(%d)",
          __FUNCTION__, camera_id_, strerror(-res), res);
    return aidl_utils::ConvertToAidlReturn(res);
  }

  if (physical_characteristics == nullptr) {
    ALOGE("%s: Physical characteristics for camera %u is nullptr.",
          __FUNCTION__, physical_camera_id);
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::INTERNAL_ERROR));
  }

  uint32_t metadata_size = physical_characteristics->GetCameraMetadataSize();
  uint8_t* physical_characteristics_p =
      (uint8_t*)physical_characteristics->GetRawCameraMetadata();
  characteristics_ret->metadata.assign(
      physical_characteristics_p, physical_characteristics_p + metadata_size);
  return ScopedAStatus::ok();
}

ScopedAStatus AidlCameraDevice::open(
    const std::shared_ptr<ICameraDeviceCallback>& callback,
    std::shared_ptr<ICameraDeviceSession>* session_ret) {
  if (session_ret == nullptr) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
  }
  *session_ret = nullptr;
  auto profiler = aidl_profiler_->MakeScopedProfiler(
      AidlProfiler::ScopedType::kOpen,
      google_camera_device_->GetProfiler(camera_id_,
                                         aidl_profiler_->GetLatencyFlag()),
      google_camera_device_->GetProfiler(camera_id_,
                                         aidl_profiler_->GetFpsFlag()));

  std::unique_ptr<google_camera_hal::CameraDeviceSession> session;
  status_t res = google_camera_device_->CreateCameraDeviceSession(&session);
  if (res != OK || session == nullptr) {
    ALOGE("%s: Creating CameraDeviceSession failed: %s(%d)", __FUNCTION__,
          strerror(-res), res);
    return aidl_utils::ConvertToAidlReturn(res);
  }

  auto aidl_session = AidlCameraDeviceSession::Create(
      callback, std::move(session), aidl_profiler_);
  if (aidl_session == nullptr) {
    ALOGE("%s: Creating AidlCameraDeviceSession failed.", __FUNCTION__);
    return aidl_utils::ConvertToAidlReturn(res);
  }
  *session_ret = aidl_session;
  return ScopedAStatus::ok();
}

ScopedAStatus AidlCameraDevice::openInjectionSession(
    const std::shared_ptr<ICameraDeviceCallback>&,
    std::shared_ptr<ICameraInjectionSession>* session) {
  if (session == nullptr) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
  }
  *session = nullptr;
  return ScopedAStatus::fromServiceSpecificError(
      static_cast<int32_t>(Status::OPERATION_NOT_SUPPORTED));
}

binder_status_t AidlCameraDevice::dump(int fd, const char** /*args*/,
                                       uint32_t /*numArgs*/) {
  google_camera_device_->DumpState(fd);
  return OK;
}

ScopedAStatus AidlCameraDevice::isStreamCombinationSupported(
    const StreamConfiguration& streams, bool* supported) {
  if (supported == nullptr) {
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
  }
  *supported = false;
  google_camera_hal::StreamConfiguration stream_config;
  status_t res = aidl_utils::ConvertToHalStreamConfig(streams, &stream_config);
  if (res != OK) {
    ALOGE("%s: ConvertToHalStreamConfig fail", __FUNCTION__);
    return ScopedAStatus::fromServiceSpecificError(
        static_cast<int32_t>(Status::INTERNAL_ERROR));
  }
  *supported =
      google_camera_device_->IsStreamCombinationSupported(stream_config);
  return ScopedAStatus::ok();
}

}  // namespace implementation
}  // namespace device
}  // namespace camera
}  // namespace hardware
}  // namespace android