blob: 22836e313fad3dbc5338b9cd170b506cbd0e76ee [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright (C) 2008, The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004**
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#define LOG_TAG "CameraService"
Iliyan Malcheva269b872011-04-14 16:55:59 -070019//#define LOG_NDEBUG 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
Chih-Chung Change25cc652010-05-06 16:36:58 +080021#include <stdio.h>
22#include <sys/types.h>
23#include <pthread.h>
Wu-cheng Lia1c41e12012-02-23 19:01:00 -080024#include <time.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080025
Mathias Agopian07952722009-05-19 19:08:10 -070026#include <binder/IPCThreadState.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080027#include <binder/IServiceManager.h>
Mathias Agopian07952722009-05-19 19:08:10 -070028#include <binder/MemoryBase.h>
29#include <binder/MemoryHeapBase.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080030#include <cutils/atomic.h>
Nipun Kwatra088146a2010-09-11 19:31:10 -070031#include <cutils/properties.h>
Jamie Gennisff2dc462010-12-20 11:51:31 -080032#include <gui/SurfaceTextureClient.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080033#include <gui/Surface.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080034#include <hardware/hardware.h>
35#include <media/AudioSystem.h>
36#include <media/mediaplayer.h>
Wu-cheng Lia1c41e12012-02-23 19:01:00 -080037#include <utils/Condition.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080038#include <utils/Errors.h>
39#include <utils/Log.h>
40#include <utils/String16.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42#include "CameraService.h"
Iliyan Malcheva269b872011-04-14 16:55:59 -070043#include "CameraHardwareInterface.h"
Eric Laurenta7f1e5c2009-03-27 16:27:16 -070044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045namespace android {
46
Wu-cheng Lia1c41e12012-02-23 19:01:00 -080047#define WAIT_RELEASE_TIMEOUT 250 // 250ms
48
Chih-Chung Change25cc652010-05-06 16:36:58 +080049// ----------------------------------------------------------------------------
50// Logging support -- this is for debugging only
51// Use "adb shell dumpsys media.camera -v 1" to change it.
52static volatile int32_t gLogLevel = 0;
53
Steve Block5baa3a62011-12-20 16:23:08 +000054#define LOG1(...) ALOGD_IF(gLogLevel >= 1, __VA_ARGS__);
55#define LOG2(...) ALOGD_IF(gLogLevel >= 2, __VA_ARGS__);
Chih-Chung Change25cc652010-05-06 16:36:58 +080056
57static void setLogLevel(int level) {
58 android_atomic_write(level, &gLogLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059}
60
Chih-Chung Change25cc652010-05-06 16:36:58 +080061// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +080063static int getCallingPid() {
64 return IPCThreadState::self()->getCallingPid();
65}
66
Chih-Chung Change25cc652010-05-06 16:36:58 +080067static int getCallingUid() {
68 return IPCThreadState::self()->getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069}
70
Wu-cheng Lia1c41e12012-02-23 19:01:00 -080071static long long getTimeInMs() {
72 struct timeval t;
73 t.tv_sec = t.tv_usec = 0;
74 gettimeofday(&t, NULL);
75 return t.tv_sec * 1000LL + t.tv_usec / 1000;
76}
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078// ----------------------------------------------------------------------------
79
Chih-Chung Change25cc652010-05-06 16:36:58 +080080// This is ugly and only safe if we never re-create the CameraService, but
81// should be ok for now.
82static CameraService *gCameraService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
Chih-Chung Change25cc652010-05-06 16:36:58 +080084CameraService::CameraService()
Iliyan Malcheva269b872011-04-14 16:55:59 -070085:mSoundRef(0), mModule(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086{
Steve Block6215d3f2012-01-04 20:05:49 +000087 ALOGI("CameraService started (pid=%d)", getpid());
Chih-Chung Change25cc652010-05-06 16:36:58 +080088 gCameraService = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089}
90
Iliyan Malcheva269b872011-04-14 16:55:59 -070091void CameraService::onFirstRef()
92{
93 BnCameraService::onFirstRef();
94
95 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
96 (const hw_module_t **)&mModule) < 0) {
Steve Block3762c312012-01-06 19:20:56 +000097 ALOGE("Could not load camera HAL module");
Iliyan Malcheva269b872011-04-14 16:55:59 -070098 mNumberOfCameras = 0;
99 }
100 else {
101 mNumberOfCameras = mModule->get_number_of_cameras();
102 if (mNumberOfCameras > MAX_CAMERAS) {
Steve Block3762c312012-01-06 19:20:56 +0000103 ALOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
Iliyan Malcheva269b872011-04-14 16:55:59 -0700104 mNumberOfCameras, MAX_CAMERAS);
105 mNumberOfCameras = MAX_CAMERAS;
106 }
107 for (int i = 0; i < mNumberOfCameras; i++) {
108 setCameraFree(i);
109 }
110 }
111}
112
Chih-Chung Change25cc652010-05-06 16:36:58 +0800113CameraService::~CameraService() {
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800114 for (int i = 0; i < mNumberOfCameras; i++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800115 if (mBusy[i]) {
Steve Block3762c312012-01-06 19:20:56 +0000116 ALOGE("camera %d is still in use in destructor!", i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 }
118 }
119
Chih-Chung Change25cc652010-05-06 16:36:58 +0800120 gCameraService = NULL;
121}
122
123int32_t CameraService::getNumberOfCameras() {
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800124 return mNumberOfCameras;
125}
126
127status_t CameraService::getCameraInfo(int cameraId,
128 struct CameraInfo* cameraInfo) {
Iliyan Malcheva269b872011-04-14 16:55:59 -0700129 if (!mModule) {
130 return NO_INIT;
131 }
132
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800133 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
134 return BAD_VALUE;
135 }
136
Iliyan Malcheva269b872011-04-14 16:55:59 -0700137 struct camera_info info;
138 status_t rc = mModule->get_camera_info(cameraId, &info);
139 cameraInfo->facing = info.facing;
140 cameraInfo->orientation = info.orientation;
141 return rc;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800142}
143
144sp<ICamera> CameraService::connect(
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800145 const sp<ICameraClient>& cameraClient, int cameraId, bool force, bool keep) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800146 int callingPid = getCallingPid();
Tyler Luu7b6da3c2011-10-06 00:00:03 -0500147 sp<CameraHardwareInterface> hardware = NULL;
148
Chih-Chung Change25cc652010-05-06 16:36:58 +0800149 LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
150
Iliyan Malcheva269b872011-04-14 16:55:59 -0700151 if (!mModule) {
Steve Block3762c312012-01-06 19:20:56 +0000152 ALOGE("Camera HAL module not loaded");
Iliyan Malcheva269b872011-04-14 16:55:59 -0700153 return NULL;
154 }
155
Chih-Chung Change25cc652010-05-06 16:36:58 +0800156 sp<Client> client;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800157 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Steve Block3762c312012-01-06 19:20:56 +0000158 ALOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
Chih-Chung Change25cc652010-05-06 16:36:58 +0800159 callingPid, cameraId);
160 return NULL;
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800161 }
162
Wu-cheng Li04c453d2011-05-20 14:54:25 +0800163 char value[PROPERTY_VALUE_MAX];
164 property_get("sys.secpolicy.camera.disabled", value, "0");
165 if (strcmp(value, "1") == 0) {
166 // Camera is disabled by DevicePolicyManager.
Steve Block6215d3f2012-01-04 20:05:49 +0000167 ALOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
Wu-cheng Li04c453d2011-05-20 14:54:25 +0800168 return NULL;
169 }
170
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800171 if (keep && !checkCallingPermission(String16("android.permission.KEEP_CAMERA"))) {
172 ALOGE("connect X (pid %d) rejected (no KEEP_CAMERA permission).", callingPid);
173 return NULL;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800174 }
175
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800176 Mutex::Autolock lock(mServiceLock);
177 // Check if there is an existing client.
178 client = mClient[cameraId].promote();
179 if (client != 0 &&
180 cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
181 LOG1("connect X (pid %d) (the same client)", callingPid);
182 return client;
183 }
184
185 if (!force) {
186 if (mClient[cameraId].promote() != 0) {
187 ALOGW("connect X (pid %d) rejected (existing client).", callingPid);
188 return NULL;
189 }
190 mClient[cameraId].clear();
191 if (mBusy[cameraId]) {
192 ALOGW("connect X (pid %d) rejected (camera %d is still busy).",
193 callingPid, cameraId);
194 return NULL;
195 }
196 } else { // force == true
197 int i = 0;
198 long long start_time = getTimeInMs();
199 while (i < mNumberOfCameras) {
200 if (getTimeInMs() - start_time >= 3000LL) {
201 ALOGE("connect X (pid %d) rejected (timeout 3s)", callingPid);
202 return NULL;
203 }
204
205 client = mClient[i].promote();
206 if (client != 0) {
207 if (client->keep()) {
208 ALOGW("connect X (pid %d) rejected (existing client wants to keeps the camera)",
209 callingPid);
210 return NULL;
211 } else {
212 ALOGW("New client (pid %d, id=%d). Disconnect the existing client (id=%d).",
213 callingPid, cameraId, i);
214 // Do not hold mServiceLock because disconnect will try to get it.
215 mServiceLock.unlock();
216 client->notifyCallback(CAMERA_MSG_ERROR, CAMERA_ERROR_RELEASED, 0, &i);
217 client->waitRelease(WAIT_RELEASE_TIMEOUT);
218 client->disconnectInternal(false);
219 mServiceLock.lock();
220 // Restart from the first client because a new client may have connected
221 // when mServiceLock is unlocked.
222 i = 0;
223 continue;
224 }
225 }
226
227 if (mBusy[i]) {
228 // Give the client a chance to release the hardware.
229 mServiceLock.unlock();
230 usleep(10 * 1000);
231 mServiceLock.lock();
232 i = 0; // Restart from the first client
233 continue;
234 }
235
236 i++;
237 }
Chih-Chung Change25cc652010-05-06 16:36:58 +0800238 }
239
Iliyan Malcheva269b872011-04-14 16:55:59 -0700240 struct camera_info info;
241 if (mModule->get_camera_info(cameraId, &info) != OK) {
Steve Block3762c312012-01-06 19:20:56 +0000242 ALOGE("Invalid camera id %d", cameraId);
Wu-cheng Lie7044382010-08-17 15:45:37 -0700243 return NULL;
244 }
Iliyan Malcheva269b872011-04-14 16:55:59 -0700245
246 char camera_device_name[10];
247 snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId);
248
Tyler Luu7b6da3c2011-10-06 00:00:03 -0500249 hardware = new CameraHardwareInterface(camera_device_name);
250 if (hardware->initialize(&mModule->common) != OK) {
251 hardware.clear();
252 return NULL;
253 }
254
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800255 client = new Client(this, cameraClient, hardware, cameraId, info.facing,
256 callingPid, keep);
257 // We need to clear the hardware here. After the destructor of mServiceLock
258 // finishes, a new client may connect and disconnect this client. If this
259 // reference is not cleared, the destructor of CameraHardwareInterface
260 // cannot run. The new client will not be able to connect.
261 hardware.clear();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800262 mClient[cameraId] = client;
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800263 LOG1("CameraService::connect X (id %d)", cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 return client;
265}
266
Chih-Chung Change25cc652010-05-06 16:36:58 +0800267void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800268 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800269 LOG1("CameraService::removeClient E (pid %d)", callingPid);
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800270
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800271 for (int i = 0; i < mNumberOfCameras; i++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800272 // Declare this before the lock to make absolutely sure the
273 // destructor won't be called with the lock held.
274 sp<Client> client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275
Chih-Chung Change25cc652010-05-06 16:36:58 +0800276 Mutex::Autolock lock(mServiceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277
Chih-Chung Change25cc652010-05-06 16:36:58 +0800278 // This happens when we have already disconnected (or this is
279 // just another unused camera).
280 if (mClient[i] == 0) continue;
281
282 // Promote mClient. It can fail if we are called from this path:
283 // Client::~Client() -> disconnect() -> removeClient().
284 client = mClient[i].promote();
285
286 if (client == 0) {
287 mClient[i].clear();
288 continue;
289 }
290
291 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
292 // Found our camera, clear and leave.
293 LOG1("removeClient: clear camera %d", i);
294 mClient[i].clear();
295 break;
296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
298
Chih-Chung Change25cc652010-05-06 16:36:58 +0800299 LOG1("CameraService::removeClient X (pid %d)", callingPid);
300}
301
302sp<CameraService::Client> CameraService::getClientById(int cameraId) {
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800303 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800304 return mClient[cameraId].promote();
305}
306
Chih-Chung Change25cc652010-05-06 16:36:58 +0800307status_t CameraService::onTransact(
308 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
309 // Permission checks
310 switch (code) {
311 case BnCameraService::CONNECT:
312 const int pid = getCallingPid();
313 const int self_pid = getpid();
314 if (pid != self_pid) {
315 // we're called from a different process, do the real check
316 if (!checkCallingPermission(
317 String16("android.permission.CAMERA"))) {
318 const int uid = getCallingUid();
Steve Block3762c312012-01-06 19:20:56 +0000319 ALOGE("Permission Denial: "
Chih-Chung Change25cc652010-05-06 16:36:58 +0800320 "can't use the camera pid=%d, uid=%d", pid, uid);
321 return PERMISSION_DENIED;
322 }
323 }
324 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 }
326
Chih-Chung Change25cc652010-05-06 16:36:58 +0800327 return BnCameraService::onTransact(code, data, reply, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328}
329
Chih-Chung Change25cc652010-05-06 16:36:58 +0800330// The reason we need this busy bit is a new CameraService::connect() request
331// may come in while the previous Client's destructor has not been run or is
332// still running. If the last strong reference of the previous Client is gone
333// but the destructor has not been finished, we should not allow the new Client
334// to be created because we need to wait for the previous Client to tear down
335// the hardware first.
336void CameraService::setCameraBusy(int cameraId) {
337 android_atomic_write(1, &mBusy[cameraId]);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800338}
339
Chih-Chung Change25cc652010-05-06 16:36:58 +0800340void CameraService::setCameraFree(int cameraId) {
341 android_atomic_write(0, &mBusy[cameraId]);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800342}
343
Chih-Chung Change25cc652010-05-06 16:36:58 +0800344// We share the media players for shutter and recording sound for all clients.
345// A reference count is kept to determine when we will actually release the
346// media players.
347
Chih-Chung Chang4091f0b2011-10-17 19:03:12 +0800348MediaPlayer* CameraService::newMediaPlayer(const char *file) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800349 MediaPlayer* mp = new MediaPlayer();
350 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Eino-Ville Talvalab7feb1c2012-01-05 15:34:53 -0800351 mp->setAudioStreamType(AUDIO_STREAM_ENFORCED_AUDIBLE);
Jason Samsb18b6912009-03-24 20:21:36 -0700352 mp->prepare();
353 } else {
Steve Block3762c312012-01-06 19:20:56 +0000354 ALOGE("Failed to load CameraService sounds: %s", file);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800355 return NULL;
Jason Samsb18b6912009-03-24 20:21:36 -0700356 }
357 return mp;
358}
359
Chih-Chung Change25cc652010-05-06 16:36:58 +0800360void CameraService::loadSound() {
361 Mutex::Autolock lock(mSoundLock);
362 LOG1("CameraService::loadSound ref=%d", mSoundRef);
363 if (mSoundRef++) return;
364
365 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
366 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
367}
368
369void CameraService::releaseSound() {
370 Mutex::Autolock lock(mSoundLock);
371 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
372 if (--mSoundRef) return;
373
374 for (int i = 0; i < NUM_SOUNDS; i++) {
375 if (mSoundPlayer[i] != 0) {
376 mSoundPlayer[i]->disconnect();
377 mSoundPlayer[i].clear();
378 }
379 }
380}
381
382void CameraService::playSound(sound_kind kind) {
383 LOG1("playSound(%d)", kind);
384 Mutex::Autolock lock(mSoundLock);
385 sp<MediaPlayer> player = mSoundPlayer[kind];
386 if (player != 0) {
Chih-Chung Changf8068712011-10-20 10:47:26 +0800387 player->seekTo(0);
388 player->start();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800389 }
390}
391
392// ----------------------------------------------------------------------------
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lie7044382010-08-17 15:45:37 -0700395 const sp<ICameraClient>& cameraClient,
396 const sp<CameraHardwareInterface>& hardware,
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800397 int cameraId, int cameraFacing, int clientPid, bool keep) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800398 int callingPid = getCallingPid();
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800399 LOG1("Client::Client E (pid %d, id %d)", callingPid, cameraId);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 mCameraService = cameraService;
402 mCameraClient = cameraClient;
Wu-cheng Lie7044382010-08-17 15:45:37 -0700403 mHardware = hardware;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800404 mCameraId = cameraId;
Wu-cheng Lid55f7e52010-10-14 20:17:44 +0800405 mCameraFacing = cameraFacing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 mClientPid = clientPid;
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800407 mKeep = keep;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800408 mMsgEnabled = 0;
Jamie Gennisff2dc462010-12-20 11:51:31 -0800409 mSurface = 0;
410 mPreviewWindow = 0;
Benny Wongda83f462009-08-12 12:01:27 -0500411 mHardware->setCallbacks(notifyCallback,
412 dataCallback,
413 dataCallbackTimestamp,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800414 (void *)cameraId);
Benny Wongda83f462009-08-12 12:01:27 -0500415
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800416 // Enable zoom, error, focus, and metadata messages by default
417 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
Wu-cheng Li9d062cf2011-11-14 20:30:14 +0800418 CAMERA_MSG_PREVIEW_METADATA | CAMERA_MSG_FOCUS_MOVE);
Jason Samsb18b6912009-03-24 20:21:36 -0700419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 // Callback is disabled by default
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700421 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lid55f7e52010-10-14 20:17:44 +0800422 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Nipun Kwatra088146a2010-09-11 19:31:10 -0700423 mPlayShutterSound = true;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800424 cameraService->setCameraBusy(cameraId);
425 cameraService->loadSound();
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800426 LOG1("Client::Client X (pid %d, id %d)", callingPid, cameraId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427}
428
Chih-Chung Change25cc652010-05-06 16:36:58 +0800429// tear down the client
430CameraService::Client::~Client() {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800431 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800432 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800433
Chih-Chung Change25cc652010-05-06 16:36:58 +0800434 // set mClientPid to let disconnet() tear down the hardware
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800435 mClientPid = callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 disconnect();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800437 mCameraService->releaseSound();
438 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439}
440
Chih-Chung Change25cc652010-05-06 16:36:58 +0800441// ----------------------------------------------------------------------------
442
443status_t CameraService::Client::checkPid() const {
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800444 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800445 if (callingPid == mClientPid) return NO_ERROR;
James Dong71d714c2010-06-09 15:57:48 -0700446
Steve Block8564c8d2012-01-05 23:22:43 +0000447 ALOGW("attempt to use a locked camera from a different process"
Chih-Chung Change25cc652010-05-06 16:36:58 +0800448 " (old pid %d, new pid %d)", mClientPid, callingPid);
449 return EBUSY;
450}
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800451
Chih-Chung Change25cc652010-05-06 16:36:58 +0800452status_t CameraService::Client::checkPidAndHardware() const {
453 status_t result = checkPid();
454 if (result != NO_ERROR) return result;
455 if (mHardware == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000456 ALOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
Chih-Chung Change25cc652010-05-06 16:36:58 +0800457 return INVALID_OPERATION;
458 }
459 return NO_ERROR;
460}
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800461
Chih-Chung Change25cc652010-05-06 16:36:58 +0800462status_t CameraService::Client::lock() {
463 int callingPid = getCallingPid();
464 LOG1("lock (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800466
467 // lock camera to this client if the the camera is unlocked
468 if (mClientPid == 0) {
469 mClientPid = callingPid;
470 return NO_ERROR;
471 }
472
473 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
474 return checkPid();
475}
476
477status_t CameraService::Client::unlock() {
478 int callingPid = getCallingPid();
479 LOG1("unlock (pid %d)", callingPid);
480 Mutex::Autolock lock(mLock);
481
482 // allow anyone to use camera (after they lock the camera)
483 status_t result = checkPid();
484 if (result == NO_ERROR) {
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800485 if (mHardware->recordingEnabled()) {
Steve Block3762c312012-01-06 19:20:56 +0000486 ALOGE("Not allowed to unlock camera during recording.");
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800487 return INVALID_OPERATION;
488 }
Chih-Chung Change25cc652010-05-06 16:36:58 +0800489 mClientPid = 0;
490 LOG1("clear mCameraClient (pid %d)", callingPid);
491 // we need to remove the reference to ICameraClient so that when the app
492 // goes away, the reference count goes to 0.
493 mCameraClient.clear();
494 }
495 return result;
496}
497
498// connect a new client to the camera
499status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
500 int callingPid = getCallingPid();
501 LOG1("connect E (pid %d)", callingPid);
502 Mutex::Autolock lock(mLock);
503
504 if (mClientPid != 0 && checkPid() != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000505 ALOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
Chih-Chung Change25cc652010-05-06 16:36:58 +0800506 mClientPid, callingPid);
507 return EBUSY;
508 }
509
510 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
511 LOG1("Connect to the same client");
512 return NO_ERROR;
513 }
514
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700515 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800516 mClientPid = callingPid;
517 mCameraClient = client;
518
519 LOG1("connect X (pid %d)", callingPid);
520 return NO_ERROR;
521}
522
Wu-cheng Li13528f72011-07-20 05:35:02 +0800523static void disconnectWindow(const sp<ANativeWindow>& window) {
524 if (window != 0) {
Mathias Agopian982d2da2011-07-29 17:55:48 -0700525 status_t result = native_window_api_disconnect(window.get(),
Wu-cheng Li13528f72011-07-20 05:35:02 +0800526 NATIVE_WINDOW_API_CAMERA);
527 if (result != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000528 ALOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
Wu-cheng Li13528f72011-07-20 05:35:02 +0800529 result);
530 }
531 }
532}
533
Chih-Chung Change25cc652010-05-06 16:36:58 +0800534void CameraService::Client::disconnect() {
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800535 disconnectInternal(true);
536}
537
538void CameraService::Client::disconnectInternal(bool needCheckPid) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800539 int callingPid = getCallingPid();
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800540 LOG1("disconnectInternal E (pid %d)", callingPid);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800541 Mutex::Autolock lock(mLock);
542
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800543 if (needCheckPid) {
544 if (checkPid() != NO_ERROR) {
545 ALOGW("different client - don't disconnect");
546 return;
547 }
Chih-Chung Change25cc652010-05-06 16:36:58 +0800548
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800549 if (mClientPid <= 0) {
550 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
551 return;
552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
554
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800555 // Make sure disconnect() is done once and once only, whether it is called
556 // from the user directly, or called by the destructor.
557 if (mHardware == 0) return;
558
Chih-Chung Change25cc652010-05-06 16:36:58 +0800559 LOG1("hardware teardown");
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800560 // Before destroying mHardware, we must make sure it's in the
561 // idle state.
Chih-Chung Change25cc652010-05-06 16:36:58 +0800562 // Turn off all messages.
563 disableMsgType(CAMERA_MSG_ALL_MSGS);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800564 mHardware->stopPreview();
Benny Wongda83f462009-08-12 12:01:27 -0500565 mHardware->cancelPicture();
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800566 // Release the hardware resources.
567 mHardware->release();
Mathias Agopiand2112302010-12-07 19:38:17 -0800568
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700569 // Release the held ANativeWindow resources.
570 if (mPreviewWindow != 0) {
Wu-cheng Li13528f72011-07-20 05:35:02 +0800571 disconnectWindow(mPreviewWindow);
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700572 mPreviewWindow = 0;
573 mHardware->setPreviewWindow(mPreviewWindow);
574 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 mHardware.clear();
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800576
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800577 mCameraService->removeClient(mCameraClient);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800578 mCameraService->setCameraFree(mCameraId);
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800579 mReleaseCondition.signal();
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800580
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800581 LOG1("disconnectInternal X (pid %d)", callingPid);
582}
583
584void CameraService::Client::waitRelease(int ms) {
585 Mutex::Autolock lock(mLock);
586 if (mHardware != 0) {
587 mReleaseCondition.waitRelative(mLock, ms * 1000000);
588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589}
590
Chih-Chung Change25cc652010-05-06 16:36:58 +0800591// ----------------------------------------------------------------------------
592
Jamie Gennisf1072002011-07-13 15:13:14 -0700593status_t CameraService::Client::setPreviewWindow(const sp<IBinder>& binder,
594 const sp<ANativeWindow>& window) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800596 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 if (result != NO_ERROR) return result;
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800598
Chih-Chung Change25cc652010-05-06 16:36:58 +0800599 // return if no change in surface.
Mathias Agopianc8a04b52011-04-05 15:44:20 -0700600 if (binder == mSurface) {
Jamie Gennisf1072002011-07-13 15:13:14 -0700601 return NO_ERROR;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800602 }
603
Jamie Gennisf1072002011-07-13 15:13:14 -0700604 if (window != 0) {
Mathias Agopian982d2da2011-07-29 17:55:48 -0700605 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
Jamie Gennisf1072002011-07-13 15:13:14 -0700606 if (result != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +0000607 ALOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
Jamie Gennisf1072002011-07-13 15:13:14 -0700608 result);
609 return result;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800610 }
611 }
612
Jamie Gennisf1072002011-07-13 15:13:14 -0700613 // If preview has been already started, register preview buffers now.
614 if (mHardware->previewEnabled()) {
615 if (window != 0) {
Mathias Agopianff86f372011-07-18 16:15:08 -0700616 native_window_set_scaling_mode(window.get(),
617 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jamie Gennisf1072002011-07-13 15:13:14 -0700618 native_window_set_buffers_transform(window.get(), mOrientation);
619 result = mHardware->setPreviewWindow(window);
620 }
621 }
622
623 if (result == NO_ERROR) {
624 // Everything has succeeded. Disconnect the old window and remember the
625 // new window.
626 disconnectWindow(mPreviewWindow);
627 mSurface = binder;
628 mPreviewWindow = window;
629 } else {
630 // Something went wrong after we connected to the new window, so
631 // disconnect here.
632 disconnectWindow(window);
633 }
634
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800635 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636}
637
Jamie Gennisf1072002011-07-13 15:13:14 -0700638// set the Surface that the preview will use
639status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
640 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
641
642 sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
643 sp<ANativeWindow> window(surface);
644 return setPreviewWindow(binder, window);
645}
646
Jamie Gennisff2dc462010-12-20 11:51:31 -0800647// set the SurfaceTexture that the preview will use
648status_t CameraService::Client::setPreviewTexture(
649 const sp<ISurfaceTexture>& surfaceTexture) {
650 LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
651 getCallingPid());
Jamie Gennisff2dc462010-12-20 11:51:31 -0800652
Jamie Gennisf1072002011-07-13 15:13:14 -0700653 sp<IBinder> binder;
654 sp<ANativeWindow> window;
Jamie Gennisff2dc462010-12-20 11:51:31 -0800655 if (surfaceTexture != 0) {
Jamie Gennisf1072002011-07-13 15:13:14 -0700656 binder = surfaceTexture->asBinder();
657 window = new SurfaceTextureClient(surfaceTexture);
Jamie Gennisff2dc462010-12-20 11:51:31 -0800658 }
Jamie Gennisf1072002011-07-13 15:13:14 -0700659 return setPreviewWindow(binder, window);
Jamie Gennisff2dc462010-12-20 11:51:31 -0800660}
661
Chih-Chung Change25cc652010-05-06 16:36:58 +0800662// set the preview callback flag to affect how the received frames from
663// preview are handled.
664void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
665 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
666 Mutex::Autolock lock(mLock);
667 if (checkPidAndHardware() != NO_ERROR) return;
668
669 mPreviewCallbackFlag = callback_flag;
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700670 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
Wu-cheng Lid02c8122010-09-03 16:40:32 -0700671 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
672 } else {
673 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800674 }
675}
676
677// start preview mode
678status_t CameraService::Client::startPreview() {
679 LOG1("startPreview (pid %d)", getCallingPid());
680 return startCameraMode(CAMERA_PREVIEW_MODE);
681}
682
683// start recording mode
684status_t CameraService::Client::startRecording() {
685 LOG1("startRecording (pid %d)", getCallingPid());
686 return startCameraMode(CAMERA_RECORDING_MODE);
687}
688
689// start preview or recording
690status_t CameraService::Client::startCameraMode(camera_mode mode) {
691 LOG1("startCameraMode(%d)", mode);
692 Mutex::Autolock lock(mLock);
693 status_t result = checkPidAndHardware();
694 if (result != NO_ERROR) return result;
695
696 switch(mode) {
697 case CAMERA_PREVIEW_MODE:
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700698 if (mSurface == 0 && mPreviewWindow == 0) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800699 LOG1("mSurface is not set yet.");
700 // still able to start preview in this case.
701 }
702 return startPreviewMode();
703 case CAMERA_RECORDING_MODE:
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700704 if (mSurface == 0 && mPreviewWindow == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000705 ALOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Chih-Chung Change25cc652010-05-06 16:36:58 +0800706 return INVALID_OPERATION;
707 }
708 return startRecordingMode();
709 default:
710 return UNKNOWN_ERROR;
711 }
712}
713
714status_t CameraService::Client::startPreviewMode() {
715 LOG1("startPreviewMode");
716 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717
718 // if preview has been enabled, nothing needs to be done
719 if (mHardware->previewEnabled()) {
720 return NO_ERROR;
721 }
722
Mathias Agopiand2112302010-12-07 19:38:17 -0800723 if (mPreviewWindow != 0) {
Mathias Agopianff86f372011-07-18 16:15:08 -0700724 native_window_set_scaling_mode(mPreviewWindow.get(),
725 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopiand2112302010-12-07 19:38:17 -0800726 native_window_set_buffers_transform(mPreviewWindow.get(),
727 mOrientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
Mathias Agopiand2112302010-12-07 19:38:17 -0800729 mHardware->setPreviewWindow(mPreviewWindow);
730 result = mHardware->startPreview();
731
Chih-Chung Change25cc652010-05-06 16:36:58 +0800732 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733}
734
Chih-Chung Change25cc652010-05-06 16:36:58 +0800735status_t CameraService::Client::startRecordingMode() {
736 LOG1("startRecordingMode");
737 status_t result = NO_ERROR;
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700738
Chih-Chung Change25cc652010-05-06 16:36:58 +0800739 // if recording has been enabled, nothing needs to be done
740 if (mHardware->recordingEnabled()) {
741 return NO_ERROR;
742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743
Chih-Chung Change25cc652010-05-06 16:36:58 +0800744 // if preview has not been started, start preview first
745 if (!mHardware->previewEnabled()) {
746 result = startPreviewMode();
747 if (result != NO_ERROR) {
748 return result;
Eric Laurent524dc042009-11-27 05:07:55 -0800749 }
Jason Samsb18b6912009-03-24 20:21:36 -0700750 }
Benny Wongda83f462009-08-12 12:01:27 -0500751
Chih-Chung Change25cc652010-05-06 16:36:58 +0800752 // start recording mode
753 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
754 mCameraService->playSound(SOUND_RECORDING);
755 result = mHardware->startRecording();
756 if (result != NO_ERROR) {
Steve Block3762c312012-01-06 19:20:56 +0000757 ALOGE("mHardware->startRecording() failed with status %d", result);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800758 }
759 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760}
761
762// stop preview mode
Chih-Chung Change25cc652010-05-06 16:36:58 +0800763void CameraService::Client::stopPreview() {
764 LOG1("stopPreview (pid %d)", getCallingPid());
765 Mutex::Autolock lock(mLock);
766 if (checkPidAndHardware() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700768
Chih-Chung Change25cc652010-05-06 16:36:58 +0800769 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
770 mHardware->stopPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771
Chih-Chung Change25cc652010-05-06 16:36:58 +0800772 mPreviewBuffer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773}
774
775// stop recording mode
Chih-Chung Change25cc652010-05-06 16:36:58 +0800776void CameraService::Client::stopRecording() {
777 LOG1("stopRecording (pid %d)", getCallingPid());
778 Mutex::Autolock lock(mLock);
779 if (checkPidAndHardware() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780
Chih-Chung Change25cc652010-05-06 16:36:58 +0800781 mCameraService->playSound(SOUND_RECORDING);
782 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
783 mHardware->stopRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784
Chih-Chung Change25cc652010-05-06 16:36:58 +0800785 mPreviewBuffer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786}
787
788// release a recording frame
Chih-Chung Change25cc652010-05-06 16:36:58 +0800789void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800791 if (checkPidAndHardware() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 mHardware->releaseRecordingFrame(mem);
793}
794
James Dong38311852010-10-18 20:42:51 -0700795status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
796{
797 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
798 Mutex::Autolock lock(mLock);
799 if (checkPidAndHardware() != NO_ERROR) {
800 return UNKNOWN_ERROR;
801 }
802 return mHardware->storeMetaDataInBuffers(enabled);
803}
804
Chih-Chung Change25cc652010-05-06 16:36:58 +0800805bool CameraService::Client::previewEnabled() {
806 LOG1("previewEnabled (pid %d)", getCallingPid());
807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800809 if (checkPidAndHardware() != NO_ERROR) return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 return mHardware->previewEnabled();
811}
812
Chih-Chung Change25cc652010-05-06 16:36:58 +0800813bool CameraService::Client::recordingEnabled() {
814 LOG1("recordingEnabled (pid %d)", getCallingPid());
815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800817 if (checkPidAndHardware() != NO_ERROR) return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 return mHardware->recordingEnabled();
819}
820
Chih-Chung Change25cc652010-05-06 16:36:58 +0800821status_t CameraService::Client::autoFocus() {
822 LOG1("autoFocus (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823
824 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800825 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 if (result != NO_ERROR) return result;
827
Benny Wongda83f462009-08-12 12:01:27 -0500828 return mHardware->autoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829}
830
Chih-Chung Change25cc652010-05-06 16:36:58 +0800831status_t CameraService::Client::cancelAutoFocus() {
832 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800833
834 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800835 status_t result = checkPidAndHardware();
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800836 if (result != NO_ERROR) return result;
837
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800838 return mHardware->cancelAutoFocus();
839}
840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841// take a picture - image is returned in callback
James Donge00cab72011-02-17 16:38:06 -0800842status_t CameraService::Client::takePicture(int msgType) {
843 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844
845 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800846 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 if (result != NO_ERROR) return result;
848
James Donge00cab72011-02-17 16:38:06 -0800849 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
850 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
Steve Block3762c312012-01-06 19:20:56 +0000851 ALOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
James Donge00cab72011-02-17 16:38:06 -0800852 " cannot be both enabled");
853 return BAD_VALUE;
854 }
855
856 // We only accept picture related message types
857 // and ignore other types of messages for takePicture().
858 int picMsgType = msgType
859 & (CAMERA_MSG_SHUTTER |
860 CAMERA_MSG_POSTVIEW_FRAME |
861 CAMERA_MSG_RAW_IMAGE |
862 CAMERA_MSG_RAW_IMAGE_NOTIFY |
863 CAMERA_MSG_COMPRESSED_IMAGE);
864
865 enableMsgType(picMsgType);
Benny Wongda83f462009-08-12 12:01:27 -0500866
867 return mHardware->takePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868}
869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870// set preview/capture parameters - key/value pairs
Chih-Chung Change25cc652010-05-06 16:36:58 +0800871status_t CameraService::Client::setParameters(const String8& params) {
872 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873
874 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800875 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 if (result != NO_ERROR) return result;
877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 CameraParameters p(params);
James Dong6085b4e2009-09-13 17:10:24 -0700879 return mHardware->setParameters(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880}
881
882// get preview/capture parameters - key/value pairs
Chih-Chung Change25cc652010-05-06 16:36:58 +0800883String8 CameraService::Client::getParameters() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800885 if (checkPidAndHardware() != NO_ERROR) return String8();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886
Wu-cheng Liab5b4242009-04-22 16:21:26 +0800887 String8 params(mHardware->getParameters().flatten());
Chih-Chung Change25cc652010-05-06 16:36:58 +0800888 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
Wu-cheng Liab5b4242009-04-22 16:21:26 +0800889 return params;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890}
891
Nipun Kwatra088146a2010-09-11 19:31:10 -0700892// enable shutter sound
893status_t CameraService::Client::enableShutterSound(bool enable) {
894 LOG1("enableShutterSound (pid %d)", getCallingPid());
895
896 status_t result = checkPidAndHardware();
897 if (result != NO_ERROR) return result;
898
899 if (enable) {
900 mPlayShutterSound = true;
901 return OK;
902 }
903
904 // Disabling shutter sound may not be allowed. In that case only
905 // allow the mediaserver process to disable the sound.
906 char value[PROPERTY_VALUE_MAX];
907 property_get("ro.camera.sound.forced", value, "0");
908 if (strcmp(value, "0") != 0) {
909 // Disabling shutter sound is not allowed. Deny if the current
910 // process is not mediaserver.
911 if (getCallingPid() != getpid()) {
Steve Block3762c312012-01-06 19:20:56 +0000912 ALOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
Nipun Kwatra088146a2010-09-11 19:31:10 -0700913 return PERMISSION_DENIED;
914 }
915 }
916
917 mPlayShutterSound = false;
918 return OK;
919}
920
Chih-Chung Change25cc652010-05-06 16:36:58 +0800921status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
922 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Lib3347bc2010-09-23 17:17:43 -0700923 int orientation;
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700924 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800925 status_t result = checkPidAndHardware();
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700926 if (result != NO_ERROR) return result;
927
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800928 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
Wu-cheng Lid55f7e52010-10-14 20:17:44 +0800929 // Mirror the preview if the camera is front-facing.
930 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
931 if (orientation == -1) return BAD_VALUE;
932
Wu-cheng Lib3347bc2010-09-23 17:17:43 -0700933 if (mOrientation != orientation) {
934 mOrientation = orientation;
Wu-cheng Lid3033622011-10-07 13:13:54 +0800935 if (mPreviewWindow != 0) {
936 native_window_set_buffers_transform(mPreviewWindow.get(),
937 mOrientation);
938 }
Wu-cheng Lib3347bc2010-09-23 17:17:43 -0700939 }
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800940 return OK;
Nipun Kwatra088146a2010-09-11 19:31:10 -0700941 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
942 switch (arg1) {
943 case 0:
944 enableShutterSound(false);
945 break;
946 case 1:
947 enableShutterSound(true);
948 break;
949 default:
950 return BAD_VALUE;
951 }
952 return OK;
Nipun Kwatra4e94c302010-09-14 16:49:08 -0700953 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
954 mCameraService->playSound(SOUND_RECORDING);
Wu-cheng Lia1c41e12012-02-23 19:01:00 -0800955 } else if (cmd == CAMERA_CMD_PING) {
956 // If mHardware is 0, checkPidAndHardware will return error.
957 return OK;
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800958 }
959
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700960 return mHardware->sendCommand(cmd, arg1, arg2);
961}
962
Chih-Chung Change25cc652010-05-06 16:36:58 +0800963// ----------------------------------------------------------------------------
964
965void CameraService::Client::enableMsgType(int32_t msgType) {
966 android_atomic_or(msgType, &mMsgEnabled);
967 mHardware->enableMsgType(msgType);
968}
969
970void CameraService::Client::disableMsgType(int32_t msgType) {
971 android_atomic_and(~msgType, &mMsgEnabled);
972 mHardware->disableMsgType(msgType);
973}
974
975#define CHECK_MESSAGE_INTERVAL 10 // 10ms
976bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
977 int sleepCount = 0;
978 while (mMsgEnabled & msgType) {
979 if (mLock.tryLock() == NO_ERROR) {
980 if (sleepCount > 0) {
981 LOG1("lockIfMessageWanted(%d): waited for %d ms",
982 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
983 }
984 return true;
985 }
986 if (sleepCount++ == 0) {
987 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
988 }
989 usleep(CHECK_MESSAGE_INTERVAL * 1000);
990 }
Steve Block8564c8d2012-01-05 23:22:43 +0000991 ALOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800992 return false;
993}
994
995// ----------------------------------------------------------------------------
996
997// Converts from a raw pointer to the client to a strong pointer during a
998// hardware callback. This requires the callbacks only happen when the client
999// is still alive.
1000sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
1001 sp<Client> client = gCameraService->getClientById((int) user);
1002
1003 // This could happen if the Client is in the process of shutting down (the
1004 // last strong reference is gone, but the destructor hasn't finished
1005 // stopping the hardware).
1006 if (client == 0) return NULL;
1007
1008 // The checks below are not necessary and are for debugging only.
1009 if (client->mCameraService.get() != gCameraService) {
Steve Block3762c312012-01-06 19:20:56 +00001010 ALOGE("mismatch service!");
Chih-Chung Change25cc652010-05-06 16:36:58 +08001011 return NULL;
1012 }
1013
1014 if (client->mHardware == 0) {
Steve Block3762c312012-01-06 19:20:56 +00001015 ALOGE("mHardware == 0: callback after disconnect()?");
Chih-Chung Change25cc652010-05-06 16:36:58 +08001016 return NULL;
1017 }
1018
1019 return client;
1020}
1021
1022// Callback messages can be dispatched to internal handlers or pass to our
1023// client's callback functions, depending on the message type.
1024//
1025// notifyCallback:
1026// CAMERA_MSG_SHUTTER handleShutter
1027// (others) c->notifyCallback
1028// dataCallback:
1029// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
1030// CAMERA_MSG_POSTVIEW_FRAME handlePostview
1031// CAMERA_MSG_RAW_IMAGE handleRawPicture
1032// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
1033// (others) c->dataCallback
1034// dataCallbackTimestamp
1035// (others) c->dataCallbackTimestamp
1036//
1037// NOTE: the *Callback functions grab mLock of the client before passing
1038// control to handle* functions. So the handle* functions must release the
1039// lock before calling the ICameraClient's callbacks, so those callbacks can
1040// invoke methods in the Client class again (For example, the preview frame
1041// callback may want to releaseRecordingFrame). The handle* functions must
1042// release the lock after all accesses to member variables, so it must be
1043// handled very carefully.
1044
1045void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
1046 int32_t ext2, void* user) {
1047 LOG2("notifyCallback(%d)", msgType);
1048
1049 sp<Client> client = getClientFromCookie(user);
1050 if (client == 0) return;
1051 if (!client->lockIfMessageWanted(msgType)) return;
1052
1053 switch (msgType) {
1054 case CAMERA_MSG_SHUTTER:
1055 // ext1 is the dimension of the yuv picture.
Iliyan Malchev40c36412011-03-28 16:10:12 -07001056 client->handleShutter();
Chih-Chung Change25cc652010-05-06 16:36:58 +08001057 break;
1058 default:
1059 client->handleGenericNotify(msgType, ext1, ext2);
1060 break;
1061 }
1062}
1063
1064void CameraService::Client::dataCallback(int32_t msgType,
Wu-cheng Lif0d6a482011-07-28 05:30:59 +08001065 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001066 LOG2("dataCallback(%d)", msgType);
1067
1068 sp<Client> client = getClientFromCookie(user);
1069 if (client == 0) return;
1070 if (!client->lockIfMessageWanted(msgType)) return;
1071
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001072 if (dataPtr == 0 && metadata == NULL) {
Steve Block3762c312012-01-06 19:20:56 +00001073 ALOGE("Null data returned in data callback");
Chih-Chung Change25cc652010-05-06 16:36:58 +08001074 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1075 return;
1076 }
1077
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001078 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001079 case CAMERA_MSG_PREVIEW_FRAME:
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001080 client->handlePreviewData(msgType, dataPtr, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001081 break;
1082 case CAMERA_MSG_POSTVIEW_FRAME:
1083 client->handlePostview(dataPtr);
1084 break;
1085 case CAMERA_MSG_RAW_IMAGE:
1086 client->handleRawPicture(dataPtr);
1087 break;
1088 case CAMERA_MSG_COMPRESSED_IMAGE:
1089 client->handleCompressedPicture(dataPtr);
1090 break;
1091 default:
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001092 client->handleGenericData(msgType, dataPtr, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001093 break;
1094 }
1095}
1096
1097void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
1098 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
1099 LOG2("dataCallbackTimestamp(%d)", msgType);
1100
1101 sp<Client> client = getClientFromCookie(user);
1102 if (client == 0) return;
James Dong74920cb2010-12-09 11:08:14 -08001103 if (!client->lockIfMessageWanted(msgType)) return;
Chih-Chung Change25cc652010-05-06 16:36:58 +08001104
1105 if (dataPtr == 0) {
Steve Block3762c312012-01-06 19:20:56 +00001106 ALOGE("Null data returned in data with timestamp callback");
Chih-Chung Change25cc652010-05-06 16:36:58 +08001107 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1108 return;
1109 }
1110
1111 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
1112}
1113
1114// snapshot taken callback
Iliyan Malchev40c36412011-03-28 16:10:12 -07001115void CameraService::Client::handleShutter(void) {
Nipun Kwatra088146a2010-09-11 19:31:10 -07001116 if (mPlayShutterSound) {
1117 mCameraService->playSound(SOUND_SHUTTER);
1118 }
Chih-Chung Change25cc652010-05-06 16:36:58 +08001119
Chih-Chung Change25cc652010-05-06 16:36:58 +08001120 sp<ICameraClient> c = mCameraClient;
1121 if (c != 0) {
1122 mLock.unlock();
1123 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1124 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1125 }
1126 disableMsgType(CAMERA_MSG_SHUTTER);
1127
Chih-Chung Change25cc652010-05-06 16:36:58 +08001128 mLock.unlock();
1129}
1130
1131// preview callback - frame buffer update
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001132void CameraService::Client::handlePreviewData(int32_t msgType,
1133 const sp<IMemory>& mem,
1134 camera_frame_metadata_t *metadata) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001135 ssize_t offset;
1136 size_t size;
1137 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1138
Chih-Chung Change25cc652010-05-06 16:36:58 +08001139 // local copy of the callback flags
1140 int flags = mPreviewCallbackFlag;
1141
1142 // is callback enabled?
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -07001143 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001144 // If the enable bit is off, the copy-out and one-shot bits are ignored
1145 LOG2("frame callback is disabled");
1146 mLock.unlock();
1147 return;
1148 }
1149
1150 // hold a strong pointer to the client
1151 sp<ICameraClient> c = mCameraClient;
1152
1153 // clear callback flags if no client or one-shot mode
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -07001154 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001155 LOG2("Disable preview callback");
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -07001156 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1157 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1158 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Lid02c8122010-09-03 16:40:32 -07001159 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001160 }
1161
1162 if (c != 0) {
1163 // Is the received frame copied out or not?
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -07001164 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001165 LOG2("frame is copied");
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001166 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001167 } else {
1168 LOG2("frame is forwarded");
1169 mLock.unlock();
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001170 c->dataCallback(msgType, mem, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001171 }
1172 } else {
1173 mLock.unlock();
1174 }
1175}
1176
1177// picture callback - postview image ready
1178void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1179 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1180
1181 sp<ICameraClient> c = mCameraClient;
1182 mLock.unlock();
1183 if (c != 0) {
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001184 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001185 }
1186}
1187
1188// picture callback - raw image ready
1189void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1190 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1191
1192 ssize_t offset;
1193 size_t size;
1194 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1195
Chih-Chung Change25cc652010-05-06 16:36:58 +08001196 sp<ICameraClient> c = mCameraClient;
1197 mLock.unlock();
1198 if (c != 0) {
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001199 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001200 }
1201}
1202
1203// picture callback - compressed picture ready
1204void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1205 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1206
1207 sp<ICameraClient> c = mCameraClient;
1208 mLock.unlock();
1209 if (c != 0) {
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001210 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001211 }
1212}
1213
1214
1215void CameraService::Client::handleGenericNotify(int32_t msgType,
1216 int32_t ext1, int32_t ext2) {
1217 sp<ICameraClient> c = mCameraClient;
1218 mLock.unlock();
1219 if (c != 0) {
1220 c->notifyCallback(msgType, ext1, ext2);
1221 }
1222}
1223
1224void CameraService::Client::handleGenericData(int32_t msgType,
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001225 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001226 sp<ICameraClient> c = mCameraClient;
1227 mLock.unlock();
1228 if (c != 0) {
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001229 c->dataCallback(msgType, dataPtr, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001230 }
1231}
1232
1233void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1234 int32_t msgType, const sp<IMemory>& dataPtr) {
1235 sp<ICameraClient> c = mCameraClient;
1236 mLock.unlock();
1237 if (c != 0) {
1238 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1239 }
1240}
1241
1242void CameraService::Client::copyFrameAndPostCopiedFrame(
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001243 int32_t msgType, const sp<ICameraClient>& client,
1244 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1245 camera_frame_metadata_t *metadata) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001246 LOG2("copyFrameAndPostCopiedFrame");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 // It is necessary to copy out of pmem before sending this to
1248 // the callback. For efficiency, reuse the same MemoryHeapBase
1249 // provided it's big enough. Don't allocate the memory or
1250 // perform the copy if there's no callback.
Dave Sparks05fd0df2009-11-10 17:08:08 -08001251 // hold the preview lock while we grab a reference to the preview buffer
Dave Sparksc8093c12009-11-06 11:47:13 -08001252 sp<MemoryHeapBase> previewBuffer;
Chih-Chung Change25cc652010-05-06 16:36:58 +08001253
1254 if (mPreviewBuffer == 0) {
1255 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1256 } else if (size > mPreviewBuffer->virtualSize()) {
1257 mPreviewBuffer.clear();
1258 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
Chih-Chung Change25cc652010-05-06 16:36:58 +08001260 if (mPreviewBuffer == 0) {
Steve Block3762c312012-01-06 19:20:56 +00001261 ALOGE("failed to allocate space for preview buffer");
Chih-Chung Change25cc652010-05-06 16:36:58 +08001262 mLock.unlock();
1263 return;
1264 }
1265 previewBuffer = mPreviewBuffer;
1266
1267 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268
Dave Sparksc8093c12009-11-06 11:47:13 -08001269 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 if (frame == 0) {
Steve Block3762c312012-01-06 19:20:56 +00001271 ALOGE("failed to allocate space for frame callback");
Chih-Chung Change25cc652010-05-06 16:36:58 +08001272 mLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 return;
1274 }
Chih-Chung Change25cc652010-05-06 16:36:58 +08001275
1276 mLock.unlock();
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001277 client->dataCallback(msgType, frame, metadata);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278}
1279
Wu-cheng Lid55f7e52010-10-14 20:17:44 +08001280int CameraService::Client::getOrientation(int degrees, bool mirror) {
1281 if (!mirror) {
1282 if (degrees == 0) return 0;
1283 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1284 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1285 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1286 } else { // Do mirror (horizontal flip)
1287 if (degrees == 0) { // FLIP_H and ROT_0
1288 return HAL_TRANSFORM_FLIP_H;
1289 } else if (degrees == 90) { // FLIP_H and ROT_90
1290 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1291 } else if (degrees == 180) { // FLIP_H and ROT_180
1292 return HAL_TRANSFORM_FLIP_V;
1293 } else if (degrees == 270) { // FLIP_H and ROT_270
1294 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1295 }
1296 }
Steve Block3762c312012-01-06 19:20:56 +00001297 ALOGE("Invalid setDisplayOrientation degrees=%d", degrees);
Wu-cheng Lid55f7e52010-10-14 20:17:44 +08001298 return -1;
1299}
1300
Wu-cheng Lia1c41e12012-02-23 19:01:00 -08001301// Whether the client wants to keep the camera from taking
1302bool CameraService::Client::keep() const {
1303 return mKeep;
1304}
Wu-cheng Lid55f7e52010-10-14 20:17:44 +08001305
Chih-Chung Change25cc652010-05-06 16:36:58 +08001306// ----------------------------------------------------------------------------
1307
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001308static const int kDumpLockRetries = 50;
1309static const int kDumpLockSleep = 60000;
1310
1311static bool tryLock(Mutex& mutex)
1312{
1313 bool locked = false;
1314 for (int i = 0; i < kDumpLockRetries; ++i) {
1315 if (mutex.tryLock() == NO_ERROR) {
1316 locked = true;
1317 break;
1318 }
1319 usleep(kDumpLockSleep);
1320 }
1321 return locked;
1322}
1323
Chih-Chung Change25cc652010-05-06 16:36:58 +08001324status_t CameraService::dump(int fd, const Vector<String16>& args) {
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001325 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 const size_t SIZE = 256;
1328 char buffer[SIZE];
1329 String8 result;
1330 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1331 snprintf(buffer, SIZE, "Permission Denial: "
1332 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +08001333 getCallingPid(),
Chih-Chung Change25cc652010-05-06 16:36:58 +08001334 getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 result.append(buffer);
1336 write(fd, result.string(), result.size());
1337 } else {
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001338 bool locked = tryLock(mServiceLock);
1339 // failed to lock - CameraService is probably deadlocked
1340 if (!locked) {
1341 String8 result(kDeadlockedString);
1342 write(fd, result.string(), result.size());
1343 }
1344
Chih-Chung Change25cc652010-05-06 16:36:58 +08001345 bool hasClient = false;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001346 for (int i = 0; i < mNumberOfCameras; i++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001347 sp<Client> client = mClient[i].promote();
1348 if (client == 0) continue;
1349 hasClient = true;
1350 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1351 i,
1352 client->getCameraClient()->asBinder().get(),
1353 client->mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 result.append(buffer);
1355 write(fd, result.string(), result.size());
Chih-Chung Change25cc652010-05-06 16:36:58 +08001356 client->mHardware->dump(fd, args);
1357 }
1358 if (!hasClient) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 result.append("No camera client yet.\n");
1360 write(fd, result.string(), result.size());
1361 }
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001362
1363 if (locked) mServiceLock.unlock();
Chih-Chung Change25cc652010-05-06 16:36:58 +08001364
1365 // change logging level
1366 int n = args.size();
1367 for (int i = 0; i + 1 < n; i++) {
1368 if (args[i] == String16("-v")) {
1369 String8 levelStr(args[i+1]);
1370 int level = atoi(levelStr.string());
1371 sprintf(buffer, "Set Log Level to %d", level);
1372 result.append(buffer);
1373 setLogLevel(level);
1374 }
1375 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 }
1377 return NO_ERROR;
1378}
1379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380}; // namespace android