• 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 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_MAX = 4,
37 };
38 
39 class ADecSignal {
40 public:
41     std::mutex inMutex_;
42     std::mutex outMutex_;
43     std::mutex startMutex_;
44     std::condition_variable inCond_;
45     std::condition_variable outCond_;
46     std::condition_variable startCond_;
47     std::queue<uint32_t> inQueue_;
48     std::queue<uint32_t> outQueue_;
49     std::queue<OH_AVMemory *> inBufferQueue_;
50     std::queue<OH_AVMemory *> outBufferQueue_;
51     std::queue<OH_AVCodecBufferAttr> attrQueue_;
52 };
53 
54 class ADecDemo : public NoCopyable {
55 public:
56     ADecDemo();
57     virtual ~ADecDemo();
58     void RunCase(AudioFormatType audioType);
59 
60 private:
61     int32_t CreateDec();
62     int32_t Configure(OH_AVFormat *format);
63     int32_t Start();
64     int32_t Stop();
65     int32_t Flush();
66     int32_t Reset();
67     int32_t Release();
68     void InputFunc();
69     void OutputFunc();
70     void HandleInputEOS(const uint32_t index);
71     int32_t HandleNormalInput(const uint32_t &index, const int64_t pts, const size_t size);
72     bool InitFile(AudioFormatType audioType);
73 
74     std::atomic<bool> isRunning_ = false;
75     std::unique_ptr<std::ifstream> testFile_;
76     std::unique_ptr<std::thread> inputLoop_;
77     std::unique_ptr<std::thread> outputLoop_;
78     OH_AVCodec *audioDec_;
79     ADecSignal *signal_;
80     struct OH_AVCodecAsyncCallback cb_;
81     bool isFirstFrame_ = true;
82     uint32_t frameCount_ = 0;
83     std::ifstream inputFile_;
84     std::ofstream pcmOutputFile_;
85     AudioFormatType audioType_;
86 };
87 } // namespace AudioDemo
88 } // namespace MediaAVCodec
89 } // namespace OHOS
90 #endif // AVCODEC_AUDIO_DECODER_DEMO_H
91