blob: 83fe94e3b7c003bf088dfd68ef947e36a1ce2408 [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 Talvala78822d72012-07-18 17:52:18 -070026#include "MediaConsumer.h"
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070027
28namespace android {
29
30/**
31 * Implements the android.hardware.camera API on top of
32 * camera device HAL version 2.
33 */
34class Camera2Client : public CameraService::Client
35{
36public:
37 // ICamera interface (see ICamera for details)
38 virtual void disconnect();
39 virtual status_t connect(const sp<ICameraClient>& client);
40 virtual status_t lock();
41 virtual status_t unlock();
42 virtual status_t setPreviewDisplay(const sp<Surface>& surface);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070043 virtual status_t setPreviewTexture(
44 const sp<ISurfaceTexture>& surfaceTexture);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070045 virtual void setPreviewCallbackFlag(int flag);
46 virtual status_t startPreview();
47 virtual void stopPreview();
48 virtual bool previewEnabled();
49 virtual status_t storeMetaDataInBuffers(bool enabled);
50 virtual status_t startRecording();
51 virtual void stopRecording();
52 virtual bool recordingEnabled();
53 virtual void releaseRecordingFrame(const sp<IMemory>& mem);
54 virtual status_t autoFocus();
55 virtual status_t cancelAutoFocus();
56 virtual status_t takePicture(int msgType);
57 virtual status_t setParameters(const String8& params);
58 virtual String8 getParameters() const;
59 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
60
61 // Interface used by CameraService
62 Camera2Client(const sp<CameraService>& cameraService,
63 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070064 int cameraId,
65 int cameraFacing,
66 int clientPid);
67 ~Camera2Client();
68
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070069 status_t initialize(camera_module_t *module);
70
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070071 virtual status_t dump(int fd, const Vector<String16>& args);
72
73private:
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -070074 enum State {
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070075 NOT_INITIALIZED,
76 STOPPED,
77 WAITING_FOR_PREVIEW_WINDOW,
Eino-Ville Talvala7f610842012-06-07 10:20:51 -070078 PREVIEW,
79 RECORD,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070080 STILL_CAPTURE,
81 VIDEO_SNAPSHOT
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070082 } mState;
83
Eino-Ville Talvala4ecfec32012-06-12 17:13:48 -070084 static const char *getStateName(State state);
85
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -070086 /** ICamera interface-related private members */
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070087
Eino-Ville Talvalaac45eb32012-06-07 10:24:51 -070088 // Mutex that must be locked by methods implementing the ICamera interface.
89 // Ensures serialization between incoming ICamera calls
90 mutable Mutex mICameraLock;
91
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070092 // The following must be called with mICamaeraLock already locked
Eino-Ville Talvalaac45eb32012-06-07 10:24:51 -070093
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070094 status_t setPreviewWindowLocked(const sp<IBinder>& binder,
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -070095 sp<ANativeWindow> window);
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070096
97 void stopPreviewLocked();
98 status_t startPreviewLocked();
99
100 // Mutex that must be locked before accessing mParameters, mParamsFlattened
Eino-Ville Talvalaac45eb32012-06-07 10:24:51 -0700101 mutable Mutex mParamsLock;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700102 String8 mParamsFlattened;
103 // Current camera state; this is the contents of the CameraParameters object
104 // in a more-efficient format. The enum values are mostly based off the
105 // corresponding camera2 enums, not the camera1 strings. A few are defined
106 // here if they don't cleanly map to camera2 values.
107 struct Parameters {
108 int previewWidth, previewHeight;
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700109 int32_t previewFpsRange[2];
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700110 int previewFps; // deprecated, here only for tracking changes
111 int previewFormat;
112
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700113 int previewTransform; // set by CAMERA_CMD_SET_DISPLAY_ORIENTATION
114
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700115 int pictureWidth, pictureHeight;
116
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700117 int32_t jpegThumbSize[2];
118 int32_t jpegQuality, jpegThumbQuality;
119 int32_t jpegRotation;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700120
121 bool gpsEnabled;
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700122 double gpsCoordinates[3];
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700123 int64_t gpsTimestamp;
124 String8 gpsProcessingMethod;
125
126 int wbMode;
127 int effectMode;
128 int antibandingMode;
129 int sceneMode;
130
131 enum flashMode_t {
132 FLASH_MODE_OFF = 0,
133 FLASH_MODE_AUTO,
134 FLASH_MODE_ON,
135 FLASH_MODE_TORCH,
136 FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE,
137 FLASH_MODE_INVALID = -1
138 } flashMode;
139
140 enum focusMode_t {
141 FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO,
142 FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO,
143 FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO,
144 FOCUS_MODE_CONTINUOUS_PICTURE =
145 ANDROID_CONTROL_AF_CONTINUOUS_PICTURE,
146 FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF,
147 FOCUS_MODE_INFINITY,
148 FOCUS_MODE_FIXED,
149 FOCUS_MODE_INVALID = -1
150 } focusMode;
151
152 struct Area {
153 int left, top, right, bottom;
154 int weight;
155 Area() {}
156 Area(int left, int top, int right, int bottom, int weight):
157 left(left), top(top), right(right), bottom(bottom),
158 weight(weight) {}
159 };
160 Vector<Area> focusingAreas;
161
Eino-Ville Talvala11b7cde2012-06-15 12:37:35 -0700162 int32_t exposureCompensation;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700163 bool autoExposureLock;
164 bool autoWhiteBalanceLock;
165
166 Vector<Area> meteringAreas;
167
168 int zoom;
169
170 int videoWidth, videoHeight;
171
172 bool recordingHint;
173 bool videoStabilization;
Eino-Ville Talvala78822d72012-07-18 17:52:18 -0700174
175 bool storeMetadataInBuffers;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700176 } mParameters;
177
178 /** Camera device-related private members */
179
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700180 class Camera2Heap;
181
182 // Number of zoom steps to simulate
183 static const unsigned int NUM_ZOOM_STEPS = 10;
184 // Used with stream IDs
185 static const int NO_STREAM = -1;
186
187 /* Preview related members */
188
189 int mPreviewStreamId;
190 camera_metadata_t *mPreviewRequest;
191 sp<IBinder> mPreviewSurface;
192 sp<ANativeWindow> mPreviewWindow;
193 // Update preview request based on mParameters
194 status_t updatePreviewRequest();
195 // Update preview stream based on mParameters
196 status_t updatePreviewStream();
197
198 /* Still image capture related members */
199
200 int mCaptureStreamId;
201 sp<CpuConsumer> mCaptureConsumer;
202 sp<ANativeWindow> mCaptureWindow;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700203 // Simple listener that forwards frame available notifications from
204 // a CPU consumer to the capture notification
205 class CaptureWaiter: public CpuConsumer::FrameAvailableListener {
206 public:
207 CaptureWaiter(Camera2Client *parent) : mParent(parent) {}
208 void onFrameAvailable() { mParent->onCaptureAvailable(); }
209 private:
210 Camera2Client *mParent;
211 };
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700212 sp<CaptureWaiter> mCaptureWaiter;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700213 camera_metadata_t *mCaptureRequest;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700214 sp<Camera2Heap> mCaptureHeap;
215 // Handle captured image buffers
216 void onCaptureAvailable();
217 // Update capture request based on mParameters
218 status_t updateCaptureRequest();
219 // Update capture stream based on mParameters
220 status_t updateCaptureStream();
221
222 /* Recording related members */
223
224 int mRecordingStreamId;
Eino-Ville Talvala78822d72012-07-18 17:52:18 -0700225 sp<MediaConsumer> mRecordingConsumer;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700226 sp<ANativeWindow> mRecordingWindow;
227 // Simple listener that forwards frame available notifications from
228 // a CPU consumer to the recording notification
Eino-Ville Talvala78822d72012-07-18 17:52:18 -0700229 class RecordingWaiter: public MediaConsumer::FrameAvailableListener {
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700230 public:
231 RecordingWaiter(Camera2Client *parent) : mParent(parent) {}
232 void onFrameAvailable() { mParent->onRecordingFrameAvailable(); }
233 private:
234 Camera2Client *mParent;
235 };
236 sp<RecordingWaiter> mRecordingWaiter;
237 camera_metadata_t *mRecordingRequest;
238 sp<Camera2Heap> mRecordingHeap;
239
240 // TODO: This needs to be queried from somewhere, or the BufferQueue needs
Eino-Ville Talvala803cbf62012-07-25 15:23:36 -0700241 // to be passed all the way to stagefright. Right now, set to a large number
242 // to avoid starvation of the video encoders.
243 static const size_t kRecordingHeapCount = 8;
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700244 size_t mRecordingHeapHead, mRecordingHeapFree;
245 // Handle new recording image buffers
246 void onRecordingFrameAvailable();
247 // Update recording request based on mParameters
248 status_t updateRecordingRequest();
249 // Update recording stream based on mParameters
250 status_t updateRecordingStream();
251
252 /** Camera2Device instance wrapping HAL2 entry */
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700253
254 sp<Camera2Device> mDevice;
255
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700256 /** Utility members */
257
258 // Utility class for managing a set of IMemory blocks
259 class Camera2Heap : public RefBase {
260 public:
261 Camera2Heap(size_t buf_size, uint_t num_buffers = 1,
262 const char *name = NULL) :
263 mBufSize(buf_size),
264 mNumBufs(num_buffers) {
265 mHeap = new MemoryHeapBase(buf_size * num_buffers, 0, name);
266 mBuffers = new sp<MemoryBase>[mNumBufs];
267 for (uint_t i = 0; i < mNumBufs; i++)
268 mBuffers[i] = new MemoryBase(mHeap,
269 i * mBufSize,
270 mBufSize);
271 }
272
273 virtual ~Camera2Heap()
274 {
275 delete [] mBuffers;
276 }
277
278 size_t mBufSize;
279 uint_t mNumBufs;
280 sp<MemoryHeapBase> mHeap;
281 sp<MemoryBase> *mBuffers;
282 };
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700283
284 // Get values for static camera info entry. min/maxCount are used for error
285 // checking the number of values in the entry. 0 for max/minCount means to
286 // do no bounds check in that direction. In case of error, the entry data
287 // pointer is null and the count is 0.
288 camera_metadata_entry_t staticInfo(uint32_t tag,
289 size_t minCount=0, size_t maxCount=0);
290
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700291 // Convert static camera info from a camera2 device to the
292 // old API parameter map.
293 status_t buildDefaultParameters();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700294
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -0700295 // Update parameters all requests use, based on mParameters
296 status_t updateRequestCommon(camera_metadata_t *request);
297
298 // Update specific metadata entry with new values. Adds entry if it does not
299 // exist, which will invalidate sorting
300 static status_t updateEntry(camera_metadata_t *buffer,
301 uint32_t tag, const void *data, size_t data_count);
302
303 // Remove metadata entry. Will invalidate sorting. If entry does not exist,
304 // does nothing.
305 static status_t deleteEntry(camera_metadata_t *buffer,
306 uint32_t tag);
307
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700308 // Convert camera1 preview format string to camera2 enum
309 static int formatStringToEnum(const char *format);
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700310 static const char *formatEnumToString(int format);
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700311
312 static int wbModeStringToEnum(const char *wbMode);
313 static int effectModeStringToEnum(const char *effectMode);
314 static int abModeStringToEnum(const char *abMode);
315 static int sceneModeStringToEnum(const char *sceneMode);
316 static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode);
317 static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode);
318 static status_t parseAreas(const char *areasCStr,
319 Vector<Parameters::Area> *areas);
320 static status_t validateAreas(const Vector<Parameters::Area> &areas,
321 size_t maxRegions);
322 static bool boolFromString(const char *boolStr);
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700323
324 // Map from camera orientation + facing to gralloc transform enum
325 static int degToTransform(int degrees, bool mirror);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700326};
327
328}; // namespace android
329
330#endif