Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright (C) 2008, The Android Open Source Project |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H |
| 19 | #define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H |
| 20 | |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 21 | #include <binder/BinderService.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 22 | #include <camera/ICameraService.h> |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 23 | #include <hardware/camera.h> |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 24 | |
| 25 | /* This needs to be increased if we can have more cameras */ |
| 26 | #define MAX_CAMERAS 2 |
| 27 | |
| 28 | namespace android { |
| 29 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 30 | extern volatile int32_t gLogLevel; |
| 31 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 32 | class MemoryHeapBase; |
| 33 | class MediaPlayer; |
| 34 | |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 35 | class CameraService : |
| 36 | public BinderService<CameraService>, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 37 | public BnCameraService, |
| 38 | public IBinder::DeathRecipient |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 39 | { |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 40 | friend class BinderService<CameraService>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 41 | public: |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 42 | class Client; |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 43 | static char const* getServiceName() { return "media.camera"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 44 | |
| 45 | CameraService(); |
| 46 | virtual ~CameraService(); |
| 47 | |
| 48 | virtual int32_t getNumberOfCameras(); |
| 49 | virtual status_t getCameraInfo(int cameraId, |
| 50 | struct CameraInfo* cameraInfo); |
Wu-cheng Li | 08ad5ef | 2012-04-19 12:35:00 +0800 | [diff] [blame] | 51 | virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 52 | virtual void removeClient(const sp<ICameraClient>& cameraClient); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 53 | // returns plain pointer of client. Note that mClientLock should be acquired to |
| 54 | // prevent the client from destruction. The result can be NULL. |
| 55 | virtual Client* getClientByIdUnsafe(int cameraId); |
| 56 | virtual Mutex* getClientLockById(int cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 57 | |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame^] | 58 | virtual sp<Client> getClientByRemote(const wp<IBinder>& cameraClient); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 59 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 60 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 61 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 62 | Parcel* reply, uint32_t flags); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 63 | virtual void onFirstRef(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 64 | |
| 65 | enum sound_kind { |
| 66 | SOUND_SHUTTER = 0, |
| 67 | SOUND_RECORDING = 1, |
| 68 | NUM_SOUNDS |
| 69 | }; |
| 70 | |
| 71 | void loadSound(); |
| 72 | void playSound(sound_kind kind); |
| 73 | void releaseSound(); |
| 74 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 75 | class Client : public BnCamera |
| 76 | { |
| 77 | public: |
| 78 | // ICamera interface (see ICamera for details) |
| 79 | virtual void disconnect(); |
| 80 | virtual status_t connect(const sp<ICameraClient>& client) = 0; |
| 81 | virtual status_t lock() = 0; |
| 82 | virtual status_t unlock() = 0; |
| 83 | virtual status_t setPreviewDisplay(const sp<Surface>& surface) = 0; |
| 84 | virtual status_t setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture) = 0; |
| 85 | virtual void setPreviewCallbackFlag(int flag) = 0; |
| 86 | virtual status_t startPreview() = 0; |
| 87 | virtual void stopPreview() = 0; |
| 88 | virtual bool previewEnabled() = 0; |
| 89 | virtual status_t storeMetaDataInBuffers(bool enabled) = 0; |
| 90 | virtual status_t startRecording() = 0; |
| 91 | virtual void stopRecording() = 0; |
| 92 | virtual bool recordingEnabled() = 0; |
| 93 | virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0; |
| 94 | virtual status_t autoFocus() = 0; |
| 95 | virtual status_t cancelAutoFocus() = 0; |
| 96 | virtual status_t takePicture(int msgType) = 0; |
| 97 | virtual status_t setParameters(const String8& params) = 0; |
| 98 | virtual String8 getParameters() const = 0; |
| 99 | virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0; |
| 100 | |
| 101 | // Interface used by CameraService |
| 102 | Client(const sp<CameraService>& cameraService, |
| 103 | const sp<ICameraClient>& cameraClient, |
| 104 | int cameraId, |
| 105 | int cameraFacing, |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 106 | int clientPid, |
| 107 | int servicePid); |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 108 | ~Client(); |
| 109 | |
| 110 | // return our camera client |
| 111 | const sp<ICameraClient>& getCameraClient() { |
| 112 | return mCameraClient; |
| 113 | } |
| 114 | |
Eino-Ville Talvala | f69c70d | 2012-05-20 15:59:14 -0700 | [diff] [blame] | 115 | virtual status_t initialize(camera_module_t *module) = 0; |
| 116 | |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 117 | virtual status_t dump(int fd, const Vector<String16>& args) = 0; |
| 118 | |
| 119 | protected: |
| 120 | static Mutex* getClientLockFromCookie(void* user); |
| 121 | // convert client from cookie. Client lock should be acquired before getting Client. |
| 122 | static Client* getClientFromCookie(void* user); |
| 123 | |
| 124 | // the instance is in the middle of destruction. When this is set, |
| 125 | // the instance should not be accessed from callback. |
| 126 | // CameraService's mClientLock should be acquired to access this. |
| 127 | bool mDestructionStarted; |
| 128 | |
| 129 | // these are initialized in the constructor. |
| 130 | sp<CameraService> mCameraService; // immutable after constructor |
| 131 | sp<ICameraClient> mCameraClient; |
| 132 | int mCameraId; // immutable after constructor |
| 133 | int mCameraFacing; // immutable after constructor |
| 134 | pid_t mClientPid; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 135 | pid_t mServicePid; // immutable after constructor |
Eino-Ville Talvala | 5e08d60 | 2012-05-16 14:59:25 -0700 | [diff] [blame] | 136 | |
| 137 | }; |
| 138 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 139 | private: |
| 140 | Mutex mServiceLock; |
| 141 | wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 142 | Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 143 | int mNumberOfCameras; |
| 144 | |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 145 | // needs to be called with mServiceLock held |
Igor Murashkin | 294d0ec | 2012-10-05 10:44:57 -0700 | [diff] [blame^] | 146 | sp<Client> findClientUnsafe(const wp<IBinder>& cameraClient, int& outIndex); |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 147 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 148 | // atomics to record whether the hardware is allocated to some client. |
| 149 | volatile int32_t mBusy[MAX_CAMERAS]; |
| 150 | void setCameraBusy(int cameraId); |
| 151 | void setCameraFree(int cameraId); |
| 152 | |
| 153 | // sounds |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 154 | MediaPlayer* newMediaPlayer(const char *file); |
| 155 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 156 | Mutex mSoundLock; |
| 157 | sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; |
| 158 | int mSoundRef; // reference count (release all MediaPlayer when 0) |
| 159 | |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 160 | camera_module_t *mModule; |
Igor Murashkin | ecf17e8 | 2012-10-02 16:05:11 -0700 | [diff] [blame] | 161 | |
| 162 | // IBinder::DeathRecipient implementation |
| 163 | virtual void binderDied(const wp<IBinder> &who); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | } // namespace android |
| 167 | |
| 168 | #endif |