1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef VIDEO_PROCESSING_NATIVE_H
17 #define VIDEO_PROCESSING_NATIVE_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <functional>
22 #include <mutex>
23 #include <queue>
24 #include <thread>
25 
26 #include "meta/format.h"
27 #include "surface.h"
28 
29 #include "algorithm_types.h"
30 #include "video_processing_callback_native.h"
31 #include "video_processing_types.h"
32 
33 namespace OHOS {
34 namespace Media {
35 namespace VideoProcessingEngine {
36 /**
37  * Native implementation for video processing.
38  */
39 class VideoProcessingNative : public std::enable_shared_from_this<VideoProcessingNative> {
40 public:
41     VideoProcessing_ErrorCode Initialize();
42     VideoProcessing_ErrorCode Deinitialize();
43     VideoProcessing_ErrorCode RegisterCallback(const VideoProcessing_Callback* callback, void* userData);
44     VideoProcessing_ErrorCode SetSurface(const OHNativeWindow* window);
45     VideoProcessing_ErrorCode GetSurface(OHNativeWindow** window);
46     VideoProcessing_ErrorCode SetParameter(const OH_AVFormat* parameter);
47     VideoProcessing_ErrorCode GetParameter(OH_AVFormat* parameter);
48     VideoProcessing_ErrorCode Start();
49     VideoProcessing_ErrorCode Stop();
50     VideoProcessing_ErrorCode RenderOutputBuffer(uint32_t index);
51 
52 protected:
VideoProcessingNative(OH_VideoProcessing * context)53     explicit VideoProcessingNative(OH_VideoProcessing* context) : context_(context) {}
54     virtual ~VideoProcessingNative() = default;
55     VideoProcessingNative(const VideoProcessingNative&) = delete;
56     VideoProcessingNative& operator=(const VideoProcessingNative&) = delete;
57     VideoProcessingNative(VideoProcessingNative&&) = delete;
58     VideoProcessingNative& operator=(VideoProcessingNative&&) = delete;
59 
60     static VideoProcessing_ErrorCode AlgoErrorToNdk(AlgoErrorCode errorCode);
61 
62     virtual VideoProcessing_ErrorCode InitializeInner();
63     virtual VideoProcessing_ErrorCode DeinitializeInner();
64     virtual VideoProcessing_ErrorCode SetParameter(const OHOS::Media::Format& parameter);
65     virtual VideoProcessing_ErrorCode GetParameter(OHOS::Media::Format& parameter);
66     virtual bool IsProducerSurfaceValid(const OHNativeWindow& window);
67     virtual VideoProcessing_ErrorCode SetProducerSurface(const OHNativeWindow& window, BufferRequestConfig& requestCfg);
68     virtual VideoProcessing_ErrorCode Process(const sptr<SurfaceBuffer>& sourceImage,
69         sptr<SurfaceBuffer>& destinationImage);
70     virtual void UpdateRequestCfg(const sptr<SurfaceBuffer>& consumerBuffer, BufferRequestConfig& requestCfg);
71 
72 private:
73     enum class VPEState : int {
74         IDLE = 0,
75         RUNNING,
76         STOPPING
77     };
78 
79     struct SurfaceBufferInfo {
80         sptr<SurfaceBuffer> buffer{};
81         int64_t timestamp{};
82     };
83 
84     class ConsumerListener : public IBufferConsumerListener {
85     public:
ConsumerListener(const std::shared_ptr<VideoProcessingNative> & owner)86         explicit ConsumerListener(const std::shared_ptr<VideoProcessingNative>& owner) : owner_(owner) {}
87         virtual ~ConsumerListener() = default;
88         ConsumerListener(const ConsumerListener&) = delete;
89         ConsumerListener& operator=(const ConsumerListener&) = delete;
90         ConsumerListener(ConsumerListener&&) = delete;
91         ConsumerListener& operator=(ConsumerListener&&) = delete;
92 
93         void OnBufferAvailable() final;
94 
95     private:
96         std::shared_ptr<VideoProcessingNative> owner_{};
97     };
98 
99     void OnError(VideoProcessing_ErrorCode errorCode);
100     void OnState(VideoProcessing_State state);
101     void OnNewOutputBuffer(uint32_t index);
102 
103     GSError OnConsumerBufferAvailable();
104     GSError OnProducerBufferReleased();
105 
106     VideoProcessing_ErrorCode RenderOutputBufferInner(uint32_t index);
107     sptr<Surface> CreateConsumerSurface(OHNativeWindow*& window);
108     bool RequestBuffer(GSError& errorCode);
109     void PrepareBuffers();
110     void ProcessBuffers();
111     bool WaitCV(std::function<bool(void)>&& checker, std::unique_lock<std::mutex>& lock);
112     void CheckStopping();
113 
114     VideoProcessing_ErrorCode ExecuteWhenIdle(std::function<VideoProcessing_ErrorCode(void)>&& operation,
115         const std::string& errorMessage);
116     VideoProcessing_ErrorCode ExecuteWhenNotIdle(std::function<VideoProcessing_ErrorCode(void)>&& operation,
117         const std::string& errorMessage);
118     VideoProcessing_ErrorCode ExecuteWhenRunning(std::function<VideoProcessing_ErrorCode(void)>&& operation,
119         const std::string& errorMessage);
120     VideoProcessing_ErrorCode ExecuteWithCheck(std::function<bool(void)>&& checker,
121         std::function<VideoProcessing_ErrorCode(void)>&& operation, const std::string& errorMessage);
122     void OnCallback(std::function<void(std::shared_ptr<VideoProcessingCallbackNative>&, void*)>&& task);
123 
124     OH_VideoProcessing* context_{};
125 
126     mutable std::mutex lock_{};
127     // Guarded by lock_ begin
128     bool isInitialized_{false};
129     std::atomic<VPEState> state_{VPEState::IDLE};
130     std::atomic<bool> isExit_{false};
131     sptr<Surface> consumer_{};
132     sptr<Surface> producer_{};
133     std::shared_ptr<VideoProcessingCallbackNative> callback_{};
134     void* userData_{};
135     std::thread worker_{};
136     // Guarded by lock_ end
137 
138     std::condition_variable cv_{};
139     std::atomic<bool> isOnNewOutputBuffer_{};
140 
141     mutable std::mutex bufferLock_{};
142     // Guarded by bufferLock_ begin
143     bool isBufferQueueReady_{};
144     BufferRequestConfig requestCfg_{};
145     std::queue<SurfaceBufferInfo> producerBufferQueue_;
146     std::queue<SurfaceBufferInfo> consumerBufferQueue_;
147     std::queue<SurfaceBufferInfo> RenderBufferQueue_;
148     // Guarded by bufferLock_ end
149 };
150 } // namespace VideoProcessingEngine
151 } // namespace Media
152 } // namespace OHOS
153 
154 #endif // VIDEO_PROCESSING_NATIVE_H
155