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 AVCODEC_AUDIO_AVBUFFER_DECODER_FLUSH_DEMO_H
17 #define AVCODEC_AUDIO_AVBUFFER_DECODER_FLUSH_DEMO_H
18 
19 #include <atomic>
20 #include <fstream>
21 #include <queue>
22 #include <string>
23 #include <thread>
24 
25 #include "native_avcodec_audiocodec.h"
26 #include "nocopyable.h"
27 #include "common/native_mfmagic.h"
28 #include "native_avdemuxer.h"
29 #include "avcodec_audio_common.h"
30 
31 namespace OHOS {
32 namespace MediaAVCodec {
33 namespace AudioBufferDemo {
34 enum class AudioBufferFormatType : int32_t {
35     TYPE_AAC = 0,
36     TYPE_FLAC = 1,
37     TYPE_MP3 = 2,
38     TYPE_VORBIS = 3,
39     TYPE_AMRNB = 4,
40     TYPE_AMRWB = 5,
41     TYPE_VIVID = 6,
42     TYPE_OPUS = 7,
43     TYPE_G711MU = 8,
44     TYPE_APE = 9,
45     TYPE_LBVC = 10,
46     TYPE_MAX = 20,
47 };
48 
49 class ADecBufferSignal {
50 public:
51     std::mutex inMutex_;
52     std::mutex outMutex_;
53     std::mutex startMutex_;
54     std::condition_variable inCond_;
55     std::condition_variable outCond_;
56     std::condition_variable startCond_;
57     std::queue<uint32_t> inQueue_;
58     std::queue<uint32_t> outQueue_;
59     std::queue<OH_AVBuffer *> inBufferQueue_;
60     std::queue<OH_AVBuffer *> outBufferQueue_;
61 };
62 
63 class ADecBufferDemo : public NoCopyable {
64 public:
65     ADecBufferDemo();
66     virtual ~ADecBufferDemo();
67     /**
68       * @functionTest
69       * @input inputFile
70       * @output outputFile
71     **/
72     bool InitFile(const std::string& inputFile);
73     bool RunCaseFlush(const uint8_t *data, size_t size);
74     OH_AVCodec* CreateByMime(const char* mime);
75     OH_AVCodec* CreateByName(const char* name);
76     OH_AVErrCode Destroy(OH_AVCodec* codec);
77     OH_AVErrCode SetCallback(OH_AVCodec* codec);
78     OH_AVErrCode Configure(OH_AVCodec* codec, OH_AVFormat* format, int32_t channel, int32_t sampleRate);
79     OH_AVErrCode Prepare(OH_AVCodec* codec);
80     OH_AVErrCode Start(OH_AVCodec* codec);
81     OH_AVErrCode Stop(OH_AVCodec* codec);
82     OH_AVErrCode Flush(OH_AVCodec* codec);
83     OH_AVErrCode Reset(OH_AVCodec* codec);
84     OH_AVFormat* GetOutputDescription(OH_AVCodec* codec);
85     OH_AVErrCode PushInputData(OH_AVCodec* codec, uint32_t index);
86     OH_AVErrCode FreeOutputData(OH_AVCodec* codec, uint32_t index);
87     OH_AVErrCode IsValid(OH_AVCodec* codec, bool* isValid);
88     uint32_t GetInputIndex();
89     OH_AVErrCode PushInputDataEOS(OH_AVCodec* codec, uint32_t index);
90     uint32_t GetOutputIndex();
91     OH_AVErrCode SetParameter(OH_AVCodec* codec, OH_AVFormat* format, int32_t channel, int32_t sampleRate);
92 
93 private:
94     int32_t CreateDec();
95     int32_t Configure(OH_AVFormat *format);
96     int32_t Start();
97     int32_t Stop();
98     int32_t Flush();
99     int32_t Reset();
100     int32_t Release();
101     void InputFunc();
102     void OutputFunc();
103     void HandleInputEOS(const uint32_t index);
104     bool InitFormat(OH_AVFormat *format);
105     std::atomic<bool> isRunning_ = false;
106     std::unique_ptr<std::thread> inputLoop_;
107     std::unique_ptr<std::thread> outputLoop_;
108     OH_AVCodec *audioDec_;
109     ADecBufferSignal *signal_;
110     struct OH_AVCodecCallback cb_;
111     bool isFirstFrame_ = true;
112     uint32_t frameCount_ = 0;
113     AudioBufferFormatType audioType_;
114     size_t inputdatasize = 0;
115     std::string inputdata;
116     bool eosFlag = false;
117 };
118 } // namespace AudioBufferDemo
119 } // namespace MediaAVCodec
120 } // namespace OHOS
121 #endif // AVCODEC_AUDIO_AVBUFFER_DECODER_FLUSH_DEMO_H
122