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_DECODER_DEMO_H
17 #define AVCODEC_AUDIO_DECODER_DEMO_H
18 
19 #include <atomic>
20 #include <fstream>
21 #include <queue>
22 #include <string>
23 #include <thread>
24 
25 #include "native_avcodec_audiodecoder.h"
26 #include "nocopyable.h"
27 
28 namespace OHOS {
29 namespace MediaAVCodec {
30 namespace AudioDemo {
31 enum AudioFormatType : int32_t {
32     TYPE_AAC = 0,
33     TYPE_FLAC = 1,
34     TYPE_MP3 = 2,
35     TYPE_VORBIS = 3,
36     TYPE_AMRNB = 4,
37     TYPE_AMRWB = 5,
38     TYPE_OPUS = 6,
39     TYPE_G711MU = 7,
40     TYPE_MAX = 8,
41 };
42 
43 class ADecSignal {
44 public:
45     std::mutex inMutex_;
46     std::mutex outMutex_;
47     std::mutex startMutex_;
48     std::condition_variable inCond_;
49     std::condition_variable outCond_;
50     std::condition_variable startCond_;
51     std::queue<uint32_t> inQueue_;
52     std::queue<uint32_t> outQueue_;
53     std::queue<OH_AVMemory *> inBufferQueue_;
54     std::queue<OH_AVMemory *> outBufferQueue_;
55     std::queue<OH_AVCodecBufferAttr> attrQueue_;
56 };
57 
58 class ADecDemo : public NoCopyable {
59 public:
60     ADecDemo();
61     virtual ~ADecDemo();
62     void RunCase(AudioFormatType audioType);
63 
64 private:
65     int32_t CreateDec();
66     int32_t Configure(OH_AVFormat *format);
67     int32_t Start();
68     int32_t Stop();
69     int32_t Flush();
70     int32_t Reset();
71     int32_t Release();
72     void InputFunc();
73     void OutputFunc();
74     void HandleInputEOS(const uint32_t index);
75     int32_t HandleNormalInput(const uint32_t &index, const int64_t pts, const size_t size);
76     bool InitFile(AudioFormatType audioType);
77 
78     std::atomic<bool> isRunning_ = false;
79     std::unique_ptr<std::ifstream> testFile_;
80     std::unique_ptr<std::thread> inputLoop_;
81     std::unique_ptr<std::thread> outputLoop_;
82     OH_AVCodec *audioDec_;
83     ADecSignal *signal_;
84     struct OH_AVCodecAsyncCallback cb_;
85     bool isFirstFrame_ = true;
86     uint32_t frameCount_ = 0;
87     std::ifstream inputFile_;
88     std::ofstream pcmOutputFile_;
89     AudioFormatType audioType_;
90 };
91 } // namespace AudioDemo
92 } // namespace MediaAVCodec
93 } // namespace OHOS
94 #endif // AVCODEC_AUDIO_DECODER_DEMO_H
95