• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,
107             const std::string& clientPackageName,
108             const std::optional<std::string>& clientFeatureId,
109             const std::string& cameraDeviceId,
110             int api1CameraId,
111             int cameraFacing,
112             int sensorOrientation,
113             int clientPid,
114             uid_t clientUid,
115             int servicePid,
116             bool overrideForPerfClass,
117             int rotationOverride,
118             bool forceSlowJpegMode);
119 
120     virtual ~Camera2Client();
121 
122     virtual status_t initialize(sp<CameraProviderManager> manager,
123             const std::string& monitorTags) override;
124 
125     virtual status_t dump(int fd, const Vector<String16>& args);
126 
127     virtual status_t dumpClient(int fd, const Vector<String16>& args);
128 
129     /**
130      * Interface used by CameraDeviceBase
131      */
132 
133     virtual void notifyAutoFocus(uint8_t newState, int triggerId);
134     virtual void notifyAutoExposure(uint8_t newState, int triggerId);
135     virtual void notifyShutter(const CaptureResultExtras& resultExtras,
136                                nsecs_t timestamp);
137 
138     /**
139      * Interface used by independent components of Camera2Client.
140      */
141 
142     camera2::SharedParameters& getParameters();
143 
144     void notifyRequestId(int32_t requestId);
145 
146     int getPreviewStreamId() const;
147     int getCaptureStreamId() const;
148     int getCallbackStreamId() const;
149     int getRecordingStreamId() const;
150     int getZslStreamId() const;
151 
152     status_t registerFrameListener(int32_t minId, int32_t maxId,
153             const wp<camera2::FrameProcessor::FilteredListener>& listener,
154             bool sendPartials = true);
155     status_t removeFrameListener(int32_t minId, int32_t maxId,
156             const wp<camera2::FrameProcessor::FilteredListener>& listener);
157 
158     status_t stopStream();
159 
160     // For the slowJpegMode to create jpeg stream when precapture sequence is done
161     status_t createJpegStreamL(camera2::Parameters &params);
162 
163     static size_t calculateBufferSize(int width, int height,
164             int format, int stride);
165 
166     static const int32_t kPreviewRequestIdStart = 10000000;
167     static const int32_t kPreviewRequestIdEnd   = 20000000;
168 
169     static const int32_t kRecordingRequestIdStart  = 20000000;
170     static const int32_t kRecordingRequestIdEnd    = 30000000;
171 
172     static const int32_t kCaptureRequestIdStart = 30000000;
173     static const int32_t kCaptureRequestIdEnd   = 40000000;
174 
175     // Constant strings for ATRACE logging
176     static const char* kAutofocusLabel;
177     static const char* kTakepictureLabel;
178 
179     // Used with stream IDs
180     static const int NO_STREAM = -1;
181 
182 private:
183     /** ICamera interface-related private members */
184     typedef camera2::Parameters Parameters;
185 
186     status_t setPreviewWindowL(const sp<IBinder>& binder,
187             const sp<Surface>& window);
188     status_t startPreviewL(Parameters &params, bool restart);
189     void     stopPreviewL();
190     status_t startRecordingL(Parameters &params, bool restart);
191     bool     recordingEnabledL();
192 
193     // Individual commands for sendCommand()
194     status_t commandStartSmoothZoomL();
195     status_t commandStopSmoothZoomL();
196     status_t commandSetDisplayOrientationL(int degrees);
197     status_t commandEnableShutterSoundL(bool enable);
198     status_t commandPlayRecordingSoundL();
199     status_t commandStartFaceDetectionL(int type);
200     status_t commandStopFaceDetectionL(Parameters &params);
201     status_t commandEnableFocusMoveMsgL(bool enable);
202     status_t commandPingL();
203     status_t commandSetVideoBufferCountL(size_t count);
204     status_t commandSetVideoFormatL(int format, android_dataspace dataSpace);
205 
206     // Current camera device configuration
207     camera2::SharedParameters mParameters;
208 
209     /** Camera device-related private members */
210 
211     void     setPreviewCallbackFlagL(Parameters &params, int flag);
212     status_t updateRequests(Parameters &params);
213 
214     template <typename ProcessorT>
215     status_t updateProcessorStream(sp<ProcessorT> processor, Parameters params);
216     template <typename ProcessorT,
217               status_t (ProcessorT::*updateStreamF)(const Parameters &)>
218     status_t updateProcessorStream(sp<ProcessorT> processor, Parameters params);
219 
220     sp<camera2::FrameProcessor> mFrameProcessor;
221 
222     /* Preview/Recording related members */
223 
224     sp<IBinder> mPreviewSurface;
225     sp<IBinder> mVideoSurface;
226     sp<camera2::StreamingProcessor> mStreamingProcessor;
227 
228     /** Preview callback related members */
229 
230     sp<camera2::CallbackProcessor> mCallbackProcessor;
231 
232     /* Still image capture related members */
233 
234     sp<camera2::CaptureSequencer> mCaptureSequencer;
235     sp<camera2::JpegProcessor> mJpegProcessor;
236     sp<camera2::ZslProcessor> mZslProcessor;
237 
238     /** Utility members */
239     bool mLegacyMode;
240 
241     // Wait until the camera device has received the latest control settings
242     status_t syncWithDevice();
243 
244     // Video snapshot jpeg size overriding helper function
245     status_t overrideVideoSnapshotSize(Parameters &params);
246 
247     template<typename TProviderPtr>
248     status_t initializeImpl(TProviderPtr providerPtr, const std::string& monitorTags);
249 
250     bool isZslEnabledInStillTemplate();
251     // The current rotate & crop mode passed by camera service
252     uint8_t mRotateAndCropMode;
253     // Synchronize access to 'mRotateAndCropMode'
254     mutable Mutex mRotateAndCropLock;
255     // Contains the preview stream transformation that would normally be applied
256     // when the display rotation is 0
257     int mRotateAndCropPreviewTransform;
258     // Flag indicating camera device support for the rotate & crop interface
259     bool mRotateAndCropIsSupported;
260 
261     mutable Mutex mLatestRequestMutex;
262     Condition mLatestRequestSignal;
263     static constexpr size_t kMaxRequestIds = BufferQueueDefs::NUM_BUFFER_SLOTS;
264     RingBuffer<int32_t> mLatestRequestIds, mLatestFailedRequestIds;
265     status_t waitUntilRequestIdApplied(int32_t requestId, nsecs_t timeout);
266     status_t waitUntilCurrentRequestIdLocked();
267 };
268 
269 }; // namespace android
270 
271 #endif
272