blob: 86986cab82e3daa8d0ca972d6852c7f1dd7c44a5 [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();
43 virtual sp<ICamera> connect(const sp<ICameraClient>& cameraClient, int cameraId);
44 virtual void removeClient(const sp<ICameraClient>& cameraClient);
45 virtual sp<Client> getClientById(int cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Chih-Chung Change25cc652010-05-06 16:36:58 +080047 virtual status_t dump(int fd, const Vector<String16>& args);
48 virtual status_t onTransact(uint32_t code, const Parcel& data,
49 Parcel* reply, uint32_t flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Chih-Chung Change25cc652010-05-06 16:36:58 +080051 enum sound_kind {
52 SOUND_SHUTTER = 0,
53 SOUND_RECORDING = 1,
54 NUM_SOUNDS
55 };
56
57 void loadSound();
58 void playSound(sound_kind kind);
59 void releaseSound();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61private:
Chih-Chung Change25cc652010-05-06 16:36:58 +080062 Mutex mServiceLock;
63 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Chih-Chung Change25cc652010-05-06 16:36:58 +080065 // atomics to record whether the hardware is allocated to some client.
66 volatile int32_t mBusy[MAX_CAMERAS];
67 void setCameraBusy(int cameraId);
68 void setCameraFree(int cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Chih-Chung Change25cc652010-05-06 16:36:58 +080070 // sounds
71 Mutex mSoundLock;
72 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
73 int mSoundRef; // reference count (release all MediaPlayer when 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
Chih-Chung Change25cc652010-05-06 16:36:58 +080075 class Client : public BnCamera
76 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 public:
Chih-Chung Change25cc652010-05-06 16:36:58 +080078 // ICamera interface (see ICamera for details)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 virtual void disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 virtual status_t connect(const sp<ICameraClient>& client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 virtual status_t lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 virtual status_t unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 virtual status_t setPreviewDisplay(const sp<ISurface>& surface);
Chih-Chung Change25cc652010-05-06 16:36:58 +080084 virtual void setPreviewCallbackFlag(int flag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 virtual status_t startPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 virtual void stopPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 virtual bool previewEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 virtual status_t startRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 virtual void stopRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 virtual bool recordingEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 virtual status_t autoFocus();
Chih-Chung Chang244f8c22009-09-15 14:51:56 +080093 virtual status_t cancelAutoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 virtual status_t takePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 virtual status_t setParameters(const String8& params);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 virtual String8 getParameters() const;
Wu-cheng Li36f68b82009-09-28 16:14:58 -070097 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 private:
99 friend class CameraService;
100 Client(const sp<CameraService>& cameraService,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800101 const sp<ICameraClient>& cameraClient,
102 int cameraId,
103 int clientPid);
104 ~Client();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
Chih-Chung Change25cc652010-05-06 16:36:58 +0800106 // return our camera client
107 const sp<ICameraClient>& getCameraClient() { return mCameraClient; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
Chih-Chung Change25cc652010-05-06 16:36:58 +0800109 // check whether the calling process matches mClientPid.
110 status_t checkPid() const;
111 status_t checkPidAndHardware() const; // also check mHardware != 0
Benny Wongda83f462009-08-12 12:01:27 -0500112
Chih-Chung Change25cc652010-05-06 16:36:58 +0800113 // these are internal functions used to set up preview buffers
114 status_t registerPreviewBuffers();
115 status_t setOverlay();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
117 // camera operation mode
118 enum camera_mode {
119 CAMERA_PREVIEW_MODE = 0, // frame automatically released
120 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
121 };
Chih-Chung Change25cc652010-05-06 16:36:58 +0800122 // these are internal functions used for preview/recording
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 status_t startCameraMode(camera_mode mode);
124 status_t startPreviewMode();
125 status_t startRecordingMode();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800126
127 // these are static callback functions
128 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
129 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user);
130 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
131 // convert client from cookie
132 static sp<Client> getClientFromCookie(void* user);
133 // handlers for messages
134 void handleShutter(image_rect_type *size);
135 void handlePreviewData(const sp<IMemory>& mem);
136 void handlePostview(const sp<IMemory>& mem);
137 void handleRawPicture(const sp<IMemory>& mem);
138 void handleCompressedPicture(const sp<IMemory>& mem);
139 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
140 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr);
141 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
142
143 void copyFrameAndPostCopiedFrame(
144 const sp<ICameraClient>& client,
145 const sp<IMemoryHeap>& heap,
146 size_t offset, size_t size);
147
148 // these are initialized in the constructor.
149 sp<CameraService> mCameraService; // immutable after constructor
150 sp<ICameraClient> mCameraClient;
151 int mCameraId; // immutable after constructor
152 pid_t mClientPid;
153 sp<CameraHardwareInterface> mHardware; // cleared after disconnect()
154 bool mUseOverlay; // immutable after constructor
155 sp<OverlayRef> mOverlayRef;
156 int mOverlayW;
157 int mOverlayH;
158 int mPreviewCallbackFlag;
159 int mOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
161 // Ensures atomicity among the public methods
Chih-Chung Change25cc652010-05-06 16:36:58 +0800162 mutable Mutex mLock;
163 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
Chih-Chung Change25cc652010-05-06 16:36:58 +0800165 // If the user want us to return a copy of the preview frame (instead
166 // of the original one), we allocate mPreviewBuffer and reuse it if possible.
167 sp<MemoryHeapBase> mPreviewBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
Chih-Chung Change25cc652010-05-06 16:36:58 +0800169 // We need to avoid the deadlock when the incoming command thread and
170 // the CameraHardwareInterface callback thread both want to grab mLock.
171 // An extra flag is used to tell the callback thread that it should stop
172 // trying to deliver the callback messages if the client is not
173 // interested in it anymore. For example, if the client is calling
174 // stopPreview(), the preview frame messages do not need to be delivered
175 // anymore.
Jason Samsb18b6912009-03-24 20:21:36 -0700176
Chih-Chung Change25cc652010-05-06 16:36:58 +0800177 // This function takes the same parameter as the enableMsgType() and
178 // disableMsgType() functions in CameraHardwareInterface.
179 void enableMsgType(int32_t msgType);
180 void disableMsgType(int32_t msgType);
181 volatile int32_t mMsgEnabled;
Benny Wong6d2090e2009-07-15 18:44:27 -0500182
Chih-Chung Change25cc652010-05-06 16:36:58 +0800183 // This function keeps trying to grab mLock, or give up if the message
184 // is found to be disabled. It returns true if mLock is grabbed.
185 bool lockIfMessageWanted(int32_t msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187};
188
Chih-Chung Change25cc652010-05-06 16:36:58 +0800189} // namespace android
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
191#endif