blob: 776f9812d8b4a9b0227cae05b50bf7141cac320a [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 Talvalad4bcfde2012-06-07 17:12:38 -0700177 // Simple listener that forwards frame available notifications from
178 // a CPU consumer to the capture notification
179 class CaptureWaiter: public CpuConsumer::FrameAvailableListener {
180 public:
181 CaptureWaiter(Camera2Client *parent) : mParent(parent) {}
182 void onFrameAvailable() { mParent->onCaptureAvailable(); }
183 private:
184 Camera2Client *mParent;
185 };
186
187 void onCaptureAvailable();
188
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700189 // Number of zoom steps to simulate
190 static const unsigned int NUM_ZOOM_STEPS = 10;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700191 // Used with mPreviewStreamId, mCaptureStreamId
192 static const int NO_STREAM = -1;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700193
194 sp<IBinder> mPreviewSurface;
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -0700195 sp<ANativeWindow> mPreviewWindow;
196
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700197 int mPreviewStreamId;
198 camera_metadata_t *mPreviewRequest;
199
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700200 int mCaptureStreamId;
201 sp<CpuConsumer> mCaptureConsumer;
202 sp<ANativeWindow> mCaptureWindow;
203 sp<CaptureWaiter> mCaptureWaiter;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700204 camera_metadata_t *mCaptureRequest;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700205 sp<MemoryHeapBase> mCaptureHeap;
206 sp<MemoryBase> mCaptureMemory;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700207
208 sp<Camera2Device> mDevice;
209
210
211 // Get values for static camera info entry. min/maxCount are used for error
212 // checking the number of values in the entry. 0 for max/minCount means to
213 // do no bounds check in that direction. In case of error, the entry data
214 // pointer is null and the count is 0.
215 camera_metadata_entry_t staticInfo(uint32_t tag,
216 size_t minCount=0, size_t maxCount=0);
217
218 /** Utility methods */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700219
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700220 // Convert static camera info from a camera2 device to the
221 // old API parameter map.
222 status_t buildDefaultParameters();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700223
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -0700224 // Update preview request based on mParameters
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700225 status_t updatePreviewRequest();
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -0700226 // Update preview stream based on mParameters
227 status_t updatePreviewStream();
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700228
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -0700229 // Update capture request based on mParameters
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700230 status_t updateCaptureRequest();
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -0700231 // Update capture stream based on mParameters
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700232 status_t updateCaptureStream();
233
Eino-Ville Talvalabe0573b2012-06-15 12:42:30 -0700234 // Update parameters all requests use, based on mParameters
235 status_t updateRequestCommon(camera_metadata_t *request);
236
237 // Update specific metadata entry with new values. Adds entry if it does not
238 // exist, which will invalidate sorting
239 static status_t updateEntry(camera_metadata_t *buffer,
240 uint32_t tag, const void *data, size_t data_count);
241
242 // Remove metadata entry. Will invalidate sorting. If entry does not exist,
243 // does nothing.
244 static status_t deleteEntry(camera_metadata_t *buffer,
245 uint32_t tag);
246
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700247 // Convert camera1 preview format string to camera2 enum
248 static int formatStringToEnum(const char *format);
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700249 static const char *formatEnumToString(int format);
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700250
251 static int wbModeStringToEnum(const char *wbMode);
252 static int effectModeStringToEnum(const char *effectMode);
253 static int abModeStringToEnum(const char *abMode);
254 static int sceneModeStringToEnum(const char *sceneMode);
255 static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode);
256 static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode);
257 static status_t parseAreas(const char *areasCStr,
258 Vector<Parameters::Area> *areas);
259 static status_t validateAreas(const Vector<Parameters::Area> &areas,
260 size_t maxRegions);
261 static bool boolFromString(const char *boolStr);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700262};
263
264}; // namespace android
265
266#endif