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