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_AMRWB_ENCODER_DEMO_H
17 #define AVCODEC_AUDIO_AVBUFFER_AMRWB_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 "common/native_mfmagic.h"
26 #include "native_avcodec_audiocodec.h"
27 #include "nocopyable.h"
28 
29 namespace OHOS {
30 namespace MediaAVCodec {
31 namespace AudioAvbufferAmrWbDemo {
32 class AEncSignal {
33    public:
34     std::mutex inMutex_;
35     std::mutex outMutex_;
36     std::mutex startMutex_;
37     std::condition_variable inCond_;
38     std::condition_variable outCond_;
39     std::condition_variable startCond_;
40     std::queue<uint32_t> inQueue_;
41     std::queue<uint32_t> outQueue_;
42     std::queue<OH_AVBuffer*> inBufferQueue_;
43     std::queue<OH_AVBuffer*> outBufferQueue_;
44 };
45 
46 class AEncAvbufferAmrWbDemo : public NoCopyable {
47    public:
48     AEncAvbufferAmrWbDemo();
49     virtual ~AEncAvbufferAmrWbDemo();
50     void RunCase();
51 
52    private:
53     int32_t CreateEnc();
54     int32_t Configure(OH_AVFormat* format);
55     int32_t Start();
56     int32_t Stop();
57     int32_t Flush();
58     int32_t Reset();
59     int32_t Release();
60     void InputFunc();
61     void OutputFunc();
62     void HandleEOS(const uint32_t& index);
63     int32_t GetFileSize(const std::string& filePath);
64 
65     std::atomic<bool> isRunning_;
66     std::unique_ptr<std::ifstream> inputFile_;
67     std::unique_ptr<std::ofstream> outputFile_;
68     std::unique_ptr<std::thread> inputLoop_;
69     std::unique_ptr<std::thread> outputLoop_;
70     OH_AVCodec* audioEnc_;
71     AEncSignal* signal_;
72     struct OH_AVCodecCallback cb_;
73     bool isFirstFrame_ = true;
74     int32_t fileSize_ = 0;
75     uint32_t frameCount_ = 0;
76 };
77 }  // namespace AudioAvbufferAmrWbDemo
78 }  // namespace MediaAVCodec
79 }  // namespace OHOS
80 #endif  // AVCODEC_AUDIO_AVBUFFER_AMRWB_ENCODER_DEMO_H