1 /*
2 * Copyright (c) 2022 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 <memory>
18
19 #define private public
20 #include "audio_decoder.h"
21 #undef private
22
23 #include "audio_data.h"
24 #include "audio_event.h"
25 #include "decoder_callback_test.h"
26 #include "daudio_errorcode.h"
27 #include "daudio_log.h"
28 #include "daudio_util.h"
29 #include "audio_decoder.h"
30
31 using namespace testing::ext;
32
33 namespace OHOS {
34 namespace DistributedHardware {
35 class DecoderTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 void SetUp();
40 void TearDown();
41
42 std::shared_ptr<AudioDecoder> audiodecoder_ = nullptr;
43 std::shared_ptr<IAudioCodecCallback> decodeCb_ = nullptr;
44 };
45
46 const AudioCommonParam LOC_COMPARA_ENC_TEST = {SAMPLE_RATE_48000, STEREO, SAMPLE_S16LE, AUDIO_CODEC_AAC};
47
SetUpTestCase(void)48 void DecoderTest::SetUpTestCase(void)
49 {
50 }
51
TearDownTestCase(void)52 void DecoderTest::TearDownTestCase(void)
53 {
54 }
55
SetUp(void)56 void DecoderTest::SetUp(void)
57 {
58 audiodecoder_ = std::make_shared<AudioDecoder>();
59 decodeCb_ = std::make_shared<AudioDecoderCallbackTest>();
60 }
61
TearDown(void)62 void DecoderTest::TearDown(void)
63 {
64 audiodecoder_ = nullptr;
65 decodeCb_ = nullptr;
66 }
67
68 /**
69 * @tc.name: decode_test_001
70 * @tc.desc: Verify decode destruct function.
71 * @tc.type: FUNC
72 * @tc.require: AR000H0E5U
73 */
74 HWTEST_F(DecoderTest, decode_test_001, TestSize.Level1)
75 {
76 EXPECT_EQ(DH_SUCCESS, audiodecoder_->ConfigureAudioCodec(LOC_COMPARA_ENC_TEST, decodeCb_));
77 audiodecoder_ = std::make_shared<AudioDecoder>();
78 EXPECT_EQ(ERR_DH_AUDIO_BAD_VALUE, audiodecoder_->ConfigureAudioCodec(LOC_COMPARA_ENC_TEST, nullptr));
79 }
80
81 /**
82 * @tc.name: decode_test_002
83 * @tc.desc: Verify start stop decoder function.
84 * @tc.type: FUNC
85 * @tc.require: AR000H0E5U
86 */
87 HWTEST_F(DecoderTest, decode_test_002, TestSize.Level1)
88 {
89 EXPECT_EQ(ERR_DH_AUDIO_BAD_VALUE, audiodecoder_->StartAudioCodec());
90 EXPECT_EQ(DH_SUCCESS, audiodecoder_->StopAudioCodec());
91 size_t bufLen = 4096;
92 std::shared_ptr<AudioData> inputData = std::make_shared<AudioData>(bufLen);
93 EXPECT_EQ(EOK, memset_s(inputData->Data(), inputData->Size(), 0, inputData->Size()));
94 EXPECT_EQ(ERR_DH_AUDIO_CODEC_INPUT, audiodecoder_->FeedAudioData(inputData));
95 EXPECT_EQ(DH_SUCCESS, audiodecoder_->ConfigureAudioCodec(LOC_COMPARA_ENC_TEST, decodeCb_));
96 EXPECT_EQ(DH_SUCCESS, audiodecoder_->StartAudioCodec());
97 EXPECT_EQ(ERR_DH_AUDIO_BAD_VALUE, audiodecoder_->FeedAudioData(nullptr));
98 EXPECT_EQ(DH_SUCCESS, audiodecoder_->StopAudioCodec());
99 EXPECT_EQ(DH_SUCCESS, audiodecoder_->ReleaseAudioCodec());
100 }
101
102 /**
103 * @tc.name: decode_test_003
104 * @tc.desc: Verify decode data function.
105 * @tc.type: FUNC
106 * @tc.require: AR000H0E5U
107 */
108 HWTEST_F(DecoderTest, decode_test_003, TestSize.Level1)
109 {
110 audiodecoder_ = std::make_shared<AudioDecoder>();
111 EXPECT_EQ(DH_SUCCESS, audiodecoder_->ConfigureAudioCodec(LOC_COMPARA_ENC_TEST, decodeCb_));
112 EXPECT_EQ(DH_SUCCESS, audiodecoder_->StartAudioCodec());
113
114 for (int32_t i = 0; i < 200; i++) {
115 size_t bufLen = 4096;
116 std::shared_ptr<AudioData> inputData = std::make_shared<AudioData>(bufLen);
117 EXPECT_EQ(EOK, memset_s(inputData->Data(), inputData->Size(), 0, inputData->Size()));
118 EXPECT_EQ(DH_SUCCESS, audiodecoder_->FeedAudioData(inputData));
119 }
120
121 EXPECT_EQ(DH_SUCCESS, audiodecoder_->StopAudioCodec());
122 EXPECT_EQ(DH_SUCCESS, audiodecoder_->ReleaseAudioCodec());
123 }
124
125 /**
126 * @tc.name: decode_test_004
127 * @tc.desc: Verify decode data function.
128 * @tc.type: FUNC
129 * @tc.require: AR000H0E5U
130 */
131 HWTEST_F(DecoderTest, decode_test_004, TestSize.Level1)
132 {
133 audiodecoder_ = std::make_shared<AudioDecoder>();
134 for (int32_t i = 0; i < 200; i++) {
135 audiodecoder_->OnInputBufferAvailable(1);
136 }
137 Media::AVCodecBufferInfo info;
138 Media::AVCodecBufferFlag flag = static_cast<Media::AVCodecBufferFlag>(0);
139 audiodecoder_->OnOutputBufferAvailable(1, info, flag);
140 Media::Format format;
141 AudioEvent event;
142 audiodecoder_->OnOutputFormatChanged(format);
143 audiodecoder_->OnError(event);
144 EXPECT_EQ(DH_SUCCESS, audiodecoder_->ConfigureAudioCodec(LOC_COMPARA_ENC_TEST, decodeCb_));
145 EXPECT_EQ(DH_SUCCESS, audiodecoder_->ReleaseAudioCodec());
146 }
147
148 /**
149 * @tc.name: DecodeDone_001
150 * @tc.desc: Verify DecodeDone function.
151 * @tc.type: FUNC
152 * @tc.require: AR000H0E5U
153 */
154 HWTEST_F(DecoderTest, DecodeDone_001, TestSize.Level1)
155 {
156 audiodecoder_ = std::make_shared<AudioDecoder>();
157 size_t bufLen = 4096;
158 std::shared_ptr<AudioData> outputData = std::make_shared<AudioData>(bufLen);
159 std::shared_ptr<IAudioCodecCallback> decodeCb = std::make_shared<AudioDecoderCallbackTest>();
160 audiodecoder_->codecCallback_ = decodeCb;
161 EXPECT_EQ(DH_SUCCESS, audiodecoder_->DecodeDone(outputData));
162 audiodecoder_->ReduceWaitDecodeCnt();
163 }
164
165 /**
166 * @tc.name: DecodeDone_002
167 * @tc.desc: Verify DecodeDone function.
168 * @tc.type: FUNC
169 * @tc.require: AR000H0E5U
170 */
171 HWTEST_F(DecoderTest, DecodeDone_002, TestSize.Level1)
172 {
173 AudioEvent event;
174 audiodecoder_ = std::make_shared<AudioDecoder>();
175 size_t bufLen = 4096;
176 std::shared_ptr<AudioData> outputData = std::make_shared<AudioData>(bufLen);
177 std::shared_ptr<IAudioCodecCallback> callback = nullptr;
178 audiodecoder_->codecCallback_ = callback;
179 EXPECT_EQ(ERR_DH_AUDIO_BAD_VALUE, audiodecoder_->DecodeDone(outputData));
180 audiodecoder_->OnError(event);
181 }
182
183 /**
184 * @tc.name: InitAudioDecoder_001
185 * @tc.desc: Verify InitAudioDecoder function.
186 * @tc.type: FUNC
187 * @tc.require: AR000H0E5U
188 */
189 HWTEST_F(DecoderTest, InitAudioDecoder_001, TestSize.Level1)
190 {
191 AudioCommonParam codecParam;
192 audiodecoder_ = std::make_shared<AudioDecoder>();
193 EXPECT_EQ(DH_SUCCESS, audiodecoder_->InitAudioDecoder(codecParam));
194 EXPECT_EQ(DH_SUCCESS, audiodecoder_->SetDecoderFormat(codecParam));
195 }
196
197 /**
198 * @tc.name: ProcessData_001
199 * @tc.desc: Verify ProcessData function.
200 * @tc.type: FUNC
201 * @tc.require: AR000H0E5U
202 */
203 HWTEST_F(DecoderTest, ProcessData_001, TestSize.Level1)
204 {
205 size_t bufLen = 4096;
206 std::shared_ptr<AudioData> inputData = std::make_shared<AudioData>(bufLen);
207 int32_t bufferIndex = 0;
208 audiodecoder_ = std::make_shared<AudioDecoder>();
209 uint32_t index = 1;
210 Media::AVCodecBufferInfo info;
211 Media::AVCodecBufferFlag flag = static_cast<Media::AVCodecBufferFlag>(0);
212 EXPECT_EQ(ERR_DH_AUDIO_BAD_VALUE, audiodecoder_->ProcessData(inputData, bufferIndex));
213 audiodecoder_->OnOutputBufferAvailable(index, info, flag);
214 }
215
216 /**
217 * @tc.name: IsInDecodeRange_001
218 * @tc.desc: Verify IsInDecodeRange function.
219 * @tc.type: FUNC
220 * @tc.require: AR000H0E5U
221 */
222 HWTEST_F(DecoderTest, IsInDecodeRange_001, TestSize.Level1)
223 {
224 AudioCommonParam codecParam;
225 EXPECT_EQ(false, audiodecoder_->IsInDecodeRange(codecParam));
226 }
227
228 /**
229 * @tc.name: IsInDecodeRange_002
230 * @tc.desc: Verify IsInDecodeRange function.
231 * @tc.type: FUNC
232 * @tc.require: AR000H0E5U
233 */
234 HWTEST_F(DecoderTest, IsInDecodeRange_002, TestSize.Level1)
235 {
236 AudioCommonParam codecParam;
237 codecParam.bitFormat = SAMPLE_S16LE;
238 EXPECT_EQ(true, audiodecoder_->IsInDecodeRange(codecParam));
239 }
240 } // namespace DistributedHardware
241 } // namespace OHOS
242