blob: 295ab29761180dd77a4391a66cdc229ba2d06796 [file] [log] [blame]
Igor Murashkine7ee7632013-06-11 18:10:18 -07001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Igor Murashkine7ee7632013-06-11 18:10:18 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H
18#define ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H
19
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080020#include <android/hardware/camera2/BnCameraDeviceUser.h>
21#include <android/hardware/camera2/ICameraDeviceCallbacks.h>
Yin-Chia Yehb97babb2015-03-12 13:42:44 -070022#include <camera/camera2/OutputConfiguration.h>
Emilian Peev35ae8262018-11-08 13:11:32 +000023#include <camera/camera2/SessionConfiguration.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080024#include <camera/camera2/SubmitInfo.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070025
Yin-Chia Yehb978c382019-10-30 00:22:37 -070026#include "CameraOfflineSessionClient.h"
Igor Murashkine7ee7632013-06-11 18:10:18 -070027#include "CameraService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070028#include "common/FrameProcessorBase.h"
29#include "common/Camera2ClientBase.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000030#include "CompositeStream.h"
Igor Murashkine7ee7632013-06-11 18:10:18 -070031
Emilian Peev40ead602017-09-26 15:46:36 +010032using android::camera3::OutputStreamInfo;
Emilian Peev538c90e2018-12-17 18:03:19 +000033using android::camera3::CompositeStream;
Emilian Peev40ead602017-09-26 15:46:36 +010034
Igor Murashkine7ee7632013-06-11 18:10:18 -070035namespace android {
36
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080037struct CameraDeviceClientBase :
38 public CameraService::BasicClient,
39 public hardware::camera2::BnCameraDeviceUser
Igor Murashkine7ee7632013-06-11 18:10:18 -070040{
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080041 typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
Igor Murashkine7ee7632013-06-11 18:10:18 -070042
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080043 const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
Igor Murashkine7ee7632013-06-11 18:10:18 -070044 return mRemoteCallback;
45 }
46
47protected:
48 CameraDeviceClientBase(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080049 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -070050 const String16& clientPackageName,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -080051 const std::unique_ptr<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080052 const String8& cameraId,
Yin-Chia Yehc3e9d6f2018-02-06 10:56:32 -080053 int api1CameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -070054 int cameraFacing,
55 int clientPid,
56 uid_t clientUid,
57 int servicePid);
58
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080059 sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
Igor Murashkine7ee7632013-06-11 18:10:18 -070060};
61
62/**
63 * Implements the binder ICameraDeviceUser API,
64 * meant for HAL3-public implementation of
65 * android.hardware.photography.CameraDevice
66 */
67class CameraDeviceClient :
68 public Camera2ClientBase<CameraDeviceClientBase>,
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070069 public camera2::FrameProcessorBase::FilteredListener
Igor Murashkine7ee7632013-06-11 18:10:18 -070070{
71public:
72 /**
73 * ICameraDeviceUser interface (see ICameraDeviceUser for details)
74 */
75
76 // Note that the callee gets a copy of the metadata.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080077 virtual binder::Status submitRequest(
78 const hardware::camera2::CaptureRequest& request,
79 bool streaming = false,
80 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080081 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Jianing Wei90e59c92014-03-12 18:29:36 -070082 // List of requests are copied.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080083 virtual binder::Status submitRequestList(
84 const std::vector<hardware::camera2::CaptureRequest>& requests,
85 bool streaming = false,
86 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080087 hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080088 virtual binder::Status cancelRequest(int requestId,
89 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080090 int64_t* lastFrameNumber = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -070091
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080092 virtual binder::Status beginConfigure() override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070093
Emilian Peev5fbe0ba2017-10-20 15:45:45 +010094 virtual binder::Status endConfigure(int operatingMode,
95 const hardware::camera2::impl::CameraMetadataNative& sessionParams) override;
Ruben Brunkb2119af2014-05-09 19:57:56 -070096
Emilian Peev35ae8262018-11-08 13:11:32 +000097 // Verify specific session configuration.
98 virtual binder::Status isSessionConfigurationSupported(
99 const SessionConfiguration& sessionConfiguration,
100 /*out*/
101 bool* streamStatus) override;
102
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700103 // Returns -EBUSY if device is not idle or in error state
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800104 virtual binder::Status deleteStream(int streamId) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700105
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800106 virtual binder::Status createStream(
107 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
108 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800109 int32_t* newStreamId = NULL) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700110
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700111 // Create an input stream of width, height, and format.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800112 virtual binder::Status createInputStream(int width, int height, int format,
113 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800114 int32_t* newStreamId = NULL) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700115
116 // Get the buffer producer of the input stream
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800117 virtual binder::Status getInputSurface(
118 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800119 view::Surface *inputSurface) override;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700120
Igor Murashkine7ee7632013-06-11 18:10:18 -0700121 // Create a request object from a template.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800122 virtual binder::Status createDefaultRequest(int templateId,
123 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800124 hardware::camera2::impl::CameraMetadataNative* request) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700125
126 // Get the static metadata for the camera
127 // -- Caller owns the newly allocated metadata
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800128 virtual binder::Status getCameraInfo(
129 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800130 hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700131
Zhijun He2ab500c2013-07-23 08:02:53 -0700132 // Wait until all the submitted requests have finished processing
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800133 virtual binder::Status waitUntilIdle() override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700134
135 // Flush all active and pending requests as fast as possible
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800136 virtual binder::Status flush(
137 /*out*/
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800138 int64_t* lastFrameNumber = NULL) override;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700139
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700140 // Prepare stream by preallocating its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800141 virtual binder::Status prepare(int32_t streamId) override;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700142
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700143 // Tear down stream resources by freeing its unused buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800144 virtual binder::Status tearDown(int32_t streamId) override;
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700145
Ruben Brunkc78ac262015-08-13 17:58:46 -0700146 // Prepare stream by preallocating up to maxCount of its buffers
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800147 virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
Ruben Brunkc78ac262015-08-13 17:58:46 -0700148
Emilian Peev40ead602017-09-26 15:46:36 +0100149 // Update an output configuration
150 virtual binder::Status updateOutputConfiguration(int streamId,
151 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
152
Shuzhen Wang758c2152017-01-10 18:26:18 -0800153 // Finalize the output configurations with surfaces not added before.
154 virtual binder::Status finalizeOutputConfigurations(int32_t streamId,
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800155 const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
Zhijun He5d677d12016-05-29 16:52:39 -0700156
Yin-Chia Yehcfab4e12019-09-09 13:08:28 -0700157 virtual binder::Status setCameraAudioRestriction(int32_t mode) override;
158
159 virtual binder::Status getGlobalAudioRestriction(/*out*/int32_t* outMode) override;
Yin-Chia Yehdba03232019-08-19 15:54:28 -0700160
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700161 virtual binder::Status switchToOffline(
162 const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
Emilian Peevb2bc5a42019-11-20 16:02:14 -0800163 const std::vector<int>& offlineOutputIds,
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700164 /*out*/
165 sp<hardware::camera2::ICameraOfflineSession>* session) override;
166
Igor Murashkine7ee7632013-06-11 18:10:18 -0700167 /**
168 * Interface used by CameraService
169 */
170
171 CameraDeviceClient(const sp<CameraService>& cameraService,
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800172 const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700173 const String16& clientPackageName,
Philip P. Moltmann9e648f62019-11-04 12:52:45 -0800174 const std::unique_ptr<String16>& clientFeatureId,
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800175 const String8& cameraId,
Igor Murashkine7ee7632013-06-11 18:10:18 -0700176 int cameraFacing,
177 int clientPid,
178 uid_t clientUid,
179 int servicePid);
180 virtual ~CameraDeviceClient();
181
Emilian Peevbd8c5032018-02-14 23:05:40 +0000182 virtual status_t initialize(sp<CameraProviderManager> manager,
183 const String8& monitorTags) override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700184
185 virtual status_t dump(int fd, const Vector<String16>& args);
186
Eino-Ville Talvalac4003962016-01-13 10:07:04 -0800187 virtual status_t dumpClient(int fd, const Vector<String16>& args);
188
Igor Murashkine7ee7632013-06-11 18:10:18 -0700189 /**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700190 * Device listener interface
191 */
192
193 virtual void notifyIdle();
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800194 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700195 const CaptureResultExtras& resultExtras);
196 virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700197 virtual void notifyPrepared(int streamId);
Shuzhen Wang9d066012016-09-30 11:30:20 -0700198 virtual void notifyRequestQueueEmpty();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700199 virtual void notifyRepeatingRequestError(long lastFrameNumber);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700200
201 /**
Igor Murashkine7ee7632013-06-11 18:10:18 -0700202 * Interface used by independent components of CameraDeviceClient.
203 */
204protected:
205 /** FilteredListener implementation **/
Jianing Weicb0652e2014-03-12 18:29:36 -0700206 virtual void onResultAvailable(const CaptureResult& result);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700207 virtual void detachDevice();
208
Igor Murashkinf8b2a6f2013-09-17 17:03:28 -0700209 // Calculate the ANativeWindow transform from android.sensor.orientation
210 status_t getRotationTransformLocked(/*out*/int32_t* transform);
211
Igor Murashkine7ee7632013-06-11 18:10:18 -0700212private:
Shuzhen Wang0129d522016-10-30 22:43:41 -0700213 // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface.
214 // streamId specifies the index of the stream the surface belongs to, and the
215 // surfaceId specifies the index of the surface within the stream. (one stream
216 // could contain multiple surfaces.)
217 class StreamSurfaceId final {
218 public:
219 StreamSurfaceId() {
220 mStreamId = -1;
221 mSurfaceId = -1;
222 }
223 StreamSurfaceId(int32_t streamId, int32_t surfaceId) {
224 mStreamId = streamId;
225 mSurfaceId = surfaceId;
226 }
227 int32_t streamId() const {
228 return mStreamId;
229 }
230 int32_t surfaceId() const {
231 return mSurfaceId;
232 }
233
234 private:
235 int32_t mStreamId;
236 int32_t mSurfaceId;
237
238 }; // class StreamSurfaceId
239
240private:
Igor Murashkine7ee7632013-06-11 18:10:18 -0700241 /** ICameraDeviceUser interface-related private members */
242
243 /** Preview callback related members */
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -0700244 sp<camera2::FrameProcessorBase> mFrameProcessor;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700245 static const int32_t FRAME_PROCESSOR_LISTENER_MIN_ID = 0;
246 static const int32_t FRAME_PROCESSOR_LISTENER_MAX_ID = 0x7fffffffL;
247
Emilian Peev00420d22018-02-05 21:33:13 +0000248 std::vector<int32_t> mSupportedPhysicalRequestKeys;
249
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800250 template<typename TProviderPtr>
Emilian Peevbd8c5032018-02-14 23:05:40 +0000251 status_t initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -0800252
Igor Murashkine7ee7632013-06-11 18:10:18 -0700253 /** Utility members */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800254 binder::Status checkPidStatus(const char* checkLocation);
Emilian Peev35ae8262018-11-08 13:11:32 +0000255 binder::Status checkOperatingModeLocked(int operatingMode) const;
256 binder::Status checkPhysicalCameraIdLocked(String8 physicalCameraId);
257 binder::Status checkSurfaceTypeLocked(size_t numBufferProducers, bool deferredConsumer,
258 int surfaceType) const;
259 static void mapStreamInfo(const OutputStreamInfo &streamInfo,
260 camera3_stream_rotation_t rotation, String8 physicalId,
261 hardware::camera::device::V3_4::Stream *stream /*out*/);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700262 bool enforceRequestPermissions(CameraMetadata& metadata);
263
Ruben Brunkbba75572014-11-20 17:29:50 -0800264 // Find the square of the euclidean distance between two points
265 static int64_t euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1);
266
Zhijun He5d677d12016-05-29 16:52:39 -0700267 // Create an output stream with surface deferred for future.
268 binder::Status createDeferredSurfaceStreamLocked(
269 const hardware::camera2::params::OutputConfiguration &outputConfiguration,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800270 bool isShared,
Zhijun He5d677d12016-05-29 16:52:39 -0700271 int* newStreamId = NULL);
272
273 // Set the stream transform flags to automatically rotate the camera stream for preview use
274 // cases.
275 binder::Status setStreamTransformLocked(int streamId);
276
Ruben Brunkbba75572014-11-20 17:29:50 -0800277 // Find the closest dimensions for a given format in available stream configurations with
278 // a width <= ROUNDING_WIDTH_CAP
Ruben Brunk77a77f62015-06-12 16:15:35 -0700279 static const int32_t ROUNDING_WIDTH_CAP = 1920;
Ruben Brunkbba75572014-11-20 17:29:50 -0800280 static bool roundBufferDimensionNearest(int32_t width, int32_t height, int32_t format,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800281 android_dataspace dataSpace, const CameraMetadata& info,
282 /*out*/int32_t* outWidth, /*out*/int32_t* outHeight);
Ruben Brunkbba75572014-11-20 17:29:50 -0800283
Eman Copty6d7af0e2016-06-17 20:46:40 -0700284 //check if format is not custom format
285 static bool isPublicFormat(int32_t format);
286
Shuzhen Wang758c2152017-01-10 18:26:18 -0800287 // Create a Surface from an IGraphicBufferProducer. Returns error if
288 // IGraphicBufferProducer's property doesn't match with streamInfo
289 binder::Status createSurfaceFromGbp(OutputStreamInfo& streamInfo, bool isStreamInfoValid,
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700290 sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp,
291 const String8& physicalCameraId);
Shuzhen Wang758c2152017-01-10 18:26:18 -0800292
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800293
294 // Utility method to insert the surface into SurfaceMap
295 binder::Status insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
Emilian Peevf873aa52018-01-26 14:58:28 +0000296 /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds,
297 /*out*/int32_t* currentStreamId);
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800298
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800299 // Check that the physicalCameraId passed in is spported by the camera
300 // device.
301 bool checkPhysicalCameraId(const String8& physicalCameraId);
302
Shuzhen Wang0129d522016-10-30 22:43:41 -0700303 // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams
304 KeyedVector<sp<IBinder>, StreamSurfaceId> mStreamMap;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700305
Yin-Chia Yeh4dfa4cc2017-11-10 20:00:09 -0800306 // Stream ID -> OutputConfiguration. Used for looking up Surface by stream/surface index
307 KeyedVector<int32_t, hardware::camera2::params::OutputConfiguration> mConfiguredOutputs;
308
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700309 struct InputStreamConfiguration {
310 bool configured;
311 int32_t width;
312 int32_t height;
313 int32_t format;
314 int32_t id;
315 } mInputStream;
316
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700317 // Streaming request ID
318 int32_t mStreamingRequestId;
Shuzhen Wangc9ca6782016-04-26 13:40:31 -0700319 Mutex mStreamingRequestIdLock;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700320 static const int32_t REQUEST_ID_NONE = -1;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700321
322 int32_t mRequestIdCounter;
Eino-Ville Talvala412fe562015-08-20 17:08:32 -0700323
Zhijun He5d677d12016-05-29 16:52:39 -0700324 // The list of output streams whose surfaces are deferred. We have to track them separately
325 // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
326 // Surface is configured, the stream id will be moved to mStreamMap.
327 Vector<int32_t> mDeferredStreams;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700328
Shuzhen Wang758c2152017-01-10 18:26:18 -0800329 // stream ID -> outputStreamInfo mapping
330 std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap;
331
Emilian Peev538c90e2018-12-17 18:03:19 +0000332 KeyedVector<sp<IBinder>, sp<CompositeStream>> mCompositeStreamMap;
333
Emilian Peev35ae8262018-11-08 13:11:32 +0000334 static const int32_t MAX_SURFACES_PER_STREAM = 4;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700335 sp<CameraProviderManager> mProviderManager;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700336};
337
338}; // namespace android
339
340#endif