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