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