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 #include <vector>
17 #include <mutex>
18 #include <gtest/gtest.h>
19 #include <iostream>
20 #include <unistd.h>
21 #include <atomic>
22 #include <fstream>
23 #include <queue>
24 #include <string>
25 #include <thread>
26 #include "avcodec_codec_name.h"
27 #include "avcodec_mime_type.h"
28 #include "avcodec_common.h"
29 #include "media_description.h"
30 #include "native_avformat.h"
31 #include "avcodec_errors.h"
32 #include "native_avcodec_audioencoder.h"
33 #include "native_avcodec_audiocodec.h"
34 #include "native_avcodec_base.h"
35 #include "native_avcapability.h"
36 #include "native_avbuffer.h"
37 #include "native_audio_channel_layout.h"
38 #include "securec.h"
39 #include "ffmpeg_converter.h"
40
41 using namespace std;
42 using namespace testing::ext;
43 using namespace OHOS::MediaAVCodec;
44
45 namespace {
46 const string CODEC_FLAC_NAME = std::string(AVCodecCodecName::AUDIO_ENCODER_FLAC_NAME);
47 const string CODEC_AAC_NAME = std::string(AVCodecCodecName::AUDIO_ENCODER_AAC_NAME);
48 const string CODEC_OPUS_NAME = std::string(AVCodecCodecName::AUDIO_ENCODER_OPUS_NAME);
49 const string CODEC_G711MU_NAME = std::string(AVCodecCodecName::AUDIO_ENCODER_G711MU_NAME);
50
51 constexpr uint32_t CHANNEL_COUNT = 2;
52 constexpr uint32_t ABNORMAL_CHANNEL_COUNT = 10;
53 constexpr uint32_t SAMPLE_RATE = 44100;
54 constexpr uint32_t ABNORMAL_SAMPLE_RATE = 9999999;
55 constexpr int64_t BITS_RATE = 261000;
56 constexpr int64_t ABNORMAL_BITS_RATE = -1;
57 constexpr int32_t BITS_PER_CODED_SAMPLE = AudioSampleFormat::SAMPLE_S16LE;
58 constexpr int32_t ABNORMAL_BITS_SAMPLE = AudioSampleFormat::INVALID_WIDTH;
59 constexpr uint32_t FRAME_DURATION_US = 33000;
60 constexpr uint64_t CHANNEL_LAYOUT = AudioChannelLayout::STEREO;
61 constexpr uint64_t ABNORMAL_CHANNEL_LAYOUT = AudioChannelLayout::CH_10POINT2;
62 constexpr int32_t SAMPLE_FORMAT = AudioSampleFormat::SAMPLE_S16LE;
63 constexpr uint32_t FLAC_DEFAULT_FRAME_BYTES = 18432;
64 constexpr uint32_t AAC_DEFAULT_FRAME_BYTES = 2 * 1024 * 4;
65 constexpr uint32_t G711MU_DEFAULT_FRAME_BYTES = 320; // 8kHz 20ms: 2*160
66 constexpr uint32_t G711MU_SAMPLE_RATE = 8000;
67 constexpr uint32_t G711MU_CHANNEL_COUNT = 1;
68 constexpr uint32_t ILLEGAL_CHANNEL_COUNT = 9;
69 constexpr uint32_t ILLEGAL_SAMPLE_RATE = 441000;
70 constexpr int32_t COMPLIANCE_LEVEL = 0;
71 constexpr int32_t ABNORMAL_COMPLIANCE_LEVEL_L = -9999999;
72 constexpr int32_t ABNORMAL_COMPLIANCE_LEVEL_R = 9999999;
73 constexpr int32_t MAX_INPUT_SIZE = 8192;
74 constexpr uint32_t OPUS_CHANNEL_COUNT = 2;
75 constexpr uint32_t OPUS_SAMPLE_RATE = 48000;
76 constexpr long OPUS_BITS_RATE = 15000;
77 constexpr int32_t OPUS_COMPLIANCE_LEVEL = 10;
78 constexpr int32_t OPUS_FRAME_SAMPLE_SIZES = 960 * 2 * 2;
79
80 constexpr string_view OPUS_INPUT_FILE_PATH = "/data/test/media/flac_2c_44100hz_261k.pcm";
81 constexpr string_view OPUS_OUTPUT_FILE_PATH = "/data/test/media/encoderTest.opus";
82 constexpr string_view FLAC_INPUT_FILE_PATH = "/data/test/media/flac_2c_44100hz_261k.pcm";
83 constexpr string_view FLAC_OUTPUT_FILE_PATH = "/data/test/media/encoderTest.flac";
84
85 constexpr string_view AAC_INPUT_FILE_PATH = "/data/test/media/aac_2c_44100hz_199k.pcm";
86 constexpr string_view AAC_OUTPUT_FILE_PATH = "/data/test/media/aac_2c_44100hz_encode.aac";
87 const string OPUS_SO_FILE_PATH = std::string(AV_CODEC_PATH) + "/libav_codec_ext_base.z.so";
88
89 constexpr string_view G711MU_INPUT_FILE_PATH = "/data/test/media/g711mu_8kHz_10s.pcm";
90 constexpr string_view G711MU_OUTPUT_FILE_PATH = "/data/test/media/g711mu_8kHz_10s_afterEncode.raw";
91 } // namespace
92
93 namespace OHOS {
94 namespace MediaAVCodec {
95 class AEncSignal {
96 public:
97 std::mutex inMutex_;
98 std::mutex outMutex_;
99 std::condition_variable inCond_;
100 std::condition_variable outCond_;
101 std::queue<uint32_t> inQueue_;
102 std::queue<uint32_t> outQueue_;
103 std::queue<OH_AVMemory *> inBufferQueue_;
104 std::queue<OH_AVMemory *> outBufferQueue_;
105 std::queue<OH_AVCodecBufferAttr> attrQueue_;
106 };
107
108 class AEncSignalAv {
109 public:
110 std::mutex inMutex_;
111 std::mutex outMutex_;
112 std::mutex startMutex_;
113 std::condition_variable inCond_;
114 std::condition_variable outCond_;
115 std::condition_variable startCond_;
116 std::queue<uint32_t> inQueue_;
117 std::queue<uint32_t> outQueue_;
118 std::queue<OH_AVBuffer*> inBufferQueue_;
119 std::queue<OH_AVBuffer*> outBufferQueue_;
120 };
121
OnError(OH_AVCodec * codec,int32_t errorCode,void * userData)122 static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
123 {
124 (void)codec;
125 (void)errorCode;
126 (void)userData;
127 cout << "Error received, errorCode:" << errorCode << endl;
128 }
129
OnOutputFormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)130 static void OnOutputFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
131 {
132 (void)codec;
133 (void)format;
134 (void)userData;
135 cout << "OnOutputFormatChanged received" << endl;
136 }
137
OnInputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,void * userData)138 static void OnInputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData)
139 {
140 (void)codec;
141 AEncSignal *signal = static_cast<AEncSignal *>(userData);
142 unique_lock<mutex> lock(signal->inMutex_);
143 signal->inQueue_.push(index);
144 signal->inBufferQueue_.push(data);
145 signal->inCond_.notify_all();
146 }
147
OnOutputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,OH_AVCodecBufferAttr * attr,void * userData)148 static void OnOutputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
149 void *userData)
150 {
151 (void)codec;
152 AEncSignal *signal = static_cast<AEncSignal *>(userData);
153 unique_lock<mutex> lock(signal->outMutex_);
154 signal->outQueue_.push(index);
155 signal->outBufferQueue_.push(data);
156 if (attr) {
157 signal->attrQueue_.push(*attr);
158 } else {
159 cout << "OnOutputBufferAvailable error, attr is nullptr!" << endl;
160 }
161 signal->outCond_.notify_all();
162 }
163
OnInputBufferAvailableAv(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)164 static void OnInputBufferAvailableAv(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
165 {
166 (void)codec;
167 AEncSignalAv *signal = static_cast<AEncSignalAv *>(userData);
168 unique_lock<mutex> lock(signal->inMutex_);
169 signal->inQueue_.push(index);
170 signal->inBufferQueue_.push(data);
171 signal->inCond_.notify_all();
172 }
173
OnOutputBufferAvailableAv(OH_AVCodec * codec,uint32_t index,OH_AVBuffer * data,void * userData)174 static void OnOutputBufferAvailableAv(OH_AVCodec *codec, uint32_t index, OH_AVBuffer *data, void *userData)
175 {
176 (void)codec;
177 AEncSignalAv *signal = static_cast<AEncSignalAv *>(userData);
178 unique_lock<mutex> lock(signal->outMutex_);
179 signal->outQueue_.push(index);
180 signal->outBufferQueue_.push(data);
181 if (data) {
182 } else {
183 cout << "OnOutputBufferAvailable error, attr is nullptr!" << endl;
184 }
185 signal->outCond_.notify_all();
186 }
187
188 class AudioCodeCapiEncoderUnitTest : public testing::Test {
189 public:
190 static void SetUpTestCase(void);
191 static void TearDownTestCase(void);
192 void SetUp();
193 void TearDown();
194 int32_t ProceFunc(const std::string codecName = CODEC_FLAC_NAME);
195 int32_t ProceByMimeFunc(const std::string mime, bool isEncoder);
196 int32_t ProceByCapabilityFunc(const std::string mime, bool isEncoder);
197 int32_t CheckSoFunc();
198 void InputFunc();
199 void OutputFunc();
200 void InputFuncAv();
201 void OutputFuncAv();
202 void HeAACSampleRateTest(int32_t profile);
203 void ChannelLayoutTest(map<OH_AudioChannelLayout, int32_t> &supportedLayoutMap,
204 map<OH_AudioChannelLayout, int32_t> &unsupportedLayoutMap,
205 int32_t profile);
206 void ChannelCountTest(set<int32_t> &supportedChannelCntSet,
207 set<int32_t> &unsupportedChannelCntSet,
208 int32_t profile);
209 protected:
210 std::atomic<bool> isRunning_ = false;
211 std::unique_ptr<std::ifstream> inputFile_;
212 std::unique_ptr<std::ifstream> soFile_;
213 std::unique_ptr<std::thread> inputLoop_;
214 std::unique_ptr<std::thread> outputLoop_;
215 int32_t index_;
216 uint32_t frameBytes_ = FLAC_DEFAULT_FRAME_BYTES; // default for flac
217 std::string inputFilePath_ = FLAC_INPUT_FILE_PATH.data();
218 std::string outputFilePath_ = FLAC_OUTPUT_FILE_PATH.data();
219
220 struct OH_AVCodecAsyncCallback cb_;
221 struct OH_AVCodecCallback avcb_;
222 AEncSignal *signal_ = nullptr;
223 AEncSignalAv *signalAv_ = nullptr;
224 OH_AVCodec *audioEnc_;
225 OH_AVFormat *format;
226 bool isFirstFrame_ = true;
227 int64_t timeStamp_ = 0;
228 };
229
SetUpTestCase(void)230 void AudioCodeCapiEncoderUnitTest::SetUpTestCase(void)
231 {
232 cout << "[SetUpTestCase]: " << endl;
233 }
234
TearDownTestCase(void)235 void AudioCodeCapiEncoderUnitTest::TearDownTestCase(void)
236 {
237 cout << "[TearDownTestCase]: " << endl;
238 }
239
SetUp(void)240 void AudioCodeCapiEncoderUnitTest::SetUp(void)
241 {
242 cout << "[SetUp]: SetUp!!!" << endl;
243 }
244
TearDown(void)245 void AudioCodeCapiEncoderUnitTest::TearDown(void)
246 {
247 if (signal_ != nullptr) {
248 delete signal_;
249 signal_ = nullptr;
250 }
251 if (signalAv_ != nullptr) {
252 delete signalAv_;
253 signalAv_ = nullptr;
254 }
255 cout << "[TearDown]: over!!!" << endl;
256 }
257
InputFunc()258 void AudioCodeCapiEncoderUnitTest::InputFunc()
259 {
260 OH_AVCodecBufferAttr info = {};
261 bool isEos = false;
262 inputFile_ = std::make_unique<std::ifstream>(inputFilePath_, std::ios::binary);
263 if (!inputFile_->is_open()) {
264 std::cout << "open file failed, path: " << inputFilePath_ << std::endl;
265 return;
266 }
267 while (isRunning_.load()) {
268 unique_lock<mutex> lock(signal_->inMutex_);
269 signal_->inCond_.wait(lock, [this]() { return (signal_->inQueue_.size() > 0 || !isRunning_.load()); });
270 if (!isRunning_.load()) {
271 break;
272 }
273 uint32_t index = signal_->inQueue_.front();
274 auto buffer = signal_->inBufferQueue_.front();
275 isEos = !inputFile_->eof();
276 if (!isEos) {
277 inputFile_->read((char *)OH_AVMemory_GetAddr(buffer), frameBytes_);
278 }
279 info.size = frameBytes_;
280 info.flags = AVCODEC_BUFFER_FLAGS_NONE;
281 if (isEos) {
282 info.size = 0;
283 info.flags = AVCODEC_BUFFER_FLAGS_EOS;
284 } else if (isFirstFrame_) {
285 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
286 isFirstFrame_ = false;
287 }
288 info.offset = 0;
289 int32_t ret = OH_AudioEncoder_PushInputData(audioEnc_, index, info);
290 signal_->inQueue_.pop();
291 signal_->inBufferQueue_.pop();
292 if (ret != AVCS_ERR_OK) {
293 isRunning_ = false;
294 break;
295 }
296 if (isEos) {
297 break;
298 }
299 timeStamp_ += FRAME_DURATION_US;
300 }
301 inputFile_->close();
302 }
303
OutputFunc()304 void AudioCodeCapiEncoderUnitTest::OutputFunc()
305 {
306 std::ofstream outputFile;
307 outputFile.open(outputFilePath_, std::ios::out | std::ios::binary);
308 if (!outputFile.is_open()) {
309 std::cout << "open file failed, path: " << outputFilePath_ << std::endl;
310 return;
311 }
312
313 while (isRunning_.load()) {
314 unique_lock<mutex> lock(signal_->outMutex_);
315 signal_->outCond_.wait(lock, [this]() { return (signal_->outQueue_.size() > 0 || !isRunning_.load()); });
316
317 if (!isRunning_.load()) {
318 cout << "wait to stop, exit" << endl;
319 break;
320 }
321
322 uint32_t index = signal_->outQueue_.front();
323
324 OH_AVCodecBufferAttr attr = signal_->attrQueue_.front();
325 OH_AVMemory *data = signal_->outBufferQueue_.front();
326 if (data != nullptr) {
327 outputFile.write(reinterpret_cast<char *>(OH_AVMemory_GetAddr(data)), attr.size);
328 }
329 if (attr.flags == AVCODEC_BUFFER_FLAGS_EOS || attr.size == 0) {
330 cout << "encode eos" << endl;
331 isRunning_.store(false);
332 }
333
334 signal_->outBufferQueue_.pop();
335 signal_->attrQueue_.pop();
336 signal_->outQueue_.pop();
337 if (OH_AudioEncoder_FreeOutputData(audioEnc_, index) != AV_ERR_OK) {
338 cout << "Fatal: FreeOutputData fail" << endl;
339 break;
340 }
341 }
342 outputFile.close();
343 }
344
ProceFunc(const std::string codecName)345 int32_t AudioCodeCapiEncoderUnitTest::ProceFunc(const std::string codecName)
346 {
347 audioEnc_ = OH_AudioEncoder_CreateByName(codecName.c_str());
348 EXPECT_NE((OH_AVCodec *)nullptr, audioEnc_);
349
350 signal_ = new AEncSignal();
351 EXPECT_NE(nullptr, signal);
352
353 cb_ = {&OnError, &OnOutputFormatChanged, &OnInputBufferAvailable, &OnOutputBufferAvailable};
354 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_SetCallback(audioEnc_, cb_, signal_));
355
356 format = OH_AVFormat_Create();
357 return AVCS_ERR_OK;
358 }
359
ProceByMimeFunc(const std::string mime,bool isEncoder)360 int32_t AudioCodeCapiEncoderUnitTest::ProceByMimeFunc(const std::string mime, bool isEncoder)
361 {
362 audioEnc_ = OH_AudioCodec_CreateByMime(mime.c_str(), isEncoder);
363 EXPECT_NE((OH_AVCodec *)nullptr, audioEnc_);
364
365 signalAv_ = new AEncSignalAv();
366 EXPECT_NE(nullptr, signal);
367
368 avcb_ = {&OnError, &OnOutputFormatChanged, &OnInputBufferAvailableAv, &OnOutputBufferAvailableAv};
369 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_RegisterCallback(audioEnc_, avcb_, signalAv_));
370
371 format = OH_AVFormat_Create();
372 return AVCS_ERR_OK;
373 }
374
ProceByCapabilityFunc(const std::string mime,bool isEncoder)375 int32_t AudioCodeCapiEncoderUnitTest::ProceByCapabilityFunc(const std::string mime, bool isEncoder)
376 {
377 OH_AVCapability *cap = OH_AVCodec_GetCapability(mime.c_str(), isEncoder);
378 const char *name = OH_AVCapability_GetName(cap);
379 audioEnc_ = OH_AudioCodec_CreateByName(name);
380 EXPECT_NE((OH_AVCodec *)nullptr, audioEnc_);
381
382 signalAv_ = new AEncSignalAv();
383 EXPECT_NE(nullptr, signal);
384
385 avcb_ = {&OnError, &OnOutputFormatChanged, &OnInputBufferAvailableAv, &OnOutputBufferAvailableAv};
386 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_RegisterCallback(audioEnc_, avcb_, signalAv_));
387
388 format = OH_AVFormat_Create();
389 return AVCS_ERR_OK;
390 }
391
InputFuncAv()392 void AudioCodeCapiEncoderUnitTest::InputFuncAv()
393 {
394 OH_AVCodecBufferAttr info = {};
395 bool isEos = false;
396 inputFile_ = std::make_unique<std::ifstream>(inputFilePath_, std::ios::binary);
397 if (!inputFile_->is_open()) {
398 std::cout << "open file failed, path: " << inputFilePath_ << std::endl;
399 return;
400 }
401 while (isRunning_.load()) {
402 unique_lock<mutex> lock(signalAv_->inMutex_);
403 signalAv_->inCond_.wait(lock, [this]() { return (signalAv_->inQueue_.size() > 0 || !isRunning_.load()); });
404 if (!isRunning_.load()) {
405 break;
406 }
407 uint32_t index = signalAv_->inQueue_.front();
408 auto buffer = signalAv_->inBufferQueue_.front();
409 isEos = !inputFile_->eof();
410 if (!isEos) {
411 inputFile_->read((char *)OH_AVBuffer_GetAddr(buffer), frameBytes_);
412 }
413 info.size = frameBytes_;
414 info.flags = AVCODEC_BUFFER_FLAGS_NONE;
415 if (isEos) {
416 info.size = 0;
417 info.flags = AVCODEC_BUFFER_FLAGS_EOS;
418 } else if (isFirstFrame_) {
419 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
420 isFirstFrame_ = false;
421 }
422 info.offset = 0;
423 OH_AVBuffer_SetBufferAttr(buffer, &info);
424 int32_t ret = OH_AudioCodec_PushInputBuffer(audioEnc_, index);
425 signalAv_->inQueue_.pop();
426 signalAv_->inBufferQueue_.pop();
427 if (ret != AVCS_ERR_OK) {
428 isRunning_ = false;
429 break;
430 }
431 if (isEos) {
432 break;
433 }
434 timeStamp_ += FRAME_DURATION_US;
435 }
436 inputFile_->close();
437 }
438
OutputFuncAv()439 void AudioCodeCapiEncoderUnitTest::OutputFuncAv()
440 {
441 std::ofstream outputFile;
442 outputFile.open(outputFilePath_, std::ios::out | std::ios::binary);
443 if (!outputFile.is_open()) {
444 std::cout << "open file failed, path: " << outputFilePath_ << std::endl;
445 return;
446 }
447
448 while (isRunning_.load()) {
449 unique_lock<mutex> lock(signalAv_->outMutex_);
450 signalAv_->outCond_.wait(lock, [this]() { return (signalAv_->outQueue_.size() > 0 || !isRunning_.load()); });
451
452 if (!isRunning_.load()) {
453 cout << "wait to stop, exit" << endl;
454 break;
455 }
456
457 uint32_t index = signalAv_->outQueue_.front();
458 auto *data = signalAv_->outBufferQueue_.front();
459 OH_AVCodecBufferAttr attr;
460 OH_AVBuffer_GetBufferAttr(data, &attr);
461 if (data != nullptr) {
462 outputFile.write(reinterpret_cast<char *>(OH_AVBuffer_GetAddr(data)), attr.size);
463 }
464 if (data != nullptr && (attr.flags == AVCODEC_BUFFER_FLAGS_EOS || attr.size == 0)) {
465 cout << "encode eos" << endl;
466 isRunning_.store(false);
467 signalAv_->startCond_.notify_all();
468 }
469 signalAv_->outBufferQueue_.pop();
470 signalAv_->outQueue_.pop();
471 if (OH_AudioCodec_FreeOutputBuffer(audioEnc_, index) != AV_ERR_OK) {
472 cout << "Fatal: FreeOutputData fail" << endl;
473 break;
474 }
475 }
476 outputFile.close();
477 }
478
CheckSoFunc()479 int32_t AudioCodeCapiEncoderUnitTest::CheckSoFunc()
480 {
481 soFile_ = std::make_unique<std::ifstream>(OPUS_SO_FILE_PATH, std::ios::binary);
482 if (!soFile_->is_open()) {
483 cout << "Fatal: Open so file failed" << endl;
484 return false;
485 }
486 soFile_->close();
487 return true;
488 }
489
HeAACSampleRateTest(int32_t profile)490 void AudioCodeCapiEncoderUnitTest::HeAACSampleRateTest(int32_t profile)
491 {
492 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
493 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
494 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
495 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, profile);
496 set<uint32_t> supportedSampleRateSet = {
497 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000,
498 };
499 set<uint32_t> unsupportedSampleRateSet = {
500 0, 4000, 8000, 11025, 12000, 441000,
501 };
502 for (const uint32_t rate : supportedSampleRateSet) {
503 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, rate);
504 EXPECT_EQ(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
505 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
506 }
507 for (const uint32_t rate : unsupportedSampleRateSet) {
508 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, rate);
509 EXPECT_NE(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
510 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
511 }
512 }
513
ChannelCountTest(set<int32_t> & supportedChannelCntSet,set<int32_t> & unsupportedChannelCntSet,int32_t profile)514 void AudioCodeCapiEncoderUnitTest::ChannelCountTest(set<int32_t> &supportedChannelCntSet,
515 set<int32_t> &unsupportedChannelCntSet,
516 int32_t profile)
517 {
518 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
519 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
520 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
521 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
522 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, profile);
523 for (const int32_t cnt : supportedChannelCntSet) {
524 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, cnt);
525 EXPECT_EQ(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
526 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
527 }
528 for (const int32_t cnt : unsupportedChannelCntSet) {
529 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, cnt);
530 EXPECT_NE(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
531 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
532 }
533 }
534
ChannelLayoutTest(map<OH_AudioChannelLayout,int32_t> & supportedLayoutMap,map<OH_AudioChannelLayout,int32_t> & unsupportedLayoutMap,int32_t profile)535 void AudioCodeCapiEncoderUnitTest::ChannelLayoutTest(map<OH_AudioChannelLayout, int32_t> &supportedLayoutMap,
536 map<OH_AudioChannelLayout, int32_t> &unsupportedLayoutMap,
537 int32_t profile)
538 {
539 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
540 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
541 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
542 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
543 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, profile);
544 map<OH_AudioChannelLayout, int32_t>::iterator iter;
545 for (iter = supportedLayoutMap.begin(); iter != supportedLayoutMap.end(); iter++) {
546 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, iter->second);
547 OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, iter->first);
548 EXPECT_EQ(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
549 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
550 }
551 for (iter = unsupportedLayoutMap.begin(); iter != unsupportedLayoutMap.end(); iter++) {
552 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, iter->second);
553 OH_AVFormat_SetLongValue(format, OH_MD_KEY_CHANNEL_LAYOUT, iter->first);
554 EXPECT_NE(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
555 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
556 }
557 }
558
559 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusCreateByName_01, TestSize.Level1)
560 {
561 if (!CheckSoFunc()) {
562 return;
563 }
564 ProceFunc(CODEC_OPUS_NAME);
565 }
566
567 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusCreateByName_02, TestSize.Level1)
568 {
569 if (!CheckSoFunc()) {
570 return;
571 }
572 audioEnc_ = OH_AudioEncoder_CreateByName((CODEC_OPUS_NAME).data());
573 EXPECT_NE(nullptr, audioEnc_);
574 }
575
576 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusCreateByMime_01, TestSize.Level1)
577 {
578 if (!CheckSoFunc()) {
579 return;
580 }
581 audioEnc_ = OH_AudioEncoder_CreateByMime((AVCodecMimeType::MEDIA_MIMETYPE_AUDIO_OPUS).data());
582 EXPECT_NE(nullptr, audioEnc_);
583 }
584
585 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusGetOutputDescription_01, TestSize.Level1)
586 {
587 if (!CheckSoFunc()) {
588 return;
589 }
590 ProceFunc(CODEC_OPUS_NAME);
591 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
592 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
593 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
594 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
595 AudioSampleFormat::SAMPLE_S16LE);
596 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
597
598 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
599 EXPECT_NE(nullptr, OH_AudioEncoder_GetOutputDescription(audioEnc_));
600 }
601
602 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusIsValid_01, TestSize.Level1)
603 {
604 if (!CheckSoFunc()) {
605 return;
606 }
607 ProceFunc(CODEC_OPUS_NAME);
608 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
609 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
610 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
611 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
612 AudioSampleFormat::SAMPLE_S16LE);
613 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
614
615 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
616 bool value = true;
617 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_IsValid(audioEnc_, &value));
618 }
619
620
621 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusSetParameter_01, TestSize.Level1)
622 {
623 if (!CheckSoFunc()) {
624 return;
625 }
626 ProceFunc(CODEC_OPUS_NAME);
627 inputFilePath_ = OPUS_INPUT_FILE_PATH;
628 outputFilePath_ = OPUS_OUTPUT_FILE_PATH;
629 frameBytes_ = OPUS_FRAME_SAMPLE_SIZES;
630 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
631 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
632 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
633 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
634 AudioSampleFormat::SAMPLE_S16LE);
635 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
636
637 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
638 isRunning_.store(true);
639
640 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
641 EXPECT_NE(nullptr, inputLoop_);
642
643 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
644 EXPECT_NE(nullptr, outputLoop_);
645
646 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
647 while (isRunning_.load()) {
648 sleep(1); // sleep 1s
649 }
650
651 isRunning_.store(false);
652 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
653 {
654 unique_lock<mutex> lock(signal_->inMutex_);
655 signal_->inCond_.notify_all();
656 }
657 inputLoop_->join();
658 }
659 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
660 {
661 unique_lock<mutex> lock(signal_->outMutex_);
662 signal_->outCond_.notify_all();
663 }
664 outputLoop_->join();
665 }
666 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
667 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_SetParameter(audioEnc_, format));
668 }
669
670 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusSetParameter_02, TestSize.Level1)
671 {
672 if (!CheckSoFunc()) {
673 return;
674 }
675 ProceFunc(CODEC_OPUS_NAME);
676 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
677 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
678 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
679 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
680 AudioSampleFormat::SAMPLE_S16LE);
681 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
682
683 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
684 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_SetParameter(audioEnc_, format));
685 }
686
687 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusConfigure_01, TestSize.Level1)
688 {
689 if (!CheckSoFunc()) {
690 return;
691 }
692 ProceFunc(CODEC_OPUS_NAME);
693 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), 9999);
694 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
695 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
696 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
697 AudioSampleFormat::SAMPLE_S16LE);
698 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
699
700 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
701 }
702
703 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusConfigure_02, TestSize.Level1)
704 {
705 if (!CheckSoFunc()) {
706 return;
707 }
708 ProceFunc(CODEC_OPUS_NAME);
709 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
710 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), 12345);
711 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
712 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
713 AudioSampleFormat::SAMPLE_S16LE);
714 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
715
716 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
717 }
718
719 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusConfigure_03, TestSize.Level1)
720 {
721 if (!CheckSoFunc()) {
722 return;
723 }
724 ProceFunc(CODEC_OPUS_NAME);
725 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
726 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
727 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), 9999999);
728 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
729 AudioSampleFormat::SAMPLE_S16LE);
730 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
731
732 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
733 }
734
735 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusConfigure_04, TestSize.Level1)
736 {
737 if (!CheckSoFunc()) {
738 return;
739 }
740 ProceFunc(CODEC_OPUS_NAME);
741 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
742 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
743 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
744 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
745 AudioSampleFormat::SAMPLE_F32LE);
746 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
747
748 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
749 }
750
751 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusConfigure_05, TestSize.Level1)
752 {
753 if (!CheckSoFunc()) {
754 return;
755 }
756 ProceFunc(CODEC_OPUS_NAME);
757 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
758 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
759 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
760 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
761 AudioSampleFormat::SAMPLE_S16LE);
762 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), 120);
763
764 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
765 }
766
767
768 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_OpusConfigure_06, TestSize.Level1)
769 {
770 if (!CheckSoFunc()) {
771 return;
772 }
773 ProceFunc(CODEC_OPUS_NAME);
774 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
775 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
776 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
777 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
778 AudioSampleFormat::SAMPLE_S16LE);
779 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
780 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
781 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
782
783 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), 100);
784 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
785 }
786
787 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Opusnormalcase_01, TestSize.Level1)
788 {
789 if (!CheckSoFunc()) {
790 return;
791 }
792 ProceFunc(CODEC_OPUS_NAME);
793 inputFilePath_ = OPUS_INPUT_FILE_PATH;
794 outputFilePath_ = OPUS_OUTPUT_FILE_PATH;
795 frameBytes_ = OPUS_FRAME_SAMPLE_SIZES;
796 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
797 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
798 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
799 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
800 AudioSampleFormat::SAMPLE_S16LE);
801 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
802
803 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
804 isRunning_.store(true);
805
806 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
807 EXPECT_NE(nullptr, inputLoop_);
808
809 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
810 EXPECT_NE(nullptr, outputLoop_);
811
812 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
813 while (isRunning_.load()) {
814 sleep(1); // sleep 1s
815 }
816
817 isRunning_.store(false);
818 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
819 {
820 unique_lock<mutex> lock(signal_->inMutex_);
821 signal_->inCond_.notify_all();
822 }
823 inputLoop_->join();
824 }
825 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
826 {
827 unique_lock<mutex> lock(signal_->outMutex_);
828 signal_->outCond_.notify_all();
829 }
830 outputLoop_->join();
831 }
832 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
833 }
834
835 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Opusnormalcase_02, TestSize.Level1)
836 {
837 if (!CheckSoFunc()) {
838 return;
839 }
840 ProceFunc(CODEC_OPUS_NAME);
841 inputFilePath_ = OPUS_INPUT_FILE_PATH;
842 outputFilePath_ = OPUS_OUTPUT_FILE_PATH;
843 frameBytes_ = OPUS_FRAME_SAMPLE_SIZES;
844 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
845 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
846 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
847 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
848 AudioSampleFormat::SAMPLE_S16LE);
849 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
850
851 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
852 isRunning_.store(true);
853 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
854 EXPECT_NE(nullptr, inputLoop_);
855 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
856 EXPECT_NE(nullptr, outputLoop_);
857
858 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
859 while (isRunning_.load()) {
860 sleep(1); // sleep 1s
861 }
862
863 isRunning_.store(false);
864 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
865 {
866 unique_lock<mutex> lock(signal_->inMutex_);
867 signal_->inCond_.notify_all();
868 }
869 inputLoop_->join();
870 }
871
872 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
873 {
874 unique_lock<mutex> lock(signal_->outMutex_);
875 signal_->outCond_.notify_all();
876 }
877 outputLoop_->join();
878 }
879 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Stop(audioEnc_));
880 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
881 }
882
883 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Opusnormalcase_03, TestSize.Level1)
884 {
885 if (!CheckSoFunc()) {
886 return;
887 }
888 ProceFunc(CODEC_OPUS_NAME);
889 inputFilePath_ = OPUS_INPUT_FILE_PATH;
890 outputFilePath_ = OPUS_OUTPUT_FILE_PATH;
891 frameBytes_ = OPUS_FRAME_SAMPLE_SIZES;
892 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
893 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
894 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
895 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
896 AudioSampleFormat::SAMPLE_S16LE);
897 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
898
899 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
900 isRunning_.store(true);
901
902 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
903 EXPECT_NE(nullptr, inputLoop_);
904
905 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
906 EXPECT_NE(nullptr, outputLoop_);
907
908 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
909 while (isRunning_.load()) {
910 sleep(1); // sleep 1s
911 }
912
913 isRunning_.store(false);
914 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
915 {
916 unique_lock<mutex> lock(signal_->inMutex_);
917 signal_->inCond_.notify_all();
918 }
919 inputLoop_->join();
920 }
921
922 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
923 {
924 unique_lock<mutex> lock(signal_->outMutex_);
925 signal_->outCond_.notify_all();
926 }
927 outputLoop_->join();
928 }
929 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
930 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
931 }
932
933 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Opusnormalcase_04, TestSize.Level1)
934 {
935 if (!CheckSoFunc()) {
936 return;
937 }
938 ProceFunc(CODEC_OPUS_NAME);
939 inputFilePath_ = OPUS_INPUT_FILE_PATH;
940 outputFilePath_ = OPUS_OUTPUT_FILE_PATH;
941 frameBytes_ = OPUS_FRAME_SAMPLE_SIZES;
942 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
943 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
944 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
945 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
946 AudioSampleFormat::SAMPLE_S16LE);
947 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
948
949 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
950 isRunning_.store(true);
951
952 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
953 EXPECT_NE(nullptr, inputLoop_);
954
955 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
956 EXPECT_NE(nullptr, outputLoop_);
957
958 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
959 while (isRunning_.load()) {
960 sleep(1); // sleep 1s
961 }
962 isRunning_.store(false);
963 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
964 {
965 unique_lock<mutex> lock(signal_->inMutex_);
966 signal_->inCond_.notify_all();
967 }
968 inputLoop_->join();
969 }
970
971 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
972 {
973 unique_lock<mutex> lock(signal_->outMutex_);
974 signal_->outCond_.notify_all();
975 }
976 outputLoop_->join();
977 }
978 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
979 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
980 }
981
982 HWTEST_F(AudioCodeCapiEncoderUnitTest, opusCheckChannelCount, TestSize.Level1)
983 {
984 if (!CheckSoFunc()) {
985 return;
986 }
987 inputFilePath_ = OPUS_INPUT_FILE_PATH;
988 outputFilePath_ = OPUS_OUTPUT_FILE_PATH;
989 frameBytes_ = OPUS_FRAME_SAMPLE_SIZES;
990 ProceFunc(CODEC_OPUS_NAME);
991 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
992 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), OPUS_BITS_RATE);
993 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), OPUS_COMPLIANCE_LEVEL);
994 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
995 AudioSampleFormat::SAMPLE_S16LE);
996
997 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing channel count
998 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
999
1000 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), 9999);
1001 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal channel count
1002
1003 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1004 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
1005 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal channel count
1006
1007 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1008 }
1009
1010 HWTEST_F(AudioCodeCapiEncoderUnitTest, opusCheckSampleFormat, TestSize.Level1)
1011 {
1012 if (!CheckSoFunc()) {
1013 return;
1014 }
1015 inputFilePath_ = OPUS_INPUT_FILE_PATH;
1016 outputFilePath_ = OPUS_OUTPUT_FILE_PATH;
1017 frameBytes_ = OPUS_FRAME_SAMPLE_SIZES;
1018 ProceFunc(CODEC_OPUS_NAME);
1019
1020 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), OPUS_CHANNEL_COUNT);
1021 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), OPUS_SAMPLE_RATE);
1022
1023 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample format
1024
1025 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1026 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1027 AudioSampleFormat::SAMPLE_U8);
1028 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample format
1029
1030 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1031 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1032 AudioSampleFormat::SAMPLE_F32LE);
1033 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample format
1034
1035 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1036 }
1037
1038 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_CreateByName_01, TestSize.Level1)
1039 {
1040 audioEnc_ = OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_ENCODER_FLAC_NAME).data());
1041 EXPECT_NE(nullptr, audioEnc_);
1042 }
1043
1044 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_CreateByMime_01, TestSize.Level1)
1045 {
1046 audioEnc_ = OH_AudioEncoder_CreateByMime((AVCodecMimeType::MEDIA_MIMETYPE_AUDIO_FLAC).data());
1047 EXPECT_NE(nullptr, audioEnc_);
1048 }
1049
1050 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Prepare_01, TestSize.Level1)
1051 {
1052 ProceFunc();
1053 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Prepare(audioEnc_));
1054 }
1055
1056 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_GetOutputDescription_01, TestSize.Level1)
1057 {
1058 ProceFunc();
1059 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1060 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1061 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1062 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1063 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1064 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1065 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1066
1067 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1068 EXPECT_NE(nullptr, OH_AudioEncoder_GetOutputDescription(audioEnc_));
1069 }
1070
1071 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_IsValid_01, TestSize.Level1)
1072 {
1073 ProceFunc();
1074 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1075 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1076 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1077 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1078 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1079 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1080 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1081
1082 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1083 bool value = true;
1084 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_IsValid(audioEnc_, &value));
1085 }
1086
1087 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_SetParameter_01, TestSize.Level1)
1088 {
1089 ProceFunc();
1090 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1091 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1092 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1093 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1094 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1095 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1096 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1097
1098 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1099 isRunning_.store(true);
1100
1101 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1102 EXPECT_NE(nullptr, inputLoop_);
1103
1104 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1105 EXPECT_NE(nullptr, outputLoop_);
1106
1107 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1108 while (isRunning_.load()) {
1109 sleep(1); // sleep 1s
1110 }
1111
1112 isRunning_.store(false);
1113 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1114 {
1115 unique_lock<mutex> lock(signal_->inMutex_);
1116 signal_->inCond_.notify_all();
1117 }
1118 inputLoop_->join();
1119 }
1120 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1121 {
1122 unique_lock<mutex> lock(signal_->outMutex_);
1123 signal_->outCond_.notify_all();
1124 }
1125 outputLoop_->join();
1126 }
1127 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
1128 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_SetParameter(audioEnc_, format));
1129 }
1130
1131 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_SetParameter_02, TestSize.Level1)
1132 {
1133 ProceFunc();
1134 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1135 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1136 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1137 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1138 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1139 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1140 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1141
1142 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1143 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_SetParameter(audioEnc_, format));
1144 }
1145
1146 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_01, TestSize.Level1)
1147 {
1148 ProceFunc();
1149 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1150 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1151 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1152 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1153 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1154 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1155 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1156
1157 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1158 }
1159
1160 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_02, TestSize.Level1)
1161 {
1162 ProceFunc();
1163 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1164 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1165 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1166 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1167 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1168 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), ABNORMAL_CHANNEL_LAYOUT);
1169 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1170
1171 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1172 }
1173
1174 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_03, TestSize.Level1)
1175 {
1176 ProceFunc();
1177 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1178 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1179 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1180 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), ABNORMAL_BITS_SAMPLE);
1181 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1182 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1183 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1184
1185 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1186 }
1187
1188 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_04, TestSize.Level1)
1189 {
1190 ProceFunc();
1191 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), ABNORMAL_CHANNEL_COUNT);
1192 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1193 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1194 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1195 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1196 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1197 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1198
1199 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1200 }
1201
1202 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_05, TestSize.Level1)
1203 {
1204 ProceFunc();
1205 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1206 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1207 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), ABNORMAL_BITS_RATE);
1208 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1209 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1210 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1211 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1212
1213 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1214 }
1215
1216 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_07, TestSize.Level1)
1217 {
1218 ProceFunc();
1219 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1220 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1221 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1222 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1223 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1224 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1225 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), ABNORMAL_COMPLIANCE_LEVEL_L);
1226
1227 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1228 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1229
1230 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), ABNORMAL_COMPLIANCE_LEVEL_R);
1231 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1232 }
1233
1234 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_08, TestSize.Level1)
1235 {
1236 ProceFunc();
1237 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1238 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), ABNORMAL_SAMPLE_RATE);
1239 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1240 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1241 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1242 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1243 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1244
1245 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1246 }
1247
1248 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_01, TestSize.Level1)
1249 {
1250 ProceFunc();
1251 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1252 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1253 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1254 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1255 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1256 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1257 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1258
1259 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1260 isRunning_.store(true);
1261
1262 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1263 EXPECT_NE(nullptr, inputLoop_);
1264
1265 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1266 EXPECT_NE(nullptr, outputLoop_);
1267
1268 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1269 while (isRunning_.load()) {
1270 sleep(1); // sleep 1s
1271 }
1272
1273 isRunning_.store(false);
1274 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1275 {
1276 unique_lock<mutex> lock(signal_->inMutex_);
1277 signal_->inCond_.notify_all();
1278 }
1279 inputLoop_->join();
1280 }
1281 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1282 {
1283 unique_lock<mutex> lock(signal_->outMutex_);
1284 signal_->outCond_.notify_all();
1285 }
1286 outputLoop_->join();
1287 }
1288 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1289 }
1290
1291 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_02, TestSize.Level1)
1292 {
1293 ProceFunc();
1294 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1295 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1296 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1297 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1298 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1299 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1300 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1301
1302 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1303 isRunning_.store(true);
1304 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1305 EXPECT_NE(nullptr, inputLoop_);
1306 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1307 EXPECT_NE(nullptr, outputLoop_);
1308
1309 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1310 while (isRunning_.load()) {
1311 sleep(1); // sleep 1s
1312 }
1313
1314 isRunning_.store(false);
1315 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1316 {
1317 unique_lock<mutex> lock(signal_->inMutex_);
1318 signal_->inCond_.notify_all();
1319 }
1320 inputLoop_->join();
1321 }
1322
1323 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1324 {
1325 unique_lock<mutex> lock(signal_->outMutex_);
1326 signal_->outCond_.notify_all();
1327 }
1328 outputLoop_->join();
1329 }
1330 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Stop(audioEnc_));
1331 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1332 }
1333
1334 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_03, TestSize.Level1)
1335 {
1336 ProceFunc();
1337 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1338 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1339 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1340 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1341 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1342 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1343 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1344
1345 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1346 isRunning_.store(true);
1347
1348 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1349 EXPECT_NE(nullptr, inputLoop_);
1350
1351 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1352 EXPECT_NE(nullptr, outputLoop_);
1353
1354 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1355 while (isRunning_.load()) {
1356 sleep(1); // sleep 1s
1357 }
1358
1359 isRunning_.store(false);
1360 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1361 {
1362 unique_lock<mutex> lock(signal_->inMutex_);
1363 signal_->inCond_.notify_all();
1364 }
1365 inputLoop_->join();
1366 }
1367
1368 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1369 {
1370 unique_lock<mutex> lock(signal_->outMutex_);
1371 signal_->outCond_.notify_all();
1372 }
1373 outputLoop_->join();
1374 }
1375 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
1376 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1377 }
1378
1379 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_04, TestSize.Level1)
1380 {
1381 ProceFunc();
1382 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1383 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1384 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1385 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1386 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1387 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1388 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1389
1390 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1391 isRunning_.store(true);
1392
1393 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1394 EXPECT_NE(nullptr, inputLoop_);
1395
1396 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1397 EXPECT_NE(nullptr, outputLoop_);
1398
1399 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1400 while (isRunning_.load()) {
1401 sleep(1); // sleep 1s
1402 }
1403 isRunning_.store(false);
1404 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1405 {
1406 unique_lock<mutex> lock(signal_->inMutex_);
1407 signal_->inCond_.notify_all();
1408 }
1409 inputLoop_->join();
1410 }
1411
1412 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1413 {
1414 unique_lock<mutex> lock(signal_->outMutex_);
1415 signal_->outCond_.notify_all();
1416 }
1417 outputLoop_->join();
1418 }
1419 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1420 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1421 }
1422
1423 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_05, TestSize.Level1)
1424 {
1425 ProceFunc();
1426 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1427 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1428 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1429 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1430 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1431 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1432 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1433
1434 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1435 isRunning_.store(true);
1436
1437 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1438 EXPECT_NE(nullptr, inputLoop_);
1439 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1440 EXPECT_NE(nullptr, outputLoop_);
1441
1442 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1443 while (isRunning_.load()) {
1444 sleep(1); // sleep 1s
1445 }
1446
1447 isRunning_.store(false);
1448 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1449 {
1450 unique_lock<mutex> lock(signal_->inMutex_);
1451 signal_->inCond_.notify_all();
1452 }
1453 inputLoop_->join();
1454 }
1455
1456 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1457 {
1458 unique_lock<mutex> lock(signal_->outMutex_);
1459 signal_->outCond_.notify_all();
1460 }
1461 outputLoop_->join();
1462 }
1463 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
1464 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1465 }
1466
1467 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckChannelCount, TestSize.Level1)
1468 {
1469 inputFilePath_ = AAC_INPUT_FILE_PATH;
1470 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1471 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1472 ProceFunc(CODEC_AAC_NAME);
1473 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1474 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1475 AudioSampleFormat::SAMPLE_F32LE);
1476 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing channel count
1477
1478 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1479 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), ILLEGAL_CHANNEL_COUNT);
1480 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal channel count
1481
1482 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1483 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1484 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal channel count
1485
1486 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1487 }
1488
1489 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckSampleFormat, TestSize.Level1)
1490 {
1491 inputFilePath_ = AAC_INPUT_FILE_PATH;
1492 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1493 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1494 ProceFunc(CODEC_AAC_NAME);
1495 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1496 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1497 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample format
1498
1499 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1500 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1501 AudioSampleFormat::SAMPLE_U8);
1502 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample format
1503
1504 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1505 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1506 AudioSampleFormat::SAMPLE_F32LE);
1507 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample format
1508
1509 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1510 }
1511
1512 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckSampleRate, TestSize.Level1)
1513 {
1514 inputFilePath_ = AAC_INPUT_FILE_PATH;
1515 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1516 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1517 ProceFunc(CODEC_AAC_NAME);
1518 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1519 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1520 AudioSampleFormat::SAMPLE_F32LE);
1521 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample rate
1522
1523 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1524 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), ILLEGAL_SAMPLE_RATE);
1525 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample rate
1526
1527 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1528 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1529 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample rate
1530
1531 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1532 }
1533
1534 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckChannelLayout, TestSize.Level1)
1535 {
1536 inputFilePath_ = AAC_INPUT_FILE_PATH;
1537 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1538 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1539 ProceFunc(CODEC_AAC_NAME);
1540 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), 2);
1541 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1542 AudioSampleFormat::SAMPLE_F32LE);
1543 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1544 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CH_7POINT1);
1545 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // channel mismatch channel_layout
1546
1547 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1548 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1549 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal channel layout
1550
1551 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1552 }
1553
1554 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckMaxinputSize, TestSize.Level1)
1555 {
1556 inputFilePath_ = AAC_INPUT_FILE_PATH;
1557 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1558 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1559 ProceFunc(CODEC_AAC_NAME);
1560 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), 2);
1561 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1562 AudioSampleFormat::SAMPLE_F32LE);
1563 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1564 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_MAX_INPUT_SIZE.data(), MAX_INPUT_SIZE);
1565 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1566
1567 auto fmt = OH_AudioEncoder_GetOutputDescription(audioEnc_);
1568 EXPECT_NE(fmt, nullptr);
1569 OH_AVFormat_Destroy(fmt);
1570
1571 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1572
1573 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1574 }
1575
1576 HWTEST_F(AudioCodeCapiEncoderUnitTest, invalidEncoderNames, TestSize.Level1)
1577 {
1578 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_MP3_NAME).data()), nullptr);
1579 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_AAC_NAME).data()), nullptr);
1580 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_API9_AAC_NAME).data()), nullptr);
1581 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_VORBIS_NAME).data()), nullptr);
1582 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_FLAC_NAME).data()), nullptr);
1583 }
1584
1585 HWTEST_F(AudioCodeCapiEncoderUnitTest, g711muCheckChannelCount, TestSize.Level1)
1586 {
1587 inputFilePath_ = G711MU_INPUT_FILE_PATH;
1588 outputFilePath_ = G711MU_OUTPUT_FILE_PATH;
1589 frameBytes_ = G711MU_DEFAULT_FRAME_BYTES;
1590 ProceFunc(CODEC_G711MU_NAME);
1591 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), G711MU_SAMPLE_RATE);
1592 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1593 AudioSampleFormat::SAMPLE_S16LE);
1594 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing channel count
1595
1596 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1597 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), ILLEGAL_CHANNEL_COUNT);
1598 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal channel count
1599
1600 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1601 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), G711MU_CHANNEL_COUNT);
1602 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal channel count
1603
1604 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1605 }
1606
1607 HWTEST_F(AudioCodeCapiEncoderUnitTest, g711muCheckSampleFormat, TestSize.Level1)
1608 {
1609 inputFilePath_ = G711MU_INPUT_FILE_PATH;
1610 outputFilePath_ = G711MU_OUTPUT_FILE_PATH;
1611 frameBytes_ = G711MU_DEFAULT_FRAME_BYTES;
1612 ProceFunc(CODEC_G711MU_NAME);
1613 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), G711MU_CHANNEL_COUNT);
1614 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), G711MU_SAMPLE_RATE);
1615
1616 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample format
1617
1618 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1619 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1620 AudioSampleFormat::SAMPLE_U8);
1621 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample format
1622
1623 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1624 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1625 AudioSampleFormat::SAMPLE_S16LE);
1626 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample format
1627
1628 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1629 }
1630
1631 HWTEST_F(AudioCodeCapiEncoderUnitTest, g711muCheckSampleRate, TestSize.Level1)
1632 {
1633 inputFilePath_ = G711MU_INPUT_FILE_PATH;
1634 outputFilePath_ = G711MU_OUTPUT_FILE_PATH;
1635 frameBytes_ = G711MU_DEFAULT_FRAME_BYTES;
1636 ProceFunc(CODEC_G711MU_NAME);
1637 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), G711MU_CHANNEL_COUNT);
1638 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1639 AudioSampleFormat::SAMPLE_S16LE);
1640 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample rate
1641
1642 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1643 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), ILLEGAL_SAMPLE_RATE);
1644 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample rate
1645
1646 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1647 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), G711MU_SAMPLE_RATE);
1648 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample rate
1649
1650 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1651 }
1652
1653 HWTEST_F(AudioCodeCapiEncoderUnitTest, g711muNormal, TestSize.Level1)
1654 {
1655 inputFilePath_ = G711MU_INPUT_FILE_PATH;
1656 outputFilePath_ = G711MU_OUTPUT_FILE_PATH;
1657 frameBytes_ = G711MU_DEFAULT_FRAME_BYTES;
1658 ProceFunc(CODEC_G711MU_NAME);
1659 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), G711MU_CHANNEL_COUNT);
1660 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1661 AudioSampleFormat::SAMPLE_S16LE);
1662 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), G711MU_SAMPLE_RATE);
1663 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1664 isRunning_.store(true);
1665
1666 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1667 EXPECT_NE(nullptr, inputLoop_);
1668 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1669 EXPECT_NE(nullptr, outputLoop_);
1670
1671 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1672 while (isRunning_.load()) {
1673 sleep(1); // sleep 1s
1674 }
1675
1676 isRunning_.store(false);
1677 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1678 {
1679 unique_lock<mutex> lock(signal_->inMutex_);
1680 signal_->inCond_.notify_all();
1681 }
1682 inputLoop_->join();
1683 }
1684
1685 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1686 {
1687 unique_lock<mutex> lock(signal_->outMutex_);
1688 signal_->outCond_.notify_all();
1689 }
1690 outputLoop_->join();
1691 }
1692 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
1693 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1694 }
1695
1696 HWTEST_F(AudioCodeCapiEncoderUnitTest, EncoderConfigureLCAAC, TestSize.Level1)
1697 {
1698 inputFilePath_ = AAC_INPUT_FILE_PATH;
1699 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1700 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1701 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1702 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
1703 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
1704 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
1705 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
1706 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1707 isRunning_.store(true);
1708
1709 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFuncAv, this);
1710 EXPECT_NE(nullptr, inputLoop_);
1711 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFuncAv, this);
1712 EXPECT_NE(nullptr, outputLoop_);
1713
1714 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Start(audioEnc_));
1715 while (isRunning_.load()) {
1716 sleep(1); // sleep 1s
1717 }
1718
1719 isRunning_.store(false);
1720 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1721 {
1722 unique_lock<mutex> lock(signalAv_->inMutex_);
1723 signalAv_->inCond_.notify_all();
1724 }
1725 inputLoop_->join();
1726 }
1727
1728 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1729 {
1730 unique_lock<mutex> lock(signalAv_->outMutex_);
1731 signalAv_->outCond_.notify_all();
1732 }
1733 outputLoop_->join();
1734 }
1735 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Flush(audioEnc_));
1736 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1737 }
1738
1739 HWTEST_F(AudioCodeCapiEncoderUnitTest, EncoderConfigureHEAAC, TestSize.Level1)
1740 {
1741 inputFilePath_ = AAC_INPUT_FILE_PATH;
1742 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1743 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1744 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1745 bool vendorExist = (tmpCodec != nullptr);
1746 if (vendorExist) {
1747 OH_AudioCodec_Destroy(tmpCodec);
1748 tmpCodec = nullptr;
1749 }
1750
1751 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1752 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
1753 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
1754 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
1755 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
1756 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, AAC_PROFILE_HE);
1757 if (vendorExist) {
1758 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1759 } else {
1760 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1761 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1762 return;
1763 }
1764
1765 isRunning_.store(true);
1766
1767 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFuncAv, this);
1768 EXPECT_NE(nullptr, inputLoop_);
1769 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFuncAv, this);
1770 EXPECT_NE(nullptr, outputLoop_);
1771
1772 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Start(audioEnc_));
1773 while (isRunning_.load()) {
1774 sleep(1); // sleep 1s
1775 }
1776
1777 isRunning_.store(false);
1778 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1779 {
1780 unique_lock<mutex> lock(signalAv_->inMutex_);
1781 signalAv_->inCond_.notify_all();
1782 }
1783 inputLoop_->join();
1784 }
1785
1786 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1787 {
1788 unique_lock<mutex> lock(signalAv_->outMutex_);
1789 signalAv_->outCond_.notify_all();
1790 }
1791 outputLoop_->join();
1792 }
1793 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Flush(audioEnc_));
1794 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1795 }
1796
1797 HWTEST_F(AudioCodeCapiEncoderUnitTest, EncoderConfigureHEAACv2, TestSize.Level1)
1798 {
1799 inputFilePath_ = AAC_INPUT_FILE_PATH;
1800 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1801 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1802 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1803 bool vendorExist = (tmpCodec != nullptr);
1804 if (vendorExist) {
1805 OH_AudioCodec_Destroy(tmpCodec);
1806 tmpCodec = nullptr;
1807 }
1808 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1809 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
1810 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
1811 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
1812 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
1813 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, AAC_PROFILE_HE_V2);
1814 if (vendorExist) {
1815 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1816 } else {
1817 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1818 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1819 return;
1820 }
1821 isRunning_.store(true);
1822
1823 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFuncAv, this);
1824 EXPECT_NE(nullptr, inputLoop_);
1825 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFuncAv, this);
1826 EXPECT_NE(nullptr, outputLoop_);
1827 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Start(audioEnc_));
1828 while (isRunning_.load()) {
1829 sleep(1); // sleep 1s
1830 }
1831
1832 isRunning_.store(false);
1833 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1834 {
1835 unique_lock<mutex> lock(signalAv_->inMutex_);
1836 signalAv_->inCond_.notify_all();
1837 }
1838 inputLoop_->join();
1839 }
1840
1841 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1842 {
1843 unique_lock<mutex> lock(signalAv_->outMutex_);
1844 signalAv_->outCond_.notify_all();
1845 }
1846 outputLoop_->join();
1847 }
1848 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Flush(audioEnc_));
1849 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1850 }
1851
1852 HWTEST_F(AudioCodeCapiEncoderUnitTest, EncoderConfigureByCap, TestSize.Level1)
1853 {
1854 inputFilePath_ = AAC_INPUT_FILE_PATH;
1855 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1856 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1857 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1858 bool vendorExist = (tmpCodec != nullptr);
1859 ProceByCapabilityFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1860 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
1861 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
1862 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
1863 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
1864 if (vendorExist) {
1865 OH_AudioCodec_Destroy(tmpCodec);
1866 tmpCodec = nullptr;
1867 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, AAC_PROFILE_HE);
1868 }
1869 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1870 isRunning_.store(true);
1871
1872 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFuncAv, this);
1873 EXPECT_NE(nullptr, inputLoop_);
1874 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFuncAv, this);
1875 EXPECT_NE(nullptr, outputLoop_);
1876 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Start(audioEnc_));
1877 while (isRunning_.load()) {
1878 sleep(1); // sleep 1s
1879 }
1880
1881 isRunning_.store(false);
1882 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1883 {
1884 unique_lock<mutex> lock(signalAv_->inMutex_);
1885 signalAv_->inCond_.notify_all();
1886 }
1887 inputLoop_->join();
1888 }
1889
1890 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1891 {
1892 unique_lock<mutex> lock(signalAv_->outMutex_);
1893 signalAv_->outCond_.notify_all();
1894 }
1895 outputLoop_->join();
1896 }
1897 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Flush(audioEnc_));
1898 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1899 }
1900
1901 HWTEST_F(AudioCodeCapiEncoderUnitTest, sample_rate, TestSize.Level1)
1902 {
1903 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1904 if (tmpCodec == nullptr) {
1905 return;
1906 }
1907 OH_AudioCodec_Destroy(tmpCodec);
1908 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1909 HeAACSampleRateTest(AAC_PROFILE_HE);
1910 HeAACSampleRateTest(AAC_PROFILE_HE_V2);
1911 }
1912
1913 HWTEST_F(AudioCodeCapiEncoderUnitTest, channel_count_v1, TestSize.Level1)
1914 {
1915 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1916 if (tmpCodec == nullptr) {
1917 return;
1918 }
1919 OH_AudioCodec_Destroy(tmpCodec);
1920 set<int32_t> supportedChannelCntSet = {1, 2, 3, 4, 5, 6, 8};
1921 set<int32_t> unsupportedChannelCntSet = {0, 7, 9};
1922 ChannelCountTest(supportedChannelCntSet, unsupportedChannelCntSet, AAC_PROFILE_HE);
1923 }
1924
1925 HWTEST_F(AudioCodeCapiEncoderUnitTest, channel_count_v2, TestSize.Level1)
1926 {
1927 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1928 if (tmpCodec == nullptr) {
1929 return;
1930 }
1931 OH_AudioCodec_Destroy(tmpCodec);
1932 set<int32_t> supportedChannelCntSet = {2};
1933 set<int32_t> unsupportedChannelCntSet = {0, 1, 3, 4, 5, 6, 7, 8, 9};
1934 ChannelCountTest(supportedChannelCntSet, unsupportedChannelCntSet, AAC_PROFILE_HE_V2);
1935 }
1936
1937 HWTEST_F(AudioCodeCapiEncoderUnitTest, channel_layout_v1, TestSize.Level1)
1938 {
1939 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1940 if (tmpCodec == nullptr) {
1941 return;
1942 }
1943 OH_AudioCodec_Destroy(tmpCodec);
1944 map<OH_AudioChannelLayout, int32_t> supportedLayoutMap = {
1945 {CH_LAYOUT_MONO, 1},
1946 {CH_LAYOUT_STEREO, 2},
1947 {CH_LAYOUT_SURROUND, 3},
1948 {CH_LAYOUT_4POINT0, 4},
1949 {CH_LAYOUT_5POINT0_BACK, 5},
1950 {CH_LAYOUT_5POINT1_BACK, 6},
1951 {CH_LAYOUT_7POINT1_WIDE_BACK, 8},
1952 {CH_LAYOUT_7POINT1, 8},
1953 };
1954 map<OH_AudioChannelLayout, int32_t> unsupportedLayoutMap = {
1955 {CH_LAYOUT_MONO, 2},
1956 {CH_LAYOUT_STEREO, 3},
1957 {CH_LAYOUT_SURROUND, 4},
1958 {CH_LAYOUT_4POINT0, 5},
1959 {CH_LAYOUT_5POINT0, 6},
1960 {CH_LAYOUT_5POINT1, 7},
1961 {CH_LAYOUT_7POINT1_WIDE, 3},
1962 {CH_LAYOUT_7POINT1, 3},
1963 {CH_LAYOUT_7POINT1_WIDE_BACK, 5},
1964 // below unsupport layout
1965 {CH_LAYOUT_2POINT1, 3},
1966 {CH_LAYOUT_4POINT1, 5},
1967 {CH_LAYOUT_QUAD_SIDE, 4},
1968 {CH_LAYOUT_QUAD, 4},
1969 {CH_LAYOUT_6POINT0, 6},
1970 };
1971 ChannelLayoutTest(supportedLayoutMap, unsupportedLayoutMap, AAC_PROFILE_HE);
1972 }
1973
1974 HWTEST_F(AudioCodeCapiEncoderUnitTest, channel_layout_v2, TestSize.Level1)
1975 {
1976 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1977 if (tmpCodec == nullptr) {
1978 return;
1979 }
1980 OH_AudioCodec_Destroy(tmpCodec);
1981 map<OH_AudioChannelLayout, int32_t> supportedLayoutMap = {
1982 {CH_LAYOUT_STEREO, 2},
1983 };
1984 map<OH_AudioChannelLayout, int32_t> unsupportedLayoutMap = {
1985 {CH_LAYOUT_MONO, 1},
1986 {CH_LAYOUT_MONO, 2},
1987 {CH_LAYOUT_SURROUND, 3},
1988 {CH_LAYOUT_4POINT0, 4},
1989 {CH_LAYOUT_5POINT0, 5},
1990 {CH_LAYOUT_5POINT1, 6},
1991 {CH_LAYOUT_7POINT1_WIDE, 8},
1992 {CH_LAYOUT_7POINT1, 8},
1993 {CH_LAYOUT_7POINT1_WIDE_BACK, 8},
1994 {CH_LAYOUT_STEREO, 3},
1995 {CH_LAYOUT_SURROUND, 4},
1996 {CH_LAYOUT_4POINT0, 5},
1997 {CH_LAYOUT_5POINT0, 6},
1998 {CH_LAYOUT_5POINT1, 7},
1999 {CH_LAYOUT_7POINT1_WIDE, 3},
2000 {CH_LAYOUT_7POINT1, 3},
2001 {CH_LAYOUT_7POINT1_WIDE_BACK, 5},
2002 // below unsupport layout
2003 {CH_LAYOUT_2POINT1, 3},
2004 {CH_LAYOUT_4POINT1, 5},
2005 {CH_LAYOUT_QUAD_SIDE, 4},
2006 {CH_LAYOUT_QUAD, 4},
2007 {CH_LAYOUT_6POINT0, 6},
2008 };
2009 ChannelLayoutTest(supportedLayoutMap, unsupportedLayoutMap, AAC_PROFILE_HE_V2);
2010 }
2011
2012 HWTEST_F(AudioCodeCapiEncoderUnitTest, aac_profile, TestSize.Level1)
2013 {
2014 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
2015 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
2016 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
2017 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
2018 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
2019 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
2020 set<int32_t> supportedAacProfile = {AAC_PROFILE_LC};
2021 if (tmpCodec != nullptr) {
2022 OH_AudioCodec_Destroy(tmpCodec);
2023 supportedAacProfile = {AAC_PROFILE_LC, AAC_PROFILE_HE, AAC_PROFILE_HE_V2};
2024 }
2025 for (const int32_t profile : supportedAacProfile) {
2026 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, profile);
2027 EXPECT_EQ(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
2028 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
2029 }
2030 set<int32_t> unsupportedAacProfile = {1, 2, 5};
2031 for (const int32_t profile : unsupportedAacProfile) {
2032 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, profile);
2033 EXPECT_NE(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
2034 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
2035 }
2036 }
2037
2038 } // namespace MediaAVCodec
2039 } // namespace OHOS
2040