blob: 8193e77be85d78b50b9f8017e9efd0558c079d77 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Agopian000479f2010-02-09 17:46:37 -080022#include <camera/ICameraService.h>
23#include <camera/CameraHardwareInterface.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;
32
Chih-Chung Change25cc652010-05-06 16:36:58 +080033class CameraService: public BnCameraService
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034{
35 class Client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036public:
Chih-Chung Change25cc652010-05-06 16:36:58 +080037 static void instantiate();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
Chih-Chung Change25cc652010-05-06 16:36:58 +080039 CameraService();
40 virtual ~CameraService();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Chih-Chung Change25cc652010-05-06 16:36:58 +080042 virtual int32_t getNumberOfCameras();
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080043 virtual status_t getCameraInfo(int cameraId,
44 struct CameraInfo* cameraInfo);
Chih-Chung Change25cc652010-05-06 16:36:58 +080045 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Chih-Chung Change25cc652010-05-06 16:36:58 +080049 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
Chih-Chung Change25cc652010-05-06 16:36:58 +080053 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();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63private:
Chih-Chung Change25cc652010-05-06 16:36:58 +080064 Mutex mServiceLock;
65 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080066 int mNumberOfCameras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Chih-Chung Change25cc652010-05-06 16:36:58 +080068 // 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Chih-Chung Change25cc652010-05-06 16:36:58 +080073 // sounds
74 Mutex mSoundLock;
75 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
76 int mSoundRef; // reference count (release all MediaPlayer when 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
Chih-Chung Change25cc652010-05-06 16:36:58 +080078 class Client : public BnCamera
79 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 public:
Chih-Chung Change25cc652010-05-06 16:36:58 +080081 // ICamera interface (see ICamera for details)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 virtual void disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 virtual status_t connect(const sp<ICameraClient>& client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 virtual status_t lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 virtual status_t unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 virtual status_t setPreviewDisplay(const sp<ISurface>& surface);
Chih-Chung Change25cc652010-05-06 16:36:58 +080087 virtual void setPreviewCallbackFlag(int flag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 virtual status_t startPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 virtual void stopPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 virtual bool previewEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 virtual status_t startRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 virtual void stopRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 virtual bool recordingEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 virtual status_t autoFocus();
Chih-Chung Chang244f8c22009-09-15 14:51:56 +080096 virtual status_t cancelAutoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 virtual status_t takePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 virtual status_t setParameters(const String8& params);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 virtual String8 getParameters() const;
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700100 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private:
102 friend class CameraService;
103 Client(const sp<CameraService>& cameraService,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800104 const sp<ICameraClient>& cameraClient,
105 int cameraId,
106 int clientPid);
107 ~Client();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
Chih-Chung Change25cc652010-05-06 16:36:58 +0800109 // return our camera client
110 const sp<ICameraClient>& getCameraClient() { return mCameraClient; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
Chih-Chung Change25cc652010-05-06 16:36:58 +0800112 // check whether the calling process matches mClientPid.
113 status_t checkPid() const;
114 status_t checkPidAndHardware() const; // also check mHardware != 0
Benny Wongda83f462009-08-12 12:01:27 -0500115
Chih-Chung Change25cc652010-05-06 16:36:58 +0800116 // these are internal functions used to set up preview buffers
117 status_t registerPreviewBuffers();
118 status_t setOverlay();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
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 };
Chih-Chung Change25cc652010-05-06 16:36:58 +0800125 // these are internal functions used for preview/recording
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 status_t startCameraMode(camera_mode mode);
127 status_t startPreviewMode();
128 status_t startRecordingMode();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800129
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163
164 // Ensures atomicity among the public methods
Chih-Chung Change25cc652010-05-06 16:36:58 +0800165 mutable Mutex mLock;
166 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
Chih-Chung Change25cc652010-05-06 16:36:58 +0800168 // 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
Chih-Chung Change25cc652010-05-06 16:36:58 +0800172 // 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.
Jason Samsb18b6912009-03-24 20:21:36 -0700179
Chih-Chung Change25cc652010-05-06 16:36:58 +0800180 // 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;
Benny Wong6d2090e2009-07-15 18:44:27 -0500185
Chih-Chung Change25cc652010-05-06 16:36:58 +0800186 // 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190};
191
Chih-Chung Change25cc652010-05-06 16:36:58 +0800192} // namespace android
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193
194#endif