• 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 #include <gui/Flags.h>  // remove with WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
27 
28 #include <fmq/AidlMessageQueueCpp.h>
29 
30 #include "CameraOfflineSessionClient.h"
31 #include "CameraService.h"
32 #include "common/FrameProcessorBase.h"
33 #include "common/Camera2ClientBase.h"
34 #include "CompositeStream.h"
35 #include "utils/CameraServiceProxyWrapper.h"
36 #include "utils/SessionConfigurationUtils.h"
37 
38 using android::camera3::OutputStreamInfo;
39 using android::camera3::CompositeStream;
40 
41 namespace android {
42 
43 #if WB_LIBCAMERASERVICE_WITH_DEPENDENCIES
44 typedef uint64_t SurfaceKey;
45 #else
46 typedef sp<IBinder> SurfaceKey;
47 #endif
48 
49 struct CameraDeviceClientBase :
50          public CameraService::BasicClient,
51          public hardware::camera2::BnCameraDeviceUser
52 {
53     typedef hardware::camera2::ICameraDeviceCallbacks TCamCallbacks;
54 
getRemoteCallbackCameraDeviceClientBase55     const sp<hardware::camera2::ICameraDeviceCallbacks>& getRemoteCallback() {
56         return mRemoteCallback;
57     }
58 
59 protected:
60     CameraDeviceClientBase(
61             const sp<CameraService>& cameraService,
62             const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
63             std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,
64             const AttributionSourceState& clientAttribution, int callingPid,
65             bool systemNativeClient, const std::string& cameraId, int api1CameraId,
66             int cameraFacing, int sensorOrientation, int servicePid, int rotationOverride,
67             bool sharedMode);
68 
69     sp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
70 };
71 
72 /**
73  * Implements the binder ICameraDeviceUser API,
74  * meant for HAL3-public implementation of
75  * android.hardware.photography.CameraDevice
76  */
77 class CameraDeviceClient :
78         public Camera2ClientBase<CameraDeviceClientBase>,
79         public camera2::FrameProcessorBase::FilteredListener
80 {
81 public:
82     /**
83      * ICameraDeviceUser interface (see ICameraDeviceUser for details)
84      */
85 
86     // Note that the callee gets a copy of the metadata.
87     virtual binder::Status submitRequest(
88             const hardware::camera2::CaptureRequest& request,
89             bool streaming = false,
90             /*out*/
91             hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
92     // List of requests are copied.
93     virtual binder::Status submitRequestList(
94             const std::vector<hardware::camera2::CaptureRequest>& requests,
95             bool streaming = false,
96             /*out*/
97             hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
98     virtual binder::Status cancelRequest(int requestId,
99             /*out*/
100             int64_t* lastFrameNumber = NULL) override;
101     virtual binder::Status startStreaming(
102             const std::vector<int>& streamIds,
103             const std::vector<int>& surfaceIds,
104             /*out*/
105             hardware::camera2::utils::SubmitInfo *submitInfo = nullptr) override;
106 
107     virtual binder::Status beginConfigure() override;
108 
109     virtual binder::Status endConfigure(int operatingMode,
110             const hardware::camera2::impl::CameraMetadataNative& sessionParams,
111             int64_t startTimeMs,
112             /*out*/
113             std::vector<int>* offlineStreamIds) override;
114 
115     // Verify specific session configuration.
116     virtual binder::Status isSessionConfigurationSupported(
117             const SessionConfiguration& sessionConfiguration,
118             /*out*/
119             bool* streamStatus) override;
120 
121     // Returns -EBUSY if device is not idle or in error state
122     virtual binder::Status deleteStream(int streamId) override;
123 
124     virtual binder::Status createStream(
125             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
126             /*out*/
127             int32_t* newStreamId = NULL) override;
128 
129     // Create an input stream of width, height, and format.
130     virtual binder::Status createInputStream(int width, int height, int format,
131             bool isMultiResolution,
132             /*out*/
133             int32_t* newStreamId = NULL) override;
134 
135     // Get the buffer producer of the input stream
136     virtual binder::Status getInputSurface(
137             /*out*/
138             view::Surface *inputSurface) override;
139 
140     // Create a request object from a template.
141     virtual binder::Status createDefaultRequest(int templateId,
142             /*out*/
143             hardware::camera2::impl::CameraMetadataNative* request) override;
144 
145     // Get the static metadata for the camera
146     // -- Caller owns the newly allocated metadata
147     virtual binder::Status getCameraInfo(
148             /*out*/
149             hardware::camera2::impl::CameraMetadataNative* cameraCharacteristics) override;
150 
151     // Wait until all the submitted requests have finished processing
152     virtual binder::Status waitUntilIdle() override;
153 
154     // Flush all active and pending requests as fast as possible
155     virtual binder::Status flush(
156             /*out*/
157             int64_t* lastFrameNumber = NULL) override;
158 
159     // Prepare stream by preallocating its buffers
160     virtual binder::Status prepare(int32_t streamId) override;
161 
162     // Tear down stream resources by freeing its unused buffers
163     virtual binder::Status tearDown(int32_t streamId) override;
164 
165     // Prepare stream by preallocating up to maxCount of its buffers
166     virtual binder::Status prepare2(int32_t maxCount, int32_t streamId) override;
167 
168     // Update an output configuration
169     virtual binder::Status updateOutputConfiguration(int streamId,
170             const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
171 
172     // Finalize the output configurations with surfaces not added before.
173     virtual binder::Status finalizeOutputConfigurations(int32_t streamId,
174             const hardware::camera2::params::OutputConfiguration &outputConfiguration) override;
175 
176     virtual binder::Status setCameraAudioRestriction(int32_t mode) override;
177 
178     virtual binder::Status getCaptureResultMetadataQueue(
179           android::hardware::common::fmq::MQDescriptor<
180           int8_t, android::hardware::common::fmq::SynchronizedReadWrite>*
181           aidl_return) override;
182 
183     virtual binder::Status getGlobalAudioRestriction(/*out*/int32_t* outMode) override;
184 
185     virtual binder::Status switchToOffline(
186             const sp<hardware::camera2::ICameraDeviceCallbacks>& cameraCb,
187             const std::vector<int>& offlineOutputIds,
188             /*out*/
189             sp<hardware::camera2::ICameraOfflineSession>* session) override;
190 
191     virtual binder::Status isPrimaryClient(/*out*/bool* isPrimary) override;
192 
193     /**
194      * Interface used by CameraService
195      */
196 
197     CameraDeviceClient(const sp<CameraService>& cameraService,
198                        const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
199                        std::shared_ptr<CameraServiceProxyWrapper> cameraServiceProxyWrapper,
200                        std::shared_ptr<AttributionAndPermissionUtils> attributionAndPermissionUtils,
201                        const AttributionSourceState& clientAttribution, int callingPid,
202                        bool clientPackageOverride, const std::string& cameraId, int cameraFacing,
203                        int sensorOrientation, int servicePid, bool overrideForPerfClass,
204                        int rotationOverride, const std::string& originalCameraId, bool sharedMode,
205                        bool isVendorClient);
206     virtual ~CameraDeviceClient();
207 
208     virtual status_t      initialize(sp<CameraProviderManager> manager,
209             const std::string& monitorTags) override;
210 
211     virtual status_t      setRotateAndCropOverride(uint8_t rotateAndCrop,
212             bool fromHal = false) override;
213 
214     virtual status_t      setAutoframingOverride(uint8_t autoframingValue) override;
215 
216     virtual bool          supportsCameraMute();
217     virtual status_t      setCameraMute(bool enabled);
218 
219     virtual bool          supportsZoomOverride() override;
220     virtual status_t      setZoomOverride(int32_t zoomOverride) override;
221 
222     virtual status_t      dump(int fd, const Vector<String16>& args);
223 
224     virtual status_t      dumpClient(int fd, const Vector<String16>& args);
225 
226     virtual status_t      startWatchingTags(const std::string &tags, int out);
227     virtual status_t      stopWatchingTags(int out);
228     virtual status_t      dumpWatchedEventsToVector(std::vector<std::string> &out);
229 
230     virtual status_t      setCameraServiceWatchdog(bool enabled);
231 
232     virtual void          setStreamUseCaseOverrides(const std::vector<int64_t>& useCaseOverrides);
233     virtual void          clearStreamUseCaseOverrides() override;
234 
235     /**
236      * Device listener interface
237      */
238 
239     virtual void notifyIdle(int64_t requestCount, int64_t resultErrorCount, bool deviceError,
240                             std::pair<int32_t, int32_t> mostRequestedFpsRange,
241                             const std::vector<hardware::CameraStreamStats>& streamStats);
242     virtual void notifyError(int32_t errorCode,
243                              const CaptureResultExtras& resultExtras);
244     virtual void notifyShutter(const CaptureResultExtras& resultExtras, nsecs_t timestamp);
245     virtual void notifyPrepared(int streamId);
246     virtual void notifyRequestQueueEmpty();
247     virtual void notifyRepeatingRequestError(long lastFrameNumber);
248     virtual void notifyClientSharedAccessPriorityChanged(bool primaryClient);
249 
setImageDumpMask(int mask)250     void setImageDumpMask(int mask) { if (mDevice != nullptr) mDevice->setImageDumpMask(mask); }
251     /**
252      * Interface used by independent components of CameraDeviceClient.
253      */
254 protected:
255     /** FilteredListener implementation **/
256 
257     size_t writeResultMetadataIntoResultQueue(const CameraMetadata &result);
258     std::vector<PhysicalCaptureResultInfo> convertToFMQ(
259             const std::vector<PhysicalCaptureResultInfo> &physicalResults);
260     virtual void          onResultAvailable(const CaptureResult& result);
261     virtual void          detachDevice();
262 
263     bool supportsUltraHighResolutionCapture(const std::string &cameraId);
264 
265     bool isSensorPixelModeConsistent(const std::list<int> &streamIdList,
266             const CameraMetadata &settings);
267 
268     const CameraMetadata &getStaticInfo(const std::string &cameraId);
269 
270 private:
271     using MetadataQueue = AidlMessageQueueCpp<
272             int8_t, android::hardware::common::fmq::SynchronizedReadWrite>;
273     using CameraMetadataInfo = android::hardware::camera2::CameraMetadataInfo;
274     status_t CreateMetadataQueue(
275             std::unique_ptr<MetadataQueue>* metadata_queue, size_t size_bytes);
276     // StreamSurfaceId encapsulates streamId + surfaceId for a particular surface.
277     // streamId specifies the index of the stream the surface belongs to, and the
278     // surfaceId specifies the index of the surface within the stream. (one stream
279     // could contain multiple surfaces.)
280     class StreamSurfaceId final {
281     public:
StreamSurfaceId()282         StreamSurfaceId() {
283             mStreamId = -1;
284             mSurfaceId = -1;
285         }
StreamSurfaceId(int32_t streamId,int32_t surfaceId)286         StreamSurfaceId(int32_t streamId, int32_t surfaceId) {
287             mStreamId = streamId;
288             mSurfaceId = surfaceId;
289         }
streamId()290         int32_t streamId() const {
291             return mStreamId;
292         }
surfaceId()293         int32_t surfaceId() const {
294             return mSurfaceId;
295         }
296 
297     private:
298         int32_t mStreamId;
299         int32_t mSurfaceId;
300 
301     }; // class StreamSurfaceId
302 
303 private:
304     /** ICameraDeviceUser interface-related private members */
305 
306     /** Preview callback related members */
307     sp<camera2::FrameProcessorBase> mFrameProcessor;
308 
309     std::vector<int32_t> mSupportedPhysicalRequestKeys;
310 
311     template<typename TProviderPtr>
312     status_t      initializeImpl(TProviderPtr providerPtr, const std::string& monitorTags);
313 
314     /** Utility members */
315     binder::Status checkPidStatus(const char* checkLocation);
316     bool enforceRequestPermissions(CameraMetadata& metadata);
317 
318     // Create an output stream with surface deferred for future.
319     binder::Status createDeferredSurfaceStreamLocked(
320             const hardware::camera2::params::OutputConfiguration &outputConfiguration,
321             bool isShared,
322             int* newStreamId = NULL);
323 
324     // Utility method to insert the surface into SurfaceMap
325     binder::Status insertSurfaceLocked(const ParcelableSurfaceType& surface,
326             /*out*/SurfaceMap* surfaceMap, /*out*/Vector<int32_t>* streamIds,
327             /*out*/int32_t*  currentStreamId);
328 
329     // A ParcelableSurfaceType can be either a view::Surface or IGBP.
330     // We use this type of surface when we need to be able to have a parcelable data type.
331     // view::Surface has helper functions to make converting between a regular Surface and a
332     // view::Surface easy.
333     status_t getSurfaceKey(ParcelableSurfaceType surface, SurfaceKey* out) const;
334     // Surface only
335     status_t getSurfaceKey(sp<Surface> surface, SurfaceKey* out) const;
336 
337     bool matchSharedStreamingRequest(int reqId);
338     bool matchSharedCaptureRequest(int reqId);
339     void markClientActive();
340     void markClientIdle();
341 
342     // IGraphicsBufferProducer binder -> Stream ID + Surface ID for output streams
343     KeyedVector<SurfaceKey, StreamSurfaceId> mStreamMap;
344 
345     // Stream ID -> OutputConfiguration. Used for looking up Surface by stream/surface index
346     KeyedVector<int32_t, hardware::camera2::params::OutputConfiguration> mConfiguredOutputs;
347 
348     // Dynamic range profile id -> Supported dynamic profiles bitmap within an single capture
349     // request
350     std::unordered_map<int64_t, int64_t> mDynamicProfileMap;
351 
352     struct InputStreamConfiguration {
353         bool configured;
354         int32_t width;
355         int32_t height;
356         int32_t format;
357         int32_t id;
358     } mInputStream;
359 
360     // Streaming request ID
361     int32_t mStreamingRequestId;
362     Mutex mStreamingRequestIdLock;
363     std::pair<int32_t, int32_t> mSharedStreamingRequest;
364     std::map<int32_t, int32_t> mSharedRequestMap;
365     int64_t mStreamingRequestLastFrameNumber;
366     static const int32_t REQUEST_ID_NONE = -1;
367 
368     int32_t mRequestIdCounter;
369 
370     // Metadata queue to write the result metadata to.
371     std::unique_ptr<MetadataQueue> mResultMetadataQueue;
372 
373     std::vector<std::string> mPhysicalCameraIds;
374 
375     // The list of output streams whose surfaces are deferred. We have to track them separately
376     // as there are no surfaces available and can not be put into mStreamMap. Once the deferred
377     // Surface is configured, the stream id will be moved to mStreamMap.
378     Vector<int32_t> mDeferredStreams;
379 
380     // stream ID -> outputStreamInfo mapping
381     std::unordered_map<int32_t, OutputStreamInfo> mStreamInfoMap;
382 
383     // map high resolution camera id (logical / physical) -> list of stream ids configured
384     std::unordered_map<std::string, std::unordered_set<int>> mHighResolutionCameraIdToStreamIdSet;
385 
386     // set of high resolution camera id (logical / physical)
387     std::unordered_set<std::string> mHighResolutionSensors;
388 
389     // Synchronize access to 'mCompositeStreamMap'
390     Mutex mCompositeLock;
391     KeyedVector<SurfaceKey, sp<CompositeStream>> mCompositeStreamMap;
392 
393     sp<CameraProviderManager> mProviderManager;
394 
395     // Override the camera characteristics for performance class primary cameras.
396     bool mOverrideForPerfClass;
397 
398     // Various fields used to collect session statistics
399     struct RunningSessionStats {
400         // The string representation of object passed into CaptureRequest.setTag.
401         std::string mUserTag;
402         // The last set video stabilization mode
403         int mVideoStabilizationMode = -1;
404         // Whether a zoom_ratio < 1.0 has been used during this session
405         bool mUsedUltraWide = false;
406         // Whether a zoom settings override has been used during this session
407         bool mUsedSettingsOverrideZoom = false;
408     } mRunningSessionStats;
409 
410     // This only exists in case of camera ID Remapping.
411     const std::string mOriginalCameraId;
412 
413     bool mIsVendorClient = false;
414 };
415 
416 }; // namespace android
417 
418 #endif
419