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