• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013-2018 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_PHOTOGRAPHY_CAMERADEVICECLIENT_H
18 #define ANDROID_SERVERS_CAMERA_PHOTOGRAPHY_CAMERADEVICECLIENT_H
19 
20 #include <android/hardware/camera2/BnCameraDeviceUser.h>
21 #include <android/hardware/camera2/ICameraDeviceCallbacks.h>
22 #include <camera/camera2/OutputConfiguration.h>
23 #include <camera/camera2/SessionConfiguration.h>
24 #include <camera/camera2/SubmitInfo.h>
25 #include <unordered_map>
26 
27 #include "CameraOfflineSessionClient.h"
28 #include "CameraService.h"
29 #include "common/FrameProcessorBase.h"
30 #include "common/Camera2ClientBase.h"
31 #include "CompositeStream.h"
32 #include "utils/SessionConfigurationUtils.h"
33 
34 using android::camera3::OutputStreamInfo;
35 using android::camera3::CompositeStream;
36 
37 namespace android {
38 
39 struct CameraDeviceClientBase :
40          public CameraService::BasicClient,
41          public hardware::camera2::BnCameraDeviceUser
42 {
43     typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
44 
getRemoteCallbackCameraDeviceClientBase45     const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
46         return mRemoteCallback;
47     }
48 
49 protected:
50     CameraDeviceClientBase(const sp<CameraService>& cameraService,
51             const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
52             const String16& clientPackageName,
53             bool systemNativeClient,
54             const std::optional<String16>& clientFeatureId,
55             const String8& cameraId,
56             int api1CameraId,
57             int cameraFacing,
58             int sensorOrientation,
59             int clientPid,
60             uid_t clientUid,
61             int servicePid,
62             bool overrideToPortrait);
63 
64     sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
65 };
66 
67 /**
68  * Implements the binder ICameraDeviceUser API,
69  * meant for HAL3-public implementation of
70  * android.hardware.photography.CameraDevice
71  */
72 class CameraDeviceClient :
73         public Camera2ClientBase<CameraDeviceClientBase>,
74         public camera2::FrameProcessorBase::FilteredListener
75 {
76 public:
77     /**
78      * ICameraDeviceUser interface (see ICameraDeviceUser for details)
79      */
80 
81     // Note that the callee gets a copy of the metadata.
82     virtual binder::Status submitRequest(
83             const hardware::camera2::CaptureRequest& request,
84             bool streaming = false,
85             /*out*/
86             hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
87     // List of requests are copied.
88     virtual binder::Status submitRequestList(
89             const std::vector<hardware::camera2::CaptureRequest>& requests,
90             bool streaming = false,
91             /*out*/
92             hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
93     virtual binder::Status cancelRequest(int requestId,
94             /*out*/
95             int64_t* lastFrameNumber = NULL) override;
96 
97     virtual binder::Status beginConfigure() override;
98 
99     virtual binder::Status endConfigure(int operatingMode,
100             const hardware::camera2::impl::CameraMetadataNative& sessionParams,
101             int64_t startTimeMs,
102             /*out*/
103             std::vector<int>* offlineStreamIds) override;
104 
105     // Verify specific session configuration.
106     virtual binder::Status isSessionConfigurationSupported(
107             const SessionConfiguration& sessionConfiguration,
108             /*out*/
109             bool* streamStatus) override;
110 
111     // Returns -EBUSY if device is not idle or in error state
112     virtual binder::Status deleteStream(int streamId) override;
113 
114     virtual binder::Status createStream(
115             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
116             /*out*/
117             int32_t* newStreamId = NULL) override;
118 
119     // Create an input stream of width, height, and format.
120     virtual binder::Status createInputStream(int width, int height, int format,
121             bool isMultiResolution,
122             /*out*/
123             int32_t* newStreamId = NULL) override;
124 
125     // Get the buffer producer of the input stream
126     virtual binder::Status getInputSurface(
127             /*out*/
128             view::Surface *inputSurface) override;
129 
130     // Create a request object from a template.
131     virtual binder::Status createDefaultRequest(int templateId,
132             /*out*/
133             hardware::camera2::impl::CameraMetadataNative* request) override;
134 
135     // Get the static metadata for the camera
136     // -- Caller owns the newly allocated metadata
137     virtual binder::Status getCameraInfo(
138             /*out*/
139             hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override;
140 
141     // Wait until all the submitted requests have finished processing
142     virtual binder::Status waitUntilIdle() override;
143 
144     // Flush all active and pending requests as fast as possible
145     virtual binder::Status flush(
146             /*out*/
147             int64_t* lastFrameNumber = NULL) override;
148 
149     // Prepare stream by preallocating its buffers
150     virtual binder::Status prepare(int32_t streamId) override;
151 
152     // Tear down stream resources by freeing its unused buffers
153     virtual binder::Status tearDown(int32_t streamId) override;
154 
155     // Prepare stream by preallocating up to maxCount of its buffers
156     virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
157 
158     // Update an output configuration
159     virtual binder::Status updateOutputConfiguration(int streamId,
160             const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
161 
162     // Finalize the output configurations with surfaces not added before.
163     virtual binder::Status finalizeOutputConfigurations(int32_t streamId,
164             const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
165 
166     virtual binder::Status setCameraAudioRestriction(int32_t mode) override;
167 
168     virtual binder::Status getGlobalAudioRestriction(/*out*/int32_t* outMode) override;
169 
170     virtual binder::Status switchToOffline(
171             const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
172             const std::vector<int>& offlineOutputIds,
173             /*out*/
174             sp<hardware::camera2::ICameraOfflineSession>* session) override;
175 
176     /**
177      * Interface used by CameraService
178      */
179 
180     CameraDeviceClient(const sp<CameraService>& cameraService,
181             const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
182             const String16& clientPackageName,
183             bool clientPackageOverride,
184             const std::optional<String16>& clientFeatureId,
185             const String8& cameraId,
186             int cameraFacing,
187             int sensorOrientation,
188             int clientPid,
189             uid_t clientUid,
190             int servicePid,
191             bool overrideForPerfClass,
192             bool overrideToPortrait);
193     virtual ~CameraDeviceClient();
194 
195     virtual status_t      initialize(sp<CameraProviderManager> manager,
196             const String8& monitorTags) override;
197 
198     virtual status_t      setRotateAndCropOverride(uint8_t rotateAndCrop) override;
199 
200     virtual bool          supportsCameraMute();
201     virtual status_t      setCameraMute(bool enabled);
202 
203     virtual status_t      dump(int fd, const Vector<String16>& args);
204 
205     virtual status_t      dumpClient(int fd, const Vector<String16>& args);
206 
207     virtual status_t      startWatchingTags(const String8 &tags, int out);
208     virtual status_t      stopWatchingTags(int out);
209     virtual status_t      dumpWatchedEventsToVector(std::vector<std::string> &out);
210 
211     virtual status_t      setCameraServiceWatchdog(bool enabled);
212 
213     virtual void          setStreamUseCaseOverrides(const std::vector<int64_t>& useCaseOverrides);
214     virtual void          clearStreamUseCaseOverrides() override;
215 
216     /**
217      * Device listener interface
218      */
219 
220     virtual void notifyIdle(int64_t requestCount, int64_t resultErrorCount, bool deviceError,
221                             const std::vector<hardware::CameraStreamStats>& streamStats);
222     virtual void notifyError(int32_t errorCode,
223                              const CaptureResultExtras& resultExtras);
224     virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
225     virtual void notifyPrepared(int streamId);
226     virtual void notifyRequestQueueEmpty();
227     virtual void notifyRepeatingRequestError(long lastFrameNumber);
228 
setImageDumpMask(int mask)229     void setImageDumpMask(int mask) { if (mDevice != nullptr) mDevice->setImageDumpMask(mask); }
230     /**
231      * Interface used by independent components of CameraDeviceClient.
232      */
233 protected:
234     /** FilteredListener implementation **/
235     virtual void          onResultAvailable(const CaptureResult& result);
236     virtual void          detachDevice();
237 
238     // Calculate the ANativeWindow transform from android.sensor.orientation
239     status_t              getRotationTransformLocked(int mirrorMode, /*out*/int32_t* transform);
240 
241     bool isUltraHighResolutionSensor(const String8 &cameraId);
242 
243     bool isSensorPixelModeConsistent(const std::list<int> &streamIdList,
244             const CameraMetadata &settings);
245 
246     const CameraMetadata &getStaticInfo(const String8 &cameraId);
247 
248 private:
249     // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface.
250     // streamId specifies the index of the stream the surface belongs to, and the
251     // surfaceId specifies the index of the surface within the stream. (one stream
252     // could contain multiple surfaces.)
253     class StreamSurfaceId final {
254     public:
StreamSurfaceId()255         StreamSurfaceId() {
256             mStreamId = -1;
257             mSurfaceId = -1;
258         }
StreamSurfaceId(int32_t streamId,int32_t surfaceId)259         StreamSurfaceId(int32_t streamId, int32_t surfaceId) {
260             mStreamId = streamId;
261             mSurfaceId = surfaceId;
262         }
streamId()263         int32_t streamId() const {
264             return mStreamId;
265         }
surfaceId()266         int32_t surfaceId() const {
267             return mSurfaceId;
268         }
269 
270     private:
271         int32_t mStreamId;
272         int32_t mSurfaceId;
273 
274     }; // class StreamSurfaceId
275 
276 private:
277     /** ICameraDeviceUser interface-related private members */
278 
279     /** Preview callback related members */
280     sp<camera2::FrameProcessorBase> mFrameProcessor;
281 
282     std::vector<int32_t> mSupportedPhysicalRequestKeys;
283 
284     template<typename TProviderPtr>
285     status_t      initializeImpl(TProviderPtr providerPtr, const String8& monitorTags);
286 
287     /** Utility members */
288     binder::Status checkPidStatus(const char* checkLocation);
289     bool enforceRequestPermissions(CameraMetadata& metadata);
290 
291     // Create an output stream with surface deferred for future.
292     binder::Status createDeferredSurfaceStreamLocked(
293             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
294             bool isShared,
295             int* newStreamId = NULL);
296 
297     // Set the stream transform flags to automatically rotate the camera stream for preview use
298     // cases.
299     binder::Status setStreamTransformLocked(int streamId, int mirrorMode);
300 
301     // Utility method to insert the surface into SurfaceMap
302     binder::Status insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
303             /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds,
304             /*out*/int32_t*  currentStreamId);
305 
306     // Utility method that maps AIDL request templates.
307     binder::Status mapRequestTemplate(int templateId,
308             camera_request_template_t* tempId /*out*/);
309 
310     // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams
311     KeyedVector<sp<IBinder>, StreamSurfaceId> mStreamMap;
312 
313     // Stream ID -> OutputConfiguration. Used for looking up Surface by stream/surface index
314     KeyedVector<int32_t, hardware::camera2::params::OutputConfiguration> mConfiguredOutputs;
315 
316     // Dynamic range profile id -> Supported dynamic profiles bitmap within an single capture
317     // request
318     std::unordered_map<int64_t, int64_t> mDynamicProfileMap;
319 
320     struct InputStreamConfiguration {
321         bool configured;
322         int32_t width;
323         int32_t height;
324         int32_t format;
325         int32_t id;
326     } mInputStream;
327 
328     // Streaming request ID
329     int32_t mStreamingRequestId;
330     Mutex mStreamingRequestIdLock;
331     static const int32_t REQUEST_ID_NONE = -1;
332 
333     int32_t mRequestIdCounter;
334 
335     std::vector<std::string> mPhysicalCameraIds;
336 
337     // The list of output streams whose surfaces are deferred. We have to track them separately
338     // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
339     // Surface is configured, the stream id will be moved to mStreamMap.
340     Vector<int32_t> mDeferredStreams;
341 
342     // stream ID -> outputStreamInfo mapping
343     std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap;
344 
345     // map high resolution camera id (logical / physical) -> list of stream ids configured
346     std::unordered_map<std::string, std::unordered_set<int>> mHighResolutionCameraIdToStreamIdSet;
347 
348     // set of high resolution camera id (logical / physical)
349     std::unordered_set<std::string> mHighResolutionSensors;
350 
351     // Synchronize access to 'mCompositeStreamMap'
352     Mutex mCompositeLock;
353     KeyedVector<sp<IBinder>, sp<CompositeStream>> mCompositeStreamMap;
354 
355     sp<CameraProviderManager> mProviderManager;
356 
357     // Override the camera characteristics for performance class primary cameras.
358     bool mOverrideForPerfClass;
359 
360     // The string representation of object passed into CaptureRequest.setTag.
361     std::string mUserTag;
362     // The last set video stabilization mode
363     int mVideoStabilizationMode = -1;
364 };
365 
366 }; // namespace android
367 
368 #endif
369