1 /*
2 * Copyright (C) 2024 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 #include <gtest/gtest.h>
16 #include "avformat_mock.h"
17 #include "meta/format.h"
18 #include "meta/meta.h"
19 #include "common/status.h"
20 #include "native_audio_channel_layout.h"
21
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace Media {
28 namespace Plugins {
29 std::vector<OH_AudioChannelLayout> nativeChanneLayouts = {CH_LAYOUT_UNKNOWN,
30 CH_LAYOUT_MONO,
31 CH_LAYOUT_STEREO,
32 CH_LAYOUT_STEREO_DOWNMIX,
33 CH_LAYOUT_2POINT1,
34 CH_LAYOUT_3POINT0,
35 CH_LAYOUT_SURROUND,
36 CH_LAYOUT_3POINT1,
37 CH_LAYOUT_4POINT0,
38 CH_LAYOUT_QUAD_SIDE,
39 CH_LAYOUT_QUAD,
40 CH_LAYOUT_2POINT0POINT2,
41 CH_LAYOUT_AMB_ORDER1_ACN_N3D,
42 CH_LAYOUT_AMB_ORDER1_ACN_SN3D,
43 CH_LAYOUT_AMB_ORDER1_FUMA,
44 CH_LAYOUT_4POINT1,
45 CH_LAYOUT_5POINT0,
46 CH_LAYOUT_5POINT0_BACK,
47 CH_LAYOUT_2POINT1POINT2,
48 CH_LAYOUT_3POINT0POINT2,
49 CH_LAYOUT_5POINT1,
50 CH_LAYOUT_5POINT1_BACK,
51 CH_LAYOUT_6POINT0,
52 CH_LAYOUT_3POINT1POINT2,
53 CH_LAYOUT_6POINT0_FRONT,
54 CH_LAYOUT_HEXAGONAL,
55 CH_LAYOUT_6POINT1,
56 CH_LAYOUT_6POINT1_BACK,
57 CH_LAYOUT_6POINT1_FRONT,
58 CH_LAYOUT_7POINT0,
59 CH_LAYOUT_7POINT0_FRONT,
60 CH_LAYOUT_7POINT1,
61 CH_LAYOUT_OCTAGONAL,
62 CH_LAYOUT_5POINT1POINT2,
63 CH_LAYOUT_7POINT1_WIDE,
64 CH_LAYOUT_7POINT1_WIDE_BACK,
65 CH_LAYOUT_AMB_ORDER2_ACN_N3D,
66 CH_LAYOUT_AMB_ORDER2_ACN_SN3D,
67 CH_LAYOUT_AMB_ORDER2_FUMA,
68 CH_LAYOUT_5POINT1POINT4,
69 CH_LAYOUT_7POINT1POINT2,
70 CH_LAYOUT_7POINT1POINT4,
71 CH_LAYOUT_10POINT2,
72 CH_LAYOUT_9POINT1POINT4,
73 CH_LAYOUT_9POINT1POINT6,
74 CH_LAYOUT_HEXADECAGONAL,
75 CH_LAYOUT_AMB_ORDER3_ACN_N3D,
76 CH_LAYOUT_AMB_ORDER3_ACN_SN3D,
77 CH_LAYOUT_AMB_ORDER3_FUMA,
78 CH_LAYOUT_22POINT2};
79
80 std::vector<AudioChannelLayout> innerChanneLayouts = {
81 UNKNOWN,
82 MONO,
83 STEREO,
84 STEREO_DOWNMIX,
85 CH_2POINT1,
86 CH_2_1, /**CH_LAYOUT_3POINT0 in native_audio_channel_layout*/
87 SURROUND,
88 CH_3POINT1,
89 CH_4POINT0,
90 CH_2_2, /**CH_LAYOUT_QUAD_SIDE in native_audio_channel_layout*/
91 QUAD,
92 CH_2POINT0POINT2,
93 HOA_ORDER1_ACN_N3D,
94 HOA_ORDER1_ACN_SN3D,
95 HOA_ORDER1_FUMA,
96 CH_4POINT1,
97 CH_5POINT0,
98 CH_5POINT0_BACK,
99 CH_2POINT1POINT2,
100 CH_3POINT0POINT2,
101 CH_5POINT1,
102 CH_5POINT1_BACK,
103 CH_6POINT0,
104 CH_3POINT1POINT2,
105 CH_6POINT0_FRONT,
106 HEXAGONAL,
107 CH_6POINT1,
108 CH_6POINT1_BACK,
109 CH_6POINT1_FRONT,
110 CH_7POINT0,
111 CH_7POINT0_FRONT,
112 CH_7POINT1,
113 OCTAGONAL,
114 CH_5POINT1POINT2,
115 CH_7POINT1_WIDE,
116 CH_7POINT1_WIDE_BACK,
117 HOA_ORDER2_ACN_N3D,
118 HOA_ORDER2_ACN_SN3D,
119 HOA_ORDER2_FUMA,
120 CH_5POINT1POINT4,
121 CH_7POINT1POINT2,
122 CH_7POINT1POINT4,
123 CH_10POINT2,
124 CH_9POINT1POINT4,
125 CH_9POINT1POINT6,
126 HEXADECAGONAL,
127 HOA_ORDER3_ACN_N3D,
128 HOA_ORDER3_ACN_SN3D,
129 HOA_ORDER3_FUMA,
130 CH_22POINT2,
131 };
132
133 class AVFormatChannelayoutUnitTest : public testing::Test {
134 public:
135 // SetUpTestCase: Called before all test cases
136 static void SetUpTestCase(void);
137 // TearDownTestCase: Called after all test case
138 static void TearDownTestCase(void);
139 // SetUp: Called before each test cases
140 void SetUp(void);
141 // TearDown: Called after each test cases
142 void TearDown(void);
143
144 protected:
145 std::shared_ptr<FormatMock> format_ = nullptr;
146 };
147
SetUpTestCase(void)148 void AVFormatChannelayoutUnitTest::SetUpTestCase(void) {}
149
TearDownTestCase(void)150 void AVFormatChannelayoutUnitTest::TearDownTestCase(void) {}
151
SetUp(void)152 void AVFormatChannelayoutUnitTest::SetUp(void)
153 {
154 format_ = FormatMockFactory::CreateFormat();
155 ASSERT_NE(nullptr, format_);
156 }
157
TearDown(void)158 void AVFormatChannelayoutUnitTest::TearDown(void)
159 {
160 if (format_ != nullptr) {
161 format_->Destroy();
162 }
163 }
164
165 /**
166 * @tc.name: channel_layout_001
167 * @tc.desc: native channel layout match inner channel layout
168 * @tc.type: FUNC
169 * @tc.require: issueI5OX06 issueI5P8N0
170 */
171 HWTEST_F(AVFormatChannelayoutUnitTest, channel_layout_001, TestSize.Level1)
172 {
173 int32_t size = nativeChanneLayouts.size();
174 int32_t innerSize = innerChanneLayouts.size();
175 EXPECT_EQ(size, innerSize);
176 for (int32_t i = 0; i < size; ++i) {
177 EXPECT_EQ(static_cast<int64_t>(nativeChanneLayouts[i]), static_cast<int64_t>(innerChanneLayouts[i]));
178 }
179 }
180
181 /**
182 * @tc.name: channel_layout_002
183 * @tc.desc: format put and get value
184 * @tc.type: FUNC
185 * @tc.require: issueI5OX06 issueI5P8N0
186 */
187 HWTEST_F(AVFormatChannelayoutUnitTest, channel_layout_002, TestSize.Level1)
188 {
189 int32_t size = nativeChanneLayouts.size();
190 int64_t getLongValue = 0;
191 for (int32_t i = 0; i < size; ++i) {
192 EXPECT_TRUE(format_->PutLongValue(Tag::AUDIO_CHANNEL_LAYOUT, nativeChanneLayouts[i]));
193 EXPECT_TRUE(format_->GetLongValue(Tag::AUDIO_CHANNEL_LAYOUT, getLongValue));
194 EXPECT_EQ(getLongValue, static_cast<int64_t>(innerChanneLayouts[i]));
195 }
196 }
197
198 /**
199 * @tc.name: channel_layout_003
200 * @tc.desc:
201 * 1. set format;
202 * 2. dmpinfo;
203 * @tc.type: FUNC
204 * @tc.require: issueI5OWXY issueI5OXCD
205 */
206 HWTEST_F(AVFormatChannelayoutUnitTest, channel_layout_003, TestSize.Level1)
207 {
208 int32_t size = nativeChanneLayouts.size();
209 for (int32_t i = 0; i < size; ++i) {
210 std::shared_ptr<Format> format = std::make_shared<Format>();
211 EXPECT_TRUE(format_->PutLongValue(Tag::AUDIO_CHANNEL_LAYOUT, nativeChanneLayouts[i]));
212 std::string dumpInfo = format_->DumpInfo();
213 std::cout << "dumpInfo: [" << dumpInfo << "]\n";
214 std::stringstream dumpStream;
215 dumpStream.str("");
216 dumpStream << Tag::AUDIO_CHANNEL_LAYOUT << " = " << nativeChanneLayouts[i];
217 EXPECT_NE(dumpInfo.find(dumpStream.str()), string::npos) << "dumpStream: [" << dumpStream.str() << "]\n"
218 << "dumpInfo: [" << dumpInfo << "]\n";
219 }
220 }
221
222 #ifndef AVFORMAT_CAPI_UNIT_TEST
223 /**
224 * @tc.name: channel_layout_004
225 * @tc.desc:
226 * 1. set format;
227 * 2. meta trans by parcel;
228 * 3. dmpinfo;
229 * @tc.type: FUNC
230 * @tc.require: issueI5OWXY issueI5OXCD
231 */
232 HWTEST_F(AVFormatChannelayoutUnitTest, channel_layout_004, TestSize.Level1)
233 {
234 int32_t size = nativeChanneLayouts.size();
235 for (int32_t i = 0; i < size; ++i) {
236 std::shared_ptr<Meta> meta = std::make_shared<Meta>();
237 std::shared_ptr<Format> format = std::make_shared<Format>();
238 meta->SetData(Tag::AUDIO_CHANNEL_LAYOUT, innerChanneLayouts[i]);
239 format->SetMeta(meta);
240 std::string dumpInfo = format->Stringify();
241 std::cout << "dumpInfo: [" << dumpInfo << "]\n";
242 std::stringstream dumpStream;
__anonf72609730102() 243 auto checkFunc = [&dumpStream, &dumpInfo, i]() {
244 dumpStream.str("");
245 dumpStream << Tag::AUDIO_CHANNEL_LAYOUT << " = " << static_cast<int64_t>(nativeChanneLayouts[i]);
246 EXPECT_NE(dumpInfo.find(dumpStream.str()), string::npos) << "dumpStream: [" << dumpStream.str() << "]\n";
247 };
248 std::cout << "before trans by parcel:\n";
249 checkFunc();
250 MessageParcel parcel;
251 meta = format->GetMeta();
252 ASSERT_TRUE(meta->ToParcel(parcel));
253 ASSERT_TRUE(meta->FromParcel(parcel));
254
255 format = std::make_shared<Format>();
256 format->SetMeta(std::move(meta));
257 dumpInfo = format->Stringify();
258 std::cout << "after trans by parcel:\n";
259 checkFunc();
260 }
261 }
262
263 /**
264 * @tc.name: channel_layout_005
265 * @tc.desc:
266 * 1. set values to meta;
267 * 2. get formatMap;
268 * @tc.type: FUNC
269 * @tc.require: issueI5OWXY issueI5OXCD
270 */
271 HWTEST_F(AVFormatChannelayoutUnitTest, channel_layout_005, TestSize.Level1)
272 {
273 int32_t size = nativeChanneLayouts.size();
274 for (int32_t i = 0; i < size; ++i) {
275 MessageParcel parcel;
276 std::shared_ptr<Meta> meta = std::make_shared<Meta>();
277 std::shared_ptr<Format> format = std::make_shared<Format>();
278 meta->SetData(Tag::AUDIO_CHANNEL_LAYOUT, innerChanneLayouts[i]);
279 format->SetMeta(meta);
280 Format::FormatDataMap formatMap = format->GetFormatMap();
281 auto iter = formatMap.find(Tag::AUDIO_CHANNEL_LAYOUT);
282 ASSERT_NE(iter, formatMap.end());
283 EXPECT_EQ(iter->second.type, FORMAT_TYPE_INT64);
284 EXPECT_EQ(iter->second.val.int64Val, static_cast<int64_t>(innerChanneLayouts[i]));
285 }
286 }
287
288 /**
289 * @tc.name: channel_layout_006
290 * @tc.desc:
291 * 1. set values to meta;
292 * 2. meta trans by parcel;
293 * 3. get formatMap;
294 * @tc.type: FUNC
295 * @tc.require: issueI5OWXY issueI5OXCD
296 */
297 HWTEST_F(AVFormatChannelayoutUnitTest, channel_layout_006, TestSize.Level1)
298 {
299 int32_t size = nativeChanneLayouts.size();
300 for (int32_t i = 0; i < size; ++i) {
301 MessageParcel parcel;
302 std::shared_ptr<Meta> meta = std::make_shared<Meta>();
303 std::shared_ptr<Format> format = std::make_shared<Format>();
304 meta->SetData(Tag::AUDIO_CHANNEL_LAYOUT, innerChanneLayouts[i]);
305 format->SetMeta(meta);
306 meta = format->GetMeta();
307 ASSERT_TRUE(meta->ToParcel(parcel));
308 ASSERT_TRUE(meta->FromParcel(parcel));
309
310 format = std::make_shared<Format>();
311 format->SetMeta(std::move(meta));
312 Format::FormatDataMap formatMap = format->GetFormatMap();
313 auto iter = formatMap.find(Tag::AUDIO_CHANNEL_LAYOUT);
314 ASSERT_NE(iter, formatMap.end());
315 EXPECT_EQ(iter->second.type, FORMAT_TYPE_INT64);
316 EXPECT_EQ(iter->second.val.int64Val, static_cast<int64_t>(innerChanneLayouts[i]));
317 }
318 }
319
320 /**
321 * @tc.name: channel_layout_007
322 * @tc.desc:
323 * 1. set values to meta;
324 * 2. get format value type;
325 * @tc.type: FUNC
326 * @tc.require: issueI5OWXY issueI5OXCD
327 */
328 HWTEST_F(AVFormatChannelayoutUnitTest, channel_layout_007, TestSize.Level1)
329 {
330 int32_t size = nativeChanneLayouts.size();
331 for (int32_t i = 0; i < size; ++i) {
332 MessageParcel parcel;
333 std::shared_ptr<Meta> meta = std::make_shared<Meta>();
334 std::shared_ptr<Format> format = std::make_shared<Format>();
335 meta->SetData(Tag::AUDIO_CHANNEL_LAYOUT, innerChanneLayouts[i]);
336 format->SetMeta(std::move(meta));
337 EXPECT_EQ(format->GetValueType(Tag::AUDIO_CHANNEL_LAYOUT), FORMAT_TYPE_INT64);
338 }
339 }
340
341 /**
342 * @tc.name: channel_layout_008
343 * @tc.desc:
344 * 1. set values to meta;
345 * 2. meta trans by parcel;
346 * 3. get format value type;
347 * @tc.type: FUNC
348 * @tc.require: issueI5OWXY issueI5OXCD
349 */
350 HWTEST_F(AVFormatChannelayoutUnitTest, channel_layout_008, TestSize.Level1)
351 {
352 int32_t size = nativeChanneLayouts.size();
353 for (int32_t i = 0; i < size; ++i) {
354 MessageParcel parcel;
355 std::shared_ptr<Meta> meta = std::make_shared<Meta>();
356 std::shared_ptr<Format> format = std::make_shared<Format>();
357 meta->SetData(Tag::AUDIO_CHANNEL_LAYOUT, innerChanneLayouts[i]);
358 format->SetMeta(meta);
359 meta = format->GetMeta();
360 ASSERT_TRUE(meta->ToParcel(parcel));
361 ASSERT_TRUE(meta->FromParcel(parcel));
362
363 format = std::make_shared<Format>();
364 format->SetMeta(std::move(meta));
365 EXPECT_EQ(format->GetValueType(Tag::AUDIO_CHANNEL_LAYOUT), FORMAT_TYPE_INT64);
366 }
367 }
368
369 #endif
370 } // namespace Plugins
371 } // namespace Media
372 } // namespace OHOS
373