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