blob: 6f9bc7ca2a6cd3e2c5d60fa89414a0b567cc5ce0 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CameraDeviceClient"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
Jianing Weicb0652e2014-03-12 18:29:36 -070019//#define LOG_NDEBUG 0
Igor Murashkine7ee7632013-06-11 18:10:18 -070020
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070021#include <cutils/properties.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070022#include <utils/Log.h>
23#include <utils/Trace.h>
Igor Murashkine7ee7632013-06-11 18:10:18 -070024#include <gui/Surface.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070025#include <camera/camera2/CaptureRequest.h>
Ruben Brunk5698d442014-06-18 10:39:40 -070026#include <camera/CameraUtils.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070027
28#include "common/CameraDeviceBase.h"
29#include "api2/CameraDeviceClient.h"
30
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080031// Convenience methods for constructing binder::Status objects for error returns
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070032
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080033#define STRINGIZE_IMPL(x) #x
34#define STRINGIZE(x) STRINGIZE_IMPL(x)
35
36#define STATUS_ERROR(errorCode, errorString) \
37 binder::Status::fromServiceSpecificError(errorCode, \
38 String8(STRINGIZE(__FUNCTION__) ":" STRINGIZE(__LINE__) ":" # errorString))
39
40#define STATUS_ERROR_FMT(errorCode, errorString, ...) \
41 binder::Status::fromServiceSpecificError(errorCode, \
42 String8::format(STRINGIZE(__FUNCTION__) ":" STRINGIZE(__LINE__) ":" # errorString, \
43 __VA_ARGS__))
Igor Murashkine7ee7632013-06-11 18:10:18 -070044
45namespace android {
46using namespace camera2;
47
48CameraDeviceClientBase::CameraDeviceClientBase(
49 const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080050 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070051 const String16& clientPackageName,
52 int cameraId,
53 int cameraFacing,
54 int clientPid,
55 uid_t clientUid,
56 int servicePid) :
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080057 BasicClient(cameraService,
Marco Nelissenf8880202014-11-14 07:58:25 -080058 IInterface::asBinder(remoteCallback),
Eino-Ville Talvalae992e752014-11-07 16:17:48 -080059 clientPackageName,
60 cameraId,
61 cameraFacing,
62 clientPid,
63 clientUid,
64 servicePid),
Igor Murashkine7ee7632013-06-11 18:10:18 -070065 mRemoteCallback(remoteCallback) {
66}
Igor Murashkine7ee7632013-06-11 18:10:18 -070067
68// Interface used by CameraService
69
70CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080071 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
72 const String16& clientPackageName,
73 int cameraId,
74 int cameraFacing,
75 int clientPid,
76 uid_t clientUid,
77 int servicePid) :
Igor Murashkine7ee7632013-06-11 18:10:18 -070078 Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
79 cameraId, cameraFacing, clientPid, clientUid, servicePid),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070080 mInputStream(),
Igor Murashkine7ee7632013-06-11 18:10:18 -070081 mRequestIdCounter(0) {
82
83 ATRACE_CALL();
84 ALOGI("CameraDeviceClient %d: Opened", cameraId);
85}
86
Yin-Chia Yehe074a932015-01-30 10:29:02 -080087status_t CameraDeviceClient::initialize(CameraModule *module)
Igor Murashkine7ee7632013-06-11 18:10:18 -070088{
89 ATRACE_CALL();
90 status_t res;
91
92 res = Camera2ClientBase::initialize(module);
93 if (res != OK) {
94 return res;
95 }
96
97 String8 threadName;
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070098 mFrameProcessor = new FrameProcessorBase(mDevice);
Igor Murashkine7ee7632013-06-11 18:10:18 -070099 threadName = String8::format("CDU-%d-FrameProc", mCameraId);
100 mFrameProcessor->run(threadName.string());
101
102 mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
103 FRAME_PROCESSOR_LISTENER_MAX_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -0800104 /*listener*/this,
Zhijun He25a0aef2014-06-25 11:40:02 -0700105 /*sendPartials*/true);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700106
107 return OK;
108}
109
110CameraDeviceClient::~CameraDeviceClient() {
111}
112
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800113binder::Status CameraDeviceClient::submitRequest(
114 const hardware::camera2::CaptureRequest& request,
115 bool streaming,
116 /*out*/
117 hardware::camera2::utils::SubmitInfo *submitInfo) {
118 std::vector<hardware::camera2::CaptureRequest> requestList = { request };
119 return submitRequestList(requestList, streaming, submitInfo);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700120}
121
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800122binder::Status CameraDeviceClient::submitRequestList(
123 const std::vector<hardware::camera2::CaptureRequest>& requests,
124 bool streaming,
125 /*out*/
126 hardware::camera2::utils::SubmitInfo *submitInfo) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700127 ATRACE_CALL();
Mark Salyzyn50468412014-06-18 16:33:43 -0700128 ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700129
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800130 binder::Status res = binder::Status::ok();
131 status_t err;
132 if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
133 return res;
134 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700135
136 Mutex::Autolock icl(mBinderSerializationLock);
137
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800138 if (!mDevice.get()) {
139 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
140 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700141
142 if (requests.empty()) {
143 ALOGE("%s: Camera %d: Sent null request. Rejecting request.",
144 __FUNCTION__, mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800145 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
Jianing Wei90e59c92014-03-12 18:29:36 -0700146 }
147
148 List<const CameraMetadata> metadataRequestList;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800149 submitInfo->mRequestId = mRequestIdCounter;
Jianing Wei90e59c92014-03-12 18:29:36 -0700150 uint32_t loopCounter = 0;
151
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800152 for (auto&& request: requests) {
153 if (request.mIsReprocess) {
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700154 if (!mInputStream.configured) {
155 ALOGE("%s: Camera %d: no input stream is configured.", __FUNCTION__, mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800156 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
157 "No input configured for camera %d but request is for reprocessing",
158 mCameraId);
Chien-Yu Chened0412e2015-04-27 15:04:22 -0700159 } else if (streaming) {
160 ALOGE("%s: Camera %d: streaming reprocess requests not supported.", __FUNCTION__,
161 mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800162 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
163 "Repeating reprocess requests not supported");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700164 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700165 }
166
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800167 CameraMetadata metadata(request.mMetadata);
Jianing Wei90e59c92014-03-12 18:29:36 -0700168 if (metadata.isEmpty()) {
169 ALOGE("%s: Camera %d: Sent empty metadata packet. Rejecting request.",
170 __FUNCTION__, mCameraId);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800171 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
172 "Request settings are empty");
173 } else if (request.mSurfaceList.isEmpty()) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700174 ALOGE("%s: Camera %d: Requests must have at least one surface target. "
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800175 "Rejecting request.", __FUNCTION__, mCameraId);
176 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
177 "Request has no output targets");
Jianing Wei90e59c92014-03-12 18:29:36 -0700178 }
179
180 if (!enforceRequestPermissions(metadata)) {
181 // Callee logs
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800182 return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
183 "Caller does not have permission to change restricted controls");
Jianing Wei90e59c92014-03-12 18:29:36 -0700184 }
185
186 /**
187 * Write in the output stream IDs which we calculate from
188 * the capture request's list of surface targets
189 */
190 Vector<int32_t> outputStreamIds;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800191 outputStreamIds.setCapacity(request.mSurfaceList.size());
192 for (sp<Surface> surface : request.mSurfaceList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700193 if (surface == 0) continue;
194
195 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
Marco Nelissenf8880202014-11-14 07:58:25 -0800196 int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
Jianing Wei90e59c92014-03-12 18:29:36 -0700197
198 // Trying to submit request with surface that wasn't created
199 if (idx == NAME_NOT_FOUND) {
200 ALOGE("%s: Camera %d: Tried to submit a request with a surface that"
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800201 " we have not called createStream on",
202 __FUNCTION__, mCameraId);
203 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
204 "Request targets Surface that is not part of current capture session");
Jianing Wei90e59c92014-03-12 18:29:36 -0700205 }
206
207 int streamId = mStreamMap.valueAt(idx);
208 outputStreamIds.push_back(streamId);
209 ALOGV("%s: Camera %d: Appending output stream %d to request",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800210 __FUNCTION__, mCameraId, streamId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700211 }
212
213 metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS, &outputStreamIds[0],
214 outputStreamIds.size());
215
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800216 if (request.mIsReprocess) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700217 metadata.update(ANDROID_REQUEST_INPUT_STREAMS, &mInputStream.id, 1);
218 }
219
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800220 metadata.update(ANDROID_REQUEST_ID, &(submitInfo->mRequestId), /*size*/1);
Jianing Wei90e59c92014-03-12 18:29:36 -0700221 loopCounter++; // loopCounter starts from 1
Mark Salyzyn50468412014-06-18 16:33:43 -0700222 ALOGV("%s: Camera %d: Creating request with ID %d (%d of %zu)",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800223 __FUNCTION__, mCameraId, submitInfo->mRequestId, loopCounter, requests.size());
Jianing Wei90e59c92014-03-12 18:29:36 -0700224
225 metadataRequestList.push_back(metadata);
226 }
227 mRequestIdCounter++;
228
229 if (streaming) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800230 err = mDevice->setStreamingRequestList(metadataRequestList, &(submitInfo->mLastFrameNumber));
231 if (err != OK) {
232 String8 msg = String8::format(
233 "Camera %d: Got error %s (%d) after trying to set streaming request",
234 mCameraId, strerror(-err), err);
235 ALOGE("%s: %s", __FUNCTION__, msg.string());
236 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
237 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700238 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800239 mStreamingRequestList.push_back(submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700240 }
241 } else {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800242 err = mDevice->captureList(metadataRequestList, &(submitInfo->mLastFrameNumber));
243 if (err != OK) {
244 String8 msg = String8::format(
245 "Camera %d: Got error %s (%d) after trying to submit capture request",
246 mCameraId, strerror(-err), err);
247 ALOGE("%s: %s", __FUNCTION__, msg.string());
248 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
249 msg.string());
Jianing Wei90e59c92014-03-12 18:29:36 -0700250 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800251 ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700252 }
253
254 ALOGV("%s: Camera %d: End of function", __FUNCTION__, mCameraId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700255 return res;
256}
257
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800258binder::Status CameraDeviceClient::cancelRequest(
259 int requestId,
260 /*out*/
261 int64_t* lastFrameNumber) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700262 ATRACE_CALL();
263 ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
264
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800265 status_t err;
266 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700267
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800268 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700269
270 Mutex::Autolock icl(mBinderSerializationLock);
271
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800272 if (!mDevice.get()) {
273 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
274 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700275
276 Vector<int>::iterator it, end;
277 for (it = mStreamingRequestList.begin(), end = mStreamingRequestList.end();
278 it != end; ++it) {
279 if (*it == requestId) {
280 break;
281 }
282 }
283
284 if (it == end) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800285 String8 msg = String8::format("Camera %d: Did not find request ID %d in list of "
286 "streaming requests", mCameraId, requestId);
287 ALOGE("%s: %s", __FUNCTION__, msg.string());
288 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700289 }
290
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800291 err = mDevice->clearStreamingRequest(lastFrameNumber);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700292
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800293 if (err == OK) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700294 ALOGV("%s: Camera %d: Successfully cleared streaming request",
295 __FUNCTION__, mCameraId);
296 mStreamingRequestList.erase(it);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800297 } else {
298 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
299 "Camera %d: Error clearing streaming request: %s (%d)",
300 mCameraId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700301 }
302
303 return res;
304}
305
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800306binder::Status CameraDeviceClient::beginConfigure() {
Ruben Brunkb2119af2014-05-09 19:57:56 -0700307 // TODO: Implement this.
Zhijun He1fa89992015-06-01 15:44:31 -0700308 ALOGV("%s: Not implemented yet.", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800309 return binder::Status::ok();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700310}
311
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800312binder::Status CameraDeviceClient::endConfigure(bool isConstrainedHighSpeed) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700313 ALOGV("%s: ending configure (%d input stream, %zu output streams)",
314 __FUNCTION__, mInputStream.configured ? 1 : 0, mStreamMap.size());
Igor Murashkine2d167e2014-08-19 16:19:59 -0700315
Zhijun He1fa89992015-06-01 15:44:31 -0700316 // Sanitize the high speed session against necessary capability bit.
317 if (isConstrainedHighSpeed) {
318 CameraMetadata staticInfo = mDevice->info();
319 camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
320 bool isConstrainedHighSpeedSupported = false;
321 for(size_t i = 0; i < entry.count; ++i) {
322 uint8_t capability = entry.data.u8[i];
323 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
324 isConstrainedHighSpeedSupported = true;
325 break;
326 }
327 }
328 if (!isConstrainedHighSpeedSupported) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800329 String8 msg = String8::format(
330 "Camera %d: Try to create a constrained high speed configuration on a device"
331 " that doesn't support it.", mCameraId);
332 ALOGE("%s: %s", __FUNCTION__, msg.string());
333 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
334 msg.string());
Zhijun He1fa89992015-06-01 15:44:31 -0700335 }
336 }
337
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800338 binder::Status res;
339 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine2d167e2014-08-19 16:19:59 -0700340
341 Mutex::Autolock icl(mBinderSerializationLock);
342
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800343 if (!mDevice.get()) {
344 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
345 }
Igor Murashkine2d167e2014-08-19 16:19:59 -0700346
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800347 status_t err = mDevice->configureStreams(isConstrainedHighSpeed);
348 if (err != OK) {
349 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
350 "Camera %d: Error configuring streams: %s (%d)",
351 mCameraId, strerror(-err), err);
352 }
353
354 return res;
Ruben Brunkb2119af2014-05-09 19:57:56 -0700355}
356
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800357binder::Status CameraDeviceClient::deleteStream(int streamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700358 ATRACE_CALL();
359 ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
360
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800361 binder::Status res;
362 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700363
364 Mutex::Autolock icl(mBinderSerializationLock);
365
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800366 if (!mDevice.get()) {
367 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
368 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700369
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700370 bool isInput = false;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700371 ssize_t index = NAME_NOT_FOUND;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700372
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700373 if (mInputStream.configured && mInputStream.id == streamId) {
374 isInput = true;
375 } else {
376 // Guard against trying to delete non-created streams
377 for (size_t i = 0; i < mStreamMap.size(); ++i) {
378 if (streamId == mStreamMap.valueAt(i)) {
379 index = i;
380 break;
381 }
382 }
383
384 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800385 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no such "
386 "stream created yet", mCameraId, streamId);
387 ALOGW("%s: %s", __FUNCTION__, msg.string());
388 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700389 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700390 }
391
392 // Also returns BAD_VALUE if stream ID was not valid
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800393 status_t err = mDevice->deleteStream(streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700394
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800395 if (err != OK) {
396 String8 msg = String8::format("Camera %d: Unexpected error %s (%d) when deleting stream %d",
397 mCameraId, strerror(-err), err, streamId);
398 ALOGE("%s: %s", __FUNCTION__, msg.string());
399 res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
400 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700401 if (isInput) {
402 mInputStream.configured = false;
403 } else {
404 mStreamMap.removeItemsAt(index);
405 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700406 }
407
408 return res;
409}
410
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800411binder::Status CameraDeviceClient::createStream(
412 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
413 /*out*/
414 int32_t* newStreamId) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700415 ATRACE_CALL();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700416
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800417 binder::Status res;
418 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700419
420 Mutex::Autolock icl(mBinderSerializationLock);
421
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700422 sp<IGraphicBufferProducer> bufferProducer = outputConfiguration.getGraphicBufferProducer();
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700423 if (bufferProducer == NULL) {
424 ALOGE("%s: bufferProducer must not be null", __FUNCTION__);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800425 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
Yin-Chia Yeh89f14da2014-06-10 16:05:44 -0700426 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800427 if (!mDevice.get()) {
428 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
429 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700430
431 // Don't create multiple streams for the same target surface
432 {
Marco Nelissenf8880202014-11-14 07:58:25 -0800433 ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
Igor Murashkine7ee7632013-06-11 18:10:18 -0700434 if (index != NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800435 String8 msg = String8::format("Camera %d: Surface already has a stream created for it "
436 "(ID %zd)", mCameraId, index);
437 ALOGW("%s: %s", __FUNCTION__, msg.string());
438 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700439 }
440 }
441
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800442 status_t err;
443
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700444 // HACK b/10949105
445 // Query consumer usage bits to set async operation mode for
446 // GLConsumer using controlledByApp parameter.
447 bool useAsync = false;
448 int32_t consumerUsage;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800449 if ((err = bufferProducer->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS,
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700450 &consumerUsage)) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800451 String8 msg = String8::format("Camera %d: Failed to query Surface consumer usage: %s (%d)",
452 mCameraId, strerror(-err), err);
453 ALOGE("%s: %s", __FUNCTION__, msg.string());
454 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvala1da3b602013-09-26 15:28:55 -0700455 }
456 if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
457 ALOGW("%s: Camera %d: Forcing asynchronous mode for stream",
458 __FUNCTION__, mCameraId);
459 useAsync = true;
460 }
461
Ruben Brunkbba75572014-11-20 17:29:50 -0800462 int32_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
463 GRALLOC_USAGE_RENDERSCRIPT;
464 int32_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
465 GraphicBuffer::USAGE_HW_TEXTURE |
466 GraphicBuffer::USAGE_HW_COMPOSER;
467 bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
468 (consumerUsage & allowedFlags) != 0;
469
Marco Nelissenf8880202014-11-14 07:58:25 -0800470 sp<IBinder> binder = IInterface::asBinder(bufferProducer);
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700471 sp<Surface> surface = new Surface(bufferProducer, useAsync);
472 ANativeWindow *anw = surface.get();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700473
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800474 int width, height, format;
475 android_dataspace dataSpace;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700476
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800477 if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
478 String8 msg = String8::format("Camera %d: Failed to query Surface width: %s (%d)",
479 mCameraId, strerror(-err), err);
480 ALOGE("%s: %s", __FUNCTION__, msg.string());
481 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700482 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800483 if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
484 String8 msg = String8::format("Camera %d: Failed to query Surface height: %s (%d)",
485 mCameraId, strerror(-err), err);
486 ALOGE("%s: %s", __FUNCTION__, msg.string());
487 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700488 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800489 if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
490 String8 msg = String8::format("Camera %d: Failed to query Surface format: %s (%d)",
491 mCameraId, strerror(-err), err);
492 ALOGE("%s: %s", __FUNCTION__, msg.string());
493 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700494 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800495 if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800496 reinterpret_cast<int*>(&dataSpace))) != OK) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800497 String8 msg = String8::format("Camera %d: Failed to query Surface dataspace: %s (%d)",
498 mCameraId, strerror(-err), err);
499 ALOGE("%s: %s", __FUNCTION__, msg.string());
500 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800501 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700502
503 // FIXME: remove this override since the default format should be
504 // IMPLEMENTATION_DEFINED. b/9487482
Igor Murashkin15811012013-07-29 12:25:59 -0700505 if (format >= HAL_PIXEL_FORMAT_RGBA_8888 &&
506 format <= HAL_PIXEL_FORMAT_BGRA_8888) {
Ruben Brunkbba75572014-11-20 17:29:50 -0800507 ALOGW("%s: Camera %d: Overriding format %#x to IMPLEMENTATION_DEFINED",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700508 __FUNCTION__, mCameraId, format);
509 format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
510 }
511
Ruben Brunkbba75572014-11-20 17:29:50 -0800512 // Round dimensions to the nearest dimensions available for this format
513 if (flexibleConsumer && !CameraDeviceClient::roundBufferDimensionNearest(width, height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800514 format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800515 String8 msg = String8::format("Camera %d: No supported stream configurations with "
516 "format %#x defined, failed to create output stream", mCameraId, format);
517 ALOGE("%s: %s", __FUNCTION__, msg.string());
518 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkbba75572014-11-20 17:29:50 -0800519 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700520
Zhijun He125684a2015-12-26 15:07:30 -0800521 int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800522 err = mDevice->createStream(surface, width, height, format, dataSpace,
523 static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
524 &streamId, outputConfiguration.getSurfaceSetID());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700525
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800526 if (err != OK) {
527 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
528 "Camera %d: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
529 mCameraId, width, height, format, dataSpace, strerror(-err), err);
530 } else {
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800531 mStreamMap.add(binder, streamId);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700532
533 ALOGV("%s: Camera %d: Successfully created a new stream ID %d",
534 __FUNCTION__, mCameraId, streamId);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700535
536 /**
537 * Set the stream transform flags to automatically
538 * rotate the camera stream for preview use cases.
539 */
540 int32_t transform = 0;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800541 err = getRotationTransformLocked(&transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700542
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800543 if (err != OK) {
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700544 // Error logged by getRotationTransformLocked.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800545 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
546 "Unable to calculate rotation transform for new stream");
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700547 }
548
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800549 err = mDevice->setStreamTransform(streamId, transform);
550 if (err != OK) {
551 String8 msg = String8::format("Failed to set stream transform (stream id %d)",
552 streamId);
553 ALOGE("%s: %s", __FUNCTION__, msg.string());
554 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700555 }
556
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800557 *newStreamId = streamId;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700558 }
559
560 return res;
561}
562
Ruben Brunkbba75572014-11-20 17:29:50 -0800563
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800564binder::Status CameraDeviceClient::createInputStream(
565 int width, int height, int format,
566 /*out*/
567 int32_t* newStreamId) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700568
569 ATRACE_CALL();
570 ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
571
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800572 binder::Status res;
573 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700574
575 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800576
577 if (!mDevice.get()) {
578 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
579 }
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700580
581 if (mInputStream.configured) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800582 String8 msg = String8::format("Camera %d: Already has an input stream "
583 "configured (ID %zd)", mCameraId, mInputStream.id);
584 ALOGE("%s: %s", __FUNCTION__, msg.string() );
585 return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700586 }
587
588 int streamId = -1;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800589 status_t err = mDevice->createInputStream(width, height, format, &streamId);
590 if (err == OK) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700591 mInputStream.configured = true;
592 mInputStream.width = width;
593 mInputStream.height = height;
594 mInputStream.format = format;
595 mInputStream.id = streamId;
596
597 ALOGV("%s: Camera %d: Successfully created a new input stream ID %d",
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800598 __FUNCTION__, mCameraId, streamId);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700599
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600 *newStreamId = streamId;
601 } else {
602 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
603 "Camera %d: Error creating new input stream: %s (%d)", mCameraId,
604 strerror(-err), err);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700605 }
606
607 return res;
608}
609
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800610binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700611
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800612 binder::Status res;
613 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
614
615 if (inputSurface == NULL) {
616 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700617 }
618
619 Mutex::Autolock icl(mBinderSerializationLock);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800620 if (!mDevice.get()) {
621 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
622 }
623 sp<IGraphicBufferProducer> producer;
624 status_t err = mDevice->getInputBufferProducer(&producer);
625 if (err != OK) {
626 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
627 "Camera %d: Error getting input Surface: %s (%d)",
628 mCameraId, strerror(-err), err);
629 } else {
630 inputSurface->name = String16("CameraInput");
631 inputSurface->graphicBufferProducer = producer;
632 }
633 return res;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700634}
635
Ruben Brunkbba75572014-11-20 17:29:50 -0800636bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800637 int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
Ruben Brunkbba75572014-11-20 17:29:50 -0800638 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
639
640 camera_metadata_ro_entry streamConfigs =
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800641 (dataSpace == HAL_DATASPACE_DEPTH) ?
642 info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
Ruben Brunkbba75572014-11-20 17:29:50 -0800643 info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
644
645 int32_t bestWidth = -1;
646 int32_t bestHeight = -1;
647
648 // Iterate through listed stream configurations and find the one with the smallest euclidean
649 // distance from the given dimensions for the given format.
650 for (size_t i = 0; i < streamConfigs.count; i += 4) {
651 int32_t fmt = streamConfigs.data.i32[i];
652 int32_t w = streamConfigs.data.i32[i + 1];
653 int32_t h = streamConfigs.data.i32[i + 2];
654
655 // Ignore input/output type for now
656 if (fmt == format) {
657 if (w == width && h == height) {
658 bestWidth = width;
659 bestHeight = height;
660 break;
661 } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
662 CameraDeviceClient::euclidDistSquare(w, h, width, height) <
663 CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
664 bestWidth = w;
665 bestHeight = h;
666 }
667 }
668 }
669
670 if (bestWidth == -1) {
671 // Return false if no configurations for this format were listed
672 return false;
673 }
674
675 // Set the outputs to the closet width/height
676 if (outWidth != NULL) {
677 *outWidth = bestWidth;
678 }
679 if (outHeight != NULL) {
680 *outHeight = bestHeight;
681 }
682
683 // Return true if at least one configuration for this format was listed
684 return true;
685}
686
687int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
688 int64_t d0 = x0 - x1;
689 int64_t d1 = y0 - y1;
690 return d0 * d0 + d1 * d1;
691}
692
Igor Murashkine7ee7632013-06-11 18:10:18 -0700693// Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800694binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
695 /*out*/
696 hardware::camera2::impl::CameraMetadataNative* request)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700697{
698 ATRACE_CALL();
699 ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
700
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800701 binder::Status res;
702 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700703
704 Mutex::Autolock icl(mBinderSerializationLock);
705
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800706 if (!mDevice.get()) {
707 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
708 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700709
710 CameraMetadata metadata;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800711 status_t err;
712 if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
Igor Murashkine7ee7632013-06-11 18:10:18 -0700713 request != NULL) {
714
715 request->swap(metadata);
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800716 } else {
717 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
718 "Camera %d: Error creating default request for template %d: %s (%d)",
719 mCameraId, templateId, strerror(-err), err);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700720 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700721 return res;
722}
723
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800724binder::Status CameraDeviceClient::getCameraInfo(
725 /*out*/
726 hardware::camera2::impl::CameraMetadataNative* info)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700727{
728 ATRACE_CALL();
729 ALOGV("%s", __FUNCTION__);
730
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800731 binder::Status res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700732
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800733 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700734
735 Mutex::Autolock icl(mBinderSerializationLock);
736
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800737 if (!mDevice.get()) {
738 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
739 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700740
Igor Murashkin099b4572013-07-12 17:52:16 -0700741 if (info != NULL) {
742 *info = mDevice->info(); // static camera metadata
743 // TODO: merge with device-specific camera metadata
744 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700745
746 return res;
747}
748
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800749binder::Status CameraDeviceClient::waitUntilIdle()
Zhijun He2ab500c2013-07-23 08:02:53 -0700750{
751 ATRACE_CALL();
752 ALOGV("%s", __FUNCTION__);
753
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800754 binder::Status res;
755 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Zhijun He2ab500c2013-07-23 08:02:53 -0700756
757 Mutex::Autolock icl(mBinderSerializationLock);
758
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800759 if (!mDevice.get()) {
760 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
761 }
Zhijun He2ab500c2013-07-23 08:02:53 -0700762
763 // FIXME: Also need check repeating burst.
764 if (!mStreamingRequestList.isEmpty()) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800765 String8 msg = String8::format(
766 "Camera %d: Try to waitUntilIdle when there are active streaming requests",
767 mCameraId);
768 ALOGE("%s: %s", __FUNCTION__, msg.string());
769 return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
Zhijun He2ab500c2013-07-23 08:02:53 -0700770 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800771 status_t err = mDevice->waitUntilDrained();
772 if (err != OK) {
773 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
774 "Camera %d: Error waiting to drain: %s (%d)",
775 mCameraId, strerror(-err), err);
776 }
Zhijun He2ab500c2013-07-23 08:02:53 -0700777 ALOGV("%s Done", __FUNCTION__);
Zhijun He2ab500c2013-07-23 08:02:53 -0700778 return res;
779}
780
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800781binder::Status CameraDeviceClient::flush(
782 /*out*/
783 int64_t* lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700784 ATRACE_CALL();
785 ALOGV("%s", __FUNCTION__);
786
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800787 binder::Status res;
788 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700789
790 Mutex::Autolock icl(mBinderSerializationLock);
791
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800792 if (!mDevice.get()) {
793 return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
794 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700795
Jianing Wei3c76fa32014-04-21 11:34:34 -0700796 mStreamingRequestList.clear();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800797 status_t err = mDevice->flush(lastFrameNumber);
798 if (err != OK) {
799 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
800 "Camera %d: Error flushing device: %s (%d)", mCameraId, strerror(-err), err);
801 }
802 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700803}
804
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800805binder::Status CameraDeviceClient::prepare(int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700806 ATRACE_CALL();
807 ALOGV("%s", __FUNCTION__);
808
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800809 binder::Status res;
810 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700811
812 Mutex::Autolock icl(mBinderSerializationLock);
813
814 // Guard against trying to prepare non-created streams
815 ssize_t index = NAME_NOT_FOUND;
816 for (size_t i = 0; i < mStreamMap.size(); ++i) {
817 if (streamId == mStreamMap.valueAt(i)) {
818 index = i;
819 break;
820 }
821 }
822
823 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800824 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
825 "with that ID exists", mCameraId, streamId);
826 ALOGW("%s: %s", __FUNCTION__, msg.string());
827 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700828 }
829
Eino-Ville Talvala261394e2015-05-13 14:28:38 -0700830 // Also returns BAD_VALUE if stream ID was not valid, or stream already
831 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800832 status_t err = mDevice->prepare(streamId);
833 if (err == BAD_VALUE) {
834 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
835 "Camera %d: Stream %d has already been used, and cannot be prepared",
836 mCameraId, streamId);
837 } else if (err != OK) {
838 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
839 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId,
840 strerror(-err), err);
841 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700842 return res;
843}
844
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800845binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -0700846 ATRACE_CALL();
847 ALOGV("%s", __FUNCTION__);
848
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800849 binder::Status res;
850 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700851
852 Mutex::Autolock icl(mBinderSerializationLock);
853
854 // Guard against trying to prepare non-created streams
855 ssize_t index = NAME_NOT_FOUND;
856 for (size_t i = 0; i < mStreamMap.size(); ++i) {
857 if (streamId == mStreamMap.valueAt(i)) {
858 index = i;
859 break;
860 }
861 }
862
863 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800864 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
865 "with that ID exists", mCameraId, streamId);
866 ALOGW("%s: %s", __FUNCTION__, msg.string());
867 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -0700868 }
869
870 if (maxCount <= 0) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800871 String8 msg = String8::format("Camera %d: maxCount (%d) must be greater than 0",
872 mCameraId, maxCount);
873 ALOGE("%s: %s", __FUNCTION__, msg.string());
874 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Ruben Brunkc78ac262015-08-13 17:58:46 -0700875 }
876
877 // Also returns BAD_VALUE if stream ID was not valid, or stream already
878 // has been used
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800879 status_t err = mDevice->prepare(maxCount, streamId);
880 if (err == BAD_VALUE) {
881 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
882 "Camera %d: Stream %d has already been used, and cannot be prepared",
883 mCameraId, streamId);
884 } else if (err != OK) {
885 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
886 "Camera %d: Error preparing stream %d: %s (%d)", mCameraId, streamId,
887 strerror(-err), err);
888 }
Ruben Brunkc78ac262015-08-13 17:58:46 -0700889
890 return res;
891}
892
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800893binder::Status CameraDeviceClient::tearDown(int streamId) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700894 ATRACE_CALL();
895 ALOGV("%s", __FUNCTION__);
896
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800897 binder::Status res;
898 if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700899
900 Mutex::Autolock icl(mBinderSerializationLock);
901
902 // Guard against trying to prepare non-created streams
903 ssize_t index = NAME_NOT_FOUND;
904 for (size_t i = 0; i < mStreamMap.size(); ++i) {
905 if (streamId == mStreamMap.valueAt(i)) {
906 index = i;
907 break;
908 }
909 }
910
911 if (index == NAME_NOT_FOUND) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800912 String8 msg = String8::format("Camera %d: Invalid stream ID (%d) specified, no stream "
913 "with that ID exists", mCameraId, streamId);
914 ALOGW("%s: %s", __FUNCTION__, msg.string());
915 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700916 }
917
918 // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
919 // use
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800920 status_t err = mDevice->tearDown(streamId);
921 if (err == BAD_VALUE) {
922 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
923 "Camera %d: Stream %d is still in use, cannot be torn down",
924 mCameraId, streamId);
925 } else if (err != OK) {
926 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
927 "Camera %d: Error tearing down stream %d: %s (%d)", mCameraId, streamId,
928 strerror(-err), err);
929 }
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700930
931 return res;
932}
933
Igor Murashkine7ee7632013-06-11 18:10:18 -0700934status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800935 return BasicClient::dump(fd, args);
936}
937
938status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700939 String8 result;
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700940 result.appendFormat("CameraDeviceClient[%d] (%p) dump:\n",
Igor Murashkine7ee7632013-06-11 18:10:18 -0700941 mCameraId,
Eino-Ville Talvalae992e752014-11-07 16:17:48 -0800942 (getRemoteCallback() != NULL ?
Marco Nelissenf8880202014-11-14 07:58:25 -0800943 IInterface::asBinder(getRemoteCallback()).get() : NULL) );
Ruben Brunkcc776712015-02-17 20:18:47 -0800944 result.appendFormat(" Current client UID %u\n", mClientUid);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700945
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700946 result.append(" State:\n");
947 result.appendFormat(" Request ID counter: %d\n", mRequestIdCounter);
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700948 if (mInputStream.configured) {
949 result.appendFormat(" Current input stream ID: %d\n",
950 mInputStream.id);
951 } else {
952 result.append(" No input stream configured.\n");
953 }
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700954 if (!mStreamMap.isEmpty()) {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700955 result.append(" Current output stream IDs:\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700956 for (size_t i = 0; i < mStreamMap.size(); i++) {
957 result.appendFormat(" Stream %d\n", mStreamMap.valueAt(i));
958 }
959 } else {
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700960 result.append(" No output streams configured.\n");
Eino-Ville Talvala67489d22014-09-18 15:52:02 -0700961 }
962 write(fd, result.string(), result.size());
Igor Murashkine7ee7632013-06-11 18:10:18 -0700963 // TODO: print dynamic/request section from most recent requests
964 mFrameProcessor->dump(fd, args);
965
966 return dumpDevice(fd, args);
967}
968
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800969void CameraDeviceClient::notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700970 const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700971 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800972 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700973
974 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700975 remoteCb->onDeviceError(errorCode, resultExtras);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700976 }
977}
978
979void CameraDeviceClient::notifyIdle() {
980 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800981 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700982
983 if (remoteCb != 0) {
984 remoteCb->onDeviceIdle();
985 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700986 Camera2ClientBase::notifyIdle();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700987}
988
Jianing Weicb0652e2014-03-12 18:29:36 -0700989void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700990 nsecs_t timestamp) {
991 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800992 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700993 if (remoteCb != 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700994 remoteCb->onCaptureStarted(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700995 }
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700996 Camera2ClientBase::notifyShutter(resultExtras, timestamp);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700997}
998
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700999void CameraDeviceClient::notifyPrepared(int streamId) {
1000 // Thread safe. Don't bother locking.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001001 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001002 if (remoteCb != 0) {
1003 remoteCb->onPrepared(streamId);
1004 }
1005}
1006
Igor Murashkine7ee7632013-06-11 18:10:18 -07001007void CameraDeviceClient::detachDevice() {
1008 if (mDevice == 0) return;
1009
1010 ALOGV("Camera %d: Stopping processors", mCameraId);
1011
1012 mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
1013 FRAME_PROCESSOR_LISTENER_MAX_ID,
1014 /*listener*/this);
1015 mFrameProcessor->requestExit();
1016 ALOGV("Camera %d: Waiting for threads", mCameraId);
1017 mFrameProcessor->join();
1018 ALOGV("Camera %d: Disconnecting device", mCameraId);
1019
1020 // WORKAROUND: HAL refuses to disconnect while there's streams in flight
1021 {
1022 mDevice->clearStreamingRequest();
1023
1024 status_t code;
1025 if ((code = mDevice->waitUntilDrained()) != OK) {
1026 ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
1027 code);
1028 }
1029 }
1030
1031 Camera2ClientBase::detachDevice();
1032}
1033
1034/** Device-related methods */
Jianing Weicb0652e2014-03-12 18:29:36 -07001035void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
Igor Murashkine7ee7632013-06-11 18:10:18 -07001036 ATRACE_CALL();
1037 ALOGV("%s", __FUNCTION__);
1038
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001039 // Thread-safe. No lock necessary.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001040 sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
Igor Murashkin4fb55c12013-08-29 17:43:01 -07001041 if (remoteCb != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001042 remoteCb->onResultReceived(result.mMetadata, result.mResultExtras);
Igor Murashkine7ee7632013-06-11 18:10:18 -07001043 }
Igor Murashkine7ee7632013-06-11 18:10:18 -07001044}
1045
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08001046binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
1047 status_t res = checkPid(checkLocation);
1048 return (res == OK) ? binder::Status::ok() :
1049 STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
1050 "Attempt to use camera from a different process than original client");
1051}
1052
Igor Murashkine7ee7632013-06-11 18:10:18 -07001053// TODO: move to Camera2ClientBase
1054bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
1055
1056 const int pid = IPCThreadState::self()->getCallingPid();
1057 const int selfPid = getpid();
1058 camera_metadata_entry_t entry;
1059
1060 /**
1061 * Mixin default important security values
1062 * - android.led.transmit = defaulted ON
1063 */
1064 CameraMetadata staticInfo = mDevice->info();
1065 entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
1066 for(size_t i = 0; i < entry.count; ++i) {
1067 uint8_t led = entry.data.u8[i];
1068
1069 switch(led) {
1070 case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
1071 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
1072 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
1073 metadata.update(ANDROID_LED_TRANSMIT,
1074 &transmitDefault, 1);
1075 }
1076 break;
1077 }
1078 }
1079 }
1080
1081 // We can do anything!
1082 if (pid == selfPid) {
1083 return true;
1084 }
1085
1086 /**
1087 * Permission check special fields in the request
1088 * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
1089 */
1090 entry = metadata.find(ANDROID_LED_TRANSMIT);
1091 if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
1092 String16 permissionString =
1093 String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
1094 if (!checkCallingPermission(permissionString)) {
1095 const int uid = IPCThreadState::self()->getCallingUid();
1096 ALOGE("Permission Denial: "
1097 "can't disable transmit LED pid=%d, uid=%d", pid, uid);
1098 return false;
1099 }
1100 }
1101
1102 return true;
1103}
1104
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001105status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
1106 ALOGV("%s: begin", __FUNCTION__);
1107
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001108 const CameraMetadata& staticInfo = mDevice->info();
Ruben Brunk5698d442014-06-18 10:39:40 -07001109 return CameraUtils::getRotationTransform(staticInfo, transform);
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -07001110}
1111
Igor Murashkine7ee7632013-06-11 18:10:18 -07001112} // namespace android