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 "securec.h"
34 #include "ffmpeg_converter.h"
35
36 using namespace std;
37 using namespace testing::ext;
38 using namespace OHOS::MediaAVCodec;
39
40 namespace {
41 const string CODEC_FLAC_NAME = std::string(AVCodecCodecName::AUDIO_ENCODER_FLAC_NAME);
42 const string CODEC_AAC_NAME = std::string(AVCodecCodecName::AUDIO_ENCODER_AAC_NAME);
43 constexpr uint32_t CHANNEL_COUNT = 2;
44 constexpr uint32_t ABNORMAL_CHANNEL_COUNT = 10;
45 constexpr uint32_t SAMPLE_RATE = 44100;
46 constexpr uint32_t ABNORMAL_SAMPLE_RATE = 9999999;
47 constexpr uint32_t BITS_RATE = 261000;
48 constexpr int32_t ABNORMAL_BITS_RATE = -1;
49 constexpr int32_t BITS_PER_CODED_SAMPLE = AudioSampleFormat::SAMPLE_S16LE;
50 constexpr int32_t ABNORMAL_BITS_SAMPLE = AudioSampleFormat::INVALID_WIDTH;
51 constexpr uint32_t FRAME_DURATION_US = 33000;
52 constexpr uint64_t CHANNEL_LAYOUT = AudioChannelLayout::STEREO;
53 constexpr uint64_t ABNORMAL_CHANNEL_LAYOUT = AudioChannelLayout::CH_10POINT2;
54 constexpr int32_t SAMPLE_FORMAT = AudioSampleFormat::SAMPLE_S16LE;
55 constexpr uint32_t FLAC_DEFAULT_FRAME_BYTES = 18432;
56 constexpr uint32_t AAC_DEFAULT_FRAME_BYTES = 2 * 1024 * 4;
57 constexpr uint32_t ILLEGAL_CHANNEL_COUNT = 9;
58 constexpr uint32_t ILLEGAL_SAMPLE_RATE = 441000;
59 constexpr int32_t COMPLIANCE_LEVEL = 0;
60 constexpr int32_t ABNORMAL_COMPLIANCE_LEVEL_L = -9999999;
61 constexpr int32_t ABNORMAL_COMPLIANCE_LEVEL_R = 9999999;
62 constexpr int32_t MAX_INPUT_SIZE = 8192;
63
64 constexpr string_view FLAC_INPUT_FILE_PATH = "/data/test/media/flac_2c_44100hz_261k.pcm";
65 constexpr string_view FLAC_OUTPUT_FILE_PATH = "/data/test/media/encoderTest.flac";
66
67 constexpr string_view AAC_INPUT_FILE_PATH = "/data/test/media/aac_2c_44100hz_199k.pcm";
68 constexpr string_view AAC_OUTPUT_FILE_PATH = "/data/test/media/aac_2c_44100hz_encode.aac";
69 } // namespace
70
71 namespace OHOS {
72 namespace MediaAVCodec {
73 class AEncSignal {
74 public:
75 std::mutex inMutex_;
76 std::mutex outMutex_;
77 std::condition_variable inCond_;
78 std::condition_variable outCond_;
79 std::queue<uint32_t> inQueue_;
80 std::queue<uint32_t> outQueue_;
81 std::queue<OH_AVMemory *> inBufferQueue_;
82 std::queue<OH_AVMemory *> outBufferQueue_;
83 std::queue<OH_AVCodecBufferAttr> attrQueue_;
84 };
85
OnError(OH_AVCodec * codec,int32_t errorCode,void * userData)86 static void OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
87 {
88 (void)codec;
89 (void)errorCode;
90 (void)userData;
91 cout << "Error received, errorCode:" << errorCode << endl;
92 }
93
OnOutputFormatChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)94 static void OnOutputFormatChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
95 {
96 (void)codec;
97 (void)format;
98 (void)userData;
99 cout << "OnOutputFormatChanged received" << endl;
100 }
101
OnInputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,void * userData)102 static void OnInputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData)
103 {
104 (void)codec;
105 AEncSignal *signal = static_cast<AEncSignal *>(userData);
106 unique_lock<mutex> lock(signal->inMutex_);
107 signal->inQueue_.push(index);
108 signal->inBufferQueue_.push(data);
109 signal->inCond_.notify_all();
110 }
111
OnOutputBufferAvailable(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,OH_AVCodecBufferAttr * attr,void * userData)112 static void OnOutputBufferAvailable(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
113 void *userData)
114 {
115 (void)codec;
116 AEncSignal *signal = static_cast<AEncSignal *>(userData);
117 unique_lock<mutex> lock(signal->outMutex_);
118 signal->outQueue_.push(index);
119 signal->outBufferQueue_.push(data);
120 if (attr) {
121 signal->attrQueue_.push(*attr);
122 } else {
123 cout << "OnOutputBufferAvailable error, attr is nullptr!" << endl;
124 }
125 signal->outCond_.notify_all();
126 }
127
128 class AudioCodeCapiEncoderUnitTest : public testing::Test {
129 public:
130 static void SetUpTestCase(void);
131 static void TearDownTestCase(void);
132 void SetUp();
133 void TearDown();
134 int32_t ProceFunc(const std::string codecName = CODEC_FLAC_NAME);
135 void InputFunc();
136 void OutputFunc();
137
138 protected:
139 std::atomic<bool> isRunning_ = false;
140 std::unique_ptr<std::ifstream> inputFile_;
141 std::unique_ptr<std::thread> inputLoop_;
142 std::unique_ptr<std::thread> outputLoop_;
143 int32_t index_;
144 uint32_t frameBytes_ = FLAC_DEFAULT_FRAME_BYTES; // default for flac
145 std::string inputFilePath_ = FLAC_INPUT_FILE_PATH.data();
146 std::string outputFilePath_ = FLAC_OUTPUT_FILE_PATH.data();
147
148 struct OH_AVCodecAsyncCallback cb_;
149 AEncSignal *signal_;
150 OH_AVCodec *audioEnc_;
151 OH_AVFormat *format;
152 bool isFirstFrame_ = true;
153 int64_t timeStamp_ = 0;
154 };
155
SetUpTestCase(void)156 void AudioCodeCapiEncoderUnitTest::SetUpTestCase(void)
157 {
158 cout << "[SetUpTestCase]: " << endl;
159 }
160
TearDownTestCase(void)161 void AudioCodeCapiEncoderUnitTest::TearDownTestCase(void)
162 {
163 cout << "[TearDownTestCase]: " << endl;
164 }
165
SetUp(void)166 void AudioCodeCapiEncoderUnitTest::SetUp(void)
167 {
168 cout << "[SetUp]: SetUp!!!" << endl;
169 }
170
TearDown(void)171 void AudioCodeCapiEncoderUnitTest::TearDown(void)
172 {
173 cout << "[TearDown]: over!!!" << endl;
174 }
175
InputFunc()176 void AudioCodeCapiEncoderUnitTest::InputFunc()
177 {
178 OH_AVCodecBufferAttr info = {};
179 bool isEos = false;
180 inputFile_ = std::make_unique<std::ifstream>(inputFilePath_, std::ios::binary);
181 if (!inputFile_->is_open()) {
182 std::cout << "open file failed, path: " << inputFilePath_ << std::endl;
183 return;
184 }
185 while (true) {
186 if (!isRunning_.load()) {
187 break;
188 }
189 unique_lock<mutex> lock(signal_->inMutex_);
190 signal_->inCond_.wait(lock, [this]() { return (signal_->inQueue_.size() > 0 || !isRunning_.load()); });
191 if (!isRunning_.load()) {
192 break;
193 }
194 uint32_t index = signal_->inQueue_.front();
195 auto buffer = signal_->inBufferQueue_.front();
196 isEos = !inputFile_->eof();
197 if (!isEos) {
198 inputFile_->read((char *)OH_AVMemory_GetAddr(buffer), frameBytes_);
199 }
200 info.size = frameBytes_;
201 info.flags = AVCODEC_BUFFER_FLAGS_NONE;
202 if (isEos) {
203 info.size = 0;
204 info.flags = AVCODEC_BUFFER_FLAGS_EOS;
205 } else if (isFirstFrame_) {
206 info.flags = AVCODEC_BUFFER_FLAGS_CODEC_DATA;
207 isFirstFrame_ = false;
208 }
209 info.offset = 0;
210 int32_t ret = OH_AudioEncoder_PushInputData(audioEnc_, index, info);
211 signal_->inQueue_.pop();
212 signal_->inBufferQueue_.pop();
213 if (ret != AVCS_ERR_OK) {
214 isRunning_ = false;
215 break;
216 }
217 if (isEos) {
218 break;
219 }
220 timeStamp_ += FRAME_DURATION_US;
221 }
222 inputFile_->close();
223 }
224
OutputFunc()225 void AudioCodeCapiEncoderUnitTest::OutputFunc()
226 {
227 std::ofstream outputFile;
228 outputFile.open(outputFilePath_, std::ios::out | std::ios::binary);
229 if (!outputFile.is_open()) {
230 std::cout << "open file failed, path: " << outputFilePath_ << std::endl;
231 return;
232 }
233
234 while (true) {
235 if (!isRunning_.load()) {
236 cout << "stop, exit" << endl;
237 break;
238 }
239
240 unique_lock<mutex> lock(signal_->outMutex_);
241 signal_->outCond_.wait(lock, [this]() { return (signal_->outQueue_.size() > 0 || !isRunning_.load()); });
242
243 if (!isRunning_.load()) {
244 cout << "wait to stop, exit" << endl;
245 break;
246 }
247
248 uint32_t index = signal_->outQueue_.front();
249
250 OH_AVCodecBufferAttr attr = signal_->attrQueue_.front();
251 OH_AVMemory *data = signal_->outBufferQueue_.front();
252 if (data != nullptr) {
253 outputFile.write(reinterpret_cast<char *>(OH_AVMemory_GetAddr(data)), attr.size);
254 }
255 if (attr.flags == AVCODEC_BUFFER_FLAGS_EOS || attr.size == 0) {
256 cout << "encode eos" << endl;
257 isRunning_.store(false);
258 }
259
260 signal_->outBufferQueue_.pop();
261 signal_->attrQueue_.pop();
262 signal_->outQueue_.pop();
263 if (OH_AudioEncoder_FreeOutputData(audioEnc_, index) != AV_ERR_OK) {
264 cout << "Fatal: FreeOutputData fail" << endl;
265 break;
266 }
267 }
268 outputFile.close();
269 }
270
ProceFunc(const std::string codecName)271 int32_t AudioCodeCapiEncoderUnitTest::ProceFunc(const std::string codecName)
272 {
273 audioEnc_ = OH_AudioEncoder_CreateByName(codecName.c_str());
274 EXPECT_NE((OH_AVCodec *)nullptr, audioEnc_);
275
276 signal_ = new AEncSignal();
277 EXPECT_NE(nullptr, signal);
278
279 cb_ = {&OnError, &OnOutputFormatChanged, &OnInputBufferAvailable, &OnOutputBufferAvailable};
280 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_SetCallback(audioEnc_, cb_, signal_));
281
282 format = OH_AVFormat_Create();
283 return AVCS_ERR_OK;
284 }
285
286 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_CreateByName_01, TestSize.Level1)
287 {
288 audioEnc_ = OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_ENCODER_FLAC_NAME).data());
289 EXPECT_NE(nullptr, audioEnc_);
290 }
291
292 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_CreateByMime_01, TestSize.Level1)
293 {
294 audioEnc_ = OH_AudioEncoder_CreateByMime((AVCodecMimeType::MEDIA_MIMETYPE_AUDIO_FLAC).data());
295 EXPECT_NE(nullptr, audioEnc_);
296 }
297
298 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Prepare_01, TestSize.Level1)
299 {
300 ProceFunc();
301 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Prepare(audioEnc_));
302 }
303
304 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_GetOutputDescription_01, TestSize.Level1)
305 {
306 ProceFunc();
307 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
308 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
309 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
310 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
311 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
312 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
313 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
314
315 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
316 EXPECT_NE(nullptr, OH_AudioEncoder_GetOutputDescription(audioEnc_));
317 }
318
319 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_IsValid_01, TestSize.Level1)
320 {
321 ProceFunc();
322 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
323 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
324 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
325 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
326 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
327 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
328 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
329
330 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
331 bool value = true;
332 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_IsValid(audioEnc_, &value));
333 }
334
335 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_SetParameter_01, TestSize.Level1)
336 {
337 ProceFunc();
338 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
339 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
340 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
341 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
342 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
343 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
344 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
345
346 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
347 isRunning_.store(true);
348
349 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
350 EXPECT_NE(nullptr, inputLoop_);
351
352 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
353 EXPECT_NE(nullptr, outputLoop_);
354
355 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
356 while (isRunning_.load()) {
357 sleep(1); // sleep 1s
358 }
359
360 isRunning_.store(false);
361 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
362 {
363 unique_lock<mutex> lock(signal_->inMutex_);
364 signal_->inCond_.notify_all();
365 }
366 inputLoop_->join();
367 }
368 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
369 {
370 unique_lock<mutex> lock(signal_->outMutex_);
371 signal_->outCond_.notify_all();
372 }
373 outputLoop_->join();
374 }
375 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
376 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_SetParameter(audioEnc_, format));
377 }
378
379 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_SetParameter_02, TestSize.Level1)
380 {
381 ProceFunc();
382 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
383 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
384 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
385 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
386 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
387 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
388 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
389
390 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
391 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_SetParameter(audioEnc_, format));
392 }
393
394 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_01, TestSize.Level1)
395 {
396 ProceFunc();
397 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
398 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
399 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
400 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
401 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
402 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
403 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
404
405 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
406 }
407
408 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_02, TestSize.Level1)
409 {
410 ProceFunc();
411 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
412 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
413 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
414 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
415 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
416 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), ABNORMAL_CHANNEL_LAYOUT);
417 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
418
419 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
420 }
421
422 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_03, TestSize.Level1)
423 {
424 ProceFunc();
425 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
426 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
427 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
428 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), ABNORMAL_BITS_SAMPLE);
429 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
430 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
431 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
432
433 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
434 }
435
436 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_04, TestSize.Level1)
437 {
438 ProceFunc();
439 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), ABNORMAL_CHANNEL_COUNT);
440 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
441 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
442 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
443 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
444 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
445 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
446
447 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
448 }
449
450 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_05, TestSize.Level1)
451 {
452 ProceFunc();
453 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
454 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
455 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), ABNORMAL_BITS_RATE);
456 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
457 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
458 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
459 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
460
461 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
462 }
463
464 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_06, TestSize.Level1)
465 {
466 ProceFunc();
467 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
468 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
469 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE); // SetIntValue
470 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
471 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
472 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
473 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
474
475 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
476 }
477
478 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_07, TestSize.Level1)
479 {
480 ProceFunc();
481 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
482 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
483 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
484 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
485 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
486 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
487 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), ABNORMAL_COMPLIANCE_LEVEL_L);
488
489 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
490 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
491
492 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), ABNORMAL_COMPLIANCE_LEVEL_R);
493 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
494 }
495
496 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_Configure_08, TestSize.Level1)
497 {
498 ProceFunc();
499 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
500 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), ABNORMAL_SAMPLE_RATE);
501 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
502 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
503 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
504 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
505 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
506
507 ASSERT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
508 }
509
510 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_01, TestSize.Level1)
511 {
512 ProceFunc();
513 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
514 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
515 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
516 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
517 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
518 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
519 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
520
521 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
522 isRunning_.store(true);
523
524 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
525 EXPECT_NE(nullptr, inputLoop_);
526
527 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
528 EXPECT_NE(nullptr, outputLoop_);
529
530 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
531 while (isRunning_.load()) {
532 sleep(1); // sleep 1s
533 }
534
535 isRunning_.store(false);
536 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
537 {
538 unique_lock<mutex> lock(signal_->inMutex_);
539 signal_->inCond_.notify_all();
540 }
541 inputLoop_->join();
542 }
543 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
544 {
545 unique_lock<mutex> lock(signal_->outMutex_);
546 signal_->outCond_.notify_all();
547 }
548 outputLoop_->join();
549 }
550 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
551 }
552
553 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_02, TestSize.Level1)
554 {
555 ProceFunc();
556 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
557 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
558 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
559 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
560 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
561 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
562 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
563
564 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
565 isRunning_.store(true);
566 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
567 EXPECT_NE(nullptr, inputLoop_);
568 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
569 EXPECT_NE(nullptr, outputLoop_);
570
571 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
572 while (isRunning_.load()) {
573 sleep(1); // sleep 1s
574 }
575
576 isRunning_.store(false);
577 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
578 {
579 unique_lock<mutex> lock(signal_->inMutex_);
580 signal_->inCond_.notify_all();
581 }
582 inputLoop_->join();
583 }
584
585 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
586 {
587 unique_lock<mutex> lock(signal_->outMutex_);
588 signal_->outCond_.notify_all();
589 }
590 outputLoop_->join();
591 }
592 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Stop(audioEnc_));
593 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
594 }
595
596 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_03, TestSize.Level1)
597 {
598 ProceFunc();
599 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
600 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
601 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
602 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
603 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
604 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
605 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
606
607 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
608 isRunning_.store(true);
609
610 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
611 EXPECT_NE(nullptr, inputLoop_);
612
613 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
614 EXPECT_NE(nullptr, outputLoop_);
615
616 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
617 while (isRunning_.load()) {
618 sleep(1); // sleep 1s
619 }
620
621 isRunning_.store(false);
622 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
623 {
624 unique_lock<mutex> lock(signal_->inMutex_);
625 signal_->inCond_.notify_all();
626 }
627 inputLoop_->join();
628 }
629
630 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
631 {
632 unique_lock<mutex> lock(signal_->outMutex_);
633 signal_->outCond_.notify_all();
634 }
635 outputLoop_->join();
636 }
637 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
638 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
639 }
640
641 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_04, TestSize.Level1)
642 {
643 ProceFunc();
644 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
645 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
646 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
647 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
648 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
649 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
650 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
651
652 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
653 isRunning_.store(true);
654
655 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
656 EXPECT_NE(nullptr, inputLoop_);
657
658 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
659 EXPECT_NE(nullptr, outputLoop_);
660
661 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
662 while (isRunning_.load()) {
663 sleep(1); // sleep 1s
664 }
665 isRunning_.store(false);
666 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
667 {
668 unique_lock<mutex> lock(signal_->inMutex_);
669 signal_->inCond_.notify_all();
670 }
671 inputLoop_->join();
672 }
673
674 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
675 {
676 unique_lock<mutex> lock(signal_->outMutex_);
677 signal_->outCond_.notify_all();
678 }
679 outputLoop_->join();
680 }
681 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
682 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
683 }
684
685 HWTEST_F(AudioCodeCapiEncoderUnitTest, audioEncoder_normalcase_05, TestSize.Level1)
686 {
687 ProceFunc();
688 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
689 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
690 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_BITRATE.data(), BITS_RATE);
691 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_BITS_PER_CODED_SAMPLE.data(), BITS_PER_CODED_SAMPLE);
692 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(), SAMPLE_FORMAT);
693 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
694 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_COMPLIANCE_LEVEL.data(), COMPLIANCE_LEVEL);
695
696 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
697 isRunning_.store(true);
698
699 inputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::InputFunc, this);
700 EXPECT_NE(nullptr, inputLoop_);
701 outputLoop_ = make_unique<thread>(&AudioCodeCapiEncoderUnitTest::OutputFunc, this);
702 EXPECT_NE(nullptr, outputLoop_);
703
704 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
705 while (isRunning_.load()) {
706 sleep(1); // sleep 1s
707 }
708
709 isRunning_.store(false);
710 if (inputLoop_ != nullptr && inputLoop_->joinable()) {
711 {
712 unique_lock<mutex> lock(signal_->inMutex_);
713 signal_->inCond_.notify_all();
714 }
715 inputLoop_->join();
716 }
717
718 if (outputLoop_ != nullptr && outputLoop_->joinable()) {
719 {
720 unique_lock<mutex> lock(signal_->outMutex_);
721 signal_->outCond_.notify_all();
722 }
723 outputLoop_->join();
724 }
725 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Flush(audioEnc_));
726 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
727 }
728
729 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckChannelCount, TestSize.Level1)
730 {
731 inputFilePath_ = AAC_INPUT_FILE_PATH;
732 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
733 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
734 ProceFunc(CODEC_AAC_NAME);
735 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
736 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
737 AudioSampleFormat::SAMPLE_F32LE);
738 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing channel count
739
740 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
741 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), ILLEGAL_CHANNEL_COUNT);
742 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal channel count
743
744 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
745 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
746 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal channel count
747
748 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
749 }
750
751 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckSampleFormat, TestSize.Level1)
752 {
753 inputFilePath_ = AAC_INPUT_FILE_PATH;
754 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
755 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
756 ProceFunc(CODEC_AAC_NAME);
757 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
758 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
759 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample format
760
761 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
762 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
763 AudioSampleFormat::SAMPLE_U8);
764 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample format
765
766 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
767 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
768 AudioSampleFormat::SAMPLE_F32LE);
769 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample format
770
771 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
772 }
773
774 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckSampleRate, TestSize.Level1)
775 {
776 inputFilePath_ = AAC_INPUT_FILE_PATH;
777 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
778 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
779 ProceFunc(CODEC_AAC_NAME);
780 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), CHANNEL_COUNT);
781 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
782 AudioSampleFormat::SAMPLE_F32LE);
783 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // missing sample rate
784
785 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
786 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), ILLEGAL_SAMPLE_RATE);
787 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // illegal sample rate
788
789 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
790 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
791 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal sample rate
792
793 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
794 }
795
796 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckChannelLayout, TestSize.Level1)
797 {
798 inputFilePath_ = AAC_INPUT_FILE_PATH;
799 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
800 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
801 ProceFunc(CODEC_AAC_NAME);
802 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), 2);
803 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
804 AudioSampleFormat::SAMPLE_F32LE);
805 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
806 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CH_7POINT1);
807 EXPECT_NE(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // channel mismatch channel_layout
808
809 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Reset(audioEnc_));
810 OH_AVFormat_SetLongValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_LAYOUT.data(), CHANNEL_LAYOUT);
811 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format)); // normal channel layout
812
813 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
814 }
815
816 HWTEST_F(AudioCodeCapiEncoderUnitTest, aacCheckMaxinputSize, TestSize.Level1)
817 {
818 inputFilePath_ = AAC_INPUT_FILE_PATH;
819 outputFilePath_ = AAC_OUTPUT_FILE_PATH;
820 frameBytes_ = AAC_DEFAULT_FRAME_BYTES;
821 ProceFunc(CODEC_AAC_NAME);
822 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_CHANNEL_COUNT.data(), 2);
823 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_AUDIO_SAMPLE_FORMAT.data(),
824 AudioSampleFormat::SAMPLE_F32LE);
825 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_SAMPLE_RATE.data(), SAMPLE_RATE);
826 OH_AVFormat_SetIntValue(format, MediaDescriptionKey::MD_KEY_MAX_INPUT_SIZE.data(), MAX_INPUT_SIZE);
827 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Configure(audioEnc_, format));
828
829 auto fmt = OH_AudioEncoder_GetOutputDescription(audioEnc_);
830 EXPECT_NE(fmt, nullptr);
831 OH_AVFormat_Destroy(fmt);
832
833 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Start(audioEnc_));
834
835 EXPECT_EQ(OH_AVErrCode::AV_ERR_OK, OH_AudioEncoder_Destroy(audioEnc_));
836 }
837
838 HWTEST_F(AudioCodeCapiEncoderUnitTest, invalidEncoderNames, TestSize.Level1)
839 {
840 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_MP3_NAME).data()), nullptr);
841 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_AAC_NAME).data()), nullptr);
842 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_API9_AAC_NAME).data()), nullptr);
843 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_VORBIS_NAME).data()), nullptr);
844 EXPECT_EQ(OH_AudioEncoder_CreateByName((AVCodecCodecName::AUDIO_DECODER_FLAC_NAME).data()), nullptr);
845 }
846 } // namespace MediaAVCodec
847 } // namespace OHOS
848