Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 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_VIRTUALCAMERASESSION_H |
| 18 | #define ANDROID_COMPANION_VIRTUALCAMERA_VIRTUALCAMERASESSION_H |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <set> |
| 22 | |
| 23 | #include "VirtualCameraRenderThread.h" |
| 24 | #include "VirtualCameraSessionContext.h" |
| 25 | #include "aidl/android/companion/virtualcamera/IVirtualCameraCallback.h" |
| 26 | #include "aidl/android/hardware/camera/device/BnCameraDeviceSession.h" |
| 27 | #include "aidl/android/hardware/camera/device/ICameraDeviceCallback.h" |
| 28 | #include "utils/Mutex.h" |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | template <typename T, typename U> |
| 33 | struct AidlMessageQueue; |
| 34 | namespace companion { |
| 35 | namespace virtualcamera { |
| 36 | |
| 37 | // Implementation of ICameraDeviceSession AIDL interface to allow camera |
| 38 | // framework to read image data from open virtual camera device. This class |
| 39 | // encapsulates possibly several image streams for the same session. |
| 40 | class VirtualCameraSession |
| 41 | : public ::aidl::android::hardware::camera::device::BnCameraDeviceSession { |
| 42 | public: |
| 43 | // Construct new virtual camera session. |
| 44 | // When virtualCameraClientCallback is null, the input surface will be filled |
| 45 | // with test pattern. |
| 46 | VirtualCameraSession( |
| 47 | const std::string& cameraId, |
| 48 | std::shared_ptr< |
| 49 | ::aidl::android::hardware::camera::device::ICameraDeviceCallback> |
| 50 | cameraDeviceCallback, |
| 51 | std::shared_ptr< |
| 52 | ::aidl::android::companion::virtualcamera::IVirtualCameraCallback> |
| 53 | virtualCameraClientCallback = nullptr); |
| 54 | |
| 55 | virtual ~VirtualCameraSession() override = default; |
| 56 | |
| 57 | ndk::ScopedAStatus close() override; |
| 58 | |
| 59 | ndk::ScopedAStatus configureStreams( |
| 60 | const ::aidl::android::hardware::camera::device::StreamConfiguration& |
| 61 | in_requestedConfiguration, |
| 62 | std::vector<::aidl::android::hardware::camera::device::HalStream>* |
| 63 | _aidl_return) override EXCLUDES(mLock); |
| 64 | |
| 65 | ndk::ScopedAStatus constructDefaultRequestSettings( |
| 66 | ::aidl::android::hardware::camera::device::RequestTemplate in_type, |
| 67 | ::aidl::android::hardware::camera::device::CameraMetadata* _aidl_return) |
| 68 | override; |
| 69 | |
| 70 | ndk::ScopedAStatus flush() override EXCLUDES(mLock); |
| 71 | |
| 72 | ndk::ScopedAStatus getCaptureRequestMetadataQueue( |
| 73 | ::aidl::android::hardware::common::fmq::MQDescriptor< |
| 74 | int8_t, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>* |
| 75 | _aidl_return) override; |
| 76 | |
| 77 | ndk::ScopedAStatus getCaptureResultMetadataQueue( |
| 78 | ::aidl::android::hardware::common::fmq::MQDescriptor< |
| 79 | int8_t, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>* |
| 80 | _aidl_return) override; |
| 81 | |
| 82 | ndk::ScopedAStatus isReconfigurationRequired( |
| 83 | const ::aidl::android::hardware::camera::device::CameraMetadata& |
| 84 | in_oldSessionParams, |
| 85 | const ::aidl::android::hardware::camera::device::CameraMetadata& |
| 86 | in_newSessionParams, |
| 87 | bool* _aidl_return) override; |
| 88 | |
| 89 | ndk::ScopedAStatus processCaptureRequest( |
| 90 | const std::vector<::aidl::android::hardware::camera::device::CaptureRequest>& |
| 91 | in_requests, |
| 92 | const std::vector<::aidl::android::hardware::camera::device::BufferCache>& |
| 93 | in_cachesToRemove, |
| 94 | int32_t* _aidl_return) override; |
| 95 | |
| 96 | ndk::ScopedAStatus signalStreamFlush(const std::vector<int32_t>& in_streamIds, |
| 97 | int32_t in_streamConfigCounter) override; |
| 98 | |
| 99 | ndk::ScopedAStatus switchToOffline( |
| 100 | const std::vector<int32_t>& in_streamsToKeep, |
| 101 | ::aidl::android::hardware::camera::device::CameraOfflineSessionInfo* |
| 102 | out_offlineSessionInfo, |
| 103 | std::shared_ptr< |
| 104 | ::aidl::android::hardware::camera::device::ICameraOfflineSession>* |
| 105 | _aidl_return) override; |
| 106 | |
| 107 | ndk::ScopedAStatus repeatingRequestEnd( |
| 108 | int32_t in_frameNumber, const std::vector<int32_t>& in_streamIds) override; |
| 109 | |
| 110 | std::set<int> getStreamIds() const EXCLUDES(mLock); |
| 111 | |
| 112 | private: |
| 113 | ndk::ScopedAStatus processCaptureRequest( |
| 114 | const ::aidl::android::hardware::camera::device::CaptureRequest& request) |
| 115 | EXCLUDES(mLock); |
| 116 | |
| 117 | const std::string mCameraId; |
| 118 | |
| 119 | mutable std::mutex mLock; |
| 120 | |
| 121 | std::shared_ptr<::aidl::android::hardware::camera::device::ICameraDeviceCallback> |
| 122 | mCameraDeviceCallback GUARDED_BY(mLock); |
| 123 | |
| 124 | const std::shared_ptr< |
| 125 | ::aidl::android::companion::virtualcamera::IVirtualCameraCallback> |
| 126 | mVirtualCameraClientCallback; |
| 127 | |
| 128 | VirtualCameraSessionContext mSessionContext; |
| 129 | |
| 130 | using RequestMetadataQueue = AidlMessageQueue< |
| 131 | int8_t, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>; |
| 132 | std::unique_ptr<RequestMetadataQueue> mRequestMetadataQueue; |
| 133 | |
| 134 | using ResultMetadataQueue = AidlMessageQueue< |
| 135 | int8_t, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>; |
| 136 | std::shared_ptr<ResultMetadataQueue> mResultMetadataQueue; |
| 137 | |
| 138 | std::atomic_bool mFirstRequest{true}; |
| 139 | |
| 140 | std::unique_ptr<VirtualCameraRenderThread> mRenderThread GUARDED_BY(mLock); |
| 141 | }; |
| 142 | |
| 143 | } // namespace virtualcamera |
| 144 | } // namespace companion |
| 145 | } // namespace android |
| 146 | |
| 147 | #endif // ANDROID_SERVICES_VIRTUAL_CAMERA_VIRTUALCAMERASESSION_H |