• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_AAC_ENCODER_DEMO_H
17 #define AVCODEC_AUDIO_AVBUFFER_AAC_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 "nocopyable.h"
26 #include "common/native_mfmagic.h"
27 #include "native_avcodec_audiocodec.h"
28 
29 namespace OHOS {
30 namespace MediaAVCodec {
31 namespace AudioAacEncDemo {
32 
33 enum class AudioBufferFormatType : int32_t {
34     TYPE_AAC = 0,
35     TYPE_FLAC = 1,
36     TYPE_MP3 = 2,
37     TYPE_VORBIS = 3,
38     TYPE_AMRNB = 4,
39     TYPE_AMRWB = 5,
40     TYPE_VIVID = 6,
41     TYPE_OPUS = 7,
42     TYPE_G711MU = 8,
43     TYPE_LBVC = 10,
44 };
45 
46 class AEncSignal {
47 public:
48     std::mutex inMutex_;
49     std::mutex outMutex_;
50     std::mutex startMutex_;
51     std::condition_variable inCond_;
52     std::condition_variable outCond_;
53     std::condition_variable startCond_;
54     std::queue<uint32_t> inQueue_;
55     std::queue<uint32_t> outQueue_;
56     std::queue<OH_AVBuffer *> inBufferQueue_;
57     std::queue<OH_AVBuffer *> outBufferQueue_;
58 };
59 
60 class AudioBufferAacEncDemo : public NoCopyable {
61 public:
62     AudioBufferAacEncDemo();
63     virtual ~AudioBufferAacEncDemo();
64 
65     bool RunCase(const uint8_t *data, size_t size);
66     bool InitFile(const std::string& inputFile);
67 
68 private:
69     int32_t CreateEnc();
70     int32_t Configure(OH_AVFormat *format);
71     int32_t Start();
72     int32_t Stop();
73     int32_t Flush();
74     int32_t Reset();
75     int32_t Release();
76     void InputFunc();
77     void OutputFunc();
78     void Setformat(OH_AVFormat *format);
79     void HandleEOS(const uint32_t &index);
80     int32_t GetFileSize(const std::string &filePath);
81 
82     std::atomic<bool> isRunning_;
83     std::ifstream inputFile_;
84     std::ofstream outputFile_;
85     std::unique_ptr<std::thread> inputLoop_;
86     std::unique_ptr<std::thread> outputLoop_;
87     OH_AVCodec *audioEnc_;
88     AEncSignal *signal_;
89     struct OH_AVCodecCallback cb_;
90     bool isFirstFrame_ = true;
91     int64_t timeStamp_ = 0;
92     uint32_t frameCount_ = 0;
93     size_t inputdatasize = 0;
94     std::string inputdata;
95     AudioBufferFormatType audioType_ = AudioBufferFormatType::TYPE_AAC;
96 };
97 } // namespace AudioAacEncDemo
98 } // namespace MediaAVCodec
99 } // namespace OHOS
100 #endif // AVCODEC_AUDIO_AVBUFFER_AAC_ENCODER_DEMO_H
101