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