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