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