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