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 <utils/Mutex.h>
21 
22 struct camera_frame_metadata;
23 
24 namespace android {
25 
26 namespace hardware {
27 
28 
29 class ICameraService;
30 class ICameraServiceListener;
31 
32 struct CameraInfo : public android::Parcelable {
33     /**
34      * The direction that the camera faces to. It should be CAMERA_FACING_BACK
35      * or CAMERA_FACING_FRONT.
36      */
37     int facing;
38 
39     /**
40      * The orientation of the camera image. The value is the angle that the
41      * camera image needs to be rotated clockwise so it shows correctly on the
42      * display in its natural orientation. It should be 0, 90, 180, or 270.
43      *
44      * For example, suppose a device has a naturally tall screen. The
45      * back-facing camera sensor is mounted in landscape. You are looking at
46      * the screen. If the top side of the camera sensor is aligned with the
47      * right edge of the screen in natural orientation, the value should be
48      * 90. If the top side of a front-facing camera sensor is aligned with the
49      * right of the screen, the value should be 270.
50      */
51     int orientation;
52 
53     virtual status_t writeToParcel(Parcel* parcel) const;
54     virtual status_t readFromParcel(const Parcel* parcel);
55 
56 };
57 
58 } // namespace hardware
59 
60 using hardware::CameraInfo;
61 
62 
63 template <typename TCam>
64 struct CameraTraits {
65 };
66 
67 template <typename TCam, typename TCamTraits = CameraTraits<TCam> >
68 class CameraBase : public IBinder::DeathRecipient
69 {
70 public:
71     typedef typename TCamTraits::TCamListener       TCamListener;
72     typedef typename TCamTraits::TCamUser           TCamUser;
73     typedef typename TCamTraits::TCamCallbacks      TCamCallbacks;
74     typedef typename TCamTraits::TCamConnectService TCamConnectService;
75 
76     static sp<TCam>      connect(int cameraId,
77                                  const String16& clientPackageName,
78                                  int clientUid, int clientPid);
79     virtual void         disconnect();
80 
81     void                 setListener(const sp<TCamListener>& listener);
82 
83     static int           getNumberOfCameras();
84 
85     static status_t      getCameraInfo(int cameraId,
86                                        /*out*/
87                                        struct hardware::CameraInfo* cameraInfo);
88 
89     static status_t      addServiceListener(
90         const sp<::android::hardware::ICameraServiceListener>& listener);
91 
92     static status_t      removeServiceListener(
93         const sp<::android::hardware::ICameraServiceListener>& listener);
94 
95     sp<TCamUser>         remote();
96 
97     // Status is set to 'UNKNOWN_ERROR' after successful (re)connection
98     status_t             getStatus();
99 
100 protected:
101     CameraBase(int cameraId);
102     virtual              ~CameraBase();
103 
104     ////////////////////////////////////////////////////////
105     // TCamCallbacks implementation
106     ////////////////////////////////////////////////////////
107     virtual void         notifyCallback(int32_t msgType, int32_t ext,
108                                         int32_t ext2);
109 
110     ////////////////////////////////////////////////////////
111     // Common instance variables
112     ////////////////////////////////////////////////////////
113     Mutex                            mLock;
114 
115     virtual void                     binderDied(const wp<IBinder>& who);
116 
117     // helper function to obtain camera service handle
118     static const sp<::android::hardware::ICameraService>& getCameraService();
119 
120     sp<TCamUser>                     mCamera;
121     status_t                         mStatus;
122 
123     sp<TCamListener>                 mListener;
124 
125     const int                        mCameraId;
126 
127     typedef CameraBase<TCam>         CameraBaseT;
128 };
129 
130 }; // namespace android
131 
132 #endif
133