CameraService.h revision 65ab47156e1c7dfcd8cc4266253a5ff30219e7f0
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 22#include <camera/ICameraService.h> 23#include <camera/CameraHardwareInterface.h> 24 25/* This needs to be increased if we can have more cameras */ 26#define MAX_CAMERAS 2 27 28namespace android { 29 30class MemoryHeapBase; 31class MediaPlayer; 32 33class CameraService: public BnCameraService 34{ 35 class Client; 36public: 37 static void instantiate(); 38 39 CameraService(); 40 virtual ~CameraService(); 41 42 virtual int32_t getNumberOfCameras(); 43 virtual status_t getCameraInfo(int cameraId, 44 struct CameraInfo* cameraInfo); 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); 48 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); 52 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(); 62 63private: 64 Mutex mServiceLock; 65 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock 66 int mNumberOfCameras; 67 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); 72 73 // sounds 74 Mutex mSoundLock; 75 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS]; 76 int mSoundRef; // reference count (release all MediaPlayer when 0) 77 78 class Client : public BnCamera 79 { 80 public: 81 // ICamera interface (see ICamera for details) 82 virtual void disconnect(); 83 virtual status_t connect(const sp<ICameraClient>& client); 84 virtual status_t lock(); 85 virtual status_t unlock(); 86 virtual status_t setPreviewDisplay(const sp<ISurface>& surface); 87 virtual void setPreviewCallbackFlag(int flag); 88 virtual status_t startPreview(); 89 virtual void stopPreview(); 90 virtual bool previewEnabled(); 91 virtual status_t startRecording(); 92 virtual void stopRecording(); 93 virtual bool recordingEnabled(); 94 virtual void releaseRecordingFrame(const sp<IMemory>& mem); 95 virtual status_t autoFocus(); 96 virtual status_t cancelAutoFocus(); 97 virtual status_t takePicture(); 98 virtual status_t setParameters(const String8& params); 99 virtual String8 getParameters() const; 100 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 101 private: 102 friend class CameraService; 103 Client(const sp<CameraService>& cameraService, 104 const sp<ICameraClient>& cameraClient, 105 int cameraId, 106 int clientPid); 107 ~Client(); 108 109 // return our camera client 110 const sp<ICameraClient>& getCameraClient() { return mCameraClient; } 111 112 // check whether the calling process matches mClientPid. 113 status_t checkPid() const; 114 status_t checkPidAndHardware() const; // also check mHardware != 0 115 116 // these are internal functions used to set up preview buffers 117 status_t registerPreviewBuffers(); 118 status_t setOverlay(); 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 }; 125 // these are internal functions used for preview/recording 126 status_t startCameraMode(camera_mode mode); 127 status_t startPreviewMode(); 128 status_t startRecordingMode(); 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; 163 164 // Ensures atomicity among the public methods 165 mutable Mutex mLock; 166 sp<ISurface> mSurface; 167 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; 171 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. 179 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; 185 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); 189 }; 190}; 191 192} // namespace android 193 194#endif 195