1 /*
2  * Copyright 2023 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_COMPANION_VIRTUALCAMERA_VIRTUALCAMERADEVICE_H
18 #define ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERADEVICE_H
19 
20 #include <cstdint>
21 #include <memory>
22 
23 #include "aidl/android/companion/virtualcamera/IVirtualCameraCallback.h"
24 #include "aidl/android/companion/virtualcamera/SupportedStreamConfiguration.h"
25 #include "aidl/android/companion/virtualcamera/VirtualCameraConfiguration.h"
26 #include "aidl/android/hardware/camera/device/BnCameraDevice.h"
27 #include "system/camera_metadata.h"
28 #include "util/Util.h"
29 
30 namespace android {
31 namespace companion {
32 namespace virtualcamera {
33 
34 // Representation of single virtual camera device, implements
35 // ICameraDevice AIDL to expose camera to camera framework.
36 class VirtualCameraDevice
37     : public ::aidl::android::hardware::camera::device::BnCameraDevice {
38  public:
39   explicit VirtualCameraDevice(
40       const std::string& cameraId,
41       const aidl::android::companion::virtualcamera::VirtualCameraConfiguration&
42           configuration,
43       int32_t deviceId);
44 
45   virtual ~VirtualCameraDevice() override = default;
46 
47   ndk::ScopedAStatus getCameraCharacteristics(
48       ::aidl::android::hardware::camera::device::CameraMetadata* _aidl_return)
49       override;
50 
51   ndk::ScopedAStatus getPhysicalCameraCharacteristics(
52       const std::string& in_physicalCameraId,
53       ::aidl::android::hardware::camera::device::CameraMetadata* _aidl_return)
54       override;
55 
56   ndk::ScopedAStatus getResourceCost(
57       ::aidl::android::hardware::camera::common::CameraResourceCost*
58           _aidl_return) override;
59 
60   ndk::ScopedAStatus isStreamCombinationSupported(
61       const ::aidl::android::hardware::camera::device::StreamConfiguration&
62           in_streams,
63       bool* _aidl_return) override;
64 
65   bool isStreamCombinationSupported(
66       const ::aidl::android::hardware::camera::device::StreamConfiguration&
67           in_streams) const;
68 
69   ndk::ScopedAStatus open(
70       const std::shared_ptr<
71           ::aidl::android::hardware::camera::device::ICameraDeviceCallback>&
72           in_callback,
73       std::shared_ptr<
74           ::aidl::android::hardware::camera::device::ICameraDeviceSession>*
75           _aidl_return) override;
76 
77   ndk::ScopedAStatus openInjectionSession(
78       const std::shared_ptr<
79           ::aidl::android::hardware::camera::device::ICameraDeviceCallback>&
80           in_callback,
81       std::shared_ptr<
82           ::aidl::android::hardware::camera::device::ICameraInjectionSession>*
83           _aidl_return) override;
84 
85   ndk::ScopedAStatus setTorchMode(bool in_on) override;
86 
87   ndk::ScopedAStatus turnOnTorchWithStrengthLevel(
88       int32_t in_torchStrength) override;
89 
90   ndk::ScopedAStatus getTorchStrengthLevel(int32_t* _aidl_return) override;
91 
92   binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
93 
94   // Returns unique virtual camera name in form
95   // "device@{major}.{minor}/virtual/{camera_id}"
96   std::string getCameraName() const;
97 
getCameraId()98   const std::string& getCameraId() const {
99     return mCameraId;
100   }
101 
102   const std::vector<
103       aidl::android::companion::virtualcamera::SupportedStreamConfiguration>&
104   getInputConfigs() const;
105 
106   // Returns largest supported input resolution.
107   Resolution getMaxInputResolution() const;
108 
109   // Allocate and return next id for input stream (input surface).
110   int allocateInputStreamId();
111 
112   // Maximal number of RAW streams - virtual camera doesn't support RAW streams.
113   static constexpr int32_t kMaxNumberOfRawStreams = 0;
114 
115   // Maximal number of non-jpeg streams configured concurrently in single
116   // session. This should be at least 3 and can be increased at the potential
117   // cost of more CPU/GPU load if there are many concurrent streams.
118   static constexpr int32_t kMaxNumberOfProcessedStreams = 3;
119 
120   // Maximal number of stalling (in case of virtual camera only jpeg for now)
121   // streams. Can be increaed at the cost of potential cost of more GPU/CPU
122   // load.
123   static constexpr int32_t kMaxNumberOfStallStreams = 1;
124 
125   // Focal length for full frame sensor.
126   static constexpr float kFocalLength = 43.0;
127 
128   // Default JPEG compression quality.
129   static constexpr uint8_t kDefaultJpegQuality = 80;
130 
131   // Default JPEG orientation.
132   static constexpr uint8_t kDefaultJpegOrientation = 0;
133 
134   // Lowest min fps advertised in supported fps ranges.
135   static constexpr int kMinFps = 1;
136 
137   // Default Make and Model for Exif
138   static constexpr char kDefaultMakeAndModel[] = "Android Virtual Camera";
139 
140   static constexpr camera_metadata_enum_android_control_capture_intent_t
141       kDefaultCaptureIntent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
142 
143  private:
144   std::shared_ptr<VirtualCameraDevice> sharedFromThis();
145 
146   const std::string mCameraId;
147   const std::shared_ptr<
148       ::aidl::android::companion::virtualcamera::IVirtualCameraCallback>
149       mVirtualCameraClientCallback;
150 
151   ::aidl::android::hardware::camera::device::CameraMetadata mCameraCharacteristics;
152 
153   const std::vector<
154       aidl::android::companion::virtualcamera::SupportedStreamConfiguration>
155       mSupportedInputConfigurations;
156 
157   std::atomic_int mNextInputStreamId;
158 };
159 
160 }  // namespace virtualcamera
161 }  // namespace companion
162 }  // namespace android
163 
164 #endif  // ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERADEVICE_H
165