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,
635fbe0ba24d88910e68610add74fd1e1b94d282b5Emilian Peev            const ACaptureRequest* sessionParameters,
64ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            const ACameraCaptureSession_stateCallbacks* callbacks,
65ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            /*out*/ACameraCaptureSession** session);
66ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
670dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    // Callbacks from camera service
68d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    class ServiceCallback : public hardware::camera2::BnCameraDeviceCallbacks {
690dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh      public:
70d19d994770a04f02c204affd29985c5aeaa58baaChih-Hung Hsieh        explicit ServiceCallback(CameraDevice* device) : mDevice(device) {}
71d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala        binder::Status onDeviceError(int32_t errorCode,
720dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh                           const CaptureResultExtras& resultExtras) override;
73d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala        binder::Status onDeviceIdle() override;
74d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala        binder::Status onCaptureStarted(const CaptureResultExtras& resultExtras,
750dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh                              int64_t timestamp) override;
76d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala        binder::Status onResultReceived(const CameraMetadata& metadata,
775c22c15ed8c593189ed9bb8fcbccace2ca63b82aShuzhen Wang                              const CaptureResultExtras& resultExtras,
785c22c15ed8c593189ed9bb8fcbccace2ca63b82aShuzhen Wang                              const std::vector<PhysicalCaptureResultInfo>& physicalResultInfos) override;
79d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala        binder::Status onPrepared(int streamId) override;
809d06601e54848f076b7472a376c672215cd70c46Shuzhen Wang        binder::Status onRequestQueueEmpty() override;
818ca23dca1cd450b4d93e8da1c8bd6f6ee61e2f5fYin-Chia Yeh        binder::Status onRepeatingRequestError(int64_t lastFrameNumber,
828ca23dca1cd450b4d93e8da1c8bd6f6ee61e2f5fYin-Chia Yeh                int32_t stoppedSequenceId) override;
830dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh      private:
840dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        const wp<CameraDevice> mDevice;
850dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    };
86d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    inline sp<hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() {
87d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala        return mServiceCallback;
88d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    };
890dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
900dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    // Camera device is only functional after remote being set
91d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    void setRemoteDevice(sp<hardware::camera2::ICameraDeviceUser> remote);
920dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
93ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    inline ACameraDevice* getWrapper() const { return mWrapper; };
94ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
950dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh  private:
96ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    friend ACameraCaptureSession;
970dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    camera_status_t checkCameraClosedOrErrorLocked() const;
980dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
99ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    // device goes into fatal error state after this
100ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    void setCameraDeviceErrorLocked(camera_status_t error);
101ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
1026e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh    void disconnectLocked(sp<ACameraCaptureSession>& session); // disconnect from camera service
103ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
104ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    camera_status_t stopRepeatingLocked();
105ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
106309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh    camera_status_t flushLocked(ACameraCaptureSession*);
107309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh
108ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    camera_status_t waitUntilIdleLocked();
109ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
110ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
111ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    camera_status_t captureLocked(sp<ACameraCaptureSession> session,
112ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
113ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            int numRequests, ACaptureRequest** requests,
114ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            /*optional*/int* captureSequenceId);
115ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
116ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    camera_status_t setRepeatingRequestsLocked(sp<ACameraCaptureSession> session,
117ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
118ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            int numRequests, ACaptureRequest** requests,
119ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            /*optional*/int* captureSequenceId);
120ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
121ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    camera_status_t submitRequestsLocked(
122ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            sp<ACameraCaptureSession> session,
123ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
124ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            int numRequests, ACaptureRequest** requests,
125ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            /*out*/int* captureSequenceId,
126ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            bool isRepeating);
127ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
1284dfa4cca7a4fcf5ea4a37dca1fef7f2f56f6dd8cYin-Chia Yeh    camera_status_t updateOutputConfigurationLocked(ACaptureSessionOutput *output);
12940ead60d7163c2d5577099d49f11da7afa6124d1Emilian Peev
1304dfa4cca7a4fcf5ea4a37dca1fef7f2f56f6dd8cYin-Chia Yeh    camera_status_t allocateCaptureRequest(
131ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            const ACaptureRequest* request, sp<CaptureRequest>& outReq);
132ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
133ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static ACaptureRequest* allocateACaptureRequest(sp<CaptureRequest>& req);
134ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static void freeACaptureRequest(ACaptureRequest*);
135ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
136ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    // only For session to hold device lock
137ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    // Always grab device lock before grabbing session lock
138ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    void lockDeviceForSessionOps() const { mDeviceLock.lock(); };
139ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    void unlockDevice() const { mDeviceLock.unlock(); };
140ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
141ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    // For capture session to notify its end of life
142ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    void notifySessionEndOfLifeLocked(ACameraCaptureSession* session);
143ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
1445fbe0ba24d88910e68610add74fd1e1b94d282b5Emilian Peev    camera_status_t configureStreamsLocked(const ACaptureSessionOutputContainer* outputs,
1455fbe0ba24d88910e68610add74fd1e1b94d282b5Emilian Peev           const ACaptureRequest* sessionParameters);
146ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
1476e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh    // Input message will be posted and cleared after this returns
1486e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh    void postSessionMsgAndCleanup(sp<AMessage>& msg);
1496e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh
150e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh    static camera_status_t getIGBPfromAnw(
151e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh            ANativeWindow* anw, sp<IGraphicBufferProducer>& out);
152ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
153ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static camera_status_t getSurfaceFromANativeWindow(
154ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            ANativeWindow* anw, sp<Surface>& out);
1550dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
1560dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    mutable Mutex mDeviceLock;
1570dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    const String8 mCameraId;                          // Camera ID
1580dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    const ACameraDevice_StateCallbacks mAppCallbacks; // Callback to app
1590dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    const std::unique_ptr<ACameraMetadata> mChars;    // Camera characteristics
1600dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    const sp<ServiceCallback> mServiceCallback;
1610dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ACameraDevice* mWrapper;
1620dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
163e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh    // stream id -> pair of (ANW* from application, OutputConfiguration used for camera service)
164e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh    std::map<int, std::pair<ANativeWindow*, OutputConfiguration>> mConfiguredOutputs;
165ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
1660dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    // TODO: maybe a bool will suffice for synchronous implementation?
1670dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    std::atomic_bool mClosing;
1680dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    inline bool isClosed() { return mClosing; }
1690dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
170309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh    bool mInError = false;
171309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh    camera_status_t mError = ACAMERA_OK;
1720dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    void onCaptureErrorLocked(
173d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala            int32_t errorCode,
1740dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh            const CaptureResultExtras& resultExtras);
1750dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
176309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh    bool mIdle = true;
177ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    // This will avoid a busy session being deleted before it's back to idle state
178ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    sp<ACameraCaptureSession> mBusySession;
1790dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
180d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    sp<hardware::camera2::ICameraDeviceUser> mRemote;
1810dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
1820dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    // Looper thread to handle callback to app
1830dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    sp<ALooper> mCbLooper;
1840dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    // definition of handler and message
1850dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    enum {
186ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        // Device state callbacks
187e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh        kWhatOnDisconnected,   // onDisconnected
188e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh        kWhatOnError,          // onError
189ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        // Session state callbacks
190e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh        kWhatSessionStateCb,   // onReady, onActive
191ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        // Capture callbacks
192e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh        kWhatCaptureStart,     // onCaptureStarted
193e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh        kWhatCaptureResult,    // onCaptureProgressed, onCaptureCompleted
194e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh        kWhatCaptureFail,      // onCaptureFailed
195e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh        kWhatCaptureSeqEnd,    // onCaptureSequenceCompleted
196e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh        kWhatCaptureSeqAbort,  // onCaptureSequenceAborted
1976e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh        kWhatCaptureBufferLost,// onCaptureBufferLost
1986e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh        // Internal cleanup
1996e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh        kWhatCleanUpSessions   // Cleanup cached sp<ACameraCaptureSession>
2000dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    };
2010dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    static const char* kContextKey;
2020dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    static const char* kDeviceKey;
2030dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    static const char* kErrorCodeKey;
204ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const char* kCallbackFpKey;
205ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const char* kSessionSpKey;
206ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const char* kCaptureRequestKey;
207ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const char* kTimeStampKey;
208ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const char* kCaptureResultKey;
209ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const char* kCaptureFailureKey;
210ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const char* kSequenceIdKey;
211ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const char* kFrameNumberKey;
212e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh    static const char* kAnwKey;
2136e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh
2140dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    class CallbackHandler : public AHandler {
2150dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh      public:
2160dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        void onMessageReceived(const sp<AMessage> &msg) override;
2176e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh
2186e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh      private:
2196e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh        // This handler will cache all capture session sp until kWhatCleanUpSessions
2206e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh        // is processed. This is used to guarantee the last session reference is always
2216e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh        // being removed in callback thread without holding camera device lock
2226e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh        Vector<sp<ACameraCaptureSession>> mCachedSessions;
2230dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    };
2240dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    sp<CallbackHandler> mHandler;
2250dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
226ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    /***********************************
227ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh     * Capture session related members *
228ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh     ***********************************/
229ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    // The current active session
2306e2353b814ae81d7c69f38fbd65467c48f3000bfYin-Chia Yeh    wp<ACameraCaptureSession> mCurrentSession;
231309d05d030903e7849affd60c58d4236147aa390Yin-Chia Yeh    bool mFlushing = false;
2320dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
233ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    int mNextSessionId = 0;
2340dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    // TODO: might need another looper/handler to handle callbacks from service
2350dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
236ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const int REQUEST_ID_NONE = -1;
237ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    int mRepeatingSequenceId = REQUEST_ID_NONE;
238ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
239ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    // sequence id -> last frame number map
240ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    std::map<int, int64_t> mSequenceLastFrameNumberMap;
241ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
242ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    struct CallbackHolder {
243ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        CallbackHolder(sp<ACameraCaptureSession>          session,
244ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh                       const Vector<sp<CaptureRequest> >& requests,
245ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh                       bool                               isRepeating,
246ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh                       ACameraCaptureSession_captureCallbacks* cbs);
247ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
248ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        static ACameraCaptureSession_captureCallbacks fillCb(
249ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh                ACameraCaptureSession_captureCallbacks* cbs) {
250ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            if (cbs != nullptr) {
251ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh                return *cbs;
252ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            }
253e081c59aeb309f5d91b99c433fc252e4570d4db8Yin-Chia Yeh            return { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
254ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        }
255ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
256ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        sp<ACameraCaptureSession>   mSession;
257ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        Vector<sp<CaptureRequest> > mRequests;
258ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        const bool                  mIsRepeating;
259ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        ACameraCaptureSession_captureCallbacks mCallbacks;
260ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    };
261ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    // sequence id -> callbacks map
262ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    std::map<int, CallbackHolder> mSequenceCallbackMap;
263ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
264ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    static const int64_t NO_FRAMES_CAPTURED = -1;
265ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    class FrameNumberTracker {
266ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh      public:
267ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        // TODO: Called in onResultReceived and onCaptureErrorLocked
268ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        void updateTracker(int64_t frameNumber, bool isError);
269ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        inline int64_t getCompletedFrameNumber() { return mCompletedFrameNumber; }
270ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh      private:
271ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        void update();
272ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        void updateCompletedFrameNumber(int64_t frameNumber);
273ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
274ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        int64_t mCompletedFrameNumber = NO_FRAMES_CAPTURED;
275ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        List<int64_t> mSkippedFrameNumbers;
276ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh        std::set<int64_t> mFutureErrorSet;
277ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    };
278ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    FrameNumberTracker mFrameNumberTracker;
279ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
280ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    void checkRepeatingSequenceCompleteLocked(const int sequenceId, const int64_t lastFrameNumber);
281ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    void checkAndFireSequenceCompleteLocked();
282ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
283ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    // Misc variables
284ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    int32_t mShadingMapSize[2];   // const after constructor
285ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    int32_t mPartialResultCount;  // const after constructor
2860dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
2870dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh};
2880dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
2890dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh} // namespace android;
2900dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
2910dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh/**
2920dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * ACameraDevice opaque struct definition
2930dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh * Leave outside of android namespace because it's NDK struct
2940dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh */
2950dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yehstruct ACameraDevice {
2960dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ACameraDevice(const char* id, ACameraDevice_StateCallbacks* cb,
2970dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh                  std::unique_ptr<ACameraMetadata> chars) :
2980dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh            mDevice(new CameraDevice(id, cb, std::move(chars), this)) {}
2990dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
3000dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    ~ACameraDevice() {};
3010dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
302ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    /*******************
303ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh     * NDK public APIs *
304ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh     *******************/
3050dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    inline const char* getId() const { return mDevice->getId(); }
3060dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
3070dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    camera_status_t createCaptureRequest(
3080dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh            ACameraDevice_request_template templateId,
3090dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh            ACaptureRequest** request) const {
3100dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        return mDevice->createCaptureRequest(templateId, request);
3110dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }
3120dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
313ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    camera_status_t createCaptureSession(
314ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            const ACaptureSessionOutputContainer*       outputs,
3155fbe0ba24d88910e68610add74fd1e1b94d282b5Emilian Peev            const ACaptureRequest* sessionParameters,
316ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            const ACameraCaptureSession_stateCallbacks* callbacks,
317ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh            /*out*/ACameraCaptureSession** session) {
3185fbe0ba24d88910e68610add74fd1e1b94d282b5Emilian Peev        return mDevice->createCaptureSession(outputs, sessionParameters, callbacks, session);
319ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    }
320ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh
321ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh    /***********************
322ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh     * Device interal APIs *
323ead9146f844ee194a4f4244ba8ae1a3aece12b63Yin-Chia Yeh     ***********************/
324d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    inline android::sp<android::hardware::camera2::ICameraDeviceCallbacks> getServiceCallback() {
3250dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        return mDevice->getServiceCallback();
3260dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    };
3270dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
3280dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    // Camera device is only functional after remote being set
329d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    inline void setRemoteDevice(android::sp<android::hardware::camera2::ICameraDeviceUser> remote) {
3300dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh        mDevice->setRemoteDevice(remote);
3310dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh    }
3320dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
3330dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh  private:
334d56db1d2bee182d1851097a9c712712fc094d117Eino-Ville Talvala    android::sp<android::CameraDevice> mDevice;
3350dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh};
3360dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh
3370dea57fd9fc4b2ccaab97d9477359fbd5a626f5cYin-Chia Yeh#endif // _ACAMERA_DEVICE_H
338