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_SAMPLE
17 #define VIDEO_SAMPLE
18 
19 #include <string>
20 #include <thread>
21 #include <memory>
22 #include <iostream>
23 
24 #include "surface/window.h"
25 #include "native_buffer.h"
26 #include "iconsumer_surface.h"
27 #include "sync_fence.h"
28 #include "video_processing.h"
29 #include "video_processing_native.h"
30 
31 typedef struct VideoProcessParam {
32     OH_NativeBuffer_Format inFmt;
33     int32_t inWidth;
34     int32_t inHeight;
35     OH_NativeBuffer_Format outFmt;
36     int32_t outWidth;
37     int32_t outHeight;
38 } VideoProcessParam;
39 
40 namespace OHOS {
41 
42 struct SurfaceBufferWrapper {
43 public:
44     SurfaceBufferWrapper() = default;
45     ~SurfaceBufferWrapper() = default;
46 
47     sptr<SurfaceBuffer> memory{nullptr};
48     sptr<SyncFence> fence{nullptr};
49     int64_t timestamp;
50 };
51 
52 class VideoSample {
53 public:
54     VideoSample();
55     ~VideoSample();
56 
57     void OnBufferAvailable();
58     void NotifyCv();
59     void SetImplLoader(bool isImpl);
60     void SetQualityLevel(VideoDetailEnhancer_QualityLevel level);
61     void UpdateErrorCount();
62     int32_t InitVideoSample(VideoProcessParam param);
63     int32_t InitVideoSampleImpl(VideoProcessParam param);
64     int32_t InputFunc();
65     int32_t SetSurfaceOnRunningImpl();
66     int32_t StartProcess();
67     int32_t StartProcessImpl();
68     int32_t WaitAndStopSample();
69     int32_t WaitAndStopSampleImpl();
70 
71 private:
72     void SetInputWindowParam();
73 
74     VideoProcessing_Callback* callback_{};
75     VideoProcessing_Callback* callbackImpl_{};
76     std::unique_ptr<std::thread> inputLoop_{};
77     std::mutex mutexListener_{};
78     sptr<Surface> cs_{};
79     int32_t errCount_{0};
80     std::queue<sptr<SurfaceBuffer>> inputBufferAvilQue_{};
81     bool isImpl_{false};
82     OHNativeWindow* inWindow_{};
83     OHNativeWindow* outWindow_{};
84     VideoProcessParam param_{};
85     struct Region::Rect* rect_{};
86     struct Region region_{};
87     OH_VideoProcessing* videoProcessor_{};
88     OH_VideoProcessing* videoProcessorImpl_{};
89     VideoDetailEnhancer_QualityLevel qualityLevel_{};
90     std::mutex mutex_{};
91     std::condition_variable cv_{};
92 };
93 } // namespace OHOS
94 
95 #endif // VIDEO_SAMPLE
96