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 |
| 4 | ** Copyright (C) 2008 HTC Inc. |
| 5 | ** |
| 6 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | ** you may not use this file except in compliance with the License. |
| 8 | ** You may obtain a copy of the License at |
| 9 | ** |
| 10 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | ** |
| 12 | ** Unless required by applicable law or agreed to in writing, software |
| 13 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | ** See the License for the specific language governing permissions and |
| 16 | ** limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H |
| 20 | #define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H |
| 21 | |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 22 | #include <camera/ICameraService.h> |
| 23 | #include <camera/CameraHardwareInterface.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; |
| 32 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 33 | class CameraService: public BnCameraService |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | { |
| 35 | class Client; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | public: |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 37 | static void instantiate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 39 | CameraService(); |
| 40 | virtual ~CameraService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 42 | virtual int32_t getNumberOfCameras(); |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame^] | 43 | virtual status_t getCameraInfo(int cameraId, |
| 44 | struct CameraInfo* cameraInfo); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 45 | virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId); |
| 46 | virtual void removeClient(const sp<ICameraClient>& cameraClient); |
| 47 | virtual sp<Client> getClientById(int cameraId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 49 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 50 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 51 | Parcel* reply, uint32_t flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 53 | enum sound_kind { |
| 54 | SOUND_SHUTTER = 0, |
| 55 | SOUND_RECORDING = 1, |
| 56 | NUM_SOUNDS |
| 57 | }; |
| 58 | |
| 59 | void loadSound(); |
| 60 | void playSound(sound_kind kind); |
| 61 | void releaseSound(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | |
| 63 | private: |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 64 | Mutex mServiceLock; |
| 65 | wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame^] | 66 | int mNumberOfCameras; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 68 | // atomics to record whether the hardware is allocated to some client. |
| 69 | volatile int32_t mBusy[MAX_CAMERAS]; |
| 70 | void setCameraBusy(int cameraId); |
| 71 | void setCameraFree(int cameraId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 73 | // sounds |
| 74 | Mutex mSoundLock; |
| 75 | sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; |
| 76 | int mSoundRef; // reference count (release all MediaPlayer when 0) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 78 | class Client : public BnCamera |
| 79 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | public: |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 81 | // ICamera interface (see ICamera for details) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | virtual void disconnect(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | virtual status_t connect(const sp<ICameraClient>& client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | virtual status_t lock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | virtual status_t unlock(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | virtual status_t setPreviewDisplay(const sp<ISurface>& surface); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 87 | virtual void setPreviewCallbackFlag(int flag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | virtual status_t startPreview(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | virtual void stopPreview(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | virtual bool previewEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | virtual status_t startRecording(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | virtual void stopRecording(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | virtual bool recordingEnabled(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | virtual void releaseRecordingFrame(const sp<IMemory>& mem); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | virtual status_t autoFocus(); |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 96 | virtual status_t cancelAutoFocus(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | virtual status_t takePicture(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | virtual status_t setParameters(const String8& params); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | virtual String8 getParameters() const; |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 100 | 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] | 101 | private: |
| 102 | friend class CameraService; |
| 103 | Client(const sp<CameraService>& cameraService, |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 104 | const sp<ICameraClient>& cameraClient, |
| 105 | int cameraId, |
| 106 | int clientPid); |
| 107 | ~Client(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 109 | // return our camera client |
| 110 | const sp<ICameraClient>& getCameraClient() { return mCameraClient; } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 112 | // check whether the calling process matches mClientPid. |
| 113 | status_t checkPid() const; |
| 114 | status_t checkPidAndHardware() const; // also check mHardware != 0 |
Benny Wong | da83f46 | 2009-08-12 12:01:27 -0500 | [diff] [blame] | 115 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 116 | // these are internal functions used to set up preview buffers |
| 117 | status_t registerPreviewBuffers(); |
| 118 | status_t setOverlay(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | |
| 120 | // camera operation mode |
| 121 | enum camera_mode { |
| 122 | CAMERA_PREVIEW_MODE = 0, // frame automatically released |
| 123 | CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame() |
| 124 | }; |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 125 | // these are internal functions used for preview/recording |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | status_t startCameraMode(camera_mode mode); |
| 127 | status_t startPreviewMode(); |
| 128 | status_t startRecordingMode(); |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 129 | |
| 130 | // these are static callback functions |
| 131 | static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user); |
| 132 | static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user); |
| 133 | static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user); |
| 134 | // convert client from cookie |
| 135 | static sp<Client> getClientFromCookie(void* user); |
| 136 | // handlers for messages |
| 137 | void handleShutter(image_rect_type *size); |
| 138 | void handlePreviewData(const sp<IMemory>& mem); |
| 139 | void handlePostview(const sp<IMemory>& mem); |
| 140 | void handleRawPicture(const sp<IMemory>& mem); |
| 141 | void handleCompressedPicture(const sp<IMemory>& mem); |
| 142 | void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2); |
| 143 | void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr); |
| 144 | void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); |
| 145 | |
| 146 | void copyFrameAndPostCopiedFrame( |
| 147 | const sp<ICameraClient>& client, |
| 148 | const sp<IMemoryHeap>& heap, |
| 149 | size_t offset, size_t size); |
| 150 | |
| 151 | // these are initialized in the constructor. |
| 152 | sp<CameraService> mCameraService; // immutable after constructor |
| 153 | sp<ICameraClient> mCameraClient; |
| 154 | int mCameraId; // immutable after constructor |
| 155 | pid_t mClientPid; |
| 156 | sp<CameraHardwareInterface> mHardware; // cleared after disconnect() |
| 157 | bool mUseOverlay; // immutable after constructor |
| 158 | sp<OverlayRef> mOverlayRef; |
| 159 | int mOverlayW; |
| 160 | int mOverlayH; |
| 161 | int mPreviewCallbackFlag; |
| 162 | int mOrientation; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | |
| 164 | // Ensures atomicity among the public methods |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 165 | mutable Mutex mLock; |
| 166 | sp<ISurface> mSurface; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 168 | // If the user want us to return a copy of the preview frame (instead |
| 169 | // of the original one), we allocate mPreviewBuffer and reuse it if possible. |
| 170 | sp<MemoryHeapBase> mPreviewBuffer; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 172 | // We need to avoid the deadlock when the incoming command thread and |
| 173 | // the CameraHardwareInterface callback thread both want to grab mLock. |
| 174 | // An extra flag is used to tell the callback thread that it should stop |
| 175 | // trying to deliver the callback messages if the client is not |
| 176 | // interested in it anymore. For example, if the client is calling |
| 177 | // stopPreview(), the preview frame messages do not need to be delivered |
| 178 | // anymore. |
Jason Sams | b18b691 | 2009-03-24 20:21:36 -0700 | [diff] [blame] | 179 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 180 | // This function takes the same parameter as the enableMsgType() and |
| 181 | // disableMsgType() functions in CameraHardwareInterface. |
| 182 | void enableMsgType(int32_t msgType); |
| 183 | void disableMsgType(int32_t msgType); |
| 184 | volatile int32_t mMsgEnabled; |
Benny Wong | 6d2090e | 2009-07-15 18:44:27 -0500 | [diff] [blame] | 185 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 186 | // This function keeps trying to grab mLock, or give up if the message |
| 187 | // is found to be disabled. It returns true if mLock is grabbed. |
| 188 | bool lockIfMessageWanted(int32_t msgType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | }; |
| 191 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 192 | } // namespace android |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | |
| 194 | #endif |