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 /**
69  * Basic status information about a camera device - its name and its current
70  * state.
71  */
72 struct CameraStatus : public android::Parcelable {
73     /**
74      * The name of the camera device
75      */
76     String8 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<String8> unavailablePhysicalIds;
87 
88     virtual status_t writeToParcel(android::Parcel* parcel) const;
89     virtual status_t readFromParcel(const android::Parcel* parcel);
90 
CameraStatusCameraStatus91     CameraStatus(String8 id, int32_t s, const std::vector<String8>& unavailSubIds) :
92             cameraId(id), status(s), unavailablePhysicalIds(unavailSubIds) {}
CameraStatusCameraStatus93     CameraStatus() : status(ICameraServiceListener::STATUS_PRESENT) {}
94 };
95 
96 } // namespace hardware
97 
98 using hardware::CameraInfo;
99 
100 
101 template <typename TCam>
102 struct CameraTraits {
103 };
104 
105 template <typename TCam, typename TCamTraits = CameraTraits<TCam> >
106 class CameraBase : public IBinder::DeathRecipient
107 {
108 public:
109     typedef typename TCamTraits::TCamListener       TCamListener;
110     typedef typename TCamTraits::TCamUser           TCamUser;
111     typedef typename TCamTraits::TCamCallbacks      TCamCallbacks;
112     typedef typename TCamTraits::TCamConnectService TCamConnectService;
113 
114     static sp<TCam>      connect(int cameraId,
115                                  const String16& clientPackageName,
116                                  int clientUid, int clientPid, int targetSdkVersion);
117     virtual void         disconnect();
118 
119     void                 setListener(const sp<TCamListener>& listener);
120 
121     static int           getNumberOfCameras();
122 
123     static status_t      getCameraInfo(int cameraId,
124                                        /*out*/
125                                        struct hardware::CameraInfo* cameraInfo);
126 
127     sp<TCamUser>         remote();
128 
129     // Status is set to 'UNKNOWN_ERROR' after successful (re)connection
130     status_t             getStatus();
131 
132 protected:
133     CameraBase(int cameraId);
134     virtual              ~CameraBase();
135 
136     ////////////////////////////////////////////////////////
137     // TCamCallbacks implementation
138     ////////////////////////////////////////////////////////
139     virtual void         notifyCallback(int32_t msgType, int32_t ext,
140                                         int32_t ext2);
141 
142     ////////////////////////////////////////////////////////
143     // Common instance variables
144     ////////////////////////////////////////////////////////
145     Mutex                            mLock;
146 
147     virtual void                     binderDied(const wp<IBinder>& who);
148 
149     // helper function to obtain camera service handle
150     static const sp<::android::hardware::ICameraService> getCameraService();
151 
152     sp<TCamUser>                     mCamera;
153     status_t                         mStatus;
154 
155     sp<TCamListener>                 mListener;
156 
157     const int                        mCameraId;
158 
159     typedef CameraBase<TCam>         CameraBaseT;
160 };
161 
162 }; // namespace android
163 
164 #endif
165