• 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 #include <iostream>
17 #include <unistd.h>
18 #include <chrono>
19 #include "securec.h"
20 #include "avcodec_common.h"
21 #include "avcodec_errors.h"
22 #include "media_description.h"
23 #include "native_avformat.h"
24 #include "demo_log.h"
25 #include "avcodec_codec_name.h"
26 #include "native_avmemory.h"
27 #include "native_avbuffer.h"
28 #include "ffmpeg_converter.h"
29 #include "audio_encoder_demo.h"
30 
31 using namespace OHOS;
32 using namespace OHOS::MediaAVCodec;
33 using namespace OHOS::MediaAVCodec::AudioAacEncDemo;
34 using namespace std;
35 namespace {
36 constexpr uint32_t SAMPLE_RATE_44100 = 44100;
37 constexpr uint32_t BIT_RATE_128000 = 128000;
38 constexpr int32_t SAMPLE_FORMAT_S16 = AudioSampleFormat::SAMPLE_S16LE;
39 constexpr int32_t CHANNEL_1 = 1;
40 constexpr int32_t CHANNEL_2 = 2;
41 constexpr int32_t CHANNEL_3 = 3;
42 constexpr int32_t CHANNEL_4 = 4;
43 constexpr int32_t CHANNEL_5 = 5;
44 constexpr int32_t CHANNEL_6 = 6;
45 constexpr int32_t CHANNEL_7 = 7;
46 constexpr int32_t CHANNEL_8 = 8;
47 } // namespace
48 
49 
GetChannelLayout(int32_t channel)50 static uint64_t GetChannelLayout(int32_t channel)
51 {
52     switch (channel) {
53         case CHANNEL_1:
54             return MONO;
55         case CHANNEL_2:
56             return STEREO;
57         case CHANNEL_3:
58             return CH_2POINT1;
59         case CHANNEL_4:
60             return CH_3POINT1;
61         case CHANNEL_5:
62             return CH_4POINT1;
63         case CHANNEL_6:
64             return CH_5POINT1;
65         case CHANNEL_7:
66             return CH_6POINT1;
67         case CHANNEL_8:
68             return CH_7POINT1;
69         default:
70             return UNKNOWN_CHANNEL_LAYOUT;
71     }
72 }
73 
OnError(OH_AVCodec * codec,int32_t errorCode,void * userData)74 static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
75 {
76     (void)codec;
77     (void)errorCode;
78     (void)userData;
79 }
80 
OnOutputFormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)81 static void OnOutputFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
82 {
83     (void)codec;
84     (void)format;
85     (void)userData;
86     cout << "OnOutputFormatChanged received" << endl;
87 }
88 
OnInputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)89 static void OnInputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
90 {
91     (void)codec;
92     AEncSignal *signal = static_cast<AEncSignal *>(userData);
93     unique_lock<mutex> lock(signal->inMutex_);
94     signal->inQueue_.push(index);
95     signal->inBufferQueue_.push(buffer);
96     signal->inCond_.notify_all();
97 }
98 
OnOutputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * buffer,void * userData)99 static void OnOutputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *buffer, void *userData)
100 {
101     (void)codec;
102     AEncSignal *signal = static_cast<AEncSignal *>(userData);
103     unique_lock<mutex> lock(signal->outMutex_);
104     signal->outQueue_.push(index);
105     signal->outBufferQueue_.push(buffer);
106     if (buffer) {
107         cout << "OnOutputBufferAvailable received, index:" << index << ", size:" << buffer->buffer_->memory_->GetSize()
108              << ", flags:" << buffer->buffer_->flag_ << ", pts: " << buffer->buffer_->pts_ << endl;
109     }
110     signal->outCond_.notify_all();
111 }
112 
InitFile(const std::string & inputFile)113 bool AudioBufferAacEncDemo::InitFile(const std::string& inputFile)
114 {
115     if (inputFile.find("mp4") != std::string::npos || inputFile.find("m4a") != std::string::npos ||
116         inputFile.find("vivid") != std::string::npos) {
117         audioType_ = AudioBufferFormatType::TYPE_VIVID;
118     } else if (inputFile.find("opus") != std::string::npos) {
119         audioType_ = AudioBufferFormatType::TYPE_OPUS;
120     } else if (inputFile.find("g711") != std::string::npos) {
121         audioType_ = AudioBufferFormatType::TYPE_G711MU;
122     } else if (inputFile.find("lbvc") != std::string::npos) {
123         audioType_ = AudioBufferFormatType::TYPE_LBVC;
124     } else if (inputFile.find("flac") != std::string::npos) {
125         audioType_ = AudioBufferFormatType::TYPE_FLAC;
126     } else if (inputFile.find("amrnb") != std::string::npos) {
127         audioType_ = AudioBufferFormatType::TYPE_AMRNB;
128     } else if (inputFile.find("amrwb") != std::string::npos) {
129         audioType_ = AudioBufferFormatType::TYPE_AMRWB;
130     } else if (inputFile.find("mp3") != std::string::npos) {
131         audioType_ = AudioBufferFormatType::TYPE_MP3;
132     } else {
133         audioType_ = AudioBufferFormatType::TYPE_AAC;
134     }
135 
136     return true;
137 }
138 
RunCase(const uint8_t * data,size_t size)139 bool AudioBufferAacEncDemo::RunCase(const uint8_t *data, size_t size)
140 {
141     std::string codecdata(reinterpret_cast<const char *>(data), size);
142     inputdata = codecdata;
143     inputdatasize = size;
144     DEMO_CHECK_AND_RETURN_RET_LOG(CreateEnc() == AVCS_ERR_OK, false, "Fatal: CreateEnc fail");
145     OH_AVFormat *format = OH_AVFormat_Create();
146     Setformat(format);
147     DEMO_CHECK_AND_RETURN_RET_LOG(Configure(format) == AVCS_ERR_OK, false, "Fatal: Configure fail");
148     DEMO_CHECK_AND_RETURN_RET_LOG(Start() == AVCS_ERR_OK, false, "Fatal: Start fail");
149     {
150         unique_lock<mutex> lock(signal_->startMutex_);
151         signal_->startCond_.wait(lock, [this]() { return (!(isRunning_.load())); });
152     }
153     DEMO_CHECK_AND_RETURN_RET_LOG(Stop() == AVCS_ERR_OK, false, "Fatal: Stop fail");
154     DEMO_CHECK_AND_RETURN_RET_LOG(Release() == AVCS_ERR_OK, false, "Fatal: Release fail");
155     OH_AVFormat_Destroy(format);
156     sleep(1);
157     return true;
158 }
159 
Setformat(OH_AVFormat * format)160 void AudioBufferAacEncDemo::Setformat(OH_AVFormat *format)
161 {
162     uint64_t channelLayout;
163     long bitrate = BIT_RATE_128000;
164     int32_t sampleRate = SAMPLE_RATE_44100;
165     int32_t channelCount = CHANNEL_2;
166     int32_t sampleFormat = SAMPLE_FORMAT_S16;
167     channelLayout = GetChannelLayout(channelCount);
168     OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, channelLayout);
169     OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, AAC_PROFILE_HE);
170     OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), bitrate);
171     OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), sampleRate);
172     OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), channelCount);
173     OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), sampleFormat);
174 }
175 
AudioBufferAacEncDemo()176 AudioBufferAacEncDemo::AudioBufferAacEncDemo() : isRunning_(false), audioEnc_(nullptr), signal_(nullptr), frameCount_(0)
177 {
178     signal_ = new AEncSignal();
179     DEMO_CHECK_AND_RETURN_LOG(signal_ != nullptr, "Fatal: No memory");
180 }
181 
~AudioBufferAacEncDemo()182 AudioBufferAacEncDemo::~AudioBufferAacEncDemo()
183 {
184     if (signal_) {
185         delete signal_;
186         signal_ = nullptr;
187     }
188 }
189 
CreateEnc()190 int32_t AudioBufferAacEncDemo::CreateEnc()
191 {
192     audioEnc_ = OH_AudioCodec_CreateByMime(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
193     DEMO_CHECK_AND_RETURN_RET_LOG(audioEnc_ != nullptr, AVCS_ERR_UNKNOWN, "Fatal: CreateByName fail");
194     if (signal_ == nullptr) {
195         signal_ = new AEncSignal();
196         DEMO_CHECK_AND_RETURN_RET_LOG(signal_ != nullptr, AVCS_ERR_UNKNOWN, "Fatal: No memory");
197     }
198     cb_ = {&OnError, &OnOutputFormatChanged, &OnInputBufferAvailable, &OnOutputBufferAvailable};
199     int32_t ret = OH_AudioCodec_RegisterCallback(audioEnc_, cb_, signal_);
200     DEMO_CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Fatal: SetCallback fail");
201 
202     return AVCS_ERR_OK;
203 }
204 
Configure(OH_AVFormat * format)205 int32_t AudioBufferAacEncDemo::Configure(OH_AVFormat *format)
206 {
207     int32_t ret = OH_AudioCodec_Configure(audioEnc_, format);
208     return ret;
209 }
210 
Start()211 int32_t AudioBufferAacEncDemo::Start()
212 {
213     isRunning_.store(true);
214 
215     inputLoop_ = make_unique<thread>(&AudioBufferAacEncDemo::InputFunc, this);
216     DEMO_CHECK_AND_RETURN_RET_LOG(inputLoop_ != nullptr, AVCS_ERR_UNKNOWN, "Fatal: No memory");
217 
218     outputLoop_ = make_unique<thread>(&AudioBufferAacEncDemo::OutputFunc, this);
219     DEMO_CHECK_AND_RETURN_RET_LOG(outputLoop_ != nullptr, AVCS_ERR_UNKNOWN, "Fatal: No memory");
220     return OH_AudioCodec_Start(audioEnc_);
221 }
222 
Stop()223 int32_t AudioBufferAacEncDemo::Stop()
224 {
225     isRunning_.store(false);
226 
227     if (inputLoop_ != nullptr && inputLoop_->joinable()) {
228         {
229             unique_lock<mutex> lock(signal_->inMutex_);
230             signal_->inCond_.notify_all();
231         }
232         inputLoop_->join();
233         inputLoop_ = nullptr;
234         while (!signal_->inQueue_.empty()) {
235             signal_->inQueue_.pop();
236         }
237         while (!signal_->inBufferQueue_.empty()) {
238             signal_->inBufferQueue_.pop();
239         }
240     }
241 
242     if (outputLoop_ != nullptr && outputLoop_->joinable()) {
243         {
244             unique_lock<mutex> lock(signal_->outMutex_);
245             signal_->outCond_.notify_all();
246         }
247         outputLoop_->join();
248         outputLoop_ = nullptr;
249         while (!signal_->outQueue_.empty()) {
250             signal_->outQueue_.pop();
251         }
252         while (!signal_->outBufferQueue_.empty()) {
253             signal_->outBufferQueue_.pop();
254         }
255     }
256 
257     return OH_AudioCodec_Stop(audioEnc_);
258 }
259 
Flush()260 int32_t AudioBufferAacEncDemo::Flush()
261 {
262     if (inputLoop_ != nullptr && inputLoop_->joinable()) {
263         {
264             unique_lock<mutex> lock(signal_->inMutex_);
265             signal_->inCond_.notify_all();
266         }
267         inputLoop_->join();
268         inputLoop_ = nullptr;
269         while (!signal_->inQueue_.empty()) {
270             signal_->inQueue_.pop();
271         }
272         while (!signal_->inBufferQueue_.empty()) {
273             signal_->inBufferQueue_.pop();
274         }
275     }
276 
277     if (outputLoop_ != nullptr && outputLoop_->joinable()) {
278         {
279             unique_lock<mutex> lock(signal_->outMutex_);
280             signal_->outCond_.notify_all();
281         }
282         outputLoop_->join();
283         outputLoop_ = nullptr;
284         while (!signal_->outQueue_.empty()) {
285             signal_->outQueue_.pop();
286         }
287         while (!signal_->outBufferQueue_.empty()) {
288             signal_->outBufferQueue_.pop();
289         }
290     }
291     return OH_AudioCodec_Flush(audioEnc_);
292 }
293 
Reset()294 int32_t AudioBufferAacEncDemo::Reset()
295 {
296     return OH_AudioCodec_Reset(audioEnc_);
297 }
298 
Release()299 int32_t AudioBufferAacEncDemo::Release()
300 {
301     return OH_AudioCodec_Destroy(audioEnc_);
302 }
303 
HandleEOS(const uint32_t & index)304 void AudioBufferAacEncDemo::HandleEOS(const uint32_t &index)
305 {
306     OH_AudioCodec_PushInputBuffer(audioEnc_, index);
307     signal_->inQueue_.pop();
308     signal_->inBufferQueue_.pop();
309 }
310 
InputFunc()311 void AudioBufferAacEncDemo::InputFunc()
312 {
313     size_t frameBytes = 1024;
314     while (isRunning_.load()) {
315         unique_lock<mutex> lock(signal_->inMutex_);
316         signal_->inCond_.wait(lock, [this]() { return (signal_->inQueue_.size() > 0 || !isRunning_.load()); });
317         if (!isRunning_.load()) {
318             break;
319         }
320         uint32_t index = signal_->inQueue_.front();
321         auto buffer = signal_->inBufferQueue_.front();
322         DEMO_CHECK_AND_BREAK_LOG(buffer != nullptr, "Fatal: GetInputBuffer fail");
323         size_t currentSize = inputdatasize < frameBytes ? inputdatasize : frameBytes;
324         if (currentSize == 0) {
325             buffer->buffer_->flag_ = AVCODEC_BUFFER_FLAGS_EOS;
326             OH_AudioCodec_PushInputBuffer(audioEnc_, index);
327             break;
328         }
329         std::string currentStr = inputdata.substr(inputOffset, currentSize);
330         int ret;
331         strncpy_s(reinterpret_cast<char*>(OH_AVBuffer_GetAddr(buffer)), currentSize, currentStr.c_str(), currentSize);
332         buffer->buffer_->memory_->SetSize(currentSize);
333         if (isFirstFrame_) {
334             buffer->buffer_->flag_ = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
335             ret = OH_AudioCodec_PushInputBuffer(audioEnc_, index);
336             isFirstFrame_ = false;
337         } else {
338             buffer->buffer_->flag_ = AVCODEC_BUFFER_FLAGS_NONE;
339             ret = OH_AudioCodec_PushInputBuffer(audioEnc_, index);
340         }
341         signal_->inQueue_.pop();
342         signal_->inBufferQueue_.pop();
343         inputdatasize -= currentSize;
344         inputOffset += currentSize;
345         frameCount_++;
346         if (ret != AVCS_ERR_OK) {
347             isRunning_.store(false);
348             break;
349         }
350     }
351     signal_->outCond_.notify_all();
352 }
353 
OutputFunc()354 void AudioBufferAacEncDemo::OutputFunc()
355 {
356     while (isRunning_.load()) {
357         unique_lock<mutex> lock(signal_->outMutex_);
358         signal_->outCond_.wait(lock, [this]() { return (signal_->outQueue_.size() > 0 || !isRunning_.load()); });
359 
360         if (!isRunning_.load()) {
361             cout << "wait to stop, exit" << endl;
362             break;
363         }
364 
365         uint32_t index = signal_->outQueue_.front();
366         OH_AVBuffer *avBuffer = signal_->outBufferQueue_.front();
367         if (avBuffer == nullptr) {
368             cout << "OutputFunc OH_AVBuffer is nullptr" << endl;
369             continue;
370         }
371         std::cout << "OutputFunc index:" << index << endl;
372         if (avBuffer != nullptr &&
373             (avBuffer->buffer_->flag_ == AVCODEC_BUFFER_FLAGS_EOS || avBuffer->buffer_->memory_->GetSize() == 0)) {
374             cout << "encode eos" << endl;
375             isRunning_.store(false);
376             signal_->startCond_.notify_all();
377         }
378 
379         signal_->outBufferQueue_.pop();
380         signal_->outQueue_.pop();
381         if (OH_AudioCodec_FreeOutputBuffer(audioEnc_, index) != AV_ERR_OK) {
382             cout << "Fatal: FreeOutputData fail" << endl;
383             break;
384         }
385     }
386     signal_->startCond_.notify_all();
387     cout << "stop, exit" << endl;
388 }