• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 VIDEOENC_NDK_SAMPLE_H
17 #define VIDEOENC_NDK_SAMPLE_H
18 
19 #include <iostream>
20 #include <cstdio>
21 #include <unistd.h>
22 #include <atomic>
23 #include <fstream>
24 #include <thread>
25 #include <mutex>
26 #include <queue>
27 #include <string>
28 #include <unordered_map>
29 #include "securec.h"
30 #include "native_avcodec_videoencoder.h"
31 #include "nocopyable.h"
32 #include "native_avmemory.h"
33 #include "native_avformat.h"
34 #include "native_averrors.h"
35 #include "surface/window.h"
36 #include "media_description.h"
37 #include "av_common.h"
38 #include "external_window.h"
39 
40 namespace OHOS {
41 namespace Media {
42 class VEncSignal {
43 public:
44     std::mutex inMutex_;
45     std::mutex outMutex_;
46     std::condition_variable inCond_;
47     std::condition_variable outCond_;
48     std::queue<uint32_t> inIdxQueue_;
49     std::queue<uint32_t> outIdxQueue_;
50     std::queue<OH_AVCodecBufferAttr> attrQueue_;
51     std::queue<OH_AVMemory *> inBufferQueue_;
52     std::queue<OH_AVMemory *> outBufferQueue_;
53 };
54 
55 class VEncNdkSample : public NoCopyable {
56 public:
57     VEncNdkSample() = default;
58     ~VEncNdkSample();
59     const char *INP_DIR = "/data/test/media/1280_720_nv.yuv";
60     const char *OUT_DIR = "/data/test/media/VEncTest.h264";
61     uint32_t DEFAULT_WIDTH = 1280;
62     uint32_t DEFAULT_HEIGHT = 720;
63     uint32_t DEFAULT_BITRATE = 10000000;
64     double DEFAULT_FRAME_RATE = 30.0;
65     uint32_t DEFAULT_KEY_FRAME_INTERVAL = 1000;
66     uint32_t repeat_time = 0;
67     int32_t CreateVideoEncoder(const char *codecName);
68     int32_t ConfigureVideoEncoder();
69     int32_t ConfigureVideoEncoder_fuzz(int32_t data);
70     int32_t SetVideoEncoderCallback();
71     int32_t CreateSurface();
72     int32_t OpenFileFail();
73     int32_t StartVideoEncoder();
74     void SetParameter(OH_AVFormat *format);
75     void testApi();
76     void WaitForEOS();
77     uint32_t ReturnZeroIfEOS(uint32_t expectedSize);
78     int64_t GetSystemTimeUs();
79     int32_t Start();
80     int32_t Flush();
81     int32_t Reset();
82     int32_t Stop();
83     int32_t Release();
84     void Flush_buffer();
85     void RepeatStartBeforeEOS();
86     bool RandomEOS(uint32_t index);
87     void SetEOS(uint32_t index);
88     int32_t PushData(OH_AVMemory *buffer, uint32_t index, int32_t &result);
89     int32_t CheckResult(bool isRandomEosSuccess, int32_t pushResult);
90     void InputFunc();
91     int32_t state_EOS();
92     void InputFuncSurface();
93     uint32_t ReadOneFrameYUV420SP(uint8_t *dst);
94     int32_t CheckAttrFlag(OH_AVCodecBufferAttr attr);
95     void OutputFuncFail();
96     void OutputFunc();
97     void ReleaseSignal();
98     void ReleaseInFile();
99     void StopInloop();
100     void StopOutloop();
101 
102     VEncSignal *signal_;
103     uint32_t errCount = 0;
104     bool enableForceIDR = false;
105     uint32_t outCount = 0;
106     uint32_t frameCount = 0;
107 
108     bool sleepOnFPS = false;
109     bool SURFACE_INPUT = false;
110     bool repeatRun = false;
111     bool showLog = false;
112     int64_t encode_count = 0;
113     bool enable_random_eos = false;
114     uint32_t REPEAT_START_STOP_BEFORE_EOS = 0;  // 1200 测试用例
115     uint32_t REPEAT_START_FLUSH_BEFORE_EOS = 0; // 1300 测试用例
116     int64_t start_time = 0;
117     int64_t end_time = 0;
118 
119 private:
120     std::atomic<bool> isRunning_ { false };
121     std::unique_ptr<std::ifstream> inFile_;
122     std::unique_ptr<std::thread> inputLoop_;
123     std::unique_ptr<std::thread> outputLoop_;
124     std::unordered_map<uint32_t, OH_AVMemory *> inBufferMap_;
125     std::unordered_map<uint32_t, OH_AVMemory *> outBufferMap_;
126     OH_AVCodec *venc_;
127     OH_AVCodecAsyncCallback cb_;
128     int64_t timeStamp_ { 0 };
129     int64_t lastRenderedTimeUs_ { 0 };
130     bool isFirstFrame_ = true;
131     OHNativeWindow *nativeWindow;
132     int stride_;
133     static constexpr uint32_t SAMPLE_RATIO = 2;
134 };
135 } // namespace Media
136 } // namespace OHOS
137 
138 #endif // VIDEODEC_NDK_SAMPLE_H
139