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