xref: /aosp_15_r20/hardware/interfaces/automotive/evs/aidl/impl/default/include/EvsVideoEmulatedCamera.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 #pragma once
18 
19 #include "ConfigManager.h"
20 #include "EvsCamera.h"
21 
22 #include <aidl/android/hardware/automotive/evs/BufferDesc.h>
23 #include <aidl/android/hardware/automotive/evs/CameraDesc.h>
24 #include <aidl/android/hardware/automotive/evs/CameraParam.h>
25 #include <aidl/android/hardware/automotive/evs/IEvsCameraStream.h>
26 #include <aidl/android/hardware/automotive/evs/IEvsDisplay.h>
27 #include <aidl/android/hardware/automotive/evs/ParameterRange.h>
28 #include <aidl/android/hardware/automotive/evs/Stream.h>
29 #include <media/NdkMediaExtractor.h>
30 #include <ui/GraphicBuffer.h>
31 
32 #include <cstdint>
33 #include <memory>
34 #include <thread>
35 #include <unordered_map>
36 #include <vector>
37 
38 namespace aidl::android::hardware::automotive::evs::implementation {
39 
40 class EvsVideoEmulatedCamera : public EvsCamera {
41   private:
42     using Base = EvsCamera;
43 
44   public:
45     EvsVideoEmulatedCamera(Sigil sigil, const char* deviceName,
46                            std::unique_ptr<ConfigManager::CameraInfo>& camInfo);
47 
48     ~EvsVideoEmulatedCamera() override = default;
49 
50     // Methods from ::android::hardware::automotive::evs::IEvsCamera follow.
51     ndk::ScopedAStatus forcePrimaryClient(
52             const std::shared_ptr<evs::IEvsDisplay>& display) override;
53     ndk::ScopedAStatus getCameraInfo(evs::CameraDesc* _aidl_return) override;
54     ndk::ScopedAStatus getExtendedInfo(int32_t opaqueIdentifier,
55                                        std::vector<uint8_t>* value) override;
56     ndk::ScopedAStatus getIntParameter(evs::CameraParam id, std::vector<int32_t>* value) override;
57     ndk::ScopedAStatus getIntParameterRange(evs::CameraParam id,
58                                             evs::ParameterRange* _aidl_return) override;
59     ndk::ScopedAStatus getParameterList(std::vector<evs::CameraParam>* _aidl_return) override;
60     ndk::ScopedAStatus getPhysicalCameraInfo(const std::string& deviceId,
61                                              evs::CameraDesc* _aidl_return) override;
62     ndk::ScopedAStatus setExtendedInfo(int32_t opaqueIdentifier,
63                                        const std::vector<uint8_t>& opaqueValue) override;
64     ndk::ScopedAStatus setIntParameter(evs::CameraParam id, int32_t value,
65                                        std::vector<int32_t>* effectiveValue) override;
66     ndk::ScopedAStatus setPrimaryClient() override;
67     ndk::ScopedAStatus unsetPrimaryClient() override;
68 
69     // Methods from EvsCameraBase follow.
70     void shutdown() override;
71 
getId()72     std::string getId() override { return mDescription.id; }
73 
getDesc()74     const evs::CameraDesc& getDesc() { return mDescription; }
75 
76     static std::shared_ptr<EvsVideoEmulatedCamera> Create(const char* deviceName);
77     static std::shared_ptr<EvsVideoEmulatedCamera> Create(
78             const char* deviceName, std::unique_ptr<ConfigManager::CameraInfo>& camInfo,
79             const evs::Stream* streamCfg = nullptr);
80 
81   private:
82     // For the camera parameters.
83     struct CameraParameterDesc {
84         CameraParameterDesc(int min = 0, int max = 0, int step = 0, int value = 0) {
85             this->range.min = min;
86             this->range.max = max;
87             this->range.step = step;
88             this->value = value;
89         }
90 
91         ParameterRange range;
92         int32_t value;
93     };
94 
95     bool initialize();
96 
97     bool initializeMediaCodec();
98 
99     void generateFrames();
100 
101     void renderOneFrame();
102 
103     void initializeParameters();
104 
105     void onCodecInputAvailable(const int32_t index);
106 
107     void onCodecOutputAvailable(const int32_t index, const AMediaCodecBufferInfo& info);
108 
109     ::android::status_t allocateOneFrame(buffer_handle_t* handle) override;
110 
111     bool startVideoStreamImpl_locked(const std::shared_ptr<evs::IEvsCameraStream>& receiver,
112                                      ndk::ScopedAStatus& status,
113                                      std::unique_lock<std::mutex>& lck) override;
114 
115     bool stopVideoStreamImpl_locked(ndk::ScopedAStatus& status,
116                                     std::unique_lock<std::mutex>& lck) override;
117 
118     bool postVideoStreamStop_locked(ndk::ScopedAStatus& status,
119                                     std::unique_lock<std::mutex>& lck) override;
120 
121     int (*mFillBuffer)(const uint8_t* src_y, int src_stride_y, const uint8_t* src_u,
122                        int src_stride_u, const uint8_t* src_v, int src_stride_v, uint8_t* dst_argb,
123                        int dst_stride_argb, int width, int height);
124 
125     // The properties of this camera.
126     CameraDesc mDescription = {};
127 
128     std::thread mCaptureThread;
129 
130     // The callback used to deliver each frame
131     std::shared_ptr<evs::IEvsCameraStream> mStream;
132 
133     std::string mVideoFileName;
134     // Media decoder resources - Owned by mDecoderThead when thread is running.
135     int mVideoFd = 0;
136 
137     struct AMediaExtractorDeleter {
operatorAMediaExtractorDeleter138         void operator()(AMediaExtractor* extractor) const { AMediaExtractor_delete(extractor); }
139     };
140     struct AMediaCodecDeleter {
operatorAMediaCodecDeleter141         void operator()(AMediaCodec* codec) const { AMediaCodec_delete(codec); }
142     };
143 
144     std::unique_ptr<AMediaExtractor, AMediaExtractorDeleter> mVideoExtractor;
145     std::unique_ptr<AMediaCodec, AMediaCodecDeleter> mVideoCodec;
146 
147     // Horizontal pixel count in the buffers
148     int32_t mWidth = 0;
149     // Vertical pixel count in the buffers
150     int32_t mHeight = 0;
151     // Values from android_pixel_format_t
152     uint32_t mFormat = 0;
153     // Values from from Gralloc.h
154     uint64_t mUsage = 0;
155     // Bytes per line in the buffers
156     uint32_t mStride = 0;
157     // Bytes per line in the output buffer
158     uint32_t mDstStride = 0;
159     // Bytes per line of U/V plane
160     uint32_t mUvStride = 0;
161 
162     // Camera parameters.
163     std::unordered_map<CameraParam, std::shared_ptr<CameraParameterDesc>> mParams;
164 
165     // Static camera module information
166     std::unique_ptr<ConfigManager::CameraInfo>& mCameraInfo;
167 
168     // For the extended info
169     std::unordered_map<uint32_t, std::vector<uint8_t>> mExtInfo;
170 };
171 
172 }  // namespace aidl::android::hardware::automotive::evs::implementation
173