blob: 05e7bcfc2ee5b64871f2d8d9481b446a52706cbd [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>
24
Mathias Agopian07952722009-05-19 19:08:10 -070025#include <binder/IPCThreadState.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080026#include <binder/IServiceManager.h>
Mathias Agopian07952722009-05-19 19:08:10 -070027#include <binder/MemoryBase.h>
28#include <binder/MemoryHeapBase.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080029#include <cutils/atomic.h>
Nipun Kwatra088146a2010-09-11 19:31:10 -070030#include <cutils/properties.h>
Jamie Gennisff2dc462010-12-20 11:51:31 -080031#include <gui/SurfaceTextureClient.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080032#include <hardware/hardware.h>
33#include <media/AudioSystem.h>
34#include <media/mediaplayer.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080035#include <surfaceflinger/ISurface.h>
Chih-Chung Change25cc652010-05-06 16:36:58 +080036#include <utils/Errors.h>
37#include <utils/Log.h>
38#include <utils/String16.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
40#include "CameraService.h"
Iliyan Malcheva269b872011-04-14 16:55:59 -070041#include "CameraHardwareInterface.h"
Eric Laurenta7f1e5c2009-03-27 16:27:16 -070042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043namespace android {
44
Chih-Chung Change25cc652010-05-06 16:36:58 +080045// ----------------------------------------------------------------------------
46// Logging support -- this is for debugging only
47// Use "adb shell dumpsys media.camera -v 1" to change it.
48static volatile int32_t gLogLevel = 0;
49
50#define LOG1(...) LOGD_IF(gLogLevel >= 1, __VA_ARGS__);
51#define LOG2(...) LOGD_IF(gLogLevel >= 2, __VA_ARGS__);
52
53static void setLogLevel(int level) {
54 android_atomic_write(level, &gLogLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055}
56
Chih-Chung Change25cc652010-05-06 16:36:58 +080057// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +080059static int getCallingPid() {
60 return IPCThreadState::self()->getCallingPid();
61}
62
Chih-Chung Change25cc652010-05-06 16:36:58 +080063static int getCallingUid() {
64 return IPCThreadState::self()->getCallingUid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065}
66
67// ----------------------------------------------------------------------------
68
Chih-Chung Change25cc652010-05-06 16:36:58 +080069// This is ugly and only safe if we never re-create the CameraService, but
70// should be ok for now.
71static CameraService *gCameraService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
Chih-Chung Change25cc652010-05-06 16:36:58 +080073CameraService::CameraService()
Iliyan Malcheva269b872011-04-14 16:55:59 -070074:mSoundRef(0), mModule(0)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075{
Chih-Chung Change25cc652010-05-06 16:36:58 +080076 LOGI("CameraService started (pid=%d)", getpid());
Chih-Chung Change25cc652010-05-06 16:36:58 +080077 gCameraService = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078}
79
Iliyan Malcheva269b872011-04-14 16:55:59 -070080void CameraService::onFirstRef()
81{
82 BnCameraService::onFirstRef();
83
84 if (hw_get_module(CAMERA_HARDWARE_MODULE_ID,
85 (const hw_module_t **)&mModule) < 0) {
86 LOGE("Could not load camera HAL module");
87 mNumberOfCameras = 0;
88 }
89 else {
90 mNumberOfCameras = mModule->get_number_of_cameras();
91 if (mNumberOfCameras > MAX_CAMERAS) {
92 LOGE("Number of cameras(%d) > MAX_CAMERAS(%d).",
93 mNumberOfCameras, MAX_CAMERAS);
94 mNumberOfCameras = MAX_CAMERAS;
95 }
96 for (int i = 0; i < mNumberOfCameras; i++) {
97 setCameraFree(i);
98 }
99 }
100}
101
Chih-Chung Change25cc652010-05-06 16:36:58 +0800102CameraService::~CameraService() {
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800103 for (int i = 0; i < mNumberOfCameras; i++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800104 if (mBusy[i]) {
105 LOGE("camera %d is still in use in destructor!", i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 }
107 }
108
Chih-Chung Change25cc652010-05-06 16:36:58 +0800109 gCameraService = NULL;
110}
111
112int32_t CameraService::getNumberOfCameras() {
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800113 return mNumberOfCameras;
114}
115
116status_t CameraService::getCameraInfo(int cameraId,
117 struct CameraInfo* cameraInfo) {
Iliyan Malcheva269b872011-04-14 16:55:59 -0700118 if (!mModule) {
119 return NO_INIT;
120 }
121
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800122 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
123 return BAD_VALUE;
124 }
125
Iliyan Malcheva269b872011-04-14 16:55:59 -0700126 struct camera_info info;
127 status_t rc = mModule->get_camera_info(cameraId, &info);
128 cameraInfo->facing = info.facing;
129 cameraInfo->orientation = info.orientation;
130 return rc;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800131}
132
133sp<ICamera> CameraService::connect(
134 const sp<ICameraClient>& cameraClient, int cameraId) {
135 int callingPid = getCallingPid();
136 LOG1("CameraService::connect E (pid %d, id %d)", callingPid, cameraId);
137
Iliyan Malcheva269b872011-04-14 16:55:59 -0700138 if (!mModule) {
139 LOGE("Camera HAL module not loaded");
140 return NULL;
141 }
142
Chih-Chung Change25cc652010-05-06 16:36:58 +0800143 sp<Client> client;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800144 if (cameraId < 0 || cameraId >= mNumberOfCameras) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800145 LOGE("CameraService::connect X (pid %d) rejected (invalid cameraId %d).",
146 callingPid, cameraId);
147 return NULL;
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800148 }
149
Wu-cheng Li04c453d2011-05-20 14:54:25 +0800150 char value[PROPERTY_VALUE_MAX];
151 property_get("sys.secpolicy.camera.disabled", value, "0");
152 if (strcmp(value, "1") == 0) {
153 // Camera is disabled by DevicePolicyManager.
154 LOGI("Camera is disabled. connect X (pid %d) rejected", callingPid);
155 return NULL;
156 }
157
Chih-Chung Change25cc652010-05-06 16:36:58 +0800158 Mutex::Autolock lock(mServiceLock);
159 if (mClient[cameraId] != 0) {
160 client = mClient[cameraId].promote();
161 if (client != 0) {
162 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
163 LOG1("CameraService::connect X (pid %d) (the same client)",
164 callingPid);
165 return client;
166 } else {
167 LOGW("CameraService::connect X (pid %d) rejected (existing client).",
168 callingPid);
169 return NULL;
170 }
171 }
172 mClient[cameraId].clear();
173 }
174
175 if (mBusy[cameraId]) {
176 LOGW("CameraService::connect X (pid %d) rejected"
177 " (camera %d is still busy).", callingPid, cameraId);
178 return NULL;
179 }
180
Iliyan Malcheva269b872011-04-14 16:55:59 -0700181 struct camera_info info;
182 if (mModule->get_camera_info(cameraId, &info) != OK) {
183 LOGE("Invalid camera id %d", cameraId);
Wu-cheng Lie7044382010-08-17 15:45:37 -0700184 return NULL;
185 }
Iliyan Malcheva269b872011-04-14 16:55:59 -0700186
187 char camera_device_name[10];
188 snprintf(camera_device_name, sizeof(camera_device_name), "%d", cameraId);
189
190 client = new Client(this, cameraClient,
191 new CameraHardwareInterface(&mModule->common,
192 camera_device_name),
193 cameraId, info.facing, callingPid);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800194 mClient[cameraId] = client;
195 LOG1("CameraService::connect X");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 return client;
197}
198
Chih-Chung Change25cc652010-05-06 16:36:58 +0800199void CameraService::removeClient(const sp<ICameraClient>& cameraClient) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800200 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800201 LOG1("CameraService::removeClient E (pid %d)", callingPid);
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800202
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800203 for (int i = 0; i < mNumberOfCameras; i++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800204 // Declare this before the lock to make absolutely sure the
205 // destructor won't be called with the lock held.
206 sp<Client> client;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207
Chih-Chung Change25cc652010-05-06 16:36:58 +0800208 Mutex::Autolock lock(mServiceLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209
Chih-Chung Change25cc652010-05-06 16:36:58 +0800210 // This happens when we have already disconnected (or this is
211 // just another unused camera).
212 if (mClient[i] == 0) continue;
213
214 // Promote mClient. It can fail if we are called from this path:
215 // Client::~Client() -> disconnect() -> removeClient().
216 client = mClient[i].promote();
217
218 if (client == 0) {
219 mClient[i].clear();
220 continue;
221 }
222
223 if (cameraClient->asBinder() == client->getCameraClient()->asBinder()) {
224 // Found our camera, clear and leave.
225 LOG1("removeClient: clear camera %d", i);
226 mClient[i].clear();
227 break;
228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 }
230
Chih-Chung Change25cc652010-05-06 16:36:58 +0800231 LOG1("CameraService::removeClient X (pid %d)", callingPid);
232}
233
234sp<CameraService::Client> CameraService::getClientById(int cameraId) {
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +0800235 if (cameraId < 0 || cameraId >= mNumberOfCameras) return NULL;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800236 return mClient[cameraId].promote();
237}
238
Chih-Chung Change25cc652010-05-06 16:36:58 +0800239status_t CameraService::onTransact(
240 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
241 // Permission checks
242 switch (code) {
243 case BnCameraService::CONNECT:
244 const int pid = getCallingPid();
245 const int self_pid = getpid();
246 if (pid != self_pid) {
247 // we're called from a different process, do the real check
248 if (!checkCallingPermission(
249 String16("android.permission.CAMERA"))) {
250 const int uid = getCallingUid();
251 LOGE("Permission Denial: "
252 "can't use the camera pid=%d, uid=%d", pid, uid);
253 return PERMISSION_DENIED;
254 }
255 }
256 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
258
Chih-Chung Change25cc652010-05-06 16:36:58 +0800259 return BnCameraService::onTransact(code, data, reply, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260}
261
Chih-Chung Change25cc652010-05-06 16:36:58 +0800262// The reason we need this busy bit is a new CameraService::connect() request
263// may come in while the previous Client's destructor has not been run or is
264// still running. If the last strong reference of the previous Client is gone
265// but the destructor has not been finished, we should not allow the new Client
266// to be created because we need to wait for the previous Client to tear down
267// the hardware first.
268void CameraService::setCameraBusy(int cameraId) {
269 android_atomic_write(1, &mBusy[cameraId]);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800270}
271
Chih-Chung Change25cc652010-05-06 16:36:58 +0800272void CameraService::setCameraFree(int cameraId) {
273 android_atomic_write(0, &mBusy[cameraId]);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800274}
275
Chih-Chung Change25cc652010-05-06 16:36:58 +0800276// We share the media players for shutter and recording sound for all clients.
277// A reference count is kept to determine when we will actually release the
278// media players.
279
280static MediaPlayer* newMediaPlayer(const char *file) {
Chih-Chung Chang9143aaa2011-10-07 13:37:42 +0800281 // Read the system property to determine if we have need to use the
282 // AUDIO_STREAM_ENFORCED_AUDIBLE type.
283 char value[PROPERTY_VALUE_MAX];
284 property_get("ro.camera.sound.forced", value, "0");
285 int audioStreamType;
286 if (strcmp(value, "0") != 0) {
287 audioStreamType = AUDIO_STREAM_ENFORCED_AUDIBLE;
288 } else {
289 audioStreamType = AUDIO_STREAM_MUSIC;
290 }
291
Chih-Chung Change25cc652010-05-06 16:36:58 +0800292 MediaPlayer* mp = new MediaPlayer();
293 if (mp->setDataSource(file, NULL) == NO_ERROR) {
Chih-Chung Chang9143aaa2011-10-07 13:37:42 +0800294 mp->setAudioStreamType(audioStreamType);
Jason Samsb18b6912009-03-24 20:21:36 -0700295 mp->prepare();
296 } else {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800297 LOGE("Failed to load CameraService sounds: %s", file);
298 return NULL;
Jason Samsb18b6912009-03-24 20:21:36 -0700299 }
300 return mp;
301}
302
Chih-Chung Change25cc652010-05-06 16:36:58 +0800303void CameraService::loadSound() {
304 Mutex::Autolock lock(mSoundLock);
305 LOG1("CameraService::loadSound ref=%d", mSoundRef);
306 if (mSoundRef++) return;
307
308 mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
309 mSoundPlayer[SOUND_RECORDING] = newMediaPlayer("/system/media/audio/ui/VideoRecord.ogg");
310}
311
312void CameraService::releaseSound() {
313 Mutex::Autolock lock(mSoundLock);
314 LOG1("CameraService::releaseSound ref=%d", mSoundRef);
315 if (--mSoundRef) return;
316
317 for (int i = 0; i < NUM_SOUNDS; i++) {
318 if (mSoundPlayer[i] != 0) {
319 mSoundPlayer[i]->disconnect();
320 mSoundPlayer[i].clear();
321 }
322 }
323}
324
325void CameraService::playSound(sound_kind kind) {
326 LOG1("playSound(%d)", kind);
327 Mutex::Autolock lock(mSoundLock);
328 sp<MediaPlayer> player = mSoundPlayer[kind];
329 if (player != 0) {
330 // do not play the sound if stream volume is 0
331 // (typically because ringer mode is silent).
332 int index;
Dima Zavin24fc2fb2011-04-19 22:30:36 -0700333 AudioSystem::getStreamVolumeIndex(AUDIO_STREAM_ENFORCED_AUDIBLE, &index);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800334 if (index != 0) {
335 player->seekTo(0);
336 player->start();
337 }
338 }
339}
340
341// ----------------------------------------------------------------------------
342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343CameraService::Client::Client(const sp<CameraService>& cameraService,
Wu-cheng Lie7044382010-08-17 15:45:37 -0700344 const sp<ICameraClient>& cameraClient,
345 const sp<CameraHardwareInterface>& hardware,
Wu-cheng Lid55f7e52010-10-14 20:17:44 +0800346 int cameraId, int cameraFacing, int clientPid) {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800347 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800348 LOG1("Client::Client E (pid %d)", callingPid);
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 mCameraService = cameraService;
351 mCameraClient = cameraClient;
Wu-cheng Lie7044382010-08-17 15:45:37 -0700352 mHardware = hardware;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800353 mCameraId = cameraId;
Wu-cheng Lid55f7e52010-10-14 20:17:44 +0800354 mCameraFacing = cameraFacing;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 mClientPid = clientPid;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800356 mMsgEnabled = 0;
Jamie Gennisff2dc462010-12-20 11:51:31 -0800357 mSurface = 0;
358 mPreviewWindow = 0;
Benny Wongda83f462009-08-12 12:01:27 -0500359 mHardware->setCallbacks(notifyCallback,
360 dataCallback,
361 dataCallbackTimestamp,
Chih-Chung Change25cc652010-05-06 16:36:58 +0800362 (void *)cameraId);
Benny Wongda83f462009-08-12 12:01:27 -0500363
Wu-cheng Libb1e2752011-07-30 05:00:37 +0800364 // Enable zoom, error, focus, and metadata messages by default
365 enableMsgType(CAMERA_MSG_ERROR | CAMERA_MSG_ZOOM | CAMERA_MSG_FOCUS |
366 CAMERA_MSG_PREVIEW_METADATA);
Jason Samsb18b6912009-03-24 20:21:36 -0700367
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 // Callback is disabled by default
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700369 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Wu-cheng Lid55f7e52010-10-14 20:17:44 +0800370 mOrientation = getOrientation(0, mCameraFacing == CAMERA_FACING_FRONT);
Nipun Kwatra088146a2010-09-11 19:31:10 -0700371 mPlayShutterSound = true;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800372 cameraService->setCameraBusy(cameraId);
373 cameraService->loadSound();
374 LOG1("Client::Client X (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375}
376
Chih-Chung Change25cc652010-05-06 16:36:58 +0800377// tear down the client
378CameraService::Client::~Client() {
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800379 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800380 LOG1("Client::~Client E (pid %d, this %p)", callingPid, this);
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800381
Chih-Chung Change25cc652010-05-06 16:36:58 +0800382 // set mClientPid to let disconnet() tear down the hardware
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800383 mClientPid = callingPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 disconnect();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800385 mCameraService->releaseSound();
386 LOG1("Client::~Client X (pid %d, this %p)", callingPid, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387}
388
Chih-Chung Change25cc652010-05-06 16:36:58 +0800389// ----------------------------------------------------------------------------
390
391status_t CameraService::Client::checkPid() const {
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800392 int callingPid = getCallingPid();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800393 if (callingPid == mClientPid) return NO_ERROR;
James Dong71d714c2010-06-09 15:57:48 -0700394
Chih-Chung Change25cc652010-05-06 16:36:58 +0800395 LOGW("attempt to use a locked camera from a different process"
396 " (old pid %d, new pid %d)", mClientPid, callingPid);
397 return EBUSY;
398}
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800399
Chih-Chung Change25cc652010-05-06 16:36:58 +0800400status_t CameraService::Client::checkPidAndHardware() const {
401 status_t result = checkPid();
402 if (result != NO_ERROR) return result;
403 if (mHardware == 0) {
404 LOGE("attempt to use a camera after disconnect() (pid %d)", getCallingPid());
405 return INVALID_OPERATION;
406 }
407 return NO_ERROR;
408}
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +0800409
Chih-Chung Change25cc652010-05-06 16:36:58 +0800410status_t CameraService::Client::lock() {
411 int callingPid = getCallingPid();
412 LOG1("lock (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800414
415 // lock camera to this client if the the camera is unlocked
416 if (mClientPid == 0) {
417 mClientPid = callingPid;
418 return NO_ERROR;
419 }
420
421 // returns NO_ERROR if the client already owns the camera, EBUSY otherwise
422 return checkPid();
423}
424
425status_t CameraService::Client::unlock() {
426 int callingPid = getCallingPid();
427 LOG1("unlock (pid %d)", callingPid);
428 Mutex::Autolock lock(mLock);
429
430 // allow anyone to use camera (after they lock the camera)
431 status_t result = checkPid();
432 if (result == NO_ERROR) {
Wu-cheng Li42419ce2011-06-01 17:22:24 +0800433 if (mHardware->recordingEnabled()) {
434 LOGE("Not allowed to unlock camera during recording.");
435 return INVALID_OPERATION;
436 }
Chih-Chung Change25cc652010-05-06 16:36:58 +0800437 mClientPid = 0;
438 LOG1("clear mCameraClient (pid %d)", callingPid);
439 // we need to remove the reference to ICameraClient so that when the app
440 // goes away, the reference count goes to 0.
441 mCameraClient.clear();
442 }
443 return result;
444}
445
446// connect a new client to the camera
447status_t CameraService::Client::connect(const sp<ICameraClient>& client) {
448 int callingPid = getCallingPid();
449 LOG1("connect E (pid %d)", callingPid);
450 Mutex::Autolock lock(mLock);
451
452 if (mClientPid != 0 && checkPid() != NO_ERROR) {
453 LOGW("Tried to connect to a locked camera (old pid %d, new pid %d)",
454 mClientPid, callingPid);
455 return EBUSY;
456 }
457
458 if (mCameraClient != 0 && (client->asBinder() == mCameraClient->asBinder())) {
459 LOG1("Connect to the same client");
460 return NO_ERROR;
461 }
462
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700463 mPreviewCallbackFlag = CAMERA_FRAME_CALLBACK_FLAG_NOOP;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800464 mClientPid = callingPid;
465 mCameraClient = client;
466
467 LOG1("connect X (pid %d)", callingPid);
468 return NO_ERROR;
469}
470
Wu-cheng Li13528f72011-07-20 05:35:02 +0800471static void disconnectWindow(const sp<ANativeWindow>& window) {
472 if (window != 0) {
Mathias Agopian982d2da2011-07-29 17:55:48 -0700473 status_t result = native_window_api_disconnect(window.get(),
Wu-cheng Li13528f72011-07-20 05:35:02 +0800474 NATIVE_WINDOW_API_CAMERA);
475 if (result != NO_ERROR) {
Mathias Agopian982d2da2011-07-29 17:55:48 -0700476 LOGW("native_window_api_disconnect failed: %s (%d)", strerror(-result),
Wu-cheng Li13528f72011-07-20 05:35:02 +0800477 result);
478 }
479 }
480}
481
Chih-Chung Change25cc652010-05-06 16:36:58 +0800482void CameraService::Client::disconnect() {
483 int callingPid = getCallingPid();
484 LOG1("disconnect E (pid %d)", callingPid);
485 Mutex::Autolock lock(mLock);
486
487 if (checkPid() != NO_ERROR) {
488 LOGW("different client - don't disconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 return;
490 }
Chih-Chung Change25cc652010-05-06 16:36:58 +0800491
492 if (mClientPid <= 0) {
493 LOG1("camera is unlocked (mClientPid = %d), don't tear down hardware", mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 return;
495 }
496
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800497 // Make sure disconnect() is done once and once only, whether it is called
498 // from the user directly, or called by the destructor.
499 if (mHardware == 0) return;
500
Chih-Chung Change25cc652010-05-06 16:36:58 +0800501 LOG1("hardware teardown");
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800502 // Before destroying mHardware, we must make sure it's in the
503 // idle state.
Chih-Chung Change25cc652010-05-06 16:36:58 +0800504 // Turn off all messages.
505 disableMsgType(CAMERA_MSG_ALL_MSGS);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800506 mHardware->stopPreview();
Benny Wongda83f462009-08-12 12:01:27 -0500507 mHardware->cancelPicture();
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800508 // Release the hardware resources.
509 mHardware->release();
Mathias Agopiand2112302010-12-07 19:38:17 -0800510
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700511 // Release the held ANativeWindow resources.
512 if (mPreviewWindow != 0) {
Wu-cheng Li13528f72011-07-20 05:35:02 +0800513 disconnectWindow(mPreviewWindow);
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700514 mPreviewWindow = 0;
515 mHardware->setPreviewWindow(mPreviewWindow);
516 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 mHardware.clear();
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800518
Chih-Chung Changd5d1ebd2009-06-24 19:59:31 +0800519 mCameraService->removeClient(mCameraClient);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800520 mCameraService->setCameraFree(mCameraId);
Chih-Chung Chang6fcba312009-06-24 13:44:37 +0800521
Chih-Chung Change25cc652010-05-06 16:36:58 +0800522 LOG1("disconnect X (pid %d)", callingPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523}
524
Chih-Chung Change25cc652010-05-06 16:36:58 +0800525// ----------------------------------------------------------------------------
526
Jamie Gennisf1072002011-07-13 15:13:14 -0700527status_t CameraService::Client::setPreviewWindow(const sp<IBinder>& binder,
528 const sp<ANativeWindow>& window) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800530 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 if (result != NO_ERROR) return result;
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800532
Chih-Chung Change25cc652010-05-06 16:36:58 +0800533 // return if no change in surface.
Mathias Agopianc8a04b52011-04-05 15:44:20 -0700534 if (binder == mSurface) {
Jamie Gennisf1072002011-07-13 15:13:14 -0700535 return NO_ERROR;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800536 }
537
Jamie Gennisf1072002011-07-13 15:13:14 -0700538 if (window != 0) {
Mathias Agopian982d2da2011-07-29 17:55:48 -0700539 result = native_window_api_connect(window.get(), NATIVE_WINDOW_API_CAMERA);
Jamie Gennisf1072002011-07-13 15:13:14 -0700540 if (result != NO_ERROR) {
Mathias Agopian982d2da2011-07-29 17:55:48 -0700541 LOGE("native_window_api_connect failed: %s (%d)", strerror(-result),
Jamie Gennisf1072002011-07-13 15:13:14 -0700542 result);
543 return result;
Chih-Chung Change25cc652010-05-06 16:36:58 +0800544 }
545 }
546
Jamie Gennisf1072002011-07-13 15:13:14 -0700547 // If preview has been already started, register preview buffers now.
548 if (mHardware->previewEnabled()) {
549 if (window != 0) {
Mathias Agopianff86f372011-07-18 16:15:08 -0700550 native_window_set_scaling_mode(window.get(),
551 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Jamie Gennisf1072002011-07-13 15:13:14 -0700552 native_window_set_buffers_transform(window.get(), mOrientation);
553 result = mHardware->setPreviewWindow(window);
554 }
555 }
556
557 if (result == NO_ERROR) {
558 // Everything has succeeded. Disconnect the old window and remember the
559 // new window.
560 disconnectWindow(mPreviewWindow);
561 mSurface = binder;
562 mPreviewWindow = window;
563 } else {
564 // Something went wrong after we connected to the new window, so
565 // disconnect here.
566 disconnectWindow(window);
567 }
568
Wu-cheng Lib8a10fe2009-06-23 23:37:36 +0800569 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570}
571
Jamie Gennisf1072002011-07-13 15:13:14 -0700572// set the Surface that the preview will use
573status_t CameraService::Client::setPreviewDisplay(const sp<Surface>& surface) {
574 LOG1("setPreviewDisplay(%p) (pid %d)", surface.get(), getCallingPid());
575
576 sp<IBinder> binder(surface != 0 ? surface->asBinder() : 0);
577 sp<ANativeWindow> window(surface);
578 return setPreviewWindow(binder, window);
579}
580
Jamie Gennisff2dc462010-12-20 11:51:31 -0800581// set the SurfaceTexture that the preview will use
582status_t CameraService::Client::setPreviewTexture(
583 const sp<ISurfaceTexture>& surfaceTexture) {
584 LOG1("setPreviewTexture(%p) (pid %d)", surfaceTexture.get(),
585 getCallingPid());
Jamie Gennisff2dc462010-12-20 11:51:31 -0800586
Jamie Gennisf1072002011-07-13 15:13:14 -0700587 sp<IBinder> binder;
588 sp<ANativeWindow> window;
Jamie Gennisff2dc462010-12-20 11:51:31 -0800589 if (surfaceTexture != 0) {
Jamie Gennisf1072002011-07-13 15:13:14 -0700590 binder = surfaceTexture->asBinder();
591 window = new SurfaceTextureClient(surfaceTexture);
Jamie Gennisff2dc462010-12-20 11:51:31 -0800592 }
Jamie Gennisf1072002011-07-13 15:13:14 -0700593 return setPreviewWindow(binder, window);
Jamie Gennisff2dc462010-12-20 11:51:31 -0800594}
595
Chih-Chung Change25cc652010-05-06 16:36:58 +0800596// set the preview callback flag to affect how the received frames from
597// preview are handled.
598void CameraService::Client::setPreviewCallbackFlag(int callback_flag) {
599 LOG1("setPreviewCallbackFlag(%d) (pid %d)", callback_flag, getCallingPid());
600 Mutex::Autolock lock(mLock);
601 if (checkPidAndHardware() != NO_ERROR) return;
602
603 mPreviewCallbackFlag = callback_flag;
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -0700604 if (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK) {
Wu-cheng Lid02c8122010-09-03 16:40:32 -0700605 enableMsgType(CAMERA_MSG_PREVIEW_FRAME);
606 } else {
607 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800608 }
609}
610
611// start preview mode
612status_t CameraService::Client::startPreview() {
613 LOG1("startPreview (pid %d)", getCallingPid());
614 return startCameraMode(CAMERA_PREVIEW_MODE);
615}
616
617// start recording mode
618status_t CameraService::Client::startRecording() {
619 LOG1("startRecording (pid %d)", getCallingPid());
620 return startCameraMode(CAMERA_RECORDING_MODE);
621}
622
623// start preview or recording
624status_t CameraService::Client::startCameraMode(camera_mode mode) {
625 LOG1("startCameraMode(%d)", mode);
626 Mutex::Autolock lock(mLock);
627 status_t result = checkPidAndHardware();
628 if (result != NO_ERROR) return result;
629
630 switch(mode) {
631 case CAMERA_PREVIEW_MODE:
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700632 if (mSurface == 0 && mPreviewWindow == 0) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800633 LOG1("mSurface is not set yet.");
634 // still able to start preview in this case.
635 }
636 return startPreviewMode();
637 case CAMERA_RECORDING_MODE:
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700638 if (mSurface == 0 && mPreviewWindow == 0) {
639 LOGE("mSurface or mPreviewWindow must be set before startRecordingMode.");
Chih-Chung Change25cc652010-05-06 16:36:58 +0800640 return INVALID_OPERATION;
641 }
642 return startRecordingMode();
643 default:
644 return UNKNOWN_ERROR;
645 }
646}
647
648status_t CameraService::Client::startPreviewMode() {
649 LOG1("startPreviewMode");
650 status_t result = NO_ERROR;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651
652 // if preview has been enabled, nothing needs to be done
653 if (mHardware->previewEnabled()) {
654 return NO_ERROR;
655 }
656
Mathias Agopiand2112302010-12-07 19:38:17 -0800657 if (mPreviewWindow != 0) {
Mathias Agopianff86f372011-07-18 16:15:08 -0700658 native_window_set_scaling_mode(mPreviewWindow.get(),
659 NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopiand2112302010-12-07 19:38:17 -0800660 native_window_set_buffers_transform(mPreviewWindow.get(),
661 mOrientation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 }
Mathias Agopiand2112302010-12-07 19:38:17 -0800663 mHardware->setPreviewWindow(mPreviewWindow);
664 result = mHardware->startPreview();
665
Chih-Chung Change25cc652010-05-06 16:36:58 +0800666 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667}
668
Chih-Chung Change25cc652010-05-06 16:36:58 +0800669status_t CameraService::Client::startRecordingMode() {
670 LOG1("startRecordingMode");
671 status_t result = NO_ERROR;
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700672
Chih-Chung Change25cc652010-05-06 16:36:58 +0800673 // if recording has been enabled, nothing needs to be done
674 if (mHardware->recordingEnabled()) {
675 return NO_ERROR;
676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677
Chih-Chung Change25cc652010-05-06 16:36:58 +0800678 // if preview has not been started, start preview first
679 if (!mHardware->previewEnabled()) {
680 result = startPreviewMode();
681 if (result != NO_ERROR) {
682 return result;
Eric Laurent524dc042009-11-27 05:07:55 -0800683 }
Jason Samsb18b6912009-03-24 20:21:36 -0700684 }
Benny Wongda83f462009-08-12 12:01:27 -0500685
Chih-Chung Change25cc652010-05-06 16:36:58 +0800686 // start recording mode
687 enableMsgType(CAMERA_MSG_VIDEO_FRAME);
688 mCameraService->playSound(SOUND_RECORDING);
689 result = mHardware->startRecording();
690 if (result != NO_ERROR) {
691 LOGE("mHardware->startRecording() failed with status %d", result);
692 }
693 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694}
695
696// stop preview mode
Chih-Chung Change25cc652010-05-06 16:36:58 +0800697void CameraService::Client::stopPreview() {
698 LOG1("stopPreview (pid %d)", getCallingPid());
699 Mutex::Autolock lock(mLock);
700 if (checkPidAndHardware() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701
Jamie Gennis85cfdd02010-08-10 16:37:53 -0700702
Chih-Chung Change25cc652010-05-06 16:36:58 +0800703 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
704 mHardware->stopPreview();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705
Chih-Chung Change25cc652010-05-06 16:36:58 +0800706 mPreviewBuffer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707}
708
709// stop recording mode
Chih-Chung Change25cc652010-05-06 16:36:58 +0800710void CameraService::Client::stopRecording() {
711 LOG1("stopRecording (pid %d)", getCallingPid());
712 Mutex::Autolock lock(mLock);
713 if (checkPidAndHardware() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800714
Chih-Chung Change25cc652010-05-06 16:36:58 +0800715 mCameraService->playSound(SOUND_RECORDING);
716 disableMsgType(CAMERA_MSG_VIDEO_FRAME);
717 mHardware->stopRecording();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718
Chih-Chung Change25cc652010-05-06 16:36:58 +0800719 mPreviewBuffer.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720}
721
722// release a recording frame
Chih-Chung Change25cc652010-05-06 16:36:58 +0800723void CameraService::Client::releaseRecordingFrame(const sp<IMemory>& mem) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800725 if (checkPidAndHardware() != NO_ERROR) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 mHardware->releaseRecordingFrame(mem);
727}
728
James Dong38311852010-10-18 20:42:51 -0700729status_t CameraService::Client::storeMetaDataInBuffers(bool enabled)
730{
731 LOG1("storeMetaDataInBuffers: %s", enabled? "true": "false");
732 Mutex::Autolock lock(mLock);
733 if (checkPidAndHardware() != NO_ERROR) {
734 return UNKNOWN_ERROR;
735 }
736 return mHardware->storeMetaDataInBuffers(enabled);
737}
738
Chih-Chung Change25cc652010-05-06 16:36:58 +0800739bool CameraService::Client::previewEnabled() {
740 LOG1("previewEnabled (pid %d)", getCallingPid());
741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800743 if (checkPidAndHardware() != NO_ERROR) return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 return mHardware->previewEnabled();
745}
746
Chih-Chung Change25cc652010-05-06 16:36:58 +0800747bool CameraService::Client::recordingEnabled() {
748 LOG1("recordingEnabled (pid %d)", getCallingPid());
749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800751 if (checkPidAndHardware() != NO_ERROR) return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 return mHardware->recordingEnabled();
753}
754
Chih-Chung Change25cc652010-05-06 16:36:58 +0800755status_t CameraService::Client::autoFocus() {
756 LOG1("autoFocus (pid %d)", getCallingPid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757
758 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800759 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 if (result != NO_ERROR) return result;
761
Benny Wongda83f462009-08-12 12:01:27 -0500762 return mHardware->autoFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763}
764
Chih-Chung Change25cc652010-05-06 16:36:58 +0800765status_t CameraService::Client::cancelAutoFocus() {
766 LOG1("cancelAutoFocus (pid %d)", getCallingPid());
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800767
768 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800769 status_t result = checkPidAndHardware();
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800770 if (result != NO_ERROR) return result;
771
Chih-Chung Chang244f8c22009-09-15 14:51:56 +0800772 return mHardware->cancelAutoFocus();
773}
774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775// take a picture - image is returned in callback
James Donge00cab72011-02-17 16:38:06 -0800776status_t CameraService::Client::takePicture(int msgType) {
777 LOG1("takePicture (pid %d): 0x%x", getCallingPid(), msgType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778
779 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800780 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 if (result != NO_ERROR) return result;
782
James Donge00cab72011-02-17 16:38:06 -0800783 if ((msgType & CAMERA_MSG_RAW_IMAGE) &&
784 (msgType & CAMERA_MSG_RAW_IMAGE_NOTIFY)) {
785 LOGE("CAMERA_MSG_RAW_IMAGE and CAMERA_MSG_RAW_IMAGE_NOTIFY"
786 " cannot be both enabled");
787 return BAD_VALUE;
788 }
789
790 // We only accept picture related message types
791 // and ignore other types of messages for takePicture().
792 int picMsgType = msgType
793 & (CAMERA_MSG_SHUTTER |
794 CAMERA_MSG_POSTVIEW_FRAME |
795 CAMERA_MSG_RAW_IMAGE |
796 CAMERA_MSG_RAW_IMAGE_NOTIFY |
797 CAMERA_MSG_COMPRESSED_IMAGE);
798
799 enableMsgType(picMsgType);
Benny Wongda83f462009-08-12 12:01:27 -0500800
801 return mHardware->takePicture();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802}
803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804// set preview/capture parameters - key/value pairs
Chih-Chung Change25cc652010-05-06 16:36:58 +0800805status_t CameraService::Client::setParameters(const String8& params) {
806 LOG1("setParameters (pid %d) (%s)", getCallingPid(), params.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807
808 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800809 status_t result = checkPidAndHardware();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 if (result != NO_ERROR) return result;
811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 CameraParameters p(params);
James Dong6085b4e2009-09-13 17:10:24 -0700813 return mHardware->setParameters(p);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814}
815
816// get preview/capture parameters - key/value pairs
Chih-Chung Change25cc652010-05-06 16:36:58 +0800817String8 CameraService::Client::getParameters() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800819 if (checkPidAndHardware() != NO_ERROR) return String8();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820
Wu-cheng Liab5b4242009-04-22 16:21:26 +0800821 String8 params(mHardware->getParameters().flatten());
Chih-Chung Change25cc652010-05-06 16:36:58 +0800822 LOG1("getParameters (pid %d) (%s)", getCallingPid(), params.string());
Wu-cheng Liab5b4242009-04-22 16:21:26 +0800823 return params;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824}
825
Nipun Kwatra088146a2010-09-11 19:31:10 -0700826// enable shutter sound
827status_t CameraService::Client::enableShutterSound(bool enable) {
828 LOG1("enableShutterSound (pid %d)", getCallingPid());
829
830 status_t result = checkPidAndHardware();
831 if (result != NO_ERROR) return result;
832
833 if (enable) {
834 mPlayShutterSound = true;
835 return OK;
836 }
837
838 // Disabling shutter sound may not be allowed. In that case only
839 // allow the mediaserver process to disable the sound.
840 char value[PROPERTY_VALUE_MAX];
841 property_get("ro.camera.sound.forced", value, "0");
842 if (strcmp(value, "0") != 0) {
843 // Disabling shutter sound is not allowed. Deny if the current
844 // process is not mediaserver.
845 if (getCallingPid() != getpid()) {
846 LOGE("Failed to disable shutter sound. Permission denied (pid %d)", getCallingPid());
847 return PERMISSION_DENIED;
848 }
849 }
850
851 mPlayShutterSound = false;
852 return OK;
853}
854
Chih-Chung Change25cc652010-05-06 16:36:58 +0800855status_t CameraService::Client::sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) {
856 LOG1("sendCommand (pid %d)", getCallingPid());
Wu-cheng Lib3347bc2010-09-23 17:17:43 -0700857 int orientation;
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700858 Mutex::Autolock lock(mLock);
Chih-Chung Change25cc652010-05-06 16:36:58 +0800859 status_t result = checkPidAndHardware();
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700860 if (result != NO_ERROR) return result;
861
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800862 if (cmd == CAMERA_CMD_SET_DISPLAY_ORIENTATION) {
Wu-cheng Lid55f7e52010-10-14 20:17:44 +0800863 // Mirror the preview if the camera is front-facing.
864 orientation = getOrientation(arg1, mCameraFacing == CAMERA_FACING_FRONT);
865 if (orientation == -1) return BAD_VALUE;
866
Wu-cheng Lib3347bc2010-09-23 17:17:43 -0700867 if (mOrientation != orientation) {
868 mOrientation = orientation;
Wu-cheng Lid3033622011-10-07 13:13:54 +0800869 if (mPreviewWindow != 0) {
870 native_window_set_buffers_transform(mPreviewWindow.get(),
871 mOrientation);
872 }
Wu-cheng Lib3347bc2010-09-23 17:17:43 -0700873 }
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800874 return OK;
Nipun Kwatra088146a2010-09-11 19:31:10 -0700875 } else if (cmd == CAMERA_CMD_ENABLE_SHUTTER_SOUND) {
876 switch (arg1) {
877 case 0:
878 enableShutterSound(false);
879 break;
880 case 1:
881 enableShutterSound(true);
882 break;
883 default:
884 return BAD_VALUE;
885 }
886 return OK;
Nipun Kwatra4e94c302010-09-14 16:49:08 -0700887 } else if (cmd == CAMERA_CMD_PLAY_RECORDING_SOUND) {
888 mCameraService->playSound(SOUND_RECORDING);
Chih-Chung Changd1d77062010-01-22 17:49:48 -0800889 }
890
Wu-cheng Li36f68b82009-09-28 16:14:58 -0700891 return mHardware->sendCommand(cmd, arg1, arg2);
892}
893
Chih-Chung Change25cc652010-05-06 16:36:58 +0800894// ----------------------------------------------------------------------------
895
896void CameraService::Client::enableMsgType(int32_t msgType) {
897 android_atomic_or(msgType, &mMsgEnabled);
898 mHardware->enableMsgType(msgType);
899}
900
901void CameraService::Client::disableMsgType(int32_t msgType) {
902 android_atomic_and(~msgType, &mMsgEnabled);
903 mHardware->disableMsgType(msgType);
904}
905
906#define CHECK_MESSAGE_INTERVAL 10 // 10ms
907bool CameraService::Client::lockIfMessageWanted(int32_t msgType) {
908 int sleepCount = 0;
909 while (mMsgEnabled & msgType) {
910 if (mLock.tryLock() == NO_ERROR) {
911 if (sleepCount > 0) {
912 LOG1("lockIfMessageWanted(%d): waited for %d ms",
913 msgType, sleepCount * CHECK_MESSAGE_INTERVAL);
914 }
915 return true;
916 }
917 if (sleepCount++ == 0) {
918 LOG1("lockIfMessageWanted(%d): enter sleep", msgType);
919 }
920 usleep(CHECK_MESSAGE_INTERVAL * 1000);
921 }
922 LOGW("lockIfMessageWanted(%d): dropped unwanted message", msgType);
923 return false;
924}
925
926// ----------------------------------------------------------------------------
927
928// Converts from a raw pointer to the client to a strong pointer during a
929// hardware callback. This requires the callbacks only happen when the client
930// is still alive.
931sp<CameraService::Client> CameraService::Client::getClientFromCookie(void* user) {
932 sp<Client> client = gCameraService->getClientById((int) user);
933
934 // This could happen if the Client is in the process of shutting down (the
935 // last strong reference is gone, but the destructor hasn't finished
936 // stopping the hardware).
937 if (client == 0) return NULL;
938
939 // The checks below are not necessary and are for debugging only.
940 if (client->mCameraService.get() != gCameraService) {
941 LOGE("mismatch service!");
942 return NULL;
943 }
944
945 if (client->mHardware == 0) {
946 LOGE("mHardware == 0: callback after disconnect()?");
947 return NULL;
948 }
949
950 return client;
951}
952
953// Callback messages can be dispatched to internal handlers or pass to our
954// client's callback functions, depending on the message type.
955//
956// notifyCallback:
957// CAMERA_MSG_SHUTTER handleShutter
958// (others) c->notifyCallback
959// dataCallback:
960// CAMERA_MSG_PREVIEW_FRAME handlePreviewData
961// CAMERA_MSG_POSTVIEW_FRAME handlePostview
962// CAMERA_MSG_RAW_IMAGE handleRawPicture
963// CAMERA_MSG_COMPRESSED_IMAGE handleCompressedPicture
964// (others) c->dataCallback
965// dataCallbackTimestamp
966// (others) c->dataCallbackTimestamp
967//
968// NOTE: the *Callback functions grab mLock of the client before passing
969// control to handle* functions. So the handle* functions must release the
970// lock before calling the ICameraClient's callbacks, so those callbacks can
971// invoke methods in the Client class again (For example, the preview frame
972// callback may want to releaseRecordingFrame). The handle* functions must
973// release the lock after all accesses to member variables, so it must be
974// handled very carefully.
975
976void CameraService::Client::notifyCallback(int32_t msgType, int32_t ext1,
977 int32_t ext2, void* user) {
978 LOG2("notifyCallback(%d)", msgType);
979
980 sp<Client> client = getClientFromCookie(user);
981 if (client == 0) return;
982 if (!client->lockIfMessageWanted(msgType)) return;
983
984 switch (msgType) {
985 case CAMERA_MSG_SHUTTER:
986 // ext1 is the dimension of the yuv picture.
Iliyan Malchev40c36412011-03-28 16:10:12 -0700987 client->handleShutter();
Chih-Chung Change25cc652010-05-06 16:36:58 +0800988 break;
989 default:
990 client->handleGenericNotify(msgType, ext1, ext2);
991 break;
992 }
993}
994
995void CameraService::Client::dataCallback(int32_t msgType,
Wu-cheng Lif0d6a482011-07-28 05:30:59 +0800996 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata, void* user) {
Chih-Chung Change25cc652010-05-06 16:36:58 +0800997 LOG2("dataCallback(%d)", msgType);
998
999 sp<Client> client = getClientFromCookie(user);
1000 if (client == 0) return;
1001 if (!client->lockIfMessageWanted(msgType)) return;
1002
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001003 if (dataPtr == 0 && metadata == NULL) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001004 LOGE("Null data returned in data callback");
1005 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1006 return;
1007 }
1008
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001009 switch (msgType & ~CAMERA_MSG_PREVIEW_METADATA) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001010 case CAMERA_MSG_PREVIEW_FRAME:
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001011 client->handlePreviewData(msgType, dataPtr, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001012 break;
1013 case CAMERA_MSG_POSTVIEW_FRAME:
1014 client->handlePostview(dataPtr);
1015 break;
1016 case CAMERA_MSG_RAW_IMAGE:
1017 client->handleRawPicture(dataPtr);
1018 break;
1019 case CAMERA_MSG_COMPRESSED_IMAGE:
1020 client->handleCompressedPicture(dataPtr);
1021 break;
1022 default:
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001023 client->handleGenericData(msgType, dataPtr, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001024 break;
1025 }
1026}
1027
1028void CameraService::Client::dataCallbackTimestamp(nsecs_t timestamp,
1029 int32_t msgType, const sp<IMemory>& dataPtr, void* user) {
1030 LOG2("dataCallbackTimestamp(%d)", msgType);
1031
1032 sp<Client> client = getClientFromCookie(user);
1033 if (client == 0) return;
James Dong74920cb2010-12-09 11:08:14 -08001034 if (!client->lockIfMessageWanted(msgType)) return;
Chih-Chung Change25cc652010-05-06 16:36:58 +08001035
1036 if (dataPtr == 0) {
1037 LOGE("Null data returned in data with timestamp callback");
1038 client->handleGenericNotify(CAMERA_MSG_ERROR, UNKNOWN_ERROR, 0);
1039 return;
1040 }
1041
1042 client->handleGenericDataTimestamp(timestamp, msgType, dataPtr);
1043}
1044
1045// snapshot taken callback
Iliyan Malchev40c36412011-03-28 16:10:12 -07001046void CameraService::Client::handleShutter(void) {
Nipun Kwatra088146a2010-09-11 19:31:10 -07001047 if (mPlayShutterSound) {
1048 mCameraService->playSound(SOUND_SHUTTER);
1049 }
Chih-Chung Change25cc652010-05-06 16:36:58 +08001050
Chih-Chung Change25cc652010-05-06 16:36:58 +08001051 sp<ICameraClient> c = mCameraClient;
1052 if (c != 0) {
1053 mLock.unlock();
1054 c->notifyCallback(CAMERA_MSG_SHUTTER, 0, 0);
1055 if (!lockIfMessageWanted(CAMERA_MSG_SHUTTER)) return;
1056 }
1057 disableMsgType(CAMERA_MSG_SHUTTER);
1058
Chih-Chung Change25cc652010-05-06 16:36:58 +08001059 mLock.unlock();
1060}
1061
1062// preview callback - frame buffer update
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001063void CameraService::Client::handlePreviewData(int32_t msgType,
1064 const sp<IMemory>& mem,
1065 camera_frame_metadata_t *metadata) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001066 ssize_t offset;
1067 size_t size;
1068 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1069
Chih-Chung Change25cc652010-05-06 16:36:58 +08001070 // local copy of the callback flags
1071 int flags = mPreviewCallbackFlag;
1072
1073 // is callback enabled?
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -07001074 if (!(flags & CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK)) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001075 // If the enable bit is off, the copy-out and one-shot bits are ignored
1076 LOG2("frame callback is disabled");
1077 mLock.unlock();
1078 return;
1079 }
1080
1081 // hold a strong pointer to the client
1082 sp<ICameraClient> c = mCameraClient;
1083
1084 // clear callback flags if no client or one-shot mode
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -07001085 if (c == 0 || (mPreviewCallbackFlag & CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK)) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001086 LOG2("Disable preview callback");
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -07001087 mPreviewCallbackFlag &= ~(CAMERA_FRAME_CALLBACK_FLAG_ONE_SHOT_MASK |
1088 CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK |
1089 CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK);
Wu-cheng Lid02c8122010-09-03 16:40:32 -07001090 disableMsgType(CAMERA_MSG_PREVIEW_FRAME);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001091 }
1092
1093 if (c != 0) {
1094 // Is the received frame copied out or not?
Iliyan Malchev9c7ac0d2011-04-14 16:51:21 -07001095 if (flags & CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001096 LOG2("frame is copied");
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001097 copyFrameAndPostCopiedFrame(msgType, c, heap, offset, size, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001098 } else {
1099 LOG2("frame is forwarded");
1100 mLock.unlock();
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001101 c->dataCallback(msgType, mem, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001102 }
1103 } else {
1104 mLock.unlock();
1105 }
1106}
1107
1108// picture callback - postview image ready
1109void CameraService::Client::handlePostview(const sp<IMemory>& mem) {
1110 disableMsgType(CAMERA_MSG_POSTVIEW_FRAME);
1111
1112 sp<ICameraClient> c = mCameraClient;
1113 mLock.unlock();
1114 if (c != 0) {
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001115 c->dataCallback(CAMERA_MSG_POSTVIEW_FRAME, mem, NULL);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001116 }
1117}
1118
1119// picture callback - raw image ready
1120void CameraService::Client::handleRawPicture(const sp<IMemory>& mem) {
1121 disableMsgType(CAMERA_MSG_RAW_IMAGE);
1122
1123 ssize_t offset;
1124 size_t size;
1125 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
1126
Chih-Chung Change25cc652010-05-06 16:36:58 +08001127 sp<ICameraClient> c = mCameraClient;
1128 mLock.unlock();
1129 if (c != 0) {
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001130 c->dataCallback(CAMERA_MSG_RAW_IMAGE, mem, NULL);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001131 }
1132}
1133
1134// picture callback - compressed picture ready
1135void CameraService::Client::handleCompressedPicture(const sp<IMemory>& mem) {
1136 disableMsgType(CAMERA_MSG_COMPRESSED_IMAGE);
1137
1138 sp<ICameraClient> c = mCameraClient;
1139 mLock.unlock();
1140 if (c != 0) {
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001141 c->dataCallback(CAMERA_MSG_COMPRESSED_IMAGE, mem, NULL);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001142 }
1143}
1144
1145
1146void CameraService::Client::handleGenericNotify(int32_t msgType,
1147 int32_t ext1, int32_t ext2) {
1148 sp<ICameraClient> c = mCameraClient;
1149 mLock.unlock();
1150 if (c != 0) {
1151 c->notifyCallback(msgType, ext1, ext2);
1152 }
1153}
1154
1155void CameraService::Client::handleGenericData(int32_t msgType,
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001156 const sp<IMemory>& dataPtr, camera_frame_metadata_t *metadata) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001157 sp<ICameraClient> c = mCameraClient;
1158 mLock.unlock();
1159 if (c != 0) {
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001160 c->dataCallback(msgType, dataPtr, metadata);
Chih-Chung Change25cc652010-05-06 16:36:58 +08001161 }
1162}
1163
1164void CameraService::Client::handleGenericDataTimestamp(nsecs_t timestamp,
1165 int32_t msgType, const sp<IMemory>& dataPtr) {
1166 sp<ICameraClient> c = mCameraClient;
1167 mLock.unlock();
1168 if (c != 0) {
1169 c->dataCallbackTimestamp(timestamp, msgType, dataPtr);
1170 }
1171}
1172
1173void CameraService::Client::copyFrameAndPostCopiedFrame(
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001174 int32_t msgType, const sp<ICameraClient>& client,
1175 const sp<IMemoryHeap>& heap, size_t offset, size_t size,
1176 camera_frame_metadata_t *metadata) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001177 LOG2("copyFrameAndPostCopiedFrame");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 // It is necessary to copy out of pmem before sending this to
1179 // the callback. For efficiency, reuse the same MemoryHeapBase
1180 // provided it's big enough. Don't allocate the memory or
1181 // perform the copy if there's no callback.
Dave Sparks05fd0df2009-11-10 17:08:08 -08001182 // hold the preview lock while we grab a reference to the preview buffer
Dave Sparksc8093c12009-11-06 11:47:13 -08001183 sp<MemoryHeapBase> previewBuffer;
Chih-Chung Change25cc652010-05-06 16:36:58 +08001184
1185 if (mPreviewBuffer == 0) {
1186 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
1187 } else if (size > mPreviewBuffer->virtualSize()) {
1188 mPreviewBuffer.clear();
1189 mPreviewBuffer = new MemoryHeapBase(size, 0, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 }
Chih-Chung Change25cc652010-05-06 16:36:58 +08001191 if (mPreviewBuffer == 0) {
1192 LOGE("failed to allocate space for preview buffer");
1193 mLock.unlock();
1194 return;
1195 }
1196 previewBuffer = mPreviewBuffer;
1197
1198 memcpy(previewBuffer->base(), (uint8_t *)heap->base() + offset, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199
Dave Sparksc8093c12009-11-06 11:47:13 -08001200 sp<MemoryBase> frame = new MemoryBase(previewBuffer, 0, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 if (frame == 0) {
1202 LOGE("failed to allocate space for frame callback");
Chih-Chung Change25cc652010-05-06 16:36:58 +08001203 mLock.unlock();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 return;
1205 }
Chih-Chung Change25cc652010-05-06 16:36:58 +08001206
1207 mLock.unlock();
Wu-cheng Libb1e2752011-07-30 05:00:37 +08001208 client->dataCallback(msgType, frame, metadata);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209}
1210
Wu-cheng Lid55f7e52010-10-14 20:17:44 +08001211int CameraService::Client::getOrientation(int degrees, bool mirror) {
1212 if (!mirror) {
1213 if (degrees == 0) return 0;
1214 else if (degrees == 90) return HAL_TRANSFORM_ROT_90;
1215 else if (degrees == 180) return HAL_TRANSFORM_ROT_180;
1216 else if (degrees == 270) return HAL_TRANSFORM_ROT_270;
1217 } else { // Do mirror (horizontal flip)
1218 if (degrees == 0) { // FLIP_H and ROT_0
1219 return HAL_TRANSFORM_FLIP_H;
1220 } else if (degrees == 90) { // FLIP_H and ROT_90
1221 return HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90;
1222 } else if (degrees == 180) { // FLIP_H and ROT_180
1223 return HAL_TRANSFORM_FLIP_V;
1224 } else if (degrees == 270) { // FLIP_H and ROT_270
1225 return HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90;
1226 }
1227 }
1228 LOGE("Invalid setDisplayOrientation degrees=%d", degrees);
1229 return -1;
1230}
1231
1232
Chih-Chung Change25cc652010-05-06 16:36:58 +08001233// ----------------------------------------------------------------------------
1234
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001235static const int kDumpLockRetries = 50;
1236static const int kDumpLockSleep = 60000;
1237
1238static bool tryLock(Mutex& mutex)
1239{
1240 bool locked = false;
1241 for (int i = 0; i < kDumpLockRetries; ++i) {
1242 if (mutex.tryLock() == NO_ERROR) {
1243 locked = true;
1244 break;
1245 }
1246 usleep(kDumpLockSleep);
1247 }
1248 return locked;
1249}
1250
Chih-Chung Change25cc652010-05-06 16:36:58 +08001251status_t CameraService::dump(int fd, const Vector<String16>& args) {
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001252 static const char* kDeadlockedString = "CameraService may be deadlocked\n";
1253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 const size_t SIZE = 256;
1255 char buffer[SIZE];
1256 String8 result;
1257 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
1258 snprintf(buffer, SIZE, "Permission Denial: "
1259 "can't dump CameraService from pid=%d, uid=%d\n",
Chih-Chung Chang1f25ec82009-06-22 16:03:41 +08001260 getCallingPid(),
Chih-Chung Change25cc652010-05-06 16:36:58 +08001261 getCallingUid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 result.append(buffer);
1263 write(fd, result.string(), result.size());
1264 } else {
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001265 bool locked = tryLock(mServiceLock);
1266 // failed to lock - CameraService is probably deadlocked
1267 if (!locked) {
1268 String8 result(kDeadlockedString);
1269 write(fd, result.string(), result.size());
1270 }
1271
Chih-Chung Change25cc652010-05-06 16:36:58 +08001272 bool hasClient = false;
Chih-Chung Changb8bb78f2010-06-10 13:32:16 +08001273 for (int i = 0; i < mNumberOfCameras; i++) {
Chih-Chung Change25cc652010-05-06 16:36:58 +08001274 sp<Client> client = mClient[i].promote();
1275 if (client == 0) continue;
1276 hasClient = true;
1277 sprintf(buffer, "Client[%d] (%p) PID: %d\n",
1278 i,
1279 client->getCameraClient()->asBinder().get(),
1280 client->mClientPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 result.append(buffer);
1282 write(fd, result.string(), result.size());
Chih-Chung Change25cc652010-05-06 16:36:58 +08001283 client->mHardware->dump(fd, args);
1284 }
1285 if (!hasClient) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 result.append("No camera client yet.\n");
1287 write(fd, result.string(), result.size());
1288 }
Chih-Chung Change86ae1b2010-03-05 11:33:03 -08001289
1290 if (locked) mServiceLock.unlock();
Chih-Chung Change25cc652010-05-06 16:36:58 +08001291
1292 // change logging level
1293 int n = args.size();
1294 for (int i = 0; i + 1 < n; i++) {
1295 if (args[i] == String16("-v")) {
1296 String8 levelStr(args[i+1]);
1297 int level = atoi(levelStr.string());
1298 sprintf(buffer, "Set Log Level to %d", level);
1299 result.append(buffer);
1300 setLogLevel(level);
1301 }
1302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
1304 return NO_ERROR;
1305}
1306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307}; // namespace android