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_ENCODER_INNER_DEMO_H
17 #define AVCODEC_AUDIO_ENCODER_INNER_DEMO_H
18 
19 #include <atomic>
20 #include <fstream>
21 #include <thread>
22 #include <queue>
23 #include <string>
24 #include "avcodec_common.h"
25 #include "avcodec_audio_encoder.h"
26 #include "nocopyable.h"
27 
28 namespace OHOS {
29 namespace MediaAVCodec {
30 namespace InnerAudioDemo {
31 class AEnSignal {
32 public:
33     std::mutex inMutex_;
34     std::mutex outMutex_;
35     std::condition_variable inCond_;
36     std::condition_variable outCond_;
37     std::queue<uint32_t> inQueue_;
38     std::queue<std::shared_ptr<AVSharedMemory>> inBufferQueue_;
39     std::queue<std::shared_ptr<AVSharedMemory>> outBufferQueue_;
40     std::queue<uint32_t> outQueue_;
41     std::queue<AVCodecBufferInfo> sizeQueue_;
42 };
43 
44 class AEnDemoCallback : public AVCodecCallback, public NoCopyable {
45 public:
46     explicit AEnDemoCallback(std::shared_ptr<AEnSignal> signal);
47     virtual ~AEnDemoCallback() = default;
48 
49     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
50     void OnOutputFormatChanged(const Format &format) override;
51     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override;
52     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
53                                  std::shared_ptr<AVSharedMemory> buffer) override;
54 
55 private:
56     std::shared_ptr<AEnSignal> signal_;
57 };
58 
59 class AEnInnerDemo : public NoCopyable {
60 public:
61     AEnInnerDemo() = default;
62     virtual ~AEnInnerDemo() = default;
63     void RunCase();
64 
65 private:
66     int32_t CreateDec();
67     int32_t Configure(const Format &format);
68     int32_t Start();
69     int32_t Stop();
70     int32_t Flush();
71     int32_t Reset();
72     int32_t Release();
73     void InputFunc();
74     void OutputFunc();
75 
76     std::atomic<bool> isRunning_ = false;
77     std::unique_ptr<std::ifstream> testFile_;
78     std::unique_ptr<std::thread> inputLoop_;
79     std::unique_ptr<std::thread> outputLoop_;
80     std::shared_ptr<AVCodecAudioEncoder> audioEn_;
81     std::shared_ptr<AEnSignal> signal_;
82     std::shared_ptr<AEnDemoCallback> cb_;
83 };
84 } // namespace InnerAudioDemo
85 } // namespace MediaAVCodec
86 } // namespace OHOS
87 #endif // AVCODEC_AUDIO_DECODER_INNER_DEMO_H
88