blob: 5b63399bb5d6ce34cf62963adeb63f7465ba6b9e [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
Mathias Agopian65ab4712010-07-14 17:59:35 -07004**
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 Agopian5462fc92010-07-14 18:41:18 -070021#include <binder/BinderService.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070022#include <camera/ICameraService.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070023#include <hardware/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070024
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;
Iliyan Malchev8951a972011-04-14 16:55:59 -070032class CameraHardwareInterface;
Mathias Agopian65ab4712010-07-14 17:59:35 -070033
Mathias Agopian5462fc92010-07-14 18:41:18 -070034class CameraService :
35 public BinderService<CameraService>,
36 public BnCameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -070037{
38 class Client;
Mathias Agopian5462fc92010-07-14 18:41:18 -070039 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070040public:
Mathias Agopian5462fc92010-07-14 18:41:18 -070041 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070042
43 CameraService();
44 virtual ~CameraService();
45
46 virtual int32_t getNumberOfCameras();
47 virtual status_t getCameraInfo(int cameraId,
48 struct CameraInfo* cameraInfo);
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +080049 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -070050 virtual void removeClient(const sp<ICameraClient>& cameraClient);
Keun young Parkd8973a72012-03-28 14:13:09 -070051 // returns plain pointer of client. Note that mClientLock should be acquired to
52 // prevent the client from destruction. The result can be NULL.
53 virtual Client* getClientByIdUnsafe(int cameraId);
54 virtual Mutex* getClientLockById(int cameraId);
Mathias Agopian65ab4712010-07-14 17:59:35 -070055
56 virtual status_t dump(int fd, const Vector<String16>& args);
57 virtual status_t onTransact(uint32_t code, const Parcel& data,
58 Parcel* reply, uint32_t flags);
Iliyan Malchev8951a972011-04-14 16:55:59 -070059 virtual void onFirstRef();
Mathias Agopian65ab4712010-07-14 17:59:35 -070060
61 enum sound_kind {
62 SOUND_SHUTTER = 0,
63 SOUND_RECORDING = 1,
64 NUM_SOUNDS
65 };
66
67 void loadSound();
68 void playSound(sound_kind kind);
69 void releaseSound();
70
71private:
72 Mutex mServiceLock;
73 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Keun young Parkd8973a72012-03-28 14:13:09 -070074 Mutex mClientLock[MAX_CAMERAS]; // prevent Client destruction inside callbacks
Mathias Agopian65ab4712010-07-14 17:59:35 -070075 int mNumberOfCameras;
76
77 // atomics to record whether the hardware is allocated to some client.
78 volatile int32_t mBusy[MAX_CAMERAS];
79 void setCameraBusy(int cameraId);
80 void setCameraFree(int cameraId);
81
82 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +080083 MediaPlayer* newMediaPlayer(const char *file);
84
Mathias Agopian65ab4712010-07-14 17:59:35 -070085 Mutex mSoundLock;
86 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
87 int mSoundRef; // reference count (release all MediaPlayer when 0)
88
89 class Client : public BnCamera
90 {
91 public:
92 // ICamera interface (see ICamera for details)
93 virtual void disconnect();
94 virtual status_t connect(const sp<ICameraClient>& client);
95 virtual status_t lock();
96 virtual status_t unlock();
Jamie Gennis4b791682010-08-10 16:37:53 -070097 virtual status_t setPreviewDisplay(const sp<Surface>& surface);
Jamie Gennisbfa33aa2010-12-20 11:51:31 -080098 virtual status_t setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture);
Mathias Agopian65ab4712010-07-14 17:59:35 -070099 virtual void setPreviewCallbackFlag(int flag);
100 virtual status_t startPreview();
101 virtual void stopPreview();
102 virtual bool previewEnabled();
James Donge2ad6732010-10-18 20:42:51 -0700103 virtual status_t storeMetaDataInBuffers(bool enabled);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700104 virtual status_t startRecording();
105 virtual void stopRecording();
106 virtual bool recordingEnabled();
107 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
108 virtual status_t autoFocus();
109 virtual status_t cancelAutoFocus();
James Donge468ac52011-02-17 16:38:06 -0800110 virtual status_t takePicture(int msgType);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700111 virtual status_t setParameters(const String8& params);
112 virtual String8 getParameters() const;
113 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
114 private:
115 friend class CameraService;
116 Client(const sp<CameraService>& cameraService,
117 const sp<ICameraClient>& cameraClient,
Wu-cheng Lib7a67942010-08-17 15:45:37 -0700118 const sp<CameraHardwareInterface>& hardware,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700119 int cameraId,
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800120 int cameraFacing,
Wu-cheng Li08ad5ef2012-04-19 12:35:00 +0800121 int clientPid);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700122 ~Client();
123
124 // return our camera client
125 const sp<ICameraClient>& getCameraClient() { return mCameraClient; }
126
127 // check whether the calling process matches mClientPid.
128 status_t checkPid() const;
129 status_t checkPidAndHardware() const; // also check mHardware != 0
130
131 // these are internal functions used to set up preview buffers
132 status_t registerPreviewBuffers();
Mathias Agopian65ab4712010-07-14 17:59:35 -0700133
134 // camera operation mode
135 enum camera_mode {
136 CAMERA_PREVIEW_MODE = 0, // frame automatically released
137 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
138 };
139 // these are internal functions used for preview/recording
140 status_t startCameraMode(camera_mode mode);
141 status_t startPreviewMode();
142 status_t startRecordingMode();
143
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700144 // internal function used by sendCommand to enable/disable shutter sound.
145 status_t enableShutterSound(bool enable);
146
Mathias Agopian65ab4712010-07-14 17:59:35 -0700147 // these are static callback functions
148 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
Wu-cheng Liff09ef82011-07-28 05:30:59 +0800149 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
150 camera_frame_metadata_t *metadata, void* user);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700151 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
Keun young Parkd8973a72012-03-28 14:13:09 -0700152 static Mutex* getClientLockFromCookie(void* user);
153 // convert client from cookie. Client lock should be acquired before getting Client.
154 static Client* getClientFromCookie(void* user);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700155 // handlers for messages
Iliyan Malchev108dddf2011-03-28 16:10:12 -0700156 void handleShutter(void);
Wu-cheng Li57c86182011-07-30 05:00:37 +0800157 void handlePreviewData(int32_t msgType, const sp<IMemory>& mem,
158 camera_frame_metadata_t *metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700159 void handlePostview(const sp<IMemory>& mem);
160 void handleRawPicture(const sp<IMemory>& mem);
161 void handleCompressedPicture(const sp<IMemory>& mem);
162 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
Wu-cheng Li57c86182011-07-30 05:00:37 +0800163 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr,
164 camera_frame_metadata_t *metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700165 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
166
167 void copyFrameAndPostCopiedFrame(
Wu-cheng Li57c86182011-07-30 05:00:37 +0800168 int32_t msgType,
Mathias Agopian65ab4712010-07-14 17:59:35 -0700169 const sp<ICameraClient>& client,
170 const sp<IMemoryHeap>& heap,
Wu-cheng Li57c86182011-07-30 05:00:37 +0800171 size_t offset, size_t size,
172 camera_frame_metadata_t *metadata);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700173
Wu-cheng Lie09591e2010-10-14 20:17:44 +0800174 int getOrientation(int orientation, bool mirror);
175
Jamie Gennis0ed3ec02011-07-13 15:13:14 -0700176 status_t setPreviewWindow(
177 const sp<IBinder>& binder,
178 const sp<ANativeWindow>& window);
179
Mathias Agopian65ab4712010-07-14 17:59:35 -0700180 // these are initialized in the constructor.
181 sp<CameraService> mCameraService; // immutable after constructor
182 sp<ICameraClient> mCameraClient;
183 int mCameraId; // immutable after constructor
Wu-cheng Li012716a2010-10-08 22:04:43 +0800184 int mCameraFacing; // immutable after constructor
Mathias Agopian65ab4712010-07-14 17:59:35 -0700185 pid_t mClientPid;
186 sp<CameraHardwareInterface> mHardware; // cleared after disconnect()
Mathias Agopian65ab4712010-07-14 17:59:35 -0700187 int mPreviewCallbackFlag;
Wu-cheng Li4a73f3d2010-09-23 17:17:43 -0700188 int mOrientation; // Current display orientation
Nipun Kwatrab5ca4612010-09-11 19:31:10 -0700189 bool mPlayShutterSound;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700190
191 // Ensures atomicity among the public methods
192 mutable Mutex mLock;
Jamie Gennisbfa33aa2010-12-20 11:51:31 -0800193 // This is a binder of Surface or SurfaceTexture.
194 sp<IBinder> mSurface;
Jamie Gennis4b791682010-08-10 16:37:53 -0700195 sp<ANativeWindow> mPreviewWindow;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196
197 // If the user want us to return a copy of the preview frame (instead
198 // of the original one), we allocate mPreviewBuffer and reuse it if possible.
199 sp<MemoryHeapBase> mPreviewBuffer;
200
Keun young Parkd8973a72012-03-28 14:13:09 -0700201 // the instance is in the middle of destruction. When this is set,
202 // the instance should not be accessed from callback.
203 // CameraService's mClientLock should be acquired to access this.
204 bool mDestructionStarted;
205
Mathias Agopian65ab4712010-07-14 17:59:35 -0700206 // We need to avoid the deadlock when the incoming command thread and
207 // the CameraHardwareInterface callback thread both want to grab mLock.
208 // An extra flag is used to tell the callback thread that it should stop
209 // trying to deliver the callback messages if the client is not
210 // interested in it anymore. For example, if the client is calling
211 // stopPreview(), the preview frame messages do not need to be delivered
212 // anymore.
213
214 // This function takes the same parameter as the enableMsgType() and
215 // disableMsgType() functions in CameraHardwareInterface.
216 void enableMsgType(int32_t msgType);
217 void disableMsgType(int32_t msgType);
218 volatile int32_t mMsgEnabled;
219
220 // This function keeps trying to grab mLock, or give up if the message
221 // is found to be disabled. It returns true if mLock is grabbed.
222 bool lockIfMessageWanted(int32_t msgType);
223 };
Iliyan Malchev8951a972011-04-14 16:55:59 -0700224
225 camera_module_t *mModule;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700226};
227
228} // namespace android
229
230#endif