blob: 53420e5d8d051ad7a45972714aa60d012e6f520a [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -07001/*
Ruben Brunkd1176ef2014-02-21 10:51:38 -08002 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mathias Agopian65ab4712010-07-14 17:59:35 -070016
17#ifndef ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
18#define ANDROID_SERVERS_CAMERA_CAMERASERVICE_H
19
Igor Murashkin634a5152013-02-20 17:15:11 -080020#include <utils/Vector.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070021#include <utils/KeyedVector.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080022#include <binder/AppOpsManager.h>
Mathias Agopian5462fc92010-07-14 18:41:18 -070023#include <binder/BinderService.h>
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -080024#include <binder/IAppOpsCallback.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070025#include <camera/ICameraService.h>
Iliyan Malchev8951a972011-04-14 16:55:59 -070026#include <hardware/camera.h>
Mathias Agopian65ab4712010-07-14 17:59:35 -070027
Igor Murashkinc073ba52013-02-26 14:32:34 -080028#include <camera/ICamera.h>
29#include <camera/ICameraClient.h>
30#include <camera/IProCameraUser.h>
31#include <camera/IProCameraCallbacks.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070032#include <camera/camera2/ICameraDeviceUser.h>
33#include <camera/camera2/ICameraDeviceCallbacks.h>
Ruben Brunkd1176ef2014-02-21 10:51:38 -080034#include <camera/VendorTagDescriptor.h>
Jianing Weicb0652e2014-03-12 18:29:36 -070035#include <camera/CaptureResult.h>
Ruben Brunkb2119af2014-05-09 19:57:56 -070036#include <camera/CameraParameters.h>
Igor Murashkinc073ba52013-02-26 14:32:34 -080037
Igor Murashkinbfc99152013-02-27 12:55:20 -080038#include <camera/ICameraServiceListener.h>
Chien-Yu Chen3068d732015-02-09 13:29:57 -080039#include "CameraFlashlight.h"
40
Yin-Chia Yehe074a932015-01-30 10:29:02 -080041#include "common/CameraModule.h"
Ruben Brunkcc776712015-02-17 20:18:47 -080042#include "utils/AutoConditionLock.h"
43#include "utils/ClientManager.h"
44#include "utils/RingBuffer.h"
Yin-Chia Yehe074a932015-01-30 10:29:02 -080045
Ruben Brunkcc776712015-02-17 20:18:47 -080046#include <set>
47#include <string>
48#include <map>
49#include <memory>
Mathias Agopian65ab4712010-07-14 17:59:35 -070050
51namespace android {
52
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070053extern volatile int32_t gLogLevel;
54
Mathias Agopian65ab4712010-07-14 17:59:35 -070055class MemoryHeapBase;
56class MediaPlayer;
57
Mathias Agopian5462fc92010-07-14 18:41:18 -070058class CameraService :
59 public BinderService<CameraService>,
Igor Murashkinecf17e82012-10-02 16:05:11 -070060 public BnCameraService,
Igor Murashkincba2c162013-03-20 15:56:31 -070061 public IBinder::DeathRecipient,
62 public camera_module_callbacks_t
Mathias Agopian65ab4712010-07-14 17:59:35 -070063{
Mathias Agopian5462fc92010-07-14 18:41:18 -070064 friend class BinderService<CameraService>;
Mathias Agopian65ab4712010-07-14 17:59:35 -070065public:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -070066 class Client;
Igor Murashkin634a5152013-02-20 17:15:11 -080067 class BasicClient;
68
Ruben Brunkcc776712015-02-17 20:18:47 -080069 enum apiLevel {
70 API_1 = 1,
71 API_2 = 2
72 };
73
74 // Process States (mirrors frameworks/base/core/java/android/app/ActivityManager.java)
75 static const int PROCESS_STATE_NONEXISTENT = -1;
76 static const int PROCESS_STATE_PERSISTENT = 0;
77 static const int PROCESS_STATE_PERSISTENT_UI = 1;
78 static const int PROCESS_STATE_TOP = 2;
79 static const int PROCESS_STATE_IMPORTANT_FOREGROUND = 3;
80 static const int PROCESS_STATE_IMPORTANT_BACKGROUND = 4;
81 static const int PROCESS_STATE_BACKUP = 5;
82 static const int PROCESS_STATE_HEAVY_WEIGHT = 6;
83 static const int PROCESS_STATE_SERVICE = 7;
84 static const int PROCESS_STATE_RECEIVER = 8;
85 static const int PROCESS_STATE_HOME = 9;
86 static const int PROCESS_STATE_LAST_ACTIVITY = 10;
87 static const int PROCESS_STATE_CACHED_ACTIVITY = 11;
88 static const int PROCESS_STATE_CACHED_ACTIVITY_CLIENT = 12;
89 static const int PROCESS_STATE_CACHED_EMPTY = 13;
90
91 // 3 second busy timeout when other clients are connecting
92 static const nsecs_t DEFAULT_CONNECT_TIMEOUT_NS = 3000000000;
93
94 // Default number of messages to store in eviction log
95 static const size_t DEFAULT_EVICTION_LOG_LENGTH = 50;
96
Igor Murashkin634a5152013-02-20 17:15:11 -080097 // Implementation of BinderService<T>
Mathias Agopian5462fc92010-07-14 18:41:18 -070098 static char const* getServiceName() { return "media.camera"; }
Mathias Agopian65ab4712010-07-14 17:59:35 -070099
100 CameraService();
101 virtual ~CameraService();
102
Igor Murashkin634a5152013-02-20 17:15:11 -0800103 /////////////////////////////////////////////////////////////////////
Igor Murashkincba2c162013-03-20 15:56:31 -0700104 // HAL Callbacks
Ruben Brunkcc776712015-02-17 20:18:47 -0800105 virtual void onDeviceStatusChanged(camera_device_status_t cameraId,
106 camera_device_status_t newStatus);
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800107 virtual void onTorchStatusChanged(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800108 ICameraServiceListener::TorchStatus
109 newStatus);
Igor Murashkincba2c162013-03-20 15:56:31 -0700110
111 /////////////////////////////////////////////////////////////////////
Igor Murashkin634a5152013-02-20 17:15:11 -0800112 // ICameraService
Mathias Agopian65ab4712010-07-14 17:59:35 -0700113 virtual int32_t getNumberOfCameras();
114 virtual status_t getCameraInfo(int cameraId,
115 struct CameraInfo* cameraInfo);
Zhijun He2b59be82013-09-25 10:14:30 -0700116 virtual status_t getCameraCharacteristics(int cameraId,
117 CameraMetadata* cameraInfo);
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800118 virtual status_t getCameraVendorTagDescriptor(/*out*/ sp<VendorTagDescriptor>& desc);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800119
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700120 virtual status_t connect(const sp<ICameraClient>& cameraClient, int cameraId,
121 const String16& clientPackageName, int clientUid,
122 /*out*/
123 sp<ICamera>& device);
124
Zhijun Heb10cdad2014-06-16 16:38:35 -0700125 virtual status_t connectLegacy(const sp<ICameraClient>& cameraClient, int cameraId,
126 int halVersion, const String16& clientPackageName, int clientUid,
127 /*out*/
128 sp<ICamera>& device);
129
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700130 virtual status_t connectPro(const sp<IProCameraCallbacks>& cameraCb,
131 int cameraId, const String16& clientPackageName, int clientUid,
132 /*out*/
133 sp<IProCameraUser>& device);
134
135 virtual status_t connectDevice(
Igor Murashkine7ee7632013-06-11 18:10:18 -0700136 const sp<ICameraDeviceCallbacks>& cameraCb,
137 int cameraId,
138 const String16& clientPackageName,
Ruben Brunk0f61d8f2013-08-08 13:07:18 -0700139 int clientUid,
140 /*out*/
141 sp<ICameraDeviceUser>& device);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700142
Igor Murashkinbfc99152013-02-27 12:55:20 -0800143 virtual status_t addListener(const sp<ICameraServiceListener>& listener);
144 virtual status_t removeListener(
145 const sp<ICameraServiceListener>& listener);
146
Igor Murashkin65d14b92014-06-17 12:03:20 -0700147 virtual status_t getLegacyParameters(
148 int cameraId,
149 /*out*/
150 String16* parameters);
151
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800152 virtual status_t setTorchMode(const String16& cameraId, bool enabled,
153 const sp<IBinder>& clientBinder);
154
Igor Murashkin65d14b92014-06-17 12:03:20 -0700155 // OK = supports api of that version, -EOPNOTSUPP = does not support
156 virtual status_t supportsCameraApi(
157 int cameraId, int apiVersion);
158
Igor Murashkin634a5152013-02-20 17:15:11 -0800159 // Extra permissions checks
Mathias Agopian65ab4712010-07-14 17:59:35 -0700160 virtual status_t onTransact(uint32_t code, const Parcel& data,
161 Parcel* reply, uint32_t flags);
Igor Murashkin634a5152013-02-20 17:15:11 -0800162
163 virtual status_t dump(int fd, const Vector<String16>& args);
164
165 /////////////////////////////////////////////////////////////////////
166 // Client functionality
Mathias Agopian65ab4712010-07-14 17:59:35 -0700167
168 enum sound_kind {
169 SOUND_SHUTTER = 0,
170 SOUND_RECORDING = 1,
171 NUM_SOUNDS
172 };
173
174 void loadSound();
175 void playSound(sound_kind kind);
176 void releaseSound();
177
Igor Murashkin98e24722013-06-19 19:51:04 -0700178 /////////////////////////////////////////////////////////////////////
179 // CameraDeviceFactory functionality
180 int getDeviceVersion(int cameraId, int* facing = NULL);
181
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700182 /////////////////////////////////////////////////////////////////////
183 // Shared utilities
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700184 static status_t filterGetInfoErrorCode(status_t err);
Igor Murashkin634a5152013-02-20 17:15:11 -0800185
186 /////////////////////////////////////////////////////////////////////
187 // CameraClient functionality
188
Igor Murashkin634a5152013-02-20 17:15:11 -0800189 class BasicClient : public virtual RefBase {
190 public:
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800191 virtual status_t initialize(CameraModule *module) = 0;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700192 virtual void disconnect();
Igor Murashkin634a5152013-02-20 17:15:11 -0800193
Igor Murashkine7ee7632013-06-11 18:10:18 -0700194 // because we can't virtually inherit IInterface, which breaks
195 // virtual inheritance
196 virtual sp<IBinder> asBinderWrapper() = 0;
197
Igor Murashkine6800ce2013-03-04 17:25:57 -0800198 // Return the remote callback binder object (e.g. IProCameraCallbacks)
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700199 sp<IBinder> getRemote() {
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800200 return mRemoteBinder;
Igor Murashkin634a5152013-02-20 17:15:11 -0800201 }
202
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700203 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700204
Ruben Brunkcc776712015-02-17 20:18:47 -0800205 // Return the package name for this client
206 virtual String16 getPackageName() const;
207
208 // Notify client about a fatal error
209 virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
210 const CaptureResultExtras& resultExtras) = 0;
211
212 // Get the PID of the application client using this
213 virtual int getClientPid() const;
Igor Murashkin634a5152013-02-20 17:15:11 -0800214 protected:
215 BasicClient(const sp<CameraService>& cameraService,
216 const sp<IBinder>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800217 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800218 int cameraId,
219 int cameraFacing,
220 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800221 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800222 int servicePid);
223
224 virtual ~BasicClient();
225
226 // the instance is in the middle of destruction. When this is set,
227 // the instance should not be accessed from callback.
228 // CameraService's mClientLock should be acquired to access this.
229 // - subclasses should set this to true in their destructors.
230 bool mDestructionStarted;
231
232 // these are initialized in the constructor.
233 sp<CameraService> mCameraService; // immutable after constructor
234 int mCameraId; // immutable after constructor
235 int mCameraFacing; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800236 const String16 mClientPackageName;
Igor Murashkin634a5152013-02-20 17:15:11 -0800237 pid_t mClientPid;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800238 uid_t mClientUid; // immutable after constructor
Igor Murashkin634a5152013-02-20 17:15:11 -0800239 pid_t mServicePid; // immutable after constructor
Ruben Brunkcc776712015-02-17 20:18:47 -0800240 bool mDisconnected;
Igor Murashkin634a5152013-02-20 17:15:11 -0800241
242 // - The app-side Binder interface to receive callbacks from us
Igor Murashkine7ee7632013-06-11 18:10:18 -0700243 sp<IBinder> mRemoteBinder; // immutable after constructor
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800244
245 // permissions management
246 status_t startCameraOps();
247 status_t finishCameraOps();
248
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800249 private:
250 AppOpsManager mAppOpsManager;
251
252 class OpsCallback : public BnAppOpsCallback {
253 public:
254 OpsCallback(wp<BasicClient> client);
255 virtual void opChanged(int32_t op, const String16& packageName);
256
257 private:
258 wp<BasicClient> mClient;
259
260 }; // class OpsCallback
261
262 sp<OpsCallback> mOpsCallback;
263 // Track whether startCameraOps was called successfully, to avoid
264 // finishing what we didn't start.
265 bool mOpsActive;
266
267 // IAppOpsCallback interface, indirected through opListener
268 virtual void opChanged(int32_t op, const String16& packageName);
269 }; // class BasicClient
Igor Murashkin634a5152013-02-20 17:15:11 -0800270
271 class Client : public BnCamera, public BasicClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700272 {
273 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800274 typedef ICameraClient TCamCallbacks;
275
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700276 // ICamera interface (see ICamera for details)
277 virtual void disconnect();
278 virtual status_t connect(const sp<ICameraClient>& client) = 0;
279 virtual status_t lock() = 0;
280 virtual status_t unlock() = 0;
Eino-Ville Talvala1ce7c342013-08-21 13:57:21 -0700281 virtual status_t setPreviewTarget(const sp<IGraphicBufferProducer>& bufferProducer)=0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700282 virtual void setPreviewCallbackFlag(int flag) = 0;
Eino-Ville Talvala3ee35502013-04-02 15:45:11 -0700283 virtual status_t setPreviewCallbackTarget(
284 const sp<IGraphicBufferProducer>& callbackProducer) = 0;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700285 virtual status_t startPreview() = 0;
286 virtual void stopPreview() = 0;
287 virtual bool previewEnabled() = 0;
288 virtual status_t storeMetaDataInBuffers(bool enabled) = 0;
289 virtual status_t startRecording() = 0;
290 virtual void stopRecording() = 0;
291 virtual bool recordingEnabled() = 0;
292 virtual void releaseRecordingFrame(const sp<IMemory>& mem) = 0;
293 virtual status_t autoFocus() = 0;
294 virtual status_t cancelAutoFocus() = 0;
295 virtual status_t takePicture(int msgType) = 0;
296 virtual status_t setParameters(const String8& params) = 0;
297 virtual String8 getParameters() const = 0;
298 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2) = 0;
299
300 // Interface used by CameraService
301 Client(const sp<CameraService>& cameraService,
302 const sp<ICameraClient>& cameraClient,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800303 const String16& clientPackageName,
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700304 int cameraId,
305 int cameraFacing,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700306 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800307 uid_t clientUid,
Igor Murashkinecf17e82012-10-02 16:05:11 -0700308 int servicePid);
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700309 ~Client();
310
311 // return our camera client
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800312 const sp<ICameraClient>& getRemoteCallback() {
313 return mRemoteCallback;
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700314 }
315
Igor Murashkine7ee7632013-06-11 18:10:18 -0700316 virtual sp<IBinder> asBinderWrapper() {
Marco Nelissen06b46062014-11-14 07:58:25 -0800317 return asBinder(this);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700318 }
319
Jianing Weicb0652e2014-03-12 18:29:36 -0700320 virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
321 const CaptureResultExtras& resultExtras);
Ruben Brunkcc776712015-02-17 20:18:47 -0800322 protected:
323 // Convert client from cookie.
324 static sp<CameraService::Client> getClientFromCookie(void* user);
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800325
Igor Murashkin634a5152013-02-20 17:15:11 -0800326 // Initialized in constructor
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700327
Igor Murashkin634a5152013-02-20 17:15:11 -0800328 // - The app-side Binder interface to receive callbacks from us
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800329 sp<ICameraClient> mRemoteCallback;
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800330
331 }; // class Client
Igor Murashkin634a5152013-02-20 17:15:11 -0800332
333 class ProClient : public BnProCameraUser, public BasicClient {
334 public:
Igor Murashkin44cfcf02013-03-01 16:22:28 -0800335 typedef IProCameraCallbacks TCamCallbacks;
336
Igor Murashkin634a5152013-02-20 17:15:11 -0800337 ProClient(const sp<CameraService>& cameraService,
338 const sp<IProCameraCallbacks>& remoteCallback,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800339 const String16& clientPackageName,
Igor Murashkin634a5152013-02-20 17:15:11 -0800340 int cameraId,
341 int cameraFacing,
342 int clientPid,
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800343 uid_t clientUid,
Igor Murashkin634a5152013-02-20 17:15:11 -0800344 int servicePid);
345
346 virtual ~ProClient();
347
348 const sp<IProCameraCallbacks>& getRemoteCallback() {
349 return mRemoteCallback;
350 }
351
Igor Murashkin634a5152013-02-20 17:15:11 -0800352 /***
353 IProCamera implementation
354 ***/
Igor Murashkine6800ce2013-03-04 17:25:57 -0800355 virtual status_t connect(const sp<IProCameraCallbacks>& callbacks)
356 = 0;
357 virtual status_t exclusiveTryLock() = 0;
358 virtual status_t exclusiveLock() = 0;
359 virtual status_t exclusiveUnlock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800360
Igor Murashkine6800ce2013-03-04 17:25:57 -0800361 virtual bool hasExclusiveLock() = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800362
363 // Note that the callee gets a copy of the metadata.
364 virtual int submitRequest(camera_metadata_t* metadata,
Igor Murashkine6800ce2013-03-04 17:25:57 -0800365 bool streaming = false) = 0;
366 virtual status_t cancelRequest(int requestId) = 0;
Igor Murashkin634a5152013-02-20 17:15:11 -0800367
Igor Murashkinbfc99152013-02-27 12:55:20 -0800368 // Callbacks from camera service
Igor Murashkine6800ce2013-03-04 17:25:57 -0800369 virtual void onExclusiveLockStolen() = 0;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800370
Jianing Weicb0652e2014-03-12 18:29:36 -0700371 virtual void notifyError(ICameraDeviceCallbacks::CameraErrorCode errorCode,
372 const CaptureResultExtras& resultExtras);
Ruben Brunkcc776712015-02-17 20:18:47 -0800373 protected:
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700374
Eino-Ville Talvalaceb388d2013-02-19 10:40:14 -0800375 sp<IProCameraCallbacks> mRemoteCallback;
376 }; // class ProClient
Eino-Ville Talvala5e08d602012-05-16 14:59:25 -0700377
Ruben Brunkcc776712015-02-17 20:18:47 -0800378 typedef std::shared_ptr<resource_policy::ClientDescriptor<String8,
379 sp<CameraService::BasicClient>>> DescriptorPtr;
380
381 /**
382 * A container class for managing active camera clients that are using HAL devices. Active
383 * clients are represented by ClientDescriptor objects that contain strong pointers to the
384 * actual BasicClient subclass binder interface implementation.
385 *
386 * This class manages the eviction behavior for the camera clients. See the parent class
387 * implementation in utils/ClientManager for the specifics of this behavior.
388 */
389 class CameraClientManager :
390 public resource_policy::ClientManager<String8, sp<CameraService::BasicClient>> {
391 public:
392 virtual ~CameraClientManager();
393
394 /**
395 * Return a strong pointer to the active BasicClient for this camera ID, or an empty
396 * if none exists.
397 */
398 sp<CameraService::BasicClient> getCameraClient(const String8& id) const;
399
400 /**
401 * Return a string describing the current state.
402 */
403 String8 toString() const;
404
405 /**
406 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer.
407 */
408 static DescriptorPtr makeClientDescriptor(const String8& key, const sp<BasicClient>& value,
409 int32_t cost, const std::set<String8>& conflictingKeys, int32_t priority,
410 int32_t ownerId);
411
412 /**
413 * Make a ClientDescriptor object wrapping the given BasicClient strong pointer with
414 * values intialized from a prior ClientDescriptor.
415 */
416 static DescriptorPtr makeClientDescriptor(const sp<BasicClient>& value,
417 const CameraService::DescriptorPtr& partial);
418
419 }; // class CameraClientManager
420
Mathias Agopian65ab4712010-07-14 17:59:35 -0700421private:
Igor Murashkin634a5152013-02-20 17:15:11 -0800422
Ruben Brunkcc776712015-02-17 20:18:47 -0800423 /**
424 * Container class for the state of each logical camera device, including: ID, status, and
425 * dependencies on other devices. The mapping of camera ID -> state saved in mCameraStates
426 * represents the camera devices advertised by the HAL (and any USB devices, when we add
427 * those).
428 *
429 * This container does NOT represent an active camera client. These are represented using
430 * the ClientDescriptors stored in mActiveClientManager.
431 */
432 class CameraState {
433 public:
434 /**
435 * Make a new CameraState and set the ID, cost, and conflicting devices using the values
436 * returned in the HAL's camera_info struct for each device.
437 */
438 CameraState(const String8& id, int cost, const std::set<String8>& conflicting);
439 virtual ~CameraState();
440
441 /**
442 * Return the status for this device.
443 *
444 * This method acquires mStatusLock.
445 */
446 ICameraServiceListener::Status getStatus() const;
447
448 /**
449 * This function updates the status for this camera device, unless the given status
450 * is in the given list of rejected status states, and execute the function passed in
451 * with a signature onStatusUpdateLocked(const String8&, ICameraServiceListener::Status)
452 * if the status has changed.
453 *
454 * This method is idempotent, and will not result in the function passed to
455 * onStatusUpdateLocked being called more than once for the same arguments.
456 * This method aquires mStatusLock.
457 */
458 template<class Func>
459 void updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
460 std::initializer_list<ICameraServiceListener::Status> rejectSourceStates,
461 Func onStatusUpdatedLocked);
462
463 /**
464 * Return the last set CameraParameters object generated from the information returned by
465 * the HAL for this device (or an empty CameraParameters object if none has been set).
466 */
467 CameraParameters getShimParams() const;
468
469 /**
470 * Set the CameraParameters for this device.
471 */
472 void setShimParams(const CameraParameters& params);
473
474 /**
475 * Return the resource_cost advertised by the HAL for this device.
476 */
477 int getCost() const;
478
479 /**
480 * Return a set of the IDs of conflicting devices advertised by the HAL for this device.
481 */
482 std::set<String8> getConflicting() const;
483
484 /**
485 * Return the ID of this camera device.
486 */
487 String8 getId() const;
488
489 private:
490 const String8 mId;
491 ICameraServiceListener::Status mStatus; // protected by mStatusLock
492 const int mCost;
493 std::set<String8> mConflicting;
494 mutable Mutex mStatusLock;
495 CameraParameters mShimParams;
496 }; // class CameraState
497
Igor Murashkin634a5152013-02-20 17:15:11 -0800498 // Delay-load the Camera HAL module
499 virtual void onFirstRef();
500
Ruben Brunkcc776712015-02-17 20:18:47 -0800501 // Check if we can connect, before we acquire the service lock.
502 status_t validateConnect(const String8& cameraId, /*inout*/int& clientUid) const;
Igor Murashkine6800ce2013-03-04 17:25:57 -0800503
Ruben Brunkcc776712015-02-17 20:18:47 -0800504 // Handle active client evictions, and update service state.
505 // Only call with with mServiceLock held.
506 status_t handleEvictionsLocked(const String8& cameraId, int clientPid,
507 apiLevel effectiveApiLevel, const sp<IBinder>& remoteCallback, const String8& packageName,
508 /*out*/
509 sp<BasicClient>* client,
510 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>>* partial);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800511
Ruben Brunkcc776712015-02-17 20:18:47 -0800512 // Single implementation shared between the various connect calls
513 template<class CALLBACK, class CLIENT>
514 status_t connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId, int halVersion,
515 const String16& clientPackageName, int clientUid, apiLevel effectiveApiLevel,
516 bool legacyMode, bool shimUpdateOnly, /*out*/sp<CLIENT>& device);
Igor Murashkine6800ce2013-03-04 17:25:57 -0800517
Igor Murashkin634a5152013-02-20 17:15:11 -0800518
Ruben Brunkcc776712015-02-17 20:18:47 -0800519 // Lock guarding camera service state
Mathias Agopian65ab4712010-07-14 17:59:35 -0700520 Mutex mServiceLock;
Ruben Brunkcc776712015-02-17 20:18:47 -0800521
522 // Condition to use with mServiceLock, used to handle simultaneous connect calls from clients
523 std::shared_ptr<WaitableMutexWrapper> mServiceLockWrapper;
524
525 // Return NO_ERROR if the device with a give ID can be connected to
526 status_t checkIfDeviceIsUsable(const String8& cameraId) const;
527
528 // Container for managing currently active application-layer clients
529 CameraClientManager mActiveClientManager;
530
531 // Mapping from camera ID -> state for each device, map is protected by mCameraStatesLock
532 std::map<String8, std::shared_ptr<CameraState>> mCameraStates;
533
534 // Mutex guarding mCameraStates map
535 mutable Mutex mCameraStatesLock;
536
537 // Circular buffer for storing event logging for dumps
538 RingBuffer<String8> mEventLog;
539
540 /**
541 * Get the camera state for a given camera id.
542 *
543 * This acquires mCameraStatesLock.
544 */
545 std::shared_ptr<CameraService::CameraState> getCameraState(const String8& cameraId) const;
546
547 /**
548 * Evict client who's remote binder has died. Returns true if this client was in the active
549 * list and was disconnected.
550 *
551 * This method acquires mServiceLock.
552 */
553 bool evictClientIdByRemote(const wp<IBinder>& cameraClient);
554
555 /**
556 * Remove the given client from the active clients list; does not disconnect the client.
557 *
558 * This method acquires mServiceLock.
559 */
560 void removeByClient(const BasicClient* client);
561
562 /**
563 * Add new client to active clients list after conflicting clients have disconnected using the
564 * values set in the partial descriptor passed in to construct the actual client descriptor.
565 * This is typically called at the end of a connect call.
566 *
567 * This method must be called with mServiceLock held.
568 */
569 void finishConnectLocked(const sp<BasicClient>& client, const DescriptorPtr& desc);
570
571 /**
572 * Returns the integer corresponding to the given camera ID string, or -1 on failure.
573 */
574 static int cameraIdToInt(const String8& cameraId);
575
576 /**
577 * Remove a single client corresponding to the given camera id from the list of active clients.
578 * If none exists, return an empty strongpointer.
579 *
580 * This method must be called with mServiceLock held.
581 */
582 sp<CameraService::BasicClient> removeClientLocked(const String8& cameraId);
583
584 /**
585 * Add a event log message that a client has been disconnected.
586 */
587 void logDisconnected(const String8& cameraId, int clientPid, const String8& clientPackage);
588
589 /**
590 * Add a event log message that a client has been connected.
591 */
592 void logConnected(const String8& cameraId, int clientPid, const String8& clientPackage);
593
Mathias Agopian65ab4712010-07-14 17:59:35 -0700594 int mNumberOfCameras;
595
Mathias Agopian65ab4712010-07-14 17:59:35 -0700596 // sounds
Chih-Chung Changff4f55c2011-10-17 19:03:12 +0800597 MediaPlayer* newMediaPlayer(const char *file);
598
Mathias Agopian65ab4712010-07-14 17:59:35 -0700599 Mutex mSoundLock;
600 sp<MediaPlayer> mSoundPlayer[NUM_SOUNDS];
601 int mSoundRef; // reference count (release all MediaPlayer when 0)
602
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800603 CameraModule* mModule;
Igor Murashkinecf17e82012-10-02 16:05:11 -0700604
Ruben Brunkcc776712015-02-17 20:18:47 -0800605 // Guarded by mStatusListenerMutex
606 std::vector<sp<ICameraServiceListener>> mListenerList;
607 Mutex mStatusListenerLock;
Igor Murashkinbfc99152013-02-27 12:55:20 -0800608
Ruben Brunkcc776712015-02-17 20:18:47 -0800609 /**
610 * Update the status for the given camera id (if that device exists), and broadcast the
611 * status update to all current ICameraServiceListeners if the status has changed. Any
612 * statuses in rejectedSourceStates will be ignored.
613 *
614 * This method must be idempotent.
615 * This method acquires mStatusLock and mStatusListenerLock.
616 */
617 void updateStatus(ICameraServiceListener::Status status, const String8& cameraId,
618 std::initializer_list<ICameraServiceListener::Status> rejectedSourceStates);
619 void updateStatus(ICameraServiceListener::Status status, const String8& cameraId);
Igor Murashkinbfc99152013-02-27 12:55:20 -0800620
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800621 // flashlight control
622 sp<CameraFlashlight> mFlashlight;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800623 // guard mTorchStatusMap
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800624 Mutex mTorchStatusMutex;
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800625 // guard mTorchClientMap
626 Mutex mTorchClientMapMutex;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800627 // camera id -> torch status
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800628 KeyedVector<String8, ICameraServiceListener::TorchStatus> mTorchStatusMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800629 // camera id -> torch client binder
630 // only store the last client that turns on each camera's torch mode
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800631 KeyedVector<String8, sp<IBinder> > mTorchClientMap;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800632
633 // check and handle if torch client's process has died
634 void handleTorchClientBinderDied(const wp<IBinder> &who);
635
636 // handle torch mode status change and invoke callbacks. mTorchStatusMutex
637 // should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800638 void onTorchStatusChangedLocked(const String8& cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800639 ICameraServiceListener::TorchStatus newStatus);
640
641 // get a camera's torch status. mTorchStatusMutex should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800642 status_t getTorchStatusLocked(const String8 &cameraId,
643 ICameraServiceListener::TorchStatus *status) const;
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800644
645 // set a camera's torch status. mTorchStatusMutex should be locked.
Chien-Yu Chen88da5262015-02-17 13:56:46 -0800646 status_t setTorchStatusLocked(const String8 &cameraId,
Chien-Yu Chen3068d732015-02-09 13:29:57 -0800647 ICameraServiceListener::TorchStatus status);
648
Igor Murashkinecf17e82012-10-02 16:05:11 -0700649 // IBinder::DeathRecipient implementation
Igor Murashkinbfc99152013-02-27 12:55:20 -0800650 virtual void binderDied(const wp<IBinder> &who);
Igor Murashkin634a5152013-02-20 17:15:11 -0800651
652 // Helpers
Igor Murashkinbfc99152013-02-27 12:55:20 -0800653
Ruben Brunkd1176ef2014-02-21 10:51:38 -0800654 bool setUpVendorTags();
Ruben Brunkb2119af2014-05-09 19:57:56 -0700655
656 /**
Ruben Brunkb2119af2014-05-09 19:57:56 -0700657 * Initialize and cache the metadata used by the HAL1 shim for a given cameraId.
658 *
659 * Returns OK on success, or a negative error code.
660 */
661 status_t initializeShimMetadata(int cameraId);
662
663 /**
Igor Murashkin65d14b92014-06-17 12:03:20 -0700664 * Get the cached CameraParameters for the camera. If they haven't been
665 * cached yet, then initialize them for the first time.
666 *
667 * Returns OK on success, or a negative error code.
668 */
669 status_t getLegacyParametersLazy(int cameraId, /*out*/CameraParameters* parameters);
670
671 /**
Ruben Brunkb2119af2014-05-09 19:57:56 -0700672 * Generate the CameraCharacteristics metadata required by the Camera2 API
673 * from the available HAL1 CameraParameters and CameraInfo.
674 *
675 * Returns OK on success, or a negative error code.
676 */
677 status_t generateShimMetadata(int cameraId, /*out*/CameraMetadata* cameraInfo);
678
Ruben Brunkcc776712015-02-17 20:18:47 -0800679 static int getCallingPid();
680
681 static int getCallingUid();
682
Ruben Brunkb2119af2014-05-09 19:57:56 -0700683 /**
Ruben Brunkcc776712015-02-17 20:18:47 -0800684 * Get the current system time as a formatted string.
Ruben Brunkb2119af2014-05-09 19:57:56 -0700685 */
Ruben Brunkcc776712015-02-17 20:18:47 -0800686 static String8 getFormattedCurrentTime();
687
688 /**
689 * Get the camera eviction priority from the current process state given by ActivityManager.
690 */
691 static int getCameraPriorityFromProcState(int procState);
692
693 static status_t makeClient(const sp<CameraService>& cameraService,
694 const sp<IInterface>& cameraCb, const String16& packageName, const String8& cameraId,
695 int facing, int clientPid, uid_t clientUid, int servicePid, bool legacyMode,
696 int halVersion, int deviceVersion, apiLevel effectiveApiLevel,
697 /*out*/sp<BasicClient>* client);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700698};
699
Ruben Brunkcc776712015-02-17 20:18:47 -0800700template<class Func>
701void CameraService::CameraState::updateStatus(ICameraServiceListener::Status status,
702 const String8& cameraId,
703 std::initializer_list<ICameraServiceListener::Status> rejectSourceStates,
704 Func onStatusUpdatedLocked) {
705 Mutex::Autolock lock(mStatusLock);
706 ICameraServiceListener::Status oldStatus = mStatus;
707 mStatus = status;
708
709 if (oldStatus == status) {
710 return;
711 }
712
713 ALOGV("%s: Status has changed for camera ID %s from %#x to %#x", __FUNCTION__,
714 cameraId.string(), oldStatus, status);
715
716 if (oldStatus == ICameraServiceListener::STATUS_NOT_PRESENT &&
717 (status != ICameraServiceListener::STATUS_PRESENT &&
718 status != ICameraServiceListener::STATUS_ENUMERATING)) {
719
720 ALOGW("%s: From NOT_PRESENT can only transition into PRESENT or ENUMERATING",
721 __FUNCTION__);
722 mStatus = oldStatus;
723 return;
724 }
725
726 /**
727 * Sometimes we want to conditionally do a transition.
728 * For example if a client disconnects, we want to go to PRESENT
729 * only if we weren't already in NOT_PRESENT or ENUMERATING.
730 */
731 for (auto& rejectStatus : rejectSourceStates) {
732 if (oldStatus == rejectStatus) {
733 ALOGV("%s: Rejecting status transition for Camera ID %s, since the source "
734 "state was was in one of the bad states.", __FUNCTION__, cameraId.string());
735 mStatus = oldStatus;
736 return;
737 }
738 }
739
740 onStatusUpdatedLocked(cameraId, status);
741}
742
743
744template<class CALLBACK, class CLIENT>
745status_t CameraService::connectHelper(const sp<CALLBACK>& cameraCb, const String8& cameraId,
746 int halVersion, const String16& clientPackageName, int clientUid,
747 apiLevel effectiveApiLevel, bool legacyMode, bool shimUpdateOnly,
748 /*out*/sp<CLIENT>& device) {
749 status_t ret = NO_ERROR;
750 String8 clientName8(clientPackageName);
751 int clientPid = getCallingPid();
752
753 ALOGI("CameraService::connect call E (PID %d \"%s\", camera ID %s) for HAL version %d and "
754 "Camera API version %d", clientPid, clientName8.string(), cameraId.string(),
755 halVersion, static_cast<int>(effectiveApiLevel));
756
757 // Enforce client permissions and do basic sanity checks
758 if((ret = validateConnect(cameraId, /*inout*/clientUid)) != NO_ERROR) {
759 return ret;
760 }
761
762 sp<CLIENT> client = nullptr;
763 {
764 // Acquire mServiceLock and prevent other clients from connecting
765 std::unique_ptr<AutoConditionLock> lock =
766 AutoConditionLock::waitAndAcquire(mServiceLockWrapper, DEFAULT_CONNECT_TIMEOUT_NS);
767 if (lock == nullptr) {
768 ALOGE("CameraService::connect X (PID %d) rejected (too many other clients connecting)."
769 , clientPid);
770 return -EBUSY;
771 }
772
773 // Check the shim parameters after acquiring lock, if they have already been updated and
774 // we were doing a shim update, return immediately
775 if (shimUpdateOnly) {
776 auto cameraState = getCameraState(cameraId);
777 if (cameraState != nullptr) {
778 if (!cameraState->getShimParams().isEmpty()) return NO_ERROR;
779 }
780 }
781
782 sp<BasicClient> clientTmp = nullptr;
783 std::shared_ptr<resource_policy::ClientDescriptor<String8, sp<BasicClient>>> partial;
784 if ((ret = handleEvictionsLocked(cameraId, clientPid, effectiveApiLevel,
785 IInterface::asBinder(cameraCb), clientName8, /*out*/&clientTmp,
786 /*out*/&partial)) != NO_ERROR) {
787 return ret;
788 }
789
790 if (clientTmp.get() != nullptr) {
791 // Handle special case for API1 MediaRecorder where the existing client is returned
792 device = static_cast<CLIENT*>(clientTmp.get());
793 return NO_ERROR;
794 }
795
796 // give flashlight a chance to close devices if necessary.
797 mFlashlight->prepareDeviceOpen(cameraId);
798
799 // TODO: Update getDeviceVersion + HAL interface to use strings for Camera IDs
800 int id = cameraIdToInt(cameraId);
801 if (id == -1) {
802 ALOGE("%s: Invalid camera ID %s, cannot get device version from HAL.", __FUNCTION__,
803 cameraId.string());
804 return BAD_VALUE;
805 }
806
807 int facing = -1;
808 int deviceVersion = getDeviceVersion(id, /*out*/&facing);
809 sp<BasicClient> tmp = nullptr;
810 if((ret = makeClient(this, cameraCb, clientPackageName, cameraId, facing, clientPid,
811 clientUid, getpid(), legacyMode, halVersion, deviceVersion, effectiveApiLevel,
812 /*out*/&tmp)) != NO_ERROR) {
813 return ret;
814 }
815 client = static_cast<CLIENT*>(tmp.get());
816
817 LOG_ALWAYS_FATAL_IF(client.get() == nullptr, "%s: CameraService in invalid state",
818 __FUNCTION__);
819
820 if ((ret = client->initialize(mModule)) != OK) {
821 ALOGE("%s: Could not initialize client from HAL module.", __FUNCTION__);
822 return ret;
823 }
824
825 sp<IBinder> remoteCallback = client->getRemote();
826 if (remoteCallback != nullptr) {
827 remoteCallback->linkToDeath(this);
828 }
829
830 // Update shim paremeters for legacy clients
831 if (effectiveApiLevel == API_1) {
832 // Assume we have always received a Client subclass for API1
833 sp<Client> shimClient = reinterpret_cast<Client*>(client.get());
834 String8 rawParams = shimClient->getParameters();
835 CameraParameters params(rawParams);
836
837 auto cameraState = getCameraState(cameraId);
838 if (cameraState != nullptr) {
839 cameraState->setShimParams(params);
840 } else {
841 ALOGE("%s: Cannot update shim parameters for camera %s, no such device exists.",
842 __FUNCTION__, cameraId.string());
843 }
844 }
845
846 if (shimUpdateOnly) {
847 // If only updating legacy shim parameters, immediately disconnect client
848 mServiceLock.unlock();
849 client->disconnect();
850 mServiceLock.lock();
851 } else {
852 // Otherwise, add client to active clients list
853 finishConnectLocked(client, partial);
854 }
855 } // lock is destroyed, allow further connect calls
856
857 // Important: release the mutex here so the client can call back into the service from its
858 // destructor (can be at the end of the call)
859 device = client;
860 return NO_ERROR;
861}
862
Mathias Agopian65ab4712010-07-14 17:59:35 -0700863} // namespace android
864
865#endif