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
17 #include <string>
18 #include "securec.h"
19 #include "gtest/gtest.h"
20 #include "audio_info.h"
21 #include "native_avcodec_base.h"
22 #include "native_avformat.h"
23
24 using namespace std;
25 using namespace testing::ext;
26
27 namespace {
28 class ActsCodecFormatNdkTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 void SetUp() override;
33 void TearDown() override;
34
35 bool CheckDecDesc(map<string, std::pair<bool, int>> InDesc, OH_AVFormat* OutDesc);
36 bool SetFormat(struct OH_AVFormat *format, map<string, std::pair<bool, int>> mediaDescription);
37 };
38
SetUpTestCase()39 void ActsCodecFormatNdkTest::SetUpTestCase() {}
TearDownTestCase()40 void ActsCodecFormatNdkTest::TearDownTestCase() {}
SetUp()41 void ActsCodecFormatNdkTest::SetUp() {}
TearDown()42 void ActsCodecFormatNdkTest::TearDown() {}
43
CheckDecDesc(map<string, std::pair<bool, int>> InDesc, OH_AVFormat* OutDesc)44 bool ActsCodecFormatNdkTest::CheckDecDesc(map<string, std::pair<bool, int>> InDesc, OH_AVFormat* OutDesc)
45 {
46 for (const auto& t: InDesc) {
47 int32_t out_int32 = 0;
48 int64_t out_int64 = 0;
49 bool res = true;
50 if (t.second.first) {
51 res = OH_AVFormat_GetLongValue(OutDesc, t.first.c_str(), &out_int64);
52 out_int32 = out_int64;
53 } else {
54 res = OH_AVFormat_GetIntValue(OutDesc, t.first.c_str(), &out_int32);
55 }
56 cout << "key: " << t.first << "; out: " << out_int32 << endl;
57 if (!res) {
58 cout << "OH_AVFormat_Get Value Fail. key:" << t.first << endl;
59 return false;
60 }
61 if (out_int32 != t.second.second) {
62 cout << "OH_AVFormat_Get Value error. key: " << t.first
63 << "; expect: " << t.second.second
64 << ", actual: " << out_int32 << endl;
65 return false;
66 }
67 }
68 return true;
69 }
70
SetFormat(struct OH_AVFormat *format, map<string, std::pair<bool, int>> mediaDesc)71 bool ActsCodecFormatNdkTest::SetFormat(struct OH_AVFormat *format, map<string, std::pair<bool, int>> mediaDesc)
72 {
73 const char *key;
74 for (const auto& t: mediaDesc) {
75 key = t.first.c_str();
76 bool ret = true;
77 if (t.second.first) {
78 ret = OH_AVFormat_SetLongValue(format, key, static_cast<int64_t>(t.second.second));
79 } else {
80 ret = OH_AVFormat_SetIntValue(format, key, static_cast<int32_t>(t.second.second));
81 }
82 if (!ret) {
83 cout << "OH_AV_Format Put Value Fail. format key: " << t.first
84 << ", value: "<< t.second.second << endl;
85 return false;
86 }
87 }
88 return true;
89 }
90 }
91
92 /**
93 * @tc.number : SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0100
94 * @tc.name : test codec format set key
95 * @tc.desc : Basic function test
96 */
HWTEST_F(ActsCodecFormatNdkTest, SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0100, TestSize.Level1)97 HWTEST_F(ActsCodecFormatNdkTest, SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0100, TestSize.Level1)
98 {
99 OH_AVFormat *codecFormatIn = OH_AVFormat_Create();
100 ASSERT_NE(nullptr, codecFormatIn);
101 OH_AVFormat *codecFormatOut = OH_AVFormat_Create();
102 ASSERT_NE(nullptr, codecFormatOut);
103 map<string, std::pair<bool, int>> CodecParam = {
104 {OH_ED_KEY_TIME_STAMP, {true, 200}}, // set timestamp 200ms
105 {OH_ED_KEY_EOS, {false, 1}}, // set end of stream
106 {OH_MD_KEY_TRACK_TYPE, {false, 1}}, // set track type video
107 {OH_MD_KEY_DURATION, {true, 200}}, // set key duration 200ms
108 {OH_MD_KEY_BITRATE, {true, 48000}}, // set key bitrate 48000
109 {OH_MD_KEY_MAX_INPUT_SIZE, {false, 2000}}, // set key max input size 2000
110 {OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, {false, 0}}, // set videoencoder bitrate mode CBR
111 {OH_MD_KEY_PROFILE, {false, 1}}, // set encode profile AVC_PROFILE_BASELINE
112 {OH_MD_KEY_I_FRAME_INTERVAL, {false, 1}}, // set key i frame 1ms
113 {OH_MD_KEY_ROTATION, {false, 90}}, // set key rotation 0 degree
114 };
115 ASSERT_EQ(true, SetFormat(codecFormatIn, CodecParam));
116 OH_AVFormat_Copy(codecFormatOut, codecFormatIn);
117 ASSERT_EQ(true, CheckDecDesc(CodecParam, codecFormatOut));
118 ASSERT_NE(nullptr, OH_AVFormat_DumpInfo(codecFormatOut));
119 cout << OH_AVFormat_DumpInfo(codecFormatIn) << endl;
120 OH_AVFormat_Destroy(codecFormatIn);
121 codecFormatIn = nullptr;
122 OH_AVFormat_Destroy(codecFormatOut);
123 codecFormatIn = nullptr;
124 }
125
126 /**
127 * @tc.number : SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0200
128 * @tc.name : test codec format set and get value
129 * @tc.desc : Basic function test
130 */
HWTEST_F(ActsCodecFormatNdkTest, SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0200, TestSize.Level1)131 HWTEST_F(ActsCodecFormatNdkTest, SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0200, TestSize.Level1)
132 {
133 const char *intKey = "int value key";
134 const char *longKey = "long value key";
135 const char *floatKey = "float value key";
136 const char *doubleKey = "double value key";
137 const char *stringKey = "string value key";
138 const char *stringValue = "string_value";
139 int32_t intValue = 1;
140 int64_t longValue = 1;
141 float floatValue = 1.0;
142 double doubleValue = 1.0;
143
144 OH_AVFormat *codecFormatIn = OH_AVFormat_Create();
145 ASSERT_NE(nullptr, codecFormatIn);
146 OH_AVFormat *codecFormatOut = OH_AVFormat_Create();
147 ASSERT_NE(nullptr, codecFormatOut);
148 OH_AVFormat_SetIntValue(codecFormatIn, intKey, intValue);
149 OH_AVFormat_SetLongValue(codecFormatIn, longKey, longValue);
150 OH_AVFormat_SetFloatValue(codecFormatIn, floatKey, floatValue);
151 OH_AVFormat_SetDoubleValue(codecFormatIn, doubleKey, doubleValue);
152 OH_AVFormat_SetStringValue(codecFormatIn, stringKey, stringValue);
153
154 OH_AVFormat_Copy(codecFormatOut, codecFormatIn);
155 int32_t intValueOut;
156 OH_AVFormat_GetIntValue(codecFormatOut, intKey, &intValueOut);
157 ASSERT_EQ(intValueOut, intValue);
158 int64_t longValueOut;
159 OH_AVFormat_GetLongValue(codecFormatOut, longKey, &longValueOut);
160 ASSERT_EQ(longValueOut, longValue);
161 float floatValueOut;
162 OH_AVFormat_GetFloatValue(codecFormatOut, floatKey, &floatValueOut);
163 ASSERT_EQ(floatValueOut, floatValue);
164 double doubleValueOut;
165 OH_AVFormat_GetDoubleValue(codecFormatOut, doubleKey, &doubleValueOut);
166 ASSERT_EQ(doubleValueOut, doubleValue);
167 const char *stringValueOut;
168 OH_AVFormat_GetStringValue(codecFormatOut, stringKey, &stringValueOut);
169 ASSERT_EQ(*stringValueOut, *stringValue);
170
171 OH_AVFormat_Destroy(codecFormatIn);
172 codecFormatIn = nullptr;
173 OH_AVFormat_Destroy(codecFormatOut);
174 codecFormatOut = nullptr;
175 }
176
177 /**
178 * @tc.number : SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0300
179 * @tc.name : test codec format set and get buffer
180 * @tc.desc : Basic function test
181 */
HWTEST_F(ActsCodecFormatNdkTest, SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0300, TestSize.Level1)182 HWTEST_F(ActsCodecFormatNdkTest, SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0300, TestSize.Level1)
183 {
184 const char *bufferKey = "buffer value key";
185 OH_AVFormat *codecFormatIn = OH_AVFormat_Create();
186 ASSERT_NE(nullptr, codecFormatIn);
187 OH_AVFormat *codecFormatOut = OH_AVFormat_Create();
188 ASSERT_NE(nullptr, codecFormatOut);
189 int32_t buffernum = 10;
190 size_t sizeIn = buffernum * sizeof(uint8_t);
191 uint8_t *buffer = reinterpret_cast<uint8_t *>(malloc(sizeIn));
192 (void)memset_s(buffer, sizeIn, 1, sizeIn);
193 OH_AVFormat_SetBuffer(codecFormatIn, bufferKey, buffer, sizeIn);
194
195 OH_AVFormat_Copy(codecFormatOut, codecFormatIn);
196 uint8_t *addrout;
197 size_t sizeOut;
198 OH_AVFormat_GetBuffer(codecFormatOut, bufferKey, &addrout, &sizeOut);
199 ASSERT_EQ(sizeIn, sizeOut);
200 for (int32_t i = 0; i < buffernum; i++) {
201 ASSERT_EQ(buffer[i], addrout[i]);
202 }
203
204 OH_AVFormat_Destroy(codecFormatIn);
205 codecFormatIn = nullptr;
206 OH_AVFormat_Destroy(codecFormatOut);
207 codecFormatOut = nullptr;
208 }