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_SetParameter_03, TestSize.Level1)
1147 {
1148 inputFilePath_ = AAC_INPUT_FILE_PATH;
1149 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1150 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1151 ProceFunc(CODEC_AAC_NAME);
1152 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1153 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1154 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1155 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1156
1157 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1158 isRunning_.store(true);
1159
1160 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1161 EXPECT_NE(nullptr, inputLoop_);
1162 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1163 EXPECT_NE(nullptr, outputLoop_);
1164 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1165 while (isRunning_.load()) {
1166 sleep(1);
1167 }
1168
1169 isRunning_.store(false);
1170 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1171 {
1172 unique_lock<mutex> lock(signal_->inMutex_);
1173 signal_->inCond_.notify_all();
1174 }
1175 inputLoop_->join();
1176 }
1177 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1178 {
1179 unique_lock<mutex> lock(signal_->outMutex_);
1180 signal_->outCond_.notify_all();
1181 }
1182 outputLoop_->join();
1183 }
1184 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
1185 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1186 audioEnc_ = nullptr;
1187 }
1188
1189 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_01, TestSize.Level1)
1190 {
1191 ProceFunc();
1192 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1193 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1194 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1195 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1196 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1197 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1198 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1199
1200 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1201 }
1202
1203 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_02, TestSize.Level1)
1204 {
1205 ProceFunc();
1206 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1207 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1208 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1209 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1210 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1211 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), ABNORMAL_CHANNEL_LAYOUT);
1212 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1213
1214 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1215 }
1216
1217 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_03, TestSize.Level1)
1218 {
1219 ProceFunc();
1220 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1221 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1222 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1223 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), ABNORMAL_BITS_SAMPLE);
1224 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1225 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1226 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1227
1228 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1229 }
1230
1231 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_04, TestSize.Level1)
1232 {
1233 ProceFunc();
1234 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), ABNORMAL_CHANNEL_COUNT);
1235 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1236 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1237 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1238 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1239 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1240 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1241
1242 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1243 }
1244
1245 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_05, TestSize.Level1)
1246 {
1247 ProceFunc();
1248 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1249 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1250 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), ABNORMAL_BITS_RATE);
1251 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1252 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1253 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1254 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1255
1256 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1257 }
1258
1259 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_07, TestSize.Level1)
1260 {
1261 ProceFunc();
1262 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1263 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1264 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1265 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1266 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1267 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1268 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), ABNORMAL_COMPLIANCE_LEVEL_L);
1269
1270 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1271 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1272
1273 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), ABNORMAL_COMPLIANCE_LEVEL_R);
1274 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1275 }
1276
1277 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_08, TestSize.Level1)
1278 {
1279 ProceFunc();
1280 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1281 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), ABNORMAL_SAMPLE_RATE);
1282 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1283 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1284 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1285 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1286 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1287
1288 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1289 }
1290
1291 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_01, 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
1305 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1306 EXPECT_NE(nullptr, inputLoop_);
1307
1308 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1309 EXPECT_NE(nullptr, outputLoop_);
1310
1311 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1312 while (isRunning_.load()) {
1313 sleep(1); // sleep 1s
1314 }
1315
1316 isRunning_.store(false);
1317 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1318 {
1319 unique_lock<mutex> lock(signal_->inMutex_);
1320 signal_->inCond_.notify_all();
1321 }
1322 inputLoop_->join();
1323 }
1324 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1325 {
1326 unique_lock<mutex> lock(signal_->outMutex_);
1327 signal_->outCond_.notify_all();
1328 }
1329 outputLoop_->join();
1330 }
1331 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1332 }
1333
1334 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_02, 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 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1348 EXPECT_NE(nullptr, inputLoop_);
1349 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1350 EXPECT_NE(nullptr, outputLoop_);
1351
1352 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1353 while (isRunning_.load()) {
1354 sleep(1); // sleep 1s
1355 }
1356
1357 isRunning_.store(false);
1358 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1359 {
1360 unique_lock<mutex> lock(signal_->inMutex_);
1361 signal_->inCond_.notify_all();
1362 }
1363 inputLoop_->join();
1364 }
1365
1366 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1367 {
1368 unique_lock<mutex> lock(signal_->outMutex_);
1369 signal_->outCond_.notify_all();
1370 }
1371 outputLoop_->join();
1372 }
1373 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Stop(audioEnc_));
1374 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1375 }
1376
1377 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_03, TestSize.Level1)
1378 {
1379 ProceFunc();
1380 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1381 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1382 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1383 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1384 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1385 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1386 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1387
1388 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1389 isRunning_.store(true);
1390
1391 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1392 EXPECT_NE(nullptr, inputLoop_);
1393
1394 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1395 EXPECT_NE(nullptr, outputLoop_);
1396
1397 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1398 while (isRunning_.load()) {
1399 sleep(1); // sleep 1s
1400 }
1401
1402 isRunning_.store(false);
1403 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1404 {
1405 unique_lock<mutex> lock(signal_->inMutex_);
1406 signal_->inCond_.notify_all();
1407 }
1408 inputLoop_->join();
1409 }
1410
1411 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1412 {
1413 unique_lock<mutex> lock(signal_->outMutex_);
1414 signal_->outCond_.notify_all();
1415 }
1416 outputLoop_->join();
1417 }
1418 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
1419 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1420 }
1421
1422 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_04, TestSize.Level1)
1423 {
1424 ProceFunc();
1425 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1426 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1427 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1428 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1429 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1430 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1431 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1432
1433 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1434 isRunning_.store(true);
1435
1436 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1437 EXPECT_NE(nullptr, inputLoop_);
1438
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 isRunning_.store(false);
1447 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1448 {
1449 unique_lock<mutex> lock(signal_->inMutex_);
1450 signal_->inCond_.notify_all();
1451 }
1452 inputLoop_->join();
1453 }
1454
1455 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1456 {
1457 unique_lock<mutex> lock(signal_->outMutex_);
1458 signal_->outCond_.notify_all();
1459 }
1460 outputLoop_->join();
1461 }
1462 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1463 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1464 }
1465
1466 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_05, TestSize.Level1)
1467 {
1468 ProceFunc();
1469 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1470 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1471 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
1472 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
1473 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
1474 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1475 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
1476
1477 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1478 isRunning_.store(true);
1479
1480 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1481 EXPECT_NE(nullptr, inputLoop_);
1482 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1483 EXPECT_NE(nullptr, outputLoop_);
1484
1485 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1486 while (isRunning_.load()) {
1487 sleep(1); // sleep 1s
1488 }
1489
1490 isRunning_.store(false);
1491 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1492 {
1493 unique_lock<mutex> lock(signal_->inMutex_);
1494 signal_->inCond_.notify_all();
1495 }
1496 inputLoop_->join();
1497 }
1498
1499 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1500 {
1501 unique_lock<mutex> lock(signal_->outMutex_);
1502 signal_->outCond_.notify_all();
1503 }
1504 outputLoop_->join();
1505 }
1506 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
1507 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1508 }
1509
1510 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckChannelCount, TestSize.Level1)
1511 {
1512 inputFilePath_ = AAC_INPUT_FILE_PATH;
1513 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1514 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1515 ProceFunc(CODEC_AAC_NAME);
1516 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1517 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1518 AudioSampleFormat::SAMPLE_F32LE);
1519 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing channel count
1520
1521 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1522 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), ILLEGAL_CHANNEL_COUNT);
1523 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal channel count
1524
1525 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1526 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1527 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal channel count
1528
1529 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1530 }
1531
1532 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckSampleFormat, TestSize.Level1)
1533 {
1534 inputFilePath_ = AAC_INPUT_FILE_PATH;
1535 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1536 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1537 ProceFunc(CODEC_AAC_NAME);
1538 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1539 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1540 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample format
1541
1542 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1543 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1544 AudioSampleFormat::SAMPLE_U8);
1545 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample format
1546
1547 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1548 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1549 AudioSampleFormat::SAMPLE_F32LE);
1550 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample format
1551
1552 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1553 }
1554
1555 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckSampleRate, TestSize.Level1)
1556 {
1557 inputFilePath_ = AAC_INPUT_FILE_PATH;
1558 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1559 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1560 ProceFunc(CODEC_AAC_NAME);
1561 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
1562 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1563 AudioSampleFormat::SAMPLE_F32LE);
1564 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample rate
1565
1566 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1567 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), ILLEGAL_SAMPLE_RATE);
1568 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample rate
1569
1570 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1571 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1572 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample rate
1573
1574 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1575 }
1576
1577 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckChannelLayout, TestSize.Level1)
1578 {
1579 inputFilePath_ = AAC_INPUT_FILE_PATH;
1580 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1581 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1582 ProceFunc(CODEC_AAC_NAME);
1583 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), 2);
1584 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1585 AudioSampleFormat::SAMPLE_F32LE);
1586 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1587 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CH_7POINT1);
1588 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // channel mismatch channel_layout
1589
1590 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1591 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
1592 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal channel layout
1593
1594 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1595 }
1596
1597 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckMaxinputSize, TestSize.Level1)
1598 {
1599 inputFilePath_ = AAC_INPUT_FILE_PATH;
1600 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1601 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1602 ProceFunc(CODEC_AAC_NAME);
1603 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), 2);
1604 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1605 AudioSampleFormat::SAMPLE_F32LE);
1606 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
1607 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_MAX_INPUT_SIZE.data(), MAX_INPUT_SIZE);
1608 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1609
1610 auto fmt = OH_AudioEncoder_GetOutputDescription(audioEnc_);
1611 EXPECT_NE(fmt, nullptr);
1612 OH_AVFormat_Destroy(fmt);
1613
1614 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
1615
1616 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1617 }
1618
1619 HWTEST_F(AudioCodeCapiEncoderUnitTest, invalidEncoderNames, TestSize.Level1)
1620 {
1621 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_MP3_NAME).data()), nullptr);
1622 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_AAC_NAME).data()), nullptr);
1623 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_API9_AAC_NAME).data()), nullptr);
1624 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_VORBIS_NAME).data()), nullptr);
1625 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_FLAC_NAME).data()), nullptr);
1626 }
1627
1628 HWTEST_F(AudioCodeCapiEncoderUnitTest, g711muCheckChannelCount, TestSize.Level1)
1629 {
1630 inputFilePath_ = G711MU_INPUT_FILE_PATH;
1631 outputFilePath_ = G711MU_OUTPUT_FILE_PATH;
1632 frameBytes_ = G711MU_DEFAULT_FRAME_BYTES;
1633 ProceFunc(CODEC_G711MU_NAME);
1634 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), G711MU_SAMPLE_RATE);
1635 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1636 AudioSampleFormat::SAMPLE_S16LE);
1637 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing channel count
1638
1639 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1640 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), ILLEGAL_CHANNEL_COUNT);
1641 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal channel count
1642
1643 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1644 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), G711MU_CHANNEL_COUNT);
1645 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal channel count
1646
1647 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1648 }
1649
1650 HWTEST_F(AudioCodeCapiEncoderUnitTest, g711muCheckSampleFormat, TestSize.Level1)
1651 {
1652 inputFilePath_ = G711MU_INPUT_FILE_PATH;
1653 outputFilePath_ = G711MU_OUTPUT_FILE_PATH;
1654 frameBytes_ = G711MU_DEFAULT_FRAME_BYTES;
1655 ProceFunc(CODEC_G711MU_NAME);
1656 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), G711MU_CHANNEL_COUNT);
1657 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), G711MU_SAMPLE_RATE);
1658
1659 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample format
1660
1661 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1662 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1663 AudioSampleFormat::SAMPLE_U8);
1664 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample format
1665
1666 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1667 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1668 AudioSampleFormat::SAMPLE_S16LE);
1669 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample format
1670
1671 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1672 }
1673
1674 HWTEST_F(AudioCodeCapiEncoderUnitTest, g711muCheckSampleRate, TestSize.Level1)
1675 {
1676 inputFilePath_ = G711MU_INPUT_FILE_PATH;
1677 outputFilePath_ = G711MU_OUTPUT_FILE_PATH;
1678 frameBytes_ = G711MU_DEFAULT_FRAME_BYTES;
1679 ProceFunc(CODEC_G711MU_NAME);
1680 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), G711MU_CHANNEL_COUNT);
1681 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1682 AudioSampleFormat::SAMPLE_S16LE);
1683 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample rate
1684
1685 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1686 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), ILLEGAL_SAMPLE_RATE);
1687 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample rate
1688
1689 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
1690 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), G711MU_SAMPLE_RATE);
1691 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample rate
1692
1693 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1694 }
1695
1696 HWTEST_F(AudioCodeCapiEncoderUnitTest, g711muNormal, TestSize.Level1)
1697 {
1698 inputFilePath_ = G711MU_INPUT_FILE_PATH;
1699 outputFilePath_ = G711MU_OUTPUT_FILE_PATH;
1700 frameBytes_ = G711MU_DEFAULT_FRAME_BYTES;
1701 ProceFunc(CODEC_G711MU_NAME);
1702 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), G711MU_CHANNEL_COUNT);
1703 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
1704 AudioSampleFormat::SAMPLE_S16LE);
1705 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), G711MU_SAMPLE_RATE);
1706 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
1707 isRunning_.store(true);
1708
1709 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
1710 EXPECT_NE(nullptr, inputLoop_);
1711 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
1712 EXPECT_NE(nullptr, outputLoop_);
1713
1714 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_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(signal_->inMutex_);
1723 signal_->inCond_.notify_all();
1724 }
1725 inputLoop_->join();
1726 }
1727
1728 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1729 {
1730 unique_lock<mutex> lock(signal_->outMutex_);
1731 signal_->outCond_.notify_all();
1732 }
1733 outputLoop_->join();
1734 }
1735 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
1736 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
1737 }
1738
1739 HWTEST_F(AudioCodeCapiEncoderUnitTest, EncoderConfigureLCAAC, TestSize.Level1)
1740 {
1741 inputFilePath_ = AAC_INPUT_FILE_PATH;
1742 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1743 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1744 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1745 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
1746 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
1747 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
1748 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
1749 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1750 isRunning_.store(true);
1751
1752 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFuncAv, this);
1753 EXPECT_NE(nullptr, inputLoop_);
1754 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFuncAv, this);
1755 EXPECT_NE(nullptr, outputLoop_);
1756
1757 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Start(audioEnc_));
1758 while (isRunning_.load()) {
1759 sleep(1); // sleep 1s
1760 }
1761
1762 isRunning_.store(false);
1763 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1764 {
1765 unique_lock<mutex> lock(signalAv_->inMutex_);
1766 signalAv_->inCond_.notify_all();
1767 }
1768 inputLoop_->join();
1769 }
1770
1771 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1772 {
1773 unique_lock<mutex> lock(signalAv_->outMutex_);
1774 signalAv_->outCond_.notify_all();
1775 }
1776 outputLoop_->join();
1777 }
1778 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Flush(audioEnc_));
1779 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1780 }
1781
1782 HWTEST_F(AudioCodeCapiEncoderUnitTest, EncoderConfigureHEAAC, TestSize.Level1)
1783 {
1784 inputFilePath_ = AAC_INPUT_FILE_PATH;
1785 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1786 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1787 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1788 bool vendorExist = (tmpCodec != nullptr);
1789 if (vendorExist) {
1790 OH_AudioCodec_Destroy(tmpCodec);
1791 tmpCodec = nullptr;
1792 }
1793
1794 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1795 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
1796 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
1797 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
1798 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
1799 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, AAC_PROFILE_HE);
1800 if (vendorExist) {
1801 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1802 } else {
1803 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1804 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1805 return;
1806 }
1807
1808 isRunning_.store(true);
1809
1810 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFuncAv, this);
1811 EXPECT_NE(nullptr, inputLoop_);
1812 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFuncAv, this);
1813 EXPECT_NE(nullptr, outputLoop_);
1814
1815 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Start(audioEnc_));
1816 while (isRunning_.load()) {
1817 sleep(1); // sleep 1s
1818 }
1819
1820 isRunning_.store(false);
1821 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1822 {
1823 unique_lock<mutex> lock(signalAv_->inMutex_);
1824 signalAv_->inCond_.notify_all();
1825 }
1826 inputLoop_->join();
1827 }
1828
1829 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1830 {
1831 unique_lock<mutex> lock(signalAv_->outMutex_);
1832 signalAv_->outCond_.notify_all();
1833 }
1834 outputLoop_->join();
1835 }
1836 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Flush(audioEnc_));
1837 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1838 }
1839
1840 HWTEST_F(AudioCodeCapiEncoderUnitTest, EncoderConfigureHEAACv2, TestSize.Level1)
1841 {
1842 inputFilePath_ = AAC_INPUT_FILE_PATH;
1843 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1844 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1845 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1846 bool vendorExist = (tmpCodec != nullptr);
1847 if (vendorExist) {
1848 OH_AudioCodec_Destroy(tmpCodec);
1849 tmpCodec = nullptr;
1850 }
1851 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1852 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
1853 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
1854 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
1855 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
1856 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, AAC_PROFILE_HE_V2);
1857 if (vendorExist) {
1858 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1859 } else {
1860 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1861 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1862 return;
1863 }
1864 isRunning_.store(true);
1865
1866 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFuncAv, this);
1867 EXPECT_NE(nullptr, inputLoop_);
1868 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFuncAv, this);
1869 EXPECT_NE(nullptr, outputLoop_);
1870 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Start(audioEnc_));
1871 while (isRunning_.load()) {
1872 sleep(1); // sleep 1s
1873 }
1874
1875 isRunning_.store(false);
1876 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1877 {
1878 unique_lock<mutex> lock(signalAv_->inMutex_);
1879 signalAv_->inCond_.notify_all();
1880 }
1881 inputLoop_->join();
1882 }
1883
1884 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1885 {
1886 unique_lock<mutex> lock(signalAv_->outMutex_);
1887 signalAv_->outCond_.notify_all();
1888 }
1889 outputLoop_->join();
1890 }
1891 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Flush(audioEnc_));
1892 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1893 }
1894
1895 HWTEST_F(AudioCodeCapiEncoderUnitTest, EncoderConfigureByCap, TestSize.Level1)
1896 {
1897 inputFilePath_ = AAC_INPUT_FILE_PATH;
1898 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
1899 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
1900 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1901 bool vendorExist = (tmpCodec != nullptr);
1902 ProceByCapabilityFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1903 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
1904 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
1905 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
1906 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
1907 if (vendorExist) {
1908 OH_AudioCodec_Destroy(tmpCodec);
1909 tmpCodec = nullptr;
1910 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, AAC_PROFILE_HE);
1911 }
1912 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
1913 isRunning_.store(true);
1914
1915 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFuncAv, this);
1916 EXPECT_NE(nullptr, inputLoop_);
1917 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFuncAv, this);
1918 EXPECT_NE(nullptr, outputLoop_);
1919 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Start(audioEnc_));
1920 while (isRunning_.load()) {
1921 sleep(1); // sleep 1s
1922 }
1923
1924 isRunning_.store(false);
1925 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
1926 {
1927 unique_lock<mutex> lock(signalAv_->inMutex_);
1928 signalAv_->inCond_.notify_all();
1929 }
1930 inputLoop_->join();
1931 }
1932
1933 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
1934 {
1935 unique_lock<mutex> lock(signalAv_->outMutex_);
1936 signalAv_->outCond_.notify_all();
1937 }
1938 outputLoop_->join();
1939 }
1940 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Flush(audioEnc_));
1941 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioCodec_Destroy(audioEnc_));
1942 }
1943
1944 HWTEST_F(AudioCodeCapiEncoderUnitTest, sample_rate, TestSize.Level1)
1945 {
1946 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1947 if (tmpCodec == nullptr) {
1948 return;
1949 }
1950 OH_AudioCodec_Destroy(tmpCodec);
1951 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
1952 HeAACSampleRateTest(AAC_PROFILE_HE);
1953 HeAACSampleRateTest(AAC_PROFILE_HE_V2);
1954 }
1955
1956 HWTEST_F(AudioCodeCapiEncoderUnitTest, channel_count_v1, TestSize.Level1)
1957 {
1958 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1959 if (tmpCodec == nullptr) {
1960 return;
1961 }
1962 OH_AudioCodec_Destroy(tmpCodec);
1963 set<int32_t> supportedChannelCntSet = {1, 2, 3, 4, 5, 6, 8};
1964 set<int32_t> unsupportedChannelCntSet = {0, 7, 9};
1965 ChannelCountTest(supportedChannelCntSet, unsupportedChannelCntSet, AAC_PROFILE_HE);
1966 }
1967
1968 HWTEST_F(AudioCodeCapiEncoderUnitTest, channel_count_v2, TestSize.Level1)
1969 {
1970 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1971 if (tmpCodec == nullptr) {
1972 return;
1973 }
1974 OH_AudioCodec_Destroy(tmpCodec);
1975 set<int32_t> supportedChannelCntSet = {2};
1976 set<int32_t> unsupportedChannelCntSet = {0, 1, 3, 4, 5, 6, 7, 8, 9};
1977 ChannelCountTest(supportedChannelCntSet, unsupportedChannelCntSet, AAC_PROFILE_HE_V2);
1978 }
1979
1980 HWTEST_F(AudioCodeCapiEncoderUnitTest, channel_layout_v1, TestSize.Level1)
1981 {
1982 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
1983 if (tmpCodec == nullptr) {
1984 return;
1985 }
1986 OH_AudioCodec_Destroy(tmpCodec);
1987 map<OH_AudioChannelLayout, int32_t> supportedLayoutMap = {
1988 {CH_LAYOUT_MONO, 1},
1989 {CH_LAYOUT_STEREO, 2},
1990 {CH_LAYOUT_SURROUND, 3},
1991 {CH_LAYOUT_4POINT0, 4},
1992 {CH_LAYOUT_5POINT0_BACK, 5},
1993 {CH_LAYOUT_5POINT1_BACK, 6},
1994 {CH_LAYOUT_7POINT1_WIDE_BACK, 8},
1995 {CH_LAYOUT_7POINT1, 8},
1996 };
1997 map<OH_AudioChannelLayout, int32_t> unsupportedLayoutMap = {
1998 {CH_LAYOUT_MONO, 2},
1999 {CH_LAYOUT_STEREO, 3},
2000 {CH_LAYOUT_SURROUND, 4},
2001 {CH_LAYOUT_4POINT0, 5},
2002 {CH_LAYOUT_5POINT0, 6},
2003 {CH_LAYOUT_5POINT1, 7},
2004 {CH_LAYOUT_7POINT1_WIDE, 3},
2005 {CH_LAYOUT_7POINT1, 3},
2006 {CH_LAYOUT_7POINT1_WIDE_BACK, 5},
2007 // below unsupport layout
2008 {CH_LAYOUT_2POINT1, 3},
2009 {CH_LAYOUT_4POINT1, 5},
2010 {CH_LAYOUT_QUAD_SIDE, 4},
2011 {CH_LAYOUT_QUAD, 4},
2012 {CH_LAYOUT_6POINT0, 6},
2013 };
2014 ChannelLayoutTest(supportedLayoutMap, unsupportedLayoutMap, AAC_PROFILE_HE);
2015 }
2016
2017 HWTEST_F(AudioCodeCapiEncoderUnitTest, channel_layout_v2, TestSize.Level1)
2018 {
2019 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
2020 if (tmpCodec == nullptr) {
2021 return;
2022 }
2023 OH_AudioCodec_Destroy(tmpCodec);
2024 map<OH_AudioChannelLayout, int32_t> supportedLayoutMap = {
2025 {CH_LAYOUT_STEREO, 2},
2026 };
2027 map<OH_AudioChannelLayout, int32_t> unsupportedLayoutMap = {
2028 {CH_LAYOUT_MONO, 1},
2029 {CH_LAYOUT_MONO, 2},
2030 {CH_LAYOUT_SURROUND, 3},
2031 {CH_LAYOUT_4POINT0, 4},
2032 {CH_LAYOUT_5POINT0, 5},
2033 {CH_LAYOUT_5POINT1, 6},
2034 {CH_LAYOUT_7POINT1_WIDE, 8},
2035 {CH_LAYOUT_7POINT1, 8},
2036 {CH_LAYOUT_7POINT1_WIDE_BACK, 8},
2037 {CH_LAYOUT_STEREO, 3},
2038 {CH_LAYOUT_SURROUND, 4},
2039 {CH_LAYOUT_4POINT0, 5},
2040 {CH_LAYOUT_5POINT0, 6},
2041 {CH_LAYOUT_5POINT1, 7},
2042 {CH_LAYOUT_7POINT1_WIDE, 3},
2043 {CH_LAYOUT_7POINT1, 3},
2044 {CH_LAYOUT_7POINT1_WIDE_BACK, 5},
2045 // below unsupport layout
2046 {CH_LAYOUT_2POINT1, 3},
2047 {CH_LAYOUT_4POINT1, 5},
2048 {CH_LAYOUT_QUAD_SIDE, 4},
2049 {CH_LAYOUT_QUAD, 4},
2050 {CH_LAYOUT_6POINT0, 6},
2051 };
2052 ChannelLayoutTest(supportedLayoutMap, unsupportedLayoutMap, AAC_PROFILE_HE_V2);
2053 }
2054
2055 HWTEST_F(AudioCodeCapiEncoderUnitTest, aac_profile, TestSize.Level1)
2056 {
2057 OH_AVCodec *tmpCodec = OH_AudioCodec_CreateByName("OH.Media.Codec.Encoder.Audio.Vendor.AAC");
2058 ProceByMimeFunc(OH_AVCODEC_MIMETYPE_AUDIO_AAC, true);
2059 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUDIO_SAMPLE_FORMAT, AudioSampleFormat::SAMPLE_S16LE);
2060 OH_AVFormat_SetLongValue(format, OH_MD_KEY_BITRATE, BITS_RATE);
2061 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
2062 OH_AVFormat_SetIntValue(format, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
2063 set<int32_t> supportedAacProfile = {AAC_PROFILE_LC};
2064 if (tmpCodec != nullptr) {
2065 OH_AudioCodec_Destroy(tmpCodec);
2066 supportedAacProfile = {AAC_PROFILE_LC, AAC_PROFILE_HE, AAC_PROFILE_HE_V2};
2067 }
2068 for (const int32_t profile : supportedAacProfile) {
2069 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, profile);
2070 EXPECT_EQ(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
2071 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
2072 }
2073 set<int32_t> unsupportedAacProfile = {1, 2, 5};
2074 for (const int32_t profile : unsupportedAacProfile) {
2075 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PROFILE, profile);
2076 EXPECT_NE(AV_ERR_OK, OH_AudioCodec_Configure(audioEnc_, format));
2077 EXPECT_EQ(OH_AudioCodec_Reset(audioEnc_), AV_ERR_OK);
2078 }
2079 }
2080
2081 } // namespace MediaAVCodec
2082 } // namespace OHOS
2083