blob: 0d6983633747d1c4fd495dd1aaaff33217abe40a [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>
22
Mathias Agopian000479f2010-02-09 17:46:37 -080023#include <camera/ICameraService.h>
24#include <camera/CameraHardwareInterface.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080025
26/* This needs to be increased if we can have more cameras */
27#define MAX_CAMERAS 2
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029namespace android {
30
Dave Sparksdd158c92009-10-15 10:02:22 -070031class MemoryHeapBase;
Jason Samsb18b6912009-03-24 20:21:36 -070032class MediaPlayer;
33
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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
Chih-Chung Change25cc652010-05-06 16:36:58 +080057 enum sound_kind {
58 SOUND_SHUTTER = 0,
59 SOUND_RECORDING = 1,
60 NUM_SOUNDS
61 };
62
63 void loadSound();
64 void playSound(sound_kind kind);
65 void releaseSound();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67private:
Chih-Chung Change25cc652010-05-06 16:36:58 +080068 Mutex mServiceLock;
69 wp<Client> mClient[MAX_CAMERAS]; // protected by mServiceLock
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +080070 int mNumberOfCameras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
Chih-Chung Change25cc652010-05-06 16:36:58 +080072 // atomics to record whether the hardware is allocated to some client.
73 volatile int32_t mBusy[MAX_CAMERAS];
74 void setCameraBusy(int cameraId);
75 void setCameraFree(int cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076
Chih-Chung Change25cc652010-05-06 16:36:58 +080077 // sounds
78 Mutex mSoundLock;
79 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
80 int mSoundRef; // reference count (release all MediaPlayer when 0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
Chih-Chung Change25cc652010-05-06 16:36:58 +080082 class Client : public BnCamera
83 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 public:
Chih-Chung Change25cc652010-05-06 16:36:58 +080085 // ICamera interface (see ICamera for details)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 virtual void disconnect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 virtual status_t connect(const sp<ICameraClient>& client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 virtual status_t lock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 virtual status_t unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 virtual status_t setPreviewDisplay(const sp<ISurface>& surface);
Chih-Chung Change25cc652010-05-06 16:36:58 +080091 virtual void setPreviewCallbackFlag(int flag);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 virtual status_t startPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 virtual void stopPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 virtual bool previewEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 virtual status_t startRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 virtual void stopRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 virtual bool recordingEnabled();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 virtual status_t autoFocus();
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800100 virtual status_t cancelAutoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 virtual status_t takePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 virtual status_t setParameters(const String8& params);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 virtual String8 getParameters() const;
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700104 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 private:
106 friend class CameraService;
107 Client(const sp<CameraService>& cameraService,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800108 const sp<ICameraClient>& cameraClient,
Wu-cheng Lie7044382010-08-17 15:45:37 -0700109 const sp<CameraHardwareInterface>& hardware,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800110 int cameraId,
111 int clientPid);
112 ~Client();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
Chih-Chung Change25cc652010-05-06 16:36:58 +0800114 // return our camera client
115 const sp<ICameraClient>& getCameraClient() { return mCameraClient; }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
Chih-Chung Change25cc652010-05-06 16:36:58 +0800117 // check whether the calling process matches mClientPid.
118 status_t checkPid() const;
119 status_t checkPidAndHardware() const; // also check mHardware != 0
Benny Wongda83f462009-08-12 12:01:27 -0500120
Chih-Chung Change25cc652010-05-06 16:36:58 +0800121 // these are internal functions used to set up preview buffers
122 status_t registerPreviewBuffers();
123 status_t setOverlay();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
125 // camera operation mode
126 enum camera_mode {
127 CAMERA_PREVIEW_MODE = 0, // frame automatically released
128 CAMERA_RECORDING_MODE = 1, // frame has to be explicitly released by releaseRecordingFrame()
129 };
Chih-Chung Change25cc652010-05-06 16:36:58 +0800130 // these are internal functions used for preview/recording
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 status_t startCameraMode(camera_mode mode);
132 status_t startPreviewMode();
133 status_t startRecordingMode();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800134
135 // these are static callback functions
136 static void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2, void* user);
137 static void dataCallback(int32_t msgType, const sp<IMemory>& dataPtr, void* user);
138 static void dataCallbackTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr, void* user);
139 // convert client from cookie
140 static sp<Client> getClientFromCookie(void* user);
141 // handlers for messages
142 void handleShutter(image_rect_type *size);
143 void handlePreviewData(const sp<IMemory>& mem);
144 void handlePostview(const sp<IMemory>& mem);
145 void handleRawPicture(const sp<IMemory>& mem);
146 void handleCompressedPicture(const sp<IMemory>& mem);
147 void handleGenericNotify(int32_t msgType, int32_t ext1, int32_t ext2);
148 void handleGenericData(int32_t msgType, const sp<IMemory>& dataPtr);
149 void handleGenericDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr);
150
151 void copyFrameAndPostCopiedFrame(
152 const sp<ICameraClient>& client,
153 const sp<IMemoryHeap>& heap,
154 size_t offset, size_t size);
155
156 // these are initialized in the constructor.
157 sp<CameraService> mCameraService; // immutable after constructor
158 sp<ICameraClient> mCameraClient;
159 int mCameraId; // immutable after constructor
160 pid_t mClientPid;
161 sp<CameraHardwareInterface> mHardware; // cleared after disconnect()
162 bool mUseOverlay; // immutable after constructor
163 sp<OverlayRef> mOverlayRef;
164 int mOverlayW;
165 int mOverlayH;
166 int mPreviewCallbackFlag;
167 int mOrientation;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
169 // Ensures atomicity among the public methods
Chih-Chung Change25cc652010-05-06 16:36:58 +0800170 mutable Mutex mLock;
171 sp<ISurface> mSurface;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172
Chih-Chung Change25cc652010-05-06 16:36:58 +0800173 // If the user want us to return a copy of the preview frame (instead
174 // of the original one), we allocate mPreviewBuffer and reuse it if possible.
175 sp<MemoryHeapBase> mPreviewBuffer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
Chih-Chung Change25cc652010-05-06 16:36:58 +0800177 // We need to avoid the deadlock when the incoming command thread and
178 // the CameraHardwareInterface callback thread both want to grab mLock.
179 // An extra flag is used to tell the callback thread that it should stop
180 // trying to deliver the callback messages if the client is not
181 // interested in it anymore. For example, if the client is calling
182 // stopPreview(), the preview frame messages do not need to be delivered
183 // anymore.
Jason Samsb18b6912009-03-24 20:21:36 -0700184
Chih-Chung Change25cc652010-05-06 16:36:58 +0800185 // This function takes the same parameter as the enableMsgType() and
186 // disableMsgType() functions in CameraHardwareInterface.
187 void enableMsgType(int32_t msgType);
188 void disableMsgType(int32_t msgType);
189 volatile int32_t mMsgEnabled;
Benny Wong6d2090e2009-07-15 18:44:27 -0500190
Chih-Chung Change25cc652010-05-06 16:36:58 +0800191 // This function keeps trying to grab mLock, or give up if the message
192 // is found to be disabled. It returns true if mLock is grabbed.
193 bool lockIfMessageWanted(int32_t msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195};
196
Chih-Chung Change25cc652010-05-06 16:36:58 +0800197} // namespace android
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
199#endif