blob: ea21af82bc0559caee5e1dbc44f837c3f95bacd5 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
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#define LOG_TAG "CameraHardwareStub"
19#include <utils/Log.h>
20
21#include "CameraHardwareStub.h"
22#include <utils/threads.h>
23#include <fcntl.h>
24#include <sys/mman.h>
25
26#include "CannedJpeg.h"
27
28namespace android {
29
30CameraHardwareStub::CameraHardwareStub()
31 : mParameters(),
32 mHeap(0),
33 mFakeCamera(0),
34 mPreviewFrameSize(0),
35 mRawPictureCallback(0),
36 mJpegPictureCallback(0),
37 mPictureCallbackCookie(0),
38 mPreviewCallback(0),
39 mPreviewCallbackCookie(0),
40 mAutoFocusCallback(0),
41 mAutoFocusCallbackCookie(0),
42 mCurrentPreviewFrame(0)
43{
44 initDefaultParameters();
45}
46
47void CameraHardwareStub::initDefaultParameters()
48{
49 CameraParameters p;
50
51 p.setPreviewSize(176, 144);
52 p.setPreviewFrameRate(15);
53 p.setPreviewFormat("yuv422sp");
54
55 p.setPictureSize(kCannedJpegWidth, kCannedJpegHeight);
56 p.setPictureFormat("jpeg");
57
58 if (setParameters(p) != NO_ERROR) {
59 LOGE("Failed to set default parameters?!");
60 }
61}
62
63void CameraHardwareStub::initHeapLocked()
64{
65 int width, height;
66 mParameters.getPreviewSize(&width, &height);
67
68 LOGD("initHeapLocked: preview size=%dx%d", width, height);
69
70 // Note that we enforce yuv422 in setParameters().
71 int how_big = width * height * 2;
72
73 // If we are being reinitialized to the same size as before, no
74 // work needs to be done.
75 if (how_big == mPreviewFrameSize)
76 return;
77
78 mPreviewFrameSize = how_big;
79
80 // Make a new mmap'ed heap that can be shared across processes.
81 // use code below to test with pmem
82 mHeap = new MemoryHeapBase(mPreviewFrameSize * kBufferCount);
83 // Make an IMemory for each frame so that we can reuse them in callbacks.
84 for (int i = 0; i < kBufferCount; i++) {
85 mBuffers[i] = new MemoryBase(mHeap, i * mPreviewFrameSize, mPreviewFrameSize);
86 }
87
88 // Recreate the fake camera to reflect the current size.
89 delete mFakeCamera;
90 mFakeCamera = new FakeCamera(width, height);
91}
92
93CameraHardwareStub::~CameraHardwareStub()
94{
95 delete mFakeCamera;
96 mFakeCamera = 0; // paranoia
97 singleton.clear();
98}
99
100sp<IMemoryHeap> CameraHardwareStub::getPreviewHeap() const
101{
102 return mHeap;
103}
104
105// ---------------------------------------------------------------------------
106
107int CameraHardwareStub::previewThread()
108{
109 mLock.lock();
110 // the attributes below can change under our feet...
111
112 int previewFrameRate = mParameters.getPreviewFrameRate();
113
114 // Find the offset within the heap of the current buffer.
115 ssize_t offset = mCurrentPreviewFrame * mPreviewFrameSize;
116
117 sp<MemoryHeapBase> heap = mHeap;
118
119 // this assumes the internal state of fake camera doesn't change
120 // (or is thread safe)
121 FakeCamera* fakeCamera = mFakeCamera;
122
123 sp<MemoryBase> buffer = mBuffers[mCurrentPreviewFrame];
124
125 mLock.unlock();
126
127 // TODO: here check all the conditions that could go wrong
128 if (buffer != 0) {
129 // Calculate how long to wait between frames.
130 int delay = (int)(1000000.0f / float(previewFrameRate));
131
132 // This is always valid, even if the client died -- the memory
133 // is still mapped in our process.
134 void *base = heap->base();
135
136 // Fill the current frame with the fake camera.
137 uint8_t *frame = ((uint8_t *)base) + offset;
138 fakeCamera->getNextFrameAsYuv422(frame);
139
140 //LOGV("previewThread: generated frame to buffer %d", mCurrentPreviewFrame);
141
142 // Notify the client of a new frame.
143 mPreviewCallback(buffer, mPreviewCallbackCookie);
144
145 // Advance the buffer pointer.
146 mCurrentPreviewFrame = (mCurrentPreviewFrame + 1) % kBufferCount;
147
148 // Wait for it...
149 usleep(delay);
150 }
151
152 return NO_ERROR;
153}
154
155status_t CameraHardwareStub::startPreview(preview_callback cb, void* user)
156{
157 Mutex::Autolock lock(mLock);
158 if (mPreviewThread != 0) {
159 // already running
160 return INVALID_OPERATION;
161 }
162 mPreviewCallback = cb;
163 mPreviewCallbackCookie = user;
164 mPreviewThread = new PreviewThread(this);
165 return NO_ERROR;
166}
167
168void CameraHardwareStub::stopPreview()
169{
170 sp<PreviewThread> previewThread;
171
172 { // scope for the lock
173 Mutex::Autolock lock(mLock);
174 previewThread = mPreviewThread;
175 }
176
177 // don't hold the lock while waiting for the thread to quit
178 if (previewThread != 0) {
179 previewThread->requestExitAndWait();
180 }
181
182 Mutex::Autolock lock(mLock);
183 mPreviewThread.clear();
184}
185
186// ---------------------------------------------------------------------------
187
188int CameraHardwareStub::beginAutoFocusThread(void *cookie)
189{
190 CameraHardwareStub *c = (CameraHardwareStub *)cookie;
191 return c->autoFocusThread();
192}
193
194int CameraHardwareStub::autoFocusThread()
195{
196 if (mAutoFocusCallback != NULL) {
197 mAutoFocusCallback(true, mAutoFocusCallbackCookie);
198 mAutoFocusCallback = NULL;
199 return NO_ERROR;
200 }
201 return UNKNOWN_ERROR;
202}
203
204status_t CameraHardwareStub::autoFocus(autofocus_callback af_cb,
205 void *user)
206{
207 Mutex::Autolock lock(mLock);
208
209 if (mAutoFocusCallback != NULL) {
210 return mAutoFocusCallback == af_cb ? NO_ERROR : INVALID_OPERATION;
211 }
212
213 mAutoFocusCallback = af_cb;
214 mAutoFocusCallbackCookie = user;
215 if (createThread(beginAutoFocusThread, this) == false)
216 return UNKNOWN_ERROR;
217 return NO_ERROR;
218}
219
220/*static*/ int CameraHardwareStub::beginPictureThread(void *cookie)
221{
222 CameraHardwareStub *c = (CameraHardwareStub *)cookie;
223 return c->pictureThread();
224}
225
226int CameraHardwareStub::pictureThread()
227{
228 if (mShutterCallback)
229 mShutterCallback(mPictureCallbackCookie);
230
231 if (mRawPictureCallback) {
232 //FIXME: use a canned YUV image!
233 // In the meantime just make another fake camera picture.
234 int w, h;
235 mParameters.getPictureSize(&w, &h);
236 sp<MemoryHeapBase> heap = new MemoryHeapBase(w * 2 * h);
237 sp<MemoryBase> mem = new MemoryBase(heap, 0, w * 2 * h);
238 FakeCamera cam(w, h);
239 cam.getNextFrameAsYuv422((uint8_t *)heap->base());
240 if (mRawPictureCallback)
241 mRawPictureCallback(mem, mPictureCallbackCookie);
242 }
243
244 if (mJpegPictureCallback) {
245 sp<MemoryHeapBase> heap = new MemoryHeapBase(kCannedJpegSize);
246 sp<MemoryBase> mem = new MemoryBase(heap, 0, kCannedJpegSize);
247 memcpy(heap->base(), kCannedJpeg, kCannedJpegSize);
248 if (mJpegPictureCallback)
249 mJpegPictureCallback(mem, mPictureCallbackCookie);
250 }
251 return NO_ERROR;
252}
253
254status_t CameraHardwareStub::takePicture(shutter_callback shutter_cb,
255 raw_callback raw_cb,
256 jpeg_callback jpeg_cb,
257 void* user)
258{
259 stopPreview();
260 mShutterCallback = shutter_cb;
261 mRawPictureCallback = raw_cb;
262 mJpegPictureCallback = jpeg_cb;
263 mPictureCallbackCookie = user;
264 if (createThread(beginPictureThread, this) == false)
265 return -1;
266 return NO_ERROR;
267}
268
269status_t CameraHardwareStub::cancelPicture(bool cancel_shutter,
270 bool cancel_raw,
271 bool cancel_jpeg)
272{
273 if (cancel_shutter) mShutterCallback = NULL;
274 if (cancel_raw) mRawPictureCallback = NULL;
275 if (cancel_jpeg) mJpegPictureCallback = NULL;
276 return NO_ERROR;
277}
278
279status_t CameraHardwareStub::dump(int fd, const Vector<String16>& args) const
280{
281 const size_t SIZE = 256;
282 char buffer[SIZE];
283 String8 result;
284 AutoMutex lock(&mLock);
285 if (mFakeCamera != 0) {
286 mFakeCamera->dump(fd, args);
287 mParameters.dump(fd, args);
288 snprintf(buffer, 255, " preview frame(%d), size (%d), running(%s)\n", mCurrentPreviewFrame, mPreviewFrameSize, mPreviewRunning?"true": "false");
289 result.append(buffer);
290 } else {
291 result.append("No camera client yet.\n");
292 }
293 write(fd, result.string(), result.size());
294 return NO_ERROR;
295}
296
297status_t CameraHardwareStub::setParameters(const CameraParameters& params)
298{
299 Mutex::Autolock lock(mLock);
300 // XXX verify params
301
302 if (strcmp(params.getPreviewFormat(), "yuv422sp") != 0) {
303 LOGE("Only yuv422sp preview is supported");
304 return -1;
305 }
306
307 if (strcmp(params.getPictureFormat(), "jpeg") != 0) {
308 LOGE("Only jpeg still pictures are supported");
309 return -1;
310 }
311
312 int w, h;
313 params.getPictureSize(&w, &h);
314 if (w != kCannedJpegWidth && h != kCannedJpegHeight) {
315 LOGE("Still picture size must be size of canned JPEG (%dx%d)",
316 kCannedJpegWidth, kCannedJpegHeight);
317 return -1;
318 }
319
320 mParameters = params;
321
322 initHeapLocked();
323
324 return NO_ERROR;
325}
326
327CameraParameters CameraHardwareStub::getParameters() const
328{
329 Mutex::Autolock lock(mLock);
330 return mParameters;
331}
332
333void CameraHardwareStub::release()
334{
335}
336
337wp<CameraHardwareInterface> CameraHardwareStub::singleton;
338
339sp<CameraHardwareInterface> CameraHardwareStub::createInstance()
340{
341 if (singleton != 0) {
342 sp<CameraHardwareInterface> hardware = singleton.promote();
343 if (hardware != 0) {
344 return hardware;
345 }
346 }
347 sp<CameraHardwareInterface> hardware(new CameraHardwareStub());
348 singleton = hardware;
349 return hardware;
350}
351
352extern "C" sp<CameraHardwareInterface> openCameraHardware()
353{
354 return CameraHardwareStub::createInstance();
355}
356
357}; // namespace android