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 AVCODEC_AUDIO_AVBUFFER_AAC_ENCODER_DEMO_H
17 #define AVCODEC_AUDIO_AVBUFFER_AAC_ENCODER_DEMO_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <fstream>
22 #include <queue>
23 #include <string>
24 #include <thread>
25 #include "nocopyable.h"
26 #include "common/native_mfmagic.h"
27 #include "native_avcodec_audiocodec.h"
28 
29 namespace OHOS {
30 namespace MediaAVCodec {
31 namespace AudioAacEncDemo {
32 
33 enum class AudioBufferFormatType : int32_t {
34     TYPE_AAC = 0,
35     TYPE_FLAC = 1,
36     TYPE_MP3 = 2,
37     TYPE_VORBIS = 3,
38     TYPE_AMRNB = 4,
39     TYPE_AMRWB = 5,
40     TYPE_vivid = 6,
41     TYPE_OPUS = 7,
42     TYPE_G711MU = 8,
43     TYPE_MAX = 9,
44 };
45 
46 class AEncSignal {
47 public:
48     std::mutex inMutex_;
49     std::mutex outMutex_;
50     std::mutex startMutex_;
51     std::condition_variable inCond_;
52     std::condition_variable outCond_;
53     std::condition_variable startCond_;
54     std::queue<uint32_t> inQueue_;
55     std::queue<uint32_t> outQueue_;
56     std::queue<OH_AVBuffer *> inBufferQueue_;
57     std::queue<OH_AVBuffer *> outBufferQueue_;
58 };
59 
60 /**
61   * @test
62   * @Status {Create, Configure, Start, Running, EOS, Flush, Stop, Reset, Release}
63   * @StatusErrCode AV_ERR_INVALID_STATE
64   * @Allow Create -> Configure
65   * @Allow Configure -> Start
66   * @Allow Start -> {Running, Flush, EOS, Stop}
67   * @Allow Flush -> {Start, Stop}
68   * @Allow Running -> {Running, EOS, Flush, Stop}
69   * @Allow EOS -> {Flush, Stop}
70   * @Allow Stop -> Start
71   * @Allow Reset -> Configure
72   * @Allow {Create, Configure, Start, Running, EOS, Flush, Stop, Reset} -> Reset
73   * @Allow {Create, Configure, Start, Running, EOS, Flush, Stop, Reset} -> Release
74 **/
75 class AudioBufferAacEncDemo : public NoCopyable {
76 public:
77     AudioBufferAacEncDemo();
78     virtual ~AudioBufferAacEncDemo();
79     /**
80       * @functionTest
81       * @input inputFile
82       * @output outputFile
83     **/
84     bool RunCase(std::string inputFile, std::string outputFile);
85     /**
86       * @interfaceTest
87       * @param mime; scope: {OH_AVCODEC_MIMETYPE_AUDIO_OPUS, OH_AVCODEC_MIMETYPE_AUDIO_G711MU};
88       *              default: OH_AVCODEC_MIMETYPE_AUDIO_OPUS;
89       * @Status Create
90     **/
91     OH_AVCodec* CreateByMime(const char* mime);
92 
93     /**
94       * @interfaceTest
95       * @param mime; scope: {"OH.Media.Codec.Encoder.Audio.OPUS", "OH.Media.Codec.Encoder.Audio.G711MU"};
96       *              default: "OH.Media.Codec.Encoder.Audio.OPUS";
97       * @Status Create
98     **/
99     OH_AVCodec* CreateByName(const char* mime);
100 
101     /**
102       * @interfaceTest
103       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
104       * @free CreateByMime, CreateByName
105       * @Status Release
106       * @return AV_ERR_OK
107     **/
108     OH_AVErrCode Destroy(OH_AVCodec* codec);
109 
110     /**
111       * @interfaceTest
112       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
113       * @return AV_ERR_OK
114     **/
115     OH_AVErrCode SetCallback(OH_AVCodec* codec);
116 
117     /**
118       * @interfaceTest
119       * @Status Configure
120       * @after SetCallback
121       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
122       * @param format; default: OH_AVFormat_Create(); code: AV_ERR_INVALID_VAL;
123       * @if mime is OH_AVCODEC_MIMETYPE_AUDIO_OPUS
124       * @param channel; scope: [1, 2]; default: 2; code: AV_ERR_INVALID_VAL;
125       * @param sampleRate; scope: {8000, 12000, 16000, 24000, 48000}; default: 48000;
126       *                    code: AV_ERR_INVALID_VAL;
127       * @param bitRate; scope: [6000, 510000]; default: 15000; code: AV_ERR_INVALID_VAL;
128       * @param sampleFormat; scope: {SAMPLE_S16LE}; default: SAMPLE_S16LE; code: AV_ERR_INVALID_VAL;
129       * @param sampleBit; scope: [16]; default: 16; code: AV_ERR_INVALID_VAL;
130       * @param complexity; scope: [1, 10]; default: 10; code: AV_ERR_INVALID_VAL;
131       * @if mime is OH_AVCODEC_MIMETYPE_AUDIO_G711MU
132       * @param channel; scope: {1}; default: 1; code: AV_ERR_INVALID_VAL;
133       * @param sampleRate; scope: {8000}; default: 8000; code: AV_ERR_INVALID_VAL;
134       * @param sampleFormat; scope: {SAMPLE_S16LE}; default: SAMPLE_S16LE; code: AV_ERR_INVALID_VAL;
135       * @return AV_ERR_OK
136     **/
137     OH_AVErrCode Configure(OH_AVCodec* codec, OH_AVFormat* format, int32_t channel, int32_t sampleRate,
138     int64_t bitRate, int32_t sampleFormat, int32_t sampleBit, int32_t complexity);
139 
140     /**
141       * @interfaceTest
142       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
143       * @return AV_ERR_OK
144     **/
145     OH_AVErrCode Prepare(OH_AVCodec* codec);
146 
147     /**
148       * @interfaceTest
149       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
150       * @Status Start
151       * @return AV_ERR_OK
152     **/
153     OH_AVErrCode Start(OH_AVCodec* codec);
154 
155     /**
156       * @interfaceTest
157       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
158       * @Status Stop
159       * @return AV_ERR_OK
160     **/
161     OH_AVErrCode Stop(OH_AVCodec* codec);
162 
163     /**
164       * @interfaceTest
165       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
166       * @Status Flush
167       * @return AV_ERR_OK
168     **/
169     OH_AVErrCode Flush(OH_AVCodec* codec);
170 
171     /**
172       * @interfaceTest
173       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
174       * @Status Reset
175       * @return AV_ERR_OK
176     **/
177     OH_AVErrCode Reset(OH_AVCodec* codec);
178 
179     /**
180       * @interfaceTest
181       * @param codec; depend: CreateByMime.return; code: nullptr;
182       * @after Start
183     **/
184     OH_AVFormat* GetOutputDescription(OH_AVCodec* codec);
185 
186     /**
187       * @interfaceTest
188       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
189       * @param index; depend: GetInputIndex.return; scope: [0, 7]; code: AV_ERR_NO_MEMORY;
190       * @param size; scope: [0, 65535]; default: 100; code: AV_ERR_INVALID_VAL;
191       * @param offset; scope: [0, 65535]; default: 0; code: AV_ERR_INVALID_VAL;
192       * @Status Running
193       * @return AV_ERR_OK
194     **/
195     OH_AVErrCode PushInputData(OH_AVCodec* codec, uint32_t index);
196 
197     /**
198       * @interfaceTest
199       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
200       * @param index; depend: GetInputIndex.return; scope: [0, 7]; code: AV_ERR_NO_MEMORY;
201       * @Status EOS
202       * @return AV_ERR_OK
203     **/
204     OH_AVErrCode PushInputDataEOS(OH_AVCodec* codec, uint32_t index);
205 
206     /**
207       * @interfaceTest
208       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
209       * @param index; depend: GetOutputIndex.return; scope: [0, 7]; code: AV_ERR_NO_MEMORY;
210       * @return AV_ERR_OK
211     **/
212     OH_AVErrCode FreeOutputData(OH_AVCodec* codec, uint32_t index);
213 
214     /**
215       * @interfaceTest
216       * @param codec; depend: CreateByMime.return; code: AV_ERR_INVALID_VAL;
217       * @param isValid; code: AV_ERR_INVALID_VAL;
218       * @return AV_ERR_OK
219     **/
220     OH_AVErrCode IsValid(OH_AVCodec* codec, bool* isValid);
221 
222     /**
223       * @after Start
224     **/
225     uint32_t GetInputIndex();
226 
227     /**
228       * @after PushInputDataEOS
229     **/
230     uint32_t GetOutputIndex();
231 
232     OH_AVErrCode SetParameter(OH_AVCodec* codec, OH_AVFormat* format, int32_t channel, int32_t sampleRate,
233     int64_t bitRate, int32_t sampleFormat, int32_t sampleBit, int32_t complexity);
234 private:
235     void ClearQueue();
236     int32_t CreateEnc();
237     int32_t Configure(OH_AVFormat *format);
238     int32_t Start();
239     int32_t Stop();
240     int32_t Flush();
241     int32_t Reset();
242     int32_t Release();
243     void InputFunc();
244     void OutputFunc();
245     void HandleEOS(const uint32_t &index);
246     int32_t GetFileSize(const std::string &filePath);
247     bool InitFile(std::string inputFile, std::string outputFile);
248     std::atomic<bool> isRunning_;
249     std::ifstream inputFile_;
250     std::ofstream outputFile_;
251     std::unique_ptr<std::thread> inputLoop_;
252     std::unique_ptr<std::thread> outputLoop_;
253     OH_AVCodec *audioEnc_;
254     AEncSignal *signal_;
255     struct OH_AVCodecCallback cb_;
256     bool isFirstFrame_ = true;
257     int64_t timeStamp_ = 0;
258     int32_t fileSize_ = 0;
259     uint32_t frameCount_ = 0;
260     int32_t sampleRate_;
261     int32_t channels_;
262     std::string inputFile_str;
263     std::string outputFile_str;
264     AudioBufferFormatType audioType_;
265 };
266 } // namespace AudioAacEncDemo
267 } // namespace MediaAVCodec
268 } // namespace OHOS
269 #endif // AVCODEC_AUDIO_AVBUFFER_AAC_ENCODER_DEMO_H
270