xref: /aosp_15_r20/frameworks/av/camera/include/camera/CameraBase.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
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 #ifndef ANDROID_HARDWARE_CAMERA_BASE_H
18 #define ANDROID_HARDWARE_CAMERA_BASE_H
19 
20 #include <android/content/AttributionSourceState.h>
21 #include <android/hardware/ICameraServiceListener.h>
22 
23 #include <utils/Mutex.h>
24 #include <binder/BinderService.h>
25 
26 struct camera_frame_metadata;
27 
28 namespace android {
29 
30 namespace hardware {
31 
32 
33 class ICameraService;
34 class ICameraServiceListener;
35 
36 enum {
37     /** The facing of the camera is opposite to that of the screen. */
38     CAMERA_FACING_BACK = 0,
39     /** The facing of the camera is the same as that of the screen. */
40     CAMERA_FACING_FRONT = 1,
41 };
42 
43 struct CameraInfo : public android::Parcelable {
44     /**
45      * The direction that the camera faces to. It should be CAMERA_FACING_BACK
46      * or CAMERA_FACING_FRONT.
47      */
48     int facing;
49 
50     /**
51      * The orientation of the camera image. The value is the angle that the
52      * camera image needs to be rotated clockwise so it shows correctly on the
53      * display in its natural orientation. It should be 0, 90, 180, or 270.
54      *
55      * For example, suppose a device has a naturally tall screen. The
56      * back-facing camera sensor is mounted in landscape. You are looking at
57      * the screen. If the top side of the camera sensor is aligned with the
58      * right edge of the screen in natural orientation, the value should be
59      * 90. If the top side of a front-facing camera sensor is aligned with the
60      * right of the screen, the value should be 270.
61      */
62     int orientation;
63 
64     virtual status_t writeToParcel(android::Parcel* parcel) const;
65     virtual status_t readFromParcel(const android::Parcel* parcel);
66 };
67 
68 /**
69  * Basic status information about a camera device - its id and its current
70  * state.
71  */
72 struct CameraStatus : public android::Parcelable {
73     /**
74      * The app-visible id of the camera device
75      */
76     std::string cameraId;
77 
78     /**
79      * Its current status, one of the ICameraService::STATUS_* fields
80      */
81     int32_t status;
82 
83     /**
84      * Unavailable physical camera names for a multi-camera device
85      */
86     std::vector<std::string> unavailablePhysicalIds;
87 
88     /**
89      * Client package name if camera is open, otherwise not applicable
90      */
91     std::string clientPackage;
92 
93     /**
94      * The id of the device owning the camera. For virtual cameras, this is the id of the virtual
95      * device owning the camera. For real cameras, this is the default device id, i.e.,
96      * kDefaultDeviceId.
97      */
98     int32_t deviceId;
99 
100     virtual status_t writeToParcel(android::Parcel* parcel) const;
101     virtual status_t readFromParcel(const android::Parcel* parcel);
102 
CameraStatusCameraStatus103     CameraStatus(std::string id, int32_t s, const std::vector<std::string>& unavailSubIds,
104             const std::string& clientPkg, int32_t devId) : cameraId(id), status(s),
105             unavailablePhysicalIds(unavailSubIds), clientPackage(clientPkg), deviceId(devId) {}
CameraStatusCameraStatus106     CameraStatus() : status(ICameraServiceListener::STATUS_PRESENT) {}
107 };
108 
109 } // namespace hardware
110 
111 using content::AttributionSourceState;
112 using hardware::CameraInfo;
113 
114 template <typename TCam>
115 struct CameraTraits {
116 };
117 
118 template <typename TCam, typename TCamTraits = CameraTraits<TCam> >
119 class CameraBase : public IBinder::DeathRecipient
120 {
121 public:
122     typedef typename TCamTraits::TCamListener       TCamListener;
123     typedef typename TCamTraits::TCamUser           TCamUser;
124     typedef typename TCamTraits::TCamCallbacks      TCamCallbacks;
125     typedef typename TCamTraits::TCamConnectService TCamConnectService;
126 
127     static sp<TCam>      connect(int cameraId,
128                                  int targetSdkVersion, int rotationOverride, bool forceSlowJpegMode,
129                                  const AttributionSourceState &clientAttribution,
130                                  int32_t devicePolicy);
131     virtual void         disconnect();
132 
133     void                 setListener(const sp<TCamListener>& listener);
134 
135     static int           getNumberOfCameras(const AttributionSourceState& clientAttribution,
136                                             int32_t devicePolicy);
137 
138     static status_t      getCameraInfo(int cameraId,
139                                        int rotationOverride,
140                                        const AttributionSourceState& clientAttribution,
141                                        int32_t devicePolicy,
142                                        /*out*/
143                                        struct hardware::CameraInfo* cameraInfo);
144 
145     sp<TCamUser>         remote();
146 
147     // Status is set to 'UNKNOWN_ERROR' after successful (re)connection
148     status_t             getStatus();
149 
150 protected:
151     CameraBase(int cameraId);
152     virtual              ~CameraBase();
153 
154     ////////////////////////////////////////////////////////
155     // TCamCallbacks implementation
156     ////////////////////////////////////////////////////////
157     virtual void         notifyCallback(int32_t msgType, int32_t ext,
158                                         int32_t ext2);
159 
160     ////////////////////////////////////////////////////////
161     // Common instance variables
162     ////////////////////////////////////////////////////////
163     Mutex                            mLock;
164 
165     virtual void                     binderDied(const wp<IBinder>& who);
166 
167     // helper function to obtain camera service handle
168     static const sp<::android::hardware::ICameraService> getCameraService();
169 
170     sp<TCamUser>                     mCamera;
171     status_t                         mStatus;
172 
173     sp<TCamListener>                 mListener;
174 
175     const int                        mCameraId;
176 
177     typedef CameraBase<TCam>         CameraBaseT;
178 };
179 
180 } // namespace android
181 
182 #endif
183