blob: 8d410f1a95a1a10497a93be1e1a0dfcc4d6aa511 [file] [log] [blame]
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H
19
20#include "Camera2Device.h"
21#include "CameraService.h"
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070022#include "camera/CameraParameters.h"
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070023#include <binder/MemoryBase.h>
24#include <binder/MemoryHeapBase.h>
25#include <gui/CpuConsumer.h>
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070026
27namespace android {
28
29/**
30 * Implements the android.hardware.camera API on top of
31 * camera device HAL version 2.
32 */
33class Camera2Client : public CameraService::Client
34{
35public:
36 // ICamera interface (see ICamera for details)
37 virtual void disconnect();
38 virtual status_t connect(const sp<ICameraClient>& client);
39 virtual status_t lock();
40 virtual status_t unlock();
41 virtual status_t setPreviewDisplay(const sp<Surface>& surface);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070042 virtual status_t setPreviewTexture(
43 const sp<ISurfaceTexture>& surfaceTexture);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070044 virtual void setPreviewCallbackFlag(int flag);
45 virtual status_t startPreview();
46 virtual void stopPreview();
47 virtual bool previewEnabled();
48 virtual status_t storeMetaDataInBuffers(bool enabled);
49 virtual status_t startRecording();
50 virtual void stopRecording();
51 virtual bool recordingEnabled();
52 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
53 virtual status_t autoFocus();
54 virtual status_t cancelAutoFocus();
55 virtual status_t takePicture(int msgType);
56 virtual status_t setParameters(const String8& params);
57 virtual String8 getParameters() const;
58 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
59
60 // Interface used by CameraService
61 Camera2Client(const sp<CameraService>& cameraService,
62 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070063 int cameraId,
64 int cameraFacing,
65 int clientPid);
66 ~Camera2Client();
67
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070068 status_t initialize(camera_module_t *module);
69
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070070 virtual status_t dump(int fd, const Vector<String16>& args);
71
72private:
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -070073 enum State {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070074 NOT_INITIALIZED,
75 STOPPED,
76 WAITING_FOR_PREVIEW_WINDOW,
Eino-Ville Talvala7f610842012-06-07 10:20:51 -070077 PREVIEW,
78 RECORD,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070079 STILL_CAPTURE,
80 VIDEO_SNAPSHOT
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070081 } mState;
82
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -070083 static const char *getStateName(State state);
84
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -070085 /** ICamera interface-related private members */
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070086
Eino-Ville Talvalaac45eb32012-06-07 10:24:51 -070087 // Mutex that must be locked by methods implementing the ICamera interface.
88 // Ensures serialization between incoming ICamera calls
89 mutable Mutex mICameraLock;
90
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070091 // The following must be called with mICamaeraLock already locked
Eino-Ville Talvalaac45eb32012-06-07 10:24:51 -070092
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070093 status_t setPreviewWindowLocked(const sp<IBinder>& binder,
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -070094 sp<ANativeWindow> window);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070095
96 void stopPreviewLocked();
97 status_t startPreviewLocked();
98
99 // Mutex that must be locked before accessing mParameters, mParamsFlattened
Eino-Ville Talvalaac45eb32012-06-07 10:24:51 -0700100 mutable Mutex mParamsLock;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700101 String8 mParamsFlattened;
102 // Current camera state; this is the contents of the CameraParameters object
103 // in a more-efficient format. The enum values are mostly based off the
104 // corresponding camera2 enums, not the camera1 strings. A few are defined
105 // here if they don't cleanly map to camera2 values.
106 struct Parameters {
107 int previewWidth, previewHeight;
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700108 int32_t previewFpsRange[2];
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700109 int previewFps; // deprecated, here only for tracking changes
110 int previewFormat;
111
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700112 int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION
113
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700114 int pictureWidth, pictureHeight;
115
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700116 int32_t jpegThumbSize[2];
117 int32_t jpegQuality, jpegThumbQuality;
118 int32_t jpegRotation;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700119
120 bool gpsEnabled;
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700121 double gpsCoordinates[3];
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700122 int64_t gpsTimestamp;
123 String8 gpsProcessingMethod;
124
125 int wbMode;
126 int effectMode;
127 int antibandingMode;
128 int sceneMode;
129
130 enum flashMode_t {
131 FLASH_MODE_OFF = 0,
132 FLASH_MODE_AUTO,
133 FLASH_MODE_ON,
134 FLASH_MODE_TORCH,
135 FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE,
136 FLASH_MODE_INVALID = -1
137 } flashMode;
138
139 enum focusMode_t {
140 FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO,
141 FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO,
142 FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO,
143 FOCUS_MODE_CONTINUOUS_PICTURE =
144 ANDROID_CONTROL_AF_CONTINUOUS_PICTURE,
145 FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF,
146 FOCUS_MODE_INFINITY,
147 FOCUS_MODE_FIXED,
148 FOCUS_MODE_INVALID = -1
149 } focusMode;
150
151 struct Area {
152 int left, top, right, bottom;
153 int weight;
154 Area() {}
155 Area(int left, int top, int right, int bottom, int weight):
156 left(left), top(top), right(right), bottom(bottom),
157 weight(weight) {}
158 };
159 Vector<Area> focusingAreas;
160
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700161 int32_t exposureCompensation;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700162 bool autoExposureLock;
163 bool autoWhiteBalanceLock;
164
165 Vector<Area> meteringAreas;
166
167 int zoom;
168
169 int videoWidth, videoHeight;
170
171 bool recordingHint;
172 bool videoStabilization;
173 } mParameters;
174
175 /** Camera device-related private members */
176
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700177 class Camera2Heap;
178
179 // Number of zoom steps to simulate
180 static const unsigned int NUM_ZOOM_STEPS = 10;
181 // Used with stream IDs
182 static const int NO_STREAM = -1;
183
184 /* Preview related members */
185
186 int mPreviewStreamId;
187 camera_metadata_t *mPreviewRequest;
188 sp<IBinder> mPreviewSurface;
189 sp<ANativeWindow> mPreviewWindow;
190 // Update preview request based on mParameters
191 status_t updatePreviewRequest();
192 // Update preview stream based on mParameters
193 status_t updatePreviewStream();
194
195 /* Still image capture related members */
196
197 int mCaptureStreamId;
198 sp<CpuConsumer> mCaptureConsumer;
199 sp<ANativeWindow> mCaptureWindow;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700200 // Simple listener that forwards frame available notifications from
201 // a CPU consumer to the capture notification
202 class CaptureWaiter: public CpuConsumer::FrameAvailableListener {
203 public:
204 CaptureWaiter(Camera2Client *parent) : mParent(parent) {}
205 void onFrameAvailable() { mParent->onCaptureAvailable(); }
206 private:
207 Camera2Client *mParent;
208 };
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700209 sp<CaptureWaiter> mCaptureWaiter;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700210 camera_metadata_t *mCaptureRequest;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700211 sp<Camera2Heap> mCaptureHeap;
212 // Handle captured image buffers
213 void onCaptureAvailable();
214 // Update capture request based on mParameters
215 status_t updateCaptureRequest();
216 // Update capture stream based on mParameters
217 status_t updateCaptureStream();
218
219 /* Recording related members */
220
221 int mRecordingStreamId;
222 sp<CpuConsumer> mRecordingConsumer;
223 sp<ANativeWindow> mRecordingWindow;
224 // Simple listener that forwards frame available notifications from
225 // a CPU consumer to the recording notification
226 class RecordingWaiter: public CpuConsumer::FrameAvailableListener {
227 public:
228 RecordingWaiter(Camera2Client *parent) : mParent(parent) {}
229 void onFrameAvailable() { mParent->onRecordingFrameAvailable(); }
230 private:
231 Camera2Client *mParent;
232 };
233 sp<RecordingWaiter> mRecordingWaiter;
234 camera_metadata_t *mRecordingRequest;
235 sp<Camera2Heap> mRecordingHeap;
236
237 // TODO: This needs to be queried from somewhere, or the BufferQueue needs
238 // to be passed all the way to stagefright
239 static const size_t kRecordingHeapCount = 4;
240 static const uint32_t kRecordingFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP;
241 size_t mRecordingHeapHead, mRecordingHeapFree;
242 // Handle new recording image buffers
243 void onRecordingFrameAvailable();
244 // Update recording request based on mParameters
245 status_t updateRecordingRequest();
246 // Update recording stream based on mParameters
247 status_t updateRecordingStream();
248
249 /** Camera2Device instance wrapping HAL2 entry */
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700250
251 sp<Camera2Device> mDevice;
252
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700253 /** Utility members */
254
255 // Utility class for managing a set of IMemory blocks
256 class Camera2Heap : public RefBase {
257 public:
258 Camera2Heap(size_t buf_size, uint_t num_buffers = 1,
259 const char *name = NULL) :
260 mBufSize(buf_size),
261 mNumBufs(num_buffers) {
262 mHeap = new MemoryHeapBase(buf_size * num_buffers, 0, name);
263 mBuffers = new sp<MemoryBase>[mNumBufs];
264 for (uint_t i = 0; i < mNumBufs; i++)
265 mBuffers[i] = new MemoryBase(mHeap,
266 i * mBufSize,
267 mBufSize);
268 }
269
270 virtual ~Camera2Heap()
271 {
272 delete [] mBuffers;
273 }
274
275 size_t mBufSize;
276 uint_t mNumBufs;
277 sp<MemoryHeapBase> mHeap;
278 sp<MemoryBase> *mBuffers;
279 };
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700280
281 // Get values for static camera info entry. min/maxCount are used for error
282 // checking the number of values in the entry. 0 for max/minCount means to
283 // do no bounds check in that direction. In case of error, the entry data
284 // pointer is null and the count is 0.
285 camera_metadata_entry_t staticInfo(uint32_t tag,
286 size_t minCount=0, size_t maxCount=0);
287
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700288 // Convert static camera info from a camera2 device to the
289 // old API parameter map.
290 status_t buildDefaultParameters();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700291
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -0700292 // Update parameters all requests use, based on mParameters
293 status_t updateRequestCommon(camera_metadata_t *request);
294
295 // Update specific metadata entry with new values. Adds entry if it does not
296 // exist, which will invalidate sorting
297 static status_t updateEntry(camera_metadata_t *buffer,
298 uint32_t tag, const void *data, size_t data_count);
299
300 // Remove metadata entry. Will invalidate sorting. If entry does not exist,
301 // does nothing.
302 static status_t deleteEntry(camera_metadata_t *buffer,
303 uint32_t tag);
304
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700305 // Convert camera1 preview format string to camera2 enum
306 static int formatStringToEnum(const char *format);
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700307 static const char *formatEnumToString(int format);
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700308
309 static int wbModeStringToEnum(const char *wbMode);
310 static int effectModeStringToEnum(const char *effectMode);
311 static int abModeStringToEnum(const char *abMode);
312 static int sceneModeStringToEnum(const char *sceneMode);
313 static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode);
314 static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode);
315 static status_t parseAreas(const char *areasCStr,
316 Vector<Parameters::Area> *areas);
317 static status_t validateAreas(const Vector<Parameters::Area> &areas,
318 size_t maxRegions);
319 static bool boolFromString(const char *boolStr);
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700320
321 // Map from camera orientation + facing to gralloc transform enum
322 static int degToTransform(int degrees, bool mirror);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700323};
324
325}; // namespace android
326
327#endif