1 /* 2 * Copyright (C) 2012 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 */ 16 17 #ifndef ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H 18 #define ANDROID_SERVERS_CAMERA_CAMERA2CLIENT_H 19 20 #include <atomic> 21 22 #include <gui/Flags.h> 23 #include <gui/view/Surface.h> 24 #include <media/RingBuffer.h> 25 #include "CameraService.h" 26 #include "api1/client2/FrameProcessor.h" 27 #include "api1/client2/Parameters.h" 28 #include "common/Camera2ClientBase.h" 29 #include "common/CameraDeviceBase.h" 30 31 namespace android { 32 33 namespace camera2 { 34 35 class StreamingProcessor; 36 class JpegProcessor; 37 class ZslProcessor; 38 class CaptureSequencer; 39 class CallbackProcessor; 40 41 } 42 43 class IMemory; 44 /** 45 * Interface between android.hardware.Camera API and Camera HAL device for versions 46 * CAMERA_DEVICE_API_VERSION_3_0 and above. 47 */ 48 class Camera2Client : 49 public Camera2ClientBase<CameraService::Client> 50 { 51 public: 52 /** 53 * ICamera interface (see ICamera for details) 54 */ 55 56 virtual binder::Status disconnect(); 57 virtual status_t connect(const sp<hardware::ICameraClient>& client); 58 virtual status_t lock(); 59 virtual status_t unlock(); 60 virtual status_t setPreviewTarget(const sp<SurfaceType>& target); 61 virtual void setPreviewCallbackFlag(int flag); 62 virtual status_t setPreviewCallbackTarget(const sp<SurfaceType>& target); 63 64 virtual status_t startPreview(); 65 virtual void stopPreview(); 66 virtual bool previewEnabled(); 67 virtual status_t setVideoBufferMode(int32_t videoBufferMode); 68 virtual status_t startRecording(); 69 virtual void stopRecording(); 70 virtual bool recordingEnabled(); 71 virtual void releaseRecordingFrame(const sp<IMemory>& mem); 72 virtual void releaseRecordingFrameHandle(native_handle_t *handle); 73 virtual void releaseRecordingFrameHandleBatch( 74 const std::vector<native_handle_t*>& handles); 75 virtual status_t autoFocus(); 76 virtual status_t cancelAutoFocus(); 77 virtual status_t takePicture(int msgType); 78 virtual status_t setParameters(const String8& params); 79 virtual String8 getParameters() const; 80 virtual status_t sendCommand(int32_t cmd, int32_t arg1, int32_t arg2); 81 virtual void notifyError(int32_t errorCode, 82 const CaptureResultExtras& resultExtras); 83 virtual status_t setVideoTarget(const sp<SurfaceType>& target); 84 virtual status_t setAudioRestriction(int mode); 85 virtual int32_t getGlobalAudioRestriction(); 86 virtual status_t setRotateAndCropOverride(uint8_t rotateAndCrop, bool fromHal = false); 87 virtual status_t setAutoframingOverride(uint8_t autoframingMode); 88 89 virtual bool supportsCameraMute(); 90 virtual status_t setCameraMute(bool enabled); 91 92 virtual status_t setCameraServiceWatchdog(bool enabled); 93 94 virtual void setStreamUseCaseOverrides( 95 const std::vector<int64_t>& useCaseOverrides); 96 virtual void clearStreamUseCaseOverrides(); 97 98 virtual bool supportsZoomOverride(); 99 virtual status_t setZoomOverride(int32_t zoomOverride); 100 101 /** 102 * Interface used by CameraService 103 */ 104 105 Camera2Client(const sp<CameraService>& cameraService, 106 const sp<hardware::ICameraClient>& cameraClient, 107 std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper, 108 std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils, 109 const AttributionSourceState& clientAttribution, int callingPid, 110 const std::string& cameraDeviceId, int api1CameraId, int cameraFacing, 111 int sensorOrientation, int servicePid, bool overrideForPerfClass, 112 int rotationOverride, bool forceSlowJpegMode, bool sharedMode); 113 114 virtual ~Camera2Client(); 115 116 virtual status_t initialize(sp<CameraProviderManager> manager, 117 const std::string& monitorTags) override; 118 119 virtual status_t dump(int fd, const Vector<String16>& args); 120 121 virtual status_t dumpClient(int fd, const Vector<String16>& args); 122 123 /** 124 * Interface used by CameraDeviceBase 125 */ 126 127 virtual void notifyAutoFocus(uint8_t newState, int triggerId); 128 virtual void notifyAutoExposure(uint8_t newState, int triggerId); 129 virtual void notifyShutter(const CaptureResultExtras& resultExtras, 130 nsecs_t timestamp); 131 132 /** 133 * Interface used by independent components of Camera2Client. 134 */ 135 136 camera2::SharedParameters& getParameters(); 137 138 void notifyRequestId(int32_t requestId); 139 140 int getPreviewStreamId() const; 141 int getCaptureStreamId() const; 142 int getCallbackStreamId() const; 143 int getRecordingStreamId() const; 144 int getZslStreamId() const; 145 146 status_t registerFrameListener(int32_t minId, int32_t maxId, 147 const wp<camera2::FrameProcessor::FilteredListener>& listener, 148 bool sendPartials = true); 149 status_t removeFrameListener(int32_t minId, int32_t maxId, 150 const wp<camera2::FrameProcessor::FilteredListener>& listener); 151 152 status_t stopStream(); 153 154 // For the slowJpegMode to create jpeg stream when precapture sequence is done 155 status_t createJpegStreamL(camera2::Parameters ¶ms); 156 157 static size_t calculateBufferSize(int width, int height, 158 int format, int stride); 159 160 static const int32_t kPreviewRequestIdStart = 10000000; 161 static const int32_t kPreviewRequestIdEnd = 20000000; 162 163 static const int32_t kRecordingRequestIdStart = 20000000; 164 static const int32_t kRecordingRequestIdEnd = 30000000; 165 166 static const int32_t kCaptureRequestIdStart = 30000000; 167 static const int32_t kCaptureRequestIdEnd = 40000000; 168 169 // Constant strings for ATRACE logging 170 static const char* kAutofocusLabel; 171 static const char* kTakepictureLabel; 172 173 // Used with stream IDs 174 static const int NO_STREAM = -1; 175 176 private: 177 /** ICamera interface-related private members */ 178 typedef camera2::Parameters Parameters; 179 180 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES 181 status_t setPreviewWindowL(const view::Surface& viewSurface, const sp<Surface>& window); 182 #else 183 status_t setPreviewWindowL(const sp<IBinder>& binder, 184 const sp<Surface>& window); 185 #endif 186 status_t startPreviewL(Parameters ¶ms, bool restart); 187 void stopPreviewL(); 188 status_t startRecordingL(Parameters ¶ms, bool restart); 189 bool recordingEnabledL(); 190 191 // Individual commands for sendCommand() 192 status_t commandStartSmoothZoomL(); 193 status_t commandStopSmoothZoomL(); 194 status_t commandSetDisplayOrientationL(int degrees); 195 status_t commandEnableShutterSoundL(bool enable); 196 status_t commandPlayRecordingSoundL(); 197 status_t commandStartFaceDetectionL(int type); 198 status_t commandStopFaceDetectionL(Parameters ¶ms); 199 status_t commandEnableFocusMoveMsgL(bool enable); 200 status_t commandPingL(); 201 status_t commandSetVideoBufferCountL(size_t count); 202 status_t commandSetVideoFormatL(int format, android_dataspace dataSpace); 203 204 // Current camera device configuration 205 camera2::SharedParameters mParameters; 206 207 /** Camera device-related private members */ 208 209 void setPreviewCallbackFlagL(Parameters ¶ms, int flag); 210 status_t updateRequests(Parameters ¶ms); 211 212 template <typename ProcessorT> 213 status_t updateProcessorStream(sp<ProcessorT> processor, Parameters params); 214 template <typename ProcessorT, 215 status_t (ProcessorT::*updateStreamF)(const Parameters &)> 216 status_t updateProcessorStream(sp<ProcessorT> processor, Parameters params); 217 218 sp<camera2::FrameProcessor> mFrameProcessor; 219 220 /* Preview/Recording related members */ 221 222 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES 223 uint64_t mPreviewViewSurfaceID; 224 uint64_t mVideoSurfaceID; 225 #else 226 sp<IBinder> mPreviewSurface; 227 sp<IBinder> mVideoSurface; 228 #endif 229 sp<camera2::StreamingProcessor> mStreamingProcessor; 230 231 /** Preview callback related members */ 232 233 sp<camera2::CallbackProcessor> mCallbackProcessor; 234 235 /* Still image capture related members */ 236 237 sp<camera2::CaptureSequencer> mCaptureSequencer; 238 sp<camera2::JpegProcessor> mJpegProcessor; 239 sp<camera2::ZslProcessor> mZslProcessor; 240 241 std::atomic<bool> mInitialized; 242 243 /** Utility members */ 244 bool mLegacyMode; 245 246 // Wait until the camera device has received the latest control settings 247 status_t syncWithDevice(); 248 249 // Video snapshot jpeg size overriding helper function 250 status_t overrideVideoSnapshotSize(Parameters ¶ms); 251 252 template<typename TProviderPtr> 253 status_t initializeImpl(TProviderPtr providerPtr, const std::string& monitorTags); 254 255 bool isZslEnabledInStillTemplate(); 256 // The current rotate & crop mode passed by camera service 257 uint8_t mRotateAndCropMode; 258 // Synchronize access to 'mRotateAndCropMode' 259 mutable Mutex mRotateAndCropLock; 260 // Contains the preview stream transformation that would normally be applied 261 // when the display rotation is 0 262 int mRotateAndCropPreviewTransform; 263 // Flag indicating camera device support for the rotate & crop interface 264 bool mRotateAndCropIsSupported; 265 266 mutable Mutex mLatestRequestMutex; 267 Condition mLatestRequestSignal; 268 static constexpr size_t kMaxRequestIds = BufferQueueDefs::NUM_BUFFER_SLOTS; 269 RingBuffer<int32_t> mLatestRequestIds, mLatestFailedRequestIds; 270 status_t waitUntilRequestIdApplied(int32_t requestId, nsecs_t timeout); 271 status_t waitUntilCurrentRequestIdLocked(); 272 }; 273 274 }; // namespace android 275 276 #endif 277