• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 AUDIODECENC_NDK_SAMPLE_H
17 #define AUDIODECENC_NDK_SAMPLE_H
18 
19 #include <iostream>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <atomic>
23 #include <fstream>
24 #include <thread>
25 #include <mutex>
26 #include <queue>
27 #include <string>
28 #include "securec.h"
29 #include "native_avcodec_base.h"
30 #include "native_avcodec_audiodecoder.h"
31 #include "native_avcodec_audioencoder.h"
32 #include "nocopyable.h"
33 #include "ndktest_log.h"
34 
35 namespace OHOS {
36 namespace Media {
37 class ADecEncSignal {
38 public:
39     std::mutex inMutexDec_;
40     std::condition_variable inCondDec_;
41     std::queue<uint32_t> inQueueDec_;
42     std::queue<uint32_t> outQueueDec_;
43     std::queue<uint32_t>  sizeQueueDec_;
44     std::queue<uint32_t>  flagQueueDec_;
45     std::queue<OH_AVMemory *> inBufferQueueDec_;
46     std::queue<OH_AVMemory *> outBufferQueueDec_;
47 
48     std::mutex inMutexEnc_;
49     std::mutex outMutexEnc_;
50     std::condition_variable inCondEnc_;
51     std::condition_variable outCondEnc_;
52     std::queue<uint32_t> inQueueEnc_;
53     std::queue<uint32_t> outQueueEnc_;
54     std::queue<uint32_t>  sizeQueueEnc_;
55     std::queue<uint32_t>  flagQueueEnc_;
56     std::queue<OH_AVMemory *> inBufferQueueEnc_;
57     std::queue<OH_AVMemory *> outBufferQueueEnc_;
58     int32_t errorNum_ = 0;
59     std::atomic<bool> isFlushing_ = false;
60 };
61 
62 class ADecEncNdkSample : public NoCopyable {
63 public:
64     ADecEncNdkSample() = default;
65     ~ADecEncNdkSample();
66 
67     struct OH_AVCodec* CreateAudioDecoderByMime(std::string mimetype);
68     struct OH_AVCodec* CreateAudioDecoderByName(std::string name);
69     int32_t ConfigureDec(struct OH_AVFormat *format);
70     int32_t SetParameterDec(struct OH_AVFormat *format);
71     int32_t PrepareDec();
72     int32_t StartDec();
73     int32_t StopDec();
74     int32_t FlushDec();
75     int32_t ResetDec();
76     int32_t ReleaseDec();
77 
78     struct OH_AVCodec* CreateAudioEncoderByMime(std::string mimetype);
79     struct OH_AVCodec* CreateAudioEncoderByName(std::string name);
80     int32_t ConfigureEnc(struct OH_AVFormat *format);
81     int32_t SetParameterEnc(struct OH_AVFormat *format);
82     int32_t PrepareEnc();
83     int32_t StartEnc();
84     int32_t StopEnc();
85     int32_t FlushEnc();
86     int32_t ResetEnc();
87     int32_t ReleaseEnc();
88     int32_t CalcuError();
89 
90     void SetReadPath(const char* inp_path, uint32_t es[], uint32_t length);
91     void SetEosState(bool needSetEos);
92     void SetSavePath(const char* outp_path);
93     void ReRead();
94     void ResetDecParam();
95     void ResetEncParam();
96     int32_t GetFrameCount();
97     bool GetEncEosState();
98     bool GetDecEosState();
99     void PopInqueueDec();
100     void PopOutqueueDec();
101     void PopInqueueEnc();
102     void PopOutqueueEnc();
103     int32_t PushInbufferDec(uint32_t index, uint32_t bufferSize);
104     int32_t PushInbufferEnc();
105 
106     ADecEncSignal* acodecSignal_ = nullptr;
107     uint32_t decInCnt_ = 0;
108     uint32_t decOutCnt_ = 0;
109     uint32_t encInCnt_ = 0;
110     uint32_t encOutCnt_ = 0;
111     bool isDecInputEOS = false;
112     bool isEncInputEOS = false;
113     bool isDecOutputEOS = false;
114     bool isEncOutputEOS = false;
115     bool setEos = true;
116 
117 private:
118     struct OH_AVCodec* adec_;
119     void InputFuncDec();
120     std::atomic<bool> isDecRunning_ = false;
121     std::unique_ptr<std::ifstream> testFile_;
122     std::unique_ptr<std::thread> inputLoopDec_;
123     std::unique_ptr<std::thread> outputLoopDec_;
124     struct OH_AVCodecAsyncCallback cbDec_;
125     int64_t timeStampDec_ = 0;
126     struct OH_AVCodec* aenc_;
127     void InputFuncEnc();
128     void OutputFuncEnc();
129     int32_t WriteToFile();
130     std::atomic<bool> isEncRunning_ = false;
131     std::unique_ptr<std::thread> inputLoopEnc_;
132     std::unique_ptr<std::thread> outputLoopEnc_;
133     struct OH_AVCodecAsyncCallback cbEnc_;
134     int64_t timeStampEnc_ = 0;
135     std::string outDir_ = "/data/media/out.aac";
136     const char* INP_FILE;
137     const char* OUT_FILE;
138     uint32_t* ES;
139     uint32_t ES_LENGTH = 0;
140 };
141 }
142 }
143 #endif // AUDIODECENC_NDK_SAMPLE_H
144