blob: 88135bac35744ca86c66d06ec20e74306268a793 [file] [log] [blame]
Yin-Chia Yehead91462016-01-06 16:45:08 -08001/*
2 * Copyright (C) 2016 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#ifndef _ACAMERA_CAPTURE_SESSION_H
17#define _ACAMERA_CAPTURE_SESSION_H
18
19#include <set>
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080020#include <string>
Yin-Chia Yehead91462016-01-06 16:45:08 -080021#include <hardware/camera3.h>
Colin Cross7e8d4ba2017-05-04 16:17:42 -070022#include <camera/NdkCameraDevice.h>
Jayant Chowdhary6df26072018-11-06 23:55:12 -080023
24#ifdef __ANDROID_VNDK__
25#include "ndk_vendor/impl/ACameraDevice.h"
26#include "ndk_vendor/impl/ACameraCaptureSessionVendor.h"
27#else
Yin-Chia Yehead91462016-01-06 16:45:08 -080028#include "ACameraDevice.h"
29
30using namespace android;
31
32struct ACaptureSessionOutput {
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080033 explicit ACaptureSessionOutput(ACameraWindowType* window, bool isShared = false,
34 const char* physicalCameraId = "") :
35 mWindow(window), mIsShared(isShared), mPhysicalCameraId(physicalCameraId) {};
Yin-Chia Yehead91462016-01-06 16:45:08 -080036
37 bool operator == (const ACaptureSessionOutput& other) const {
38 return mWindow == other.mWindow;
39 }
40 bool operator != (const ACaptureSessionOutput& other) const {
41 return mWindow != other.mWindow;
42 }
43 bool operator < (const ACaptureSessionOutput& other) const {
44 return mWindow < other.mWindow;
45 }
46 bool operator > (const ACaptureSessionOutput& other) const {
47 return mWindow > other.mWindow;
48 }
49
Avichal Rakeshf099b232022-10-27 15:44:50 -070050 inline bool isWindowEqual(ACameraWindowType* window) const {
51 return mWindow == window;
52 }
53
54 // returns true if the window was successfully added, false otherwise.
55 inline bool addSharedWindow(ACameraWindowType* window) {
56 auto ret = mSharedWindows.insert(window);
57 return ret.second;
58 }
59
60 // returns the number of elements removed.
61 inline size_t removeSharedWindow(ACameraWindowType* window) {
62 return mSharedWindows.erase(window);
63 }
64
Jayant Chowdhary6df26072018-11-06 23:55:12 -080065 ACameraWindowType* mWindow;
66 std::set<ACameraWindowType *> mSharedWindows;
Emilian Peev40ead602017-09-26 15:46:36 +010067 bool mIsShared;
Yin-Chia Yehead91462016-01-06 16:45:08 -080068 int mRotation = CAMERA3_STREAM_ROTATION_0;
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080069 std::string mPhysicalCameraId;
Yin-Chia Yehead91462016-01-06 16:45:08 -080070};
Jayant Chowdhary6df26072018-11-06 23:55:12 -080071#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -080072
73struct ACaptureSessionOutputContainer {
74 std::set<ACaptureSessionOutput> mOutputs;
75};
76
77/**
Jayant Chowdhary719b4662023-03-14 20:30:57 +000078 * Capture session state callbacks used in {@link ACameraDevice_setPrepareCallbacks}
79 */
80typedef struct ACameraCaptureSession_prepareCallbacks {
81 /// optional application context. This will be passed in the context
82 /// parameter of the {@link onWindowPrepared} callback.
83 void* context;
84
85 ACameraCaptureSession_prepareCallback onWindowPrepared;
86} ACameraCaptureSession_prepareCallbacks;
87
88/**
Yin-Chia Yehead91462016-01-06 16:45:08 -080089 * ACameraCaptureSession opaque struct definition
90 * Leave outside of android namespace because it's NDK struct
91 */
92struct ACameraCaptureSession : public RefBase {
93 public:
Avichal Rakeshf099b232022-10-27 15:44:50 -070094#ifdef __ANDROID_VNDK__
95 ACameraCaptureSession(
96 int id,
97 const ACaptureSessionOutputContainer* outputs,
98 const ACameraCaptureSession_stateCallbacks* cb,
99 std::weak_ptr<android::acam::CameraDevice> device) :
100 mId(id), mOutput(*outputs), mUserSessionCallback(*cb),
101 mDevice(std::move(device)) {}
102#else
Yin-Chia Yehead91462016-01-06 16:45:08 -0800103 ACameraCaptureSession(
104 int id,
105 const ACaptureSessionOutputContainer* outputs,
106 const ACameraCaptureSession_stateCallbacks* cb,
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800107 android::acam::CameraDevice* device) :
Yin-Chia Yehead91462016-01-06 16:45:08 -0800108 mId(id), mOutput(*outputs), mUserSessionCallback(*cb),
109 mDevice(device) {}
Avichal Rakeshf099b232022-10-27 15:44:50 -0700110#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -0800111
112 // This can be called in app calling close() or after some app callback is finished
113 // Make sure the caller does not hold device or session lock!
114 ~ACameraCaptureSession();
115
116 // No API except Session_Close will work if device is closed
117 // A session will enter closed state when one of the following happens:
118 // 1. Explicitly closed by app
119 // 2. Replaced by a newer session
120 // 3. Device is closed
121 bool isClosed() { Mutex::Autolock _l(mSessionLock); return mIsClosed; }
122
123 // Close the session and mark app no longer need this session.
124 void closeByApp();
125
126 camera_status_t stopRepeating();
127
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700128 camera_status_t abortCaptures();
129
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800130 template<class T>
Yin-Chia Yehead91462016-01-06 16:45:08 -0800131 camera_status_t setRepeatingRequest(
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800132 /*optional*/T* cbs,
Yin-Chia Yehead91462016-01-06 16:45:08 -0800133 int numRequests, ACaptureRequest** requests,
134 /*optional*/int* captureSequenceId);
135
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800136 template<class T>
Yin-Chia Yehead91462016-01-06 16:45:08 -0800137 camera_status_t capture(
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800138 /*optional*/T* cbs,
Yin-Chia Yehead91462016-01-06 16:45:08 -0800139 int numRequests, ACaptureRequest** requests,
140 /*optional*/int* captureSequenceId);
141
Emilian Peev40ead602017-09-26 15:46:36 +0100142 camera_status_t updateOutputConfiguration(ACaptureSessionOutput *output);
143
Jayant Chowdhary719b4662023-03-14 20:30:57 +0000144 void setWindowPreparedCallback(void *context,
145 ACameraCaptureSession_prepareCallback cb) {
Jayant Chowdhary09b368b2023-02-13 06:53:05 +0000146 Mutex::Autolock _l(mSessionLock);
Jayant Chowdhary719b4662023-03-14 20:30:57 +0000147 mPreparedCb.context = context;
148 mPreparedCb.onWindowPrepared = cb;
Jayant Chowdhary09b368b2023-02-13 06:53:05 +0000149 }
150 camera_status_t prepare(ACameraWindowType *window);
151
Yin-Chia Yehead91462016-01-06 16:45:08 -0800152 ACameraDevice* getDevice();
153
154 private:
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800155 friend class android::acam::CameraDevice;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800156
157 // Close session because app close camera device, camera device got ERROR_DISCONNECTED,
158 // or a new session is replacing this session.
159 void closeByDevice();
160
Avichal Rakeshf099b232022-10-27 15:44:50 -0700161#ifdef __ANDROID_VNDK__
162 std::shared_ptr<android::acam::CameraDevice> getDevicePtr();
163#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800164 sp<android::acam::CameraDevice> getDeviceSp();
Avichal Rakeshf099b232022-10-27 15:44:50 -0700165#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -0800166
167 const int mId;
168 const ACaptureSessionOutputContainer mOutput;
169 const ACameraCaptureSession_stateCallbacks mUserSessionCallback;
Avichal Rakeshf099b232022-10-27 15:44:50 -0700170#ifdef __ANDROID_VNDK__
171 const std::weak_ptr<android::acam::CameraDevice> mDevice;
172#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800173 const wp<android::acam::CameraDevice> mDevice;
Avichal Rakeshf099b232022-10-27 15:44:50 -0700174#endif
175
Yin-Chia Yehead91462016-01-06 16:45:08 -0800176 bool mIsClosed = false;
Yin-Chia Yeh085dd092016-03-02 14:16:31 -0800177 bool mClosedByApp = false;
Jayant Chowdhary09b368b2023-02-13 06:53:05 +0000178 ACameraCaptureSession_prepareCallbacks mPreparedCb;
Yin-Chia Yehead91462016-01-06 16:45:08 -0800179 Mutex mSessionLock;
180};
181
182#endif // _ACAMERA_CAPTURE_SESSION_H