ACameraDevice.h revision 4dfa4cca7a4fcf5ea4a37dca1fef7f2f56f6dd8c
10dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh/* 20dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * Copyright (C) 2015 The Android Open Source Project 30dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * 40dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * Licensed under the Apache License, Version 2.0 (the "License"); 50dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * you may not use this file except in compliance with the License. 60dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * You may obtain a copy of the License at 70dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * 80dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * http://www.apache.org/licenses/LICENSE-2.0 90dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * 100dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * Unless required by applicable law or agreed to in writing, software 110dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * distributed under the License is distributed on an "AS IS" BASIS, 120dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 130dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * See the License for the specific language governing permissions and 140dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * limitations under the License. 150dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh */ 160dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#ifndef _ACAMERA_DEVICE_H 170dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#define _ACAMERA_DEVICE_H 180dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 190dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <memory> 20ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh#include <map> 21ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh#include <set> 220dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <atomic> 23e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh#include <utility> 240dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <utils/StrongPointer.h> 250dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <utils/Mutex.h> 260dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <utils/String8.h> 27ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh#include <utils/List.h> 28ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh#include <utils/Vector.h> 290dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 30d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala#include <android/hardware/camera2/BnCameraDeviceCallbacks.h> 31d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala#include <android/hardware/camera2/ICameraDeviceUser.h> 320dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <media/stagefright/foundation/ALooper.h> 330dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <media/stagefright/foundation/AHandler.h> 340dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include <media/stagefright/foundation/AMessage.h> 35ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh#include <camera/CaptureResult.h> 36ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh#include <camera/camera2/OutputConfiguration.h> 37ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh#include <camera/camera2/CaptureRequest.h> 380dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 3940ead60d7163c2d5577099d49f11da7afa6124d1Emilian Peev#include <camera/NdkCameraManager.h> 4040ead60d7163c2d5577099d49f11da7afa6124d1Emilian Peev#include <camera/NdkCameraCaptureSession.h> 410dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#include "ACameraMetadata.h" 420dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 430dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehnamespace android { 440dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 45d21c46b3cc97497be689c344046b51cba00aa4b5Yin-Chia Yeh// Wrap ACameraCaptureFailure so it can be ref-counted 46ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yehstruct CameraCaptureFailure : public RefBase, public ACameraCaptureFailure {}; 47ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 48ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yehclass CameraDevice final : public RefBase { 490dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh public: 500dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh CameraDevice(const char* id, ACameraDevice_StateCallbacks* cb, 510dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh std::unique_ptr<ACameraMetadata> chars, 520dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh ACameraDevice* wrapper); 530dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh ~CameraDevice(); 540dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 550dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh inline const char* getId() const { return mCameraId.string(); } 560dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 570dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh camera_status_t createCaptureRequest( 580dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh ACameraDevice_request_template templateId, 590dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh ACaptureRequest** request) const; 600dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 61ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh camera_status_t createCaptureSession( 62ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh const ACaptureSessionOutputContainer* outputs, 63ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh const ACameraCaptureSession_stateCallbacks* callbacks, 64ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*out*/ACameraCaptureSession** session); 65ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 660dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh // Callbacks from camera service 67d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala class ServiceCallback : public hardware::camera2::BnCameraDeviceCallbacks { 680dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh public: 69d19d994770a04f02c204affd29985c5aeaa58baaChih-Hung Hsieh explicit ServiceCallback(CameraDevice* device) : mDevice(device) {} 70d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala binder::Status onDeviceError(int32_t errorCode, 710dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh const CaptureResultExtras& resultExtras) override; 72d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala binder::Status onDeviceIdle() override; 73d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras, 740dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh int64_t timestamp) override; 75d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala binder::Status onResultReceived(const CameraMetadata& metadata, 760dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh const CaptureResultExtras& resultExtras) override; 77d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala binder::Status onPrepared(int streamId) override; 789d06601e54848f076b7472a376c672215cd70c46Shuzhen Wang binder::Status onRequestQueueEmpty() override; 798ca23dca1cd450b4d93e8da1c8bd6f6ee61e2f5fYin-Chia Yeh binder::Status onRepeatingRequestError(int64_t lastFrameNumber, 808ca23dca1cd450b4d93e8da1c8bd6f6ee61e2f5fYin-Chia Yeh int32_t stoppedSequenceId) override; 810dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh private: 820dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh const wp<CameraDevice> mDevice; 830dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh }; 84d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala inline sp<hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() { 85d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala return mServiceCallback; 86d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala }; 870dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 880dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh // Camera device is only functional after remote being set 89d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala void setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote); 900dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 91ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh inline ACameraDevice* getWrapper() const { return mWrapper; }; 92ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 930dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh private: 94ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh friend ACameraCaptureSession; 950dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh camera_status_t checkCameraClosedOrErrorLocked() const; 960dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 97ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // device goes into fatal error state after this 98ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh void setCameraDeviceErrorLocked(camera_status_t error); 99ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 1006e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh void disconnectLocked(sp<ACameraCaptureSession>& session); // disconnect from camera service 101ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 102ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh camera_status_t stopRepeatingLocked(); 103ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 104309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh camera_status_t flushLocked(ACameraCaptureSession*); 105309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh 106ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh camera_status_t waitUntilIdleLocked(); 107ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 108ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 109ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh camera_status_t captureLocked(sp<ACameraCaptureSession> session, 110ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*optional*/ACameraCaptureSession_captureCallbacks* cbs, 111ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh int numRequests, ACaptureRequest** requests, 112ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*optional*/int* captureSequenceId); 113ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 114ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh camera_status_t setRepeatingRequestsLocked(sp<ACameraCaptureSession> session, 115ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*optional*/ACameraCaptureSession_captureCallbacks* cbs, 116ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh int numRequests, ACaptureRequest** requests, 117ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*optional*/int* captureSequenceId); 118ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 119ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh camera_status_t submitRequestsLocked( 120ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh sp<ACameraCaptureSession> session, 121ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*optional*/ACameraCaptureSession_captureCallbacks* cbs, 122ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh int numRequests, ACaptureRequest** requests, 123ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*out*/int* captureSequenceId, 124ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh bool isRepeating); 125ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 1264dfa4cca7a4fcf5ea4a37dca1fef7f2f56f6dd8cYin-Chia Yeh camera_status_t updateOutputConfigurationLocked(ACaptureSessionOutput *output); 12740ead60d7163c2d5577099d49f11da7afa6124d1Emilian Peev 1284dfa4cca7a4fcf5ea4a37dca1fef7f2f56f6dd8cYin-Chia Yeh camera_status_t allocateCaptureRequest( 129ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh const ACaptureRequest* request, sp<CaptureRequest>& outReq); 130ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 131ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static ACaptureRequest* allocateACaptureRequest(sp<CaptureRequest>& req); 132ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static void freeACaptureRequest(ACaptureRequest*); 133ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 134ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // only For session to hold device lock 135ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // Always grab device lock before grabbing session lock 136ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh void lockDeviceForSessionOps() const { mDeviceLock.lock(); }; 137ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh void unlockDevice() const { mDeviceLock.unlock(); }; 138ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 139ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // For capture session to notify its end of life 140ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh void notifySessionEndOfLifeLocked(ACameraCaptureSession* session); 141ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 142ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh camera_status_t configureStreamsLocked(const ACaptureSessionOutputContainer* outputs); 143ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 1446e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh // Input message will be posted and cleared after this returns 1456e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh void postSessionMsgAndCleanup(sp<AMessage>& msg); 1466e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh 147e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh static camera_status_t getIGBPfromAnw( 148e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh ANativeWindow* anw, sp<IGraphicBufferProducer>& out); 149ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 150ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static camera_status_t getSurfaceFromANativeWindow( 151ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh ANativeWindow* anw, sp<Surface>& out); 1520dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 1530dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh mutable Mutex mDeviceLock; 1540dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh const String8 mCameraId; // Camera ID 1550dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh const ACameraDevice_StateCallbacks mAppCallbacks; // Callback to app 1560dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh const std::unique_ptr<ACameraMetadata> mChars; // Camera characteristics 1570dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh const sp<ServiceCallback> mServiceCallback; 1580dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh ACameraDevice* mWrapper; 1590dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 160e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh // stream id -> pair of (ANW* from application, OutputConfiguration used for camera service) 161e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh std::map<int, std::pair<ANativeWindow*, OutputConfiguration>> mConfiguredOutputs; 162ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 1630dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh // TODO: maybe a bool will suffice for synchronous implementation? 1640dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh std::atomic_bool mClosing; 1650dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh inline bool isClosed() { return mClosing; } 1660dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 167309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh bool mInError = false; 168309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh camera_status_t mError = ACAMERA_OK; 1690dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh void onCaptureErrorLocked( 170d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala int32_t errorCode, 1710dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh const CaptureResultExtras& resultExtras); 1720dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 173309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh bool mIdle = true; 174ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // This will avoid a busy session being deleted before it's back to idle state 175ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh sp<ACameraCaptureSession> mBusySession; 1760dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 177d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala sp<hardware::camera2::ICameraDeviceUser> mRemote; 1780dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 1790dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh // Looper thread to handle callback to app 1800dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh sp<ALooper> mCbLooper; 1810dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh // definition of handler and message 1820dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh enum { 183ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // Device state callbacks 184e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh kWhatOnDisconnected, // onDisconnected 185e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh kWhatOnError, // onError 186ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // Session state callbacks 187e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh kWhatSessionStateCb, // onReady, onActive 188ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // Capture callbacks 189e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh kWhatCaptureStart, // onCaptureStarted 190e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh kWhatCaptureResult, // onCaptureProgressed, onCaptureCompleted 191e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh kWhatCaptureFail, // onCaptureFailed 192e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh kWhatCaptureSeqEnd, // onCaptureSequenceCompleted 193e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh kWhatCaptureSeqAbort, // onCaptureSequenceAborted 1946e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh kWhatCaptureBufferLost,// onCaptureBufferLost 1956e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh // Internal cleanup 1966e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh kWhatCleanUpSessions // Cleanup cached sp<ACameraCaptureSession> 1970dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh }; 1980dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh static const char* kContextKey; 1990dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh static const char* kDeviceKey; 2000dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh static const char* kErrorCodeKey; 201ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const char* kCallbackFpKey; 202ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const char* kSessionSpKey; 203ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const char* kCaptureRequestKey; 204ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const char* kTimeStampKey; 205ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const char* kCaptureResultKey; 206ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const char* kCaptureFailureKey; 207ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const char* kSequenceIdKey; 208ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const char* kFrameNumberKey; 209e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh static const char* kAnwKey; 2106e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh 2110dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh class CallbackHandler : public AHandler { 2120dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh public: 2130dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh void onMessageReceived(const sp<AMessage> &msg) override; 2146e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh 2156e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh private: 2166e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh // This handler will cache all capture session sp until kWhatCleanUpSessions 2176e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh // is processed. This is used to guarantee the last session reference is always 2186e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh // being removed in callback thread without holding camera device lock 2196e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh Vector<sp<ACameraCaptureSession>> mCachedSessions; 2200dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh }; 2210dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh sp<CallbackHandler> mHandler; 2220dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 223ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*********************************** 224ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh * Capture session related members * 225ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh ***********************************/ 226ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // The current active session 2276e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh wp<ACameraCaptureSession> mCurrentSession; 228309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh bool mFlushing = false; 2290dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 230ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh int mNextSessionId = 0; 2310dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh // TODO: might need another looper/handler to handle callbacks from service 2320dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 233ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const int REQUEST_ID_NONE = -1; 234ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh int mRepeatingSequenceId = REQUEST_ID_NONE; 235ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 236ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // sequence id -> last frame number map 237ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh std::map<int, int64_t> mSequenceLastFrameNumberMap; 238ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 239ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh struct CallbackHolder { 240ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh CallbackHolder(sp<ACameraCaptureSession> session, 241ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh const Vector<sp<CaptureRequest> >& requests, 242ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh bool isRepeating, 243ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh ACameraCaptureSession_captureCallbacks* cbs); 244ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 245ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static ACameraCaptureSession_captureCallbacks fillCb( 246ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh ACameraCaptureSession_captureCallbacks* cbs) { 247ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh if (cbs != nullptr) { 248ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh return *cbs; 249ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh } 250e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh return { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr }; 251ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh } 252ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 253ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh sp<ACameraCaptureSession> mSession; 254ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh Vector<sp<CaptureRequest> > mRequests; 255ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh const bool mIsRepeating; 256ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh ACameraCaptureSession_captureCallbacks mCallbacks; 257ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh }; 258ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // sequence id -> callbacks map 259ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh std::map<int, CallbackHolder> mSequenceCallbackMap; 260ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 261ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh static const int64_t NO_FRAMES_CAPTURED = -1; 262ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh class FrameNumberTracker { 263ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh public: 264ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // TODO: Called in onResultReceived and onCaptureErrorLocked 265ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh void updateTracker(int64_t frameNumber, bool isError); 266ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh inline int64_t getCompletedFrameNumber() { return mCompletedFrameNumber; } 267ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh private: 268ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh void update(); 269ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh void updateCompletedFrameNumber(int64_t frameNumber); 270ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 271ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh int64_t mCompletedFrameNumber = NO_FRAMES_CAPTURED; 272ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh List<int64_t> mSkippedFrameNumbers; 273ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh std::set<int64_t> mFutureErrorSet; 274ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh }; 275ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh FrameNumberTracker mFrameNumberTracker; 276ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 277ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh void checkRepeatingSequenceCompleteLocked(const int sequenceId, const int64_t lastFrameNumber); 278ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh void checkAndFireSequenceCompleteLocked(); 279ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 280ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh // Misc variables 281ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh int32_t mShadingMapSize[2]; // const after constructor 282ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh int32_t mPartialResultCount; // const after constructor 2830dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 2840dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh}; 2850dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 2860dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh} // namespace android; 2870dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 2880dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh/** 2890dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * ACameraDevice opaque struct definition 2900dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * Leave outside of android namespace because it's NDK struct 2910dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh */ 2920dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehstruct ACameraDevice { 2930dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh ACameraDevice(const char* id, ACameraDevice_StateCallbacks* cb, 2940dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh std::unique_ptr<ACameraMetadata> chars) : 2950dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh mDevice(new CameraDevice(id, cb, std::move(chars), this)) {} 2960dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 2970dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh ~ACameraDevice() {}; 2980dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 299ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /******************* 300ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh * NDK public APIs * 301ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh *******************/ 3020dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh inline const char* getId() const { return mDevice->getId(); } 3030dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 3040dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh camera_status_t createCaptureRequest( 3050dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh ACameraDevice_request_template templateId, 3060dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh ACaptureRequest** request) const { 3070dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh return mDevice->createCaptureRequest(templateId, request); 3080dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh } 3090dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 310ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh camera_status_t createCaptureSession( 311ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh const ACaptureSessionOutputContainer* outputs, 312ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh const ACameraCaptureSession_stateCallbacks* callbacks, 313ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*out*/ACameraCaptureSession** session) { 314ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh return mDevice->createCaptureSession(outputs, callbacks, session); 315ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh } 316ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh 317ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh /*********************** 318ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh * Device interal APIs * 319ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh ***********************/ 320d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala inline android::sp<android::hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() { 3210dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh return mDevice->getServiceCallback(); 3220dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh }; 3230dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 3240dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh // Camera device is only functional after remote being set 325d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala inline void setRemoteDevice(android::sp<android::hardware::camera2::ICameraDeviceUser> remote) { 3260dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh mDevice->setRemoteDevice(remote); 3270dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh } 3280dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 3290dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh private: 330d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala android::sp<android::CameraDevice> mDevice; 3310dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh}; 3320dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh 3330dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#endif // _ACAMERA_DEVICE_H 334