• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 "gtest/gtest.h"
17 #include "audio_resample.h"
18 #include "avcodec_log.h"
19 #include "securec.h"
20 #include "ffmpeg_converter.h"
21 #include "audio_buffer_info.h"
22 
23 using namespace std;
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace MediaAVCodec {
28 class AudioResampleUnitTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34     std::shared_ptr<AudioResample> audioResample_;
35 };
36 
SetUpTestCase(void)37 void AudioResampleUnitTest::SetUpTestCase(void)
38 {
39     cout << "[SetUpTestCase]: " << endl;
40 }
41 
TearDownTestCase(void)42 void AudioResampleUnitTest::TearDownTestCase(void)
43 {
44     cout << "[TearDownTestCase]: " << endl;
45 }
46 
SetUp(void)47 void AudioResampleUnitTest::SetUp(void)
48 {
49     audioResample_ = std::make_shared<AudioResample>();
50     cout << "[SetUp]: SetUp!!!" << endl;
51 }
52 
TearDown(void)53 void AudioResampleUnitTest::TearDown(void)
54 {
55     cout << "[TearDown]: over!!!" << endl;
56 }
57 
58 /**
59  * @tc.name: InitSwrContext_001
60  * @tc.desc: err < 0
61  * @tc.type: FUNC
62  */
63 HWTEST_F(AudioResampleUnitTest, InitSwrContext_001, TestSize.Level1)
64 {
65     ResamplePara para;
66     para.sampleRate = -1; // invalid samplerate
67 
68     int32_t ret = audioResample_->InitSwrContext(para);
69     EXPECT_NE(ret, AVCodecServiceErrCode::AVCS_ERR_OK);
70 }
71 
72 /**
73  * @tc.name: InitSwrContext_002
74  * @tc.desc: swr_init(swrContext) == 0
75  * @tc.type: FUNC
76  */
77 HWTEST_F(AudioResampleUnitTest, InitSwrContext_002, TestSize.Level1)
78 {
79     ResamplePara para;
80     para.sampleRate = 44100;
81     para.srcFmt = AV_SAMPLE_FMT_S16;
82     para.destFmt = AV_SAMPLE_FMT_S16;
83     para.channels = 2; // STEREO
84     para.destSamplesPerFrame = 1024;
85 
86     int32_t ret = audioResample_->InitSwrContext(para);
87     EXPECT_NE(ret, AVCodecServiceErrCode::AVCS_ERR_OK);
88 }
89 
90 /**
91  * @tc.name: Init_001
92  * @tc.desc: Init failed
93  * @tc.type: FUNC
94  */
95 HWTEST_F(AudioResampleUnitTest, Init_001, TestSize.Level1)
96 {
97     ResamplePara para;
98     para.sampleRate = 44100;
99     para.srcFmt = AV_SAMPLE_FMT_S16;
100     para.destFmt = AV_SAMPLE_FMT_S16;
101     para.channels = 2; // STEREO
102     para.destSamplesPerFrame = 1024;
103 
104     int32_t ret = audioResample_->Init(para);
105     EXPECT_NE(ret, AVCodecServiceErrCode::AVCS_ERR_OK);
106 }
107 
108 /**
109  * @tc.name: ConvertFrame_001
110  * @tc.desc: ConvertFrame failed
111  * @tc.type: FUNC
112  */
113 HWTEST_F(AudioResampleUnitTest, ConvertFrame_001, TestSize.Level1)
114 {
115     auto outputFrame = nullptr;
116     auto inputFrame = nullptr;
117 
118     auto ret = audioResample_->ConvertFrame(outputFrame, inputFrame);
119     EXPECT_NE(ret, AVCodecServiceErrCode::AVCS_ERR_OK);
120 }
121 } // namespace MediaAVCodec
122 } // namespace OHOS
123