• 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 "api1/client2/StreamingProcessor.h"
26 //#include "api1/client2/JpegProcessor.h"
27 //#include "api1/client2/ZslProcessor.h"
28 //#include "api1/client2/CaptureSequencer.h"
29 //#include "api1/client2/CallbackProcessor.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(
61         const sp<IGraphicBufferProducer>& bufferProducer);
62     virtual void            setPreviewCallbackFlag(int flag);
63     virtual status_t        setPreviewCallbackTarget(
64         const sp<IGraphicBufferProducer>& callbackProducer);
65 
66     virtual status_t        startPreview();
67     virtual void            stopPreview();
68     virtual bool            previewEnabled();
69     virtual status_t        setVideoBufferMode(int32_t videoBufferMode);
70     virtual status_t        startRecording();
71     virtual void            stopRecording();
72     virtual bool            recordingEnabled();
73     virtual void            releaseRecordingFrame(const sp<IMemory>& mem);
74     virtual void            releaseRecordingFrameHandle(native_handle_t *handle);
75     virtual void            releaseRecordingFrameHandleBatch(
76                                     const std::vector<native_handle_t*>& handles);
77     virtual status_t        autoFocus();
78     virtual status_t        cancelAutoFocus();
79     virtual status_t        takePicture(int msgType);
80     virtual status_t        setParameters(const String8& params);
81     virtual String8         getParameters() const;
82     virtual status_t        sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
83     virtual void            notifyError(int32_t errorCode,
84                                         const CaptureResultExtras& resultExtras);
85     virtual status_t        setVideoTarget(const sp<IGraphicBufferProducer>& bufferProducer);
86     virtual status_t        setAudioRestriction(int mode);
87     virtual int32_t         getGlobalAudioRestriction();
88     virtual status_t        setRotateAndCropOverride(uint8_t rotateAndCrop);
89 
90     virtual bool            supportsCameraMute();
91     virtual status_t        setCameraMute(bool enabled);
92 
93     virtual status_t        setCameraServiceWatchdog(bool enabled);
94 
95     virtual void            setStreamUseCaseOverrides(
96                                     const std::vector<int64_t>& useCaseOverrides);
97     virtual void            clearStreamUseCaseOverrides();
98 
99     /**
100      * Interface used by CameraService
101      */
102 
103     Camera2Client(const sp<CameraService>& cameraService,
104             const sp<hardware::ICameraClient>& cameraClient,
105             const String16& clientPackageName,
106             const std::optional<String16>& clientFeatureId,
107             const String8& cameraDeviceId,
108             int api1CameraId,
109             int cameraFacing,
110             int sensorOrientation,
111             int clientPid,
112             uid_t clientUid,
113             int servicePid,
114             bool overrideForPerfClass,
115             bool overrideToPortrait,
116             bool forceSlowJpegMode);
117 
118     virtual ~Camera2Client();
119 
120     virtual status_t initialize(sp<CameraProviderManager> manager,
121             const String8& monitorTags) override;
122 
123     virtual status_t dump(int fd, const Vector<String16>& args);
124 
125     virtual status_t dumpClient(int fd, const Vector<String16>& args);
126 
127     /**
128      * Interface used by CameraDeviceBase
129      */
130 
131     virtual void notifyAutoFocus(uint8_t newState, int triggerId);
132     virtual void notifyAutoExposure(uint8_t newState, int triggerId);
133     virtual void notifyShutter(const CaptureResultExtras& resultExtras,
134                                nsecs_t timestamp);
135 
136     /**
137      * Interface used by independent components of Camera2Client.
138      */
139 
140     camera2::SharedParameters& getParameters();
141 
142     void notifyRequestId(int32_t requestId);
143 
144     int getPreviewStreamId() const;
145     int getCaptureStreamId() const;
146     int getCallbackStreamId() const;
147     int getRecordingStreamId() const;
148     int getZslStreamId() const;
149 
150     status_t registerFrameListener(int32_t minId, int32_t maxId,
151             const wp<camera2::FrameProcessor::FilteredListener>& listener,
152             bool sendPartials = true);
153     status_t removeFrameListener(int32_t minId, int32_t maxId,
154             const wp<camera2::FrameProcessor::FilteredListener>& listener);
155 
156     status_t stopStream();
157 
158     // For the slowJpegMode to create jpeg stream when precapture sequence is done
159     status_t createJpegStreamL(camera2::Parameters &params);
160 
161     static size_t calculateBufferSize(int width, int height,
162             int format, int stride);
163 
164     static const int32_t kPreviewRequestIdStart = 10000000;
165     static const int32_t kPreviewRequestIdEnd   = 20000000;
166 
167     static const int32_t kRecordingRequestIdStart  = 20000000;
168     static const int32_t kRecordingRequestIdEnd    = 30000000;
169 
170     static const int32_t kCaptureRequestIdStart = 30000000;
171     static const int32_t kCaptureRequestIdEnd   = 40000000;
172 
173     // Constant strings for ATRACE logging
174     static const char* kAutofocusLabel;
175     static const char* kTakepictureLabel;
176 
177     // Used with stream IDs
178     static const int NO_STREAM = -1;
179 
180 private:
181     /** ICamera interface-related private members */
182     typedef camera2::Parameters Parameters;
183 
184     status_t setPreviewWindowL(const sp<IBinder>& binder,
185             const sp<Surface>& window);
186     status_t startPreviewL(Parameters &params, bool restart);
187     void     stopPreviewL();
188     status_t startRecordingL(Parameters &params, 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 &params);
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 &params, int flag);
210     status_t updateRequests(Parameters &params);
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     sp<IBinder> mPreviewSurface;
223     sp<IBinder> mVideoSurface;
224     sp<camera2::StreamingProcessor> mStreamingProcessor;
225 
226     /** Preview callback related members */
227 
228     sp<camera2::CallbackProcessor> mCallbackProcessor;
229 
230     /* Still image capture related members */
231 
232     sp<camera2::CaptureSequencer> mCaptureSequencer;
233     sp<camera2::JpegProcessor> mJpegProcessor;
234     sp<camera2::ZslProcessor> mZslProcessor;
235 
236     /** Utility members */
237     bool mLegacyMode;
238 
239     // Wait until the camera device has received the latest control settings
240     status_t syncWithDevice();
241 
242     // Video snapshot jpeg size overriding helper function
243     status_t overrideVideoSnapshotSize(Parameters &params);
244 
245     template<typename TProviderPtr>
246     status_t initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
247 
248     bool isZslEnabledInStillTemplate();
249     // The current rotate & crop mode passed by camera service
250     uint8_t mRotateAndCropMode;
251     // Synchronize access to 'mRotateAndCropMode'
252     mutable Mutex mRotateAndCropLock;
253     // Contains the preview stream transformation that would normally be applied
254     // when the display rotation is 0
255     int mRotateAndCropPreviewTransform;
256     // Flag indicating camera device support for the rotate & crop interface
257     bool mRotateAndCropIsSupported;
258 
259     mutable Mutex mLatestRequestMutex;
260     Condition mLatestRequestSignal;
261     int32_t mLatestRequestId = -1;
262     int32_t mLatestFailedRequestId = -1;
263     status_t waitUntilRequestIdApplied(int32_t requestId, nsecs_t timeout);
264     status_t waitUntilCurrentRequestIdLocked();
265 };
266 
267 }; // namespace android
268 
269 #endif
270