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> |
| 22 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 23 | #include <camera/ICameraService.h> |
| 24 | #include <camera/CameraHardwareInterface.h> |
| 25 | |
| 26 | /* This needs to be increased if we can have more cameras */ |
| 27 | #define MAX_CAMERAS 2 |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | class MemoryHeapBase; |
| 32 | class MediaPlayer; |
| 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); |
| 49 | virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId); |
| 50 | virtual void removeClient(const sp<ICameraClient>& cameraClient); |
| 51 | virtual sp<Client> getClientById(int cameraId); |
| 52 | |
| 53 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 54 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 55 | Parcel* reply, uint32_t flags); |
| 56 | |
| 57 | enum sound_kind { |
| 58 | SOUND_SHUTTER = 0, |
| 59 | SOUND_RECORDING = 1, |
| 60 | NUM_SOUNDS |
| 61 | }; |
| 62 | |
| 63 | void loadSound(); |
| 64 | void playSound(sound_kind kind); |
| 65 | void releaseSound(); |
| 66 | |
| 67 | private: |
| 68 | Mutex mServiceLock; |
| 69 | wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock |
| 70 | int mNumberOfCameras; |
| 71 | |
| 72 | // atomics to record whether the hardware is allocated to some client. |
| 73 | volatile int32_t mBusy[MAX_CAMERAS]; |
| 74 | void setCameraBusy(int cameraId); |
| 75 | void setCameraFree(int cameraId); |
| 76 | |
| 77 | // sounds |
| 78 | Mutex mSoundLock; |
| 79 | sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; |
| 80 | int mSoundRef; // reference count (release all MediaPlayer when 0) |
| 81 | |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 82 | // Used by Client objects to extract the ISurface from a Surface object. |
| 83 | // This is used because making Client a friend class of Surface would |
| 84 | // require including this header in Surface.h since Client is a nested |
| 85 | // class. |
| 86 | static sp<ISurface> getISurface(const sp<Surface>& surface); |
| 87 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 88 | class Client : public BnCamera |
| 89 | { |
| 90 | public: |
| 91 | // ICamera interface (see ICamera for details) |
| 92 | virtual void disconnect(); |
| 93 | virtual status_t connect(const sp<ICameraClient>& client); |
| 94 | virtual status_t lock(); |
| 95 | virtual status_t unlock(); |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 96 | virtual status_t setPreviewDisplay(const sp<Surface>& surface); |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 97 | virtual void setPreviewCallbackFlag(int flag); |
| 98 | virtual status_t startPreview(); |
| 99 | virtual void stopPreview(); |
| 100 | virtual bool previewEnabled(); |
| 101 | virtual status_t startRecording(); |
| 102 | virtual void stopRecording(); |
| 103 | virtual bool recordingEnabled(); |
| 104 | virtual void releaseRecordingFrame(const sp<IMemory>& mem); |
| 105 | virtual status_t autoFocus(); |
| 106 | virtual status_t cancelAutoFocus(); |
| 107 | virtual status_t takePicture(); |
| 108 | virtual status_t setParameters(const String8& params); |
| 109 | virtual String8 getParameters() const; |
| 110 | virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); |
| 111 | private: |
| 112 | friend class CameraService; |
| 113 | Client(const sp<CameraService>& cameraService, |
| 114 | const sp<ICameraClient>& cameraClient, |
Wu-cheng Li | b7a6794 | 2010-08-17 15:45:37 -0700 | [diff] [blame] | 115 | const sp<CameraHardwareInterface>& hardware, |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 116 | int cameraId, |
| 117 | int clientPid); |
| 118 | ~Client(); |
| 119 | |
| 120 | // return our camera client |
| 121 | const sp<ICameraClient>& getCameraClient() { return mCameraClient; } |
| 122 | |
| 123 | // check whether the calling process matches mClientPid. |
| 124 | status_t checkPid() const; |
| 125 | status_t checkPidAndHardware() const; // also check mHardware != 0 |
| 126 | |
| 127 | // these are internal functions used to set up preview buffers |
| 128 | status_t registerPreviewBuffers(); |
| 129 | status_t setOverlay(); |
| 130 | |
| 131 | // camera operation mode |
| 132 | enum camera_mode { |
| 133 | CAMERA_PREVIEW_MODE = 0, // frame automatically released |
| 134 | CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame() |
| 135 | }; |
| 136 | // these are internal functions used for preview/recording |
| 137 | status_t startCameraMode(camera_mode mode); |
| 138 | status_t startPreviewMode(); |
| 139 | status_t startRecordingMode(); |
| 140 | |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 141 | // internal function used by sendCommand to enable/disable shutter sound. |
| 142 | status_t enableShutterSound(bool enable); |
| 143 | |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 144 | // these are static callback functions |
| 145 | static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user); |
| 146 | static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user); |
| 147 | static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user); |
| 148 | // convert client from cookie |
| 149 | static sp<Client> getClientFromCookie(void* user); |
| 150 | // handlers for messages |
| 151 | void handleShutter(image_rect_type *size); |
| 152 | void handlePreviewData(const sp<IMemory>& mem); |
| 153 | void handlePostview(const sp<IMemory>& mem); |
| 154 | void handleRawPicture(const sp<IMemory>& mem); |
| 155 | void handleCompressedPicture(const sp<IMemory>& mem); |
| 156 | void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2); |
| 157 | void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr); |
| 158 | void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); |
| 159 | |
| 160 | void copyFrameAndPostCopiedFrame( |
| 161 | const sp<ICameraClient>& client, |
| 162 | const sp<IMemoryHeap>& heap, |
| 163 | size_t offset, size_t size); |
| 164 | |
| 165 | // these are initialized in the constructor. |
| 166 | sp<CameraService> mCameraService; // immutable after constructor |
| 167 | sp<ICameraClient> mCameraClient; |
| 168 | int mCameraId; // immutable after constructor |
Wu-cheng Li | 012716a | 2010-10-08 22:04:43 +0800 | [diff] [blame^] | 169 | int mCameraFacing; // immutable after constructor |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 170 | pid_t mClientPid; |
| 171 | sp<CameraHardwareInterface> mHardware; // cleared after disconnect() |
| 172 | bool mUseOverlay; // immutable after constructor |
| 173 | sp<OverlayRef> mOverlayRef; |
| 174 | int mOverlayW; |
| 175 | int mOverlayH; |
| 176 | int mPreviewCallbackFlag; |
Wu-cheng Li | 4a73f3d | 2010-09-23 17:17:43 -0700 | [diff] [blame] | 177 | int mOrientation; // Current display orientation |
| 178 | // True if display orientation has been changed. This is only used in overlay. |
| 179 | int mOrientationChanged; |
Wu-cheng Li | 012716a | 2010-10-08 22:04:43 +0800 | [diff] [blame^] | 180 | int mPreviewWindowFlag; |
Nipun Kwatra | b5ca461 | 2010-09-11 19:31:10 -0700 | [diff] [blame] | 181 | bool mPlayShutterSound; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 182 | |
| 183 | // Ensures atomicity among the public methods |
| 184 | mutable Mutex mLock; |
| 185 | sp<ISurface> mSurface; |
Jamie Gennis | 4b79168 | 2010-08-10 16:37:53 -0700 | [diff] [blame] | 186 | sp<ANativeWindow> mPreviewWindow; |
Mathias Agopian | 65ab471 | 2010-07-14 17:59:35 -0700 | [diff] [blame] | 187 | |
| 188 | // If the user want us to return a copy of the preview frame (instead |
| 189 | // of the original one), we allocate mPreviewBuffer and reuse it if possible. |
| 190 | sp<MemoryHeapBase> mPreviewBuffer; |
| 191 | |
| 192 | // We need to avoid the deadlock when the incoming command thread and |
| 193 | // the CameraHardwareInterface callback thread both want to grab mLock. |
| 194 | // An extra flag is used to tell the callback thread that it should stop |
| 195 | // trying to deliver the callback messages if the client is not |
| 196 | // interested in it anymore. For example, if the client is calling |
| 197 | // stopPreview(), the preview frame messages do not need to be delivered |
| 198 | // anymore. |
| 199 | |
| 200 | // This function takes the same parameter as the enableMsgType() and |
| 201 | // disableMsgType() functions in CameraHardwareInterface. |
| 202 | void enableMsgType(int32_t msgType); |
| 203 | void disableMsgType(int32_t msgType); |
| 204 | volatile int32_t mMsgEnabled; |
| 205 | |
| 206 | // This function keeps trying to grab mLock, or give up if the message |
| 207 | // is found to be disabled. It returns true if mLock is grabbed. |
| 208 | bool lockIfMessageWanted(int32_t msgType); |
| 209 | }; |
| 210 | }; |
| 211 | |
| 212 | } // namespace android |
| 213 | |
| 214 | #endif |