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 | |
| 30 | class MemoryHeapBase; |
| 31 | class MediaPlayer; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 32 | class CameraHardwareInterface; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 33 | |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 34 | class CameraService : |
| 35 | public BinderService<CameraService>, |
| 36 | public BnCameraService |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 37 | { |
| 38 | class Client; |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 39 | friend class BinderService<CameraService>; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 40 | public: |
Mathias Agopian | 5462fc9 | 2010-07-14 18:41:18 -0700 | [diff] [blame] | 41 | static char const* getServiceName() { return "media.camera"; } |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 42 | |
| 43 | CameraService(); |
| 44 | virtual ~CameraService(); |
| 45 | |
| 46 | virtual int32_t getNumberOfCameras(); |
| 47 | virtual status_t getCameraInfo(int cameraId, |
| 48 | struct CameraInfo* cameraInfo); |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 49 | virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId, |
| 50 | bool force, bool keep); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 51 | virtual void removeClient(const sp<ICameraClient>& cameraClient); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 52 | // returns plain pointer of client. Note that mClientLock should be acquired to |
| 53 | // prevent the client from destruction. The result can be NULL. |
| 54 | virtual Client* getClientByIdUnsafe(int cameraId); |
| 55 | virtual Mutex* getClientLockById(int cameraId); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 56 | |
| 57 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 58 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 59 | Parcel* reply, uint32_t flags); |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 60 | virtual void onFirstRef(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 61 | |
| 62 | enum sound_kind { |
| 63 | SOUND_SHUTTER = 0, |
| 64 | SOUND_RECORDING = 1, |
| 65 | NUM_SOUNDS |
| 66 | }; |
| 67 | |
| 68 | void loadSound(); |
| 69 | void playSound(sound_kind kind); |
| 70 | void releaseSound(); |
| 71 | |
| 72 | private: |
| 73 | Mutex mServiceLock; |
| 74 | wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 75 | Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 76 | int mNumberOfCameras; |
| 77 | |
| 78 | // atomics to record whether the hardware is allocated to some client. |
| 79 | volatile int32_t mBusy[MAX_CAMERAS]; |
| 80 | void setCameraBusy(int cameraId); |
| 81 | void setCameraFree(int cameraId); |
| 82 | |
| 83 | // sounds |
Chih-Chung Chang | ff4f55c | 2011-10-17 19:03:12 +0800 | [diff] [blame] | 84 | MediaPlayer* newMediaPlayer(const char *file); |
| 85 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 86 | Mutex mSoundLock; |
| 87 | sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; |
| 88 | int mSoundRef; // reference count (release all MediaPlayer when 0) |
| 89 | |
| 90 | class Client : public BnCamera |
| 91 | { |
| 92 | public: |
| 93 | // ICamera interface (see ICamera for details) |
| 94 | virtual void disconnect(); |
| 95 | virtual status_t connect(const sp<ICameraClient>& client); |
| 96 | virtual status_t lock(); |
| 97 | virtual status_t unlock(); |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 98 | virtual status_t setPreviewDisplay(const sp<Surface>& surface); |
Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 99 | virtual status_t setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 100 | virtual void setPreviewCallbackFlag(int flag); |
| 101 | virtual status_t startPreview(); |
| 102 | virtual void stopPreview(); |
| 103 | virtual bool previewEnabled(); |
James Dong | e2ad673 | 2010-10-18 20:42:51 -0700 | [diff] [blame] | 104 | virtual status_t storeMetaDataInBuffers(bool enabled); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 105 | virtual status_t startRecording(); |
| 106 | virtual void stopRecording(); |
| 107 | virtual bool recordingEnabled(); |
| 108 | virtual void releaseRecordingFrame(const sp<IMemory>& mem); |
| 109 | virtual status_t autoFocus(); |
| 110 | virtual status_t cancelAutoFocus(); |
James Dong | e468ac5 | 2011-02-17 16:38:06 -0800 | [diff] [blame] | 111 | virtual status_t takePicture(int msgType); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 112 | virtual status_t setParameters(const String8& params); |
| 113 | virtual String8 getParameters() const; |
| 114 | virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); |
| 115 | private: |
| 116 | friend class CameraService; |
| 117 | Client(const sp<CameraService>& cameraService, |
| 118 | const sp<ICameraClient>& cameraClient, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 119 | const sp<CameraHardwareInterface>& hardware, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 120 | int cameraId, |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 121 | int cameraFacing, |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 122 | int clientPid, |
| 123 | bool keep); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 124 | ~Client(); |
| 125 | |
| 126 | // return our camera client |
| 127 | const sp<ICameraClient>& getCameraClient() { return mCameraClient; } |
| 128 | |
| 129 | // check whether the calling process matches mClientPid. |
| 130 | status_t checkPid() const; |
| 131 | status_t checkPidAndHardware() const; // also check mHardware != 0 |
| 132 | |
| 133 | // these are internal functions used to set up preview buffers |
| 134 | status_t registerPreviewBuffers(); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 135 | |
| 136 | // camera operation mode |
| 137 | enum camera_mode { |
| 138 | CAMERA_PREVIEW_MODE = 0, // frame automatically released |
| 139 | CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame() |
| 140 | }; |
| 141 | // these are internal functions used for preview/recording |
| 142 | status_t startCameraMode(camera_mode mode); |
| 143 | status_t startPreviewMode(); |
| 144 | status_t startRecordingMode(); |
| 145 | |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 146 | // internal function used by sendCommand to enable/disable shutter sound. |
| 147 | status_t enableShutterSound(bool enable); |
| 148 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 149 | // these are static callback functions |
| 150 | static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user); |
Wu-cheng Li | ff09ef8 | 2011-07-28 05:30:59 +0800 | [diff] [blame] | 151 | static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, |
| 152 | camera_frame_metadata_t *metadata, void* user); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 153 | static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user); |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 154 | static Mutex* getClientLockFromCookie(void* user); |
| 155 | // convert client from cookie. Client lock should be acquired before getting Client. |
| 156 | static Client* getClientFromCookie(void* user); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 157 | // handlers for messages |
Iliyan Malchev | 108dddf | 2011-03-28 16:10:12 -0700 | [diff] [blame] | 158 | void handleShutter(void); |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 159 | void handlePreviewData(int32_t msgType, const sp<IMemory>& mem, |
| 160 | camera_frame_metadata_t *metadata); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 161 | void handlePostview(const sp<IMemory>& mem); |
| 162 | void handleRawPicture(const sp<IMemory>& mem); |
| 163 | void handleCompressedPicture(const sp<IMemory>& mem); |
| 164 | void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2); |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 165 | void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr, |
| 166 | camera_frame_metadata_t *metadata); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 167 | void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); |
| 168 | |
| 169 | void copyFrameAndPostCopiedFrame( |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 170 | int32_t msgType, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 171 | const sp<ICameraClient>& client, |
| 172 | const sp<IMemoryHeap>& heap, |
Wu-cheng Li | 57c8618 | 2011-07-30 05:00:37 +0800 | [diff] [blame] | 173 | size_t offset, size_t size, |
| 174 | camera_frame_metadata_t *metadata); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 175 | |
Wu-cheng Li | e09591e | 2010-10-14 20:17:44 +0800 | [diff] [blame] | 176 | int getOrientation(int orientation, bool mirror); |
| 177 | |
Jamie Gennis | 0ed3ec0 | 2011-07-13 15:13:14 -0700 | [diff] [blame] | 178 | status_t setPreviewWindow( |
| 179 | const sp<IBinder>& binder, |
| 180 | const sp<ANativeWindow>& window); |
| 181 | |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 182 | void disconnectInternal(bool needCheckPid); |
| 183 | bool keep() const; |
| 184 | void waitRelease(int ms); |
| 185 | |
| 186 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 187 | // these are initialized in the constructor. |
| 188 | sp<CameraService> mCameraService; // immutable after constructor |
| 189 | sp<ICameraClient> mCameraClient; |
| 190 | int mCameraId; // immutable after constructor |
Wu-cheng Li | 012716a | 2010-10-08 22:04:43 +0800 | [diff] [blame] | 191 | int mCameraFacing; // immutable after constructor |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 192 | pid_t mClientPid; |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 193 | // Client wants to keep the camera from taking by other clients. |
| 194 | bool mKeep; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 195 | sp<CameraHardwareInterface> mHardware; // cleared after disconnect() |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 196 | int mPreviewCallbackFlag; |
Wu-cheng Li | 4a73f3d | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 197 | int mOrientation; // Current display orientation |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 198 | bool mPlayShutterSound; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 199 | |
| 200 | // Ensures atomicity among the public methods |
| 201 | mutable Mutex mLock; |
Wu-cheng Li | 2fd2440 | 2012-02-23 19:01:00 -0800 | [diff] [blame] | 202 | // This will get notified when the hardware is released. |
| 203 | Condition mReleaseCondition; |
Jamie Gennis | bfa33aa | 2010-12-20 11:51:31 -0800 | [diff] [blame] | 204 | // This is a binder of Surface or SurfaceTexture. |
| 205 | sp<IBinder> mSurface; |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 206 | sp<ANativeWindow> mPreviewWindow; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 207 | |
| 208 | // If the user want us to return a copy of the preview frame (instead |
| 209 | // of the original one), we allocate mPreviewBuffer and reuse it if possible. |
| 210 | sp<MemoryHeapBase> mPreviewBuffer; |
| 211 | |
Keun young Park | d8973a7 | 2012-03-28 14:13:09 -0700 | [diff] [blame] | 212 | // the instance is in the middle of destruction. When this is set, |
| 213 | // the instance should not be accessed from callback. |
| 214 | // CameraService's mClientLock should be acquired to access this. |
| 215 | bool mDestructionStarted; |
| 216 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 217 | // We need to avoid the deadlock when the incoming command thread and |
| 218 | // the CameraHardwareInterface callback thread both want to grab mLock. |
| 219 | // An extra flag is used to tell the callback thread that it should stop |
| 220 | // trying to deliver the callback messages if the client is not |
| 221 | // interested in it anymore. For example, if the client is calling |
| 222 | // stopPreview(), the preview frame messages do not need to be delivered |
| 223 | // anymore. |
| 224 | |
| 225 | // This function takes the same parameter as the enableMsgType() and |
| 226 | // disableMsgType() functions in CameraHardwareInterface. |
| 227 | void enableMsgType(int32_t msgType); |
| 228 | void disableMsgType(int32_t msgType); |
| 229 | volatile int32_t mMsgEnabled; |
| 230 | |
| 231 | // This function keeps trying to grab mLock, or give up if the message |
| 232 | // is found to be disabled. It returns true if mLock is grabbed. |
| 233 | bool lockIfMessageWanted(int32_t msgType); |
| 234 | }; |
Iliyan Malchev | 8951a97 | 2011-04-14 16:55:59 -0700 | [diff] [blame] | 235 | |
| 236 | camera_module_t *mModule; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | } // namespace android |
| 240 | |
| 241 | #endif |