CameraService.h revision 8b1027d3f873fc15c70f8645f1856936b69241a4
1/* 2** 3** Copyright (C) 2008, The Android Open Source Project 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 21#include <binder/BinderService.h> 22 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 29namespace android { 30 31class MemoryHeapBase; 32class MediaPlayer; 33 34class CameraService : 35 public BinderService<CameraService>, 36 public BnCameraService 37{ 38 class Client; 39 friend class BinderService<CameraService>; 40public: 41 static char const* getServiceName() { return "media.camera"; } 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 67private: 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 82 class Client : public BnCamera 83 { 84 public: 85 // ICamera interface (see ICamera for details) 86 virtual void disconnect(); 87 virtual status_t connect(const sp<ICameraClient>& client); 88 virtual status_t lock(); 89 virtual status_t unlock(); 90 virtual status_t setPreviewDisplay(const sp<Surface>& surface); 91 virtual status_t setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture); 92 virtual void setPreviewCallbackFlag(int flag); 93 virtual status_t startPreview(); 94 virtual void stopPreview(); 95 virtual bool previewEnabled(); 96 virtual status_t storeMetaDataInBuffers(bool enabled); 97 virtual status_t startRecording(); 98 virtual void stopRecording(); 99 virtual bool recordingEnabled(); 100 virtual void releaseRecordingFrame(const sp<IMemory>& mem); 101 virtual status_t autoFocus(); 102 virtual status_t cancelAutoFocus(); 103 virtual status_t takePicture(int msgType); 104 virtual status_t setParameters(const String8& params); 105 virtual String8 getParameters() const; 106 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 107 private: 108 friend class CameraService; 109 Client(const sp<CameraService>& cameraService, 110 const sp<ICameraClient>& cameraClient, 111 const sp<CameraHardwareInterface>& hardware, 112 int cameraId, 113 int cameraFacing, 114 int clientPid); 115 ~Client(); 116 117 // return our camera client 118 const sp<ICameraClient>& getCameraClient() { return mCameraClient; } 119 120 // check whether the calling process matches mClientPid. 121 status_t checkPid() const; 122 status_t checkPidAndHardware() const; // also check mHardware != 0 123 124 // these are internal functions used to set up preview buffers 125 status_t registerPreviewBuffers(); 126 127 // camera operation mode 128 enum camera_mode { 129 CAMERA_PREVIEW_MODE = 0, // frame automatically released 130 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame() 131 }; 132 // these are internal functions used for preview/recording 133 status_t startCameraMode(camera_mode mode); 134 status_t startPreviewMode(); 135 status_t startRecordingMode(); 136 137 // internal function used by sendCommand to enable/disable shutter sound. 138 status_t enableShutterSound(bool enable); 139 140 // these are static callback functions 141 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user); 142 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user); 143 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user); 144 // convert client from cookie 145 static sp<Client> getClientFromCookie(void* user); 146 // handlers for messages 147 void handleShutter(void); 148 void handlePreviewData(const sp<IMemory>& mem); 149 void handlePostview(const sp<IMemory>& mem); 150 void handleRawPicture(const sp<IMemory>& mem); 151 void handleCompressedPicture(const sp<IMemory>& mem); 152 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2); 153 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr); 154 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); 155 156 void copyFrameAndPostCopiedFrame( 157 const sp<ICameraClient>& client, 158 const sp<IMemoryHeap>& heap, 159 size_t offset, size_t size); 160 161 int getOrientation(int orientation, bool mirror); 162 163 // these are initialized in the constructor. 164 sp<CameraService> mCameraService; // immutable after constructor 165 sp<ICameraClient> mCameraClient; 166 int mCameraId; // immutable after constructor 167 int mCameraFacing; // immutable after constructor 168 pid_t mClientPid; 169 sp<CameraHardwareInterface> mHardware; // cleared after disconnect() 170 int mPreviewCallbackFlag; 171 int mOrientation; // Current display orientation 172 bool mPlayShutterSound; 173 174 // Ensures atomicity among the public methods 175 mutable Mutex mLock; 176 // This is a binder of Surface or SurfaceTexture. 177 sp<IBinder> mSurface; 178 sp<ANativeWindow> mPreviewWindow; 179 180 // If the user want us to return a copy of the preview frame (instead 181 // of the original one), we allocate mPreviewBuffer and reuse it if possible. 182 sp<MemoryHeapBase> mPreviewBuffer; 183 184 // We need to avoid the deadlock when the incoming command thread and 185 // the CameraHardwareInterface callback thread both want to grab mLock. 186 // An extra flag is used to tell the callback thread that it should stop 187 // trying to deliver the callback messages if the client is not 188 // interested in it anymore. For example, if the client is calling 189 // stopPreview(), the preview frame messages do not need to be delivered 190 // anymore. 191 192 // This function takes the same parameter as the enableMsgType() and 193 // disableMsgType() functions in CameraHardwareInterface. 194 void enableMsgType(int32_t msgType); 195 void disableMsgType(int32_t msgType); 196 volatile int32_t mMsgEnabled; 197 198 // This function keeps trying to grab mLock, or give up if the message 199 // is found to be disabled. It returns true if mLock is grabbed. 200 bool lockIfMessageWanted(int32_t msgType); 201 }; 202}; 203 204} // namespace android 205 206#endif 207