ACameraManager.h revision 0dea57fd9fc4b2ccaab97d9477359fbd5a626f5c
1/*
2 * Copyright (C) 2015 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 _ACAMERA_MANAGER_H
18#define _ACAMERA_MANAGER_H
19
20#include "NdkCameraManager.h"
21
22#include <camera/CameraMetadata.h>
23#include <camera/ICameraService.h>
24#include <camera/ICameraServiceListener.h>
25#include <binder/IServiceManager.h>
26#include <utils/StrongPointer.h>
27#include <utils/Mutex.h>
28
29#include <media/stagefright/foundation/ALooper.h>
30#include <media/stagefright/foundation/AHandler.h>
31#include <media/stagefright/foundation/AMessage.h>
32
33#include <set>
34#include <map>
35
36using namespace android;
37
38namespace android {
39
40/**
41 * Per-process singleton instance of CameraManger. Shared by all ACameraManager
42 * instances. Created when first ACameraManager is created and destroyed when
43 * all ACameraManager instances are deleted.
44 *
45 * TODO: maybe CameraManagerGlobal is better sutied in libcameraclient?
46 */
47class CameraManagerGlobal final : public RefBase {
48  public:
49    static CameraManagerGlobal& getInstance();
50    sp<ICameraService> getCameraService();
51
52    void registerAvailabilityCallback(
53            const ACameraManager_AvailabilityCallbacks *callback);
54    void unregisterAvailabilityCallback(
55            const ACameraManager_AvailabilityCallbacks *callback);
56
57  private:
58    sp<ICameraService> mCameraService;
59    const int          kCameraServicePollDelay = 500000; // 0.5s
60    const char*        kCameraServiceName      = "media.camera";
61    Mutex              mLock;
62
63    class DeathNotifier : public IBinder::DeathRecipient {
64      public:
65        DeathNotifier(CameraManagerGlobal* cm) : mCameraManager(cm) {}
66      protected:
67        // IBinder::DeathRecipient implementation
68        virtual void binderDied(const wp<IBinder>& who);
69      private:
70        const wp<CameraManagerGlobal> mCameraManager;
71    };
72    sp<DeathNotifier> mDeathNotifier;
73
74    class CameraServiceListener final : public BnCameraServiceListener {
75      public:
76        CameraServiceListener(CameraManagerGlobal* cm) : mCameraManager(cm) {}
77        virtual void onStatusChanged(Status status, int32_t cameraId);
78
79        // Torch API not implemented yet
80        virtual void onTorchStatusChanged(TorchStatus, const String16&) {};
81      private:
82        const wp<CameraManagerGlobal> mCameraManager;
83    };
84    sp<CameraServiceListener> mCameraServiceListener;
85
86    // Wrapper of ACameraManager_AvailabilityCallbacks so we can store it in std::set
87    struct Callback {
88        Callback(const ACameraManager_AvailabilityCallbacks *callback) :
89            mAvailable(callback->onCameraAvailable),
90            mUnavailable(callback->onCameraUnavailable),
91            mContext(callback->context) {}
92
93        bool operator == (const Callback& other) const {
94            return (mAvailable == other.mAvailable &&
95                    mUnavailable == other.mUnavailable &&
96                    mContext == other.mContext);
97        }
98        bool operator != (const Callback& other) const {
99            return !(*this == other);
100        }
101        bool operator < (const Callback& other) const {
102            if (*this == other) return false;
103            if (mContext != other.mContext) return mContext < other.mContext;
104            if (mAvailable != other.mAvailable) return mAvailable < other.mAvailable;
105            return mUnavailable < other.mUnavailable;
106        }
107        bool operator > (const Callback& other) const {
108            return (*this != other && !(*this < other));
109        }
110        ACameraManager_AvailabilityCallback mAvailable;
111        ACameraManager_AvailabilityCallback mUnavailable;
112        void*                               mContext;
113    };
114    std::set<Callback> mCallbacks;
115
116    // definition of handler and message
117    enum {
118        kWhatSendSingleCallback
119    };
120    static const char* kCameraIdKey;
121    static const char* kCallbackFpKey;
122    static const char* kContextKey;
123    class CallbackHandler : public AHandler {
124      public:
125        CallbackHandler() {}
126        void onMessageReceived(const sp<AMessage> &msg) override;
127      private:
128        inline void sendSingleCallback(
129                int32_t cameraId, void* context,
130                ACameraManager_AvailabilityCallback cb) const;
131    };
132    sp<CallbackHandler> mHandler;
133    sp<ALooper>         mCbLooper; // Looper thread where callbacks actually happen on
134
135    typedef ICameraServiceListener::Status Status;
136    void onStatusChanged(Status status, int32_t cameraId);
137    void onStatusChangedLocked(Status status, int32_t cameraId);
138    // Utils for status
139    static bool validStatus(Status status);
140    static bool isStatusAvailable(Status status);
141
142    // Map camera_id -> status
143    std::map<int32_t, Status> mDeviceStatusMap;
144
145    // For the singleton instance
146    static Mutex sLock;
147    static CameraManagerGlobal* sInstance;
148    CameraManagerGlobal() {};
149    ~CameraManagerGlobal();
150};
151
152} // namespace android;
153
154/**
155 * ACameraManager opaque struct definition
156 * Leave outside of android namespace because it's NDK struct
157 */
158struct ACameraManager {
159    ACameraManager() :
160            mCachedCameraIdList({kCameraIdListNotInit, nullptr}),
161            mGlobalManager(&(CameraManagerGlobal::getInstance())) {}
162    ~ACameraManager();
163    camera_status_t getCameraIdList(ACameraIdList** cameraIdList);
164    static void     deleteCameraIdList(ACameraIdList* cameraIdList);
165
166    camera_status_t getCameraCharacteristics(
167            const char *cameraId, ACameraMetadata **characteristics);
168    camera_status_t openCamera(const char* cameraId,
169                               ACameraDevice_StateCallbacks* callback,
170                               /*out*/ACameraDevice** device);
171
172  private:
173    camera_status_t getOrCreateCameraIdListLocked(ACameraIdList** cameraIdList);
174
175    enum {
176        kCameraIdListNotInit = -1
177    };
178    Mutex         mLock;
179    std::set<int> mCameraIds;          // Init by getOrCreateCameraIdListLocked
180    ACameraIdList mCachedCameraIdList; // Init by getOrCreateCameraIdListLocked
181    sp<CameraManagerGlobal> mGlobalManager;
182};
183
184#endif //_ACAMERA_MANAGER_H
185