blob: 5457343bea90f5c5075d8ac6d8682b2c1e539df7 [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 Talvala6db981c2012-05-21 18:54:30 -070073 enum {
74 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 Talvala3cca1362012-06-07 10:07:18 -070083 /** ICamera interface-related private members */
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070084
Eino-Ville Talvalaac45eb32012-06-07 10:24:51 -070085 // Mutex that must be locked by methods implementing the ICamera interface.
86 // Ensures serialization between incoming ICamera calls
87 mutable Mutex mICameraLock;
88
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070089 // The following must be called with mICamaeraLock already locked
Eino-Ville Talvalaac45eb32012-06-07 10:24:51 -070090
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070091 status_t setPreviewWindowLocked(const sp<IBinder>& binder,
92 const sp<ANativeWindow>& window);
93
94 void stopPreviewLocked();
95 status_t startPreviewLocked();
96
97 // Mutex that must be locked before accessing mParameters, mParamsFlattened
Eino-Ville Talvalaac45eb32012-06-07 10:24:51 -070098 mutable Mutex mParamsLock;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -070099 String8 mParamsFlattened;
100 // Current camera state; this is the contents of the CameraParameters object
101 // in a more-efficient format. The enum values are mostly based off the
102 // corresponding camera2 enums, not the camera1 strings. A few are defined
103 // here if they don't cleanly map to camera2 values.
104 struct Parameters {
105 int previewWidth, previewHeight;
106 int previewFpsRangeMin, previewFpsRangeMax;
107 int previewFps; // deprecated, here only for tracking changes
108 int previewFormat;
109
110 int pictureWidth, pictureHeight;
111
112 int jpegThumbWidth, jpegThumbHeight;
113 int jpegQuality, jpegThumbQuality;
114 int jpegRotation;
115
116 bool gpsEnabled;
117 double gpsLatitude;
118 double gpsLongitude;
119 double gpsAltitude;
120 int64_t gpsTimestamp;
121 String8 gpsProcessingMethod;
122
123 int wbMode;
124 int effectMode;
125 int antibandingMode;
126 int sceneMode;
127
128 enum flashMode_t {
129 FLASH_MODE_OFF = 0,
130 FLASH_MODE_AUTO,
131 FLASH_MODE_ON,
132 FLASH_MODE_TORCH,
133 FLASH_MODE_RED_EYE = ANDROID_CONTROL_AE_ON_AUTO_FLASH_REDEYE,
134 FLASH_MODE_INVALID = -1
135 } flashMode;
136
137 enum focusMode_t {
138 FOCUS_MODE_AUTO = ANDROID_CONTROL_AF_AUTO,
139 FOCUS_MODE_MACRO = ANDROID_CONTROL_AF_MACRO,
140 FOCUS_MODE_CONTINUOUS_VIDEO = ANDROID_CONTROL_AF_CONTINUOUS_VIDEO,
141 FOCUS_MODE_CONTINUOUS_PICTURE =
142 ANDROID_CONTROL_AF_CONTINUOUS_PICTURE,
143 FOCUS_MODE_EDOF = ANDROID_CONTROL_AF_EDOF,
144 FOCUS_MODE_INFINITY,
145 FOCUS_MODE_FIXED,
146 FOCUS_MODE_INVALID = -1
147 } focusMode;
148
149 struct Area {
150 int left, top, right, bottom;
151 int weight;
152 Area() {}
153 Area(int left, int top, int right, int bottom, int weight):
154 left(left), top(top), right(right), bottom(bottom),
155 weight(weight) {}
156 };
157 Vector<Area> focusingAreas;
158
159 int exposureCompensation;
160 bool autoExposureLock;
161 bool autoWhiteBalanceLock;
162
163 Vector<Area> meteringAreas;
164
165 int zoom;
166
167 int videoWidth, videoHeight;
168
169 bool recordingHint;
170 bool videoStabilization;
171 } mParameters;
172
173 /** Camera device-related private members */
174
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700175 // Simple listener that forwards frame available notifications from
176 // a CPU consumer to the capture notification
177 class CaptureWaiter: public CpuConsumer::FrameAvailableListener {
178 public:
179 CaptureWaiter(Camera2Client *parent) : mParent(parent) {}
180 void onFrameAvailable() { mParent->onCaptureAvailable(); }
181 private:
182 Camera2Client *mParent;
183 };
184
185 void onCaptureAvailable();
186
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700187 // Number of zoom steps to simulate
188 static const unsigned int NUM_ZOOM_STEPS = 10;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700189 // Used with mPreviewStreamId, mCaptureStreamId
190 static const int NO_STREAM = -1;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700191
192 sp<IBinder> mPreviewSurface;
193 int mPreviewStreamId;
194 camera_metadata_t *mPreviewRequest;
195
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700196 int mCaptureStreamId;
197 sp<CpuConsumer> mCaptureConsumer;
198 sp<ANativeWindow> mCaptureWindow;
199 sp<CaptureWaiter> mCaptureWaiter;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700200 camera_metadata_t *mCaptureRequest;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700201 sp<MemoryHeapBase> mCaptureHeap;
202 sp<MemoryBase> mCaptureMemory;
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700203
204 sp<Camera2Device> mDevice;
205
206
207 // Get values for static camera info entry. min/maxCount are used for error
208 // checking the number of values in the entry. 0 for max/minCount means to
209 // do no bounds check in that direction. In case of error, the entry data
210 // pointer is null and the count is 0.
211 camera_metadata_entry_t staticInfo(uint32_t tag,
212 size_t minCount=0, size_t maxCount=0);
213
214 /** Utility methods */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700215
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700216 // Convert static camera info from a camera2 device to the
217 // old API parameter map.
218 status_t buildDefaultParameters();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700219
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700220 // Update preview request based on mParams
221 status_t updatePreviewRequest();
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700222
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700223 // Update capture request based on mParams
224 status_t updateCaptureRequest();
225 // Update capture stream based on mParams
226 status_t updateCaptureStream();
227
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700228 // Convert camera1 preview format string to camera2 enum
229 static int formatStringToEnum(const char *format);
Eino-Ville Talvala3cca1362012-06-07 10:07:18 -0700230 static const char *formatEnumToString(int format);
Eino-Ville Talvala6861a4e2012-06-07 10:32:12 -0700231
232 static int wbModeStringToEnum(const char *wbMode);
233 static int effectModeStringToEnum(const char *effectMode);
234 static int abModeStringToEnum(const char *abMode);
235 static int sceneModeStringToEnum(const char *sceneMode);
236 static Parameters::flashMode_t flashModeStringToEnum(const char *flashMode);
237 static Parameters::focusMode_t focusModeStringToEnum(const char *focusMode);
238 static status_t parseAreas(const char *areasCStr,
239 Vector<Parameters::Area> *areas);
240 static status_t validateAreas(const Vector<Parameters::Area> &areas,
241 size_t maxRegions);
242 static bool boolFromString(const char *boolStr);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700243};
244
245}; // namespace android
246
247#endif