blob: bad41f5a1e783ea29803f9d9bf15737e5c65c1df [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004**
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 Agopian24651682010-07-14 18:41:18 -070021#include <binder/BinderService.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080022#include <camera/ICameraService.h>
Iliyan Malcheva269b872011-04-14 16:55:59 -070023#include <hardware/camera.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080024
25/* This needs to be increased if we can have more cameras */
26#define MAX_CAMERAS 2
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028namespace android {
29
Dave Sparksdd158c92009-10-15 10:02:22 -070030class MemoryHeapBase;
Jason Samsb18b6912009-03-24 20:21:36 -070031class MediaPlayer;
Iliyan Malcheva269b872011-04-14 16:55:59 -070032class CameraHardwareInterface;
Jason Samsb18b6912009-03-24 20:21:36 -070033
Mathias Agopian24651682010-07-14 18:41:18 -070034class CameraService :
35 public BinderService<CameraService>,
36 public BnCameraService
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037{
38 class Client;
Mathias Agopian24651682010-07-14 18:41:18 -070039 friend class BinderService<CameraService>;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040public:
Mathias Agopian24651682010-07-14 18:41:18 -070041 static char const* getServiceName() { return "media.camera"; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Chih-Chung Change25cc652010-05-06 16:36:58 +080043 CameraService();
44 virtual ~CameraService();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
Chih-Chung Change25cc652010-05-06 16:36:58 +080046 virtual int32_t getNumberOfCameras();
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080047 virtual status_t getCameraInfo(int cameraId,
48 struct CameraInfo* cameraInfo);
Chih-Chung Change25cc652010-05-06 16:36:58 +080049 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
Chih-Chung Change25cc652010-05-06 16:36:58 +080053 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);
Iliyan Malcheva269b872011-04-14 16:55:59 -070056 virtual void onFirstRef();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Chih-Chung Change25cc652010-05-06 16:36:58 +080058 enum sound_kind {
59 SOUND_SHUTTER = 0,
60 SOUND_RECORDING = 1,
61 NUM_SOUNDS
62 };
63
64 void loadSound();
65 void playSound(sound_kind kind);
66 void releaseSound();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68private:
Chih-Chung Change25cc652010-05-06 16:36:58 +080069 Mutex mServiceLock;
70 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080071 int mNumberOfCameras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Chih-Chung Change25cc652010-05-06 16:36:58 +080073 // atomics to record whether the hardware is allocated to some client.
74 volatile int32_t mBusy[MAX_CAMERAS];
75 void setCameraBusy(int cameraId);
76 void setCameraFree(int cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
Chih-Chung Change25cc652010-05-06 16:36:58 +080078 // sounds
Chih-Chung Chang4091f0b2011-10-17 19:03:12 +080079 MediaPlayer* newMediaPlayer(const char *file);
80
Chih-Chung Change25cc652010-05-06 16:36:58 +080081 Mutex mSoundLock;
82 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
83 int mSoundRef; // reference count (release all MediaPlayer when 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
Chih-Chung Change25cc652010-05-06 16:36:58 +080085 class Client : public BnCamera
86 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 public:
Chih-Chung Change25cc652010-05-06 16:36:58 +080088 // ICamera interface (see ICamera for details)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 virtual void disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 virtual status_t connect(const sp<ICameraClient>& client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 virtual status_t lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 virtual status_t unlock();
Jamie Gennis85cfdd02010-08-10 16:37:53 -070093 virtual status_t setPreviewDisplay(const sp<Surface>& surface);
Jamie Gennisff2dc462010-12-20 11:51:31 -080094 virtual status_t setPreviewTexture(const sp<ISurfaceTexture>& surfaceTexture);
Chih-Chung Change25cc652010-05-06 16:36:58 +080095 virtual void setPreviewCallbackFlag(int flag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 virtual status_t startPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 virtual void stopPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 virtual bool previewEnabled();
James Dong38311852010-10-18 20:42:51 -070099 virtual status_t storeMetaDataInBuffers(bool enabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 virtual status_t startRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 virtual void stopRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 virtual bool recordingEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 virtual status_t autoFocus();
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800105 virtual status_t cancelAutoFocus();
James Donge00cab72011-02-17 16:38:06 -0800106 virtual status_t takePicture(int msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 virtual status_t setParameters(const String8& params);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 virtual String8 getParameters() const;
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700109 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 private:
111 friend class CameraService;
112 Client(const sp<CameraService>& cameraService,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800113 const sp<ICameraClient>& cameraClient,
Wu-cheng Lie7044382010-08-17 15:45:37 -0700114 const sp<CameraHardwareInterface>& hardware,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800115 int cameraId,
Wu-cheng Lid55f7e52010-10-14 20:17:44 +0800116 int cameraFacing,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800117 int clientPid);
118 ~Client();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
Chih-Chung Change25cc652010-05-06 16:36:58 +0800120 // return our camera client
121 const sp<ICameraClient>& getCameraClient() { return mCameraClient; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
Chih-Chung Change25cc652010-05-06 16:36:58 +0800123 // check whether the calling process matches mClientPid.
124 status_t checkPid() const;
125 status_t checkPidAndHardware() const; // also check mHardware != 0
Benny Wongda83f462009-08-12 12:01:27 -0500126
Chih-Chung Change25cc652010-05-06 16:36:58 +0800127 // these are internal functions used to set up preview buffers
128 status_t registerPreviewBuffers();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129
130 // camera operation mode
131 enum camera_mode {
132 CAMERA_PREVIEW_MODE = 0, // frame automatically released
133 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
134 };
Chih-Chung Change25cc652010-05-06 16:36:58 +0800135 // these are internal functions used for preview/recording
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 status_t startCameraMode(camera_mode mode);
137 status_t startPreviewMode();
138 status_t startRecordingMode();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800139
Nipun Kwatra088146a2010-09-11 19:31:10 -0700140 // internal function used by sendCommand to enable/disable shutter sound.
141 status_t enableShutterSound(bool enable);
142
Chih-Chung Change25cc652010-05-06 16:36:58 +0800143 // these are static callback functions
144 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
Wu-cheng Lif0d6a482011-07-28 05:30:59 +0800145 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr,
146 camera_frame_metadata_t *metadata, void* user);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800147 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
148 // convert client from cookie
149 static sp<Client> getClientFromCookie(void* user);
150 // handlers for messages
Iliyan Malchev40c36412011-03-28 16:10:12 -0700151 void handleShutter(void);
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800152 void handlePreviewData(int32_t msgType, const sp<IMemory>& mem,
153 camera_frame_metadata_t *metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800154 void handlePostview(const sp<IMemory>& mem);
155 void handleRawPicture(const sp<IMemory>& mem);
156 void handleCompressedPicture(const sp<IMemory>& mem);
157 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800158 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr,
159 camera_frame_metadata_t *metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800160 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
161
162 void copyFrameAndPostCopiedFrame(
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800163 int32_t msgType,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800164 const sp<ICameraClient>& client,
165 const sp<IMemoryHeap>& heap,
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800166 size_t offset, size_t size,
167 camera_frame_metadata_t *metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800168
Wu-cheng Lid55f7e52010-10-14 20:17:44 +0800169 int getOrientation(int orientation, bool mirror);
170
Jamie Gennisf1072002011-07-13 15:13:14 -0700171 status_t setPreviewWindow(
172 const sp<IBinder>& binder,
173 const sp<ANativeWindow>& window);
174
Chih-Chung Change25cc652010-05-06 16:36:58 +0800175 // these are initialized in the constructor.
176 sp<CameraService> mCameraService; // immutable after constructor
177 sp<ICameraClient> mCameraClient;
178 int mCameraId; // immutable after constructor
Wu-cheng Lib0f7d952010-10-08 22:04:43 +0800179 int mCameraFacing; // immutable after constructor
Chih-Chung Change25cc652010-05-06 16:36:58 +0800180 pid_t mClientPid;
181 sp<CameraHardwareInterface> mHardware; // cleared after disconnect()
Chih-Chung Change25cc652010-05-06 16:36:58 +0800182 int mPreviewCallbackFlag;
Wu-cheng Lib3347bc2010-09-23 17:17:43 -0700183 int mOrientation; // Current display orientation
Nipun Kwatra088146a2010-09-11 19:31:10 -0700184 bool mPlayShutterSound;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
186 // Ensures atomicity among the public methods
Chih-Chung Change25cc652010-05-06 16:36:58 +0800187 mutable Mutex mLock;
Jamie Gennisff2dc462010-12-20 11:51:31 -0800188 // This is a binder of Surface or SurfaceTexture.
189 sp<IBinder> mSurface;
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700190 sp<ANativeWindow> mPreviewWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191
Chih-Chung Change25cc652010-05-06 16:36:58 +0800192 // If the user want us to return a copy of the preview frame (instead
193 // of the original one), we allocate mPreviewBuffer and reuse it if possible.
194 sp<MemoryHeapBase> mPreviewBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
Chih-Chung Change25cc652010-05-06 16:36:58 +0800196 // We need to avoid the deadlock when the incoming command thread and
197 // the CameraHardwareInterface callback thread both want to grab mLock.
198 // An extra flag is used to tell the callback thread that it should stop
199 // trying to deliver the callback messages if the client is not
200 // interested in it anymore. For example, if the client is calling
201 // stopPreview(), the preview frame messages do not need to be delivered
202 // anymore.
Jason Samsb18b6912009-03-24 20:21:36 -0700203
Chih-Chung Change25cc652010-05-06 16:36:58 +0800204 // This function takes the same parameter as the enableMsgType() and
205 // disableMsgType() functions in CameraHardwareInterface.
206 void enableMsgType(int32_t msgType);
207 void disableMsgType(int32_t msgType);
208 volatile int32_t mMsgEnabled;
Benny Wong6d2090e2009-07-15 18:44:27 -0500209
Chih-Chung Change25cc652010-05-06 16:36:58 +0800210 // This function keeps trying to grab mLock, or give up if the message
211 // is found to be disabled. It returns true if mLock is grabbed.
212 bool lockIfMessageWanted(int32_t msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 };
Iliyan Malcheva269b872011-04-14 16:55:59 -0700214
215 camera_module_t *mModule;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216};
217
Chih-Chung Change25cc652010-05-06 16:36:58 +0800218} // namespace android
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219
220#endif