1 /*
2  * Copyright (c) 2021-2021 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 "plugin/common/any.h"
18 #include "plugin/plugins/ffmpeg_adapter/utils/aac_audio_config_parser.h"
19 #include "plugin/plugins/ffmpeg_adapter/utils/ffmpeg_utils.h"
20 
21 #define private public
22 #define protected public
23 
24 using namespace testing::ext;
25 
26 namespace OHOS {
27 namespace Media {
28 namespace Test {
29 using namespace Plugin;
30 using namespace Plugin::Ffmpeg;
31 
32 HWTEST(ChannelLayoutTest, test_convert_from_ffmpeg_mono, TestSize.Level1)
33 {
34     int channels = 1;
35     uint64_t ffChannelLayout = 0x4;
36 
37     AudioChannelLayout channelLayout = Ffmpeg::ConvertChannelLayoutFromFFmpeg(channels, ffChannelLayout);
38     EXPECT_EQ(AudioChannelLayout::MONO, channelLayout);
39 }
40 
41 HWTEST(ChannelLayoutTest, test_convert_from_ffmpeg_stereo, TestSize.Level1)
42 {
43     int channels = 2;
44     uint64_t ffChannelLayout = 0x3;
45 
46     AudioChannelLayout channelLayout = Ffmpeg::ConvertChannelLayoutFromFFmpeg(channels, ffChannelLayout);
47     EXPECT_EQ(AudioChannelLayout::STEREO, channelLayout);
48 }
49 
50 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_1, TestSize.Level1)
51 {
52     const uint8_t config[2] = {0x8A, 0xE6};
53     AACAudioConfigParser aacAudioConfigParser(config, 2);
54     bool status = aacAudioConfigParser.ParseConfigs();
55     EXPECT_EQ(true, status);
56     uint32_t level = aacAudioConfigParser.GetLevel();
57     EXPECT_EQ(2, level);
58 }
59 
60 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_2, TestSize.Level1)
61 {
62     const uint8_t config[2] = {0x8F, 0xE6};
63     AACAudioConfigParser aacAudioConfigParser(config, 2);
64     bool status = aacAudioConfigParser.ParseConfigs();
65     EXPECT_EQ(false, status);
66 }
67 
68 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_3, TestSize.Level1)
69 {
70     const uint8_t config[5] = {0x8F, 0xE6, 0x47, 0x6F, 0x5B};
71     AACAudioConfigParser aacAudioConfigParser(config, 5);
72     bool status = aacAudioConfigParser.ParseConfigs();
73     EXPECT_EQ(false, status);
74 }
75 
76 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_4, TestSize.Level1)
77 {
78     const uint8_t config[2] = {0x8E, 0xE6};
79     AACAudioConfigParser aacAudioConfigParser(config, 2);
80     bool status = aacAudioConfigParser.ParseConfigs();
81     EXPECT_EQ(false, status);
82 }
83 
84 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_5, TestSize.Level1)
85 {
86     const uint8_t config[2] = {0x2A, 0xE6};
87     AACAudioConfigParser aacAudioConfigParser(config, 2);
88     bool status = aacAudioConfigParser.ParseConfigs();
89     EXPECT_EQ(false, status);
90 }
91 
92 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_6, TestSize.Level1)
93 {
94     const uint8_t config[2] = {0x8A, 0xE6};
95     AACAudioConfigParser aacAudioConfigParser(config, 2);
96     bool status = aacAudioConfigParser.ParseConfigs();
97     EXPECT_EQ(true, status);
98 }
99 
100 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_7, TestSize.Level1)
101 {
102     const uint8_t config[2] = {0b00000000, 0b00000000};
103     AACAudioConfigParser aacAudioConfigParser(config, 2);
104     bool status = aacAudioConfigParser.ParseConfigs();
105     EXPECT_EQ(false, status);
106 }
107 
108 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_8, TestSize.Level1)
109 {
110     const uint8_t config[2] = {0b00010001, 0b10001001};
111     AACAudioConfigParser aacAudioConfigParser(config, 2);
112     bool status = aacAudioConfigParser.ParseConfigs();
113     EXPECT_EQ(true, status);
114 }
115 
116 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_9, TestSize.Level1)
117 {
118     const uint8_t config[2] = {0b00011010, 0b10010010};
119     AACAudioConfigParser aacAudioConfigParser(config, 2);
120     bool status = aacAudioConfigParser.ParseConfigs();
121     EXPECT_EQ(true, status);
122 }
123 
124 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_10, TestSize.Level1)
125 {
126     const uint8_t config[2] = {0b00100011, 0b00011011};
127     AACAudioConfigParser aacAudioConfigParser(config, 2);
128     bool status = aacAudioConfigParser.ParseConfigs();
129     EXPECT_EQ(true, status);
130 }
131 
132 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_11, TestSize.Level1)
133 {
134     const uint8_t config[2] = {0b00001100, 0b10100100};
135     AACAudioConfigParser aacAudioConfigParser(config, 2);
136     bool status = aacAudioConfigParser.ParseConfigs();
137     EXPECT_EQ(true, status);
138 }
139 
140 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_12, TestSize.Level1)
141 {
142     const uint8_t config[2] = {0b10000110, 0b10101101};
143     AACAudioConfigParser aacAudioConfigParser(config, 2);
144     bool status = aacAudioConfigParser.ParseConfigs();
145     EXPECT_EQ(false, status);
146 }
147 
148 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_13, TestSize.Level1)
149 {
150     const uint8_t config[2] = {0b10000111, 0b10110110};
151     AACAudioConfigParser aacAudioConfigParser(config, 2);
152     bool status = aacAudioConfigParser.ParseConfigs();
153     EXPECT_EQ(false, status);
154 }
155 
156 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_14, TestSize.Level1)
157 {
158     const uint8_t config[2] = {0b10000111, 0b01011111};
159     AACAudioConfigParser aacAudioConfigParser(config, 2);
160     bool status = aacAudioConfigParser.ParseConfigs();
161     EXPECT_EQ(false, status);
162 }
163 
164 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_15, TestSize.Level1)
165 {
166     const uint8_t config[2] = {0b10000111, 0b01100101};
167     AACAudioConfigParser aacAudioConfigParser(config, 2);
168     bool status = aacAudioConfigParser.ParseConfigs();
169     EXPECT_EQ(false, status);
170 }
171 
172 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_16, TestSize.Level1)
173 {
174     const uint8_t config[1] = {0xFA};
175     AACAudioConfigParser aacAudioConfigParser(config, 1);
176     bool status = aacAudioConfigParser.ParseConfigs();
177     EXPECT_EQ(false, status);
178 }
179 
180 HWTEST(AACAudioConfigParserTest, test_aac_audio_config_parser_17, TestSize.Level1)
181 {
182     const uint8_t config[2] = {0xFA, 0xE6};
183     AACAudioConfigParser aacAudioConfigParser(config, 2);
184     bool status = aacAudioConfigParser.ParseConfigs();
185     EXPECT_EQ(false, status);
186 }
187 
188 HWTEST(UtilsTest, testAVStrError, TestSize.Level1)
189 {
190     const int number = 1;
191     auto res = AVStrError(number);
192     ASSERT_STREQ(res.c_str(), "No error information");
193 }
194 
195 HWTEST(UtilsTest, testConvertTimeFromFFmpeg, TestSize.Level1)
196 {
197     int64_t number = 1000;
198     AVRational rational = av_make_q(1000, 500);
199     auto res = ConvertTimeFromFFmpeg(number, rational);
200     ASSERT_EQ(res, 2000000000000);
201     number = ((int64_t)UINT64_C(0x8000000000000000));
202     auto res2 = ConvertTimeFromFFmpeg(number, rational);
203     ASSERT_EQ(res2, -1);
204 }
205 
206 HWTEST(UtilsTest, testConvertTimeToFFmpeg, TestSize.Level1)
207 {
208     int64_t number = 1045566545;
209     AVRational rational = av_make_q(0, 50);
210     auto res = ConvertTimeToFFmpeg(number, rational);
211     ASSERT_EQ(res, ((int64_t)UINT64_C(0x8000000000000000)));
212     rational = av_make_q(10, 500);
213     auto res2 = ConvertTimeToFFmpeg(number, rational);
214     ASSERT_EQ(res2, 52);
215 }
216 
217 HWTEST(UtilsTest, testFillAVPicture, TestSize.Level1)
218 {
219     AVFrame* frame = av_frame_alloc();
220     uint8_t ptr = 10;
221     auto res = FillAVPicture(frame, &ptr, AVPixelFormat::AV_PIX_FMT_ABGR, 1920, 1080);
222     ASSERT_EQ(res, 0);
223 }
224 
225 HWTEST(UtilsTest, testGetAVPictureSize, TestSize.Level1)
226 {
227     AVPixelFormat format = AVPixelFormat::AV_PIX_FMT_ABGR;
228     int height = 1920;
229     int width = 1080;
230     auto res = GetAVPictureSize(format, height, width);
231     ASSERT_EQ(res, 0);
232 }
233 
234 HWTEST(UtilsTest, testRemoveDelimiter, TestSize.Level1)
235 {
236     const char* str = "hello";
237     char ch = 'o';
238     auto res = RemoveDelimiter(str, ch);
239     ASSERT_STREQ(res.c_str(), "hell");
240 }
241 
242 HWTEST(UtilsTest, testRemoveDelimiter2, TestSize.Level1)
243 {
244     std::string str = "hello";
245     char ch = 'l';
246     RemoveDelimiter(ch, str);
247     ASSERT_STREQ(str.c_str(), "heo");
248 }
249 
250 HWTEST(UtilsTest, testReplaceDelimiter, TestSize.Level1)
251 {
252     std::string limit = "he";
253     char ch = 'e';
254     std::string str = "hello";
255     ReplaceDelimiter(limit, ch, str);
256     ASSERT_STREQ(str.c_str(), "eeeee");
257     char ch2 = 'o';
258     std::string str2 = "hello";
259     ReplaceDelimiter(limit, ch2, str2);
260     ASSERT_STREQ(str2.c_str(), "hello");
261 }
262 
263 HWTEST(UtilsTest, testSplitString, TestSize.Level1)
264 {
265     const char* limit = "hello";
266     char ch = 'l';
267     std::vector<std::string> res = SplitString(limit, ch);
268     ASSERT_EQ(res.size(), 1);
269     ASSERT_STREQ(res[0].c_str(), "he");
270 }
271 
272 HWTEST(UtilsTest, testConvertChannelLayoutFromFFmpeg, TestSize.Level1)
273 {
274     for (int index = 0; index <= 24; index++) {
275         auto res = ConvertChannelLayoutFromFFmpeg(index, 0);
276         switch (index) {
277             case 1:
278                 ASSERT_EQ(AudioChannelLayout::MONO, res);
279                 break;
280             case 2:
281                 ASSERT_EQ(AudioChannelLayout::STEREO, res);
282                 break;
283             case 4:
284                 ASSERT_EQ(AudioChannelLayout::CH_4POINT0, res);
285                 break;
286             case 6:
287                 ASSERT_EQ(AudioChannelLayout::CH_5POINT1, res);
288                 break;
289             case 8:
290                 ASSERT_EQ(AudioChannelLayout::CH_5POINT1POINT2, res);
291                 break;
292             case 10:
293                 ASSERT_EQ(AudioChannelLayout::CH_7POINT1POINT2, res);
294                 break;
295             case 12:
296                 ASSERT_EQ(AudioChannelLayout::CH_7POINT1POINT4, res);
297                 break;
298             case 14:
299                 ASSERT_EQ(AudioChannelLayout::CH_9POINT1POINT4, res);
300                 break;
301             case 16:
302                 ASSERT_EQ(AudioChannelLayout::CH_9POINT1POINT6, res);
303                 break;
304             case 24:
305                 ASSERT_EQ(AudioChannelLayout::CH_22POINT2, res);
306                 break;
307             default:
308                 ASSERT_EQ(AudioChannelLayout::UNKNOWN, res);
309                 break;
310         }
311     }
312 }
313 
314 } // namespace Test
315 } // namespace Media
316 } // namespace OHOS